Fixed MJ2 codec bugs (issues 23-24 on google code). Thanks to Winfried for these...
[openjpeg.git] / mj2 / wrap_j2k_in_mj2.c
index dd2af757bd57c65b4e936afdbd360d3a8fe4af71..7615cfde73c3ccf59387fef0ece52cce042e6e22 100644 (file)
@@ -78,7 +78,7 @@ static void read_siz_marker(FILE *file, opj_image_t *image)
 {
   int len,i;
   char buf, buf2[2];
-  char *siz_buffer;
+  unsigned char *siz_buffer;
        opj_cio_t *cio;
   
   fseek(file, 0, SEEK_SET);
@@ -92,7 +92,7 @@ static void read_siz_marker(FILE *file, opj_image_t *image)
   fread(buf2,2,1,file);                /* Lsiz                */
   len = ((buf2[0])<<8) + buf2[1];
   
-  siz_buffer = (char*) malloc(len * sizeof(char));
+  siz_buffer = (unsigned char*) malloc(len * sizeof(unsigned char));
   fread(siz_buffer,len, 1, file);
        cio = opj_cio_open(NULL, siz_buffer, len);
   
@@ -119,6 +119,7 @@ static void read_siz_marker(FILE *file, opj_image_t *image)
   }
   fseek(file, 0, SEEK_SET);
        opj_cio_close(cio);
