[trunk] Fix a set of warnings about C90 issues
[openjpeg.git] / src / bin / mj2 / opj_mj2_wrap.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 
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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "openjpeg.h"
34 #include "cio.h"
35 #include "j2k.h"
36 #include "jp2.h"
37 #include "mj2.h"
38
39 static int int_ceildiv(int a, int b) {
40         return (a + b - 1) / b;
41 }
42
43 /**
44 Size of memory first allocated for MOOV box
45 */
46 #define TEMP_BUF 10000 
47
48 #define ENUMCS_GRAY 16
49 #define ENUMCS_SRGB 17
50 #define ENUMCS_SYCC 18
51
52 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
53
54 /* -------------------------------------------------------------------------- */
55
56 static int test_image(const char *fname, mj2_cparameters_t *cp)
57 {
58         FILE *reader;
59         opj_image_t *image;
60         unsigned char *src;
61         opj_dinfo_t *dinfo;
62         opj_cio_t *cio;
63         opj_dparameters_t dparameters;
64         int success;
65         long src_len;
66
67         success = 0;
68
69         if((reader = fopen(fname, "rb")) == NULL) return success;
70
71         fseek(reader, 0, SEEK_END);
72         src_len = ftell(reader);
73         fseek(reader, 0, SEEK_SET);
74         src = (unsigned char*) malloc(src_len);
75         fread(src, 1, src_len, reader);
76         fclose(reader);
77
78         if(memcmp(src, J2K_CODESTREAM_MAGIC, 4) != 0)
79    {
80         free(src); return success;
81    }
82         memset(&dparameters, 0, sizeof(opj_dparameters_t));
83
84         opj_set_default_decoder_parameters(&dparameters);
85
86         dinfo = opj_create_decompress(CODEC_J2K);
87
88         opj_setup_decoder(dinfo, &dparameters);
89
90         cio = opj_cio_open((opj_common_ptr)dinfo, src, src_len);
91
92 #if 0 /* MM: FIXME */
93         image = opj_decode(dinfo, cio);
94 #endif
95
96         free(src); cio->buffer = NULL;
97         opj_cio_close(cio);
98
99         if(image == NULL) goto fin;
100
101         cp->numcomps = image->numcomps;
102         cp->w = image->comps[0].w;
103         cp->h = image->comps[0].h;
104         cp->prec = image->comps[0].prec;
105
106         if(image->numcomps > 2 )
107    {
108         if((image->comps[0].dx == 1)
109         && (image->comps[1].dx == 2)
110         && (image->comps[2].dx == 2)
111         && (image->comps[0].dy == 1)
112         && (image->comps[1].dy == 2)
113         && (image->comps[2].dy == 2))/* horizontal and vertical*/
114   {
115 /*   Y420*/
116         cp->enumcs = ENUMCS_SYCC;
117         cp->CbCr_subsampling_dx = 2;
118         cp->CbCr_subsampling_dy = 2;
119   }
120         else
121         if((image->comps[0].dx == 1)
122         && (image->comps[1].dx == 2)
123         && (image->comps[2].dx == 2)
124         && (image->comps[0].dy == 1)
125         && (image->comps[1].dy == 1)
126         && (image->comps[2].dy == 1))/* horizontal only*/
127   {
128 /*   Y422*/
129         cp->enumcs = ENUMCS_SYCC;
130         cp->CbCr_subsampling_dx = 2;
131         cp->CbCr_subsampling_dy = 1;
132   }
133         else
134         if((image->comps[0].dx == 1)
135         && (image->comps[1].dx == 1)
136         && (image->comps[2].dx == 1)
137         && (image->comps[0].dy == 1)
138         && (image->comps[1].dy == 1)
139         && (image->comps[2].dy == 1))
140   {
141 /*   Y444 or RGB */
142
143         if(image->color_space ==  CLRSPC_SRGB)
144  {
145         cp->enumcs = ENUMCS_SRGB;
146
147 /*    cp->CbCr_subsampling_dx = 0; */
148 /*    cp->CbCr_subsampling_dy = 0; */
149  }
150         else
151  {
152         cp->enumcs = ENUMCS_SYCC;
153
154         cp->CbCr_subsampling_dx = 1;
155         cp->CbCr_subsampling_dy = 1;
156  }
157   }
158         else
159   {
160         goto fin;
161   }
162    }
163         else
164    {
165         cp->enumcs = ENUMCS_GRAY;
166 /*  cp->CbCr_subsampling_dx = 0; */
167 /*  cp->CbCr_subsampling_dy = 0; */
168    }
169         if(image->icc_profile_buf)
170    {
171         cp->meth = 2;
172         free(image->icc_profile_buf); image->icc_profile_buf = NULL;
173    }
174         else cp->meth = 1;
175
176         success = 1;
177 fin:
178         if(dinfo)
179          opj_destroy_decompress(dinfo);
180
181         if(image)
182          opj_image_destroy(image);
183
184         return success;
185 }
186
187 /**
188 sample error callback expecting a FILE* client object
189 */
190 void error_callback(const char *msg, void *client_data) {
191         FILE *stream = (FILE*)client_data;
192         fprintf(stream, "[ERROR] %s", msg);
193 }
194 /**
195 sample warning callback expecting a FILE* client object
196 */
197 void warning_callback(const char *msg, void *client_data) {
198         FILE *stream = (FILE*)client_data;
199         fprintf(stream, "[WARNING] %s", msg);
200 }
201 /**
202 sample debug callback expecting a FILE* client object
203 */
204 void info_callback(const char *msg, void *client_data) {
205         FILE *stream = (FILE*)client_data;
206         fprintf(stream, "[INFO] %s", msg);
207 }
208
209 /* -------------------------------------------------------------------------- */
210
211
212
213 static void read_siz_marker(FILE *file, opj_image_t *image)
214 {
215   int len,i;
216   char buf, buf2[2];
217   unsigned char *siz_buffer;
218         opj_cio_t *cio;
219   
220   fseek(file, 0, SEEK_SET);
221   do {
222     fread(&buf,1,1, file);
223     if (buf==(char)0xff)
224       fread(&buf,1,1, file);
225   }
226   while (!(buf==(char)0x51));
227   
228   fread(buf2,2,1,file);         /* Lsiz                */
229   len = ((buf2[0])<<8) + buf2[1];
230   
231   siz_buffer = (unsigned char*) malloc(len * sizeof(unsigned char));
232   fread(siz_buffer,len, 1, file);
233         cio = opj_cio_open(NULL, siz_buffer, len);
234   
235   cio_read(cio, 2);                     /* Rsiz (capabilities) */
236   image->x1 = cio_read(cio, 4); /* Xsiz                */
237   image->y1 = cio_read(cio, 4); /* Ysiz                */
238   image->x0 = cio_read(cio, 4); /* X0siz               */
239   image->y0 = cio_read(cio, 4); /* Y0siz               */
240   cio_skip(cio, 16);                    /* XTsiz, YTsiz, XT0siz, YT0siz        */
241   
242   image->numcomps = cio_read(cio,2);    /* Csiz                */
243   image->comps =
244     (opj_image_comp_t *) malloc(image->numcomps * sizeof(opj_image_comp_t));
245         
246   for (i = 0; i < image->numcomps; i++) {
247     int tmp;
248     tmp = cio_read(cio,1);              /* Ssiz_i          */
249     image->comps[i].prec = (tmp & 0x7f) + 1;
250     image->comps[i].sgnd = tmp >> 7;
251     image->comps[i].dx = cio_read(cio,1);       /* XRsiz_i         */
252     image->comps[i].dy = cio_read(cio,1);       /* YRsiz_i         */
253     image->comps[i].resno_decoded = 0;  /* number of resolution decoded */
254     image->comps[i].factor = 0; /* reducing factor by component */
255   }
256   fseek(file, 0, SEEK_SET);
257         opj_cio_close(cio);
258   free(siz_buffer);
259 }
260
261 static void setparams(opj_mj2_t *movie, opj_image_t *image) {
262   int i, depth_0, depth, sign;
263   
264   movie->tk[0].w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
265   movie->tk[0].h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
266   mj2_init_stdmovie(movie);
267   
268   movie->tk[0].depth = image->comps[0].prec;
269         
270   if (image->numcomps==3) {
271     if ((image->comps[0].dx == 1) 
272         && (image->comps[1].dx == 1) 
273         && (image->comps[2].dx == 1)) 
274       movie->tk[0].CbCr_subsampling_dx = 1;
275     else 
276         if ((image->comps[0].dx == 1) 
277         && (image->comps[1].dx == 2) 
278         && (image->comps[2].dx == 2))
279       movie->tk[0].CbCr_subsampling_dx = 2;
280     else
281       fprintf(stderr,"Image component sizes are incoherent\n");
282     
283     if ((image->comps[0].dy == 1) 
284         && (image->comps[1].dy == 1) 
285         && (image->comps[2].dy == 1)) 
286       movie->tk[0].CbCr_subsampling_dy = 1;
287     else 
288         if ((image->comps[0].dy == 1) 
289         && (image->comps[1].dy == 2) 
290         && (image->comps[2].dy == 2))
291       movie->tk[0].CbCr_subsampling_dy = 2;
292     else
293       fprintf(stderr,"Image component sizes are incoherent\n");
294   }
295   
296   movie->tk[0].sample_rate = 25;
297   
298   movie->tk[0].jp2_struct.numcomps = image->numcomps;   /* NC */
299         
300         /* Init Standard jp2 structure */
301         
302         movie->tk[0].jp2_struct.comps =
303     (opj_jp2_comps_t *) malloc(movie->tk[0].jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
304   movie->tk[0].jp2_struct.precedence = 0;   /* PRECEDENCE*/
305   movie->tk[0].jp2_struct.approx = 0;   /* APPROX*/
306   movie->tk[0].jp2_struct.brand = JP2_JP2;      /* BR         */
307   movie->tk[0].jp2_struct.minversion = 0;       /* MinV       */
308   movie->tk[0].jp2_struct.numcl = 1;
309   movie->tk[0].jp2_struct.cl = (unsigned int *) malloc(movie->tk[0].jp2_struct.numcl * sizeof(int));
310   movie->tk[0].jp2_struct.cl[0] = JP2_JP2;      /* CL0 : JP2  */
311   movie->tk[0].jp2_struct.C = 7;      /* C : Always 7*/
312   movie->tk[0].jp2_struct.UnkC = 0;      /* UnkC, colorspace specified in colr box*/
313   movie->tk[0].jp2_struct.IPR = 0;      /* IPR, no intellectual property*/
314   movie->tk[0].jp2_struct.w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
315   movie->tk[0].jp2_struct.h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
316   
317   depth_0 = image->comps[0].prec - 1;
318   sign = image->comps[0].sgnd;
319   movie->tk[0].jp2_struct.bpc = depth_0 + (sign << 7);
320   
321   for (i = 1; i < image->numcomps; i++) {
322     depth = image->comps[i].prec - 1;
323     sign = image->comps[i].sgnd;
324     if (depth_0 != depth)
325       movie->tk[0].jp2_struct.bpc = 255;
326   }
327   
328   for (i = 0; i < image->numcomps; i++)
329     movie->tk[0].jp2_struct.comps[i].bpcc =
330     image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
331   
332   if ((image->numcomps == 1 || image->numcomps == 3)
333     && (movie->tk[0].jp2_struct.bpc != 255))
334     movie->tk[0].jp2_struct.meth = 1;
335   else
336     movie->tk[0].jp2_struct.meth = 2;
337         
338     if (image->numcomps == 1)
339      movie->tk[0].jp2_struct.enumcs = 17;  /* Grayscale */
340   
341     else   
342         if ((image->comps[0].dx == 1) 
343         && (image->comps[1].dx == 1) 
344         && (image->comps[2].dx == 1) 
345     && (image->comps[0].dy == 1) 
346         && (image->comps[1].dy == 1) 
347         && (image->comps[2].dy == 1)) 
348      movie->tk[0].jp2_struct.enumcs = 16;    /* RGB */
349   
350     else   
351         if ((image->comps[0].dx == 1) 
352         && (image->comps[1].dx == 2) 
353         && (image->comps[2].dx == 2) 
354     && (image->comps[0].dy == 1) 
355         && (image->comps[1].dy == 2) 
356         && (image->comps[2].dy == 2)) 
357      movie->tk[0].jp2_struct.enumcs = 18;  /* YUV */
358   
359   else
360     movie->tk[0].jp2_struct.enumcs = 0; /* Unkown profile */
361 }
362
363 int main(int argc, char *argv[]) {
364         opj_cinfo_t* cinfo; 
365         opj_event_mgr_t event_mgr;              /* event manager */  
366   unsigned int snum;
367   opj_mj2_t *movie;
368   mj2_sample_t *sample;
369   unsigned char* frame_codestream;
370   FILE *mj2file, *j2kfile;
371   char *j2kfilename;
372   unsigned char *buf;
373   int offset, mdat_initpos;
374   opj_image_t img;
375         opj_cio_t *cio;
376         mj2_cparameters_t parameters;
377         
378   if (argc != 3) {
379     printf("Usage: %s source_location mj2_filename\n",argv[0]);
380     printf("Example: %s input/input output.mj2\n",argv[0]);
381     return 1;
382   }
383   
384   mj2file = fopen(argv[2], "wb");
385   
386   if (!mj2file) {
387     fprintf(stderr, "failed to open %s for writing\n", argv[2]);
388     return 1;
389   }
390         memset(&img, 0, sizeof(opj_image_t));
391         /*
392         configure the event callbacks (not required)
393         setting of each callback is optionnal
394         */
395         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
396         event_mgr.error_handler = error_callback;
397         event_mgr.warning_handler = warning_callback;
398         event_mgr.info_handler = info_callback;
399
400         /* get a MJ2 decompressor handle */
401         cinfo = mj2_create_compress();
402
403         /* catch events using our callbacks and give a local context */
404         opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);   
405         
406         /* setup the decoder encoding parameters using user parameters */
407         memset(&parameters, 0, sizeof(mj2_cparameters_t));
408         movie = (opj_mj2_t*) cinfo->mj2_handle;
409
410         j2kfilename = (char*)malloc(strlen(argv[1]) + 12);/* max. '%6d' */
411         sprintf(j2kfilename, "%s_00001.j2k",argv[1]);
412
413         if(test_image(j2kfilename, &parameters) == 0) goto fin;
414
415         parameters.frame_rate = 25; /* DEFAULT */
416
417         mj2_setup_encoder(movie, &parameters);
418
419   
420         /* Writing JP, FTYP and MDAT boxes 
421         Assuming that the JP and FTYP boxes won't be longer than 300 bytes */
422         
423   buf = (unsigned char*) malloc (300 * sizeof(unsigned char)); 
424   cio = opj_cio_open(movie->cinfo, buf, 300);
425   mj2_write_jp(cio);
426   mj2_write_ftyp(movie, cio);
427   mdat_initpos = cio_tell(cio);
428   cio_skip(cio, 4);
429   cio_write(cio,MJ2_MDAT, 4);   
430   fwrite(buf,cio_tell(cio),1,mj2file);
431   free(buf);
432         
433   /* Insert each j2k codestream in a JP2C box */
434   snum=0;
435   offset = 0;  
436   while(1)
437   {
438     mj2_sample_t * new_sample;
439     mj2_chunk_t * new_chunk;
440     sample = &movie->tk[0].sample[snum];
441     sprintf(j2kfilename,"%s_%05d.j2k",argv[1],snum);
442     j2kfile = fopen(j2kfilename, "rb");
443     if (!j2kfile) {
444       if (snum==0) {  /* Could not open a single codestream */
445                                 fprintf(stderr, "failed to open %s for reading\n",j2kfilename);
446                                 return 1;
447       }
448       else {          /* Tried to open a inexistant codestream */
449                                 fprintf(stdout,"%d frames are being added to the MJ2 file\n",snum);
450                                 break;
451       }
452     }
453
454     /* Calculating offset for samples and chunks */
455     offset += cio_tell(cio);     
456     sample->offset = offset;
457     movie->tk[0].chunk[snum].offset = offset;  /* There will be one sample per chunk */
458     
459     /* Calculating sample size */
460     fseek(j2kfile,0,SEEK_END);  
461     sample->sample_size = ftell(j2kfile) + 8; /* Sample size is codestream + JP2C box header */
462     fseek(j2kfile,0,SEEK_SET);
463     
464     /* Reading siz marker of j2k image for the first codestream */
465     if (snum==0)              
466       read_siz_marker(j2kfile, &img);
467     
468     /* Writing JP2C box header */
469     frame_codestream = (unsigned char*) malloc (sample->sample_size+8); 
470                 cio = opj_cio_open(movie->cinfo, frame_codestream, sample->sample_size);    
471     cio_write(cio,sample->sample_size, 4);  /* Sample size */
472     cio_write(cio,JP2_JP2C, 4); /* JP2C */
473     
474     /* Writing codestream from J2K file to MJ2 file */
475     fread(frame_codestream+8,sample->sample_size-8,1,j2kfile);
476     fwrite(frame_codestream,sample->sample_size,1,mj2file);
477     cio_skip(cio, sample->sample_size-8);
478     
479     /* Ending loop */
480     fclose(j2kfile);
481     snum++;
482     new_sample = (mj2_sample_t*)
483                 realloc(movie->tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
484     new_chunk = (mj2_chunk_t*)
485                 realloc(movie->tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
486     if (new_sample && new_chunk) {
487         movie->tk[0].sample = new_sample;
488         movie->tk[0].chunk = new_chunk;
489     } else {
490        fprintf(stderr, "Failed to allocate enough memory to read %s\n", j2kfilename);
491        return 1;
492     }
493     free(frame_codestream);
494   }
495   
496   /* Writing the MDAT box length in header */
497   offset += cio_tell(cio);
498   buf = (unsigned char*) malloc (4 * sizeof(unsigned char));
499         cio = opj_cio_open(movie->cinfo, buf, 4);
500   cio_write(cio,offset-mdat_initpos,4); 
501   fseek(mj2file,(long)mdat_initpos,SEEK_SET);
502   fwrite(buf,4,1,mj2file);
503   fseek(mj2file,0,SEEK_END);
504   free(buf);
505         
506   /* Setting movie parameters */
507   movie->tk[0].num_samples=snum;
508   movie->tk[0].num_chunks=snum;
509   setparams(movie, &img);
510         
511   /* Writing MOOV box */
512         buf = (unsigned char*) malloc ((TEMP_BUF+snum*20) * sizeof(unsigned char));
513         cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+snum*20));
514         mj2_write_moov(movie, cio);
515   fwrite(buf,cio_tell(cio),1,mj2file);
516         
517   /* Ending program */
518   free(img.comps);
519   opj_cio_close(cio);
520
521 fin:
522   fclose(mj2file);
523   mj2_destroy_compress(movie);
524   free(j2kfilename);
525
526   return 0;
527 }