image_loader.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. local gm = require 'graphicsmagick'
  2. local ffi = require 'ffi'
  3. require 'pl'
  4. local image_loader = {}
  5. function image_loader.decode_float(blob)
  6. local im, alpha = image_loader.decode_byte(blob)
  7. if im then
  8. im = im:float():div(255)
  9. end
  10. return im, alpha
  11. end
  12. function image_loader.encode_png(rgb, alpha)
  13. if rgb:type() == "torch.ByteTensor" then
  14. rgb = rgb:float():div(255)
  15. end
  16. if alpha then
  17. if not (alpha:size(2) == rgb:size(2) and alpha:size(3) == rgb:size(3)) then
  18. alpha = gm.Image(alpha, "I", "DHW"):size(rgb:size(3), rgb:size(2), "SincFast"):toTensor("float", "I", "DHW")
  19. end
  20. local rgba = torch.Tensor(4, rgb:size(2), rgb:size(3))
  21. rgba[1]:copy(rgb[1])
  22. rgba[2]:copy(rgb[2])
  23. rgba[3]:copy(rgb[3])
  24. rgba[4]:copy(alpha)
  25. local im = gm.Image():fromTensor(rgba, "RGBA", "DHW")
  26. im:format("png")
  27. return im:toBlob(9)
  28. else
  29. local im = gm.Image(rgb, "RGB", "DHW")
  30. im:format("png")
  31. return im:toBlob(9)
  32. end
  33. end
  34. function image_loader.save_png(filename, rgb, alpha)
  35. local blob, len = image_loader.encode_png(rgb, alpha)
  36. local fp = io.open(filename, "wb")
  37. fp:write(ffi.string(blob, len))
  38. fp:close()
  39. return true
  40. end
  41. function image_loader.decode_byte(blob)
  42. local load_image = function()
  43. local im = gm.Image()
  44. local alpha = nil
  45. im:fromBlob(blob, #blob)
  46. if im:colorspace() == "CMYK" then
  47. im:colorspace("RGB")
  48. end
  49. -- FIXME: How to detect that a image has an alpha channel?
  50. if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
  51. -- split alpha channel
  52. im = im:toTensor('float', 'RGBA', 'DHW')
  53. local sum_alpha = (im[4] - 1.0):sum()
  54. if sum_alpha < 0 then
  55. alpha = im[4]:reshape(1, im:size(2), im:size(3))
  56. end
  57. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  58. new_im[1]:copy(im[1])
  59. new_im[2]:copy(im[2])
  60. new_im[3]:copy(im[3])
  61. im = new_im:mul(255):byte()
  62. else
  63. im = im:toTensor('byte', 'RGB', 'DHW')
  64. end
  65. return {im, alpha}
  66. end
  67. load_image()
  68. local state, ret = pcall(load_image)
  69. if state then
  70. return ret[1], ret[2]
  71. else
  72. return nil
  73. end
  74. end
  75. function image_loader.load_float(file)
  76. local fp = io.open(file, "rb")
  77. if not fp then
  78. error(file .. ": failed to load image")
  79. end
  80. local buff = fp:read("*a")
  81. fp:close()
  82. return image_loader.decode_float(buff)
  83. end
  84. function image_loader.load_byte(file)
  85. local fp = io.open(file, "rb")
  86. if not fp then
  87. error(file .. ": failed to load image")
  88. end
  89. local buff = fp:read("*a")
  90. fp:close()
  91. return image_loader.decode_byte(buff)
  92. end
  93. local function test()
  94. require 'image'
  95. local img
  96. img = image_loader.load_float("./a.jpg")
  97. if img then
  98. print(img:min())
  99. print(img:max())
  100. image.display(img)
  101. end
  102. img = image_loader.load_float("./b.png")
  103. if img then
  104. image.display(img)
  105. end
  106. end
  107. --test()
  108. return image_loader