Fixed bitmap order interpretation; SDL defaults to MSB ordering so a bitstream corresponds to a pixel stream.
authorSam Lantinga <slouken@libsdl.org>
Mon, 07 Mar 2011 00:30:05 -0800
changeset 5438b705640cb34a
parent 5437 ce10653e370c
child 5439 3a778c6c0269
Fixed bitmap order interpretation; SDL defaults to MSB ordering so a bitstream corresponds to a pixel stream.

The bitmap ordering is defined such that the numbering refers to the pixel index from left to right, and the number position refers to the bit position in the byte.

SDL_BITMAPORDER_4321 is the fourth pixel at the high bit and the first pixel at the low bit (LSBFirst)

SDL_BITMAPORDER_1234 is the first pixel at the high bit and the fourth pixel at the low bit (MSBFirst)
include/SDL_pixels.h
src/video/SDL_pixels.c
src/video/SDL_surface.c
     1.1 --- a/include/SDL_pixels.h	Mon Mar 07 00:08:23 2011 -0800
     1.2 +++ b/include/SDL_pixels.h	Mon Mar 07 00:30:05 2011 -0800
     1.3 @@ -149,16 +149,16 @@
     1.4  {
     1.5      SDL_PIXELFORMAT_UNKNOWN,
     1.6      SDL_PIXELFORMAT_INDEX1LSB =
     1.7 +        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0,
     1.8 +                               1, 0),
     1.9 +    SDL_PIXELFORMAT_INDEX1MSB =
    1.10          SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_1234, 0,
    1.11                                 1, 0),
    1.12 -    SDL_PIXELFORMAT_INDEX1MSB =
    1.13 -        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0,
    1.14 -                               1, 0),
    1.15      SDL_PIXELFORMAT_INDEX4LSB =
    1.16 -        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_1234, 0,
    1.17 +        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_4321, 0,
    1.18                                 4, 0),
    1.19      SDL_PIXELFORMAT_INDEX4MSB =
    1.20 -        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_4321, 0,
    1.21 +        SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_1234, 0,
    1.22                                 4, 0),
    1.23      SDL_PIXELFORMAT_INDEX8 =
    1.24          SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1),
     2.1 --- a/src/video/SDL_pixels.c	Mon Mar 07 00:08:23 2011 -0800
     2.2 +++ b/src/video/SDL_pixels.c	Mon Mar 07 00:30:05 2011 -0800
     2.3 @@ -242,9 +242,11 @@
     2.4  {
     2.5      switch (bpp) {
     2.6      case 1:
     2.7 +        /* SDL defaults to MSB ordering */
     2.8 +        return SDL_PIXELFORMAT_INDEX1MSB;
     2.9      case 4:
    2.10 -        /* Can't tell if this is LSB or MSB bitmap ordering... */
    2.11 -        break;
    2.12 +        /* SDL defaults to MSB ordering */
    2.13 +        return SDL_PIXELFORMAT_INDEX4MSB;
    2.14      case 8:
    2.15          if (Rmask == 0) {
    2.16              return SDL_PIXELFORMAT_INDEX8;
     3.1 --- a/src/video/SDL_surface.c	Mon Mar 07 00:08:23 2011 -0800
     3.2 +++ b/src/video/SDL_surface.c	Mon Mar 07 00:30:05 2011 -0800
     3.3 @@ -75,44 +75,7 @@
     3.4              SDL_FreeSurface(surface);
     3.5              return NULL;
     3.6          }
     3.7 -        if (Rmask || Bmask || Gmask) {
     3.8 -            const SDL_PixelFormat *format = surface->format;
     3.9 -
    3.10 -            /* create palette according to masks */
    3.11 -            int i;
    3.12 -            int Rm = 0, Gm = 0, Bm = 0;
    3.13 -            int Rw = 0, Gw = 0, Bw = 0;
    3.14 -
    3.15 -            if (Rmask) {
    3.16 -                Rw = 8 - format->Rloss;
    3.17 -                for (i = format->Rloss; i > 0; i -= Rw)
    3.18 -                    Rm |= 1 << i;
    3.19 -            }
    3.20 -            if (Gmask) {
    3.21 -                Gw = 8 - format->Gloss;
    3.22 -                for (i = format->Gloss; i > 0; i -= Gw)
    3.23 -                    Gm |= 1 << i;
    3.24 -            }
    3.25 -            if (Bmask) {
    3.26 -                Bw = 8 - format->Bloss;
    3.27 -                for (i = format->Bloss; i > 0; i -= Bw)
    3.28 -                    Bm |= 1 << i;
    3.29 -            }
    3.30 -            for (i = 0; i < palette->ncolors; ++i) {
    3.31 -                int r, g, b;
    3.32 -                r = (i & Rmask) >> format->Rshift;
    3.33 -                r = (r << format->Rloss) | ((r * Rm) >> Rw);
    3.34 -                palette->colors[i].r = r;
    3.35 -
    3.36 -                g = (i & Gmask) >> format->Gshift;
    3.37 -                g = (g << format->Gloss) | ((g * Gm) >> Gw);
    3.38 -                palette->colors[i].g = g;
    3.39 -
    3.40 -                b = (i & Bmask) >> format->Bshift;
    3.41 -                b = (b << format->Bloss) | ((b * Bm) >> Bw);
    3.42 -                palette->colors[i].b = b;
    3.43 -            }
    3.44 -        } else if (palette->ncolors == 2) {
    3.45 +        if (palette->ncolors == 2) {
    3.46              /* Create a black and white bitmap palette */
    3.47              palette->colors[0].r = 0xFF;
    3.48              palette->colors[0].g = 0xFF;