hdrpng.js
/** Convert a float buffer to a RGB9_E5 buffer. (ref https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_shared_exponent.txt)
    * @param {Float32Array} Buffer Floating point input buffer (96 bits/pixel).
    * @param {Uint32Array} [res] Optional output buffer with 32 bit RGB9_E5 per pixel.
    * @returns {Uint32Array} A 32bit uint32 array in RGB9_E5
    */
Convert a float buffer to a RGB9_E5 buffer.
Convert a float buffer to a RGB9_E5 buffer .
function floatToRgb9_e5(buffer,res) {
    var r,g,b,v,maxColor,ExpShared,denom,s,l=(buffer.byteLength/12)|0, res=res||new Uint32Array(l);
    for (var i=0;i<l;i++) {
      r=Math.min(32768.0,buffer[i*3]); g=Math.min(32768.0,buffer[i*3+1]); b=Math.min(32768.0,buffer[i*3+2]);
      maxColor = Math.max(Math.max(r,g),b);
      ExpShared = Math.max(-16,Math.floor(Math.log2(maxColor))) + 16;
      denom = Math.pow(2,ExpShared-24);
      if (Math.floor(maxColor/denom+0.5) == 511) { denom *= 2; ExpShared += 1; }
      res[i] = (Math.floor(r/denom+0.5)<<23)+(Math.floor(g/denom+0.5)<<14)+(Math.floor(b/denom+0.5)<<5)+ (ExpShared|0);
    }
    return res;
  }
function floatToRgb9_e5 ( buffer , res ) { var r , g , b , v , maxColor , ExpShared , denom , s , l = ( buffer . byteLength / 12 ) | 0 , res = res || new Uint32Array ( l ) ; for ( var i = 0 ; i < l ; i ++ ) { r = Math . min ( 32768.0 , buffer [ i * 3 ] ) ; g = Math . min ( 32768.0 , buffer [ i * 3 + 1 ] ) ; b = Math . min ( 32768.0 , buffer [ i * 3 + 2 ] ) ; maxColor = Math . max ( Math . max ( r , g ) , b ) ; ExpShared = Math . max ( - 16 , Math . floor ( Math . log2 ( maxColor ) ) ) + 16 ; denom = Math . pow ( 2 , ExpShared - 24 ) ; if ( Math . floor ( maxColor / denom + 0.5 ) == 511 ) { denom *= 2 ; ExpShared += 1 ; } res [ i ] = ( Math . floor ( r / denom + 0.5 ) << 23 ) + ( Math . floor ( g / denom + 0.5 ) << 14 ) + ( Math . floor ( b / denom + 0.5 ) << 5 ) + ( ExpShared | 0 ) ; } return res ; }
Convert a float buffer to a RGB9_E5 buffer.
Convert a float buffer to a RGB9_E5 buffer .