]> granicus.if.org Git - imagemagick/blobdiff - coders/dpx.c
(no commit message)
[imagemagick] / coders / dpx.c
index 8a77f335e027671cd8d00eebbec5f55bd947ef7a..32fb55e7d7f843dbb1234da0fb9c6034004cc3ce 100644 (file)
@@ -17,7 +17,7 @@
 %                                March 2001                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "MagickCore/string_.h"
 #include "MagickCore/string-private.h"
 \f
+/*
+  Define declaration.
+*/
+#define MaxNumberImageElements  8
+\f
 /*
   Typedef declaration.
 */
@@ -230,7 +235,7 @@ typedef struct _DPXImageInfo
     lines_per_element;
 
   DPXImageElement
-    image_element[8];
+    image_element[MaxNumberImageElements];
 
   unsigned char
     reserve[52];
@@ -436,8 +441,14 @@ static size_t GetBytesPerRow(const size_t columns,
     }
     case 16:
     {
-      bytes_per_row=2*(((size_t) samples_per_pixel*columns*bits_per_pixel+8)/
-        16);
+      if (pad == MagickFalse)
+        { 
+          bytes_per_row=2*(((size_t) samples_per_pixel*columns*bits_per_pixel+
+            15)/16);
+          break;
+        }
+      bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+31)/
+        32);
       break;
     }
     case 32:
