image_loader.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. local gm = require 'graphicsmagick'
  2. local ffi = require 'ffi'
  3. require 'pl'
  4. local image_loader = {}
  5. local clip_eta8 = (1.0 / 255.0) * 0.5 - (1.0e-7 * (1.0 / 255.0) * 0.5)
  6. local clip_eta16 = (1.0 / 65535.0) * 0.5 - (1.0e-7 * (1.0 / 65535.0) * 0.5)
  7. function image_loader.decode_float(blob)
  8. local im, alpha = image_loader.decode_byte(blob)
  9. if im then
  10. im = im:float():div(255)
  11. end
  12. return im, alpha, blob
  13. end
  14. function image_loader.encode_png(rgb, alpha, depth)
  15. depth = depth or 8
  16. if rgb:type() == "torch.ByteTensor" then
  17. rgb = rgb:float():div(255)
  18. end
  19. if alpha then
  20. if not (alpha:size(2) == rgb:size(2) and alpha:size(3) == rgb:size(3)) then
  21. alpha = gm.Image(alpha, "I", "DHW"):size(rgb:size(3), rgb:size(2), "SincFast"):toTensor("float", "I", "DHW")
  22. end
  23. local rgba = torch.Tensor(4, rgb:size(2), rgb:size(3))
  24. rgba[1]:copy(rgb[1])
  25. rgba[2]:copy(rgb[2])
  26. rgba[3]:copy(rgb[3])
  27. rgba[4]:copy(alpha)
  28. if depth < 16 then
  29. rgba:add(clip_eta8)
  30. rgba[torch.lt(rgba, 0.0)] = 0.0
  31. rgba[torch.gt(rgba, 1.0)] = 1.0
  32. else
  33. rgba:add(clip_eta16)
  34. rgba[torch.lt(rgba, 0.0)] = 0.0
  35. rgba[torch.gt(rgba, 1.0)] = 1.0
  36. end
  37. local im = gm.Image():fromTensor(rgba, "RGBA", "DHW")
  38. return im:depth(depth):format("PNG"):toBlob(9)
  39. else
  40. if depth < 16 then
  41. rgb = rgb:clone():add(clip_eta8)
  42. rgb[torch.lt(rgb, 0.0)] = 0.0
  43. rgb[torch.gt(rgb, 1.0)] = 1.0
  44. else
  45. rgb = rgb:clone():add(clip_eta16)
  46. rgb[torch.lt(rgb, 0.0)] = 0.0
  47. rgb[torch.gt(rgb, 1.0)] = 1.0
  48. end
  49. local im = gm.Image(rgb, "RGB", "DHW")
  50. return im:depth(depth):format("PNG"):toBlob(9)
  51. end
  52. end
  53. function image_loader.save_png(filename, rgb, alpha, depth)
  54. depth = depth or 8
  55. local blob, len = image_loader.encode_png(rgb, alpha, depth)
  56. local fp = io.open(filename, "wb")
  57. if not fp then
  58. error("IO error: " .. filename)
  59. end
  60. fp:write(ffi.string(blob, len))
  61. fp:close()
  62. return true
  63. end
  64. function image_loader.decode_byte(blob)
  65. local load_image = function()
  66. local im = gm.Image()
  67. local alpha = nil
  68. local gamma_lcd = 0.454545
  69. im:fromBlob(blob, #blob)
  70. if im:colorspace() == "CMYK" then
  71. im:colorspace("RGB")
  72. end
  73. local gamma = math.floor(im:gamma() * 1000000) / 1000000
  74. if gamma ~= 0 and gamma ~= gamma_lcd then
  75. im:gammaCorrection(gamma / gamma_lcd)
  76. end
  77. -- FIXME: How to detect that a image has an alpha channel?
  78. if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
  79. -- split alpha channel
  80. im = im:toTensor('float', 'RGBA', 'DHW')
  81. local sum_alpha = (im[4] - 1.0):sum()
  82. if sum_alpha < 0 then
  83. alpha = im[4]:reshape(1, im:size(2), im:size(3))
  84. end
  85. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  86. new_im[1]:copy(im[1])
  87. new_im[2]:copy(im[2])
  88. new_im[3]:copy(im[3])
  89. im = new_im:mul(255):byte()
  90. else
  91. im = im:toTensor('byte', 'RGB', 'DHW')
  92. end
  93. return {im, alpha, blob}
  94. end
  95. local state, ret = pcall(load_image)
  96. if state then
  97. return ret[1], ret[2], ret[3]
  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. require 'image'
  122. local img
  123. img = image_loader.load_float("./a.jpg")
  124. if img then
  125. print(img:min())
  126. print(img:max())
  127. image.display(img)
  128. end
  129. img = image_loader.load_float("./b.png")
  130. if img then
  131. image.display(img)
  132. end
  133. end
  134. --test()
  135. return image_loader