minor changes in header inclusions
[openjpeg.git] / mj2 / extract_j2k_from_mj2.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) 2003-2007, Francois-Olivier Devaux 
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "openjpeg.h"
34 #include "../libopenjpeg/j2k.h"
35 #include "../libopenjpeg/jp2.h"
36 #include "mj2.h"
37
38 /* -------------------------------------------------------------------------- */
39
40 /**
41 sample error callback expecting a FILE* client object
42 */
43 void error_callback(const char *msg, void *client_data) {
44         FILE *stream = (FILE*)client_data;
45         fprintf(stream, "[ERROR] %s", msg);
46 }
47 /**
48 sample warning callback expecting a FILE* client object
49 */
50 void warning_callback(const char *msg, void *client_data) {
51         FILE *stream = (FILE*)client_data;
52         fprintf(stream, "[WARNING] %s", msg);
53 }
54 /**
55 sample debug callback expecting a FILE* client object
56 */
57 void info_callback(const char *msg, void *client_data) {
58         FILE *stream = (FILE*)client_data;
59         fprintf(stream, "[INFO] %s", msg);
60 }
61
62 /* -------------------------------------------------------------------------- */
63
64
65 int main(int argc, char *argv[]) {
66         opj_dinfo_t* dinfo; 
67         opj_event_mgr_t event_mgr;              /* event manager */
68   int tnum;
69   unsigned int snum;
70   opj_mj2_t *movie;
71   mj2_tk_t *track;
72   mj2_sample_t *sample;
73   unsigned char* frame_codestream;
74   FILE *file, *outfile;
75   char outfilename[50];
76         mj2_dparameters_t parameters;
77
78   if (argc != 3) {
79     printf("Usage: %s mj2filename output_location\n",argv[0]); 
80     printf("Example: %s foreman.mj2 output/foreman\n",argv[0]);
81     return 1;
82   }
83   
84   file = fopen(argv[1], "rb");
85   
86   if (!file) {
87     fprintf(stderr, "failed to open %s for reading\n", argv[1]);
88     return 1;
89   }
90
91         /*
92         configure the event callbacks (not required)
93         setting of each callback is optionnal
94         */
95         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
96         event_mgr.error_handler = error_callback;
97         event_mgr.warning_handler = warning_callback;
98         event_mgr.info_handler = info_callback;
99
100         /* get a MJ2 decompressor handle */
101         dinfo = mj2_create_decompress();
102
103         /* catch events using our callbacks and give a local context */
104         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);           
105
106         /* setup the decoder decoding parameters using user parameters */
107         movie = (opj_mj2_t*) dinfo->mj2_handle;
108         mj2_setup_decoder((opj_mj2_t*)dinfo->mj2_handle, &parameters);
109
110   if (mj2_read_struct(file, movie)) // Creating the movie structure
111     return 1;
112
113   // Decode first video track 
114   tnum = 0;
115   while (movie->tk[tnum].track_type != 0)
116     tnum ++;
117
118   track = &movie->tk[tnum];
119
120   fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
121
122   for (snum=0; snum < track->num_samples; snum++)
123   {
124     sample = &track->sample[snum];
125     frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
126     fseek(file,sample->offset+8,SEEK_SET);
127     fread(frame_codestream,sample->sample_size-8,1, file);  // Assuming that jp and ftyp markers size do
128
129     sprintf(outfilename,"%s_%05d.j2k",argv[2],snum);
130     outfile = fopen(outfilename, "wb");
131     if (!outfile) {
132       fprintf(stderr, "failed to open %s for writing\n",outfilename);
133       return 1;
134     }
135     fwrite(frame_codestream,sample->sample_size-8,1,outfile);
136     fclose(outfile);
137     free(frame_codestream);
138     }
139   fclose(file);
140   fprintf(stdout, "%d frames correctly extracted\n", snum);
141         
142         /* free remaining structures */
143         if(dinfo) {
144                 mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
145         }
146         
147   return 0;
148 }