Update imagetotif to support more output TIF formats
[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  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include "opj_apps_config.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44
45 #ifndef OPJ_HAVE_LIBTIFF
46 # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
47 #endif /* OPJ_HAVE_LIBTIFF */
48
49 #include <tiffio.h>
50 #include "openjpeg.h"
51 #include "convert.h"
52
53 /* -->> -->> -->> -->>
54  
55  TIFF IMAGE FORMAT
56  
57  <<-- <<-- <<-- <<-- */
58 typedef void (* tif_32stoX)(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length);
59
60 static void tif_32sto1u(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)8U); i+=8U) {
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                 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
69                 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
70                 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
71                 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
72                 
73                 *pDst++ = (src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7;
74         }
75         
76         if (length & 7U) {
77                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
78                 OPJ_UINT32 src1 = 0U;
79                 OPJ_UINT32 src2 = 0U;
80                 OPJ_UINT32 src3 = 0U;
81                 OPJ_UINT32 src4 = 0U;
82                 OPJ_UINT32 src5 = 0U;
83                 OPJ_UINT32 src6 = 0U;
84                 length = length & 7U;
85                 
86                 if (length > 1U) {
87                         src1 = (OPJ_UINT32)pSrc[i+1];
88                         if (length > 2U) {
89                                 src2 = (OPJ_UINT32)pSrc[i+2];
90                                 if (length > 3U) {
91                                         src3 = (OPJ_UINT32)pSrc[i+3];
92                                         if (length > 4U) {
93                                                 src4 = (OPJ_UINT32)pSrc[i+4];
94                                                 if (length > 5U) {
95                                                         src5 = (OPJ_UINT32)pSrc[i+5];
96                                                         if (length > 6U) {
97                                                                 src6 = (OPJ_UINT32)pSrc[i+6];
98                                                         }
99                                                 }
100                                         }
101                                 }
102                         }
103                 }
104                 *pDst++ = (src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1);
105         }
106 }
107
108 static void tif_32sto2u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
109 {
110         OPJ_SIZE_T i;
111         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
112                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
113                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
114                 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
115                 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
116                 
117                 *pDst++ = (src0 << 6) | (src1 << 4) | (src2 << 2) | src3;
118         }
119         
120         if (length & 3U) {
121                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
122                 OPJ_UINT32 src1 = 0U;
123                 OPJ_UINT32 src2 = 0U;
124                 length = length & 3U;
125                 
126                 if (length > 1U) {
127                         src1 = (OPJ_UINT32)pSrc[i+1];
128                         if (length > 2U) {
129                                 src2 = (OPJ_UINT32)pSrc[i+2];
130                         }
131                 }
132                 *pDst++ = (src0 << 6) | (src1 << 4) | (src2 << 2);
133         }
134 }
135
136 static void tif_32sto4u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
137 {
138         OPJ_SIZE_T i;
139         for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
140                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
141                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
142                 
143                 *pDst++ = (src0 << 4) | src1;
144         }
145         
146         if (length & 1U) {
147                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
148                 *pDst++ = (src0 << 4);
149         }
150 }
151
152 static void tif_32sto6u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
153 {
154         OPJ_SIZE_T i;
155         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
156                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
157                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
158                 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
159                 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
160                 
161                 *pDst++ = (src0 << 2) | (src1 >> 4);
162                 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 2);
163                 *pDst++ = ((src2 & 0x3U) << 6) | src3;
164         }
165         
166         if (length & 3U) {
167                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
168                 OPJ_UINT32 src1 = 0U;
169                 OPJ_UINT32 src2 = 0U;
170                 length = length & 3U;
171                 
172                 if (length > 1U) {
173                         src1 = (OPJ_UINT32)pSrc[i+1];
174                         if (length > 2U) {
175                                 src2 = (OPJ_UINT32)pSrc[i+2];
176                         }
177                 }
178                 *pDst++ = (src0 << 2) | (src1 >> 4);
179                 if (length > 1U) {
180                         *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 2);
181                         if (length > 2U) {
182                                 *pDst++ = ((src2 & 0x3U) << 6);
183                         }
184                 }
185         }
186 }
187 static void tif_32sto8u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
188 {
189         OPJ_SIZE_T i;
190         for (i = 0; i < length; ++i) {
191                 pDst[i] = (OPJ_BYTE)pSrc[i];
192         }
193 }
194 static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
195 {
196         OPJ_SIZE_T i;
197         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
198                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
199                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
200                 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
201                 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
202                 
203                 *pDst++ = src0 >> 2;
204                 *pDst++ = ((src0 & 0x3U) << 6) | (src1 >> 4);
205                 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 6);
206                 *pDst++ = ((src2 & 0x3FU) << 2) | (src3 >> 8);
207                 *pDst++ = src3;
208         }
209         
210         if (length & 3U) {
211                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
212                 OPJ_UINT32 src1 = 0U;
213                 OPJ_UINT32 src2 = 0U;
214                 length = length & 3U;
215                 
216                 if (length > 1U) {
217                         src1 = (OPJ_UINT32)pSrc[i+1];
218                         if (length > 2U) {
219                                 src2 = (OPJ_UINT32)pSrc[i+2];
220                         }
221                 }
222                 *pDst++ = src0 >> 2;
223                 *pDst++ = ((src0 & 0x3U) << 6) | (src1 >> 4);
224                 if (length > 1U) {
225                         *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 6);
226                         if (length > 2U) {
227                                 *pDst++ = ((src2 & 0x3FU) << 2);
228                         }
229                 }
230         }
231 }
232 static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
233 {
234         OPJ_SIZE_T i;
235         for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
236                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
237                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
238                 
239                 *pDst++ = src0 >> 4;
240                 *pDst++ = ((src0 & 0xFU) << 4) | (src1 >> 8);
241                 *pDst++ = src1;
242         }
243         
244         if (length & 1U) {
245                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
246                 *pDst++ = src0 >> 4;
247                 *pDst++ = ((src0 & 0xFU) << 4);
248         }
249 }
250 static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
251 {
252         OPJ_SIZE_T i;
253         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
254                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
255                 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
256                 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
257                 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
258                 
259                 *pDst++ = src0 >> 6;
260                 *pDst++ = ((src0 & 0x3FU) << 2) | (src1 >> 12);
261                 *pDst++ = src1 >> 4;
262                 *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 10);
263                 *pDst++ = src2 >> 2;
264                 *pDst++ = ((src2 & 0x3U) << 6) | (src3 >> 8);
265                 *pDst++ = src3;
266         }
267         
268         if (length & 3U) {
269                 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
270                 OPJ_UINT32 src1 = 0U;
271                 OPJ_UINT32 src2 = 0U;
272                 length = length & 3U;
273                 
274                 if (length > 1U) {
275                         src1 = (OPJ_UINT32)pSrc[i+1];
276                         if (length > 2U) {
277                                 src2 = (OPJ_UINT32)pSrc[i+2];
278                         }
279                 }
280                 *pDst++ = src0 >> 6;
281                 *pDst++ = ((src0 & 0x3FU) << 2) | (src1 >> 12);
282                 if (length > 1U) {
283                         *pDst++ = src1 >> 4;
284                         *pDst++ = ((src1 & 0xFU) << 4) | (src2 >> 10);
285                         if (length > 2U) {
286                                 *pDst++ = src2 >> 2;
287                                 *pDst++ = ((src2 & 0x3U) << 6);
288                         }
289                 }
290         }
291 }
292 static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
293 {
294         OPJ_SIZE_T i;
295         for (i = 0; i < length; ++i) {
296                 pDst[i] = (OPJ_UINT16)pSrc[i];
297         }
298 }
299
300 typedef void (* convert_32s_PXCX)(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust);
301 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
302 {
303         OPJ_SIZE_T i;
304         const OPJ_INT32* pSrc0 = pSrc[0];
305         
306         for (i = 0; i < length; i++) {
307                 pDst[i] = pSrc0[i] + adjust;
308         }
309 }
310 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
311 {
312         OPJ_SIZE_T i;
313         const OPJ_INT32* pSrc0 = pSrc[0];
314         const OPJ_INT32* pSrc1 = pSrc[1];
315         
316         for (i = 0; i < length; i++) {
317                 pDst[2*i+0] = pSrc0[i] + adjust;
318                 pDst[2*i+1] = pSrc1[i] + adjust;
319         }
320 }
321 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
322 {
323         OPJ_SIZE_T i;
324         const OPJ_INT32* pSrc0 = pSrc[0];
325         const OPJ_INT32* pSrc1 = pSrc[1];
326         const OPJ_INT32* pSrc2 = pSrc[2];
327         
328         for (i = 0; i < length; i++) {
329                 pDst[3*i+0] = pSrc0[i] + adjust;
330                 pDst[3*i+1] = pSrc1[i] + adjust;
331                 pDst[3*i+2] = pSrc2[i] + adjust;
332         }
333 }
334 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
335 {
336         OPJ_SIZE_T i;
337         const OPJ_INT32* pSrc0 = pSrc[0];
338         const OPJ_INT32* pSrc1 = pSrc[1];
339         const OPJ_INT32* pSrc2 = pSrc[2];
340         const OPJ_INT32* pSrc3 = pSrc[3];
341         
342         for (i = 0; i < length; i++) {
343                 pDst[4*i+0] = pSrc0[i] + adjust;
344                 pDst[4*i+1] = pSrc1[i] + adjust;
345                 pDst[4*i+2] = pSrc2[i] + adjust;
346                 pDst[4*i+3] = pSrc3[i] + adjust;
347         }
348 }
349
350 int imagetotif(opj_image_t * image, const char *outfile)
351 {
352         int width, height;
353         int bps,adjust, sgnd;
354         int has_alpha;
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         has_alpha = 0;
367         bps = (int)image->comps[0].prec;
368         planes[0] = image->comps[0].data;
369         
370         numcomps = image->numcomps;
371         
372         if (numcomps > 2U) {
373                 tiPhoto = PHOTOMETRIC_RGB;
374                 if (numcomps > 4U) {
375                         numcomps = 4U;
376                 }
377         } else {
378                 tiPhoto = PHOTOMETRIC_MINISBLACK;
379         }
380         for (i = 1U; i < numcomps; ++i) {
381                 if (image->comps[0].dx != image->comps[i].dx) {
382                         break;
383                 }
384                 if (image->comps[0].dy != image->comps[i].dy) {
385                         break;
386                 }
387                 if (image->comps[0].prec != image->comps[i].prec) {
388                         break;
389                 }
390                 if (image->comps[0].sgnd != image->comps[i].sgnd) {
391                         break;
392                 }
393                 planes[i] = image->comps[i].data;
394         }
395         if (i != numcomps) {
396                 fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
397                 fprintf(stderr,"\tAborting\n");
398                 return 1;
399         }
400         
401         if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0;
402         if(bps == 0)
403         {
404                 fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps);
405                 fprintf(stderr,"\tAborting\n");
406                 return 1;
407         }
408         tif = TIFFOpen(outfile, "wb");
409         if (!tif)
410         {
411                 fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
412                 return 1;
413         }
414         for (i = 0U; i < numcomps; ++i) {
415                 clip_component(&(image->comps[i]), image->comps[0].prec);
416         }
417         switch (numcomps) {
418                 case 1:
419                         cvtPxToCx = convert_32s_P1C1;
420                         break;
421                 case 2:
422                         cvtPxToCx = convert_32s_P2C2;
423                         break;
424                 case 3:
425                         cvtPxToCx = convert_32s_P3C3;
426                         break;
427                 case 4:
428                         cvtPxToCx = convert_32s_P4C4;
429                         break;
430                 default:
431                         /* never here */
432                         break;
433         }
434         switch (bps) {
435                 case 1:
436                         cvt32sToTif = tif_32sto1u;
437                         break;
438                 case 2:
439                         cvt32sToTif = tif_32sto2u;
440                         break;
441                 case 4:
442                         cvt32sToTif = tif_32sto4u;
443                         break;
444                 case 6:
445                         cvt32sToTif = tif_32sto6u;
446                         break;
447                 case 8:
448                         cvt32sToTif = tif_32sto8u;
449                         break;
450                 case 10:
451                         cvt32sToTif = tif_32sto10u;
452                         break;
453                 case 12:
454                         cvt32sToTif = tif_32sto12u;
455                         break;
456                 case 14:
457                         cvt32sToTif = tif_32sto14u;
458                         break;
459                 case 16:
460                         cvt32sToTif = (tif_32stoX)tif_32sto16u;
461                         break;
462                 default:
463                         /* never here */
464                         break;
465         }
466         sgnd = (int)image->comps[0].sgnd;
467         adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
468         width   = (int)image->comps[0].w;
469         height  = (int)image->comps[0].h;
470         
471         TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
472         TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
473         TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
474         TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
475         TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
476         TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
477         TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
478         TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
479         strip_size = TIFFStripSize(tif);
480         rowStride = ((OPJ_SIZE_T)width * numcomps * bps + 7U) / 8U;
481         if (rowStride != (OPJ_SIZE_T)strip_size) {
482                 fprintf(stderr, "Invalid TIFF strip size\n");
483                 TIFFClose(tif);
484                 return 1;
485         }
486         buf = _TIFFmalloc(strip_size);
487         if (buf == NULL) {
488                 TIFFClose(tif);
489                 return 1;
490         }
491         buffer32s = malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
492         if (buffer32s == NULL) {
493                 _TIFFfree(buf);
494                 TIFFClose(tif);
495                 return 1;
496         }
497         
498         for (i = 0; i < image->comps[0].h; ++i) {
499                 cvtPxToCx(planes, buffer32s, width, adjust);
500                 cvt32sToTif(buffer32s, buf, width * numcomps);
501                 (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
502                 planes[0] += width;
503                 planes[1] += width;
504                 planes[2] += width;
505                 planes[3] += width;
506         }
507         _TIFFfree((void*)buf);
508         TIFFClose(tif);
509         free(buffer32s);
510                 
511         return 0;
512 }/* imagetotif() */
513
514 typedef void (* tif_Xto32s)(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length);
515
516 static void tif_1uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
517 {
518         OPJ_SIZE_T i;
519         for (i = 0; i < (length & -(OPJ_SIZE_T)8U); i+=8U) {
520                 OPJ_UINT8 val = *pSrc++;
521                 pDst[i+0] =  val >> 7;
522                 pDst[i+1] = (val >> 6) & 0x1U;
523                 pDst[i+2] = (val >> 5) & 0x1U;
524                 pDst[i+3] = (val >> 4) & 0x1U;
525                 pDst[i+4] = (val >> 3) & 0x1U;
526                 pDst[i+5] = (val >> 2) & 0x1U;
527                 pDst[i+6] = (val >> 1) & 0x1U;
528                 pDst[i+7] = val & 0x1U;
529         }
530         if (length & 7U) {
531                 OPJ_UINT8 val = *pSrc++;
532                 length = length & 7U;
533                 pDst[i+0] =  val >> 7;
534                 
535                 if (length > 1U) {
536                         pDst[i+1] = (val >> 6) & 0x1U;
537                         if (length > 2U) {
538                                 pDst[i+2] = (val >> 5) & 0x1U;
539                                 if (length > 3U) {
540                                         pDst[i+3] = (val >> 4) & 0x1U;
541                                         if (length > 4U) {
542                                                 pDst[i+4] = (val >> 3) & 0x1U;
543                                                 if (length > 5U) {
544                                                         pDst[i+5] = (val >> 2) & 0x1U;
545                                                         if (length > 6U) {
546                                                                 pDst[i+6] = (val >> 1) & 0x1U;
547                                                         }
548                                                 }
549                                         }
550                                 }
551                         }
552                 }
553         }
554 }
555 static void tif_2uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
556 {
557         OPJ_SIZE_T i;
558         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
559                 OPJ_UINT8 val = *pSrc++;
560                 pDst[i+0] =  val >> 6;
561                 pDst[i+1] = (val >> 4) & 0x3U;
562                 pDst[i+2] = (val >> 2) & 0x3U;
563                 pDst[i+3] = val & 0x3U;
564         }
565         if (length & 3U) {
566                 OPJ_UINT8 val = *pSrc++;
567                 length = length & 3U;
568                 pDst[i+0] =  val >> 6;
569                 
570                 if (length > 1U) {
571                         pDst[i+1] = (val >> 4) & 0x3U;
572                         if (length > 2U) {
573                                 pDst[i+2] = (val >> 2) & 0x3U;
574                                 
575                         }
576                 }
577         }
578 }
579 static void tif_4uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
580 {
581         OPJ_SIZE_T i;
582         for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
583                 OPJ_UINT8 val = *pSrc++;
584                 pDst[i+0] = val >> 4;
585                 pDst[i+1] = val & 0xFU;
586         }
587         if (length & 1U) {
588                 OPJ_UINT8 val = *pSrc++;
589                 pDst[i+0] = val >> 4;
590         }
591 }
592 static void tif_6uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
593 {
594         OPJ_SIZE_T i;
595         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
596                 OPJ_UINT8 val0 = *pSrc++;
597                 OPJ_UINT8 val1 = *pSrc++;
598                 OPJ_UINT8 val2 = *pSrc++;
599                 pDst[i+0] = val0 >> 2;
600                 pDst[i+1] = ((val0 & 0x3U) << 4) | (val1 >> 4);
601                 pDst[i+2] = ((val1 & 0xFU) << 2) | (val2 >> 6);
602                 pDst[i+3] = val2 & 0x3FU;
603                 
604         }
605         if (length & 3U) {
606                 OPJ_UINT8 val0 = *pSrc++;
607                 length = length & 3U;
608                 pDst[i+0] =  val0 >> 2;
609                 
610                 if (length > 1U) {
611                         OPJ_UINT8 val1 = *pSrc++;
612                         pDst[i+1] = ((val0 & 0x3U) << 4) | (val1 >> 4);
613                         if (length > 2U) {
614                                 OPJ_UINT8 val2 = *pSrc++;
615                                 pDst[i+2] = ((val1 & 0xFU) << 2) | (val2 >> 6);
616                         }
617                 }
618         }
619 }
620 static void tif_8uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
621 {
622         OPJ_SIZE_T i;
623         for (i = 0; i < length; ++i) {
624                 pDst[i] = pSrc[i];
625         }
626 }
627 static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
628 {
629         OPJ_SIZE_T i;
630         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
631                 OPJ_INT32 val0 = *pSrc++;
632                 OPJ_INT32 val1 = *pSrc++;
633                 OPJ_INT32 val2 = *pSrc++;
634                 OPJ_INT32 val3 = *pSrc++;
635                 OPJ_INT32 val4 = *pSrc++;
636                 
637                 pDst[i+0] = (val0 << 2) | (val1 >> 6);
638                 pDst[i+1] = ((val1 & 0x3FU) << 4) | (val2 >> 4);
639                 pDst[i+2] = ((val2 & 0xFU) << 6) | (val3 >> 2);
640                 pDst[i+3] = ((val3 & 0x3U) << 8) | val4;
641                 
642         }
643         if (length & 3U) {
644                 OPJ_INT32 val0 = *pSrc++;
645                 OPJ_INT32 val1 = *pSrc++;
646                 length = length & 3U;
647                 pDst[i+0] = (val0 << 2) | (val1 >> 6);
648                 
649                 if (length > 1U) {
650                         OPJ_INT32 val2 = *pSrc++;
651                         pDst[i+1] = ((val1 & 0x3FU) << 4) | (val2 >> 4);
652                         if (length > 2U) {
653                                 OPJ_INT32 val3 = *pSrc++;
654                                 pDst[i+2] = ((val2 & 0xFU) << 6) | (val3 >> 2);
655                         }
656                 }
657         }
658 }
659 static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
660 {
661         OPJ_SIZE_T i;
662         for (i = 0; i < (length & -(OPJ_SIZE_T)2U); i+=2U) {
663                 OPJ_INT32 val0 = *pSrc++;
664                 OPJ_INT32 val1 = *pSrc++;
665                 OPJ_INT32 val2 = *pSrc++;
666
667                 pDst[i+0] = (val0 << 4) | (val1 >> 4);
668                 pDst[i+1] = ((val1 & 0xFU) << 8) | val2;
669         }
670         if (length & 1U) {
671                 OPJ_INT32 val0 = *pSrc++;
672                 OPJ_INT32 val1 = *pSrc++;
673                 pDst[i+0] = (val0 << 4) | (val1 >> 4);
674         }
675 }
676 static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
677 {
678         OPJ_SIZE_T i;
679         for (i = 0; i < (length & -(OPJ_SIZE_T)4U); i+=4U) {
680                 OPJ_INT32 val0 = *pSrc++;
681                 OPJ_INT32 val1 = *pSrc++;
682                 OPJ_INT32 val2 = *pSrc++;
683                 OPJ_INT32 val3 = *pSrc++;
684                 OPJ_INT32 val4 = *pSrc++;
685                 OPJ_INT32 val5 = *pSrc++;
686                 OPJ_INT32 val6 = *pSrc++;
687                 
688                 pDst[i+0] = (val0 << 6) | (val1 >> 2);
689                 pDst[i+1] = ((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4);
690                 pDst[i+2] = ((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6);
691                 pDst[i+3] = ((val5 & 0x3FU) << 8) | val6;
692                 
693         }
694         if (length & 3U) {
695                 OPJ_INT32 val0 = *pSrc++;
696                 OPJ_INT32 val1 = *pSrc++;
697                 length = length & 3U;
698                 pDst[i+0] = (val0 << 6) | (val1 >> 2);
699                 
700                 if (length > 1U) {
701                         OPJ_INT32 val2 = *pSrc++;
702                         OPJ_INT32 val3 = *pSrc++;
703                         pDst[i+1] = ((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4);
704                         if (length > 2U) {
705                                 OPJ_INT32 val4 = *pSrc++;
706                                 OPJ_INT32 val5 = *pSrc++;
707                                 pDst[i+2] = ((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6);
708                         }
709                 }
710         }
711 }
712 #if 0
713 static void tif_16uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
714 {
715         OPJ_SIZE_T i;
716         for (i = 0; i < length; i++) {
717                 OPJ_INT32 val0 = *pSrc++;
718                 OPJ_INT32 val1 = *pSrc++;
719 #ifdef OPJ_BIG_ENDIAN
720                 pDst[i] = (val0 << 8) | val1;
721 #else
722                 pDst[i] = (val1 << 8) | val0;
723 #endif
724         }
725 }
726 #else
727 /* seems that libtiff decodes this to machine endianness */
728 static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
729 {
730         OPJ_SIZE_T i;
731         for (i = 0; i < length; i++) {
732                 pDst[i] = pSrc[i];
733         }
734 }
735 #endif
736
737 typedef void (* convert_32s_CXPX)(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length);
738 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
739 {
740         memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
741 }
742 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
743 {
744         OPJ_SIZE_T i;
745         OPJ_INT32* pDst0 = pDst[0];
746         OPJ_INT32* pDst1 = pDst[1];
747         
748         for (i = 0; i < length; i++) {
749                 pDst0[i] = pSrc[2*i+0];
750                 pDst1[i] = pSrc[2*i+1];
751         }
752 }
753 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
754 {
755         OPJ_SIZE_T i;
756         OPJ_INT32* pDst0 = pDst[0];
757         OPJ_INT32* pDst1 = pDst[1];
758         OPJ_INT32* pDst2 = pDst[2];
759         
760         for (i = 0; i < length; i++) {
761                 pDst0[i] = pSrc[3*i+0];
762                 pDst1[i] = pSrc[3*i+1];
763                 pDst2[i] = pSrc[3*i+2];
764         }
765 }
766 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
767 {
768         OPJ_SIZE_T i;
769         OPJ_INT32* pDst0 = pDst[0];
770         OPJ_INT32* pDst1 = pDst[1];
771         OPJ_INT32* pDst2 = pDst[2];
772         OPJ_INT32* pDst3 = pDst[3];
773         
774         for (i = 0; i < length; i++) {
775                 pDst0[i] = pSrc[4*i+0];
776                 pDst1[i] = pSrc[4*i+1];
777                 pDst2[i] = pSrc[4*i+2];
778                 pDst3[i] = pSrc[4*i+3];
779         }
780 }
781
782
783 /*
784  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
785  * CINEMA                 : 12 bit precision
786  */
787 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
788 {
789         int subsampling_dx = parameters->subsampling_dx;
790         int subsampling_dy = parameters->subsampling_dy;
791         TIFF *tif;
792         tdata_t buf;
793         tstrip_t strip;
794         tsize_t strip_size;
795         int j, currentPlane, numcomps = 0, w, h;
796         OPJ_COLOR_SPACE color_space;
797         opj_image_cmptparm_t cmptparm[4]; /* RGBA */
798         opj_image_t *image = NULL;
799         int has_alpha = 0;
800         unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
801         unsigned int tiWidth, tiHeight;
802         OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
803         tif_Xto32s cvtTifTo32s = NULL;
804         convert_32s_CXPX cvtCxToPx = NULL;
805         OPJ_INT32* buffer32s = NULL;
806         OPJ_INT32* planes[4];
807         OPJ_SIZE_T rowStride;
808         
809         tif = TIFFOpen(filename, "r");
810         
811         if(!tif)
812         {
813                 fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
814                 return 0;
815         }
816         tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
817         tiWidth = tiHeight = 0;
818         
819         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
820         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
821         TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
822         TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
823         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
824         TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
825         TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
826         w= (int)tiWidth;
827         h= (int)tiHeight;
828         
829         if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) tiBps = 0U;
830         if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) tiPhoto = 0;
831         
832         if( !tiBps || !tiPhoto)
833         {
834                 if( !tiBps)
835                         fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
836                 else
837                         if( !tiPhoto)
838                                 fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
839                                                                 " and GRAY(A) has been implemented\n",(int) tiPhoto);
840                 
841                 fprintf(stderr,"\tAborting\n");
842                 TIFFClose(tif);
843                 return NULL;
844         }
845         
846         switch (tiBps) {
847                 case 1:
848                         cvtTifTo32s = tif_1uto32s;
849                         break;
850                 case 2:
851                         cvtTifTo32s = tif_2uto32s;
852                         break;
853                 case 4:
854                         cvtTifTo32s = tif_4uto32s;
855                         break;
856                 case 6:
857                         cvtTifTo32s = tif_6uto32s;
858                         break;
859                 case 8:
860                         cvtTifTo32s = tif_8uto32s;
861                         break;
862                 case 10:
863                         cvtTifTo32s = tif_10uto32s;
864                         break;
865                 case 12:
866                         cvtTifTo32s = tif_12uto32s;
867                         break;
868                 case 14:
869                         cvtTifTo32s = tif_14uto32s;
870                         break;
871                 case 16:
872                         cvtTifTo32s = (tif_Xto32s)tif_16uto32s;
873                         break;
874                 default:
875                         /* never here */
876                         break;
877         }
878         
879         {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
880                 uint16* sampleinfo;
881                 uint16 extrasamples;
882                 
883                 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
884                                                                                                         &extrasamples, &sampleinfo);
885                 
886                 if(extrasamples >= 1)
887                 {
888                         switch(sampleinfo[0])
889                         {
890                                 case EXTRASAMPLE_UNSPECIFIED:
891                                         /* Workaround for some images without correct info about alpha channel
892                                          */
893                                         if(tiSpp > 3)
894                                                 has_alpha = 1;
895                                         break;
896                                         
897                                 case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
898                                 case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
899                                         has_alpha = 1;
900                                         break;
901                         }
902                 }
903                 else /* extrasamples == 0 */
904                         if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
905         }
906         
907         /* initialize image components */
908         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
909         
910         if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
911                 fprintf(stdout,"WARNING:\n"
912                                                 "Input image bitdepth is %d bits\n"
913                                                 "TIF conversion has automatically rescaled to 12-bits\n"
914                                                 "to comply with cinema profiles.\n",
915                                                 tiBps);
916         } else {
917                 is_cinema = 0U;
918         }
919         
920         if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
921         {
922                 numcomps = 3 + has_alpha;
923                 color_space = OPJ_CLRSPC_SRGB;
924         }
925         else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
926         {
927                 numcomps = 1 + has_alpha;
928                 color_space = OPJ_CLRSPC_GRAY;
929         }
930         
931         switch (numcomps) {
932                 case 1:
933                         cvtCxToPx = convert_32s_C1P1;
934                         break;
935                 case 2:
936                         cvtCxToPx = convert_32s_C2P2;
937                         break;
938                 case 3:
939                         cvtCxToPx = convert_32s_C3P3;
940                         break;
941                 case 4:
942                         cvtCxToPx = convert_32s_C4P4;
943                         break;
944                 default:
945                         /* never here */
946                         break;
947         }
948         if (tiPC == PLANARCONFIG_SEPARATE) {
949                 cvtCxToPx = convert_32s_C1P1; /* override */
950                 tiSpp = 1U; /* consider only one sample per plane */
951         }
952
953         for(j = 0; j < numcomps; j++)
954         {
955                 cmptparm[j].prec = tiBps;
956                 cmptparm[j].bpp = tiBps;
957                 cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
958                 cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
959                 cmptparm[j].w = (OPJ_UINT32)w;
960                 cmptparm[j].h = (OPJ_UINT32)h;
961         }
962                 
963         image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
964         if(!image)
965         {
966                 TIFFClose(tif);
967                 return NULL;
968         }
969         /* set image offset and reference grid */
970         image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
971         image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
972         image->x1 =     !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
973         image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
974         image->y1 =     !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
975         image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
976
977         for(j = 0; j < numcomps; j++)
978         {
979                 planes[j] = image->comps[j].data;
980         }
981                 
982         strip_size = TIFFStripSize(tif);
983         
984         buf = _TIFFmalloc(strip_size);
985         if (buf == NULL) {
986                 TIFFClose(tif);
987                 opj_image_destroy(image);
988                 return NULL;
989         }
990         rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
991         buffer32s = malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
992         if (buffer32s == NULL) {
993                 _TIFFfree(buf);
994                 TIFFClose(tif);
995                 opj_image_destroy(image);
996                 return NULL;
997         }
998         
999         strip = 0;
1000         currentPlane = 0;
1001         do
1002         {
1003                 planes[0] = image->comps[currentPlane].data; /* to manage planar data */
1004                 h= (int)tiHeight;
1005                 /* Read the Image components */
1006                 for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
1007                 {
1008                                 const OPJ_UINT8 *dat8;
1009                                 tsize_t ssize;
1010                                 
1011                                 ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
1012                                 dat8 = (const OPJ_UINT8*)buf;
1013                                 
1014                                 while (ssize >= rowStride) {
1015                                         cvtTifTo32s(dat8, buffer32s, w * tiSpp);
1016                                         cvtCxToPx(buffer32s, planes, w);
1017                                         planes[0] += w;
1018                                         planes[1] += w;
1019                                         planes[2] += w;
1020                                         planes[3] += w;
1021                                         dat8  += rowStride;
1022                                         ssize -= rowStride;
1023                                         h--;
1024                                 }
1025                 }
1026                 currentPlane++;
1027         } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
1028         
1029         free(buffer32s);
1030         _TIFFfree(buf);
1031         TIFFClose(tif);
1032         
1033         if (is_cinema) {
1034                 for (j=0; j < numcomps; ++j) {
1035                         scale_component(&(image->comps[j]), 12);
1036                 }
1037                 
1038         }
1039         return image;
1040
1041 }/* tiftoimage() */
1042