image_loader.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. local gm = require 'graphicsmagick'
  2. local ffi = require 'ffi'
  3. local iproc = require 'iproc'
  4. require 'pl'
  5. local image_loader = {}
  6. local clip_eps8 = (1.0 / 255.0) * 0.5 - (1.0e-7 * (1.0 / 255.0) * 0.5)
  7. local clip_eps16 = (1.0 / 65535.0) * 0.5 - (1.0e-7 * (1.0 / 65535.0) * 0.5)
  8. local background_color = 0.5
  9. function image_loader.encode_png(rgb, options)
  10. options = options or {}
  11. options.depth = options.depth or 8
  12. if options.inplace == nil then
  13. options.inplace = false
  14. end
  15. rgb = iproc.byte2float(rgb)
  16. if options.depth < 16 then
  17. if options.inplace then
  18. rgb:add(clip_eps8)
  19. else
  20. rgb = rgb:clone():add(clip_eps8)
  21. end
  22. rgb:clamp(0.0, 1.0)
  23. rgb = rgb:mul(255):floor():div(255)
  24. else
  25. if options.inplace then
  26. rgb:add(clip_eps16)
  27. else
  28. rgb = rgb:clone():add(clip_eps16)
  29. end
  30. rgb:clamp(0.0, 1.0)
  31. rgb = rgb:mul(65535):floor():div(65535)
  32. end
  33. local im
  34. if rgb:size(1) == 4 then -- RGBA
  35. im = gm.Image(rgb, "RGBA", "DHW")
  36. if options.grayscale then
  37. im:type("GrayscaleMatte")
  38. end
  39. elseif rgb:size(1) == 3 then -- RGB
  40. im = gm.Image(rgb, "RGB", "DHW")
  41. if options.grayscale then
  42. im:type("Grayscale")
  43. end
  44. elseif rgb:size(1) == 1 then -- Y
  45. im = gm.Image(rgb, "I", "DHW")
  46. im:type("Grayscale")
  47. end
  48. if options.gamma then
  49. im:gamma(options.gamma)
  50. end
  51. return im:depth(options.depth):format("PNG"):toString()
  52. end
  53. function image_loader.save_png(filename, rgb, options)
  54. local blob = image_loader.encode_png(rgb, options)
  55. local fp = io.open(filename, "wb")
  56. if not fp then
  57. error("IO error: " .. filename)
  58. end
  59. fp:write(blob)
  60. fp:close()
  61. return true
  62. end
  63. function image_loader.decode_float(blob)
  64. local load_image = function()
  65. local meta = {}
  66. local im = gm.Image()
  67. local gamma_lcd = 0.454545
  68. im:fromBlob(blob, #blob)
  69. if im:colorspace() == "CMYK" then
  70. im:colorspace("RGB")
  71. end
  72. if gamma ~= 0 and math.floor(im:gamma() * 1000000) / 1000000 ~= gamma_lcd then
  73. meta.gamma = im:gamma()
  74. end
  75. local image_type = im:type()
  76. if image_type == "Grayscale" or image_type == "GrayscaleMatte" then
  77. meta.grayscale = true
  78. end
  79. if image_type == "TrueColorMatte" or image_type == "GrayscaleMatte" then
  80. -- split alpha channel
  81. im = im:toTensor('float', 'RGBA', 'DHW')
  82. meta.alpha = im[4]:reshape(1, im:size(2), im:size(3))
  83. -- drop full transparent background
  84. local mask = torch.le(meta.alpha, 0.0)
  85. im[1][mask] = background_color
  86. im[2][mask] = background_color
  87. im[3][mask] = background_color
  88. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  89. new_im[1]:copy(im[1])
  90. new_im[2]:copy(im[2])
  91. new_im[3]:copy(im[3])
  92. im = new_im
  93. else
  94. im = im:toTensor('float', 'RGB', 'DHW')
  95. end
  96. meta.blob = blob
  97. return {im, meta}
  98. end
  99. local state, ret = pcall(load_image)
  100. if state then
  101. return ret[1], ret[2]
  102. else
  103. return nil, nil
  104. end
  105. end
  106. function image_loader.decode_byte(blob)
  107. local im, meta
  108. im, meta = image_loader.decode_float(blob)
  109. if im then
  110. im = iproc.float2byte(im)
  111. -- hmm, alpha does not convert here
  112. return im, meta
  113. else
  114. return nil, nil
  115. end
  116. end
  117. function image_loader.load_float(file)
  118. local fp = io.open(file, "rb")
  119. if not fp then
  120. error(file .. ": failed to load image")
  121. end
  122. local buff = fp:read("*a")
  123. fp:close()
  124. return image_loader.decode_float(buff)
  125. end
  126. function image_loader.load_byte(file)
  127. local fp = io.open(file, "rb")
  128. if not fp then
  129. error(file .. ": failed to load image")
  130. end
  131. local buff = fp:read("*a")
  132. fp:close()
  133. return image_loader.decode_byte(buff)
  134. end
  135. local function test()
  136. torch.setdefaulttensortype("torch.FloatTensor")
  137. local a = image_loader.load_float("../images/lena.png")
  138. local blob = image_loader.encode_png(a)
  139. local b = image_loader.decode_float(blob)
  140. assert((b - a):abs():sum() == 0)
  141. a = image_loader.load_byte("../images/lena.png")
  142. blob = image_loader.encode_png(a)
  143. b = image_loader.decode_byte(blob)
  144. assert((b:float() - a:float()):abs():sum() == 0)
  145. end
  146. --test()
  147. return image_loader