0

I'm trying to program a triangle rasterizer and have problems with the right pixel values. When putting a the pixel value from one surface to another I use buffer(/direct) pixel access. My Problem is that when I "blit" the whole Image. The color values get swapped.

Here is the result picture:

tux drawn with custom blit routine

Here is a simplified version of the my code:

src = PySurface_AsSurface(sobj);
dest = PySurface_AsSurface(dobj);

uint32_t* src_buff = (uint32_t*) src->pixels;
uint32_t* dest_buff = (uint32_t*) dest->pixels;

SDL_LockSurface(dest);
SDL_LockSurface(src);

int x, y;

for(y = 0; y < src->h; y++) {
    for(x = 0; x < src->w; x++) {

        uint32_t c = src_buff[y * src->w + x];
        dest_buff[(y + y1) * dest->w + (x + x1)] = c;
    }
}

SDL_UnlockSurface(src);
SDL_UnlockSurface(dest);

Apparently the pixel order in the surfaces are different, but shouldn't they be all either big endian or little endian, because they are on the same machine?

Do I have to change the whole code, or is this normally a valid and quick and dirty way of doing things?

Thanks for help in advance.

BenjiClub
  • 21
  • 2
  • I found the answer. The bitmap surface I loaded had a different order than the graphics card memory (or the area assigned to the graphics card since it's software mode). SDL_BYTE_ORDER lets you check the byte order. You had to check via #if SDL_BYTE_ORDER == SDL_BIG_ENDIAN which byte order to put the color information in. Here is an answer where someone discribes it correctly: http://stackoverflow.com/questions/6852055/how-can-i-modify-pixels-using-sdl Also in the PyGame source code you can look up the "PyGame.Surface.set_at"-Method how it work correctly :-P – BenjiClub Nov 25 '16 at 21:20
  • Ok it's not only the Big/Little Endian thing. It's the surface format. Memory on Graphics Card have bgra format, like images have rgba format. In the PyGame source code you can find the right implementation: https://bitbucket.org/pygame/pygame/src/7b6709de3be6e629c0f5e9c7ce1598ea9d4a0a0f/src/surface.c?at=default&fileviewer=file-view-default#surface.c-704 You can directly read the pixels from the image surface and save them in the window surface by converting the image surface to window surface pixelformat. https://wiki.libsdl.org/SDL_ConvertSurface – BenjiClub Dec 09 '16 at 23:56

0 Answers0