Skip to content

Commit

Permalink
Remove unnecesary decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurczyk committed Jul 10, 2024
1 parent 09fb705 commit cc07672
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions examples/jsm/loaders/UltraHDRLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ class UltraHDRLoader extends Loader {
const textDecoder = new TextDecoder();

const data = new DataView( buffer );
const dataAsString = textDecoder.decode( data );

/* Minimal sufficient validation - https://developer.android.com/media/platform/hdr-image-format#signal_of_the_format */
if ( ! dataAsString.includes( 'hdrgm:Version="1.0"' ) ) {

throw new Error( 'THREE.UltraHDRLoader: Not a valid UltraHDR image' );

}

let byteOffset = 0;
const sections = [];
Expand Down Expand Up @@ -147,9 +139,7 @@ class UltraHDRLoader extends Loader {
const { sectionType, section, sectionOffset } = sections[ i ];

if ( sectionType === 0xe0 ) {

/* JPEG Header - no useful information */

} else if ( sectionType === 0xe1 ) {

/* XMP Metadata */
Expand Down Expand Up @@ -233,6 +223,13 @@ class UltraHDRLoader extends Loader {

}

/* Minimal sufficient validation - https://developer.android.com/media/platform/hdr-image-format#signal_of_the_format */
if ( ! xmpMetadata.version ) {

throw new Error( 'THREE.UltraHDRLoader: Not a valid UltraHDR image' );

}

if ( primaryImage && gainmapImage ) {

this._applyGainmapToSDR(
Expand Down Expand Up @@ -342,16 +339,14 @@ class UltraHDRLoader extends Loader {
);

if ( hasHDRContainerDescriptor ) {

/* There's not much useful information in the container descriptor besides memory-validation */

} else {

/* Gainmap descriptor - defaults from https://developer.android.com/media/platform/hdr-image-format#HDR_gain_map_metadata */

const [ gainmapNode ] = xmpXml.getElementsByTagName( 'rdf:Description' );

xmpMetadata.version = gainmapNode.getAttribute( 'hdrgm:Version' ) || '1.0';
xmpMetadata.version = gainmapNode.getAttribute( 'hdrgm:Version' );
xmpMetadata.baseRenditionIsHDR =
gainmapNode.getAttribute( 'hdrgm:BaseRenditionIsHDR' ) === 'True';
xmpMetadata.gainMapMin = parseFloat(
Expand Down Expand Up @@ -547,25 +542,25 @@ class UltraHDRLoader extends Loader {
: Math.pow( gainmapValue, 1.0 / xmpMetadata.gamma );

const logBoost =
xmpMetadata.gainMapMin * ( 1.0 - logRecovery ) +
xmpMetadata.gainMapMax * logRecovery;
xmpMetadata.gainMapMin * ( 1.0 - logRecovery ) +
xmpMetadata.gainMapMax * logRecovery;

const hdrValue =
( sdrValue + xmpMetadata.offsetSDR ) *
( logBoost * weightFactor === 0.0
? 1.0
: Math.pow( 2, logBoost * weightFactor ) ) -
xmpMetadata.offsetHDR;
( sdrValue + xmpMetadata.offsetSDR ) *
( logBoost * weightFactor === 0.0
? 1.0
: Math.pow( 2, logBoost * weightFactor ) ) -
xmpMetadata.offsetHDR;

const linearHDRValue = Math.min(
Math.max( this._srgbToLinear( hdrValue ), 0 ),
65504
);

hdrBuffer[ pixelIndex + channelIndex ] =
this.type === HalfFloatType
? DataUtils.toHalfFloat( linearHDRValue )
: linearHDRValue;
this.type === HalfFloatType
? DataUtils.toHalfFloat( linearHDRValue )
: linearHDRValue;

}

Expand All @@ -586,4 +581,4 @@ class UltraHDRLoader extends Loader {

}

export { UltraHDRLoader };
export { UltraHDRLoader };

0 comments on commit cc07672

Please sign in to comment.