image_loader.lua 3.8 KB

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