Commit f764ebaed for imagemagick.org
commit f764ebaedd6c8edeb0c3df37b38f62a32542c468
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sun Nov 29 19:06:39 2026 -0500
PseudoClass images with more than 256 colors are promoted to 24/32-bit images
diff --git a/coders/sun.c b/coders/sun.c
index 8e75a07bf..4a96f0457 100644
--- a/coders/sun.c
+++ b/coders/sun.c
@@ -65,6 +65,16 @@
#include "MagickCore/string_.h"
#include "MagickCore/module.h"
+/*
+ Define declarations.
+*/
+#define RMT_EQUAL_RGB 1
+#define RMT_NONE 0
+#define RMT_RAW 2
+#define RT_STANDARD 1
+#define RT_ENCODED 2
+#define RT_FORMAT_RGB 3
+
/*
Forward declarations.
*/
@@ -217,13 +227,6 @@ static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
*/
static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
-#define RMT_EQUAL_RGB 1
-#define RMT_NONE 0
-#define RMT_RAW 2
-#define RT_STANDARD 1
-#define RT_ENCODED 2
-#define RT_FORMAT_RGB 3
-
typedef struct _SUNInfo
{
unsigned int
@@ -394,6 +397,19 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(
sun_colormap[i]);
sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
+ /*
+ Skip any bytes of the map not consumed above (maplength is not a
+ multiple of 3, or the image is 24/32-bit and has no palette).
+ */
+ if (sun_info.maplength > (3*image->colors))
+ {
+ size_t
+ skip;
+
+ skip=(size_t) sun_info.maplength-3*image->colors;
+ if (SeekBlob(image,(MagickOffsetType) skip,SEEK_CUR) < 0)
+ ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+ }
break;
}
case RMT_RAW:
@@ -430,10 +446,10 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (status == MagickFalse)
return(DestroyImageList(image));
if (sun_info.length == 0)
- ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
- number_pixels=(MagickSizeType) (image->columns*image->rows);
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ number_pixels=(MagickSizeType) image->columns*image->rows;
if ((sun_info.type != RT_ENCODED) &&
- ((number_pixels*sun_info.depth) > (8UL*sun_info.length)))
+ ((number_pixels*sun_info.depth) > ((MagickSizeType) 8*sun_info.length)))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
if (HeapOverflowSanityCheckGetSize(sun_info.width,sun_info.depth,&bytes_per_line) != MagickFalse)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
@@ -583,8 +599,6 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
bytes_per_pixel=3;
if (image->alpha_trait != UndefinedPixelTrait)
bytes_per_pixel++;
- if (bytes_per_line == 0)
- bytes_per_line=bytes_per_pixel*image->columns;
for (y=0; y < (ssize_t) image->rows; y++)
{
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
@@ -594,8 +608,11 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
if (image->alpha_trait != UndefinedPixelTrait)
SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
- if (sun_info.type == RT_STANDARD)
+ if (sun_info.type != RT_FORMAT_RGB)
{
+ /*
+ RT_STANDARD and RT_ENCODED store [A]BGR.
+ */
SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
SetPixelRed(image,ScaleCharToQuantum(*p++),q);
@@ -606,15 +623,6 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
}
- if (image->colors != 0)
- {
- SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
- GetPixelRed(image,q)].red),q);
- SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
- GetPixelGreen(image,q)].green),q);
- SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
- GetPixelBlue(image,q)].blue),q);
- }
q+=(ptrdiff_t) GetPixelChannels(image);
}
if (((bytes_per_pixel*image->columns) % 2) != 0)
@@ -630,9 +638,17 @@ static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
}
}
+ sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
+ if (y < (ssize_t) image->rows)
+ {
+ /*
+ Pixel cache failure or user cancellation; the cause is already
+ recorded in the exception.
+ */
+ return(DestroyImageList(image));
+ }
if (image->storage_class == PseudoClass)
(void) SyncImage(image,exception);
- sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
if (EOFBlob(image) != MagickFalse)
{
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
@@ -768,12 +784,6 @@ ModuleExport void UnregisterSUNImage(void)
static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
ExceptionInfo *exception)
{
-#define RMT_EQUAL_RGB 1
-#define RMT_NONE 0
-#define RMT_RAW 2
-#define RT_STANDARD 1
-#define RT_FORMAT_RGB 3
-
typedef struct _SUNInfo
{
unsigned int
@@ -791,12 +801,14 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
*p;
MagickBooleanType
+ monochrome,
status;
MagickOffsetType
scene;
MagickSizeType
+ length_bytes,
number_pixels;
size_t
@@ -824,6 +836,7 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
if (status == MagickFalse)
return(status);
+ monochrome=MagickFalse;
scene=0;
number_scenes=GetImageListLength(image);
do
@@ -833,6 +846,20 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
*/
if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
(void) TransformImageColorspace(image,sRGBColorspace,exception);
+ if ((image->storage_class == PseudoClass) &&
+ ((image->colors > 256) || (image->alpha_trait != UndefinedPixelTrait)))
+ {
+ /*
+ An 8-bit colormapped SUN raster cannot hold more than 256 colors or
+ an alpha channel; write full color instead.
+ */
+ status=SetImageStorageClass(image,DirectClass,exception);
+ if (status == MagickFalse)
+ {
+ (void) CloseBlob(image);
+ return(MagickFalse);
+ }
+ }
sun_info.magic=0x59a66a95;
if ((image->columns != (unsigned int) image->columns) ||
(image->rows != (unsigned int) image->rows))
@@ -852,15 +879,18 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
/*
Full color SUN raster.
*/
- sun_info.depth=(unsigned int) image->alpha_trait !=
- UndefinedPixelTrait ? 32U : 24U;
- sun_info.length=(unsigned int) ((image->alpha_trait !=
- UndefinedPixelTrait ? 4 : 3)*number_pixels);
- sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
- 0;
+ sun_info.depth=(image->alpha_trait != UndefinedPixelTrait) ? 32U :
+ 24U;
+ length_bytes=(MagickSizeType) ((image->alpha_trait !=
+ UndefinedPixelTrait) ? 4 : 3)*number_pixels;
+ if ((length_bytes & 0x01) != 0)
+ length_bytes+=image->rows;
+ if (length_bytes > 0xffffffffUL)
+ ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
+ sun_info.length=(unsigned int) length_bytes;
}
else
- if (SetImageMonochrome(image,exception) != MagickFalse)
+ if ((monochrome=SetImageMonochrome(image,exception)) != MagickFalse)
{
/*
Monochrome SUN raster.
@@ -941,7 +971,11 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
}
if (((bytes_per_pixel*image->columns) & 0x01) != 0)
*q++='\0'; /* pad scanline */
- (void) WriteBlob(image,(size_t) (q-pixels),pixels);
+ if (WriteBlob(image,(size_t) (q-pixels),pixels) != (ssize_t) (q-pixels))
+ {
+ status=MagickFalse;
+ break;
+ }
if (image->previous == (Image *) NULL)
{
status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -951,9 +985,11 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
}
}
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+ if (status == MagickFalse)
+ break;
}
else
- if (SetImageMonochrome(image,exception) != MagickFalse)
+ if (monochrome != MagickFalse)
{
unsigned char
bit,