From d3ae04db4e134a2366b529b142a563769a29dcc1 Mon Sep 17 00:00:00 2001 From: Chizoba Jude Ojini Date: Tue, 27 Oct 2026 21:24:34 +0000 Subject: [PATCH 1/3] feat: performance improvement changes --- AGENTS.md | 5 +- apps/web/src/ToolPage.tsx | 3 +- apps/web/src/runtime.ts | 37 ++++++++-- apps/web/src/worker.ts | 6 +- packages/core/src/core.test.ts | 38 +++++++++- packages/core/src/flate.ts | 34 +++++---- packages/core/src/index.ts | 3 +- packages/image-core/src/browser.ts | 36 ++++++++-- packages/image-core/src/filters.ts | 87 +++++++++++++++++++---- packages/image-core/src/ops.ts | 55 ++++++-------- packages/image-core/src/png.ts | 38 +++++++--- packages/tools/package.json | 1 + packages/tools/src/animated-gif/run.ts | 21 +++--- packages/tools/src/compress-image/run.ts | 10 ++- packages/tools/src/compress-pdf/images.ts | 21 +++--- packages/tools/src/compress-pdf/run.ts | 9 +-- packages/tools/src/image-common.test.ts | 21 ++++++ packages/tools/src/image-common.ts | 20 +++++- packages/tools/src/m4-tools.test.ts | 14 ++++ packages/tools/src/m7-tools.test.ts | 2 - packages/tools/src/m7b-tools.test.ts | 6 +- packages/tools/src/pdf-to-image/run.ts | 5 +- 22 files changed, 352 insertions(+), 120 deletions(-) create mode 100644 packages/tools/src/image-common.test.ts diff --git a/AGENTS.md b/AGENTS.md index 7205801..99b6125 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -125,7 +125,7 @@ To add a tool: manifest, run, panel, three registry entries, and tests. Reuse th ## Platform seams -- Image codec: `codec()` / `setCodec()` in `image-core`. The app's worker sets `appCodec` (`image-core/browser`): our own decoders and encoders for PNG, JPEG, GIF and BMP, and the browser (OffscreenCanvas) only for WebP and for files ours refuse with `UnsupportedImage`. Tests call `setCodec(ownCodec)`; WebP can't be tested in Node. Decoders and encoders are pure and synchronous (`decodeJpeg`, `encodeJpeg`, `decodeGif`, `encodeGif`, `decodeBmp`, `encodeBmp`, `decodePng`, `encodePng`) and are used directly by tools that need more than one picture out (GIF frames, JPEG metadata segments). +- Image codec: `codec()` / `setCodec()` in `image-core`. The app's worker sets `appCodec` (`image-core/browser`): the browser's native codec (OffscreenCanvas) where ours is many times slower (JPEG read and written, opaque PNG written, WebP), ours for PNG reading, see-through PNG writing (a canvas premultiplies alpha, so lossless would stop being lossless), GIF and BMP, and ours again whenever the browser fails. Native JPEG reading keeps samples as stored (`colorSpaceConversion: 'none'`) like ours; the browser's JPEG adds an sRGB profile, so Compress image keeps only its JFIF header and puts the file's own segments back. Tests call `setCodec(ownCodec)`; the native paths can't be tested in Node and are checked in Chrome. Decoders and encoders are pure and synchronous (`decodeJpeg`, `encodeJpeg`, `decodeGif`, `encodeGif`, `decodeBmp`, `encodeBmp`, `decodePng`, `encodePng`) and are used directly where the codec can't do the job (GIF frames, palette PNGs (PDF to image keeps ours for its pages: small files for text), grey JPEGs). Compress PDF reads colour JPEGs through `Platform.decodeJpeg` (the browser's, as the renderer draws them) and the rest with ours. - Text on pictures: the engine has no font code. Panels draw text to a PNG with the browser canvas (`bitmaps.ts`: `textMark`, `pictureMark`, `trimmedPng`; `meme/caption.ts`) and put it in the options; `run.ts` only decodes and places it with `overlay`. Keep it that way; a Node test passes a ready-made PNG. `blur-image` is the one panel that imports engine functions (`apply.ts`): it shows the same operations on a 1400 px copy so the preview matches the saved file. - Flate: `deflate` / `inflate` in `core` use `CompressionStream` (async); `inflate` keeps its output when bytes follow the end of the data (a stream Length that counts a line break), which the platform stream rejects. - Renderer `Platform`: `imageFromRGBA` and `decodeJpeg`. Tests pass fakes. @@ -137,7 +137,7 @@ To add a tool: manifest, run, panel, three registry entries, and tests. Reuse th - `ToolPage.tsx`: files | preview | options at `xl`; at `lg` the options stay on the right with the files above the preview; stacked below that, options before the preview. Options are always the right-hand region. Tools with their own surface (crop, sign) set `placesActions` and render the `actions` prop (run button, progress, errors) beside their controls. A finished run opens a modal `` (download, keep editing, start over, use in another tool), not a card under the options. Locked PDFs open a password dialog and are replaced by a decrypted in-memory copy; the password is never stored. - `previewController.ts` (pure, fake-timer tested) runs the same engine as the Run button: **600 ms debounce**, cancels stale runs, keeps the last result on screen. `ready` states have `stale` (outdated; Run never reuses a stale result) and `computing` (dim and "Updating..."). Do not make the UI react per keystroke. - `previewWorker.ts` / `previewClient.ts`: one worker for inspect, render, unlock and metadata. The main thread never parses a PDF. Page canvases keep the old picture until the new one is ready (no flashing). -- Tool runs go through `runtime.ts` and `worker.ts` (progress, cancel, ZIP for several outputs). Limits: 50 files, 200 MB per file, 500 MB total. +- Tool runs go through `runtime.ts` and `worker.ts` (progress, cancel, ZIP for several outputs). Limits: 50 files, 200 MB per file, 500 MB total. One tool worker is kept warm between runs (`warmTool` loads the engine when a tool page opens; a run that starts while it's busy gets its own worker; cancelling terminates it). Each input carries a `key` (the same `File`, the same key), and `decodeFile` keeps decoded pictures under it (up to 512 MB, only for the current inputs), so a preview after an option change doesn't decode again. Run functions get a copy: they may change their pixels. - Office files preview as PDFs: `previewClient` sends the kind with the bytes (`officeKind` in `usePdfInfo.ts`, kept free of engine code), `previewWorker` converts once with `officeToPdf` from `@ream/tools/office-preview` and then inspects and draws it like a PDF, adding an "approximate" warning. `isPreviewable` (PDF or Office) decides what `Preview` and `Thumb` show. - Zoom: `ZoomBar`, `useZoom`, `zoomedWidth`, `renderWidthFor` and `useWidth` live in `@ream/ui` (`Zoom.tsx`). The Original/Result pane starts at fit width, with its `ZoomBar` (Fit width and Fit page) on the tabs row: `Preview` owns the zoom and `PdfView` reads it from `ZoomContext` and reports the size shown (no bar for pictures or text); panels that work on one page (Crop PDF, Sign PDF, Redact PDF, Fill PDF form) use `usePageZoom` (`tools/src/PageZoom.tsx`), which starts with the whole page in view and also offers Fit width. A zoom is a share of the actual size (a point is 4/3 CSS pixels), the page box is sized in CSS pixels inside a scrolling area, and everything drawn over it is positioned in fractions, so overlays scale with it. Pages are re-rendered at the shown size (in 200 px steps, at most 4000). - Edit PDF (`tools/src/edit-pdf`): @@ -195,6 +195,7 @@ To add a tool: manifest, run, panel, three registry entries, and tests. Reuse th - Match the surrounding code: comment density, naming and idiom. Comments explain *why* (limits, gotchas, reasons), not what. - Mark deliberate shortcuts that have a known ceiling with a `ponytail:` comment naming the ceiling and the upgrade path. - Small files, one folder per tool, no abstractions without a second user, no config for values that never change. +- Pixel loops are hot (a phone photo is 12 to 50 million pixels): no allocation per pixel (`data.set(src.subarray(i, i + 4))`, array literals, destructuring), write the four bytes; walk memory in row order (a column at a time misses the cache); compare against the old code's output when speeding something up. Measure with a 12 MP photo and a few hundred pages before and after. - Prefer the platform (CSS, native elements) over code, and the standard library over dependencies. Do not add runtime dependencies to engine packages. UI-only asset packages (fonts) are fine. Ream is MIT: anything shipped must be MIT-compatible (no GPL, AGPL or LGPL), and adding, removing or updating a runtime dependency means running `node scripts/notices.ts`. Code copied in by hand (like Lucide's shapes) keeps its notice in the file and gets an entry in `scripts/notices.ts`. - Do not put personal data in URLs, logs or tests. diff --git a/apps/web/src/ToolPage.tsx b/apps/web/src/ToolPage.tsx index 77d95d2..8006b0a 100644 --- a/apps/web/src/ToolPage.tsx +++ b/apps/web/src/ToolPage.tsx @@ -3,7 +3,7 @@ import { Button, Card, Dropzone, Icon } from '@ream/ui' import { UserError, acceptsFile, formatBytes, type FileData, type Tool } from '@ream/core' import { tools } from '@ream/tools' import { panels, type PanelModule } from '@ream/tools/panels' -import { runTool, type Running } from './runtime' +import { runTool, warmTool, type Running } from './runtime' import { download, toFile } from './download' import { clearHandoff, peekHandoff, setHandoff } from './handoff' import { FileMeta, Thumb } from './Thumb' @@ -77,6 +77,7 @@ export function ToolPage({ tool }: { tool?: Tool }) { useEffect(() => { clearHandoff() if (!tool || tool.status === 'soon' || !panels[tool.id]) return + warmTool(tool.id) let live = true panels[tool.id]().then((m) => { if (!live) return diff --git a/apps/web/src/runtime.ts b/apps/web/src/runtime.ts index 0f79720..ed1a6b6 100644 --- a/apps/web/src/runtime.ts +++ b/apps/web/src/runtime.ts @@ -2,32 +2,57 @@ import { UserError, checkLimits, type FileData } from '@ream/core' export type Running = { promise: Promise; cancel: () => void } -/** Run a tool's engine in a fresh Web Worker. Cancelling terminates the worker and rejects with an AbortError. */ +const spawn = () => new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }) + +// One worker is kept between runs, so a preview after an option change doesn't load the engine again and finds the +// files it decoded last time (see `keyed` in worker.ts). A run that starts while it is busy gets a worker of its own. +let idle: Worker | undefined + +/** start the worker and load a tool's engine before the first run needs them (a tool page calls it on open) */ +export function warmTool(id: string) { + idle ??= spawn() + idle.postMessage({ warm: id }) +} + +// the same File keeps its key across runs, so the worker can tell it already has it +const keys = new WeakMap() +let nextKey = 0 +const keyOf = (f: Blob) => keys.get(f) ?? (keys.set(f, String(++nextKey)), keys.get(f)!) + +/** Run a tool's engine in a Web Worker. Cancelling terminates the worker (the only way to stop work in progress) and rejects with an AbortError. */ export function runTool(id: string, files: File[], options: unknown, onProgress: (fraction: number) => void): Running { - const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }) + const worker = idle ?? spawn() + idle = undefined + let finished = false + const finish = (reuse: boolean) => { + finished = true + if (reuse && !idle) idle = worker + else worker.terminate() + } let reject: (e: unknown) => void = () => {} const promise = new Promise((resolve, rej) => { reject = rej worker.onmessage = (e) => { const m = e.data if (m.type === 'progress') return onProgress(m.value) - worker.terminate() + finish(true) if (m.type === 'done') resolve(m.outputs) else rej(m.user ? new UserError(m.message) : new Error(m.message)) } worker.onerror = (e) => { - worker.terminate() + finish(false) rej(new Error(e.message || "Couldn't start the background worker")) } checkLimits(files) // throws inside the executor, which rejects the promise - Promise.all(files.map(async (f) => ({ name: f.name, type: f.type, bytes: new Uint8Array(await f.arrayBuffer()) }))) + Promise.all(files.map(async (f) => ({ name: f.name, type: f.type, key: keyOf(f), bytes: new Uint8Array(await f.arrayBuffer()) }))) .then((inputs) => worker.postMessage({ id, files: inputs, options }, inputs.map((i) => i.bytes.buffer))) .catch(rej) }) return { promise, cancel() { - worker.terminate() + if (finished) return + finish(false) reject(new DOMException('Cancelled', 'AbortError')) }, } diff --git a/apps/web/src/worker.ts b/apps/web/src/worker.ts index 927cb37..47f2d65 100644 --- a/apps/web/src/worker.ts +++ b/apps/web/src/worker.ts @@ -1,18 +1,22 @@ import { UserError, type FileData } from '@ream/core' import { setCodec } from '@ream/image-core' import { appCodec } from '@ream/image-core/browser' +import { keepDecoded } from '@ream/tools/image-common' import { runners } from '@ream/tools/runners' import { installStandardFonts } from '@ream/tools/standard-fonts' setCodec(appCodec) installStandardFonts() -type Request = { id: string; files: FileData[]; options: unknown } +type Request = { id: string; files: FileData[]; options: unknown } | { warm: string } const post = (message: unknown, transfer: Transferable[] = []) => (self as unknown as Worker).postMessage(message, transfer) self.onmessage = async (e: MessageEvent) => { + // a tool page opened: load its engine now, quietly (runtime.ts expects no answer) + if ('warm' in e.data) return void runners[e.data.warm]?.().catch(() => {}) const { id, files, options } = e.data + keepDecoded(files.map((f) => f.key)) try { const load = runners[id] if (!load) throw new UserError("That tool isn't available yet.") diff --git a/packages/core/src/core.test.ts b/packages/core/src/core.test.ts index 6df9683..e9802de 100644 --- a/packages/core/src/core.test.ts +++ b/packages/core/src/core.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest' -import { UserError, acceptsFile, checkLimits, crc32, formatBytes, unzip, zip, zipPacked } from './index.ts' +import { UserError, acceptsFile, checkLimits, crc32, formatBytes, pngPredictor, unzip, zip, zipPacked } from './index.ts' const enc = (s: string) => new TextEncoder().encode(s) @@ -85,3 +85,39 @@ describe('unzip', () => { await expect(unzip(packed)).rejects.toBeInstanceOf(UserError) }) }) + +test('pngPredictor matches the PNG definition for every filter, pixel width and bit depth, the first row included', () => { + // the definition, byte by byte (PNG spec section 9) + const reference = (data: Uint8Array, columns: number, colors: number, bpc: number) => { + const bpp = Math.max(1, (colors * bpc + 7) >> 3) + const row = (colors * bpc * columns + 7) >> 3 + const rows = Math.floor(data.length / (row + 1)) + const out = new Uint8Array(rows * row) + for (let r = 0; r < rows; r++) { + const ft = data[r * (row + 1)] + for (let i = 0; i < row; i++) { + const x = data[r * (row + 1) + 1 + i] + const a = i >= bpp ? out[r * row + i - bpp] : 0 + const b = r > 0 ? out[(r - 1) * row + i] : 0 + const c = i >= bpp && r > 0 ? out[(r - 1) * row + i - bpp] : 0 + const p = a + b - c + const paeth = Math.abs(p - a) <= Math.abs(p - b) && Math.abs(p - a) <= Math.abs(p - c) ? a : Math.abs(p - b) <= Math.abs(p - c) ? b : c + out[r * row + i] = x + [0, a, b, (a + b) >> 1, paeth][ft > 4 ? 0 : ft] + } + } + return out + } + let seed = 7 + const rand = () => (seed = (seed * 1103515245 + 12345) & 0x7fffffff) >> 16 + for (const [colors, bpc] of [[1, 8], [3, 8], [4, 8], [1, 1], [2, 16], [4, 16]]) { + const columns = 13 + const row = (colors * bpc * columns + 7) >> 3 + const data = new Uint8Array(12 * (row + 1)).map(() => rand() & 255) + // every filter type on the first row and below, and one unknown type + for (let r = 0; r < 12; r++) data[r * (row + 1)] = r % 6 === 5 ? 9 : r % 6 + for (const first of [0, 1, 2, 3, 4, 9]) { + data[0] = first + expect(Array.from(pngPredictor(data, columns, colors, bpc))).toEqual(Array.from(reference(data, columns, colors, bpc))) + } + } +}) diff --git a/packages/core/src/flate.ts b/packages/core/src/flate.ts index c5a77eb..5d93f6d 100644 --- a/packages/core/src/flate.ts +++ b/packages/core/src/flate.ts @@ -58,27 +58,37 @@ export function pngPredictor(data: Uint8Array, columns: number, colors = 1, bpc const row = (colors * bpc * columns + 7) >> 3 const rows = Math.floor(data.length / (row + 1)) const out = new Uint8Array(rows * row) + // one loop per filter type: deciding per byte was most of the cost of reading a big PNG or a predicted PDF stream for (let r = 0; r < rows; r++) { const ft = data[r * (row + 1)] const src = r * (row + 1) + 1 const dst = r * row - for (let i = 0; i < row; i++) { - const x = data[src + i] - const a = i >= bpp ? out[dst + i - bpp] : 0 - const b = r > 0 ? out[dst - row + i] : 0 - const c = i >= bpp && r > 0 ? out[dst - row + i - bpp] : 0 - let v = x - if (ft === 1) v = x + a - else if (ft === 2) v = x + b - else if (ft === 3) v = x + ((a + b) >> 1) - else if (ft === 4) { + const up = dst - row // the row above, when there is one (r > 0) + if (ft === 1) { + for (let i = 0; i < row; i++) out[dst + i] = data[src + i] + (i >= bpp ? out[dst + i - bpp] : 0) + } else if (ft === 2 && r > 0) { + for (let i = 0; i < row; i++) out[dst + i] = data[src + i] + out[up + i] + } else if (ft === 3) { + for (let i = 0; i < row; i++) out[dst + i] = data[src + i] + (((i >= bpp ? out[dst + i - bpp] : 0) + (r > 0 ? out[up + i] : 0)) >> 1) + } else if (ft === 4 && r === 0) { + // Paeth with nothing above always picks the left neighbour (Sub) + for (let i = 0; i < row; i++) out[dst + i] = data[src + i] + (i >= bpp ? out[dst + i - bpp] : 0) + } else if (ft === 4) { + // with nothing to the left it picks the byte above (Up) + for (let i = 0; i < bpp && i < row; i++) out[dst + i] = data[src + i] + out[up + i] + for (let i = bpp; i < row; i++) { + const a = out[dst + i - bpp] + const b = out[up + i] + const c = out[up + i - bpp] const p = a + b - c const pa = Math.abs(p - a) const pb = Math.abs(p - b) const pc = Math.abs(p - c) - v = x + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c) + out[dst + i] = data[src + i] + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c) } - out[dst + i] = v & 255 + } else { + // none, "up" on the first row, and unknown types: the bytes as they are + out.set(data.subarray(src, src + row), dst) } } return out diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 420cf04..4b4b718 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -28,7 +28,8 @@ export type Tool = { status: 'ready' | 'soon' } -export type FileData = { name: string; type: string; bytes: Uint8Array } +/** `key` names the same input across runs (the app's worker keeps decoded pictures under it); absent in tests and outputs */ +export type FileData = { name: string; type: string; bytes: Uint8Array; key?: string } export type RunContext = { progress: (fraction: number) => void } diff --git a/packages/image-core/src/browser.ts b/packages/image-core/src/browser.ts index b5490a8..e94cc5c 100644 --- a/packages/image-core/src/browser.ts +++ b/packages/image-core/src/browser.ts @@ -1,5 +1,5 @@ import { UserError } from '@ream/core' -import { UnsupportedImage, checkPixels } from './buffer.ts' +import { UnsupportedImage, checkPixels, type ImageBuffer } from './buffer.ts' import type { Codec, OutputMime } from './codec.ts' import { ownCodec } from './own.ts' @@ -10,7 +10,8 @@ export const browserCodec: Codec = { async decode(bytes, type) { let bitmap: ImageBitmap try { - bitmap = await createImageBitmap(new Blob([bytes as BlobPart], { type })) + // samples as stored, like our decoder (a colour profile is carried over by the tools that keep it, not applied); turned upright by EXIF + bitmap = await createImageBitmap(new Blob([bytes as BlobPart], { type }), { colorSpaceConversion: 'none', imageOrientation: 'from-image' }) } catch { throw new UserError("Couldn't read this image. It may be damaged, or this browser can't open that kind of file.") } @@ -35,12 +36,27 @@ export const browserCodec: Codec = { }, } +const isJpeg = (b: Uint8Array) => b[0] === 0xff && b[1] === 0xd8 +const opaque = (img: ImageBuffer) => { + for (let i = 3; i < img.data.length; i += 4) if (img.data[i] !== 255) return false + return true +} + /** - * What the app uses: our own codec for everything it can do, the browser only for WebP and for files that use something - * ours doesn't (when the browser can't read them either, the more precise message of ours is shown). + * What the app uses: the browser's native codec where it is many times faster than ours (JPEG both ways, PNG writing), + * ours for everything else and whenever the browser fails. A canvas stores premultiplied alpha, so it can't keep + * see-through pixels exact: PNGs with transparency, and every PNG read, stay with ours (lossless stays lossless). + * GIF and BMP are ours only: browsers can't write them. */ export const appCodec: Codec = { async decode(bytes, type) { + if (isJpeg(bytes)) { + try { + return await browserCodec.decode(bytes, 'image/jpeg') + } catch { + // ours gives the precise reason, or reads what the browser couldn't + } + } try { return await ownCodec.decode(bytes, type) } catch (e) { @@ -52,5 +68,15 @@ export const appCodec: Codec = { } } }, - encode: (img, type, quality) => (type === 'image/webp' ? browserCodec : ownCodec).encode(img, type, quality), + async encode(img, type, quality) { + if (type === 'image/webp') return browserCodec.encode(img, type, quality) + if (type === 'image/jpeg' || (type === 'image/png' && opaque(img))) { + try { + return await browserCodec.encode(img, type, quality) + } catch { + // too big for a canvas here, or out of memory: ours can still do it + } + } + return ownCodec.encode(img, type, quality) + }, } diff --git a/packages/image-core/src/filters.ts b/packages/image-core/src/filters.ts index ea4e119..fec2d99 100644 --- a/packages/image-core/src/filters.ts +++ b/packages/image-core/src/filters.ts @@ -15,15 +15,52 @@ function within(img: ImageBuffer, a?: Area): Area | null { // ---------- blur: three box passes per direction, close to a Gaussian of the same radius ---------- -/** one box pass along a line of `n` pixels starting at `start`, `step` apart in the data; edges repeat their last pixel */ -function boxLine(d: Uint8ClampedArray, start: number, step: number, n: number, r: number, line: Float32Array) { - for (let c = 0; c < 4; c++) { - for (let i = 0; i < n; i++) line[i] = d[start + i * step + c] +/** what `sum / div` becomes when stored in the picture, for every sum a box of bytes can have (the same rounding, no division per pixel) */ +function quotients(div: number): Uint8ClampedArray { + const q = new Uint8ClampedArray(255 * div + 1) + for (let s = 0; s < q.length; s++) q[s] = s / div + return q +} + +/** + * one box pass along a row of `n` pixels starting at `start`, 4 bytes apart; edges repeat their last pixel. `channels` 3 + * leaves alpha alone (an opaque picture stays opaque). `line` holds the row's old values while it is rewritten. + */ +function boxRow(d: Uint8ClampedArray, start: number, n: number, r: number, channels: number, q: Uint8ClampedArray, line: Uint8Array) { + line.set(d.subarray(start, start + n * 4)) + const last = (n - 1) * 4 + for (let c = 0; c < channels; c++) { let sum = 0 - for (let k = -r; k <= r; k++) sum += line[Math.min(n - 1, Math.max(0, k))] - for (let i = 0; i < n; i++) { - d[start + i * step + c] = sum / (2 * r + 1) - sum += line[Math.min(n - 1, i + r + 1)] - line[Math.max(0, i - r)] + for (let k = -r; k <= r; k++) sum += line[(k < 0 ? 0 : k >= n ? n - 1 : k) * 4 + c] + for (let i = 0, p = start + c; i < n; i++, p += 4) { + d[p] = q[sum] + const add = i + r + 1 + const sub = i - r + sum += line[add < n ? add * 4 + c : last + c] - line[sub > 0 ? sub * 4 + c : c] + } + } +} + +/** + * the same pass down every column of the area at once, row by row (in memory order: walking one column at a time + * misses the cache on every pixel). `src` is a copy of the area's rows, the values before this pass. + */ +function boxColumns(d: Uint8ClampedArray, at: number, row: number, w: number, h: number, r: number, channels: number, q: Uint8ClampedArray, src: Uint8Array, sums: Int32Array) { + const span = w * 4 + sums.fill(0) + for (let k = -r; k <= r; k++) { + const from = (k < 0 ? 0 : k >= h ? h - 1 : k) * span + for (let j = 0; j < span; j++) sums[j] += src[from + j] + } + for (let i = 0; i < h; i++) { + const add = (i + r + 1 < h ? i + r + 1 : h - 1) * span + const sub = (i - r > 0 ? i - r : 0) * span + const out = at + i * row + for (let j = 0; j < span; j += 4) { + for (let c = 0; c < channels; c++) { + d[out + j + c] = q[sums[j + c]] + sums[j + c] += src[add + j + c] - src[sub + j + c] + } } } } @@ -38,7 +75,7 @@ export function blur(img: ImageBuffer, radius: number, area?: Area): void { const at = (x: number, y: number) => (y * img.width + x) * 4 // colour is averaged weighted by alpha, so see-through pixels (often black underneath) don't darken their neighbours let translucent = false - for (let y = a.y; y < a.y + a.h && !translucent; y++) for (let x = a.x; x < a.x + a.w; x++) if (d[at(x, y) + 3] < 255) { translucent = true; break } + for (let y = a.y; y < a.y + a.h && !translucent; y++) for (let i = at(a.x, y), end = i + a.w * 4; i < end; i += 4) if (d[i + 3] < 255) { translucent = true; break } if (translucent) { for (let y = a.y; y < a.y + a.h; y++) for (let x = a.x; x < a.x + a.w; x++) { const i = at(x, y) @@ -46,10 +83,15 @@ export function blur(img: ImageBuffer, radius: number, area?: Area): void { d[i] *= k; d[i + 1] *= k; d[i + 2] *= k } } - const line = new Float32Array(Math.max(a.w, a.h)) + const q = quotients(2 * r + 1) + const line = new Uint8Array(a.w * 4) + const rows = new Uint8Array(a.w * 4 * a.h) + const sums = new Int32Array(a.w * 4) + const channels = translucent ? 4 : 3 for (let pass = 0; pass < 3; pass++) { - for (let y = a.y; y < a.y + a.h; y++) boxLine(d, at(a.x, y), 4, a.w, r, line) - for (let x = a.x; x < a.x + a.w; x++) boxLine(d, at(x, a.y), row, a.h, r, line) + for (let y = a.y; y < a.y + a.h; y++) boxRow(d, at(a.x, y), a.w, r, channels, q, line) + for (let y = 0; y < a.h; y++) rows.set(d.subarray(at(a.x, a.y + y), at(a.x + a.w, a.y + y)), y * a.w * 4) + boxColumns(d, at(a.x, a.y), row, a.w, a.h, r, channels, q, rows, sums) } if (translucent) { for (let y = a.y; y < a.y + a.h; y++) for (let x = a.x; x < a.x + a.w; x++) { @@ -77,8 +119,15 @@ export function pixelate(img: ImageBuffer, block: number, area?: Area): void { r += d[i] * k; g += d[i + 1] * k; bl += d[i + 2] * k; al += k } const n = (x1 - bx) * (y1 - by) - const avg = [al ? r / al : 0, al ? g / al : 0, al ? bl / al : 0, al / n] - for (let y = by; y < y1; y++) for (let x = bx; x < x1; x++) d.set(avg, (y * img.width + x) * 4) + const [ar, ag, ab, aa] = [al ? r / al : 0, al ? g / al : 0, al ? bl / al : 0, al / n] + for (let y = by; y < y1; y++) { + for (let i = (y * img.width + bx) * 4, end = (y * img.width + x1) * 4; i < end; i += 4) { + d[i] = ar + d[i + 1] = ag + d[i + 2] = ab + d[i + 3] = aa + } + } } } } @@ -87,7 +136,15 @@ export function pixelate(img: ImageBuffer, block: number, area?: Area): void { export function fill(img: ImageBuffer, rgb: [number, number, number], area?: Area): void { const a = within(img, area) if (!a) return - for (let y = a.y; y < a.y + a.h; y++) for (let x = a.x; x < a.x + a.w; x++) img.data.set([rgb[0], rgb[1], rgb[2], 255], (y * img.width + x) * 4) + const d = img.data + for (let y = a.y; y < a.y + a.h; y++) { + for (let i = (y * img.width + a.x) * 4, end = i + a.w * 4; i < end; i += 4) { + d[i] = rgb[0] + d[i + 1] = rgb[1] + d[i + 2] = rgb[2] + d[i + 3] = 255 + } + } } // ---------- colour ---------- diff --git a/packages/image-core/src/ops.ts b/packages/image-core/src/ops.ts index 96af950..273409a 100644 --- a/packages/image-core/src/ops.ts +++ b/packages/image-core/src/ops.ts @@ -1,5 +1,6 @@ import { UserError } from '@ream/core' import { checkPixels, createImage, type ImageBuffer } from './buffer.ts' +import { overlay } from './compose.ts' const copy = (img: ImageBuffer): ImageBuffer => ({ width: img.width, height: img.height, data: img.data.slice() }) @@ -120,15 +121,16 @@ export function rotate(img: ImageBuffer, turns: number): ImageBuffer { if (t === 0) return copy(img) const { width: w, height: h, data } = img const out = t === 2 ? createImage(w, h) : createImage(h, w) + // each source row lands as a column (or a reversed row) of the result: its first pixel's place and the step between them + const step = t === 1 ? h * 4 : t === 2 ? -4 : -h * 4 + const o = out.data for (let y = 0; y < h; y++) { - for (let x = 0; x < w; x++) { - const [dx, dy] = t === 1 ? [h - 1 - y, x] : t === 2 ? [w - 1 - x, h - 1 - y] : [y, w - 1 - x] - const s = (y * w + x) * 4 - const d = (dy * out.width + dx) * 4 - out.data[d] = data[s] - out.data[d + 1] = data[s + 1] - out.data[d + 2] = data[s + 2] - out.data[d + 3] = data[s + 3] + let d = (t === 1 ? h - 1 - y : t === 2 ? (h - 1 - y) * w + w - 1 : (w - 1) * h + y) * 4 + for (let s = y * w * 4, end = s + w * 4; s < end; s += 4, d += step) { + o[d] = data[s] + o[d + 1] = data[s + 1] + o[d + 2] = data[s + 2] + o[d + 3] = data[s + 3] } } return out @@ -160,10 +162,16 @@ export function flip(img: ImageBuffer, axis: 'horizontal' | 'vertical'): ImageBu const { width: w, height: h, data } = img const out = createImage(w, h) for (let y = 0; y < h; y++) { - for (let x = 0; x < w; x++) { - const s = (y * w + x) * 4 - const d = (axis === 'horizontal' ? y * w + (w - 1 - x) : (h - 1 - y) * w + x) * 4 - out.data.set(data.subarray(s, s + 4), d) + const row = y * w * 4 + if (axis === 'vertical') { + out.data.set(data.subarray(row, row + w * 4), (h - 1 - y) * w * 4) + continue + } + for (let s = row, d = row + (w - 1) * 4; s < row + w * 4; s += 4, d -= 4) { + out.data[d] = data[s] + out.data[d + 1] = data[s + 1] + out.data[d + 2] = data[s + 2] + out.data[d + 3] = data[s + 3] } } return out @@ -182,27 +190,6 @@ export function flattenOnWhite(img: ImageBuffer): ImageBuffer { return out } -/** source-over blend of src onto dst at (ox, oy) */ -function blit(dst: ImageBuffer, src: ImageBuffer, ox: number, oy: number) { - for (let y = 0; y < src.height; y++) { - for (let x = 0; x < src.width; x++) { - const s = (y * src.width + x) * 4 - const sa = src.data[s + 3] - if (sa === 0) continue - const d = ((oy + y) * dst.width + ox + x) * 4 - if (sa === 255) { - dst.data.set(src.data.subarray(s, s + 4), d) - continue - } - const a = sa / 255 - const b = dst.data[d + 3] / 255 - const oa = a + b * (1 - a) - for (let c = 0; c < 3; c++) dst.data[d + c] = (src.data[s + c] * a + dst.data[d + c] * b * (1 - a)) / oa - dst.data[d + 3] = oa * 255 - } - } -} - export type StitchOptions = { direction: 'horizontal' | 'vertical' | 'grid' /** grid only */ @@ -266,6 +253,6 @@ export function stitch(images: ImageBuffer[], o: StitchOptions): ImageBuffer { const out = createImage(width, height) if (o.background === 'white') out.data.fill(255) - for (const p of placed) blit(out, p.img, p.x, p.y) + for (const p of placed) overlay(out, p.img, p.x, p.y) return out } diff --git a/packages/image-core/src/png.ts b/packages/image-core/src/png.ts index 373604d..c73417e 100644 --- a/packages/image-core/src/png.ts +++ b/packages/image-core/src/png.ts @@ -56,6 +56,17 @@ export async function decodePng(bytes: Uint8Array): Promise { const rowBytes = (channels * depth * pw + 7) >> 3 if (raw.length < off + ph * (rowBytes + 1)) throw new UserError('This PNG is damaged.') const px = pngPredictor(raw.subarray(off, off + ph * (rowBytes + 1)), pw, channels, depth) + // the common photos and screenshots, straight across (no interlacing: the place of a pixel is its index) + if (!hdr.interlace && depth === 8 && color === 6) return out.data.set(px) + if (!hdr.interlace && depth === 8 && color === 2 && !trns) { + for (let i = 0, o = 0; i < px.length; i += 3, o += 4) { + out.data[o] = px[i] + out.data[o + 1] = px[i + 1] + out.data[o + 2] = px[i + 2] + out.data[o + 3] = 255 + } + return + } for (let y = 0; y < ph; y++) { const row = y * rowBytes // one sample (0 .. 2^depth-1; 16-bit keeps its high byte) @@ -78,12 +89,11 @@ export async function decodePng(bytes: Uint8Array): Promise { // ponytail: colour-key transparency (tRNS) is honoured for depths up to 8 only else if (trns && depth < 16 && s === trns[1]) a = 0 } else { - const s = [sample(x * channels), sample(x * channels + 1), sample(x * channels + 2)] - r = s[0] - g = s[1] - b = s[2] + r = sample(x * channels) + g = sample(x * channels + 1) + b = sample(x * channels + 2) if (color === 6) a = sample(x * 4 + 3) - else if (trns && depth === 8 && s[0] === trns[1] && s[1] === trns[3] && s[2] === trns[5]) a = 0 + else if (trns && depth === 8 && r === trns[1] && g === trns[3] && b === trns[5]) a = 0 } out.data[o] = r out.data[o + 1] = g @@ -184,7 +194,12 @@ function layoutOf(img: ImageBuffer, optimize: boolean, allowPalette = true): Lay // up to 256 distinct colours are kept, as r, g, b, a packed into one number const index = new Map() let many = false + // neighbours mostly share a colour (pages, screenshots): the map is only asked when it changes + let last = -1 + // a pixel the same as the one before tells nothing new (a 32-bit view compares it in one go, when the bytes are aligned) + const same = data.byteOffset % 4 === 0 ? new Uint32Array(data.buffer, data.byteOffset, n) : null for (let i = 0; i < n; i++) { + if (same && i && same[i] === same[i - 1]) continue const r = data[i * 4] const g = data[i * 4 + 1] const b = data[i * 4 + 2] @@ -193,11 +208,12 @@ function layoutOf(img: ImageBuffer, optimize: boolean, allowPalette = true): Lay if (r !== g || r !== b) grey = false if (!many) { const key = ((r << 24) | (g << 16) | (b << 8) | a) >>> 0 - if (!index.has(key)) { + if (key !== last && !index.has(key)) { if (index.size === 256) many = true else index.set(key, index.size) } - } + last = key + } else if (alpha && !grey) break // nothing left to learn } // a palette pays off up to 256 colours, except for plain grey with many levels (grey8 has no palette to store) @@ -219,10 +235,16 @@ function layoutOf(img: ImageBuffer, optimize: boolean, allowPalette = true): Lay const depth = entries.length <= 2 ? 1 : entries.length <= 4 ? 2 : entries.length <= 16 ? 4 : 8 const stride = (w * depth + 7) >> 3 const pixels = new Uint8Array(stride * h) + let lastKey = -1 + let k = 0 for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = (y * w + x) * 4 - const k = slot.get((((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]) >>> 0))! + const key = ((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]) >>> 0 + if (key !== lastKey) { + k = slot.get(key)! + lastKey = key + } if (depth === 8) pixels[y * stride + x] = k else pixels[y * stride + ((x * depth) >> 3)] |= k << (8 - depth - ((x * depth) & 7)) } diff --git a/packages/tools/package.json b/packages/tools/package.json index 2c1b8e6..2ccc356 100644 --- a/packages/tools/package.json +++ b/packages/tools/package.json @@ -7,6 +7,7 @@ "exports": { ".": "./src/index.ts", "./runners": "./src/runners.ts", + "./image-common": "./src/image-common.ts", "./panels": "./src/panels.ts", "./office-preview": "./src/office-preview.ts", "./standard-fonts": "./src/fonts/standard-browser.ts" diff --git a/packages/tools/src/animated-gif/run.ts b/packages/tools/src/animated-gif/run.ts index 3595bc4..d519879 100644 --- a/packages/tools/src/animated-gif/run.ts +++ b/packages/tools/src/animated-gif/run.ts @@ -1,5 +1,5 @@ import { UserError, type FileData, type RunFn } from '@ream/core' -import { createImage, crop, encodeGif, resize, type ImageBuffer } from '@ream/image-core' +import { createImage, crop, encodeGif, overlay, resize, type ImageBuffer } from '@ream/image-core' import { decodeFile } from '../image-common.ts' import { MAX_SIDE, type AnimatedGifOptions } from './options.ts' @@ -9,8 +9,6 @@ const MAX_FRAMES = 300 // every frame is held as full-colour pixels while the animation is built const MAX_PIXELS = 160_000_000 -const BACKGROUND = { white: [255, 255, 255, 255], black: [0, 0, 0, 255], transparent: [0, 0, 0, 0] } - /** one picture made into a w x h frame the way `fit` says */ function frame(img: ImageBuffer, w: number, h: number, fit: AnimatedGifOptions['fit'], background: AnimatedGifOptions['background']): ImageBuffer { if (img.width === w && img.height === h) return img @@ -22,19 +20,16 @@ function frame(img: ImageBuffer, w: number, h: number, fit: AnimatedGifOptions[' const scaled = sw === img.width && sh === img.height ? img : resize(img, sw, sh) if (fit === 'cover') return crop(scaled, (sw - w) >> 1, (sh - h) >> 1, w, h) const out = createImage(w, h) - const bg = BACKGROUND[background] - for (let i = 0; i < out.data.length; i += 4) out.data.set(bg, i) const ox = (w - sw) >> 1 const oy = (h - sh) >> 1 - for (let y = 0; y < sh; y++) { - for (let x = 0; x < sw; x++) { - const s = (y * sw + x) * 4 - const d = ((oy + y) * w + ox + x) * 4 - const a = scaled.data[s + 3] / 255 - if (background === 'transparent' || a === 1) out.data.set(scaled.data.subarray(s, s + 4), d) - else for (let c = 0; c < 3; c++) out.data[d + c] = scaled.data[s + c] * a + bg[c] * (1 - a) - } + if (background === 'transparent') { + for (let y = 0; y < sh; y++) out.data.set(scaled.data.subarray(y * sw * 4, (y + 1) * sw * 4), ((oy + y) * w + ox) * 4) + return out } + out.data.fill(background === 'white' ? 255 : 0) + if (background === 'black') for (let i = 3; i < out.data.length; i += 4) out.data[i] = 255 + // see-through parts of the picture show the background + overlay(out, scaled, ox, oy) return out } diff --git a/packages/tools/src/compress-image/run.ts b/packages/tools/src/compress-image/run.ts index 2ba3849..cd4ab88 100644 --- a/packages/tools/src/compress-image/run.ts +++ b/packages/tools/src/compress-image/run.ts @@ -1,5 +1,5 @@ import { UserError, type FileData, type RunFn } from '@ream/core' -import { colourChunks, decodeGif, encodeGif, encodeJpeg, encodePng, quantize, readSegments, withOrientation, type ImageBuffer } from '@ream/image-core' +import { codec, colourChunks, decodeGif, encodeGif, encodePng, quantize, readSegments, withOrientation, type ImageBuffer } from '@ream/image-core' import { decodeFile, encodeFile, outName } from '../image-common.ts' import { formatFor } from '../image-formats.ts' import type { CompressOptions } from './options.ts' @@ -36,7 +36,13 @@ async function compress(f: FileData, o: CompressOptions): Promise { for (let c = 0; c < 3; c++) flat[i + c] = flat[i + c] * a + 255 * (1 - a) flat[i + 3] = 255 } - bytes = encodeJpeg({ width: img.width, height: img.height, data: flat }, { quality: o.quality, segments: segmentsToKeep(f.bytes, o.keepMetadata) }) + const jpeg = await codec().encode({ width: img.width, height: img.height, data: flat }, 'image/jpeg', o.quality / 100) + // the encoder's own header (JFIF), then the kept metadata; anything else the encoder wrote goes (the browser + // adds an sRGB profile, which would contradict the file's own) + const { segments: own, rest } = readSegments(jpeg) + const parts = [jpeg.subarray(0, 2), ...own.filter((s) => s.kind === 'jfif').map((s) => s.bytes), ...segmentsToKeep(f.bytes, o.keepMetadata), jpeg.subarray(rest)] + bytes = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)) + parts.reduce((at, p) => (bytes.set(p, at), at + p.length), 0) } else if (fmt.mime === 'image/png') { bytes = await encodePng(o.reduceColors ? quantize(img, o.colors, o.dither) : img, { chunks: colourChunks(f.bytes) }) } else { diff --git a/packages/tools/src/compress-pdf/images.ts b/packages/tools/src/compress-pdf/images.ts index e16f5ab..489d829 100644 --- a/packages/tools/src/compress-pdf/images.ts +++ b/packages/tools/src/compress-pdf/images.ts @@ -1,9 +1,9 @@ // Saving a PDF's pictures smaller: JPEGs and photographic pictures are decoded, brought down to the resolution they are // shown at, and saved as JPEG again. Anything this can't be sure about is left exactly as it is. import { deflate } from '@ream/core' -import { PdfStream, decodeUntilImage, isName, name, type PdfDocument, type PdfObj } from '@ream/pdf-core' -import { createImage, decodeJpeg, encodeJpeg, pngFilterRows, resize, type ImageBuffer } from '@ream/image-core' -import type { Placement } from '@ream/pdf-render' +import { PdfStream, decodeUntilImage, isName, jpegInfo, name, type PdfDocument, type PdfObj } from '@ream/pdf-core' +import { codec, createImage, decodeJpeg, encodeJpeg, pngFilterRows, resize, type ImageBuffer } from '@ream/image-core' +import type { Placement, Platform } from '@ream/pdf-render' export type ImagePlan = { /** 1 to 100 */ @@ -67,7 +67,7 @@ async function flateSamples(img: ImageBuffer, n: 1 | 3): Promise { } /** the picture saved smaller, or null when it should stay as it is (including when there is nothing to gain) */ -export async function recompressImage(doc: PdfDocument, im: PdfStream, placed: Placement | undefined, plan: ImagePlan): Promise { +export async function recompressImage(doc: PdfDocument, im: PdfStream, placed: Placement | undefined, plan: ImagePlan, platform?: Platform): Promise { const d = im.dict if (!isName(doc.resolve(d.get('Subtype')), 'Image')) return null // stencils, colour-key masks and unusual decode ranges rely on exact sample values @@ -90,9 +90,11 @@ export async function recompressImage(doc: PdfDocument, im: PdfStream, placed: P let wasJpeg = false if (dec.rest) { if (dec.rest.filter !== 'DCTDecode' && dec.rest.filter !== 'DCT') return null // JPEG 2000, CCITT, JBIG2: not ours - const j = decodeJpeg(dec.data) - if (j.components !== n) return null - pixels = j + if (jpegInfo(dec.data)?.components !== n) return null + // colour photos through the platform (the browser's decoder, ~5x faster than ours, drawn as stored like the renderer + // does); grey and CMYK, and anything the platform can't read, through ours + const native = n === 3 && platform ? await platform.decodeJpeg(dec.data).catch(() => null) : null + pixels = native ? { width: native.width, height: native.height, data: native.rgba } : decodeJpeg(dec.data) wasJpeg = true } else { if (dec.data.length < w * h * n) return null @@ -131,7 +133,10 @@ export async function recompressImage(doc: PdfDocument, im: PdfStream, placed: P dict.set('Height', nh) dict.set('BitsPerComponent', 8) if (photo) { - const bytes = encodeJpeg(pixels, { quality: plan.quality, grey: n === 1 }) + // the app's codec (the browser's, much faster) when it gives the components the dictionary says; ours otherwise + // (grey pictures, and RGB that only looks grey, which a codec for image files stores as grey) + let bytes = n === 3 ? await codec().encode(pixels, 'image/jpeg', plan.quality / 100) : null + if (!bytes || jpegInfo(bytes)?.components !== n) bytes = encodeJpeg(pixels, { quality: plan.quality, grey: n === 1 }) // saving a JPEG again at a similar size only costs quality if (bytes.length >= im.data.length * (resized || !wasJpeg ? 1 : 0.9)) return null dict.set('Filter', name('DCTDecode')) diff --git a/packages/tools/src/compress-pdf/run.ts b/packages/tools/src/compress-pdf/run.ts index 6733dff..b509d4a 100644 --- a/packages/tools/src/compress-pdf/run.ts +++ b/packages/tools/src/compress-pdf/run.ts @@ -1,13 +1,14 @@ -import { UserError, type RunFn } from '@ream/core' +import { UserError, type FileData, type RunContext } from '@ream/core' import { PdfStream, isName, optimize } from '@ream/pdf-core' -import { imagePlacements } from '@ream/pdf-render' +import { browserPlatform, imagePlacements, type Platform } from '@ream/pdf-render' import { baseName, loadPdf, pdfFile } from '../pdf-common.ts' import { recompressImage } from './images.ts' import { LEVELS, type CompressPdfOptions } from './options.ts' export type { CompressPdfOptions } -export const run: RunFn = async (files, { level }, { progress }) => { +/** `platform` decodes colour photos (the browser's decoder; where it can't, as in Node tests, ours is used) */ +export async function run(files: FileData[], { level }: CompressPdfOptions, { progress }: RunContext, platform: Platform | undefined = browserPlatform): Promise { const plan = LEVELS[level] if (!plan) throw new UserError('Choose how much to compress.') const f = files[0] @@ -21,7 +22,7 @@ export const run: RunFn = async (files, { level }, { progres progress(0.3) for (const [i, im] of images.entries()) { try { - const smaller = await recompressImage(doc, im, placed.get(im), plan) + const smaller = await recompressImage(doc, im, placed.get(im), plan, platform) if (smaller) replace.set(im, smaller) } catch { // a picture that can't be redone stays as it is diff --git a/packages/tools/src/image-common.test.ts b/packages/tools/src/image-common.test.ts new file mode 100644 index 0000000..c9a0a18 --- /dev/null +++ b/packages/tools/src/image-common.test.ts @@ -0,0 +1,21 @@ +import { beforeAll, expect, test } from 'vitest' +import { createImage, encodePng, ownCodec, setCodec } from '@ream/image-core' +import { decodeFile, keepDecoded } from './image-common.ts' + +beforeAll(() => setCodec(ownCodec)) + +test('a keyed input is decoded once, each caller gets its own copy, and it is forgotten when no longer used', async () => { + const img = createImage(4, 3) + img.data.fill(200) + const f = { name: 'a.png', type: 'image/png', key: '1', bytes: await encodePng(img) } + const first = await decodeFile(f) + first.data.fill(0) // a tool drawing on its copy + // the same key with unreadable bytes: only the kept picture can answer + const again = await decodeFile({ ...f, bytes: new Uint8Array(8) }) + expect(Array.from(again.data)).toEqual(Array.from(img.data)) + keepDecoded(['2']) + await expect(decodeFile({ ...f, bytes: new Uint8Array(8) })).rejects.toThrow() + // no key (tests, outputs): never kept + await decodeFile({ ...f, key: undefined }) + await expect(decodeFile({ ...f, key: undefined, bytes: new Uint8Array(8) })).rejects.toThrow() +}) diff --git a/packages/tools/src/image-common.ts b/packages/tools/src/image-common.ts index f7d82fb..3050e37 100644 --- a/packages/tools/src/image-common.ts +++ b/packages/tools/src/image-common.ts @@ -3,13 +3,31 @@ import { codec, flattenOnWhite, type ImageBuffer } from '@ream/image-core' import { baseName } from './names.ts' import type { Format } from './image-formats.ts' +// Pictures decoded in earlier runs, by input key: a preview after an option change skips decoding (seconds for a big +// photo). Tools change the pixels they get, so each caller gets its own copy. +// ponytail: whole pictures kept up to a fixed budget; keep a smaller copy too if previews of many big files matter +const decoded = new Map() +const BUDGET = 512 * 1024 ** 2 + +/** forget pictures of inputs other than these (the app's worker calls it before each run) */ +export function keepDecoded(keys: (string | undefined)[]) { + for (const k of decoded.keys()) if (!keys.includes(k)) decoded.delete(k) +} + /** decode an input image, prefixing user-facing errors with the file name */ export async function decodeFile(f: FileData): Promise { + const kept = f.key === undefined ? undefined : decoded.get(f.key) + if (kept) return { ...kept, data: kept.data.slice() } + let img: ImageBuffer try { - return await codec().decode(f.bytes, f.type) + img = await codec().decode(f.bytes, f.type) } catch (e) { throw e instanceof UserError ? new UserError(`${f.name}: ${e.message}`) : e } + const used = [...decoded.values()].reduce((n, i) => n + i.data.length, 0) + if (f.key === undefined || used + img.data.length > BUDGET) return img + decoded.set(f.key, img) + return { ...img, data: img.data.slice() } } /** quality is 1-100; formats without transparency (JPG) get transparent areas flattened onto white */ diff --git a/packages/tools/src/m4-tools.test.ts b/packages/tools/src/m4-tools.test.ts index 9433e02..f632f36 100644 --- a/packages/tools/src/m4-tools.test.ts +++ b/packages/tools/src/m4-tools.test.ts @@ -78,6 +78,20 @@ describe('compress-image', () => { expect([pixels.width, pixels.height]).toEqual([40, 64]) // the sideways photo was turned }) + test("the file's own colour profile replaces one the encoder adds (the browser's writes sRGB)", async () => { + const own = icc() + const input = file('IMG.jpg', 'image/jpeg', tagged(photo(160, 120))) + const srgb = Uint8Array.from([0xff, 0xe2, 0, 16, ...ascii('ICC_PROFILE'), 1, 1]) + setCodec({ ...ownCodec, encode: async (img) => encodeJpeg(img, { quality: 60, segments: [srgb] }) }) + try { + const [out] = await compress([input], { ...compressDefaults, quality: 60 }, ctx) + const profiles = readSegments(out.bytes).segments.filter((s) => s.kind === 'icc') + expect(profiles.map((s) => Array.from(s.bytes))).toEqual([Array.from(own)]) + } finally { + setCodec(ownCodec) + } + }) + test('a lower quality is smaller', async () => { const input = file('a.jpg', 'image/jpeg', tagged(photo(160, 120))) const sizes = await Promise.all([30, 60, 90].map(async (quality) => (await compress([input], { ...compressDefaults, quality }, ctx))[0].bytes.length)) diff --git a/packages/tools/src/m7-tools.test.ts b/packages/tools/src/m7-tools.test.ts index 339cc74..cf2b96e 100644 --- a/packages/tools/src/m7-tools.test.ts +++ b/packages/tools/src/m7-tools.test.ts @@ -142,8 +142,6 @@ describe('repair-pdf', () => { }) }) -const form = (): FileData => ({ name: 'form.pdf', type: 'application/pdf', bytes: formPdf() }) - describe('pdf-to-text', () => { const decode = (f: FileData) => new TextDecoder().decode(f.bytes) diff --git a/packages/tools/src/m7b-tools.test.ts b/packages/tools/src/m7b-tools.test.ts index 7f27789..0066bc7 100644 --- a/packages/tools/src/m7b-tools.test.ts +++ b/packages/tools/src/m7b-tools.test.ts @@ -1,7 +1,7 @@ import { readFileSync } from 'node:fs' -import { describe, expect, test } from 'vitest' +import { beforeAll, describe, expect, test } from 'vitest' import type { FileData } from '@ream/core' -import { decodeJpeg, decodePng } from '@ream/image-core' +import { decodeJpeg, decodePng, ownCodec, setCodec } from '@ream/image-core' import { PdfDocument } from '@ream/pdf-core' import type { PageRaster } from '@ream/pdf-render' import { readXlsx, writeXlsx, writePptx, parseXml, kids, child, type Sheet, type XmlNode } from '@ream/office-core' @@ -20,6 +20,8 @@ import { nodeFonts } from './fonts/node.ts' import { parseNumber } from './pdf-to-excel/number.ts' import { docxToPdf, officeToPdf } from './office-preview.ts' +beforeAll(() => setCodec(ownCodec)) // JPG pages go through the codec; the browser's is checked in the app + const ctx = { progress: () => {} } const pdf = (n = 3, name = 'doc.pdf'): FileData => ({ name, type: 'application/pdf', bytes: classicPdf(Array.from({ length: n }, (_, i) => `P${i + 1}`)) }) diff --git a/packages/tools/src/pdf-to-image/run.ts b/packages/tools/src/pdf-to-image/run.ts index 5a57870..b7ad2c5 100644 --- a/packages/tools/src/pdf-to-image/run.ts +++ b/packages/tools/src/pdf-to-image/run.ts @@ -1,5 +1,5 @@ import { UserError, type FileData, type RunContext } from '@ream/core' -import { checkPixels, flattenOnWhite, encodePng, encodeJpeg } from '@ream/image-core' +import { checkPixels, codec, flattenOnWhite, encodePng } from '@ream/image-core' import { browserRaster, rasterSize, type PageRaster } from '@ream/pdf-render' import { baseName, clampInt, loadPdf, selectPages } from '../pdf-common.ts' @@ -38,7 +38,8 @@ export async function run(files: FileData[], o: PdfToImageOptions, { progress }: for (const [n, p] of pages.entries()) { progress(n / pages.length) const px = await raster(doc, p, widths[n]) - const bytes = jpg ? encodeJpeg(flattenOnWhite(px), { quality }) : await encodePng(px) + // JPG through the app's codec (the browser's, many times faster); PNG stays ours, whose palette keeps pages of text small + const bytes = jpg ? await codec().encode(flattenOnWhite(px), 'image/jpeg', quality / 100) : await encodePng(px) out.push({ name: `${base}-page-${String(p + 1).padStart(digits, '0')}.${jpg ? 'jpg' : 'png'}`, type: jpg ? 'image/jpeg' : 'image/png', bytes }) } progress(1) From 4fb6656b9877f4d0e3a18a89cbe5e48dc9a9822a Mon Sep 17 00:00:00 2001 From: Chizoba Jude Ojini Date: Tue, 27 Oct 2026 22:17:21 +0000 Subject: [PATCH 2/3] Release v0.2.0: changelog per version, yarn release and the Prepare release workflow --- .github/workflows/prepare-release.yml | 62 ++++++++++++++++++ .github/workflows/web.yml | 6 +- AGENTS.md | 2 +- CHANGELOG.md | 12 ++++ CONTRIBUTING.md | 2 +- docs/deploy.md | 7 +- package.json | 5 +- scripts/release.ts | 92 +++++++++++++++++++++------ 8 files changed, 159 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/prepare-release.yml create mode 100644 CHANGELOG.md diff --git a/.github/workflows/prepare-release.yml b/.github/workflows/prepare-release.yml new file mode 100644 index 0000000..9c25512 --- /dev/null +++ b/.github/workflows/prepare-release.yml @@ -0,0 +1,62 @@ +# Start a release from GitHub (Actions → Prepare release → Run workflow): raises the version on a copy of dev, writes +# its CHANGELOG.md section (yarn release), and opens the pull request "Release v" into dev. Review and edit the +# changelog there, merge it, then open dev → main as usual. +# RELEASE_TOKEN (a fine-grained token: this repo, Contents and Pull requests read and write) opens the pull request as +# you, so `check` and the preview run on it; GitHub doesn't start workflows for pull requests its own token opens. +name: Prepare release + +on: + workflow_dispatch: + inputs: + kind: + description: 'patch (fixes), minor (new tools or features) or major (breaks how people use Ream)' + type: choice + options: [patch, minor, major] + default: minor + +permissions: + contents: write + pull-requests: write + +concurrency: prepare-release + +jobs: + prepare: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v7 + with: + ref: dev + fetch-depth: 0 # the tags and history the changelog is made from + token: ${{ secrets.RELEASE_TOKEN || github.token }} + - uses: actions/setup-node@v7 + with: + node-version: 22 # scripts/release.ts is TypeScript, run directly; it needs only git + - name: Raise the version and write the changelog + id: release + run: | + node scripts/release.ts prepare "${{ inputs.kind }}" + echo "version=$(node scripts/release.ts version)" >> "$GITHUB_OUTPUT" + - name: Open the pull request into dev + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN || github.token }} + VERSION: ${{ steps.release.outputs.version }} + run: | + branch="release/v$VERSION" + if git ls-remote --exit-code --heads origin "$branch" >/dev/null; then + echo "::error::$branch already exists: merge or delete its pull request first." + exit 1 + fi + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git switch -c "$branch" + git add package.json CHANGELOG.md + git commit -m "Release v$VERSION" + git push origin "$branch" + { + echo "Raises the version to $VERSION and adds its changelog section. Edit \`CHANGELOG.md\` here if the wording needs it; after merging, open the pull request from \`dev\` into \`main\` to release." + echo + node scripts/release.ts notes + } > body.md + gh pr create --base dev --head "$branch" --title "Release v$VERSION" --body-file body.md diff --git a/.github/workflows/web.yml b/.github/workflows/web.yml index 04d2808..ed7c8bf 100644 --- a/.github/workflows/web.yml +++ b/.github/workflows/web.yml @@ -127,10 +127,12 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy dist --project-name ream --branch main --commit-hash "${{ github.sha }}" --commit-message "v${{ steps.version.outputs.version }}" - - name: Tag the release and write its notes (the pull requests merged since the last one) + - name: Tag the release, with its CHANGELOG.md section as the notes env: GH_TOKEN: ${{ github.token }} - run: gh release create "v${{ steps.version.outputs.version }}" --repo "${{ github.repository }}" --target "${{ github.sha }}" --title "v${{ steps.version.outputs.version }}" --generate-notes + run: | + node repo/scripts/release.ts notes > notes.md + gh release create "v${{ steps.version.outputs.version }}" --repo "${{ github.repository }}" --target "${{ github.sha }}" --title "v${{ steps.version.outputs.version }}" --notes-file notes.md # the image for self-hosting: Intel/AMD and ARM, tagged with the version and latest. The site is built once, on # the runner's own platform; each platform's image only copies the files, so no emulation is needed. - uses: docker/setup-buildx-action@v4 diff --git a/AGENTS.md b/AGENTS.md index 99b6125..e472ce4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -156,7 +156,7 @@ To add a tool: manifest, run, panel, three registry entries, and tests. Reuse th - Routes (`App.tsx`, hash based): `#/tool/` the tool page, `#/tools`, `#/tools/pdf` and `#/tools/image` the tools home for a category (`Home.tsx`, `filterHref` in `TopBar.tsx`), `#/licence` and `#/notices` (`Legal.tsx`: the repo's `LICENSE` and `THIRD-PARTY-NOTICES.md`, imported `?raw` when the page opens) and `#/` (`Landing.tsx`); any other hash shows the landing page. The web-only pages share `Marketing.tsx` (nav, footer, `primary`/`secondary` link styles); app pages render `TopBar` (logo on the left; search and theme on the right; 1600 px like the pages) and `Breadcrumb` under it (All tools, the category, the tool; every level a link). The breadcrumb replaces visible headings on the tools home and tool pages (the `h1` is `sr-only`); on All tools the PDF tools and Image tools section titles link to their category. `focus` in `TopBar.tsx` is the shared focus ring. - Tools home (`Home.tsx`, at most 1600 px wide) is drop-first: the `Dropzone` `bar` layout, and dropped files handed to a tool through `handoff.ts`, the same mechanism as "Use in another tool". - Docker: `Dockerfile` builds the site with Node (on the build machine's platform) and serves it with `nginxinc/nginx-unprivileged` (non-root, port 8080). Its NGINX site is generated from `apps/web/public/_headers` by `scripts/nginx-conf.ts`, so change headers there only. The image build skips the typecheck (tests are left out by `.dockerignore`; CI typechecks anyway). `check` builds and runs it and checks the page, the headers, `sw.js` not being cached, the manifest's type, a 404 and a non-root user; the release pushes amd64 and arm64 images to `ghcr.io/deityhub/ream` and, with `DOCKERHUB_USERNAME`/`DOCKERHUB_TOKEN` set, `docker.io//ream`, tagged with the version and `latest`. Containers can't start in this Mac's Docker; test a server inside a throwaway `docker build` (start it in a `RUN`, query it with curl) or rely on CI. -- Versions: the root `package.json` `version` (semver, starting at 0.1.0) is the release version. A pull request into `main` must raise it (`yarn version patch|minor|major`; `node scripts/release.ts check` fails on a version already tagged or older); the merge deploys production, tags `v` and creates a GitHub Release with generated notes. The version is in the footer (`__APP_VERSION__`, defined in `vite.config.ts`, declared in `version.d.ts`) and in the offline cache's name. `.github/workflows/deploy-version.yml` builds a released tag again and deploys it to production (rollback when the Cloudflare dashboard's can't). +- Versions: the root `package.json` `version` (semver, starting at 0.1.0) is the release version. A pull request into `main` must raise it and have its `CHANGELOG.md` section: `yarn release patch|minor|major` (`scripts/release.ts prepare`) writes both, the section from the pull requests merged since the last tag (first-parent merges of `dev`, their titles and links; earlier releases skipped), and `node scripts/release.ts check` fails on a version already tagged or older or one without a section. The merge deploys production, tags `v` and creates a GitHub Release whose notes are that section (`release.ts notes`). `.github/workflows/prepare-release.yml` (Actions → Prepare release) runs `prepare` on `dev` and opens the "Release v" pull request into `dev`, as the `RELEASE_TOKEN` secret so `check` and the preview run on it. The version is in the footer (`__APP_VERSION__`, defined in `vite.config.ts`, declared in `version.d.ts`) and in the offline cache's name. `.github/workflows/deploy-version.yml` builds a released tag again and deploys it to production (rollback when the Cloudflare dashboard's can't). - Hosting: Cloudflare Pages, deployed by `.github/workflows/web.yml`: a pull request into `dev` runs `check` and gets a preview (its link is commented on the PR), a pull request into `main` runs `check`, a merge into `main` runs `check` and deploys production. Plain pushes run nothing. `check` is the status `.github/rulesets/protect-dev-main.json` requires (import it under Settings → Rules once the repo is public: rulesets aren't free on private repos). `apps/web/public/_headers` holds the security headers: the CSP allows only the site's own files, plus `api.fontsource.org` and `cdn.jsdelivr.net` in `connect-src` for Download free fonts, and no inline scripts or styles (`style-src 'self'`: React's `style` props are fine, `