Memory leak bug fixed in the read_mdat() function
[openjpeg.git] / mj2 / mj2_convert.c
1 /*
2 * Copyright (c) 2003-2004, Fran�ois-Olivier Devaux
3 * Copyright (c) 2003-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <openjpeg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #ifndef WIN32
33 #include <unistd.h>
34 #endif
35 #include <string.h>
36 #include "mj2.h"
37
38 // -->> -->> -->> -->>
39
40 //  YUV IMAGE FORMAT
41
42 //-- <<-- <<-- <<-- */
43
44
45 /*  -----------------------           */
46 /*                                    */
47 /*                                    */
48 /*  Count the number of frames        */
49 /*  in a YUV file                     */
50 /*                                    */
51 /*  -----------------------           */
52
53 int yuv_num_frames(mj2_tk_t * tk)
54 {
55   FILE *f;
56   int numimages, frame_size;
57   long end_of_f;
58
59   f = fopen(tk->imagefile, "rb");
60   if (!f) {
61     fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile);
62     return 0;
63   }
64
65   frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));       /* Calculate frame size */
66
67   fseek(f, 0, SEEK_END);
68   end_of_f = ftell(f);          /* Calculate file size */
69
70   if (end_of_f < frame_size) {
71     fprintf(stderr,
72             "YUV does not contains any frame of %d x %d size\n", tk->w,
73             tk->h);
74     return 0;
75   }
76
77   numimages = end_of_f / frame_size;    /* Calculate number of images */
78
79   return numimages;
80   fclose(f);
81 }
82
83 //  -----------------------
84 //
85 //
86 //  YUV to IMAGE
87 //
88 //  -----------------------
89
90 int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num)
91 {
92   FILE *f;
93   int i, j;
94   int offset;
95   long end_of_f, position;
96   FILE *Compo;
97
98
99
100   f = fopen(tk->imagefile, "rb");
101   if (!f) {
102     fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile);
103     return 0;
104   }
105
106   offset =
107     (int) ((double) (frame_num * tk->w * tk->h) *
108            (1.0 +
109             1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx *
110                                          tk->CbCr_subsampling_dy)));
111   fseek(f, 0, SEEK_END);
112   end_of_f = ftell(f);
113   fseek(f, sizeof(unsigned char) * offset, SEEK_SET);
114   position = ftell(f);
115   if (position >= end_of_f) {
116     fprintf(stderr, "Cannot reach frame number %d in %s file !!\n",
117             frame_num, tk->imagefile);
118     return 0;
119   }
120
121   img->x0 = tk->Dim[0];
122   img->y0 = tk->Dim[1];
123   img->x1 =
124     !tk->Dim[0] ? (tk->w - 1) * tk->subsampling_dx + 1 : tk->Dim[0] +
125     (tk->w - 1) * tk->subsampling_dx + 1;
126   img->y1 =
127     !tk->Dim[1] ? (tk->h - 1) * tk->subsampling_dy + 1 : tk->Dim[1] +
128     (tk->h - 1) * tk->subsampling_dy + 1;
129   img->numcomps = 3;
130   img->color_space = 3;
131   img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
132
133   for (i = 0; i < img->numcomps; i++) {
134     img->comps[i].data = (int *) malloc(sizeof(int) * tk->w * tk->h);
135     img->comps[i].prec = 8;
136     img->comps[i].bpp = 8;
137     img->comps[i].sgnd = 0;
138     if (i == 0) {
139       img->comps[i].dx = tk->subsampling_dx;
140       img->comps[i].dy = tk->subsampling_dy;
141     } else {
142       img->comps[i].dx = tk->subsampling_dx * tk->CbCr_subsampling_dx;
143       img->comps[i].dy = tk->subsampling_dy * tk->CbCr_subsampling_dy;
144     }
145   }
146
147   Compo = fopen("Compo0", "wb");
148   if (!Compo) {
149     fprintf(stderr, "Failed to open Compo0 for writing !\n");
150   }
151
152   for (i = 0; i < (tk->w * tk->h / (img->comps[0].dx * img->comps[0].dy))
153        && !feof(f); i++) {
154     unsigned char y;
155     j = fread(&y, 1, 1, f);
156     fwrite(&y, 1, 1, Compo);
157   }
158
159   fclose(Compo);
160
161   Compo = fopen("Compo1", "wb");
162   if (!Compo) {
163     fprintf(stderr, "Failed to open Compo1 for writing !\n");
164   }
165
166
167   for (i = 0; i < (tk->w * tk->h / (img->comps[1].dx * img->comps[1].dy))
168        && !feof(f); i++) {
169     unsigned char cb;
170     j = fread(&cb, sizeof(unsigned char), 1, f);
171     fwrite(&cb, 1, 1, Compo);
172   }
173
174   fclose(Compo);
175
176   Compo = fopen("Compo2", "wb");
177   if (!Compo) {
178     fprintf(stderr, "Failed to open Compo2 for writing !\n");
179   }
180
181
182   for (i = 0; i < (tk->w * tk->h / (img->comps[2].dx * img->comps[2].dy))
183        && !feof(f); i++) {
184     unsigned char cr;
185     j = fread(&cr, sizeof(unsigned char), 1, f);
186     fwrite(&cr, 1, 1, Compo);
187   }
188
189   fclose(Compo);
190
191   return 1;
192 }
193
194
195 //  -----------------------
196 //
197 //
198 //  IMAGE to YUV
199 //
200 //  -----------------------
201
202
203 int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
204 {
205   FILE *f;
206   int i;
207
208   if (img->numcomps == 3) {
209     if (img->comps[0].dx != img->comps[1].dx / 2
210         || img->comps[1].dx != img->comps[2].dx) {
211       fprintf(stderr,
212               "Error with the input image components size: cannot create yuv file)\n");
213       return 1;
214     }
215   } else if (!(img->numcomps == 1)) {
216     fprintf(stderr,
217             "Error with the number of image components(must be one or three)\n");
218     return 1;
219   }
220
221   f = fopen(outfile, "a+b");
222   if (!f) {
223     fprintf(stderr, "failed to open %s for writing\n", outfile);
224     return 1;
225   }
226
227
228   for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) {
229     unsigned char y;
230     y = img->comps[0].data[i];
231     fwrite(&y, 1, 1, f);
232   }
233
234
235   if (img->numcomps == 3) {
236     for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) {
237       unsigned char cb;
238       cb = img->comps[1].data[i];
239       fwrite(&cb, 1, 1, f);
240     }
241
242
243     for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) {
244       unsigned char cr;
245       cr = img->comps[2].data[i];
246       fwrite(&cr, 1, 1, f);
247     }
248   } else if (img->numcomps == 1) {
249     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
250       unsigned char cb = 125;
251       fwrite(&cb, 1, 1, f);
252     }
253
254
255     for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
256       unsigned char cr = 125;
257       fwrite(&cr, 1, 1, f);
258     }
259   }
260
261
262
263   fclose(f);
264   return 0;
265
266
267 }