9 #define _CRTDBG_MAP_ALLOC
10 #include <stdlib.h> // Must be included first
17 int main(int argc, char *argv[]) {
19 unsigned int tnum, snum;
23 unsigned char* frame_codestream;
28 printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n");
29 printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
33 file = fopen(argv[1], "rb");
36 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
40 if (mj2_read_struct(file, &movie)) // Creating the movie structure
43 // Decode first video track
45 while (movie.tk[tnum].track_type != 0)
48 track = &movie.tk[tnum];
50 fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
52 for (snum=0; snum < track->num_samples; snum++)
54 sample = &track->sample[snum];
55 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
56 fseek(file,sample->offset+8,SEEK_SET);
57 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
59 sprintf(outfilename,"%s_%d.j2k",argv[2],snum);
60 outfile = fopen(outfilename, "wb");
62 fprintf(stderr, "failed to open %s for writing\n",outfilename);
65 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
67 free(frame_codestream);
70 fprintf(stdout, "%d frames correctly extracted\n", snum);
71 mj2_memory_free(&movie);
75 _CrtDumpMemoryLeaks();