]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/stream.c
(no commit message)
[imagemagick] / MagickCore / stream.c
index ee3a483f147bdbcd198b0fe3ce0f522d9acc0c5d..d32ae859d930959622dfba15363f1cb07f5ab278 100644 (file)
 %                       MagickCore Pixel Stream Methods                       %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 March 2000                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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  %
@@ -52,6 +52,7 @@
 #include "MagickCore/exception-private.h"
 #include "MagickCore/geometry.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/memory-private.h"
 #include "MagickCore/pixel.h"
 #include "MagickCore/pixel-accessor.h"
 #include "MagickCore/quantum.h"
@@ -141,14 +142,18 @@ static Quantum
 %
 %  The format of the AcquireStreamInfo method is:
 %
-%      StreamInfo *AcquireStreamInfo(const ImageInfo *image_info)
+%      StreamInfo *AcquireStreamInfo(const ImageInfo *image_info,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
 %    o image_info: the image info.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
-MagickExport StreamInfo *AcquireStreamInfo(const ImageInfo *image_info)
+MagickExport StreamInfo *AcquireStreamInfo(const ImageInfo *image_info,
+  ExceptionInfo *exception)
 {
   StreamInfo
     *stream_info;
@@ -157,13 +162,13 @@ MagickExport StreamInfo *AcquireStreamInfo(const ImageInfo *image_info)
   if (stream_info == (StreamInfo *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   (void) ResetMagickMemory(stream_info,0,sizeof(*stream_info));
-  stream_info->pixels=(unsigned char *) AcquireMagickMemory(
-    sizeof(*stream_info->pixels));
+  stream_info->pixels=(unsigned char *) MagickAssumeAligned(
+    AcquireAlignedMemory(1,sizeof(*stream_info->pixels)));
   if (stream_info->pixels == (unsigned char *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   stream_info->map=ConstantString("RGB");
   stream_info->storage_type=CharPixel;
-  stream_info->stream=AcquireImage(image_info);
+  stream_info->stream=AcquireImage(image_info,exception);
   stream_info->signature=MagickSignature;
   return(stream_info);
 }
@@ -195,7 +200,7 @@ static inline void RelinquishStreamPixels(CacheInfo *cache_info)
 {
   assert(cache_info != (CacheInfo *) NULL);
   if (cache_info->mapped == MagickFalse)
-    (void) RelinquishMagickMemory(cache_info->pixels);
+    (void) RelinquishAlignedMemory(cache_info->pixels);
   else
     (void) UnmapBlob(cache_info->pixels,(size_t) cache_info->length);
   cache_info->pixels=(Quantum *) NULL;
@@ -230,10 +235,10 @@ static void DestroyPixelStream(Image *image)
   if (cache_info->nexus_info != (NexusInfo **) NULL)
     cache_info->nexus_info=DestroyPixelCacheNexus(cache_info->nexus_info,
       cache_info->number_threads);
-  if (cache_info->disk_semaphore != (SemaphoreInfo *) NULL)
-    DestroySemaphoreInfo(&cache_info->disk_semaphore);
+  if (cache_info->file_semaphore != (SemaphoreInfo *) NULL)
+    RelinquishSemaphoreInfo(&cache_info->file_semaphore);
   if (cache_info->semaphore != (SemaphoreInfo *) NULL)
-    DestroySemaphoreInfo(&cache_info->semaphore);
+    RelinquishSemaphoreInfo(&cache_info->semaphore);
   cache_info=(CacheInfo *) RelinquishMagickMemory(cache_info);
 }
 \f
@@ -268,7 +273,7 @@ MagickExport StreamInfo *DestroyStreamInfo(StreamInfo *stream_info)
   if (stream_info->map != (char *) NULL)
     stream_info->map=DestroyString(stream_info->map);
   if (stream_info->pixels != (unsigned char *) NULL)
-    stream_info->pixels=(unsigned char *) RelinquishMagickMemory(
+    stream_info->pixels=(unsigned char *) RelinquishAlignedMemory(
       stream_info->pixels);
   if (stream_info->stream != (Image *) NULL)
     {
@@ -421,7 +426,7 @@ static Quantum *GetAuthenticPixelsFromStream(const Image *image)
 %  The format of the GetOneAuthenticPixelFromStream() method is:
 %
 %      MagickBooleanType GetOneAuthenticPixelFromStream(const Image image,
-%        const ssize_t x,const ssize_t y,PixelPacket *pixel,
+%        const ssize_t x,const ssize_t y,Quantum *pixel,
 %        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
@@ -436,18 +441,32 @@ static Quantum *GetAuthenticPixelsFromStream(const Image *image)
 %
 */
 static MagickBooleanType GetOneAuthenticPixelFromStream(Image *image,
-  const ssize_t x,const ssize_t y,PixelPacket *pixel,ExceptionInfo *exception)
+  const ssize_t x,const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
 {
   register Quantum
     *q;
 
+  register ssize_t
+    i;
+
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
-  *pixel=image->background_color;
+  (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
   q=GetAuthenticPixelsStream(image,x,y,1,1,exception);
   if (q != (Quantum *) NULL)
-    return(MagickFalse);
-  GetPixelPacket(image,q,pixel);
+    {
+      pixel[RedPixelChannel]=ClampToQuantum(image->background_color.red);
+      pixel[GreenPixelChannel]=ClampToQuantum(image->background_color.green);
+      pixel[BluePixelChannel]=ClampToQuantum(image->background_color.blue);
+      pixel[BlackPixelChannel]=ClampToQuantum(image->background_color.black);
+      pixel[AlphaPixelChannel]=ClampToQuantum(image->background_color.alpha);
+      return(MagickFalse);
+    }
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    pixel[channel]=q[i];
+  }
   return(MagickTrue);
 }
 \f
@@ -469,7 +488,7 @@ static MagickBooleanType GetOneAuthenticPixelFromStream(Image *image,
 %
 %      MagickBooleanType GetOneVirtualPixelFromStream(const Image image,
 %        const VirtualPixelMethod virtual_pixel_method,const ssize_t x,
-%        const ssize_t y,PixelPacket *pixel,ExceptionInfo *exception)
+%        const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -486,18 +505,32 @@ static MagickBooleanType GetOneAuthenticPixelFromStream(Image *image,
 */
 static MagickBooleanType GetOneVirtualPixelFromStream(const Image *image,
   const VirtualPixelMethod virtual_pixel_method,const ssize_t x,const ssize_t y,
-  PixelPacket *pixel,ExceptionInfo *exception)
+  Quantum *pixel,ExceptionInfo *exception)
 {
   const Quantum
-    *q;
+    *p;
+
+  register ssize_t
+    i;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
-  *pixel=image->background_color;
-  q=GetVirtualPixelStream(image,virtual_pixel_method,x,y,1,1,exception);
-  if (q != (const Quantum *) NULL)
-    return(MagickFalse);
-  GetPixelPacket(image,q,pixel);
+  (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
+  p=GetVirtualPixelStream(image,virtual_pixel_method,x,y,1,1,exception);
+  if (p == (const Quantum *) NULL)
+    {
+      pixel[RedPixelChannel]=ClampToQuantum(image->background_color.red);
+      pixel[GreenPixelChannel]=ClampToQuantum(image->background_color.green);
+      pixel[BluePixelChannel]=ClampToQuantum(image->background_color.blue);
+      pixel[BlackPixelChannel]=ClampToQuantum(image->background_color.black);
+      pixel[AlphaPixelChannel]=ClampToQuantum(image->background_color.alpha);
+      return(MagickFalse);
+    }
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    pixel[channel]=p[i];
+  }
   return(MagickTrue);
 }
 \f
@@ -514,7 +547,7 @@ static MagickBooleanType GetOneVirtualPixelFromStream(const Image *image,
 %
 %  GetStreamInfoClientData() gets the stream info client data.
 %
-%  The format of the SetStreamInfoClientData method is:
+%  The format of the GetStreamInfoClientData method is:
 %
 %      const void *GetStreamInfoClientData(StreamInfo *stream_info)
 %
@@ -523,7 +556,7 @@ static MagickBooleanType GetOneVirtualPixelFromStream(const Image *image,
 %    o stream_info: the stream info.
 %
 */
-MagickExport const void *GetStreamInfoClientData(StreamInfo *stream_info)
+MagickPrivate const void *GetStreamInfoClientData(StreamInfo *stream_info)
 {
   assert(stream_info != (StreamInfo *) NULL);
   assert(stream_info->signature == MagickSignature);
@@ -581,8 +614,8 @@ static const Quantum *GetVirtualPixelsStream(const Image *image)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetVirtualMetacontentFromStream() returns the associated pixel 
-%  channels corresponding with the last call to QueueAuthenticPixelsStream() or
+%  GetVirtualMetacontentFromStream() returns the associated pixel channels
+%  corresponding with the last call to QueueAuthenticPixelsStream() or
 %  GetVirtualPixelStream().
 %
 %  The format of the GetVirtualMetacontentFromStream() method is:
@@ -594,8 +627,7 @@ static const Quantum *GetVirtualPixelsStream(const Image *image)
 %    o image: the image.
 %
 */
-static const void *GetVirtualMetacontentFromStream(
-  const Image *image)
+static const void *GetVirtualMetacontentFromStream(const Image *image)
 {
   CacheInfo
     *cache_info;
@@ -651,7 +683,7 @@ static inline MagickBooleanType AcquireStreamPixels(CacheInfo *cache_info,
   if (cache_info->length != (MagickSizeType) ((size_t) cache_info->length))
     return(MagickFalse);
   cache_info->mapped=MagickFalse;
-  cache_info->pixels=(Quantum *) AcquireMagickMemory((size_t)
+  cache_info->pixels=(Quantum *) AcquireAlignedMemory(1,(size_t)
     cache_info->length);
   if (cache_info->pixels == (Quantum *) NULL)
     {
@@ -708,7 +740,9 @@ static const Quantum *GetVirtualPixelStream(const Image *image,
     Pixels are stored in a temporary buffer until they are synced to the cache.
   */
   number_pixels=(MagickSizeType) columns*rows;
-  length=(size_t) number_pixels*cache_info->pixel_channels*sizeof(Quantum);
+  length=(size_t) number_pixels*cache_info->number_channels*sizeof(Quantum);
+  if (cache_info->number_channels == 0)
+    length=number_pixels*sizeof(Quantum);
   if (cache_info->metacontent_extent != 0)
     length+=number_pixels*cache_info->metacontent_extent;
   if (cache_info->pixels == (Quantum *) NULL)
@@ -722,7 +756,7 @@ static const Quantum *GetVirtualPixelStream(const Image *image,
         }
     }
   else
-    if (cache_info->length != length)
+    if (cache_info->length < length)
       {
         RelinquishStreamPixels(cache_info);
         cache_info->length=length;
@@ -736,7 +770,7 @@ static const Quantum *GetVirtualPixelStream(const Image *image,
   cache_info->metacontent=(void *) NULL;
   if (cache_info->metacontent_extent != 0)
     cache_info->metacontent=(void *) (cache_info->pixels+number_pixels*
-      cache_info->pixel_channels);
+      cache_info->number_channels);
   return(cache_info->pixels);
 }
 \f
@@ -818,6 +852,9 @@ static Quantum *QueueAuthenticPixelsStream(Image *image,const ssize_t x,
   CacheInfo
     *cache_info;
 
+  MagickBooleanType
+    status;
+
   MagickSizeType
     number_pixels;
 
@@ -867,27 +904,37 @@ static Quantum *QueueAuthenticPixelsStream(Image *image,const ssize_t x,
   cache_info->columns=columns;
   cache_info->rows=rows;
   number_pixels=(MagickSizeType) columns*rows;
-  length=(size_t) number_pixels*cache_info->pixel_channels*sizeof(Quantum);
+  length=(size_t) number_pixels*cache_info->number_channels*sizeof(Quantum);
+  if (cache_info->number_channels == 0)
+    length=number_pixels*sizeof(Quantum);
   if (cache_info->metacontent_extent != 0)
     length+=number_pixels*cache_info->metacontent_extent;
   if (cache_info->pixels == (Quantum *) NULL)
     {
-      cache_info->pixels=(Quantum *) AcquireMagickMemory(length);
-      cache_info->length=(MagickSizeType) length;
+      cache_info->length=length;
+      status=AcquireStreamPixels(cache_info,exception);
+      if (status == MagickFalse)
+        {
+          cache_info->length=0;
+          return((Quantum *) NULL);
+        }
     }
   else
-    if (cache_info->length < (MagickSizeType) length)
+    if (cache_info->length < length)
       {
-        cache_info->pixels=(Quantum *) ResizeMagickMemory(
-          cache_info->pixels,length);
-        cache_info->length=(MagickSizeType) length;
+        RelinquishStreamPixels(cache_info);
+        cache_info->length=length;
+        status=AcquireStreamPixels(cache_info,exception);
+        if (status == MagickFalse)
+          {
+            cache_info->length=0;
+            return((Quantum *) NULL);
+          }
       }
-  if (cache_info->pixels == (void *) NULL)
-    return((Quantum *) NULL);
   cache_info->metacontent=(void *) NULL;
   if (cache_info->metacontent_extent != 0)
     cache_info->metacontent=(void *) (cache_info->pixels+number_pixels*
-      cache_info->pixel_channels);
+      cache_info->number_channels);
   return(cache_info->pixels);
 }
 \f
@@ -990,7 +1037,7 @@ MagickExport Image *ReadStream(const ImageInfo *image_info,StreamHandler stream,
 %    o client_data: the client data.
 %
 */
-MagickExport void SetStreamInfoClientData(StreamInfo *stream_info,
+MagickPrivate void SetStreamInfoClientData(StreamInfo *stream_info,
   const void *client_data)
 {
   assert(stream_info != (StreamInfo *) NULL);
@@ -1118,12 +1165,12 @@ static size_t WriteStreamImage(const Image *image,const void *pixels,
   stream_info=(StreamInfo *) image->client_data;
   switch (stream_info->storage_type)
   {
-    default: packet_size=sizeof(char); break;
-    case CharPixel: packet_size=sizeof(char); break;
+    default: packet_size=sizeof(unsigned char); break;
+    case CharPixel: packet_size=sizeof(unsigned char); break;
     case DoublePixel: packet_size=sizeof(double); break;
     case FloatPixel: packet_size=sizeof(float); break;
-    case IntegerPixel: packet_size=sizeof(int); break;
-    case LongPixel: packet_size=sizeof(ssize_t); break;
+    case LongPixel: packet_size=sizeof(unsigned int); break;
+    case LongLongPixel: packet_size=sizeof(MagickSizeType); break;
     case QuantumPixel: packet_size=sizeof(Quantum); break;
     case ShortPixel: packet_size=sizeof(unsigned short); break;
   }
@@ -1139,10 +1186,11 @@ static size_t WriteStreamImage(const Image *image,const void *pixels,
       /*
         Prepare stream for writing.
       */
-      stream_info->pixels=(unsigned char *) ResizeQuantumMemory(
-        stream_info->pixels,length,sizeof(*stream_info->pixels));
+      (void) RelinquishAlignedMemory(stream_info->pixels);
+      stream_info->pixels=(unsigned char *) AcquireAlignedMemory(1,length);
       if (stream_info->pixels == (unsigned char *) NULL)
         return(0);
+      (void) ResetMagickMemory(stream_info->pixels,0,length);
       stream_info->image=image;
       write_info=CloneImageInfo(stream_info->image_info);
       (void) SetImageInfo(write_info,1,stream_info->exception);
@@ -1203,10 +1251,12 @@ MagickExport Image *StreamImage(const ImageInfo *image_info,
   assert(exception != (ExceptionInfo *) NULL);
   read_info=CloneImageInfo(image_info);
   stream_info->image_info=image_info;
+  stream_info->quantum_info=AcquireQuantumInfo(image_info,(Image *) NULL);
   stream_info->exception=exception;
   read_info->client_data=(void *) stream_info;
   image=ReadStream(read_info,&WriteStreamImage,exception);
   read_info=DestroyImageInfo(read_info);
+  stream_info->quantum_info=DestroyQuantumInfo(stream_info->quantum_info);
   stream_info->quantum_info=AcquireQuantumInfo(image_info,image);
   if (stream_info->quantum_info == (QuantumInfo *) NULL)
     image=DestroyImage(image);
@@ -1416,7 +1466,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
         {
           p=GetAuthenticPixelQueue(image);
           if (p == (const Quantum *) NULL)
-              break;
+            break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
             *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
@@ -1434,7 +1484,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
+            *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(image,p)));
             p++;
           }
           break;
@@ -1529,7 +1579,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             }
             case IndexQuantum:
             {
-              *q=ScaleQuantumToChar(GetPixelIntensity(image,p));
+              *q=ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(image,p)));
               break;
             }
             default:
@@ -1925,7 +1975,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
       }
       break;
     }
-    case IntegerPixel:
+    case LongPixel:
     {
       register unsigned int
         *q;
@@ -1938,9 +1988,9 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
             p++;
           }
           break;
@@ -1952,10 +2002,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelAlpha(image,p));
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
             p++;
           }
           break;
@@ -1967,10 +2017,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=0U;
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=0;
             p++;
           }
           break;
@@ -1982,8 +2032,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(
-              GetPixelIntensity(image,p));
+            *q++=ScaleQuantumToLong(ClampToQuantum(GetPixelIntensity(image,p)));
             p++;
           }
           break;
@@ -1995,9 +2044,9 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
             p++;
           }
           break;
@@ -2009,11 +2058,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong((Quantum)
-              (GetPixelAlpha(image,p)));
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
             p++;
           }
           break;
@@ -2025,10 +2073,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=0U;
+            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=0;
             p++;
           }
           break;
@@ -2046,45 +2094,44 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             case RedQuantum:
             case CyanQuantum:
             {
-              *q=(unsigned int) ScaleQuantumToLong(GetPixelRed(image,p));
+              *q=ScaleQuantumToLong(GetPixelRed(image,p));
               break;
             }
             case GreenQuantum:
             case MagentaQuantum:
             {
-              *q=(unsigned int) ScaleQuantumToLong(GetPixelGreen(image,p));
+              *q=ScaleQuantumToLong(GetPixelGreen(image,p));
               break;
             }
             case BlueQuantum:
             case YellowQuantum:
             {
-              *q=(unsigned int) ScaleQuantumToLong(GetPixelBlue(image,p));
+              *q=ScaleQuantumToLong(GetPixelBlue(image,p));
               break;
             }
             case AlphaQuantum:
             {
-              *q=(unsigned int) ScaleQuantumToLong(GetPixelAlpha(image,p));
+              *q=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
               break;
             }
             case OpacityQuantum:
             {
-              *q=(unsigned int) ScaleQuantumToLong(GetPixelAlpha(image,p));
+              *q=ScaleQuantumToLong(GetPixelAlpha(image,p));
               break;
             }
             case BlackQuantum:
             {
               if (image->colorspace == CMYKColorspace)
-                *q=(unsigned int) ScaleQuantumToLong(GetPixelBlack(image,p));
+                *q=ScaleQuantumToLong(GetPixelBlack(image,p));
               break;
             }
             case IndexQuantum:
             {
-              *q=(unsigned int)
-                ScaleQuantumToLong(GetPixelIntensity(image,p));
+              *q=ScaleQuantumToLong(ClampToQuantum(GetPixelIntensity(image,p)));
               break;
             }
             default:
-              *q=0;
+              break;
           }
           q++;
         }
@@ -2092,12 +2139,12 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
       }
       break;
     }
-    case LongPixel:
+    case LongLongPixel:
     {
-      register size_t
+      register MagickSizeType
         *q;
 
-      q=(size_t *) stream_info->pixels;
+      q=(MagickSizeType *) stream_info->pixels;
       if (LocaleCompare(stream_info->map,"BGR") == 0)
         {
           p=GetAuthenticPixelQueue(image);
@@ -2105,9 +2152,9 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
             p++;
           }
           break;
@@ -2119,10 +2166,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelAlpha(image,p));
             p++;
           }
           break;
@@ -2134,10 +2181,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=0;
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
+            *q++=0U;
             p++;
           }
           break;
@@ -2149,7 +2196,8 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelIntensity(image,p));
+            *q++=ScaleQuantumToLongLong(ClampToQuantum(
+              GetPixelIntensity(image,p)));
             p++;
           }
           break;
