diff --git a/CHANGES.rst b/CHANGES.rst index 33226eac5..ae917acb1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changelog (Pillow) 2.4.0 (2014-04-01 est.) ------------------ +- Fixed DOS with invalid palette size or invalid image size in BMP file + [wiredfool] + +- Added support for BMP version 4 and 5 + [eddwardo, wiredfool] + - Fix segfault in getfont when passed a memory resident font [wiredfool] diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index a4eb1d3f2..436ca5dce 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -82,7 +82,7 @@ class BmpImageFile(ImageFile.ImageFile): colors = 0 direction = -1 - elif len(s) in [40, 64]: + elif len(s) in [40, 64, 108, 124]: # WIN 3.1 or OS/2 2.0 INFO bits = i16(s[14:]) @@ -99,6 +99,10 @@ class BmpImageFile(ImageFile.ImageFile): else: raise IOError("Unsupported BMP header type (%d)" % len(s)) + if (self.size[0]*self.size[1]) > 2**31: + # Prevent DOS for > 2gb images + raise IOError("Unsupported BMP Size: (%dx%d)" % self.size) + if not colors: colors = 1 << bits @@ -129,6 +133,8 @@ class BmpImageFile(ImageFile.ImageFile): greyscale = 1 if colors == 2: indices = (0, 255) + elif colors > 2**16 or colors <=0: #We're reading a i32. + raise IOError("Unsupported BMP Palette size (%d)" % colors) else: indices = list(range(colors)) for i in indices: diff --git a/Tests/images/bmp/README.txt b/Tests/images/bmp/README.txt new file mode 100644 index 000000000..4829834f4 --- /dev/null +++ b/Tests/images/bmp/README.txt @@ -0,0 +1,3 @@ +These images are from the bmpsuite: +https://github.com/jsummers/bmpsuite and are in the public domain +according to the readme in the project. diff --git a/Tests/images/bmp/b/badbitcount.bmp b/Tests/images/bmp/b/badbitcount.bmp new file mode 100644 index 000000000..d4fa4e8b8 Binary files /dev/null and b/Tests/images/bmp/b/badbitcount.bmp differ diff --git a/Tests/images/bmp/b/badbitssize.bmp b/Tests/images/bmp/b/badbitssize.bmp new file mode 100644 index 000000000..0a99a605a Binary files /dev/null and b/Tests/images/bmp/b/badbitssize.bmp differ diff --git a/Tests/images/bmp/b/baddens1.bmp b/Tests/images/bmp/b/baddens1.bmp new file mode 100644 index 000000000..a6150a6fe Binary files /dev/null and b/Tests/images/bmp/b/baddens1.bmp differ diff --git a/Tests/images/bmp/b/baddens2.bmp b/Tests/images/bmp/b/baddens2.bmp new file mode 100644 index 000000000..f2c1dfb66 Binary files /dev/null and b/Tests/images/bmp/b/baddens2.bmp differ diff --git a/Tests/images/bmp/b/badfilesize.bmp b/Tests/images/bmp/b/badfilesize.bmp new file mode 100644 index 000000000..da52cb51d Binary files /dev/null and b/Tests/images/bmp/b/badfilesize.bmp differ diff --git a/Tests/images/bmp/b/badheadersize.bmp b/Tests/images/bmp/b/badheadersize.bmp new file mode 100644 index 000000000..2a4083a6f Binary files /dev/null and b/Tests/images/bmp/b/badheadersize.bmp differ diff --git a/Tests/images/bmp/b/badpalettesize.bmp b/Tests/images/bmp/b/badpalettesize.bmp new file mode 100644 index 000000000..7d9d1b745 Binary files /dev/null and b/Tests/images/bmp/b/badpalettesize.bmp differ diff --git a/Tests/images/bmp/b/badplanes.bmp b/Tests/images/bmp/b/badplanes.bmp new file mode 100644 index 000000000..92d2855b6 Binary files /dev/null and b/Tests/images/bmp/b/badplanes.bmp differ diff --git a/Tests/images/bmp/b/badrle.bmp b/Tests/images/bmp/b/badrle.bmp new file mode 100644 index 000000000..cbf8fdc2e Binary files /dev/null and b/Tests/images/bmp/b/badrle.bmp differ diff --git a/Tests/images/bmp/b/badwidth.bmp b/Tests/images/bmp/b/badwidth.bmp new file mode 100644 index 000000000..9fca005dc Binary files /dev/null and b/Tests/images/bmp/b/badwidth.bmp differ diff --git a/Tests/images/bmp/b/pal8badindex.bmp b/Tests/images/bmp/b/pal8badindex.bmp new file mode 100644 index 000000000..efe16c05c Binary files /dev/null and b/Tests/images/bmp/b/pal8badindex.bmp differ diff --git a/Tests/images/bmp/b/reallybig.bmp b/Tests/images/bmp/b/reallybig.bmp new file mode 100644 index 000000000..101e0b494 Binary files /dev/null and b/Tests/images/bmp/b/reallybig.bmp differ diff --git a/Tests/images/bmp/b/rletopdown.bmp b/Tests/images/bmp/b/rletopdown.bmp new file mode 100644 index 000000000..21a909fda Binary files /dev/null and b/Tests/images/bmp/b/rletopdown.bmp differ diff --git a/Tests/images/bmp/b/shortfile.bmp b/Tests/images/bmp/b/shortfile.bmp new file mode 100644 index 000000000..73960797b Binary files /dev/null and b/Tests/images/bmp/b/shortfile.bmp differ diff --git a/Tests/images/bmp/g/pal1.bmp b/Tests/images/bmp/g/pal1.bmp new file mode 100644 index 000000000..4776f8277 Binary files /dev/null and b/Tests/images/bmp/g/pal1.bmp differ diff --git a/Tests/images/bmp/g/pal1bg.bmp b/Tests/images/bmp/g/pal1bg.bmp new file mode 100644 index 000000000..466d0ba72 Binary files /dev/null and b/Tests/images/bmp/g/pal1bg.bmp differ diff --git a/Tests/images/bmp/g/pal1wb.bmp b/Tests/images/bmp/g/pal1wb.bmp new file mode 100644 index 000000000..56cb93203 Binary files /dev/null and b/Tests/images/bmp/g/pal1wb.bmp differ diff --git a/Tests/images/bmp/g/pal4.bmp b/Tests/images/bmp/g/pal4.bmp new file mode 100644 index 000000000..7fd36303c Binary files /dev/null and b/Tests/images/bmp/g/pal4.bmp differ diff --git a/Tests/images/bmp/g/pal4rle.bmp b/Tests/images/bmp/g/pal4rle.bmp new file mode 100644 index 000000000..a5672aebd Binary files /dev/null and b/Tests/images/bmp/g/pal4rle.bmp differ diff --git a/Tests/images/bmp/g/pal8-0.bmp b/Tests/images/bmp/g/pal8-0.bmp new file mode 100644 index 000000000..ab8815a36 Binary files /dev/null and b/Tests/images/bmp/g/pal8-0.bmp differ diff --git a/Tests/images/bmp/g/pal8.bmp b/Tests/images/bmp/g/pal8.bmp new file mode 100644 index 000000000..96b2f8668 Binary files /dev/null and b/Tests/images/bmp/g/pal8.bmp differ diff --git a/Tests/images/bmp/g/pal8nonsquare.bmp b/Tests/images/bmp/g/pal8nonsquare.bmp new file mode 100644 index 000000000..0aa8de04c Binary files /dev/null and b/Tests/images/bmp/g/pal8nonsquare.bmp differ diff --git a/Tests/images/bmp/g/pal8os2.bmp b/Tests/images/bmp/g/pal8os2.bmp new file mode 100644 index 000000000..14901b388 Binary files /dev/null and b/Tests/images/bmp/g/pal8os2.bmp differ diff --git a/Tests/images/bmp/g/pal8rle.bmp b/Tests/images/bmp/g/pal8rle.bmp new file mode 100644 index 000000000..d43101490 Binary files /dev/null and b/Tests/images/bmp/g/pal8rle.bmp differ diff --git a/Tests/images/bmp/g/pal8topdown.bmp b/Tests/images/bmp/g/pal8topdown.bmp new file mode 100644 index 000000000..4b2f8e019 Binary files /dev/null and b/Tests/images/bmp/g/pal8topdown.bmp differ diff --git a/Tests/images/bmp/g/pal8v4.bmp b/Tests/images/bmp/g/pal8v4.bmp new file mode 100644 index 000000000..7064be315 Binary files /dev/null and b/Tests/images/bmp/g/pal8v4.bmp differ diff --git a/Tests/images/bmp/g/pal8v5.bmp b/Tests/images/bmp/g/pal8v5.bmp new file mode 100644 index 000000000..c54647a31 Binary files /dev/null and b/Tests/images/bmp/g/pal8v5.bmp differ diff --git a/Tests/images/bmp/g/pal8w124.bmp b/Tests/images/bmp/g/pal8w124.bmp new file mode 100644 index 000000000..b7cc2d8bf Binary files /dev/null and b/Tests/images/bmp/g/pal8w124.bmp differ diff --git a/Tests/images/bmp/g/pal8w125.bmp b/Tests/images/bmp/g/pal8w125.bmp new file mode 100644 index 000000000..06efed744 Binary files /dev/null and b/Tests/images/bmp/g/pal8w125.bmp differ diff --git a/Tests/images/bmp/g/pal8w126.bmp b/Tests/images/bmp/g/pal8w126.bmp new file mode 100644 index 000000000..112aa9fe6 Binary files /dev/null and b/Tests/images/bmp/g/pal8w126.bmp differ diff --git a/Tests/images/bmp/g/rgb16-565.bmp b/Tests/images/bmp/g/rgb16-565.bmp new file mode 100644 index 000000000..c03a27975 Binary files /dev/null and b/Tests/images/bmp/g/rgb16-565.bmp differ diff --git a/Tests/images/bmp/g/rgb16-565pal.bmp b/Tests/images/bmp/g/rgb16-565pal.bmp new file mode 100644 index 000000000..e7632e344 Binary files /dev/null and b/Tests/images/bmp/g/rgb16-565pal.bmp differ diff --git a/Tests/images/bmp/g/rgb16.bmp b/Tests/images/bmp/g/rgb16.bmp new file mode 100644 index 000000000..6bfe47af4 Binary files /dev/null and b/Tests/images/bmp/g/rgb16.bmp differ diff --git a/Tests/images/bmp/g/rgb24.bmp b/Tests/images/bmp/g/rgb24.bmp new file mode 100644 index 000000000..40f8bb094 Binary files /dev/null and b/Tests/images/bmp/g/rgb24.bmp differ diff --git a/Tests/images/bmp/g/rgb24pal.bmp b/Tests/images/bmp/g/rgb24pal.bmp new file mode 100644 index 000000000..102e971dd Binary files /dev/null and b/Tests/images/bmp/g/rgb24pal.bmp differ diff --git a/Tests/images/bmp/g/rgb32.bmp b/Tests/images/bmp/g/rgb32.bmp new file mode 100644 index 000000000..5d57eaaea Binary files /dev/null and b/Tests/images/bmp/g/rgb32.bmp differ diff --git a/Tests/images/bmp/g/rgb32bf.bmp b/Tests/images/bmp/g/rgb32bf.bmp new file mode 100644 index 000000000..20fa9a132 Binary files /dev/null and b/Tests/images/bmp/g/rgb32bf.bmp differ diff --git a/Tests/images/bmp/html/bkgd.png b/Tests/images/bmp/html/bkgd.png new file mode 100644 index 000000000..d66ca9d65 Binary files /dev/null and b/Tests/images/bmp/html/bkgd.png differ diff --git a/Tests/images/bmp/html/bmpsuite.html b/Tests/images/bmp/html/bmpsuite.html new file mode 100644 index 000000000..6604102bb --- /dev/null +++ b/Tests/images/bmp/html/bmpsuite.html @@ -0,0 +1,578 @@ + + + + + +BMP Suite Image List + + + + + + + +

