Merge pull request #1244 from rouault/fix_pi_warnings
[openjpeg.git] / src / bin / mj2 / opj_mj2_decompress.c
1 /*
2 * Copyright (c) 2003-2004, Francois-Olivier Devaux
3 * Copyright (c) 2002-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
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
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "opj_apps_config.h"
33 #include "openjpeg.h"
34 #include "j2k_lib.h"
35 #include "cio.h"
36 #include "j2k.h"
37 #include "jp2.h"
38 #include "mj2.h"
39 #include "mj2_convert.h"
40
41 #ifdef OPJ_HAVE_LIBLCMS2
42 #include <lcms2.h>
43 #endif
44 #ifdef OPJ_HAVE_LIBLCMS1
45 #include <lcms.h>
46 #endif
47 #include "color.h"
48 /* -------------------------------------------------------------------------- */
49
50 /**
51 sample error callback expecting a FILE* client object
52 */
53 static void error_callback(const char *msg, void *client_data)
54 {
55     FILE *stream = (FILE*)client_data;
56     fprintf(stream, "[ERROR] %s", msg);
57 }
58 /**
59 sample warning callback expecting a FILE* client object
60 */
61 static void warning_callback(const char *msg, void *client_data)
62 {
63     FILE *stream = (FILE*)client_data;
64     fprintf(stream, "[WARNING] %s", msg);
65 }
66
67 /* -------------------------------------------------------------------------- */
68
69
70 int main(int argc, char *argv[])
71 {
72     mj2_dparameters_t mj2_parameters;           /* decompression parameters */
73     opj_dinfo_t* dinfo;
74     opj_event_mgr_t event_mgr;      /* event manager */
75     opj_cio_t *cio = NULL;
76     unsigned int tnum, snum;
77     opj_mj2_t *movie;
78     mj2_tk_t *track;
79     mj2_sample_t *sample;
80     unsigned char* frame_codestream;
81     FILE *file, *outfile;
82     char outfilename[50];
83     opj_image_t *img = NULL;
84     unsigned int max_codstrm_size = 0;
85     double total_time = 0;
86     unsigned int numframes = 0;
87
88     if (argc != 3) {
89         printf("Usage: %s inputfile.mj2 outputfile.yuv\n", argv[0]);
90         return 1;
91     }
92
93     file = fopen(argv[1], "rb");
94
95     if (!file) {
96         fprintf(stderr, "failed to open %s for reading\n", argv[1]);
97         return 1;
98     }
99
100     /* Checking output file */
101     outfile = fopen(argv[2], "w");
102     if (!outfile) {
103         fprintf(stderr, "failed to open %s for writing\n", argv[2]);
104         fclose(file);
105         return 1;
106     }
107     fclose(outfile);
108
109     /*
110     configure the event callbacks (not required)
111     setting of each callback is optional
112     */
113     memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
114     event_mgr.error_handler = error_callback;
115     event_mgr.warning_handler = warning_callback;
116     event_mgr.info_handler = NULL;
117
118     /* get a MJ2 decompressor handle */
119     dinfo = mj2_create_decompress();
120     movie = (opj_mj2_t*)dinfo->mj2_handle;
121
122     /* catch events using our callbacks and give a local context */
123     opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
124
125     memset(&mj2_parameters, 0, sizeof(mj2_dparameters_t));
126     /* set J2K decoding parameters to default values */
127     opj_set_default_decoder_parameters(&mj2_parameters.j2k_parameters);
128
129     /* setup the decoder decoding parameters using user parameters */
130     mj2_setup_decoder(movie, &mj2_parameters);
131
132     if (mj2_read_struct(file, movie)) { /* Creating the movie structure */
133         return 1;
134     }
135
136     /* Decode first video track */
137     for (tnum = 0;
138             tnum < (unsigned int)(movie->num_htk + movie->num_stk + movie->num_vtk);
139             tnum++) {
140         if (movie->tk[tnum].track_type == 0) {
141             break;
142         }
143     }
144
145     if (movie->tk[tnum].track_type != 0) {
146         printf("Error. Movie does not contain any video track\n");
147         return 1;
148     }
149
150     track = &movie->tk[tnum];
151
152     /* Output info on first video tracl */
153     fprintf(stdout,
154             "The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
155             track->num_samples, track->w, track->h);
156
157     max_codstrm_size = track->sample[0].sample_size - 8;
158     frame_codestream = (unsigned char*) malloc(max_codstrm_size * sizeof(
159                            unsigned char));
160
161     numframes = track->num_samples;
162
163     for (snum = 0; snum < numframes; snum++) {
164         double init_time = opj_clock();
165         double elapsed_time;
166
167         sample = &track->sample[snum];
168         if (sample->sample_size - 8 > max_codstrm_size) {
169             max_codstrm_size =  sample->sample_size - 8;
170             if ((frame_codestream = (unsigned char*)
171                                     realloc(frame_codestream, max_codstrm_size)) == NULL) {
172                 printf("Error reallocation memory\n");
173                 free(frame_codestream);
174                 return 1;
175             };
176         }
177         fseek(file, sample->offset + 8, SEEK_SET);
178         fread(frame_codestream, sample->sample_size - 8, 1,
179               file); /* Assuming that jp and ftyp markers size do */
180
181         /* open a byte stream */
182         cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream,
183                            sample->sample_size - 8);
184
185         img = opj_decode(dinfo, cio); /* Decode J2K to image */
186
187 #ifdef WANT_SYCC_TO_RGB
188         if (img->color_space == CLRSPC_SYCC) {
189             color_sycc_to_rgb(img);
190         }
191 #endif
192
193         if (img->icc_profile_buf) {
194 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
195             color_apply_icc_profile(img);
196 #endif
197
198             free(img->icc_profile_buf);
199             img->icc_profile_buf = NULL;
200             img->icc_profile_len = 0;
201         }
202
203         if (((img->numcomps == 3) && (img->comps[0].dx == img->comps[1].dx / 2)
204                 && (img->comps[0].dx == img->comps[2].dx / 2) && (img->comps[0].dx == 1))
205                 || (img->numcomps == 1)) {
206
207             if (!imagetoyuv(img, argv[2])) {  /* Convert image to YUV */
208                 return 1;
209             }
210         } else if ((img->numcomps == 3) &&
211                    (img->comps[0].dx == 1) && (img->comps[1].dx == 1) &&
212                    (img->comps[2].dx == 1)) { /* If YUV 4:4:4 input --> to bmp */
213             fprintf(stdout,
214                     "The frames will be output in a bmp format (output_1.bmp, ...)\n");
215             sprintf(outfilename, "output_%d.bmp", snum);
216             if (imagetobmp(img, outfilename)) { /* Convert image to BMP */
217                 return 1;
218             }
219
220         } else {
221             fprintf(stdout,
222                     "Image component dimensions are unknown. Unable to output image\n");
223             fprintf(stdout,
224                     "The frames will be output in a j2k file (output_1.j2k, ...)\n");
225
226             sprintf(outfilename, "output_%d.j2k", snum);
227             outfile = fopen(outfilename, "wb");
228             if (!outfile) {
229                 fprintf(stderr, "failed to open %s for writing\n", outfilename);
230                 return 1;
231             }
232             fwrite(frame_codestream, sample->sample_size - 8, 1, outfile);
233             fclose(outfile);
234         }
235         /* close the byte stream */
236         opj_cio_close(cio);
237         /* free image data structure */
238         opj_image_destroy(img);
239         elapsed_time = opj_clock() - init_time;
240         fprintf(stderr, "Frame number %d/%d decoded in %.2f mseconds\n", snum + 1,
241                 numframes, elapsed_time * 1000);
242         total_time += elapsed_time;
243
244     }
245
246     free(frame_codestream);
247     fclose(file);
248
249     /* free remaining structures */
250     if (dinfo) {
251         mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
252     }
253     free(dinfo);
254
255     fprintf(stdout, "%d frame(s) correctly decompressed\n", snum);
256     fprintf(stdout, "Total decoding time: %.2f seconds (%.1f fps)\n", total_time,
257             (float)numframes / total_time);
258
259     return 0;
260 }