RGBELoader.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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
  84. * after "#?". defaults to "RGBE" */
  85. format: '', /* RGBE format, default 32-bit_rle_rgbe */
  86. gamma: 1.0, /* image has already been gamma corrected with
  87. * given gamma. defaults to 1.0 (no correction) */
  88. exposure: 1.0, /* a value of 1.0 in an image corresponds to
  89. * <exposure> watts/steradian/m^2.
  90. * defaults to 1.0 */
  91. width: 0, height: 0 /* image dimensions, width/height */
  92. }
  93. ;
  94. if ( buffer.pos >= buffer.byteLength || ! ( line = fgets( buffer ) ) ) {
  95. return rgbe_error( rgbe_read_error, "no header found" );
  96. }
  97. /* if you want to require the magic token then uncomment the next line */
  98. if ( ! ( match = line.match( magic_token_re ) ) ) {
  99. return rgbe_error( rgbe_format_error, "bad initial token" );
  100. }
  101. header.valid |= RGBE_VALID_PROGRAMTYPE;
  102. header.programtype = match[ 1 ];
  103. header.string += line + "\n";
  104. while ( true ) {
  105. line = fgets( buffer );
  106. if ( false === line ) break;
  107. header.string += line + "\n";
  108. if ( '#' === line.charAt( 0 ) ) {
  109. header.comments += line + "\n";
  110. continue; // comment line
  111. }
  112. if ( match = line.match( gamma_re ) ) {
  113. header.gamma = parseFloat( match[ 1 ], 10 );
  114. }
  115. if ( match = line.match( exposure_re ) ) {
  116. header.exposure = parseFloat( match[ 1 ], 10 );
  117. }
  118. if ( match = line.match( format_re ) ) {
  119. header.valid |= RGBE_VALID_FORMAT;
  120. header.format = match[ 1 ];//'32-bit_rle_rgbe';
  121. }
  122. if ( match = line.match( dimensions_re ) ) {
  123. header.valid |= RGBE_VALID_DIMENSIONS;
  124. header.height = parseInt( match[ 1 ], 10 );
  125. header.width = parseInt( match[ 2 ], 10 );
  126. }
  127. if ( ( header.valid & RGBE_VALID_FORMAT ) && ( header.valid & RGBE_VALID_DIMENSIONS ) ) break;
  128. }
  129. if ( ! ( header.valid & RGBE_VALID_FORMAT ) ) {
  130. return rgbe_error( rgbe_format_error, "missing format specifier" );
  131. }
  132. if ( ! ( header.valid & RGBE_VALID_DIMENSIONS ) ) {
  133. return rgbe_error( rgbe_format_error, "missing image size specifier" );
  134. }
  135. return header;
  136. },
  137. RGBE_ReadPixels_RLE = function( buffer, w, h ) {
  138. var data_rgba, offset, pos, count, byteValue,
  139. scanline_buffer, ptr, ptr_end, i, l, off, isEncodedRun,
  140. scanline_width = w, num_scanlines = h, rgbeStart
  141. ;
  142. if (
  143. // run length encoding is not allowed so read flat
  144. ( ( scanline_width < 8 ) || ( scanline_width > 0x7fff ) ) ||
  145. // this file is not run length encoded
  146. ( ( 2 !== buffer[ 0 ] ) || ( 2 !== buffer[ 1 ] ) || ( buffer[ 2 ] & 0x80 ) )
  147. ) {
  148. // return the flat buffer
  149. return new Uint8Array( buffer );
  150. }
  151. if ( scanline_width !== ( ( buffer[ 2 ] << 8 ) | buffer[ 3 ] ) ) {
  152. return rgbe_error( rgbe_format_error, "wrong scanline width" );
  153. }
  154. data_rgba = new Uint8Array( 4 * w * h );
  155. if ( ! data_rgba || ! data_rgba.length ) {
  156. return rgbe_error( rgbe_memory_error, "unable to allocate buffer space" );
  157. }
  158. offset = 0; pos = 0; ptr_end = 4 * scanline_width;
  159. rgbeStart = new Uint8Array( 4 );
  160. scanline_buffer = new Uint8Array( ptr_end );
  161. // read in each successive scanline
  162. while ( ( num_scanlines > 0 ) && ( pos < buffer.byteLength ) ) {
  163. if ( pos + 4 > buffer.byteLength ) {
  164. return rgbe_error( rgbe_read_error );
  165. }
  166. rgbeStart[ 0 ] = buffer[ pos ++ ];
  167. rgbeStart[ 1 ] = buffer[ pos ++ ];
  168. rgbeStart[ 2 ] = buffer[ pos ++ ];
  169. rgbeStart[ 3 ] = buffer[ pos ++ ];
  170. if ( ( 2 != rgbeStart[ 0 ] ) || ( 2 != rgbeStart[ 1 ] ) || ( ( ( rgbeStart[ 2 ] << 8 ) | rgbeStart[ 3 ] ) != scanline_width ) ) {
  171. return rgbe_error( rgbe_format_error, "bad rgbe scanline format" );
  172. }
  173. // read each of the four channels for the scanline into the buffer
  174. // first red, then green, then blue, then exponent
  175. ptr = 0;
  176. while ( ( ptr < ptr_end ) && ( pos < buffer.byteLength ) ) {
  177. count = buffer[ pos ++ ];
  178. isEncodedRun = count > 128;
  179. if ( isEncodedRun ) count -= 128;
  180. if ( ( 0 === count ) || ( ptr + count > ptr_end ) ) {
  181. return rgbe_error( rgbe_format_error, "bad scanline data" );
  182. }
  183. if ( isEncodedRun ) {
  184. // a (encoded) run of the same value
  185. byteValue = buffer[ pos ++ ];
  186. for ( i = 0; i < count; i ++ ) {
  187. scanline_buffer[ ptr ++ ] = byteValue;
  188. }
  189. //ptr += count;
  190. } else {
  191. // a literal-run
  192. scanline_buffer.set( buffer.subarray( pos, pos + count ), ptr );
  193. ptr += count; pos += count;
  194. }
  195. }
  196. // now convert data from buffer into rgba
  197. // first red, then green, then blue, then exponent (alpha)
  198. l = scanline_width; //scanline_buffer.byteLength;
  199. for ( i = 0; i < l; i ++ ) {
  200. off = 0;
  201. data_rgba[ offset ] = scanline_buffer[ i + off ];
  202. off += scanline_width; //1;
  203. data_rgba[ offset + 1 ] = scanline_buffer[ i + off ];
  204. off += scanline_width; //1;
  205. data_rgba[ offset + 2 ] = scanline_buffer[ i + off ];
  206. off += scanline_width; //1;
  207. data_rgba[ offset + 3 ] = scanline_buffer[ i + off ];
  208. offset += 4;
  209. }
  210. num_scanlines --;
  211. }
  212. return data_rgba;
  213. }
  214. ;
  215. var byteArray = new Uint8Array( buffer ),
  216. byteLength = byteArray.byteLength;
  217. byteArray.pos = 0;
  218. var rgbe_header_info = RGBE_ReadHeader( byteArray );
  219. if ( RGBE_RETURN_FAILURE !== rgbe_header_info ) {
  220. var w = rgbe_header_info.width,
  221. h = rgbe_header_info.height
  222. , image_rgba_data = RGBE_ReadPixels_RLE( byteArray.subarray( byteArray.pos ), w, h )
  223. ;
  224. if ( RGBE_RETURN_FAILURE !== image_rgba_data ) {
  225. return {
  226. width: w, height: h,
  227. data: image_rgba_data,
  228. header: rgbe_header_info.string,
  229. gamma: rgbe_header_info.gamma,
  230. exposure: rgbe_header_info.exposure,
  231. format: THREE.RGBEFormat, // handled as THREE.RGBAFormat in shaders
  232. type: THREE.UnsignedByteType
  233. };
  234. }
  235. }
  236. return null;
  237. };