2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include "opj_apps_config.h"
49 * Get logarithm of an integer and round downwards.
53 static int int_floorlog2(int a)
56 for (l = 0; a > 1; l++) {
62 /* Component precision scaling */
63 void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision)
67 OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32) - 1);
69 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
71 umax = (1U << precision) - 1U;
74 if (component->sgnd) {
75 OPJ_INT32* l_data = component->data;
76 OPJ_INT32 max = (OPJ_INT32)(umax / 2U);
77 OPJ_INT32 min = -max - 1;
78 for (i = 0; i < len; ++i) {
79 if (l_data[i] > max) {
81 } else if (l_data[i] < min) {
86 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
87 for (i = 0; i < len; ++i) {
88 if (l_data[i] > umax) {
93 component->prec = precision;
96 /* Component precision scaling */
97 static void scale_component_up(opj_image_comp_t* component,
102 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
103 if (component->sgnd) {
104 OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1));
105 OPJ_INT64 oldMax = (OPJ_INT64)(1U << (component->prec - 1));
106 OPJ_INT32* l_data = component->data;
107 for (i = 0; i < len; ++i) {
108 l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax);
111 OPJ_UINT64 newMax = (OPJ_UINT64)((1U << precision) - 1U);
112 OPJ_UINT64 oldMax = (OPJ_UINT64)((1U << component->prec) - 1U);
113 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
114 for (i = 0; i < len; ++i) {
115 l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax);
118 component->prec = precision;
119 component->bpp = precision;
121 void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
126 if (component->prec == precision) {
129 if (component->prec < precision) {
130 scale_component_up(component, precision);
133 shift = (int)(component->prec - precision);
134 len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
135 if (component->sgnd) {
136 OPJ_INT32* l_data = component->data;
137 for (i = 0; i < len; ++i) {
141 OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
142 for (i = 0; i < len; ++i) {
146 component->bpp = precision;
147 component->prec = precision;
151 /* planar / interleaved conversions */
152 /* used by PNG/TIFF */
153 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
156 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
158 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
162 OPJ_INT32* pDst0 = pDst[0];
163 OPJ_INT32* pDst1 = pDst[1];
165 for (i = 0; i < length; i++) {
166 pDst0[i] = pSrc[2 * i + 0];
167 pDst1[i] = pSrc[2 * i + 1];
170 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
174 OPJ_INT32* pDst0 = pDst[0];
175 OPJ_INT32* pDst1 = pDst[1];
176 OPJ_INT32* pDst2 = pDst[2];
178 for (i = 0; i < length; i++) {
179 pDst0[i] = pSrc[3 * i + 0];
180 pDst1[i] = pSrc[3 * i + 1];
181 pDst2[i] = pSrc[3 * i + 2];
184 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
188 OPJ_INT32* pDst0 = pDst[0];
189 OPJ_INT32* pDst1 = pDst[1];
190 OPJ_INT32* pDst2 = pDst[2];
191 OPJ_INT32* pDst3 = pDst[3];
193 for (i = 0; i < length; i++) {
194 pDst0[i] = pSrc[4 * i + 0];
195 pDst1[i] = pSrc[4 * i + 1];
196 pDst2[i] = pSrc[4 * i + 2];
197 pDst3[i] = pSrc[4 * i + 3];
200 const convert_32s_CXPX convert_32s_CXPX_LUT[5] = {
208 static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
209 OPJ_SIZE_T length, OPJ_INT32 adjust)
212 const OPJ_INT32* pSrc0 = pSrc[0];
214 for (i = 0; i < length; i++) {
215 pDst[i] = pSrc0[i] + adjust;
218 static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
219 OPJ_SIZE_T length, OPJ_INT32 adjust)
222 const OPJ_INT32* pSrc0 = pSrc[0];
223 const OPJ_INT32* pSrc1 = pSrc[1];
225 for (i = 0; i < length; i++) {
226 pDst[2 * i + 0] = pSrc0[i] + adjust;
227 pDst[2 * i + 1] = pSrc1[i] + adjust;
230 static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
231 OPJ_SIZE_T length, OPJ_INT32 adjust)
234 const OPJ_INT32* pSrc0 = pSrc[0];
235 const OPJ_INT32* pSrc1 = pSrc[1];
236 const OPJ_INT32* pSrc2 = pSrc[2];
238 for (i = 0; i < length; i++) {
239 pDst[3 * i + 0] = pSrc0[i] + adjust;
240 pDst[3 * i + 1] = pSrc1[i] + adjust;
241 pDst[3 * i + 2] = pSrc2[i] + adjust;
244 static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
245 OPJ_SIZE_T length, OPJ_INT32 adjust)
248 const OPJ_INT32* pSrc0 = pSrc[0];
249 const OPJ_INT32* pSrc1 = pSrc[1];
250 const OPJ_INT32* pSrc2 = pSrc[2];
251 const OPJ_INT32* pSrc3 = pSrc[3];
253 for (i = 0; i < length; i++) {
254 pDst[4 * i + 0] = pSrc0[i] + adjust;
255 pDst[4 * i + 1] = pSrc1[i] + adjust;
256 pDst[4 * i + 2] = pSrc2[i] + adjust;
257 pDst[4 * i + 3] = pSrc3[i] + adjust;
260 const convert_32s_PXCX convert_32s_PXCX_LUT[5] = {
268 /* bit depth conversions */
269 /* used by PNG/TIFF up to 8bpp */
270 static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
274 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
275 OPJ_UINT32 val = *pSrc++;
276 pDst[i + 0] = (OPJ_INT32)(val >> 7);
277 pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
278 pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
279 pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
280 pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
281 pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
282 pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
283 pDst[i + 7] = (OPJ_INT32)(val & 0x1U);
286 OPJ_UINT32 val = *pSrc++;
287 length = length & 7U;
288 pDst[i + 0] = (OPJ_INT32)(val >> 7);
291 pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
293 pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
295 pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
297 pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
299 pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
301 pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
310 static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
314 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
315 OPJ_UINT32 val = *pSrc++;
316 pDst[i + 0] = (OPJ_INT32)(val >> 6);
317 pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
318 pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
319 pDst[i + 3] = (OPJ_INT32)(val & 0x3U);
322 OPJ_UINT32 val = *pSrc++;
323 length = length & 3U;
324 pDst[i + 0] = (OPJ_INT32)(val >> 6);
327 pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
329 pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
335 static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
339 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
340 OPJ_UINT32 val = *pSrc++;
341 pDst[i + 0] = (OPJ_INT32)(val >> 4);
342 pDst[i + 1] = (OPJ_INT32)(val & 0xFU);
345 OPJ_UINT8 val = *pSrc++;
346 pDst[i + 0] = (OPJ_INT32)(val >> 4);
349 static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
353 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
354 OPJ_UINT32 val0 = *pSrc++;
355 OPJ_UINT32 val1 = *pSrc++;
356 OPJ_UINT32 val2 = *pSrc++;
357 pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
358 pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
359 pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
360 pDst[i + 3] = (OPJ_INT32)(val2 & 0x3FU);
364 OPJ_UINT32 val0 = *pSrc++;
365 length = length & 3U;
366 pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
369 OPJ_UINT32 val1 = *pSrc++;
370 pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
372 OPJ_UINT32 val2 = *pSrc++;
373 pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
378 static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
382 for (i = 0; i < length; i++) {
386 const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = {
399 static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
403 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
404 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
405 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
406 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
407 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
408 OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
409 OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
410 OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
411 OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
413 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
414 (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
418 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
419 OPJ_UINT32 src1 = 0U;
420 OPJ_UINT32 src2 = 0U;
421 OPJ_UINT32 src3 = 0U;
422 OPJ_UINT32 src4 = 0U;
423 OPJ_UINT32 src5 = 0U;
424 OPJ_UINT32 src6 = 0U;
425 length = length & 7U;
428 src1 = (OPJ_UINT32)pSrc[i + 1];
430 src2 = (OPJ_UINT32)pSrc[i + 2];
432 src3 = (OPJ_UINT32)pSrc[i + 3];
434 src4 = (OPJ_UINT32)pSrc[i + 4];
436 src5 = (OPJ_UINT32)pSrc[i + 5];
438 src6 = (OPJ_UINT32)pSrc[i + 6];
445 *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
446 (src4 << 3) | (src5 << 2) | (src6 << 1));
450 static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
454 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
455 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
456 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
457 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
458 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
460 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
464 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
465 OPJ_UINT32 src1 = 0U;
466 OPJ_UINT32 src2 = 0U;
467 length = length & 3U;
470 src1 = (OPJ_UINT32)pSrc[i + 1];
472 src2 = (OPJ_UINT32)pSrc[i + 2];
475 *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
479 static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
483 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
484 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
485 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
487 *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
491 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
492 *pDst++ = (OPJ_BYTE)((src0 << 4));
496 static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
500 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
501 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
502 OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
503 OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
504 OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
506 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
507 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
508 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
512 OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
513 OPJ_UINT32 src1 = 0U;
514 OPJ_UINT32 src2 = 0U;
515 length = length & 3U;
518 src1 = (OPJ_UINT32)pSrc[i + 1];
520 src2 = (OPJ_UINT32)pSrc[i + 2];
523 *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
525 *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
527 *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
532 static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
536 for (i = 0; i < length; ++i) {
537 pDst[i] = (OPJ_BYTE)pSrc[i];
540 const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = {
552 /* -->> -->> -->> -->>
556 <<-- <<-- <<-- <<-- */
558 #ifdef INFORMATION_ONLY
559 /* TGA header definition. */
561 unsigned char id_length; /* Image id field length */
562 unsigned char colour_map_type; /* Colour map type */
563 unsigned char image_type; /* Image type */
565 ** Colour map specification
567 unsigned short colour_map_index; /* First entry index */
568 unsigned short colour_map_length; /* Colour map length */
569 unsigned char colour_map_entry_size; /* Colour map entry size */
571 ** Image specification
573 unsigned short x_origin; /* x origin of image */
574 unsigned short y_origin; /* u origin of image */
575 unsigned short image_width; /* Image width */
576 unsigned short image_height; /* Image height */
577 unsigned char pixel_depth; /* Pixel depth */
578 unsigned char image_desc; /* Image descriptor */
580 #endif /* INFORMATION_ONLY */
582 static unsigned short get_ushort(const unsigned char *data)
584 unsigned short val = *(const unsigned short *)data;
585 #ifdef OPJ_BIG_ENDIAN
586 val = ((val & 0xffU) << 8) | (val >> 8);
591 #define TGA_HEADER_SIZE 18
593 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
594 unsigned int *width, unsigned int *height, int *flip_image)
597 unsigned char tga[TGA_HEADER_SIZE];
598 unsigned char id_len, /*cmap_type,*/ image_type;
599 unsigned char pixel_depth, image_desc;
600 unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
601 unsigned short /*x_origin, y_origin,*/ image_w, image_h;
603 if (!bits_per_pixel || !width || !height || !flip_image) {
607 if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
609 "\nError: fread return a number of element different from the expected.\n");
613 /*cmap_type = tga[1];*/
615 /*cmap_index = get_ushort(&tga[3]);*/
616 cmap_len = get_ushort(&tga[5]);
617 cmap_entry_size = tga[7];
621 x_origin = get_ushort(&tga[8]);
622 y_origin = get_ushort(&tga[10]);
624 image_w = get_ushort(&tga[12]);
625 image_h = get_ushort(&tga[14]);
626 pixel_depth = tga[16];
627 image_desc = tga[17];
629 *bits_per_pixel = (unsigned int)pixel_depth;
630 *width = (unsigned int)image_w;
631 *height = (unsigned int)image_h;
633 /* Ignore tga identifier, if present ... */
635 unsigned char *id = (unsigned char *) malloc(id_len);
637 fprintf(stderr, "tga_readheader: memory out\n");
640 if (!fread(id, id_len, 1, fp)) {
642 "\nError: fread return a number of element different from the expected.\n");
649 /* Test for compressed formats ... not yet supported ...
650 // Note :- 9 - RLE encoded palettized.
651 // 10 - RLE encoded RGB. */
652 if (image_type > 8) {
653 fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
657 *flip_image = !(image_desc & 32);
659 /* Palettized formats are not yet supported, skip over the palette, if present ... */
660 palette_size = cmap_len * (cmap_entry_size / 8);
662 if (palette_size > 0) {
663 fprintf(stderr, "File contains a palette - not yet supported.");
664 fseek(fp, palette_size, SEEK_CUR);
669 #ifdef OPJ_BIG_ENDIAN
671 static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
673 return (OPJ_UINT16)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
678 static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
681 OPJ_UINT16 image_w, image_h, us0;
682 unsigned char uc0, image_type;
683 unsigned char pixel_depth, image_desc;
685 if (!bits_per_pixel || !width || !height) {
691 if (bits_per_pixel < 256) {
692 pixel_depth = (unsigned char)bits_per_pixel;
694 fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
699 if (fwrite(&uc0, 1, 1, fp) != 1) {
700 goto fails; /* id_length */
702 if (fwrite(&uc0, 1, 1, fp) != 1) {
703 goto fails; /* colour_map_type */
706 image_type = 2; /* Uncompressed. */
707 if (fwrite(&image_type, 1, 1, fp) != 1) {
712 if (fwrite(&us0, 2, 1, fp) != 1) {
713 goto fails; /* colour_map_index */
715 if (fwrite(&us0, 2, 1, fp) != 1) {
716 goto fails; /* colour_map_length */
718 if (fwrite(&uc0, 1, 1, fp) != 1) {
719 goto fails; /* colour_map_entry_size */
722 if (fwrite(&us0, 2, 1, fp) != 1) {
723 goto fails; /* x_origin */
725 if (fwrite(&us0, 2, 1, fp) != 1) {
726 goto fails; /* y_origin */
729 image_w = (unsigned short)width;
730 image_h = (unsigned short) height;
732 #ifndef OPJ_BIG_ENDIAN
733 if (fwrite(&image_w, 2, 1, fp) != 1) {
736 if (fwrite(&image_h, 2, 1, fp) != 1) {
740 image_w = swap16(image_w);
741 image_h = swap16(image_h);
742 if (fwrite(&image_w, 2, 1, fp) != 1) {
745 if (fwrite(&image_h, 2, 1, fp) != 1) {
750 if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
754 image_desc = 8; /* 8 bits per component. */
759 if (fwrite(&image_desc, 1, 1, fp) != 1) {
766 fputs("\nwrite_tgaheader: write ERROR\n", stderr);
770 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
774 unsigned int image_width, image_height, pixel_bit_depth;
777 opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
779 OPJ_COLOR_SPACE color_space;
782 int subsampling_dx, subsampling_dy;
785 f = fopen(filename, "rb");
787 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
791 if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
797 /* We currently only support 24 & 32 bit tga's ... */
798 if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
803 /* initialize image components */
804 memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
806 mono = (pixel_bit_depth == 8) ||
807 (pixel_bit_depth == 16); /* Mono with & without alpha. */
808 save_alpha = (pixel_bit_depth == 16) ||
809 (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
812 color_space = OPJ_CLRSPC_GRAY;
813 numcomps = save_alpha ? 2 : 1;
815 numcomps = save_alpha ? 4 : 3;
816 color_space = OPJ_CLRSPC_SRGB;
819 subsampling_dx = parameters->subsampling_dx;
820 subsampling_dy = parameters->subsampling_dy;
822 for (i = 0; i < numcomps; i++) {
823 cmptparm[i].prec = 8;
825 cmptparm[i].sgnd = 0;
826 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
827 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
828 cmptparm[i].w = image_width;
829 cmptparm[i].h = image_height;
832 /* create the image */
833 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
841 /* set image offset and reference grid */
842 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
843 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
844 image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) *
845 (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) *
846 (OPJ_UINT32)subsampling_dx + 1;
847 image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) *
848 (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) *
849 (OPJ_UINT32)subsampling_dy + 1;
852 for (y = 0; y < image_height; y++) {
856 index = (int)((image_height - y - 1) * image_width);
858 index = (int)(y * image_width);
862 for (x = 0; x < image_width; x++) {
863 unsigned char r, g, b;
865 if (!fread(&b, 1, 1, f)) {
867 "\nError: fread return a number of element different from the expected.\n");
868 opj_image_destroy(image);
872 if (!fread(&g, 1, 1, f)) {
874 "\nError: fread return a number of element different from the expected.\n");
875 opj_image_destroy(image);
879 if (!fread(&r, 1, 1, f)) {
881 "\nError: fread return a number of element different from the expected.\n");
882 opj_image_destroy(image);
887 image->comps[0].data[index] = r;
888 image->comps[1].data[index] = g;
889 image->comps[2].data[index] = b;
892 } else if (numcomps == 4) {
893 for (x = 0; x < image_width; x++) {
894 unsigned char r, g, b, a;
895 if (!fread(&b, 1, 1, f)) {
897 "\nError: fread return a number of element different from the expected.\n");
898 opj_image_destroy(image);
902 if (!fread(&g, 1, 1, f)) {
904 "\nError: fread return a number of element different from the expected.\n");
905 opj_image_destroy(image);
909 if (!fread(&r, 1, 1, f)) {
911 "\nError: fread return a number of element different from the expected.\n");
912 opj_image_destroy(image);
916 if (!fread(&a, 1, 1, f)) {
918 "\nError: fread return a number of element different from the expected.\n");
919 opj_image_destroy(image);
924 image->comps[0].data[index] = r;
925 image->comps[1].data[index] = g;
926 image->comps[2].data[index] = b;
927 image->comps[3].data[index] = a;
931 fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
938 int imagetotga(opj_image_t * image, const char *outfile)
940 int width, height, bpp, x, y;
941 OPJ_BOOL write_alpha;
943 int adjustR, adjustG, adjustB, fails;
944 unsigned int alpha_channel;
952 fdest = fopen(outfile, "wb");
954 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
958 for (i = 0; i < image->numcomps - 1; i++) {
959 if ((image->comps[0].dx != image->comps[i + 1].dx)
960 || (image->comps[0].dy != image->comps[i + 1].dy)
961 || (image->comps[0].prec != image->comps[i + 1].prec)) {
964 "Unable to create a tga file with such J2K image charateristics.");
969 width = (int)image->comps[0].w;
970 height = (int)image->comps[0].h;
972 /* Mono with alpha, or RGB with alpha. */
973 write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
975 /* Write TGA header */
976 bpp = write_alpha ? 32 : 24;
978 if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
982 alpha_channel = image->numcomps - 1;
984 scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
986 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
987 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
988 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
990 for (y = 0; y < height; y++) {
991 unsigned int index = (unsigned int)(y * width);
993 for (x = 0; x < width; x++, index++) {
994 r = (float)(image->comps[0].data[index] + adjustR);
996 if (image->numcomps > 2) {
997 g = (float)(image->comps[1].data[index] + adjustG);
998 b = (float)(image->comps[2].data[index] + adjustB);
1005 /* TGA format writes BGR ... */
1008 } else if (b < 0.) {
1011 value = (unsigned char)(b * scale);
1012 res = fwrite(&value, 1, 1, fdest);
1015 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1020 } else if (g < 0.) {
1023 value = (unsigned char)(g * scale);
1024 res = fwrite(&value, 1, 1, fdest);
1027 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1032 } else if (r < 0.) {
1035 value = (unsigned char)(r * scale);
1036 res = fwrite(&value, 1, 1, fdest);
1039 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1044 a = (float)(image->comps[alpha_channel].data[index]);
1047 } else if (a < 0.) {
1050 value = (unsigned char)(a * scale);
1051 res = fwrite(&value, 1, 1, fdest);
1054 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
1067 /* -->> -->> -->> -->>
1071 <<-- <<-- <<-- <<-- */
1074 static unsigned char readuchar(FILE * f)
1077 if (!fread(&c1, 1, 1, f)) {
1079 "\nError: fread return a number of element different from the expected.\n");
1085 static unsigned short readushort(FILE * f, int bigendian)
1087 unsigned char c1, c2;
1088 if (!fread(&c1, 1, 1, f)) {
1090 "\nError: fread return a number of element different from the expected.\n");
1093 if (!fread(&c2, 1, 1, f)) {
1095 "\nError: fread return a number of element different from the expected.\n");
1099 return (unsigned short)((c1 << 8) + c2);
1101 return (unsigned short)((c2 << 8) + c1);
1105 static unsigned int readuint(FILE * f, int bigendian)
1107 unsigned char c1, c2, c3, c4;
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");
1118 if (!fread(&c3, 1, 1, f)) {
1120 "\nError: fread return a number of element different from the expected.\n");
1123 if (!fread(&c4, 1, 1, f)) {
1125 "\nError: fread return a number of element different from the expected.\n");
1129 return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(
1132 return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(
1137 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
1141 int i, numcomps, max;
1142 OPJ_COLOR_SPACE color_space;
1143 opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
1144 opj_image_t * image = NULL;
1145 int adjustS, ushift, dshift, force8;
1147 char endian1, endian2, sign;
1152 opj_image_comp_t *comp = NULL;
1155 color_space = OPJ_CLRSPC_GRAY;
1157 memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));
1161 f = fopen(filename, "rb");
1163 fprintf(stderr, "Failed to open %s for reading !\n", filename);
1167 fseek(f, 0, SEEK_SET);
1168 if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
1169 &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
1172 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
1178 while (signtmp[i] != '\0') {
1179 if (signtmp[i] == '-') {
1186 if (endian1 == 'M' && endian2 == 'L') {
1188 } else if (endian2 == 'M' && endian1 == 'L') {
1192 fprintf(stderr, "Bad pgx header, please check input file\n");
1196 /* initialize image component */
1198 cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
1199 cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
1200 cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx +
1201 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx
1203 cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy +
1204 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy
1215 dshift = prec - ushift;
1216 if (cmptparm.sgnd) {
1217 adjustS = (1 << (prec - 1));
1224 ushift = dshift = force8 = adjustS = 0;
1227 cmptparm.prec = (OPJ_UINT32)prec;
1228 cmptparm.bpp = (OPJ_UINT32)prec;
1229 cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
1230 cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
1232 /* create the image */
1233 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
1238 /* set image offset and reference grid */
1239 image->x0 = cmptparm.x0;
1240 image->y0 = cmptparm.x0;
1241 image->x1 = cmptparm.w;
1242 image->y1 = cmptparm.h;
1244 /* set image data */
1246 comp = &image->comps[0];
1248 for (i = 0; i < w * h; i++) {
1251 v = readuchar(f) + adjustS;
1252 v = (v << ushift) + (v >> dshift);
1253 comp->data[i] = (unsigned char)v;
1261 if (comp->prec == 8) {
1265 v = (char) readuchar(f);
1267 } else if (comp->prec <= 16) {
1269 v = readushort(f, bigendian);
1271 v = (short) readushort(f, bigendian);
1275 v = (int)readuint(f, bigendian);
1277 v = (int) readuint(f, bigendian);
1286 comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
1291 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
1293 static INLINE int clamp(const int value, const int prec, const int sgnd)
1297 return CLAMP(value, -128, 127);
1298 } else if (prec <= 16) {
1299 return CLAMP(value, -32768, 32767);
1301 return CLAMP(value, -2147483647 - 1, 2147483647);
1305 return CLAMP(value, 0, 255);
1306 } else if (prec <= 16) {
1307 return CLAMP(value, 0, 65535);
1309 return value; /*CLAMP(value,0,4294967295);*/
1314 int imagetopgx(opj_image_t * image, const char *outfile)
1317 int i, j, fails = 1;
1318 unsigned int compno;
1321 for (compno = 0; compno < image->numcomps; compno++) {
1322 opj_image_comp_t *comp = &image->comps[compno];
1323 char bname[256]; /* buffer for name */
1324 char *name = bname; /* pointer */
1327 const size_t olen = strlen(outfile);
1328 const size_t dotpos = olen - 4;
1329 const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
1331 if (outfile[dotpos] != '.') {
1332 /* `pgx` was recognized but there is no dot at expected position */
1333 fprintf(stderr, "ERROR -> Impossible happen.");
1337 name = (char*)malloc(total + 1);
1339 fprintf(stderr, "imagetopgx: memory out\n");
1343 strncpy(name, outfile, dotpos);
1344 sprintf(name + dotpos, "_%u.pgx", compno);
1345 fdest = fopen(name, "wb");
1346 /* don't need name anymore */
1350 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
1357 w = (int)image->comps[compno].w;
1358 h = (int)image->comps[compno].h;
1360 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
1363 if (comp->prec <= 8) {
1365 } else if (comp->prec <= 16) {
1371 for (i = 0; i < w * h; i++) {
1372 /* FIXME: clamp func is being called within a loop */
1373 const int val = clamp(image->comps[compno].data[i],
1374 (int)comp->prec, (int)comp->sgnd);
1376 for (j = nbytes - 1; j >= 0; j--) {
1377 int v = (int)(val >> (j * 8));
1378 unsigned char byte = (unsigned char)v;
1379 res = fwrite(&byte, 1, 1, fdest);
1382 fprintf(stderr, "failed to write 1 byte for %s\n", name);
1405 /* -->> -->> -->> -->>
1409 <<-- <<-- <<-- <<-- */
1412 int width, height, maxval, depth, format;
1413 char rgb, rgba, gray, graya, bw;
1417 static char *skip_white(char *s)
1421 if (*s == '\n' || *s == '\r') {
1434 static char *skip_int(char *start, int *out_n)
1441 s = skip_white(start);
1455 *out_n = atoi(start);
1460 static char *skip_idf(char *start, char out_idf[256])
1465 s = skip_white(start);
1472 if (isalpha(*s) || *s == '_') {
1480 strncpy(out_idf, start, 255);
1485 static void read_pnm_header(FILE *reader, struct pnm_header *ph)
1487 int format, end, ttype;
1488 char idf[256], type[256];
1491 if (fgets(line, 250, reader) == NULL) {
1492 fprintf(stderr, "\nWARNING: fgets return a NULL value");
1496 if (line[0] != 'P') {
1497 fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
1500 format = atoi(line + 1);
1501 if (format < 1 || format > 7) {
1502 fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
1505 ph->format = format;
1508 while (fgets(line, 250, reader)) {
1519 s = skip_idf(s, idf);
1521 if (s == NULL || *s == 0) {
1525 if (strcmp(idf, "ENDHDR") == 0) {
1529 if (strcmp(idf, "WIDTH") == 0) {
1530 s = skip_int(s, &ph->width);
1531 if (s == NULL || *s == 0) {
1537 if (strcmp(idf, "HEIGHT") == 0) {
1538 s = skip_int(s, &ph->height);
1539 if (s == NULL || *s == 0) {
1545 if (strcmp(idf, "DEPTH") == 0) {
1546 s = skip_int(s, &ph->depth);
1547 if (s == NULL || *s == 0) {
1553 if (strcmp(idf, "MAXVAL") == 0) {
1554 s = skip_int(s, &ph->maxval);
1555 if (s == NULL || *s == 0) {
1561 if (strcmp(idf, "TUPLTYPE") == 0) {
1562 s = skip_idf(s, type);
1563 if (s == NULL || *s == 0) {
1567 if (strcmp(type, "BLACKANDWHITE") == 0) {
1572 if (strcmp(type, "GRAYSCALE") == 0) {
1577 if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
1582 if (strcmp(type, "RGB") == 0) {
1587 if (strcmp(type, "RGB_ALPHA") == 0) {
1592 fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
1595 fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
1597 } /* if(format == 7) */
1599 /* Here format is in range [1,6] */
1600 if (ph->width == 0) {
1601 s = skip_int(s, &ph->width);
1602 if ((s == NULL) || (*s == 0) || (ph->width < 1)) {
1607 if (ph->height == 0) {
1608 s = skip_int(s, &ph->height);
1609 if ((s == NULL) && allow_null) {
1612 if ((s == NULL) || (*s == 0) || (ph->height < 1)) {
1615 if (format == 1 || format == 4) {
1620 /* here, format is in P2, P3, P5, P6 */
1621 s = skip_int(s, &ph->maxval);
1622 if ((s == NULL) && allow_null) {
1625 if ((s == NULL) || (*s == 0)) {
1629 }/* while(fgets( ) */
1630 if (format == 2 || format == 3 || format > 4) {
1631 if (ph->maxval < 1 || ph->maxval > 65535) {
1635 if (ph->width < 1 || ph->height < 1) {
1641 fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
1644 if (ph->depth < 1 || ph->depth > 4) {
1653 if (format == 1 || format == 4) {
1659 static int has_prec(int val)
1709 opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
1711 int subsampling_dx = parameters->subsampling_dx;
1712 int subsampling_dy = parameters->subsampling_dy;
1715 int i, compno, numcomps, w, h, prec, format;
1716 OPJ_COLOR_SPACE color_space;
1717 opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */
1718 opj_image_t * image = NULL;
1719 struct pnm_header header_info;
1721 if ((fp = fopen(filename, "rb")) == NULL) {
1722 fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
1725 memset(&header_info, 0, sizeof(struct pnm_header));
1727 read_pnm_header(fp, &header_info);
1729 if (!header_info.ok) {
1734 format = header_info.format;
1737 case 1: /* ascii bitmap */
1738 case 4: /* raw bitmap */
1742 case 2: /* ascii greymap */
1743 case 5: /* raw greymap */
1747 case 3: /* ascii pixmap */
1748 case 6: /* raw pixmap */
1752 case 7: /* arbitrary map */
1753 numcomps = header_info.depth;
1761 color_space = OPJ_CLRSPC_GRAY; /* GRAY, GRAYA */
1763 color_space = OPJ_CLRSPC_SRGB; /* RGB, RGBA */
1766 prec = has_prec(header_info.maxval);
1772 w = header_info.width;
1773 h = header_info.height;
1774 subsampling_dx = parameters->subsampling_dx;
1775 subsampling_dy = parameters->subsampling_dy;
1777 memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
1779 for (i = 0; i < numcomps; i++) {
1780 cmptparm[i].prec = (OPJ_UINT32)prec;
1781 cmptparm[i].bpp = (OPJ_UINT32)prec;
1782 cmptparm[i].sgnd = 0;
1783 cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
1784 cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
1785 cmptparm[i].w = (OPJ_UINT32)w;
1786 cmptparm[i].h = (OPJ_UINT32)h;
1788 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
1795 /* set image offset and reference grid */
1796 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
1797 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
1798 image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx
1800 image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy
1803 if ((format == 2) || (format == 3)) { /* ascii pixmap */
1806 for (i = 0; i < w * h; i++) {
1807 for (compno = 0; compno < numcomps; compno++) {
1809 if (fscanf(fp, "%u", &index) != 1) {
1811 "\nWARNING: fscanf return a number of element different from the expected.\n");
1814 image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval;
1817 } else if ((format == 5)
1820 && (header_info.gray || header_info.graya
1821 || header_info.rgb || header_info.rgba))) { /* binary pixmap */
1822 unsigned char c0, c1, one;
1826 for (i = 0; i < w * h; i++) {
1827 for (compno = 0; compno < numcomps; compno++) {
1828 if (!fread(&c0, 1, 1, fp)) {
1830 "\nError: fread return a number of element different from the expected.\n");
1831 opj_image_destroy(image);
1836 image->comps[compno].data[i] = c0;
1838 if (!fread(&c1, 1, 1, fp)) {
1840 "\nError: fread return a number of element different from the expected.\n");
1843 image->comps[compno].data[i] = ((c0 << 8) | c1);
1847 } else if (format == 1) { /* ascii bitmap */
1848 for (i = 0; i < w * h; i++) {
1851 if (fscanf(fp, "%u", &index) != 1) {
1853 "\nWARNING: fscanf return a number of element different from the expected.\n");
1856 image->comps[0].data[i] = (index ? 0 : 255);
1858 } else if (format == 4) {
1863 for (y = 0; y < h; ++y) {
1867 for (x = 0; x < w; ++x) {
1870 uc = (unsigned char)getc(fp);
1872 image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
1877 } else if ((format == 7 && header_info.bw)) { /*MONO*/
1880 for (i = 0; i < w * h; ++i) {
1881 if (!fread(&uc, 1, 1, fp)) {
1883 "\nError: fread return a number of element different from the expected.\n");
1885 image->comps[0].data[i] = (uc & 1) ? 0 : 255;
1893 int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
1895 int *red, *green, *blue, *alpha;
1898 unsigned int compno, ncomp;
1899 int adjustR, adjustG, adjustB, adjustA;
1900 int fails, two, want_gray, has_alpha, triple;
1903 const char *tmp = outfile;
1908 if ((prec = (int)image->comps[0].prec) > 16) {
1909 fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
1910 "\n\t: refused.\n", __FILE__, __LINE__, prec);
1913 two = has_alpha = 0;
1915 ncomp = image->numcomps;
1921 want_gray = (*tmp == 'g' || *tmp == 'G');
1922 ncomp = image->numcomps;
1928 if ((force_split == 0) &&
1929 (ncomp == 2 /* GRAYA */
1930 || (ncomp > 2 /* RGB, RGBA */
1931 && image->comps[0].dx == image->comps[1].dx
1932 && image->comps[1].dx == image->comps[2].dx
1933 && image->comps[0].dy == image->comps[1].dy
1934 && image->comps[1].dy == image->comps[2].dy
1935 && image->comps[0].prec == image->comps[1].prec
1936 && image->comps[1].prec == image->comps[2].prec
1938 fdest = fopen(outfile, "wb");
1941 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
1945 triple = (ncomp > 2);
1946 wr = (int)image->comps[0].w;
1947 hr = (int)image->comps[0].h;
1948 max = (1 << prec) - 1;
1949 has_alpha = (ncomp == 4 || ncomp == 2);
1951 red = image->comps[0].data;
1954 green = image->comps[1].data;
1955 blue = image->comps[2].data;
1957 green = blue = NULL;
1961 const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
1963 fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n"
1964 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(),
1965 wr, hr, ncomp, max, tt);
1966 alpha = image->comps[ncomp - 1].data;
1967 adjustA = (image->comps[ncomp - 1].sgnd ?
1968 1 << (image->comps[ncomp - 1].prec - 1) : 0);
1970 fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
1971 opj_version(), wr, hr, max);
1974 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
1977 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
1978 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
1980 adjustG = adjustB = 0;
1983 for (i = 0; i < wr * hr; ++i) {
1994 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
1997 v = *green + adjustG;
2006 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2008 v = *blue + adjustB;
2017 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2022 v = *alpha + adjustA;
2031 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2045 fprintf(fdest, "%c", (unsigned char)v);
2054 fprintf(fdest, "%c", (unsigned char)v);
2062 fprintf(fdest, "%c", (unsigned char)v);
2072 fprintf(fdest, "%c", (unsigned char)v);
2082 if (image->numcomps > ncomp) {
2083 fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
2084 fprintf(stderr, " is written to the file\n");
2086 destname = (char*)malloc(strlen(outfile) + 8);
2087 if (destname == NULL) {
2088 fprintf(stderr, "imagetopnm: memory out\n");
2091 for (compno = 0; compno < ncomp; compno++) {
2093 /*sprintf(destname, "%d.%s", compno, outfile);*/
2094 const size_t olen = strlen(outfile);
2095 const size_t dotpos = olen - 4;
2097 strncpy(destname, outfile, dotpos);
2098 sprintf(destname + dotpos, "_%u.pgm", compno);
2100 sprintf(destname, "%s", outfile);
2103 fdest = fopen(destname, "wb");
2105 fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
2109 wr = (int)image->comps[compno].w;
2110 hr = (int)image->comps[compno].h;
2111 prec = (int)image->comps[compno].prec;
2112 max = (1 << prec) - 1;
2114 fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
2115 opj_version(), wr, hr, max);
2117 red = image->comps[compno].data;
2119 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
2122 for (i = 0; i < wr * hr; i++) {
2132 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2143 fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
2146 } else { /* prec <= 8 */
2147 for (i = 0; i < wr * hr; ++i) {
2156 fprintf(fdest, "%c", (unsigned char)v);
2166 /* -->> -->> -->> -->>
2170 <<-- <<-- <<-- <<-- */
2171 static opj_image_t* rawtoimage_common(const char *filename,
2172 opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian)
2174 int subsampling_dx = parameters->subsampling_dx;
2175 int subsampling_dy = parameters->subsampling_dy;
2178 int i, compno, numcomps, w, h;
2179 OPJ_COLOR_SPACE color_space;
2180 opj_image_cmptparm_t *cmptparm;
2181 opj_image_t * image = NULL;
2184 if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
2185 raw_cp->rawBitDepth)) == 0) {
2186 fprintf(stderr, "\nError: invalid raw image parameters\n");
2187 fprintf(stderr, "Please use the Format option -F:\n");
2189 "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
2191 "If subsampling is omitted, 1x1 is assumed for all components\n");
2193 "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
2194 fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
2195 fprintf(stderr, "Aborting.\n");
2199 f = fopen(filename, "rb");
2201 fprintf(stderr, "Failed to open %s for reading !!\n", filename);
2202 fprintf(stderr, "Aborting\n");
2205 numcomps = raw_cp->rawComp;
2207 /* FIXME ADE at this point, tcp_mct has not been properly set in calling function */
2208 if (numcomps == 1) {
2209 color_space = OPJ_CLRSPC_GRAY;
2210 } else if ((numcomps >= 3) && (parameters->tcp_mct == 0)) {
2211 color_space = OPJ_CLRSPC_SYCC;
2212 } else if ((numcomps >= 3) && (parameters->tcp_mct != 2)) {
2213 color_space = OPJ_CLRSPC_SRGB;
2215 color_space = OPJ_CLRSPC_UNKNOWN;
2217 w = raw_cp->rawWidth;
2218 h = raw_cp->rawHeight;
2219 cmptparm = (opj_image_cmptparm_t*) calloc((OPJ_UINT32)numcomps,
2220 sizeof(opj_image_cmptparm_t));
2222 fprintf(stderr, "Failed to allocate image components parameters !!\n");
2223 fprintf(stderr, "Aborting\n");
2227 /* initialize image components */
2228 for (i = 0; i < numcomps; i++) {
2229 cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
2230 cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
2231 cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
2232 cmptparm[i].dx = (OPJ_UINT32)(subsampling_dx * raw_cp->rawComps[i].dx);
2233 cmptparm[i].dy = (OPJ_UINT32)(subsampling_dy * raw_cp->rawComps[i].dy);
2234 cmptparm[i].w = (OPJ_UINT32)w;
2235 cmptparm[i].h = (OPJ_UINT32)h;
2237 /* create the image */
2238 image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
2244 /* set image offset and reference grid */
2245 image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
2246 image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
2247 image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) *
2248 (OPJ_UINT32)subsampling_dx + 1;
2249 image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) *
2250 (OPJ_UINT32)subsampling_dy + 1;
2252 if (raw_cp->rawBitDepth <= 8) {
2253 unsigned char value = 0;
2254 for (compno = 0; compno < numcomps; compno++) {
2255 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2256 raw_cp->rawComps[compno].dy);
2257 for (i = 0; i < nloop; i++) {
2258 if (!fread(&value, 1, 1, f)) {
2259 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2260 opj_image_destroy(image);
2264 image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value;
2267 } else if (raw_cp->rawBitDepth <= 16) {
2268 unsigned short value;
2269 for (compno = 0; compno < numcomps; compno++) {
2270 int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
2271 raw_cp->rawComps[compno].dy);
2272 for (i = 0; i < nloop; i++) {
2273 unsigned char temp1;
2274 unsigned char temp2;
2275 if (!fread(&temp1, 1, 1, f)) {
2276 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2277 opj_image_destroy(image);
2281 if (!fread(&temp2, 1, 1, f)) {
2282 fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
2283 opj_image_destroy(image);
2288 value = (unsigned short)((temp1 << 8) + temp2);
2290 value = (unsigned short)((temp2 << 8) + temp1);
2292 image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
2297 "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
2298 opj_image_destroy(image);
2303 if (fread(&ch, 1, 1, f)) {
2304 fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
2311 opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters,
2312 raw_cparameters_t *raw_cp)
2314 return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
2317 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters,
2318 raw_cparameters_t *raw_cp)
2320 return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
2323 static int imagetoraw_common(opj_image_t * image, const char *outfile,
2324 OPJ_BOOL big_endian)
2326 FILE *rawFile = NULL;
2328 unsigned int compno;
2330 int line, row, curr, mask;
2335 if ((image->numcomps * image->x1 * image->y1) == 0) {
2336 fprintf(stderr, "\nError: invalid raw image parameters\n");
2340 rawFile = fopen(outfile, "wb");
2342 fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
2347 fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps);
2349 for (compno = 0; compno < image->numcomps; compno++) {
2350 fprintf(stdout, "Component %u characteristics: %dx%dx%d %s\n", compno,
2351 image->comps[compno].w,
2352 image->comps[compno].h, image->comps[compno].prec,
2353 image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
2355 w = (int)image->comps[compno].w;
2356 h = (int)image->comps[compno].h;
2358 if (image->comps[compno].prec <= 8) {
2359 if (image->comps[compno].sgnd == 1) {
2360 mask = (1 << image->comps[compno].prec) - 1;
2361 ptr = image->comps[compno].data;
2362 for (line = 0; line < h; line++) {
2363 for (row = 0; row < w; row++) {
2367 } else if (curr < -128) {
2370 uc = (unsigned char)(curr & mask);
2371 res = fwrite(&uc, 1, 1, rawFile);
2373 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2379 } else if (image->comps[compno].sgnd == 0) {
2380 mask = (1 << image->comps[compno].prec) - 1;
2381 ptr = image->comps[compno].data;
2382 for (line = 0; line < h; line++) {
2383 for (row = 0; row < w; row++) {
2387 } else if (curr < 0) {
2390 uc = (unsigned char)(curr & mask);
2391 res = fwrite(&uc, 1, 1, rawFile);
2393 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
2400 } else if (image->comps[compno].prec <= 16) {
2401 if (image->comps[compno].sgnd == 1) {
2404 signed char vals[2];
2406 mask = (1 << image->comps[compno].prec) - 1;
2407 ptr = image->comps[compno].data;
2408 for (line = 0; line < h; line++) {
2409 for (row = 0; row < w; row++) {
2413 } else if (curr < -32768) {
2416 uc16.val = (signed short)(curr & mask);
2417 res = fwrite(uc16.vals, 1, 2, rawFile);
2419 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2425 } else if (image->comps[compno].sgnd == 0) {
2428 unsigned char vals[2];
2430 mask = (1 << image->comps[compno].prec) - 1;
2431 ptr = image->comps[compno].data;
2432 for (line = 0; line < h; line++) {
2433 for (row = 0; row < w; row++) {
2437 } else if (curr < 0) {
2440 uc16.val = (unsigned short)(curr & mask);
2441 res = fwrite(uc16.vals, 1, 2, rawFile);
2443 fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
2450 } else if (image->comps[compno].prec <= 32) {
2451 fprintf(stderr, "More than 16 bits per component no handled yet\n");
2454 fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
2464 int imagetoraw(opj_image_t * image, const char *outfile)
2466 return imagetoraw_common(image, outfile, OPJ_TRUE);
2469 int imagetorawl(opj_image_t * image, const char *outfile)
2471 return imagetoraw_common(image, outfile, OPJ_FALSE);