BMP Suite Image List

+ +

For BMP Suite +version 2.3

+ +

This document describes the images in BMP Suite, and shows what +I allege to be the correct way to interpret them. PNG and JPEG images are +used for reference. +

+ +

It also shows how your web browser displays the BMP images, +but that’s not its main purpose. +BMP is poor image format to use on web pages, so a web browser’s +level of support for it is arguably not important.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FileVer.Correct displayIn your browserNotes
g/pal1.bmp31 bit/pixel paletted image, in which black is the first color in + the palette.
g/pal1wb.bmp31 bit/pixel paletted image, in which white is the first color in + the palette.
g/pal1bg.bmp31 bit/pixel paletted image, with colors other than black and white.
q/pal1p1.bmp31 bit/pixel paletted image, with only one color in the palette. + The documentation says that 1-bpp images have a palette size of 2 + (not “up to 2”), but it would be silly for a viewer not to + support a size of 1.
q/pal2.bmp3A paletted image with 2 bits/pixel. Usually only 1, 4, + and 8 are allowed, but 2 is legal on Windows CE.
g/pal4.bmp3Paletted image with 12 palette colors, and 4 bits/pixel.
g/pal4rle.bmp34-bit image that uses RLE compression.
q/pal4rletrns.bmp3
+ or

+ or
An RLE-compressed image that used “delta” + codes to skip over some pixels, leaving them undefined. Some viewers + make undefined pixels transparent, others make them black, and + others assign them palette color 0 (purple, in this case).
g/pal8.bmp3Our standard paletted image, with 252 palette colors, and 8 + bits/pixel.
g/pal8-0.bmp3Every field that can be set to 0 is set to 0: pixels/meter=0; + colors used=0 (meaning the default 256); size-of-image=0.
g/pal8rle.bmp38-bit image that uses RLE compression.
q/pal8rletrns.bmp3
+ or

