]> Creatis software - gdcm.git/blobdiff - src/gdcmmpeg2/src/mpeg2dec/store.c
New method SerieHelper::AddSeriesDetail() to allow user to specifiy
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / store.c
index 8867416a942f9901a08a0d1ca0c4c11b66def477..ae42ffb2e6fd0ad0a0b29d91a82ee034d7e679ea 100644 (file)
@@ -27,9 +27,8 @@
  *
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
+#include <stdlib.h> /* for malloc */
+#include <string.h> /* for strcat */
 
 #include "config.h"
 #include "global.h"
@@ -43,17 +42,29 @@ static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
   int offset, int incr, int height));
 static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
   int offset, int incr, int height, int tgaflag));
+static void store_in_mem _ANSI_ARGS_((char *outmem, unsigned char *src[],
+  int offset, int incr, int height));
 static void store_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
   int offset, int incr, int width, int height));
 static void putbyte _ANSI_ARGS_((int c));
 static void putword _ANSI_ARGS_((int w));
+static void mem_putbyte _ANSI_ARGS_((int c));
+/*static void mem_putword _ANSI_ARGS_((int w));*/
 static void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
 
 #define OBFRSIZE 4096
 static unsigned char obfr[OBFRSIZE];
 static unsigned char *optr;
-static int outfile;
+static ostream *outfile;
+unsigned char *static_malloc[6] = {0,0,0,0,0,0}; /*worse case there is 6 buffer in this impl unit.*/
+
+void FreeStaticBuffer()
+{
+  int i;
+  for(i=0;i<6;++i)
+    free(static_malloc[i]);
+}
 
 /*
  * store a picture as either one frame or two fields
@@ -67,16 +78,16 @@ int frame;
   if (progressive_sequence || progressive_frame || Frame_Store_Flag)
   {
     /* progressive */
-    sprintf(outname,Output_Picture_Filename,frame,'f');
+    my_sprintf(outname,Output_Picture_Filename,frame,'f');
     store_one(outname,src,0,Coded_Picture_Width,vertical_size);
   }
   else
   {
     /* interlaced */
-    sprintf(outname,Output_Picture_Filename,frame,'a');
+    my_sprintf(outname,Output_Picture_Filename,frame,'a');
     store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
 
-    sprintf(outname,Output_Picture_Filename,frame,'b');
+    my_sprintf(outname,Output_Picture_Filename,frame,'b');
     store_one(outname,src,
       Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
   }
@@ -104,6 +115,9 @@ int offset, incr, height;
   case T_PPM:
     store_ppm_tga(outname,src,offset,incr,height,0);
     break;
+  case T_MEM:
+    store_in_mem(OUTMEM,src,offset,incr,height);
+    break;
 #ifdef DISPLAY
   case T_X11:
     dither(src);
@@ -125,7 +139,7 @@ int offset,incr,height;
 
   hsize = horizontal_size;
 
-  sprintf(tmpname,"%s.Y",outname);
+  my_sprintf(tmpname,"%s.Y",outname);
   store_yuv1(tmpname,src[0],offset,incr,hsize,height);
 
   if (chroma_format!=CHROMA444)
@@ -138,10 +152,10 @@ int offset,incr,height;
     height>>=1;
   }
 
-  sprintf(tmpname,"%s.U",outname);
+  my_sprintf(tmpname,"%s.U",outname);
   store_yuv1(tmpname,src[1],offset,incr,hsize,height);
 
-  sprintf(tmpname,"%s.V",outname);
+  my_sprintf(tmpname,"%s.V",outname);
   store_yuv1(tmpname,src[2],offset,incr,hsize,height);
 }
 
@@ -153,13 +167,15 @@ int offset,incr,width,height;
 {
   int i, j;
   unsigned char *p;
+  ostream file;
 
   if (!Quiet_Flag)
-    fprintf(stderr,"saving %s\n",name);
+    my_fprintf("saving %s\n",name);
 
-  if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
+  outfile = &file;
+  if(!my_fopen(name, "wb", outfile))
   {
-    sprintf(Error_Text,"Couldn't create %s\n",name);
+    my_sprintf(Error_Text,"Couldn't create %s\n",name);
     Error(Error_Text);
   }
 
@@ -173,9 +189,9 @@ int offset,incr,width,height;
   }
 
   if (optr!=obfr)
