|
| 1 | +// Geometry of the output frame: quarter turns, mirror, straighten angle and crop. |
| 2 | +// |
| 3 | +// Affine transforms are stored as [a, b, c, d, e, f]: |
| 4 | +// x' = a·x + c·y + e |
| 5 | +// y' = b·x + d·y + f |
| 6 | +// |
| 7 | +// The pipeline works in source space; the final pass maps every output pixel |
| 8 | +// back to the source with `toSource`. Normalised coordinates have their origin |
| 9 | +// at the top-left corner, y pointing down. |
| 10 | + |
| 11 | +export const IDENTITY = Object.freeze([1, 0, 0, 1, 0, 0]); |
| 12 | +const MIRROR = Object.freeze([-1, 0, 0, 1, 0, 0]); |
| 13 | +const QUARTER_TURNS = [ |
| 14 | + [1, 0, 0, 1, 0, 0], |
| 15 | + [0, 1, -1, 0, 0, 0], |
| 16 | + [-1, 0, 0, -1, 0, 0], |
| 17 | + [0, -1, 1, 0, 0, 0], |
| 18 | +]; |
| 19 | + |
| 20 | +export function multiply(m, n) { |
| 21 | + const [a1, b1, c1, d1, e1, f1] = m; |
| 22 | + const [a2, b2, c2, d2, e2, f2] = n; |
| 23 | + return [ |
| 24 | + a1 * a2 + c1 * b2, |
| 25 | + b1 * a2 + d1 * b2, |
| 26 | + a1 * c2 + c1 * d2, |
| 27 | + b1 * c2 + d1 * d2, |
| 28 | + a1 * e2 + c1 * f2 + e1, |
| 29 | + b1 * e2 + d1 * f2 + f1, |
| 30 | + ]; |
| 31 | +} |
| 32 | + |
| 33 | +export function invert([a, b, c, d, e, f]) { |
| 34 | + const det = a * d - b * c; |
| 35 | + return [d / det, -b / det, -c / det, a / det, (c * f - d * e) / det, (b * e - a * f) / det]; |
| 36 | +} |
| 37 | + |
| 38 | +export function apply([a, b, c, d, e, f], x, y) { |
| 39 | + return [a * x + c * y + e, b * x + d * y + f]; |
| 40 | +} |
| 41 | + |
| 42 | +export function rotation(radians) { |
| 43 | + const cos = Math.cos(radians); |
| 44 | + const sin = Math.sin(radians); |
| 45 | + return [cos, sin, -sin, cos, 0, 0]; |
| 46 | +} |
| 47 | + |
| 48 | +export function toMat3([a, b, c, d, e, f]) { |
| 49 | + return new Float32Array([a, b, 0, c, d, 0, e, f, 1]); |
| 50 | +} |
| 51 | + |
| 52 | +export function frameSize(width, height, rotate) { |
| 53 | + return rotate % 2 ? [height, width] : [width, height]; |
| 54 | +} |
| 55 | + |
| 56 | +// Zoom needed so that the straightened image still covers the whole frame. |
| 57 | +export function straightenScale(frameWidth, frameHeight, radians) { |
| 58 | + const cos = Math.abs(Math.cos(radians)); |
| 59 | + const sin = Math.abs(Math.sin(radians)); |
| 60 | + return Math.max(cos + (frameHeight / frameWidth) * sin, (frameWidth / frameHeight) * sin + cos); |
| 61 | +} |
| 62 | + |
| 63 | +export function outputGeometry(crop, width, height, { frame = false } = {}) { |
| 64 | + const [frameWidth, frameHeight] = frameSize(width, height, crop.rotate); |
| 65 | + const radians = (crop.angle * Math.PI) / 180; |
| 66 | + const scale = straightenScale(frameWidth, frameHeight, radians); |
| 67 | + const rect = frame ? { x: 0, y: 0, w: 1, h: 1 } : crop; |
| 68 | + |
| 69 | + const steps = [ |
| 70 | + [1 / width, 0, 0, 1 / height, 0.5, 0.5], |
| 71 | + multiply(crop.flip ? MIRROR : IDENTITY, QUARTER_TURNS[(4 - crop.rotate) % 4]), |
| 72 | + multiply([1 / scale, 0, 0, 1 / scale, 0, 0], rotation(-radians)), |
| 73 | + [frameWidth, 0, 0, frameHeight, -frameWidth / 2, -frameHeight / 2], |
| 74 | + [rect.w, 0, 0, rect.h, rect.x, rect.y], |
| 75 | + ]; |
| 76 | + const toSource = steps.reduce(multiply); |
| 77 | + |
| 78 | + return { |
| 79 | + width: rect.w * frameWidth, |
| 80 | + height: rect.h * frameHeight, |
| 81 | + frameWidth, |
| 82 | + frameHeight, |
| 83 | + scale, |
| 84 | + toSource, |
| 85 | + fromSource: invert(toSource), |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +export function rotateCrop(crop, direction) { |
| 90 | + const { x, y, w, h } = crop; |
| 91 | + const rect = direction > 0 |
| 92 | + ? { x: 1 - y - h, y: x, w: h, h: w } |
| 93 | + : { x: y, y: 1 - x - w, w: h, h: w }; |
| 94 | + return { |
| 95 | + ...crop, |
| 96 | + ...rect, |
| 97 | + rotate: (crop.rotate + (direction > 0 ? 1 : 3)) % 4, |
| 98 | + aspect: crop.aspect ? 1 / crop.aspect : null, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +export function mirrorCrop(crop) { |
| 103 | + return { |
| 104 | + ...crop, |
| 105 | + x: 1 - crop.x - crop.w, |
| 106 | + rotate: (4 - crop.rotate) % 4, |
| 107 | + flip: !crop.flip, |
| 108 | + angle: crop.angle ? -crop.angle : 0, |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +// Largest centred rectangle with the given pixel aspect ratio. |
| 113 | +export function fitAspect(crop, ratio, frameWidth, frameHeight) { |
| 114 | + if (!ratio) return { ...crop, aspect: null }; |
| 115 | + let w = 1; |
| 116 | + let h = 1; |
| 117 | + if (frameWidth / frameHeight > ratio) w = (ratio * frameHeight) / frameWidth; |
| 118 | + else h = frameWidth / (ratio * frameHeight); |
| 119 | + return { ...crop, x: (1 - w) / 2, y: (1 - h) / 2, w, h, aspect: ratio }; |
| 120 | +} |
| 121 | + |
| 122 | +const clamp = (v, min, max) => Math.min(max, Math.max(min, v)); |
| 123 | + |
| 124 | +export function moveCrop(rect, dx, dy) { |
| 125 | + return { |
| 126 | + ...rect, |
| 127 | + x: clamp(rect.x + dx, 0, 1 - rect.w), |
| 128 | + y: clamp(rect.y + dy, 0, 1 - rect.h), |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +// `handle` combines the letters n, s, e and w ("nw", "e"…). With a ratio the |
| 133 | +// opposite corner stays anchored and the pointer is projected on the diagonal. |
| 134 | +export function resizeCrop(rect, handle, px, py, options = {}) { |
| 135 | + const { ratio = null, frameWidth = 1, frameHeight = 1, min = 0.04 } = options; |
| 136 | + let x0 = rect.x; |
| 137 | + let y0 = rect.y; |
| 138 | + let x1 = rect.x + rect.w; |
| 139 | + let y1 = rect.y + rect.h; |
| 140 | + |
| 141 | + if (!ratio) { |
| 142 | + if (handle.includes('w')) x0 = clamp(px, 0, x1 - min); |
| 143 | + if (handle.includes('e')) x1 = clamp(px, x0 + min, 1); |
| 144 | + if (handle.includes('n')) y0 = clamp(py, 0, y1 - min); |
| 145 | + if (handle.includes('s')) y1 = clamp(py, y0 + min, 1); |
| 146 | + return { ...rect, x: x0, y: y0, w: x1 - x0, h: y1 - y0 }; |
| 147 | + } |
| 148 | + |
| 149 | + const r = (ratio * frameHeight) / frameWidth; |
| 150 | + const west = handle.includes('w'); |
| 151 | + const north = handle.includes('n'); |
| 152 | + const anchorX = west ? x1 : x0; |
| 153 | + const anchorY = north ? y1 : y0; |
| 154 | + const w = Math.abs(px - anchorX); |
| 155 | + const h = Math.abs(py - anchorY); |
| 156 | + let t = (w * r + h) / (r * r + 1); |
| 157 | + const maxW = west ? anchorX : 1 - anchorX; |
| 158 | + const maxH = north ? anchorY : 1 - anchorY; |
| 159 | + t = clamp(t, min, Math.min(maxH, maxW / r)); |
| 160 | + const nw = t * r; |
| 161 | + const nh = t; |
| 162 | + return { |
| 163 | + ...rect, |
| 164 | + x: west ? anchorX - nw : anchorX, |
| 165 | + y: north ? anchorY - nh : anchorY, |
| 166 | + w: nw, |
| 167 | + h: nh, |
| 168 | + }; |
| 169 | +} |
0 commit comments