/** * Compare `getImageEdit` with `createExportCamera` on the same image. * For each edit and output point, invert both paths to source coordinates * and allow less than one pixel of floating-point error. A single-axis * flip reverses the rotation angle because the camera rotates before * flipping while the edit flips before rotating. * * This simulates the edit order in WordPress Core. It does not run the * server, which may edit a different source file and rounds crop pixels. */ import { describe, expect, it } from 'vitest'; import { mat2d, vec2 } from 'gl-matrix'; import { getImageEdit } from '../get-image-edit'; import { getOutputSize } from '../get-output-size'; import { applyImageEdit } from '../apply-image-edit'; import type { ImageEdit } from '../get-image-edit'; import { createExportCamera, getRotatedBBox } from '../core/camera'; import { DEFAULT_STATE } from '../core/constants'; import type { InternalImageEditorState, Size } from '../core/types'; const IMAGE: Size = { width: 1600, height: 900 }; function makeState( overrides: Partial< InternalImageEditorState > = {} ): InternalImageEditorState { return { ...DEFAULT_STATE, image: { src: 'test.jpg', naturalWidth: IMAGE.width, naturalHeight: IMAGE.height, }, ...overrides, }; } /** * Source pixel the editor draws at output position (u, v). Inverts the * export-camera matrix — the same one the preview uses — so whatever the * user sees on-screen is exactly what this returns. * * @param state ImageEditorCanvas state (rotation/zoom/pan/flip/cropRect). * @param imageSize Natural dimensions of the source image. * @param outputSize Output canvas dimensions the camera renders into. * @param u Horizontal probe in [0, 1] across the output. * @param v Vertical probe in [0, 1] across the output. */ function exportSourcePixel( state: InternalImageEditorState, imageSize: Size, outputSize: Size, u: number, v: number ): { x: number; y: number } { const camera = createExportCamera( state, imageSize, outputSize ); const inv = mat2d.create(); mat2d.invert( inv, camera ); const out = vec2.create(); vec2.transformMat2d( out, [ u * outputSize.width, v * outputSize.height ], inv ); return { x: out[ 0 ], y: out[ 1 ] }; } function readEdit( edit: ImageEdit | null ): { flip: { horizontal: boolean; vertical: boolean }; rotation: number; crop: { x: number; y: number; width: number; height: number } | null; } { return { flip: edit?.flip ?? { horizontal: false, vertical: false }, rotation: edit?.rotation ?? 0, crop: edit?.crop ?? null, }; } /** * Source pixel the server lands on for output point (u, v). Simulates * Core's `[flip, rotate, crop]` pipeline in reverse: * 1. Output point o = (u · outW, v · outH). * 2. Canvas point c = o + (cropX, cropY) in the post-rotate full AABB. * 3. Undo rotation about the source center to reach the flipped frame. * 4. Undo flip about the source center. * * @param edit The edit the client sends. * @param imageSize Natural dimensions of the source image. * @param u Horizontal probe in [0, 1] across the output. * @param v Vertical probe in [0, 1] across the output. */ function serverSourcePixel( edit: ImageEdit | null, imageSize: Size, u: number, v: number ): { x: number; y: number } { const { flip, rotation, crop } = readEdit( edit ); const fullBBox = getRotatedBBox( imageSize.width, imageSize.height, rotation ); // Fractions → pixels in the full-AABB frame. When crop is absent the // output canvas is the full AABB itself. const cropW = crop ? crop.width * fullBBox.width : fullBBox.width; const cropH = crop ? crop.height * fullBBox.height : fullBBox.height; const cropX = crop ? crop.x * fullBBox.width : 0; const cropY = crop ? crop.y * fullBBox.height : 0; // Undo crop translation. const cx = u * cropW + cropX; const cy = v * cropH + cropY; // Undo rotation about source center. Core applies rotation to the // flipped source, so inverting rotation brings us back to the flipped // frame (source center at full-AABB center). const rad = ( rotation * Math.PI ) / 180; const cos = Math.cos( rad ); const sin = Math.sin( rad ); const dx = cx - fullBBox.width / 2; const dy = cy - fullBBox.height / 2; // gl-matrix mat2d.rotate: (x, y) → (x·cos - y·sin, x·sin + y·cos). // Inverse is the transpose: (x, y) → (x·cos + y·sin, -x·sin + y·cos). let fx = dx * cos + dy * sin + imageSize.width / 2; let fy = -dx * sin + dy * cos + imageSize.height / 2; // Undo flip about source center (flip is its own inverse). if ( flip.horizontal ) { fx = imageSize.width - fx; } if ( flip.vertical ) { fy = imageSize.height - fy; } return { x: fx, y: fy }; } // Seven probe points — corners catch offset/scale drift, center catches // uniform bugs, two asymmetric interior points catch cross-axis bugs // that a center probe would miss. const PROBES: { label: string; u: number; v: number }[] = [ { label: 'top-left', u: 0, v: 0 }, { label: 'top-right', u: 1, v: 0 }, { label: 'bottom-right', u: 1, v: 1 }, { label: 'bottom-left', u: 0, v: 1 }, { label: 'center', u: 0.5, v: 0.5 }, { label: 'interior-1', u: 0.3, v: 0.7 }, { label: 'interior-2', u: 0.8, v: 0.25 }, ]; interface ParityRow { label: string; state: InternalImageEditorState; probeLabel: string; u: number; v: number; } function buildRows(): ParityRow[] { const rotations = [ 0, 15, 45, 60, 90, 135, 201, 270 ]; const zooms = [ 1, 1.5, 2.5 ]; const pans = [ { x: 0, y: 0 }, { x: 0.08, y: -0.05 }, { x: -0.1, y: 0.12 }, ]; const flips = [ { horizontal: false, vertical: false }, { horizontal: true, vertical: false }, { horizontal: false, vertical: true }, { horizontal: true, vertical: true }, ]; const cropRects = [ { label: 'full', rect: { x: 0, y: 0, width: 1, height: 1 } }, { label: 'centered', rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 }, }, { label: 'off-center', rect: { x: 0.1, y: 0.2, width: 0.45, height: 0.55 }, }, ]; const rows: ParityRow[] = []; for ( const rotation of rotations ) { for ( const zoom of zooms ) { for ( const pan of pans ) { for ( const flip of flips ) { for ( const { label: cropLabel, rect } of cropRects ) { const flipLabel = `${ flip.horizontal ? 'H' : '-' }${ flip.vertical ? 'V' : '-' }`; const stateLabel = `rot=${ rotation } zoom=${ zoom } pan=(${ pan.x },${ pan.y }) flip=${ flipLabel } crop=${ cropLabel }`; const state = makeState( { rotation, zoom, pan, flip, cropRect: rect, } ); for ( const { label: probeLabel, u, v } of PROBES ) { rows.push( { label: `${ stateLabel } probe=${ probeLabel }`, state, probeLabel, u, v, } ); } } } } } } const subOneZoomState = makeState( { rotation: 19, zoom: 0.98, cropRect: { x: 0.48, y: 0, width: 0.04, height: 1 }, } ); for ( const { label: probeLabel, u, v } of PROBES ) { rows.push( { label: `fine-rotation sub-1 zoom probe=${ probeLabel }`, state: subOneZoomState, probeLabel, u, v, } ); } return rows; } /** * Output size the server would produce, computed from the edit. * With a crop: the crop's pixel dims in the full-AABB frame. Without a * crop: the full rotated AABB. * * @param edit The edit the server will apply. * @param imageSize Natural dimensions of the source image. */ function outputSizeFor( edit: ImageEdit | null, imageSize: Size ): Size { const { rotation, crop } = readEdit( edit ); const full = getRotatedBBox( imageSize.width, imageSize.height, rotation ); if ( ! crop ) { return { width: full.width, height: full.height }; } return { width: crop.width * full.width, height: crop.height * full.height, }; } describe( 'getImageEdit ↔ Export-camera parity', () => { const rows = buildRows(); for ( const { label, state, u, v } of rows ) { it( label, () => { const edit = getImageEdit( state ); // Output canvas is whatever the server would emit: the crop rect // if present, otherwise the full rotated AABB. This makes (u, v) // map to the same *output* pixel on both paths. const outputSize = outputSizeFor( edit, IMAGE ); const exported = exportSourcePixel( state, IMAGE, outputSize, u, v ); const server = serverSourcePixel( edit, IMAGE, u, v ); // Tolerance of 1 source pixel. The two paths compose rotations // and scales in different orders, so sub-pixel disagreement is // expected from floating-point. Anything larger is a real bug. expect( server.x ).toBeCloseTo( exported.x, 0 ); expect( server.y ).toBeCloseTo( exported.y, 0 ); } ); } } ); describe( 'applyImageEdit ↔ Export-camera parity', () => { // Written by hand rather than read from a state, so the exporter is an // independent check. const edits: [ string, ImageEdit ][] = [ [ 'a flip, a quarter turn and an off-center crop', { flip: { horizontal: true, vertical: false }, rotation: 90, crop: { x: 0.1, y: 0.2, width: 0.5, height: 0.3 }, }, ], [ 'a fine rotation with a crop inside the image', { rotation: 15, crop: { x: 0.35, y: 0.35, width: 0.3, height: 0.3 }, }, ], ]; for ( const [ name, edit ] of edits ) { for ( const { label, u, v } of PROBES ) { it( `${ name }, probe=${ label }`, () => { const applied = applyImageEdit( makeState(), edit ); if ( ! ( 'state' in applied ) ) { throw new Error( `Rejected as ${ applied.rejected }` ); } const exported = exportSourcePixel( applied.state as InternalImageEditorState, IMAGE, outputSizeFor( edit, IMAGE ), u, v ); const server = serverSourcePixel( edit, IMAGE, u, v ); expect( exported.x ).toBeCloseTo( server.x, 0 ); expect( exported.y ).toBeCloseTo( server.y, 0 ); } ); } } it( 'never applies a hand-built state as a different edit', () => { // Built by hand, so some frame a part of the image the editor would // not allow. In strict mode each must come back unchanged or be // rejected. const states = buildRows() .filter( ( { probeLabel } ) => probeLabel === 'center' ) .map( ( { state } ) => state ); const different: string[] = []; let applied = 0; let rejected = 0; for ( const state of states ) { const edit = getImageEdit( state ); if ( ! edit ) { continue; } const result = applyImageEdit( makeState(), edit, { strict: true, } ); if ( ! ( 'state' in result ) ) { rejected++; continue; } applied++; const asked = readEdit( edit ); const held = readEdit( getImageEdit( result.state ) ); const whole = { x: 0, y: 0, width: 1, height: 1 }; const askedCrop = asked.crop ?? whole; const heldCrop = held.crop ?? whole; const same = asked.flip.horizontal === held.flip.horizontal && asked.flip.vertical === held.flip.vertical && Math.abs( asked.rotation - held.rotation ) < 1e-6 && ( [ 'x', 'y', 'width', 'height' ] as const ).every( ( key ) => Math.abs( askedCrop[ key ] - heldCrop[ key ] ) <= 1e-3 ); if ( ! same ) { different.push( JSON.stringify( edit ) ); } } expect( different ).toEqual( [] ); expect( applied ).toBeGreaterThan( 0 ); expect( rejected ).toBeGreaterThan( 0 ); } ); } ); describe( 'getOutputSize agrees with getImageEdit', () => { // Big enough that a crop just inside the pruning tolerance is still // tens of pixels, which a smallest-size check would act on. const BIG = { src: 'big.jpg', naturalWidth: 20000, naturalHeight: 10000 }; it( 'reports the full size when the crop is too near full to be saved', () => { const state = makeState( { image: BIG, zoom: 1.0005 } ); expect( getImageEdit( state ) ).toEqual( {} ); expect( getOutputSize( state ) ).toEqual( { width: 20000, height: 10000, } ); } ); it( 'swaps the sides on a quarter turn', () => { const state = makeState( { image: BIG, rotation: 90 } ); expect( getOutputSize( state ) ).toEqual( { width: 10000, height: 20000, } ); } ); it( 'takes the crop out of the image at its rotation', () => { // Half the width and all the height of the quarter-turned image. const state = makeState( { image: BIG, rotation: 90, cropRect: { x: 0.25, y: 0, width: 0.5, height: 1 }, } ); expect( getOutputSize( state ) ).toEqual( { width: 5000, height: 20000, } ); } ); } ); // The pan and zoom that `applyImageEdit` works out leave floating-point // noise, such as a width of 1439.9999999999998 pixels. describe( 'reading an edit back removes floating-point noise', () => { function stateOf( edit: ImageEdit ) { const result = applyImageEdit( makeState(), edit ); if ( ! ( 'state' in result ) ) { throw new Error( `Rejected: ${ result.rejected }` ); } return result.state; } it( 'reads a crop of nine tenths as whole pixels', () => { const state = stateOf( { crop: { x: 0.05, y: 0.05, width: 0.9, height: 0.9 }, } ); expect( getOutputSize( state ) ).toEqual( { width: 1440, height: 810, } ); } ); it( 'reads a crop on the edge of the image as starting at 0', () => { const state = stateOf( { crop: { x: 0, y: 0, width: 0.5, height: 1 }, } ); expect( getImageEdit( state )?.crop ).toEqual( { x: 0, y: 0, width: 0.5, height: 1, } ); } ); it( 'keeps a size that is not near whole pixels', () => { const size = getOutputSize( stateOf( { rotation: 5 } ) ); expect( size?.width ).toBeCloseTo( 1389.9286, 3 ); expect( Number.isInteger( size?.width ) ).toBe( false ); } ); } ); describe( 'getImageEdit without a loaded image', () => { it.each( [ [ 'no image', null ], [ 'no usable size', { src: 'test.jpg', naturalWidth: Number.NaN, naturalHeight: 900 }, ], ] )( 'returns null for %s, whatever the edit', ( _, image ) => { const state = makeState( { image, rotation: 45 } ); expect( getImageEdit( state ) ).toBeNull(); expect( getOutputSize( state ) ).toBeNull(); } ); } );