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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
38 #include "opj_apps_config.h"
49 * Get logarithm of an integer and round downwards.
53 static int int_floorlog2(int a) {
55 for (l = 0; a > 1; l++) {
61 /* Component precision scaling */
62 void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision)
66 OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32)-1);
68 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
70 umax = (1U << precision) - 1U;
73 if (component->sgnd) {
74 OPJ_INT32* l_data = component->data;
75 OPJ_INT32 max = (OPJ_INT32)(umax / 2U);
76 OPJ_INT32 min = -max - 1;
77 for (i = 0; i < len; ++i) {
78 if (l_data[i] > max) {
80 } else if (l_data[i] < min) {
85 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
86 for (i = 0; i < len; ++i) {
87 if (l_data[i] > umax) {
92 component->prec = precision;
95 /* Component precision scaling */
96 static void scale_component_up(opj_image_comp_t* component, OPJ_UINT32 precision)
100 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
101 if (component->sgnd) {
102 OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1));
103 OPJ_INT64 oldMax = (OPJ_INT64)(1U << (component->prec - 1));
104 OPJ_INT32* l_data = component->data;
105 for (i = 0; i < len; ++i) {
106 l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax);
109 OPJ_UINT64 newMax = (OPJ_UINT64)((1U << precision) - 1U);
110 OPJ_UINT64 oldMax = (OPJ_UINT64)((1U << component->prec) - 1U);
111 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
112 for (i = 0; i < len; ++i) {
113 l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax);
116 component->prec = precision;
118 void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
123 if (component->prec == precision) {
126 if (component->prec < precision) {
127 scale_component_up(component, precision);
130 shift = (int)(component->prec - precision);
131 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
132 if (component->sgnd) {
133 OPJ_INT32* l_data = component->data;
134 for (i = 0; i < len; ++i) {
138 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
139 for (i = 0; i < len; ++i) {
143 component->bpp = precision;
144 component->prec = precision;
148 /* planar / interleaved conversions */
149 /* used by PNG/TIFF */
150 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
152 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
154 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
157 OPJ_INT32* pDst0 = pDst[0];
158 OPJ_INT32* pDst1 = pDst[1];
160 for (i = 0; i < length; i++) {
161 pDst0[i] = pSrc[2*i+0];
162 pDst1[i] = pSrc[2*i+1];
165 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
168 OPJ_INT32* pDst0 = pDst[0];
169 OPJ_INT32* pDst1 = pDst[1];
170 OPJ_INT32* pDst2 = pDst[2];
172 for (i = 0; i < length; i++) {
173 pDst0[i] = pSrc[3*i+0];
174 pDst1[i] = pSrc[3*i+1];
175 pDst2[i] = pSrc[3*i+2];
178 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
181 OPJ_INT32* pDst0 = pDst[0];
182 OPJ_INT32* pDst1 = pDst[1];
183 OPJ_INT32* pDst2 = pDst[2];
184 OPJ_INT32* pDst3 = pDst[3];
186 for (i = 0; i < length; i++) {
187 pDst0[i] = pSrc[4*i+0];
188 pDst1[i] = pSrc[4*i+1];
189 pDst2[i] = pSrc[4*i+2];
190 pDst3[i] = pSrc[4*i+3];
193 const convert_32s_CXPX convert_32s_CXPX_LUT[5] = {
201 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
204 const OPJ_INT32* pSrc0 = pSrc[0];
206 for (i = 0; i < length; i++) {
207 pDst[i] = pSrc0[i] + adjust;
210 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
213 const OPJ_INT32* pSrc0 = pSrc[0];
214 const OPJ_INT32* pSrc1 = pSrc[1];
216 for (i = 0; i < length; i++) {
217 pDst[2*i+0] = pSrc0[i] + adjust;
218 pDst[2*i+1] = pSrc1[i] + adjust;
221 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
224 const OPJ_INT32* pSrc0 = pSrc[0];
225 const OPJ_INT32* pSrc1 = pSrc[1];
226 const OPJ_INT32* pSrc2 = pSrc[2];
228 for (i = 0; i < length; i++) {
229 pDst[3*i+0] = pSrc0[i] + adjust;
230 pDst[3*i+1] = pSrc1[i] + adjust;
231 pDst[3*i+2] = pSrc2[i] + adjust;
234 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
237 const OPJ_INT32* pSrc0 = pSrc[0];
238 const OPJ_INT32* pSrc1 = pSrc[1];
239 const OPJ_INT32* pSrc2 = pSrc[2];
240 const OPJ_INT32* pSrc3 = pSrc[3];
242 for (i = 0; i < length; i++) {
243 pDst[4*i+0] = pSrc0[i] + adjust;
244 pDst[4*i+1] = pSrc1[i] + adjust;
245 pDst[4*i+2] = pSrc2[i] + adjust;
246 pDst[4*i+3] = pSrc3[i] + adjust;
249 const convert_32s_PXCX convert_32s_PXCX_LUT[5] = {
257 /* bit depth conversions */
258 /* used by PNG/TIFF up to 8bpp */
259 static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
262 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
263 OPJ_UINT32 val = *pSrc++;
264 pDst[i+0] = (OPJ_INT32)( val >> 7);
265 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
266 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
267 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
268 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
269 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
270 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
271 pDst[i+7] = (OPJ_INT32)(val & 0x1U);
274 OPJ_UINT32 val = *pSrc++;
275 length = length & 7U;
276 pDst[i+0] = (OPJ_INT32)(val >> 7);
279 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
281 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
283 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
285 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
287 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
289 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
298 static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
301 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
302 OPJ_UINT32 val = *pSrc++;
303 pDst[i+0] = (OPJ_INT32)( val >> 6);
304 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
305 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
306 pDst[i+3] = (OPJ_INT32)(val & 0x3U);
309 OPJ_UINT32 val = *pSrc++;
310 length = length & 3U;
311 pDst[i+0] = (OPJ_INT32)(val >> 6);
314 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
316 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
322 static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
325 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
326 OPJ_UINT32 val = *pSrc++;
327 pDst[i+0] = (OPJ_INT32)(val >> 4);
328 pDst[i+1] = (OPJ_INT32)(val & 0xFU);
331 OPJ_UINT8 val = *pSrc++;
332 pDst[i+0] = (OPJ_INT32)(val >> 4);
335 static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
338 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
339 OPJ_UINT32 val0 = *pSrc++;
340 OPJ_UINT32 val1 = *pSrc++;
341 OPJ_UINT32 val2 = *pSrc++;
342 pDst[i+0] = (OPJ_INT32)(val0 >> 2);
343 pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
344 pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
345 pDst[i+3] = (OPJ_INT32)(val2 & 0x3FU);
349 OPJ_UINT32 val0 = *pSrc++;
350 length = length & 3U;
351 pDst[i+0] = (OPJ_INT32)(val0 >> 2);
354 OPJ_UINT32 val1 = *pSrc++;
355 pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
357 OPJ_UINT32 val2 = *pSrc++;
358 pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
363 static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
366 for (i = 0; i < length; i++) {
370 const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = {
383 static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
386 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
387 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
388 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
389 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
390 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
391 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
392 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
393 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
394 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
396 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
400 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
401 OPJ_UINT32 src1 = 0U;
402 OPJ_UINT32 src2 = 0U;
403 OPJ_UINT32 src3 = 0U;
404 OPJ_UINT32 src4 = 0U;
405 OPJ_UINT32 src5 = 0U;
406 OPJ_UINT32 src6 = 0U;
407 length = length & 7U;
410 src1 = (OPJ_UINT32)pSrc[i+1];
412 src2 = (OPJ_UINT32)pSrc[i+2];
414 src3 = (OPJ_UINT32)pSrc[i+3];
416 src4 = (OPJ_UINT32)pSrc[i+4];
418 src5 = (OPJ_UINT32)pSrc[i+5];
420 src6 = (OPJ_UINT32)pSrc[i+6];
427 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1));
431 static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
434 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
435 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
436 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
437 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
438 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
440 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
444 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
445 OPJ_UINT32 src1 = 0U;
446 OPJ_UINT32 src2 = 0U;
447 length = length & 3U;
450 src1 = (OPJ_UINT32)pSrc[i+1];
452 src2 = (OPJ_UINT32)pSrc[i+2];
455 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
459 static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
462 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
463 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
464 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
466 *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
470 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
471 *pDst++ = (OPJ_BYTE)((src0 << 4));
475 static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
478 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
479 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
480 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
481 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
482 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
484 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
485 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
486 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
490 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
491 OPJ_UINT32 src1 = 0U;
492 OPJ_UINT32 src2 = 0U;
493 length = length & 3U;
496 src1 = (OPJ_UINT32)pSrc[i+1];
498 src2 = (OPJ_UINT32)pSrc[i+2];
501 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
503 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
505 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
510 static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
513 for (i = 0; i < length; ++i) {
514 pDst[i] = (OPJ_BYTE)pSrc[i];
517 const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = {
529 /* -->> -->> -->> -->>
533 <<-- <<-- <<-- <<-- */
535 #ifdef INFORMATION_ONLY
536 /* TGA header definition. */
539 unsigned char id_length; /* Image id field length */
540 unsigned char colour_map_type; /* Colour map type */
541 unsigned char image_type; /* Image type */
543 ** Colour map specification
545 unsigned short colour_map_index; /* First entry index */
546 unsigned short colour_map_length; /* Colour map length */
547 unsigned char colour_map_entry_size; /* Colour map entry size */
549 ** Image specification
551 unsigned short x_origin; /* x origin of image */
552 unsigned short y_origin; /* u origin of image */
553 unsigned short image_width; /* Image width */
554 unsigned short image_height; /* Image height */
555 unsigned char pixel_depth; /* Pixel depth */
556 unsigned char image_desc; /* Image descriptor */
558 #endif /* INFORMATION_ONLY */
560 static unsigned short get_ushort(unsigned short val) {
562 #ifdef OPJ_BIG_ENDIAN
563 return (unsigned short)(((val & 0xffU) << 8) | (val >> 8));
570 #define TGA_HEADER_SIZE 18
572 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
573 unsigned int *width, unsigned int *height, int *flip_image)
576 unsigned char tga[TGA_HEADER_SIZE];
577 unsigned char id_len, /*cmap_type,*/ image_type;
578 unsigned char pixel_depth, image_desc;
579 unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
580 unsigned short /*x_origin, y_origin,*/ image_w, image_h;
582 if (!bits_per_pixel || !width || !height || !flip_image)
585 if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
587 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
590 id_len = (unsigned char)tga[0];
591 /*cmap_type = (unsigned char)tga[1];*/
592 image_type = (unsigned char)tga[2];
593 /*cmap_index = get_ushort(*(unsigned short*)(&tga[3]));*/
594 cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
595 cmap_entry_size = (unsigned char)tga[7];
599 x_origin = get_ushort(*(unsigned short*)(&tga[8]));
600 y_origin = get_ushort(*(unsigned short*)(&tga[10]));
602 image_w = get_ushort(*(unsigned short*)(&tga[12]));
603 image_h = get_ushort(*(unsigned short*)(&tga[14]));
604 pixel_depth = (unsigned char)tga[16];
605 image_desc = (unsigned char)tga[17];
607 *bits_per_pixel = (unsigned int)pixel_depth;
608 *width = (unsigned int)image_w;
609 *height = (unsigned int)image_h;
611 /* Ignore tga identifier, if present ... */
614 unsigned char *id = (unsigned char *) malloc(id_len);
615 if ( !fread(id, id_len, 1, fp) )
617 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
624 /* Test for compressed formats ... not yet supported ...
625 // Note :- 9 - RLE encoded palettized.
626 // 10 - RLE encoded RGB. */
629 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
633 *flip_image = !(image_desc & 32);
635 /* Palettized formats are not yet supported, skip over the palette, if present ... */
636 palette_size = cmap_len * (cmap_entry_size/8);
640 fprintf(stderr, "File contains a palette - not yet supported.");
641 fseek(fp, palette_size, SEEK_CUR);
646 #ifdef OPJ_BIG_ENDIAN
648 static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
650 return (OPJ_UINT16)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
655 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
658 OPJ_UINT16 image_w, image_h, us0;
659 unsigned char uc0, image_type;
660 unsigned char pixel_depth, image_desc;
662 if (!bits_per_pixel || !width || !height)
667 if ( bits_per_pixel < 256 )
668 pixel_depth = (unsigned char)bits_per_pixel;
670 fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
675 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
676 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
678 image_type = 2; /* Uncompressed. */
679 if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
682 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
683 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
684 if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
686 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
687 if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
689 image_w = (unsigned short)width;
690 image_h = (unsigned short) height;
692 #ifndef OPJ_BIG_ENDIAN
693 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
694 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
696 image_w = swap16(image_w);
697 image_h = swap16(image_h);
698 if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
699 if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
702 if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
704 image_desc = 8; /* 8 bits per component. */
708 if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
713 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
717 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
720 unsigned int image_width, image_height, pixel_bit_depth;
723 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
725 OPJ_COLOR_SPACE color_space;
728 int subsampling_dx, subsampling_dy;
731 f = fopen(filename, "rb");
733 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
737 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
740 /* We currently only support 24 & 32 bit tga's ... */
741 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
744 /* initialize image components */
745 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
747 mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
748 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
751 color_space = OPJ_CLRSPC_GRAY;
752 numcomps = save_alpha ? 2 : 1;
755 numcomps = save_alpha ? 4 : 3;
756 color_space = OPJ_CLRSPC_SRGB;
759 subsampling_dx = parameters->subsampling_dx;
760 subsampling_dy = parameters->subsampling_dy;
762 for (i = 0; i < numcomps; i++) {
763 cmptparm[i].prec = 8;
765 cmptparm[i].sgnd = 0;
766 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
767 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
768 cmptparm[i].w = image_width;
769 cmptparm[i].h = image_height;
772 /* create the image */
773 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
778 /* set image offset and reference grid */
779 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
780 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
781 image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1;
782 image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1;
785 for (y=0; y < image_height; y++)
790 index = (int)((image_height-y-1)*image_width);
792 index = (int)(y*image_width);
796 for (x=0;x<image_width;x++)
800 if( !fread(&b, 1, 1, f) )
802 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
803 opj_image_destroy(image);
806 if ( !fread(&g, 1, 1, f) )
808 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
809 opj_image_destroy(image);
812 if ( !fread(&r, 1, 1, f) )
814 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
815 opj_image_destroy(image);
819 image->comps[0].data[index]=r;
820 image->comps[1].data[index]=g;
821 image->comps[2].data[index]=b;
825 else if (numcomps==4)
827 for (x=0;x<image_width;x++)
829 unsigned char r,g,b,a;
830 if ( !fread(&b, 1, 1, f) )
832 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
833 opj_image_destroy(image);
836 if ( !fread(&g, 1, 1, f) )
838 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
839 opj_image_destroy(image);
842 if ( !fread(&r, 1, 1, f) )
844 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
845 opj_image_destroy(image);
848 if ( !fread(&a, 1, 1, f) )
850 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
851 opj_image_destroy(image);
855 image->comps[0].data[index]=r;
856 image->comps[1].data[index]=g;
857 image->comps[2].data[index]=b;
858 image->comps[3].data[index]=a;
863 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
869 int imagetotga(opj_image_t * image, const char *outfile) {
870 int width, height, bpp, x, y;
871 OPJ_BOOL write_alpha;
873 int adjustR, adjustG, adjustB, fails;
874 unsigned int alpha_channel;
882 fdest = fopen(outfile, "wb");
884 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
888 for (i = 0; i < image->numcomps-1; i++) {
889 if ((image->comps[0].dx != image->comps[i+1].dx)
890 ||(image->comps[0].dy != image->comps[i+1].dy)
891 ||(image->comps[0].prec != image->comps[i+1].prec)) {
892 fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
897 width = (int)image->comps[0].w;
898 height = (int)image->comps[0].h;
900 /* Mono with alpha, or RGB with alpha. */
901 write_alpha = (image->numcomps==2) || (image->numcomps==4);
903 /* Write TGA header */
904 bpp = write_alpha ? 32 : 24;
906 if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
909 alpha_channel = image->numcomps-1;
911 scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
913 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
914 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
915 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
917 for (y=0; y < height; y++)
919 unsigned int index= (unsigned int)(y*width);
921 for (x=0; x < width; x++, index++)
923 r = (float)(image->comps[0].data[index] + adjustR);
925 if (image->numcomps > 2)
927 g = (float)(image->comps[1].data[index] + adjustG);
928 b = (float)(image->comps[2].data[index] + adjustB);
936 /* TGA format writes BGR ... */
937 if(b > 255.) b = 255.; else if(b < 0.) b = 0.;
938 value = (unsigned char)(b*scale);
939 res = fwrite(&value,1,1,fdest);
943 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
946 if(g > 255.) g = 255.; else if(g < 0.) g = 0.;
947 value = (unsigned char)(g*scale);
948 res = fwrite(&value,1,1,fdest);
952 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
955 if(r > 255.) r = 255.; else if(r < 0.) r = 0.;
956 value = (unsigned char)(r*scale);
957 res = fwrite(&value,1,1,fdest);
961 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
967 a = (float)(image->comps[alpha_channel].data[index]);
968 if(a > 255.) a = 255.; else if(a < 0.) a = 0.;
969 value = (unsigned char)(a*scale);
970 res = fwrite(&value,1,1,fdest);
974 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
987 /* -->> -->> -->> -->>
991 <<-- <<-- <<-- <<-- */
994 static unsigned char readuchar(FILE * f)
997 if ( !fread(&c1, 1, 1, f) )
999 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1005 static unsigned short readushort(FILE * f, int bigendian)
1007 unsigned char c1, c2;
1008 if ( !fread(&c1, 1, 1, f) )
1010 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1013 if ( !fread(&c2, 1, 1, f) )
1015 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1019 return (unsigned short)((c1 << 8) + c2);
1021 return (unsigned short)((c2 << 8) + c1);
1024 static unsigned int readuint(FILE * f, int bigendian)
1026 unsigned char c1, c2, c3, c4;
1027 if ( !fread(&c1, 1, 1, f) )
1029 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1032 if ( !fread(&c2, 1, 1, f) )
1034 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1037 if ( !fread(&c3, 1, 1, f) )
1039 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1042 if ( !fread(&c4, 1, 1, f) )
1044 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1048 return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(c3 << 8) + c4;
1050 return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1;
1053 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
1056 int i, numcomps, max;
1057 OPJ_COLOR_SPACE color_space;
1058 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1059 opj_image_t * image = NULL;
1060 int adjustS, ushift, dshift, force8;
1062 char endian1,endian2,sign;
1067 opj_image_comp_t *comp = NULL;
1070 color_space = OPJ_CLRSPC_GRAY;
1072 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1076 f = fopen(filename, "rb");
1078 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1082 fseek(f, 0, SEEK_SET);
1083 if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
1084 fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1090 while (signtmp[i]!='\0') {
1091 if (signtmp[i]=='-') sign='-';
1096 if (endian1=='M' && endian2=='L') {
1098 } else if (endian2=='M' && endian1=='L') {
1101 fprintf(stderr, "Bad pgx header, please check input file\n");
1105 /* initialize image component */
1107 cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
1108 cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
1109 cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx + 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx + 1;
1110 cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy + 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy + 1;
1120 ushift = 8 - prec; dshift = prec - ushift;
1121 if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
1125 else ushift = dshift = force8 = adjustS = 0;
1127 cmptparm.prec = (OPJ_UINT32)prec;
1128 cmptparm.bpp = (OPJ_UINT32)prec;
1129 cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
1130 cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
1132 /* create the image */
1133 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
1138 /* set image offset and reference grid */
1139 image->x0 = cmptparm.x0;
1140 image->y0 = cmptparm.x0;
1141 image->x1 = cmptparm.w;
1142 image->y1 = cmptparm.h;
1144 /* set image data */
1146 comp = &image->comps[0];
1148 for (i = 0; i < w * h; i++) {
1152 v = readuchar(f) + adjustS;
1153 v = (v<<ushift) + (v>>dshift);
1154 comp->data[i] = (unsigned char)v;
1156 if(v > max) max = v;
1160 if (comp->prec == 8) {
1164 v = (char) readuchar(f);
1166 } else if (comp->prec <= 16) {
1168 v = readushort(f, bigendian);
1170 v = (short) readushort(f, bigendian);
1174 v = (int)readuint(f, bigendian);
1176 v = (int) readuint(f, bigendian);
1184 comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
1189 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
1191 static INLINE int clamp( const int value, const int prec, const int sgnd )
1195 if (prec <= 8) return CLAMP(value,-128,127);
1196 else if (prec <= 16) return CLAMP(value,-32768,32767);
1197 else return CLAMP(value,-2147483647-1,2147483647);
1201 if (prec <= 8) return CLAMP(value,0,255);
1202 else if (prec <= 16) return CLAMP(value,0,65535);
1203 else return value; /*CLAMP(value,0,4294967295);*/
1207 int imagetopgx(opj_image_t * image, const char *outfile)
1210 int i, j, fails = 1;
1211 unsigned int compno;
1214 for (compno = 0; compno < image->numcomps; compno++)
1216 opj_image_comp_t *comp = &image->comps[compno];
1217 char bname[256]; /* buffer for name */
1218 char *name = bname; /* pointer */
1221 const size_t olen = strlen(outfile);
1222 const size_t dotpos = olen - 4;
1223 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1225 if( outfile[dotpos] != '.' )
1227 /* `pgx` was recognized but there is no dot at expected position */
1228 fprintf(stderr, "ERROR -> Impossible happen." );
1233 name = (char*)malloc(total+1);
1235 strncpy(name, outfile, dotpos);
1236 sprintf(name+dotpos, "_%d.pgx", compno);
1237 fdest = fopen(name, "wb");
1238 /* dont need name anymore */
1239 if( total > 256 ) free(name);
1242 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1246 w = (int)image->comps[compno].w;
1247 h = (int)image->comps[compno].h;
1249 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
1252 if (comp->prec <= 8)
1254 else if (comp->prec <= 16)
1259 for (i = 0; i < w * h; i++)
1261 /* FIXME: clamp func is being called within a loop */
1262 const int val = clamp(image->comps[compno].data[i],
1263 (int)comp->prec, (int)comp->sgnd);
1265 for (j = nbytes - 1; j >= 0; j--)
1267 int v = (int)(val >> (j * 8));
1268 unsigned char byte = (unsigned char)v;
1269 res = fwrite(&byte, 1, 1, fdest);
1273 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1278 fclose(fdest); fdest = NULL;
1282 if(fdest) fclose(fdest);
1287 /* -->> -->> -->> -->>
1291 <<-- <<-- <<-- <<-- */
1295 int width, height, maxval, depth, format;
1296 char rgb, rgba, gray, graya, bw;
1300 static char *skip_white(char *s)
1304 if(*s == '\n' || *s == '\r') return NULL;
1305 if(isspace(*s)) { ++s; continue; }
1311 static char *skip_int(char *start, int *out_n)
1316 *out_n = 0; s = start;
1318 s = skip_white(start);
1319 if(s == NULL) return NULL;
1324 if( !isdigit(*s)) break;
1327 c = *s; *s = 0; *out_n = atoi(start); *s = c;
1331 static char *skip_idf(char *start, char out_idf[256])
1336 s = skip_white(start);
1337 if(s == NULL) return NULL;
1342 if(isalpha(*s) || *s == '_') { ++s; continue; }
1345 c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
1349 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1351 int format, have_wh, end, ttype;
1352 char idf[256], type[256];
1355 if (fgets(line, 250, reader) == NULL)
1357 fprintf(stderr,"\nWARNING: fgets return a NULL value");
1363 fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
1365 format = atoi(line + 1);
1366 if(format < 1 || format > 7)
1368 fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
1371 ph->format = format;
1372 ttype = end = have_wh = 0;
1374 while(fgets(line, 250, reader))
1378 if(*line == '#') continue;
1384 s = skip_idf(s, idf);
1386 if(s == NULL || *s == 0) return;
1388 if(strcmp(idf, "ENDHDR") == 0)
1392 if(strcmp(idf, "WIDTH") == 0)
1394 s = skip_int(s, &ph->width);
1395 if(s == NULL || *s == 0) return;
1399 if(strcmp(idf, "HEIGHT") == 0)
1401 s = skip_int(s, &ph->height);
1402 if(s == NULL || *s == 0) return;
1406 if(strcmp(idf, "DEPTH") == 0)
1408 s = skip_int(s, &ph->depth);
1409 if(s == NULL || *s == 0) return;
1413 if(strcmp(idf, "MAXVAL") == 0)
1415 s = skip_int(s, &ph->maxval);
1416 if(s == NULL || *s == 0) return;
1420 if(strcmp(idf, "TUPLTYPE") == 0)
1422 s = skip_idf(s, type);
1423 if(s == NULL || *s == 0) return;
1425 if(strcmp(type, "BLACKANDWHITE") == 0)
1427 ph->bw = 1; ttype = 1; continue;
1429 if(strcmp(type, "GRAYSCALE") == 0)
1431 ph->gray = 1; ttype = 1; continue;
1433 if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
1435 ph->graya = 1; ttype = 1; continue;
1437 if(strcmp(type, "RGB") == 0)
1439 ph->rgb = 1; ttype = 1; continue;
1441 if(strcmp(type, "RGB_ALPHA") == 0)
1443 ph->rgba = 1; ttype = 1; continue;
1445 fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
1448 fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
1450 } /* if(format == 7) */
1454 s = skip_int(s, &ph->width);
1456 s = skip_int(s, &ph->height);
1460 if(format == 1 || format == 4) break;
1462 if(format == 2 || format == 3 || format == 5 || format == 6)
1464 if (skip_int(s, &ph->maxval) != NULL) {
1465 if(ph->maxval > 65535) {
1475 if(format == 2 || format == 3 || format == 5 || format == 6)
1477 /* P2, P3, P5, P6: */
1478 s = skip_int(s, &ph->maxval);
1480 if(ph->maxval > 65535) return;
1483 }/* while(fgets( ) */
1484 if(format == 2 || format == 3 || format > 4)
1486 if(ph->maxval < 1 || ph->maxval > 65535) return;
1488 if(ph->width < 1 || ph->height < 1) return;
1494 fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
1496 if(ph->depth < 1 || ph->depth > 4) return;
1498 if(ph->width && ph->height && ph->depth && ph->maxval && ttype)
1503 if(format != 1 && format != 4)
1505 if(ph->width && ph->height && ph->maxval) ph->ok = 1;
1509 if(ph->width && ph->height) ph->ok = 1;
1515 static int has_prec(int val)
1517 if(val < 2) return 1;
1518 if(val < 4) return 2;
1519 if(val < 8) return 3;
1520 if(val < 16) return 4;
1521 if(val < 32) return 5;
1522 if(val < 64) return 6;
1523 if(val < 128) return 7;
1524 if(val < 256) return 8;
1525 if(val < 512) return 9;
1526 if(val < 1024) return 10;
1527 if(val < 2048) return 11;
1528 if(val < 4096) return 12;
1529 if(val < 8192) return 13;
1530 if(val < 16384) return 14;
1531 if(val < 32768) return 15;
1535 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
1536 int subsampling_dx = parameters->subsampling_dx;
1537 int subsampling_dy = parameters->subsampling_dy;
1540 int i, compno, numcomps, w, h, prec, format;
1541 OPJ_COLOR_SPACE color_space;
1542 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1543 opj_image_t * image = NULL;
1544 struct pnm_header header_info;
1546 if((fp = fopen(filename, "rb")) == NULL)
1548 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
1551 memset(&header_info, 0, sizeof(struct pnm_header));
1553 read_pnm_header(fp, &header_info);
1555 if(!header_info.ok) { fclose(fp); return NULL; }
1557 format = header_info.format;
1561 case 1: /* ascii bitmap */
1562 case 4: /* raw bitmap */
1566 case 2: /* ascii greymap */
1567 case 5: /* raw greymap */
1571 case 3: /* ascii pixmap */
1572 case 6: /* raw pixmap */
1576 case 7: /* arbitrary map */
1577 numcomps = header_info.depth;
1580 default: fclose(fp); return NULL;
1583 color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */
1585 color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */
1587 prec = has_prec(header_info.maxval);
1589 if(prec < 8) prec = 8;
1591 w = header_info.width;
1592 h = header_info.height;
1593 subsampling_dx = parameters->subsampling_dx;
1594 subsampling_dy = parameters->subsampling_dy;
1596 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
1598 for(i = 0; i < numcomps; i++)
1600 cmptparm[i].prec = (OPJ_UINT32)prec;
1601 cmptparm[i].bpp = (OPJ_UINT32)prec;
1602 cmptparm[i].sgnd = 0;
1603 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
1604 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
1605 cmptparm[i].w = (OPJ_UINT32)w;
1606 cmptparm[i].h = (OPJ_UINT32)h;
1608 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
1610 if(!image) { fclose(fp); return NULL; }
1612 /* set image offset and reference grid */
1613 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
1614 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
1615 image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1);
1616 image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1);
1618 if((format == 2) || (format == 3)) /* ascii pixmap */
1622 for (i = 0; i < w * h; i++)
1624 for(compno = 0; compno < numcomps; compno++)
1627 if (fscanf(fp, "%u", &index) != 1)
1628 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1630 image->comps[compno].data[i] = (OPJ_INT32)(index * 255)/header_info.maxval;
1638 && ( header_info.gray || header_info.graya
1639 || header_info.rgb || header_info.rgba)))/* binary pixmap */
1641 unsigned char c0, c1, one;
1645 for (i = 0; i < w * h; i++)
1647 for(compno = 0; compno < numcomps; compno++)
1649 if ( !fread(&c0, 1, 1, fp) )
1651 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1652 opj_image_destroy(image);
1657 image->comps[compno].data[i] = c0;
1661 if ( !fread(&c1, 1, 1, fp) )
1662 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1664 image->comps[compno].data[i] = ((c0<<8) | c1);
1670 if(format == 1) /* ascii bitmap */
1672 for (i = 0; i < w * h; i++)
1676 if ( fscanf(fp, "%u", &index) != 1)
1677 fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
1679 image->comps[0].data[i] = (index?0:255);
1689 for(y = 0; y < h; ++y)
1693 for(x = 0; x < w; ++x)
1698 uc = (unsigned char)getc(fp);
1700 image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
1706 if((format == 7 && header_info.bw)) /*MONO*/
1710 for(i = 0; i < w * h; ++i)
1712 if ( !fread(&uc, 1, 1, fp) )
1713 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
1714 image->comps[0].data[i] = (uc & 1)?0:255;
1722 int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
1724 int *red, *green, *blue, *alpha;
1727 unsigned int compno, ncomp;
1728 int adjustR, adjustG, adjustB, adjustA;
1729 int fails, two, want_gray, has_alpha, triple;
1732 const char *tmp = outfile;
1737 if((prec = (int)image->comps[0].prec) > 16)
1739 fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1740 "\n\t: refused.\n",__FILE__,__LINE__,prec);
1743 two = has_alpha = 0; fails = 1;
1744 ncomp = image->numcomps;
1746 while (*tmp) ++tmp; tmp -= 2;
1747 want_gray = (*tmp == 'g' || *tmp == 'G');
1748 ncomp = image->numcomps;
1750 if(want_gray) ncomp = 1;
1752 if ((force_split == 0) &&
1753 (ncomp == 2 /* GRAYA */
1754 || (ncomp > 2 /* RGB, RGBA */
1755 && image->comps[0].dx == image->comps[1].dx
1756 && image->comps[1].dx == image->comps[2].dx
1757 && image->comps[0].dy == image->comps[1].dy
1758 && image->comps[1].dy == image->comps[2].dy
1759 && image->comps[0].prec == image->comps[1].prec
1760 && image->comps[1].prec == image->comps[2].prec
1763 fdest = fopen(outfile, "wb");
1767 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1771 triple = (ncomp > 2);
1772 wr = (int)image->comps[0].w; hr = (int)image->comps[0].h;
1773 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
1775 red = image->comps[0].data;
1779 green = image->comps[1].data;
1780 blue = image->comps[2].data;
1782 else green = blue = NULL;
1786 const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
1788 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
1789 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1790 wr, hr, ncomp, max, tt);
1791 alpha = image->comps[ncomp - 1].data;
1792 adjustA = (image->comps[ncomp - 1].sgnd ?
1793 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1797 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1798 opj_version(), wr, hr, max);
1801 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1805 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1806 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1808 else adjustG = adjustB = 0;
1810 for(i = 0; i < wr * hr; ++i)
1814 v = *red + adjustR; ++red;
1815 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1818 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1822 v = *green + adjustG; ++green;
1823 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1826 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1828 v = *blue + adjustB; ++blue;
1829 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1832 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1838 v = *alpha + adjustA; ++alpha;
1839 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1842 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1850 if(v > 255) v = 255; else if(v < 0) v = 0;
1852 fprintf(fdest, "%c", (unsigned char)v);
1856 if(v > 255) v = 255; else if(v < 0) v = 0;
1858 fprintf(fdest, "%c", (unsigned char)v);
1860 if(v > 255) v = 255; else if(v < 0) v = 0;
1862 fprintf(fdest, "%c", (unsigned char)v);
1867 if(v > 255) v = 255; else if(v < 0) v = 0;
1869 fprintf(fdest, "%c", (unsigned char)v);
1873 fclose(fdest); return 0;
1878 if (image->numcomps > ncomp)
1880 fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
1881 fprintf(stderr," is written to the file\n");
1883 destname = (char*)malloc(strlen(outfile) + 8);
1885 for (compno = 0; compno < ncomp; compno++)
1889 /*sprintf(destname, "%d.%s", compno, outfile);*/
1890 const size_t olen = strlen(outfile);
1891 const size_t dotpos = olen - 4;
1893 strncpy(destname, outfile, dotpos);
1894 sprintf(destname+dotpos, "_%d.pgm", compno);
1897 sprintf(destname, "%s", outfile);
1899 fdest = fopen(destname, "wb");
1902 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
1906 wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h;
1907 prec = (int)image->comps[compno].prec;
1908 max = (1<<prec) - 1;
1910 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
1911 opj_version(), wr, hr, max);
1913 red = image->comps[compno].data;
1915 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
1919 for (i = 0; i < wr * hr; i++)
1921 v = *red + adjustR; ++red;
1922 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1925 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1930 if(v > 65535) v = 65535; else if(v < 0) v = 0;
1933 fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
1937 else /* prec <= 8 */
1939 for(i = 0; i < wr * hr; ++i)
1941 v = *red + adjustR; ++red;
1942 if(v > 255) v = 255; else if(v < 0) v = 0;
1944 fprintf(fdest, "%c", (unsigned char)v);
1954 /* -->> -->> -->> -->>
1958 <<-- <<-- <<-- <<-- */
1959 static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) {
1960 int subsampling_dx = parameters->subsampling_dx;
1961 int subsampling_dy = parameters->subsampling_dy;
1964 int i, compno, numcomps, w, h;
1965 OPJ_COLOR_SPACE color_space;
1966 opj_image_cmptparm_t *cmptparm;
1967 opj_image_t * image = NULL;
1970 if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
1972 fprintf(stderr,"\nError: invalid raw image parameters\n");
1973 fprintf(stderr,"Please use the Format option -F:\n");
1974 fprintf(stderr,"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
1975 fprintf(stderr,"If subsampling is omitted, 1x1 is assumed for all components\n");
1976 fprintf(stderr,"Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
1977 fprintf(stderr," for raw 512x512 image with 4:2:0 subsampling\n");
1978 fprintf(stderr,"Aborting.\n");
1982 f = fopen(filename, "rb");
1984 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
1985 fprintf(stderr,"Aborting\n");
1988 numcomps = raw_cp->rawComp;
1990 /* FIXME ADE at this point, tcp_mct has not been properly set in calling function */
1991 if (numcomps == 1) {
1992 color_space = OPJ_CLRSPC_GRAY;
1993 } else if ((numcomps >= 3) && (parameters->tcp_mct == 0)) {
1994 color_space = OPJ_CLRSPC_SYCC;
1995 } else if ((numcomps >= 3) && (parameters->tcp_mct != 2)) {
1996 color_space = OPJ_CLRSPC_SRGB;
1998 color_space = OPJ_CLRSPC_UNKNOWN;
2000 w = raw_cp->rawWidth;
2001 h = raw_cp->rawHeight;
2002 cmptparm = (opj_image_cmptparm_t*) calloc((OPJ_UINT32)numcomps,sizeof(opj_image_cmptparm_t));
2004 fprintf(stderr, "Failed to allocate image components parameters !!\n");
2005 fprintf(stderr,"Aborting\n");
2008 /* initialize image components */
2009 for(i = 0; i < numcomps; i++) {
2010 cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
2011 cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
2012 cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
2013 cmptparm[i].dx = (OPJ_UINT32)(subsampling_dx * raw_cp->rawComps[i].dx);
2014 cmptparm[i].dy = (OPJ_UINT32)(subsampling_dy * raw_cp->rawComps[i].dy);
2015 cmptparm[i].w = (OPJ_UINT32)w;
2016 cmptparm[i].h = (OPJ_UINT32)h;
2018 /* create the image */
2019 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2025 /* set image offset and reference grid */
2026 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2027 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2028 image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
2029 image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
2031 if(raw_cp->rawBitDepth <= 8)
2033 unsigned char value = 0;
2034 for(compno = 0; compno < numcomps; compno++) {
2035 int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy);
2036 for (i = 0; i < nloop; i++) {
2037 if (!fread(&value, 1, 1, f)) {
2038 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2041 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
2045 else if(raw_cp->rawBitDepth <= 16)
2047 unsigned short value;
2048 for(compno = 0; compno < numcomps; compno++) {
2049 int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy);
2050 for (i = 0; i < nloop; i++) {
2051 unsigned char temp1;
2052 unsigned char temp2;
2053 if (!fread(&temp1, 1, 1, f)) {
2054 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2057 if (!fread(&temp2, 1, 1, f)) {
2058 fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
2063 value = (unsigned short)((temp1 << 8) + temp2);
2067 value = (unsigned short)((temp2 << 8) + temp1);
2069 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
2074 fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2078 if (fread(&ch, 1, 1, f)) {
2079 fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
2086 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2087 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
2090 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
2091 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
2094 static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL big_endian)
2096 FILE *rawFile = NULL;
2098 unsigned int compno;
2100 int line, row, curr, mask;
2105 if((image->numcomps * image->x1 * image->y1) == 0)
2107 fprintf(stderr,"\nError: invalid raw image parameters\n");
2111 rawFile = fopen(outfile, "wb");
2113 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2118 fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
2120 for(compno = 0; compno < image->numcomps; compno++)
2122 fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
2123 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
2125 w = (int)image->comps[compno].w;
2126 h = (int)image->comps[compno].h;
2128 if(image->comps[compno].prec <= 8)
2130 if(image->comps[compno].sgnd == 1)
2132 mask = (1 << image->comps[compno].prec) - 1;
2133 ptr = image->comps[compno].data;
2134 for (line = 0; line < h; line++) {
2135 for(row = 0; row < w; row++) {
2137 if(curr > 127) curr = 127; else if(curr < -128) curr = -128;
2138 uc = (unsigned char) (curr & mask);
2139 res = fwrite(&uc, 1, 1, rawFile);
2141 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2148 else if(image->comps[compno].sgnd == 0)
2150 mask = (1 << image->comps[compno].prec) - 1;
2151 ptr = image->comps[compno].data;
2152 for (line = 0; line < h; line++) {
2153 for(row = 0; row < w; row++) {
2155 if(curr > 255) curr = 255; else if(curr < 0) curr = 0;
2156 uc = (unsigned char) (curr & mask);
2157 res = fwrite(&uc, 1, 1, rawFile);
2159 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2167 else if(image->comps[compno].prec <= 16)
2169 if(image->comps[compno].sgnd == 1)
2171 union { signed short val; signed char vals[2]; } uc16;
2172 mask = (1 << image->comps[compno].prec) - 1;
2173 ptr = image->comps[compno].data;
2174 for (line = 0; line < h; line++) {
2175 for(row = 0; row < w; row++) {
2177 if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768;
2178 uc16.val = (signed short)(curr & mask);
2179 res = fwrite(uc16.vals, 1, 2, rawFile);
2181 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2188 else if(image->comps[compno].sgnd == 0)
2190 union { unsigned short val; unsigned char vals[2]; } uc16;
2191 mask = (1 << image->comps[compno].prec) - 1;
2192 ptr = image->comps[compno].data;
2193 for (line = 0; line < h; line++) {
2194 for(row = 0; row < w; row++) {
2196 if(curr > 65535 ) curr = 65535; else if( curr < 0) curr = 0;
2197 uc16.val = (unsigned short)(curr & mask);
2198 res = fwrite(uc16.vals, 1, 2, rawFile);
2200 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2208 else if (image->comps[compno].prec <= 32)
2210 fprintf(stderr,"More than 16 bits per component no handled yet\n");
2215 fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
2225 int imagetoraw(opj_image_t * image, const char *outfile)
2227 return imagetoraw_common(image, outfile, OPJ_TRUE);
2230 int imagetorawl(opj_image_t * image, const char *outfile)
2232 return imagetoraw_common(image, outfile, OPJ_FALSE);