]> Creatis software - gdcm.git/blobdiff - src/gdcmmpeg2/src/mpeg2dec/store.c
ENH: Trying to write the worse hack ever...
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / store.c
index 670c252a563b15b72f18e98e65bcbafa33286885..852b843593b4cd6ea0e67317f89a6b8e45c33a05 100644 (file)
  *
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h> // for strcat
+#include <stdlib.h> /* for malloc */
+#include <string.h> /* for strcat */
 
 #include "config.h"
 #include "global.h"
@@ -54,7 +52,15 @@ 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
@@ -68,16 +74,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);
   }
@@ -126,7 +132,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)
@@ -139,10 +145,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);
 }
 
@@ -154,13 +160,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);
   }
 
@@ -174,9 +182,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);
 }
 
 /*
@@ -190,6 +198,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");
@@ -206,9 +215,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);
@@ -218,11 +229,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);
   }
 
@@ -244,9 +256,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);
 }
 
 /*
@@ -265,6 +277,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)
   {
@@ -280,18 +293,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)
@@ -311,11 +328,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);
   }
 
@@ -333,7 +351,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]);
@@ -372,9 +390,9 @@ int tgaflag;
   }
 
   if (optr!=obfr)
-    write(outfile,obfr,optr-obfr);
+    my_fwrite(obfr,optr-obfr,1,outfile);
 
-  close(outfile);
+  my_fclose(outfile);
 }
 
 static void putbyte(c)
@@ -384,7 +402,7 @@ int c;
 
   if (optr == obfr+OBFRSIZE)
   {
-    write(outfile,obfr,OBFRSIZE);
+    my_fwrite(obfr,OBFRSIZE,1,outfile);
     optr = obfr;
   }
 }