Blender V2.27

logImageCore.c

00001 /*
00002  *       Cineon image file format library routines.
00003  *
00004  *       Copyright 1999,2000,2001 David Hodson <hodsond@acm.org>
00005  *
00006  *       This program is free software; you can redistribute it and/or modify it
00007  *       under the terms of the GNU General Public License as published by the Free
00008  *       Software Foundation; either version 2 of the License, or (at your option)
00009  *       any later version.
00010  *
00011  *       This program is distributed in the hope that it will be useful, but
00012  *       WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013  *       or FITNESS FOR A PARTICULAR PURPOSE.    See the GNU General Public License
00014  *       for more details.
00015  *
00016  *       You should have received a copy of the GNU General Public License
00017  *       along with this program; if not, write to the Free Software
00018  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  */
00021 
00022 #include "logImageCore.h"
00023 
00024 #include <time.h>                                /* strftime() */
00025 #include <math.h>
00026 /* Makes rint consistent in Windows and Linux: */
00027 #define rint(x) floor(x+0.5)
00028 
00029 #ifdef WIN32
00030 #include <winsock.h>
00031 #else
00032 #include <sys/types.h>
00033 #include <sys/socket.h>
00034 #include <netinet/in.h>
00035 #include <sys/param.h>
00036 #endif
00037 
00038 #if defined(__hpux)
00039 /* These are macros in hpux */
00040 #ifdef htonl
00041 #undef htonl
00042 #undef htons
00043 #undef ntohl
00044 #undef ntohs
00045 #endif
00046 unsigned int htonl(h) unsigned int h; { return(h); }
00047 unsigned short htons(h) unsigned short h; { return(h); }
00048 unsigned int ntohl(n) unsigned int n; { return(n); }
00049 unsigned short ntohs(n) unsigned short n; { return(n); }
00050 #endif
00051         
00052         
00053 /* obscure LogImage conversion */
00054 /* from 10 bit int to 0.0 - 1.0 */
00055 /* magic numbers left intact */
00056 static double
00057 convertTo(int inp, int white, float gamma) {
00058         /*      return pow(pow(10.0, ((inp - white) * 0.002 / 0.6)), gamma); */
00059         return pow(10.0, (inp - white) * gamma * 0.002 / 0.6);
00060 }
00061 
00062 static double
00063 convertFrom(double inp, int white, float gamma) {
00064         return white + log10(inp) / (gamma * 0.002 / 0.6);
00065 }
00066 
00067 /* set up the 10 bit to 8 bit and 8 bit to 10 bit tables */
00068 void
00069 setupLut(LogImageFile *logImage) {
00070 
00071         int i;
00072         double f_black;
00073         double scale;
00074 
00075         f_black = convertTo(logImage->params.blackPoint, logImage->params.whitePoint, logImage->params.gamma);
00076         scale = 255.0 / (1.0 - f_black);
00077 
00078         for (i = 0; i <= logImage->params.blackPoint; ++i) {
00079                 logImage->lut10[i] = 0;
00080         }
00081         for (; i < logImage->params.whitePoint; ++i) {
00082                 double f_i;
00083                 f_i = convertTo(i, logImage->params.whitePoint, logImage->params.gamma);
00084                 logImage->lut10[i] = (int)rint(scale * (f_i - f_black));
00085         }
00086         for (; i < 1024; ++i) {
00087                 logImage->lut10[i] = 255;
00088         }
00089 
00090         for (i = 0; i < 256; ++i) {
00091                 double f_i = f_black + (i / 255.0) * (1.0 - f_black);
00092                 logImage->lut8[i] = convertFrom(f_i, logImage->params.whitePoint, logImage->params.gamma);
00093         }
00094 }
00095 
00096 /* set up the 10 bit to 16 bit and 16 bit to 10 bit tables */
00097 void
00098 setupLut16(LogImageFile *logImage) {
00099 
00100         int i;
00101         double f_black;
00102         double scale;
00103 
00104         f_black = convertTo(logImage->params.blackPoint, logImage->params.whitePoint, logImage->params.gamma);
00105         scale = 65535.0 / (1.0 - f_black);
00106 
00107         for (i = 0; i <= logImage->params.blackPoint; ++i) {
00108                 logImage->lut10_16[i] = 0;
00109         }
00110         for (; i < logImage->params.whitePoint; ++i) {
00111                 double f_i;
00112                 f_i = convertTo(i, logImage->params.whitePoint, logImage->params.gamma);
00113                 logImage->lut10_16[i] = (int)rint(scale * (f_i - f_black));
00114         }
00115         for (; i < 1024; ++i) {
00116                 logImage->lut10_16[i] = 65535;
00117         }
00118 
00119         for (i = 0; i < 65536; ++i) {
00120                 double f_i = f_black + (i / 65535.0) * (1.0 - f_black);
00121                 logImage->lut16_16[i] = convertFrom(f_i, logImage->params.whitePoint, logImage->params.gamma);
00122         }
00123 }
00124 
00125 /* how many longwords to hold this many pixels? */
00126 int
00127 pixelsToLongs(int numPixels) {
00128         return (numPixels + 2) / 3;
00129 }
00130 
00131 /* byte reversed float */
00132 
00133 typedef union {
00134         U32 i;
00135         R32 f;
00136 } Hack;
00137 
00138 R32
00139 htonf(R32 f) {
00140         Hack hack;
00141         hack.f = f;
00142         hack.i = htonl(hack.i);
00143         return hack.f;
00144 }
00145 
00146 R32
00147 ntohf(R32 f) {
00148         Hack hack;
00149         hack.f = f;
00150         hack.i = ntohl(hack.i);
00151         return hack.f;
00152 }
00153 
00154 #define UNDEF_FLOAT 0x7F800000
00155 
00156 R32
00157 undefined(void) {
00158         Hack hack;
00159         hack.i = UNDEF_FLOAT;
00160         return hack.f;
00161 }
00162 
00163 /* reverse an endian-swapped U16 */
00164 U16
00165 reverseU16(U16 value) {
00166 
00167         union {
00168                 U16 whole;
00169                 char part[2];
00170         } buff;
00171         char temp;
00172         buff.whole = value;
00173         temp = buff.part[0];
00174         buff.part[0] = buff.part[1];
00175         buff.part[1] = temp;
00176         return buff.whole;
00177 }
00178 
00179 /* reverse an endian-swapped U32 */
00180 U32
00181 reverseU32(U32 value) {
00182 
00183         union {
00184                 U32 whole;
00185                 char part[4];
00186         } buff;
00187         char temp;
00188         buff.whole = value;
00189         temp = buff.part[0];
00190         buff.part[0] = buff.part[3];
00191         buff.part[3] = temp;
00192         temp = buff.part[1];
00193         buff.part[1] = buff.part[2];
00194         buff.part[2] = temp;
00195         return buff.whole;
00196 }
00197 
00198 /* reverse an endian-swapped R32 */
00199 R32
00200 reverseR32(R32 value) {
00201 
00202         union {
00203                 R32 whole;
00204                 char part[4];
00205         } buff;
00206         char temp;
00207         buff.whole = value;
00208         temp = buff.part[0];
00209         buff.part[0] = buff.part[3];
00210         buff.part[3] = temp;
00211         temp = buff.part[1];
00212         buff.part[1] = buff.part[2];
00213         buff.part[2] = temp;
00214         return buff.whole;
00215 }
00216 
00217 #if 0
00218 /* bytes per line for images packed 3 10 bit pixels to 32 bits, 32 bit aligned */
00219 int
00220 bytesPerLine_10_4(int numPixels) {
00221         return ((numPixels + 2) / 3) * 4;
00222 }
00223 
00224 void
00225 seekLine_noPadding(LogImageFile* logImage, int lineNumber) {
00226         int fileOffset = bytesPerLine_10_4(lineNumber * logImage->width * logImage->depth);
00227         int filePos = logImage->imageOffset + fileOffset;
00228         if (fseek(logImage->file, filePos, SEEK_SET) != 0) {
00229                 /* complain? */
00230         }
00231 }
00232 
00233 void
00234 seekLine_padding(LogImageFile* logImage, int lineNumber) {
00235         int fileOffset = lineNumber * bytesPerLine_10_4(logImage->width * logImage->depth);
00236         int filePos = logImage->imageOffset + fileOffset;
00237         if (fseek(logImage->file, filePos, SEEK_SET) != 0) {
00238                 /* complain? */
00239         }
00240 }
00241 #endif