Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | 248x 248x 20534058x 20534058x 20534058x 20534058x 248x 167x 167x 167x 167x 167x 167x 167x 167x 5335x 231815x 119341x 119341x 119341x 622598x 622598x 3518890x 3518890x 3518890x 3518890x 167x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 13849x 4880429x 4880429x 4880429x 4880429x 4880429x 2500602x 2500602x 2500602x 2500602x 2500602x 2500602x 7501806x 7501806x 7501806x 7501806x 7501806x 40x 11x 11x 11x 11x 11x 11x 11x 4214x 1658932x 1658932x 1497031x 1497031x 1497031x 1497031x 1497031x 1497031x 4491093x 4491093x 4491093x 4491093x 4491093x 11x 8x 8x 8x 8x 8x 8x 2930x 1104196x 1104196x 5409996x 5409996x 1104196x 8x 2930x 1104196x 1104196x 5409996x 5409996x 1104196x 1104196x 1104196x 1104196x 1104196x 8x 6x 6x 676688x 676688x 676688x 676688x 676688x 6x 4x 4x 4x 4x 4x 1570x 641732x 641732x 641732x 641732x 641732x 641732x 641732x 4x 2x 2x 2x 680x 231232x 231232x 231232x 231232x 231232x 231232x 231232x 2x 2x 2x 2x 87x 87x 3825x 3825x 3825x 3825x 3825x 2x 3x 3x 216904x 216904x 216904x 3x 2x 2x 566x 167620x 167620x 167620x 167620x 167620x 2x 3x 3x 5x 5x 765x 765x 765x 118325x 118325x 118325x 118325x 118325x 118325x 118325x 3x | import type { QRMatrix } from "@usefy/qr-code/headless";
import { PerspectiveTransform, type Quad4 } from "../detect/perspective";
/**
* Deterministic rendering and degradation of QR symbols, for the detection
* oracle (SPEC §7).
*
* Everything here is pure arithmetic over `ImageData` — no canvas, no DOM — so
* the corpus is byte-identical on every machine and a failure can be replayed
* from its seed. That matters more than realism: a "sometimes decodes" test
* teaches nothing, and a corpus that differs between CI and a laptop cannot be
* debugged.
*/
export interface RenderOptions {
/** Pixels per module. */
scale?: number;
/** Quiet zone in modules. */
margin?: number;
/** Rotation about the image centre, in degrees. */
rotate?: number;
/** Extra padding in pixels around the rotated result. */
padding?: number;
/** Luminance of a dark module (0–255). */
dark?: number;
/** Luminance of a light module (0–255). */
light?: number;
}
function createImage(width: number, height: number, fill: number): ImageData {
const data = new Uint8ClampedArray(width * height * 4);
for (let i = 0; i < data.length; i += 4) {
data[i] = fill;
data[i + 1] = fill;
data[i + 2] = fill;
data[i + 3] = 255;
}
return { data, width, height, colorSpace: "srgb" } as ImageData;
}
/** Render a symbol to an `ImageData`, optionally rotated about its centre. */
export function renderMatrix(matrix: QRMatrix, options: RenderOptions = {}): ImageData {
const scale = options.scale ?? 6;
const margin = options.margin ?? 4;
const dark = options.dark ?? 0;
const light = options.light ?? 255;
const padding = options.padding ?? 0;
const side = (matrix.size + margin * 2) * scale;
const flat = createImage(side, side, light);
for (let y = 0; y < matrix.size; y++) {
for (let x = 0; x < matrix.size; x++) {
if (!matrix.get(x, y)) continue;
const px = (x + margin) * scale;
const py = (y + margin) * scale;
for (let dy = 0; dy < scale; dy++) {
let index = ((py + dy) * side + px) * 4;
for (let dx = 0; dx < scale; dx++) {
flat.data[index] = dark;
flat.data[index + 1] = dark;
flat.data[index + 2] = dark;
index += 4;
}
}
}
}
if (!options.rotate && !padding) return flat;
return rotate(flat, options.rotate ?? 0, padding, light);
}
/**
* Rotate an image about its centre, sampling the source bilinearly.
*
* Bilinear rather than nearest-neighbour on purpose: a rotated QR in the real
* world has soft module edges, and testing against hard-edged nearest-neighbour
* output would flatter the binarizer into looking better than it is.
*/
export function rotate(image: ImageData, degrees: number, padding = 0, fill = 255): ImageData {
const radians = (degrees * Math.PI) / 180;
const cos = Math.cos(radians);
const sin = Math.sin(radians);
const width = image.width + padding * 2;
const height = image.height + padding * 2;
const out = createImage(width, height, fill);
const cx = width / 2;
const cy = height / 2;
const scx = image.width / 2;
const scy = image.height / 2;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const dx = x - cx;
const dy = y - cy;
const sx = scx + dx * cos + dy * sin;
const sy = scy - dx * sin + dy * cos;
if (sx < 0 || sy < 0 || sx >= image.width - 1 || sy >= image.height - 1) continue;
const x0 = Math.floor(sx);
const y0 = Math.floor(sy);
const fx = sx - x0;
const fy = sy - y0;
const index = (y * width + x) * 4;
for (let channel = 0; channel < 3; channel++) {
const p00 = image.data[(y0 * image.width + x0) * 4 + channel]!;
const p10 = image.data[(y0 * image.width + x0 + 1) * 4 + channel]!;
const p01 = image.data[((y0 + 1) * image.width + x0) * 4 + channel]!;
const p11 = image.data[((y0 + 1) * image.width + x0 + 1) * 4 + channel]!;
out.data[index + channel] =
p00 * (1 - fx) * (1 - fy) + p10 * fx * (1 - fy) + p01 * (1 - fx) * fy + p11 * fx * fy;
}
}
}
return out;
}
/**
* Apply a true projective warp — what photographing a flat code at an angle
* actually does.
*
* A homography, not a per-row stretch: in a real photograph the rows also
* *bunch up* towards the far edge, and a fixture that scales each row while
* leaving the vertical spacing uniform is a bilinear warp no camera produces —
* and one no four-point transform can model, so it would test the decoder
* against an impossible target.
*
* `strength` is the fraction of the image width by which the top edge narrows;
* 0.2 is a pronounced but entirely ordinary hand-held angle.
*/
export function perspective(image: ImageData, strength: number, fill = 255): ImageData {
const { width, height } = image;
const inset = (width * strength) / 2;
return warp(
image,
[
{ x: inset, y: 0 },
{ x: width - inset, y: 0 },
{ x: width, y: height - 1 },
{ x: 0, y: height - 1 },
],
fill,
);
}
/**
* Map the image's corners onto an arbitrary quadrilateral, sampling the source
* bilinearly.
*
* The transform is the package's own `PerspectiveTransform`. That is not
* circular: the transform is unit-tested independently, and what this fixture
* exercises is everything the decoder does *around* it — finding the finder
* patterns, locating the alignment pattern, and recovering a transform of its
* own from those points alone.
*/
export function warp(image: ImageData, destination: Quad4, fill = 255): ImageData {
const { width, height } = image;
const out = createImage(width, height, fill);
// Destination → source, so every output pixel is filled exactly once.
const inverse = PerspectiveTransform.quadToQuad(destination, [
{ x: 0, y: 0 },
{ x: width - 1, y: 0 },
{ x: width - 1, y: height - 1 },
{ x: 0, y: height - 1 },
]);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const { x: sx, y: sy } = inverse.map(x, y);
if (sx < 0 || sy < 0 || sx >= width - 1 || sy >= height - 1) continue;
const x0 = Math.floor(sx);
const y0 = Math.floor(sy);
const fx = sx - x0;
const fy = sy - y0;
const index = (y * width + x) * 4;
for (let channel = 0; channel < 3; channel++) {
const p00 = image.data[(y0 * width + x0) * 4 + channel]!;
const p10 = image.data[(y0 * width + x0 + 1) * 4 + channel]!;
const p01 = image.data[((y0 + 1) * width + x0) * 4 + channel]!;
const p11 = image.data[((y0 + 1) * width + x0 + 1) * 4 + channel]!;
out.data[index + channel] =
p00 * (1 - fx) * (1 - fy) + p10 * fx * (1 - fy) + p01 * (1 - fx) * fy + p11 * fx * fy;
}
}
}
return out;
}
/** Separable box blur — a cheap, deterministic stand-in for defocus. */
export function blur(image: ImageData, radius: number): ImageData {
Iif (radius < 1) return image;
const { width, height } = image;
const out = createImage(width, height, 255);
const window = radius * 2 + 1;
const horizontal = new Float32Array(width * height);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let sum = 0;
for (let k = -radius; k <= radius; k++) {
const sx = Math.min(width - 1, Math.max(0, x + k));
sum += image.data[(y * width + sx) * 4]!;
}
horizontal[y * width + x] = sum / window;
}
}
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let sum = 0;
for (let k = -radius; k <= radius; k++) {
const sy = Math.min(height - 1, Math.max(0, y + k));
sum += horizontal[sy * width + x]!;
}
const value = sum / window;
const index = (y * width + x) * 4;
out.data[index] = value;
out.data[index + 1] = value;
out.data[index + 2] = value;
}
}
return out;
}
/** Additive uniform noise, seeded. */
export function noise(image: ImageData, amplitude: number, random: () => number): ImageData {
const out = createImage(image.width, image.height, 0);
for (let i = 0; i < image.data.length; i += 4) {
const delta = (random() * 2 - 1) * amplitude;
const value = image.data[i]! + delta;
out.data[i] = value;
out.data[i + 1] = value;
out.data[i + 2] = value;
}
return out;
}
/**
* A smooth illumination gradient across the image — the shadow of the hand
* holding the phone, which is what defeats a single global threshold.
*
* `strength` is the fraction of full range lost at the darkest corner.
*/
export function illuminate(image: ImageData, strength: number, angle = 0): ImageData {
const out = createImage(image.width, image.height, 0);
const dx = Math.cos(angle);
const dy = Math.sin(angle);
const span = Math.abs(dx) * image.width + Math.abs(dy) * image.height;
for (let y = 0; y < image.height; y++) {
for (let x = 0; x < image.width; x++) {
const t = (x * dx + y * dy) / span;
const factor = 1 - strength * t;
const index = (y * image.width + x) * 4;
const value = image.data[index]! * factor;
out.data[index] = value;
out.data[index + 1] = value;
out.data[index + 2] = value;
}
}
return out;
}
/**
* A soft-edged shadow over part of the image — the hand holding the phone.
*
* Unlike a smooth ramp across the whole frame, a shadow has an *edge*: bright
* paper on one side, dim paper on the other, with both black and white present
* in each region. That is what actually defeats a single global cutoff, because
* the "white" under the shadow is darker than the "black" outside it.
*
* @param fraction - How much of the width the shadow covers, from the left.
* @param factor - Brightness multiplier inside the shadow.
* @param feather - Width of the transition, in pixels.
*/
export function shadow(
image: ImageData,
fraction: number,
factor: number,
feather = 8,
): ImageData {
const out = createImage(image.width, image.height, 0);
const edge = image.width * fraction;
for (let y = 0; y < image.height; y++) {
for (let x = 0; x < image.width; x++) {
const t = Math.min(1, Math.max(0, (edge - x) / feather));
const multiplier = 1 - (1 - factor) * t;
const index = (y * image.width + x) * 4;
const value = image.data[index]! * multiplier;
out.data[index] = value;
out.data[index + 1] = value;
out.data[index + 2] = value;
}
}
return out;
}
/** Paint a filled rectangle — occlusion, a finger over the corner, a logo. */
export function occlude(
image: ImageData,
x: number,
y: number,
width: number,
height: number,
value = 128,
): ImageData {
const out = createImage(image.width, image.height, 0);
out.data.set(image.data);
for (let py = y; py < y + height; py++) {
Iif (py < 0 || py >= image.height) continue;
for (let px = x; px < x + width; px++) {
Iif (px < 0 || px >= image.width) continue;
const index = (py * image.width + px) * 4;
out.data[index] = value;
out.data[index + 1] = value;
out.data[index + 2] = value;
}
}
return out;
}
/** Invert every channel — a light-on-dark symbol. */
export function invertImage(image: ImageData): ImageData {
const out = createImage(image.width, image.height, 0);
for (let i = 0; i < image.data.length; i += 4) {
out.data[i] = 255 - image.data[i]!;
out.data[i + 1] = 255 - image.data[i + 1]!;
out.data[i + 2] = 255 - image.data[i + 2]!;
}
return out;
}
/** Mirror horizontally. */
export function mirrorImage(image: ImageData): ImageData {
const out = createImage(image.width, image.height, 0);
for (let y = 0; y < image.height; y++) {
for (let x = 0; x < image.width; x++) {
const from = (y * image.width + (image.width - 1 - x)) * 4;
const to = (y * image.width + x) * 4;
out.data[to] = image.data[from]!;
out.data[to + 1] = image.data[from + 1]!;
out.data[to + 2] = image.data[from + 2]!;
}
}
return out;
}
/** Place several images side by side on one canvas — a multi-symbol frame. */
export function compose(
images: readonly ImageData[],
positions: ReadonlyArray<{ x: number; y: number }>,
width: number,
height: number,
fill = 255,
): ImageData {
const out = createImage(width, height, fill);
images.forEach((image, index) => {
const { x: ox, y: oy } = positions[index]!;
for (let y = 0; y < image.height; y++) {
const ty = oy + y;
Iif (ty < 0 || ty >= height) continue;
for (let x = 0; x < image.width; x++) {
const tx = ox + x;
Iif (tx < 0 || tx >= width) continue;
const from = (y * image.width + x) * 4;
const to = (ty * width + tx) * 4;
out.data[to] = image.data[from]!;
out.data[to + 1] = image.data[from + 1]!;
out.data[to + 2] = image.data[from + 2]!;
}
}
});
return out;
}
|