7509600d76cd17d811aa6d33c74f08dccb8a293e
[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   if (argc != 3) {
33     printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n"); 
34     printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
35     return 1;
36   }
37   
38   file = fopen(argv[1], "rb");
39   
40   if (!file) {
41     fprintf(stderr, "failed to open %s for reading\n", argv[1]);
42     return 1;
43   }
44
45   // Checking output file
46   outfile = fopen(argv[2], "w");
47   if (!file) {
48     fprintf(stderr, "failed to open %s for writing\n", argv[2]);
49     return 1;
50   }
51   fclose(outfile);
52
53   if (mj2_read_struct(file, &movie)) // Creating the movie structure
54     return 1;
55
56
57   // Decode first video track 
58   tnum = 0;
59   while (movie.tk[tnum].track_type != 0)
60     tnum ++;
61
62   track = &movie.tk[tnum];
63
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);
67
68   for (snum=0; snum < track->num_samples; snum++)
69   {
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
75
76     if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
77       return 1;
78
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)) {
82       
83       if (imagetoyuv(&img, &cp, argv[2]))       // Convert image to YUV
84         return 1;
85     }
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
89     {
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
93         return 1;
94       
95     }
96     else {
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");
99
100       sprintf(outfilename,"output_%d.j2k",snum);
101       outfile = fopen(outfilename, "wb");
102       if (!outfile) {
103         fprintf(stderr, "failed to open %s for writing\n",outfilename);
104         return 1;
105       }
106       fwrite(frame_codestream,sample->sample_size-8,1,outfile);
107       fclose(outfile);
108     }
109     for (i=0; i<img.numcomps; i++)
110       free(img.comps[i].data);
111     j2k_dec_release();
112     free(frame_codestream);
113   }
114
115   fclose(file);
116   fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
117   mj2_memory_free(&movie);
118
119
120   //MEMORY LEAK
121   #ifdef _DEBUG
122     _CrtDumpMemoryLeaks();
123   #endif
124   //MEM
125
126   return 0;
127 }