Newline at end of file
[openjpeg.git] / mj2 / mj2_to_frames.c
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <setjmp.h>
4
5 #include "mj2.h"
6 #include "mj2_convert.h"
7 #include <openjpeg.h>
8
9 //MEMORY LEAK
10 #ifdef _DEBUG
11 #define _CRTDBG_MAP_ALLOC
12 #include <stdlib.h>  // Must be included first
13 #include <crtdbg.h>
14 #endif
15 //MEM
16
17 jmp_buf j2k_error;
18
19 int main(int argc, char *argv[]) {
20
21   unsigned int tnum, snum;
22   mj2_movie_t movie;
23   mj2_tk_t *track;
24   mj2_sample_t *sample;
25   unsigned char* frame_codestream;
26   FILE *file, *outfile;
27   char outfilename[50];
28   j2k_image_t img;
29   j2k_cp_t cp;
30   int i;
31
32   cp.layer=0;
33   cp.reduce=0;
34
35   if (argc != 3) {
36     printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n"); 
37     printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
38     return 1;
39   }
40   
41   file = fopen(argv[1], "rb");
42   
43   if (!file) {
44     fprintf(stderr, "failed to open %s for reading\n", argv[1]);
45     return 1;
46   }
47
48   // Checking output file
49   outfile = fopen(argv[2], "w");
50   if (!file) {
51     fprintf(stderr, "failed to open %s for writing\n", argv[2]);
52     return 1;
53   }
54   fclose(outfile);
55
56   if (mj2_read_struct(file, &movie)) // Creating the movie structure
57     return 1;
58
59
60   // Decode first video track 
61   tnum = 0;
62   while (movie.tk[tnum].track_type != 0)
63     tnum ++;
64
65   track = &movie.tk[tnum];
66
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);
70
71   for (snum=0; snum < track->num_samples; snum++)
72   {
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
78
79     if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
80       return 1;
81
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)) {
85       
86       if (imagetoyuv(&img, &cp, argv[2]))       // Convert image to YUV
87         return 1;
88     }
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
92     {
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
96         return 1;
97       
98     }
99     else {
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");
102
103       sprintf(outfilename,"output_%d.j2k",snum);
104       outfile = fopen(outfilename, "wb");
105       if (!outfile) {
106         fprintf(stderr, "failed to open %s for writing\n",outfilename);
107         return 1;
108       }
109       fwrite(frame_codestream,sample->sample_size-8,1,outfile);
110       fclose(outfile);
111     }
112     for (i=0; i<img.numcomps; i++)
113       free(img.comps[i].data);
114     j2k_dec_release();
115     free(frame_codestream);
116   }
117
118   fclose(file);
119   fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
120   mj2_memory_free(&movie);
121
122
123   //MEMORY LEAK
124   #ifdef _DEBUG
125     _CrtDumpMemoryLeaks();
126   #endif
127   //MEM
128
129   return 0;
130 }