oxedyne/fe2o3/fe2o3_graphics/tests/raster/gen.sh
9.7 KiB, 1 run, executable
created by r1870400018:75714, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/bin/bash |
| 2 | # Regenerates the BMP, GIF and TIFF fixtures in this directory, and the PNG beside each that the |
| 3 | # test compares against. |
| 4 | # |
| 5 | # Nothing here was written by the codec under test. A short Python program draws the sources: a |
| 6 | # one-bit page of text-like strokes, whose runs are long and varied the way a scanned letter's are, |
| 7 | # and small colour, grey and alpha gradients. Pillow (with libtiff) and ImageMagick write each |
| 8 | # fixture, and ImageMagick reads the fixture back and writes what it saw as `<name>.png`, eight-bit |
| 9 | # RGBA. So a fixture's expected pixels come from an independent reader, never from this crate. |
| 10 | # |
| 11 | # The two BMP shapes neither writer offers -- RLE4 and top-down rows -- are built here by a few lines |
| 12 | # of Python from the same source pixels, and still checked against ImageMagick's reading of them. |
| 13 | # Each fixture's header is re-read after writing, and the script stops if a writer quietly chose a |
| 14 | # different depth or coding from the one the name claims. |
| 15 | # |
| 16 | # Requires ImageMagick 7 (`magick`) and Python 3 with Pillow built against libtiff. Run from here. |
| 17 | set -eu |
| 18 | cd "$(dirname "$0")" |
| 19 | rm -f ./*.bmp ./*.gif ./*.tif ./*.png |
| 20 | python3 - <<'PYEOF' |
| 21 | import struct |
| 22 | import subprocess |
| 23 | from PIL import Image |
| 24 | |
| 25 | def text(w, h): |
| 26 | """A page of text-like strokes: lines of words, each word a run of letter-like stems.""" |
| 27 | im = Image.new('1', (w, h), 1) |
| 28 | px = im.load() |
| 29 | seed = 12345 |
| 30 | def rnd(n): |
| 31 | nonlocal seed |
| 32 | seed = (seed * 1103515245 + 12345) & 0x7fffffff |
| 33 | return seed % n |
| 34 | y = 3 |
| 35 | while y + 9 < h: |
| 36 | x = 2 + rnd(6) |
| 37 | while x < w - 4: |
| 38 | word = 3 + rnd(40) |
| 39 | for xx in range(x, min(x + word, w - 1)): |
| 40 | stem = (xx - x) % (2 + rnd(4)) == 0 |
| 41 | for yy in range(y, y + 9): |
| 42 | if stem or yy in (y, y + 4 + rnd(2), y + 8) and rnd(3): |
| 43 | px[xx, yy] = 0 |
| 44 | x += word + 2 + rnd(9) |
| 45 | y += 12 + rnd(4) |
| 46 | # A solid bar and a blank band, so the longest runs and their makeup codes are exercised. |
| 47 | for xx in range(w // 5, w - w // 7): |
| 48 | for yy in range(h - 3, h): |
| 49 | px[xx, yy] = 0 |
| 50 | return im |
| 51 | |
| 52 | def colour(w, h, alpha): |
| 53 | im = Image.new('RGBA' if alpha else 'RGB', (w, h)) |
| 54 | px = im.load() |
| 55 | for y in range(h): |
| 56 | for x in range(w): |
| 57 | r = (x * 47 + y * 11 + 3) % 256 |
| 58 | g = (x * 13 + y * 61 + 71) % 256 |
| 59 | b = (x * 29 + y * 89 + 151) % 256 |
| 60 | a = (x * 37 + y * 101 + 19) % 256 |
| 61 | px[x, y] = (r, g, b, a) if alpha else (r, g, b) |
| 62 | return im |
| 63 | |
| 64 | def grey(w, h): |
| 65 | im = Image.new('L', (w, h)) |
| 66 | px = im.load() |
| 67 | for y in range(h): |
| 68 | for x in range(w): |
| 69 | px[x, y] = (x * 7 + y * 19) % 256 |
| 70 | return im |
| 71 | |
| 72 | def run(*a): |
| 73 | subprocess.run(list(a), check=True) |
| 74 | |
| 75 | T = text(203, 97) # odd width, so every row is padded |
| 76 | W = text(1800, 40) # wider than a fax line, for the long makeup codes |
| 77 | C = colour(37, 23, False) |
| 78 | A = colour(37, 23, True) |
| 79 | G = grey(37, 23) |
| 80 | P = C.quantize(16) # sixteen colours, for the 4-bit and palette cases |
| 81 | P256 = C.quantize(200) |
| 82 | |
| 83 | T.save('src_text.pbm') |
| 84 | C.save('src_rgb.ppm') |
| 85 | A.save('src_rgba.png') |
| 86 | G.save('src_grey.pgm') |
| 87 | P.save('src_p16.png') |
| 88 | P256.save('src_p256.png') |
| 89 | |
| 90 | # ---- BMP ---- |
| 91 | def bmp_bpp(f): |
| 92 | b = open(f, 'rb').read() |
| 93 | hs = struct.unpack('<I', b[14:18])[0] |
| 94 | if hs == 12: |
| 95 | return hs, struct.unpack('<H', b[24:26])[0], 0 |
| 96 | return hs, struct.unpack('<H', b[28:30])[0], struct.unpack('<I', b[30:34])[0] |
| 97 | |
| 98 | def bmp(name, want_bpp, want_comp, *cmd): |
| 99 | run('magick', *cmd, name) |
| 100 | hs, bpp, comp = bmp_bpp(name) |
| 101 | assert (bpp, comp) == (want_bpp, want_comp), (name, hs, bpp, comp) |
| 102 | |
| 103 | run('magick', 'src_text.pbm', '-type', 'bilevel', 'BMP3:bmp1_text.bmp') |
| 104 | assert bmp_bpp('bmp1_text.bmp')[1:] == (1, 0), bmp_bpp('bmp1_text.bmp') |
| 105 | T.save('bmp1_text_pil.bmp') |
| 106 | assert bmp_bpp('bmp1_text_pil.bmp')[1:] == (1, 0) |
| 107 | run('magick', 'src_p16.png', '-type', 'palette', 'BMP3:bmp4.bmp') |
| 108 | assert bmp_bpp('bmp4.bmp')[1:] == (4, 0), bmp_bpp('bmp4.bmp') |
| 109 | P256.save('bmp8.bmp') |
| 110 | assert bmp_bpp('bmp8.bmp')[1:] == (8, 0), bmp_bpp('bmp8.bmp') |
| 111 | run('magick', 'src_p256.png', '-type', 'palette', '-compress', 'RLE', 'BMP3:bmp8_rle.bmp') |
| 112 | assert bmp_bpp('bmp8_rle.bmp')[1:] == (8, 1), bmp_bpp('bmp8_rle.bmp') |
| 113 | run('magick', 'src_rgb.ppm', '-define', 'bmp:subtype=RGB555', 'BMP:bmp16_555.bmp') |
| 114 | assert bmp_bpp('bmp16_555.bmp')[1] == 16, bmp_bpp('bmp16_555.bmp') |
| 115 | run('magick', 'src_rgb.ppm', '-define', 'bmp:subtype=RGB565', 'BMP:bmp16_565.bmp') |
| 116 | assert bmp_bpp('bmp16_565.bmp')[1:] == (16, 3), bmp_bpp('bmp16_565.bmp') |
| 117 | run('magick', 'src_rgb.ppm', 'BMP3:bmp24.bmp') |
| 118 | assert bmp_bpp('bmp24.bmp')[1:] == (24, 0), bmp_bpp('bmp24.bmp') |
| 119 | run('magick', 'src_rgb.ppm', 'BMP2:bmp24_os2.bmp') |
| 120 | assert bmp_bpp('bmp24_os2.bmp')[0] == 12, bmp_bpp('bmp24_os2.bmp') |
| 121 | run('magick', 'src_rgba.png', 'BMP:bmp32_v5.bmp') |
| 122 | hs, bpp, comp = bmp_bpp('bmp32_v5.bmp') |
| 123 | assert (hs, bpp, comp) == (124, 32, 3), (hs, bpp, comp) |
| 124 | |
| 125 | # Top-down: bmp24 with its height negated and its rows reversed. |
| 126 | b = bytearray(open('bmp24.bmp', 'rb').read()) |
| 127 | off = struct.unpack('<I', b[10:14])[0] |
| 128 | w, h = struct.unpack('<ii', b[18:26]) |
| 129 | stride = (w * 24 + 31) // 32 * 4 |
| 130 | rows = [b[off + r * stride: off + (r + 1) * stride] for r in range(h)] |
| 131 | b[22:26] = struct.pack('<i', -h) |
| 132 | b[off:] = b''.join(reversed(rows)) |
| 133 | open('bmp24_topdown.bmp', 'wb').write(bytes(b)) |
| 134 | |
| 135 | # RLE4: encoded runs only, a pair of indices alternating within each run, rows bottom-up. |
| 136 | pal = P.getpalette()[:48] |
| 137 | pp = P.load() |
| 138 | w, h = P.size |
| 139 | data = bytearray() |
| 140 | for y in range(h - 1, -1, -1): |
| 141 | x = 0 |
| 142 | while x < w: |
| 143 | a = pp[x, y] |
| 144 | bb = pp[x + 1, y] if x + 1 < w else a |
| 145 | n = 2 |
| 146 | while x + n < w and n < 254 and pp[x + n, y] == (a if n % 2 == 0 else bb): |
| 147 | n += 1 |
| 148 | n = min(n, w - x) |
| 149 | data += bytes([n, (a << 4) | bb]) |
| 150 | x += n |
| 151 | data += b'\x00\x00' |
| 152 | data += b'\x00\x01' |
| 153 | palb = b''.join(bytes([pal[i * 3 + 2], pal[i * 3 + 1], pal[i * 3], 0]) for i in range(16)) |
| 154 | off = 14 + 40 + len(palb) |
| 155 | hdr = struct.pack('<IiiHHIIiiII', 40, w, h, 1, 4, 2, len(data), 2835, 2835, 16, 0) |
| 156 | open('bmp4_rle.bmp', 'wb').write(b'BM' + struct.pack('<IHHI', off + len(data), 0, 0, off) + hdr + palb + bytes(data)) |
| 157 | |
| 158 | # ---- GIF ---- |
| 159 | run('magick', 'src_p256.png', 'gif_plain.gif') |
| 160 | run('magick', 'src_p256.png', '-interlace', 'GIF', 'gif_interlaced.gif') |
| 161 | run('magick', 'src_p16.png', '-transparent', '#%02x%02x%02x' % tuple(P.getpalette()[:3]), 'gif_transparent.gif') |
| 162 | run('magick', 'src_text.pbm', 'gif_text.gif') |
| 163 | |
| 164 | # ---- TIFF ---- |
| 165 | def tags(f, page=0): |
| 166 | im = Image.open(f) |
| 167 | im.seek(page) |
| 168 | return im.tag_v2 |
| 169 | |
| 170 | def check(f, comp, **kw): |
| 171 | t = tags(f) |
| 172 | assert t.get(259) == comp, (f, t.get(259)) |
| 173 | for k, v in kw.items(): |
| 174 | got = t.get(int(k[1:])) |
| 175 | assert got == v, (f, k, got, v) |
| 176 | |
| 177 | T.save('tif_g1_none.tif', compression='raw') |
| 178 | check('tif_g1_none.tif', 1) |
| 179 | T.save('tif_g1_packbits.tif', compression='packbits') |
| 180 | check('tif_g1_packbits.tif', 32773) |
| 181 | T.save('tif_g1_lzw.tif', compression='tiff_lzw') |
| 182 | check('tif_g1_lzw.tif', 5) |
| 183 | T.save('tif_fax_mh.tif', compression='tiff_ccitt') |
| 184 | check('tif_fax_mh.tif', 2) |
| 185 | T.save('tif_fax_g3_1d.tif', compression='group3') |
| 186 | check('tif_fax_g3_1d.tif', 3) |
| 187 | T.save('tif_fax_g3_2d.tif', compression='group3', tiffinfo={292: 1}) |
| 188 | check('tif_fax_g3_2d.tif', 3, t292=1) |
| 189 | T.save('tif_fax_g3_2d_fill.tif', compression='group3', tiffinfo={292: 5}) |
| 190 | check('tif_fax_g3_2d_fill.tif', 3, t292=5) |
| 191 | T.save('tif_fax_g4.tif', compression='group4') |
| 192 | check('tif_fax_g4.tif', 4) |
| 193 | W.save('tif_fax_g4_wide.tif', compression='group4') |
| 194 | W.save('tif_fax_g3_wide.tif', compression='group3', tiffinfo={292: 1}) |
| 195 | run('magick', 'src_text.pbm', '-compress', 'Group4', '-define', 'tiff:fill-order=lsb', 'tif_fax_g4_lsb.tif') |
| 196 | check('tif_fax_g4_lsb.tif', 4, t266=2) |
| 197 | T.save('tif_fax_g4_strips.tif', compression='group4', tiffinfo={278: 10}) |
| 198 | check('tif_fax_g4_strips.tif', 4, t278=10) |
| 199 | |
| 200 | for d in (2, 4): |
| 201 | run('magick', 'src_grey.pgm', '-depth', str(d), '-compress', 'None', 'tif_grey%d.tif' % d) |
| 202 | check('tif_grey%d.tif' % d, 1, t258=(d,)) |
| 203 | G.save('tif_grey8_lzw.tif', compression='tiff_lzw') |
| 204 | check('tif_grey8_lzw.tif', 5) |
| 205 | run('magick', 'src_grey.pgm', '-depth', '16', '-compress', 'Zip', 'tif_grey16_deflate.tif') |
| 206 | check('tif_grey16_deflate.tif', 8, t258=(16,)) |
| 207 | run('magick', 'src_grey.pgm', '-depth', '16', '-compress', 'None', '-define', 'tiff:endian=msb', 'tif_grey16_be.tif') |
| 208 | assert open('tif_grey16_be.tif', 'rb').read(2) == b'MM' |
| 209 | run('magick', 'src_grey.pgm', '-negate', '-define', 'quantum:polarity=min-is-white', '-compress', 'None', 'tif_grey8_miniswhite.tif') |
| 210 | check('tif_grey8_miniswhite.tif', 1, t262=0) |
| 211 | |
| 212 | P256.save('tif_palette_lzw.tif', compression='tiff_lzw') |
| 213 | check('tif_palette_lzw.tif', 5, t262=3) |
| 214 | run('magick', 'src_p16.png', '-type', 'palette', '-depth', '4', '-compress', 'None', 'tif_palette4.tif') |
| 215 | check('tif_palette4.tif', 1, t262=3) |
| 216 | |
| 217 | C.save('tif_rgb_none.tif', compression='raw') |
| 218 | check('tif_rgb_none.tif', 1) |
| 219 | C.save('tif_rgb_packbits.tif', compression='packbits') |
| 220 | check('tif_rgb_packbits.tif', 32773) |
| 221 | run('magick', 'src_rgb.ppm', '-compress', 'LZW', '-define', 'tiff:predictor=2', 'tif_rgb_lzw_pred.tif') |
| 222 | check('tif_rgb_lzw_pred.tif', 5, t317=2) |
| 223 | C.save('tif_rgb_deflate.tif', compression='tiff_adobe_deflate') |
| 224 | check('tif_rgb_deflate.tif', 8) |
| 225 | run('magick', 'src_rgb.ppm', '-compress', 'JPEG', '-quality', '95', 'tif_rgb_jpeg.tif') |
| 226 | check('tif_rgb_jpeg.tif', 7) |
| 227 | run('magick', 'src_rgb.ppm', '-compress', 'LZW', '-define', 'tiff:tile-geometry=16x16', 'tif_rgb_tiled.tif') |
| 228 | check('tif_rgb_tiled.tif', 5, t322=16) |
| 229 | run('magick', 'src_rgb.ppm', '-compress', 'None', '-interlace', 'Plane', 'tif_rgb_planar.tif') |
| 230 | check('tif_rgb_planar.tif', 1, t284=2) |
| 231 | A.save('tif_rgba_lzw.tif', compression='tiff_lzw') |
| 232 | check('tif_rgba_lzw.tif', 5) |
| 233 | run('magick', 'src_rgb.ppm', '-colorspace', 'CMYK', '-compress', 'None', 'tif_cmyk.tif') |
| 234 | check('tif_cmyk.tif', 1, t262=5) |
| 235 | run('magick', 'src_text.pbm', 'src_rgb.ppm', 'src_grey.pgm', '-compress', 'LZW', 'tif_multipage.tif') |
| 236 | PYEOF |
| 237 | |
| 238 | # The expected pixels: ImageMagick's own reading of each fixture, as eight-bit RGBA. |
| 239 | for f in ./*.bmp ./*.gif ./*.tif; do |
| 240 | n=$(magick identify "$f" | wc -l) |
| 241 | b=${f%.*} |
| 242 | if [ "$n" -gt 1 ]; then |
| 243 | for ((i = 0; i < n; i++)); do |
| 244 | magick "$f[$i]" -depth 8 "PNG32:${b}.p$i.png" |
| 245 | done |
| 246 | else |
| 247 | magick "$f" -depth 8 "PNG32:${b}.png" |
| 248 | fi |
| 249 | done |
| 250 | rm -f src_* |
| 251 | ls |