Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/OpenEXR/IlmImf/dwaLookups.cpp view on Meta::CPAN
void
generateNoop()
{
printf("const unsigned short dwaCompressorNoOp[] = \n");
printf("{");
for (int i=0; i<65536; ++i) {
if (i % 8 == 0) {
printf("\n ");
}
unsigned short dst;
char *tmp = (char *)(&dst);
unsigned short src = (unsigned short)i;
Xdr::write <CharPtrIO> (tmp, src);
printf("0x%04x, ", dst);
}
printf("\n};\n");
}
//
// Nonlinearly encode luminance. For values below 1.0, we want
// to use a gamma 2.2 function to match what is fairly common
// for storing output referred. However, > 1, gamma functions blow up,
// and log functions are much better behaved. We could use a log
// function everywhere, but it tends to over-sample dark
// regions and undersample the brighter regions, when
// compared to the way real devices reproduce values.
//
// So, above 1, use a log function which is a smooth blend
// into the gamma function.
//
// Nonlinear(linear) =
//
// linear^(1./2.2) / linear <= 1.0
// |
// ln(linear)/ln(e^2.2) + 1 \ otherwise
//
//
// toNonlinear[] needs to take in XDR format half float values,
// and output NATIVE format float.
//
// toLinear[] does the opposite - takes in NATIVE half and
// outputs XDR half values.
//
void
generateToLinear()
{
unsigned short toLinear[65536];
toLinear[0] = 0;
for (int i=1; i<65536; ++i) {
half h;
float sign = 1;
float logBase = pow(2.7182818, 2.2);
// map NaN and inf to 0
if ((i & 0x7c00) == 0x7c00) {
toLinear[i] = 0;
continue;
}
//
// _toLinear - assume i is NATIVE, but our output needs
// to get flipped to XDR
//
h.setBits(i);
sign = 1;
if ((float)h < 0) {
sign = -1;
}
if ( fabs( (float)h) <= 1.0 ) {
h = (half)(sign * pow((float)fabs((float)h), 2.2f));
} else {
h = (half)(sign * pow(logBase, (float)(fabs((float)h) - 1.0)));
}
{
char *tmp = (char *)(&toLinear[i]);
Xdr::write <CharPtrIO> ( tmp, h.bits());
}
}
printf("const unsigned short dwaCompressorToLinear[] = \n");
printf("{");
for (int i=0; i<65536; ++i) {
if (i % 8 == 0) {
printf("\n ");
}
printf("0x%04x, ", toLinear[i]);
}
printf("\n};\n");
}
void
generateToNonlinear()
{
unsigned short toNonlinear[65536];
toNonlinear[0] = 0;
for (int i=1; i<65536; ++i) {
unsigned short usNative, usXdr;
half h;
float sign = 1;
float logBase = pow(2.7182818, 2.2);
usXdr = i;
{
const char *tmp = (char *)(&usXdr);
Xdr::read<CharPtrIO>(tmp, usNative);
}
// map NaN and inf to 0
if ((usNative & 0x7c00) == 0x7c00) {
toNonlinear[i] = 0;
continue;
}
//
// toNonlinear - assume i is XDR
//
h.setBits(usNative);
sign = 1;
if ((float)h < 0) {
sign = -1;
}
if ( fabs( (float)h ) <= 1.0) {
h = (half)(sign * pow(fabs((float)h), 1.f/2.2f));
} else {
h = (half)(sign * ( log(fabs((float)h)) / log(logBase) + 1.0) );
}
toNonlinear[i] = h.bits();
}
printf("const unsigned short dwaCompressorToNonlinear[] = \n");
printf("{");
for (int i=0; i<65536; ++i) {
if (i % 8 == 0) {
printf("\n ");
}
printf("0x%04x, ", toNonlinear[i]);
}
printf("\n};\n");
}
//
// Attempt to get available CPUs in a somewhat portable way.
//
int
cpuCount()
{
if (!IlmThread::supportsThreads()) return 1;
int cpuCount = 1;
#if defined (OPENEXR_IMF_HAVE_SYSCONF_NPROCESSORS_ONLN)
cpuCount = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined (_WIN32)
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
cpuCount = sysinfo.dwNumberOfProcessors;
#endif
if (cpuCount < 1) cpuCount = 1;
return cpuCount;
}
( run in 0.416 second using v1.01-cache-2.11-cpan-f6376fbd888 )