image_loader.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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[torch.lt(rgb, 0.0)] = 0.0
  23. rgb[torch.gt(rgb, 1.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[torch.lt(rgb, 0.0)] = 0.0
  32. rgb[torch.gt(rgb, 1.0)] = 1.0
  33. rgb = rgb:mul(65535):floor():div(65535)
  34. end
  35. local im
  36. if rgb:size(1) == 4 then -- RGBA
  37. im = gm.Image(rgb, "RGBA", "DHW")
  38. elseif rgb:size(1) == 3 then -- RGB
  39. im = gm.Image(rgb, "RGB", "DHW")
  40. elseif rgb:size(1) == 1 then -- Y
  41. im = gm.Image(rgb, "I", "DHW")
  42. -- im:colorspace("GRAY") -- it does not work
  43. end
  44. if options.gamma then
  45. im:gamma(options.gamma)
  46. end
  47. return im:depth(options.depth):format("PNG"):toString(9)
  48. end
  49. function image_loader.save_png(filename, rgb, options)
  50. local blob = image_loader.encode_png(rgb, options)
  51. local fp = io.open(filename, "wb")
  52. if not fp then
  53. error("IO error: " .. filename)
  54. end
  55. fp:write(blob)
  56. fp:close()
  57. return true
  58. end
  59. function image_loader.decode_float(blob)
  60. local load_image = function()
  61. local meta = {}
  62. local im = gm.Image()
  63. local gamma_lcd = 0.454545
  64. im:fromBlob(blob, #blob)
  65. if im:colorspace() == "CMYK" then
  66. im:colorspace("RGB")
  67. end
  68. if gamma ~= 0 and math.floor(im:gamma() * 1000000) / 1000000 ~= gamma_lcd then
  69. meta.gamma = im:gamma()
  70. end
  71. local image_type = im:type()
  72. if image_type == "TrueColorMatte" or image_type == "GrayscaleMatte" then
  73. -- split alpha channel
  74. im = im:toTensor('float', 'RGBA', 'DHW')
  75. meta.alpha = im[4]:reshape(1, im:size(2), im:size(3))
  76. -- drop full transparent background
  77. local mask = torch.le(meta.alpha, 0.0)
  78. im[1][mask] = background_color
  79. im[2][mask] = background_color
  80. im[3][mask] = background_color
  81. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  82. new_im[1]:copy(im[1])
  83. new_im[2]:copy(im[2])
  84. new_im[3]:copy(im[3])
  85. im = new_im
  86. else
  87. im = im:toTensor('float', 'RGB', 'DHW')
  88. end
  89. meta.blob = blob
  90. return {im, meta}
  91. end
  92. local state, ret = pcall(load_image)
  93. if state then
  94. return ret[1], ret[2]
  95. else
  96. return nil, nil
  97. end
  98. end
  99. function image_loader.decode_byte(blob)
  100. local im, meta
  101. im, meta = image_loader.decode_float(blob)
  102. if im then
  103. im = iproc.float2byte(im)
  104. -- hmm, alpha does not convert here
  105. return im, meta
  106. else
  107. return nil, nil
  108. end
  109. end
  110. function image_loader.load_float(file)
  111. local fp = io.open(file, "rb")
  112. if not fp then
  113. error(file .. ": failed to load image")
  114. end
  115. local buff = fp:read("*a")
  116. fp:close()
  117. return image_loader.decode_float(buff)
  118. end
  119. function image_loader.load_byte(file)
  120. local fp = io.open(file, "rb")
  121. if not fp then
  122. error(file .. ": failed to load image")
  123. end
  124. local buff = fp:read("*a")
  125. fp:close()
  126. return image_loader.decode_byte(buff)
  127. end
  128. local function test()
  129. torch.setdefaulttensortype("torch.FloatTensor")
  130. local a = image_loader.load_float("../images/lena.png")
  131. local blob = image_loader.encode_png(a)
  132. local b = image_loader.decode_float(blob)
  133. assert((b - a):abs():sum() == 0)
  134. a = image_loader.load_byte("../images/lena.png")
  135. blob = image_loader.encode_png(a)
  136. b = image_loader.decode_byte(blob)
  137. assert((b:float() - a:float()):abs():sum() == 0)
  138. end
  139. --test()
  140. return image_loader