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 mj2_init_stdmovie(&movie);
46 // Decode first video track
48 while (movie.tk[tnum].track_type != 0)
51 track = &movie.tk[tnum];
53 fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
55 for (snum=0; snum < track->num_samples; snum++)
57 sample = &track->sample[snum];
58 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
59 fseek(file,sample->offset+8,SEEK_SET);
60 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
62 sprintf(outfilename,"%s_%d.j2k",argv[2],snum);
63 outfile = fopen(outfilename, "wb");
65 fprintf(stderr, "failed to open %s for writing\n",outfilename);
68 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
70 free(frame_codestream);
73 fprintf(stdout, "%d frames correctly extracted\n", snum);
74 mj2_memory_free(&movie);
78 _CrtDumpMemoryLeaks();