Reload image doesn't crash in OPJViewer; more settings saved to registry
[openjpeg.git] / 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 "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;
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         
52   frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));       /* Calculate frame size */
53         
54   fseek(f, 0, SEEK_END);
55   end_of_f = ftell(f);          /* Calculate file size */
56         
57   if (end_of_f < frame_size) {
58     fprintf(stderr,
59                         "YUV does not contains any frame of %d x %d size\n", tk->w,
60                         tk->h);
61     return 0;
62   }
63         
64   numimages = end_of_f / frame_size;    /* Calculate number of images */
65         fclose(f);
66
67   return numimages;
68 }
69
70 //  -----------------------
71 //
72 //
73 //  YUV to IMAGE
74 //
75 //  -----------------------
76
77 opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters)
78 {
79         opj_image_cmptparm_t cmptparm[3];
80         opj_image_t * img;
81         int i;
82         int numcomps = 3;
83         int subsampling_dx = parameters->subsampling_dx;
84         int subsampling_dy = parameters->subsampling_dy;
85
86         /* initialize image components */
87         memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
88         for(i = 0; i < numcomps; i++) {
89                 cmptparm[i].prec = 8;
90                 cmptparm[i].bpp = 8;
91                 cmptparm[i].sgnd = 0;           
92                 cmptparm[i].dx = i ? subsampling_dx * tk->CbCr_subsampling_dx : subsampling_dx;
93                 cmptparm[i].dy = i ? subsampling_dy * tk->CbCr_subsampling_dy : subsampling_dy;
94                 cmptparm[i].w = tk->w;
95                 cmptparm[i].h = tk->h;
96         }
97         /* create the image */
98         img = opj_image_create(numcomps, cmptparm, CLRSPC_SRGB);
99         return img;
100 }
101
102 bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile)
103 {
104   int i, compno;
105   int offset;
106   long end_of_f, position;
107         int numcomps = 3;
108         int subsampling_dx = parameters->subsampling_dx;
109         int subsampling_dy = parameters->subsampling_dy;
110         FILE *yuvfile;
111         
112   yuvfile = fopen(infile,"rb");
113   if (!yuvfile) {  
114     fprintf(stderr, "failed to open %s for reading\n",parameters->infile);
115     return false;
116   }
117
118   offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 +
119                 1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));
120   fseek(yuvfile, 0, SEEK_END);
121   end_of_f = ftell(yuvfile);
122   fseek(yuvfile, sizeof(unsigned char) * offset, SEEK_SET);
123   position = ftell(yuvfile);
124   if (position >= end_of_f) {
125     fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
126                         frame_num);
127                 fclose(yuvfile);
128     return false;
129   }
130         
131   img->x0 = tk->Dim[0];
132   img->y0 = tk->Dim[1];
133   img->x1 = !tk->Dim[0] ? (tk->w - 1) * subsampling_dx + 1 : tk->Dim[0] +
134     (tk->w - 1) * subsampling_dx + 1;
135   img->y1 = !tk->Dim[1] ? (tk->h - 1) * subsampling_dy + 1 : tk->Dim[1] +
136     (tk->h - 1) * subsampling_dy + 1;
137         
138         for(compno = 0; compno < numcomps; compno++) {
139                 for (i = 0; i < (tk->w * tk->h / (img->comps[compno].dx * img->comps[compno].dy))
140                         && !feof(yuvfile); i++) {
141                         if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) {
142                                 fprintf(stderr, "Error reading %s file !!\n", infile);                          
143                                 return false;
144                         }
145                 }
146         }
147         fclose(yuvfile);
148         
149   return true;
150 }
151
152
153
154 //  -----------------------
155 //
156 //
157 //  IMAGE to YUV
158 //
159 //  -----------------------
160
161
162 bool imagetoyuv(opj_image_t * img, char *outfile)
163 {
164   FILE *f;
165   int i;
166   
167   if (img->numcomps == 3) {
168     if (img->comps[0].dx != img->comps[1].dx / 2
169       || img->comps[1].dx != img->comps[2].dx) {
170       fprintf(stderr,
171                                 "Error with the input image components size: cannot create yuv file)\n");
172       return false;
173     }
174   } else if (!(img->numcomps == 1)) {
175     fprintf(stderr,
176       "Error with the number of image components(must be one or three)\n");
177     return false;
178   }
179   
180   f = fopen(outfile, "a+b");
181   if (!f) {
182     fprintf(stderr, "failed to open %s for writing\n", outfile);
183     return false;
184   }
185   
186   
187   for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) {
188     unsigned char y;
189     y = img->comps[0].data[i];
190     fwrite(&y, 1, 1, f);
191   }
192   
193   
194   if (img->numcomps == 3) {
195     for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) {
196       unsigned char cb;
197       cb = img->comps[1].data[i];
198       fwrite(&cb, 1, 1, f);
199     }
200     
201     
202     for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) {
203       unsigned char cr;
204       cr = img->comps[2].data[i];
205       fwrite(&cr, 1, 1, f);
206     }
207   } else if (img->numcomps == 1) {
208     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
209       unsigned char cb = 125;
210       fwrite(&cb, 1, 1, f);
211     }
212     
213     
214     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
215       unsigned char cr = 125;
216       fwrite(&cr, 1, 1, f);
217     }
218   }  
219   fclose(f);
220   return true;
221 }
222
223 //  -----------------------
224 //
225 //
226 //  IMAGE to BMP
227 //
228 //  -----------------------
229
230 int imagetobmp(opj_image_t * img, char *outfile) {
231   int w,wr,h,hr,i,pad;
232   FILE *f;
233   
234   if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
235     && img->comps[1].dx == img->comps[2].dx
236     && img->comps[0].dy == img->comps[1].dy
237     && img->comps[1].dy == img->comps[2].dy
238     && img->comps[0].prec == img->comps[1].prec
239     && img->comps[1].prec == img->comps[2].prec) {
240     /* -->> -->> -->> -->>
241     
242       24 bits color
243       
244     <<-- <<-- <<-- <<-- */
245     
246     f = fopen(outfile, "wb");
247     if (!f) {
248       fprintf(stderr, "failed to open %s for writing\n", outfile);
249       return 1;
250     }   
251     
252     w = img->comps[0].w;
253     wr = int_ceildivpow2(img->comps[0].w, img->comps[0].factor);
254     
255     h = img->comps[0].h;
256     hr = int_ceildivpow2(img->comps[0].h, img->comps[0].factor);
257     
258     fprintf(f, "BM");
259     
260     /* FILE HEADER */
261     /* ------------- */
262     fprintf(f, "%c%c%c%c",
263       (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
264       54) & 0xff,
265       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
266       >> 8) & 0xff,
267       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
268       >> 16) & 0xff,
269       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
270       >> 24) & 0xff);
271     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
272       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
273     fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
274       ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
275     
276     /* INFO HEADER   */
277     /* ------------- */
278     fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
279       ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
280     fprintf(f, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
281       (unsigned char) ((wr) >> 8) & 0xff,
282       (unsigned char) ((wr) >> 16) & 0xff,
283       (unsigned char) ((wr) >> 24) & 0xff);
284     fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
285       (unsigned char) ((hr) >> 8) & 0xff,
286       (unsigned char) ((hr) >> 16) & 0xff,
287       (unsigned char) ((hr) >> 24) & 0xff);
288     fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
289     fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
290     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
291       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
292     fprintf(f, "%c%c%c%c",
293       (unsigned char) (3 * hr * wr +
294       3 * hr * (wr % 2)) & 0xff,
295       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
296       8) & 0xff,
297       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
298       16) & 0xff,
299       (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
300       24) & 0xff);
301     fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
302       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
303     fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
304       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
305     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
306       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
307     fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
308       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
309     
310     for (i = 0; i < wr * hr; i++) {
311       unsigned char R, G, B;
312       /* a modifier */
313       // R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
314       R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
315       // G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
316       G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
317       // B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
318       B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
319       fprintf(f, "%c%c%c", B, G, R);
320       
321       if ((i + 1) % wr == 0) {
322                                 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
323                                         fprintf(f, "%c", 0);
324       }
325     }
326     fclose(f);
327     opj_free(img->comps[1].data);
328     opj_free(img->comps[2].data);
329   }
330   return 0;
331 }