def testSaveJPG(self):
""" JPG equivalent to issue #211 - color channel swapping
Make sure the SDL surface color masks represent the rgb memory format
required by the JPG library. The masks are machine endian dependent
"""
from pygame import Color, Rect
# The source image is a 2 by 2 square of four colors. Since JPEG is
# lossy, there can be color bleed. Make each color square 16 by 16,
# to avoid the significantly color value distorts found at color
# boundaries due to the compression value set by Pygame.
square_len = 16
sz = 2 * square_len, 2 * square_len
# +---------------------------------+
# | red | green |
# |----------------+----------------|
# | blue | (255, 128, 64) |
# +---------------------------------+
#
# as (rect, color) pairs.
def as_rect(square_x, square_y):
return Rect(square_x * square_len, square_y * square_len,
square_len, square_len)
squares = [(as_rect(0, 0), Color("red")),
(as_rect(1, 0), Color("green")),
(as_rect(0, 1), Color("blue")),
(as_rect(1, 1), Color(255, 128, 64))]
# A surface format which is not directly usable with libjpeg.
surf = pygame.Surface(sz, 0, 32)
for rect, color in squares:
surf.fill(color, rect)
# Assume pygame.image.Load works correctly as it is handled by the
# third party SDL_image library.
f_path = tempfile.mktemp(suffix='.jpg')
pygame.image.save(surf, f_path)
jpg_surf = pygame.image.load(f_path)
# Allow for small differences in the restored colors.
def approx(c):
mask = 0xFC
return pygame.Color(c.r & mask, c.g & mask, c.b & mask)
offset = square_len // 2
for rect, color in squares:
posn = rect.move((offset, offset)).topleft
self.assertEqual(approx(jpg_surf.get_at(posn)), approx(color))
评论列表
文章目录