RGBELoader.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * @author Nikos M. / https://github.com/foo123/
  3. */
  4. // https://github.com/mrdoob/three.js/issues/5552
  5. // http://en.wikipedia.org/wiki/RGBE_image_format
  6. THREE.HDRLoader = THREE.RGBELoader = function ( manager ) {
  7. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  8. };
  9. // extend THREE.DataTextureLoader
  10. THREE.RGBELoader.prototype = Object.create( THREE.DataTextureLoader.prototype );
  11. // adapted from http://www.graphics.cornell.edu/~bjw/rgbe.html
  12. THREE.RGBELoader.prototype._parser = function ( buffer ) {
  13. var
  14. /* return codes for rgbe routines */
  15. RGBE_RETURN_SUCCESS = 0,
  16. RGBE_RETURN_FAILURE = - 1,
  17. /* default error routine. change this to change error handling */
  18. rgbe_read_error = 1,
  19. rgbe_write_error = 2,
  20. rgbe_format_error = 3,
  21. rgbe_memory_error = 4,
  22. rgbe_error = function ( rgbe_error_code, msg ) {
  23. switch ( rgbe_error_code ) {
  24. case rgbe_read_error: console.error( "THREE.RGBELoader Read Error: " + ( msg || '' ) );
  25. break;
  26. case rgbe_write_error: console.error( "THREE.RGBELoader Write Error: " + ( msg || '' ) );
  27. break;
  28. case rgbe_format_error: console.error( "THREE.RGBELoader Bad File Format: " + ( msg || '' ) );
  29. break;
  30. default:
  31. case rgbe_memory_error: console.error( "THREE.RGBELoader: Error: " + ( msg || '' ) );
  32. }
  33. return RGBE_RETURN_FAILURE;
  34. },
  35. /* offsets to red, green, and blue components in a data (float) pixel */
  36. RGBE_DATA_RED = 0,
  37. RGBE_DATA_GREEN = 1,
  38. RGBE_DATA_BLUE = 2,
  39. /* number of floats per pixel, use 4 since stored in rgba image format */
  40. RGBE_DATA_SIZE = 4,
  41. /* flags indicating which fields in an rgbe_header_info are valid */
  42. RGBE_VALID_PROGRAMTYPE = 1,
  43. RGBE_VALID_FORMAT = 2,
  44. RGBE_VALID_DIMENSIONS = 4,
  45. NEWLINE = "\n",
  46. fgets = function ( buffer, lineLimit, consume ) {
  47. lineLimit = ! lineLimit ? 1024 : lineLimit;
  48. var p = buffer.pos,
  49. i = - 1, len = 0, s = '', chunkSize = 128,
  50. chunk = String.fromCharCode.apply( null, new Uint16Array( buffer.subarray( p, p + chunkSize ) ) )
  51. ;
  52. while ( ( 0 > ( i = chunk.indexOf( NEWLINE ) ) ) && ( len < lineLimit ) && ( p < buffer.byteLength ) ) {
  53. s += chunk; len += chunk.length;
  54. p += chunkSize;
  55. chunk += String.fromCharCode.apply( null, new Uint16Array( buffer.subarray( p, p + chunkSize ) ) );
  56. }
  57. if ( - 1 < i ) {
  58. /*for (i=l-1; i>=0; i--) {
  59. byteCode = m.charCodeAt(i);
  60. if (byteCode > 0x7f && byteCode <= 0x7ff) byteLen++;
  61. else if (byteCode > 0x7ff && byteCode <= 0xffff) byteLen += 2;
  62. if (byteCode >= 0xDC00 && byteCode <= 0xDFFF) i--; //trail surrogate
  63. }*/
  64. if ( false !== consume ) buffer.pos += len + i + 1;
  65. return s + chunk.slice( 0, i );
  66. }
  67. return false;
  68. },
  69. /* minimal header reading. modify if you want to parse more information */
  70. RGBE_ReadHeader = function ( buffer ) {
  71. var line, match,
  72. // regexes to parse header info fields
  73. magic_token_re = /^#\?(\S+)$/,
  74. gamma_re = /^\s*GAMMA\s*=\s*(\d+(\.\d+)?)\s*$/,
  75. exposure_re = /^\s*EXPOSURE\s*=\s*(\d+(\.\d+)?)\s*$/,
  76. format_re = /^\s*FORMAT=(\S+)\s*$/,
  77. dimensions_re = /^\s*\-Y\s+(\d+)\s+\+X\s+(\d+)\s*$/,
  78. // RGBE format header struct
  79. header = {
  80. valid: 0, /* indicate which fields are valid */
  81. string: '', /* the actual header string */
  82. comments: '', /* comments found in header */
  83. programtype: 'RGBE', /* listed at beginning of file to identify it after "#?". defaults to "RGBE" */
  84. format: '', /* RGBE format, default 32-bit_rle_rgbe */
  85. gamma: 1.0, /* image has already been gamma corrected with given gamma. defaults to 1.0 (no correction) */
  86. exposure: 1.0, /* a value of 1.0 in an image corresponds to <exposure> watts/steradian/m^2. defaults to 1.0 */
  87. width: 0, height: 0 /* image dimensions, width/height */
  88. };
  89. if ( buffer.pos >= buffer.byteLength || ! ( line = fgets( buffer ) ) ) {
  90. return rgbe_error( rgbe_read_error, "no header found" );
  91. }
  92. /* if you want to require the magic token then uncomment the next line */
  93. if ( ! ( match = line.match( magic_token_re ) ) ) {
  94. return rgbe_error( rgbe_format_error, "bad initial token" );
  95. }
  96. header.valid |= RGBE_VALID_PROGRAMTYPE;
  97. header.programtype = match[ 1 ];
  98. header.string += line + "\n";
  99. while ( true ) {
  100. line = fgets( buffer );
  101. if ( false === line ) break;
  102. header.string += line + "\n";
  103. if ( '#' === line.charAt( 0 ) ) {
  104. header.comments += line + "\n";
  105. continue; // comment line
  106. }
  107. if ( match = line.match( gamma_re ) ) {
  108. header.gamma = parseFloat( match[ 1 ], 10 );
  109. }
  110. if ( match = line.match( exposure_re ) ) {
  111. header.exposure = parseFloat( match[ 1 ], 10 );
  112. }
  113. if ( match = line.match( format_re ) ) {
  114. header.valid |= RGBE_VALID_FORMAT;
  115. header.format = match[ 1 ];//'32-bit_rle_rgbe';
  116. }
  117. if ( match = line.match( dimensions_re ) ) {
  118. header.valid |= RGBE_VALID_DIMENSIONS;
  119. header.height = parseInt( match[ 1 ], 10 );
  120. header.width = parseInt( match[ 2 ], 10 );
  121. }
  122. if ( ( header.valid & RGBE_VALID_FORMAT ) && ( header.valid & RGBE_VALID_DIMENSIONS ) ) break;
  123. }
  124. if ( ! ( header.valid & RGBE_VALID_FORMAT ) ) {
  125. return rgbe_error( rgbe_format_error, "missing format specifier" );
  126. }
  127. if ( ! ( header.valid & RGBE_VALID_DIMENSIONS ) ) {
  128. return rgbe_error( rgbe_format_error, "missing image size specifier" );
  129. }
  130. return header;
  131. },
  132. RGBE_ReadPixels_RLE = function ( buffer, w, h ) {
  133. var data_rgba, offset, pos, count, byteValue,
  134. scanline_buffer, ptr, ptr_end, i, l, off, isEncodedRun,
  135. scanline_width = w, num_scanlines = h, rgbeStart
  136. ;
  137. if (
  138. // run length encoding is not allowed so read flat
  139. ( ( scanline_width < 8 ) || ( scanline_width > 0x7fff ) ) ||
  140. // this file is not run length encoded
  141. ( ( 2 !== buffer[ 0 ] ) || ( 2 !== buffer[ 1 ] ) || ( buffer[ 2 ] & 0x80 ) )
  142. ) {
  143. // return the flat buffer
  144. return new Uint8Array( buffer );
  145. }
  146. if ( scanline_width !== ( ( buffer[ 2 ] << 8 ) | buffer[ 3 ] ) ) {
  147. return rgbe_error( rgbe_format_error, "wrong scanline width" );
  148. }
  149. data_rgba = new Uint8Array( 4 * w * h );
  150. if ( ! data_rgba || ! data_rgba.length ) {
  151. return rgbe_error( rgbe_memory_error, "unable to allocate buffer space" );
  152. }
  153. offset = 0; pos = 0; ptr_end = 4 * scanline_width;
  154. rgbeStart = new Uint8Array( 4 );
  155. scanline_buffer = new Uint8Array( ptr_end );
  156. // read in each successive scanline
  157. while ( ( num_scanlines > 0 ) && ( pos < buffer.byteLength ) ) {
  158. if ( pos + 4 > buffer.byteLength ) {
  159. return rgbe_error( rgbe_read_error );
  160. }
  161. rgbeStart[ 0 ] = buffer[ pos ++ ];
  162. rgbeStart[ 1 ] = buffer[ pos ++ ];
  163. rgbeStart[ 2 ] = buffer[ pos ++ ];
  164. rgbeStart[ 3 ] = buffer[ pos ++ ];
  165. if ( ( 2 != rgbeStart[ 0 ] ) || ( 2 != rgbeStart[ 1 ] ) || ( ( ( rgbeStart[ 2 ] << 8 ) | rgbeStart[ 3 ] ) != scanline_width ) ) {
  166. return rgbe_error( rgbe_format_error, "bad rgbe scanline format" );
  167. }
  168. // read each of the four channels for the scanline into the buffer
  169. // first red, then green, then blue, then exponent
  170. ptr = 0;
  171. while ( ( ptr < ptr_end ) && ( pos < buffer.byteLength ) ) {
  172. count = buffer[ pos ++ ];
  173. isEncodedRun = count > 128;
  174. if ( isEncodedRun ) count -= 128;
  175. if ( ( 0 === count ) || ( ptr + count > ptr_end ) ) {
  176. return rgbe_error( rgbe_format_error, "bad scanline data" );
  177. }
  178. if ( isEncodedRun ) {
  179. // a (encoded) run of the same value
  180. byteValue = buffer[ pos ++ ];
  181. for ( i = 0; i < count; i ++ ) {
  182. scanline_buffer[ ptr ++ ] = byteValue;
  183. }
  184. //ptr += count;
  185. } else {
  186. // a literal-run
  187. scanline_buffer.set( buffer.subarray( pos, pos + count ), ptr );
  188. ptr += count; pos += count;
  189. }
  190. }
  191. // now convert data from buffer into rgba
  192. // first red, then green, then blue, then exponent (alpha)
  193. l = scanline_width; //scanline_buffer.byteLength;
  194. for ( i = 0; i < l; i ++ ) {
  195. off = 0;
  196. data_rgba[ offset ] = scanline_buffer[ i + off ];
  197. off += scanline_width; //1;
  198. data_rgba[ offset + 1 ] = scanline_buffer[ i + off ];
  199. off += scanline_width; //1;
  200. data_rgba[ offset + 2 ] = scanline_buffer[ i + off ];
  201. off += scanline_width; //1;
  202. data_rgba[ offset + 3 ] = scanline_buffer[ i + off ];
  203. offset += 4;
  204. }
  205. num_scanlines --;
  206. }
  207. return data_rgba;
  208. }
  209. ;
  210. var byteArray = new Uint8Array( buffer ),
  211. byteLength = byteArray.byteLength;
  212. byteArray.pos = 0;
  213. var rgbe_header_info = RGBE_ReadHeader( byteArray );
  214. if ( RGBE_RETURN_FAILURE !== rgbe_header_info ) {
  215. var w = rgbe_header_info.width,
  216. h = rgbe_header_info.height,
  217. image_rgba_data = RGBE_ReadPixels_RLE( byteArray.subarray( byteArray.pos ), w, h )
  218. ;
  219. if ( RGBE_RETURN_FAILURE !== image_rgba_data ) {
  220. return {
  221. width: w, height: h,
  222. data: image_rgba_data,
  223. header: rgbe_header_info.string,
  224. gamma: rgbe_header_info.gamma,
  225. exposure: rgbe_header_info.exposure,
  226. format: THREE.RGBEFormat, // handled as THREE.RGBAFormat in shaders
  227. type: THREE.UnsignedByteType
  228. };
  229. }
  230. }
  231. return null;
  232. };