Debian Codegraph @

pTk/mTk/additions/imgTIFFpixar.c

raw
1 /*
2 * Copyright (c) 1996-1997 Sam Leffler
3 * Copyright (c) 1996 Pixar
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Pixar, Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include "tiffiop.h"
26
27 /*
28 * TIFF Library.
29 * PixarLog Compression Support
30 *
31 * Contributed by Dan McCoy.
32 *
33 * PixarLog film support uses the TIFF library to store companded
34 * 11 bit values into a tiff file, which are compressed using the
35 * zip compressor.
36 *
37 * The codec can take as input and produce as output 32-bit IEEE float values
38 * as well as 16-bit or 8-bit unsigned integer values.
39 *
40 * On writing any of the above are converted into the internal
41 * 11-bit log format. In the case of 8 and 16 bit values, the
42 * input is assumed to be unsigned linear color values that represent
43 * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
44 * be the normal linear color range, in addition over 1 values are
45 * accepted up to a value of about 25.0 to encode "hot" hightlights and such.
46 * The encoding is lossless for 8-bit values, slightly lossy for the
47 * other bit depths. The actual color precision should be better
48 * than the human eye can perceive with extra room to allow for
49 * error introduced by further image computation. As with any quantized
50 * color format, it is possible to perform image calculations which
51 * expose the quantization error. This format should certainly be less
52 * susceptable to such errors than standard 8-bit encodings, but more
53 * susceptable than straight 16-bit or 32-bit encodings.
54 *
55 * On reading the internal format is converted to the desired output format.
56 * The program can request which format it desires by setting the internal
57 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
58 * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
59 * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
60 * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
61 *
62 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
63 * values with the difference that if there are exactly three or four channels
64 * (rgb or rgba) it swaps the channel order (bgr or abgr).
65 *
66 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
67 * packed in 16-bit values. However no tools are supplied for interpreting
68 * these values.
69 *
70 * "hot" (over 1.0) areas written in floating point get clamped to
71 * 1.0 in the integer data types.
72 *
73 * When the file is closed after writing, the bit depth and sample format
74 * are set always to appear as if 8-bit data has been written into it.
75 * That way a naive program unaware of the particulars of the encoding
76 * gets the format it is most likely able to handle.
77 *
78 * The codec does it's own horizontal differencing step on the coded
79 * values so the libraries predictor stuff should be turned off.
80 * The codec also handle byte swapping the encoded values as necessary
81 * since the library does not have the information necessary
82 * to know the bit depth of the raw unencoded buffer.
83 *
84 */
85
86 #include "tif_predict.h"
87 #undef EXPORT
88 #include "zlib.h"
89
90 #include <assert.h>
91 #include <math.h>
92 #include "imgInt.h"
93
94 #ifdef __WIN32__
95 #define Z_LIB_NAME "zlib.dll"
96 #else
97 #define WINAPI /**/
98 #endif
99
100 #ifndef Z_LIB_NAME
101 #define Z_LIB_NAME "libz.so"
102 #endif
103
104 /* Tables for converting to/from 11 bit coded values */
105
106 #define TSIZE 2048 /* decode table size (11-bit tokens) */
107 #define TSIZEP1 2049 /* Plus one for slop */
108 #define ONE 1250 /* token value of 1.0 exactly */
109 #define RATIO 1.004 /* nominal ratio for log part */
110
111 #define CODE_MASK 0x7ff /* 11 bits. */
112
113 static float Fltsize;
114 static float LogK1, LogK2;
115
116 #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
117
118 /*
119 * Stuff to support dynamical loading of libz.
120 */
121
122 static struct LibzFunctions {
123 VOID *handle;
124 int (WINAPI * deflate) _ANSI_ARGS_((z_streamp, int));
125 int (WINAPI * dInit) _ANSI_ARGS_((z_streamp, int, CONST char *, int));
126 int (WINAPI * deflateReset) _ANSI_ARGS_((z_streamp));
127 int (WINAPI * deflateParams) _ANSI_ARGS_((z_streamp, int, int));
128 int (WINAPI * deflateEnd) _ANSI_ARGS_((z_streamp));
129 int (WINAPI * inflate) _ANSI_ARGS_((z_streamp, int));
130 int (WINAPI * iInit) _ANSI_ARGS_((z_streamp, CONST char *, int));
131 int (WINAPI * inflateReset) _ANSI_ARGS_((z_streamp));
132 int (WINAPI * inflateSync) _ANSI_ARGS_((z_streamp));
133 int (WINAPI * inflateEnd) _ANSI_ARGS_((z_streamp));
134 } zlib = {0};
135
136 static char *symbols[] = {
137 "deflate",
138 "deflateInit_",
139 "deflateReset",
140 "deflateParams",
141 "deflateEnd",
142 "inflate",
143 "inflateInit_",
144 "inflateReset",
145 "inflateSync",
146 "inflateEnd",
147 (char *) NULL
148 };
149
150 extern void ImgTIFFfree _ANSI_ARGS_((tdata_t data));
151 extern tdata_t ImgTIFFmalloc _ANSI_ARGS_((tsize_t size));
152 extern tdata_t ImgTIFFrealloc _ANSI_ARGS_((tdata_t data, tsize_t size));
153 extern void ImgTIFFError _ANSI_ARGS_(TCL_VARARGS(CONST char *, module));
154 extern int ImgTIFFPredictorInit _ANSI_ARGS_((TIFF *tif));
155 extern void ImgTIFFMergeFieldInfo _ANSI_ARGS_((TIFF* tif, CONST VOID *voidp, int i));
156 extern int ImgTIFFFlushData1 _ANSI_ARGS_((TIFF *tif));
157 extern void ImgTIFFSwabArrayOfShort _ANSI_ARGS_((uint16*, unsigned long));
158 extern int ImgTIFFSetField _ANSI_ARGS_((TIFF* tif, ttag_t tag, ...));
159 extern tsize_t ImgTIFFTileSize _ANSI_ARGS_((TIFF *tif));
160 extern tsize_t ImgTIFFScanlineSize _ANSI_ARGS_((TIFF *tif));
161 extern void ImgTIFFNoPostDecode _ANSI_ARGS_((TIFF* tiff, tidata_t data, tsize_t size));
162
163 static void
164 horizontalAccumulateF(wp, n, stride, op, ToLinearF)
165 uint16 *wp;
166 int n;
167 int stride;
168 float *op;
169 float *ToLinearF;
170 {
171 register unsigned int cr, cg, cb, ca, mask;
172 register float t0, t1, t2, t3;
173
174 if (n >= stride) {
175 mask = CODE_MASK;
176 if (stride == 3) {
177 t0 = ToLinearF[cr = wp[0]];
178 t1 = ToLinearF[cg = wp[1]];
179 t2 = ToLinearF[cb = wp[2]];
180 op[0] = t0;
181 op[1] = t1;
182 op[2] = t2;
183 n -= 3;
184 while (n > 0) {
185 wp += 3;
186 op += 3;
187 n -= 3;
188 t0 = ToLinearF[(cr += wp[0]) & mask];
189 t1 = ToLinearF[(cg += wp[1]) & mask];
190 t2 = ToLinearF[(cb += wp[2]) & mask];
191 op[0] = t0;
192 op[1] = t1;
193 op[2] = t2;
194 }
195 } else if (stride == 4) {
196 t0 = ToLinearF[cr = wp[0]];
197 t1 = ToLinearF[cg = wp[1]];
198 t2 = ToLinearF[cb = wp[2]];
199 t3 = ToLinearF[ca = wp[3]];
200 op[0] = t0;
201 op[1] = t1;
202 op[2] = t2;
203 op[3] = t3;
204 n -= 4;
205 while (n > 0) {
206 wp += 4;
207 op += 4;
208 n -= 4;
209 t0 = ToLinearF[(cr += wp[0]) & mask];
210 t1 = ToLinearF[(cg += wp[1]) & mask];
211 t2 = ToLinearF[(cb += wp[2]) & mask];
212 t3 = ToLinearF[(ca += wp[3]) & mask];
213 op[0] = t0;
214 op[1] = t1;
215 op[2] = t2;
216 op[3] = t3;
217 }
218 } else {
219 REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
220 n -= stride;
221 while (n > 0) {
222 REPEAT(stride,
223 wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
224 n -= stride;
225 }
226 }
227 }
228 }
229
230 static void
231 horizontalAccumulate12(wp, n, stride, op, ToLinearF)
232 uint16 *wp;
233 int n;
234 int stride;
235 int16 *op;
236 float *ToLinearF;
237 {
238 register unsigned int cr, cg, cb, ca, mask;
239 register float t0, t1, t2, t3;
240
241 #define SCALE12 2048.0
242 #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
243
244 if (n >= stride) {
245 mask = CODE_MASK;
246 if (stride == 3) {
247 t0 = ToLinearF[cr = wp[0]] * SCALE12;
248 t1 = ToLinearF[cg = wp[1]] * SCALE12;
249 t2 = ToLinearF[cb = wp[2]] * SCALE12;
250 op[0] = CLAMP12(t0);
251 op[1] = CLAMP12(t1);
252 op[2] = CLAMP12(t2);
253 n -= 3;
254 while (n > 0) {
255 wp += 3;
256 op += 3;
257 n -= 3;
258 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
259 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
260 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
261 op[0] = CLAMP12(t0);
262 op[1] = CLAMP12(t1);
263 op[2] = CLAMP12(t2);
264 }
265 } else if (stride == 4) {
266 t0 = ToLinearF[cr = wp[0]] * SCALE12;
267 t1 = ToLinearF[cg = wp[1]] * SCALE12;
268 t2 = ToLinearF[cb = wp[2]] * SCALE12;
269 t3 = ToLinearF[ca = wp[3]] * SCALE12;
270 op[0] = CLAMP12(t0);
271 op[1] = CLAMP12(t1);
272 op[2] = CLAMP12(t2);
273 op[3] = CLAMP12(t3);
274 n -= 4;
275 while (n > 0) {
276 wp += 4;
277 op += 4;
278 n -= 4;
279 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
280 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
281 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
282 t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
283 op[0] = CLAMP12(t0);
284 op[1] = CLAMP12(t1);
285 op[2] = CLAMP12(t2);
286 op[3] = CLAMP12(t3);
287 }
288 } else {
289 REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
290 *op = CLAMP12(t0); wp++; op++)
291 n -= stride;
292 while (n > 0) {
293 REPEAT(stride,
294 wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
295 *op = CLAMP12(t0); wp++; op++)
296 n -= stride;
297 }
298 }
299 }
300 }
301
302 static void
303 horizontalAccumulate16(wp, n, stride, op, ToLinear16)
304 uint16 *wp;
305 int n;
306 int stride;
307 uint16 *op;
308 uint16 *ToLinear16;
309 {
310 register unsigned int cr, cg, cb, ca, mask;
311
312 if (n >= stride) {
313 mask = CODE_MASK;
314 if (stride == 3) {
315 op[0] = ToLinear16[cr = wp[0]];
316 op[1] = ToLinear16[cg = wp[1]];
317 op[2] = ToLinear16[cb = wp[2]];
318 n -= 3;
319 while (n > 0) {
320 wp += 3;
321 op += 3;
322 n -= 3;
323 op[0] = ToLinear16[(cr += wp[0]) & mask];
324 op[1] = ToLinear16[(cg += wp[1]) & mask];
325 op[2] = ToLinear16[(cb += wp[2]) & mask];
326 }
327 } else if (stride == 4) {
328 op[0] = ToLinear16[cr = wp[0]];
329 op[1] = ToLinear16[cg = wp[1]];
330 op[2] = ToLinear16[cb = wp[2]];
331 op[3] = ToLinear16[ca = wp[3]];
332 n -= 4;
333 while (n > 0) {
334 wp += 4;
335 op += 4;
336 n -= 4;
337 op[0] = ToLinear16[(cr += wp[0]) & mask];
338 op[1] = ToLinear16[(cg += wp[1]) & mask];
339 op[2] = ToLinear16[(cb += wp[2]) & mask];
340 op[3] = ToLinear16[(ca += wp[3]) & mask];
341 }
342 } else {
343 REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
344 n -= stride;
345 while (n > 0) {
346 REPEAT(stride,
347 wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
348 n -= stride;
349 }
350 }
351 }
352 }
353
354 /*
355 * Returns the log encoded 11-bit values with the horizontal
356 * differencing undone.
357 */
358 static void
359 horizontalAccumulate11(wp, n, stride, op)
360 uint16 *wp;
361 int n;
362 int stride;
363 uint16 *op;
364 {
365 register unsigned int cr, cg, cb, ca, mask;
366
367 if (n >= stride) {
368 mask = CODE_MASK;
369 if (stride == 3) {
370 op[0] = cr = wp[0]; op[1] = cg = wp[1]; op[2] = cb = wp[2];
371 n -= 3;
372 while (n > 0) {
373 wp += 3;
374 op += 3;
375 n -= 3;
376 op[0] = (cr += wp[0]) & mask;
377 op[1] = (cg += wp[1]) & mask;
378 op[2] = (cb += wp[2]) & mask;
379 }
380 } else if (stride == 4) {
381 op[0] = cr = wp[0]; op[1] = cg = wp[1];
382 op[2] = cb = wp[2]; op[3] = ca = wp[3];
383 n -= 4;
384 while (n > 0) {
385 wp += 4;
386 op += 4;
387 n -= 4;
388 op[0] = (cr += wp[0]) & mask;
389 op[1] = (cg += wp[1]) & mask;
390 op[2] = (cb += wp[2]) & mask;
391 op[3] = (ca += wp[3]) & mask;
392 }
393 } else {
394 REPEAT(stride, *op = *wp&mask; wp++; op++)
395 n -= stride;
396 while (n > 0) {
397 REPEAT(stride,
398 wp[stride] += *wp; *op = *wp&mask; wp++; op++)
399 n -= stride;
400 }
401 }
402 }
403 }
404
405 static void
406 horizontalAccumulate8(wp, n, stride, op, ToLinear8)
407 uint16 *wp;
408 int n;
409 int stride;
410 unsigned char *op;
411 unsigned char *ToLinear8;
412 {
413 register unsigned int cr, cg, cb, ca, mask;
414
415 if (n >= stride) {
416 mask = CODE_MASK;
417 if (stride == 3) {
418 op[0] = ToLinear8[cr = wp[0]];
419 op[1] = ToLinear8[cg = wp[1]];
420 op[2] = ToLinear8[cb = wp[2]];
421 n -= 3;
422 while (n > 0) {
423 n -= 3;
424 wp += 3;
425 op += 3;
426 op[0] = ToLinear8[(cr += wp[0]) & mask];
427 op[1] = ToLinear8[(cg += wp[1]) & mask];
428 op[2] = ToLinear8[(cb += wp[2]) & mask];
429 }
430 } else if (stride == 4) {
431 op[0] = ToLinear8[cr = wp[0]];
432 op[1] = ToLinear8[cg = wp[1]];
433 op[2] = ToLinear8[cb = wp[2]];
434 op[3] = ToLinear8[ca = wp[3]];
435 n -= 4;
436 while (n > 0) {
437 n -= 4;
438 wp += 4;
439 op += 4;
440 op[0] = ToLinear8[(cr += wp[0]) & mask];
441 op[1] = ToLinear8[(cg += wp[1]) & mask];
442 op[2] = ToLinear8[(cb += wp[2]) & mask];
443 op[3] = ToLinear8[(ca += wp[3]) & mask];
444 }
445 } else {
446 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
447 n -= stride;
448 while (n > 0) {
449 REPEAT(stride,
450 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
451 n -= stride;
452 }
453 }
454 }
455 }
456
457
458 static void
459 horizontalAccumulate8abgr(wp, n, stride, op, ToLinear8)
460 uint16 *wp;
461 int n;
462 int stride;
463 unsigned char *op;
464 unsigned char *ToLinear8;
465 {
466 register unsigned int cr, cg, cb, ca, mask;
467 register unsigned char t0, t1, t2, t3;
468
469 if (n >= stride) {
470 mask = CODE_MASK;
471 if (stride == 3) {
472 op[0] = 0;
473 t1 = ToLinear8[cb = wp[2]];
474 t2 = ToLinear8[cg = wp[1]];
475 t3 = ToLinear8[cr = wp[0]];
476 op[1] = t1;
477 op[2] = t2;
478 op[3] = t3;
479 n -= 3;
480 while (n > 0) {
481 n -= 3;
482 wp += 3;
483 op += 4;
484 op[0] = 0;
485 t1 = ToLinear8[(cb += wp[2]) & mask];
486 t2 = ToLinear8[(cg += wp[1]) & mask];
487 t3 = ToLinear8[(cr += wp[0]) & mask];
488 op[1] = t1;
489 op[2] = t2;
490 op[3] = t3;
491 }
492 } else if (stride == 4) {
493 t0 = ToLinear8[ca = wp[3]];
494 t1 = ToLinear8[cb = wp[2]];
495 t2 = ToLinear8[cg = wp[1]];
496 t3 = ToLinear8[cr = wp[0]];
497 op[0] = t0;
498 op[1] = t1;
499 op[2] = t2;
500 op[3] = t3;
501 n -= 4;
502 while (n > 0) {
503 n -= 4;
504 wp += 4;
505 op += 4;
506 t0 = ToLinear8[(ca += wp[3]) & mask];
507 t1 = ToLinear8[(cb += wp[2]) & mask];
508 t2 = ToLinear8[(cg += wp[1]) & mask];
509 t3 = ToLinear8[(cr += wp[0]) & mask];
510 op[0] = t0;
511 op[1] = t1;
512 op[2] = t2;
513 op[3] = t3;
514 }
515 } else {
516 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
517 n -= stride;
518 while (n > 0) {
519 REPEAT(stride,
520 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
521 n -= stride;
522 }
523 }
524 }
525 }
526
527 /*
528 * State block for each open TIFF
529 * file using PixarLog compression/decompression.
530 */
531 typedef struct {
532 TIFFPredictorState predict;
533 z_stream stream;
534 uint16 *tbuf;
535 uint16 stride;
536 int state;
537 int user_datafmt;
538 int quality;
539 #define PLSTATE_INIT 1
540
541 TIFFVSetMethod vgetparent; /* super-class method */
542 TIFFVSetMethod vsetparent; /* super-class method */
543
544 float *ToLinearF;
545 uint16 *ToLinear16;
546 unsigned char *ToLinear8;
547 uint16 *FromLT2;
548 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
549 uint16 *From8;
550
551 } PixarLogState;
552
553 static int
554 PixarLogMakeTables(sp)
555 PixarLogState *sp;
556 {
557
558 /*
559 * We make several tables here to convert between various external
560 * representations (float, 16-bit, and 8-bit) and the internal
561 * 11-bit companded representation. The 11-bit representation has two
562 * distinct regions. A linear bottom end up through .018316 in steps
563 * of about .000073, and a region of constant ratio up to about 25.
564 * These floating point numbers are stored in the main table ToLinearF.
565 * All other tables are derived from this one. The tables (and the
566 * ratios) are continuous at the internal seam.
567 */
568
569 int nlin, lt2size;
570 int i, j;
571 double b, c, linstep;
572 double v;
573 float *ToLinearF;
574 uint16 *ToLinear16;
575 unsigned char *ToLinear8;
576 uint16 *FromLT2;
577 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
578 uint16 *From8;
579
580 c = log(RATIO);
581 nlin = 1./c; /* nlin must be an integer */
582 c = 1./nlin;
583 b = exp(-c*ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
584 linstep = b*c*exp(1.);
585
586 LogK1 = 1./c; /* if (v >= 2) token = k1*log(v*k2) */
587 LogK2 = 1./b;
588 lt2size = (2./linstep)+1;
589 FromLT2 = (uint16 *)ImgTIFFmalloc(lt2size*sizeof(uint16));
590 From14 = (uint16 *)ImgTIFFmalloc(16384*sizeof(uint16));
591 From8 = (uint16 *)ImgTIFFmalloc(256*sizeof(uint16));
592 ToLinearF = (float *)ImgTIFFmalloc(TSIZEP1 * sizeof(float));
593 ToLinear16 = (uint16 *)ImgTIFFmalloc(TSIZEP1 * sizeof(uint16));
594 ToLinear8 = (unsigned char *)ImgTIFFmalloc(TSIZEP1 * sizeof(unsigned char));
595 if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
596 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
597 if (FromLT2) ImgTIFFfree(FromLT2);
598 if (From14) ImgTIFFfree(From14);
599 if (From8) ImgTIFFfree(From8);
600 if (ToLinearF) ImgTIFFfree(ToLinearF);
601 if (ToLinear16) ImgTIFFfree(ToLinear16);
602 if (ToLinear8) ImgTIFFfree(ToLinear8);
603 sp->FromLT2 = NULL;
604 sp->From14 = NULL;
605 sp->From8 = NULL;
606 sp->ToLinearF = NULL;
607 sp->ToLinear16 = NULL;
608 sp->ToLinear8 = NULL;
609 return 0;
610 }
611
612 j = 0;
613
614 for (i = 0; i < nlin; i++) {
615 v = i * linstep;
616 ToLinearF[j++] = v;
617 }
618
619 for (i = nlin; i < TSIZE; i++)
620 ToLinearF[j++] = b*exp(c*i);
621
622 ToLinearF[2048] = ToLinearF[2047];
623
624 for (i = 0; i < TSIZEP1; i++) {
625 v = ToLinearF[i]*65535.0 + 0.5;
626 ToLinear16[i] = (v > 65535.0) ? 65535 : v;
627 v = ToLinearF[i]*255.0 + 0.5;
628 ToLinear8[i] = (v > 255.0) ? 255 : v;
629 }
630
631 j = 0;
632 for (i = 0; i < lt2size; i++) {
633 if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
634 j++;
635 FromLT2[i] = j;
636 }
637
638 /*
639 * Since we lose info anyway on 16-bit data, we set up a 14-bit
640 * table and shift 16-bit values down two bits on input.
641 * saves a little table space.
642 */
643 j = 0;
644 for (i = 0; i < 16384; i++) {
645 while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
646 j++;
647 From14[i] = j;
648 }
649
650 j = 0;
651 for (i = 0; i < 256; i++) {
652 while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
653 j++;
654 From8[i] = j;
655 }
656
657 Fltsize = lt2size/2;
658
659 sp->ToLinearF = ToLinearF;
660 sp->ToLinear16 = ToLinear16;
661 sp->ToLinear8 = ToLinear8;
662 sp->FromLT2 = FromLT2;
663 sp->From14 = From14;
664 sp->From8 = From8;
665
666 return 1;
667 }
668
669 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
670 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
671
672 #define N(a) (sizeof(a)/sizeof(a[0]))
673 #define PIXARLOGDATAFMT_UNKNOWN -1
674
675 static int
676 PixarLogGuessDataFmt(td)
677 TIFFDirectory *td;
678 {
679 int guess = PIXARLOGDATAFMT_UNKNOWN;
680 int format = td->td_sampleformat;
681
682 /* If the user didn't tell us his datafmt,
683 * take our best guess from the bitspersample.
684 */
685 switch (td->td_bitspersample) {
686 case 32:
687 if (format == SAMPLEFORMAT_IEEEFP)
688 guess = PIXARLOGDATAFMT_FLOAT;
689 break;
690 case 16:
691 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
692 guess = PIXARLOGDATAFMT_16BIT;
693 break;
694 case 12:
695 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
696 guess = PIXARLOGDATAFMT_12BITPICIO;
697 break;
698 case 11:
699 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
700 guess = PIXARLOGDATAFMT_11BITLOG;
701 break;
702 case 8:
703 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
704 guess = PIXARLOGDATAFMT_8BIT;
705 break;
706 }
707
708 return guess;
709 }
710
711 static int
712 PixarLogSetupDecode(tif)
713 TIFF* tif;
714 {
715 TIFFDirectory *td = &tif->tif_dir;
716 PixarLogState* sp = DecoderState(tif);
717 static const char module[] = "PixarLogSetupDecode";
718
719 assert(sp != NULL);
720
721 /* Make sure no byte swapping happens on the data
722 * after decompression. */
723 tif->tif_postdecode = ImgTIFFNoPostDecode;
724
725 /* for some reason, we can't do this in TIFFInitPixarLog */
726
727 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
728 td->td_samplesperpixel : 1);
729 sp->tbuf = (uint16 *) ImgTIFFmalloc(sp->stride *
730 td->td_imagewidth * td->td_rowsperstrip * sizeof(uint16));
731 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
732 sp->user_datafmt = PixarLogGuessDataFmt(td);
733 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
734 ImgTIFFError(module,
735 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
736 td->td_bitspersample);
737 return (0);
738 }
739
740 if (zlib.iInit(&sp->stream, ZLIB_VERSION, sizeof(z_stream)) != Z_OK) {
741 ImgTIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
742 return (0);
743 } else {
744 sp->state |= PLSTATE_INIT;
745 return (1);
746 }
747 }
748
749 /*
750 * Setup state for decoding a strip.
751 */
752 static int
753 #ifdef _USING_PROTOTYPES_
754 PixarLogPreDecode (
755 TIFF* tif,
756 tsample_t s)
757 #else
758 PixarLogPreDecode(tif, s)
759 TIFF* tif;
760 tsample_t s;
761 #endif
762 {
763 PixarLogState* sp = DecoderState(tif);
764
765 (void) s;
766 assert(sp != NULL);
767 sp->stream.next_in = tif->tif_rawdata;
768 sp->stream.avail_in = tif->tif_rawcc;
769 return (zlib.inflateReset(&sp->stream) == Z_OK);
770 }
771
772 static int
773 #ifdef _USING_PROTOTYPES_
774 PixarLogDecode(
775 TIFF* tif,
776 tidata_t op,
777 tsize_t occ,
778 tsample_t s)
779 #else
780 PixarLogDecode(tif, op, occ, s)
781 TIFF* tif;
782 tidata_t op;
783 tsize_t occ;
784 tsample_t s;
785 #endif
786 {
787 TIFFDirectory *td = &tif->tif_dir;
788 PixarLogState* sp = DecoderState(tif);
789 static const char module[] = "PixarLogDecode";
790 int i, nsamples, llen;
791 uint16 *up;
792
793 switch (sp->user_datafmt) {
794 case PIXARLOGDATAFMT_FLOAT:
795 nsamples = occ / sizeof(float); /* XXX float == 32 bits */
796 break;
797 case PIXARLOGDATAFMT_16BIT:
798 case PIXARLOGDATAFMT_12BITPICIO:
799 case PIXARLOGDATAFMT_11BITLOG:
800 nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
801 break;
802 case PIXARLOGDATAFMT_8BIT:
803 case PIXARLOGDATAFMT_8BITABGR:
804 nsamples = occ;
805 break;
806 default:
807 ImgTIFFError(tif->tif_name,
808 "%d bit input not supported in PixarLog",
809 td->td_bitspersample);
810 return 0;
811 }
812
813 llen = sp->stride * td->td_imagewidth;
814
815 (void) s;
816 assert(sp != NULL);
817 sp->stream.next_out = (unsigned char *) sp->tbuf;
818 sp->stream.avail_out = nsamples * sizeof(uint16);
819 do {
820 int state = zlib.inflate(&sp->stream, Z_PARTIAL_FLUSH);
821 if (state == Z_STREAM_END) {
822 break; /* XXX */
823 }
824 if (state == Z_DATA_ERROR) {
825 ImgTIFFError(module,
826 "%s: Decoding error at scanline %d, %s",
827 tif->tif_name, tif->tif_row, sp->stream.msg);
828 if (zlib.inflateSync(&sp->stream) != Z_OK)
829 return (0);
830 continue;
831 }
832 if (state != Z_OK) {
833 ImgTIFFError(module, "%s: zlib error: %s",
834 tif->tif_name, sp->stream.msg);
835 return (0);
836 }
837 } while (sp->stream.avail_out > 0);
838
839 /* hopefully, we got all the bytes we needed */
840 if (sp->stream.avail_out != 0) {
841 ImgTIFFError(module,
842 "%s: Not enough data at scanline %d (short %d bytes)",
843 tif->tif_name, tif->tif_row, sp->stream.avail_out);
844 return (0);
845 }
846
847 up = sp->tbuf;
848 /* Swap bytes in the data if from a different endian machine. */
849 if (tif->tif_flags & TIFF_SWAB)
850 ImgTIFFSwabArrayOfShort(up, nsamples);
851
852 for (i = 0; i < nsamples; i += llen, up += llen) {
853 switch (sp->user_datafmt) {
854 case PIXARLOGDATAFMT_FLOAT:
855 horizontalAccumulateF(up, llen, sp->stride,
856 (float *)op, sp->ToLinearF);
857 op += llen * sizeof(float);
858 break;
859 case PIXARLOGDATAFMT_16BIT:
860 horizontalAccumulate16(up, llen, sp->stride,
861 (uint16 *)op, sp->ToLinear16);
862 op += llen * sizeof(uint16);
863 break;
864 case PIXARLOGDATAFMT_12BITPICIO:
865 horizontalAccumulate12(up, llen, sp->stride,
866 (int16 *)op, sp->ToLinearF);
867 op += llen * sizeof(int16);
868 break;
869 case PIXARLOGDATAFMT_11BITLOG:
870 horizontalAccumulate11(up, llen, sp->stride,
871 (uint16 *)op);
872 op += llen * sizeof(uint16);
873 break;
874 case PIXARLOGDATAFMT_8BIT:
875 horizontalAccumulate8(up, llen, sp->stride,
876 (unsigned char *)op, sp->ToLinear8);
877 op += llen * sizeof(unsigned char);
878 break;
879 case PIXARLOGDATAFMT_8BITABGR:
880 horizontalAccumulate8abgr(up, llen, sp->stride,
881 (unsigned char *)op, sp->ToLinear8);
882 op += llen * sizeof(unsigned char);
883 break;
884 default:
885 ImgTIFFError(tif->tif_name,
886 "PixarLogDecode: unsupported bits/sample: %d",
887 td->td_bitspersample);
888 return (0);
889 }
890 }
891
892 return (1);
893 }
894
895 static int
896 PixarLogSetupEncode(tif)
897 TIFF* tif;
898 {
899 TIFFDirectory *td = &tif->tif_dir;
900 PixarLogState* sp = EncoderState(tif);
901 static const char module[] = "PixarLogSetupEncode";
902
903 assert(sp != NULL);
904
905 /* for some reason, we can't do this in TIFFInitPixarLog */
906
907 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
908 td->td_samplesperpixel : 1);
909 sp->tbuf = (uint16 *) ImgTIFFmalloc(sp->stride *
910 td->td_imagewidth * td->td_rowsperstrip * sizeof(uint16));
911 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
912 sp->user_datafmt = PixarLogGuessDataFmt(td);
913 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
914 ImgTIFFError(module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
915 return (0);
916 }
917
918 if (zlib.dInit(&sp->stream, sp->quality, ZLIB_VERSION, sizeof(z_stream)) != Z_OK) {
919 ImgTIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
920 return (0);
921 } else {
922 sp->state |= PLSTATE_INIT;
923 return (1);
924 }
925 }
926
927 /*
928 * Reset encoding state at the start of a strip.
929 */
930 static int
931 #ifdef _USING_PROTOTYPES_
932 PixarLogPreEncode (
933 TIFF* tif,
934 tsample_t s)
935 #else
936 PixarLogPreEncode(tif, s)
937 TIFF* tif;
938 tsample_t s;
939 #endif
940 {
941 PixarLogState *sp = EncoderState(tif);
942
943 (void) s;
944 assert(sp != NULL);
945 sp->stream.next_out = tif->tif_rawdata;
946 sp->stream.avail_out = tif->tif_rawdatasize;
947 return (zlib.deflateReset(&sp->stream) == Z_OK);
948 }
949
950 static void
951 horizontalDifferenceF(ip, n, stride, wp, FromLT2)
952 float *ip;
953 int n;
954 int stride;
955 uint16 *wp;
956 uint16 *FromLT2;
957 {
958
959 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
960 register float fltsize = Fltsize;
961
962 #define CLAMP(v) ( (v<(float)0.) ? 0 \
963 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
964 : (v>(float)24.2) ? 2047 \
965 : LogK1*log(v*LogK2) + 0.5 )
966
967 mask = CODE_MASK;
968 if (n >= stride) {
969 if (stride == 3) {
970 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
971 b2 = wp[2] = CLAMP(ip[2]);
972 n -= 3;
973 while (n > 0) {
974 n -= 3;
975 wp += 3;
976 ip += 3;
977 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
978 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
979 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
980 }
981 } else if (stride == 4) {
982 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
983 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
984 n -= 4;
985 while (n > 0) {
986 n -= 4;
987 wp += 4;
988 ip += 4;
989 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
990 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
991 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
992 a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
993 }
994 } else {
995 ip += n - 1; /* point to last one */
996 wp += n - 1; /* point to last one */
997 n -= stride;
998 while (n > 0) {
999 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1000 wp[stride] -= wp[0];
1001 wp[stride] &= mask;
1002 wp--; ip--)
1003 n -= stride;
1004 }
1005 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1006 }
1007 }
1008 }
1009
1010 static void
1011 horizontalDifference16(ip, n, stride, wp, From14)
1012 unsigned short *ip;
1013 int n;
1014 int stride;
1015 unsigned short *wp;
1016 uint16 *From14;
1017 {
1018 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1019
1020 /* assumption is unsigned pixel values */
1021 #undef CLAMP
1022 #define CLAMP(v) From14[(v) >> 2]
1023
1024 mask = CODE_MASK;
1025 if (n >= stride) {
1026 if (stride == 3) {
1027 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1028 b2 = wp[2] = CLAMP(ip[2]);
1029 n -= 3;
1030 while (n > 0) {
1031 n -= 3;
1032 wp += 3;
1033 ip += 3;
1034 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
1035 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
1036 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
1037 }
1038 } else if (stride == 4) {
1039 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1040 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1041 n -= 4;
1042 while (n > 0) {
1043 n -= 4;
1044 wp += 4;
1045 ip += 4;
1046 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
1047 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
1048 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
1049 a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
1050 }
1051 } else {
1052 ip += n - 1; /* point to last one */
1053 wp += n - 1; /* point to last one */
1054 n -= stride;
1055 while (n > 0) {
1056 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1057 wp[stride] -= wp[0];
1058 wp[stride] &= mask;
1059 wp--; ip--)
1060 n -= stride;
1061 }
1062 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1063 }
1064 }
1065 }
1066
1067
1068 static void
1069 horizontalDifference8(ip, n, stride, wp, From8)
1070 unsigned char *ip;
1071 int n;
1072 int stride;
1073 unsigned short *wp;
1074 uint16 *From8;
1075 {
1076 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1077
1078 #undef CLAMP
1079 #define CLAMP(v) (From8[(v)])
1080
1081 mask = CODE_MASK;
1082 if (n >= stride) {
1083 if (stride == 3) {
1084 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1085 b2 = wp[2] = CLAMP(ip[2]);
1086 n -= 3;
1087 while (n > 0) {
1088 n -= 3;
1089 r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
1090 g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
1091 b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
1092 wp += 3;
1093 ip += 3;
1094 }
1095 } else if (stride == 4) {
1096 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1097 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1098 n -= 4;
1099 while (n > 0) {
1100 n -= 4;
1101 r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
1102 g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
1103 b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
1104 a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
1105 wp += 4;
1106 ip += 4;
1107 }
1108 } else {
1109 wp += n + stride - 1; /* point to last one */
1110 ip += n + stride - 1; /* point to last one */
1111 n -= stride;
1112 while (n > 0) {
1113 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1114 wp[stride] -= wp[0];
1115 wp[stride] &= mask;
1116 wp--; ip--)
1117 n -= stride;
1118 }
1119 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1120 }
1121 }
1122 }
1123
1124 /*
1125 * Encode a chunk of pixels.
1126 */
1127 static int
1128 #ifdef _USING_PROTOTYPES_
1129 PixarLogEncode(
1130 TIFF* tif,
1131 tidata_t bp,
1132 tsize_t cc,
1133 tsample_t s)
1134 #else
1135 PixarLogEncode(tif, bp, cc, s)
1136 TIFF* tif;
1137 tidata_t bp;
1138 tsize_t cc;
1139 tsample_t s;
1140 #endif
1141 {
1142 TIFFDirectory *td = &tif->tif_dir;
1143 PixarLogState *sp = EncoderState(tif);
1144 static const char module[] = "PixarLogEncode";
1145 int i, n, llen;
1146 unsigned short * up;
1147
1148 (void) s;
1149
1150 switch (sp->user_datafmt) {
1151 case PIXARLOGDATAFMT_FLOAT:
1152 n = cc / sizeof(float); /* XXX float == 32 bits */
1153 break;
1154 case PIXARLOGDATAFMT_16BIT:
1155 case PIXARLOGDATAFMT_12BITPICIO:
1156 case PIXARLOGDATAFMT_11BITLOG:
1157 n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
1158 break;
1159 case PIXARLOGDATAFMT_8BIT:
1160 case PIXARLOGDATAFMT_8BITABGR:
1161 n = cc;
1162 break;
1163 default:
1164 ImgTIFFError(tif->tif_name,
1165 "%d bit input not supported in PixarLog",
1166 td->td_bitspersample);
1167 return 0;
1168 }
1169
1170 llen = sp->stride * td->td_imagewidth;
1171
1172 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
1173 switch (sp->user_datafmt) {
1174 case PIXARLOGDATAFMT_FLOAT:
1175 horizontalDifferenceF((float *)bp, llen,
1176 sp->stride, up, sp->FromLT2);
1177 bp += llen * sizeof(float);
1178 break;
1179 case PIXARLOGDATAFMT_16BIT:
1180 horizontalDifference16((uint16 *)bp, llen,
1181 sp->stride, up, sp->From14);
1182 bp += llen * sizeof(uint16);
1183 break;
1184 case PIXARLOGDATAFMT_8BIT:
1185 horizontalDifference8((unsigned char *)bp, llen,
1186 sp->stride, up, sp->From8);
1187 bp += llen * sizeof(unsigned char);
1188 break;
1189 default:
1190 ImgTIFFError(tif->tif_name,
1191 "%d bit input not supported in PixarLog",
1192 td->td_bitspersample);
1193 return 0;
1194 }
1195 }
1196
1197 sp->stream.next_in = (unsigned char *) sp->tbuf;
1198 sp->stream.avail_in = n * sizeof(uint16);
1199
1200 do {
1201 if (zlib.deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
1202 ImgTIFFError(module, "%s: Encoder error: %s",
1203 tif->tif_name, sp->stream.msg);
1204 return (0);
1205 }
1206 if (sp->stream.avail_out == 0) {
1207 tif->tif_rawcc = tif->tif_rawdatasize;
1208 ImgTIFFFlushData1(tif);
1209 sp->stream.next_out = tif->tif_rawdata;
1210 sp->stream.avail_out = tif->tif_rawdatasize;
1211 }
1212 } while (sp->stream.avail_in > 0);
1213 return (1);
1214 }
1215
1216 /*
1217 * Finish off an encoded strip by flushing the last
1218 * string and tacking on an End Of Information code.
1219 */
1220
1221 static int
1222 PixarLogPostEncode(tif)
1223 TIFF* tif;
1224 {
1225 PixarLogState *sp = EncoderState(tif);
1226 static const char module[] = "PixarLogPostEncode";
1227 int state;
1228
1229 sp->stream.avail_in = 0;
1230
1231 do {
1232 state = zlib.deflate(&sp->stream, Z_FINISH);
1233 switch (state) {
1234 case Z_STREAM_END:
1235 case Z_OK:
1236 if (sp->stream.avail_out != tif->tif_rawdatasize) {
1237 tif->tif_rawcc =
1238 tif->tif_rawdatasize - sp->stream.avail_out;
1239 ImgTIFFFlushData1(tif);
1240 sp->stream.next_out = tif->tif_rawdata;
1241 sp->stream.avail_out = tif->tif_rawdatasize;
1242 }
1243 break;
1244 default:
1245 ImgTIFFError(module, "%s: zlib error: %s",
1246 tif->tif_name, sp->stream.msg);
1247 return (0);
1248 }
1249 } while (state != Z_STREAM_END);
1250 return (1);
1251 }
1252
1253 static void
1254 PixarLogClose(tif)
1255 TIFF* tif;
1256 {
1257 TIFFDirectory *td = &tif->tif_dir;
1258
1259 /* In a really sneaky maneuver, on close, we covertly modify both
1260 * bitspersample and sampleformat in the directory to indicate
1261 * 8-bit linear. This way, the decode "just works" even for
1262 * readers that don't know about PixarLog, or how to set
1263 * the PIXARLOGDATFMT pseudo-tag.
1264 */
1265 td->td_bitspersample = 8;
1266 td->td_sampleformat = SAMPLEFORMAT_UINT;
1267 }
1268
1269 static void
1270 PixarLogCleanup(tif)
1271 TIFF* tif;
1272 {
1273 PixarLogState* sp = (PixarLogState*) tif->tif_data;
1274
1275 if (sp) {
1276 if (sp->FromLT2) ImgTIFFfree(sp->FromLT2);
1277 if (sp->From14) ImgTIFFfree(sp->From14);
1278 if (sp->From8) ImgTIFFfree(sp->From8);
1279 if (sp->ToLinearF) ImgTIFFfree(sp->ToLinearF);
1280 if (sp->ToLinear16) ImgTIFFfree(sp->ToLinear16);
1281 if (sp->ToLinear8) ImgTIFFfree(sp->ToLinear8);
1282 if (sp->state&PLSTATE_INIT) {
1283 if (tif->tif_mode == O_RDONLY)
1284 zlib.inflateEnd(&sp->stream);
1285 else
1286 zlib.deflateEnd(&sp->stream);
1287 }
1288 if (sp->tbuf)
1289 ImgTIFFfree(sp->tbuf);
1290 ImgTIFFfree(sp);
1291 tif->tif_data = NULL;
1292 }
1293 }
1294
1295 static int
1296 PixarLogVSetField(tif, tag, ap)
1297 TIFF* tif;
1298 ttag_t tag;
1299 va_list ap;
1300 {
1301 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1302 int result;
1303 static const char module[] = "PixarLogVSetField";
1304
1305 switch (tag) {
1306 case TIFFTAG_PIXARLOGQUALITY:
1307 sp->quality = va_arg(ap, int);
1308 if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
1309 if (zlib.deflateParams(&sp->stream,
1310 sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
1311 ImgTIFFError(module, "%s: zlib error: %s",
1312 tif->tif_name, sp->stream.msg);
1313 return (0);
1314 }
1315 }
1316 return (1);
1317 case TIFFTAG_PIXARLOGDATAFMT:
1318 sp->user_datafmt = va_arg(ap, int);
1319 /* Tweak the TIFF header so that the rest of libtiff knows what
1320 * size of data will be passed between app and library, and
1321 * assume that the app knows what it is doing and is not
1322 * confused by these header manipulations...
1323 */
1324 switch (sp->user_datafmt) {
1325 case PIXARLOGDATAFMT_8BIT:
1326 case PIXARLOGDATAFMT_8BITABGR:
1327 ImgTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1328 ImgTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1329 break;
1330 case PIXARLOGDATAFMT_11BITLOG:
1331 ImgTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1332 ImgTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1333 break;
1334 case PIXARLOGDATAFMT_12BITPICIO:
1335 ImgTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1336 ImgTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1337 break;
1338 case PIXARLOGDATAFMT_16BIT:
1339 ImgTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1340 ImgTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1341 break;
1342 case PIXARLOGDATAFMT_FLOAT:
1343 ImgTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1344 ImgTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
1345 break;
1346 }
1347 /*
1348 * Must recalculate sizes should bits/sample change.
1349 */
1350 tif->tif_tilesize = ImgTIFFTileSize(tif);
1351 tif->tif_scanlinesize = ImgTIFFScanlineSize(tif);
1352 result = 1; /* NB: pseudo tag */
1353 break;
1354 default:
1355 result = (*sp->vsetparent)(tif, tag, ap);
1356 }
1357 return (result);
1358 }
1359
1360 static int
1361 PixarLogVGetField(tif, tag, ap)
1362 TIFF* tif;
1363 ttag_t tag;
1364 va_list ap;
1365 {
1366 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1367
1368 switch (tag) {
1369 case TIFFTAG_PIXARLOGQUALITY:
1370 *va_arg(ap, int*) = sp->quality;
1371 break;
1372 case TIFFTAG_PIXARLOGDATAFMT:
1373 *va_arg(ap, int*) = sp->user_datafmt;
1374 break;
1375 default:
1376 return (*sp->vgetparent)(tif, tag, ap);
1377 }
1378 return (1);
1379 }
1380
1381 static voidpf
1382 PixarLogAlloc(opaque, items, size)
1383 voidpf opaque;
1384 uInt items;
1385 uInt size;
1386 {
1387 return (voidpf) ImgTIFFmalloc((tsize_t)(items * size));
1388 }
1389
1390 static void
1391 PixarLogFree(opaque, address)
1392 voidpf opaque;
1393 voidpf address;
1394 {
1395 ImgTIFFfree((tdata_t) address);
1396 }
1397
1398 static const TIFFFieldInfo pixarlogFieldInfo[] = {
1399 {TIFFTAG_PIXARLOGDATAFMT,0,0,TIFF_ANY, FIELD_PSEUDO,FALSE,FALSE,""},
1400 {TIFFTAG_PIXARLOGQUALITY,0,0,TIFF_ANY, FIELD_PSEUDO,FALSE,FALSE,""}
1401 };
1402
1403 int
1404 ImgInitTIFFpixar(handle, scheme)
1405 VOID * handle;
1406 int scheme;
1407 {
1408 TIFF* tif = (TIFF *) handle;
1409 PixarLogState* sp;
1410
1411 assert(scheme == COMPRESSION_PIXARLOG);
1412
1413 if (ImgLoadLib(NULL, Z_LIB_NAME, &zlib.handle, symbols, 10)
1414 != TCL_OK) {
1415 ImgTIFFError("", "%s: Cannot load %s", tif->tif_name, Z_LIB_NAME);
1416 return (0);
1417 }
1418 /*
1419 * Allocate state block so tag methods have storage to record values.
1420 */
1421 tif->tif_data = (tidata_t) ImgTIFFmalloc(sizeof (PixarLogState));
1422 if (tif->tif_data == NULL)
1423 goto bad;
1424 sp = (PixarLogState*) tif->tif_data;
1425 memset(sp, 0, sizeof (*sp));
1426 sp->stream.data_type = Z_BINARY;
1427 sp->stream.zalloc = PixarLogAlloc;
1428 sp->stream.zfree = PixarLogFree;
1429 sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1430
1431 /*
1432 * Install codec methods.
1433 */
1434 tif->tif_setupdecode = PixarLogSetupDecode;
1435 tif->tif_predecode = PixarLogPreDecode;
1436 tif->tif_decoderow = PixarLogDecode;
1437 tif->tif_decodestrip = PixarLogDecode;
1438 tif->tif_decodetile = PixarLogDecode;
1439 tif->tif_setupencode = PixarLogSetupEncode;
1440 tif->tif_preencode = PixarLogPreEncode;
1441 tif->tif_postencode = PixarLogPostEncode;
1442 tif->tif_encoderow = PixarLogEncode;
1443 tif->tif_encodestrip = PixarLogEncode;
1444 tif->tif_encodetile = PixarLogEncode;
1445 tif->tif_close = PixarLogClose;
1446 tif->tif_cleanup = PixarLogCleanup;
1447
1448 /* Override SetField so we can handle our private pseudo-tag */
1449 ImgTIFFMergeFieldInfo(tif, pixarlogFieldInfo, N(pixarlogFieldInfo));
1450 sp->vgetparent = tif->tif_vgetfield;
1451 tif->tif_vgetfield = PixarLogVGetField; /* hook for codec tags */
1452 sp->vsetparent = tif->tif_vsetfield;
1453 tif->tif_vsetfield = PixarLogVSetField; /* hook for codec tags */
1454
1455 /* Default values for codec-specific fields */
1456 sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1457 sp->state = 0;
1458
1459 /* we don't wish to use the predictor,
1460 * the default is none, which predictor value 1
1461 */
1462 (void) ImgTIFFPredictorInit(tif);
1463
1464 /*
1465 * build the companding tables
1466 */
1467 PixarLogMakeTables(sp);
1468
1469 return (1);
1470 bad:
1471 ImgTIFFError("TIFFInitPixarLog", "No space for PixarLog state block");
1472 return (0);
1473 }