qoi_decoder_magick.sf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/ruby
  2. # Implementation of the QOI decoder (generating a PNG file).
  3. # See also:
  4. # https://qoiformat.org/
  5. # https://github.com/phoboslab/qoi
  6. require('Image::Magick')
  7. func qoi_decoder(bytes) {
  8. func decode32(a,b,c,d) {
  9. (a << 8*3) | (b << 8*2) | (c << 8*1) | (d << 8*0)
  10. }
  11. func invalid() {
  12. die "Not a QOIF image"
  13. }
  14. var index = 0
  15. [bytes[index..index+3]] == %b'qoif' || invalid()
  16. index += 4
  17. var width = decode32(bytes[index..index+3])
  18. index += 4
  19. var height = decode32(bytes[index..index+3])
  20. index += 4
  21. var channels = bytes[index++]
  22. var colorspace = bytes[index++]
  23. width>0 && height>0 || invalid()
  24. channels ~~ 1..4 || invalid()
  25. colorspace ~~ [0,1] || invalid()
  26. bytes.pop == 0x01 || invalid()
  27. 7.times { bytes.pop == 0x00 || invalid() }
  28. say [width, height, channels, colorspace]
  29. var img = %O<Image::Magick>.new(join('x', width, height))
  30. if (channels == 4) {
  31. img.ReadImage('canvas:transparent')
  32. img.Set(alpha => 'Transparent')
  33. }
  34. else {
  35. img.ReadImage('canvas:white')
  36. }
  37. var w = 0
  38. var run = 0
  39. var px = [0, 0, 0, 255]
  40. var colors = 64.of { [0,0,0,0] }
  41. loop {
  42. if (run > 0) {
  43. --run
  44. }
  45. else {
  46. var byte = (bytes[index++] \\ break)
  47. if (byte == 0b_11_11_11_10) { # OP RGB
  48. px[0] = bytes[index++]
  49. px[1] = bytes[index++]
  50. px[2] = bytes[index++]
  51. }
  52. elsif (byte == 0b_11_11_11_11) { # OP RGBA
  53. px[0] = bytes[index++]
  54. px[1] = bytes[index++]
  55. px[2] = bytes[index++]
  56. px[3] = bytes[index++]
  57. }
  58. elsif (byte >> 6 == 0b00) { # OP INDEX
  59. px = colors[byte].clone
  60. }
  61. elsif (byte >> 6 == 0b01) { # OP DIFF
  62. var dr = (byte & 0b00_11_00_00 >> 4)-2
  63. var dg = (byte & 0b00_00_11_00 >> 2)-2
  64. var db = (byte & 0b00_00_00_11 >> 0)-2
  65. px[0].addmod!(dr, 256)
  66. px[1].addmod!(dg, 256)
  67. px[2].addmod!(db, 256)
  68. }
  69. elsif (byte >> 6 == 0b10) { # OP LUMA
  70. var byte2 = bytes[index++]
  71. var dg = (byte & 0b00_111_111)-32
  72. var dr_dg = (byte2 >> 4)-8
  73. var db_dg = (byte2 & 0b0000_1111)-8
  74. var dr = (dr_dg+dg)
  75. var db = (db_dg+dg)
  76. px[0].addmod!(dr, 256)
  77. px[1].addmod!(dg, 256)
  78. px[2].addmod!(db, 256)
  79. }
  80. elsif (byte >> 6 == 0b11) { # OP RUN
  81. run = (byte & 0b00_111_111)
  82. }
  83. colors[(px[0]*3 + px[1]*5 + px[2]*7 + px[3]*11)%64] = px.clone
  84. }
  85. img.Set('pixel[' + join(',', w%width, idiv(w, width)) + ']', sprintf("#%02x%02x%02x%02x", px...))
  86. ++w
  87. }
  88. return img
  89. }
  90. ARGV || do {
  91. STDERR << "usage: #{File(__MAIN__).basename} [input.qoi] [output.png]\n"
  92. Sys.exit(2)
  93. }
  94. var in_file = File(ARGV[0])
  95. var out_file = (ARGV[1] \\ (in_file - /\.qoi\z/i + '.png'))
  96. var bytes = in_file.read(:raw).bytes
  97. var img = qoi_decoder(bytes)
  98. img.Write(out_file) && die "Error: couldn't save file!"