document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // float RGB -> U8 RGBE quantization
  2. void float_to_rgbe(unsigned char * rgbe,const float * rgbf)
  3. {
  4.     // rgbf[] should all be >= 0 , RGBE does not support signed values
  5.        
  6.     float maxf = rgbf[0] > rgbf[1] ? rgbf[0] : rgbf[1];
  7.     maxf = maxf > rgbf[2] ? maxf : rgbf[2];
  8.  
  9.     if ( maxf <= 1e-32f )
  10.     {
  11.         // Exponent byte = 0 is a special encoding that makes RGB output = 0
  12.         rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
  13.     }
  14.     else
  15.     {
  16.         int exponent;
  17.         frexpf(maxf, &exponent);
  18.         float scale = ldexpf(1.f, -exponent + 8);
  19.                
  20.         rgbe[0] = (unsigned char)( rgbf[0] * scale );
  21.         rgbe[1] = (unsigned char)( rgbf[1] * scale );
  22.         rgbe[2] = (unsigned char)( rgbf[2] * scale );
  23.         rgbe[3] = (unsigned char)( exponent + 128 );
  24.     }
  25. }
  26.  
  27. // U8 RGBE -> float RGB dequantization
  28. void rgbe_to_float(float * rgbf,const unsigned char * rgbe)
  29. {
  30.     if ( rgbe[3] == 0 )
  31.     {
  32.         rgbf[0] = rgbf[1] = rgbf[2] = 0.f;
  33.     }
  34.     else
  35.     {
  36.         // the extra 8 here does the /256
  37.         float fexp = ldexpf(1.f, (int)rgbe[3] - (128 + 8));
  38.         rgbf[0] = (rgbe[0] + 0.5f) * fexp;
  39.         rgbf[1] = (rgbe[1] + 0.5f) * fexp;
  40.         rgbf[2] = (rgbe[2] + 0.5f) * fexp;
  41.     }
  42. }
');