2 * Copyright (c) 2003-2004, Fran�ois-Olivier Devaux
3 * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
38 // -->> -->> -->> -->>
42 //-- <<-- <<-- <<-- */
45 /* ----------------------- */
48 /* Count the number of frames */
51 /* ----------------------- */
53 int yuv_num_frames(mj2_tk_t * tk)
56 int numimages, frame_size;
59 f = fopen(tk->imagefile, "rb");
61 fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile);
65 frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */
67 fseek(f, 0, SEEK_END);
68 end_of_f = ftell(f); /* Calculate file size */
70 if (end_of_f < frame_size) {
72 "YUV does not contains any frame of %d x %d size\n", tk->w,
77 numimages = end_of_f / frame_size; /* Calculate number of images */
83 // -----------------------
88 // -----------------------
90 int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num)
95 long end_of_f, position;
100 f = fopen(tk->imagefile, "rb");
102 fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile);
107 (int) ((double) (frame_num * tk->w * tk->h) *
109 1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx *
110 tk->CbCr_subsampling_dy)));
111 fseek(f, 0, SEEK_END);
113 fseek(f, sizeof(unsigned char) * offset, SEEK_SET);
115 if (position >= end_of_f) {
116 fprintf(stderr, "Cannot reach frame number %d in %s file !!\n",
117 frame_num, tk->imagefile);
121 img->x0 = tk->Dim[0];
122 img->y0 = tk->Dim[1];
124 !tk->Dim[0] ? (tk->w - 1) * tk->subsampling_dx + 1 : tk->Dim[0] +
125 (tk->w - 1) * tk->subsampling_dx + 1;
127 !tk->Dim[1] ? (tk->h - 1) * tk->subsampling_dy + 1 : tk->Dim[1] +
128 (tk->h - 1) * tk->subsampling_dy + 1;
130 img->color_space = 3;
131 img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
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;
139 img->comps[i].dx = tk->subsampling_dx;
140 img->comps[i].dy = tk->subsampling_dy;
142 img->comps[i].dx = tk->subsampling_dx * tk->CbCr_subsampling_dx;
143 img->comps[i].dy = tk->subsampling_dy * tk->CbCr_subsampling_dy;
147 Compo = fopen("Compo0", "wb");
149 fprintf(stderr, "Failed to open Compo0 for writing !\n");
152 for (i = 0; i < (tk->w * tk->h / (img->comps[0].dx * img->comps[0].dy))
155 j = fread(&y, 1, 1, f);
156 fwrite(&y, 1, 1, Compo);
161 Compo = fopen("Compo1", "wb");
163 fprintf(stderr, "Failed to open Compo1 for writing !\n");
167 for (i = 0; i < (tk->w * tk->h / (img->comps[1].dx * img->comps[1].dy))
170 j = fread(&cb, sizeof(unsigned char), 1, f);
171 fwrite(&cb, 1, 1, Compo);
176 Compo = fopen("Compo2", "wb");
178 fprintf(stderr, "Failed to open Compo2 for writing !\n");
182 for (i = 0; i < (tk->w * tk->h / (img->comps[2].dx * img->comps[2].dy))
185 j = fread(&cr, sizeof(unsigned char), 1, f);
186 fwrite(&cr, 1, 1, Compo);
195 // -----------------------
200 // -----------------------
203 int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
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) {
212 "Error with the input image components size: cannot create yuv file)\n");
215 } else if (!(img->numcomps == 1)) {
217 "Error with the number of image components(must be one or three)\n");
221 f = fopen(outfile, "a+b");
223 fprintf(stderr, "failed to open %s for writing\n", outfile);
228 for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) {
230 y = img->comps[0].data[i];
235 if (img->numcomps == 3) {
236 for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) {
238 cb = img->comps[1].data[i];
239 fwrite(&cb, 1, 1, f);
243 for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) {
245 cr = img->comps[2].data[i];
246 fwrite(&cr, 1, 1, f);
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);
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);