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