Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@monogrid/gainmap-js
Advanced tools
A Javascript (TypeScript) Port of Adobe Gainmap Technology for storing HDR Images using an SDR Image + a gain map
A Javascript (TypeScript) Encoder/Decoder Implementation of Adobe's Gain Map Technology for storing HDR Images using an SDR Image + a "Gain map"
:warning: This library is primarly intended for encoding and decoding gain map images for the three.js 3D Library
It can be used for general encode/decode of gain maps but it depends on the three.js library which, in itself, is quite heavy if you only use it to encode/decode gainmaps.
$ npm install @monogrid/gainmap-js threejs
See here for a detailed explanation, here are some relevant parts:
A gain map is a single file with a second pseudo-image embedded in it to create an optimized result for a specific monitor. It can be used to generate the HDR version (which looks dramatically better where supported), the SDR version (without tone mapping to ensures great quality), or anything in between (to better support less capable HDR displays).
Gain maps are not a new type of file, but rather a technology which can be embedded into a variety of image formats. There are reference specs already for the JPG, AVIF, JXL, and HEIF file formats. JPG is especially notable as it could not properly support HDR without gain maps and it offers a very useful bridge to the future (i.e. highly compatible with today’s software).
A gain map includes:
- A base (default) image. This can be an SDR or an HDR image (JPG gain maps are always encoded with SDR as the base). If the browser or viewing software does not understand gain maps, it will just the treat file as if it were just the base image.
- The gain map. This is a secondary “image” embedded in the file. It is not a real image, but rather contains data to convert each pixel from the base image into the other (SDR or HDR) version of the image.
- Gain map metadata. This tells the browser how the gain map is encoded as well as critical information to optimize rendering on any display.
Refer to the WIKI for detailed documentation about the API.
The main use case of this library is to decode a JPEG file that contains gain map data
and use it instead of a traditional .exr
or .hdr
image.
This approach lets you load a single file with an embedded Gain Map.
The advantage is to have a single file to load.
The disadvantages are:
@monogrid/gainmap-js/libultrahdr
package which is heavier and requires loading a wasm
in order to extract the gainmap from the JPEG.import { JPEGRLoader } from '@monogrid/gainmap-js/libultrahdr'
import {
EquirectangularReflectionMapping,
LinearFilter,
Mesh,
MeshBasicMaterial,
PerspectiveCamera,
PlaneGeometry,
Scene,
WebGLRenderer
} from 'three'
const renderer = new WebGLRenderer()
const loader = new JPEGRLoader(renderer)
const result = loader.load('gainmap.jpeg')
// `result` can be used to populate a Texture
const scene = new Scene()
const mesh = new Mesh(
new PlaneGeometry(),
new MeshBasicMaterial({ map: result.renderTarget.texture })
)
scene.add(mesh)
renderer.render(scene, new PerspectiveCamera())
// `result.renderTarget.texture` must be
// converted to `DataTexture` in order
// to use it as Equirectanmgular scene background
// if needed
scene.background = result.toDataTexture()
scene.background.mapping = EquirectangularReflectionMapping
scene.background.minFilter = LinearFilter
Using separate files will get rid of the limitations of using a single JPEG file but it will force to use three separate files
This solution will use the lighter @monogrid/gainmap-js
package which will not load a wasm
file and contains less code.
import { GainMapLoader } from '@monogrid/gainmap-js'
import {
EquirectangularReflectionMapping,
LinearFilter,
Mesh,
MeshBasicMaterial,
PerspectiveCamera,
PlaneGeometry,
Scene,
WebGLRenderer
} from 'three'
const renderer = new WebGLRenderer()
const loader = new GainMapLoader(renderer)
const result = loader.load(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
// `result` can be used to populate a Texture
const scene = new Scene()
const mesh = new Mesh(
new PlaneGeometry(),
new MeshBasicMaterial({ map: result.renderTarget.texture })
)
scene.add(mesh)
renderer.render(scene, new PerspectiveCamera())
// `result.renderTarget.texture` must be
// converted to `DataTexture` in order
// to use it as Equirectanmgular scene background
// if needed
scene.background = result.toDataTexture()
scene.background.mapping = EquirectangularReflectionMapping
scene.background.minFilter = LinearFilter
Encoding a Gain map starting from an EXR file.
This is generally not useful in a three.js
site but this library exposes methods
that allow to encode an .exr
or hdr
file into a jpeg
with an embedded gain map.
import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
// load an HDR file
const loader = new EXRLoader()
const image = await loader.loadAsync('image.exr')
// find RAW RGB Max value of a texture
const textureMax = await findTextureMinMax(image)
// Encode the gainmap
const encodingResult = encode({
image,
// this will encode the full HDR range
maxContentBoost: Math.max.apply(this, textureMax)
})
// obtain the RAW RGBA SDR buffer and create an ImageData
const sdrImageData = new ImageData(
encodingResult.sdr.toArray(),
encodingResult.sdr.width,
encodingResult.sdr.height
)
// obtain the RAW RGBA Gain map buffer and create an ImageData
const gainMapImageData = new ImageData(
encodingResult.gainMap.toArray(),
encodingResult.gainMap.width,
encodingResult.gainMap.height
)
// parallel compress the RAW buffers into the specified mimeType
const mimeType = 'image/jpeg'
const quality = 0.9
const [sdr, gainMap] = await Promise.all([
compress({
source: sdrImageData,
mimeType,
quality,
flipY: true // output needs to be flipped
}),
compress({
source: gainMapImageData,
mimeType,
quality,
flipY: true // output needs to be flipped
})
])
// obtain the metadata which will be embedded into
// and XMP tag inside the final JPEG file
const metadata = encodingResult.getMetadata()
// embed the compressed images + metadata into a single
// JPEG file
const jpeg = await encodeJPEGMetadata({
...encodingResult,
...metadata,
sdr,
gainMap
})
// `jpeg` will be an `Uint8Array` which can be saved somewhere
If you import @monogrid/gainmap-js/libultrahdr
You will need to exclude it from Vite optimizations.
// vite.config.js
module.exports = defineConfig({
...
optimizeDeps: {
exclude: ['@monogrid/gainmap-js/libultrahdr']
},
...
})
FAQs
A Javascript (TypeScript) Port of Adobe Gainmap Technology for storing HDR Images using an SDR Image + a gain map
The npm package @monogrid/gainmap-js receives a total of 154,735 weekly downloads. As such, @monogrid/gainmap-js popularity was classified as popular.
We found that @monogrid/gainmap-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.