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 /* Returns a ushort from a little-endian serialized value */
584 static unsigned short get_tga_ushort(const unsigned char *data)
586 return (unsigned short)(data[0] | (data[1] << 8));
589 #define TGA_HEADER_SIZE 18
591 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
592 unsigned int *width, unsigned int *height, int *flip_image)
595 unsigned char tga[TGA_HEADER_SIZE];
596 unsigned char id_len, /*cmap_type,*/ image_type;
597 unsigned char pixel_depth, image_desc;
598 unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
599 unsigned short /*x_origin, y_origin,*/ image_w, image_h;
601 if (!bits_per_pixel || !width || !height || !flip_image) {
605 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
607 "\nError: fread return a number of element different from the expected.\n");
611 /*cmap_type = tga[1];*/
613 /*cmap_index = get_tga_ushort(&tga[3]);*/
614 cmap_len = get_tga_ushort(&tga[5]);
615 cmap_entry_size = tga[7];
619 x_origin = get_tga_ushort(&tga[8]);
620 y_origin = get_tga_ushort(&tga[10]);
622 image_w = get_tga_ushort(&tga[12]);
623 image_h = get_tga_ushort(&tga[14]);
624 pixel_depth = tga[16];
625 image_desc = tga[17];
627 *bits_per_pixel = (unsigned int)pixel_depth;
628 *width = (unsigned int)image_w;
629 *height = (unsigned int)image_h;
631 /* Ignore tga identifier, if present ... */
633 unsigned char *id = (unsigned char *) malloc(id_len);
635 fprintf(stderr, "tga_readheader: memory out\n");
638 if (!fread(id, id_len, 1, fp)) {
640 "\nError: fread return a number of element different from the expected.\n");
647 /* Test for compressed formats ... not yet supported ...
648 // Note :- 9 - RLE encoded palettized.
649 // 10 - RLE encoded RGB. */
650 if (image_type > 8) {
651 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
655 *flip_image = !(image_desc & 32);
657 /* Palettized formats are not yet supported, skip over the palette, if present ... */
658 palette_size = cmap_len * (cmap_entry_size / 8);
660 if (palette_size > 0) {
661 fprintf(stderr, "File contains a palette - not yet supported.");
662 fseek(fp, palette_size, SEEK_CUR);
667 #ifdef OPJ_BIG_ENDIAN
669 static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
671 return (OPJ_UINT16)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
676 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
679 OPJ_UINT16 image_w, image_h, us0;
680 unsigned char uc0, image_type;
681 unsigned char pixel_depth, image_desc;
683 if (!bits_per_pixel || !width || !height) {
689 if (bits_per_pixel < 256) {
690 pixel_depth = (unsigned char)bits_per_pixel;
692 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
697 if (fwrite(&uc0, 1, 1, fp) != 1) {
698 goto fails; /* id_length */
700 if (fwrite(&uc0, 1, 1, fp) != 1) {
701 goto fails; /* colour_map_type */
704 image_type = 2; /* Uncompressed. */
705 if (fwrite(&image_type, 1, 1, fp) != 1) {
710 if (fwrite(&us0, 2, 1, fp) != 1) {
711 goto fails; /* colour_map_index */
713 if (fwrite(&us0, 2, 1, fp) != 1) {
714 goto fails; /* colour_map_length */
716 if (fwrite(&uc0, 1, 1, fp) != 1) {
717 goto fails; /* colour_map_entry_size */
720 if (fwrite(&us0, 2, 1, fp) != 1) {
721 goto fails; /* x_origin */
723 if (fwrite(&us0, 2, 1, fp) != 1) {
724 goto fails; /* y_origin */
727 image_w = (unsigned short)width;
728 image_h = (unsigned short) height;
730 #ifndef OPJ_BIG_ENDIAN
731 if (fwrite(&image_w, 2, 1, fp) != 1) {
734 if (fwrite(&image_h, 2, 1, fp) != 1) {
738 image_w = swap16(image_w);
739 image_h = swap16(image_h);
740 if (fwrite(&image_w, 2, 1, fp) != 1) {
743 if (fwrite(&image_h, 2, 1, fp) != 1) {
748 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
752 image_desc = 8; /* 8 bits per component. */
757 if (fwrite(&image_desc, 1, 1, fp) != 1) {
764 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
768 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
772 unsigned int image_width, image_height, pixel_bit_depth;
775 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
777 OPJ_COLOR_SPACE color_space;
780 int subsampling_dx, subsampling_dy;
783 f = fopen(filename, "rb");
785 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
789 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
795 /* We currently only support 24 & 32 bit tga's ... */
796 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
801 /* initialize image components */
802 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
804 mono = (pixel_bit_depth == 8) ||
805 (pixel_bit_depth == 16); /* Mono with & without alpha. */
806 save_alpha = (pixel_bit_depth == 16) ||
807 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
810 color_space = OPJ_CLRSPC_GRAY;
811 numcomps = save_alpha ? 2 : 1;
813 numcomps = save_alpha ? 4 : 3;
814 color_space = OPJ_CLRSPC_SRGB;
817 /* If the declared file size is > 10 MB, check that the file is big */
818 /* enough to avoid excessive memory allocations */
819 if (image_height != 0 &&
820 image_width > 10000000U / image_height / (OPJ_UINT32)numcomps) {
822 OPJ_UINT64 expected_file_size =
823 (OPJ_UINT64)image_width * image_height * (OPJ_UINT32)numcomps;
824 long curpos = ftell(f);
825 if (expected_file_size > (OPJ_UINT64)INT_MAX) {
826 expected_file_size = (OPJ_UINT64)INT_MAX;
828 fseek(f, (long)expected_file_size - 1, SEEK_SET);
829 if (fread(&ch, 1, 1, f) != 1) {
833 fseek(f, curpos, SEEK_SET);
836 subsampling_dx = parameters->subsampling_dx;
837 subsampling_dy = parameters->subsampling_dy;
839 for (i = 0; i < numcomps; i++) {
840 cmptparm[i].prec = 8;
842 cmptparm[i].sgnd = 0;
843 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
844 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
845 cmptparm[i].w = image_width;
846 cmptparm[i].h = image_height;
849 /* create the image */
850 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
858 /* set image offset and reference grid */
859 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
860 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
861 image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) *
862 (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) *
863 (OPJ_UINT32)subsampling_dx + 1;
864 image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) *
865 (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) *
866 (OPJ_UINT32)subsampling_dy + 1;
869 for (y = 0; y < image_height; y++) {
873 index = (int)((image_height - y - 1) * image_width);
875 index = (int)(y * image_width);
879 for (x = 0; x < image_width; x++) {
880 unsigned char r, g, b;
882 if (!fread(&b, 1, 1, f)) {
884 "\nError: fread return a number of element different from the expected.\n");
885 opj_image_destroy(image);
889 if (!fread(&g, 1, 1, f)) {
891 "\nError: fread return a number of element different from the expected.\n");
892 opj_image_destroy(image);
896 if (!fread(&r, 1, 1, f)) {
898 "\nError: fread return a number of element different from the expected.\n");
899 opj_image_destroy(image);
904 image->comps[0].data[index] = r;
905 image->comps[1].data[index] = g;
906 image->comps[2].data[index] = b;
909 } else if (numcomps == 4) {
910 for (x = 0; x < image_width; x++) {
911 unsigned char r, g, b, a;
912 if (!fread(&b, 1, 1, f)) {
914 "\nError: fread return a number of element different from the expected.\n");
915 opj_image_destroy(image);
919 if (!fread(&g, 1, 1, f)) {
921 "\nError: fread return a number of element different from the expected.\n");
922 opj_image_destroy(image);
926 if (!fread(&r, 1, 1, f)) {
928 "\nError: fread return a number of element different from the expected.\n");
929 opj_image_destroy(image);
933 if (!fread(&a, 1, 1, f)) {
935 "\nError: fread return a number of element different from the expected.\n");
936 opj_image_destroy(image);
941 image->comps[0].data[index] = r;
942 image->comps[1].data[index] = g;
943 image->comps[2].data[index] = b;
944 image->comps[3].data[index] = a;
948 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
955 int imagetotga(opj_image_t * image, const char *outfile)
957 int width, height, bpp, x, y;
958 OPJ_BOOL write_alpha;
960 int adjustR, adjustG = 0, adjustB = 0, fails;
961 unsigned int alpha_channel;
969 fdest = fopen(outfile, "wb");
971 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
975 for (i = 0; i < image->numcomps - 1; i++) {
976 if ((image->comps[0].dx != image->comps[i + 1].dx)
977 || (image->comps[0].dy != image->comps[i + 1].dy)
978 || (image->comps[0].prec != image->comps[i + 1].prec)
979 || (image->comps[0].sgnd != image->comps[i + 1].sgnd)) {
982 "Unable to create a tga file with such J2K image charateristics.\n");
987 width = (int)image->comps[0].w;
988 height = (int)image->comps[0].h;
990 /* Mono with alpha, or RGB with alpha. */
991 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
993 /* Write TGA header */
994 bpp = write_alpha ? 32 : 24;
996 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
1000 alpha_channel = image->numcomps - 1;
1002 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
1004 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1005 if (image->numcomps >= 3) {
1006 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1007 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1010 for (y = 0; y < height; y++) {
1011 unsigned int index = (unsigned int)(y * width);
1013 for (x = 0; x < width; x++, index++) {
1014 r = (float)(image->comps[0].data[index] + adjustR);
1016 if (image->numcomps > 2) {
1017 g = (float)(image->comps[1].data[index] + adjustG);
1018 b = (float)(image->comps[2].data[index] + adjustB);
1025 /* TGA format writes BGR ... */
1028 } else if (b < 0.) {
1031 value = (unsigned char)(b * scale);
1032 res = fwrite(&value, 1, 1, fdest);
1035 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1040 } else if (g < 0.) {
1043 value = (unsigned char)(g * scale);
1044 res = fwrite(&value, 1, 1, fdest);
1047 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1052 } else if (r < 0.) {
1055 value = (unsigned char)(r * scale);
1056 res = fwrite(&value, 1, 1, fdest);
1059 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1064 a = (float)(image->comps[alpha_channel].data[index]);
1067 } else if (a < 0.) {
1070 value = (unsigned char)(a * scale);
1071 res = fwrite(&value, 1, 1, fdest);
1074 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1087 /* -->> -->> -->> -->>
1091 <<-- <<-- <<-- <<-- */
1094 static unsigned char readuchar(FILE * f)
1097 if (!fread(&c1, 1, 1, f)) {
1099 "\nError: fread return a number of element different from the expected.\n");
1105 static unsigned short readushort(FILE * f, int bigendian)
1107 unsigned char c1, c2;
1108 if (!fread(&c1, 1, 1, f)) {
1110 "\nError: fread return a number of element different from the expected.\n");
1113 if (!fread(&c2, 1, 1, f)) {
1115 "\nError: fread return a number of element different from the expected.\n");
1119 return (unsigned short)((c1 << 8) + c2);
1121 return (unsigned short)((c2 << 8) + c1);
1125 static unsigned int readuint(FILE * f, int bigendian)
1127 unsigned char c1, c2, c3, c4;
1128 if (!fread(&c1, 1, 1, f)) {
1130 "\nError: fread return a number of element different from the expected.\n");
1133 if (!fread(&c2, 1, 1, f)) {
1135 "\nError: fread return a number of element different from the expected.\n");
1138 if (!fread(&c3, 1, 1, f)) {
1140 "\nError: fread return a number of element different from the expected.\n");
1143 if (!fread(&c4, 1, 1, f)) {
1145 "\nError: fread return a number of element different from the expected.\n");
1149 return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(
1152 return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(
1157 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1161 int i, numcomps, max;
1162 OPJ_COLOR_SPACE color_space;
1163 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1164 opj_image_t * image = NULL;
1165 int adjustS, ushift, dshift, force8;
1166 OPJ_UINT64 expected_file_size;
1168 char endian1, endian2, sign;
1173 opj_image_comp_t *comp = NULL;
1176 color_space = OPJ_CLRSPC_GRAY;
1178 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1182 f = fopen(filename, "rb");
1184 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1188 fseek(f, 0, SEEK_SET);
1189 if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
1190 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1193 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1199 while (signtmp[i] != '\0') {
1200 if (signtmp[i] == '-') {
1207 if (endian1 == 'M' && endian2 == 'L') {
1209 } else if (endian2 == 'M' && endian1 == 'L') {
1213 fprintf(stderr, "Bad pgx header, please check input file\n");
1217 if (w < 1 || h < 1 || prec < 1 || prec > 31) {
1219 fprintf(stderr, "Bad pgx header, please check input file\n");
1223 expected_file_size =
1224 (OPJ_UINT64)w * (OPJ_UINT64)h * (prec > 16 ? 4 : prec > 8 ? 2 : 1);
1225 if (expected_file_size > 10000000U) {
1227 long curpos = ftell(f);
1228 if (expected_file_size > (OPJ_UINT64)INT_MAX) {
1229 expected_file_size = (OPJ_UINT64)INT_MAX;
1231 fseek(f, (long)expected_file_size - 1, SEEK_SET);
1232 if (fread(&ch, 1, 1, f) != 1) {
1233 fprintf(stderr, "File too short\n");
1237 fseek(f, curpos, SEEK_SET);
1240 /* initialize image component */
1242 cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
1243 cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
1244 cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx +
1245 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx
1247 cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy +
1248 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy
1259 dshift = prec - ushift;
1260 if (cmptparm.sgnd) {
1261 adjustS = (1 << (prec - 1));
1268 ushift = dshift = force8 = adjustS = 0;
1271 cmptparm.prec = (OPJ_UINT32)prec;
1272 cmptparm.bpp = (OPJ_UINT32)prec;
1273 cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
1274 cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
1276 /* create the image */
1277 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
1282 /* set image offset and reference grid */
1283 image->x0 = cmptparm.x0;
1284 image->y0 = cmptparm.x0;
1285 image->x1 = cmptparm.w;
1286 image->y1 = cmptparm.h;
1288 /* set image data */
1290 comp = &image->comps[0];
1292 for (i = 0; i < w * h; i++) {
1295 v = readuchar(f) + adjustS;
1296 v = (v << ushift) + (v >> dshift);
1297 comp->data[i] = (unsigned char)v;
1305 if (comp->prec == 8) {
1309 v = (char) readuchar(f);
1311 } else if (comp->prec <= 16) {
1313 v = readushort(f, bigendian);
1315 v = (short) readushort(f, bigendian);
1319 v = (int)readuint(f, bigendian);
1321 v = (int) readuint(f, bigendian);
1330 comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
1335 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
1337 static INLINE int clamp(const int value, const int prec, const int sgnd)
1341 return CLAMP(value, -128, 127);
1342 } else if (prec <= 16) {
1343 return CLAMP(value, -32768, 32767);
1345 return CLAMP(value, -2147483647 - 1, 2147483647);
1349 return CLAMP(value, 0, 255);
1350 } else if (prec <= 16) {
1351 return CLAMP(value, 0, 65535);
1353 return value; /*CLAMP(value,0,4294967295);*/
1358 int imagetopgx(opj_image_t * image, const char *outfile)
1361 int i, j, fails = 1;
1362 unsigned int compno;
1365 for (compno = 0; compno < image->numcomps; compno++) {
1366 opj_image_comp_t *comp = &image->comps[compno];
1367 char bname[256]; /* buffer for name */
1368 char *name = bname; /* pointer */
1371 const size_t olen = strlen(outfile);
1372 const size_t dotpos = olen - 4;
1373 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1375 if (outfile[dotpos] != '.') {
1376 /* `pgx` was recognized but there is no dot at expected position */
1377 fprintf(stderr, "ERROR -> Impossible happen.");
1381 name = (char*)malloc(total + 1);
1383 fprintf(stderr, "imagetopgx: memory out\n");
1387 strncpy(name, outfile, dotpos);
1388 sprintf(name + dotpos, "_%u.pgx", compno);
1389 fdest = fopen(name, "wb");
1390 /* don't need name anymore */
1394 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1401 w = (int)image->comps[compno].w;
1402 h = (int)image->comps[compno].h;
1404 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
1407 if (comp->prec <= 8) {
1409 } else if (comp->prec <= 16) {
1415 for (i = 0; i < w * h; i++) {
1416 /* FIXME: clamp func is being called within a loop */
1417 const int val = clamp(image->comps[compno].data[i],
1418 (int)comp->prec, (int)comp->sgnd);
1420 for (j = nbytes - 1; j >= 0; j--) {
1421 int v = (int)(val >> (j * 8));
1422 unsigned char byte = (unsigned char)v;
1423 res = fwrite(&byte, 1, 1, fdest);
1426 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1449 /* -->> -->> -->> -->>
1453 <<-- <<-- <<-- <<-- */
1456 int width, height, maxval, depth, format;
1457 char rgb, rgba, gray, graya, bw;
1461 static char *skip_white(char *s)
1465 if (*s == '\n' || *s == '\r') {
1478 static char *skip_int(char *start, int *out_n)
1485 s = skip_white(start);
1499 *out_n = atoi(start);
1504 static char *skip_idf(char *start, char out_idf[256])
1509 s = skip_white(start);
1516 if (isalpha(*s) || *s == '_') {
1524 strncpy(out_idf, start, 255);
1529 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1531 int format, end, ttype;
1532 char idf[256], type[256];
1535 if (fgets(line, 250, reader) == NULL) {
1536 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1540 if (line[0] != 'P') {
1541 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1544 format = atoi(line + 1);
1545 if (format < 1 || format > 7) {
1546 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1549 ph->format = format;
1552 while (fgets(line, 250, reader)) {
1563 s = skip_idf(s, idf);
1565 if (s == NULL || *s == 0) {
1569 if (strcmp(idf, "ENDHDR") == 0) {
1573 if (strcmp(idf, "WIDTH") == 0) {
1574 s = skip_int(s, &ph->width);
1575 if (s == NULL || *s == 0) {
1581 if (strcmp(idf, "HEIGHT") == 0) {
1582 s = skip_int(s, &ph->height);
1583 if (s == NULL || *s == 0) {
1589 if (strcmp(idf, "DEPTH") == 0) {
1590 s = skip_int(s, &ph->depth);
1591 if (s == NULL || *s == 0) {
1597 if (strcmp(idf, "MAXVAL") == 0) {
1598 s = skip_int(s, &ph->maxval);
1599 if (s == NULL || *s == 0) {
1605 if (strcmp(idf, "TUPLTYPE") == 0) {
1606 s = skip_idf(s, type);
1607 if (s == NULL || *s == 0) {
1611 if (strcmp(type, "BLACKANDWHITE") == 0) {
1616 if (strcmp(type, "GRAYSCALE") == 0) {
1621 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1626 if (strcmp(type, "RGB") == 0) {
1631 if (strcmp(type, "RGB_ALPHA") == 0) {
1636 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1639 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1641 } /* if(format == 7) */
1643 /* Here format is in range [1,6] */
1644 if (ph->width == 0) {
1645 s = skip_int(s, &ph->width);
1646 if ((s == NULL) || (*s == 0) || (ph->width < 1)) {
1651 if (ph->height == 0) {
1652 s = skip_int(s, &ph->height);
1653 if ((s == NULL) && allow_null) {
1656 if ((s == NULL) || (*s == 0) || (ph->height < 1)) {
1659 if (format == 1 || format == 4) {
1664 /* here, format is in P2, P3, P5, P6 */
1665 s = skip_int(s, &ph->maxval);
1666 if ((s == NULL) && allow_null) {
1669 if ((s == NULL) || (*s == 0)) {
1673 }/* while(fgets( ) */
1674 if (format == 2 || format == 3 || format > 4) {
1675 if (ph->maxval < 1 || ph->maxval > 65535) {
1679 if (ph->width < 1 || ph->height < 1) {
1685 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1688 if (ph->depth < 1 || ph->depth > 4) {
1697 if (format == 1 || format == 4) {
1703 static int has_prec(int val)
1753 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1755 int subsampling_dx = parameters->subsampling_dx;
1756 int subsampling_dy = parameters->subsampling_dy;
1759 int i, compno, numcomps, w, h, prec, format;
1760 OPJ_COLOR_SPACE color_space;
1761 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1762 opj_image_t * image = NULL;
1763 struct pnm_header header_info;
1765 if ((fp = fopen(filename, "rb")) == NULL) {
1766 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1769 memset(&header_info, 0, sizeof(struct pnm_header));
1771 read_pnm_header(fp, &header_info);
1773 if (!header_info.ok) {
1778 /* This limitation could be removed by making sure to use size_t below */
1779 if (header_info.height != 0 &&
1780 header_info.width > INT_MAX / header_info.height) {
1781 fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
1782 header_info.width, header_info.height);
1787 format = header_info.format;
1790 case 1: /* ascii bitmap */
1791 case 4: /* raw bitmap */
1795 case 2: /* ascii greymap */
1796 case 5: /* raw greymap */
1800 case 3: /* ascii pixmap */
1801 case 6: /* raw pixmap */
1805 case 7: /* arbitrary map */
1806 numcomps = header_info.depth;
1814 color_space = OPJ_CLRSPC_GRAY; /* GRAY, GRAYA */
1816 color_space = OPJ_CLRSPC_SRGB; /* RGB, RGBA */
1819 prec = has_prec(header_info.maxval);
1825 w = header_info.width;
1826 h = header_info.height;
1827 subsampling_dx = parameters->subsampling_dx;
1828 subsampling_dy = parameters->subsampling_dy;
1830 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
1832 for (i = 0; i < numcomps; i++) {
1833 cmptparm[i].prec = (OPJ_UINT32)prec;
1834 cmptparm[i].bpp = (OPJ_UINT32)prec;
1835 cmptparm[i].sgnd = 0;
1836 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
1837 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
1838 cmptparm[i].w = (OPJ_UINT32)w;
1839 cmptparm[i].h = (OPJ_UINT32)h;
1841 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
1848 /* set image offset and reference grid */
1849 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
1850 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
1851 image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx
1853 image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy
1856 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1859 for (i = 0; i < w * h; i++) {
1860 for (compno = 0; compno < numcomps; compno++) {
1862 if (fscanf(fp, "%u", &index) != 1) {
1864 "\nWARNING: fscanf return a number of element different from the expected.\n");
1867 image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval;
1870 } else if ((format == 5)
1873 && (header_info.gray || header_info.graya
1874 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1875 unsigned char c0, c1, one;
1879 for (i = 0; i < w * h; i++) {
1880 for (compno = 0; compno < numcomps; compno++) {
1881 if (!fread(&c0, 1, 1, fp)) {
1883 "\nError: fread return a number of element different from the expected.\n");
1884 opj_image_destroy(image);
1889 image->comps[compno].data[i] = c0;
1891 if (!fread(&c1, 1, 1, fp)) {
1893 "\nError: fread return a number of element different from the expected.\n");
1896 image->comps[compno].data[i] = ((c0 << 8) | c1);
1900 } else if (format == 1) { /* ascii bitmap */
1901 for (i = 0; i < w * h; i++) {
1904 if (fscanf(fp, "%u", &index) != 1) {
1906 "\nWARNING: fscanf return a number of element different from the expected.\n");
1909 image->comps[0].data[i] = (index ? 0 : 255);
1911 } else if (format == 4) {
1916 for (y = 0; y < h; ++y) {
1920 for (x = 0; x < w; ++x) {
1923 uc = (unsigned char)getc(fp);
1925 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
1930 } else if ((format == 7 && header_info.bw)) { /*MONO*/
1933 for (i = 0; i < w * h; ++i) {
1934 if (!fread(&uc, 1, 1, fp)) {
1936 "\nError: fread return a number of element different from the expected.\n");
1938 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
1946 static int are_comps_similar(opj_image_t * image)
1949 for (i = 1; i < image->numcomps; i++) {
1950 if (image->comps[0].dx != image->comps[i].dx ||
1951 image->comps[0].dy != image->comps[i].dy ||
1953 (image->comps[0].prec != image->comps[i].prec ||
1954 image->comps[0].sgnd != image->comps[i].sgnd))) {
1962 int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
1964 int *red, *green, *blue, *alpha;
1967 unsigned int compno, ncomp;
1968 int adjustR, adjustG, adjustB, adjustA;
1969 int fails, two, want_gray, has_alpha, triple;
1972 const char *tmp = outfile;
1977 if ((prec = (int)image->comps[0].prec) > 16) {
1978 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1979 "\n\t: refused.\n", __FILE__, __LINE__, prec);
1982 two = has_alpha = 0;
1984 ncomp = image->numcomps;
1990 want_gray = (*tmp == 'g' || *tmp == 'G');
1991 ncomp = image->numcomps;
1997 if ((force_split == 0) && ncomp >= 2 &&
1998 are_comps_similar(image)) {
1999 fdest = fopen(outfile, "wb");
2002 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
2006 triple = (ncomp > 2);
2007 wr = (int)image->comps[0].w;
2008 hr = (int)image->comps[0].h;
2009 max = (1 << prec) - 1;
2010 has_alpha = (ncomp == 4 || ncomp == 2);
2012 red = image->comps[0].data;
2015 green = image->comps[1].data;
2016 blue = image->comps[2].data;
2018 green = blue = NULL;
2022 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
2024 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n"
2025 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
2026 wr, hr, ncomp, max, tt);
2027 alpha = image->comps[ncomp - 1].data;
2028 adjustA = (image->comps[ncomp - 1].sgnd ?
2029 1 << (image->comps[ncomp - 1].prec - 1) : 0);
2031 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
2032 opj_version(), wr, hr, max);
2035 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
2038 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
2039 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
2041 adjustG = adjustB = 0;
2044 for (i = 0; i < wr * hr; ++i) {
2055 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2058 v = *green + adjustG;
2067 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2069 v = *blue + adjustB;
2078 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2083 v = *alpha + adjustA;
2092 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2106 fprintf(fdest, "%c", (unsigned char)v);
2115 fprintf(fdest, "%c", (unsigned char)v);
2123 fprintf(fdest, "%c", (unsigned char)v);
2133 fprintf(fdest, "%c", (unsigned char)v);
2143 if (image->numcomps > ncomp) {
2144 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2145 fprintf(stderr, " is written to the file\n");
2147 destname = (char*)malloc(strlen(outfile) + 8);
2148 if (destname == NULL) {
2149 fprintf(stderr, "imagetopnm: memory out\n");
2152 for (compno = 0; compno < ncomp; compno++) {
2154 /*sprintf(destname, "%d.%s", compno, outfile);*/
2155 const size_t olen = strlen(outfile);
2156 const size_t dotpos = olen - 4;
2158 strncpy(destname, outfile, dotpos);
2159 sprintf(destname + dotpos, "_%u.pgm", compno);
2161 sprintf(destname, "%s", outfile);
2164 fdest = fopen(destname, "wb");
2166 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2170 wr = (int)image->comps[compno].w;
2171 hr = (int)image->comps[compno].h;
2172 prec = (int)image->comps[compno].prec;
2173 max = (1 << prec) - 1;
2175 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2176 opj_version(), wr, hr, max);
2178 red = image->comps[compno].data;
2180 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2183 for (i = 0; i < wr * hr; i++) {
2193 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2204 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2207 } else { /* prec <= 8 */
2208 for (i = 0; i < wr * hr; ++i) {
2217 fprintf(fdest, "%c", (unsigned char)v);
2227 /* -->> -->> -->> -->>
2231 <<-- <<-- <<-- <<-- */
2232 static opj_image_t* rawtoimage_common(const char *filename,
2233 opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian)
2235 int subsampling_dx = parameters->subsampling_dx;
2236 int subsampling_dy = parameters->subsampling_dy;
2239 int i, compno, numcomps, w, h;
2240 OPJ_COLOR_SPACE color_space;
2241 opj_image_cmptparm_t *cmptparm;
2242 opj_image_t * image = NULL;
2245 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2246 raw_cp->rawBitDepth)) == 0) {
2247 fprintf(stderr, "\nError: invalid raw image parameters\n");
2248 fprintf(stderr, "Please use the Format option -F:\n");
2250 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
2252 "If subsampling is omitted, 1x1 is assumed for all components\n");
2254 "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
2255 fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
2256 fprintf(stderr, "Aborting.\n");
2260 f = fopen(filename, "rb");
2262 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2263 fprintf(stderr, "Aborting\n");
2266 numcomps = raw_cp->rawComp;
2268 /* FIXME ADE at this point, tcp_mct has not been properly set in calling function */
2269 if (numcomps == 1) {
2270 color_space = OPJ_CLRSPC_GRAY;
2271 } else if ((numcomps >= 3) && (parameters->tcp_mct == 0)) {
2272 color_space = OPJ_CLRSPC_SYCC;
2273 } else if ((numcomps >= 3) && (parameters->tcp_mct != 2)) {
2274 color_space = OPJ_CLRSPC_SRGB;
2276 color_space = OPJ_CLRSPC_UNKNOWN;
2278 w = raw_cp->rawWidth;
2279 h = raw_cp->rawHeight;
2280 cmptparm = (opj_image_cmptparm_t*) calloc((OPJ_UINT32)numcomps,
2281 sizeof(opj_image_cmptparm_t));
2283 fprintf(stderr, "Failed to allocate image components parameters !!\n");
2284 fprintf(stderr, "Aborting\n");
2288 /* initialize image components */
2289 for (i = 0; i < numcomps; i++) {
2290 cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
2291 cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
2292 cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
2293 cmptparm[i].dx = (OPJ_UINT32)(subsampling_dx * raw_cp->rawComps[i].dx);
2294 cmptparm[i].dy = (OPJ_UINT32)(subsampling_dy * raw_cp->rawComps[i].dy);
2295 cmptparm[i].w = (OPJ_UINT32)w;
2296 cmptparm[i].h = (OPJ_UINT32)h;
2298 /* create the image */
2299 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2305 /* set image offset and reference grid */
2306 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2307 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2308 image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) *
2309 (OPJ_UINT32)subsampling_dx + 1;
2310 image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) *
2311 (OPJ_UINT32)subsampling_dy + 1;
2313 if (raw_cp->rawBitDepth <= 8) {
2314 unsigned char value = 0;
2315 for (compno = 0; compno < numcomps; compno++) {
2316 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2317 raw_cp->rawComps[compno].dy);
2318 for (i = 0; i < nloop; i++) {
2319 if (!fread(&value, 1, 1, f)) {
2320 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2321 opj_image_destroy(image);
2325 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
2328 } else if (raw_cp->rawBitDepth <= 16) {
2329 unsigned short value;
2330 for (compno = 0; compno < numcomps; compno++) {
2331 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2332 raw_cp->rawComps[compno].dy);
2333 for (i = 0; i < nloop; i++) {
2334 unsigned char temp1;
2335 unsigned char temp2;
2336 if (!fread(&temp1, 1, 1, f)) {
2337 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2338 opj_image_destroy(image);
2342 if (!fread(&temp2, 1, 1, f)) {
2343 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2344 opj_image_destroy(image);
2349 value = (unsigned short)((temp1 << 8) + temp2);
2351 value = (unsigned short)((temp2 << 8) + temp1);
2353 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
2358 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2359 opj_image_destroy(image);
2364 if (fread(&ch, 1, 1, f)) {
2365 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
2372 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters,
2373 raw_cparameters_t *raw_cp)
2375 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
2378 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2379 raw_cparameters_t *raw_cp)
2381 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
2384 static int imagetoraw_common(opj_image_t * image, const char *outfile,
2385 OPJ_BOOL big_endian)
2387 FILE *rawFile = NULL;
2389 unsigned int compno, numcomps;
2391 int line, row, curr, mask;
2396 if ((image->numcomps * image->x1 * image->y1) == 0) {
2397 fprintf(stderr, "\nError: invalid raw image parameters\n");
2401 numcomps = image->numcomps;
2407 for (compno = 1; compno < numcomps; ++compno) {
2408 if (image->comps[0].dx != image->comps[compno].dx) {
2411 if (image->comps[0].dy != image->comps[compno].dy) {
2414 if (image->comps[0].prec != image->comps[compno].prec) {
2417 if (image->comps[0].sgnd != image->comps[compno].sgnd) {
2421 if (compno != numcomps) {
2423 "imagetoraw_common: All components shall have the same subsampling, same bit depth, same sign.\n");
2424 fprintf(stderr, "\tAborting\n");
2428 rawFile = fopen(outfile, "wb");
2430 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2435 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
2437 for (compno = 0; compno < image->numcomps; compno++) {
2438 fprintf(stdout, "Component %u characteristics: %dx%dx%d %s\n", compno,
2439 image->comps[compno].w,
2440 image->comps[compno].h, image->comps[compno].prec,
2441 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
2443 w = (int)image->comps[compno].w;
2444 h = (int)image->comps[compno].h;
2446 if (image->comps[compno].prec <= 8) {
2447 if (image->comps[compno].sgnd == 1) {
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 < -128) {
2458 uc = (unsigned char)(curr & mask);
2459 res = fwrite(&uc, 1, 1, rawFile);
2461 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2467 } else if (image->comps[compno].sgnd == 0) {
2468 mask = (1 << image->comps[compno].prec) - 1;
2469 ptr = image->comps[compno].data;
2470 for (line = 0; line < h; line++) {
2471 for (row = 0; row < w; row++) {
2475 } else if (curr < 0) {
2478 uc = (unsigned char)(curr & mask);
2479 res = fwrite(&uc, 1, 1, rawFile);
2481 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2488 } else if (image->comps[compno].prec <= 16) {
2489 if (image->comps[compno].sgnd == 1) {
2492 signed char vals[2];
2494 mask = (1 << image->comps[compno].prec) - 1;
2495 ptr = image->comps[compno].data;
2496 for (line = 0; line < h; line++) {
2497 for (row = 0; row < w; row++) {
2501 } else if (curr < -32768) {
2504 uc16.val = (signed short)(curr & mask);
2505 res = fwrite(uc16.vals, 1, 2, rawFile);
2507 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2513 } else if (image->comps[compno].sgnd == 0) {
2516 unsigned char vals[2];
2518 mask = (1 << image->comps[compno].prec) - 1;
2519 ptr = image->comps[compno].data;
2520 for (line = 0; line < h; line++) {
2521 for (row = 0; row < w; row++) {
2525 } else if (curr < 0) {
2528 uc16.val = (unsigned short)(curr & mask);
2529 res = fwrite(uc16.vals, 1, 2, rawFile);
2531 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2538 } else if (image->comps[compno].prec <= 32) {
2539 fprintf(stderr, "More than 16 bits per component not handled yet\n");
2542 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
2552 int imagetoraw(opj_image_t * image, const char *outfile)
2554 return imagetoraw_common(image, outfile, OPJ_TRUE);
2557 int imagetorawl(opj_image_t * image, const char *outfile)
2559 return imagetoraw_common(image, outfile, OPJ_FALSE);