image_loader.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. im:gammaCorrection(gamma / gamma_lcd)
  57. end
  58. -- FIXME: How to detect that a image has an alpha channel?
  59. if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
  60. -- split alpha channel
  61. im = im:toTensor('float', 'RGBA', 'DHW')
  62. local sum_alpha = (im[4] - 1.0):sum()
  63. if sum_alpha < 0 then
  64. alpha = im[4]:reshape(1, im:size(2), im:size(3))
  65. -- drop full transparent background
  66. local mask = torch.le(alpha, 0.0)
  67. im[1][mask] = background_color
  68. im[2][mask] = background_color
  69. im[3][mask] = background_color
  70. end
  71. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  72. new_im[1]:copy(im[1])
  73. new_im[2]:copy(im[2])
  74. new_im[3]:copy(im[3])
  75. im = new_im
  76. else
  77. im = im:toTensor('float', 'RGB', 'DHW')
  78. end
  79. return {im, alpha, blob}
  80. end
  81. local state, ret = pcall(load_image)
  82. if state then
  83. return ret[1], ret[2], ret[3]
  84. else
  85. return nil, nil, nil
  86. end
  87. end
  88. function image_loader.decode_byte(blob)
  89. local im, alpha
  90. im, alpha, blob = image_loader.decode_float(blob)
  91. if im then
  92. im = iproc.float2byte(im)
  93. -- hmm, alpha does not convert here
  94. return im, alpha, blob
  95. else
  96. return nil, nil, nil
  97. end
  98. end
  99. function image_loader.load_float(file)
  100. local fp = io.open(file, "rb")
  101. if not fp then
  102. error(file .. ": failed to load image")
  103. end
  104. local buff = fp:read("*a")
  105. fp:close()
  106. return image_loader.decode_float(buff)
  107. end
  108. function image_loader.load_byte(file)
  109. local fp = io.open(file, "rb")
  110. if not fp then
  111. error(file .. ": failed to load image")
  112. end
  113. local buff = fp:read("*a")
  114. fp:close()
  115. return image_loader.decode_byte(buff)
  116. end
  117. local function test()
  118. torch.setdefaulttensortype("torch.FloatTensor")
  119. local a = image_loader.load_float("../images/lena.png")
  120. local blob = image_loader.encode_png(a)
  121. local b = image_loader.decode_float(blob)
  122. assert((b - a):abs():sum() == 0)
  123. a = image_loader.load_byte("../images/lena.png")
  124. blob = image_loader.encode_png(a)
  125. b = image_loader.decode_byte(blob)
  126. assert((b:float() - a:float()):abs():sum() == 0)
  127. end
  128. --test()
  129. return image_loader