@@ -629,7 +640,7 @@ static void TimeCodeToString(const size_t timestamp,char *code)
   shift=4*TimeFields;
   for (i=0; i <= TimeFields; i++)
   {
-    (void) FormatLocaleString(code,MaxTextExtent-strlen(code),"%x",
+    (void) FormatLocaleString(code,MagickPathExtent-strlen(code),"%x",
       (unsigned int) ((timestamp >> shift) & 0x0fU));
     code++;
     if (((i % 2) != 0) && (i < TimeFields))
@@ -643,7 +654,7 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   char
     magick[4],
-    value[MaxTextExtent];
+    value[MagickPathExtent];
 
   DPXInfo
     dpx;
@@ -773,6 +784,8 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     Read DPX image header.
   */
   dpx.image.orientation=ReadBlobShort(image);
+  if (dpx.image.orientation > 7)
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   offset+=2;
   if (dpx.image.orientation != (unsigned short) ~0)
     (void) FormatImageProperty(image,"dpx:image.orientation","%d",
@@ -790,6 +803,8 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     case 7: image->orientation=RightBottomOrientation; break;
   }
   dpx.image.number_elements=ReadBlobShort(image);
+  if (dpx.image.number_elements > MaxNumberImageElements)
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   offset+=2;
   dpx.image.pixels_per_line=ReadBlobLong(image);
   offset+=4;
@@ -800,7 +815,7 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   for (i=0; i < 8; i++)
   {
     char
-      property[MaxTextExtent];
+      property[MagickPathExtent];
 
     dpx.image.image_element[i].data_sign=ReadBlobLong(image);
     offset+=4;
@@ -816,7 +831,7 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     offset++;
     dpx.image.image_element[i].transfer_characteristic=(unsigned char)
       ReadBlobByte(image);
-    (void) FormatLocaleString(property,MaxTextExtent,
+    (void) FormatLocaleString(property,MagickPathExtent,
       "dpx:image.element[%lu].transfer-characteristic",(long) i);
     (void) FormatImageProperty(image,property,"%s",
       GetImageTransferCharacteristic((DPXTransferCharacteristic)
@@ -1091,7 +1106,7 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           StringInfo
             *profile;
 
-           profile=BlobToStringInfo((const void *) NULL,
+           profile=BlobToStringInfo((const unsigned char *) NULL,
              dpx.file.user_size-sizeof(dpx.user.id));
            if (profile == (StringInfo *) NULL)
              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
@@ -1108,6 +1123,9 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       (void) CloseBlob(image);
       return(GetFirstImageInList(image));
     }
+  status=SetImageExtent(image,image->columns,image->rows,exception);
+  if (status == MagickFalse)
+    return(DestroyImageList(image));
   for (n=0; n < (ssize_t) dpx.image.number_elements; n++)
   {
     /*
@@ -1205,6 +1223,9 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       MagickTrue : MagickFalse);
     for (y=0; y < (ssize_t) image->rows; y++)
     {
+      const unsigned char
+        *pixels;
+
       MagickBooleanType
         sync;
 
@@ -1218,29 +1239,24 @@ static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
         count,
         offset;
 
-      unsigned char
-        *pixels;
-
       if (status == MagickFalse)
         continue;
-      pixels=GetQuantumPixels(quantum_info);
-      {
-        count=ReadBlob(image,extent,pixels);
-        if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-            (image->previous == (Image *) NULL))
-          {
-            MagickBooleanType
-              proceed;
-
-            proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) row,
-              image->rows);
-            if (proceed == MagickFalse)
-              status=MagickFalse;
-          }
-        offset=row++;
-      }
+      pixels=(const unsigned char *) ReadBlobStream(image,extent,
+        GetQuantumPixels(quantum_info),&count);
       if (count != (ssize_t) extent)
         status=MagickFalse;
+      if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+          (image->previous == (Image *) NULL))
+        {
+          MagickBooleanType
+            proceed;
+
+          proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) row,
+            image->rows);
+          if (proceed == MagickFalse)
+            status=MagickFalse;
+        }
+      offset=row++;
       q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
       if (q == (Quantum *) NULL)
         {
@@ -1318,15 +1334,13 @@ ModuleExport size_t RegisterDPXImage(void)
       "See SMPTE 268M-2003 specification at http://www.smtpe.org\n"
     };
 
-  entry=SetMagickInfo("DPX");
+  entry=AcquireMagickInfo("DPX","DPX","SMPTE 268M-2003 (DPX 2.0)");
   entry->decoder=(DecodeImageHandler *) ReadDPXImage;
   entry->encoder=(EncodeImageHandler *) WriteDPXImage;
   entry->magick=(IsImageFormatHandler *) IsDPX;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("SMPTE 268M-2003 (DPX 2.0)");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderSeekableStreamFlag;
   entry->note=ConstantString(DPXNote);
-  entry->module=ConstantString("DPX");
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -1611,18 +1625,18 @@ static MagickBooleanType WriteDPXImage(const ImageInfo *image_info,Image *image,
         case YCbCrColorspace:
         {
           dpx.image.image_element[i].descriptor=CbYCr444ComponentType;
-          if (image->alpha_trait == BlendPixelTrait)
+          if (image->alpha_trait != UndefinedPixelTrait)
             dpx.image.image_element[i].descriptor=CbYCrA4444ComponentType;
           break;
         }
         default:
         {
           dpx.image.image_element[i].descriptor=RGBComponentType;
-          if (image->alpha_trait == BlendPixelTrait)
+          if (image->alpha_trait != UndefinedPixelTrait)
             dpx.image.image_element[i].descriptor=RGBAComponentType;
           if ((image_info->type != TrueColorType) &&
-              (image->alpha_trait != BlendPixelTrait) &&
-              (IsImageGray(image,exception) != MagickFalse))
+              (image->alpha_trait == UndefinedPixelTrait) &&
+              (SetImageGray(image,exception) != MagickFalse))
             dpx.image.image_element[i].descriptor=LumaComponentType;
           break;
         }
@@ -1941,26 +1955,28 @@ static MagickBooleanType WriteDPXImage(const ImageInfo *image_info,Image *image,
   SetQuantumPack(quantum_info,dpx.image.image_element[0].packing == 0 ?
     MagickTrue : MagickFalse);
   quantum_type=RGBQuantum;
-  if (image->alpha_trait == BlendPixelTrait)
+  if (image->alpha_trait != UndefinedPixelTrait)
     quantum_type=RGBAQuantum;
   if (image->colorspace == YCbCrColorspace)
     {
       quantum_type=CbYCrQuantum;
-      if (image->alpha_trait == BlendPixelTrait)
+      if (image->alpha_trait != UndefinedPixelTrait)
         quantum_type=CbYCrAQuantum;
       if ((horizontal_factor == 2) || (vertical_factor == 2))
         quantum_type=CbYCrYQuantum;
     }
-  extent=GetBytesPerRow(image->columns,image->alpha_trait == BlendPixelTrait ?
-    4UL : 3UL,image->depth,MagickTrue);
+  extent=GetBytesPerRow(image->columns,
+    image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL,image->depth,
+    dpx.image.image_element[0].packing == 0 ? MagickFalse : MagickTrue);
   if ((image_info->type != TrueColorType) &&
-      (image->alpha_trait != BlendPixelTrait) &&
-      (IsImageGray(image,exception) != MagickFalse))
+      (image->alpha_trait == UndefinedPixelTrait) &&
+      (SetImageGray(image,exception) != MagickFalse))
     {
       quantum_type=GrayQuantum;
-      extent=GetBytesPerRow(image->columns,1UL,image->depth,MagickTrue);
+      extent=GetBytesPerRow(image->columns,1UL,image->depth,
+        dpx.image.image_element[0].packing == 0 ? MagickFalse : MagickTrue);
     }
-  pixels=GetQuantumPixels(quantum_info);
+  pixels=(unsigned char *) GetQuantumPixels(quantum_info);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     p=GetVirtualPixels(image,0,y,image->columns,1,exception);