minor bugs fixed (see issue 31 on googlecode - Thanks to winfried)
[openjpeg.git] / test_V2_tile_handling / test_decoder.c
1 /*
2  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 #define USE_OPJ_DEPRECATED
27 /* set this macro to enable profiling for the given test */
28 /* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */
29 //#define _PROFILE
30
31 #include "openjpeg.h"
32 #include "stdlib.h"
33 #ifdef WIN32
34 #include <malloc.h>
35 #else
36 #include <stdlib.h>
37 #endif
38 #define DA_X0 0
39 #define DA_Y0 0
40 #define DA_X1 1000
41 #define DA_Y1 1000
42 #define INPUT_FILE                      "test.j2k"
43
44 /* -------------------------------------------------------------------------- */
45
46 /**
47 sample error callback expecting a FILE* client object
48 */
49 void error_callback_file(const char *msg, void *client_data) {
50         FILE *stream = (FILE*)client_data;
51         fprintf(stream, "[ERROR] %s", msg);
52 }
53 /**
54 sample warning callback expecting a FILE* client object
55 */
56 void warning_callback_file(const char *msg, void *client_data) {
57         FILE *stream = (FILE*)client_data;
58         fprintf(stream, "[WARNING] %s", msg);
59 }
60 /**
61 sample error debug callback expecting no client object
62 */
63 void error_callback(const char *msg, void *client_data) {
64         (void)client_data;
65         fprintf(stdout, "[ERROR] %s", msg);
66 }
67 /**
68 sample warning debug callback expecting no client object
69 */
70 void warning_callback(const char *msg, void *client_data) {
71         (void)client_data;
72         fprintf(stdout, "[WARNING] %s", msg);
73 }
74 /**
75 sample debug callback expecting no client object
76 */
77 void info_callback(const char *msg, void *client_data) {
78         (void)client_data;
79         fprintf(stdout, "[INFO] %s", msg);
80 }
81
82 /* -------------------------------------------------------------------------- */
83
84 int main ()
85 {
86         opj_dparameters_t l_param;
87         opj_codec_t * l_codec;
88         opj_image_t * l_image;
89         FILE * l_file;
90         opj_stream_t * l_stream;
91         OPJ_UINT32 l_data_size;
92         OPJ_UINT32 l_max_data_size = 1000;
93         OPJ_UINT32 l_tile_index;
94         OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
95         bool l_go_on = true;
96         OPJ_INT32 l_tile_x0,l_tile_y0;
97         OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
98         OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
99         
100         PROFINIT();
101
102
103         if
104                 (! l_data)
105         {
106                 return 1;
107         }
108         opj_set_default_decoder_parameters(&l_param);
109
110         /** you may here add custom decoding parameters */
111         /* do not use layer decoding limitations */
112         l_param.cp_layer = 0;
113
114         /* do not use resolutions reductions */
115         l_param.cp_reduce = 0;
116
117         /* to decode only a part of the image data */
118         //opj_restrict_decoding(&l_param,0,0,1000,1000);
119         
120         l_codec = opj_create_decompress(CODEC_J2K);
121         if
122                 (! l_codec)
123         {
124                 free(l_data);
125                 return 1;
126         }
127
128         /* catch events using our callbacks and give a local context */         
129         opj_set_info_handler(l_codec, info_callback,00);
130         opj_set_warning_handler(l_codec, warning_callback,00);
131         opj_set_error_handler(l_codec, error_callback,00);
132         
133         if
134                 (! opj_setup_decoder(l_codec,&l_param))
135         {
136                 free(l_data);
137                 opj_destroy_codec(l_codec);
138                 return 1;
139         }
140         
141         l_file = fopen(INPUT_FILE,"rb");
142         if
143                 (! l_file)
144         {
145                 fprintf(stdout, "Error opening input file\n");
146                 free(l_data);
147                 opj_destroy_codec(l_codec);
148                 return 1;
149         }
150
151         l_stream = opj_stream_create_default_file_stream(l_file,true);
152
153         if
154                 (! opj_read_header(l_codec,
155                                                         &l_image,
156                                                         &l_tile_x0,
157                                                         &l_tile_y0,
158                                                         &l_tile_width,
159                                                         &l_tile_height,
160                                                         &l_nb_tiles_x,
161                                                         &l_nb_tiles_y,
162                                                         l_stream))
163         {
164                 free(l_data);
165                 opj_stream_destroy(l_stream);
166                 fclose(l_file);
167                 opj_destroy_codec(l_codec);
168                 return 1;
169         }
170         printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
171         opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1);
172         while
173                 (l_go_on)
174         {
175                 if
176                         (! opj_read_tile_header(
177                                                 l_codec,
178                                                 &l_tile_index,
179                                                 &l_data_size,
180                                                 &l_current_tile_x0,
181                                                 &l_current_tile_y0,
182                                                 &l_current_tile_x1,
183                                                 &l_current_tile_y1,
184                                                 &l_nb_comps,
185                                                 &l_go_on,
186                                                 l_stream))
187                 {
188                         free(l_data);
189                         opj_stream_destroy(l_stream);
190                         fclose(l_file);
191                         opj_destroy_codec(l_codec);
192                         opj_image_destroy(l_image);
193                         return 1;
194                 }
195                 if
196                         (l_go_on)
197                 {
198                         if
199                                 (l_data_size > l_max_data_size)
200                         {
201                                 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
202                                 if
203                                         (! l_data)
204                                 {
205                                         opj_stream_destroy(l_stream);
206                                         fclose(l_file);
207                                         opj_destroy_codec(l_codec);
208                                         opj_image_destroy(l_image);
209                                         return 1;
210                                 }
211                                 l_max_data_size = l_data_size;
212                         }
213
214                         if
215                                 (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
216                         {
217                                 free(l_data);
218                                 opj_stream_destroy(l_stream);
219                                 fclose(l_file);
220                                 opj_destroy_codec(l_codec);
221                                 opj_image_destroy(l_image);
222                                 return 1;
223                         }
224                         /** now should inspect image to know the reduction factor and then how to behave with data */
225                 }
226         }
227         if
228                 (! opj_end_decompress(l_codec,l_stream))
229         {
230                 free(l_data);
231                 opj_stream_destroy(l_stream);
232                 fclose(l_file);
233                 opj_destroy_codec(l_codec);
234                 opj_image_destroy(l_image);
235                 return 1;
236         }
237         free(l_data);
238         opj_stream_destroy(l_stream);
239         fclose(l_file);
240         opj_destroy_codec(l_codec);
241         opj_image_destroy(l_image);
242
243         // Print profiling
244         PROFPRINT();
245
246         return 0;
247 }