[LIBTIFF]
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_pixarlog.c
1 /* $Id: tif_pixarlog.c,v 1.53 2017-05-17 09:53:06 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 #define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
641 #define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
642
643 static tmsize_t
644 multiply_ms(tmsize_t m1, tmsize_t m2)
645 {
646 if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
647 return 0;
648 return m1 * m2;
649 }
650
651 static tmsize_t
652 add_ms(tmsize_t m1, tmsize_t m2)
653 {
654 /* if either input is zero, assume overflow already occurred */
655 if (m1 == 0 || m2 == 0)
656 return 0;
657 else if (m1 > TIFF_TMSIZE_T_MAX - m2)
658 return 0;
659
660 return m1 + m2;
661 }
662
663 static int
664 PixarLogFixupTags(TIFF* tif)
665 {
666 (void) tif;
667 return (1);
668 }
669
670 static int
671 PixarLogSetupDecode(TIFF* tif)
672 {
673 static const char module[] = "PixarLogSetupDecode";
674 TIFFDirectory *td = &tif->tif_dir;
675 PixarLogState* sp = DecoderState(tif);
676 tmsize_t tbuf_size;
677
678 assert(sp != NULL);
679
680 /* This function can possibly be called several times by */
681 /* PredictorSetupDecode() if this function succeeds but */
682 /* PredictorSetup() fails */
683 if( (sp->state & PLSTATE_INIT) != 0 )
684 return 1;
685
686 /* Make sure no byte swapping happens on the data
687 * after decompression. */
688 tif->tif_postdecode = _TIFFNoPostDecode;
689
690 /* for some reason, we can't do this in TIFFInitPixarLog */
691
692 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
693 td->td_samplesperpixel : 1);
694 tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
695 td->td_rowsperstrip), sizeof(uint16));
696 /* add one more stride in case input ends mid-stride */
697 tbuf_size = add_ms(tbuf_size, sizeof(uint16) * sp->stride);
698 if (tbuf_size == 0)
699 return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
700 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
701 if (sp->tbuf == NULL)
702 return (0);
703 sp->tbuf_size = tbuf_size;
704 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
705 sp->user_datafmt = PixarLogGuessDataFmt(td);
706 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
707 _TIFFfree(sp->tbuf);
708 sp->tbuf = NULL;
709 sp->tbuf_size = 0;
710 TIFFErrorExt(tif->tif_clientdata, module,
711 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
712 td->td_bitspersample);
713 return (0);
714 }
715
716 if (inflateInit(&sp->stream) != Z_OK) {
717 _TIFFfree(sp->tbuf);
718 sp->tbuf = NULL;
719 sp->tbuf_size = 0;
720 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg ? sp->stream.msg : "(null)");
721 return (0);
722 } else {
723 sp->state |= PLSTATE_INIT;
724 return (1);
725 }
726 }
727
728 /*
729 * Setup state for decoding a strip.
730 */
731 static int
732 PixarLogPreDecode(TIFF* tif, uint16 s)
733 {
734 static const char module[] = "PixarLogPreDecode";
735 PixarLogState* sp = DecoderState(tif);
736
737 (void) s;
738 assert(sp != NULL);
739 sp->stream.next_in = tif->tif_rawdata;
740 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
741 we need to simplify this code to reflect a ZLib that is likely updated
742 to deal with 8byte memory sizes, though this code will respond
743 appropriately even before we simplify it */
744 sp->stream.avail_in = (uInt) tif->tif_rawcc;
745 if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
746 {
747 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
748 return (0);
749 }
750 return (inflateReset(&sp->stream) == Z_OK);
751 }
752
753 static int
754 PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
755 {
756 static const char module[] = "PixarLogDecode";
757 TIFFDirectory *td = &tif->tif_dir;
758 PixarLogState* sp = DecoderState(tif);
759 tmsize_t i;
760 tmsize_t nsamples;
761 int llen;
762 uint16 *up;
763
764 switch (sp->user_datafmt) {
765 case PIXARLOGDATAFMT_FLOAT:
766 nsamples = occ / sizeof(float); /* XXX float == 32 bits */
767 break;
768 case PIXARLOGDATAFMT_16BIT:
769 case PIXARLOGDATAFMT_12BITPICIO:
770 case PIXARLOGDATAFMT_11BITLOG:
771 nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
772 break;
773 case PIXARLOGDATAFMT_8BIT:
774 case PIXARLOGDATAFMT_8BITABGR:
775 nsamples = occ;
776 break;
777 default:
778 TIFFErrorExt(tif->tif_clientdata, module,
779 "%d bit input not supported in PixarLog",
780 td->td_bitspersample);
781 return 0;
782 }
783
784 llen = sp->stride * td->td_imagewidth;
785
786 (void) s;
787 assert(sp != NULL);
788
789 sp->stream.next_in = tif->tif_rawcp;
790 sp->stream.avail_in = (uInt) tif->tif_rawcc;
791
792 sp->stream.next_out = (unsigned char *) sp->tbuf;
793 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
794 we need to simplify this code to reflect a ZLib that is likely updated
795 to deal with 8byte memory sizes, though this code will respond
796 appropriately even before we simplify it */
797 sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
798 if (sp->stream.avail_out != nsamples * sizeof(uint16))
799 {
800 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
801 return (0);
802 }
803 /* Check that we will not fill more than what was allocated */
804 if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
805 {
806 TIFFErrorExt(tif->tif_clientdata, module, "sp->stream.avail_out > sp->tbuf_size");
807 return (0);
808 }
809 do {
810 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
811 if (state == Z_STREAM_END) {
812 break; /* XXX */
813 }
814 if (state == Z_DATA_ERROR) {
815 TIFFErrorExt(tif->tif_clientdata, module,
816 "Decoding error at scanline %lu, %s",
817 (unsigned long) tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)");
818 if (inflateSync(&sp->stream) != Z_OK)
819 return (0);
820 continue;
821 }
822 if (state != Z_OK) {
823 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
824 sp->stream.msg ? sp->stream.msg : "(null)");
825 return (0);
826 }
827 } while (sp->stream.avail_out > 0);
828
829 /* hopefully, we got all the bytes we needed */
830 if (sp->stream.avail_out != 0) {
831 TIFFErrorExt(tif->tif_clientdata, module,
832 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
833 (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
834 return (0);
835 }
836
837 tif->tif_rawcp = sp->stream.next_in;
838 tif->tif_rawcc = sp->stream.avail_in;
839
840 up = sp->tbuf;
841 /* Swap bytes in the data if from a different endian machine. */
842 if (tif->tif_flags & TIFF_SWAB)
843 TIFFSwabArrayOfShort(up, nsamples);
844
845 /*
846 * if llen is not an exact multiple of nsamples, the decode operation
847 * may overflow the output buffer, so truncate it enough to prevent
848 * that but still salvage as much data as possible.
849 */
850 if (nsamples % llen) {
851 TIFFWarningExt(tif->tif_clientdata, module,
852 "stride %lu is not a multiple of sample count, "
853 "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
854 nsamples -= nsamples % llen;
855 }
856
857 for (i = 0; i < nsamples; i += llen, up += llen) {
858 switch (sp->user_datafmt) {
859 case PIXARLOGDATAFMT_FLOAT:
860 horizontalAccumulateF(up, llen, sp->stride,
861 (float *)op, sp->ToLinearF);
862 op += llen * sizeof(float);
863 break;
864 case PIXARLOGDATAFMT_16BIT:
865 horizontalAccumulate16(up, llen, sp->stride,
866 (uint16 *)op, sp->ToLinear16);
867 op += llen * sizeof(uint16);
868 break;
869 case PIXARLOGDATAFMT_12BITPICIO:
870 horizontalAccumulate12(up, llen, sp->stride,
871 (int16 *)op, sp->ToLinearF);
872 op += llen * sizeof(int16);
873 break;
874 case PIXARLOGDATAFMT_11BITLOG:
875 horizontalAccumulate11(up, llen, sp->stride,
876 (uint16 *)op);
877 op += llen * sizeof(uint16);
878 break;
879 case PIXARLOGDATAFMT_8BIT:
880 horizontalAccumulate8(up, llen, sp->stride,
881 (unsigned char *)op, sp->ToLinear8);
882 op += llen * sizeof(unsigned char);
883 break;
884 case PIXARLOGDATAFMT_8BITABGR:
885 horizontalAccumulate8abgr(up, llen, sp->stride,
886 (unsigned char *)op, sp->ToLinear8);
887 op += llen * sizeof(unsigned char);
888 break;
889 default:
890 TIFFErrorExt(tif->tif_clientdata, module,
891 "Unsupported bits/sample: %d",
892 td->td_bitspersample);
893 return (0);
894 }
895 }
896
897 return (1);
898 }
899
900 static int
901 PixarLogSetupEncode(TIFF* tif)
902 {
903 static const char module[] = "PixarLogSetupEncode";
904 TIFFDirectory *td = &tif->tif_dir;
905 PixarLogState* sp = EncoderState(tif);
906 tmsize_t tbuf_size;
907
908 assert(sp != NULL);
909
910 /* for some reason, we can't do this in TIFFInitPixarLog */
911
912 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
913 td->td_samplesperpixel : 1);
914 tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
915 td->td_rowsperstrip), sizeof(uint16));
916 if (tbuf_size == 0)
917 return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
918 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
919 if (sp->tbuf == NULL)
920 return (0);
921 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
922 sp->user_datafmt = PixarLogGuessDataFmt(td);
923 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
924 TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
925 return (0);
926 }
927
928 if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
929 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg ? sp->stream.msg : "(null)");
930 return (0);
931 } else {
932 sp->state |= PLSTATE_INIT;
933 return (1);
934 }
935 }
936
937 /*
938 * Reset encoding state at the start of a strip.
939 */
940 static int
941 PixarLogPreEncode(TIFF* tif, uint16 s)
942 {
943 static const char module[] = "PixarLogPreEncode";
944 PixarLogState *sp = EncoderState(tif);
945
946 (void) s;
947 assert(sp != NULL);
948 sp->stream.next_out = tif->tif_rawdata;
949 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
950 we need to simplify this code to reflect a ZLib that is likely updated
951 to deal with 8byte memory sizes, though this code will respond
952 appropriately even before we simplify it */
953 sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
954 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
955 {
956 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
957 return (0);
958 }
959 return (deflateReset(&sp->stream) == Z_OK);
960 }
961
962 static void
963 horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
964 {
965 int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
966 float fltsize = Fltsize;
967
968 #define CLAMP(v) ( (v<(float)0.) ? 0 \
969 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
970 : (v>(float)24.2) ? 2047 \
971 : LogK1*log(v*LogK2) + 0.5 )
972
973 mask = CODE_MASK;
974 if (n >= stride) {
975 if (stride == 3) {
976 r2 = wp[0] = (uint16) CLAMP(ip[0]);
977 g2 = wp[1] = (uint16) CLAMP(ip[1]);
978 b2 = wp[2] = (uint16) CLAMP(ip[2]);
979 n -= 3;
980 while (n > 0) {
981 n -= 3;
982 wp += 3;
983 ip += 3;
984 r1 = (int32) CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
985 g1 = (int32) CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
986 b1 = (int32) CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
987 }
988 } else if (stride == 4) {
989 r2 = wp[0] = (uint16) CLAMP(ip[0]);
990 g2 = wp[1] = (uint16) CLAMP(ip[1]);
991 b2 = wp[2] = (uint16) CLAMP(ip[2]);
992 a2 = wp[3] = (uint16) CLAMP(ip[3]);
993 n -= 4;
994 while (n > 0) {
995 n -= 4;
996 wp += 4;
997 ip += 4;
998 r1 = (int32) CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
999 g1 = (int32) CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
1000 b1 = (int32) CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
1001 a1 = (int32) CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
1002 }
1003 } else {
1004 REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp++; ip++)
1005 n -= stride;
1006 while (n > 0) {
1007 REPEAT(stride,
1008 wp[0] = (uint16)(((int32)CLAMP(ip[0])-(int32)CLAMP(ip[-stride])) & mask);
1009 wp++; ip++)
1010 n -= stride;
1011 }
1012 }
1013 }
1014 }
1015
1016 static void
1017 horizontalDifference16(unsigned short *ip, int n, int stride,
1018 unsigned short *wp, uint16 *From14)
1019 {
1020 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1021
1022 /* assumption is unsigned pixel values */
1023 #undef CLAMP
1024 #define CLAMP(v) From14[(v) >> 2]
1025
1026 mask = CODE_MASK;
1027 if (n >= stride) {
1028 if (stride == 3) {
1029 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1030 b2 = wp[2] = CLAMP(ip[2]);
1031 n -= 3;
1032 while (n > 0) {
1033 n -= 3;
1034 wp += 3;
1035 ip += 3;
1036 r1 = CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
1037 g1 = CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
1038 b1 = CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
1039 }
1040 } else if (stride == 4) {
1041 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1042 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1043 n -= 4;
1044 while (n > 0) {
1045 n -= 4;
1046 wp += 4;
1047 ip += 4;
1048 r1 = CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
1049 g1 = CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
1050 b1 = CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
1051 a1 = CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
1052 }
1053 } else {
1054 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1055 n -= stride;
1056 while (n > 0) {
1057 REPEAT(stride,
1058 wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
1059 wp++; ip++)
1060 n -= stride;
1061 }
1062 }
1063 }
1064 }
1065
1066
1067 static void
1068 horizontalDifference8(unsigned char *ip, int n, int stride,
1069 unsigned short *wp, uint16 *From8)
1070 {
1071 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1072
1073 #undef CLAMP
1074 #define CLAMP(v) (From8[(v)])
1075
1076 mask = CODE_MASK;
1077 if (n >= stride) {
1078 if (stride == 3) {
1079 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1080 b2 = wp[2] = CLAMP(ip[2]);
1081 n -= 3;
1082 while (n > 0) {
1083 n -= 3;
1084 r1 = CLAMP(ip[3]); wp[3] = (uint16)((r1-r2) & mask); r2 = r1;
1085 g1 = CLAMP(ip[4]); wp[4] = (uint16)((g1-g2) & mask); g2 = g1;
1086 b1 = CLAMP(ip[5]); wp[5] = (uint16)((b1-b2) & mask); b2 = b1;
1087 wp += 3;
1088 ip += 3;
1089 }
1090 } else if (stride == 4) {
1091 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1092 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1093 n -= 4;
1094 while (n > 0) {
1095 n -= 4;
1096 r1 = CLAMP(ip[4]); wp[4] = (uint16)((r1-r2) & mask); r2 = r1;
1097 g1 = CLAMP(ip[5]); wp[5] = (uint16)((g1-g2) & mask); g2 = g1;
1098 b1 = CLAMP(ip[6]); wp[6] = (uint16)((b1-b2) & mask); b2 = b1;
1099 a1 = CLAMP(ip[7]); wp[7] = (uint16)((a1-a2) & mask); a2 = a1;
1100 wp += 4;
1101 ip += 4;
1102 }
1103 } else {
1104 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1105 n -= stride;
1106 while (n > 0) {
1107 REPEAT(stride,
1108 wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
1109 wp++; ip++)
1110 n -= stride;
1111 }
1112 }
1113 }
1114 }
1115
1116 /*
1117 * Encode a chunk of pixels.
1118 */
1119 static int
1120 PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1121 {
1122 static const char module[] = "PixarLogEncode";
1123 TIFFDirectory *td = &tif->tif_dir;
1124 PixarLogState *sp = EncoderState(tif);
1125 tmsize_t i;
1126 tmsize_t n;
1127 int llen;
1128 unsigned short * up;
1129
1130 (void) s;
1131
1132 switch (sp->user_datafmt) {
1133 case PIXARLOGDATAFMT_FLOAT:
1134 n = cc / sizeof(float); /* XXX float == 32 bits */
1135 break;
1136 case PIXARLOGDATAFMT_16BIT:
1137 case PIXARLOGDATAFMT_12BITPICIO:
1138 case PIXARLOGDATAFMT_11BITLOG:
1139 n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
1140 break;
1141 case PIXARLOGDATAFMT_8BIT:
1142 case PIXARLOGDATAFMT_8BITABGR:
1143 n = cc;
1144 break;
1145 default:
1146 TIFFErrorExt(tif->tif_clientdata, module,
1147 "%d bit input not supported in PixarLog",
1148 td->td_bitspersample);
1149 return 0;
1150 }
1151
1152 llen = sp->stride * td->td_imagewidth;
1153 /* Check against the number of elements (of size uint16) of sp->tbuf */
1154 if( n > (tmsize_t)(td->td_rowsperstrip * llen) )
1155 {
1156 TIFFErrorExt(tif->tif_clientdata, module,
1157 "Too many input bytes provided");
1158 return 0;
1159 }
1160
1161 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
1162 switch (sp->user_datafmt) {
1163 case PIXARLOGDATAFMT_FLOAT:
1164 horizontalDifferenceF((float *)bp, llen,
1165 sp->stride, up, sp->FromLT2);
1166 bp += llen * sizeof(float);
1167 break;
1168 case PIXARLOGDATAFMT_16BIT:
1169 horizontalDifference16((uint16 *)bp, llen,
1170 sp->stride, up, sp->From14);
1171 bp += llen * sizeof(uint16);
1172 break;
1173 case PIXARLOGDATAFMT_8BIT:
1174 horizontalDifference8((unsigned char *)bp, llen,
1175 sp->stride, up, sp->From8);
1176 bp += llen * sizeof(unsigned char);
1177 break;
1178 default:
1179 TIFFErrorExt(tif->tif_clientdata, module,
1180 "%d bit input not supported in PixarLog",
1181 td->td_bitspersample);
1182 return 0;
1183 }
1184 }
1185
1186 sp->stream.next_in = (unsigned char *) sp->tbuf;
1187 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
1188 we need to simplify this code to reflect a ZLib that is likely updated
1189 to deal with 8byte memory sizes, though this code will respond
1190 appropriately even before we simplify it */
1191 sp->stream.avail_in = (uInt) (n * sizeof(uint16));
1192 if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
1193 {
1194 TIFFErrorExt(tif->tif_clientdata, module,
1195 "ZLib cannot deal with buffers this size");
1196 return (0);
1197 }
1198
1199 do {
1200 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
1201 TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
1202 sp->stream.msg ? sp->stream.msg : "(null)");
1203 return (0);
1204 }
1205 if (sp->stream.avail_out == 0) {
1206 tif->tif_rawcc = tif->tif_rawdatasize;
1207 TIFFFlushData1(tif);
1208 sp->stream.next_out = tif->tif_rawdata;
1209 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
1210 }
1211 } while (sp->stream.avail_in > 0);
1212 return (1);
1213 }
1214
1215 /*
1216 * Finish off an encoded strip by flushing the last
1217 * string and tacking on an End Of Information code.
1218 */
1219
1220 static int
1221 PixarLogPostEncode(TIFF* tif)
1222 {
1223 static const char module[] = "PixarLogPostEncode";
1224 PixarLogState *sp = EncoderState(tif);
1225 int state;
1226
1227 sp->stream.avail_in = 0;
1228
1229 do {
1230 state = deflate(&sp->stream, Z_FINISH);
1231 switch (state) {
1232 case Z_STREAM_END:
1233 case Z_OK:
1234 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
1235 tif->tif_rawcc =
1236 tif->tif_rawdatasize - sp->stream.avail_out;
1237 TIFFFlushData1(tif);
1238 sp->stream.next_out = tif->tif_rawdata;
1239 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
1240 }
1241 break;
1242 default:
1243 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
1244 sp->stream.msg ? sp->stream.msg : "(null)");
1245 return (0);
1246 }
1247 } while (state != Z_STREAM_END);
1248 return (1);
1249 }
1250
1251 static void
1252 PixarLogClose(TIFF* tif)
1253 {
1254 PixarLogState* sp = (PixarLogState*) tif->tif_data;
1255 TIFFDirectory *td = &tif->tif_dir;
1256
1257 assert(sp != 0);
1258 /* In a really sneaky (and really incorrect, and untruthful, and
1259 * troublesome, and error-prone) maneuver that completely goes against
1260 * the spirit of TIFF, and breaks TIFF, on close, we covertly
1261 * modify both bitspersample and sampleformat in the directory to
1262 * indicate 8-bit linear. This way, the decode "just works" even for
1263 * readers that don't know about PixarLog, or how to set
1264 * the PIXARLOGDATFMT pseudo-tag.
1265 */
1266
1267 if (sp->state&PLSTATE_INIT) {
1268 /* We test the state to avoid an issue such as in
1269 * http://bugzilla.maptools.org/show_bug.cgi?id=2604
1270 * What appends in that case is that the bitspersample is 1 and
1271 * a TransferFunction is set. The size of the TransferFunction
1272 * depends on 1<<bitspersample. So if we increase it, an access
1273 * out of the buffer will happen at directory flushing.
1274 * Another option would be to clear those targs.
1275 */
1276 td->td_bitspersample = 8;
1277 td->td_sampleformat = SAMPLEFORMAT_UINT;
1278 }
1279 }
1280
1281 static void
1282 PixarLogCleanup(TIFF* tif)
1283 {
1284 PixarLogState* sp = (PixarLogState*) tif->tif_data;
1285
1286 assert(sp != 0);
1287
1288 (void)TIFFPredictorCleanup(tif);
1289
1290 tif->tif_tagmethods.vgetfield = sp->vgetparent;
1291 tif->tif_tagmethods.vsetfield = sp->vsetparent;
1292
1293 if (sp->FromLT2) _TIFFfree(sp->FromLT2);
1294 if (sp->From14) _TIFFfree(sp->From14);
1295 if (sp->From8) _TIFFfree(sp->From8);
1296 if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
1297 if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
1298 if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
1299 if (sp->state&PLSTATE_INIT) {
1300 if (tif->tif_mode == O_RDONLY)
1301 inflateEnd(&sp->stream);
1302 else
1303 deflateEnd(&sp->stream);
1304 }
1305 if (sp->tbuf)
1306 _TIFFfree(sp->tbuf);
1307 _TIFFfree(sp);
1308 tif->tif_data = NULL;
1309
1310 _TIFFSetDefaultCompressionState(tif);
1311 }
1312
1313 static int
1314 PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
1315 {
1316 static const char module[] = "PixarLogVSetField";
1317 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1318 int result;
1319
1320 switch (tag) {
1321 case TIFFTAG_PIXARLOGQUALITY:
1322 sp->quality = (int) va_arg(ap, int);
1323 if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
1324 if (deflateParams(&sp->stream,
1325 sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
1326 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
1327 sp->stream.msg ? sp->stream.msg : "(null)");
1328 return (0);
1329 }
1330 }
1331 return (1);
1332 case TIFFTAG_PIXARLOGDATAFMT:
1333 sp->user_datafmt = (int) va_arg(ap, int);
1334 /* Tweak the TIFF header so that the rest of libtiff knows what
1335 * size of data will be passed between app and library, and
1336 * assume that the app knows what it is doing and is not
1337 * confused by these header manipulations...
1338 */
1339 switch (sp->user_datafmt) {
1340 case PIXARLOGDATAFMT_8BIT:
1341 case PIXARLOGDATAFMT_8BITABGR:
1342 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1343 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1344 break;
1345 case PIXARLOGDATAFMT_11BITLOG:
1346 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1347 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1348 break;
1349 case PIXARLOGDATAFMT_12BITPICIO:
1350 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1351 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1352 break;
1353 case PIXARLOGDATAFMT_16BIT:
1354 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1355 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1356 break;
1357 case PIXARLOGDATAFMT_FLOAT:
1358 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1359 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
1360 break;
1361 }
1362 /*
1363 * Must recalculate sizes should bits/sample change.
1364 */
1365 tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
1366 tif->tif_scanlinesize = TIFFScanlineSize(tif);
1367 result = 1; /* NB: pseudo tag */
1368 break;
1369 default:
1370 result = (*sp->vsetparent)(tif, tag, ap);
1371 }
1372 return (result);
1373 }
1374
1375 static int
1376 PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
1377 {
1378 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1379
1380 switch (tag) {
1381 case TIFFTAG_PIXARLOGQUALITY:
1382 *va_arg(ap, int*) = sp->quality;
1383 break;
1384 case TIFFTAG_PIXARLOGDATAFMT:
1385 *va_arg(ap, int*) = sp->user_datafmt;
1386 break;
1387 default:
1388 return (*sp->vgetparent)(tif, tag, ap);
1389 }
1390 return (1);
1391 }
1392
1393 static const TIFFField pixarlogFields[] = {
1394 {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
1395 {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
1396 };
1397
1398 int
1399 TIFFInitPixarLog(TIFF* tif, int scheme)
1400 {
1401 static const char module[] = "TIFFInitPixarLog";
1402
1403 PixarLogState* sp;
1404
1405 assert(scheme == COMPRESSION_PIXARLOG);
1406
1407 /*
1408 * Merge codec-specific tag information.
1409 */
1410 if (!_TIFFMergeFields(tif, pixarlogFields,
1411 TIFFArrayCount(pixarlogFields))) {
1412 TIFFErrorExt(tif->tif_clientdata, module,
1413 "Merging PixarLog codec-specific tags failed");
1414 return 0;
1415 }
1416
1417 /*
1418 * Allocate state block so tag methods have storage to record values.
1419 */
1420 tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
1421 if (tif->tif_data == NULL)
1422 goto bad;
1423 sp = (PixarLogState*) tif->tif_data;
1424 _TIFFmemset(sp, 0, sizeof (*sp));
1425 sp->stream.data_type = Z_BINARY;
1426 sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1427
1428 /*
1429 * Install codec methods.
1430 */
1431 tif->tif_fixuptags = PixarLogFixupTags;
1432 tif->tif_setupdecode = PixarLogSetupDecode;
1433 tif->tif_predecode = PixarLogPreDecode;
1434 tif->tif_decoderow = PixarLogDecode;
1435 tif->tif_decodestrip = PixarLogDecode;
1436 tif->tif_decodetile = PixarLogDecode;
1437 tif->tif_setupencode = PixarLogSetupEncode;
1438 tif->tif_preencode = PixarLogPreEncode;
1439 tif->tif_postencode = PixarLogPostEncode;
1440 tif->tif_encoderow = PixarLogEncode;
1441 tif->tif_encodestrip = PixarLogEncode;
1442 tif->tif_encodetile = PixarLogEncode;
1443 tif->tif_close = PixarLogClose;
1444 tif->tif_cleanup = PixarLogCleanup;
1445
1446 /* Override SetField so we can handle our private pseudo-tag */
1447 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1448 tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
1449 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1450 tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
1451
1452 /* Default values for codec-specific fields */
1453 sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1454 sp->state = 0;
1455
1456 /* we don't wish to use the predictor,
1457 * the default is none, which predictor value 1
1458 */
1459 (void) TIFFPredictorInit(tif);
1460
1461 /*
1462 * build the companding tables
1463 */
1464 PixarLogMakeTables(sp);
1465
1466 return (1);
1467 bad:
1468 TIFFErrorExt(tif->tif_clientdata, module,
1469 "No space for PixarLog state block");
1470 return (0);
1471 }
1472 #endif /* PIXARLOG_SUPPORT */
1473
1474 /* vim: set ts=8 sts=8 sw=8 noet: */
1475 /*
1476 * Local Variables:
1477 * mode: c
1478 * c-basic-offset: 8
1479 * fill-column: 78
1480 * End:
1481 */