]> granicus.if.org Git - imagemagick/blobdiff - coders/mat.c
(no commit message)
[imagemagick] / coders / mat.c
index a5fd169e70e8ffd575cdb5830a22e79397392c72..001c4b09d6e5315a3eaa18dab14fef905123fc20 100644 (file)
@@ -55,6 +55,7 @@
 #include "MagickCore/color-private.h"
 #include "MagickCore/colormap.h"
 #include "MagickCore/colorspace-private.h"
+#include "MagickCore/distort.h"
 #include "MagickCore/exception.h"
 #include "MagickCore/exception-private.h"
 #include "MagickCore/image.h"
 #include "MagickCore/option.h"
 #include "MagickCore/pixel.h"
 #include "MagickCore/resource_.h"
-#include "MagickCore/shear.h"
 #include "MagickCore/static.h"
 #include "MagickCore/string_.h"
 #include "MagickCore/module.h"
 #include "MagickCore/transform.h"
+#include "MagickCore/utility-private.h"
 #if defined(MAGICKCORE_ZLIB_DELEGATE)
  #include "zlib.h"
 #endif
@@ -177,11 +178,9 @@ typedef enum
 static const QuantumType z2qtype[4] = {GrayQuantum, BlueQuantum, GreenQuantum, RedQuantum};
 
 
