Repository navigation
encode.webgpu.Class.GainMapEncoder
@monogrid/gainmap-js / encode/webgpu / GainMapEncoder
Defined in: src/encode/webgpu/GainMapEncoder.ts:115
Encodes an HDR image into an SDR rendition + a gain map, on the GPU, with TSL nodes.
GainMapEncoder.sdr and GainMapEncoder.gainMap are live texture nodes: every encoding parameter is a uniform, changing one re-renders the affected nodes once, when they're next used. No renderer is needed until the result is read back with GainMapEncoder.read or GainMapEncoder.compress.
Read back images are oriented top to bottom (like ImageData), no flipY is needed when compressing them.
import { GainMapEncoder } from '@monogrid/gainmap-js/encode/webgpu'
import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
import { AgXToneMapping, MeshBasicNodeMaterial, WebGPURenderer } from 'three/webgpu'
const renderer = new WebGPURenderer()
await renderer.init()
const image = await new EXRLoader().loadAsync('image.exr')
const encoder = new GainMapEncoder(image, { toneMapping: AgXToneMapping })
// encode the full HDR range
await encoder.fitMaxContentBoost(renderer)
// live previews, they follow any parameter change
sdrMaterial.colorNode = encoder.sdr
gainMapMaterial.colorNode = encoder.gainMap
hdrMaterial.colorNode = encoder.createPreviewNode() // decodes the encoded result
encoder.sdr.exposure.value = 0.9
encoder.gainMap.gamma.value.setScalar(1.1)
// compress and embed into a single JPEG file
const result = await encoder.compress(renderer, { mimeType: 'image/jpeg', quality: 0.9 })
const jpeg = encodeJPEGMetadata(result)new GainMapEncoder(
image,options?):GainMapEncoder
Defined in: src/encode/webgpu/GainMapEncoder.ts:131
DataTextureLoaderTexData | HDRInput
the HDR image: a texture, raw texture data (e.g. from a worker) or a TSL node (which requires width and height)
encoding parameters, all of them can be changed later through the node uniforms
GainMapEncoder
readonlygainMap:GainMapEncoderNode
Defined in: src/encode/webgpu/GainMapEncoder.ts:123
The gain map node
readonlysdr:SDRRenditionNode
Defined in: src/encode/webgpu/GainMapEncoder.ts:119
The SDR rendition node
get height():
number
Defined in: src/encode/webgpu/GainMapEncoder.ts:146
Height of the encoded images
number
get metadata():
GainMapMetadata
Defined in: src/encode/webgpu/GainMapEncoder.ts:151
The gain map metadata matching the current parameters
get width():
number
Defined in: src/encode/webgpu/GainMapEncoder.ts:144
Width of the encoded images
number
compress(
renderer,options):Promise<GainMapEncoderCompressedResult>
Defined in: src/encode/webgpu/GainMapEncoder.ts:213
Renders (if needed), reads back and compresses the SDR rendition and gain map
WebGPURenderer
CompressOptions & object
compression options, flipY is not needed
Promise<GainMapEncoderCompressedResult>
if the browser does not support createImageBitmap
createPreviewNode():
GainMapNode
Defined in: src/encode/webgpu/GainMapEncoder.ts:173
Creates a GainMapNode decoding the encoded result on the fly,
it follows any change to the encoding parameters.
Its maxDisplayBoost is initialized to restore the full HDR range, and is not changed afterwards.
dispose():
void
Defined in: src/encode/webgpu/GainMapEncoder.ts:235
Disposes the nodes, and the HDR texture if it was created by the encoder from raw data
void
fitMaxContentBoost(
renderer):Promise<number>
Defined in: src/encode/webgpu/GainMapEncoder.ts:162
Finds the max value of the HDR image and uses it as maxContentBoost,
so the full HDR range is encoded.
WebGPURenderer
Promise<number>
the new maxContentBoost
read(
renderer):Promise<GainMapEncoderRawResult>
Defined in: src/encode/webgpu/GainMapEncoder.ts:192
Renders (if needed) and reads back the raw SDR rendition and gain map
WebGPURenderer
Promise<GainMapEncoderRawResult>
render(
renderer):void
Defined in: src/encode/webgpu/GainMapEncoder.ts:182
Renders the SDR rendition and the gain map, only if something changed since the last render
WebGPURenderer
void