Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logImageCore.c « cineon « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1f1500cb83415276cf7fac057ed861392ad1321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/** \file blender/imbuf/intern/cineon/logImageCore.c
 *  \ingroup imbcineon
 */
/*
 *	 Cineon image file format library routines.
 *
 *	 Copyright 1999,2000,2001 David Hodson <hodsond@acm.org>
 *
 *	 This program is free software; you can redistribute it and/or modify it
 *	 under the terms of the GNU General Public License as published by the Free
 *	 Software Foundation; either version 2 of the License, or (at your option)
 *	 any later version.
 *
 *	 This program is distributed in the hope that it will be useful, but
 *	 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *	 or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU General Public License
 *	 for more details.
 *
 *	 You should have received a copy of the GNU General Public License
 *	 along with this program; if not, write to the Free Software
 *	 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "logImageCore.h"

#include <time.h>				 /* strftime() */
#include <math.h>
/* Makes rint consistent in Windows and Linux: */
#define rint(x) floor(x+0.5)

#ifdef WIN32
#include <winsock.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/param.h>
#endif

#if defined(__hpux)
/* These are macros in hpux */
#ifdef htonl
#undef htonl
#undef htons
#undef ntohl
#undef ntohs
#endif
unsigned int htonl(h) unsigned int h; { return(h); }
unsigned short htons(h) unsigned short h; { return(h); }
unsigned int ntohl(n) unsigned int n; { return(n); }
unsigned short ntohs(n) unsigned short n; { return(n); }
#endif
	
	
/* obscure LogImage conversion */
/* from 10 bit int to 0.0 - 1.0 */
/* magic numbers left intact */
static double
convertTo(int inp, int white, float gamma) {
	/*	return pow(pow(10.0, ((inp - white) * 0.002 / 0.6)), gamma); */
	return pow(10.0, (inp - white) * gamma * 0.002 / 0.6);
}

static double
convertFrom(double inp, int white, float gamma) {
	return white + log10(inp) / (gamma * 0.002 / 0.6);
}

/* set up the 10 bit to 8 bit and 8 bit to 10 bit tables */
void
setupLut(LogImageFile *logImage) {

	int i;
	double f_black;
	double scale;

	f_black = convertTo(logImage->params.blackPoint, logImage->params.whitePoint, logImage->params.gamma);
	scale = 255.0 / (1.0 - f_black);

	for (i = 0; i <= logImage->params.blackPoint; ++i) {
		logImage->lut10[i] = 0;
	}
	for (; i < logImage->params.whitePoint; ++i) {
		double f_i;
		f_i = convertTo(i, logImage->params.whitePoint, logImage->params.gamma);
		logImage->lut10[i] = (int)rint(scale * (f_i - f_black));
	}
	for (; i < 1024; ++i) {
		logImage->lut10[i] = 255;
	}

	for (i = 0; i < 256; ++i) {
		double f_i = f_black + (i / 255.0) * (1.0 - f_black);
		logImage->lut8[i] = convertFrom(f_i, logImage->params.whitePoint, logImage->params.gamma);
	}
}

/* set up the 10 bit to 16 bit and 16 bit to 10 bit tables */
void
setupLut16(LogImageFile *logImage) {

	int i;
	double f_black;
	double scale;

	f_black = convertTo(logImage->params.blackPoint, logImage->params.whitePoint, logImage->params.gamma);
	scale = 65535.0 / (1.0 - f_black);

	for (i = 0; i <= logImage->params.blackPoint; ++i) {
		logImage->lut10_16[i] = 0;
	}
	for (; i < logImage->params.whitePoint; ++i) {
		double f_i;
		f_i = convertTo(i, logImage->params.whitePoint, logImage->params.gamma);
		logImage->lut10_16[i] = (int)rint(scale * (f_i - f_black));
	}
	for (; i < 1024; ++i) {
		logImage->lut10_16[i] = 65535;
	}

	for (i = 0; i < 65536; ++i) {
		double f_i = f_black + (i / 65535.0) * (1.0 - f_black);
		logImage->lut16_16[i] = convertFrom(f_i, logImage->params.whitePoint, logImage->params.gamma);
	}
}

/* how many longwords to hold this many pixels? */
int
pixelsToLongs(int numPixels) {
	return (numPixels + 2) / 3;
}

/* byte reversed float */

typedef union {
	U32 i;
	R32 f;
} Hack;

R32
htonf(R32 f) {
	Hack hack;
	hack.f = f;
	hack.i = htonl(hack.i);
	return hack.f;
}

R32
ntohf(R32 f) {
	Hack hack;
	hack.f = f;
	hack.i = ntohl(hack.i);
	return hack.f;
}

#define UNDEF_FLOAT 0x7F800000

R32
undefined(void) {
	Hack hack;
	hack.i = UNDEF_FLOAT;
	return hack.f;
}

/* reverse an endian-swapped U16 */
U16
reverseU16(U16 value) {

	union {
		U16 whole;
		char part[2];
	} buff;
	char temp;
	buff.whole = value;
	temp = buff.part[0];
	buff.part[0] = buff.part[1];
	buff.part[1] = temp;
	return buff.whole;
}

/* reverse an endian-swapped U32 */
U32
reverseU32(U32 value) {

	union {
		U32 whole;
		char part[4];
	} buff;
	char temp;
	buff.whole = value;
	temp = buff.part[0];
	buff.part[0] = buff.part[3];
	buff.part[3] = temp;
	temp = buff.part[1];
	buff.part[1] = buff.part[2];
	buff.part[2] = temp;
	return buff.whole;
}

/* reverse an endian-swapped R32 */
R32
reverseR32(R32 value) {

	union {
		R32 whole;
		char part[4];
	} buff;
	char temp;
	buff.whole = value;
	temp = buff.part[0];
	buff.part[0] = buff.part[3];
	buff.part[3] = temp;
	temp = buff.part[1];
	buff.part[1] = buff.part[2];
	buff.part[2] = temp;
	return buff.whole;
}

#if 0
/* bytes per line for images packed 3 10 bit pixels to 32 bits, 32 bit aligned */
int
bytesPerLine_10_4(int numPixels) {
	return ((numPixels + 2) / 3) * 4;
}

void
seekLine_noPadding(LogImageFile* logImage, int lineNumber) {
	int fileOffset = bytesPerLine_10_4(lineNumber * logImage->width * logImage->depth);
	int filePos = logImage->imageOffset + fileOffset;
	if (fseek(logImage->file, filePos, SEEK_SET) != 0) {
		/* complain? */
	}
}

void
seekLine_padding(LogImageFile* logImage, int lineNumber) {
	int fileOffset = lineNumber * bytesPerLine_10_4(logImage->width * logImage->depth);
	int filePos = logImage->imageOffset + fileOffset;
	if (fseek(logImage->file, filePos, SEEK_SET) != 0) {
		/* complain? */
	}
}
#endif