+ or
8-bit version of q/pal4rletrns.bmp.
g/pal8w126.bmp3Images with different widths and heights. + In BMP format, rows are padded to a multiple of four bytes, so we + test all four possibilities.
g/pal8w125.bmp3
g/pal8w124.bmp3
g/pal8topdown.bmp3BMP images are normally stored from the bottom up, but + there is a way to store them from the top down.
q/pal8offs.bmp3A file with some unused bytes between the palette and the + image. This is probably valid, but I’m not 100% sure.
q/pal8oversizepal.bmp3An 8-bit image with 300 palette colors. This may be invalid, + because the documentation could + be interpreted to imply that 8-bit images aren’t allowed + to have more than 256 colors.
g/pal8nonsquare.bmp3 +
+ or
+ +
An image with non-square pixels: the X pixels/meter is twice + the Y pixels/meter. Image editors can be expected to + leave the image “squashed”; image viewers should + consider stretching it to its correct proportions.
g/pal8os2.bmpOS/2v1An OS/2-style bitmap.
q/pal8os2sp.bmpOS/2v1An OS/2v1 with a less-than-full-sized palette. + Probably not valid, but such files have been seen in the wild.
q/pal8os2v2.bmpOS/2v2My attempt to make an OS/2v2 bitmap.
q/pal8os2v2-16.bmpOS/2v2An OS/2v2 bitmap whose header has only 16 bytes, instead of the full 64.
g/pal8v4.bmp4A v4 bitmap. I’m not sure that the gamma and chromaticity values in + this file are sensible, because I can’t find any detailed documentation + of them.
g/pal8v5.bmp5A v5 bitmap. Version 5 has additional colorspace options over v4, so it + is easier to create, and ought to be more portable.
g/rgb16.bmp3A 16-bit image with the default color format: 5 bits each for red, + green, and blue, and 1 unused bit. + The whitest colors should (I assume) be displayed as pure white: + (255,255,255), not + (248,248,248).
g/rgb16-565.bmp3A 16-bit image with a BITFIELDS segment indicating 5 red, 6 green, + and 5 blue bits. This is a standard 16-bit format, even supported by + old versions of Windows that don’t support any other non-default 16-bit + formats. + The whitest colors should be displayed as pure white: + (255,255,255), not + (248,252,248).
g/rgb16-565pal.bmp3A 16-bit image with both a BITFIELDS segment and a palette.
q/rgb16-231.bmp3An unusual and silly 16-bit image, with 2 red bits, 3 green bits, and 1 + blue bit. Most viewers do support this image, but the colors may be darkened + with a yellow-green shadow. That’s because they’re doing simple + bit-shifting (possibly including one round of bit replication), instead of + proper scaling.
q/rgba16-4444.bmp5A 16-bit image with an alpha channel. There are 4 bits for each color + channel, and 4 bits for the alpha channel. + It’s not clear if this is valid, but I can’t find anything that + suggests it isn’t. +
g/rgb24.bmp3A perfectly ordinary 24-bit (truecolor) image.
g/rgb24pal.bmp3A 24-bit image, with a palette containing 256 colors. There is little if + any reason for a truecolor image to contain a palette, but it is legal.
q/rgb24largepal.bmp3A 24-bit image, with a palette containing 300 colors. + The fact that the palette has more than 256 colors may cause some viewers + to complain, but the documentation does not mention a size limit.
q/rgb24prof.bmp5My attempt to make a BMP file with an embedded color profile.
q/rgb24lprof.bmp5My attempt to make a BMP file with a linked color profile.
q/rgb24jpeg.bmp5My attempt to make BMP files with embedded JPEG and PNG images. + These are not likely to be supported by much of anything (they’re + intended for printers).
q/rgb24png.bmp5
g/rgb32.bmp3A 32-bit image using the default color format for 32-bit images (no + BITFIELDS segment). There are 8 bits per color channel, and 8 unused + bits. The unused bits are set to 0.
g/rgb32bf.bmp3A 32-bit image with a BITFIELDS segment. As usual, there are 8 bits per + color channel, and 8 unused bits. But the color channels are in an unusual + order, so the viewer must read the BITFIELDS, and not just guess.
q/rgb32fakealpha.bmp3
+ or
+ +
Same as g/rgb32.bmp, except that the unused bits are set to something + other than 0. + If the image becomes transparent toward the bottom, it probably means + the viewer uses heuristics to guess whether the undefined + data represents transparency.
q/rgb32-111110.bmp3A 32 bits/pixel image, with all 32 bits used: 11 each for red and + green, and 10 for blue. As far as I know, this is perfectly valid, but it + is unusual.
q/rgba32.bmp5A BMP with an alpha channel. Transparency is barely documented, + so it’s possible that this file is not correctly formed. + The color channels are in an unusual order, to prevent viewers from + passing this test by making a lucky guess.
q/rgba32abf.bmp3An image of type BI_ALHPABITFIELDS. Supposedly, this was used on + Windows CE. I don’t know whether it is constructed correctly.
b/badbitcount.bmp3N/AHeader indicates an absurdly large number of bits/pixel.
b/badbitssize.bmp3N/AHeader incorrectly indicates that the bitmap is several GB in size.
b/baddens1.bmp3N/ADensity (pixels per meter) suggests the image is much + larger in one dimension than the other.
b/baddens2.bmp3N/A
b/badfilesize.bmp3N/AHeader incorrectly indicates that the file is several GB in size.
b/badheadersize.bmp?N/AHeader size is 66 bytes, which is not a valid size for any known BMP + version.
b/badpalettesize.bmp3N/AHeader incorrectly indicates that the palette contains an absurdly large + number of colors.
b/badplanes.bmp3N/AThe “planes” setting, which is required to be 1, is not 1.
b/badrle.bmp3N/AAn invalid RLE-compressed image that tries to cause buffer overruns.
b/badwidth.bmp3N/AThe image claims to be a negative number of pixels in width.
b/pal8badindex.bmp3N/AMany of the palette indices used in the image are not present in the + palette.
b/reallybig.bmp3N/AAn image with a very large reported width and height.
b/rletopdown.bmp3N/AAn RLE-compressed image that tries to use top-down orientation, + which isn’t allowed.
b/shortfile.bmp3N/AA file that has been truncated in the middle of the bitmap.
+ + + + diff --git a/Tests/images/bmp/html/fakealpha.png b/Tests/images/bmp/html/fakealpha.png new file mode 100644 index 000000000..89292bcbb Binary files /dev/null and b/Tests/images/bmp/html/fakealpha.png differ diff --git a/Tests/images/bmp/html/pal1.png b/Tests/images/bmp/html/pal1.png new file mode 100644 index 000000000..89a433ed7 Binary files /dev/null and b/Tests/images/bmp/html/pal1.png differ diff --git a/Tests/images/bmp/html/pal1bg.png b/Tests/images/bmp/html/pal1bg.png new file mode 100644 index 000000000..20c4bb838 Binary files /dev/null and b/Tests/images/bmp/html/pal1bg.png differ diff --git a/Tests/images/bmp/html/pal1p1.png b/Tests/images/bmp/html/pal1p1.png new file mode 100644 index 000000000..92fc0f945 Binary files /dev/null and b/Tests/images/bmp/html/pal1p1.png differ diff --git a/Tests/images/bmp/html/pal2.png b/Tests/images/bmp/html/pal2.png new file mode 100644 index 000000000..1bbfe175f Binary files /dev/null and b/Tests/images/bmp/html/pal2.png differ diff --git a/Tests/images/bmp/html/pal4.png b/Tests/images/bmp/html/pal4.png new file mode 100644 index 000000000..188bb0499 Binary files /dev/null and b/Tests/images/bmp/html/pal4.png differ diff --git a/Tests/images/bmp/html/pal4rletrns-0.png b/Tests/images/bmp/html/pal4rletrns-0.png new file mode 100644 index 000000000..b689c842a Binary files /dev/null and b/Tests/images/bmp/html/pal4rletrns-0.png differ diff --git a/Tests/images/bmp/html/pal4rletrns-b.png b/Tests/images/bmp/html/pal4rletrns-b.png new file mode 100644 index 000000000..9befa575f Binary files /dev/null and b/Tests/images/bmp/html/pal4rletrns-b.png differ diff --git a/Tests/images/bmp/html/pal4rletrns.png b/Tests/images/bmp/html/pal4rletrns.png new file mode 100644 index 000000000..9b0c04436 Binary files /dev/null and b/Tests/images/bmp/html/pal4rletrns.png differ diff --git a/Tests/images/bmp/html/pal8.png b/Tests/images/bmp/html/pal8.png new file mode 100644 index 000000000..2bfd3e650 Binary files /dev/null and b/Tests/images/bmp/html/pal8.png differ diff --git a/Tests/images/bmp/html/pal8nonsquare-e.png b/Tests/images/bmp/html/pal8nonsquare-e.png new file mode 100644 index 000000000..646665f2d Binary files /dev/null and b/Tests/images/bmp/html/pal8nonsquare-e.png differ diff --git a/Tests/images/bmp/html/pal8nonsquare-v.png b/Tests/images/bmp/html/pal8nonsquare-v.png new file mode 100644 index 000000000..a1cd1ab18 Binary files /dev/null and b/Tests/images/bmp/html/pal8nonsquare-v.png differ diff --git a/Tests/images/bmp/html/pal8rletrns-0.png b/Tests/images/bmp/html/pal8rletrns-0.png new file mode 100644 index 000000000..a1c1fda50 Binary files /dev/null and b/Tests/images/bmp/html/pal8rletrns-0.png differ diff --git a/Tests/images/bmp/html/pal8rletrns-b.png b/Tests/images/bmp/html/pal8rletrns-b.png new file mode 100644 index 000000000..1ede504d4 Binary files /dev/null and b/Tests/images/bmp/html/pal8rletrns-b.png differ diff --git a/Tests/images/bmp/html/pal8rletrns.png b/Tests/images/bmp/html/pal8rletrns.png new file mode 100644 index 000000000..2d8e957f1 Binary files /dev/null and b/Tests/images/bmp/html/pal8rletrns.png differ diff --git a/Tests/images/bmp/html/pal8w124.png b/Tests/images/bmp/html/pal8w124.png new file mode 100644 index 000000000..f80236df6 Binary files /dev/null and b/Tests/images/bmp/html/pal8w124.png differ diff --git a/Tests/images/bmp/html/pal8w125.png b/Tests/images/bmp/html/pal8w125.png new file mode 100644 index 000000000..2a45116b9 Binary files /dev/null and b/Tests/images/bmp/html/pal8w125.png differ diff --git a/Tests/images/bmp/html/pal8w126.png b/Tests/images/bmp/html/pal8w126.png new file mode 100644 index 000000000..a41eab93d Binary files /dev/null and b/Tests/images/bmp/html/pal8w126.png differ diff --git a/Tests/images/bmp/html/rgb16-231.png b/Tests/images/bmp/html/rgb16-231.png new file mode 100644 index 000000000..76efe526e Binary files /dev/null and b/Tests/images/bmp/html/rgb16-231.png differ diff --git a/Tests/images/bmp/html/rgb16-565.png b/Tests/images/bmp/html/rgb16-565.png new file mode 100644 index 000000000..04a3121d2 Binary files /dev/null and b/Tests/images/bmp/html/rgb16-565.png differ diff --git a/Tests/images/bmp/html/rgb16.png b/Tests/images/bmp/html/rgb16.png new file mode 100644 index 000000000..d9545840a Binary files /dev/null and b/Tests/images/bmp/html/rgb16.png differ diff --git a/Tests/images/bmp/html/rgb24.jpg b/Tests/images/bmp/html/rgb24.jpg new file mode 100644 index 000000000..c43698c9b Binary files /dev/null and b/Tests/images/bmp/html/rgb24.jpg differ diff --git a/Tests/images/bmp/html/rgb24.png b/Tests/images/bmp/html/rgb24.png new file mode 100644 index 000000000..86a9c945b Binary files /dev/null and b/Tests/images/bmp/html/rgb24.png differ diff --git a/Tests/images/bmp/html/rgba16-4444.png b/Tests/images/bmp/html/rgba16-4444.png new file mode 100644 index 000000000..bfeda6fae Binary files /dev/null and b/Tests/images/bmp/html/rgba16-4444.png differ diff --git a/Tests/images/bmp/html/rgba32.png b/Tests/images/bmp/html/rgba32.png new file mode 100644 index 000000000..25e542a65 Binary files /dev/null and b/Tests/images/bmp/html/rgba32.png differ diff --git a/Tests/images/bmp/q/pal1p1.bmp b/Tests/images/bmp/q/pal1p1.bmp new file mode 100644 index 000000000..b68321c4c Binary files /dev/null and b/Tests/images/bmp/q/pal1p1.bmp differ diff --git a/Tests/images/bmp/q/pal2.bmp b/Tests/images/bmp/q/pal2.bmp new file mode 100644 index 000000000..983e9fa92 Binary files /dev/null and b/Tests/images/bmp/q/pal2.bmp differ diff --git a/Tests/images/bmp/q/pal4rletrns.bmp b/Tests/images/bmp/q/pal4rletrns.bmp new file mode 100644 index 000000000..58994e92b Binary files /dev/null and b/Tests/images/bmp/q/pal4rletrns.bmp differ diff --git a/Tests/images/bmp/q/pal8offs.bmp b/Tests/images/bmp/q/pal8offs.bmp new file mode 100644 index 000000000..8673e9740 Binary files /dev/null and b/Tests/images/bmp/q/pal8offs.bmp differ diff --git a/Tests/images/bmp/q/pal8os2sp.bmp b/Tests/images/bmp/q/pal8os2sp.bmp new file mode 100644 index 000000000..e532c8986 Binary files /dev/null and b/Tests/images/bmp/q/pal8os2sp.bmp differ diff --git a/Tests/images/bmp/q/pal8os2v2-16.bmp b/Tests/images/bmp/q/pal8os2v2-16.bmp new file mode 100644 index 000000000..95a1d2345 Binary files /dev/null and b/Tests/images/bmp/q/pal8os2v2-16.bmp differ diff --git a/Tests/images/bmp/q/pal8os2v2.bmp b/Tests/images/bmp/q/pal8os2v2.bmp new file mode 100644 index 000000000..1324a40d0 Binary files /dev/null and b/Tests/images/bmp/q/pal8os2v2.bmp differ diff --git a/Tests/images/bmp/q/pal8oversizepal.bmp b/Tests/images/bmp/q/pal8oversizepal.bmp new file mode 100644 index 000000000..93b8187ca Binary files /dev/null and b/Tests/images/bmp/q/pal8oversizepal.bmp differ diff --git a/Tests/images/bmp/q/pal8rletrns.bmp b/Tests/images/bmp/q/pal8rletrns.bmp new file mode 100644 index 000000000..a2af88d87 Binary files /dev/null and b/Tests/images/bmp/q/pal8rletrns.bmp differ diff --git a/Tests/images/bmp/q/rgb16-231.bmp b/Tests/images/bmp/q/rgb16-231.bmp new file mode 100644 index 000000000..6300f69f0 Binary files /dev/null and b/Tests/images/bmp/q/rgb16-231.bmp differ diff --git a/Tests/images/bmp/q/rgb24jpeg.bmp b/Tests/images/bmp/q/rgb24jpeg.bmp new file mode 100644 index 000000000..87d73d75b Binary files /dev/null and b/Tests/images/bmp/q/rgb24jpeg.bmp differ diff --git a/Tests/images/bmp/q/rgb24largepal.bmp b/Tests/images/bmp/q/rgb24largepal.bmp new file mode 100644 index 000000000..d5e418c2d Binary files /dev/null and b/Tests/images/bmp/q/rgb24largepal.bmp differ diff --git a/Tests/images/bmp/q/rgb24lprof.bmp b/Tests/images/bmp/q/rgb24lprof.bmp new file mode 100644 index 000000000..b868b88f2 Binary files /dev/null and b/Tests/images/bmp/q/rgb24lprof.bmp differ diff --git a/Tests/images/bmp/q/rgb24png.bmp b/Tests/images/bmp/q/rgb24png.bmp new file mode 100644 index 000000000..e87ec7add Binary files /dev/null and b/Tests/images/bmp/q/rgb24png.bmp differ diff --git a/Tests/images/bmp/q/rgb24prof.bmp b/Tests/images/bmp/q/rgb24prof.bmp new file mode 100644 index 000000000..627e676ea Binary files /dev/null and b/Tests/images/bmp/q/rgb24prof.bmp differ diff --git a/Tests/images/bmp/q/rgb32-111110.bmp b/Tests/images/bmp/q/rgb32-111110.bmp new file mode 100644 index 000000000..ec07d89b5 Binary files /dev/null and b/Tests/images/bmp/q/rgb32-111110.bmp differ diff --git a/Tests/images/bmp/q/rgb32fakealpha.bmp b/Tests/images/bmp/q/rgb32fakealpha.bmp new file mode 100644 index 000000000..cb544da5b Binary files /dev/null and b/Tests/images/bmp/q/rgb32fakealpha.bmp differ diff --git a/Tests/images/bmp/q/rgba16-4444.bmp b/Tests/images/bmp/q/rgba16-4444.bmp new file mode 100644 index 000000000..051ff2358 Binary files /dev/null and b/Tests/images/bmp/q/rgba16-4444.bmp differ diff --git a/Tests/images/bmp/q/rgba32.bmp b/Tests/images/bmp/q/rgba32.bmp new file mode 100644 index 000000000..829c7c7e3 Binary files /dev/null and b/Tests/images/bmp/q/rgba32.bmp differ diff --git a/Tests/images/bmp/q/rgba32abf.bmp b/Tests/images/bmp/q/rgba32abf.bmp new file mode 100644 index 000000000..d9bb0189c Binary files /dev/null and b/Tests/images/bmp/q/rgba32abf.bmp differ diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py new file mode 100644 index 000000000..0e03df79a --- /dev/null +++ b/Tests/test_bmp_reference.py @@ -0,0 +1,86 @@ +from tester import * + +from PIL import Image +import os + +base = 'Tests/images/bmp/' + + +def get_files(d, ext='.bmp'): + return [os.path.join(base,d,f) for f + in os.listdir(os.path.join(base, d)) if ext in f] + +def test_bad(): + """ These shouldn't crash/dos, but they shouldn't return anything either """ + for f in get_files('b'): + try: + im = Image.open(f) + im.load() + except Exception as msg: + pass + # print ("Bad Image %s: %s" %(f,msg)) + +def test_questionable(): + """ These shouldn't crash/dos, but its not well defined that these are in spec """ + for f in get_files('q'): + try: + im = Image.open(f) + im.load() + except Exception as msg: + pass + # print ("Bad Image %s: %s" %(f,msg)) + + +def test_good(): + """ These should all work. There's a set of target files in the + html directory that we can compare against. """ + + # Target files, if they're not just replacing the extension + file_map = { 'pal1wb.bmp': 'pal1.png', + 'pal4rle.bmp': 'pal4.png', + 'pal8-0.bmp': 'pal8.png', + 'pal8rle.bmp': 'pal8.png', + 'pal8topdown.bmp': 'pal8.png', + 'pal8nonsquare.bmp': 'pal8nonsquare-v.png', + 'pal8os2.bmp': 'pal8.png', + 'pal8os2sp.bmp': 'pal8.png', + 'pal8os2v2.bmp': 'pal8.png', + 'pal8os2v2-16.bmp': 'pal8.png', + 'pal8v4.bmp': 'pal8.png', + 'pal8v5.bmp': 'pal8.png', + 'rgb16-565pal.bmp': 'rgb16-565.png', + 'rgb24pal.bmp': 'rgb24.png', + 'rgb32.bmp': 'rgb24.png', + 'rgb32bf.bmp': 'rgb24.png' + } + + def get_compare(f): + (head, name) = os.path.split(f) + if name in file_map: + return os.path.join(base, 'html', file_map[name]) + (name,ext) = os.path.splitext(name) + return os.path.join(base, 'html', "%s.png"%name) + + for f in get_files('g'): + try: + im = Image.open(f) + im.load() + compare = Image.open(get_compare(f)) + compare.load() + if im.mode == 'P': + # assert image similar doesn't really work + # with paletized image, since the palette might + # be differently ordered for an equivalent image. + im = im.convert('RGBA') + compare = im.convert('RGBA') + assert_image_similar(im, compare,5) + + + except Exception as msg: + # there are three here that are unsupported: + unsupported = ('Tests/images/bmp/g/rgb32bf.bmp', + 'Tests/images/bmp/g/pal8rle.bmp', + 'Tests/images/bmp/g/pal4rle.bmp') + if f not in unsupported: + assert_true(False, "Unsupported Image %s: %s" %(f,msg)) + diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index e0584641c..dd5f31fd2 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -1,27 +1,38 @@ from tester import * from PIL import Image +import io + +def roundtrip(im): + outfile = tempfile("temp.bmp") + + im.save(outfile, 'BMP') + + reloaded = Image.open(outfile) + reloaded.load() + assert_equal(im.mode, reloaded.mode) + assert_equal(im.size, reloaded.size) + assert_equal(reloaded.format, "BMP") + def test_sanity(): + roundtrip(lena()) + + roundtrip(lena("1")) + roundtrip(lena("L")) + roundtrip(lena("P")) + roundtrip(lena("RGB")) - file = tempfile("temp.bmp") - lena().save(file) +def test_save_to_bytes(): + output = io.BytesIO() + im = lena() + im.save(output, "BMP") - im = Image.open(file) - im.load() - assert_equal(im.mode, "RGB") - assert_equal(im.size, (128, 128)) - assert_equal(im.format, "BMP") - - lena("1").save(file) - im = Image.open(file) - - lena("L").save(file) - im = Image.open(file) - - lena("P").save(file) - im = Image.open(file) - - lena("RGB").save(file) - im = Image.open(file) + output.seek(0) + reloaded = Image.open(output) + + assert_equal(im.mode, reloaded.mode) + assert_equal(im.size, reloaded.size) + assert_equal(reloaded.format, "BMP") + diff --git a/Tests/tester.py b/Tests/tester.py index 7252b5b47..2c6fa071c 100644 --- a/Tests/tester.py +++ b/Tests/tester.py @@ -27,6 +27,7 @@ def success(): success.count += 1 if _logfile: print(sys.argv[0], success.count, failure.count, file=_logfile) + return True def failure(msg=None, frame=None): import sys, linecache @@ -44,6 +45,7 @@ def failure(msg=None, frame=None): print("- " + msg) if _logfile: print(sys.argv[0], success.count, failure.count, file=_logfile) + return False success.count = failure.count = 0 @@ -198,9 +200,9 @@ def assert_image_similar(a, b, epsilon, msg=None): diff += abs(abyte-bbyte) ave_diff = float(diff)/(a.size[0]*a.size[1]) if epsilon < ave_diff: - failure(msg or "average pixel value difference %.4f > epsilon %.4f" %(ave_diff, epsilon)) + return failure(msg or "average pixel value difference %.4f > epsilon %.4f" %(ave_diff, epsilon)) else: - success() + return success() def tempfile(template, *extra): import os, os.path, sys, tempfile