source: liacs/MIR2010/SourceCode/cximage/tiff/tif_pixarlog.c@ 107

Last change on this file since 107 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

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