6 #include "mj2_convert.h"
11 #define _CRTDBG_MAP_ALLOC
12 #include <stdlib.h> // Must be included first
19 int main(int argc, char *argv[]) {
21 unsigned int tnum, snum;
25 unsigned char* frame_codestream;
36 printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n");
37 printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
41 file = fopen(argv[1], "rb");
44 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
48 // Checking output file
49 outfile = fopen(argv[2], "w");
51 fprintf(stderr, "failed to open %s for writing\n", argv[2]);
56 if (mj2_read_struct(file, &movie)) // Creating the movie structure
60 // Decode first video track
62 while (movie.tk[tnum].track_type != 0)
65 track = &movie.tk[tnum];
67 // Output info on first video tracl
68 fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
69 track->num_samples, track->w, track->h);
71 for (snum=0; snum < track->num_samples; snum++)
73 fprintf(stdout,"Frame %d: ",snum+1);
74 sample = &track->sample[snum];
75 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
76 fseek(file,sample->offset+8,SEEK_SET);
77 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
79 if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
82 if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2)
83 && (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1))
84 || (img.numcomps == 1)) {
86 if (imagetoyuv(&img, &cp, argv[2])) // Convert image to YUV
89 else if ((img.numcomps == 3) &&
90 (img.comps[0].dx == 1) && (img.comps[1].dx == 1)&&
91 (img.comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
93 fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
94 sprintf(outfilename,"output_%d.bmp",snum);
95 if (imagetobmp(&img, &cp, outfilename)) // Convert image to YUV
100 fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
101 fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
103 sprintf(outfilename,"output_%d.j2k",snum);
104 outfile = fopen(outfilename, "wb");
106 fprintf(stderr, "failed to open %s for writing\n",outfilename);
109 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
112 for (i=0; i<img.numcomps; i++)
113 free(img.comps[i].data);
115 free(frame_codestream);
119 fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
120 mj2_memory_free(&movie);
125 _CrtDumpMemoryLeaks();