]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/stream.c
(no commit message)
[imagemagick] / MagickCore / stream.c
index cb0f3c6d35b58b19c8bac8dceba33bbfff143caf..d32ae859d930959622dfba15363f1cb07f5ab278 100644 (file)
 %                       MagickCore Pixel Stream Methods                       %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 March 2000                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2012 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"
@@ -161,8 +162,8 @@ 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");
@@ -199,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;
@@ -234,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
@@ -272,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)
     {
@@ -454,18 +455,16 @@ static MagickBooleanType GetOneAuthenticPixelFromStream(Image *image,
   q=GetAuthenticPixelsStream(image,x,y,1,1,exception);
   if (q != (Quantum *) NULL)
     {
-      pixel[RedPixelChannel]=image->background_color.red;
-      pixel[GreenPixelChannel]=image->background_color.green;
-      pixel[BluePixelChannel]=image->background_color.blue;
-      pixel[AlphaPixelChannel]=image->background_color.alpha;
+      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;
-
-    channel=GetPixelChannelMapChannel(image,i);
+    PixelChannel channel=GetPixelChannelChannel(image,i);
     pixel[channel]=q[i];
   }
   return(MagickTrue);
@@ -520,18 +519,16 @@ static MagickBooleanType GetOneVirtualPixelFromStream(const Image *image,
   p=GetVirtualPixelStream(image,virtual_pixel_method,x,y,1,1,exception);
   if (p == (const Quantum *) NULL)
     {
-      pixel[RedPixelChannel]=image->background_color.red;
-      pixel[GreenPixelChannel]=image->background_color.green;
-      pixel[BluePixelChannel]=image->background_color.blue;
-      pixel[AlphaPixelChannel]=image->background_color.alpha;
+      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;
-
-    channel=GetPixelChannelMapChannel(image,i);
+    PixelChannel channel=GetPixelChannelChannel(image,i);
     pixel[channel]=p[i];
   }
   return(MagickTrue);
@@ -617,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:
@@ -630,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;
@@ -687,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)
     {
@@ -745,6 +741,8 @@ static const Quantum *GetVirtualPixelStream(const Image *image,
   */
   number_pixels=(MagickSizeType) columns*rows;
   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)
@@ -758,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;
@@ -854,6 +852,9 @@ static Quantum *QueueAuthenticPixelsStream(Image *image,const ssize_t x,
   CacheInfo
     *cache_info;
 
+  MagickBooleanType
+    status;
+
   MagickSizeType
     number_pixels;
 
@@ -904,22 +905,32 @@ static Quantum *QueueAuthenticPixelsStream(Image *image,const ssize_t x,
   cache_info->rows=rows;
   number_pixels=(MagickSizeType) columns*rows;
   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*
@@ -1154,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;
   }
@@ -1175,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);
@@ -1239,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);
@@ -1452,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));
@@ -1470,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;
@@ -1565,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:
@@ -1961,7 +1975,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
       }
       break;
     }
-    case IntegerPixel:
+    case LongPixel:
     {
       register unsigned int
         *q;
@@ -1974,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;
@@ -1988,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;
@@ -2003,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;
@@ -2018,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;
@@ -2031,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;
@@ -2045,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;
@@ -2061,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;
@@ -2082,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++;
         }
@@ -2128,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);
@@ -2141,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;
@@ -2155,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;
@@ -2170,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;
@@ -2185,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;
@@ -2197,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;
@@ -2211,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;
@@ -2226,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;
@@ -2247,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++;
         }
@@ -2322,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;
@@ -2349,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;
@@ -2378,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;
@@ -2444,7 +2457,7 @@ static MagickBooleanType StreamImagePixels(const StreamInfo *stream_info,
             }
             case IndexQuantum:
             {
-              *q=(GetPixelIntensity(image,p));
+              *q=ClampToQuantum(GetPixelIntensity(image,p));
               break;
             }
             default:
@@ -2513,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;
@@ -2608,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: