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