SDL2: How to work with monochrome or 16 color surfaces?

Viewed 354

I'm working on a project where I mainly use paletized images with few colors (4bpp). It's not a constraint but I don't need more.

SDL_PixelFormatEnum defines the following values : SDL_PIXELFORMAT_INDEX1LSB, SDL_PIXELFORMAT_INDEX1MSB, SDL_PIXELFORMAT_INDEX4LSB and SDL_PIXELFORMAT_INDEX4MSB.

1/ When I create a surface from a bitmap with a depth of 4bpp or less, I expect the pixel format to be one of those:

SDL_Surface *surface = SDL_LoadBMP("test.bmp"); // bitmap with a 16-color palette
if(surface != NULL)
{
    printf("%s\n", SDL_GetPixelFormatName(surface->format->format));
}
else
{
    printf("%s\n", SDL_GetError());
}

But it outputs invariably:

SDL_PIXELFORMAT_INDEX8

What is the reason?

2/ The other case is the creation of a surface from scratch:

Uint8 pixels[32] = {
    0xf0, 0x0f, 0xf0, 0x0f, // trying to put 2 pixels per byte
    0x0f, 0xf0, 0x0f, 0xf0,
    0xf0, 0x0f, 0xf0, 0x0f,
    0x0f, 0xf0, 0x0f, 0xf0,
    0xf0, 0x0f, 0xf0, 0x0f,
    0x0f, 0xf0, 0x0f, 0xf0,
    0xf0, 0x0f, 0xf0, 0x0f,
    0x0f, 0xf0, 0x0f, 0xf0
};

SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, 8, 8, 4, 4, SDL_PIXELFORMAT_INDEX4LSB); // 8px width, 0.5 byte for 1 pixel, is the pitch 4?
if(surface != NULL)
{
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
    if(texture != NULL)
    {
        // ...
    }
    else
    {
        printf("%s\n", SDL_GetError());
    }
}

Output:

Blit combination not supported

Surface creation seems OK, but I can't use it. Any attempt to create a texture from the surface, or convert the format (with SDL_ConvertSurface or SDL_ConvertSurfaceFormat) gives the same error.

What is the point of these pixel formats and how can I use them?

0 Answers
Related