llquantize.h

Go to the documentation of this file.
00001 
00033 #ifndef LL_LLQUANTIZE_H
00034 #define LL_LLQUANTIZE_H
00035 
00036 const U16 U16MAX = 65535;
00037 const F32 OOU16MAX = 1.f/(F32)(U16MAX);
00038 
00039 const U8 U8MAX = 255;
00040 const F32 OOU8MAX = 1.f/(F32)(U8MAX);
00041 
00042 const U8 FIRSTVALIDCHAR = 54;
00043 const U8 MAXSTRINGVAL = U8MAX - FIRSTVALIDCHAR; //we don't allow newline or null 
00044 
00045 
00046 inline U16 F32_to_U16_ROUND(F32 val, F32 lower, F32 upper)
00047 {
00048         val = llclamp(val, lower, upper);
00049         // make sure that the value is positive and normalized to <0, 1>
00050         val -= lower;
00051         val /= (upper - lower);
00052 
00053         // round the value.   Sreturn the U16
00054         return (U16)(llround(val*U16MAX));
00055 }
00056 
00057 
00058 inline U16 F32_to_U16(F32 val, F32 lower, F32 upper)
00059 {
00060         val = llclamp(val, lower, upper);
00061         // make sure that the value is positive and normalized to <0, 1>
00062         val -= lower;
00063         val /= (upper - lower);
00064 
00065         // return the U16
00066         return (U16)(llfloor(val*U16MAX));
00067 }
00068 
00069 inline F32 U16_to_F32(U16 ival, F32 lower, F32 upper)
00070 {
00071         F32 val = ival*OOU16MAX;
00072         F32 delta = (upper - lower);
00073         val *= delta;
00074         val += lower;
00075 
00076         F32 max_error = delta*OOU16MAX;
00077 
00078         // make sure that zero's come through as zero
00079         if (fabsf(val) < max_error)
00080                 val = 0.f;
00081 
00082         return val;
00083 }
00084 
00085 
00086 inline U8 F32_to_U8_ROUND(F32 val, F32 lower, F32 upper)
00087 {
00088         val = llclamp(val, lower, upper);
00089         // make sure that the value is positive and normalized to <0, 1>
00090         val -= lower;
00091         val /= (upper - lower);
00092 
00093         // return the rounded U8
00094         return (U8)(llround(val*U8MAX));
00095 }
00096 
00097 
00098 inline U8 F32_to_U8(F32 val, F32 lower, F32 upper)
00099 {
00100         val = llclamp(val, lower, upper);
00101         // make sure that the value is positive and normalized to <0, 1>
00102         val -= lower;
00103         val /= (upper - lower);
00104 
00105         // return the U8
00106         return (U8)(llfloor(val*U8MAX));
00107 }
00108 
00109 inline F32 U8_to_F32(U8 ival, F32 lower, F32 upper)
00110 {
00111         F32 val = ival*OOU8MAX;
00112         F32 delta = (upper - lower);
00113         val *= delta;
00114         val += lower;
00115 
00116         F32 max_error = delta*OOU8MAX;
00117 
00118         // make sure that zero's come through as zero
00119         if (fabsf(val) < max_error)
00120                 val = 0.f;
00121 
00122         return val;
00123 }
00124 
00125 inline U8 F32_TO_STRING(F32 val, F32 lower, F32 upper)
00126 {
00127         val = llclamp(val, lower, upper); //[lower, upper]
00128         // make sure that the value is positive and normalized to <0, 1>
00129         val -= lower;                                   //[0, upper-lower]
00130         val /= (upper - lower);                 //[0,1]
00131         val = val * MAXSTRINGVAL;               //[0, MAXSTRINGVAL]
00132         val = floor(val + 0.5f);                //[0, MAXSTRINGVAL]
00133 
00134         U8 stringVal = (U8)(val) + FIRSTVALIDCHAR;                      //[FIRSTVALIDCHAR, MAXSTRINGVAL + FIRSTVALIDCHAR]
00135         return stringVal;
00136 }
00137 
00138 inline F32 STRING_TO_F32(U8 ival, F32 lower, F32 upper)
00139 {
00140         // remove empty space left for NULL, newline, etc.
00141         ival -= FIRSTVALIDCHAR;                                                         //[0, MAXSTRINGVAL]
00142 
00143         F32 val = (F32)ival * (1.f / (F32)MAXSTRINGVAL);        //[0, 1]
00144         F32 delta = (upper - lower);
00145         val *= delta;                                                                           //[0, upper - lower]
00146         val += lower;                                                                           //[lower, upper]
00147 
00148         return val;
00149 }
00150 
00151 #endif

Generated on Thu Jul 1 06:09:03 2010 for Second Life Viewer by  doxygen 1.4.7