Fix some typos (found by `codespell` and `typos`)
[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     OPJ_UINT32 index;
154         opj_dparameters_t parameters;                   /* decompression parameters */
155         opj_image_t* image = NULL;
156         opj_stream_t *l_stream = NULL;                          /* Stream */
157         opj_codec_t* l_codec = NULL;                            /* Handle to a decompressor */
158         opj_codestream_info_v2_t* cstr_info = NULL;
159
160         /* Index of corner tiles */
161         OPJ_UINT32 tile_ul = 0;
162         OPJ_UINT32 tile_ur = 0;
163         OPJ_UINT32 tile_lr = 0;
164         OPJ_UINT32 tile_ll = 0;
165
166         if (argc != 2) {
167                 fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
168                 return EXIT_FAILURE;
169         }
170
171         /* Set decoding parameters to default values */
172         opj_set_default_decoder_parameters(&parameters);
173
174         strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);
175
176
177         /* decode the JPEG2000 stream */
178         /* -------------------------- */
179         parameters.decod_format = infile_format(parameters.infile);
180
181         switch(parameters.decod_format) {
182                 case J2K_CFMT:  /* JPEG-2000 codestream */
183                 {
184                         /* Get a decoder handle */
185                         l_codec = opj_create_decompress(OPJ_CODEC_J2K);
186                         break;
187                 }
188                 case JP2_CFMT:  /* JPEG 2000 compressed image data */
189                 {
190                         /* Get a decoder handle */
191                         l_codec = opj_create_decompress(OPJ_CODEC_JP2);
192                         break;
193                 }
194                 case JPT_CFMT:  /* JPEG 2000, JPIP */
195                 {
196                         /* Get a decoder handle */
197                         l_codec = opj_create_decompress(OPJ_CODEC_JPT);
198                         break;
199                 }
200                 default:
201                         fprintf(stderr,
202                                 "Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
203                                 parameters.infile);
204                         return EXIT_FAILURE;
205         }
206
207         /* catch events using our callbacks and give a local context */         
208         opj_set_info_handler(l_codec, info_callback,00);
209         opj_set_warning_handler(l_codec, warning_callback,00);
210         opj_set_error_handler(l_codec, error_callback,00);
211
212     l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
213         if (!l_stream){
214         fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
215                 return EXIT_FAILURE;
216         }
217
218         /* Setup the decoder decoding parameters using user parameters */
219         if ( !opj_setup_decoder(l_codec, &parameters) ){
220                 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
221                 opj_stream_destroy(l_stream);
222                 opj_destroy_codec(l_codec);
223                 return EXIT_FAILURE;
224         }
225
226         /* Read the main header of the codestream and if necessary the JP2 boxes*/
227         if(! opj_read_header(l_stream, l_codec, &image)){
228                 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
229                 opj_stream_destroy(l_stream);
230                 opj_destroy_codec(l_codec);
231                 opj_image_destroy(image);
232                 return EXIT_FAILURE;
233         }
234
235         /* Extract some info from the code stream */
236         cstr_info = opj_get_cstr_info(l_codec);
237
238         fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);
239
240         tile_ul = 0;
241         tile_ur = cstr_info->tw - 1;
242         tile_lr = cstr_info->tw * cstr_info->th - 1;
243         tile_ll = tile_lr - cstr_info->tw;
244
245 #define TEST_TILE( tile_index ) \
246         fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
247         if(!opj_get_decoded_tile(l_codec, l_stream, image, tile_index )){ \
248                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
249                 opj_stream_destroy(l_stream); \
250                 opj_destroy_cstr_info(&cstr_info); \
251                 opj_destroy_codec(l_codec); \
252                 opj_image_destroy(image); \
253                 return EXIT_FAILURE; \
254         } \
255   for(index = 0; index < image->numcomps; ++index) { \
256     if( image->comps[index].data == NULL ){ \
257         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
258                 opj_stream_destroy(l_stream); \
259                 opj_destroy_cstr_info(&cstr_info); \
260                 opj_destroy_codec(l_codec); \
261                 opj_image_destroy(image); \
262         return EXIT_FAILURE; \
263         } \
264   } \
265         fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);
266
267         TEST_TILE(tile_ul)
268         TEST_TILE(tile_lr)
269         TEST_TILE(tile_ul)
270         TEST_TILE(tile_ll)
271         TEST_TILE(tile_ur)
272         TEST_TILE(tile_lr)
273
274         /* Close the byte stream */
275         opj_stream_destroy(l_stream);
276
277         /* Destroy code stream info */
278         opj_destroy_cstr_info(&cstr_info);
279
280         /* Free remaining structures */
281         opj_destroy_codec(l_codec);
282
283         /* Free image data structure */
284         opj_image_destroy(image);
285
286         return EXIT_SUCCESS;
287 }
288 /*end main*/
289
290
291
292