image_loader.lua 4.4 KB

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