123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #!/usr/bin/ruby
- # Implementation of the QOI decoder (generating a PNG file).
- # See also:
- # https://qoiformat.org/
- # https://github.com/phoboslab/qoi
- require('Image::Magick')
- func qoi_decoder(bytes) {
- func decode32(a,b,c,d) {
- (a << 8*3) | (b << 8*2) | (c << 8*1) | (d << 8*0)
- }
- func invalid() {
- die "Not a QOIF image"
- }
- var index = 0
- [bytes[index..index+3]] == %b'qoif' || invalid()
- index += 4
- var width = decode32(bytes[index..index+3])
- index += 4
- var height = decode32(bytes[index..index+3])
- index += 4
- var channels = bytes[index++]
- var colorspace = bytes[index++]
- width>0 && height>0 || invalid()
- channels ~~ 1..4 || invalid()
- colorspace ~~ [0,1] || invalid()
- bytes.pop == 0x01 || invalid()
- 7.times { bytes.pop == 0x00 || invalid() }
- say [width, height, channels, colorspace]
- var img = %O<Image::Magick>.new(join('x', width, height))
- if (channels == 4) {
- img.ReadImage('canvas:transparent')
- img.Set(alpha => 'Transparent')
- }
- else {
- img.ReadImage('canvas:white')
- }
- var w = 0
- var run = 0
- var px = [0, 0, 0, 255]
- var colors = 64.of { [0,0,0,0] }
- loop {
- if (run > 0) {
- --run
- }
- else {
- var byte = (bytes[index++] \\ break)
- if (byte == 0b_11_11_11_10) { # OP RGB
- px[0] = bytes[index++]
- px[1] = bytes[index++]
- px[2] = bytes[index++]
- }
- elsif (byte == 0b_11_11_11_11) { # OP RGBA
- px[0] = bytes[index++]
- px[1] = bytes[index++]
- px[2] = bytes[index++]
- px[3] = bytes[index++]
- }
- elsif (byte >> 6 == 0b00) { # OP INDEX
- px = colors[byte].clone
- }
- elsif (byte >> 6 == 0b01) { # OP DIFF
- var dr = (byte & 0b00_11_00_00 >> 4)-2
- var dg = (byte & 0b00_00_11_00 >> 2)-2
- var db = (byte & 0b00_00_00_11 >> 0)-2
- px[0].addmod!(dr, 256)
- px[1].addmod!(dg, 256)
- px[2].addmod!(db, 256)
- }
- elsif (byte >> 6 == 0b10) { # OP LUMA
- var byte2 = bytes[index++]
- var dg = (byte & 0b00_111_111)-32
- var dr_dg = (byte2 >> 4)-8
- var db_dg = (byte2 & 0b0000_1111)-8
- var dr = (dr_dg+dg)
- var db = (db_dg+dg)
- px[0].addmod!(dr, 256)
- px[1].addmod!(dg, 256)
- px[2].addmod!(db, 256)
- }
- elsif (byte >> 6 == 0b11) { # OP RUN
- run = (byte & 0b00_111_111)
- }
- colors[(px[0]*3 + px[1]*5 + px[2]*7 + px[3]*11)%64] = px.clone
- }
- img.Set('pixel[' + join(',', w%width, idiv(w, width)) + ']', sprintf("#%02x%02x%02x%02x", px...))
- ++w
- }
- return img
- }
- ARGV || do {
- STDERR << "usage: #{File(__MAIN__).basename} [input.qoi] [output.png]\n"
- Sys.exit(2)
- }
- var in_file = File(ARGV[0])
- var out_file = (ARGV[1] \\ (in_file - /\.qoi\z/i + '.png'))
- var bytes = in_file.read(:raw).bytes
- var img = qoi_decoder(bytes)
- img.Write(out_file) && die "Error: couldn't save file!"
|