9 #define _CRTDBG_MAP_ALLOC
10 #include <stdlib.h> // Must be included first
17 int main(int argc, char *argv[]) {
24 unsigned char* frame_codestream;
29 printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n");
30 printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
34 file = fopen(argv[1], "rb");
37 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
41 if (mj2_read_struct(file, &movie)) // Creating the movie structure
44 // Decode first video track
46 while (movie.tk[tnum].track_type != 0)
49 track = &movie.tk[tnum];
51 fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
53 for (snum=0; snum < track->num_samples; snum++)
55 sample = &track->sample[snum];
56 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
57 fseek(file,sample->offset+8,SEEK_SET);
58 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
60 sprintf(outfilename,"%s_%d.j2k",argv[2],snum);
61 outfile = fopen(outfilename, "wb");
63 fprintf(stderr, "failed to open %s for writing\n",outfilename);
66 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
68 free(frame_codestream);
71 fprintf(stdout, "%d frames correctly extracted\n", snum);
72 mj2_memory_free(&movie);
76 _CrtDumpMemoryLeaks();