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;
33 printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n");
34 printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
38 file = fopen(argv[1], "rb");
41 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
45 // Checking output file
46 outfile = fopen(argv[2], "w");
48 fprintf(stderr, "failed to open %s for writing\n", argv[2]);
53 if (mj2_read_struct(file, &movie)) // Creating the movie structure
57 // Decode first video track
59 while (movie.tk[tnum].track_type != 0)
62 track = &movie.tk[tnum];
64 // Output info on first video tracl
65 fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
66 track->num_samples, track->w, track->h);
68 for (snum=0; snum < track->num_samples; snum++)
70 fprintf(stdout,"Frame %d: ",snum+1);
71 sample = &track->sample[snum];
72 frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
73 fseek(file,sample->offset+8,SEEK_SET);
74 fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
76 if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
79 if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2)
80 && (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1))
81 || (img.numcomps == 1)) {
83 if (imagetoyuv(&img, &cp, argv[2])) // Convert image to YUV
86 else if ((img.numcomps == 3) &&
87 (img.comps[0].dx == 1) && (img.comps[1].dx == 1)&&
88 (img.comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
90 fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
91 sprintf(outfilename,"output_%d.bmp",snum);
92 if (imagetobmp(&img, &cp, outfilename)) // Convert image to YUV
97 fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
98 fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
100 sprintf(outfilename,"output_%d.j2k",snum);
101 outfile = fopen(outfilename, "wb");
103 fprintf(stderr, "failed to open %s for writing\n",outfilename);
106 fwrite(frame_codestream,sample->sample_size-8,1,outfile);
109 for (i=0; i<img.numcomps; i++)
110 free(img.comps[i].data);
112 free(frame_codestream);
116 fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
117 mj2_memory_free(&movie);
122 _CrtDumpMemoryLeaks();