Correct error message for invalid TIFF input
[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 typedef void (* tif_32stoX)(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length);
60
61 static void tif_32sto1u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
62 {
63         OPJ_SIZE_T i;
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];
73                 
74                 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
75         }
76         
77         if (length & 7U) {
78                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
79                 OPJ_UINT32 src1 = 0U;
80                 OPJ_UINT32 src2 = 0U;
81                 OPJ_UINT32 src3 = 0U;
82                 OPJ_UINT32 src4 = 0U;
83                 OPJ_UINT32 src5 = 0U;
84                 OPJ_UINT32 src6 = 0U;
85                 length = length & 7U;
86                 
87                 if (length > 1U) {
88                         src1 = (OPJ_UINT32)pSrc[i+1];
89                         if (length > 2U) {
90                                 src2 = (OPJ_UINT32)pSrc[i+2];
91                                 if (length > 3U) {
92                                         src3 = (OPJ_UINT32)pSrc[i+3];
93                                         if (length > 4U) {
94                                                 src4 = (OPJ_UINT32)pSrc[i+4];
95                                                 if (length > 5U) {
96                                                         src5 = (OPJ_UINT32)pSrc[i+5];
97                                                         if (length > 6U) {
98                                                                 src6 = (OPJ_UINT32)pSrc[i+6];
99                                                         }
100                                                 }
101                                         }
102                                 }
103                         }
104                 }
105                 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1));
106         }
107 }
108
109 static void tif_32sto2u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
110 {
111         OPJ_SIZE_T i;
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];
117                 
118                 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
119         }
120         
121         if (length & 3U) {
122                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
123                 OPJ_UINT32 src1 = 0U;
124                 OPJ_UINT32 src2 = 0U;
125                 length = length & 3U;
126                 
127                 if (length > 1U) {
128                         src1 = (OPJ_UINT32)pSrc[i+1];
129                         if (length > 2U) {
130                                 src2 = (OPJ_UINT32)pSrc[i+2];
131                         }
132                 }
133                 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
134         }
135 }
136
137 static void tif_32sto4u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
138 {
139         OPJ_SIZE_T i;
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];
143                 
144                 *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
145         }
146         
147         if (length & 1U) {
148                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
149                 *pDst++ = (OPJ_BYTE)((src0 << 4));
150         }
151 }
152
153 static void tif_32sto6u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
154 {
155         OPJ_SIZE_T i;
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];
161                 
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);
165         }
166         
167         if (length & 3U) {
168                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
169                 OPJ_UINT32 src1 = 0U;
170                 OPJ_UINT32 src2 = 0U;
171                 length = length & 3U;
172                 
173                 if (length > 1U) {
174                         src1 = (OPJ_UINT32)pSrc[i+1];
175                         if (length > 2U) {
176                                 src2 = (OPJ_UINT32)pSrc[i+2];
177                         }
178                 }
179                 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
180                 if (length > 1U) {
181                         *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
182                         if (length > 2U) {
183                                 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
184                         }
185                 }
186         }
187 }
188 static void tif_32sto8u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
189 {
190         OPJ_SIZE_T i;
191         for (i = 0; i < length; ++i) {
192                 pDst[i] = (OPJ_BYTE)pSrc[i];
193         }
194 }
195 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
196 {
197         OPJ_SIZE_T i;
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];
203                 
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);
209         }
210         
211         if (length & 3U) {
212                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
213                 OPJ_UINT32 src1 = 0U;
214                 OPJ_UINT32 src2 = 0U;
215                 length = length & 3U;
216                 
217                 if (length > 1U) {
218                         src1 = (OPJ_UINT32)pSrc[i+1];
219                         if (length > 2U) {
220                                 src2 = (OPJ_UINT32)pSrc[i+2];
221                         }
222                 }
223                 *pDst++ = (OPJ_BYTE)(src0 >> 2);
224                 *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
225                 if (length > 1U) {
226                         *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
227                         if (length > 2U) {
228                                 *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
229                         }
230                 }
231         }
232 }
233 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
234 {
235         OPJ_SIZE_T i;
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];
239                 
240                 *pDst++ = (OPJ_BYTE)(src0 >> 4);
241                 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
242                 *pDst++ = (OPJ_BYTE)(src1);
243         }
244         
245         if (length & 1U) {
246                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
247                 *pDst++ = (OPJ_BYTE)(src0 >> 4);
248                 *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
249         }
250 }
251 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
252 {
253         OPJ_SIZE_T i;
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];
259                 
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);
267         }
268         
269         if (length & 3U) {
270                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
271                 OPJ_UINT32 src1 = 0U;
272                 OPJ_UINT32 src2 = 0U;
273                 length = length & 3U;
274                 
275                 if (length > 1U) {
276                         src1 = (OPJ_UINT32)pSrc[i+1];
277                         if (length > 2U) {
278                                 src2 = (OPJ_UINT32)pSrc[i+2];
279                         }
280                 }
281                 *pDst++ = (OPJ_BYTE)(src0 >> 6);
282                 *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
283                 if (length > 1U) {
284                         *pDst++ = (OPJ_BYTE)(src1 >> 4);
285                         *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
286                         if (length > 2U) {
287                                 *pDst++ = (OPJ_BYTE)(src2 >> 2);
288                                 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
289                         }
290                 }
291         }
292 }
293 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
294 {
295         OPJ_SIZE_T i;
296         for (i = 0; i < length; ++i) {
297                 pDst[i] = (OPJ_UINT16)pSrc[i];
298         }
299 }
300
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)
303 {
304         OPJ_SIZE_T i;
305         const OPJ_INT32* pSrc0 = pSrc[0];
306         
307         for (i = 0; i < length; i++) {
308                 pDst[i] = pSrc0[i] + adjust;
309         }
310 }
311 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
312 {
313         OPJ_SIZE_T i;
314         const OPJ_INT32* pSrc0 = pSrc[0];
315         const OPJ_INT32* pSrc1 = pSrc[1];
316         
317         for (i = 0; i < length; i++) {
318                 pDst[2*i+0] = pSrc0[i] + adjust;
319                 pDst[2*i+1] = pSrc1[i] + adjust;
320         }
321 }
322 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
323 {
324         OPJ_SIZE_T i;
325         const OPJ_INT32* pSrc0 = pSrc[0];
326         const OPJ_INT32* pSrc1 = pSrc[1];
327         const OPJ_INT32* pSrc2 = pSrc[2];
328         
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;
333         }
334 }
335 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
336 {
337         OPJ_SIZE_T i;
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];
342         
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;
348         }
349 }
350
351 int imagetotif(opj_image_t * image, const char *outfile)
352 {
353         int width, height;
354         int bps,adjust, sgnd;
355         int tiPhoto;
356         TIFF *tif;
357         tdata_t buf;
358         tsize_t strip_size;
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;
365
366         bps = (int)image->comps[0].prec;
367         planes[0] = image->comps[0].data;
368         
369         numcomps = image->numcomps;
370         
371         if (numcomps > 2U) {
372                 tiPhoto = PHOTOMETRIC_RGB;
373                 if (numcomps > 4U) {
374                         numcomps = 4U;
375                 }
376         } else {
377                 tiPhoto = PHOTOMETRIC_MINISBLACK;
378         }
379         for (i = 1U; i < numcomps; ++i) {
380                 if (image->comps[0].dx != image->comps[i].dx) {
381                         break;
382                 }
383                 if (image->comps[0].dy != image->comps[i].dy) {
384                         break;
385                 }
386                 if (image->comps[0].prec != image->comps[i].prec) {
387                         break;
388                 }
389                 if (image->comps[0].sgnd != image->comps[i].sgnd) {
390                         break;
391                 }
392                 planes[i] = image->comps[i].data;
393         }
394         if (i != numcomps) {
395                 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
396                 fprintf(stderr,"\tAborting\n");
397                 return 1;
398         }
399         
400         if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
401         if(bps == 0)
402         {
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");
405                 return 1;
406         }
407         tif = TIFFOpen(outfile, "wb");
408         if (!tif)
409         {
410                 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
411                 return 1;
412         }
413         for (i = 0U; i < numcomps; ++i) {
414                 clip_component(&(image->comps[i]), image->comps[0].prec);
415         }
416         switch (numcomps) {
417                 case 1:
418                         cvtPxToCx = convert_32s_P1C1;
419                         break;
420                 case 2:
421                         cvtPxToCx = convert_32s_P2C2;
422                         break;
423                 case 3:
424                         cvtPxToCx = convert_32s_P3C3;
425                         break;
426                 case 4:
427                         cvtPxToCx = convert_32s_P4C4;
428                         break;
429                 default:
430                         /* never here */
431                         break;
432         }
433         switch (bps) {
434                 case 1:
435                         cvt32sToTif = tif_32sto1u;
436                         break;
437                 case 2:
438                         cvt32sToTif = tif_32sto2u;
439                         break;
440                 case 4:
441                         cvt32sToTif = tif_32sto4u;
442                         break;
443                 case 6:
444                         cvt32sToTif = tif_32sto6u;
445                         break;
446                 case 8:
447                         cvt32sToTif = tif_32sto8u;
448                         break;
449                 case 10:
450                         cvt32sToTif = tif_32sto10u;
451                         break;
452                 case 12:
453                         cvt32sToTif = tif_32sto12u;
454                         break;
455                 case 14:
456                         cvt32sToTif = tif_32sto14u;
457                         break;
458                 case 16:
459                         cvt32sToTif = (tif_32stoX)tif_32sto16u;
460                         break;
461                 default:
462                         /* never here */
463                         break;
464         }
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;
469         
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");
482                 TIFFClose(tif);
483                 return 1;
484         }
485         buf = _TIFFmalloc(strip_size);
486         if (buf == NULL) {
487                 TIFFClose(tif);
488                 return 1;
489         }
490         buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
491         if (buffer32s == NULL) {
492                 _TIFFfree(buf);
493                 TIFFClose(tif);
494                 return 1;
495         }
496         
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);
501                 planes[0] += width;
502                 planes[1] += width;
503                 planes[2] += width;
504                 planes[3] += width;
505         }
506         _TIFFfree((void*)buf);
507         TIFFClose(tif);
508         free(buffer32s);
509                 
510         return 0;
511 }/* imagetotif() */
512
513 typedef void (* tif_Xto32s)(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length);
514
515 static void tif_1uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
516 {
517         OPJ_SIZE_T i;
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);
528         }
529         if (length & 7U) {
530                 OPJ_UINT32 val = *pSrc++;
531                 length = length & 7U;
532                 pDst[i+0] = (OPJ_INT32)(val >> 7);
533                 
534                 if (length > 1U) {
535                         pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
536                         if (length > 2U) {
537                                 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
538                                 if (length > 3U) {
539                                         pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
540                                         if (length > 4U) {
541                                                 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
542                                                 if (length > 5U) {
543                                                         pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
544                                                         if (length > 6U) {
545                                                                 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
546                                                         }
547                                                 }
548                                         }
549                                 }
550                         }
551                 }
552         }
553 }
554 static void tif_2uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
555 {
556         OPJ_SIZE_T i;
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);
563         }
564         if (length & 3U) {
565                 OPJ_UINT32 val = *pSrc++;
566                 length = length & 3U;
567                 pDst[i+0] =  (OPJ_INT32)(val >> 6);
568                 
569                 if (length > 1U) {
570                         pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
571                         if (length > 2U) {
572                                 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
573                                 
574                         }
575                 }
576         }
577 }
578 static void tif_4uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
579 {
580         OPJ_SIZE_T i;
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);
585         }
586         if (length & 1U) {
587                 OPJ_UINT8 val = *pSrc++;
588                 pDst[i+0] = (OPJ_INT32)(val >> 4);
589         }
590 }
591 static void tif_6uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
592 {
593         OPJ_SIZE_T i;
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);
602                 
603         }
604         if (length & 3U) {
605                 OPJ_UINT32 val0 = *pSrc++;
606                 length = length & 3U;
607                 pDst[i+0] = (OPJ_INT32)(val0 >> 2);
608                 
609                 if (length > 1U) {
610                         OPJ_UINT32 val1 = *pSrc++;
611                         pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
612                         if (length > 2U) {
613                                 OPJ_UINT32 val2 = *pSrc++;
614                                 pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
615                         }
616                 }
617         }
618 }
619 static void tif_8uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
620 {
621         OPJ_SIZE_T i;
622         for (i = 0; i < length; ++i) {
623                 pDst[i] = pSrc[i];
624         }
625 }
626 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
627 {
628         OPJ_SIZE_T i;
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++;
635                 
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);
640                 
641         }
642         if (length & 3U) {
643                 OPJ_UINT32 val0 = *pSrc++;
644                 OPJ_UINT32 val1 = *pSrc++;
645                 length = length & 3U;
646                 pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
647                 
648                 if (length > 1U) {
649                         OPJ_UINT32 val2 = *pSrc++;
650                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
651                         if (length > 2U) {
652                                 OPJ_UINT32 val3 = *pSrc++;
653                                 pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
654                         }
655                 }
656         }
657 }
658 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
659 {
660         OPJ_SIZE_T i;
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++;
665
666                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
667                 pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
668         }
669         if (length & 1U) {
670                 OPJ_UINT32 val0 = *pSrc++;
671                 OPJ_UINT32 val1 = *pSrc++;
672                 pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
673         }
674 }
675 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
676 {
677         OPJ_SIZE_T i;
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++;
686                 
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);
691                 
692         }
693         if (length & 3U) {
694                 OPJ_UINT32 val0 = *pSrc++;
695                 OPJ_UINT32 val1 = *pSrc++;
696                 length = length & 3U;
697                 pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
698                 
699                 if (length > 1U) {
700                         OPJ_UINT32 val2 = *pSrc++;
701                         OPJ_UINT32 val3 = *pSrc++;
702                         pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
703                         if (length > 2U) {
704                                 OPJ_UINT32 val4 = *pSrc++;
705                                 OPJ_UINT32 val5 = *pSrc++;
706                                 pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
707                         }
708                 }
709         }
710 }
711 #if 0
712 static void tif_16uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
713 {
714         OPJ_SIZE_T i;
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);
720 #else
721                 pDst[i] = (OPJ_INT32)((val1 << 8) | val0);
722 #endif
723         }
724 }
725 #else
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)
728 {
729         OPJ_SIZE_T i;
730         for (i = 0; i < length; i++) {
731                 pDst[i] = pSrc[i];
732         }
733 }
734 #endif
735
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)
738 {
739         memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
740 }
741 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
742 {
743         OPJ_SIZE_T i;
744         OPJ_INT32* pDst0 = pDst[0];
745         OPJ_INT32* pDst1 = pDst[1];
746         
747         for (i = 0; i < length; i++) {
748                 pDst0[i] = pSrc[2*i+0];
749                 pDst1[i] = pSrc[2*i+1];
750         }
751 }
752 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
753 {
754         OPJ_SIZE_T i;
755         OPJ_INT32* pDst0 = pDst[0];
756         OPJ_INT32* pDst1 = pDst[1];
757         OPJ_INT32* pDst2 = pDst[2];
758         
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];
763         }
764 }
765 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
766 {
767         OPJ_SIZE_T i;
768         OPJ_INT32* pDst0 = pDst[0];
769         OPJ_INT32* pDst1 = pDst[1];
770         OPJ_INT32* pDst2 = pDst[2];
771         OPJ_INT32* pDst3 = pDst[3];
772         
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];
778         }
779 }
780
781
782 /*
783  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
784  * CINEMA                 : 12 bit precision
785  */
786 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
787 {
788         int subsampling_dx = parameters->subsampling_dx;
789         int subsampling_dy = parameters->subsampling_dy;
790         TIFF *tif;
791         tdata_t buf;
792         tstrip_t strip;
793         tsize_t strip_size;
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;
798         int has_alpha = 0;
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;
807         
808         tif = TIFFOpen(filename, "r");
809         
810         if(!tif)
811         {
812                 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
813                 return 0;
814         }
815         tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
816         tiWidth = tiHeight = 0;
817         
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);
825         w= (int)tiWidth;
826         h= (int)tiHeight;
827         
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");
831                 TIFFClose(tif);
832                 return NULL;
833         }
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");
837                 TIFFClose(tif);
838                 return NULL;
839         }
840         
841         switch (tiBps) {
842                 case 1:
843                         cvtTifTo32s = tif_1uto32s;
844                         break;
845                 case 2:
846                         cvtTifTo32s = tif_2uto32s;
847                         break;
848                 case 4:
849                         cvtTifTo32s = tif_4uto32s;
850                         break;
851                 case 6:
852                         cvtTifTo32s = tif_6uto32s;
853                         break;
854                 case 8:
855                         cvtTifTo32s = tif_8uto32s;
856                         break;
857                 case 10:
858                         cvtTifTo32s = tif_10uto32s;
859                         break;
860                 case 12:
861                         cvtTifTo32s = tif_12uto32s;
862                         break;
863                 case 14:
864                         cvtTifTo32s = tif_14uto32s;
865                         break;
866                 case 16:
867                         cvtTifTo32s = (tif_Xto32s)tif_16uto32s;
868                         break;
869                 default:
870                         /* never here */
871                         break;
872         }
873         
874         {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
875                 uint16* sampleinfo;
876                 uint16 extrasamples;
877                 
878                 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
879                                                                                                         &extrasamples, &sampleinfo);
880                 
881                 if(extrasamples >= 1)
882                 {
883                         switch(sampleinfo[0])
884                         {
885                                 case EXTRASAMPLE_UNSPECIFIED:
886                                         /* Workaround for some images without correct info about alpha channel
887                                          */
888                                         if(tiSpp > 3)
889                                                 has_alpha = 1;
890                                         break;
891                                         
892                                 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
893                                 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
894                                         has_alpha = 1;
895                                         break;
896                         }
897                 }
898                 else /* extrasamples == 0 */
899                         if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
900         }
901         
902         /* initialize image components */
903         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
904         
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",
910                                                 tiBps);
911         } else {
912                 is_cinema = 0U;
913         }
914         
915         if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
916         {
917                 numcomps = 3 + has_alpha;
918                 color_space = OPJ_CLRSPC_SRGB;
919         }
920         else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
921         {
922                 numcomps = 1 + has_alpha;
923                 color_space = OPJ_CLRSPC_GRAY;
924         }
925         
926         switch (numcomps) {
927                 case 1:
928                         cvtCxToPx = convert_32s_C1P1;
929                         break;
930                 case 2:
931                         cvtCxToPx = convert_32s_C2P2;
932                         break;
933                 case 3:
934                         cvtCxToPx = convert_32s_C3P3;
935                         break;
936                 case 4:
937                         cvtCxToPx = convert_32s_C4P4;
938                         break;
939                 default:
940                         /* never here */
941                         break;
942         }
943         if (tiPC == PLANARCONFIG_SEPARATE) {
944                 cvtCxToPx = convert_32s_C1P1; /* override */
945                 tiSpp = 1U; /* consider only one sample per plane */
946         }
947
948         for(j = 0; j < numcomps; j++)
949         {
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;
956         }
957                 
958         image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
959         if(!image)
960         {
961                 TIFFClose(tif);
962                 return NULL;
963         }
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;
971
972         for(j = 0; j < numcomps; j++)
973         {
974                 planes[j] = image->comps[j].data;
975         }
976         image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
977                 
978         strip_size = TIFFStripSize(tif);
979         
980         buf = _TIFFmalloc(strip_size);
981         if (buf == NULL) {
982                 TIFFClose(tif);
983                 opj_image_destroy(image);
984                 return NULL;
985         }
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) {
989                 _TIFFfree(buf);
990                 TIFFClose(tif);
991                 opj_image_destroy(image);
992                 return NULL;
993         }
994         
995         strip = 0;
996         currentPlane = 0;
997         do
998         {
999                 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
1000                 h= (int)tiHeight;
1001                 /* Read the Image components */
1002                 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
1003                 {
1004                                 const OPJ_UINT8 *dat8;
1005                                 OPJ_SIZE_T ssize;
1006                                 
1007                                 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1008                                 dat8 = (const OPJ_UINT8*)buf;
1009                                 
1010                                 while (ssize >= rowStride) {
1011                                         cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
1012                                         cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
1013                                         planes[0] += w;
1014                                         planes[1] += w;
1015                                         planes[2] += w;
1016                                         planes[3] += w;
1017                                         dat8  += rowStride;
1018                                         ssize -= rowStride;
1019                                         h--;
1020                                 }
1021                 }
1022                 currentPlane++;
1023         } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
1024         
1025         free(buffer32s);
1026         _TIFFfree(buf);
1027         TIFFClose(tif);
1028         
1029         if (is_cinema) {
1030                 for (j=0; j < numcomps; ++j) {
1031                         scale_component(&(image->comps[j]), 12);
1032                 }
1033                 
1034         }
1035         return image;
1036
1037 }/* tiftoimage() */
1038