fc0d6d1d4f9cd114c7875d71d1bed004a5864ea9
[openjpeg.git] / tests / test_tile_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 <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <math.h>
35
36 #ifdef _WIN32
37 #include <malloc.h>
38 #else
39 #include <stdlib.h>
40 #endif
41
42 #include "opj_config.h"
43 #include "openjpeg.h"
44 #include "stdlib.h"
45
46 #define DA_X0 0
47 #define DA_Y0 0
48 #define DA_X1 1000
49 #define DA_Y1 1000
50 #define INPUT_FILE                      "test.j2k"
51
52 /* -------------------------------------------------------------------------- */
53
54 /**
55 sample error callback expecting a FILE* client object
56 */
57 void error_callback_file(const char *msg, void *client_data) {
58         FILE *stream = (FILE*)client_data;
59         fprintf(stream, "[ERROR] %s", msg);
60 }
61 /**
62 sample warning callback expecting a FILE* client object
63 */
64 void warning_callback_file(const char *msg, void *client_data) {
65         FILE *stream = (FILE*)client_data;
66         fprintf(stream, "[WARNING] %s", msg);
67 }
68 /**
69 sample error debug callback expecting no client object
70 */
71 void error_callback(const char *msg, void *client_data) {
72         (void)client_data;
73         fprintf(stdout, "[ERROR] %s", msg);
74 }
75 /**
76 sample warning debug callback expecting no client object
77 */
78 void warning_callback(const char *msg, void *client_data) {
79         (void)client_data;
80         fprintf(stdout, "[WARNING] %s", msg);
81 }
82 /**
83 sample debug callback expecting no client object
84 */
85 void info_callback(const char *msg, void *client_data) {
86         (void)client_data;
87         fprintf(stdout, "[INFO] %s", msg);
88 }
89
90 /* -------------------------------------------------------------------------- */
91
92 int main ()
93 {
94         opj_dparameters_t l_param;
95         opj_codec_t * l_codec;
96         opj_image_t * l_image;
97         FILE * l_file;
98         opj_stream_t * l_stream;
99         OPJ_UINT32 l_data_size;
100         OPJ_UINT32 l_max_data_size = 1000;
101         OPJ_UINT32 l_tile_index;
102         OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
103         opj_bool l_go_on = OPJ_TRUE;
104         OPJ_INT32 l_tile_x0,l_tile_y0;
105         OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
106         OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
107         
108         PROFINIT();
109
110
111         if
112                 (! l_data)
113         {
114                 return 1;
115         }
116         opj_set_default_decoder_parameters(&l_param);
117
118         /** you may here add custom decoding parameters */
119         /* do not use layer decoding limitations */
120         l_param.cp_layer = 0;
121
122         /* do not use resolutions reductions */
123         l_param.cp_reduce = 0;
124
125         /* to decode only a part of the image data */
126         //opj_restrict_decoding(&l_param,0,0,1000,1000);
127         
128         l_codec = opj_create_decompress_v2(CODEC_J2K);
129         if
130                 (! l_codec)
131         {
132                 free(l_data);
133                 return 1;
134         }
135
136         /* catch events using our callbacks and give a local context */         
137         opj_set_info_handler(l_codec, info_callback,00);
138         opj_set_warning_handler(l_codec, warning_callback,00);
139         opj_set_error_handler(l_codec, error_callback,00);
140   opj_event_mgr_t l_event_mgr;
141         
142         if
143                 (! opj_setup_decoder_v2(l_codec,&l_param))
144         {
145                 free(l_data);
146                 opj_destroy_codec(l_codec);
147                 return 1;
148         }
149         
150         l_file = fopen(INPUT_FILE,"rb");
151         if
152                 (! l_file)
153         {
154                 fprintf(stdout, "Error opening input file\n");
155                 free(l_data);
156                 opj_destroy_codec(l_codec);
157                 return 1;
158         }
159
160         l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);
161
162         if
163                 (! opj_read_header(l_codec,
164                                                         &l_image,
165                                                         &l_tile_x0,
166                                                         &l_tile_y0,
167                                                         &l_tile_width,
168                                                         &l_tile_height,
169                                                         &l_nb_tiles_x,
170                                                         &l_nb_tiles_y,
171                                                         l_stream))
172         {
173                 free(l_data);
174                 opj_stream_destroy(l_stream);
175                 fclose(l_file);
176                 opj_destroy_codec(l_codec);
177                 return 1;
178         }
179         printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
180         opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1);
181         while
182                 (l_go_on)
183         {
184                 if
185                         (! opj_read_tile_header(
186                                                 l_codec,
187                                                 &l_tile_index,
188                                                 &l_data_size,
189                                                 &l_current_tile_x0,
190                                                 &l_current_tile_y0,
191                                                 &l_current_tile_x1,
192                                                 &l_current_tile_y1,
193                                                 &l_nb_comps,
194                                                 &l_go_on,
195                                                 l_stream))
196                 {
197                         free(l_data);
198                         opj_stream_destroy(l_stream);
199                         fclose(l_file);
200                         opj_destroy_codec(l_codec);
201                         opj_image_destroy(l_image);
202                         return 1;
203                 }
204                 if
205                         (l_go_on)
206                 {
207                         if
208                                 (l_data_size > l_max_data_size)
209                         {
210                                 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
211                                 if
212                                         (! l_data)
213                                 {
214                                         opj_stream_destroy(l_stream);
215                                         fclose(l_file);
216                                         opj_destroy_codec(l_codec);
217                                         opj_image_destroy(l_image);
218                                         return 1;
219                                 }
220                                 l_max_data_size = l_data_size;
221                         }
222
223                         if
224                                 (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
225                         {
226                                 free(l_data);
227                                 opj_stream_destroy(l_stream);
228                                 fclose(l_file);
229                                 opj_destroy_codec(l_codec);
230                                 opj_image_destroy(l_image);
231                                 return 1;
232                         }
233                         /** now should inspect image to know the reduction factor and then how to behave with data */
234                 }
235         }
236         if
237                 (! opj_end_decompress(l_codec,l_stream))
238         {
239                 free(l_data);
240                 opj_stream_destroy(l_stream);
241                 fclose(l_file);
242                 opj_destroy_codec(l_codec);
243                 opj_image_destroy(l_image);
244                 return 1;
245         }
246         free(l_data);
247         opj_stream_destroy(l_stream);
248         fclose(l_file);
249         opj_destroy_codec(l_codec);
250         opj_image_destroy(l_image);
251
252         // Print profiling
253         PROFPRINT();
254
255         return 0;
256 }