]> 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 7bc9906bb6f32dd674b073e9d9117d18b3ad765d..852b843593b4cd6ea0e67317f89a6b8e45c33a05 100644 (file)
@@ -28,7 +28,6 @@
  */
 
 #include <stdlib.h> /* for malloc */
-#include <fcntl.h>
 #include <string.h> /* for strcat */
 
 #include "config.h"
@@ -53,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
@@ -153,11 +160,13 @@ int offset,incr,width,height;
 {
   int i, j;
   unsigned char *p;
+  ostream file;
 
   if (!Quiet_Flag)
     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))
   {
     my_sprintf(Error_Text,"Couldn't create %s\n",name);
     Error(Error_Text);
@@ -173,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);
 }
 
 /*
@@ -189,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");
@@ -205,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);
@@ -219,7 +231,8 @@ int offset, incr, height;
   if (!Quiet_Flag)
     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))
   {
     my_sprintf(Error_Text,"Couldn't create %s\n",outname);
     Error(Error_Text);
@@ -243,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);
 }
 
 /*
@@ -264,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)
   {
@@ -279,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)
@@ -312,7 +330,8 @@ int tgaflag;
   if (!Quiet_Flag)
     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))
   {
     my_sprintf(Error_Text,"Couldn't create %s\n",outname);
     Error(Error_Text);
@@ -371,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)
@@ -383,7 +402,7 @@ int c;
 
   if (optr == obfr+OBFRSIZE)
   {
-    write(outfile,obfr,OBFRSIZE);
+    my_fwrite(obfr,OBFRSIZE,1,outfile);
     optr = obfr;
   }
 }