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"
50 * Get logarithm of an integer and round downwards.
54 static int int_floorlog2(int a)
57 for (l = 0; a > 1; l++) {
63 /* Component precision scaling */
64 void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision)
68 OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32) - 1);
70 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
72 umax = (1U << precision) - 1U;
75 if (component->sgnd) {
76 OPJ_INT32* l_data = component->data;
77 OPJ_INT32 max = (OPJ_INT32)(umax / 2U);
78 OPJ_INT32 min = -max - 1;
79 for (i = 0; i < len; ++i) {
80 if (l_data[i] > max) {
82 } else if (l_data[i] < min) {
87 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
88 for (i = 0; i < len; ++i) {
89 if (l_data[i] > umax) {
94 component->prec = precision;
97 /* Component precision scaling */
98 static void scale_component_up(opj_image_comp_t* component,
103 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
104 if (component->sgnd) {
105 OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1));
106 OPJ_INT64 oldMax = (OPJ_INT64)(1U << (component->prec - 1));
107 OPJ_INT32* l_data = component->data;
108 for (i = 0; i < len; ++i) {
109 l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax);
112 OPJ_UINT64 newMax = (OPJ_UINT64)((1U << precision) - 1U);
113 OPJ_UINT64 oldMax = (OPJ_UINT64)((1U << component->prec) - 1U);
114 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
115 for (i = 0; i < len; ++i) {
116 l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax);
119 component->prec = precision;
120 component->bpp = precision;
122 void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
127 if (component->prec == precision) {
130 if (component->prec < precision) {
131 scale_component_up(component, precision);
134 shift = (int)(component->prec - precision);
135 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
136 if (component->sgnd) {
137 OPJ_INT32* l_data = component->data;
138 for (i = 0; i < len; ++i) {
142 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
143 for (i = 0; i < len; ++i) {
147 component->bpp = precision;
148 component->prec = precision;
152 /* planar / interleaved conversions */
153 /* used by PNG/TIFF */
154 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
157 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
159 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
163 OPJ_INT32* pDst0 = pDst[0];
164 OPJ_INT32* pDst1 = pDst[1];
166 for (i = 0; i < length; i++) {
167 pDst0[i] = pSrc[2 * i + 0];
168 pDst1[i] = pSrc[2 * i + 1];
171 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
175 OPJ_INT32* pDst0 = pDst[0];
176 OPJ_INT32* pDst1 = pDst[1];
177 OPJ_INT32* pDst2 = pDst[2];
179 for (i = 0; i < length; i++) {
180 pDst0[i] = pSrc[3 * i + 0];
181 pDst1[i] = pSrc[3 * i + 1];
182 pDst2[i] = pSrc[3 * i + 2];
185 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
189 OPJ_INT32* pDst0 = pDst[0];
190 OPJ_INT32* pDst1 = pDst[1];
191 OPJ_INT32* pDst2 = pDst[2];
192 OPJ_INT32* pDst3 = pDst[3];
194 for (i = 0; i < length; i++) {
195 pDst0[i] = pSrc[4 * i + 0];
196 pDst1[i] = pSrc[4 * i + 1];
197 pDst2[i] = pSrc[4 * i + 2];
198 pDst3[i] = pSrc[4 * i + 3];
201 const convert_32s_CXPX convert_32s_CXPX_LUT[5] = {
209 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
210 OPJ_SIZE_T length, OPJ_INT32 adjust)
213 const OPJ_INT32* pSrc0 = pSrc[0];
215 for (i = 0; i < length; i++) {
216 pDst[i] = pSrc0[i] + adjust;
219 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
220 OPJ_SIZE_T length, OPJ_INT32 adjust)
223 const OPJ_INT32* pSrc0 = pSrc[0];
224 const OPJ_INT32* pSrc1 = pSrc[1];
226 for (i = 0; i < length; i++) {
227 pDst[2 * i + 0] = pSrc0[i] + adjust;
228 pDst[2 * i + 1] = pSrc1[i] + adjust;
231 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
232 OPJ_SIZE_T length, OPJ_INT32 adjust)
235 const OPJ_INT32* pSrc0 = pSrc[0];
236 const OPJ_INT32* pSrc1 = pSrc[1];
237 const OPJ_INT32* pSrc2 = pSrc[2];
239 for (i = 0; i < length; i++) {
240 pDst[3 * i + 0] = pSrc0[i] + adjust;
241 pDst[3 * i + 1] = pSrc1[i] + adjust;
242 pDst[3 * i + 2] = pSrc2[i] + adjust;
245 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
246 OPJ_SIZE_T length, OPJ_INT32 adjust)
249 const OPJ_INT32* pSrc0 = pSrc[0];
250 const OPJ_INT32* pSrc1 = pSrc[1];
251 const OPJ_INT32* pSrc2 = pSrc[2];
252 const OPJ_INT32* pSrc3 = pSrc[3];
254 for (i = 0; i < length; i++) {
255 pDst[4 * i + 0] = pSrc0[i] + adjust;
256 pDst[4 * i + 1] = pSrc1[i] + adjust;
257 pDst[4 * i + 2] = pSrc2[i] + adjust;
258 pDst[4 * i + 3] = pSrc3[i] + adjust;
261 const convert_32s_PXCX convert_32s_PXCX_LUT[5] = {
269 /* bit depth conversions */
270 /* used by PNG/TIFF up to 8bpp */
271 static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
275 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
276 OPJ_UINT32 val = *pSrc++;
277 pDst[i + 0] = (OPJ_INT32)(val >> 7);
278 pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
279 pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
280 pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
281 pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
282 pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
283 pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
284 pDst[i + 7] = (OPJ_INT32)(val & 0x1U);
287 OPJ_UINT32 val = *pSrc++;
288 length = length & 7U;
289 pDst[i + 0] = (OPJ_INT32)(val >> 7);
292 pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
294 pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
296 pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
298 pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
300 pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
302 pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
311 static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
315 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
316 OPJ_UINT32 val = *pSrc++;
317 pDst[i + 0] = (OPJ_INT32)(val >> 6);
318 pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
319 pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
320 pDst[i + 3] = (OPJ_INT32)(val & 0x3U);
323 OPJ_UINT32 val = *pSrc++;
324 length = length & 3U;
325 pDst[i + 0] = (OPJ_INT32)(val >> 6);
328 pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
330 pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
336 static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
340 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
341 OPJ_UINT32 val = *pSrc++;
342 pDst[i + 0] = (OPJ_INT32)(val >> 4);
343 pDst[i + 1] = (OPJ_INT32)(val & 0xFU);
346 OPJ_UINT8 val = *pSrc++;
347 pDst[i + 0] = (OPJ_INT32)(val >> 4);
350 static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
354 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
355 OPJ_UINT32 val0 = *pSrc++;
356 OPJ_UINT32 val1 = *pSrc++;
357 OPJ_UINT32 val2 = *pSrc++;
358 pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
359 pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
360 pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
361 pDst[i + 3] = (OPJ_INT32)(val2 & 0x3FU);
365 OPJ_UINT32 val0 = *pSrc++;
366 length = length & 3U;
367 pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
370 OPJ_UINT32 val1 = *pSrc++;
371 pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
373 OPJ_UINT32 val2 = *pSrc++;
374 pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
379 static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
383 for (i = 0; i < length; i++) {
387 const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = {
400 static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
404 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
405 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
406 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
407 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
408 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
409 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
410 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
411 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
412 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
414 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
415 (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
419 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
420 OPJ_UINT32 src1 = 0U;
421 OPJ_UINT32 src2 = 0U;
422 OPJ_UINT32 src3 = 0U;
423 OPJ_UINT32 src4 = 0U;
424 OPJ_UINT32 src5 = 0U;
425 OPJ_UINT32 src6 = 0U;
426 length = length & 7U;
429 src1 = (OPJ_UINT32)pSrc[i + 1];
431 src2 = (OPJ_UINT32)pSrc[i + 2];
433 src3 = (OPJ_UINT32)pSrc[i + 3];
435 src4 = (OPJ_UINT32)pSrc[i + 4];
437 src5 = (OPJ_UINT32)pSrc[i + 5];
439 src6 = (OPJ_UINT32)pSrc[i + 6];
446 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
447 (src4 << 3) | (src5 << 2) | (src6 << 1));
451 static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
455 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
456 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
457 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
458 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
459 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
461 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
465 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
466 OPJ_UINT32 src1 = 0U;
467 OPJ_UINT32 src2 = 0U;
468 length = length & 3U;
471 src1 = (OPJ_UINT32)pSrc[i + 1];
473 src2 = (OPJ_UINT32)pSrc[i + 2];
476 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
480 static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
484 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
485 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
486 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
488 *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
492 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
493 *pDst++ = (OPJ_BYTE)((src0 << 4));
497 static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
501 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
502 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
503 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
504 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
505 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
507 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
508 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
509 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
513 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
514 OPJ_UINT32 src1 = 0U;
515 OPJ_UINT32 src2 = 0U;
516 length = length & 3U;
519 src1 = (OPJ_UINT32)pSrc[i + 1];
521 src2 = (OPJ_UINT32)pSrc[i + 2];
524 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
526 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
528 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
533 static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
537 for (i = 0; i < length; ++i) {
538 pDst[i] = (OPJ_BYTE)pSrc[i];
541 const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = {
553 /* -->> -->> -->> -->>
557 <<-- <<-- <<-- <<-- */
559 #ifdef INFORMATION_ONLY
560 /* TGA header definition. */
562 unsigned char id_length; /* Image id field length */
563 unsigned char colour_map_type; /* Colour map type */
564 unsigned char image_type; /* Image type */
566 ** Colour map specification
568 unsigned short colour_map_index; /* First entry index */
569 unsigned short colour_map_length; /* Colour map length */
570 unsigned char colour_map_entry_size; /* Colour map entry size */
572 ** Image specification
574 unsigned short x_origin; /* x origin of image */
575 unsigned short y_origin; /* u origin of image */
576 unsigned short image_width; /* Image width */
577 unsigned short image_height; /* Image height */
578 unsigned char pixel_depth; /* Pixel depth */
579 unsigned char image_desc; /* Image descriptor */
581 #endif /* INFORMATION_ONLY */
583 static unsigned short get_ushort(const unsigned char *data)
585 unsigned short val = *(const unsigned short *)data;
586 #ifdef OPJ_BIG_ENDIAN
587 val = ((val & 0xffU) << 8) | (val >> 8);
592 #define TGA_HEADER_SIZE 18
594 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
595 unsigned int *width, unsigned int *height, int *flip_image)
598 unsigned char tga[TGA_HEADER_SIZE];
599 unsigned char id_len, /*cmap_type,*/ image_type;
600 unsigned char pixel_depth, image_desc;
601 unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
602 unsigned short /*x_origin, y_origin,*/ image_w, image_h;
604 if (!bits_per_pixel || !width || !height || !flip_image) {
608 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
610 "\nError: fread return a number of element different from the expected.\n");
614 /*cmap_type = tga[1];*/
616 /*cmap_index = get_ushort(&tga[3]);*/
617 cmap_len = get_ushort(&tga[5]);
618 cmap_entry_size = tga[7];
622 x_origin = get_ushort(&tga[8]);
623 y_origin = get_ushort(&tga[10]);
625 image_w = get_ushort(&tga[12]);
626 image_h = get_ushort(&tga[14]);
627 pixel_depth = tga[16];
628 image_desc = tga[17];
630 *bits_per_pixel = (unsigned int)pixel_depth;
631 *width = (unsigned int)image_w;
632 *height = (unsigned int)image_h;
634 /* Ignore tga identifier, if present ... */
636 unsigned char *id = (unsigned char *) malloc(id_len);
638 fprintf(stderr, "tga_readheader: memory out\n");
641 if (!fread(id, id_len, 1, fp)) {
643 "\nError: fread return a number of element different from the expected.\n");
650 /* Test for compressed formats ... not yet supported ...
651 // Note :- 9 - RLE encoded palettized.
652 // 10 - RLE encoded RGB. */
653 if (image_type > 8) {
654 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
658 *flip_image = !(image_desc & 32);
660 /* Palettized formats are not yet supported, skip over the palette, if present ... */
661 palette_size = cmap_len * (cmap_entry_size / 8);
663 if (palette_size > 0) {
664 fprintf(stderr, "File contains a palette - not yet supported.");
665 fseek(fp, palette_size, SEEK_CUR);
670 #ifdef OPJ_BIG_ENDIAN
672 static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
674 return (OPJ_UINT16)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
679 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
682 OPJ_UINT16 image_w, image_h, us0;
683 unsigned char uc0, image_type;
684 unsigned char pixel_depth, image_desc;
686 if (!bits_per_pixel || !width || !height) {
692 if (bits_per_pixel < 256) {
693 pixel_depth = (unsigned char)bits_per_pixel;
695 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
700 if (fwrite(&uc0, 1, 1, fp) != 1) {
701 goto fails; /* id_length */
703 if (fwrite(&uc0, 1, 1, fp) != 1) {
704 goto fails; /* colour_map_type */
707 image_type = 2; /* Uncompressed. */
708 if (fwrite(&image_type, 1, 1, fp) != 1) {
713 if (fwrite(&us0, 2, 1, fp) != 1) {
714 goto fails; /* colour_map_index */
716 if (fwrite(&us0, 2, 1, fp) != 1) {
717 goto fails; /* colour_map_length */
719 if (fwrite(&uc0, 1, 1, fp) != 1) {
720 goto fails; /* colour_map_entry_size */
723 if (fwrite(&us0, 2, 1, fp) != 1) {
724 goto fails; /* x_origin */
726 if (fwrite(&us0, 2, 1, fp) != 1) {
727 goto fails; /* y_origin */
730 image_w = (unsigned short)width;
731 image_h = (unsigned short) height;
733 #ifndef OPJ_BIG_ENDIAN
734 if (fwrite(&image_w, 2, 1, fp) != 1) {
737 if (fwrite(&image_h, 2, 1, fp) != 1) {
741 image_w = swap16(image_w);
742 image_h = swap16(image_h);
743 if (fwrite(&image_w, 2, 1, fp) != 1) {
746 if (fwrite(&image_h, 2, 1, fp) != 1) {
751 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
755 image_desc = 8; /* 8 bits per component. */
760 if (fwrite(&image_desc, 1, 1, fp) != 1) {
767 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
771 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
775 unsigned int image_width, image_height, pixel_bit_depth;
778 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
780 OPJ_COLOR_SPACE color_space;
783 int subsampling_dx, subsampling_dy;
786 f = fopen(filename, "rb");
788 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
792 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
798 /* We currently only support 24 & 32 bit tga's ... */
799 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
804 /* initialize image components */
805 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
807 mono = (pixel_bit_depth == 8) ||
808 (pixel_bit_depth == 16); /* Mono with & without alpha. */
809 save_alpha = (pixel_bit_depth == 16) ||
810 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
813 color_space = OPJ_CLRSPC_GRAY;
814 numcomps = save_alpha ? 2 : 1;
816 numcomps = save_alpha ? 4 : 3;
817 color_space = OPJ_CLRSPC_SRGB;
820 subsampling_dx = parameters->subsampling_dx;
821 subsampling_dy = parameters->subsampling_dy;
823 for (i = 0; i < numcomps; i++) {
824 cmptparm[i].prec = 8;
826 cmptparm[i].sgnd = 0;
827 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
828 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
829 cmptparm[i].w = image_width;
830 cmptparm[i].h = image_height;
833 /* create the image */
834 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
842 /* set image offset and reference grid */
843 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
844 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
845 image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) *
846 (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) *
847 (OPJ_UINT32)subsampling_dx + 1;
848 image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) *
849 (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) *
850 (OPJ_UINT32)subsampling_dy + 1;
853 for (y = 0; y < image_height; y++) {
857 index = (int)((image_height - y - 1) * image_width);
859 index = (int)(y * image_width);
863 for (x = 0; x < image_width; x++) {
864 unsigned char r, g, b;
866 if (!fread(&b, 1, 1, f)) {
868 "\nError: fread return a number of element different from the expected.\n");
869 opj_image_destroy(image);
873 if (!fread(&g, 1, 1, f)) {
875 "\nError: fread return a number of element different from the expected.\n");
876 opj_image_destroy(image);
880 if (!fread(&r, 1, 1, f)) {
882 "\nError: fread return a number of element different from the expected.\n");
883 opj_image_destroy(image);
888 image->comps[0].data[index] = r;
889 image->comps[1].data[index] = g;
890 image->comps[2].data[index] = b;
893 } else if (numcomps == 4) {
894 for (x = 0; x < image_width; x++) {
895 unsigned char r, g, b, a;
896 if (!fread(&b, 1, 1, f)) {
898 "\nError: fread return a number of element different from the expected.\n");
899 opj_image_destroy(image);
903 if (!fread(&g, 1, 1, f)) {
905 "\nError: fread return a number of element different from the expected.\n");
906 opj_image_destroy(image);
910 if (!fread(&r, 1, 1, f)) {
912 "\nError: fread return a number of element different from the expected.\n");
913 opj_image_destroy(image);
917 if (!fread(&a, 1, 1, f)) {
919 "\nError: fread return a number of element different from the expected.\n");
920 opj_image_destroy(image);
925 image->comps[0].data[index] = r;
926 image->comps[1].data[index] = g;
927 image->comps[2].data[index] = b;
928 image->comps[3].data[index] = a;
932 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
939 int imagetotga(opj_image_t * image, const char *outfile)
941 int width, height, bpp, x, y;
942 OPJ_BOOL write_alpha;
944 int adjustR, adjustG, adjustB, fails;
945 unsigned int alpha_channel;
953 fdest = fopen(outfile, "wb");
955 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
959 for (i = 0; i < image->numcomps - 1; i++) {
960 if ((image->comps[0].dx != image->comps[i + 1].dx)
961 || (image->comps[0].dy != image->comps[i + 1].dy)
962 || (image->comps[0].prec != image->comps[i + 1].prec)) {
965 "Unable to create a tga file with such J2K image charateristics.");
970 width = (int)image->comps[0].w;
971 height = (int)image->comps[0].h;
973 /* Mono with alpha, or RGB with alpha. */
974 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
976 /* Write TGA header */
977 bpp = write_alpha ? 32 : 24;
979 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
983 alpha_channel = image->numcomps - 1;
985 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
987 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
988 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
989 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
991 for (y = 0; y < height; y++) {
992 unsigned int index = (unsigned int)(y * width);
994 for (x = 0; x < width; x++, index++) {
995 r = (float)(image->comps[0].data[index] + adjustR);
997 if (image->numcomps > 2) {
998 g = (float)(image->comps[1].data[index] + adjustG);
999 b = (float)(image->comps[2].data[index] + adjustB);
1006 /* TGA format writes BGR ... */
1009 } else if (b < 0.) {
1012 value = (unsigned char)(b * scale);
1013 res = fwrite(&value, 1, 1, fdest);
1016 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1021 } else if (g < 0.) {
1024 value = (unsigned char)(g * scale);
1025 res = fwrite(&value, 1, 1, fdest);
1028 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1033 } else if (r < 0.) {
1036 value = (unsigned char)(r * scale);
1037 res = fwrite(&value, 1, 1, fdest);
1040 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1045 a = (float)(image->comps[alpha_channel].data[index]);
1048 } else if (a < 0.) {
1051 value = (unsigned char)(a * scale);
1052 res = fwrite(&value, 1, 1, fdest);
1055 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1068 /* -->> -->> -->> -->>
1072 <<-- <<-- <<-- <<-- */
1075 static unsigned char readuchar(FILE * f)
1078 if (!fread(&c1, 1, 1, f)) {
1080 "\nError: fread return a number of element different from the expected.\n");
1086 static unsigned short readushort(FILE * f, int bigendian)
1088 unsigned char c1, c2;
1089 if (!fread(&c1, 1, 1, f)) {
1091 "\nError: fread return a number of element different from the expected.\n");
1094 if (!fread(&c2, 1, 1, f)) {
1096 "\nError: fread return a number of element different from the expected.\n");
1100 return (unsigned short)((c1 << 8) + c2);
1102 return (unsigned short)((c2 << 8) + c1);
1106 static unsigned int readuint(FILE * f, int bigendian)
1108 unsigned char c1, c2, c3, c4;
1109 if (!fread(&c1, 1, 1, f)) {
1111 "\nError: fread return a number of element different from the expected.\n");
1114 if (!fread(&c2, 1, 1, f)) {
1116 "\nError: fread return a number of element different from the expected.\n");
1119 if (!fread(&c3, 1, 1, f)) {
1121 "\nError: fread return a number of element different from the expected.\n");
1124 if (!fread(&c4, 1, 1, f)) {
1126 "\nError: fread return a number of element different from the expected.\n");
1130 return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(
1133 return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(
1138 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1142 int i, numcomps, max;
1143 OPJ_COLOR_SPACE color_space;
1144 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1145 opj_image_t * image = NULL;
1146 int adjustS, ushift, dshift, force8;
1148 char endian1, endian2, sign;
1153 opj_image_comp_t *comp = NULL;
1156 color_space = OPJ_CLRSPC_GRAY;
1158 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1162 f = fopen(filename, "rb");
1164 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1168 fseek(f, 0, SEEK_SET);
1169 if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
1170 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1173 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1179 while (signtmp[i] != '\0') {
1180 if (signtmp[i] == '-') {
1187 if (endian1 == 'M' && endian2 == 'L') {
1189 } else if (endian2 == 'M' && endian1 == 'L') {
1193 fprintf(stderr, "Bad pgx header, please check input file\n");
1197 /* initialize image component */
1199 cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
1200 cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
1201 cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx +
1202 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx
1204 cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy +
1205 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy
1216 dshift = prec - ushift;
1217 if (cmptparm.sgnd) {
1218 adjustS = (1 << (prec - 1));
1225 ushift = dshift = force8 = adjustS = 0;
1228 cmptparm.prec = (OPJ_UINT32)prec;
1229 cmptparm.bpp = (OPJ_UINT32)prec;
1230 cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
1231 cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
1233 /* create the image */
1234 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
1239 /* set image offset and reference grid */
1240 image->x0 = cmptparm.x0;
1241 image->y0 = cmptparm.x0;
1242 image->x1 = cmptparm.w;
1243 image->y1 = cmptparm.h;
1245 /* set image data */
1247 comp = &image->comps[0];
1249 for (i = 0; i < w * h; i++) {
1252 v = readuchar(f) + adjustS;
1253 v = (v << ushift) + (v >> dshift);
1254 comp->data[i] = (unsigned char)v;
1262 if (comp->prec == 8) {
1266 v = (char) readuchar(f);
1268 } else if (comp->prec <= 16) {
1270 v = readushort(f, bigendian);
1272 v = (short) readushort(f, bigendian);
1276 v = (int)readuint(f, bigendian);
1278 v = (int) readuint(f, bigendian);
1287 comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
1292 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
1294 static INLINE int clamp(const int value, const int prec, const int sgnd)
1298 return CLAMP(value, -128, 127);
1299 } else if (prec <= 16) {
1300 return CLAMP(value, -32768, 32767);
1302 return CLAMP(value, -2147483647 - 1, 2147483647);
1306 return CLAMP(value, 0, 255);
1307 } else if (prec <= 16) {
1308 return CLAMP(value, 0, 65535);
1310 return value; /*CLAMP(value,0,4294967295);*/
1315 int imagetopgx(opj_image_t * image, const char *outfile)
1318 int i, j, fails = 1;
1319 unsigned int compno;
1322 for (compno = 0; compno < image->numcomps; compno++) {
1323 opj_image_comp_t *comp = &image->comps[compno];
1324 char bname[256]; /* buffer for name */
1325 char *name = bname; /* pointer */
1328 const size_t olen = strlen(outfile);
1329 const size_t dotpos = olen - 4;
1330 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1332 if (outfile[dotpos] != '.') {
1333 /* `pgx` was recognized but there is no dot at expected position */
1334 fprintf(stderr, "ERROR -> Impossible happen.");
1338 name = (char*)malloc(total + 1);
1340 fprintf(stderr, "imagetopgx: memory out\n");
1344 strncpy(name, outfile, dotpos);
1345 sprintf(name + dotpos, "_%u.pgx", compno);
1346 fdest = fopen(name, "wb");
1347 /* don't need name anymore */
1351 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1358 w = (int)image->comps[compno].w;
1359 h = (int)image->comps[compno].h;
1361 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
1364 if (comp->prec <= 8) {
1366 } else if (comp->prec <= 16) {
1372 for (i = 0; i < w * h; i++) {
1373 /* FIXME: clamp func is being called within a loop */
1374 const int val = clamp(image->comps[compno].data[i],
1375 (int)comp->prec, (int)comp->sgnd);
1377 for (j = nbytes - 1; j >= 0; j--) {
1378 int v = (int)(val >> (j * 8));
1379 unsigned char byte = (unsigned char)v;
1380 res = fwrite(&byte, 1, 1, fdest);
1383 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1406 /* -->> -->> -->> -->>
1410 <<-- <<-- <<-- <<-- */
1413 int width, height, maxval, depth, format;
1414 char rgb, rgba, gray, graya, bw;
1418 static char *skip_white(char *s)
1422 if (*s == '\n' || *s == '\r') {
1435 static char *skip_int(char *start, int *out_n)
1442 s = skip_white(start);
1456 *out_n = atoi(start);
1461 static char *skip_idf(char *start, char out_idf[256])
1466 s = skip_white(start);
1473 if (isalpha(*s) || *s == '_') {
1481 strncpy(out_idf, start, 255);
1486 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1488 int format, end, ttype;
1489 char idf[256], type[256];
1492 if (fgets(line, 250, reader) == NULL) {
1493 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1497 if (line[0] != 'P') {
1498 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1501 format = atoi(line + 1);
1502 if (format < 1 || format > 7) {
1503 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1506 ph->format = format;
1509 while (fgets(line, 250, reader)) {
1520 s = skip_idf(s, idf);
1522 if (s == NULL || *s == 0) {
1526 if (strcmp(idf, "ENDHDR") == 0) {
1530 if (strcmp(idf, "WIDTH") == 0) {
1531 s = skip_int(s, &ph->width);
1532 if (s == NULL || *s == 0) {
1538 if (strcmp(idf, "HEIGHT") == 0) {
1539 s = skip_int(s, &ph->height);
1540 if (s == NULL || *s == 0) {
1546 if (strcmp(idf, "DEPTH") == 0) {
1547 s = skip_int(s, &ph->depth);
1548 if (s == NULL || *s == 0) {
1554 if (strcmp(idf, "MAXVAL") == 0) {
1555 s = skip_int(s, &ph->maxval);
1556 if (s == NULL || *s == 0) {
1562 if (strcmp(idf, "TUPLTYPE") == 0) {
1563 s = skip_idf(s, type);
1564 if (s == NULL || *s == 0) {
1568 if (strcmp(type, "BLACKANDWHITE") == 0) {
1573 if (strcmp(type, "GRAYSCALE") == 0) {
1578 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1583 if (strcmp(type, "RGB") == 0) {
1588 if (strcmp(type, "RGB_ALPHA") == 0) {
1593 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1596 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1598 } /* if(format == 7) */
1600 /* Here format is in range [1,6] */
1601 if (ph->width == 0) {
1602 s = skip_int(s, &ph->width);
1603 if ((s == NULL) || (*s == 0) || (ph->width < 1)) {
1608 if (ph->height == 0) {
1609 s = skip_int(s, &ph->height);
1610 if ((s == NULL) && allow_null) {
1613 if ((s == NULL) || (*s == 0) || (ph->height < 1)) {
1616 if (format == 1 || format == 4) {
1621 /* here, format is in P2, P3, P5, P6 */
1622 s = skip_int(s, &ph->maxval);
1623 if ((s == NULL) && allow_null) {
1626 if ((s == NULL) || (*s == 0)) {
1630 }/* while(fgets( ) */
1631 if (format == 2 || format == 3 || format > 4) {
1632 if (ph->maxval < 1 || ph->maxval > 65535) {
1636 if (ph->width < 1 || ph->height < 1) {
1642 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1645 if (ph->depth < 1 || ph->depth > 4) {
1654 if (format == 1 || format == 4) {
1660 static int has_prec(int val)
1710 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1712 int subsampling_dx = parameters->subsampling_dx;
1713 int subsampling_dy = parameters->subsampling_dy;
1716 int i, compno, numcomps, w, h, prec, format;
1717 OPJ_COLOR_SPACE color_space;
1718 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1719 opj_image_t * image = NULL;
1720 struct pnm_header header_info;
1722 if ((fp = fopen(filename, "rb")) == NULL) {
1723 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1726 memset(&header_info, 0, sizeof(struct pnm_header));
1728 read_pnm_header(fp, &header_info);
1730 if (!header_info.ok) {
1735 /* This limitation could be removed by making sure to use size_t below */
1736 if (header_info.height != 0 &&
1737 header_info.width > INT_MAX / header_info.height) {
1738 fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
1739 header_info.width, header_info.height);
1744 format = header_info.format;
1747 case 1: /* ascii bitmap */
1748 case 4: /* raw bitmap */
1752 case 2: /* ascii greymap */
1753 case 5: /* raw greymap */
1757 case 3: /* ascii pixmap */
1758 case 6: /* raw pixmap */
1762 case 7: /* arbitrary map */
1763 numcomps = header_info.depth;
1771 color_space = OPJ_CLRSPC_GRAY; /* GRAY, GRAYA */
1773 color_space = OPJ_CLRSPC_SRGB; /* RGB, RGBA */
1776 prec = has_prec(header_info.maxval);
1782 w = header_info.width;
1783 h = header_info.height;
1784 subsampling_dx = parameters->subsampling_dx;
1785 subsampling_dy = parameters->subsampling_dy;
1787 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
1789 for (i = 0; i < numcomps; i++) {
1790 cmptparm[i].prec = (OPJ_UINT32)prec;
1791 cmptparm[i].bpp = (OPJ_UINT32)prec;
1792 cmptparm[i].sgnd = 0;
1793 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
1794 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
1795 cmptparm[i].w = (OPJ_UINT32)w;
1796 cmptparm[i].h = (OPJ_UINT32)h;
1798 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
1805 /* set image offset and reference grid */
1806 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
1807 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
1808 image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx
1810 image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy
1813 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1816 for (i = 0; i < w * h; i++) {
1817 for (compno = 0; compno < numcomps; compno++) {
1819 if (fscanf(fp, "%u", &index) != 1) {
1821 "\nWARNING: fscanf return a number of element different from the expected.\n");
1824 image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval;
1827 } else if ((format == 5)
1830 && (header_info.gray || header_info.graya
1831 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1832 unsigned char c0, c1, one;
1836 for (i = 0; i < w * h; i++) {
1837 for (compno = 0; compno < numcomps; compno++) {
1838 if (!fread(&c0, 1, 1, fp)) {
1840 "\nError: fread return a number of element different from the expected.\n");
1841 opj_image_destroy(image);
1846 image->comps[compno].data[i] = c0;
1848 if (!fread(&c1, 1, 1, fp)) {
1850 "\nError: fread return a number of element different from the expected.\n");
1853 image->comps[compno].data[i] = ((c0 << 8) | c1);
1857 } else if (format == 1) { /* ascii bitmap */
1858 for (i = 0; i < w * h; i++) {
1861 if (fscanf(fp, "%u", &index) != 1) {
1863 "\nWARNING: fscanf return a number of element different from the expected.\n");
1866 image->comps[0].data[i] = (index ? 0 : 255);
1868 } else if (format == 4) {
1873 for (y = 0; y < h; ++y) {
1877 for (x = 0; x < w; ++x) {
1880 uc = (unsigned char)getc(fp);
1882 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
1887 } else if ((format == 7 && header_info.bw)) { /*MONO*/
1890 for (i = 0; i < w * h; ++i) {
1891 if (!fread(&uc, 1, 1, fp)) {
1893 "\nError: fread return a number of element different from the expected.\n");
1895 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
1903 static int are_comps_similar(opj_image_t * image)
1906 for (i = 1; i < image->numcomps; i++) {
1907 if (image->comps[0].dx != image->comps[i].dx ||
1908 image->comps[0].dy != image->comps[i].dy ||
1910 (image->comps[0].prec != image->comps[i].prec ||
1911 image->comps[0].sgnd != image->comps[i].sgnd))) {
1919 int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
1921 int *red, *green, *blue, *alpha;
1924 unsigned int compno, ncomp;
1925 int adjustR, adjustG, adjustB, adjustA;
1926 int fails, two, want_gray, has_alpha, triple;
1929 const char *tmp = outfile;
1934 if ((prec = (int)image->comps[0].prec) > 16) {
1935 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1936 "\n\t: refused.\n", __FILE__, __LINE__, prec);
1939 two = has_alpha = 0;
1941 ncomp = image->numcomps;
1947 want_gray = (*tmp == 'g' || *tmp == 'G');
1948 ncomp = image->numcomps;
1954 if ((force_split == 0) && ncomp >= 2 &&
1955 are_comps_similar(image)) {
1956 fdest = fopen(outfile, "wb");
1959 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1963 triple = (ncomp > 2);
1964 wr = (int)image->comps[0].w;
1965 hr = (int)image->comps[0].h;
1966 max = (1 << prec) - 1;
1967 has_alpha = (ncomp == 4 || ncomp == 2);
1969 red = image->comps[0].data;
1972 green = image->comps[1].data;
1973 blue = image->comps[2].data;
1975 green = blue = NULL;
1979 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
1981 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n"
1982 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1983 wr, hr, ncomp, max, tt);
1984 alpha = image->comps[ncomp - 1].data;
1985 adjustA = (image->comps[ncomp - 1].sgnd ?
1986 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1988 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1989 opj_version(), wr, hr, max);
1992 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1995 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1996 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1998 adjustG = adjustB = 0;
2001 for (i = 0; i < wr * hr; ++i) {
2012 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2015 v = *green + adjustG;
2024 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2026 v = *blue + adjustB;
2035 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2040 v = *alpha + adjustA;
2049 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2063 fprintf(fdest, "%c", (unsigned char)v);
2072 fprintf(fdest, "%c", (unsigned char)v);
2080 fprintf(fdest, "%c", (unsigned char)v);
2090 fprintf(fdest, "%c", (unsigned char)v);
2100 if (image->numcomps > ncomp) {
2101 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2102 fprintf(stderr, " is written to the file\n");
2104 destname = (char*)malloc(strlen(outfile) + 8);
2105 if (destname == NULL) {
2106 fprintf(stderr, "imagetopnm: memory out\n");
2109 for (compno = 0; compno < ncomp; compno++) {
2111 /*sprintf(destname, "%d.%s", compno, outfile);*/
2112 const size_t olen = strlen(outfile);
2113 const size_t dotpos = olen - 4;
2115 strncpy(destname, outfile, dotpos);
2116 sprintf(destname + dotpos, "_%u.pgm", compno);
2118 sprintf(destname, "%s", outfile);
2121 fdest = fopen(destname, "wb");
2123 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2127 wr = (int)image->comps[compno].w;
2128 hr = (int)image->comps[compno].h;
2129 prec = (int)image->comps[compno].prec;
2130 max = (1 << prec) - 1;
2132 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2133 opj_version(), wr, hr, max);
2135 red = image->comps[compno].data;
2137 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2140 for (i = 0; i < wr * hr; i++) {
2150 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2161 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2164 } else { /* prec <= 8 */
2165 for (i = 0; i < wr * hr; ++i) {
2174 fprintf(fdest, "%c", (unsigned char)v);
2184 /* -->> -->> -->> -->>
2188 <<-- <<-- <<-- <<-- */
2189 static opj_image_t* rawtoimage_common(const char *filename,
2190 opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian)
2192 int subsampling_dx = parameters->subsampling_dx;
2193 int subsampling_dy = parameters->subsampling_dy;
2196 int i, compno, numcomps, w, h;
2197 OPJ_COLOR_SPACE color_space;
2198 opj_image_cmptparm_t *cmptparm;
2199 opj_image_t * image = NULL;
2202 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2203 raw_cp->rawBitDepth)) == 0) {
2204 fprintf(stderr, "\nError: invalid raw image parameters\n");
2205 fprintf(stderr, "Please use the Format option -F:\n");
2207 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
2209 "If subsampling is omitted, 1x1 is assumed for all components\n");
2211 "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
2212 fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
2213 fprintf(stderr, "Aborting.\n");
2217 f = fopen(filename, "rb");
2219 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2220 fprintf(stderr, "Aborting\n");
2223 numcomps = raw_cp->rawComp;
2225 /* FIXME ADE at this point, tcp_mct has not been properly set in calling function */
2226 if (numcomps == 1) {
2227 color_space = OPJ_CLRSPC_GRAY;
2228 } else if ((numcomps >= 3) && (parameters->tcp_mct == 0)) {
2229 color_space = OPJ_CLRSPC_SYCC;
2230 } else if ((numcomps >= 3) && (parameters->tcp_mct != 2)) {
2231 color_space = OPJ_CLRSPC_SRGB;
2233 color_space = OPJ_CLRSPC_UNKNOWN;
2235 w = raw_cp->rawWidth;
2236 h = raw_cp->rawHeight;
2237 cmptparm = (opj_image_cmptparm_t*) calloc((OPJ_UINT32)numcomps,
2238 sizeof(opj_image_cmptparm_t));
2240 fprintf(stderr, "Failed to allocate image components parameters !!\n");
2241 fprintf(stderr, "Aborting\n");
2245 /* initialize image components */
2246 for (i = 0; i < numcomps; i++) {
2247 cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
2248 cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
2249 cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
2250 cmptparm[i].dx = (OPJ_UINT32)(subsampling_dx * raw_cp->rawComps[i].dx);
2251 cmptparm[i].dy = (OPJ_UINT32)(subsampling_dy * raw_cp->rawComps[i].dy);
2252 cmptparm[i].w = (OPJ_UINT32)w;
2253 cmptparm[i].h = (OPJ_UINT32)h;
2255 /* create the image */
2256 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2262 /* set image offset and reference grid */
2263 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2264 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2265 image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) *
2266 (OPJ_UINT32)subsampling_dx + 1;
2267 image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) *
2268 (OPJ_UINT32)subsampling_dy + 1;
2270 if (raw_cp->rawBitDepth <= 8) {
2271 unsigned char value = 0;
2272 for (compno = 0; compno < numcomps; compno++) {
2273 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2274 raw_cp->rawComps[compno].dy);
2275 for (i = 0; i < nloop; i++) {
2276 if (!fread(&value, 1, 1, f)) {
2277 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2278 opj_image_destroy(image);
2282 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
2285 } else if (raw_cp->rawBitDepth <= 16) {
2286 unsigned short value;
2287 for (compno = 0; compno < numcomps; compno++) {
2288 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2289 raw_cp->rawComps[compno].dy);
2290 for (i = 0; i < nloop; i++) {
2291 unsigned char temp1;
2292 unsigned char temp2;
2293 if (!fread(&temp1, 1, 1, f)) {
2294 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2295 opj_image_destroy(image);
2299 if (!fread(&temp2, 1, 1, f)) {
2300 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2301 opj_image_destroy(image);
2306 value = (unsigned short)((temp1 << 8) + temp2);
2308 value = (unsigned short)((temp2 << 8) + temp1);
2310 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
2315 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2316 opj_image_destroy(image);
2321 if (fread(&ch, 1, 1, f)) {
2322 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
2329 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters,
2330 raw_cparameters_t *raw_cp)
2332 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
2335 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2336 raw_cparameters_t *raw_cp)
2338 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
2341 static int imagetoraw_common(opj_image_t * image, const char *outfile,
2342 OPJ_BOOL big_endian)
2344 FILE *rawFile = NULL;
2346 unsigned int compno;
2348 int line, row, curr, mask;
2353 if ((image->numcomps * image->x1 * image->y1) == 0) {
2354 fprintf(stderr, "\nError: invalid raw image parameters\n");
2358 rawFile = fopen(outfile, "wb");
2360 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2365 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
2367 for (compno = 0; compno < image->numcomps; compno++) {
2368 fprintf(stdout, "Component %u characteristics: %dx%dx%d %s\n", compno,
2369 image->comps[compno].w,
2370 image->comps[compno].h, image->comps[compno].prec,
2371 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
2373 w = (int)image->comps[compno].w;
2374 h = (int)image->comps[compno].h;
2376 if (image->comps[compno].prec <= 8) {
2377 if (image->comps[compno].sgnd == 1) {
2378 mask = (1 << image->comps[compno].prec) - 1;
2379 ptr = image->comps[compno].data;
2380 for (line = 0; line < h; line++) {
2381 for (row = 0; row < w; row++) {
2385 } else if (curr < -128) {
2388 uc = (unsigned char)(curr & mask);
2389 res = fwrite(&uc, 1, 1, rawFile);
2391 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2397 } else if (image->comps[compno].sgnd == 0) {
2398 mask = (1 << image->comps[compno].prec) - 1;
2399 ptr = image->comps[compno].data;
2400 for (line = 0; line < h; line++) {
2401 for (row = 0; row < w; row++) {
2405 } else if (curr < 0) {
2408 uc = (unsigned char)(curr & mask);
2409 res = fwrite(&uc, 1, 1, rawFile);
2411 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2418 } else if (image->comps[compno].prec <= 16) {
2419 if (image->comps[compno].sgnd == 1) {
2422 signed char vals[2];
2424 mask = (1 << image->comps[compno].prec) - 1;
2425 ptr = image->comps[compno].data;
2426 for (line = 0; line < h; line++) {
2427 for (row = 0; row < w; row++) {
2431 } else if (curr < -32768) {
2434 uc16.val = (signed short)(curr & mask);
2435 res = fwrite(uc16.vals, 1, 2, rawFile);
2437 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2443 } else if (image->comps[compno].sgnd == 0) {
2446 unsigned char vals[2];
2448 mask = (1 << image->comps[compno].prec) - 1;
2449 ptr = image->comps[compno].data;
2450 for (line = 0; line < h; line++) {
2451 for (row = 0; row < w; row++) {
2455 } else if (curr < 0) {
2458 uc16.val = (unsigned short)(curr & mask);
2459 res = fwrite(uc16.vals, 1, 2, rawFile);
2461 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2468 } else if (image->comps[compno].prec <= 32) {
2469 fprintf(stderr, "More than 16 bits per component no handled yet\n");
2472 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
2482 int imagetoraw(opj_image_t * image, const char *outfile)
2484 return imagetoraw_common(image, outfile, OPJ_TRUE);
2487 int imagetorawl(opj_image_t * image, const char *outfile)
2489 return imagetoraw_common(image, outfile, OPJ_FALSE);