Add JPEG XL export with a lossless option
Export now offers JPEG XL (8-bit sRGB) next to JPEG, PNG, WebP and 16-bit TIFF, encoded on-device by libjxl compiled to WebAssembly so the export stays offline. The codec loads only when a JXL is chosen, the quality slider applies to the lossy mode, and a lossless switch disables it. The user guide and changelog describe the format and its current limits (no ICC or XMP yet). Also adds SECURITY-AUDIT.md with the dependency scan and the review of the server's trust boundaries.
This commit is contained in:
@@ -23,6 +23,7 @@ const FORMATS: { value: ExportFormat; label: string }[] = [
|
||||
{ value: 'png', label: 'PNG (8-bit)' },
|
||||
{ value: 'webp', label: 'WebP' },
|
||||
{ value: 'tiff16', label: 'TIFF (16-bit)' },
|
||||
{ value: 'jxl', label: 'JPEG XL' },
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -360,7 +361,7 @@ export default function ExportModal({ photos, initial, recipes, onSaveRecipe, on
|
||||
)}
|
||||
</div>
|
||||
|
||||
{(opts.format === 'jpeg' || opts.format === 'webp') && (
|
||||
{(opts.format === 'jpeg' || opts.format === 'webp' || opts.format === 'jxl') && (
|
||||
<label className="field">
|
||||
<span>Quality · {opts.quality}</span>
|
||||
<input
|
||||
@@ -369,11 +370,29 @@ export default function ExportModal({ photos, initial, recipes, onSaveRecipe, on
|
||||
max={100}
|
||||
value={opts.quality}
|
||||
onChange={(e) => patch({ quality: Number(e.target.value) })}
|
||||
disabled={busy}
|
||||
disabled={busy || (opts.format === 'jxl' && opts.jxlLossless === true)}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
|
||||
{opts.format === 'jxl' && (
|
||||
<>
|
||||
<label className="check-row">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={opts.jxlLossless === true}
|
||||
onChange={(e) => patch({ jxlLossless: e.target.checked })}
|
||||
disabled={busy}
|
||||
/>
|
||||
Lossless — keeps every pixel and ignores the quality setting
|
||||
</label>
|
||||
<p className="muted small">
|
||||
JPEG XL exports are 8-bit sRGB. Chrome, Firefox and Safari open them; Microsoft Edge
|
||||
does not yet.
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
|
||||
{sample && <ExportDetailStrip photo={sample} size={previewSize} />}
|
||||
|
||||
<label className="field">
|
||||
@@ -478,7 +497,7 @@ export default function ExportModal({ photos, initial, recipes, onSaveRecipe, on
|
||||
type="checkbox"
|
||||
checked={opts.embedProfile}
|
||||
onChange={(e) => patch({ embedProfile: e.target.checked })}
|
||||
disabled={opts.format === 'webp' || opts.format === 'png'}
|
||||
disabled={opts.format === 'webp' || opts.format === 'png' || opts.format === 'jxl'}
|
||||
/>
|
||||
Embed {COLOR_SPACE_OPTIONS.find((option) => option.value === (opts.colorSpace ?? 'srgb'))?.label ?? 'sRGB'} profile (JPEG/TIFF)
|
||||
</label>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { outputSize } from '../gl/renderer';
|
||||
import { defaultEdits } from './edits';
|
||||
import { buildFileName, DEFAULT_EXPORT } from './export';
|
||||
import type { PhotoRecord } from '../types';
|
||||
|
||||
describe('export output dimensions', () => {
|
||||
it('matches the source for an uncropped image', () => {
|
||||
@@ -19,6 +21,12 @@ describe('export output dimensions', () => {
|
||||
expect(outputSize(edits, 4000, 3000)).toEqual({ w: 4950, h: 4950 });
|
||||
});
|
||||
|
||||
it('writes the JPEG XL extension for a JXL export', () => {
|
||||
const photo = { name: 'DSCF1234.RAF', addedAt: Date.now() } as PhotoRecord;
|
||||
const name = buildFileName('{name}', photo, 0, { ...DEFAULT_EXPORT, format: 'jxl' });
|
||||
expect(name).toBe('DSCF1234.jxl');
|
||||
});
|
||||
|
||||
it('applies the crop after the rotated frame is calculated', () => {
|
||||
const edits = defaultEdits();
|
||||
edits.crop.angle = 45;
|
||||
|
||||
+27
-1
@@ -8,8 +8,9 @@ import { createZipBlob, zipFileName } from './zip';
|
||||
import { profileFor, type OutputColorSpace } from './icc';
|
||||
import { convertFloatPixels, convertImageData } from './colorSpace';
|
||||
import { encodeTiff16 } from './tiff';
|
||||
import { encodeJxl } from './jxl';
|
||||
|
||||
export type ExportFormat = 'jpeg' | 'png' | 'webp' | 'tiff16';
|
||||
export type ExportFormat = 'jpeg' | 'png' | 'webp' | 'tiff16' | 'jxl';
|
||||
export type ResizeMode = 'none' | 'longEdge' | 'percent';
|
||||
|
||||
export type WatermarkPosition = 'br' | 'bl' | 'tr' | 'tl' | 'center';
|
||||
@@ -27,6 +28,8 @@ export interface ExportOptions {
|
||||
zip: boolean;
|
||||
/** Extra capture sharpening applied only to the exported pixels. */
|
||||
outputSharpen: number;
|
||||
/** JPEG XL only: keep every pixel instead of using the quality slider. */
|
||||
jxlLossless?: boolean;
|
||||
/** Write title/caption/creator/copyright/keywords into the file. */
|
||||
metadata: boolean;
|
||||
/** Embed the generated sRGB ICC profile in JPEG/TIFF files. */
|
||||
@@ -56,6 +59,7 @@ export const DEFAULT_EXPORT: ExportOptions = {
|
||||
nameTemplate: '{name}',
|
||||
zip: true,
|
||||
outputSharpen: 0,
|
||||
jxlLossless: false,
|
||||
metadata: true,
|
||||
embedProfile: true,
|
||||
watermark: { enabled: false, text: '', position: 'br', opacity: 55, size: 4 },
|
||||
@@ -66,6 +70,7 @@ const MIME: Record<ExportFormat, string> = {
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
tiff16: 'image/tiff',
|
||||
jxl: 'image/jxl',
|
||||
};
|
||||
|
||||
const EXT: Record<ExportFormat, string> = {
|
||||
@@ -73,6 +78,7 @@ const EXT: Record<ExportFormat, string> = {
|
||||
png: 'png',
|
||||
webp: 'webp',
|
||||
tiff16: 'tif',
|
||||
jxl: 'jxl',
|
||||
};
|
||||
|
||||
function download(blob: Blob, filename: string) {
|
||||
@@ -178,6 +184,12 @@ export async function renderPhotoBlob(
|
||||
if (opts.watermark.enabled && opts.watermark.text.trim()) {
|
||||
output = paintWatermark(output, photo, opts);
|
||||
}
|
||||
if (opts.format === 'jxl') {
|
||||
return encodeJxl(readPixels(output), {
|
||||
quality: opts.quality,
|
||||
lossless: opts.jxlLossless === true,
|
||||
});
|
||||
}
|
||||
const mime = MIME[opts.format];
|
||||
let blob = await new Promise<Blob | null>((resolve) =>
|
||||
output.toBlob(resolve, mime, Math.min(1, Math.max(0.1, opts.quality / 100))),
|
||||
@@ -197,6 +209,20 @@ export async function renderPhotoBlob(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The render canvas holds a WebGL context, so its pixels have to be drawn into
|
||||
* a 2D canvas before an encoder can read them.
|
||||
*/
|
||||
function readPixels(source: HTMLCanvasElement): ImageData {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = source.width;
|
||||
canvas.height = source.height;
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) throw new Error('Could not read the export pixels');
|
||||
ctx.drawImage(source, 0, 0);
|
||||
return ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
}
|
||||
|
||||
function convertCanvas(source: HTMLCanvasElement, space: OutputColorSpace) {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = source.width;
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* JPEG XL export (8-bit sRGB), encoded by libjxl compiled to WebAssembly.
|
||||
*
|
||||
* The codec is imported on demand: it is roughly 1.4 MB of wasm plus glue, and
|
||||
* most sessions never export a JXL. Only the single-threaded build is used —
|
||||
* the app is not cross-origin isolated, so the multithreaded glue would fall
|
||||
* back anyway, and its worker entry points cannot be bundled.
|
||||
*
|
||||
* The glue resolves its wasm relative to its own module URL, which a bundler
|
||||
* rewrites; `locateFile` points it at the URL Vite actually emitted.
|
||||
*/
|
||||
|
||||
import jxlWasm from '@jsquash/jxl/codec/enc/jxl_enc.wasm?url';
|
||||
|
||||
export interface JxlOptions {
|
||||
/** 1–100, the same number the modal's quality slider shows. */
|
||||
quality: number;
|
||||
lossless: boolean;
|
||||
}
|
||||
|
||||
interface JxlEncodeOptions {
|
||||
effort: number;
|
||||
quality: number;
|
||||
lossless: boolean;
|
||||
progressive: boolean;
|
||||
epf: number;
|
||||
lossyPalette: boolean;
|
||||
decodingSpeedTier: number;
|
||||
photonNoiseIso: number;
|
||||
lossyModular: boolean;
|
||||
}
|
||||
|
||||
interface JxlModule {
|
||||
encode(data: BufferSource, width: number, height: number, options: JxlEncodeOptions): Uint8Array | null;
|
||||
}
|
||||
|
||||
let encoder: Promise<JxlModule> | null = null;
|
||||
|
||||
function loadEncoder(): Promise<JxlModule> {
|
||||
if (!encoder) {
|
||||
encoder = (async () => {
|
||||
const [{ default: factory }, { initEmscriptenModule }] = await Promise.all([
|
||||
import('@jsquash/jxl/codec/enc/jxl_enc.js'),
|
||||
import('@jsquash/jxl/utils.js'),
|
||||
]);
|
||||
const module = await initEmscriptenModule(factory as never, undefined, {
|
||||
locateFile: (path: string) => (path.endsWith('jxl_enc.wasm') ? jxlWasm : path),
|
||||
});
|
||||
return module as unknown as JxlModule;
|
||||
})().catch((error) => {
|
||||
// A failed load must not poison every later export.
|
||||
encoder = null;
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return encoder;
|
||||
}
|
||||
|
||||
export async function encodeJxl(image: ImageData, options: JxlOptions): Promise<Blob> {
|
||||
const module = await loadEncoder();
|
||||
const lossless = options.lossless === true;
|
||||
const quality = Math.min(100, Math.max(1, Math.round(options.quality || 90)));
|
||||
const result = module.encode(image.data, image.width, image.height, {
|
||||
quality: lossless ? 100 : quality,
|
||||
lossless,
|
||||
// libjxl defaults to effort 7, which is a long wait single-threaded on a
|
||||
// 45 MP frame. 5 keeps a batch moving and costs little for photographs.
|
||||
effort: 5,
|
||||
progressive: false,
|
||||
epf: -1,
|
||||
lossyPalette: false,
|
||||
decodingSpeedTier: 0,
|
||||
photonNoiseIso: 0,
|
||||
lossyModular: false,
|
||||
});
|
||||
if (!result || !result.byteLength) throw new Error('JPEG XL encoding failed');
|
||||
// A fresh buffer: the encoder returns a view that may over-allocate, and a
|
||||
// Blob must not keep the wasm heap alive.
|
||||
const bytes = new Uint8Array(result.byteLength);
|
||||
bytes.set(result);
|
||||
return new Blob([bytes], { type: 'image/jxl' });
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
||||
Reference in New Issue
Block a user