-    write(outfile,obfr,optr-obfr);
+    my_fwrite(obfr,optr-obfr,1,outfile);
 
-  close(outfile);
+  my_fclose(outfile);
 }
 
 /*
@@ -189,6 +205,7 @@ int offset, incr, height;
   int i,j;
   unsigned char *py, *pu, *pv;
   static unsigned char *u422, *v422;
+  ostream file;
 
   if (chroma_format==CHROMA444)
     Error("4:4:4 not supported for SIF format");
@@ -205,9 +222,11 @@ int offset, incr, height;
       if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
                                            *Coded_Picture_Height)))
         Error("malloc failed");
+      static_malloc[0] = u422;
       if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
                                            *Coded_Picture_Height)))
         Error("malloc failed");
+      static_malloc[1] = v422;
     }
   
     conv420to422(src[1],u422);
@@ -217,11 +236,12 @@ int offset, incr, height;
   strcat(outname,".SIF");
 
   if (!Quiet_Flag)
-    fprintf(stderr,"saving %s\n",outname);
+    my_fprintf("saving %s\n",outname);
 
-  if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
+  outfile = &file;
+  if(!my_fopen(outname, "wb", outfile))
   {
-    sprintf(Error_Text,"Couldn't create %s\n",outname);
+    my_sprintf(Error_Text,"Couldn't create %s\n",outname);
     Error(Error_Text);
   }
 
@@ -243,9 +263,9 @@ int offset, incr, height;
   }
 
   if (optr!=obfr)
-    write(outfile,obfr,optr-obfr);
+    my_fwrite(obfr,optr-obfr,1,outfile);
 
-  close(outfile);
+  my_fclose(outfile);
 }
 
 /*
@@ -264,6 +284,7 @@ int tgaflag;
   static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
   char header[FILENAME_LENGTH];
   static unsigned char *u422, *v422, *u444, *v444;
+  ostream file;
 
   if (chroma_format==CHROMA444)
   {
@@ -279,18 +300,22 @@ int tgaflag;
         if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
                                              *Coded_Picture_Height)))
           Error("malloc failed");
+        static_malloc[2] = u422;
         if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
                                              *Coded_Picture_Height)))
           Error("malloc failed");
+        static_malloc[3] = v422;
       }
 
       if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
                                            *Coded_Picture_Height)))
         Error("malloc failed");
+      static_malloc[4] = u444;
 
       if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
                                            *Coded_Picture_Height)))
         Error("malloc failed");
+      static_malloc[5] = v444;
     }
 
     if (chroma_format==CHROMA420)
@@ -310,11 +335,12 @@ int tgaflag;
   strcat(outname,tgaflag ? ".tga" : ".ppm");
 
   if (!Quiet_Flag)
-    fprintf(stderr,"saving %s\n",outname);
+    my_fprintf("saving %s\n",outname);
 
-  if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
+  outfile = &file;
+  if(! my_fopen(outname, "wb", outfile))
   {
-    sprintf(Error_Text,"Couldn't create %s\n",outname);
+    my_sprintf(Error_Text,"Couldn't create %s\n",outname);
     Error(Error_Text);
   }
 
@@ -332,7 +358,7 @@ int tgaflag;
   else
   {
     /* PPM header */
-    sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
+    my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
 
     for (i=0; header[i]!=0; i++)
       putbyte(header[i]);
@@ -371,9 +397,152 @@ int tgaflag;
   }
 
   if (optr!=obfr)
-    write(outfile,obfr,optr-obfr);
+    my_fwrite(obfr,optr-obfr,1,outfile);
 
