LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_pixarlog.c
Go to the documentation of this file.
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
30#include "tiffiop.h"
31#ifdef PIXARLOG_SUPPORT
32
33/*
34 * TIFF Library.
35 * PixarLog Compression Support
36 *
37 * Contributed by Dan McCoy.
38 *
39 * PixarLog film support uses the TIFF library to store companded
40 * 11 bit values into a tiff file, which are compressed using the
41 * zip compressor.
42 *
43 * The codec can take as input and produce as output 32-bit IEEE float values
44 * as well as 16-bit or 8-bit unsigned integer values.
45 *
46 * On writing any of the above are converted into the internal
47 * 11-bit log format. In the case of 8 and 16 bit values, the
48 * input is assumed to be unsigned linear color values that represent
49 * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
50 * be the normal linear color range, in addition over 1 values are
51 * accepted up to a value of about 25.0 to encode "hot" highlights and such.
52 * The encoding is lossless for 8-bit values, slightly lossy for the
53 * other bit depths. The actual color precision should be better
54 * than the human eye can perceive with extra room to allow for
55 * error introduced by further image computation. As with any quantized
56 * color format, it is possible to perform image calculations which
57 * expose the quantization error. This format should certainly be less
58 * susceptible to such errors than standard 8-bit encodings, but more
59 * susceptible than straight 16-bit or 32-bit encodings.
60 *
61 * On reading the internal format is converted to the desired output format.
62 * The program can request which format it desires by setting the internal
63 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
64 * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
65 * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
66 * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
67 *
68 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
69 * values with the difference that if there are exactly three or four channels
70 * (rgb or rgba) it swaps the channel order (bgr or abgr).
71 *
72 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
73 * packed in 16-bit values. However no tools are supplied for interpreting
74 * these values.
75 *
76 * "hot" (over 1.0) areas written in floating point get clamped to
77 * 1.0 in the integer data types.
78 *
79 * When the file is closed after writing, the bit depth and sample format
80 * are set always to appear as if 8-bit data has been written into it.
81 * That way a naive program unaware of the particulars of the encoding
82 * gets the format it is most likely able to handle.
83 *
84 * The codec does it's own horizontal differencing step on the coded
85 * values so the libraries predictor stuff should be turned off.
86 * The codec also handle byte swapping the encoded values as necessary
87 * since the library does not have the information necessary
88 * to know the bit depth of the raw unencoded buffer.
89 *
90 * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
91 * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
92 * as noted in http://trac.osgeo.org/gdal/ticket/3894. FrankW - Jan'11
93 */
94
95#include "tif_predict.h"
96#include "zlib.h"
97
98#include <math.h>
99#include <stdio.h>
100#include <stdlib.h>
101
102/* Tables for converting to/from 11 bit coded values */
103
104#define TSIZE 2048 /* decode table size (11-bit tokens) */
105#define TSIZEP1 2049 /* Plus one for slop */
106#define ONE 1250 /* token value of 1.0 exactly */
107#define RATIO 1.004 /* nominal ratio for log part */
108
109#define CODE_MASK 0x7ff /* 11 bits. */
110
120static float Fltsize;
130static float LogK1;
138static float LogK2;
139
140#define REPEAT(n, op) \
141 { \
142 int i; \
143 i = n; \
144 do \
145 { \
146 i--; \
147 op; \
148 } while (i > 0); \
149 }
150
151/*
152 * PIXARLOGDATAFMT_* buffers are application-facing user data buffers in
153 * native byte order. Use fixed-size memcpy() calls directly so optimizing
154 * compilers can expand them in these per-sample paths while callers remain
155 * free to provide unaligned public buffers. _TIFFmemcpy() is an out-of-line
156 * wrapper in non-LTO builds.
157 */
169static float PixarLogLoadFloatNativeUnaligned(const uint8_t *cp)
170{
171 float v;
172 memcpy(&v, cp, sizeof(v));
173 return v;
174}
175
187static void PixarLogStoreFloatNativeUnaligned(uint8_t *cp, float v)
188{
189 memcpy(cp, &v, sizeof(v));
190}
191
203static uint16_t PixarLogLoad16NativeUnaligned(const uint8_t *cp)
204{
205 uint16_t v;
206 memcpy(&v, cp, sizeof(v));
207 return v;
208}
209
221static void PixarLogStore16NativeUnaligned(uint8_t *cp, uint16_t v)
222{
223 memcpy(cp, &v, sizeof(v));
224}
225
248static void horizontalAccumulateF(uint16_t *wp, tmsize_t n, int stride,
249 uint8_t *op, float *ToLinearF)
250{
251 unsigned int cr, cg, cb, ca, mask;
252 float t0, t1, t2, t3;
253
254 if (n >= stride)
255 {
256 mask = CODE_MASK;
257 if (stride == 3)
258 {
259 t0 = ToLinearF[cr = (wp[0] & mask)];
260 t1 = ToLinearF[cg = (wp[1] & mask)];
261 t2 = ToLinearF[cb = (wp[2] & mask)];
263 PixarLogStoreFloatNativeUnaligned(op + sizeof(float), t1);
264 PixarLogStoreFloatNativeUnaligned(op + 2 * sizeof(float), t2);
265 n -= 3;
266 while (n > 0)
267 {
268 wp += 3;
269 op += 3 * sizeof(float);
270 n -= 3;
271 t0 = ToLinearF[(cr += wp[0]) & mask];
272 t1 = ToLinearF[(cg += wp[1]) & mask];
273 t2 = ToLinearF[(cb += wp[2]) & mask];
275 PixarLogStoreFloatNativeUnaligned(op + sizeof(float), t1);
276 PixarLogStoreFloatNativeUnaligned(op + 2 * sizeof(float), t2);
277 }
278 }
279 else if (stride == 4)
280 {
281 t0 = ToLinearF[cr = (wp[0] & mask)];
282 t1 = ToLinearF[cg = (wp[1] & mask)];
283 t2 = ToLinearF[cb = (wp[2] & mask)];
284 t3 = ToLinearF[ca = (wp[3] & mask)];
286 PixarLogStoreFloatNativeUnaligned(op + sizeof(float), t1);
287 PixarLogStoreFloatNativeUnaligned(op + 2 * sizeof(float), t2);
288 PixarLogStoreFloatNativeUnaligned(op + 3 * sizeof(float), t3);
289 n -= 4;
290 while (n > 0)
291 {
292 wp += 4;
293 op += 4 * sizeof(float);
294 n -= 4;
295 t0 = ToLinearF[(cr += wp[0]) & mask];
296 t1 = ToLinearF[(cg += wp[1]) & mask];
297 t2 = ToLinearF[(cb += wp[2]) & mask];
298 t3 = ToLinearF[(ca += wp[3]) & mask];
300 PixarLogStoreFloatNativeUnaligned(op + sizeof(float), t1);
301 PixarLogStoreFloatNativeUnaligned(op + 2 * sizeof(float), t2);
302 PixarLogStoreFloatNativeUnaligned(op + 3 * sizeof(float), t3);
303 }
304 }
305 else
306 {
307 REPEAT(stride,
308 PixarLogStoreFloatNativeUnaligned(op, ToLinearF[*wp & mask]);
309 wp++; op += sizeof(float))
310 n -= stride;
311 while (n > 0)
312 {
313 REPEAT(stride, *wp = (uint16_t)(*wp + wp[-stride]);
315 ToLinearF[*wp & mask]);
316 wp++; op += sizeof(float))
317 n -= stride;
318 }
319 }
320 }
321}
322
346static void horizontalAccumulate12(uint16_t *wp, tmsize_t n, int stride,
347 uint8_t *op, float *ToLinearF)
348{
349 unsigned int cr, cg, cb, ca, mask;
350 float t0, t1, t2, t3;
351
352#define SCALE12 2048.0f
353#define CLAMP12(t) (((t) < 3071) ? (int16_t)(uint16_t)(t) : (int16_t)3071)
354
355 if (n >= stride)
356 {
357 mask = CODE_MASK;
358 if (stride == 3)
359 {
360 t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
361 t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
362 t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
363 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
364 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
365 (uint16_t)CLAMP12(t1));
366 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
367 (uint16_t)CLAMP12(t2));
368 n -= 3;
369 while (n > 0)
370 {
371 wp += 3;
372 op += 3 * sizeof(uint16_t);
373 n -= 3;
374 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
375 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
376 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
377 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
378 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
379 (uint16_t)CLAMP12(t1));
380 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
381 (uint16_t)CLAMP12(t2));
382 }
383 }
384 else if (stride == 4)
385 {
386 t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
387 t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
388 t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
389 t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
390 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
391 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
392 (uint16_t)CLAMP12(t1));
393 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
394 (uint16_t)CLAMP12(t2));
395 PixarLogStore16NativeUnaligned(op + 3 * sizeof(uint16_t),
396 (uint16_t)CLAMP12(t3));
397 n -= 4;
398 while (n > 0)
399 {
400 wp += 4;
401 op += 4 * sizeof(uint16_t);
402 n -= 4;
403 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
404 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
405 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
406 t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
407 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
408 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
409 (uint16_t)CLAMP12(t1));
410 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
411 (uint16_t)CLAMP12(t2));
412 PixarLogStore16NativeUnaligned(op + 3 * sizeof(uint16_t),
413 (uint16_t)CLAMP12(t3));
414 }
415 }
416 else
417 {
418 REPEAT(stride, t0 = ToLinearF[*wp & mask] * SCALE12;
419 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
420 wp++; op += sizeof(uint16_t))
421 n -= stride;
422 while (n > 0)
423 {
424 REPEAT(
425 stride, *wp = (uint16_t)(*wp + wp[-stride]);
426 t0 = ToLinearF[*wp & mask] * SCALE12;
427 PixarLogStore16NativeUnaligned(op, (uint16_t)CLAMP12(t0));
428 wp++; op += sizeof(uint16_t))
429 n -= stride;
430 }
431 }
432 }
433}
434
458static void horizontalAccumulate16(uint16_t *wp, tmsize_t n, int stride,
459 uint8_t *op, uint16_t *ToLinear16)
460{
461 unsigned int cr, cg, cb, ca, mask;
462
463 if (n >= stride)
464 {
465 mask = CODE_MASK;
466 if (stride == 3)
467 {
468 PixarLogStore16NativeUnaligned(op, ToLinear16[cr = (wp[0] & mask)]);
469 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
470 ToLinear16[cg = (wp[1] & mask)]);
471 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
472 ToLinear16[cb = (wp[2] & mask)]);
473 n -= 3;
474 while (n > 0)
475 {
476 wp += 3;
477 op += 3 * sizeof(uint16_t);
478 n -= 3;
480 op, ToLinear16[(cr += wp[0]) & mask]);
482 op + sizeof(uint16_t), ToLinear16[(cg += wp[1]) & mask]);
484 op + 2 * sizeof(uint16_t),
485 ToLinear16[(cb += wp[2]) & mask]);
486 }
487 }
488 else if (stride == 4)
489 {
490 PixarLogStore16NativeUnaligned(op, ToLinear16[cr = (wp[0] & mask)]);
491 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t),
492 ToLinear16[cg = (wp[1] & mask)]);
493 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t),
494 ToLinear16[cb = (wp[2] & mask)]);
495 PixarLogStore16NativeUnaligned(op + 3 * sizeof(uint16_t),
496 ToLinear16[ca = (wp[3] & mask)]);
497 n -= 4;
498 while (n > 0)
499 {
500 wp += 4;
501 op += 4 * sizeof(uint16_t);
502 n -= 4;
504 op, ToLinear16[(cr += wp[0]) & mask]);
506 op + sizeof(uint16_t), ToLinear16[(cg += wp[1]) & mask]);
508 op + 2 * sizeof(uint16_t),
509 ToLinear16[(cb += wp[2]) & mask]);
511 op + 3 * sizeof(uint16_t),
512 ToLinear16[(ca += wp[3]) & mask]);
513 }
514 }
515 else
516 {
517 REPEAT(stride,
518 PixarLogStore16NativeUnaligned(op, ToLinear16[*wp & mask]);
519 wp++; op += sizeof(uint16_t))
520 n -= stride;
521 while (n > 0)
522 {
523 REPEAT(
524 stride, *wp = (uint16_t)(*wp + wp[-stride]);
525 PixarLogStore16NativeUnaligned(op, ToLinear16[*wp & mask]);
526 wp++; op += sizeof(uint16_t))
527 n -= stride;
528 }
529 }
530 }
531}
532
533/*
534 * Returns the log encoded 11-bit values with the horizontal
535 * differencing undone.
536 */
560static void horizontalAccumulate11(uint16_t *wp, tmsize_t n, int stride,
561 uint8_t *op)
562{
563 unsigned int cr, cg, cb, ca, mask;
564
565 if (n >= stride)
566 {
567 mask = CODE_MASK;
568 if (stride == 3)
569 {
571 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t), wp[1]);
572 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t), wp[2]);
573 cr = wp[0];
574 cg = wp[1];
575 cb = wp[2];
576 n -= 3;
577 while (n > 0)
578 {
579 wp += 3;
580 op += 3 * sizeof(uint16_t);
581 n -= 3;
583 op, (uint16_t)((cr += wp[0]) & mask));
585 op + sizeof(uint16_t), (uint16_t)((cg += wp[1]) & mask));
587 op + 2 * sizeof(uint16_t),
588 (uint16_t)((cb += wp[2]) & mask));
589 }
590 }
591 else if (stride == 4)
592 {
594 PixarLogStore16NativeUnaligned(op + sizeof(uint16_t), wp[1]);
595 PixarLogStore16NativeUnaligned(op + 2 * sizeof(uint16_t), wp[2]);
596 PixarLogStore16NativeUnaligned(op + 3 * sizeof(uint16_t), wp[3]);
597 cr = wp[0];
598 cg = wp[1];
599 cb = wp[2];
600 ca = wp[3];
601 n -= 4;
602 while (n > 0)
603 {
604 wp += 4;
605 op += 4 * sizeof(uint16_t);
606 n -= 4;
608 op, (uint16_t)((cr += wp[0]) & mask));
610 op + sizeof(uint16_t), (uint16_t)((cg += wp[1]) & mask));
612 op + 2 * sizeof(uint16_t),
613 (uint16_t)((cb += wp[2]) & mask));
615 op + 3 * sizeof(uint16_t),
616 (uint16_t)((ca += wp[3]) & mask));
617 }
618 }
619 else
620 {
621 REPEAT(stride,
622 PixarLogStore16NativeUnaligned(op, (uint16_t)(*wp & mask));
623 wp++; op += sizeof(uint16_t))
624 n -= stride;
625 while (n > 0)
626 {
627 REPEAT(
628 stride, *wp = (uint16_t)(*wp + wp[-stride]);
629 PixarLogStore16NativeUnaligned(op, (uint16_t)(*wp & mask));
630 wp++; op += sizeof(uint16_t))
631 n -= stride;
632 }
633 }
634 }
635}
636
659static void horizontalAccumulate8(uint16_t *wp, tmsize_t n, int stride,
660 unsigned char *op, unsigned char *ToLinear8)
661{
662 unsigned int cr, cg, cb, ca, mask;
663
664 if (n >= stride)
665 {
666 mask = CODE_MASK;
667 if (stride == 3)
668 {
669 op[0] = ToLinear8[cr = (wp[0] & mask)];
670 op[1] = ToLinear8[cg = (wp[1] & mask)];
671 op[2] = ToLinear8[cb = (wp[2] & mask)];
672 n -= 3;
673 while (n > 0)
674 {
675 n -= 3;
676 wp += 3;
677 op += 3;
678 op[0] = ToLinear8[(cr += wp[0]) & mask];
679 op[1] = ToLinear8[(cg += wp[1]) & mask];
680 op[2] = ToLinear8[(cb += wp[2]) & mask];
681 }
682 }
683 else if (stride == 4)
684 {
685 op[0] = ToLinear8[cr = (wp[0] & mask)];
686 op[1] = ToLinear8[cg = (wp[1] & mask)];
687 op[2] = ToLinear8[cb = (wp[2] & mask)];
688 op[3] = ToLinear8[ca = (wp[3] & mask)];
689 n -= 4;
690 while (n > 0)
691 {
692 n -= 4;
693 wp += 4;
694 op += 4;
695 op[0] = ToLinear8[(cr += wp[0]) & mask];
696 op[1] = ToLinear8[(cg += wp[1]) & mask];
697 op[2] = ToLinear8[(cb += wp[2]) & mask];
698 op[3] = ToLinear8[(ca += wp[3]) & mask];
699 }
700 }
701 else
702 {
703 REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
704 n -= stride;
705 while (n > 0)
706 {
707 REPEAT(stride, *wp = (uint16_t)(*wp + wp[-stride]);
708 *op = ToLinear8[*wp & mask]; wp++; op++)
709 n -= stride;
710 }
711 }
712 }
713}
714
739static void horizontalAccumulate8abgr(uint16_t *wp, tmsize_t n, int stride,
740 unsigned char *op,
741 unsigned char *ToLinear8)
742{
743 unsigned int cr, cg, cb, ca, mask;
744 unsigned char t0, t1, t2, t3;
745
746 if (n >= stride)
747 {
748 mask = CODE_MASK;
749 if (stride == 3)
750 {
751 op[0] = 0;
752 t1 = ToLinear8[cb = (wp[2] & mask)];
753 t2 = ToLinear8[cg = (wp[1] & mask)];
754 t3 = ToLinear8[cr = (wp[0] & mask)];
755 op[1] = t1;
756 op[2] = t2;
757 op[3] = t3;
758 n -= 3;
759 while (n > 0)
760 {
761 n -= 3;
762 wp += 3;
763 op += 4;
764 op[0] = 0;
765 t1 = ToLinear8[(cb += wp[2]) & mask];
766 t2 = ToLinear8[(cg += wp[1]) & mask];
767 t3 = ToLinear8[(cr += wp[0]) & mask];
768 op[1] = t1;
769 op[2] = t2;
770 op[3] = t3;
771 }
772 }
773 else if (stride == 4)
774 {
775 t0 = ToLinear8[ca = (wp[3] & mask)];
776 t1 = ToLinear8[cb = (wp[2] & mask)];
777 t2 = ToLinear8[cg = (wp[1] & mask)];
778 t3 = ToLinear8[cr = (wp[0] & mask)];
779 op[0] = t0;
780 op[1] = t1;
781 op[2] = t2;
782 op[3] = t3;
783 n -= 4;
784 while (n > 0)
785 {
786 n -= 4;
787 wp += 4;
788 op += 4;
789 t0 = ToLinear8[(ca += wp[3]) & mask];
790 t1 = ToLinear8[(cb += wp[2]) & mask];
791 t2 = ToLinear8[(cg += wp[1]) & mask];
792 t3 = ToLinear8[(cr += wp[0]) & mask];
793 op[0] = t0;
794 op[1] = t1;
795 op[2] = t2;
796 op[3] = t3;
797 }
798 }
799 else
800 {
801 REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
802 n -= stride;
803 while (n > 0)
804 {
805 REPEAT(stride, *wp = (uint16_t)(*wp + wp[-stride]);
806 *op = ToLinear8[*wp & mask]; wp++; op++)
807 n -= stride;
808 }
809 }
810 }
811}
812
813/*
814 * State block for each open TIFF
815 * file using PixarLog compression/decompression.
816 */
825typedef struct
826{
830 z_stream stream;
837 uint16_t *tbuf;
839 uint16_t stride;
841 int state;
848#define PLSTATE_INIT 1
849
857
859 float *ToLinearF;
861 uint16_t *ToLinear16;
863 unsigned char *ToLinear8;
865 uint16_t *FromLT2;
867 uint16_t *From14;
869 uint16_t *From8;
870
872
891{
892
893 /*
894 * We make several tables here to convert between various external
895 * representations (float, 16-bit, and 8-bit) and the internal
896 * 11-bit companded representation. The 11-bit representation has two
897 * distinct regions. A linear bottom end up through .018316 in steps
898 * of about .000073, and a region of constant ratio up to about 25.
899 * These floating point numbers are stored in the main table ToLinearF.
900 * All other tables are derived from this one. The tables (and the
901 * ratios) are continuous at the internal seam.
902 */
903
904 int nlin, lt2size;
905 int i, j;
906 double b, c, linstep, v;
907 float *ToLinearF;
908 uint16_t *ToLinear16;
909 unsigned char *ToLinear8;
910 uint16_t *FromLT2;
911 uint16_t *From14; /* Really for 16-bit data, but we shift down 2 */
912 uint16_t *From8;
913
914 c = log(RATIO);
915 nlin = (int)(1. / c); /* nlin must be an integer */
916 c = 1. / nlin;
917 b = exp(-c * ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
918 linstep = b * c * exp(1.);
919
920 LogK1 = (float)(1. / c); /* if (v >= 2) token = k1*log(v*k2) */
921 LogK2 = (float)(1. / b);
922 lt2size = (int)(2. / linstep) + 1;
923 FromLT2 = (uint16_t *)_TIFFmallocExt(
924 tif, (tmsize_t)((size_t)lt2size * sizeof(uint16_t)));
925 From14 = (uint16_t *)_TIFFmallocExt(tif, 16384 * sizeof(uint16_t));
926 From8 = (uint16_t *)_TIFFmallocExt(tif, 256 * sizeof(uint16_t));
927 ToLinearF = (float *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(float));
928 ToLinear16 = (uint16_t *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(uint16_t));
929 ToLinear8 =
930 (unsigned char *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(unsigned char));
931 if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
932 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL)
933 {
934 if (FromLT2)
935 _TIFFfreeExt(tif, FromLT2);
936 if (From14)
937 _TIFFfreeExt(tif, From14);
938 if (From8)
939 _TIFFfreeExt(tif, From8);
940 if (ToLinearF)
941 _TIFFfreeExt(tif, ToLinearF);
942 if (ToLinear16)
943 _TIFFfreeExt(tif, ToLinear16);
944 if (ToLinear8)
945 _TIFFfreeExt(tif, ToLinear8);
946 sp->FromLT2 = NULL;
947 sp->From14 = NULL;
948 sp->From8 = NULL;
949 sp->ToLinearF = NULL;
950 sp->ToLinear16 = NULL;
951 sp->ToLinear8 = NULL;
952 return 0;
953 }
954
955 j = 0;
956
957 for (i = 0; i < nlin; i++)
958 {
959 v = i * linstep;
960 ToLinearF[j++] = (float)v;
961 }
962
963 for (i = nlin; i < TSIZE; i++)
964 ToLinearF[j++] = (float)(b * exp(c * i));
965
966 ToLinearF[2048] = ToLinearF[2047];
967
968 for (i = 0; i < TSIZEP1; i++)
969 {
970 v = (double)ToLinearF[i] * 65535.0 + 0.5;
971 ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16_t)v;
972 v = (double)ToLinearF[i] * 255.0 + 0.5;
973 ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
974 }
975
976 j = 0;
977 for (i = 0; i < lt2size; i++)
978 {
979 if ((i * linstep) * (i * linstep) >
980 (double)ToLinearF[j] * (double)ToLinearF[j + 1])
981 j++;
982 FromLT2[i] = (uint16_t)j;
983 }
984
985 /*
986 * Since we lose info anyway on 16-bit data, we set up a 14-bit
987 * table and shift 16-bit values down two bits on input.
988 * saves a little table space.
989 */
990 j = 0;
991 for (i = 0; i < 16384; i++)
992 {
993 while ((i / 16383.) * (i / 16383.) >
994 (double)ToLinearF[j] * (double)ToLinearF[j + 1])
995 j++;
996 From14[i] = (uint16_t)j;
997 }
998
999 j = 0;
1000 for (i = 0; i < 256; i++)
1001 {
1002 while ((i / 255.) * (i / 255.) >
1003 (double)ToLinearF[j] * (double)ToLinearF[j + 1])
1004 j++;
1005 From8[i] = (uint16_t)j;
1006 }
1007
1008 Fltsize = (float)(lt2size / 2);
1009
1010 sp->ToLinearF = ToLinearF;
1011 sp->ToLinear16 = ToLinear16;
1012 sp->ToLinear8 = ToLinear8;
1013 sp->FromLT2 = FromLT2;
1014 sp->From14 = From14;
1015 sp->From8 = From8;
1016
1017 return 1;
1018}
1019
1020#define PixarLogDecoderState(tif) ((PixarLogState *)(tif)->tif_data)
1021#define PixarLogEncoderState(tif) ((PixarLogState *)(tif)->tif_data)
1022
1023static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
1024static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
1025
1026#define PIXARLOGDATAFMT_UNKNOWN -1
1027
1040{
1041 int guess = PIXARLOGDATAFMT_UNKNOWN;
1042 int format = td->td_sampleformat;
1043
1044 /* If the user didn't tell us his datafmt,
1045 * take our best guess from the bitspersample.
1046 */
1047 switch (td->td_bitspersample)
1048 {
1049 case 32:
1050 if (format == SAMPLEFORMAT_IEEEFP)
1051 guess = PIXARLOGDATAFMT_FLOAT;
1052 break;
1053 case 16:
1054 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
1055 guess = PIXARLOGDATAFMT_16BIT;
1056 break;
1057 case 12:
1058 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
1060 break;
1061 case 11:
1062 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
1064 break;
1065 case 8:
1066 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
1067 guess = PIXARLOGDATAFMT_8BIT;
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 return guess;
1074}
1075
1091static tmsize_t multiply_ms(TIFF *tif, const char *module, tmsize_t m1,
1092 tmsize_t m2)
1093{
1094 return _TIFFMultiplySSize(tif, m1, m2, module);
1095}
1096
1110{
1111 assert(m1 >= 0 && m2 >= 0);
1112 /* if either input is zero, assume overflow already occurred */
1113 if (m1 == 0 || m2 == 0)
1114 return 0;
1115 else if (m1 > TIFF_TMSIZE_T_MAX - m2)
1116 return 0;
1117
1118 return m1 + m2;
1119}
1120
1130static int PixarLogFixupTags(TIFF *tif)
1131{
1132 (void)tif;
1133 return (1);
1134}
1135
1155{
1156 static const char module[] = "PixarLogSetupDecode";
1157 TIFFDirectory *td = &tif->tif_dir;
1159 tmsize_t tbuf_size;
1160 uint32_t segment_width, segment_height;
1161
1162 assert(sp != NULL);
1163
1164 /* This function can possibly be called several times by */
1165 /* PredictorSetupDecode() if this function succeeds but */
1166 /* PredictorSetup() fails */
1167 if ((sp->state & PLSTATE_INIT) != 0)
1168 return 1;
1169
1170 if (isTiled(tif))
1171 {
1172 segment_width = td->td_tilewidth;
1173 segment_height = td->td_tilelength;
1174 }
1175 else
1176 {
1177 segment_width = td->td_imagewidth;
1178 segment_height = td->td_rowsperstrip;
1179 if (segment_height > td->td_imagelength)
1180 segment_height = td->td_imagelength;
1181 }
1182
1183 /* Make sure no byte swapping happens on the data
1184 * after decompression. */
1186
1187 /* for some reason, we can't do this in TIFFInitPixarLog */
1188
1189 sp->stride =
1190 (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1191 : 1);
1192 /* multiply_ms() reports its own failures. Stop at the first one rather
1193 * than pass its zero result on to be reported again. */
1194 tbuf_size = multiply_ms(tif, module, sp->stride, segment_width);
1195 if (tbuf_size != 0)
1196 tbuf_size = multiply_ms(tif, module, tbuf_size, segment_height);
1197 if (tbuf_size != 0)
1198 tbuf_size =
1199 multiply_ms(tif, module, tbuf_size, (tmsize_t)sizeof(uint16_t));
1200 /* add one more stride in case input ends mid-stride */
1201 tbuf_size =
1202 add_ms(tbuf_size, (tmsize_t)(sizeof(uint16_t) * (size_t)sp->stride));
1203 if (tbuf_size == 0)
1204 return (0); /* TODO: this is an error return without error report
1205 through TIFFErrorExt */
1206 sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
1207 if (sp->tbuf == NULL)
1208 return (0);
1209 sp->tbuf_size = tbuf_size;
1213 {
1214 _TIFFfreeExt(tif, sp->tbuf);
1215 sp->tbuf = NULL;
1216 sp->tbuf_size = 0;
1217 TIFFErrorExtR(tif, module,
1218 "PixarLog compression can't handle bits depth/data "
1219 "format combination (depth: %" PRIu16 ")",
1220 td->td_bitspersample);
1221 return (0);
1222 }
1223
1224 if (inflateInit(&sp->stream) != Z_OK)
1225 {
1226 _TIFFfreeExt(tif, sp->tbuf);
1227 sp->tbuf = NULL;
1228 sp->tbuf_size = 0;
1229 TIFFErrorExtR(tif, module, "%s",
1230 sp->stream.msg ? sp->stream.msg : "(null)");
1231 return (0);
1232 }
1233 else
1234 {
1235 sp->state |= PLSTATE_INIT;
1236 return (1);
1237 }
1238}
1239
1240/*
1241 * Setup state for decoding a strip.
1242 */
1256static int PixarLogPreDecode(TIFF *tif, uint16_t s)
1257{
1258 static const char module[] = "PixarLogPreDecode";
1260
1261 (void)s;
1262 assert(sp != NULL);
1263 sp->stream.next_in = tif->tif_rawdata;
1264 assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
1265 we need to simplify this code to reflect a ZLib that is likely updated
1266 to deal with 8byte memory sizes, though this code will respond
1267 appropriately even before we simplify it */
1268 sp->stream.avail_in = (uInt)tif->tif_rawcc;
1269 if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
1270 {
1271 TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1272 return (0);
1273 }
1274 return (inflateReset(&sp->stream) == Z_OK);
1275}
1276
1302static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
1303{
1304 static const char module[] = "PixarLogDecode";
1305 TIFFDirectory *td = &tif->tif_dir;
1307 tmsize_t i;
1308 tmsize_t nsamples;
1309 tmsize_t llen;
1310 uint32_t row_width;
1311 uint16_t *up;
1312 uint8_t *op_start = op;
1313
1314 switch (sp->user_datafmt)
1315 {
1317 nsamples = (tmsize_t)((uint64_t)occ /
1318 sizeof(float)); /* XXX float == 32 bits */
1319 break;
1323 nsamples =
1324 (tmsize_t)((uint64_t)occ /
1325 sizeof(uint16_t)); /* XXX uint16_t == 16 bits */
1326 break;
1329 nsamples = occ;
1330 break;
1331 default:
1332 TIFFErrorExtR(tif, module,
1333 "%" PRIu16 " bit input not supported in PixarLog",
1334 td->td_bitspersample);
1335 memset(op, 0, (size_t)occ);
1336 return 0;
1337 }
1338
1339 row_width = isTiled(tif) ? td->td_tilewidth : td->td_imagewidth;
1340 /* stride (≤ td_samplesperpixel, max 65535) × row_width: fits tmsize_t */
1341 llen = (tmsize_t)sp->stride * row_width;
1342
1343 /* Fix: ABGR with stride=3 expands 3 samples to 4 output bytes per pixel */
1344 if (sp->user_datafmt == PIXARLOGDATAFMT_8BITABGR && sp->stride == 3)
1345 {
1346 /* row_width × 4: fits tmsize_t (row_width is uint32) */
1347 tmsize_t required = (tmsize_t)row_width * 4;
1348 tmsize_t max_rows;
1349 tmsize_t max_nsamples;
1350
1351 /*
1352 * Ensure at least one expanded output row fits.
1353 */
1354 if (occ < required)
1355 {
1356 TIFFErrorExtR(tif, module,
1357 "Output buffer too small for PixarLog ABGR data");
1358 memset(op, 0, (size_t)occ);
1359 return (0);
1360 }
1361
1362 /*
1363 * The caller-provided output buffer size must represent a whole
1364 * number of expanded ABGR scanlines.
1365 */
1366 if (occ % required)
1367 {
1369 tif, module,
1370 "Fractional scanline not supported for PixarLog ABGR data");
1371 memset(op, 0, (size_t)occ);
1372 return (0);
1373 }
1374
1375 /*
1376 * PixarLogDecode() may process multiple rows per call
1377 * (e.g. strip decoding). Limit nsamples so the total
1378 * output written by the loop below never exceeds occ.
1379 */
1380 max_rows = occ / required;
1381 max_nsamples = max_rows * llen;
1382
1383 if (nsamples > max_nsamples)
1384 {
1385 TIFFErrorExtR(tif, module,
1386 "Output buffer too small for PixarLog ABGR data");
1387 memset(op, 0, (size_t)occ);
1388 return (0);
1389 }
1390 }
1391
1392 (void)s;
1393 assert(sp != NULL);
1394
1395 sp->stream.next_in = tif->tif_rawcp;
1396 sp->stream.avail_in = (uInt)tif->tif_rawcc;
1397
1398 sp->stream.next_out = (unsigned char *)sp->tbuf;
1399 assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
1400 we need to simplify this code to reflect a ZLib that is likely updated
1401 to deal with 8byte memory sizes, though this code will respond
1402 appropriately even before we simplify it */
1403 sp->stream.avail_out = (uInt)((unsigned long)nsamples * sizeof(uint16_t));
1404 if (sp->stream.avail_out != (unsigned long)nsamples * sizeof(uint16_t))
1405 {
1406 TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1407 memset(op, 0, (size_t)occ);
1408 return (0);
1409 }
1410 /* Check that we will not fill more than what was allocated */
1411 if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
1412 {
1413 TIFFErrorExtR(tif, module, "sp->stream.avail_out > sp->tbuf_size");
1414 memset(op, 0, (size_t)occ);
1415 return (0);
1416 }
1417 do
1418 {
1419 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
1420 if (state == Z_STREAM_END)
1421 {
1422 break; /* XXX */
1423 }
1424 if (state == Z_DATA_ERROR)
1425 {
1426 TIFFErrorExtR(tif, module,
1427 "Decoding error at scanline %" PRIu32 ", %s",
1428 tif->tif_dir.td_row,
1429 sp->stream.msg ? sp->stream.msg : "(null)");
1430 memset(op, 0, (size_t)occ);
1431 return (0);
1432 }
1433 if (state != Z_OK)
1434 {
1435 TIFFErrorExtR(tif, module, "ZLib error: %s",
1436 sp->stream.msg ? sp->stream.msg : "(null)");
1437 memset(op, 0, (size_t)occ);
1438 return (0);
1439 }
1440 } while (sp->stream.avail_out > 0);
1441
1442 /* hopefully, we got all the bytes we needed */
1443 if (sp->stream.avail_out != 0)
1444 {
1445 TIFFErrorExtR(tif, module,
1446 "Not enough data at scanline %" PRIu32
1447 " (short %u bytes)",
1448 tif->tif_dir.td_row, sp->stream.avail_out);
1449 memset(op, 0, (size_t)occ);
1450 return (0);
1451 }
1452
1453 tif->tif_rawcp = sp->stream.next_in;
1454 tif->tif_rawcc = sp->stream.avail_in;
1455
1456 up = sp->tbuf;
1457 /* Swap bytes in the data if from a different endian machine. */
1458 if (tif->tif_flags & TIFF_SWAB)
1459 TIFFSwabArrayOfShort(up, nsamples);
1460
1461 /*
1462 * if llen is not an exact multiple of nsamples, the decode operation
1463 * may overflow the output buffer, so truncate it enough to prevent
1464 * that but still salvage as much data as possible.
1465 */
1466 if (nsamples % llen)
1467 {
1468 TIFFWarningExtR(tif, module,
1469 "stride %" TIFF_SSIZE_FORMAT
1470 " is not a multiple of sample count, "
1471 "%" TIFF_SSIZE_FORMAT ", data truncated.",
1472 llen, nsamples);
1473 nsamples -= nsamples % llen;
1474 }
1475
1476 for (i = 0; i < nsamples; i += llen, up += llen)
1477 {
1478 switch (sp->user_datafmt)
1479 {
1481 horizontalAccumulateF(up, llen, sp->stride, op, sp->ToLinearF);
1482 op += (unsigned long)llen * sizeof(float);
1483 break;
1485 horizontalAccumulate16(up, llen, sp->stride, op,
1486 sp->ToLinear16);
1487 op += (unsigned long)llen * sizeof(uint16_t);
1488 break;
1490 horizontalAccumulate12(up, llen, sp->stride, op, sp->ToLinearF);
1491 op += (unsigned long)llen * sizeof(int16_t);
1492 break;
1494 horizontalAccumulate11(up, llen, sp->stride, op);
1495 op += (unsigned long)llen * sizeof(uint16_t);
1496 break;
1498 horizontalAccumulate8(up, llen, sp->stride, (unsigned char *)op,
1499 sp->ToLinear8);
1500 op += (unsigned long)llen * sizeof(unsigned char);
1501 break;
1503 horizontalAccumulate8abgr(up, llen, sp->stride,
1504 (unsigned char *)op, sp->ToLinear8);
1505
1506 /* For stride == 3 (RGB), horizontalAccumulate8abgr expands to 4
1507 * bytes/pixel (ABGR) */
1508 if (sp->stride == 3)
1509 op += (unsigned long)row_width * 4;
1510 else
1511 op += (unsigned long)llen * sizeof(unsigned char);
1512 break;
1513 default:
1514 TIFFErrorExtR(tif, module, "Unsupported bits/sample: %" PRIu16,
1515 td->td_bitspersample);
1516 memset(op, 0, (size_t)occ);
1517 return (0);
1518 }
1519 }
1520
1521 /* A partial final row was not reconstructed. Do not expose its old data. */
1522 if (op - op_start < occ)
1523 memset(op, 0, (size_t)(occ - (op - op_start)));
1524
1525 return (1);
1526}
1527
1544{
1545 static const char module[] = "PixarLogSetupEncode";
1546 TIFFDirectory *td = &tif->tif_dir;
1548
1549 assert(sp != NULL);
1550
1551 /* for some reason, we can't do this in TIFFInitPixarLog */
1552
1553 sp->stride =
1554 (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1555 : 1);
1556 /* sp->tbuf is sized in PixarLogEncode() from the amount of data to
1557 * encode: td_rowsperstrip is (uint32_t)-1 for tiled images and when
1558 * RowsPerStrip is not set, and the image may still grow. */
1562 {
1563 TIFFErrorExtR(tif, module,
1564 "PixarLog compression can't handle %" PRIu16
1565 " bit linear encodings",
1566 td->td_bitspersample);
1567 return (0);
1568 }
1569
1570 if (deflateInit(&sp->stream, sp->quality) != Z_OK)
1571 {
1572 TIFFErrorExtR(tif, module, "%s",
1573 sp->stream.msg ? sp->stream.msg : "(null)");
1574 return (0);
1575 }
1576 else
1577 {
1578 sp->state |= PLSTATE_INIT;
1579 return (1);
1580 }
1581}
1582
1583/*
1584 * Reset encoding state at the start of a strip.
1585 */
1600static int PixarLogPreEncode(TIFF *tif, uint16_t s)
1601{
1602 static const char module[] = "PixarLogPreEncode";
1604
1605 (void)s;
1606 assert(sp != NULL);
1607 sp->stream.next_out = tif->tif_rawdata;
1608 assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
1609 we need to simplify this code to reflect a ZLib that is likely updated
1610 to deal with 8byte memory sizes, though this code will respond
1611 appropriately even before we simplify it */
1612 sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
1613 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
1614 {
1615 TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1616 return (0);
1617 }
1618 return (deflateReset(&sp->stream) == Z_OK);
1619}
1620
1645static void horizontalDifferenceF(const uint8_t *ip, tmsize_t n, int stride,
1646 uint16_t *wp, uint16_t *FromLT2)
1647{
1648 int32_t r1, g1, b1, a1, r2, g2, b2, a2, mask;
1649 float fltsize = Fltsize;
1650
1651#define CLAMP(v) \
1652 ((v < (float)0.) ? 0 \
1653 : (v < (float)2.) ? FromLT2[(int)(v * fltsize)] \
1654 : (v > (float)24.2) \
1655 ? 2047 \
1656 : (double)LogK1 * log((double)v * (double)LogK2) + 0.5)
1657
1658 mask = CODE_MASK;
1659 if (n >= stride)
1660 {
1661 if (stride == 3)
1662 {
1663 r2 = wp[0] = (uint16_t)CLAMP(PixarLogLoadFloatNativeUnaligned(ip));
1664 g2 = wp[1] = (uint16_t)CLAMP(
1665 PixarLogLoadFloatNativeUnaligned(ip + sizeof(float)));
1666 b2 = wp[2] = (uint16_t)CLAMP(
1667 PixarLogLoadFloatNativeUnaligned(ip + 2 * sizeof(float)));
1668 n -= 3;
1669 while (n > 0)
1670 {
1671 n -= 3;
1672 wp += 3;
1673 ip += 3 * sizeof(float);
1674 r1 = (int32_t)CLAMP(PixarLogLoadFloatNativeUnaligned(ip));
1675 wp[0] = (uint16_t)((r1 - r2) & mask);
1676 r2 = r1;
1677 g1 = (int32_t)CLAMP(
1678 PixarLogLoadFloatNativeUnaligned(ip + sizeof(float)));
1679 wp[1] = (uint16_t)((g1 - g2) & mask);
1680 g2 = g1;
1681 b1 = (int32_t)CLAMP(
1682 PixarLogLoadFloatNativeUnaligned(ip + 2 * sizeof(float)));
1683 wp[2] = (uint16_t)((b1 - b2) & mask);
1684 b2 = b1;
1685 }
1686 }
1687 else if (stride == 4)
1688 {
1689 r2 = wp[0] = (uint16_t)CLAMP(PixarLogLoadFloatNativeUnaligned(ip));
1690 g2 = wp[1] = (uint16_t)CLAMP(
1691 PixarLogLoadFloatNativeUnaligned(ip + sizeof(float)));
1692 b2 = wp[2] = (uint16_t)CLAMP(
1693 PixarLogLoadFloatNativeUnaligned(ip + 2 * sizeof(float)));
1694 a2 = wp[3] = (uint16_t)CLAMP(
1695 PixarLogLoadFloatNativeUnaligned(ip + 3 * sizeof(float)));
1696 n -= 4;
1697 while (n > 0)
1698 {
1699 n -= 4;
1700 wp += 4;
1701 ip += 4 * sizeof(float);
1702 r1 = (int32_t)CLAMP(PixarLogLoadFloatNativeUnaligned(ip));
1703 wp[0] = (uint16_t)((r1 - r2) & mask);
1704 r2 = r1;
1705 g1 = (int32_t)CLAMP(
1706 PixarLogLoadFloatNativeUnaligned(ip + sizeof(float)));
1707 wp[1] = (uint16_t)((g1 - g2) & mask);
1708 g2 = g1;
1709 b1 = (int32_t)CLAMP(
1710 PixarLogLoadFloatNativeUnaligned(ip + 2 * sizeof(float)));
1711 wp[2] = (uint16_t)((b1 - b2) & mask);
1712 b2 = b1;
1713 a1 = (int32_t)CLAMP(
1714 PixarLogLoadFloatNativeUnaligned(ip + 3 * sizeof(float)));
1715 wp[3] = (uint16_t)((a1 - a2) & mask);
1716 a2 = a1;
1717 }
1718 }
1719 else
1720 {
1721 REPEAT(stride, wp[0] = (uint16_t)CLAMP(
1723 wp++; ip += sizeof(float))
1724 n -= stride;
1725 while (n > 0)
1726 {
1727 REPEAT(
1728 stride,
1729 wp[0] =
1730 (uint16_t)(((int32_t)CLAMP(
1732 (int32_t)CLAMP(
1734 ip -
1735 (tmsize_t)stride *
1736 (tmsize_t)sizeof(float)))) &
1737 mask);
1738 wp++; ip += sizeof(float))
1739 n -= stride;
1740 }
1741 }
1742 }
1743}
1744
1768static void horizontalDifference16(const uint8_t *ip, tmsize_t n, int stride,
1769 unsigned short *wp, uint16_t *From14)
1770{
1771 int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1772
1773/* assumption is unsigned pixel values */
1774#undef CLAMP
1775#define CLAMP(v) From14[(v) >> 2]
1776
1777 mask = CODE_MASK;
1778 if (n >= stride)
1779 {
1780 if (stride == 3)
1781 {
1782 r2 = wp[0] = CLAMP(PixarLogLoad16NativeUnaligned(ip));
1783 g2 = wp[1] =
1784 CLAMP(PixarLogLoad16NativeUnaligned(ip + sizeof(uint16_t)));
1785 b2 = wp[2] =
1786 CLAMP(PixarLogLoad16NativeUnaligned(ip + 2 * sizeof(uint16_t)));
1787 n -= 3;
1788 while (n > 0)
1789 {
1790 n -= 3;
1791 wp += 3;
1792 ip += 3 * sizeof(uint16_t);
1794 wp[0] = (uint16_t)((r1 - r2) & mask);
1795 r2 = r1;
1796 g1 =
1797 CLAMP(PixarLogLoad16NativeUnaligned(ip + sizeof(uint16_t)));
1798 wp[1] = (uint16_t)((g1 - g2) & mask);
1799 g2 = g1;
1800 b1 = CLAMP(
1801 PixarLogLoad16NativeUnaligned(ip + 2 * sizeof(uint16_t)));
1802 wp[2] = (uint16_t)((b1 - b2) & mask);
1803 b2 = b1;
1804 }
1805 }
1806 else if (stride == 4)
1807 {
1808 r2 = wp[0] = CLAMP(PixarLogLoad16NativeUnaligned(ip));
1809 g2 = wp[1] =
1810 CLAMP(PixarLogLoad16NativeUnaligned(ip + sizeof(uint16_t)));
1811 b2 = wp[2] =
1812 CLAMP(PixarLogLoad16NativeUnaligned(ip + 2 * sizeof(uint16_t)));
1813 a2 = wp[3] =
1814 CLAMP(PixarLogLoad16NativeUnaligned(ip + 3 * sizeof(uint16_t)));
1815 n -= 4;
1816 while (n > 0)
1817 {
1818 n -= 4;
1819 wp += 4;
1820 ip += 4 * sizeof(uint16_t);
1822 wp[0] = (uint16_t)((r1 - r2) & mask);
1823 r2 = r1;
1824 g1 =
1825 CLAMP(PixarLogLoad16NativeUnaligned(ip + sizeof(uint16_t)));
1826 wp[1] = (uint16_t)((g1 - g2) & mask);
1827 g2 = g1;
1828 b1 = CLAMP(
1829 PixarLogLoad16NativeUnaligned(ip + 2 * sizeof(uint16_t)));
1830 wp[2] = (uint16_t)((b1 - b2) & mask);
1831 b2 = b1;
1832 a1 = CLAMP(
1833 PixarLogLoad16NativeUnaligned(ip + 3 * sizeof(uint16_t)));
1834 wp[3] = (uint16_t)((a1 - a2) & mask);
1835 a2 = a1;
1836 }
1837 }
1838 else
1839 {
1840 REPEAT(stride, wp[0] = CLAMP(PixarLogLoad16NativeUnaligned(ip));
1841 wp++; ip += sizeof(uint16_t))
1842 n -= stride;
1843 while (n > 0)
1844 {
1845 REPEAT(
1846 stride,
1847 wp[0] =
1848 (uint16_t)((CLAMP(PixarLogLoad16NativeUnaligned(ip)) -
1850 ip - (tmsize_t)stride *
1851 (tmsize_t)sizeof(uint16_t)))) &
1852 mask);
1853 wp++; ip += sizeof(uint16_t))
1854 n -= stride;
1855 }
1856 }
1857 }
1858}
1859
1878static void horizontalDifference8(unsigned char *ip, tmsize_t n, int stride,
1879 unsigned short *wp, uint16_t *From8)
1880{
1881 int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1882
1883#undef CLAMP
1884#define CLAMP(v) (From8[(v)])
1885
1886 mask = CODE_MASK;
1887 if (n >= stride)
1888 {
1889 if (stride == 3)
1890 {
1891 r2 = wp[0] = CLAMP(ip[0]);
1892 g2 = wp[1] = CLAMP(ip[1]);
1893 b2 = wp[2] = CLAMP(ip[2]);
1894 n -= 3;
1895 while (n > 0)
1896 {
1897 n -= 3;
1898 r1 = CLAMP(ip[3]);
1899 wp[3] = (uint16_t)((r1 - r2) & mask);
1900 r2 = r1;
1901 g1 = CLAMP(ip[4]);
1902 wp[4] = (uint16_t)((g1 - g2) & mask);
1903 g2 = g1;
1904 b1 = CLAMP(ip[5]);
1905 wp[5] = (uint16_t)((b1 - b2) & mask);
1906 b2 = b1;
1907 wp += 3;
1908 ip += 3;
1909 }
1910 }
1911 else if (stride == 4)
1912 {
1913 r2 = wp[0] = CLAMP(ip[0]);
1914 g2 = wp[1] = CLAMP(ip[1]);
1915 b2 = wp[2] = CLAMP(ip[2]);
1916 a2 = wp[3] = CLAMP(ip[3]);
1917 n -= 4;
1918 while (n > 0)
1919 {
1920 n -= 4;
1921 r1 = CLAMP(ip[4]);
1922 wp[4] = (uint16_t)((r1 - r2) & mask);
1923 r2 = r1;
1924 g1 = CLAMP(ip[5]);
1925 wp[5] = (uint16_t)((g1 - g2) & mask);
1926 g2 = g1;
1927 b1 = CLAMP(ip[6]);
1928 wp[6] = (uint16_t)((b1 - b2) & mask);
1929 b2 = b1;
1930 a1 = CLAMP(ip[7]);
1931 wp[7] = (uint16_t)((a1 - a2) & mask);
1932 a2 = a1;
1933 wp += 4;
1934 ip += 4;
1935 }
1936 }
1937 else
1938 {
1939 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1940 n -= stride;
1941 while (n > 0)
1942 {
1943 REPEAT(stride,
1944 wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
1945 mask);
1946 wp++; ip++)
1947 n -= stride;
1948 }
1949 }
1950 }
1951}
1952
1953/*
1954 * Encode a chunk of pixels.
1955 */
1986static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1987{
1988 static const char module[] = "PixarLogEncode";
1989 TIFFDirectory *td = &tif->tif_dir;
1991 tmsize_t i;
1992 tmsize_t n;
1993 tmsize_t llen;
1994 unsigned short *up;
1995
1996 (void)s;
1997
1998 switch (sp->user_datafmt)
1999 {
2001 n = (tmsize_t)((unsigned long)cc /
2002 sizeof(float)); /* XXX float == 32 bits */
2003 break;
2007 n = (tmsize_t)((unsigned long)cc /
2008 sizeof(uint16_t)); /* XXX uint16_t == 16 bits */
2009 break;
2012 n = cc;
2013 break;
2014 default:
2015 TIFFErrorExtR(tif, module,
2016 "%" PRIu16 " bit input not supported in PixarLog",
2017 td->td_bitspersample);
2018 return 0;
2019 }
2020
2021 /* stride (≤ td_samplesperpixel, max 65535) × width: fits tmsize_t */
2022 llen = (tmsize_t)sp->stride *
2023 (isTiled(tif) ? td->td_tilewidth : td->td_imagewidth);
2024 /* The loop below converts whole rows of llen samples from bp. */
2025 if (llen == 0 || n % llen != 0)
2026 {
2027 TIFFErrorExtR(tif, module, "Fractional scanline cannot be written");
2028 return 0;
2029 }
2030 if ((uint64_t)(n / llen) >
2031 (isTiled(tif) ? td->td_tilelength : td->td_rowsperstrip))
2032 {
2033 TIFFErrorExtR(tif, module, "Too many input bytes provided");
2034 return 0;
2035 }
2036 if (n > sp->tbuf_size / (tmsize_t)sizeof(uint16_t))
2037 {
2038 uint16_t *new_tbuf = (uint16_t *)_TIFFCheckRealloc(
2039 tif, sp->tbuf, n, sizeof(uint16_t), "PixarLog encoding");
2040 if (new_tbuf == NULL)
2041 return 0;
2042 sp->tbuf = new_tbuf;
2043 sp->tbuf_size = n * (tmsize_t)sizeof(uint16_t);
2044 }
2045
2046 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen)
2047 {
2048 switch (sp->user_datafmt)
2049 {
2051 horizontalDifferenceF(bp, llen, sp->stride, up, sp->FromLT2);
2052 bp += (unsigned long)llen * sizeof(float);
2053 break;
2055 horizontalDifference16(bp, llen, sp->stride, up, sp->From14);
2056 bp += (unsigned long)llen * sizeof(uint16_t);
2057 break;
2059 horizontalDifference8((unsigned char *)bp, llen, sp->stride, up,
2060 sp->From8);
2061 bp += (unsigned long)llen * sizeof(unsigned char);
2062 break;
2063 default:
2064 TIFFErrorExtR(tif, module,
2065 "%" PRIu16 " bit input not supported in PixarLog",
2066 td->td_bitspersample);
2067 return 0;
2068 }
2069 }
2070
2071 sp->stream.next_in = (unsigned char *)sp->tbuf;
2072 assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
2073 we need to simplify this code to reflect a ZLib that is likely updated
2074 to deal with 8byte memory sizes, though this code will respond
2075 appropriately even before we simplify it */
2076 sp->stream.avail_in = (uInt)((unsigned long)n * sizeof(uint16_t));
2077 if ((sp->stream.avail_in / sizeof(uint16_t)) != (unsigned long)n)
2078 {
2079 TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
2080 return (0);
2081 }
2082
2083 do
2084 {
2085 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
2086 {
2087 TIFFErrorExtR(tif, module, "Encoder error: %s",
2088 sp->stream.msg ? sp->stream.msg : "(null)");
2089 return (0);
2090 }
2091 if (sp->stream.avail_out == 0)
2092 {
2093 tif->tif_rawcc = tif->tif_rawdatasize;
2094 if (!TIFFFlushData1(tif))
2095 return 0;
2096 sp->stream.next_out = tif->tif_rawdata;
2097 sp->stream.avail_out =
2098 (uInt)tif
2099 ->tif_rawdatasize; /* this is a safe typecast, as check is
2100 made already in PixarLogPreEncode */
2101 }
2102 } while (sp->stream.avail_in > 0);
2103 return (1);
2104}
2105
2106/*
2107 * Finish off an encoded strip by flushing the last
2108 * string and tacking on an End Of Information code.
2109 */
2110
2124static int PixarLogPostEncode(TIFF *tif)
2125{
2126 static const char module[] = "PixarLogPostEncode";
2128 int state;
2129
2130 sp->stream.avail_in = 0;
2131
2132 do
2133 {
2134 state = deflate(&sp->stream, Z_FINISH);
2135 switch (state)
2136 {
2137 case Z_STREAM_END:
2138 case Z_OK:
2139 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
2140 {
2141 tif->tif_rawcc =
2142 tif->tif_rawdatasize - sp->stream.avail_out;
2143 if (!TIFFFlushData1(tif))
2144 return 0;
2145 sp->stream.next_out = tif->tif_rawdata;
2146 sp->stream.avail_out =
2147 (uInt)tif->tif_rawdatasize; /* this is a safe typecast,
2148 as check is made already
2149 in PixarLogPreEncode */
2150 }
2151 break;
2152 default:
2153 TIFFErrorExtR(tif, module, "ZLib error: %s",
2154 sp->stream.msg ? sp->stream.msg : "(null)");
2155 return (0);
2156 }
2157 } while (state != Z_STREAM_END);
2158 return (1);
2159}
2160
2179static void PixarLogClose(TIFF *tif)
2180{
2181 PixarLogState *sp = (PixarLogState *)tif->tif_data;
2182 TIFFDirectory *td = &tif->tif_dir;
2183
2184 assert(sp != 0);
2185 /* In a really sneaky (and really incorrect, and untruthful, and
2186 * troublesome, and error-prone) maneuver that completely goes against
2187 * the spirit of TIFF, and breaks TIFF, on close, we covertly
2188 * modify both bitspersample and sampleformat in the directory to
2189 * indicate 8-bit linear. This way, the decode "just works" even for
2190 * readers that don't know about PixarLog, or how to set
2191 * the PIXARLOGDATFMT pseudo-tag.
2192 */
2193
2194 if (sp->state & PLSTATE_INIT)
2195 {
2196 /* Invalidate BPS-dependent tags before changing BitsPerSample.
2197 * ColorMap and TransferFunction have 1<<BPS entries per plane;
2198 * increasing BPS to 8 causes an OOB read in the directory writer
2199 * (see bugzilla 2604). SMin/SMaxSampleValue are double arrays
2200 * sized by SamplesPerPixel, not BPS, but are invalidated because
2201 * the codec also forces SampleFormat to UINT and stores 8-bit
2202 * data, so the old extrema no longer describe the samples.
2203 * We cannot use TIFFSetField() here because OkToChangeTag()
2204 * blocks BPS changes after TIFF_BEENWRITING.
2205 * See LogLuvClose for the same class of issue. */
2206 if (td->td_bitspersample != 8)
2207 {
2208 for (int i = 0; i < 3; i++)
2209 {
2210 _TIFFfreeExt(tif, td->td_colormap[i]);
2211 td->td_colormap[i] = NULL;
2212 _TIFFfreeExt(tif, td->td_transferfunction[i]);
2213 td->td_transferfunction[i] = NULL;
2214 }
2215 td->td_colormap_count = 0;
2216 td->td_colormap_alloc = 0;
2221 td->td_sminsamplevalue = NULL;
2224 td->td_smaxsamplevalue = NULL;
2226 }
2227 td->td_bitspersample = 8;
2229 }
2230}
2231
2245static void PixarLogCleanup(TIFF *tif)
2246{
2247 PixarLogState *sp = (PixarLogState *)tif->tif_data;
2248
2249 assert(sp != 0);
2250
2251 (void)TIFFPredictorCleanup(tif);
2252
2255
2256 if (sp->FromLT2)
2257 _TIFFfreeExt(tif, sp->FromLT2);
2258 if (sp->From14)
2259 _TIFFfreeExt(tif, sp->From14);
2260 if (sp->From8)
2261 _TIFFfreeExt(tif, sp->From8);
2262 if (sp->ToLinearF)
2263 _TIFFfreeExt(tif, sp->ToLinearF);
2264 if (sp->ToLinear16)
2265 _TIFFfreeExt(tif, sp->ToLinear16);
2266 if (sp->ToLinear8)
2267 _TIFFfreeExt(tif, sp->ToLinear8);
2268 if (sp->state & PLSTATE_INIT)
2269 {
2270 if (tif->tif_mode == O_RDONLY)
2271 inflateEnd(&sp->stream);
2272 else
2273 deflateEnd(&sp->stream);
2274 }
2275 if (sp->tbuf)
2276 _TIFFfreeExt(tif, sp->tbuf);
2277 _TIFFfreeExt(tif, sp);
2278 tif->tif_data = NULL;
2279
2281}
2282
2304static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap)
2305{
2306 static const char module[] = "PixarLogVSetField";
2307 PixarLogState *sp = (PixarLogState *)tif->tif_data;
2308 int result;
2309
2310 switch (tag)
2311 {
2313 sp->quality = (int)va_arg(ap, int);
2314 if (tif->tif_mode != O_RDONLY && (sp->state & PLSTATE_INIT))
2315 {
2316 if (deflateParams(&sp->stream, sp->quality,
2317 Z_DEFAULT_STRATEGY) != Z_OK)
2318 {
2319 TIFFErrorExtR(tif, module, "ZLib error: %s",
2320 sp->stream.msg ? sp->stream.msg : "(null)");
2321 return (0);
2322 }
2323 }
2324 return (1);
2326 sp->user_datafmt = (int)va_arg(ap, int);
2327 /* Tweak the TIFF header so that the rest of libtiff knows what
2328 * size of data will be passed between app and library, and
2329 * assume that the app knows what it is doing and is not
2330 * confused by these header manipulations...
2331 */
2332 switch (sp->user_datafmt)
2333 {
2338 break;
2342 break;
2346 break;
2350 break;
2355 break;
2356 default:
2357 break;
2358 }
2359 /*
2360 * Must recalculate sizes should bits/sample change.
2361 */
2362 tif->tif_dir.td_tilesize =
2363 isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
2365 result = 1; /* NB: pseudo tag */
2366 break;
2367 default:
2368 result = (*sp->vsetparent)(tif, tag, ap);
2369 }
2370 return (result);
2371}
2372
2388static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap)
2389{
2390 PixarLogState *sp = (PixarLogState *)tif->tif_data;
2391
2392 switch (tag)
2393 {
2395 *va_arg(ap, int *) = sp->quality;
2396 break;
2398 *va_arg(ap, int *) = sp->user_datafmt;
2399 break;
2400 default:
2401 return (*sp->vgetparent)(tif, tag, ap);
2402 }
2403 return (1);
2404}
2405
2418
2430{
2431 (void)tif;
2432 /* cf https://zlib.net/zlib_tech.html */
2433 const uint64_t MAX_DEFLATE_RATIO = 1032;
2434
2435 /* security margin as I don't understand what this codec does */
2436 return MAX_DEFLATE_RATIO * (uint64_t)4;
2437}
2438
2439int TIFFInitPixarLog(TIFF *tif, int scheme)
2440{
2441 static const char module[] = "TIFFInitPixarLog";
2442
2443 PixarLogState *sp;
2444
2445 (void)scheme;
2446 assert(scheme == COMPRESSION_PIXARLOG);
2447
2448 /*
2449 * Merge codec-specific tag information.
2450 */
2452 {
2453 TIFFErrorExtR(tif, module,
2454 "Merging PixarLog codec-specific tags failed");
2455 return 0;
2456 }
2457
2458 /*
2459 * Allocate state block so tag methods have storage to record values.
2460 */
2461 tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(PixarLogState));
2462 if (tif->tif_data == NULL)
2463 goto bad;
2464 sp = (PixarLogState *)tif->tif_data;
2465 _TIFFmemset(sp, 0, sizeof(*sp));
2466 sp->stream.data_type = Z_BINARY;
2467 sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
2468
2469 /*
2470 * Install codec methods.
2471 */
2484 tif->tif_close = PixarLogClose;
2487
2488 /* Override SetField so we can handle our private pseudo-tag */
2489 sp->vgetparent = tif->tif_tagmethods.vgetfield;
2490 tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
2491 sp->vsetparent = tif->tif_tagmethods.vsetfield;
2492 tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
2493
2494 /* Default values for codec-specific fields */
2495 sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
2496 sp->state = 0;
2497
2498 /* we don't wish to use the predictor,
2499 * the default is none, which predictor value 1
2500 */
2501 (void)TIFFPredictorInit(tif);
2502
2503 /*
2504 * build the companding tables
2505 */
2506 if (!PixarLogMakeTables(tif, sp))
2507 {
2508 TIFFErrorExtR(tif, module,
2509 "Failed to allocate memory for PixarLog tables");
2510 return (0);
2511 }
2512
2513 return (1);
2514bad:
2515 TIFFErrorExtR(tif, module, "No space for PixarLog state block");
2516 return (0);
2517}
2518#endif /* PIXARLOG_SUPPORT */
2519
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
Definition tif_aux.c:91
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
Definition tif_config.h:294
#define FIELD_TRANSFERFUNCTION
Index the transfer function presence bit; not an on-disk tag number.
Definition tif_dir.h:580
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define FIELD_SMAXSAMPLEVALUE
Index the per-sample maximum presence bit; not an on-disk tag number.
Definition tif_dir.h:544
#define FIELD_COLORMAP
Index the colormap presence bit; not an on-disk tag number.
Definition tif_dir.h:524
#define FIELD_SMINSAMPLEVALUE
Index the per-sample minimum presence bit; not an on-disk tag number.
Definition tif_dir.h:539
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
Definition tif_dir.h:620
#define TIFFClrFieldBit(tif, field)
Clear a directory presence bit.
Definition tif_dir.h:688
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Leave decoded bytes unchanged.
Definition tif_read.c:2209
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFCheckRealloc(TIFF *tif, void *buffer, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Resize a checked array with an allocation-failure diagnostic.
Definition tif_aux.c:249
static void PixarLogStore16NativeUnaligned(uint8_t *cp, uint16_t v)
Write one unaligned native-order sixteen-bit sample.
static float PixarLogLoadFloatNativeUnaligned(const uint8_t *cp)
Read one unaligned native-order float.
static void horizontalAccumulate11(uint16_t *wp, tmsize_t n, int stride, uint8_t *op)
Accumulate PixarLog prediction into raw eleven-bit code words in native uint16_t slots.
#define TSIZE
Count the 2048 eleven-bit code values.
#define TSIZEP1
Include one repeated endpoint in decode tables.
static const TIFFField pixarlogFields[]
Define PixarLog format and quality pseudo-tags.
static void horizontalDifferenceF(const uint8_t *ip, tmsize_t n, int stride, uint16_t *wp, uint16_t *FromLT2)
Compand finite native float32 samples and difference eleven-bit codes.
static void horizontalDifference16(const uint8_t *ip, tmsize_t n, int stride, unsigned short *wp, uint16_t *From14)
Compand native unsigned sixteen-bit samples and difference eleven-bit codes.
static int PixarLogMakeTables(TIFF *tif, PixarLogState *sp)
Build linear/log companding tables for PixarLog sample formats.
#define ONE
Select code 1250 for linear value one.
static tmsize_t add_ms(tmsize_t m1, tmsize_t m2)
Add positive buffer extents with signed overflow rejection.
#define SCALE12
Scale PICIO output by 2048.
static int PixarLogPreEncode(TIFF *tif, uint16_t s)
Reset PixarLog deflation with the current raw output buffer.
static int PixarLogSetupDecode(TIFF *tif)
Allocate companded row storage and initialise PixarLog inflation.
static uint64_t PixarLogGetMaxCompressionRatio(TIFF *tif)
Report a conservative PixarLog expansion allocation heuristic.
static int PixarLogGuessDataFmt(TIFFDirectory *td)
Infer the PixarLog user format from depth and sample format.
static void horizontalAccumulate12(uint16_t *wp, tmsize_t n, int stride, uint8_t *op, float *ToLinearF)
Accumulate PixarLog prediction into twelve-bit PICIO values scaled by 2048 and capped at 3071.
static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Compand and difference complete input rows then deflate their codes.
static int PixarLogPostEncode(TIFF *tif)
Finish PixarLog deflation and flush encoded output.
int TIFFInitPixarLog(TIFF *tif, int scheme)
Install PixarLog codec state methods and companding tables.
static void horizontalDifference8(unsigned char *ip, tmsize_t n, int stride, unsigned short *wp, uint16_t *From8)
Compand unsigned eight-bit samples and difference eleven-bit codes.
static void horizontalAccumulate8(uint16_t *wp, tmsize_t n, int stride, unsigned char *op, unsigned char *ToLinear8)
Accumulate PixarLog prediction into clipped unsigned eight-bit linear samples.
static void PixarLogCleanup(TIFF *tif)
Restore PixarLog method chains and release companding storage.
#define CLAMP12(t)
Cap positive PICIO output at 3071.
#define RATIO
Choose the nominal log-region step ratio.
static void PixarLogClose(TIFF *tif)
Present initialised PixarLog directory metadata as unsigned eight-bit samples.
static void horizontalAccumulate16(uint16_t *wp, tmsize_t n, int stride, uint8_t *op, uint16_t *ToLinear16)
Accumulate PixarLog prediction into clipped unsigned sixteen-bit linear samples.
#define PixarLogDecoderState(tif)
Borrow the handle-owned PixarLog decoder state.
#define PIXARLOGDATAFMT_UNKNOWN
Mark an application format not yet inferred or selected.
static void horizontalAccumulate8abgr(uint16_t *wp, tmsize_t n, int stride, unsigned char *op, unsigned char *ToLinear8)
Accumulate PixarLog prediction into eight-bit ABGR bytes.
static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set PixarLog quality or user data format and update directory sizes.
static int PixarLogSetupEncode(TIFF *tif)
Allocate PixarLog code staging and initialise zlib deflation.
#define CODE_MASK
Mask accumulated codes to eleven bits.
#define PixarLogEncoderState(tif)
Borrow the handle-owned PixarLog encoder state.
static float LogK1
Scale logarithmic float companding.
static float Fltsize
Scale low-range float lookup indices.
#define CLAMP(v)
Map one float sixteen-bit or byte input to a companded code in its enclosing converter.
static void PixarLogStoreFloatNativeUnaligned(uint8_t *cp, float v)
Write one unaligned native-order float.
static void horizontalAccumulateF(uint16_t *wp, tmsize_t n, int stride, uint8_t *op, float *ToLinearF)
Accumulate PixarLog prediction into float32 linear samples.
#define PLSTATE_INIT
Mark dependency stream initialised.
static float LogK2
Normalise logarithmic float companding.
#define REPEAT(n, op)
Repeat a statement n times, and at least once.
static uint16_t PixarLogLoad16NativeUnaligned(const uint8_t *cp)
Read one unaligned native-order sixteen-bit sample.
static int PixarLogPreDecode(TIFF *tif, uint16_t s)
Reset PixarLog inflation over the current raw strile.
static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve PixarLog quality or format or delegate another query.
static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Inflate companded samples and convert complete PixarLog output rows.
static int PixarLogFixupTags(TIFF *tif)
Accept PixarLog tag fixup without changes.
static tmsize_t multiply_ms(TIFF *tif, const char *module, tmsize_t m1, tmsize_t m2)
Multiply positive buffer extents with signed-size checking.
int TIFFPredictorCleanup(TIFF *tif)
Restore saved Predictor tag print and setup hooks.
int TIFFPredictorInit(TIFF *tif)
Install Predictor tag and setup hooks around a codec.
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFF_SWAB
Request swapping multibyte file values to or from host order.
Definition tiffiop.h:441
#define TIFFArrayCount(a)
Compute the element count of an actual array.
Definition tiffiop.h:1239
void TIFFSwabArrayOfShort(uint16_t *wp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:87
#define TIFF_ANY
Match any data type when searching for a field descriptor.
Definition tiffio.h:1289
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
#define PIXARLOGDATAFMT_8BITABGR
ABGR-order u_chars.
Definition tiff.h:1566
#define PIXARLOGDATAFMT_8BIT
Regular u_char samples.
Definition tiff.h:1564
#define PIXARLOGDATAFMT_16BIT
Signed short samples.
Definition tiff.h:1572
#define PIXARLOGDATAFMT_12BITPICIO
As per PICIO (1.0==2048).
Definition tiff.h:1570
#define TIFFTAG_PIXARLOGDATAFMT
Pseudo-tag: PixarLogCodec I/O data sz.
Definition tiff.h:1562
#define TIFFTAG_PIXARLOGQUALITY
Pseudo-tag: PixarLog compression level control.
Definition tiff.h:1612
#define PIXARLOGDATAFMT_11BITLOG
11-bit log-encoded (raw).
Definition tiff.h:1568
#define PIXARLOGDATAFMT_FLOAT
IEEE float samples.
Definition tiff.h:1574
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
Definition tiffio.h:1521
#define TIFFTAG_BITSPERSAMPLE
TIFF tag: Bits per channel (sample).
Definition tiff.h:298
#define COMPRESSION_PIXARLOG
Pixar companded 11bit ZIP.
Definition tiff.h:347
#define SAMPLEFORMAT_UINT
Unsigned integer data (introduced in TIFF 6.0).
Definition tiff.h:610
#define SAMPLEFORMAT_IEEEFP
IEEE floating point data (introduced in TIFF 6.0).
Definition tiff.h:614
#define TIFFTAG_SAMPLEFORMAT
TIFF tag: Data sample format (introduced in TIFF 6.0).
Definition tiff.h:608
#define SAMPLEFORMAT_VOID
Untyped data (introduced in TIFF 6.0).
Definition tiff.h:616
#define SAMPLEFORMAT_INT
Signed integer data (introduced in TIFF 6.0).
Definition tiff.h:612
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
Definition tiffio.h:91
Own PixarLog companding tables predictor prefix and zlib progress.
uint16_t * From14
Owned Ext 16384-entry lookup for input shifted right two bits.
unsigned char * ToLinear8
Owned Ext clipped eight-bit decode table.
TIFFPredictorState predict
Predictor prefix, first in codec allocation.
TIFFVSetMethod vgetparent
Borrowed parent getter stored under the declared compatible method typedef.
uint16_t stride
Samples per interleaved pixel or one for separate data.
uint16_t * FromLT2
Owned Ext low-range float-to-code mapping.
z_stream stream
Zlib stream whose internal allocation ends during cleanup.
TIFFVSetMethod vsetparent
Borrowed parent setter restored during cleanup.
int user_datafmt
Cached application-facing format, initially UNKNOWN.
uint16_t * ToLinear16
Owned Ext clipped sixteen-bit decode table.
uint16_t * tbuf
Owned Ext aligned uint16_t code staging, released by cleanup.
uint16_t * From8
Owned Ext 256-entry byte-to-code lookup.
tmsize_t tbuf_size
Byte capacity of tbuf; set by decode setup and grown on demand by the encoder.
int quality
Cached zlib quality, applied at setup or active parameter update.
int state
PLSTATE_INIT after dependency setup.
float * ToLinearF
Owned Ext float decode table, TSIZEP1 entries.
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_colormap_alloc
Number of entries allocated in each ColorMap plane, zero when none is installed.
Definition tif_dir.h:228
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
Definition tif_dir.h:259
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
double * td_sminsamplevalue
Owned SamplesPerPixel-length array of minimum values, or NULL.
Definition tif_dir.h:190
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
uint16_t * td_transferfunction[3]
Up to three owned gamma curves of td_transferfunction_count entries each.
Definition tif_dir.h:320
uint32_t td_colormap_count
Number of entries in each ColorMap plane that the directory uses, zero when none is installed.
Definition tif_dir.h:223
double * td_smaxsamplevalue
Owned SamplesPerPixel-length array of maximum values, or NULL.
Definition tif_dir.h:192
uint16_t * td_colormap[3]
Three owned palette planes of td_colormap_alloc entries each, or NULL slots; presence is FIELD_COLORM...
Definition tif_dir.h:218
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
Definition tif_dir.h:170
uint32_t td_transferfunction_count
Number of entries in each installed TransferFunction curve, zero when none is installed; the only rec...
Definition tif_dir.h:325
Prefix codec state with Predictor method chains and row geometry.
Definition tif_predict.h:76
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
Definition tiffio.h:1577
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Definition tiffio.h:1575
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
Definition tiffiop.h:742
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
Definition tiffiop.h:639
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
Definition tiffiop.h:630
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
Definition tiffiop.h:670
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
TIFFVoidMethod tif_close
Codec close callback; distinct from the client I/O close callback.
Definition tiffiop.h:657
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
Definition tiffiop.h:675
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
int tif_mode
Current access mode using the O_* constants.
Definition tiffiop.h:400
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.