-static void InsertComplexDoubleRow(double *p, int y, Image * image, double MinVal,
-                                  double MaxVal)
+static void InsertComplexDoubleRow(Image *image,double *p,int y,double MinVal,
+  double MaxVal,ExceptionInfo *exception)
 {
-  ExceptionInfo
-    *exception;
 
   double f;
   int x;
@@ -192,9 +191,8 @@ static void InsertComplexDoubleRow(double *p, int y, Image * image, double MinVa
   if (MaxVal == 0)
     MaxVal = 1;
 
-  exception=(&image->exception);
   q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-  if (q == (const Quantum *) NULL)
+  if (q == (Quantum *) NULL)
     return;
   for (x = 0; x < (ssize_t) image->columns; x++)
   {
@@ -243,12 +241,9 @@ static void InsertComplexDoubleRow(double *p, int y, Image * image, double MinVa
 }
 
 
-static void InsertComplexFloatRow(float *p, int y, Image * image, double MinVal,
-                                  double MaxVal)
+static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal,
+  double MaxVal,ExceptionInfo *exception)
 {
-  ExceptionInfo
-    *exception;
-
   double f;
   int x;
   register Quantum *q;
@@ -258,9 +253,8 @@ static void InsertComplexFloatRow(float *p, int y, Image * image, double MinVal,
   if (MaxVal == 0)
     MaxVal = 1;
 
-  exception=(&image->exception);
   q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
-  if (q == (const Quantum *) NULL)
+  if (q == (Quantum *) NULL)
     return;
   for (x = 0; x < (ssize_t) image->columns; x++)
   {
@@ -486,6 +480,7 @@ z_stream zip_info;
 FILE *mat_file;
 size_t magick_size;
 size_t extent;
+int file;
 
 int status;
 
@@ -494,7 +489,7 @@ int status;
   {
     fclose(clone_info->file);
     clone_info->file = NULL;
-    (void) unlink(clone_info->filename);
+    (void) remove_utf8(clone_info->filename);
   }
 
   CacheBlock = AcquireQuantumMemory((size_t)((Size<16384)?Size:16384),sizeof(unsigned char *));
@@ -506,7 +501,10 @@ int status;
     return NULL;
   }
 
-  mat_file = fdopen(AcquireUniqueFileResource(clone_info->filename),"w");
+  mat_file=0;
+  file = AcquireUniqueFileResource(clone_info->filename);
+  if (file != -1)
+    mat_file = fdopen(file,"w");
   if(!mat_file)
   {
     RelinquishMagickMemory(CacheBlock);
@@ -549,7 +547,7 @@ DblBreak:
   RelinquishMagickMemory(DecompressBlock);
 
   if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) goto UnlinkFile;
-  if( (image2 = AcquireImage(clone_info))==NULL ) goto EraseFile;  
+  if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile;  
   status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
   {
@@ -558,7 +556,7 @@ EraseFile:
     fclose(clone_info->file);
     clone_info->file = NULL;
 UnlinkFile:
-    (void) unlink(clone_info->filename);
+    (void) remove_utf8(clone_info->filename);
     return NULL; 
   }
 
@@ -620,8 +618,8 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
   ssize_t ldblk;
   unsigned char *BImgBuff = NULL;
   double MinVal, MaxVal;
-  size_t Unknown6;
-  unsigned z;
+  unsigned z, z2;
+  unsigned Frames;
   int logging;
   int sample_size;
   MagickOffsetType filepos=0x80;
@@ -643,7 +641,7 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
   /*
      Open image file.
    */
-  image = AcquireImage(image_info);
+  image = AcquireImage(image_info,exception);
 
   status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
   if (status == MagickFalse)
@@ -688,6 +686,7 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   filepos = TellBlob(image);
   while(!EOFBlob(image)) /* object parser loop */
   {
+    Frames = 1;
     (void) SeekBlob(image,filepos,SEEK_SET);
     /* printf("pos=%X\n",TellBlob(image)); */
 
@@ -727,12 +726,16 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
 
     switch(MATLAB_HDR.DimFlag)
     {     
-      case  8: z=1; break;      /* 2D matrix*/
-      case 12: z = ReadBlobXXXLong(image2);  /* 3D matrix RGB*/
-           Unknown6 = ReadBlobXXXLong(image2);
-           (void) Unknown6;
+      case  8: z2=z=1; break;      /* 2D matrix*/
+      case 12: z2=z = ReadBlobXXXLong(image2);  /* 3D matrix RGB*/
+           (void) ReadBlobXXXLong(image2);
          if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
          break;
+      case 16: z2=z = ReadBlobXXXLong(image2);  /* 4D matrix animation */
+         if(z!=3 && z!=1)
+            ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
+           Frames = ReadBlobXXXLong(image2);
+         break;
       default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
     }  
 
@@ -778,11 +781,7 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   
     (void) ReadBlob(image2, 4, (unsigned char *) &size);     /* data size */
 
-      /* Image is gray when no complex flag is set and 2D Matrix */
-    if ((MATLAB_HDR.DimFlag == 8) &&
-        ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
-      image->type=GrayscaleType;
-
+    NEXT_FRAME:
     switch (CellType)
     {
       case miINT8:
@@ -825,7 +824,9 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
         sample_size = 64; 
         image->depth = 64;        /* double type cell */
         (void) SetImageOption(clone_info,"quantum:format","floating-point");
+DisableMSCWarning(4127)
         if (sizeof(double) != 8)
+RestoreMSCWarning
           ThrowReaderException(CoderError, "IncompatibleSizeOfDouble");
         if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
   {                         /* complex double type cell */        
@@ -845,17 +846,14 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     image->colors = one << image->depth;
     if (image->columns == 0 || image->rows == 0)
       goto MATLAB_KO;
-
-    /* ----- Create gray palette ----- */
-
-    if (CellType==miUINT8 && z!=3)
-    {
-      if(image->colors>256) image->colors = 256;
-
-      if (!AcquireImageColormap(image, image->colors))
+    /* Image is gray when no complex flag is set and 2D Matrix */
+    if ((MATLAB_HDR.DimFlag == 8) &&
+        ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
       {
- NoMemory:ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");}
-    }
+        image->type=GrayscaleType;
+        SetImageColorspace(image,GRAYColorspace,exception);
+      }
+
 
     /*
       If ping is true, then only set image size and colors without
@@ -870,9 +868,9 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     }  
 
   /* ----- Load raster data ----- */
-    BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char *));    /* Ldblk was set in the check phase */
+    BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char));    /* Ldblk was set in the check phase */
     if (BImgBuff == NULL)
-      goto NoMemory;
+      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
 
     MinVal = 0;
     MaxVal = 0;
@@ -888,8 +886,8 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     {
       for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
       {
-        q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
-        if (q == (const Quantum *)NULL)
+        q=GetAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
+        if (q == (Quantum *)NULL)
   {
     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
               "  MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
@@ -949,14 +947,16 @@ ExitLoop:
         for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
   {
           ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff);
-          InsertComplexDoubleRow((double *)BImgBuff, i, image, MinVal, MaxVal);
+          InsertComplexDoubleRow(image, (double *)BImgBuff, i, MinVal, MaxVal,
+            exception);
   }
 
       if (CellType==miSINGLE)
         for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
   {
           ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff);
-          InsertComplexFloatRow((float *)BImgBuff, i, image, MinVal, MaxVal);
+          InsertComplexFloatRow(image,(float *)BImgBuff,i,MinVal,MaxVal,
+            exception);
   }    
     }
 
@@ -998,13 +998,13 @@ done_reading:
     {
             fclose(clone_info->file);
             clone_info->file = NULL;
-            (void) unlink(clone_info->filename);
+            (void) remove_utf8(clone_info->filename);
     }
         }    
       }
 
       /* Allocate next image structure. */    
-    AcquireNextImage(image_info,image);
+    AcquireNextImage(image_info,image,exception);
     if (image->next == (Image *) NULL) break;                
     image=SyncNextImageInList(image);
     image->columns=image->rows=0;
@@ -1013,9 +1013,30 @@ done_reading:
       /* row scan buffer is no longer needed */
     RelinquishMagickMemory(BImgBuff);
     BImgBuff = NULL;
+
+    if(--Frames>0)
+    {
+      z = z2;
+      if(image2==NULL) image2 = image;
+      goto NEXT_FRAME;
+    }
+    if ((image2!=NULL) && (image2!=image))   /* Does shadow temporary decompressed image exist? */
+      {
+/*  CloseBlob(image2); */
+        DeleteImageFromList(&image2);
+        if(clone_info)
+        {
+          if(clone_info->file)
+          {
+            fclose(clone_info->file);
+            clone_info->file = NULL;
+            (void) remove_utf8(clone_info->filename);
+          }
+        }
+        }
   }
-    clone_info=DestroyImageInfo(clone_info);
 
+  clone_info=DestroyImageInfo(clone_info);
   RelinquishMagickMemory(BImgBuff);
   CloseBlob(image);
 
@@ -1054,7 +1075,7 @@ done_reading:
     {
       fclose(clone_info->file);
       clone_info->file = NULL;
-      (void) unlink(clone_info->filename);
+      (void) remove_utf8(clone_info->filename);
     }
     DestroyImageInfo(clone_info);
     clone_info = NULL;
@@ -1201,7 +1222,8 @@ static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
   (void) memcpy(&local_time,localtime(&current_time),sizeof(local_time));
 #endif
   (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124));
-  FormatLocaleString(MATLAB_HDR,MaxTextExtent,"MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d",
+  FormatLocaleString(MATLAB_HDR,sizeof(MATLAB_HDR),
+    "MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d",
     OsDesc,DayOfWTab[local_time.tm_wday],MonthsTab[local_time.tm_mon],
     local_time.tm_mday,local_time.tm_hour,local_time.tm_min,
     local_time.tm_sec,local_time.tm_year+1900);
@@ -1213,9 +1235,7 @@ static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
   scene=0;
   do
   {
-    if (IsRGBColorspace(image->colorspace) == MagickFalse)
-      (void) TransformImageColorspace(image,RGBColorspace);
-
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
     is_gray = IsImageGray(image,exception);
     z = is_gray ? 0 : 3;