Merge pull request #1244 from rouault/fix_pi_warnings
[openjpeg.git] / src / lib / openjpip / jp2k_decoder.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <limits.h>
36 #include "jp2k_decoder.h"
37 #include "openjpeg.h"
38
39
40 static void error_callback(const char *msg, void *client_data);
41 static void warning_callback(const char *msg, void *client_data);
42 static void info_callback(const char *msg, void *client_data);
43
44 static Byte_t * imagetopnm(opj_image_t *image, ihdrbox_param_t **ihdrbox);
45
46 Byte_t * j2k_to_pnm(const char *fn, ihdrbox_param_t **ihdrbox)
47 {
48     Byte_t *pnmstream = NULL;
49     opj_dparameters_t parameters; /* decompression parameters */
50     opj_image_t *image = NULL;
51     opj_codec_t *l_codec = NULL;  /* handle to a decompressor */
52     opj_stream_t *l_stream = NULL;
53
54     /* set decoding parameters to default values */
55     opj_set_default_decoder_parameters(&parameters);
56
57     /* set a byte stream */
58     l_stream = opj_stream_create_default_file_stream(fn, OPJ_TRUE);
59     if (!l_stream) {
60         fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
61         return NULL;
62     }
63
64     /* decode the code-stream */
65     /* ---------------------- */
66
67     /* JPEG-2000 codestream */
68     /* get a decoder handle */
69     l_codec = opj_create_decompress(OPJ_CODEC_J2K);
70
71     /* catch events using our callbacks and give a local context */
72     opj_set_info_handler(l_codec, info_callback, 00);
73     opj_set_warning_handler(l_codec, warning_callback, 00);
74     opj_set_error_handler(l_codec, error_callback, 00);
75
76     /* setup the decoder decoding parameters using user parameters */
77     if (!opj_setup_decoder(l_codec, &parameters)) {
78         fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
79         opj_stream_destroy(l_stream);
80         opj_destroy_codec(l_codec);
81         return NULL;
82     }
83
84     /* Read the main header of the codestream and if necessary the JP2 boxes*/
85     if (! opj_read_header(l_stream, l_codec, &image)) {
86         fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
87         opj_stream_destroy(l_stream);
88         opj_destroy_codec(l_codec);
89         opj_image_destroy(image);
90         return NULL;
91     }
92
93 #ifdef TODO /*decode area could be set from j2k_to_pnm call, modify the protocol between JPIP viewer and opj_dec_server*/
94     if (! opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0,
95                               parameters.DA_x1, parameters.DA_y1)) {
96         fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n");
97         opj_stream_destroy(l_stream);
98         opj_destroy_codec(l_codec);
99         opj_image_destroy(image);
100         return NULL;
101     }
102 #endif /*TODO*/
103
104     /* Get the decoded image */
105     if (!(opj_decode(l_codec, l_stream, image) &&
106             opj_end_decompress(l_codec, l_stream))) {
107         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
108         opj_stream_destroy(l_stream);
109         opj_destroy_codec(l_codec);
110         opj_image_destroy(image);
111         return NULL;
112     }
113
114     fprintf(stderr, "image is decoded!\n");
115
116     /* close the byte stream */
117     opj_stream_destroy(l_stream);
118
119     /* create output image */
120     /* ------------------- */
121     if ((pnmstream = imagetopnm(image, ihdrbox)) == NULL) {
122         fprintf(stderr, "PNM image not generated\n");
123     }
124
125     /* free remaining structures */
126     if (l_codec) {
127         opj_destroy_codec(l_codec);
128     }
129
130     /* free image data structure */
131     opj_image_destroy(image);
132
133     return pnmstream;
134 }
135
136
137 /**
138    sample error callback expecting a FILE* client object
139 */
140 static void error_callback(const char *msg, void *client_data)
141 {
142     FILE *stream = (FILE*)client_data;
143     fprintf(stream, "[ERROR] %s", msg);
144 }
145 /**
146    sample warning callback expecting a FILE* client object
147 */
148 static void warning_callback(const char *msg, void *client_data)
149 {
150     FILE *stream = (FILE*)client_data;
151     fprintf(stream, "[WARNING] %s", msg);
152 }
153 /**
154    sample debug callback expecting no client object
155 */
156 static void info_callback(const char *msg, void *client_data)
157 {
158     (void)client_data;
159     (void)msg;
160     /*  fprintf(stdout, "[INFO] %s", msg); */
161 }
162
163
164 static Byte_t * imagetopnm(opj_image_t *image, ihdrbox_param_t **ihdrbox)
165 {
166     OPJ_UINT32 adjustR, adjustG = 0, adjustB = 0;
167     OPJ_SIZE_T datasize;
168     Byte_t *pix = NULL, *ptr = NULL;
169     OPJ_UINT32 i;
170
171     if (*ihdrbox) {
172         if ((*ihdrbox)->nc != image->numcomps) {
173             fprintf(stderr,
174                     "Exception: num of components not identical, codestream: %d, ihdrbox: %d\n",
175                     image->numcomps, (*ihdrbox)->nc);
176         }
177
178         if ((*ihdrbox)->width != image->comps[0].w) {
179             (*ihdrbox)->width = image->comps[0].w;
180         }
181
182         if ((*ihdrbox)->height != image->comps[0].h) {
183             (*ihdrbox)->height = image->comps[0].h;
184         }
185
186         if ((*ihdrbox)->bpc != image->comps[0].prec) {
187             fprintf(stderr,
188                     "Exception: bits per component not identical, codestream: %d, ihdrbox: %d\n",
189                     image->comps[0].prec, (*ihdrbox)->bpc);
190         }
191     } else {
192         *ihdrbox = (ihdrbox_param_t *)malloc(sizeof(ihdrbox_param_t));
193         (*ihdrbox)->width  = image->comps[0].w;
194         (*ihdrbox)->height = image->comps[0].h;
195         assert(image->comps[0].prec < 256);
196         (*ihdrbox)->bpc    = (Byte_t)image->comps[0].prec;
197         assert(image->numcomps < USHRT_MAX);
198         (*ihdrbox)->nc     = (Byte2_t)image->numcomps;
199     }
200
201     datasize = (image->numcomps) * (image->comps[0].w) * (image->comps[0].h);
202
203     if (image->comps[0].prec > 8) {
204         adjustR = image->comps[0].prec - 8;
205         printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n",
206                image->comps[0].prec);
207     } else {
208         adjustR = 0;
209     }
210
211     if (image->numcomps == 3) {
212         if (image->comps[1].prec > 8) {
213             adjustG = image->comps[1].prec - 8;
214             printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n",
215                    image->comps[1].prec);
216         } else {
217             adjustG = 0;
218         }
219
220         if (image->comps[2].prec > 8) {
221             adjustB = image->comps[2].prec - 8;
222             printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n",
223                    image->comps[2].prec);
224         } else {
225             adjustB = 0;
226         }
227     }
228
229     pix = (Byte_t *)malloc(datasize);
230     ptr = pix;
231
232     for (i = 0; i < image->comps[0].w * image->comps[0].h; i++) {
233         int r, g, b;
234         r = image->comps[0].data[i];
235         r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
236
237         /*    if( adjustR > 0) */
238         *(ptr++) = (Byte_t)((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
239
240         if (image->numcomps == 3) {
241             g = image->comps[1].data[i];
242             g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
243             *(ptr++) = (Byte_t)((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
244
245             b = image->comps[2].data[i];
246             b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
247             *(ptr++) = (Byte_t)((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
248         }
249     }
250
251     return pix;
252 }