Skip to content

Commit

Permalink
move check for number of colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristy committed Sep 30, 2023
1 parent 1926121 commit c504e54
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions coders/bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,19 @@ static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
/*
Microsoft Windows BMP image file.
*/
if (bmp_info.size < 40)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
switch (bmp_info.size)
{
case 40: case 52: case 56: case 64: case 78: case 108: case 124:
{
break;
}
default:
{
if (bmp_info.size < 64)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
break;
}
}
bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
bmp_info.planes=ReadBlobLSBShort(image);
Expand Down Expand Up @@ -974,16 +985,24 @@ static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
if (bmp_info.planes != 1)
ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
(bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
(bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
if (bmp_info.bits_per_pixel < 16 &&
bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
switch (bmp_info.bits_per_pixel)
{
case 1: case 4: case 8: case 16: case 24: case 32: case 64:
{
break;
}
default:
ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
}
if ((bmp_info.bits_per_pixel < 16) &&
(bmp_info.number_colors > (1U << bmp_info.bits_per_pixel)))
ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
if ((MagickSizeType) bmp_info.number_colors > blob_size)
ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
if ((bmp_info.compression == BI_RLE8) && (bmp_info.bits_per_pixel != 8))
ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
if ((bmp_info.compression == BI_RLE4) && (bmp_info.bits_per_pixel != 4))
if ((bmp_info.compression == BI_RLE4) &&
(bmp_info.bits_per_pixel != 4))
ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
if ((bmp_info.compression == BI_BITFIELDS) && (bmp_info.bits_per_pixel < 16))
ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
Expand Down Expand Up @@ -1549,6 +1568,42 @@ static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
break;
}
case 64:
{
/*
Convert DirectColor scanline.
*/
bytes_per_line=4*((image->columns*64+31)/32);
for (y=(ssize_t) image->rows-1; y >= 0; y--)
{
unsigned short
*p16;

p16=(unsigned short *) pixels+(image->rows-y-1)*bytes_per_line;
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (PixelPacket *) NULL)
break;
for (x=0; x < (ssize_t) image->columns; x++)
{
SetPixelBlue(q,ScaleCharToQuantum(*p16++));
SetPixelGreen(q,ScaleCharToQuantum(*p16++));
SetPixelRed(q,ScaleCharToQuantum(*p16++));
SetPixelAlpha(q,ScaleCharToQuantum(*p16++));
q++;
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
offset=(MagickOffsetType) (image->rows-y-1);
if (image->previous == (Image *) NULL)
{
status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
(image->rows-y),image->rows);
if (status == MagickFalse)
break;
}
}
break;
}
default:
{
pixel_info=RelinquishVirtualMemory(pixel_info);
Expand Down

0 comments on commit c504e54

Please sign in to comment.