///| fn px(p : Pixmap, x : Int, y : Int) -> (Int, Int, Int, Int) { let o = (y * p.width + x) * 4 (p.data[o].to_int(), p.data[o + 1].to_int(), p.data[o + 2].to_int(), p.data[o + 3].to_int()) } ///| test "decode_png: RGBA rows using all five filters (fixed Huffman)" { let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x05\x08\x06\x00\x00\x00\x80\x71\x56\xa2\x00\x00\x00\x3e\x49\x44\x41\x54\x78\xda\x63\x60\x60\x60\xf8\xaf\xc1\xc0\x70\x38\x80\x81\xa1\x9d\x91\xdd\x08\xc4\x91\x3b\x02\xc2\x4c\x40\x0e\x03\xbb\x91\x1c\x10\xdb\x30\x30\xf3\xa5\x30\x34\x48\x48\xda\x3c\x92\x90\xf4\x7e\xc4\x02\x96\x61\x00\xca\x30\xd8\x30\x00\x00\x9a\x17\x0b\x5d\x72\xb0\x16\x16\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() assert_eq((p.width, p.height), (3, 5)) for y = 0; y < 5; y = y + 1 { for x = 0; x < 3; x = x + 1 { assert_eq(px(p, x, y), ((x * 40 + y * 7) & 255, (y * 50) & 255, (x * y * 30) & 255, 255 - x * 60)) } } } ///| test "decode_png: RGB with dynamic Huffman blocks" { let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16\x8b\xc5\x62\xb1\xf8\xc7\xf8\x00\xd3\x7b\x15\x32\x6b\xd7\xe3\x22\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() assert_eq((p.width, p.height), (40, 40)) for y = 0; y < 40; y = y + 1 { for x = 0; x < 40; x = x + 1 { assert_eq(px(p, x, y), (x * 6, y * 6, (x + y) * 3, 255)) } } } ///| test "decode_png: 2-bit palette with tRNS, and stored grey+alpha" { let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x02\x02\x03\x00\x00\x00\xe0\x1a\x8e\x89\x00\x00\x00\x0c\x50\x4c\x54\x45\xff\x00\x00\x00\xff\x00\x00\x00\xff\x09\x09\x09\x5c\x71\x7e\x86\x00\x00\x00\x03\x74\x52\x4e\x53\xff\x80\x00\x7f\x6d\x68\x78\x00\x00\x00\x0c\x49\x44\x41\x54\x78\xda\x63\x90\x60\x7c\x02\x00\x01\x32\x00\xfe\x06\xf2\x6e\x18\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() assert_eq(px(p, 0, 0), (255, 0, 0, 255)) assert_eq(px(p, 1, 0), (0, 255, 0, 128)) assert_eq(px(p, 2, 0), (0, 0, 255, 0)) assert_eq(px(p, 0, 1), (9, 9, 9, 255)) assert_eq(px(p, 2, 1), (0, 255, 0, 128)) let g = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x02\x00\x00\x00\x01\x08\x04\x00\x00\x00\x5e\x2b\xb7\x01\x00\x00\x00\x10\x49\x44\x41\x54\x78\x01\x01\x05\x00\xfa\xff\x00\x0a\x14\x1e\x28\x00\xcd\x00\x65\x7b\x27\x54\xba\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() assert_eq(px(g, 0, 0), (10, 10, 10, 20)) assert_eq(px(g, 1, 0), (30, 30, 30, 40)) } ///| test "decode_png refuses what isn't a PNG it can read" { assert_true(decode_png(b"not a png at all, not a png at all, nope").is_empty()) assert_true(decode_png(b"").is_empty()) // a truncated file let cut = b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16" assert_true(decode_png(cut).is_empty()) } ///| test "scale_pixmap averages down and draw_image blends the icon into the frame" { let src = Pixmap::new(4, 4) for i = 0; i < 16; i = i + 1 { src.data[i * 4] = b'\xff' src.data[i * 4 + 3] = if i % 2 == 0 { b'\xff' } else { b'\x00' } } let s = scale_pixmap(src, 2, 2) // half the source pixels are opaque red: the result is red at half alpha assert_eq(px(s, 0, 0), (255, 0, 0, 127)) let h = add_image(src) assert_true(h != 0U) let dst = Pixmap::new(10, 10) draw_ops(dst, [Image({ x: 2, y: 2, w: 4, h: 4 }, h)], 0, 0) assert_eq(px(dst, 2, 2), (255, 0, 0, 255)) assert_eq(px(dst, 3, 2).3, 0) assert_eq(px(dst, 0, 0), (0, 0, 0, 0)) // unknown handles draw nothing draw_ops(dst, [Image({ x: 0, y: 0, w: 2, h: 2 }, 9999U)], 0, 0) assert_eq(px(dst, 0, 0), (0, 0, 0, 0)) }