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