@@ -2161,9 +2209,9 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
             p++;
           }
           break;
@@ -2175,10 +2223,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelAlpha(image,p));
             p++;
           }
           break;
@@ -2190,10 +2238,10 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToLong(GetPixelRed(image,p));
-            *q++=ScaleQuantumToLong(GetPixelGreen(image,p));
-            *q++=ScaleQuantumToLong(GetPixelBlue(image,p));
-            *q++=0;
+            *q++=ScaleQuantumToLongLong(GetPixelRed(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToLongLong(GetPixelBlue(image,p));
+            *q++=0U;
             p++;
           }
           break;
@@ -2211,44 +2259,45 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             case RedQuantum:
             case CyanQuantum:
             {
-              *q=ScaleQuantumToLong(GetPixelRed(image,p));
+              *q=ScaleQuantumToLongLong(GetPixelRed(image,p));
               break;
             }
             case GreenQuantum:
             case MagentaQuantum:
             {
-              *q=ScaleQuantumToLong(GetPixelGreen(image,p));
+              *q=ScaleQuantumToLongLong(GetPixelGreen(image,p));
               break;
             }
             case BlueQuantum:
             case YellowQuantum:
             {
-              *q=ScaleQuantumToLong(GetPixelBlue(image,p));
+              *q=ScaleQuantumToLongLong(GetPixelBlue(image,p));
               break;
             }
             case AlphaQuantum:
             {
-              *q=ScaleQuantumToLong((Quantum) (GetPixelAlpha(image,p)));
+              *q=ScaleQuantumToLongLong(GetPixelAlpha(image,p));
               break;
             }
             case OpacityQuantum:
             {
-              *q=ScaleQuantumToLong(GetPixelAlpha(image,p));
+              *q=ScaleQuantumToLongLong(GetPixelAlpha(image,p));
               break;
             }
             case BlackQuantum:
             {
               if (image->colorspace == CMYKColorspace)
-                *q=ScaleQuantumToLong(GetPixelBlack(image,p));
+                *q=ScaleQuantumToLongLong(GetPixelBlack(image,p));
               break;
             }
             case IndexQuantum:
             {
-              *q=ScaleQuantumToLong(GetPixelIntensity(image,p));
+              *q=ScaleQuantumToLongLong(ClampToQuantum(
+                GetPixelIntensity(image,p)));
               break;
             }
             default:
-              break;
+              *q=0;
           }
           q++;
         }