-  close(outfile);
+  my_fclose(outfile);
+}
+
+/*
+ * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
+ */
+static void store_in_mem(outmem,src,offset,incr,height)
+char *outmem;
+unsigned char *src[];
+int offset, incr, height;
+{
+  int i, j;
+  int y, u, v, r, g, b;
+  int crv, cbu, cgu, cgv;
+  unsigned char *py, *pu, *pv;
+  /*static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};*/
+  /*char header[FILENAME_LENGTH];*/
+  static unsigned char *u422, *v422, *u444, *v444;
+  (void)outmem;
+
+  if (chroma_format==CHROMA444)
+  {
+    u444 = src[1];
+    v444 = src[2];
+  }
+  else
+  {
+    if (!u444)
+    {
+      if (chroma_format==CHROMA420)
+      {
+        if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
+                                             *Coded_Picture_Height)))
+          Error("malloc failed");
+        static_malloc[2] = u422;
+        if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
+                                             *Coded_Picture_Height)))
+          Error("malloc failed");
+        static_malloc[3] = v422;
+      }
+
+      if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
+                                           *Coded_Picture_Height)))
+        Error("malloc failed");
+      static_malloc[4] = u444;
+
+      if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
+                                           *Coded_Picture_Height)))
+        Error("malloc failed");
+      static_malloc[5] = v444;
+    }
+
+    if (chroma_format==CHROMA420)
+    {
+      conv420to422(src[1],u422);
+      conv420to422(src[2],v422);
+      conv422to444(u422,u444);
+      conv422to444(v422,v444);
+    }
+    else
+    {
+      conv422to444(src[1],u444);
+      conv422to444(src[2],v444);
+    }
+  }
+
+  /*strcat(outname,tgaflag ? ".tga" : ".ppm");*/
+
+  /*if (!Quiet_Flag)
+    my_fprintf("saving %s\n",outname);*/
+
+ /* outfile = &file;
+  if(! my_fopen(outname, "wb", outfile))
+  {
+    my_sprintf(Error_Text,"Couldn't create %s\n",outname);
+    Error(Error_Text);
+  }*/
+
+  optr = obfr;
+
+#if 0
+  if (tgaflag)
+  {
+    /* TGA header */
+    for (i=0; i<12; i++)
+      putbyte(tga24[i]);
+
+    putword(horizontal_size); putword(height);
+    putbyte(tga24[12]); putbyte(tga24[13]);
+  }
+  else
+  {
+    /* PPM header */
+    my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
+
+    for (i=0; header[i]!=0; i++)
+      putbyte(header[i]);
+  }
+#endif
+
+  /* matrix coefficients */
+  crv = Inverse_Table_6_9[matrix_coefficients][0];
+  cbu = Inverse_Table_6_9[matrix_coefficients][1];
+  cgu = Inverse_Table_6_9[matrix_coefficients][2];
+  cgv = Inverse_Table_6_9[matrix_coefficients][3];
+  
+  for (i=0; i<height; i++)
+  {
+    py = src[0] + offset + incr*i;
+    pu = u444 + offset + incr*i;
+    pv = v444 + offset + incr*i;
+
+    for (j=0; j<horizontal_size; j++)
+    {
+      u = *pu++ - 128;
+      v = *pv++ - 128;
+      y = 76309 * (*py++ - 16); /* (255/219)*65536 */
+      r = Clip[(y + crv*v + 32768)>>16];
+      g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
+      b = Clip[(y + cbu*u + 32786)>>16];
+
+
+#if 0
+      if (tgaflag)
+      {
+        putbyte(b); putbyte(g); putbyte(r);
+      }
+      else
+      {
+        putbyte(r); putbyte(g); putbyte(b);
+      }
+#else
+     mem_putbyte(r); mem_putbyte(g); mem_putbyte(b);
+#endif
+    }
+  }
+
+  if (optr!=obfr)
+    {
+    /*my_fwrite(obfr,optr-obfr,1,outfile);*/
+    memcpy(OUTMEM,obfr,optr-obfr); OUTMEM+=(optr-obfr);
+    }
+
+  /*my_fclose(outfile);*/
 }
 
 static void putbyte(c)
@@ -383,7 +552,7 @@ int c;
 
   if (optr == obfr+OBFRSIZE)
   {
-    write(outfile,obfr,OBFRSIZE);
+    my_fwrite(obfr,OBFRSIZE,1,outfile);
     optr = obfr;
   }
 }
@@ -394,6 +563,25 @@ int w;
   putbyte(w); putbyte(w>>8);
 }
 
+static void mem_putbyte(c)
+int c;
+{
+  *optr++ = c;
+
+  if (optr == obfr+OBFRSIZE)
+  {
+    /*my_fwrite(obfr,OBFRSIZE,1,outfile);*/
+    memcpy(OUTMEM,obfr,OBFRSIZE);OUTMEM+=OBFRSIZE;
+    optr = obfr;
+  }
+}
+
+/*static void mem_putword(w)
+int w;
+{
+  mem_putbyte(w); mem_putbyte(w>>8);
+}*/
+
 /* horizontal 1:2 interpolation filter */
 static void conv422to444(src,dst)
 unsigned char *src,*dst;