Oregami
Repositories/oxedyne/fe2o3

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.
17set -eu
18cd "$(dirname "$0")"
19rm -f ./*.bmp ./*.gif ./*.tif ./*.png
20python3 - <<'PYEOF'
21import struct
22import subprocess
23from PIL import Image
24
25def 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
52def 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
64def 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
72def run(*a):
73 subprocess.run(list(a), check=True)
74
75T = text(203, 97) # odd width, so every row is padded
76W = text(1800, 40) # wider than a fax line, for the long makeup codes
77C = colour(37, 23, False)
78A = colour(37, 23, True)
79G = grey(37, 23)
80P = C.quantize(16) # sixteen colours, for the 4-bit and palette cases
81P256 = C.quantize(200)
82
83T.save('src_text.pbm')
84C.save('src_rgb.ppm')
85A.save('src_rgba.png')
86G.save('src_grey.pgm')
87P.save('src_p16.png')
88P256.save('src_p256.png')
89
90# ---- BMP ----
91def 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
98def 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
103run('magick', 'src_text.pbm', '-type', 'bilevel', 'BMP3:bmp1_text.bmp')
104assert bmp_bpp('bmp1_text.bmp')[1:] == (1, 0), bmp_bpp('bmp1_text.bmp')
105T.save('bmp1_text_pil.bmp')
106assert bmp_bpp('bmp1_text_pil.bmp')[1:] == (1, 0)
107run('magick', 'src_p16.png', '-type', 'palette', 'BMP3:bmp4.bmp')
108assert bmp_bpp('bmp4.bmp')[1:] == (4, 0), bmp_bpp('bmp4.bmp')
109P256.save('bmp8.bmp')
110assert bmp_bpp('bmp8.bmp')[1:] == (8, 0), bmp_bpp('bmp8.bmp')
111run('magick', 'src_p256.png', '-type', 'palette', '-compress', 'RLE', 'BMP3:bmp8_rle.bmp')
112assert bmp_bpp('bmp8_rle.bmp')[1:] == (8, 1), bmp_bpp('bmp8_rle.bmp')
113run('magick', 'src_rgb.ppm', '-define', 'bmp:subtype=RGB555', 'BMP:bmp16_555.bmp')
114assert bmp_bpp('bmp16_555.bmp')[1] == 16, bmp_bpp('bmp16_555.bmp')
115run('magick', 'src_rgb.ppm', '-define', 'bmp:subtype=RGB565', 'BMP:bmp16_565.bmp')
116assert bmp_bpp('bmp16_565.bmp')[1:] == (16, 3), bmp_bpp('bmp16_565.bmp')
117run('magick', 'src_rgb.ppm', 'BMP3:bmp24.bmp')
118assert bmp_bpp('bmp24.bmp')[1:] == (24, 0), bmp_bpp('bmp24.bmp')
119run('magick', 'src_rgb.ppm', 'BMP2:bmp24_os2.bmp')
120assert bmp_bpp('bmp24_os2.bmp')[0] == 12, bmp_bpp('bmp24_os2.bmp')
121run('magick', 'src_rgba.png', 'BMP:bmp32_v5.bmp')
122hs, bpp, comp = bmp_bpp('bmp32_v5.bmp')
123assert (hs, bpp, comp) == (124, 32, 3), (hs, bpp, comp)
124
125# Top-down: bmp24 with its height negated and its rows reversed.
126b = bytearray(open('bmp24.bmp', 'rb').read())
127off = struct.unpack('<I', b[10:14])[0]
128w, h = struct.unpack('<ii', b[18:26])
129stride = (w * 24 + 31) // 32 * 4
130rows = [b[off + r * stride: off + (r + 1) * stride] for r in range(h)]
131b[22:26] = struct.pack('<i', -h)
132b[off:] = b''.join(reversed(rows))
133open('bmp24_topdown.bmp', 'wb').write(bytes(b))
134
135# RLE4: encoded runs only, a pair of indices alternating within each run, rows bottom-up.
136pal = P.getpalette()[:48]
137pp = P.load()
138w, h = P.size
139data = bytearray()
140for 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'
152data += b'\x00\x01'
153palb = b''.join(bytes([pal[i * 3 + 2], pal[i * 3 + 1], pal[i * 3], 0]) for i in range(16))
154off = 14 + 40 + len(palb)
155hdr = struct.pack('<IiiHHIIiiII', 40, w, h, 1, 4, 2, len(data), 2835, 2835, 16, 0)
156open('bmp4_rle.bmp', 'wb').write(b'BM' + struct.pack('<IHHI', off + len(data), 0, 0, off) + hdr + palb + bytes(data))
157
158# ---- GIF ----
159run('magick', 'src_p256.png', 'gif_plain.gif')
160run('magick', 'src_p256.png', '-interlace', 'GIF', 'gif_interlaced.gif')
161run('magick', 'src_p16.png', '-transparent', '#%02x%02x%02x' % tuple(P.getpalette()[:3]), 'gif_transparent.gif')
162run('magick', 'src_text.pbm', 'gif_text.gif')
163
164# ---- TIFF ----
165def tags(f, page=0):
166 im = Image.open(f)
167 im.seek(page)
168 return im.tag_v2
169
170def 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
177T.save('tif_g1_none.tif', compression='raw')
178check('tif_g1_none.tif', 1)
179T.save('tif_g1_packbits.tif', compression='packbits')
180check('tif_g1_packbits.tif', 32773)
181T.save('tif_g1_lzw.tif', compression='tiff_lzw')
182check('tif_g1_lzw.tif', 5)
183T.save('tif_fax_mh.tif', compression='tiff_ccitt')
184check('tif_fax_mh.tif', 2)
185T.save('tif_fax_g3_1d.tif', compression='group3')
186check('tif_fax_g3_1d.tif', 3)
187T.save('tif_fax_g3_2d.tif', compression='group3', tiffinfo={292: 1})
188check('tif_fax_g3_2d.tif', 3, t292=1)
189T.save('tif_fax_g3_2d_fill.tif', compression='group3', tiffinfo={292: 5})
190check('tif_fax_g3_2d_fill.tif', 3, t292=5)
191T.save('tif_fax_g4.tif', compression='group4')
192check('tif_fax_g4.tif', 4)
193W.save('tif_fax_g4_wide.tif', compression='group4')
194W.save('tif_fax_g3_wide.tif', compression='group3', tiffinfo={292: 1})
195run('magick', 'src_text.pbm', '-compress', 'Group4', '-define', 'tiff:fill-order=lsb', 'tif_fax_g4_lsb.tif')
196check('tif_fax_g4_lsb.tif', 4, t266=2)
197T.save('tif_fax_g4_strips.tif', compression='group4', tiffinfo={278: 10})
198check('tif_fax_g4_strips.tif', 4, t278=10)
199
200for 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,))
203G.save('tif_grey8_lzw.tif', compression='tiff_lzw')
204check('tif_grey8_lzw.tif', 5)
205run('magick', 'src_grey.pgm', '-depth', '16', '-compress', 'Zip', 'tif_grey16_deflate.tif')
206check('tif_grey16_deflate.tif', 8, t258=(16,))
207run('magick', 'src_grey.pgm', '-depth', '16', '-compress', 'None', '-define', 'tiff:endian=msb', 'tif_grey16_be.tif')
208assert open('tif_grey16_be.tif', 'rb').read(2) == b'MM'
209run('magick', 'src_grey.pgm', '-negate', '-define', 'quantum:polarity=min-is-white', '-compress', 'None', 'tif_grey8_miniswhite.tif')
210check('tif_grey8_miniswhite.tif', 1, t262=0)
211
212P256.save('tif_palette_lzw.tif', compression='tiff_lzw')
213check('tif_palette_lzw.tif', 5, t262=3)
214run('magick', 'src_p16.png', '-type', 'palette', '-depth', '4', '-compress', 'None', 'tif_palette4.tif')
215check('tif_palette4.tif', 1, t262=3)
216
217C.save('tif_rgb_none.tif', compression='raw')
218check('tif_rgb_none.tif', 1)
219C.save('tif_rgb_packbits.tif', compression='packbits')
220check('tif_rgb_packbits.tif', 32773)
221run('magick', 'src_rgb.ppm', '-compress', 'LZW', '-define', 'tiff:predictor=2', 'tif_rgb_lzw_pred.tif')
222check('tif_rgb_lzw_pred.tif', 5, t317=2)
223C.save('tif_rgb_deflate.tif', compression='tiff_adobe_deflate')
224check('tif_rgb_deflate.tif', 8)
225run('magick', 'src_rgb.ppm', '-compress', 'JPEG', '-quality', '95', 'tif_rgb_jpeg.tif')
226check('tif_rgb_jpeg.tif', 7)
227run('magick', 'src_rgb.ppm', '-compress', 'LZW', '-define', 'tiff:tile-geometry=16x16', 'tif_rgb_tiled.tif')
228check('tif_rgb_tiled.tif', 5, t322=16)
229run('magick', 'src_rgb.ppm', '-compress', 'None', '-interlace', 'Plane', 'tif_rgb_planar.tif')
230check('tif_rgb_planar.tif', 1, t284=2)
231A.save('tif_rgba_lzw.tif', compression='tiff_lzw')
232check('tif_rgba_lzw.tif', 5)
233run('magick', 'src_rgb.ppm', '-colorspace', 'CMYK', '-compress', 'None', 'tif_cmyk.tif')
234check('tif_cmyk.tif', 1, t262=5)
235run('magick', 'src_text.pbm', 'src_rgb.ppm', 'src_grey.pgm', '-compress', 'LZW', 'tif_multipage.tif')
236PYEOF
237
238# The expected pixels: ImageMagick's own reading of each fixture, as eight-bit RGBA.
239for 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
249done
250rm -f src_*
251ls