Possibility to choose to apply MCT (multiple component transform) enabled, and new...
[openjpeg.git] / mj2 / MJ2_codec / extract_j2k_from_mj2.c
1 #include <stdio.h>
2 #ifdef WIN32
3 #include <malloc.h>
4 #else
5 #include <stdlib.h>
6 #endif
7 #include <setjmp.h>
8
9 #include "mj2.h"
10
11 //MEMORY LEAK
12 #ifdef _DEBUG
13 #define _CRTDBG_MAP_ALLOC
14 #include <stdlib.h>  // Must be included first
15 #include <crtdbg.h>
16 #endif
17 //MEM
18
19 jmp_buf j2k_error;
20
21 int main(int argc, char *argv[]) {
22
23   int tnum;
24   unsigned int snum;
25   mj2_movie_t movie;
26   mj2_tk_t *track;
27   mj2_sample_t *sample;
28   unsigned char* frame_codestream;
29   FILE *file, *outfile;
30   char outfilename[50];
31
32   if (argc != 3) {
33     printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n"); 
34     printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
35     return 1;
36   }
37   
38   file = fopen(argv[1], "rb");
39   
40   if (!file) {
41     fprintf(stderr, "failed to open %s for reading\n", argv[1]);
42     return 1;
43   }
44
45   if (mj2_read_struct(file, &movie)) // Creating the movie structure
46     return 1;
47
48   // Decode first video track 
49   tnum = 0;
50   while (movie.tk[tnum].track_type != 0)
51     tnum ++;
52
53   track = &movie.tk[tnum];
54
55   fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
56
57   for (snum=0; snum < track->num_samples; snum++)
58   {
59     sample = &track->sample[snum];
60     frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
61     fseek(file,sample->offset+8,SEEK_SET);
62     fread(frame_codestream,sample->sample_size-8,1, file);  // Assuming that jp and ftyp markers size do
63
64     sprintf(outfilename,"%s_%d.j2k",argv[2],snum);
65     outfile = fopen(outfilename, "wb");
66     if (!outfile) {
67       fprintf(stderr, "failed to open %s for writing\n",outfilename);
68       return 1;
69     }
70     fwrite(frame_codestream,sample->sample_size-8,1,outfile);
71     fclose(outfile);
72     free(frame_codestream);
73     }
74   fclose(file);
75   fprintf(stdout, "%d frames correctly extracted\n", snum);
76   mj2_memory_free(&movie);
77
78   //MEMORY LEAK
79   #ifdef _DEBUG
80     _CrtDumpMemoryLeaks();
81   #endif
82   //MEM
83
84   return 0;
85 }