+  free(siz_buffer);
 }
 
 static void setparams(opj_mj2_t *movie, opj_image_t *image) {
@@ -132,16 +133,26 @@ static void setparams(opj_mj2_t *movie, opj_image_t *image) {
   movie->tk[0].depth = image->comps[0].prec;
        
   if (image->numcomps==3) {
-    if ((image->comps[0].dx == 1) && (image->comps[1].dx == 1) && (image->comps[1].dx == 1)) 
+    if ((image->comps[0].dx == 1) 
+       && (image->comps[1].dx == 1) 
+       && (image->comps[2].dx == 1)) 
       movie->tk[0].CbCr_subsampling_dx = 1;
-    else if ((image->comps[0].dx == 1) && (image->comps[1].dx == 2) && (image->comps[1].dx == 2))
+    else 
+       if ((image->comps[0].dx == 1) 
+       && (image->comps[1].dx == 2) 
+       && (image->comps[2].dx == 2))
       movie->tk[0].CbCr_subsampling_dx = 2;
     else
       fprintf(stderr,"Image component sizes are incoherent\n");
     
-    if ((image->comps[0].dy == 1) && (image->comps[1].dy == 1) && (image->comps[1].dy == 1)) 
+    if ((image->comps[0].dy == 1) 
+       && (image->comps[1].dy == 1) 
+       && (image->comps[2].dy == 1)) 
       movie->tk[0].CbCr_subsampling_dy = 1;
-    else if ((image->comps[0].dy == 1) && (image->comps[1].dy == 2) && (image->comps[1].dy == 2))
+    else 
+       if ((image->comps[0].dy == 1) 
+       && (image->comps[1].dy == 2) 
+       && (image->comps[2].dy == 2))
       movie->tk[0].CbCr_subsampling_dy = 2;
     else
       fprintf(stderr,"Image component sizes are incoherent\n");
@@ -189,16 +200,26 @@ static void setparams(opj_mj2_t *movie, opj_image_t *image) {
   else
     movie->tk[0].jp2_struct.meth = 2;
        
-  if (image->numcomps == 1)
-    movie->tk[0].jp2_struct.enumcs = 17;  // Grayscale
+    if (image->numcomps == 1)
+     movie->tk[0].jp2_struct.enumcs = 17;  // Grayscale
   
-  else   if ((image->comps[0].dx == 1) && (image->comps[1].dx == 1) && (image->comps[1].dx == 1) &&
-    (image->comps[0].dy == 1) && (image->comps[1].dy == 1) && (image->comps[1].dy == 1)) 
-    movie->tk[0].jp2_struct.enumcs = 16;    // RGB
+    else   
+       if ((image->comps[0].dx == 1) 
+       && (image->comps[1].dx == 1) 
+       && (image->comps[2].dx == 1) 
+    && (image->comps[0].dy == 1) 
+       && (image->comps[1].dy == 1) 
+       && (image->comps[2].dy == 1)) 
+     movie->tk[0].jp2_struct.enumcs = 16;    // RGB
   
-  else   if ((image->comps[0].dx == 1) && (image->comps[1].dx == 2) && (image->comps[1].dx == 2) &&
-    (image->comps[0].dy == 1) && (image->comps[1].dy == 2) && (image->comps[1].dy == 2)) 
-    movie->tk[0].jp2_struct.enumcs = 18;  // YUV
+    else   
+       if ((image->comps[0].dx == 1) 
+       && (image->comps[1].dx == 2) 
+       && (image->comps[2].dx == 2) 
+    && (image->comps[0].dy == 1) 
+       && (image->comps[1].dy == 2) 
+       && (image->comps[2].dy == 2)) 
+     movie->tk[0].jp2_struct.enumcs = 18;  // YUV
   
   else
     movie->tk[0].jp2_struct.enumcs = 0;        // Unkown profile */
@@ -213,7 +234,7 @@ int main(int argc, char *argv[]) {
   unsigned char* frame_codestream;
   FILE *mj2file, *j2kfile;
   char j2kfilename[50];
-  char *buf;
+  unsigned char *buf;
   int offset, mdat_initpos;
   opj_image_t img;
        opj_cio_t *cio;
@@ -249,13 +270,13 @@ int main(int argc, char *argv[]) {
        
        /* setup the decoder encoding parameters using user parameters */
        movie = (opj_mj2_t*) cinfo->mj2_handle;
-       mj2_setup_encoder(cinfo->mj2_handle, &parameters);
+       mj2_setup_encoder((opj_mj2_t*)cinfo->mj2_handle, &parameters);
 
   
        /* Writing JP, FTYP and MDAT boxes 
        Assuming that the JP and FTYP boxes won't be longer than 300 bytes */
        
-  buf = (char*) malloc (300 * sizeof(char)); 
+  buf = (unsigned char*) malloc (300 * sizeof(unsigned char)); 
   cio = opj_cio_open(movie->cinfo, buf, 300);
   mj2_write_jp(cio);
   mj2_write_ftyp(movie, cio);
@@ -312,14 +333,16 @@ int main(int argc, char *argv[]) {
     // Ending loop
     fclose(j2kfile);
     snum++;
-    movie->tk[0].sample = realloc(movie->tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
-    movie->tk[0].chunk = realloc(movie->tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
+    movie->tk[0].sample = (mj2_sample_t*)
+               realloc(movie->tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
+    movie->tk[0].chunk = (mj2_chunk_t*)
+               realloc(movie->tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
     free(frame_codestream);
   }
   
   // Writing the MDAT box length in header
   offset += cio_tell(cio);
-  buf = (char*) malloc (4 * sizeof(char));
+  buf = (unsigned char*) malloc (4 * sizeof(unsigned char));
        cio = opj_cio_open(movie->cinfo, buf, 4);
   cio_write(cio,offset-mdat_initpos,4); 
   fseek(mj2file,(long)mdat_initpos,SEEK_SET);
@@ -333,7 +356,7 @@ int main(int argc, char *argv[]) {
   setparams(movie, &img);
        
   // Writing MOOV box 
-       buf = (char*) malloc ((TEMP_BUF+snum*20) * sizeof(char));
+       buf = (unsigned char*) malloc ((TEMP_BUF+snum*20) * sizeof(unsigned char));
        cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+snum*20));
        mj2_write_moov(movie, cio);
   fwrite(buf,cio_tell(cio),1,mj2file);