[trunk] avoid memory leak in j2k_random_tile_access.c
[openjpeg.git] / tests / j2k_random_tile_access.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "opj_config.h"
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <math.h>
38
39 #ifdef _WIN32
40 #include <windows.h>
41 #else
42 #include <strings.h>
43 #define _stricmp strcasecmp
44 #define _strnicmp strncasecmp
45 #endif /* _WIN32 */
46
47 #include "openjpeg.h"
48 #include "format_defs.h"
49
50 /* -------------------------------------------------------------------------- */
51 static int get_file_format(const char *filename) {
52         unsigned int i;
53         static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
54         static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
55         char * ext = strrchr(filename, '.');
56         if (ext == NULL)
57                 return -1;
58         ext++;
59         if(ext) {
60                 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
61                         if(_strnicmp(ext, extension[i], 3) == 0) {
62                                 return format[i];
63                         }
64                 }
65         }
66
67         return -1;
68 }
69
70 /* -------------------------------------------------------------------------- */
71
72 /**
73 sample error callback expecting a FILE* client object
74 */
75 static void error_callback(const char *msg, void *client_data) {
76         (void)client_data;
77         fprintf(stdout, "[ERROR] %s", msg);
78 }
79 /**
80 sample warning callback expecting a FILE* client object
81 */
82 static void warning_callback(const char *msg, void *client_data) {
83         (void)client_data;
84         fprintf(stdout, "[WARNING] %s", msg);
85 }
86 /**
87 sample debug callback expecting no client object
88 */
89 static void info_callback(const char *msg, void *client_data) {
90         (void)client_data;
91         fprintf(stdout, "[INFO] %s", msg);
92 }
93
94
95 /* -------------------------------------------------------------------------- */
96 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
97 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
98 /* position 45: "\xff\x52" */
99 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
100
101 static int infile_format(const char *fname)
102 {
103         FILE *reader;
104         const char *s, *magic_s;
105         int ext_format, magic_format;
106         unsigned char buf[12];
107         OPJ_SIZE_T l_nb_read;
108
109         reader = fopen(fname, "rb");
110
111         if (reader == NULL)
112                 return -1;
113
114         memset(buf, 0, 12);
115         l_nb_read = fread(buf, 1, 12, reader);
116         fclose(reader);
117         if (l_nb_read != 12)
118                 return -1;
119
120
121
122         ext_format = get_file_format(fname);
123
124         if (ext_format == JPT_CFMT)
125                 return JPT_CFMT;
126
127         if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
128                 magic_format = JP2_CFMT;
129                 magic_s = ".jp2";
130         }
131         else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
132                 magic_format = J2K_CFMT;
133                 magic_s = ".j2k or .jpc or .j2c";
134         }
135         else
136                 return -1;
137
138         if (magic_format == ext_format)
139                 return ext_format;
140
141         s = fname + strlen(fname) - 4;
142
143         fputs("\n===========================================\n", stderr);
144         fprintf(stderr, "The extension of this file is incorrect.\n"
145                                         "FOUND %s. SHOULD BE %s\n", s, magic_s);
146         fputs("===========================================\n", stderr);
147
148         return magic_format;
149 }
150
151 /* -------------------------------------------------------------------------- */
152 /**
153  * J2K_RANDOM_TILE_ACCESS MAIN
154  */
155 /* -------------------------------------------------------------------------- */
156 int main(int argc, char **argv)
157 {
158         FILE *fsrc = NULL;
159
160   OPJ_UINT32 index;
161         opj_dparameters_t parameters;                   /* decompression parameters */
162         opj_image_t* image = NULL;
163         opj_stream_t *l_stream = NULL;                          /* Stream */
164         opj_codec_t* l_codec = NULL;                            /* Handle to a decompressor */
165         opj_codestream_info_v2_t* cstr_info = NULL;
166
167         /* Index of corner tiles */
168         OPJ_UINT32 tile_ul = 0;
169         OPJ_UINT32 tile_ur = 0;
170         OPJ_UINT32 tile_lr = 0;
171         OPJ_UINT32 tile_ll = 0;
172
173         if (argc != 2) {
174                 fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
175                 return EXIT_FAILURE;
176         }
177
178         /* Set decoding parameters to default values */
179         opj_set_default_decoder_parameters(&parameters);
180
181         strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);
182
183         /* read the input file */
184         /* ------------------- */
185         fsrc = fopen(parameters.infile, "rb");
186         if (!fsrc) {
187                 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
188                 return EXIT_FAILURE;
189         }
190
191         /* decode the JPEG2000 stream */
192         /* -------------------------- */
193         parameters.decod_format = infile_format(parameters.infile);
194
195         switch(parameters.decod_format) {
196                 case J2K_CFMT:  /* JPEG-2000 codestream */
197                 {
198                         /* Get a decoder handle */
199                         l_codec = opj_create_decompress(OPJ_CODEC_J2K);
200                         break;
201                 }
202                 case JP2_CFMT:  /* JPEG 2000 compressed image data */
203                 {
204                         /* Get a decoder handle */
205                         l_codec = opj_create_decompress(OPJ_CODEC_JP2);
206                         break;
207                 }
208                 case JPT_CFMT:  /* JPEG 2000, JPIP */
209                 {
210                         /* Get a decoder handle */
211                         l_codec = opj_create_decompress(OPJ_CODEC_JPT);
212                         break;
213                 }
214                 default:
215                         fprintf(stderr,
216                                 "Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
217                                 parameters.infile);
218                         return EXIT_FAILURE;
219         }
220
221         /* catch events using our callbacks and give a local context */         
222         opj_set_info_handler(l_codec, info_callback,00);
223         opj_set_warning_handler(l_codec, warning_callback,00);
224         opj_set_error_handler(l_codec, error_callback,00);
225
226         l_stream = opj_stream_create_default_file_stream(fsrc,1);
227         if (!l_stream){
228                 fclose(fsrc);
229                 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
230                 return EXIT_FAILURE;
231         }
232
233         /* Setup the decoder decoding parameters using user parameters */
234         if ( !opj_setup_decoder(l_codec, &parameters) ){
235                 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
236                 opj_stream_destroy(l_stream);
237                 fclose(fsrc);
238                 opj_destroy_codec(l_codec);
239                 return EXIT_FAILURE;
240         }
241
242         /* Read the main header of the codestream and if necessary the JP2 boxes*/
243         if(! opj_read_header(l_stream, l_codec, &image)){
244                 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
245                 opj_stream_destroy(l_stream);
246                 fclose(fsrc);
247                 opj_destroy_codec(l_codec);
248                 opj_image_destroy(image);
249                 return EXIT_FAILURE;
250         }
251
252         /* Extract some info from the code stream */
253         cstr_info = opj_get_cstr_info(l_codec);
254
255         fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);
256
257         tile_ul = 0;
258         tile_ur = cstr_info->tw - 1;
259         tile_lr = cstr_info->tw * cstr_info->th - 1;
260         tile_ll = tile_lr - cstr_info->tw;
261
262 #define TEST_TILE( tile_index ) \
263         fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
264         if(!opj_get_decoded_tile(l_codec, l_stream, image, tile_index )){ \
265                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
266                 opj_stream_destroy(l_stream); \
267                 opj_destroy_cstr_info(&cstr_info); \
268                 opj_destroy_codec(l_codec); \
269                 opj_image_destroy(image); \
270                 fclose(fsrc); \
271                 return EXIT_FAILURE; \
272         } \
273   for(index = 0; index < image->numcomps; ++index) { \
274     if( image->comps[index].data == NULL ){ \
275         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
276                 opj_stream_destroy(l_stream); \
277                 opj_destroy_cstr_info(&cstr_info); \
278                 opj_destroy_codec(l_codec); \
279                 opj_image_destroy(image); \
280                 fclose(fsrc); \
281         return EXIT_FAILURE; \
282         } \
283   } \
284         fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);
285
286         TEST_TILE(tile_ul)
287         TEST_TILE(tile_lr)
288         TEST_TILE(tile_ul)
289         TEST_TILE(tile_ll)
290         TEST_TILE(tile_ur)
291         TEST_TILE(tile_lr)
292
293         /* Close the byte stream */
294         opj_stream_destroy(l_stream);
295
296         /* Destroy code stream info */
297         opj_destroy_cstr_info(&cstr_info);
298
299         /* Free remaining structures */
300         opj_destroy_codec(l_codec);
301
302         /* Free image data structure */
303         opj_image_destroy(image);
304
305         /* Close the input file */
306         fclose(fsrc);
307
308         return EXIT_SUCCESS;
309 }
310 /*end main*/
311
312
313
314