GraphicsMagick: ReadBMPImage(): Re-indent/format for consistency.
GraphicsMagick Commits <[email protected]>
| Newsgroups | gmane.comp.video.graphicsmagick.cvs |
|---|---|
| Message-ID | <mailman.7826.1691523261.1439.graphicsmagick-commit@lists.sourceforge.net> |
changeset 0a8640cc344d in /hg/GraphicsMagick details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=0a8640cc344d summary: ReadBMPImage(): Re-indent/format for consistency. diffstat: ChangeLog | 6 +- coders/bmp.c | 1858 ++++++++++++++++++++++++++------------------------- www/Changelog.html | 13 + 3 files changed, 951 insertions(+), 926 deletions(-) diffs (truncated from 1964 to 500 lines): diff -r 050448a88dde -r 0a8640cc344d ChangeLog --- a/ChangeLog Tue Aug 08 18:26:17 2023 +0200 +++ b/ChangeLog Tue Aug 08 14:34:04 2023 -0500 @@ -1,7 +1,11 @@ +2023-08-08 Bob Friesenhahn <[email protected]> + + * coders/bmp.c (ReadBMPImage): Re-indent/format for consistency. + 2023-08-08 Fojtik Jaroslav <[email protected]> * coders/bmp.c: Data behind declared BMP header should not be read. - It could poison newly converted image with garbage. + It could poison newly converted image with garbage. 2023-08-08 Bob Friesenhahn <[email protected]> diff -r 050448a88dde -r 0a8640cc344d coders/bmp.c --- a/coders/bmp.c Tue Aug 08 18:26:17 2023 +0200 +++ b/coders/bmp.c Tue Aug 08 14:34:04 2023 -0500 @@ -625,988 +625,996 @@ magick[0]=magick[1]=0; count=ReadBlob(image,2,(char *) magick); do - { - PixelPacket - quantum_bits, - shift; - - magick_uint32_t - profile_data, - profile_size; + { + PixelPacket + quantum_bits, + shift; - /* - Verify BMP identifier. - */ - /* if (bmp_info.ba_offset == 0) */ /* FIXME: Investigate. Start position needs to always advance! */ - start_position=TellBlob(image)-2; - bmp_info.ba_offset=0; - /* "BA" is OS/2 bitmap array file */ - while (LocaleNCompare((char *) magick,"BA",2) == 0) - { - bmp_info.file_size=ReadBlobLSBLong(image); - bmp_info.ba_offset=ReadBlobLSBLong(image); - bmp_info.offset_bits=ReadBlobLSBLong(image); - if ((count=ReadBlob(image,2,(char *) magick)) != 2) - break; - } + magick_uint32_t + profile_data, + profile_size; - if (count != 2) /* Found "BA" header from above above */ - ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); - - if (logging ) - (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c", - magick[0],magick[1]); + /* + Verify BMP identifier. + */ + /* if (bmp_info.ba_offset == 0) */ /* FIXME: Investigate. Start position needs to always advance! */ + start_position=TellBlob(image)-2; + bmp_info.ba_offset=0; + /* "BA" is OS/2 bitmap array file */ + while (LocaleNCompare((char *) magick,"BA",2) == 0) + { + bmp_info.file_size=ReadBlobLSBLong(image); + bmp_info.ba_offset=ReadBlobLSBLong(image); + bmp_info.offset_bits=ReadBlobLSBLong(image); + if ((count=ReadBlob(image,2,(char *) magick)) != 2) + break; + } - bmp_info.file_size=ReadBlobLSBLong(image); /* File size in bytes */ - if (logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " File size: Claimed=%" MAGICK_SIZE_T_F "u, Actual=%" - MAGICK_OFF_F "d", - (MAGICK_SIZE_T) bmp_info.file_size, file_size); - (void) ReadBlobLSBLong(image); /* Reserved */ - bmp_info.offset_bits=ReadBlobLSBLong(image); /* Bit map offset from start of file */ - bmp_info.size=ReadBlobLSBLong(image); /* BMP Header size */ - if (logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Header size: %u\n" - " Offset bits: %u\n" - " Image data offset: %u", - bmp_info.size, - bmp_info.offset_bits, - bmp_info.ba_offset); + if (count != 2) /* Found "BA" header from above above */ + ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); - if(LocaleNCompare((char *) magick,"BM",2) != 0) /* "BM" is Windows or OS/2 file. */ - { - if((LocaleNCompare((char *) magick,"CI",2) != 0) || /* "CI" is OS/2 Color Icon */ - (bmp_info.size!=12 && bmp_info.size!=40)) /* CI chunk must have biSize only 12 or 40 */ - ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); - } - - if ((bmp_info.file_size != 0) && ((magick_off_t) bmp_info.file_size > file_size)) - ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); - if (bmp_info.offset_bits < bmp_info.size) - ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); + if (logging ) + (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c", + magick[0],magick[1]); - if (bmp_info.size == 12) - { - /* - Windows 2.X or OS/2 BMP image file. - */ - bmp_info.width=(magick_int16_t) ReadBlobLSBShort(image); /* Width */ - bmp_info.height=(magick_int16_t) ReadBlobLSBShort(image); /* Height */ - bmp_info.planes=ReadBlobLSBShort(image); /* # of color planes */ - bmp_info.bits_per_pixel=ReadBlobLSBShort(image); /* Bits per pixel */ - bmp_info.x_pixels=0; - bmp_info.y_pixels=0; - bmp_info.number_colors=0; - bmp_info.compression=BI_RGB; - bmp_info.image_size=0; - bmp_info.alpha_mask=0; - if (logging) - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Format: Windows 2.X or OS/2 Bitmap"); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Geometry: %dx%d",bmp_info.width,bmp_info.height); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Planes: %u",bmp_info.planes); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Bits per pixel: %u",bmp_info.bits_per_pixel); - } - } - else - { - /* - Microsoft Windows 3.X or later BMP image file. - */ - switch(bmp_info.size) + bmp_info.file_size=ReadBlobLSBLong(image); /* File size in bytes */ + if (logging) + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " File size: Claimed=%" MAGICK_SIZE_T_F "u, Actual=%" + MAGICK_OFF_F "d", + (MAGICK_SIZE_T) bmp_info.file_size, file_size); + (void) ReadBlobLSBLong(image); /* Reserved */ + bmp_info.offset_bits=ReadBlobLSBLong(image); /* Bit map offset from start of file */ + bmp_info.size=ReadBlobLSBLong(image); /* BMP Header size */ + if (logging) + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Header size: %u\n" + " Offset bits: %u\n" + " Image data offset: %u", + bmp_info.size, + bmp_info.offset_bits, + bmp_info.ba_offset); + + if (LocaleNCompare((char *) magick,"BM",2) != 0) /* "BM" is Windows or OS/2 file. */ { - case 40: - case 56: - case 78: - case 108: break; - default: if(bmp_info.size <= 64) - ThrowBMPReaderException(CorruptImageError, NonOS2HeaderSizeError, image); - break; + if ((LocaleNCompare((char *) magick,"CI",2) != 0) || /* "CI" is OS/2 Color Icon */ + (bmp_info.size!=12 && bmp_info.size!=40)) /* CI chunk must have biSize only 12 or 40 */ + ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); } - /* - BMP v3 defines width and hight as signed LONG (32 bit) values. If - height is a positive number, then the image is a "bottom-up" - bitmap with origin in the lower-left corner. If height is a - negative number, then the image is a "top-down" bitmap with the - origin in the upper-left corner. The meaning of negative values - is not defined for width. - */ - bmp_info.width=(magick_int32_t) ReadBlobLSBLong(image); /* Width */ - bmp_info.height=(magick_int32_t) ReadBlobLSBLong(image); /* Height */ - bmp_info.planes=ReadBlobLSBShort(image); /* # of color planes */ - bmp_info.bits_per_pixel=ReadBlobLSBShort(image); /* Bits per pixel (1/4/8/16/24/32) */ - bmp_info.compression=ReadBlobLSBLong(image); /* Compression method */ - bmp_info.image_size=ReadBlobLSBLong(image); /* Bitmap size (bytes) */ - bmp_info.x_pixels=ReadBlobLSBLong(image); /* Horizontal resolution (pixels/meter) */ - bmp_info.y_pixels=ReadBlobLSBLong(image); /* Vertical resolution (pixels/meter) */ - bmp_info.number_colors=ReadBlobLSBLong(image); /* Number of colors */ - bmp_info.colors_important=ReadBlobLSBLong(image); /* Minimum important colors */ - profile_data=0; - profile_size=0; - if (logging) - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Format: MS Windows bitmap 3.X"); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Geometry: %dx%d",bmp_info.width,bmp_info.height); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Planes: %u",bmp_info.planes); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Bits per pixel: %u",bmp_info.bits_per_pixel); - switch ((int) bmp_info.compression) + if ((bmp_info.file_size != 0) && ((magick_off_t) bmp_info.file_size > file_size)) + ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); + if (bmp_info.offset_bits < bmp_info.size) + ThrowBMPReaderException(CorruptImageError,ImproperImageHeader,image); + + if (bmp_info.size == 12) + { + /* + Windows 2.X or OS/2 BMP image file. + */ + bmp_info.width=(magick_int16_t) ReadBlobLSBShort(image); /* Width */ + bmp_info.height=(magick_int16_t) ReadBlobLSBShort(image); /* Height */ + bmp_info.planes=ReadBlobLSBShort(image); /* # of color planes */ + bmp_info.bits_per_pixel=ReadBlobLSBShort(image); /* Bits per pixel */ + bmp_info.x_pixels=0; + bmp_info.y_pixels=0; + bmp_info.number_colors=0; + bmp_info.compression=BI_RGB; + bmp_info.image_size=0; + bmp_info.alpha_mask=0; + if (logging) { - case BI_RGB: - { - /* uncompressed */ - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_RGB"); - break; - } - case BI_RLE4: - { - /* 4 bit RLE */ - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_RLE4"); - break; - } - case BI_RLE8: - { - /* 8 bit RLE */ - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_RLE8"); - break; - } - case BI_BITFIELDS: - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_BITFIELDS"); - break; - } - case BI_PNG: - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_PNG"); - break; - } - case BI_JPEG: - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: BI_JPEG"); - break; - } - default: - { - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Compression: UNKNOWN (%u)",bmp_info.compression); - } + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Format: Windows 2.X or OS/2 Bitmap"); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Geometry: %dx%d",bmp_info.width,bmp_info.height); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Planes: %u",bmp_info.planes); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Bits per pixel: %u",bmp_info.bits_per_pixel); } - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Number of colors: %u",bmp_info.number_colors); - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " Important colors: %u",bmp_info.colors_important); - } - - if(bmp_info.size >= 52) + } + else { - bmp_info.red_mask=ReadBlobLSBLong(image); - bmp_info.green_mask=ReadBlobLSBLong(image); - bmp_info.blue_mask=ReadBlobLSBLong(image); - if(bmp_info.size >= 56) - { - /* - Read color management information. - */ - bmp_info.alpha_mask=ReadBlobLSBLong(image); - if (image->logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - "Alpha Mask: 0x%04x", - bmp_info.alpha_mask); - - if(bmp_info.size > 64) + /* + Microsoft Windows 3.X or later BMP image file. + */ + switch(bmp_info.size) { - /* - https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapv4header - */ - magick_uint32_t - v4_red_primary_x, v4_red_primary_y, v4_red_primary_z, - v4_green_primary_x, v4_green_primary_y, v4_green_primary_z, - v4_blue_primary_x, v4_blue_primary_y, v4_blue_primary_z, - v4_gamma_x, v4_gamma_y, v4_gamma_z; - - double - bmp_gamma, - sum; - - bmp_info.colorspace=(magick_int32_t) ReadBlobLSBLong(image); - if (image->logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - "BMP Colorspace: 0x%04x", - bmp_info.colorspace); - - v4_red_primary_x=ReadBlobLSBLong(image); - v4_red_primary_y=ReadBlobLSBLong(image); - v4_red_primary_z=ReadBlobLSBLong(image); - v4_green_primary_x=ReadBlobLSBLong(image); - v4_green_primary_y=ReadBlobLSBLong(image); - v4_green_primary_z=ReadBlobLSBLong(image); - v4_blue_primary_x=ReadBlobLSBLong(image); - v4_blue_primary_y=ReadBlobLSBLong(image); - v4_blue_primary_z=ReadBlobLSBLong(image); - v4_gamma_x = ReadBlobLSBLong(image); - v4_gamma_y = ReadBlobLSBLong(image); - v4_gamma_z = ReadBlobLSBLong(image); - - if (LCS_CALIBRATED_RGB == bmp_info.colorspace) - { - /* - Decode 2^30 fixed point formatted CIE primaries. - https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-ciexyztriple - https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-ciexyz - */ - bmp_info.red_primary.x=(double) v4_red_primary_x/0x3ffffff; - bmp_info.red_primary.y=(double) v4_red_primary_y/0x3ffffff; - bmp_info.red_primary.z=(double) v4_red_primary_z/0x3ffffff; - - bmp_info.green_primary.x=(double) v4_green_primary_x/0x3ffffff; - bmp_info.green_primary.y=(double) v4_green_primary_y/0x3ffffff; - bmp_info.green_primary.z=(double) v4_green_primary_z/0x3ffffff; - - bmp_info.blue_primary.x=(double) v4_blue_primary_x/0x3ffffff; - bmp_info.blue_primary.y=(double) v4_blue_primary_y/0x3ffffff; - bmp_info.blue_primary.z=(double) v4_blue_primary_z/0x3ffffff; + case 40: + case 56: + case 78: + case 108: break; + default: if (bmp_info.size <= 64) + ThrowBMPReaderException(CorruptImageError, NonOS2HeaderSizeError, image); + break; + } - if (image->logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - "BMP Primaries: red(%g,%g,%g), green(%g,%g,%g), blue(%g,%g,%g)", - bmp_info.red_primary.x, bmp_info.red_primary.y, bmp_info.red_primary.z, - bmp_info.green_primary.x, bmp_info.green_primary.y, bmp_info.green_primary.z, - bmp_info.blue_primary.x, bmp_info.blue_primary.y, bmp_info.blue_primary.z); - - sum=bmp_info.red_primary.x+bmp_info.red_primary.y+bmp_info.red_primary.z; - sum=Max(MagickEpsilon,sum); - bmp_info.red_primary.x/=sum; - bmp_info.red_primary.y/=sum; - image->chromaticity.red_primary.x=bmp_info.red_primary.x; - image->chromaticity.red_primary.y=bmp_info.red_primary.y; - - sum=bmp_info.green_primary.x+bmp_info.green_primary.y+bmp_info.green_primary.z; - sum=Max(MagickEpsilon,sum); - bmp_info.green_primary.x/=sum; - bmp_info.green_primary.y/=sum; - image->chromaticity.green_primary.x=bmp_info.green_primary.x; - image->chromaticity.green_primary.y=bmp_info.green_primary.y; - - sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+bmp_info.blue_primary.z; - sum=Max(MagickEpsilon,sum); - bmp_info.blue_primary.x/=sum; - bmp_info.blue_primary.y/=sum; - image->chromaticity.blue_primary.x=bmp_info.blue_primary.x; - image->chromaticity.blue_primary.y=bmp_info.blue_primary.y; - - /* - Decode 16^16 fixed point formatted gamma_scales. - Gamma encoded in unsigned fixed 16.16 format. The - upper 16 bits are the unsigned integer value. The - lower 16 bits are the fractional part. - */ - bmp_info.gamma_scale.x=v4_gamma_x/0xffff; - bmp_info.gamma_scale.y=v4_gamma_y/0xffff; - bmp_info.gamma_scale.z=v4_gamma_z/0xffff; - - /* - Compute a single averaged gamma from the BMP 3-channel gamma. - */ - bmp_gamma = (bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+ - bmp_info.gamma_scale.z)/3.0; - if (image->logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - "BMP Gamma: %g", bmp_gamma); - /* This range is based on what libpng is willing to accept */ - if (bmp_gamma > 0.00016 && bmp_gamma < 6250.0) + /* + BMP v3 defines width and hight as signed LONG (32 bit) values. If + height is a positive number, then the image is a "bottom-up" + bitmap with origin in the lower-left corner. If height is a + negative number, then the image is a "top-down" bitmap with the + origin in the upper-left corner. The meaning of negative values + is not defined for width. + */ + bmp_info.width=(magick_int32_t) ReadBlobLSBLong(image); /* Width */ + bmp_info.height=(magick_int32_t) ReadBlobLSBLong(image); /* Height */ + bmp_info.planes=ReadBlobLSBShort(image); /* # of color planes */ + bmp_info.bits_per_pixel=ReadBlobLSBShort(image); /* Bits per pixel (1/4/8/16/24/32) */ + bmp_info.compression=ReadBlobLSBLong(image); /* Compression method */ + bmp_info.image_size=ReadBlobLSBLong(image); /* Bitmap size (bytes) */ + bmp_info.x_pixels=ReadBlobLSBLong(image); /* Horizontal resolution (pixels/meter) */ + bmp_info.y_pixels=ReadBlobLSBLong(image); /* Vertical resolution (pixels/meter) */ + bmp_info.number_colors=ReadBlobLSBLong(image); /* Number of colors */ + bmp_info.colors_important=ReadBlobLSBLong(image); /* Minimum important colors */ + profile_data=0; + profile_size=0; + if (logging) + { + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Format: MS Windows bitmap 3.X"); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Geometry: %dx%d",bmp_info.width,bmp_info.height); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Planes: %u",bmp_info.planes); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Bits per pixel: %u",bmp_info.bits_per_pixel); + switch ((int) bmp_info.compression) + { + case BI_RGB: { - image->gamma=bmp_gamma; + /* uncompressed */ + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_RGB"); + break; } - else + case BI_RLE4: + { + /* 4 bit RLE */ + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_RLE4"); + break; + } + case BI_RLE8: + { + /* 8 bit RLE */ + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_RLE8"); + break; + } + case BI_BITFIELDS: { - if (image->logging) - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - "Ignoring illegal BMP gamma value %g " - "(gamma scale xyz %g,%g,%g)", - bmp_gamma, bmp_info.gamma_scale.x, - bmp_info.gamma_scale.y, bmp_info.gamma_scale.z); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_BITFIELDS"); + break; + } + case BI_PNG: + { + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_PNG"); + break; + } + case BI_JPEG: + { + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Compression: BI_JPEG"); + break; + } + default: