diff --git a/apps/api/src/delivery/render.ts b/apps/api/src/delivery/render.ts index 7c1c71f..c369dd3 100644 --- a/apps/api/src/delivery/render.ts +++ b/apps/api/src/delivery/render.ts @@ -4,8 +4,12 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import sharp, { type Sharp } from "sharp"; +import { CROP_FLOOR_PX } from "../composition/compose.js"; import type { RenderEngine, Settings } from "../develop/engine.js"; +/** A crop that only just missed the delivery size (3000 px on a 4000 px export) may be enlarged. A third more, and no further. */ +const MAX_LIFT = 4 / 3; + /** The photo was edited: render it with these develop settings. */ export interface DeliveryEdit { engine: RenderEngine; @@ -26,8 +30,9 @@ export interface RenderOptions { } /** - * The delivered JPEG: oriented, at most `longEdge` px on the longest side (never enlarged), 4:4:4 chroma at - * the requested quality, camera EXIF and colour profile kept, IPTC written into both IIM and XMP. + * The delivered JPEG: oriented, at most `longEdge` px on the longest side, 4:4:4 chroma at the requested + * quality, camera EXIF and colour profile kept, IPTC written into both IIM and XMP. A file that is already at + * least {@link CROP_FLOOR_PX} and only a little under `longEdge` is enlarged up to it. Anything smaller stays. */ export async function renderDelivery(original: string, iptc: IptcValues, opts: RenderOptions, created?: CreatedStamp, edit?: DeliveryEdit): Promise { const dir = await mkdtemp(path.join(tmpdir(), "pr-render-")); @@ -45,6 +50,7 @@ export async function renderDelivery(original: string, iptc: IptcValues, opts: R // losslessly enough (quality 100) for the last pass to sharpen and encode at the delivered quality. const rendered = path.join(dir, "edited.jpg"); await edit.engine.render({ input: original, output: rendered, settings: edit.settings, maxEdge: opts.longEdge, quality: opts.sharpen || opts.quality < 100 ? 100 : opts.quality }); + await liftNearTarget(rendered, opts.longEdge); await writeFile(file, opts.sharpen || opts.quality < 100 ? await finish(sharp(rendered)) : await readFile(rendered)); // The camera EXIF is copied back, without the orientation (the pixels are already upright). const copyExif = exif ? ["-TagsFromFile", original, "-EXIF:all", "--Orientation", "--ThumbnailImage", "--PreviewImage", "-Orientation#=1"] : []; @@ -77,6 +83,21 @@ export async function originalWithMetadata(original: string, iptc: IptcValues, c } } +/** The engine never enlarges. A crop held at the 3000 px floor is brought up to a 4000 px delivery; a small file is not. */ +async function liftNearTarget(file: string, longEdge: number): Promise { + if (longEdge <= CROP_FLOOR_PX) return; + const meta = await sharp(file, { failOn: "none" }).metadata(); + const width = meta.width ?? 0; + const height = meta.height ?? 0; + const edge = Math.max(width, height); + if (edge < CROP_FLOOR_PX || edge >= longEdge || longEdge / edge > MAX_LIFT + 0.01) return; + const lifted = await sharp(file) + .resize({ width: longEdge, height: longEdge, fit: "inside", withoutEnlargement: false }) + .jpeg({ quality: 100, chromaSubsampling: "4:4:4", mozjpeg: false }) + .toBuffer(); + await writeFile(file, lifted); +} + export async function closeRenderer(): Promise { await exiftool.end(); } diff --git a/apps/api/test/render-lift.test.ts b/apps/api/test/render-lift.test.ts new file mode 100644 index 0000000..6637d65 --- /dev/null +++ b/apps/api/test/render-lift.test.ts @@ -0,0 +1,62 @@ +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import sharp from "sharp"; +import { afterAll, describe, expect, it } from "vitest"; +import type { RenderEngine, RenderJob } from "../src/develop/engine.js"; +import { closeRenderer, renderDelivery } from "../src/delivery/render.js"; + +function engineWriting(width: number, height: number): RenderEngine { + return { + async replay() { + return { settings: {}, skipped: [], unedited: false }; + }, + async auto() { + return { values: {}, angle: null }; + }, + async close() {}, + async render(job: RenderJob) { + await sharp({ create: { width, height, channels: 3, background: { r: 40, g: 80, b: 120 } } }) + .jpeg({ quality: 90 }) + .toFile(job.output); + return { width, height }; + }, + }; +} + +async function deliver(width: number, height: number, longEdge: number): Promise<{ width?: number; height?: number }> { + const dir = await mkdtemp(path.join(tmpdir(), "pr-lift-")); + try { + const original = path.join(dir, "in.jpg"); + await sharp({ create: { width: 32, height: 24, channels: 3, background: { r: 1, g: 1, b: 1 } } }) + .jpeg() + .toFile(original); + const out = await renderDelivery(original, {}, { longEdge, quality: 90, exif: false }, undefined, { + engine: engineWriting(width, height), + settings: {}, + }); + const meta = await sharp(out).metadata(); + return { width: meta.width, height: meta.height }; + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +describe("delivery lift", () => { + afterAll(() => closeRenderer()); + + it("brings a 3000 px crop up to a 4000 px export", async () => { + const meta = await deliver(3000, 2000, 4000); + expect(meta.width).toBe(4000); + expect(meta.height).toBeGreaterThan(2660); + expect(meta.height).toBeLessThan(2670); + }); + + it("leaves a small file at its own size", async () => { + expect(await deliver(1600, 1000, 4000)).toEqual({ width: 1600, height: 1000 }); + }); + + it("does not blow a 3000 px crop up to a 12000 px export", async () => { + expect(await deliver(3000, 2000, 12000)).toEqual({ width: 3000, height: 2000 }); + }); +}); diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index 296a351..e5d656f 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -55,8 +55,8 @@ Following the AP and NPPA codes: as long as nothing that matters goes with them. - **Resolution:** never crop below what the delivery needs. Default: the crop keeps at least 2400 px on its long edge of the original (configurable per organisation; agencies often ask for more). On a large file the floor - is 3000 px, not 35% of the frame (that held a modern body near 4000 px). An export at 4000 px may enlarge - that crop; the difference is not visible. + is 3000 px, not 35% of the frame (that held a modern body near 4000 px). An export at 4000 px enlarges + that crop, and only that far (at most a third). A smaller file is not blown up. - **Not too much:** if the only crop that works is tighter than the resolution allows, keep the wider frame. A good photo uncropped beats a bad crop. - **Not for nothing:** a crop of more than 92% of the width is not made: the photo stays as shot. The agencies