"""Coarse pixel comparison of screenshots, with the standard library only. Decodes 8-bit, non-interlaced gray, gray+alpha, RGB and RGBA PNGs, which is what Chrome captures; palette, 16-bit and interlaced images raise ValueError. """ from __future__ import annotations import math import struct import zlib CHANNELS = {0: 1, 2: 3, 4: 2, 6: 4} # Pixels per block side. At half scale Chrome averages each 2x2 itself, so a # block covers 8 page pixels or more and dither inside it cancels out. BLOCK = 4 # Most pixels one comparison captures: about 10 ms to decode and average. SAMPLES = 40_000 def scale(width: float, height: float) -> float: return min(0.5, math.sqrt(SAMPLES / (width * height))) def decode(png: bytes) -> tuple[int, int, int, bytes]: """Width, height, channels per pixel and raw rows of an 8-bit PNG.""" if png[:8] != b"\x89PNG\r\n\x1a\n" or png[12:16] != b"IHDR": raise ValueError("not a PNG") width, height, depth, colour, _, _, interlace = struct.unpack( ">IIBBBBB", png[16:29] ) if depth != 8 or colour not in CHANNELS or interlace: raise ValueError("only 8-bit, non-interlaced, non-palette PNGs decode here") at, data = 8, bytearray() while at + 8 <= len(png): size, chunk = struct.unpack(">I4s", png[at : at + 8]) if chunk == b"IDAT": data += png[at + 8 : at + 8 + size] at += 12 + size raw = zlib.decompress(data) step = CHANNELS[colour] stride = width * step out = bytearray(stride * height) prior = bytearray(stride) for y in range(height): start = y * (stride + 1) method = raw[start] row = bytearray(raw[start + 1 : start + 1 + stride]) if method == 1: for i in range(step, stride): row[i] = (row[i] + row[i - step]) & 255 elif method == 2: for i in range(stride): row[i] = (row[i] + prior[i]) & 255 elif method == 3: for i in range(stride): left = row[i - step] if i >= step else 0 row[i] = (row[i] + ((left + prior[i]) >> 1)) & 255 elif method == 4: for i in range(stride): a = row[i - step] if i >= step else 0 b = prior[i] c = prior[i - step] if i >= step else 0 p = a + b - c pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) row[i] = ( row[i] + (a if pa <= pb and pa <= pc else b if pb <= pc else c) ) & 255 out[y * stride : (y + 1) * stride] = row prior = row return width, height, step, bytes(out) def blocks(png: bytes) -> list[tuple[float, ...]]: """Mean colour of each BLOCK x BLOCK square, row by row, alpha ignored.""" width, height, step, pixels = decode(png) colours = 3 if step >= 3 else 1 stride = width * step means = [] for top in range(0, height, BLOCK): rows = range(top, min(top + BLOCK, height)) for left in range(0, width, BLOCK): right = min(left + BLOCK, width) count = len(rows) * (right - left) means.append( tuple( sum( sum( pixels[ y * stride + left * step + c : y * stride + right * step : step ] ) for y in rows ) / count for c in range(colours) ) ) return means def difference(a: list[tuple[float, ...]], b: list[tuple[float, ...]]) -> list[float]: """How far each block's colour moved, in 0-255 channel units.""" return [max(abs(x - y) for x, y in zip(p, q)) for p, q in zip(a, b)]