@@ -2286,7 +2335,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             *q++=GetPixelBlue(image,p);
             *q++=GetPixelGreen(image,p);
             *q++=GetPixelRed(image,p);
-            *q++=(Quantum) (GetPixelAlpha(image,p));
+            *q++=GetPixelAlpha(image,p);
             p++;
           }
           break;
@@ -2313,7 +2362,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=GetPixelIntensity(image,p);
+            *q++=ClampToQuantum(GetPixelIntensity(image,p));
             p++;
           }
           break;
@@ -2342,7 +2391,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             *q++=GetPixelRed(image,p);
             *q++=GetPixelGreen(image,p);
             *q++=GetPixelBlue(image,p);
-            *q++=(Quantum) (GetPixelAlpha(image,p));
+            *q++=GetPixelAlpha(image,p);
             p++;
           }
           break;
@@ -2408,7 +2457,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             }
             case IndexQuantum:
             {
-              *q=(GetPixelIntensity(image,p));
+              *q=ClampToQuantum(GetPixelIntensity(image,p));
               break;
             }
             default:
@@ -2477,7 +2526,8 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             break;
           for (x=0; x < (ssize_t) GetImageExtent(image); x++)
           {
-            *q++=ScaleQuantumToShort(GetPixelIntensity(image,p));
+            *q++=ScaleQuantumToShort(ClampToQuantum(
+              GetPixelIntensity(image,p)));
             p++;
           }
           break;
