From fb716b0a814f0db0b37e7b8f22894520e7d58218 Mon Sep 17 00:00:00 2001 From: Cody Bennett Date: Sat, 27 Apr 2024 18:51:20 -0500 Subject: [PATCH] fix(RGBELoader): upstream fixes (#358) --- src/loaders/RGBELoader.js | 114 +++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 64 deletions(-) diff --git a/src/loaders/RGBELoader.js b/src/loaders/RGBELoader.js index cc05475c..6c5fc8d6 100644 --- a/src/loaders/RGBELoader.js +++ b/src/loaders/RGBELoader.js @@ -13,10 +13,7 @@ class RGBELoader extends DataTextureLoader { // adapted from http://www.graphics.cornell.edu/~bjw/rgbe.html parse(buffer) { - const /* return codes for rgbe routines */ - //RGBE_RETURN_SUCCESS = 0, - RGBE_RETURN_FAILURE = -1, - /* default error routine. change this to change error handling */ + const /* default error routine. change this to change error handling */ rgbe_read_error = 1, rgbe_write_error = 2, rgbe_format_error = 3, @@ -24,20 +21,15 @@ class RGBELoader extends DataTextureLoader { rgbe_error = function (rgbe_error_code, msg) { switch (rgbe_error_code) { case rgbe_read_error: - console.error('THREE.RGBELoader Read Error: ' + (msg || '')) - break + throw new Error('THREE.RGBELoader: Read Error: ' + (msg || '')) case rgbe_write_error: - console.error('THREE.RGBELoader Write Error: ' + (msg || '')) - break + throw new Error('THREE.RGBELoader: Write Error: ' + (msg || '')) case rgbe_format_error: - console.error('THREE.RGBELoader Bad File Format: ' + (msg || '')) - break + throw new Error('THREE.RGBELoader: Bad File Format: ' + (msg || '')) default: case rgbe_memory_error: - console.error('THREE.RGBELoader: Error: ' + (msg || '')) + throw new Error('THREE.RGBELoader: Memory Error: ' + (msg || '')) } - - return RGBE_RETURN_FAILURE }, /* offsets to red, green, and blue components in a data (float) pixel */ //RGBE_DATA_RED = 0, @@ -113,12 +105,12 @@ class RGBELoader extends DataTextureLoader { let line, match if (buffer.pos >= buffer.byteLength || !(line = fgets(buffer))) { - return rgbe_error(rgbe_read_error, 'no header found') + rgbe_error(rgbe_read_error, 'no header found') } /* if you want to require the magic token then uncomment the next line */ if (!(match = line.match(magic_token_re))) { - return rgbe_error(rgbe_format_error, 'bad initial token') + rgbe_error(rgbe_format_error, 'bad initial token') } header.valid |= RGBE_VALID_PROGRAMTYPE @@ -158,11 +150,11 @@ class RGBELoader extends DataTextureLoader { } if (!(header.valid & RGBE_VALID_FORMAT)) { - return rgbe_error(rgbe_format_error, 'missing format specifier') + rgbe_error(rgbe_format_error, 'missing format specifier') } if (!(header.valid & RGBE_VALID_DIMENSIONS)) { - return rgbe_error(rgbe_format_error, 'missing image size specifier') + rgbe_error(rgbe_format_error, 'missing image size specifier') } return header @@ -184,13 +176,13 @@ class RGBELoader extends DataTextureLoader { } if (scanline_width !== ((buffer[2] << 8) | buffer[3])) { - return rgbe_error(rgbe_format_error, 'wrong scanline width') + rgbe_error(rgbe_format_error, 'wrong scanline width') } const data_rgba = new Uint8Array(4 * w * h) if (!data_rgba.length) { - return rgbe_error(rgbe_memory_error, 'unable to allocate buffer space') + rgbe_error(rgbe_memory_error, 'unable to allocate buffer space') } let offset = 0, @@ -204,7 +196,7 @@ class RGBELoader extends DataTextureLoader { // read in each successive scanline while (num_scanlines > 0 && pos < buffer.byteLength) { if (pos + 4 > buffer.byteLength) { - return rgbe_error(rgbe_read_error) + rgbe_error(rgbe_read_error) } rgbeStart[0] = buffer[pos++] @@ -213,7 +205,7 @@ class RGBELoader extends DataTextureLoader { rgbeStart[3] = buffer[pos++] if (2 != rgbeStart[0] || 2 != rgbeStart[1] || ((rgbeStart[2] << 8) | rgbeStart[3]) != scanline_width) { - return rgbe_error(rgbe_format_error, 'bad rgbe scanline format') + rgbe_error(rgbe_format_error, 'bad rgbe scanline format') } // read each of the four channels for the scanline into the buffer @@ -227,7 +219,7 @@ class RGBELoader extends DataTextureLoader { if (isEncodedRun) count -= 128 if (0 === count || ptr + count > ptr_end) { - return rgbe_error(rgbe_format_error, 'bad scanline data') + rgbe_error(rgbe_format_error, 'bad scanline data') } if (isEncodedRun) { @@ -291,58 +283,52 @@ class RGBELoader extends DataTextureLoader { byteArray.pos = 0 const rgbe_header_info = RGBE_ReadHeader(byteArray) - if (RGBE_RETURN_FAILURE !== rgbe_header_info) { - const w = rgbe_header_info.width, - h = rgbe_header_info.height, - image_rgba_data = RGBE_ReadPixels_RLE(byteArray.subarray(byteArray.pos), w, h) - - if (RGBE_RETURN_FAILURE !== image_rgba_data) { - let data, type - let numElements - - switch (this.type) { - case FloatType: - numElements = image_rgba_data.length / 4 - const floatArray = new Float32Array(numElements * 4) + const w = rgbe_header_info.width, + h = rgbe_header_info.height, + image_rgba_data = RGBE_ReadPixels_RLE(byteArray.subarray(byteArray.pos), w, h) - for (let j = 0; j < numElements; j++) { - RGBEByteToRGBFloat(image_rgba_data, j * 4, floatArray, j * 4) - } + let data, type + let numElements - data = floatArray - type = FloatType - break + switch (this.type) { + case FloatType: + numElements = image_rgba_data.length / 4 + const floatArray = new Float32Array(numElements * 4) - case HalfFloatType: - numElements = image_rgba_data.length / 4 - const halfArray = new Uint16Array(numElements * 4) + for (let j = 0; j < numElements; j++) { + RGBEByteToRGBFloat(image_rgba_data, j * 4, floatArray, j * 4) + } - for (let j = 0; j < numElements; j++) { - RGBEByteToRGBHalf(image_rgba_data, j * 4, halfArray, j * 4) - } + data = floatArray + type = FloatType + break - data = halfArray - type = HalfFloatType - break + case HalfFloatType: + numElements = image_rgba_data.length / 4 + const halfArray = new Uint16Array(numElements * 4) - default: - console.error('THREE.RGBELoader: unsupported type: ', this.type) - break + for (let j = 0; j < numElements; j++) { + RGBEByteToRGBHalf(image_rgba_data, j * 4, halfArray, j * 4) } - return { - width: w, - height: h, - data: data, - header: rgbe_header_info.string, - gamma: rgbe_header_info.gamma, - exposure: rgbe_header_info.exposure, - type: type, - } - } + data = halfArray + type = HalfFloatType + break + + default: + throw new Error('THREE.RGBELoader: Unsupported type: ' + this.type) + break } - return null + return { + width: w, + height: h, + data: data, + header: rgbe_header_info.string, + gamma: rgbe_header_info.gamma, + exposure: rgbe_header_info.exposure, + type: type, + } } setDataType(value) {