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;
187 tiPhoto = PHOTOMETRIC_RGB;
192 tiPhoto = PHOTOMETRIC_MINISBLACK;
194 for (i = 1U; i < numcomps; ++i) {
195 if (image->comps[0].dx != image->comps[i].dx) {
198 if (image->comps[0].dy != image->comps[i].dy) {
201 if (image->comps[0].prec != image->comps[i].prec) {
204 if (image->comps[0].sgnd != image->comps[i].sgnd) {
207 planes[i] = image->comps[i].data;
210 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
211 fprintf(stderr,"\tAborting\n");
215 if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
218 fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps);
219 fprintf(stderr,"\tAborting\n");
222 tif = TIFFOpen(outfile, "wb");
225 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
228 for (i = 0U; i < numcomps; ++i) {
229 clip_component(&(image->comps[i]), image->comps[0].prec);
231 cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
238 cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
241 cvt32sToTif = tif_32sto10u;
244 cvt32sToTif = tif_32sto12u;
247 cvt32sToTif = tif_32sto14u;
250 cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
256 sgnd = (int)image->comps[0].sgnd;
257 adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
258 width = (int)image->comps[0].w;
259 height = (int)image->comps[0].h;
261 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
262 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
263 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
264 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
265 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
266 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
267 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
268 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
269 strip_size = TIFFStripSize(tif);
270 rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
271 if (rowStride != (OPJ_SIZE_T)strip_size) {
272 fprintf(stderr, "Invalid TIFF strip size\n");
276 buf = _TIFFmalloc(strip_size);
281 buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
282 if (buffer32s == NULL) {
288 for (i = 0; i < image->comps[0].h; ++i) {
289 cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
290 cvt32sToTif(buffer32s, buf, (OPJ_SIZE_T)width * numcomps);
291 (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
297 _TIFFfree((void*)buf);
304 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
307 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
308 OPJ_UINT32 val0 = *pSrc++;
309 OPJ_UINT32 val1 = *pSrc++;
310 OPJ_UINT32 val2 = *pSrc++;
311 OPJ_UINT32 val3 = *pSrc++;
312 OPJ_UINT32 val4 = *pSrc++;
314 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
315 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
316 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
317 pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4);
321 OPJ_UINT32 val0 = *pSrc++;
322 OPJ_UINT32 val1 = *pSrc++;
323 length = length & 3U;
324 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
327 OPJ_UINT32 val2 = *pSrc++;
328 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
330 OPJ_UINT32 val3 = *pSrc++;
331 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
336 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
339 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
340 OPJ_UINT32 val0 = *pSrc++;
341 OPJ_UINT32 val1 = *pSrc++;
342 OPJ_UINT32 val2 = *pSrc++;
344 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
345 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
348 OPJ_UINT32 val0 = *pSrc++;
349 OPJ_UINT32 val1 = *pSrc++;
350 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
353 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
356 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
357 OPJ_UINT32 val0 = *pSrc++;
358 OPJ_UINT32 val1 = *pSrc++;
359 OPJ_UINT32 val2 = *pSrc++;
360 OPJ_UINT32 val3 = *pSrc++;
361 OPJ_UINT32 val4 = *pSrc++;
362 OPJ_UINT32 val5 = *pSrc++;
363 OPJ_UINT32 val6 = *pSrc++;
365 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
366 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
367 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
368 pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6);
372 OPJ_UINT32 val0 = *pSrc++;
373 OPJ_UINT32 val1 = *pSrc++;
374 length = length & 3U;
375 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
378 OPJ_UINT32 val2 = *pSrc++;
379 OPJ_UINT32 val3 = *pSrc++;
380 pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
382 OPJ_UINT32 val4 = *pSrc++;
383 OPJ_UINT32 val5 = *pSrc++;
384 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
390 /* seems that libtiff decodes this to machine endianness */
391 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
394 for (i = 0; i < length; i++) {
400 * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
401 * CINEMA : 12 bit precision
403 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
405 int subsampling_dx = parameters->subsampling_dx;
406 int subsampling_dy = parameters->subsampling_dy;
411 int j, currentPlane, numcomps = 0, w, h;
412 OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
413 opj_image_cmptparm_t cmptparm[4]; /* RGBA */
414 opj_image_t *image = NULL;
416 unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
417 unsigned int tiWidth, tiHeight;
418 OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
419 convert_XXx32s_C1R cvtTifTo32s = NULL;
420 convert_32s_CXPX cvtCxToPx = NULL;
421 OPJ_INT32* buffer32s = NULL;
422 OPJ_INT32* planes[4];
423 OPJ_SIZE_T rowStride;
425 tif = TIFFOpen(filename, "r");
429 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
432 tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
433 tiWidth = tiHeight = 0;
435 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
436 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
437 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
438 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
439 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
440 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
441 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
445 if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) {
446 fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
447 fprintf(stderr,"\tAborting\n");
451 if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
452 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
453 fprintf(stderr,"\tAborting\n");
464 cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
466 /* others are specific to TIFF */
468 cvtTifTo32s = tif_10uto32s;
471 cvtTifTo32s = tif_12uto32s;
474 cvtTifTo32s = tif_14uto32s;
477 cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
484 {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
488 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
489 &extrasamples, &sampleinfo);
491 if(extrasamples >= 1)
493 switch(sampleinfo[0])
495 case EXTRASAMPLE_UNSPECIFIED:
496 /* Workaround for some images without correct info about alpha channel
502 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
503 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
508 else /* extrasamples == 0 */
509 if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
512 /* initialize image components */
513 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
515 if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
516 fprintf(stdout,"WARNING:\n"
517 "Input image bitdepth is %d bits\n"
518 "TIF conversion has automatically rescaled to 12-bits\n"
519 "to comply with cinema profiles.\n",
525 if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
527 numcomps = 3 + has_alpha;
528 color_space = OPJ_CLRSPC_SRGB;
530 else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
532 numcomps = 1 + has_alpha;
533 color_space = OPJ_CLRSPC_GRAY;
536 cvtCxToPx = convert_32s_CXPX_LUT[numcomps];
537 if (tiPC == PLANARCONFIG_SEPARATE) {
538 cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */
539 tiSpp = 1U; /* consider only one sample per plane */
542 for(j = 0; j < numcomps; j++)
544 cmptparm[j].prec = tiBps;
545 cmptparm[j].bpp = tiBps;
546 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
547 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
548 cmptparm[j].w = (OPJ_UINT32)w;
549 cmptparm[j].h = (OPJ_UINT32)h;
552 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
558 /* set image offset and reference grid */
559 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
560 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
561 image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
562 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
563 image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
564 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
566 for(j = 0; j < numcomps; j++)
568 planes[j] = image->comps[j].data;
570 image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
572 strip_size = TIFFStripSize(tif);
574 buf = _TIFFmalloc(strip_size);
577 opj_image_destroy(image);
580 rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
581 buffer32s = malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
582 if (buffer32s == NULL) {
585 opj_image_destroy(image);
593 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
595 /* Read the Image components */
596 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
598 const OPJ_UINT8 *dat8;
601 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
602 dat8 = (const OPJ_UINT8*)buf;
604 while (ssize >= rowStride) {
605 cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
606 cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
617 } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
624 for (j=0; j < numcomps; ++j) {
625 scale_component(&(image->comps[j]), 12);