Merge branch 'master' into travis-abi
[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 (image->color_space == OPJ_CLRSPC_CMYK) {
187                 if (numcomps < 4U) {
188                         fprintf(stderr,"imagetotif: CMYK images shall be composed of at least 4 planes.\n");
189                         fprintf(stderr,"\tAborting\n");
190                         return 1;
191                 }
192                 tiPhoto = PHOTOMETRIC_SEPARATED;
193                 if (numcomps > 4U) {
194                         numcomps = 4U; /* Alpha not supported */
195                 }
196         }
197         else if (numcomps > 2U) {
198                 tiPhoto = PHOTOMETRIC_RGB;
199                 if (numcomps > 4U) {
200                         numcomps = 4U;
201                 }
202         } else {
203                 tiPhoto = PHOTOMETRIC_MINISBLACK;
204         }
205         for (i = 1U; i < numcomps; ++i) {
206                 if (image->comps[0].dx != image->comps[i].dx) {
207                         break;
208                 }
209                 if (image->comps[0].dy != image->comps[i].dy) {
210                         break;
211                 }
212                 if (image->comps[0].prec != image->comps[i].prec) {
213                         break;
214                 }
215                 if (image->comps[0].sgnd != image->comps[i].sgnd) {
216                         break;
217                 }
218                 planes[i] = image->comps[i].data;
219         }
220         if (i != numcomps) {
221                 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
222                 fprintf(stderr,"\tAborting\n");
223                 return 1;
224         }
225         
226         if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
227         if(bps == 0)
228         {
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");
231                 return 1;
232         }
233         tif = TIFFOpen(outfile, "wb");
234         if (!tif)
235         {
236                 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
237                 return 1;
238         }
239         for (i = 0U; i < numcomps; ++i) {
240                 clip_component(&(image->comps[i]), image->comps[0].prec);
241         }
242         cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
243         switch (bps) {
244                 case 1:
245                 case 2:
246                 case 4:
247                 case 6:
248                 case 8:
249                         cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
250                         break;
251                 case 10:
252                         cvt32sToTif = tif_32sto10u;
253                         break;
254                 case 12:
255                         cvt32sToTif = tif_32sto12u;
256                         break;
257                 case 14:
258                         cvt32sToTif = tif_32sto14u;
259                         break;
260                 case 16:
261                         cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
262                         break;
263                 default:
264                         /* never here */
265                         break;
266         }
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;
271         
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);
280         
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");
285                 TIFFClose(tif);
286                 return 1;
287         }
288         buf = _TIFFmalloc(strip_size);
289         if (buf == NULL) {
290                 TIFFClose(tif);
291                 return 1;
292         }
293         buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
294         if (buffer32s == NULL) {
295                 _TIFFfree(buf);
296                 TIFFClose(tif);
297                 return 1;
298         }
299         
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);
304                 planes[0] += width;
305                 planes[1] += width;
306                 planes[2] += width;
307                 planes[3] += width;
308         }
309         _TIFFfree((void*)buf);
310         TIFFClose(tif);
311         free(buffer32s);
312                 
313         return 0;
314 }/* imagetotif() */
315
316 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
317 {
318         OPJ_SIZE_T i;
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++;
325                 
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);
330                 
331         }
332         if (length & 3U) {
333                 OPJ_UINT32 val0 = *pSrc++;
334                 OPJ_UINT32 val1 = *pSrc++;
335                 length = length & 3U;
336                 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
337                 
338                 if (length > 1U) {
339                         OPJ_UINT32 val2 = *pSrc++;
340                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
341                         if (length > 2U) {
342                                 OPJ_UINT32 val3 = *pSrc++;
343                                 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
344                         }
345                 }
346         }
347 }
348 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
349 {
350         OPJ_SIZE_T i;
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++;
355
356                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
357                 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
358         }
359         if (length & 1U) {
360                 OPJ_UINT32 val0 = *pSrc++;
361                 OPJ_UINT32 val1 = *pSrc++;
362                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
363         }
364 }
365 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
366 {
367         OPJ_SIZE_T i;
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++;
376                 
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);
381                 
382         }
383         if (length & 3U) {
384                 OPJ_UINT32 val0 = *pSrc++;
385                 OPJ_UINT32 val1 = *pSrc++;
386                 length = length & 3U;
387                 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
388                 
389                 if (length > 1U) {
390                         OPJ_UINT32 val2 = *pSrc++;
391                         OPJ_UINT32 val3 = *pSrc++;
392                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
393                         if (length > 2U) {
394                                 OPJ_UINT32 val4 = *pSrc++;
395                                 OPJ_UINT32 val5 = *pSrc++;
396                                 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
397                         }
398                 }
399         }
400 }
401
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)
404 {
405         OPJ_SIZE_T i;
406         for (i = 0; i < length; i++) {
407                 pDst[i] = pSrc[i];
408         }
409 }
410
411 /*
412  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
413  * CINEMA                 : 12 bit precision
414  */
415 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
416 {
417         int subsampling_dx = parameters->subsampling_dx;
418         int subsampling_dy = parameters->subsampling_dy;
419         TIFF *tif;
420         tdata_t buf;
421         tstrip_t strip;
422         tsize_t strip_size;
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;
427         int has_alpha = 0;
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;
436         
437         tif = TIFFOpen(filename, "r");
438         
439         if(!tif)
440         {
441                 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
442                 return 0;
443         }
444         tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
445         tiWidth = tiHeight = 0;
446         
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);
454         w= (int)tiWidth;
455         h= (int)tiHeight;
456         
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");
460                 TIFFClose(tif);
461                 return NULL;
462         }
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");
466                 TIFFClose(tif);
467                 return NULL;
468         }
469         
470         switch (tiBps) {
471                 case 1:
472                 case 2:
473                 case 4:
474                 case 6:
475                 case 8:
476                         cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
477                         break;
478                 /* others are specific to TIFF */
479                 case 10:
480                         cvtTifTo32s = tif_10uto32s;
481                         break;
482                 case 12:
483                         cvtTifTo32s = tif_12uto32s;
484                         break;
485                 case 14:
486                         cvtTifTo32s = tif_14uto32s;
487                         break;
488                 case 16:
489                         cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
490                         break;
491                 default:
492                         /* never here */
493                         break;
494         }
495         
496         {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
497                 uint16* sampleinfo;
498                 uint16 extrasamples;
499                 
500                 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
501                                                                                                         &extrasamples, &sampleinfo);
502                 
503                 if(extrasamples >= 1)
504                 {
505                         switch(sampleinfo[0])
506                         {
507                                 case EXTRASAMPLE_UNSPECIFIED:
508                                         /* Workaround for some images without correct info about alpha channel
509                                          */
510                                         if(tiSpp > 3)
511                                                 has_alpha = 1;
512                                         break;
513                                         
514                                 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
515                                 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
516                                         has_alpha = 1;
517                                         break;
518                         }
519                 }
520                 else /* extrasamples == 0 */
521                         if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
522         }
523         
524         /* initialize image components */
525         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
526         
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",
532                                                 tiBps);
533         } else {
534                 is_cinema = 0U;
535         }
536         
537         if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
538         {
539                 numcomps = 3 + has_alpha;
540                 color_space = OPJ_CLRSPC_SRGB;
541         }
542         else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
543         {
544                 numcomps = 1 + has_alpha;
545                 color_space = OPJ_CLRSPC_GRAY;
546         }
547         
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 */
552         }
553
554         for(j = 0; j < numcomps; j++)
555         {
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;
562         }
563                 
564         image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
565         if(!image)
566         {
567                 TIFFClose(tif);
568                 return NULL;
569         }
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;
577
578         for(j = 0; j < numcomps; j++)
579         {
580                 planes[j] = image->comps[j].data;
581         }
582         image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
583                 
584         strip_size = TIFFStripSize(tif);
585         
586         buf = _TIFFmalloc(strip_size);
587         if (buf == NULL) {
588                 TIFFClose(tif);
589                 opj_image_destroy(image);
590                 return NULL;
591         }
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) {
595                 _TIFFfree(buf);
596                 TIFFClose(tif);
597                 opj_image_destroy(image);
598                 return NULL;
599         }
600         
601         strip = 0;
602         currentPlane = 0;
603         do
604         {
605                 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
606                 h= (int)tiHeight;
607                 /* Read the Image components */
608                 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
609                 {
610                                 const OPJ_UINT8 *dat8;
611                                 OPJ_SIZE_T ssize;
612                                 
613                                 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
614                                 dat8 = (const OPJ_UINT8*)buf;
615                                 
616                                 while (ssize >= rowStride) {
617                                         cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
618                                         cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
619                                         planes[0] += w;
620                                         planes[1] += w;
621                                         planes[2] += w;
622                                         planes[3] += w;
623                                         dat8  += rowStride;
624                                         ssize -= rowStride;
625                                         h--;
626                                 }
627                 }
628                 currentPlane++;
629         } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
630         
631         free(buffer32s);
632         _TIFFfree(buf);
633         TIFFClose(tif);
634         
635         if (is_cinema) {
636                 for (j=0; j < numcomps; ++j) {
637                         scale_component(&(image->comps[j]), 12);
638                 }
639                 
640         }
641         return image;
642
643 }/* tiftoimage() */
644