tests/run_tests.sh: fake sfile on PATH (tests/bin/sfile, canned GPS values in section + EXIF tag-name forms), a local tile server (tests/tile_server.py, $SGEO_TILE_BASE) so sgeo tests run offline, and tests/check_map.py (stdlib PNG decode: marker-cluster count + attribution plaque). Cases: multi-file CSV byte-for-byte, multi + single-point maps, no-geotag dir, empty dir, default outfiles. 8 checks, all passing. docs/geotag-contract.md: the field-name contract between the decoder (what sfile prints in its Geotagging: section) and the client parser (section + EXIF tag-name / uniform-table fallback forms it accepts), incl. first-source-wins, defaults, and ranges. Note: tests/bin/sfile is force-added -- .gitignore has a bare sfile rule for the built binary.
138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Check a map.png produced by sgeo (stdlib only, no PIL needed).
|
|
|
|
Usage: check_map.py map.png <expected-marker-clusters>
|
|
|
|
Decodes the PNG, finds red marker pixels (r>=200, g<=90, b<=90),
|
|
groups them into connected clusters, and requires exactly the
|
|
expected number of clusters, each at least 20 pixels (a marker is a
|
|
~7px-radius red disc). Also requires the attribution plaque: the
|
|
bottom 16 rows must contain both white plaque pixels and dark text
|
|
pixels. Prints the image size, cluster count and attribution counts.
|
|
"""
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
|
|
|
|
def read_png(path):
|
|
with open(path, "rb") as f:
|
|
data = f.read()
|
|
assert data[:8] == b"\x89PNG\r\n\x1a\n", "not a PNG"
|
|
pos = 8
|
|
idat = b""
|
|
w = h = None
|
|
bit_depth = color_type = None
|
|
while pos < len(data):
|
|
ln = struct.unpack(">I", data[pos:pos + 4])[0]
|
|
typ = data[pos + 4:pos + 8]
|
|
body = data[pos + 8:pos + 8 + ln]
|
|
if typ == b"IHDR":
|
|
w, h, bit_depth, color_type = struct.unpack(">IIBB", body[:10])
|
|
elif typ == b"IDAT":
|
|
idat += body
|
|
elif typ == b"IEND":
|
|
break
|
|
pos += 12 + ln
|
|
assert bit_depth == 8 and color_type in (2, 6), \
|
|
"unsupported PNG: depth %s type %s" % (bit_depth, color_type)
|
|
ch = 3 if color_type == 2 else 4
|
|
raw = zlib.decompress(idat)
|
|
stride = w * ch
|
|
px = bytearray(w * h * 3)
|
|
prev = bytearray(stride)
|
|
p = 0
|
|
for y in range(h):
|
|
filt = raw[p]
|
|
p += 1
|
|
line = bytearray(raw[p:p + stride])
|
|
p += stride
|
|
if filt == 1:
|
|
for i in range(ch, stride):
|
|
line[i] = (line[i] + line[i - ch]) & 0xFF
|
|
elif filt == 2:
|
|
for i in range(stride):
|
|
line[i] = (line[i] + prev[i]) & 0xFF
|
|
elif filt == 3:
|
|
for i in range(stride):
|
|
a = line[i - ch] if i >= ch else 0
|
|
line[i] = (line[i] + ((a + prev[i]) >> 1)) & 0xFF
|
|
elif filt == 4:
|
|
for i in range(stride):
|
|
a = line[i - ch] if i >= ch else 0
|
|
b = prev[i]
|
|
c = prev[i - ch] if i >= ch else 0
|
|
pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - 2 * c)
|
|
pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c)
|
|
line[i] = (line[i] + pr) & 0xFF
|
|
elif filt != 0:
|
|
raise ValueError("unknown PNG filter %d" % filt)
|
|
for x in range(w):
|
|
o = (y * w + x) * 3
|
|
px[o:o + 3] = line[x * ch:x * ch + 3]
|
|
prev = line
|
|
return w, h, px
|
|
|
|
|
|
def main():
|
|
path, expected = sys.argv[1], int(sys.argv[2])
|
|
w, h, px = read_png(path)
|
|
|
|
def rgb(x, y):
|
|
o = (y * w + x) * 3
|
|
return px[o], px[o + 1], px[o + 2]
|
|
|
|
red = set()
|
|
for y in range(h):
|
|
for x in range(w):
|
|
r, g, b = rgb(x, y)
|
|
if r >= 200 and g <= 90 and b <= 90:
|
|
red.add((x, y))
|
|
# Cluster red pixels: union neighbours within 2px (a marker disc
|
|
# is solid, so 8-connectivity with a 2px reach is plenty).
|
|
parent = {}
|
|
|
|
def find(a):
|
|
while parent[a] != a:
|
|
parent[a] = parent[parent[a]]
|
|
a = parent[a]
|
|
return a
|
|
|
|
cells = {}
|
|
for p in red:
|
|
parent[p] = p
|
|
cells.setdefault((p[0] // 4, p[1] // 4), []).append(p)
|
|
for (x, y) in red:
|
|
for cx in (x // 4 - 1, x // 4, x // 4 + 1):
|
|
for cy in (y // 4 - 1, y // 4, y // 4 + 1):
|
|
for q in cells.get((cx, cy), []):
|
|
if q != (x, y) and abs(q[0] - x) <= 2 and abs(q[1] - y) <= 2:
|
|
ra, rb = find((x, y)), find(q)
|
|
if ra != rb:
|
|
parent[ra] = rb
|
|
sizes = {}
|
|
for p in red:
|
|
sizes[find(p)] = sizes.get(find(p), 0) + 1
|
|
clusters = sorted((s for s in sizes.values() if s >= 20), reverse=True)
|
|
|
|
# Attribution plaque: white + dark pixels in the bottom 16 rows.
|
|
white = dark = 0
|
|
for y in range(max(0, h - 16), h):
|
|
for x in range(w):
|
|
r, g, b = rgb(x, y)
|
|
if r > 240 and g > 240 and b > 240:
|
|
white += 1
|
|
elif r < 100 and g < 100 and b < 100:
|
|
dark += 1
|
|
|
|
print("map %dx%d clusters=%s (sizes %s) plaque white=%d dark=%d"
|
|
% (w, h, len(clusters), clusters, white, dark))
|
|
ok = (len(clusters) == expected and white >= 100 and dark >= 10
|
|
and w % 256 == 0 and h % 256 == 0)
|
|
print("check_map: %s" % ("PASS" if ok else "FAIL"))
|
|
return 0 if ok else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|