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) 2003-2007, Francois-Olivier Devaux
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include "opj_includes.h"
32 /* -------------------------------------------------------------------------- */
35 sample error callback expecting a FILE* client object
37 void error_callback(const char *msg, void *client_data) {
38 FILE *stream = (FILE*)client_data;
39 fprintf(stream, "[ERROR] %s", msg);
42 sample warning callback expecting a FILE* client object
44 void warning_callback(const char *msg, void *client_data) {
45 FILE *stream = (FILE*)client_data;
46 fprintf(stream, "[WARNING] %s", msg);
49 sample debug callback expecting a FILE* client object
51 void info_callback(const char *msg, void *client_data) {
52 FILE *stream = (FILE*)client_data;
53 fprintf(stream, "[INFO] %s", msg);
56 /* -------------------------------------------------------------------------- */
59 int main(int argc, char *argv[]) {
61 opj_event_mgr_t event_mgr; /* event manager */
67 unsigned char* frame_codestream;
70 mj2_dparameters_t parameters;
73 printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n");
74 printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
78 file = fopen(argv[1], "rb");
81 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
86 configure the event callbacks (not required)
87 setting of each callback is optionnal
89 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
90 event_mgr.error_handler = error_callback;
91 event_mgr.warning_handler = warning_callback;
92 event_mgr.info_handler = info_callback;
94 /* get a MJ2 decompressor handle */
95 dinfo = mj2_create_decompress();
97 /* catch events using our callbacks and give a local context */
98 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
100 /* setup the decoder decoding parameters using user parameters */
101 movie = (opj_mj2_t*) dinfo->mj2_handle;
102 mj2_setup_decoder(dinfo->mj2_handle, ¶meters);
104 if (mj2_read_struct(file, movie)) // Creating the movie structure
107 // Decode first video track
109 while (movie->tk[tnum].track_type != 0)
112 track = &movie->tk[tnum];
114 fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
116 for (snum=0; snum < track->num_samples; snum++)
118 sample = &track->sample[snum];
119 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
120 fseek(file,sample->offset+8,SEEK_SET);
121 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
123 sprintf(outfilename,"%s_%05d.j2k",argv[2],snum);
124 outfile = fopen(outfilename, "wb");
126 fprintf(stderr, "failed to open %s for writing\n",outfilename);
129 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
131 free(frame_codestream);
134 fprintf(stdout, "%d frames correctly extracted\n", snum);
136 /* free remaining structures */
138 mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);