Remove some warnings when building
[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)7U); 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)3U); 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)1U); 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)3U); 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)3U); 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)1U); 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)3U); 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)7U); 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)3U); 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)1U); 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)3U); 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)3U); 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)1U); 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)3U); 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
712 /* seems that libtiff decodes this to machine endianness */
713 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
714 {
715         OPJ_SIZE_T i;
716         for (i = 0; i < length; i++) {
717                 pDst[i] = pSrc[i];
718         }
719 }
720
721 typedef void (* convert_32s_CXPX)(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length);
722 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
723 {
724         memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
725 }
726 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
727 {
728         OPJ_SIZE_T i;
729         OPJ_INT32* pDst0 = pDst[0];
730         OPJ_INT32* pDst1 = pDst[1];
731         
732         for (i = 0; i < length; i++) {
733                 pDst0[i] = pSrc[2*i+0];
734                 pDst1[i] = pSrc[2*i+1];
735         }
736 }
737 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
738 {
739         OPJ_SIZE_T i;
740         OPJ_INT32* pDst0 = pDst[0];
741         OPJ_INT32* pDst1 = pDst[1];
742         OPJ_INT32* pDst2 = pDst[2];
743         
744         for (i = 0; i < length; i++) {
745                 pDst0[i] = pSrc[3*i+0];
746                 pDst1[i] = pSrc[3*i+1];
747                 pDst2[i] = pSrc[3*i+2];
748         }
749 }
750 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
751 {
752         OPJ_SIZE_T i;
753         OPJ_INT32* pDst0 = pDst[0];
754         OPJ_INT32* pDst1 = pDst[1];
755         OPJ_INT32* pDst2 = pDst[2];
756         OPJ_INT32* pDst3 = pDst[3];
757         
758         for (i = 0; i < length; i++) {
759                 pDst0[i] = pSrc[4*i+0];
760                 pDst1[i] = pSrc[4*i+1];
761                 pDst2[i] = pSrc[4*i+2];
762                 pDst3[i] = pSrc[4*i+3];
763         }
764 }
765
766
767 /*
768  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
769  * CINEMA                 : 12 bit precision
770  */
771 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
772 {
773         int subsampling_dx = parameters->subsampling_dx;
774         int subsampling_dy = parameters->subsampling_dy;
775         TIFF *tif;
776         tdata_t buf;
777         tstrip_t strip;
778         tsize_t strip_size;
779         int j, currentPlane, numcomps = 0, w, h;
780         OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
781         opj_image_cmptparm_t cmptparm[4]; /* RGBA */
782         opj_image_t *image = NULL;
783         int has_alpha = 0;
784         unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
785         unsigned int tiWidth, tiHeight;
786         OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
787         tif_Xto32s cvtTifTo32s = NULL;
788         convert_32s_CXPX cvtCxToPx = NULL;
789         OPJ_INT32* buffer32s = NULL;
790         OPJ_INT32* planes[4];
791         OPJ_SIZE_T rowStride;
792         
793         tif = TIFFOpen(filename, "r");
794         
795         if(!tif)
796         {
797                 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
798                 return 0;
799         }
800         tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
801         tiWidth = tiHeight = 0;
802         
803         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
804         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
805         TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
806         TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
807         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
808         TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
809         TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
810         w= (int)tiWidth;
811         h= (int)tiHeight;
812         
813         if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) {
814                 fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
815                 fprintf(stderr,"\tAborting\n");
816                 TIFFClose(tif);
817                 return NULL;
818         }
819         if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
820                 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
821                 fprintf(stderr,"\tAborting\n");
822                 TIFFClose(tif);
823                 return NULL;
824         }
825         
826         switch (tiBps) {
827                 case 1:
828                         cvtTifTo32s = tif_1uto32s;
829                         break;
830                 case 2:
831                         cvtTifTo32s = tif_2uto32s;
832                         break;
833                 case 4:
834                         cvtTifTo32s = tif_4uto32s;
835                         break;
836                 case 6:
837                         cvtTifTo32s = tif_6uto32s;
838                         break;
839                 case 8:
840                         cvtTifTo32s = tif_8uto32s;
841                         break;
842                 case 10:
843                         cvtTifTo32s = tif_10uto32s;
844                         break;
845                 case 12:
846                         cvtTifTo32s = tif_12uto32s;
847                         break;
848                 case 14:
849                         cvtTifTo32s = tif_14uto32s;
850                         break;
851                 case 16:
852                         cvtTifTo32s = (tif_Xto32s)tif_16uto32s;
853                         break;
854                 default:
855                         /* never here */
856                         break;
857         }
858         
859         {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
860                 uint16* sampleinfo;
861                 uint16 extrasamples;
862                 
863                 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
864                                                                                                         &extrasamples, &sampleinfo);
865                 
866                 if(extrasamples >= 1)
867                 {
868                         switch(sampleinfo[0])
869                         {
870                                 case EXTRASAMPLE_UNSPECIFIED:
871                                         /* Workaround for some images without correct info about alpha channel
872                                          */
873                                         if(tiSpp > 3)
874                                                 has_alpha = 1;
875                                         break;
876                                         
877                                 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
878                                 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
879                                         has_alpha = 1;
880                                         break;
881                         }
882                 }
883                 else /* extrasamples == 0 */
884                         if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
885         }
886         
887         /* initialize image components */
888         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
889         
890         if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
891                 fprintf(stdout,"WARNING:\n"
892                                                 "Input image bitdepth is %d bits\n"
893                                                 "TIF conversion has automatically rescaled to 12-bits\n"
894                                                 "to comply with cinema profiles.\n",
895                                                 tiBps);
896         } else {
897                 is_cinema = 0U;
898         }
899         
900         if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
901         {
902                 numcomps = 3 + has_alpha;
903                 color_space = OPJ_CLRSPC_SRGB;
904         }
905         else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
906         {
907                 numcomps = 1 + has_alpha;
908                 color_space = OPJ_CLRSPC_GRAY;
909         }
910         
911         switch (numcomps) {
912                 case 1:
913                         cvtCxToPx = convert_32s_C1P1;
914                         break;
915                 case 2:
916                         cvtCxToPx = convert_32s_C2P2;
917                         break;
918                 case 3:
919                         cvtCxToPx = convert_32s_C3P3;
920                         break;
921                 case 4:
922                         cvtCxToPx = convert_32s_C4P4;
923                         break;
924                 default:
925                         /* never here */
926                         break;
927         }
928         if (tiPC == PLANARCONFIG_SEPARATE) {
929                 cvtCxToPx = convert_32s_C1P1; /* override */
930                 tiSpp = 1U; /* consider only one sample per plane */
931         }
932
933         for(j = 0; j < numcomps; j++)
934         {
935                 cmptparm[j].prec = tiBps;
936                 cmptparm[j].bpp = tiBps;
937                 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
938                 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
939                 cmptparm[j].w = (OPJ_UINT32)w;
940                 cmptparm[j].h = (OPJ_UINT32)h;
941         }
942                 
943         image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
944         if(!image)
945         {
946                 TIFFClose(tif);
947                 return NULL;
948         }
949         /* set image offset and reference grid */
950         image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
951         image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
952         image->x1 =     !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
953         image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
954         image->y1 =     !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
955         image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
956
957         for(j = 0; j < numcomps; j++)
958         {
959                 planes[j] = image->comps[j].data;
960         }
961         image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
962                 
963         strip_size = TIFFStripSize(tif);
964         
965         buf = _TIFFmalloc(strip_size);
966         if (buf == NULL) {
967                 TIFFClose(tif);
968                 opj_image_destroy(image);
969                 return NULL;
970         }
971         rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
972         buffer32s = malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
973         if (buffer32s == NULL) {
974                 _TIFFfree(buf);
975                 TIFFClose(tif);
976                 opj_image_destroy(image);
977                 return NULL;
978         }
979         
980         strip = 0;
981         currentPlane = 0;
982         do
983         {
984                 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
985                 h= (int)tiHeight;
986                 /* Read the Image components */
987                 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
988                 {
989                                 const OPJ_UINT8 *dat8;
990                                 OPJ_SIZE_T ssize;
991                                 
992                                 ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
993                                 dat8 = (const OPJ_UINT8*)buf;
994                                 
995                                 while (ssize >= rowStride) {
996                                         cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
997                                         cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
998                                         planes[0] += w;
999                                         planes[1] += w;
1000                                         planes[2] += w;
1001                                         planes[3] += w;
1002                                         dat8  += rowStride;
1003                                         ssize -= rowStride;
1004                                         h--;
1005                                 }
1006                 }
1007                 currentPlane++;
1008         } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
1009         
1010         free(buffer32s);
1011         _TIFFfree(buf);
1012         TIFFClose(tif);
1013         
1014         if (is_cinema) {
1015                 for (j=0; j < numcomps; ++j) {
1016                         scale_component(&(image->comps[j]), 12);
1017                 }
1018                 
1019         }
1020         return image;
1021
1022 }/* tiftoimage() */
1023