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 * Copyright (c) 2015, Matthieu Darbois
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_apps_config.h"
46 #ifndef OPJ_HAVE_LIBTIFF
47 # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
48 #endif /* OPJ_HAVE_LIBTIFF */
54 /* -->> -->> -->> -->>
58 <<-- <<-- <<-- <<-- */
59 typedef void (* tif_32stoX)(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length);
61 static void tif_32sto1u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
64 for (i = 0; i < (length & -(OPJ_SIZE_T)8U); i+=8U) {
65 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
66 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
67 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
68 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
69 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
70 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
71 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
72 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
74 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
78 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
88 src1 = (OPJ_UINT32)pSrc[i+1];
90 src2 = (OPJ_UINT32)pSrc[i+2];
92 src3 = (OPJ_UINT32)pSrc[i+3];
94 src4 = (OPJ_UINT32)pSrc[i+4];
96 src5 = (OPJ_UINT32)pSrc[i+5];
98 src6 = (OPJ_UINT32)pSrc[i+6];
105 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1));
109 static void tif_32sto2u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
112 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
113 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
114 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
115 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
116 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
118 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
122 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
123 OPJ_UINT32 src1 = 0U;
124 OPJ_UINT32 src2 = 0U;
125 length = length & 3U;
128 src1 = (OPJ_UINT32)pSrc[i+1];
130 src2 = (OPJ_UINT32)pSrc[i+2];
133 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
137 static void tif_32sto4u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
140 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
141 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
142 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
144 *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
148 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
149 *pDst++ = (OPJ_BYTE)((src0 << 4));
153 static void tif_32sto6u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
156 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
157 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
158 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
159 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
160 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
162 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
163 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
164 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
168 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
169 OPJ_UINT32 src1 = 0U;
170 OPJ_UINT32 src2 = 0U;
171 length = length & 3U;
174 src1 = (OPJ_UINT32)pSrc[i+1];
176 src2 = (OPJ_UINT32)pSrc[i+2];
179 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
181 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
183 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
188 static void tif_32sto8u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
191 for (i = 0; i < length; ++i) {
192 pDst[i] = (OPJ_BYTE)pSrc[i];
195 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
198 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
199 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
200 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
201 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
202 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
204 *pDst++ = (OPJ_BYTE)(src0 >> 2);
205 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
206 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
207 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8));
208 *pDst++ = (OPJ_BYTE)(src3);
212 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
213 OPJ_UINT32 src1 = 0U;
214 OPJ_UINT32 src2 = 0U;
215 length = length & 3U;
218 src1 = (OPJ_UINT32)pSrc[i+1];
220 src2 = (OPJ_UINT32)pSrc[i+2];
223 *pDst++ = (OPJ_BYTE)(src0 >> 2);
224 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
226 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
228 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
233 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
236 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
237 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
238 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
240 *pDst++ = (OPJ_BYTE)(src0 >> 4);
241 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
242 *pDst++ = (OPJ_BYTE)(src1);
246 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
247 *pDst++ = (OPJ_BYTE)(src0 >> 4);
248 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
251 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
254 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
255 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
256 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
257 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
258 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
260 *pDst++ = (OPJ_BYTE)(src0 >> 6);
261 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
262 *pDst++ = (OPJ_BYTE)(src1 >> 4);
263 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
264 *pDst++ = (OPJ_BYTE)(src2 >> 2);
265 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8));
266 *pDst++ = (OPJ_BYTE)(src3);
270 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
271 OPJ_UINT32 src1 = 0U;
272 OPJ_UINT32 src2 = 0U;
273 length = length & 3U;
276 src1 = (OPJ_UINT32)pSrc[i+1];
278 src2 = (OPJ_UINT32)pSrc[i+2];
281 *pDst++ = (OPJ_BYTE)(src0 >> 6);
282 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
284 *pDst++ = (OPJ_BYTE)(src1 >> 4);
285 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
287 *pDst++ = (OPJ_BYTE)(src2 >> 2);
288 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
293 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
296 for (i = 0; i < length; ++i) {
297 pDst[i] = (OPJ_UINT16)pSrc[i];
301 typedef void (* convert_32s_PXCX)(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust);
302 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
305 const OPJ_INT32* pSrc0 = pSrc[0];
307 for (i = 0; i < length; i++) {
308 pDst[i] = pSrc0[i] + adjust;
311 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
314 const OPJ_INT32* pSrc0 = pSrc[0];
315 const OPJ_INT32* pSrc1 = pSrc[1];
317 for (i = 0; i < length; i++) {
318 pDst[2*i+0] = pSrc0[i] + adjust;
319 pDst[2*i+1] = pSrc1[i] + adjust;
322 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
325 const OPJ_INT32* pSrc0 = pSrc[0];
326 const OPJ_INT32* pSrc1 = pSrc[1];
327 const OPJ_INT32* pSrc2 = pSrc[2];
329 for (i = 0; i < length; i++) {
330 pDst[3*i+0] = pSrc0[i] + adjust;
331 pDst[3*i+1] = pSrc1[i] + adjust;
332 pDst[3*i+2] = pSrc2[i] + adjust;
335 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
338 const OPJ_INT32* pSrc0 = pSrc[0];
339 const OPJ_INT32* pSrc1 = pSrc[1];
340 const OPJ_INT32* pSrc2 = pSrc[2];
341 const OPJ_INT32* pSrc3 = pSrc[3];
343 for (i = 0; i < length; i++) {
344 pDst[4*i+0] = pSrc0[i] + adjust;
345 pDst[4*i+1] = pSrc1[i] + adjust;
346 pDst[4*i+2] = pSrc2[i] + adjust;
347 pDst[4*i+3] = pSrc3[i] + adjust;
351 int imagetotif(opj_image_t * image, const char *outfile)
354 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;
366 bps = (int)image->comps[0].prec;
367 planes[0] = image->comps[0].data;
369 numcomps = image->numcomps;
372 tiPhoto = PHOTOMETRIC_RGB;
377 tiPhoto = PHOTOMETRIC_MINISBLACK;
379 for (i = 1U; i < numcomps; ++i) {
380 if (image->comps[0].dx != image->comps[i].dx) {
383 if (image->comps[0].dy != image->comps[i].dy) {
386 if (image->comps[0].prec != image->comps[i].prec) {
389 if (image->comps[0].sgnd != image->comps[i].sgnd) {
392 planes[i] = image->comps[i].data;
395 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
396 fprintf(stderr,"\tAborting\n");
400 if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
403 fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps);
404 fprintf(stderr,"\tAborting\n");
407 tif = TIFFOpen(outfile, "wb");
410 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
413 for (i = 0U; i < numcomps; ++i) {
414 clip_component(&(image->comps[i]), image->comps[0].prec);
418 cvtPxToCx = convert_32s_P1C1;
421 cvtPxToCx = convert_32s_P2C2;
424 cvtPxToCx = convert_32s_P3C3;
427 cvtPxToCx = convert_32s_P4C4;
435 cvt32sToTif = tif_32sto1u;
438 cvt32sToTif = tif_32sto2u;
441 cvt32sToTif = tif_32sto4u;
444 cvt32sToTif = tif_32sto6u;
447 cvt32sToTif = tif_32sto8u;
450 cvt32sToTif = tif_32sto10u;
453 cvt32sToTif = tif_32sto12u;
456 cvt32sToTif = tif_32sto14u;
459 cvt32sToTif = (tif_32stoX)tif_32sto16u;
465 sgnd = (int)image->comps[0].sgnd;
466 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
467 width = (int)image->comps[0].w;
468 height = (int)image->comps[0].h;
470 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
471 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
472 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
473 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
474 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
475 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
476 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
477 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
478 strip_size = TIFFStripSize(tif);
479 rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
480 if (rowStride != (OPJ_SIZE_T)strip_size) {
481 fprintf(stderr, "Invalid TIFF strip size\n");
485 buf = _TIFFmalloc(strip_size);
490 buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
491 if (buffer32s == NULL) {
497 for (i = 0; i < image->comps[0].h; ++i) {
498 cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
499 cvt32sToTif(buffer32s, buf, (OPJ_SIZE_T)width * numcomps);
500 (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
506 _TIFFfree((void*)buf);
513 typedef void (* tif_Xto32s)(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length);
515 static void tif_1uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
518 for (i = 0; i < (length & -(OPJ_SIZE_T)8U); i+=8U) {
519 OPJ_UINT32 val = *pSrc++;
520 pDst[i+0] = (OPJ_INT32)( val >> 7);
521 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
522 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
523 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
524 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
525 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
526 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
527 pDst[i+7] = (OPJ_INT32)(val & 0x1U);
530 OPJ_UINT32 val = *pSrc++;
531 length = length & 7U;
532 pDst[i+0] = (OPJ_INT32)(val >> 7);
535 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
537 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
539 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
541 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
543 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
545 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
554 static void tif_2uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
557 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
558 OPJ_UINT32 val = *pSrc++;
559 pDst[i+0] = (OPJ_INT32)( val >> 6);
560 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
561 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
562 pDst[i+3] = (OPJ_INT32)(val & 0x3U);
565 OPJ_UINT32 val = *pSrc++;
566 length = length & 3U;
567 pDst[i+0] = (OPJ_INT32)(val >> 6);
570 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
572 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
578 static void tif_4uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
581 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
582 OPJ_UINT32 val = *pSrc++;
583 pDst[i+0] = (OPJ_INT32)(val >> 4);
584 pDst[i+1] = (OPJ_INT32)(val & 0xFU);
587 OPJ_UINT8 val = *pSrc++;
588 pDst[i+0] = (OPJ_INT32)(val >> 4);
591 static void tif_6uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
594 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
595 OPJ_UINT32 val0 = *pSrc++;
596 OPJ_UINT32 val1 = *pSrc++;
597 OPJ_UINT32 val2 = *pSrc++;
598 pDst[i+0] = (OPJ_INT32)(val0 >> 2);
599 pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
600 pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
601 pDst[i+3] = (OPJ_INT32)(val2 & 0x3FU);
605 OPJ_UINT32 val0 = *pSrc++;
606 length = length & 3U;
607 pDst[i+0] = (OPJ_INT32)(val0 >> 2);
610 OPJ_UINT32 val1 = *pSrc++;
611 pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
613 OPJ_UINT32 val2 = *pSrc++;
614 pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
619 static void tif_8uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
622 for (i = 0; i < length; ++i) {
626 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
629 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
630 OPJ_UINT32 val0 = *pSrc++;
631 OPJ_UINT32 val1 = *pSrc++;
632 OPJ_UINT32 val2 = *pSrc++;
633 OPJ_UINT32 val3 = *pSrc++;
634 OPJ_UINT32 val4 = *pSrc++;
636 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
637 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
638 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
639 pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4);
643 OPJ_UINT32 val0 = *pSrc++;
644 OPJ_UINT32 val1 = *pSrc++;
645 length = length & 3U;
646 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
649 OPJ_UINT32 val2 = *pSrc++;
650 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
652 OPJ_UINT32 val3 = *pSrc++;
653 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
658 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
661 for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
662 OPJ_UINT32 val0 = *pSrc++;
663 OPJ_UINT32 val1 = *pSrc++;
664 OPJ_UINT32 val2 = *pSrc++;
666 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
667 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
670 OPJ_UINT32 val0 = *pSrc++;
671 OPJ_UINT32 val1 = *pSrc++;
672 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
675 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
678 for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
679 OPJ_UINT32 val0 = *pSrc++;
680 OPJ_UINT32 val1 = *pSrc++;
681 OPJ_UINT32 val2 = *pSrc++;
682 OPJ_UINT32 val3 = *pSrc++;
683 OPJ_UINT32 val4 = *pSrc++;
684 OPJ_UINT32 val5 = *pSrc++;
685 OPJ_UINT32 val6 = *pSrc++;
687 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
688 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
689 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
690 pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6);
694 OPJ_UINT32 val0 = *pSrc++;
695 OPJ_UINT32 val1 = *pSrc++;
696 length = length & 3U;
697 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
700 OPJ_UINT32 val2 = *pSrc++;
701 OPJ_UINT32 val3 = *pSrc++;
702 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
704 OPJ_UINT32 val4 = *pSrc++;
705 OPJ_UINT32 val5 = *pSrc++;
706 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
712 static void tif_16uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
715 for (i = 0; i < length; i++) {
716 OPJ_UINT32 val0 = *pSrc++;
717 OPJ_UINT32 val1 = *pSrc++;
718 #ifdef OPJ_BIG_ENDIAN
719 pDst[i] = (OPJ_INT32)((val0 << 8) | val1);
721 pDst[i] = (OPJ_INT32)((val1 << 8) | val0);
726 /* seems that libtiff decodes this to machine endianness */
727 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
730 for (i = 0; i < length; i++) {
736 typedef void (* convert_32s_CXPX)(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length);
737 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
739 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
741 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
744 OPJ_INT32* pDst0 = pDst[0];
745 OPJ_INT32* pDst1 = pDst[1];
747 for (i = 0; i < length; i++) {
748 pDst0[i] = pSrc[2*i+0];
749 pDst1[i] = pSrc[2*i+1];
752 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
755 OPJ_INT32* pDst0 = pDst[0];
756 OPJ_INT32* pDst1 = pDst[1];
757 OPJ_INT32* pDst2 = pDst[2];
759 for (i = 0; i < length; i++) {
760 pDst0[i] = pSrc[3*i+0];
761 pDst1[i] = pSrc[3*i+1];
762 pDst2[i] = pSrc[3*i+2];
765 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
768 OPJ_INT32* pDst0 = pDst[0];
769 OPJ_INT32* pDst1 = pDst[1];
770 OPJ_INT32* pDst2 = pDst[2];
771 OPJ_INT32* pDst3 = pDst[3];
773 for (i = 0; i < length; i++) {
774 pDst0[i] = pSrc[4*i+0];
775 pDst1[i] = pSrc[4*i+1];
776 pDst2[i] = pSrc[4*i+2];
777 pDst3[i] = pSrc[4*i+3];
783 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
784 * CINEMA : 12 bit precision
786 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
788 int subsampling_dx = parameters->subsampling_dx;
789 int subsampling_dy = parameters->subsampling_dy;
794 int j, currentPlane, numcomps = 0, w, h;
795 OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
796 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
797 opj_image_t *image = NULL;
799 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
800 unsigned int tiWidth, tiHeight;
801 OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
802 tif_Xto32s cvtTifTo32s = NULL;
803 convert_32s_CXPX cvtCxToPx = NULL;
804 OPJ_INT32* buffer32s = NULL;
805 OPJ_INT32* planes[4];
806 OPJ_SIZE_T rowStride;
808 tif = TIFFOpen(filename, "r");
812 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
815 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
816 tiWidth = tiHeight = 0;
818 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
819 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
820 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
821 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
822 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
823 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
824 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
828 if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) {
829 fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
830 fprintf(stderr,"\tAborting\n");
834 if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
835 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
836 fprintf(stderr,"\tAborting\n");
843 cvtTifTo32s = tif_1uto32s;
846 cvtTifTo32s = tif_2uto32s;
849 cvtTifTo32s = tif_4uto32s;
852 cvtTifTo32s = tif_6uto32s;
855 cvtTifTo32s = tif_8uto32s;
858 cvtTifTo32s = tif_10uto32s;
861 cvtTifTo32s = tif_12uto32s;
864 cvtTifTo32s = tif_14uto32s;
867 cvtTifTo32s = (tif_Xto32s)tif_16uto32s;
874 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
878 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
879 &extrasamples, &sampleinfo);
881 if(extrasamples >= 1)
883 switch(sampleinfo[0])
885 case EXTRASAMPLE_UNSPECIFIED:
886 /* Workaround for some images without correct info about alpha channel
892 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
893 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
898 else /* extrasamples == 0 */
899 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
902 /* initialize image components */
903 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
905 if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
906 fprintf(stdout,"WARNING:\n"
907 "Input image bitdepth is %d bits\n"
908 "TIF conversion has automatically rescaled to 12-bits\n"
909 "to comply with cinema profiles.\n",
915 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
917 numcomps = 3 + has_alpha;
918 color_space = OPJ_CLRSPC_SRGB;
920 else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
922 numcomps = 1 + has_alpha;
923 color_space = OPJ_CLRSPC_GRAY;
928 cvtCxToPx = convert_32s_C1P1;
931 cvtCxToPx = convert_32s_C2P2;
934 cvtCxToPx = convert_32s_C3P3;
937 cvtCxToPx = convert_32s_C4P4;
943 if (tiPC == PLANARCONFIG_SEPARATE) {
944 cvtCxToPx = convert_32s_C1P1; /* override */
945 tiSpp = 1U; /* consider only one sample per plane */
948 for(j = 0; j < numcomps; j++)
950 cmptparm[j].prec = tiBps;
951 cmptparm[j].bpp = tiBps;
952 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
953 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
954 cmptparm[j].w = (OPJ_UINT32)w;
955 cmptparm[j].h = (OPJ_UINT32)h;
958 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
964 /* set image offset and reference grid */
965 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
966 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
967 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
968 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
969 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
970 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
972 for(j = 0; j < numcomps; j++)
974 planes[j] = image->comps[j].data;
976 image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
978 strip_size = TIFFStripSize(tif);
980 buf = _TIFFmalloc(strip_size);
983 opj_image_destroy(image);
986 rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
987 buffer32s = malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
988 if (buffer32s == NULL) {
991 opj_image_destroy(image);
999 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
1001 /* Read the Image components */
1002 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
1004 const OPJ_UINT8 *dat8;
1007 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1008 dat8 = (const OPJ_UINT8*)buf;
1010 while (ssize >= rowStride) {
1011 cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
1012 cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
1023 } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
1030 for (j=0; j < numcomps; ++j) {
1031 scale_component(&(image->comps[j]), 12);