re-organization of openjpeg directories hierarchy : step 1
[openjpeg.git] / applications / mj2 / mj2_convert.c
1 /*
2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "../libopenjpeg/opj_includes.h"
30 #include "mj2.h"
31
32 /*  -----------------------           */
33 /*                                    */
34 /*                                    */
35 /*  Count the number of frames        */
36 /*  in a YUV file                     */
37 /*                                    */
38 /*  -----------------------           */
39
40 int yuv_num_frames(mj2_tk_t * tk, char *infile)
41 {
42   int numimages, frame_size, prec_size;
43   long end_of_f;
44         FILE *f;
45
46   f = fopen(infile,"rb");
47   if (!f) {  
48     fprintf(stderr, "failed to open %s for reading\n",infile);
49     return -1;
50   }
51         prec_size = (tk->depth + 7)/8;/* bytes of precision */
52
53   frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));       /* Calculate frame size */
54         frame_size *= prec_size; 
55         
56   fseek(f, 0, SEEK_END);
57   end_of_f = ftell(f);          /* Calculate file size */
58         
59   if (end_of_f < frame_size) {
60     fprintf(stderr,
61                         "YUV does not contains any frame of %d x %d size\n", tk->w,
62                         tk->h);
63     return -1;
64   }
65         
66   numimages = end_of_f / frame_size;    /* Calculate number of images */
67         fclose(f);
68
69   return numimages;
70 }
71
72 //  -----------------------
73 //
74 //
75 //  YUV to IMAGE
76 //
77 //  -----------------------
78
79 opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters)
80 {
81         opj_image_cmptparm_t cmptparm[3];
82         opj_image_t * img;
83         int i;
84         int numcomps = 3;
85         int subsampling_dx = parameters->subsampling_dx;
86         int subsampling_dy = parameters->subsampling_dy;
87
88         /* initialize image components */
89         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
90         for(i = 0; i < numcomps; i++) {
91                 cmptparm[i].prec = tk->depth;
92                 cmptparm[i].bpp = tk->depth;
93                 cmptparm[i].sgnd = 0;           
94                 cmptparm[i].dx = i ? subsampling_dx * tk->CbCr_subsampling_dx : subsampling_dx;
95                 cmptparm[i].dy = i ? subsampling_dy * tk->CbCr_subsampling_dy : subsampling_dy;
96                 cmptparm[i].w = tk->w;
97                 cmptparm[i].h = tk->h;
98         }
99         /* create the image */
100         img = opj_image_create(numcomps, cmptparm, CLRSPC_SRGB);
101         return img;
102 }
103
104 char yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile)
105 {
106   int i, compno;
107   int offset, size, max, prec_bytes, is_16, v;
108   long end_of_f, position;
109         int numcomps = 3;
110         int subsampling_dx = parameters->subsampling_dx;
111         int subsampling_dy = parameters->subsampling_dy;
112         FILE *yuvfile;
113         int *data;
114         unsigned char uc;
115
116   yuvfile = fopen(infile,"rb");
117   if (!yuvfile) {  
118     fprintf(stderr, "failed to open %s for readings\n",parameters->infile);
119     return 1;
120   }
121         is_16 = (tk->depth > 8);
122         prec_bytes = (is_16?2:1);
123
124   offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 +
125                 1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));
126   offset *= prec_bytes;
127
128   fseek(yuvfile, 0, SEEK_END);
129   end_of_f = ftell(yuvfile);
130   fseek(yuvfile, sizeof(unsigned char) * offset, SEEK_SET);
131   position = ftell(yuvfile);
132   if (position >= end_of_f) {
133     fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
134                         frame_num);
135                 fclose(yuvfile);
136     return 1;
137   }
138         
139   img->x0 = tk->Dim[0];
140   img->y0 = tk->Dim[1];
141   img->x1 = !tk->Dim[0] ? (tk->w - 1) * subsampling_dx + 1 : tk->Dim[0] +
142     (tk->w - 1) * subsampling_dx + 1;
143   img->y1 = !tk->Dim[1] ? (tk->h - 1) * subsampling_dy + 1 : tk->Dim[1] +
144     (tk->h - 1) * subsampling_dy + 1;
145
146         size = tk->w * tk->h * prec_bytes;
147         
148         for(compno = 0; compno < numcomps; compno++) 
149    {
150         max = size/(img->comps[compno].dx * img->comps[compno].dy);
151         data = img->comps[compno].data;
152
153         for (i = 0; i < max && !feof(yuvfile); i++)
154   {
155         v = 0;
156         fread(&uc, 1, 1, yuvfile);
157         v = uc;
158
159         if(is_16)
160  {
161         fread(&uc, 1, 1, yuvfile);
162         v |= (uc<<8);
163  }
164         *data++ = v;
165   }
166    }
167         fclose(yuvfile);
168         
169   return 0;
170 }
171
172
173
174 //  -----------------------
175 //
176 //
177 //  IMAGE to YUV
178 //
179 //  -----------------------
180
181
182 bool imagetoyuv(opj_image_t * img, char *outfile)
183 {
184   FILE *f;
185   int *data;
186   int i, v, is_16, prec_bytes;
187   unsigned char buf[2];
188
189   if (img->numcomps == 3) {
190     if (img->comps[0].dx != img->comps[1].dx / 2
191       || img->comps[1].dx != img->comps[2].dx) {
192       fprintf(stderr,
193                                 "Error with the input image components size: cannot create yuv file)\n");
194       return false;
195     }
196   } else if (!(img->numcomps == 1)) {
197     fprintf(stderr,
198       "Error with the number of image components(must be one or three)\n");
199     return false;
200   }
201   
202   f = fopen(outfile, "a+b");
203   if (!f) {
204     fprintf(stderr, "failed to open %s for writing\n", outfile);
205     return false;
206   }
207   is_16 = (img->comps[0].prec > 8);
208   prec_bytes = (is_16?2:1);
209   data = img->comps[0].data;
210   
211   for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) {
212     v = *data++;
213     buf[0] = (unsigned char)v;
214
215         if(is_16) buf[1] = (unsigned char)(v>>8);
216
217     fwrite(buf, 1, prec_bytes, f);
218   }
219   
220   
221   if (img->numcomps == 3) {
222         data = img->comps[1].data;
223
224     for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) {
225       v = *data++;
226       buf[0] = (unsigned char)v;
227
228       if(is_16) buf[1] = (unsigned char)(v>>8);
229
230       fwrite(buf, 1, prec_bytes, f);
231     }
232     data = img->comps[2].data;
233     
234     for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) {
235       v = *data++;
236       buf[0] = (unsigned char)v;
237
238       if(is_16) buf[1] = (unsigned char)(v>>8);
239
240       fwrite(buf, 1, prec_bytes, f);
241     }
242   } else if (img->numcomps == 1) {
243 /* fake CbCr values */
244         if(is_16) 
245   { 
246         buf[0] = 255;
247         if(img->comps[0].prec == 10) buf[1] = 1;
248         else
249         if(img->comps[0].prec == 12) buf[1] = 3;
250         else
251          buf[1] = 125;
252   } 
253         else buf[0] = 125;
254
255     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
256       fwrite(buf, 1, prec_bytes, f);
257     }
258     
259     
260     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
261       fwrite(buf, 1, prec_bytes, f);
262     }
263   }  
264   fclose(f);
265   return true;
266 }
267
268 //  -----------------------
269 //
270 //
271 //  IMAGE to BMP
272 //
273 //  -----------------------
274
275 int imagetobmp(opj_image_t * img, char *outfile) {
276   int w,wr,h,hr,i,pad;
277   FILE *f;
278   
279   if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
280     && img->comps[1].dx == img->comps[2].dx
281     && img->comps[0].dy == img->comps[1].dy
282     && img->comps[1].dy == img->comps[2].dy
283     && img->comps[0].prec == img->comps[1].prec
284     && img->comps[1].prec == img->comps[2].prec) {
285     /* -->> -->> -->> -->>
286     
287       24 bits color
288       
289     <<-- <<-- <<-- <<-- */
290     
291     f = fopen(outfile, "wb");
292     if (!f) {
293       fprintf(stderr, "failed to open %s for writing\n", outfile);
294       return 1;
295     }   
296     
297     w = img->comps[0].w;
298     wr = int_ceildivpow2(img->comps[0].w, img->comps[0].factor);
299     
300     h = img->comps[0].h;
301     hr = int_ceildivpow2(img->comps[0].h, img->comps[0].factor);
302     
303     fprintf(f, "BM");
304     
305     /* FILE HEADER */
306     /* ------------- */
307     fprintf(f, "%c%c%c%c",
308       (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
309       54) & 0xff,
310       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
311       >> 8) & 0xff,
312       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
313       >> 16) & 0xff,
314       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
315       >> 24) & 0xff);
316     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
317       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
318     fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
319       ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
320     
321     /* INFO HEADER   */
322     /* ------------- */
323     fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
324       ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
325     fprintf(f, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
326       (unsigned char) ((wr) >> 8) & 0xff,
327       (unsigned char) ((wr) >> 16) & 0xff,
328       (unsigned char) ((wr) >> 24) & 0xff);
329     fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
330       (unsigned char) ((hr) >> 8) & 0xff,
331       (unsigned char) ((hr) >> 16) & 0xff,
332       (unsigned char) ((hr) >> 24) & 0xff);
333     fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
334     fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
335     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
336       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
337     fprintf(f, "%c%c%c%c",
338       (unsigned char) (3 * hr * wr +
339       3 * hr * (wr % 2)) & 0xff,
340       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
341       8) & 0xff,
342       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
343       16) & 0xff,
344       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
345       24) & 0xff);
346     fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
347       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
348     fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
349       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
350     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
351       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
352     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
353       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
354     
355     for (i = 0; i < wr * hr; i++) {
356       unsigned char R, G, B;
357       /* a modifier */
358       // R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
359       R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
360       // G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
361       G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
362       // B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
363       B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
364       fprintf(f, "%c%c%c", B, G, R);
365       
366       if ((i + 1) % wr == 0) {
367                                 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
368                                         fprintf(f, "%c", 0);
369       }
370     }
371     fclose(f);
372   }
373   return 0;
374 }