2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include "opj_apps_config.h"
45 #ifndef OPJ_HAVE_LIBTIFF
46 # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
47 #endif /* OPJ_HAVE_LIBTIFF */
53 /* -->> -->> -->> -->>
57 <<-- <<-- <<-- <<-- */
58 typedef void (* tif_32stoX)(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length);
60 static void tif_32sto1u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
63 for (i = 0; i < (length & -(OPJ_SIZE_T)8U); i+=8U) {
64 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
65 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
66 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
67 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
68 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
69 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
70 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
71 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
73 *pDst++ = (src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7;
77 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
87 src1 = (OPJ_UINT32)pSrc[i+1];
89 src2 = (OPJ_UINT32)pSrc[i+2];
91 src3 = (OPJ_UINT32)pSrc[i+3];
93 src4 = (OPJ_UINT32)pSrc[i+4];
95 src5 = (OPJ_UINT32)pSrc[i+5];
97 src6 = (OPJ_UINT32)pSrc[i+6];
104 *pDst++ = (src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1);
108 static void tif_32sto2u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
111 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
112 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
113 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
114 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
115 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
117 *pDst++ = (src0 << 6) | (src1 << 4) | (src2 << 2) | src3;
121 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
122 OPJ_UINT32 src1 = 0U;
123 OPJ_UINT32 src2 = 0U;
124 length = length & 3U;
127 src1 = (OPJ_UINT32)pSrc[i+1];
129 src2 = (OPJ_UINT32)pSrc[i+2];
132 *pDst++ = (src0 << 6) | (src1 << 4) | (src2 << 2);
136 static void tif_32sto4u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
139 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
140 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
141 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
143 *pDst++ = (src0 << 4) | src1;
147 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
148 *pDst++ = (src0 << 4);
152 static void tif_32sto6u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
155 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
156 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
157 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
158 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
159 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
161 *pDst++ = (src0 << 2) | (src1 >> 4);
162 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 2);
163 *pDst++ = ((src2 & 0x3U) << 6) | src3;
167 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
168 OPJ_UINT32 src1 = 0U;
169 OPJ_UINT32 src2 = 0U;
170 length = length & 3U;
173 src1 = (OPJ_UINT32)pSrc[i+1];
175 src2 = (OPJ_UINT32)pSrc[i+2];
178 *pDst++ = (src0 << 2) | (src1 >> 4);
180 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 2);
182 *pDst++ = ((src2 & 0x3U) << 6);
187 static void tif_32sto8u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
190 for (i = 0; i < length; ++i) {
191 pDst[i] = (OPJ_BYTE)pSrc[i];
194 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
197 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
198 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
199 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
200 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
201 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
204 *pDst++ = ((src0 & 0x3U) << 6) | (src1 >> 4);
205 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 6);
206 *pDst++ = ((src2 & 0x3FU) << 2) | (src3 >> 8);
211 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
212 OPJ_UINT32 src1 = 0U;
213 OPJ_UINT32 src2 = 0U;
214 length = length & 3U;
217 src1 = (OPJ_UINT32)pSrc[i+1];
219 src2 = (OPJ_UINT32)pSrc[i+2];
223 *pDst++ = ((src0 & 0x3U) << 6) | (src1 >> 4);
225 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 6);
227 *pDst++ = ((src2 & 0x3FU) << 2);
232 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
235 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
236 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
237 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
240 *pDst++ = ((src0 & 0xFU) << 4) | (src1 >> 8);
245 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
247 *pDst++ = ((src0 & 0xFU) << 4);
250 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
253 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
254 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
255 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
256 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
257 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
260 *pDst++ = ((src0 & 0x3FU) << 2) | (src1 >> 12);
262 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 10);
264 *pDst++ = ((src2 & 0x3U) << 6) | (src3 >> 8);
269 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
270 OPJ_UINT32 src1 = 0U;
271 OPJ_UINT32 src2 = 0U;
272 length = length & 3U;
275 src1 = (OPJ_UINT32)pSrc[i+1];
277 src2 = (OPJ_UINT32)pSrc[i+2];
281 *pDst++ = ((src0 & 0x3FU) << 2) | (src1 >> 12);
284 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 10);
287 *pDst++ = ((src2 & 0x3U) << 6);
292 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
295 for (i = 0; i < length; ++i) {
296 pDst[i] = (OPJ_UINT16)pSrc[i];
300 typedef void (* convert_32s_PXCX)(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust);
301 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
304 const OPJ_INT32* pSrc0 = pSrc[0];
306 for (i = 0; i < length; i++) {
307 pDst[i] = pSrc0[i] + adjust;
310 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
313 const OPJ_INT32* pSrc0 = pSrc[0];
314 const OPJ_INT32* pSrc1 = pSrc[1];
316 for (i = 0; i < length; i++) {
317 pDst[2*i+0] = pSrc0[i] + adjust;
318 pDst[2*i+1] = pSrc1[i] + adjust;
321 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
324 const OPJ_INT32* pSrc0 = pSrc[0];
325 const OPJ_INT32* pSrc1 = pSrc[1];
326 const OPJ_INT32* pSrc2 = pSrc[2];
328 for (i = 0; i < length; i++) {
329 pDst[3*i+0] = pSrc0[i] + adjust;
330 pDst[3*i+1] = pSrc1[i] + adjust;
331 pDst[3*i+2] = pSrc2[i] + adjust;
334 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
337 const OPJ_INT32* pSrc0 = pSrc[0];
338 const OPJ_INT32* pSrc1 = pSrc[1];
339 const OPJ_INT32* pSrc2 = pSrc[2];
340 const OPJ_INT32* pSrc3 = pSrc[3];
342 for (i = 0; i < length; i++) {
343 pDst[4*i+0] = pSrc0[i] + adjust;
344 pDst[4*i+1] = pSrc1[i] + adjust;
345 pDst[4*i+2] = pSrc2[i] + adjust;
346 pDst[4*i+3] = pSrc3[i] + adjust;
350 int imagetotif(opj_image_t * image, const char *outfile)
353 int bps,adjust, sgnd;
359 OPJ_UINT32 i, numcomps;
360 OPJ_SIZE_T rowStride;
361 OPJ_INT32* buffer32s = NULL;
362 OPJ_INT32 const* planes[4];
363 convert_32s_PXCX cvtPxToCx = NULL;
364 tif_32stoX cvt32sToTif = NULL;
367 bps = (int)image->comps[0].prec;
368 planes[0] = image->comps[0].data;
370 numcomps = image->numcomps;
373 tiPhoto = PHOTOMETRIC_RGB;
378 tiPhoto = PHOTOMETRIC_MINISBLACK;
380 for (i = 1U; i < numcomps; ++i) {
381 if (image->comps[0].dx != image->comps[i].dx) {
384 if (image->comps[0].dy != image->comps[i].dy) {
387 if (image->comps[0].prec != image->comps[i].prec) {
390 if (image->comps[0].sgnd != image->comps[i].sgnd) {
393 planes[i] = image->comps[i].data;
396 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
397 fprintf(stderr,"\tAborting\n");
401 if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
404 fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps);
405 fprintf(stderr,"\tAborting\n");
408 tif = TIFFOpen(outfile, "wb");
411 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
414 for (i = 0U; i < numcomps; ++i) {
415 clip_component(&(image->comps[i]), image->comps[0].prec);
419 cvtPxToCx = convert_32s_P1C1;
422 cvtPxToCx = convert_32s_P2C2;
425 cvtPxToCx = convert_32s_P3C3;
428 cvtPxToCx = convert_32s_P4C4;
436 cvt32sToTif = tif_32sto1u;
439 cvt32sToTif = tif_32sto2u;
442 cvt32sToTif = tif_32sto4u;
445 cvt32sToTif = tif_32sto6u;
448 cvt32sToTif = tif_32sto8u;
451 cvt32sToTif = tif_32sto10u;
454 cvt32sToTif = tif_32sto12u;
457 cvt32sToTif = tif_32sto14u;
460 cvt32sToTif = (tif_32stoX)tif_32sto16u;
466 sgnd = (int)image->comps[0].sgnd;
467 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
468 width = (int)image->comps[0].w;
469 height = (int)image->comps[0].h;
471 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
472 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
473 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
474 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
475 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
476 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
477 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
478 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
479 strip_size = TIFFStripSize(tif);
480 rowStride = ((OPJ_SIZE_T)width * numcomps * bps + 7U) / 8U;
481 if (rowStride != (OPJ_SIZE_T)strip_size) {
482 fprintf(stderr, "Invalid TIFF strip size\n");
486 buf = _TIFFmalloc(strip_size);
491 buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
492 if (buffer32s == NULL) {
498 for (i = 0; i < image->comps[0].h; ++i) {
499 cvtPxToCx(planes, buffer32s, width, adjust);
500 cvt32sToTif(buffer32s, buf, width * numcomps);
501 (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
507 _TIFFfree((void*)buf);
514 typedef void (* tif_Xto32s)(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length);
516 static void tif_1uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
519 for (i = 0; i < (length & -(OPJ_SIZE_T)8U); i+=8U) {
520 OPJ_UINT8 val = *pSrc++;
521 pDst[i+0] = val >> 7;
522 pDst[i+1] = (val >> 6) & 0x1U;
523 pDst[i+2] = (val >> 5) & 0x1U;
524 pDst[i+3] = (val >> 4) & 0x1U;
525 pDst[i+4] = (val >> 3) & 0x1U;
526 pDst[i+5] = (val >> 2) & 0x1U;
527 pDst[i+6] = (val >> 1) & 0x1U;
528 pDst[i+7] = val & 0x1U;
531 OPJ_UINT8 val = *pSrc++;
532 length = length & 7U;
533 pDst[i+0] = val >> 7;
536 pDst[i+1] = (val >> 6) & 0x1U;
538 pDst[i+2] = (val >> 5) & 0x1U;
540 pDst[i+3] = (val >> 4) & 0x1U;
542 pDst[i+4] = (val >> 3) & 0x1U;
544 pDst[i+5] = (val >> 2) & 0x1U;
546 pDst[i+6] = (val >> 1) & 0x1U;
555 static void tif_2uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
558 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
559 OPJ_UINT8 val = *pSrc++;
560 pDst[i+0] = val >> 6;
561 pDst[i+1] = (val >> 4) & 0x3U;
562 pDst[i+2] = (val >> 2) & 0x3U;
563 pDst[i+3] = val & 0x3U;
566 OPJ_UINT8 val = *pSrc++;
567 length = length & 3U;
568 pDst[i+0] = val >> 6;
571 pDst[i+1] = (val >> 4) & 0x3U;
573 pDst[i+2] = (val >> 2) & 0x3U;
579 static void tif_4uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
582 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
583 OPJ_UINT8 val = *pSrc++;
584 pDst[i+0] = val >> 4;
585 pDst[i+1] = val & 0xFU;
588 OPJ_UINT8 val = *pSrc++;
589 pDst[i+0] = val >> 4;
592 static void tif_6uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
595 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
596 OPJ_UINT8 val0 = *pSrc++;
597 OPJ_UINT8 val1 = *pSrc++;
598 OPJ_UINT8 val2 = *pSrc++;
599 pDst[i+0] = val0 >> 2;
600 pDst[i+1] = ((val0 & 0x3U) << 4) | (val1 >> 4);
601 pDst[i+2] = ((val1 & 0xFU) << 2) | (val2 >> 6);
602 pDst[i+3] = val2 & 0x3FU;
606 OPJ_UINT8 val0 = *pSrc++;
607 length = length & 3U;
608 pDst[i+0] = val0 >> 2;
611 OPJ_UINT8 val1 = *pSrc++;
612 pDst[i+1] = ((val0 & 0x3U) << 4) | (val1 >> 4);
614 OPJ_UINT8 val2 = *pSrc++;
615 pDst[i+2] = ((val1 & 0xFU) << 2) | (val2 >> 6);
620 static void tif_8uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
623 for (i = 0; i < length; ++i) {
627 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
630 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
631 OPJ_INT32 val0 = *pSrc++;
632 OPJ_INT32 val1 = *pSrc++;
633 OPJ_INT32 val2 = *pSrc++;
634 OPJ_INT32 val3 = *pSrc++;
635 OPJ_INT32 val4 = *pSrc++;
637 pDst[i+0] = (val0 << 2) | (val1 >> 6);
638 pDst[i+1] = ((val1 & 0x3FU) << 4) | (val2 >> 4);
639 pDst[i+2] = ((val2 & 0xFU) << 6) | (val3 >> 2);
640 pDst[i+3] = ((val3 & 0x3U) << 8) | val4;
644 OPJ_INT32 val0 = *pSrc++;
645 OPJ_INT32 val1 = *pSrc++;
646 length = length & 3U;
647 pDst[i+0] = (val0 << 2) | (val1 >> 6);
650 OPJ_INT32 val2 = *pSrc++;
651 pDst[i+1] = ((val1 & 0x3FU) << 4) | (val2 >> 4);
653 OPJ_INT32 val3 = *pSrc++;
654 pDst[i+2] = ((val2 & 0xFU) << 6) | (val3 >> 2);
659 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
662 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
663 OPJ_INT32 val0 = *pSrc++;
664 OPJ_INT32 val1 = *pSrc++;
665 OPJ_INT32 val2 = *pSrc++;
667 pDst[i+0] = (val0 << 4) | (val1 >> 4);
668 pDst[i+1] = ((val1 & 0xFU) << 8) | val2;
671 OPJ_INT32 val0 = *pSrc++;
672 OPJ_INT32 val1 = *pSrc++;
673 pDst[i+0] = (val0 << 4) | (val1 >> 4);
676 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
679 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
680 OPJ_INT32 val0 = *pSrc++;
681 OPJ_INT32 val1 = *pSrc++;
682 OPJ_INT32 val2 = *pSrc++;
683 OPJ_INT32 val3 = *pSrc++;
684 OPJ_INT32 val4 = *pSrc++;
685 OPJ_INT32 val5 = *pSrc++;
686 OPJ_INT32 val6 = *pSrc++;
688 pDst[i+0] = (val0 << 6) | (val1 >> 2);
689 pDst[i+1] = ((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4);
690 pDst[i+2] = ((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6);
691 pDst[i+3] = ((val5 & 0x3FU) << 8) | val6;
695 OPJ_INT32 val0 = *pSrc++;
696 OPJ_INT32 val1 = *pSrc++;
697 length = length & 3U;
698 pDst[i+0] = (val0 << 6) | (val1 >> 2);
701 OPJ_INT32 val2 = *pSrc++;
702 OPJ_INT32 val3 = *pSrc++;
703 pDst[i+1] = ((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4);
705 OPJ_INT32 val4 = *pSrc++;
706 OPJ_INT32 val5 = *pSrc++;
707 pDst[i+2] = ((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6);
713 static void tif_16uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
716 for (i = 0; i < length; i++) {
717 OPJ_INT32 val0 = *pSrc++;
718 OPJ_INT32 val1 = *pSrc++;
719 #ifdef OPJ_BIG_ENDIAN
720 pDst[i] = (val0 << 8) | val1;
722 pDst[i] = (val1 << 8) | val0;
727 /* seems that libtiff decodes this to machine endianness */
728 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
731 for (i = 0; i < length; i++) {
737 typedef void (* convert_32s_CXPX)(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length);
738 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
740 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
742 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
745 OPJ_INT32* pDst0 = pDst[0];
746 OPJ_INT32* pDst1 = pDst[1];
748 for (i = 0; i < length; i++) {
749 pDst0[i] = pSrc[2*i+0];
750 pDst1[i] = pSrc[2*i+1];
753 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
756 OPJ_INT32* pDst0 = pDst[0];
757 OPJ_INT32* pDst1 = pDst[1];
758 OPJ_INT32* pDst2 = pDst[2];
760 for (i = 0; i < length; i++) {
761 pDst0[i] = pSrc[3*i+0];
762 pDst1[i] = pSrc[3*i+1];
763 pDst2[i] = pSrc[3*i+2];
766 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
769 OPJ_INT32* pDst0 = pDst[0];
770 OPJ_INT32* pDst1 = pDst[1];
771 OPJ_INT32* pDst2 = pDst[2];
772 OPJ_INT32* pDst3 = pDst[3];
774 for (i = 0; i < length; i++) {
775 pDst0[i] = pSrc[4*i+0];
776 pDst1[i] = pSrc[4*i+1];
777 pDst2[i] = pSrc[4*i+2];
778 pDst3[i] = pSrc[4*i+3];
784 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
785 * CINEMA : 12 bit precision
787 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
789 int subsampling_dx = parameters->subsampling_dx;
790 int subsampling_dy = parameters->subsampling_dy;
795 int j, currentPlane, numcomps = 0, w, h;
796 OPJ_COLOR_SPACE color_space;
797 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
798 opj_image_t *image = NULL;
800 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
801 unsigned int tiWidth, tiHeight;
802 OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
803 tif_Xto32s cvtTifTo32s = NULL;
804 convert_32s_CXPX cvtCxToPx = NULL;
805 OPJ_INT32* buffer32s = NULL;
806 OPJ_INT32* planes[4];
807 OPJ_SIZE_T rowStride;
809 tif = TIFFOpen(filename, "r");
813 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
816 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
817 tiWidth = tiHeight = 0;
819 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
820 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
821 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
822 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
823 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
824 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
825 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
829 if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) tiBps = 0U;
830 if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) tiPhoto = 0;
832 if( !tiBps || !tiPhoto)
835 fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
838 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
839 " and GRAY(A) has been implemented\n",(int) tiPhoto);
841 fprintf(stderr,"\tAborting\n");
848 cvtTifTo32s = tif_1uto32s;
851 cvtTifTo32s = tif_2uto32s;
854 cvtTifTo32s = tif_4uto32s;
857 cvtTifTo32s = tif_6uto32s;
860 cvtTifTo32s = tif_8uto32s;
863 cvtTifTo32s = tif_10uto32s;
866 cvtTifTo32s = tif_12uto32s;
869 cvtTifTo32s = tif_14uto32s;
872 cvtTifTo32s = (tif_Xto32s)tif_16uto32s;
879 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
883 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
884 &extrasamples, &sampleinfo);
886 if(extrasamples >= 1)
888 switch(sampleinfo[0])
890 case EXTRASAMPLE_UNSPECIFIED:
891 /* Workaround for some images without correct info about alpha channel
897 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
898 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
903 else /* extrasamples == 0 */
904 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
907 /* initialize image components */
908 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
910 if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
911 fprintf(stdout,"WARNING:\n"
912 "Input image bitdepth is %d bits\n"
913 "TIF conversion has automatically rescaled to 12-bits\n"
914 "to comply with cinema profiles.\n",
920 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
922 numcomps = 3 + has_alpha;
923 color_space = OPJ_CLRSPC_SRGB;
925 else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
927 numcomps = 1 + has_alpha;
928 color_space = OPJ_CLRSPC_GRAY;
933 cvtCxToPx = convert_32s_C1P1;
936 cvtCxToPx = convert_32s_C2P2;
939 cvtCxToPx = convert_32s_C3P3;
942 cvtCxToPx = convert_32s_C4P4;
948 if (tiPC == PLANARCONFIG_SEPARATE) {
949 cvtCxToPx = convert_32s_C1P1; /* override */
950 tiSpp = 1U; /* consider only one sample per plane */
953 for(j = 0; j < numcomps; j++)
955 cmptparm[j].prec = tiBps;
956 cmptparm[j].bpp = tiBps;
957 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
958 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
959 cmptparm[j].w = (OPJ_UINT32)w;
960 cmptparm[j].h = (OPJ_UINT32)h;
963 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
969 /* set image offset and reference grid */
970 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
971 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
972 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
973 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
974 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
975 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
977 for(j = 0; j < numcomps; j++)
979 planes[j] = image->comps[j].data;
982 strip_size = TIFFStripSize(tif);
984 buf = _TIFFmalloc(strip_size);
987 opj_image_destroy(image);
990 rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
991 buffer32s = malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
992 if (buffer32s == NULL) {
995 opj_image_destroy(image);
1003 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
1005 /* Read the Image components */
1006 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
1008 const OPJ_UINT8 *dat8;
1011 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1012 dat8 = (const OPJ_UINT8*)buf;
1014 while (ssize >= rowStride) {
1015 cvtTifTo32s(dat8, buffer32s, w * tiSpp);
1016 cvtCxToPx(buffer32s, planes, w);
1027 } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
1034 for (j=0; j < numcomps; ++j) {
1035 scale_component(&(image->comps[j]), 12);