diff --git a/package-lock.json b/package-lock.json index 94e2dcf115e3e1..808e2ef46e3c10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56251,6 +56251,15 @@ "integrity": "sha512-zksaLKM2fVlnB5jQQDqKXXwYHLQUVH9es+5TOOHwGOVJOCeRBCiPjwSg+3tN2AdTCzjgli4jijCH290kXb/zWQ==", "license": "Apache-2.0" }, + "node_modules/wasm-vips": { + "version": "0.0.17", + "resolved": "https://registry.npmjs.org/wasm-vips/-/wasm-vips-0.0.17.tgz", + "integrity": "sha512-nhkqUNJDUymImoXGrVfImC4wzIFTb9KfBpAngb7dcEQNPP1gVTx4+WL3VVVDSXQpMsyeacsQDOx0+DM33Rpurg==", + "license": "MIT", + "engines": { + "node": ">=16.4.0" + } + }, "node_modules/watchpack": { "version": "1.7.5", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-1.7.5.tgz", @@ -65887,22 +65896,13 @@ "license": "GPL-2.0-or-later", "dependencies": { "@wordpress/worker-threads": "file:../worker-threads", - "wasm-vips": "^0.0.16" + "wasm-vips": "^0.0.17" }, "engines": { "node": ">=18.12.0", "npm": ">=8.19.2" } }, - "packages/vips/node_modules/wasm-vips": { - "version": "0.0.16", - "resolved": "https://registry.npmjs.org/wasm-vips/-/wasm-vips-0.0.16.tgz", - "integrity": "sha512-4/bEq8noAFt7DX3VT+Vt5AgNtnnOLwvmrDbduWfiv9AV+VYkbUU4f9Dam9e6khRqPinyClFHCqiwATTTJEiGwA==", - "license": "MIT", - "engines": { - "node": ">=16.4.0" - } - }, "packages/warning": { "name": "@wordpress/warning", "version": "3.47.0", diff --git a/packages/upload-media/CHANGELOG.md b/packages/upload-media/CHANGELOG.md index c9aedd43c38dcf..bdf64053b542c6 100644 --- a/packages/upload-media/CHANGELOG.md +++ b/packages/upload-media/CHANGELOG.md @@ -4,6 +4,7 @@ ### Enhancement +- UltraHDR (ISO 21496-1 gain map) JPEGs are now detected and resized via libvips's native `uhdrload`/`uhdrsave` pipeline, so gain maps are preserved automatically through the existing resize step ([#74873](https://github.com/WordPress/gutenberg/pull/74873)). - Automatically retry failed uploads with exponential backoff for transient (network/server) errors. Retry behavior is configurable via the `retry` store setting; non-transient failures and child sideloads are not retried. The upload queue can also be paused and resumed, allowing uploads to halt while the browser is offline and continue on reconnect ([#76765](https://github.com/WordPress/gutenberg/pull/76765)). ### Bug Fix diff --git a/packages/upload-media/src/store/private-actions.ts b/packages/upload-media/src/store/private-actions.ts index 32ed0b35ddbf9e..2b8db744997999 100644 --- a/packages/upload-media/src/store/private-actions.ts +++ b/packages/upload-media/src/store/private-actions.ts @@ -29,6 +29,7 @@ import { vipsRotateImage, vipsConvertImageFormat, vipsHasTransparency, + vipsGetUltraHdrInfo, terminateVipsWorker, maybeRecycleVipsWorker, } from './utils'; @@ -67,6 +68,15 @@ import { clearRetryTimer } from './utils/retry'; const DEFAULT_OUTPUT_QUALITY = 0.82; +/** + * Tracks parent item IDs whose source file is an UltraHDR JPEG so that + * sub-size resize operations can route through libvips's uhdrload/uhdrsave + * to preserve the gain map. Entries are cleared in `removeItem` when the + * parent item leaves the queue, covering both successful completion and + * cancellation. + */ +const ultraHdrItems = new Set< QueueItemId >(); + type ActionCreators = { cancelItem: typeof cancelItem; executeRetry: typeof executeRetry; @@ -86,6 +96,7 @@ type ActionCreators = { finalizeItem: typeof finalizeItem; updateItemProgress: typeof updateItemProgress; revokeBlobUrls: typeof revokeBlobUrls; + detectUltraHdr: typeof detectUltraHdr; < T = Record< string, unknown > >( args: T ): void; }; @@ -447,6 +458,10 @@ export function processItem( id: QueueItemId ) { case OperationType.Finalize: dispatch.finalizeItem( id ); break; + + case OperationType.DetectUltraHdr: + dispatch.detectUltraHdr( id ); + break; } }; } @@ -515,6 +530,11 @@ export function removeItem( id: QueueItemId ) { return; } + // Clear any UltraHDR tracking for this item. removeItem runs on both + // successful completion and cancellation, so this prevents the set + // from growing unbounded over a long editing session. + ultraHdrItems.delete( id ); + // Clear any pending retry timer for this item. clearRetryTimer( id ); @@ -674,6 +694,9 @@ export async function getTranscodeImageOperation( * Or videos need to be compressed, and then need poster generation * before upload. * + * UltraHDR JPEG images are detected and uploaded unmodified — they are + * already backwards compatible (SDR displays use the embedded base image). + * * @param id Item ID. */ export function prepareItem( id: QueueItemId ) { @@ -708,6 +731,14 @@ export function prepareItem( id: QueueItemId ) { } } + // Check for UltraHDR in JPEG files before other operations. Skipped for + // images routed to the server: the gain map is only preserved by the + // client-side resize path, and the probe runs wasm-vips, which the + // large-image gate above is specifically meant to avoid. + if ( file.type === 'image/jpeg' && ! tooLargeForClient ) { + operations.push( OperationType.DetectUltraHdr ); + } + // For images that can be processed by vips, upload the original and // let generateThumbnails() handle threshold scaling as a sideload. // @@ -815,6 +846,40 @@ export function prepareItem( id: QueueItemId ) { }; } +/** + * Detects whether a JPEG is an UltraHDR image and records the parent item + * ID so that downstream resize operations route through libvips's + * uhdrload/uhdrsave pipeline (which preserves the gain map). + * + * @param id Item ID. + */ +export function detectUltraHdr( id: QueueItemId ) { + return async ( { select, dispatch }: ThunkArgs ) => { + const item = select.getItem( id ); + if ( ! item ) { + return; + } + + let info; + try { + const buffer = await item.file.arrayBuffer(); + info = await vipsGetUltraHdrInfo( buffer ); + } catch { + // If UltraHDR detection fails, continue with regular upload. + } + + // Track the item so downstream resize operations preserve the gain + // map and skip format transcoding. The original file is uploaded + // unmodified — UltraHDR JPEGs are already backwards compatible (SDR + // displays use the embedded base image). + if ( info ) { + ultraHdrItems.add( id ); + } + + dispatch.finishOperation( id, {} ); + }; +} + /** * Uploads an item to the server. * @@ -1203,6 +1268,13 @@ export function generateThumbnails( id: QueueItemId ) { : thumbnailSource; const batchId = uuidv4(); + // Sub-sizes inherit the parent's UltraHDR status so that the + // resize step routes through libvips's uhdrload/uhdrsave pipeline + // (which preserves the gain map). Format transcoding is skipped + // for UltraHDR sources because converting to a different codec + // would strip the ISO 21496-1 gain map data. + const isUltraHdr = ultraHdrItems.has( item.id ); + // Read per-file format conversion data from the attachment response. const outputMimeType = attachment.image_output_format; const interlaced = attachment.image_save_progressive ?? false; @@ -1217,7 +1289,7 @@ export function generateThumbnails( id: QueueItemId ) { ] | null = null; - if ( outputMimeType ) { + if ( ! isUltraHdr && outputMimeType ) { thumbnailTranscodeOperation = await getTranscodeImageOperation( thumbnailSource, outputMimeType, @@ -1251,14 +1323,15 @@ export function generateThumbnails( id: QueueItemId ) { for ( const [ , names ] of dimensionGroups ) { const imageSize = allImageSizes[ names[ 0 ] ]; - // Build operations list for this thumbnail. + // Build operations list for this thumbnail. The resize step + // is UltraHDR-aware and will preserve the gain map automatically. const thumbnailOperations: Operation[] = [ [ OperationType.ResizeCrop, { resize: imageSize } ], ]; - // Add transcoding if format conversion is configured and - // the transparency check passed. - if ( thumbnailTranscodeOperation ) { + if ( ! isUltraHdr && thumbnailTranscodeOperation ) { + // Add transcoding if format conversion is configured and + // the transparency check passed. thumbnailOperations.push( thumbnailTranscodeOperation ); } @@ -1302,7 +1375,8 @@ export function generateThumbnails( id: QueueItemId ) { ? renameFile( thumbnailSource, attachment.filename ) : thumbnailSource; - // Add scaling to queue. + // Add scaling to queue. The resize step is UltraHDR-aware + // and will preserve the gain map automatically. const scaledOperations: Operation[] = [ [ OperationType.ResizeCrop, @@ -1316,8 +1390,8 @@ export function generateThumbnails( id: QueueItemId ) { ], ]; - // Add transcoding if format conversion is configured. - if ( thumbnailTranscodeOperation ) { + if ( ! isUltraHdr && thumbnailTranscodeOperation ) { + // Add transcoding if format conversion is configured. scaledOperations.push( thumbnailTranscodeOperation ); diff --git a/packages/upload-media/src/store/test/private-actions.js b/packages/upload-media/src/store/test/private-actions.js index c918fbf2ffa319..22b5ff1cfdf454 100644 --- a/packages/upload-media/src/store/test/private-actions.js +++ b/packages/upload-media/src/store/test/private-actions.js @@ -6,9 +6,15 @@ import { createBlobURL, revokeBlobURL } from '@wordpress/blob'; /** * Internal dependencies */ -import { getTranscodeImageOperation, finalizeItem } from '../private-actions'; +import { + getTranscodeImageOperation, + finalizeItem, + detectUltraHdr, + generateThumbnails, + removeItem, +} from '../private-actions'; import { OperationType } from '../types'; -import { vipsHasTransparency } from '../utils'; +import { vipsHasTransparency, vipsGetUltraHdrInfo } from '../utils'; // Mock @wordpress/blob jest.mock( '@wordpress/blob', () => ( { @@ -19,6 +25,9 @@ jest.mock( '@wordpress/blob', () => ( { // Mock vips utilities jest.mock( '../utils', () => ( { vipsHasTransparency: jest.fn(), + vipsGetUltraHdrInfo: jest.fn(), + terminateVipsWorker: jest.fn(), + maybeRecycleVipsWorker: jest.fn(), } ) ); describe( 'private actions', () => { @@ -417,4 +426,260 @@ describe( 'private actions', () => { expect( finishOperation ).not.toHaveBeenCalled(); } ); } ); + + describe( 'detectUltraHdr', () => { + const makeItem = () => ( { + file: new File( [ 'fake' ], 'photo.jpg', { + type: 'image/jpeg', + } ), + attachment: { meta: {} }, + } ); + + beforeEach( () => { + jest.clearAllMocks(); + } ); + + it( 'probes the file and finishes the operation when UltraHDR', async () => { + vipsGetUltraHdrInfo.mockResolvedValue( { + width: 1024, + height: 768, + hdrCapacity: 3, + } ); + const finishOperation = jest.fn(); + const item = makeItem(); + const select = { getItem: () => item }; + const dispatch = { finishOperation }; + + const thunk = detectUltraHdr( 'test-id' ); + await thunk( { select, dispatch } ); + + // The thunk only probes and tracks the item (tracking is verified + // via the generateThumbnails routing tests); the upload itself is + // untouched, so it finishes with no attachment updates. + expect( vipsGetUltraHdrInfo ).toHaveBeenCalledTimes( 1 ); + expect( finishOperation ).toHaveBeenCalledWith( 'test-id', {} ); + } ); + + it( 'finishes cleanly when buffer is not UltraHDR', async () => { + vipsGetUltraHdrInfo.mockResolvedValue( null ); + const finishOperation = jest.fn(); + const select = { getItem: () => makeItem() }; + const dispatch = { finishOperation }; + + const thunk = detectUltraHdr( 'test-id' ); + await thunk( { select, dispatch } ); + + expect( finishOperation ).toHaveBeenCalledWith( 'test-id', {} ); + } ); + + it( 'falls through gracefully when probe throws', async () => { + vipsGetUltraHdrInfo.mockRejectedValue( + new Error( 'wasm module unavailable' ) + ); + const finishOperation = jest.fn(); + const select = { getItem: () => makeItem() }; + const dispatch = { finishOperation }; + + const thunk = detectUltraHdr( 'test-id' ); + await thunk( { select, dispatch } ); + + // Probe failure must not cancel the upload — pass-through finish. + expect( finishOperation ).toHaveBeenCalledWith( 'test-id', {} ); + } ); + + it( 'returns early when item is not found', async () => { + const finishOperation = jest.fn(); + const select = { getItem: () => undefined }; + const dispatch = { finishOperation }; + + const thunk = detectUltraHdr( 'missing' ); + await thunk( { select, dispatch } ); + + expect( vipsGetUltraHdrInfo ).not.toHaveBeenCalled(); + expect( finishOperation ).not.toHaveBeenCalled(); + } ); + } ); + + describe( 'generateThumbnails UltraHDR gain-map routing', () => { + const ULTRAHDR_INFO = { width: 1024, height: 768, hdrCapacity: 3 }; + + // Two sizes with distinct dimensions/crop so they form two separate + // groups: a hard-cropped square thumbnail and a proportional medium + // downscale. Together they exercise both resize paths. + const ALL_IMAGE_SIZES = { + thumbnail: { width: 150, height: 150, crop: true }, + medium: { width: 300, height: 0, crop: false }, + }; + + const makeThumbnailItem = ( attachmentOverrides = {} ) => ( { + id: 'uhdr-parent', + file: new File( [ 'fake' ], 'photo.jpg', { type: 'image/jpeg' } ), + sourceFile: new File( [ 'fake' ], 'photo.jpg', { + type: 'image/jpeg', + } ), + attachment: { + id: 42, + filename: 'photo.jpg', + missing_image_sizes: [ 'thumbnail', 'medium' ], + exif_orientation: 1, + ...attachmentOverrides, + }, + } ); + + const makeHarness = ( item, settings = {} ) => { + const addSideloadItem = jest.fn(); + const finishOperation = jest.fn(); + const dispatch = { + addSideloadItem, + finishOperation, + addItem: jest.fn(), + }; + const select = { + getItem: () => item, + getSettings: () => ( { + allImageSizes: ALL_IMAGE_SIZES, + ...settings, + } ), + }; + return { select, dispatch, addSideloadItem }; + }; + + // Normalizes an operations list to the bare operation types, whether + // an entry is a plain type or a [ type, args ] tuple. + const operationTypes = ( sideloadArgs ) => + sideloadArgs.operations.map( ( op ) => + Array.isArray( op ) ? op[ 0 ] : op + ); + + // Marks an item as UltraHDR by running the real detectUltraHdr thunk, + // which is what populates the module-level tracking set that + // generateThumbnails reads. + const markUltraHdr = async ( item ) => { + vipsGetUltraHdrInfo.mockResolvedValue( ULTRAHDR_INFO ); + await detectUltraHdr( item.id )( { + select: { getItem: () => item }, + dispatch: { finishOperation: jest.fn() }, + } ); + }; + + // removeItem owns teardown of the tracking set, so use it to reset + // state between tests (and to exercise the cleanup path itself). + const clearTracking = async ( id ) => { + await removeItem( id )( { + select: { getItem: () => ( {} ), getAllItems: () => [] }, + dispatch: jest.fn(), + } ); + }; + + beforeEach( () => { + jest.clearAllMocks(); + } ); + + afterEach( async () => { + await clearTracking( 'uhdr-parent' ); + } ); + + it( 'routes every sub-size through resize without transcoding, preserving the gain map', async () => { + const item = makeThumbnailItem( { + image_output_format: 'image/webp', + } ); + await markUltraHdr( item ); + + const { select, dispatch, addSideloadItem } = makeHarness( item ); + await generateThumbnails( item.id )( { select, dispatch } ); + + // Both the cropped thumbnail and the proportional medium size + // were dispatched as sideloads. + const sizes = addSideloadItem.mock.calls.map( + ( [ args ] ) => args.additionalData.image_size + ); + expect( sizes ).toEqual( + expect.arrayContaining( [ 'thumbnail', 'medium' ] ) + ); + + // Crucially, none of the sub-size operations transcode the image, + // which would strip the ISO 21496-1 gain map. Each one resizes + // (gain-map-aware) and uploads. + for ( const [ args ] of addSideloadItem.mock.calls ) { + const types = operationTypes( args ); + expect( types ).toContain( OperationType.ResizeCrop ); + expect( types ).toContain( OperationType.Upload ); + expect( types ).not.toContain( OperationType.TranscodeImage ); + } + } ); + + it( 'keeps the over-threshold -scaled full-size copy as UltraHDR (no transcode)', async () => { + const originalCreateImageBitmap = global.createImageBitmap; + global.createImageBitmap = jest.fn( async () => ( { + width: 5000, + height: 4000, + close: jest.fn(), + } ) ); + + try { + const item = makeThumbnailItem( { + image_output_format: 'image/webp', + } ); + await markUltraHdr( item ); + + const { select, dispatch, addSideloadItem } = makeHarness( + item, + { bigImageSizeThreshold: 2560 } + ); + await generateThumbnails( item.id )( { select, dispatch } ); + + const scaledCall = addSideloadItem.mock.calls.find( + ( [ args ] ) => args.additionalData.image_size === 'scaled' + ); + expect( scaledCall ).toBeDefined(); + + const types = operationTypes( scaledCall[ 0 ] ); + expect( types ).toContain( OperationType.ResizeCrop ); + expect( types ).not.toContain( OperationType.TranscodeImage ); + } finally { + global.createImageBitmap = originalCreateImageBitmap; + } + } ); + + it( 'transcodes sub-sizes for non-UltraHDR sources', async () => { + // This item is never marked UltraHDR, so the gain-map guard does + // not apply and the configured format conversion runs as usual. + const item = makeThumbnailItem( { + image_output_format: 'image/webp', + } ); + + const { select, dispatch, addSideloadItem } = makeHarness( item ); + await generateThumbnails( item.id )( { select, dispatch } ); + + const anyTranscode = addSideloadItem.mock.calls.some( + ( [ args ] ) => + operationTypes( args ).includes( + OperationType.TranscodeImage + ) + ); + expect( anyTranscode ).toBe( true ); + } ); + + it( 'clears UltraHDR tracking on removeItem so later processing transcodes normally', async () => { + const item = makeThumbnailItem( { + image_output_format: 'image/webp', + } ); + await markUltraHdr( item ); + // Both success and cancellation route through removeItem. + await clearTracking( item.id ); + + const { select, dispatch, addSideloadItem } = makeHarness( item ); + await generateThumbnails( item.id )( { select, dispatch } ); + + // With tracking cleared the source is treated as a normal JPEG, + // re-enabling transcoding — proving the set was emptied. + const anyTranscode = addSideloadItem.mock.calls.some( + ( [ args ] ) => + operationTypes( args ).includes( + OperationType.TranscodeImage + ) + ); + expect( anyTranscode ).toBe( true ); + } ); + } ); } ); diff --git a/packages/upload-media/src/store/types.ts b/packages/upload-media/src/store/types.ts index 95b635ab1bfdfe..12b315709d88e2 100644 --- a/packages/upload-media/src/store/types.ts +++ b/packages/upload-media/src/store/types.ts @@ -243,6 +243,11 @@ export interface Attachment { featured_media?: number; missing_image_sizes?: string[]; poster?: string; + meta?: + | [] + | { + [ k: string ]: unknown; + }; /** * EXIF orientation value from the original image. * Values 1-8 follow the EXIF specification. @@ -287,6 +292,8 @@ export enum OperationType { TranscodeImage = 'TRANSCODE_IMAGE', ThumbnailGeneration = 'THUMBNAIL_GENERATION', Finalize = 'FINALIZE', + // UltraHDR operations + DetectUltraHdr = 'DETECT_ULTRAHDR', } /** diff --git a/packages/upload-media/src/store/utils/index.ts b/packages/upload-media/src/store/utils/index.ts index 782e7c61d71d8b..841bda99d6482f 100644 --- a/packages/upload-media/src/store/utils/index.ts +++ b/packages/upload-media/src/store/utils/index.ts @@ -125,9 +125,24 @@ export async function vipsHasTransparency( url: string ) { return hasTransparency( await response.arrayBuffer() ); } +/** + * Probes a JPEG buffer for UltraHDR (ISO 21496-1 gain map) support using vips + * in a web worker. + * + * @param buffer Image buffer to probe. + * @return UltraHDR info if the buffer is a valid UltraHDR JPEG, otherwise null. + */ +export async function vipsGetUltraHdrInfo( buffer: ArrayBuffer ) { + const { vipsGetUltraHdrInfo: getUltraHdrInfo } = await loadVipsModule(); + return getUltraHdrInfo( buffer ); +} + /** * Resizes an image using vips in a web worker. * + * UltraHDR JPEGs are auto-detected by libvips and their gain map is + * preserved through the resize. + * * @param id Queue item ID. * @param file File object. * @param resize Resize options (width, height, crop). diff --git a/packages/vips/CHANGELOG.md b/packages/vips/CHANGELOG.md index 4a4763ad35e0a8..e8f863d86295b3 100644 --- a/packages/vips/CHANGELOG.md +++ b/packages/vips/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +### New Features + +- Add `getUltraHdrInfo` for probing UltraHDR (ISO 21496-1 gain map) JPEGs ([#74873](https://github.com/WordPress/gutenberg/pull/74873)). +- Preserve UltraHDR gain maps through `resizeImage`: libvips's `uhdrload`/`uhdrsave` priority is leveraged automatically, the save path keeps `icc|gainmap` metadata, and positional crop now also crops the attached gain map ([#74873](https://github.com/WordPress/gutenberg/pull/74873)). +- Bump `wasm-vips` to `^0.0.17` for native UltraHDR support ([#74873](https://github.com/WordPress/gutenberg/pull/74873)). + ## 2.0.0 (2026-05-27) ### Breaking Changes diff --git a/packages/vips/README.md b/packages/vips/README.md index e5fc1146909014..62be8adcd642b6 100644 --- a/packages/vips/README.md +++ b/packages/vips/README.md @@ -57,6 +57,20 @@ _Parameters_ - _quality_ Desired quality. - _interlaced_ Whether to use interlaced/progressive mode. Only used if the outputType supports it. +### getUltraHdrInfo + +Probes a JPEG to determine whether it is an UltraHDR image with an embedded gain map. + +Returns dimensions and HDR headroom on success, or `null` if the buffer is not a valid UltraHDR JPEG (no gain map, decode failure, or unsupported format). + +_Parameters_ + +- _buffer_ `ArrayBuffer`: Image buffer. + +_Returns_ + +- `Promise< UltraHdrInfo | null >`: UltraHDR info, or null when the buffer is not UltraHDR. + ### hasTransparency Determines whether an image has an alpha channel. @@ -73,6 +87,8 @@ _Returns_ Resizes an image using vips. +UltraHDR JPEGs are auto-detected and preserved: libvips's `uhdrload*` has higher priority than `jpegload*`, so `newFromBuffer`/`thumbnailBuffer` decode the gain map alongside the base image, and `jpegsave*` delegates to `uhdrsave*` on output when a gain map is attached. + _Parameters_ - _id_ `ItemId`: Item ID. @@ -146,6 +162,20 @@ _Parameters_ - _quality_ Desired quality. - _interlaced_ Whether to use interlaced/progressive mode. Only used if the outputType supports it. +### vipsGetUltraHdrInfo + +Probes a JPEG to determine whether it is an UltraHDR image with an embedded gain map. + +Returns dimensions and HDR headroom on success, or `null` if the buffer is not a valid UltraHDR JPEG (no gain map, decode failure, or unsupported format). + +_Parameters_ + +- _buffer_ `ArrayBuffer`: Image buffer. + +_Returns_ + +- `Promise< UltraHdrInfo | null >`: UltraHDR info, or null when the buffer is not UltraHDR. + ### vipsHasTransparency Determines whether an image has an alpha channel. @@ -162,6 +192,8 @@ _Returns_ Resizes an image using vips. +UltraHDR JPEGs are auto-detected and preserved: libvips's `uhdrload*` has higher priority than `jpegload*`, so `newFromBuffer`/`thumbnailBuffer` decode the gain map alongside the base image, and `jpegsave*` delegates to `uhdrsave*` on output when a gain map is attached. + _Parameters_ - _id_ `ItemId`: Item ID. diff --git a/packages/vips/package.json b/packages/vips/package.json index 813b18a8246d57..a17ca7d7b0f1c2 100644 --- a/packages/vips/package.json +++ b/packages/vips/package.json @@ -56,7 +56,7 @@ "types": "build-types", "dependencies": { "@wordpress/worker-threads": "file:../worker-threads", - "wasm-vips": "^0.0.16" + "wasm-vips": "^0.0.17" }, "publishConfig": { "access": "public" diff --git a/packages/vips/src/index.ts b/packages/vips/src/index.ts index 28371ef2e6b532..799ffbf7051fe4 100644 --- a/packages/vips/src/index.ts +++ b/packages/vips/src/index.ts @@ -221,6 +221,11 @@ function applyResizeAndCrop< width: number; height: number; crop: ( ...args: number[] ) => T; + // Optional UltraHDR support: present on Vips.Image instances when the + // source has an embedded gain map. + gainmap?: T; + copy?: () => T; + setImage?: ( name: string, value: T ) => void; }, >( resize: ImageSizeCrop, @@ -298,7 +303,46 @@ function applyResizeAndCrop< const cropWidth = Math.min( image.width, target.width ); const cropHeight = Math.min( image.height, target.height ); - return image.crop( left, top, cropWidth, cropHeight ); + const cropped = image.crop( left, top, cropWidth, cropHeight ); + + // For UltraHDR sources, also crop the attached gain map. The gain map + // can be smaller than the main image, so we scale the crop coordinates + // to its resolution. See: + // https://www.libvips.org/API/current/uhdr.html#a-la-carte-processing + const gainmap = image.gainmap; + const copy = cropped.copy; + const setImage = cropped.setImage; + if ( ! gainmap || ! copy || ! setImage ) { + return cropped; + } + + // Scale the crop rect to the gain map's resolution. `crop` expects integer + // pixel coordinates, so round here rather than relying on an implicit + // float-to-int conversion, and clamp to the gain map bounds so the rect + // never extends past its edges. + const hscale = gainmap.width / image.width; + const vscale = gainmap.height / image.height; + const gainmapLeft = Math.round( left * hscale ); + const gainmapTop = Math.round( top * vscale ); + const gainmapWidth = Math.min( + Math.round( cropWidth * hscale ), + gainmap.width - gainmapLeft + ); + const gainmapHeight = Math.min( + Math.round( cropHeight * vscale ), + gainmap.height - gainmapTop + ); + const newGainmap = gainmap.crop( + gainmapLeft, + gainmapTop, + gainmapWidth, + gainmapHeight + ); + + // setImage mutates, so produce a unique copy first. + const result = copy.call( cropped ); + setImage.call( result, 'gainmap', newGainmap ); + return result; } /** @@ -313,9 +357,9 @@ function buildSaveOptions( quality: number ): SaveOptions< typeof type > { const saveOptions: SaveOptions< typeof type > = { - // Strip metadata except ICC color profiles, + // Strip metadata except ICC color profiles or gainmaps, // matching WordPress core's behavior. - keep: 'icc', + keep: 'icc|gainmap', }; if ( supportsQuality( type ) ) { @@ -333,6 +377,11 @@ function buildSaveOptions( /** * Resizes an image using vips. * + * UltraHDR JPEGs are auto-detected and preserved: libvips's `uhdrload*` + * has higher priority than `jpegload*`, so `newFromBuffer`/`thumbnailBuffer` + * decode the gain map alongside the base image, and `jpegsave*` delegates + * to `uhdrsave*` on output when a gain map is attached. + * * @param id Item ID. * @param buffer Original file buffer. * @param type Mime type. @@ -424,6 +473,66 @@ export async function resizeImage( } } +/** + * Information returned by getUltraHdrInfo() for a successfully probed + * UltraHDR JPEG. + */ +interface UltraHdrInfo { + width: number; + height: number; + /** HDR headroom in stops (log2 of the linear capacity). */ + hdrCapacity: number; +} + +/** + * Probes a JPEG to determine whether it is an UltraHDR image with an embedded + * gain map. + * + * Returns dimensions and HDR headroom on success, or `null` if the buffer is + * not a valid UltraHDR JPEG (no gain map, decode failure, or unsupported + * format). + * + * @param buffer Image buffer. + * @return UltraHDR info, or null when the buffer is not UltraHDR. + */ +export async function getUltraHdrInfo( + buffer: ArrayBuffer +): Promise< UltraHdrInfo | null > { + try { + const vips = await getVips(); + const image = vips.Image.uhdrloadBuffer( buffer ); + if ( ! image.gainmap ) { + cleanup?.(); + return null; + } + + // `gainmap-hdr-capacity-max` is libultrahdr's linear-scale max capacity. + // Convert to log2 stops so the value stored in attachment metadata + // represents HDR headroom in stops. + let hdrCapacityLinear = 1; + try { + hdrCapacityLinear = image.getDouble( 'gainmap-hdr-capacity-max' ); + } catch { + // Field may be missing; fall back to no headroom. + } + const hdrCapacity = + hdrCapacityLinear > 0 ? Math.log2( hdrCapacityLinear ) : 0; + + const info: UltraHdrInfo = { + width: image.width, + height: image.pageHeight, + hdrCapacity, + }; + + cleanup?.(); + return info; + } catch { + // Not an UltraHDR image (or libultrahdr decoder unavailable). + cleanup?.(); + return null; + } +} + /** * Rotates an image based on EXIF orientation value. * @@ -555,5 +664,6 @@ export { resizeImage as vipsResizeImage, rotateImage as vipsRotateImage, hasTransparency as vipsHasTransparency, + getUltraHdrInfo as vipsGetUltraHdrInfo, cancelOperations as vipsCancelOperations, }; diff --git a/packages/vips/src/test/ultrahdr.ts b/packages/vips/src/test/ultrahdr.ts new file mode 100644 index 00000000000000..d8f5da2bbee543 --- /dev/null +++ b/packages/vips/src/test/ultrahdr.ts @@ -0,0 +1,245 @@ +/** + * Internal dependencies + */ +import { getUltraHdrInfo, resizeImage } from '../'; + +const mockThumbnailImage = jest.fn(); +const mockThumbnailBuffer = jest.fn(); +const mockNewFromBuffer = jest.fn(); +const mockUhdrLoadBuffer = jest.fn(); +const mockWriteToBuffer = jest.fn( () => ( { buffer: new ArrayBuffer( 0 ) } ) ); +const mockGetDouble = jest.fn(); +const mockSetImage = jest.fn(); +const mockCrop = jest.fn(); + +class MockImage { + width = 100; + height = 100; + pageHeight = 100; + gainmap: MockImage | undefined; + thumbnailImage = mockThumbnailImage.mockImplementation( () => this ); + writeToBuffer = mockWriteToBuffer; + getDouble = mockGetDouble; + crop = mockCrop.mockImplementation( () => this ); + copy = jest.fn( () => this ); + setImage = mockSetImage; +} + +class MockVipsImage { + static thumbnailBuffer = mockThumbnailBuffer.mockImplementation( + () => new MockImage() + ); + static newFromBuffer = mockNewFromBuffer.mockImplementation( + () => new MockImage() + ); + static uhdrloadBuffer = mockUhdrLoadBuffer; +} + +jest.mock( 'wasm-vips', () => + jest.fn( () => ( { + Image: MockVipsImage, + // getVips() calls Cache.max(0) to disable libvips's operation cache. + Cache: { max: jest.fn() }, + } ) ) +); + +describe( 'UltraHDR helpers', () => { + afterEach( () => { + jest.clearAllMocks(); + } ); + + describe( 'getUltraHdrInfo', () => { + it( 'returns dims + log2 capacity for valid UltraHDR', async () => { + const image = new MockImage(); + image.width = 1024; + image.pageHeight = 768; + // Mark as having a gain map so the helper recognizes it. + image.gainmap = new MockImage(); + mockGetDouble.mockReturnValueOnce( 8 ); // linear capacity 8 → 3 stops + mockUhdrLoadBuffer.mockReturnValueOnce( image ); + + const buffer = new ArrayBuffer( 16 ); + const result = await getUltraHdrInfo( buffer ); + + expect( mockUhdrLoadBuffer ).toHaveBeenCalledWith( buffer ); + expect( mockGetDouble ).toHaveBeenCalledWith( + 'gainmap-hdr-capacity-max' + ); + expect( result ).toEqual( { + width: 1024, + height: 768, + hdrCapacity: 3, + } ); + } ); + + it( 'returns null when image has no gain map', async () => { + const image = new MockImage(); + // gainmap stays undefined. + mockUhdrLoadBuffer.mockReturnValueOnce( image ); + + const result = await getUltraHdrInfo( new ArrayBuffer( 16 ) ); + + expect( result ).toBeNull(); + expect( mockGetDouble ).not.toHaveBeenCalled(); + } ); + + it( 'returns null when uhdrloadBuffer throws', async () => { + mockUhdrLoadBuffer.mockImplementationOnce( () => { + throw new Error( 'not an UltraHDR jpeg' ); + } ); + + const result = await getUltraHdrInfo( new ArrayBuffer( 16 ) ); + + expect( result ).toBeNull(); + } ); + + it( 'falls back to zero capacity when metadata missing', async () => { + const image = new MockImage(); + image.width = 256; + image.pageHeight = 256; + image.gainmap = new MockImage(); + mockGetDouble.mockImplementationOnce( () => { + throw new Error( 'no such field' ); + } ); + mockUhdrLoadBuffer.mockReturnValueOnce( image ); + + const result = await getUltraHdrInfo( new ArrayBuffer( 16 ) ); + + expect( result ).toEqual( { + width: 256, + height: 256, + hdrCapacity: 0, + } ); + } ); + } ); + + describe( 'resizeImage uses libvips auto-detection for UltraHDR', () => { + it( 'uses newFromBuffer/thumbnailBuffer/writeToBuffer regardless of UltraHDR status', async () => { + // libvips auto-detects UltraHDR via uhdrload's higher priority, + // and writeToBuffer auto-delegates to uhdrsave when a gain map + // is attached. No separate uhdr code path is needed here. + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 200, + height: 200, + } ); + + expect( mockUhdrLoadBuffer ).not.toHaveBeenCalled(); + expect( mockNewFromBuffer ).toHaveBeenCalled(); + expect( mockThumbnailBuffer ).toHaveBeenCalled(); + expect( mockWriteToBuffer ).toHaveBeenCalled(); + } ); + + it( 'passes keep:icc|gainmap to writeToBuffer to preserve gain maps', async () => { + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 200, + height: 200, + } ); + + expect( mockWriteToBuffer ).toHaveBeenCalledWith( + '.jpeg', + expect.objectContaining( { keep: 'icc|gainmap' } ) + ); + } ); + + it( 'crops the gain map alongside the main image on positional crop', async () => { + // Build a thumbnail with a gain map at half resolution. + const thumb = new MockImage(); + thumb.width = 200; + thumb.height = 200; + thumb.pageHeight = 200; + const thumbGainmap = new MockImage(); + thumbGainmap.width = 100; + thumbGainmap.height = 100; + thumb.gainmap = thumbGainmap; + mockThumbnailBuffer.mockImplementationOnce( () => thumb ); + + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 100, + height: 100, + crop: [ 'center', 'top' ], + } ); + + // Main image is cropped at full resolution. + expect( thumb.crop ).toHaveBeenCalledWith( 50, 0, 100, 100 ); + // Gain map is cropped at its (halved) scale. + expect( thumbGainmap.crop ).toHaveBeenCalledWith( 25, 0, 50, 50 ); + // Result is set as the gain map on a copy of the cropped image. + expect( mockSetImage ).toHaveBeenCalledWith( + 'gainmap', + expect.anything() + ); + } ); + + it( 'preserves the gain map on a simple downscale without manually cropping it', async () => { + // A proportional downscale (no crop) relies on libvips's + // `thumbnail` to resize the attached gain map in lockstep, so the + // resize step must not crop it manually, and `writeToBuffer` must + // keep the gain map metadata. + const thumb = new MockImage(); + thumb.width = 300; + thumb.height = 225; + thumb.pageHeight = 225; + thumb.gainmap = new MockImage(); + mockThumbnailBuffer.mockImplementationOnce( () => thumb ); + + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 300, + height: 0, + } ); + + // No positional crop, so neither the image nor its gain map is + // cropped, and no gain map is re-attached manually. + expect( thumb.crop ).not.toHaveBeenCalled(); + expect( mockSetImage ).not.toHaveBeenCalled(); + // The gain map still survives via the keep flag on save. + expect( mockWriteToBuffer ).toHaveBeenCalledWith( + '.jpeg', + expect.objectContaining( { keep: 'icc|gainmap' } ) + ); + } ); + + it( 'preserves the gain map on a boolean (centre) crop without manually cropping it', async () => { + // A boolean crop is handled entirely by `thumbnail` (with its + // crop option), which updates the gain map automatically, so the + // resize step must not crop the gain map itself. + const thumb = new MockImage(); + thumb.width = 150; + thumb.height = 150; + thumb.pageHeight = 150; + thumb.gainmap = new MockImage(); + mockThumbnailBuffer.mockImplementationOnce( () => thumb ); + + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 150, + height: 150, + crop: true, + } ); + + expect( mockThumbnailBuffer ).toHaveBeenCalled(); + expect( thumb.crop ).not.toHaveBeenCalled(); + expect( mockSetImage ).not.toHaveBeenCalled(); + expect( mockWriteToBuffer ).toHaveBeenCalledWith( + '.jpeg', + expect.objectContaining( { keep: 'icc|gainmap' } ) + ); + } ); + + it( 'skips gain-map crop when no gain map is present', async () => { + const thumb = new MockImage(); + thumb.width = 200; + thumb.height = 200; + thumb.pageHeight = 200; + // gainmap stays undefined. + mockThumbnailBuffer.mockImplementationOnce( () => thumb ); + + await resizeImage( 'itemId', new ArrayBuffer( 16 ), 'image/jpeg', { + width: 100, + height: 100, + crop: [ 'center', 'top' ], + } ); + + expect( thumb.crop ).toHaveBeenCalledWith( 50, 0, 100, 100 ); + expect( mockSetImage ).not.toHaveBeenCalled(); + } ); + } ); +} ); diff --git a/packages/vips/src/types.ts b/packages/vips/src/types.ts index d0978ff7979634..685586ee3cf6ca 100644 --- a/packages/vips/src/types.ts +++ b/packages/vips/src/types.ts @@ -33,9 +33,21 @@ type Interesting = * iptc: Keep IPTC metadata. * icc: Keep ICC metadata. * other: Keep other metadata (e.g. PNG comments and some TIFF tags). + * gainmap: Keep gainmap metadata (UltraHDR JPEG). * all: Keep all metadata. + * + * Multiple values can be combined with `|`, e.g. `'icc|gainmap'`. */ -type ForeignKeep = 'none' | 'exif' | 'xmp' | 'iptc' | 'icc' | 'other' | 'all'; +type ForeignKeepValue = + | 'none' + | 'exif' + | 'xmp' + | 'iptc' + | 'icc' + | 'other' + | 'gainmap' + | 'all'; +type ForeignKeep = ForeignKeepValue | `${ ForeignKeepValue }|${ string }`; /** * The rendering intent.'absolute' is best for diff --git a/packages/vips/src/vips-worker.ts b/packages/vips/src/vips-worker.ts index 17e7bd50add466..bc8e9a7253d0d6 100644 --- a/packages/vips/src/vips-worker.ts +++ b/packages/vips/src/vips-worker.ts @@ -103,6 +103,9 @@ export async function vipsCompressImage( /** * Resizes an image using vips in a worker. * + * UltraHDR JPEGs are auto-detected by libvips and their gain map is + * preserved through the resize. + * * @param id Item ID. * @param buffer Original file buffer. * @param type Mime type. @@ -142,6 +145,23 @@ export async function vipsHasTransparency( return api.hasTransparency( buffer ); } +/** + * Probes a JPEG buffer for UltraHDR (ISO 21496-1 gain map) support using vips + * in a worker. + * + * @param buffer Image buffer to probe. + * @return UltraHDR info (dimensions and HDR headroom in stops) if the buffer + * is a valid UltraHDR JPEG; otherwise null. + */ +export async function vipsGetUltraHdrInfo( buffer: ArrayBuffer ): Promise< { + width: number; + height: number; + hdrCapacity: number; +} | null > { + const api = getWorkerAPI(); + return api.getUltraHdrInfo( buffer ); +} + /** * Rotates an image based on EXIF orientation using vips in a worker. * diff --git a/packages/vips/src/worker.ts b/packages/vips/src/worker.ts index 2fad366825604f..6cad9e16731676 100644 --- a/packages/vips/src/worker.ts +++ b/packages/vips/src/worker.ts @@ -21,6 +21,7 @@ import { resizeImage, rotateImage, hasTransparency, + getUltraHdrInfo, } from './index'; /** @@ -33,6 +34,7 @@ const api = { resizeImage, rotateImage, hasTransparency, + getUltraHdrInfo, }; expose( api ); diff --git a/test/e2e/assets/1024x768_e2e_test_image_ultrahdr.jpeg b/test/e2e/assets/1024x768_e2e_test_image_ultrahdr.jpeg new file mode 100644 index 00000000000000..405327cb42b594 Binary files /dev/null and b/test/e2e/assets/1024x768_e2e_test_image_ultrahdr.jpeg differ diff --git a/test/e2e/bin/gen-ultrahdr-fixture.mjs b/test/e2e/bin/gen-ultrahdr-fixture.mjs new file mode 100644 index 00000000000000..9ea019bca16b95 --- /dev/null +++ b/test/e2e/bin/gen-ultrahdr-fixture.mjs @@ -0,0 +1,76 @@ +/** + * One-shot generator for the UltraHDR e2e fixture. + * + * Regenerate with: + * cd test/e2e/bin + * node gen-ultrahdr-fixture.mjs ../assets/1024x768_e2e_test_image_ultrahdr.jpeg + * + * Synthesizes a small UltraHDR JPEG (ISO 21496-1 gain map) using wasm-vips's + * encoder so we don't need to vendor a binary asset from a third-party source. + * + * Run manually (not part of CI). `wasm-vips` resolves through the workspace + * via `@wordpress/vips`; console output is the script's user interface. + */ +/* eslint-disable import/no-extraneous-dependencies */ +import Vips from 'wasm-vips'; +import fs from 'node:fs'; + +const out = process.argv[ 2 ]; +if ( ! out ) { + console.error( 'Usage: node gen-ultrahdr-fixture.mjs ' ); + process.exit( 1 ); +} + +const vips = await Vips( {} ); + +const w = 1024; +const h = 768; + +// Base sRGB SDR rendition: simple horizontal gradient. +const xramp = vips.Image.identity( { size: w } ) + .cast( 'uchar' ) + .embed( 0, 0, w, h, { extend: 'copy' } ); +const yramp = vips.Image.identity( { size: h } ) + .cast( 'uchar' ) + .rot90() + .embed( 0, 0, w, h, { extend: 'copy' } ); +const constB = vips.Image.black( w, h ).add( 80 ).cast( 'uchar' ); +const base = xramp + .bandjoin( [ yramp, constB ] ) + .copy( { interpretation: 'srgb' } ); + +// Gain map: half-resolution greyscale, mid-grey for moderate boost. +const gw = w / 2; +const gh = h / 2; +const gainmap = vips.Image.black( gw, gh ) + .add( 128 ) + .cast( 'uchar' ) + .copy( { interpretation: 'b-w' } ); + +// Attach the gain map to the base. libvips understands the `gainmap` +// metadata key and embeds it in the saved UltraHDR JPEG. +base.setImage( 'gainmap', gainmap ); + +// Required ISO 21496-1 metadata fields libvips/libultrahdr expects on +// the base image. Per-channel fields are RGB triplets; capacity fields are +// scalars. Values match a moderate 3-stop boost. +base.setArrayDouble( 'gainmap-min-content-boost', [ 1.0, 1.0, 1.0 ] ); +base.setArrayDouble( 'gainmap-max-content-boost', [ 8.0, 8.0, 8.0 ] ); +base.setDouble( 'gainmap-hdr-capacity-min', 1.0 ); +base.setDouble( 'gainmap-hdr-capacity-max', 8.0 ); +base.setArrayDouble( 'gainmap-gamma', [ 1.0, 1.0, 1.0 ] ); +base.setArrayDouble( 'gainmap-offset-sdr', [ 0.015625, 0.015625, 0.015625 ] ); +base.setArrayDouble( 'gainmap-offset-hdr', [ 0.015625, 0.015625, 0.015625 ] ); +base.setInt( 'gainmap-base-rendition-is-hdr', 0 ); +base.setInt( 'gainmap-use-base-cg', 1 ); + +try { + const buf = base.uhdrsaveBuffer( { Q: 85 } ); + fs.writeFileSync( out, Buffer.from( buf ) ); + console.log( `Wrote ${ buf.length } bytes to ${ out }` ); +} catch ( err ) { + console.error( 'uhdrsaveBuffer failed:', err.message ); + console.error( vips.error || vips.Error?.message || '(no extra detail)' ); + process.exit( 1 ); +} +/* eslint-enable import/no-extraneous-dependencies */ diff --git a/test/e2e/specs/editor/various/client-side-media-processing.spec.js b/test/e2e/specs/editor/various/client-side-media-processing.spec.js index 90f5f13fb9dbbe..1b2487e70c62f4 100644 --- a/test/e2e/specs/editor/various/client-side-media-processing.spec.js +++ b/test/e2e/specs/editor/various/client-side-media-processing.spec.js @@ -11,6 +11,31 @@ const { v4: uuid } = require( 'uuid' ); */ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); +/** + * Probes a remote JPEG for an embedded UltraHDR gain map. + * + * Loaded lazily so the wasm-vips runtime (~10MB) only instantiates when an + * UltraHDR test actually runs. + * + * @param {string} url Image URL to fetch. + * @return {Promise<{ width: number, height: number, hasGainmap: boolean }>} Probe result. + */ +async function probeUltraHdrUrl( url ) { + const { default: Vips } = await import( 'wasm-vips' ); + const vips = await Vips( {} ); + const response = await fetch( url ); + if ( ! response.ok ) { + throw new Error( `Failed to fetch ${ url }: ${ response.status }` ); + } + const bytes = new Uint8Array( await response.arrayBuffer() ); + const image = vips.Image.uhdrloadBuffer( bytes ); + return { + width: image.width, + height: image.pageHeight, + hasGainmap: !! image.gainmap, + }; +} + /** * @typedef {import('@playwright/test').Page} Page */ @@ -270,6 +295,47 @@ test.describe( 'Client-side media processing', () => { expect( media.media_details.height ).toBe( 150 ); } ); + test( 'preserves UltraHDR gain map through sub-size resize', async ( { + editor, + mediaProcessingUtils, + requestUtils, + } ) => { + const media = await mediaProcessingUtils.uploadImageAndGetMedia( + editor, + requestUtils, + '1024x768_e2e_test_image_ultrahdr.jpeg' + ); + + expect( media.mime_type ).toBe( 'image/jpeg' ); + expect( media.media_details.width ).toBe( 1024 ); + expect( media.media_details.height ).toBe( 768 ); + + // Default sub-sizes must be generated. + const sizes = media.media_details.sizes; + expect( sizes.thumbnail ).toBeDefined(); + expect( sizes.medium ).toBeDefined(); + + // Every generated sub-size must still carry the embedded UltraHDR + // gain map. This proves the resize step routed through libvips's + // uhdrload/uhdrsave pipeline rather than the regular JPEG path + // (which would have stripped the gain map). + // + // `thumbnail` is a hard-cropped square (exercising the manual + // gain-map crop path) while `medium` is a proportional downscale + // (exercising libvips's automatic gain-map resize), so checking both + // covers both code paths end to end. + for ( const sizeName of [ 'thumbnail', 'medium' ] ) { + const size = sizes[ sizeName ]; + const probed = await probeUltraHdrUrl( size.source_url ); + expect( probed.hasGainmap ).toBe( true ); + // The decoded base image must match the registered sub-size + // dimensions, confirming the gain map travels with a correctly + // resized/cropped image rather than a stale full-size one. + expect( probed.width ).toBe( size.width ); + expect( probed.height ).toBe( size.height ); + } + } ); + test( 'scales oversized images and generates the standard sub-sizes', async ( { editor, mediaProcessingUtils, diff --git a/test/performance/specs/media-processing.spec.js b/test/performance/specs/media-processing.spec.js index adaf689cf82996..494c5b5c19c0d6 100644 --- a/test/performance/specs/media-processing.spec.js +++ b/test/performance/specs/media-processing.spec.js @@ -48,6 +48,12 @@ async function getVips() { } ); }, } ); + // Disable libvips's operation cache. The cache pins decoded image + // references, which in turn prevents libheif from releasing its + // internal AVIF decoder state. Across the 8 perf-test iterations on + // a 3000x2000 AVIF, this manifests as a `heif: Memory allocation + // error` once the WASM heap is exhausted. + vips.Cache.max( 0 ); return vips; }