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 <<-- <<-- <<-- <<-- */
60 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
63 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
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];
69 *pDst++ = (OPJ_BYTE)(src0 >> 2);
70 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
71 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
72 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8));
73 *pDst++ = (OPJ_BYTE)(src3);
77 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
83 src1 = (OPJ_UINT32)pSrc[i+1];
85 src2 = (OPJ_UINT32)pSrc[i+2];
88 *pDst++ = (OPJ_BYTE)(src0 >> 2);
89 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
91 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
93 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
98 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
101 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
102 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
103 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
105 *pDst++ = (OPJ_BYTE)(src0 >> 4);
106 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
107 *pDst++ = (OPJ_BYTE)(src1);
111 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
112 *pDst++ = (OPJ_BYTE)(src0 >> 4);
113 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
116 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
119 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
120 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
121 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
122 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
123 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
125 *pDst++ = (OPJ_BYTE)(src0 >> 6);
126 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
127 *pDst++ = (OPJ_BYTE)(src1 >> 4);
128 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
129 *pDst++ = (OPJ_BYTE)(src2 >> 2);
130 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8));
131 *pDst++ = (OPJ_BYTE)(src3);
135 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
136 OPJ_UINT32 src1 = 0U;
137 OPJ_UINT32 src2 = 0U;
138 length = length & 3U;
141 src1 = (OPJ_UINT32)pSrc[i+1];
143 src2 = (OPJ_UINT32)pSrc[i+2];
146 *pDst++ = (OPJ_BYTE)(src0 >> 6);
147 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
149 *pDst++ = (OPJ_BYTE)(src1 >> 4);
150 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
152 *pDst++ = (OPJ_BYTE)(src2 >> 2);
153 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
158 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
161 for (i = 0; i < length; ++i) {
162 pDst[i] = (OPJ_UINT16)pSrc[i];
166 int imagetotif(opj_image_t * image, const char *outfile)
169 int bps,adjust, sgnd;
174 OPJ_UINT32 i, numcomps;
175 OPJ_SIZE_T rowStride;
176 OPJ_INT32* buffer32s = NULL;
177 OPJ_INT32 const* planes[4];
178 convert_32s_PXCX cvtPxToCx = NULL;
179 convert_32sXXx_C1R cvt32sToTif = NULL;
181 bps = (int)image->comps[0].prec;
182 planes[0] = image->comps[0].data;
184 numcomps = image->numcomps;
186 if (image->color_space == OPJ_CLRSPC_CMYK) {
188 fprintf(stderr,"imagetotif: CMYK images shall be composed of at least 4 planes.\n");
189 fprintf(stderr,"\tAborting\n");
192 tiPhoto = PHOTOMETRIC_SEPARATED;
194 numcomps = 4U; /* Alpha not supported */
197 else if (numcomps > 2U) {
198 tiPhoto = PHOTOMETRIC_RGB;
203 tiPhoto = PHOTOMETRIC_MINISBLACK;
205 for (i = 1U; i < numcomps; ++i) {
206 if (image->comps[0].dx != image->comps[i].dx) {
209 if (image->comps[0].dy != image->comps[i].dy) {
212 if (image->comps[0].prec != image->comps[i].prec) {
215 if (image->comps[0].sgnd != image->comps[i].sgnd) {
218 planes[i] = image->comps[i].data;
221 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
222 fprintf(stderr,"\tAborting\n");
226 if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
229 fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps);
230 fprintf(stderr,"\tAborting\n");
233 tif = TIFFOpen(outfile, "wb");
236 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
239 for (i = 0U; i < numcomps; ++i) {
240 clip_component(&(image->comps[i]), image->comps[0].prec);
242 cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
249 cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
252 cvt32sToTif = tif_32sto10u;
255 cvt32sToTif = tif_32sto12u;
258 cvt32sToTif = tif_32sto14u;
261 cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
267 sgnd = (int)image->comps[0].sgnd;
268 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
269 width = (int)image->comps[0].w;
270 height = (int)image->comps[0].h;
272 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
273 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
274 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
275 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
276 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
277 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
278 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
279 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
281 strip_size = TIFFStripSize(tif);
282 rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
283 if (rowStride != (OPJ_SIZE_T)strip_size) {
284 fprintf(stderr, "Invalid TIFF strip size\n");
288 buf = _TIFFmalloc(strip_size);
293 buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
294 if (buffer32s == NULL) {
300 for (i = 0; i < image->comps[0].h; ++i) {
301 cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
302 cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps);
303 (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
309 _TIFFfree((void*)buf);
316 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
319 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
320 OPJ_UINT32 val0 = *pSrc++;
321 OPJ_UINT32 val1 = *pSrc++;
322 OPJ_UINT32 val2 = *pSrc++;
323 OPJ_UINT32 val3 = *pSrc++;
324 OPJ_UINT32 val4 = *pSrc++;
326 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
327 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
328 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
329 pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4);
333 OPJ_UINT32 val0 = *pSrc++;
334 OPJ_UINT32 val1 = *pSrc++;
335 length = length & 3U;
336 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
339 OPJ_UINT32 val2 = *pSrc++;
340 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
342 OPJ_UINT32 val3 = *pSrc++;
343 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
348 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
351 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
352 OPJ_UINT32 val0 = *pSrc++;
353 OPJ_UINT32 val1 = *pSrc++;
354 OPJ_UINT32 val2 = *pSrc++;
356 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
357 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
360 OPJ_UINT32 val0 = *pSrc++;
361 OPJ_UINT32 val1 = *pSrc++;
362 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
365 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
368 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
369 OPJ_UINT32 val0 = *pSrc++;
370 OPJ_UINT32 val1 = *pSrc++;
371 OPJ_UINT32 val2 = *pSrc++;
372 OPJ_UINT32 val3 = *pSrc++;
373 OPJ_UINT32 val4 = *pSrc++;
374 OPJ_UINT32 val5 = *pSrc++;
375 OPJ_UINT32 val6 = *pSrc++;
377 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
378 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
379 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
380 pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6);
384 OPJ_UINT32 val0 = *pSrc++;
385 OPJ_UINT32 val1 = *pSrc++;
386 length = length & 3U;
387 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
390 OPJ_UINT32 val2 = *pSrc++;
391 OPJ_UINT32 val3 = *pSrc++;
392 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
394 OPJ_UINT32 val4 = *pSrc++;
395 OPJ_UINT32 val5 = *pSrc++;
396 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
402 /* seems that libtiff decodes this to machine endianness */
403 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
406 for (i = 0; i < length; i++) {
412 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
413 * CINEMA : 12 bit precision
415 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
417 int subsampling_dx = parameters->subsampling_dx;
418 int subsampling_dy = parameters->subsampling_dy;
423 int j, currentPlane, numcomps = 0, w, h;
424 OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
425 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
426 opj_image_t *image = NULL;
428 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
429 unsigned int tiWidth, tiHeight;
430 OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
431 convert_XXx32s_C1R cvtTifTo32s = NULL;
432 convert_32s_CXPX cvtCxToPx = NULL;
433 OPJ_INT32* buffer32s = NULL;
434 OPJ_INT32* planes[4];
435 OPJ_SIZE_T rowStride;
437 tif = TIFFOpen(filename, "r");
441 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
444 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
445 tiWidth = tiHeight = 0;
447 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
448 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
449 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
450 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
451 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
452 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
453 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
457 if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) {
458 fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
459 fprintf(stderr,"\tAborting\n");
463 if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
464 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
465 fprintf(stderr,"\tAborting\n");
476 cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
478 /* others are specific to TIFF */
480 cvtTifTo32s = tif_10uto32s;
483 cvtTifTo32s = tif_12uto32s;
486 cvtTifTo32s = tif_14uto32s;
489 cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
496 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
500 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
501 &extrasamples, &sampleinfo);
503 if(extrasamples >= 1)
505 switch(sampleinfo[0])
507 case EXTRASAMPLE_UNSPECIFIED:
508 /* Workaround for some images without correct info about alpha channel
514 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
515 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
520 else /* extrasamples == 0 */
521 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
524 /* initialize image components */
525 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
527 if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
528 fprintf(stdout,"WARNING:\n"
529 "Input image bitdepth is %d bits\n"
530 "TIF conversion has automatically rescaled to 12-bits\n"
531 "to comply with cinema profiles.\n",
537 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
539 numcomps = 3 + has_alpha;
540 color_space = OPJ_CLRSPC_SRGB;
542 else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
544 numcomps = 1 + has_alpha;
545 color_space = OPJ_CLRSPC_GRAY;
548 cvtCxToPx = convert_32s_CXPX_LUT[numcomps];
549 if (tiPC == PLANARCONFIG_SEPARATE) {
550 cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */
551 tiSpp = 1U; /* consider only one sample per plane */
554 for(j = 0; j < numcomps; j++)
556 cmptparm[j].prec = tiBps;
557 cmptparm[j].bpp = tiBps;
558 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
559 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
560 cmptparm[j].w = (OPJ_UINT32)w;
561 cmptparm[j].h = (OPJ_UINT32)h;
564 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
570 /* set image offset and reference grid */
571 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
572 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
573 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
574 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
575 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
576 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
578 for(j = 0; j < numcomps; j++)
580 planes[j] = image->comps[j].data;
582 image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
584 strip_size = TIFFStripSize(tif);
586 buf = _TIFFmalloc(strip_size);
589 opj_image_destroy(image);
592 rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
593 buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
594 if (buffer32s == NULL) {
597 opj_image_destroy(image);
605 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
607 /* Read the Image components */
608 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
610 const OPJ_UINT8 *dat8;
613 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
614 dat8 = (const OPJ_UINT8*)buf;
616 while (ssize >= rowStride) {
617 cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
618 cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
629 } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
636 for (j=0; j < numcomps; ++j) {
637 scale_component(&(image->comps[j]), 12);