image_loader.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if not fp then
  38. error("IO error: " .. filename)
  39. end
  40. fp:write(ffi.string(blob, len))
  41. fp:close()
  42. return true
  43. end
  44. function image_loader.decode_byte(blob)
  45. local load_image = function()
  46. local im = gm.Image()
  47. local alpha = nil
  48. local gamma_lcd = 0.454545
  49. im:fromBlob(blob, #blob)
  50. if im:colorspace() == "CMYK" then
  51. im:colorspace("RGB")
  52. end
  53. local gamma = math.floor(im:gamma() * 1000000) / 1000000
  54. if gamma ~= 0 and gamma ~= gamma_lcd then
  55. im:gammaCorrection(gamma / gamma_lcd)
  56. end
  57. -- FIXME: How to detect that a image has an alpha channel?
  58. if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
  59. -- split alpha channel
  60. im = im:toTensor('float', 'RGBA', 'DHW')
  61. local sum_alpha = (im[4] - 1.0):sum()
  62. if sum_alpha < 0 then
  63. alpha = im[4]:reshape(1, im:size(2), im:size(3))
  64. end
  65. local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
  66. new_im[1]:copy(im[1])
  67. new_im[2]:copy(im[2])
  68. new_im[3]:copy(im[3])
  69. im = new_im:mul(255):byte()
  70. else
  71. im = im:toTensor('byte', 'RGB', 'DHW')
  72. end
  73. return {im, alpha}
  74. end
  75. load_image()
  76. local state, ret = pcall(load_image)
  77. if state then
  78. return ret[1], ret[2]
  79. else
  80. return nil
  81. end
  82. end
  83. function image_loader.load_float(file)
  84. local fp = io.open(file, "rb")
  85. if not fp then
  86. error(file .. ": failed to load image")
  87. end
  88. local buff = fp:read("*a")
  89. fp:close()
  90. return image_loader.decode_float(buff)
  91. end
  92. function image_loader.load_byte(file)
  93. local fp = io.open(file, "rb")
  94. if not fp then
  95. error(file .. ": failed to load image")
  96. end
  97. local buff = fp:read("*a")
  98. fp:close()
  99. return image_loader.decode_byte(buff)
  100. end
  101. local function test()
  102. require 'image'
  103. local img
  104. img = image_loader.load_float("./a.jpg")
  105. if img then
  106. print(img:min())
  107. print(img:max())
  108. image.display(img)
  109. end
  110. img = image_loader.load_float("./b.png")
  111. if img then
  112. image.display(img)
  113. end
  114. end
  115. --test()
  116. return image_loader