Update convert for PNG output
[openjpeg.git] / src / bin / jp2 / converttif.c
1 /*
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.
6  *
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.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
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.
26  *
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.
38  */
39 #include "opj_apps_config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45
46 #ifndef OPJ_HAVE_LIBTIFF
47 # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
48 #endif /* OPJ_HAVE_LIBTIFF */
49
50 #include <tiffio.h>
51 #include "openjpeg.h"
52 #include "convert.h"
53
54 /* -->> -->> -->> -->>
55  
56  TIFF IMAGE FORMAT
57  
58  <<-- <<-- <<-- <<-- */
59
60 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
61 {
62         OPJ_SIZE_T i;
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];
68                 
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);
74         }
75         
76         if (length & 3U) {
77                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
78                 OPJ_UINT32 src1 = 0U;
79                 OPJ_UINT32 src2 = 0U;
80                 length = length & 3U;
81                 
82                 if (length > 1U) {
83                         src1 = (OPJ_UINT32)pSrc[i+1];
84                         if (length > 2U) {
85                                 src2 = (OPJ_UINT32)pSrc[i+2];
86                         }
87                 }
88                 *pDst++ = (OPJ_BYTE)(src0 >> 2);
89                 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
90                 if (length > 1U) {
91                         *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
92                         if (length > 2U) {
93                                 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
94                         }
95                 }
96         }
97 }
98 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
99 {
100         OPJ_SIZE_T i;
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];
104                 
105                 *pDst++ = (OPJ_BYTE)(src0 >> 4);
106                 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
107                 *pDst++ = (OPJ_BYTE)(src1);
108         }
109         
110         if (length & 1U) {
111                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
112                 *pDst++ = (OPJ_BYTE)(src0 >> 4);
113                 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
114         }
115 }
116 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
117 {
118         OPJ_SIZE_T i;
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];
124                 
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);
132         }
133         
134         if (length & 3U) {
135                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
136                 OPJ_UINT32 src1 = 0U;
137                 OPJ_UINT32 src2 = 0U;
138                 length = length & 3U;
139                 
140                 if (length > 1U) {
141                         src1 = (OPJ_UINT32)pSrc[i+1];
142                         if (length > 2U) {
143                                 src2 = (OPJ_UINT32)pSrc[i+2];
144                         }
145                 }
146                 *pDst++ = (OPJ_BYTE)(src0 >> 6);
147                 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
148                 if (length > 1U) {
149                         *pDst++ = (OPJ_BYTE)(src1 >> 4);
150                         *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
151                         if (length > 2U) {
152                                 *pDst++ = (OPJ_BYTE)(src2 >> 2);
153                                 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
154                         }
155                 }
156         }
157 }
158 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
159 {
160         OPJ_SIZE_T i;
161         for (i = 0; i < length; ++i) {
162                 pDst[i] = (OPJ_UINT16)pSrc[i];
163         }
164 }
165
166 int imagetotif(opj_image_t * image, const char *outfile)
167 {
168         int width, height;
169         int bps,adjust, sgnd;
170         int tiPhoto;
171         TIFF *tif;
172         tdata_t buf;
173         tsize_t strip_size;
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;
180
181         bps = (int)image->comps[0].prec;
182         planes[0] = image->comps[0].data;
183         
184         numcomps = image->numcomps;
185         
186         if (numcomps > 2U) {
187                 tiPhoto = PHOTOMETRIC_RGB;
188                 if (numcomps > 4U) {
189                         numcomps = 4U;
190                 }
191         } else {
192                 tiPhoto = PHOTOMETRIC_MINISBLACK;
193         }
194         for (i = 1U; i < numcomps; ++i) {
195                 if (image->comps[0].dx != image->comps[i].dx) {
196                         break;
197                 }
198                 if (image->comps[0].dy != image->comps[i].dy) {
199                         break;
200                 }
201                 if (image->comps[0].prec != image->comps[i].prec) {
202                         break;
203                 }
204                 if (image->comps[0].sgnd != image->comps[i].sgnd) {
205                         break;
206                 }
207                 planes[i] = image->comps[i].data;
208         }
209         if (i != numcomps) {
210                 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
211                 fprintf(stderr,"\tAborting\n");
212                 return 1;
213         }
214         
215         if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
216         if(bps == 0)
217         {
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");
220                 return 1;
221         }
222         tif = TIFFOpen(outfile, "wb");
223         if (!tif)
224         {
225                 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
226                 return 1;
227         }
228         for (i = 0U; i < numcomps; ++i) {
229                 clip_component(&(image->comps[i]), image->comps[0].prec);
230         }
231         cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
232         switch (bps) {
233                 case 1:
234                 case 2:
235                 case 4:
236                 case 6:
237                 case 8:
238                         cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
239                         break;
240                 case 10:
241                         cvt32sToTif = tif_32sto10u;
242                         break;
243                 case 12:
244                         cvt32sToTif = tif_32sto12u;
245                         break;
246                 case 14:
247                         cvt32sToTif = tif_32sto14u;
248                         break;
249                 case 16:
250                         cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
251                         break;
252                 default:
253                         /* never here */
254                         break;
255         }
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;
260         
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");
273                 TIFFClose(tif);
274                 return 1;
275         }
276         buf = _TIFFmalloc(strip_size);
277         if (buf == NULL) {
278                 TIFFClose(tif);
279                 return 1;
280         }
281         buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
282         if (buffer32s == NULL) {
283                 _TIFFfree(buf);
284                 TIFFClose(tif);
285                 return 1;
286         }
287         
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);
292                 planes[0] += width;
293                 planes[1] += width;
294                 planes[2] += width;
295                 planes[3] += width;
296         }
297         _TIFFfree((void*)buf);
298         TIFFClose(tif);
299         free(buffer32s);
300                 
301         return 0;
302 }/* imagetotif() */
303
304 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
305 {
306         OPJ_SIZE_T i;
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++;
313                 
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);
318                 
319         }
320         if (length & 3U) {
321                 OPJ_UINT32 val0 = *pSrc++;
322                 OPJ_UINT32 val1 = *pSrc++;
323                 length = length & 3U;
324                 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
325                 
326                 if (length > 1U) {
327                         OPJ_UINT32 val2 = *pSrc++;
328                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
329                         if (length > 2U) {
330                                 OPJ_UINT32 val3 = *pSrc++;
331                                 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
332                         }
333                 }
334         }
335 }
336 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
337 {
338         OPJ_SIZE_T i;
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++;
343
344                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
345                 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
346         }
347         if (length & 1U) {
348                 OPJ_UINT32 val0 = *pSrc++;
349                 OPJ_UINT32 val1 = *pSrc++;
350                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
351         }
352 }
353 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
354 {
355         OPJ_SIZE_T i;
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++;
364                 
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);
369                 
370         }
371         if (length & 3U) {
372                 OPJ_UINT32 val0 = *pSrc++;
373                 OPJ_UINT32 val1 = *pSrc++;
374                 length = length & 3U;
375                 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
376                 
377                 if (length > 1U) {
378                         OPJ_UINT32 val2 = *pSrc++;
379                         OPJ_UINT32 val3 = *pSrc++;
380                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
381                         if (length > 2U) {
382                                 OPJ_UINT32 val4 = *pSrc++;
383                                 OPJ_UINT32 val5 = *pSrc++;
384                                 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
385                         }
386                 }
387         }
388 }
389
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)
392 {
393         OPJ_SIZE_T i;
394         for (i = 0; i < length; i++) {
395                 pDst[i] = pSrc[i];
396         }
397 }
398
399 /*
400  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
401  * CINEMA                 : 12 bit precision
402  */
403 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
404 {
405         int subsampling_dx = parameters->subsampling_dx;
406         int subsampling_dy = parameters->subsampling_dy;
407         TIFF *tif;
408         tdata_t buf;
409         tstrip_t strip;
410         tsize_t strip_size;
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;
415         int has_alpha = 0;
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;
424         
425         tif = TIFFOpen(filename, "r");
426         
427         if(!tif)
428         {
429                 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
430                 return 0;
431         }
432         tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
433         tiWidth = tiHeight = 0;
434         
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);
442         w= (int)tiWidth;
443         h= (int)tiHeight;
444         
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");
448                 TIFFClose(tif);
449                 return NULL;
450         }
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");
454                 TIFFClose(tif);
455                 return NULL;
456         }
457         
458         switch (tiBps) {
459                 case 1:
460                 case 2:
461                 case 4:
462                 case 6:
463                 case 8:
464                         cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
465                         break;
466                 /* others are specific to TIFF */
467                 case 10:
468                         cvtTifTo32s = tif_10uto32s;
469                         break;
470                 case 12:
471                         cvtTifTo32s = tif_12uto32s;
472                         break;
473                 case 14:
474                         cvtTifTo32s = tif_14uto32s;
475                         break;
476                 case 16:
477                         cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
478                         break;
479                 default:
480                         /* never here */
481                         break;
482         }
483         
484         {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
485                 uint16* sampleinfo;
486                 uint16 extrasamples;
487                 
488                 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
489                                                                                                         &extrasamples, &sampleinfo);
490                 
491                 if(extrasamples >= 1)
492                 {
493                         switch(sampleinfo[0])
494                         {
495                                 case EXTRASAMPLE_UNSPECIFIED:
496                                         /* Workaround for some images without correct info about alpha channel
497                                          */
498                                         if(tiSpp > 3)
499                                                 has_alpha = 1;
500                                         break;
501                                         
502                                 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
503                                 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
504                                         has_alpha = 1;
505                                         break;
506                         }
507                 }
508                 else /* extrasamples == 0 */
509                         if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
510         }
511         
512         /* initialize image components */
513         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
514         
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",
520                                                 tiBps);
521         } else {
522                 is_cinema = 0U;
523         }
524         
525         if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
526         {
527                 numcomps = 3 + has_alpha;
528                 color_space = OPJ_CLRSPC_SRGB;
529         }
530         else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
531         {
532                 numcomps = 1 + has_alpha;
533                 color_space = OPJ_CLRSPC_GRAY;
534         }
535         
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 */
540         }
541
542         for(j = 0; j < numcomps; j++)
543         {
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;
550         }
551                 
552         image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
553         if(!image)
554         {
555                 TIFFClose(tif);
556                 return NULL;
557         }
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;
565
566         for(j = 0; j < numcomps; j++)
567         {
568                 planes[j] = image->comps[j].data;
569         }
570         image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
571                 
572         strip_size = TIFFStripSize(tif);
573         
574         buf = _TIFFmalloc(strip_size);
575         if (buf == NULL) {
576                 TIFFClose(tif);
577                 opj_image_destroy(image);
578                 return NULL;
579         }
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) {
583                 _TIFFfree(buf);
584                 TIFFClose(tif);
585                 opj_image_destroy(image);
586                 return NULL;
587         }
588         
589         strip = 0;
590         currentPlane = 0;
591         do
592         {
593                 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
594                 h= (int)tiHeight;
595                 /* Read the Image components */
596                 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
597                 {
598                                 const OPJ_UINT8 *dat8;
599                                 OPJ_SIZE_T ssize;
600                                 
601                                 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
602                                 dat8 = (const OPJ_UINT8*)buf;
603                                 
604                                 while (ssize >= rowStride) {
605                                         cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
606                                         cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
607                                         planes[0] += w;
608                                         planes[1] += w;
609                                         planes[2] += w;
610                                         planes[3] += w;
611                                         dat8  += rowStride;
612                                         ssize -= rowStride;
613                                         h--;
614                                 }
615                 }
616                 currentPlane++;
617         } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
618         
619         free(buffer32s);
620         _TIFFfree(buf);
621         TIFFClose(tif);
622         
623         if (is_cinema) {
624                 for (j=0; j < numcomps; ++j) {
625                         scale_component(&(image->comps[j]), 12);
626                 }
627                 
628         }
629         return image;
630
631 }/* tiftoimage() */
632