image_loader.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, depth)
  10. depth = depth or 8
  11. rgb = iproc.byte2float(rgb)
  12. if depth < 16 then
  13. rgb = rgb:clone():add(clip_eps8)
  14. rgb[torch.lt(rgb, 0.0)] = 0.0
  15. rgb[torch.gt(rgb, 1.0)] = 1.0
  16. rgb = rgb:mul(255):long():float():div(255)
  17. else
  18. rgb = rgb:clone():add(clip_eps16)
  19. rgb[torch.lt(rgb, 0.0)] = 0.0
  20. rgb[torch.gt(rgb, 1.0)] = 1.0
  21. rgb = rgb:mul(65535):long():float():div(65535)
  22. end
  23. local im
  24. if rgb:size(1) == 4 then -- RGBA
  25. im = gm.Image(rgb, "RGBA", "DHW")
  26. elseif rgb:size(1) == 3 then -- RGB
  27. im = gm.Image(rgb, "RGB", "DHW")
  28. elseif rgb:size(1) == 1 then -- Y
  29. im = gm.Image(rgb, "I", "DHW")
  30. -- im:colorspace("GRAY") -- it does not work
  31. end
  32. return im:depth(depth):format("PNG"):toString(9)
  33. end
  34. function image_loader.save_png(filename, rgb, depth)
  35. depth = depth or 8
  36. local blob = image_loader.encode_png(rgb, depth)
  37. local fp = io.open(filename, "wb")
  38. if not fp then
  39. error("IO error: " .. filename)
  40. end
  41. fp:write(blob)
  42. fp:close()
  43. return true
  44. end
  45. function image_loader.decode_float(blob)
  46. local load_image = function()
  47. local im = gm.Image()
  48. local alpha = nil
  49. local gamma_lcd = 0.454545
  50. im:fromBlob(blob, #blob)
  51. if im:colorspace() == "CMYK" then
  52. im:colorspace("RGB")
  53. end
  54. local gamma = math.floor(im:gamma() * 1000000) / 1000000
  55. if gamma ~= 0 and gamma ~= gamma_lcd then
  56. local cg = gamma / gamma_lcd
  57. im:gammaCorrection(cg, "Red")
  58. im:gammaCorrection(cg, "Blue")
  59. im:gammaCorrection(cg, "Green")
  60. end
  61. -- FIXME: How to detect that a image has an alpha channel?
  62. if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
  63. -- split alpha channel
  64. im = im:toTensor('float', 'RGBA', 'DHW')
  65. local sum_alpha = (im[4] - 1.0):sum()
  66. if sum_alpha < 0 then
  67. alpha = im[4]:reshape(1, im:size(2), im:size(3))
  68. -- drop full transparent background
  69. local mask = torch.le(alpha, 0.0)
  70. im[1][mask] = background_color
  71. im[2][mask] = background_color
  72. im[3][mask] = background_color
  73. end
  74. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  75. new_im[1]:copy(im[1])
  76. new_im[2]:copy(im[2])
  77. new_im[3]:copy(im[3])
  78. im = new_im
  79. else
  80. im = im:toTensor('float', 'RGB', 'DHW')
  81. end
  82. return {im, alpha, blob}
  83. end
  84. local state, ret = pcall(load_image)
  85. if state then
  86. return ret[1], ret[2], ret[3]
  87. else
  88. return nil, nil, nil
  89. end
  90. end
  91. function image_loader.decode_byte(blob)
  92. local im, alpha
  93. im, alpha, blob = image_loader.decode_float(blob)
  94. if im then
  95. im = iproc.float2byte(im)
  96. -- hmm, alpha does not convert here
  97. return im, alpha, blob
  98. else
  99. return nil, nil, nil
  100. end
  101. end
  102. function image_loader.load_float(file)
  103. local fp = io.open(file, "rb")
  104. if not fp then
  105. error(file .. ": failed to load image")
  106. end
  107. local buff = fp:read("*a")
  108. fp:close()
  109. return image_loader.decode_float(buff)
  110. end
  111. function image_loader.load_byte(file)
  112. local fp = io.open(file, "rb")
  113. if not fp then
  114. error(file .. ": failed to load image")
  115. end
  116. local buff = fp:read("*a")
  117. fp:close()
  118. return image_loader.decode_byte(buff)
  119. end
  120. local function test()
  121. torch.setdefaulttensortype("torch.FloatTensor")
  122. local a = image_loader.load_float("../images/lena.png")
  123. local blob = image_loader.encode_png(a)
  124. local b = image_loader.decode_float(blob)
  125. assert((b - a):abs():sum() == 0)
  126. a = image_loader.load_byte("../images/lena.png")
  127. blob = image_loader.encode_png(a)
  128. b = image_loader.decode_byte(blob)
  129. assert((b:float() - a:float()):abs():sum() == 0)
  130. end
  131. --test()
  132. return image_loader