pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Certain PNGs load incorrectly.

Open Starbuck5 opened this issue 1 year ago • 2 comments
trafficstars

Reported by @geometrian on discord

1_img_okay.png: 1_img_okay

2_img_broken.png 2_img_broken

Inside pygame-ce however, image 1 load correctly and image 2 does not.

image

Basic display script:

import pygame

pygame.init()

im1 = pygame.image.load("1_img_okay.png")
print("load 1 finished")
im2 = pygame.image.load("2_img_broken.png")

print(im1.get_at((20,20)), im2.get_at((20,20)))
print(im1, im1.get_shifts())
print(im2, im2.get_shifts())

running = True

screen = pygame.display.set_mode((500,500))
print(screen, screen.get_shifts())

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("grey")
    screen.blit(im1, (0,0))
    screen.blit(im2, (im1.get_width(),0))

    pygame.display.flip()

pygame.quit()

Starbuck5 avatar Aug 05 '24 06:08 Starbuck5

I've been looking into this for several days now, and this is a problem with how SDL_image is interfacing with libpng to load images.

Using SDL_image prebuilts (which use stb-image for PNGs), this problem goes away. Using our prebuilts (with libpng) the error is present.

pngcheck output on the broken image:

File: 2_img_broken.png (1379 bytes)
  chunk IHDR at offset 0x0000c, length 13
    64 x 64 image, 8-bit grayscale, non-interlaced
  chunk tRNS at offset 0x00025, length 2
    gray = 0x00ff
  chunk IDAT at offset 0x00033, length 1282
    zlib: deflated, 8K window, maximum compression
    row filters (0 none, 1 sub, 2 up, 3 avg, 4 paeth):
      1 1 2 1 2 1 2 1 4 2 1 2 1 2 4 2 4 1 2 1 2 4 2 4 4
      2 4 2 4 2 4 2 4 1 2 4 2 1 2 4 1 2 4 2 1 2 1 2 4 1
      2 1 2 1 2 4 1 2 1 2 1 2 1 2 (64 out of 64)
  chunk eXIf at offset 0x00541, length 14: EXIF metadata, big-endian (MM) format
  chunk IEND at offset 0x0055b, length 0
No errors detected in 2_img_broken.png (5 chunks, 66.3% compression).

This is a grayscale image with alpha transparency provided by a colorkey, basically. This is the erroring case.

Starbuck5 avatar Aug 05 '24 06:08 Starbuck5

Alright, it's fixed in SDL_image now!

We'll need to wait for them to put out a new patch release with the fix, or we could use the git version of SDL_image since we control builds on all platforms. (It's just we're not used to using git versions).

It would also be good to add this image to our test suite to prevent regressions here.

Starbuck5 avatar Aug 06 '24 06:08 Starbuck5