[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] PATCH: bug in pygame.image.frombuffer



Hi,

Found a problem with pygame.image.frombuffer and 8 bit images. The
call to SDL_CreateRGBSurfaceFrom uses incorrect pitch, which causes
the image to be be corrupted. pygame.image.fromstring does not have
this problem so you can use that instead as a workaround. The bug is
present in latest subversion code.

Demo script and patch are attached.

-- 
Alistair Buxton
a.j.buxton@xxxxxxxxx
from struct import unpack
from imageop import mono2grey, scale

import pygame
from pygame.locals import *

pygame.init()
        
sx,sy = 320,320
    
screen = pygame.display.set_mode((sx,sy))

# 8x8 checkerboard
pixels = "\x00\xff\x00\xff\x00\xff\x00\xff\xff\x00\xff\x00\xff\x00\xff\x00"*4
# scale to 320x320
newpixels = scale(pixels, 1, 8, 8, 320, 320)

# this produces an image which is warped horizontally:
surface = pygame.image.frombuffer(newpixels, (320, 320), "P")
# this works ok:
#surface = pygame.image.fromstring(newpixels, (320, 320), "P")
surface.set_palette([(0,0,0), (255,255,255)]*128)

running = True

while running:
        
    screen.fill((0,0,0))
    screen.blit(surface, (0,0))

    for event in pygame.event.get():
        if event.type == QUIT:
            running = 0
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = 0

        pygame.display.flip()        
        pygame.time.delay(10)


Index: src/image.c
===================================================================
--- src/image.c	(revision 1113)
+++ src/image.c	(working copy)
@@ -938,7 +938,7 @@
                 (PyExc_ValueError,
                  "Buffer length does not equal format and resolution size");
 
-        surf = SDL_CreateRGBSurfaceFrom (data, w, h, 8, 3, 0, 0, 0, 0);
+        surf = SDL_CreateRGBSurfaceFrom (data, w, h, 8, w, 0, 0, 0, 0);
     }
     else if (!strcmp (format, "RGB"))
     {