@@ -2572,7 +2622,8 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             }
             case IndexQuantum:
             {
-              *q=ScaleQuantumToShort(GetPixelIntensity(image,p));
+              *q=ScaleQuantumToShort(ClampToQuantum(
+                GetPixelIntensity(image,p)));
               break;
             }
             default:
@@ -2668,7 +2719,7 @@ static MagickBooleanType SyncAuthenticPixelsStream(Image *image,
 %  The format of the WriteStream() method is:
 %
 %      MagickBooleanType WriteStream(const ImageInfo *image_info,Image *,
-%        StreamHandler stream)
+%        StreamHandler stream,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -2676,9 +2727,11 @@ static MagickBooleanType SyncAuthenticPixelsStream(Image *image,
 %
 %    o stream: A callback method.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType WriteStream(const ImageInfo *image_info,
-  Image *image,StreamHandler stream)
+  Image *image,StreamHandler stream,ExceptionInfo *exception)
 {
   ImageInfo
     *write_info;
@@ -2695,7 +2748,7 @@ MagickExport MagickBooleanType WriteStream(const ImageInfo *image_info,
   assert(image->signature == MagickSignature);
   write_info=CloneImageInfo(image_info);
   write_info->stream=stream;
-  status=WriteImage(write_info,image);
+  status=WriteImage(write_info,image,exception);
   write_info=DestroyImageInfo(write_info);
   return(status);
 }