Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bitmap order interpretation; SDL defaults to MSB ordering so a …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
slouken committed Mar 7, 2011
1 parent 969cbfe commit 8aad056
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 44 deletions.
8 changes: 4 additions & 4 deletions include/SDL_pixels.h
Expand Up @@ -149,16 +149,16 @@ enum
{
SDL_PIXELFORMAT_UNKNOWN,
SDL_PIXELFORMAT_INDEX1LSB =
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_1234, 0,
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0,
1, 0),
SDL_PIXELFORMAT_INDEX1MSB =
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0,
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_1234, 0,
1, 0),
SDL_PIXELFORMAT_INDEX4LSB =
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_1234, 0,
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_4321, 0,
4, 0),
SDL_PIXELFORMAT_INDEX4MSB =
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_4321, 0,
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_1234, 0,
4, 0),
SDL_PIXELFORMAT_INDEX8 =
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1),
Expand Down
6 changes: 4 additions & 2 deletions src/video/SDL_pixels.c
Expand Up @@ -242,9 +242,11 @@ SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
{
switch (bpp) {
case 1:
/* SDL defaults to MSB ordering */
return SDL_PIXELFORMAT_INDEX1MSB;
case 4:
/* Can't tell if this is LSB or MSB bitmap ordering... */
break;
/* SDL defaults to MSB ordering */
return SDL_PIXELFORMAT_INDEX4MSB;
case 8:
if (Rmask == 0) {
return SDL_PIXELFORMAT_INDEX8;
Expand Down
39 changes: 1 addition & 38 deletions src/video/SDL_surface.c
Expand Up @@ -75,44 +75,7 @@ SDL_CreateRGBSurface(Uint32 flags,
SDL_FreeSurface(surface);
return NULL;
}
if (Rmask || Bmask || Gmask) {
const SDL_PixelFormat *format = surface->format;

/* create palette according to masks */
int i;
int Rm = 0, Gm = 0, Bm = 0;
int Rw = 0, Gw = 0, Bw = 0;

if (Rmask) {
Rw = 8 - format->Rloss;
for (i = format->Rloss; i > 0; i -= Rw)
Rm |= 1 << i;
}
if (Gmask) {
Gw = 8 - format->Gloss;
for (i = format->Gloss; i > 0; i -= Gw)
Gm |= 1 << i;
}
if (Bmask) {
Bw = 8 - format->Bloss;
for (i = format->Bloss; i > 0; i -= Bw)
Bm |= 1 << i;
}
for (i = 0; i < palette->ncolors; ++i) {
int r, g, b;
r = (i & Rmask) >> format->Rshift;
r = (r << format->Rloss) | ((r * Rm) >> Rw);
palette->colors[i].r = r;

g = (i & Gmask) >> format->Gshift;
g = (g << format->Gloss) | ((g * Gm) >> Gw);
palette->colors[i].g = g;

b = (i & Bmask) >> format->Bshift;
b = (b << format->Bloss) | ((b * Bm) >> Bw);
palette->colors[i].b = b;
}
} else if (palette->ncolors == 2) {
if (palette->ncolors == 2) {
/* Create a black and white bitmap palette */
palette->colors[0].r = 0xFF;
palette->colors[0].g = 0xFF;
Expand Down

0 comments on commit 8aad056

Please sign in to comment.