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