Hello !
This code i use to create Blitting Masks :
#include <stdio.h>
#include “SDL.h”
Uint32 getpixel(SDL_Surface surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/ Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void putpixel(SDL_Surface surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/ Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
int main (int argc, char *argv [])
{
Uint32 rmask = 0, gmask = 0, bmask = 0, amask = 0;
Uint32 index_x = 0, index_y = 0;
Uint32 color = 0;
SDL_Surface *orig = NULL;
SDL_Surface *target = NULL;
if ( argc != 3 ) return 1;
orig = SDL_LoadBMP (argv [1]);
if ( orig == NULL ) return 1;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
target = SDL_CreateRGBSurface (SDL_SWSURFACE, orig -> w, orig -> h, 32,
rmask, gmask, bmask, amask);
if ( target == NULL ) return 1;
for (index_y = 0; index_y < orig -> h; index_y ++)
{
for (index_x = 0; index_x < orig -> w; index_x ++)
{
color = getpixel (orig, index_x, index_y);
if ( color == SDL_MapRGB (orig -> format, 255, 0, 255) )
putpixel (target, index_x, index_y,
SDL_MapRGB (target -> format, 255, 0, 255) );
else
putpixel (target, index_x, index_y,
SDL_MapRGB (target -> format, 255, 255, 255) );
}
}
SDL_SaveBMP (target, argv [2]);
SDL_FreeSurface (orig);
SDL_FreeSurface (target);
return 0;
}
Compiled with c++ Create_BM.cxx -o Create_BM sdl-config --cflags --libs
on Mac OSX PPC.
Running it with ./Create_BM STAND.bmp STAND_MW.bmp for example :
When you look at the generated STAND_MW.bmp with GIMP you see the
R,G,B Nummers are the correct ones - 1. So i don’t get 255 only 254.
Does anybody see the error in this routine ?
getpixel, putpixel are the ones from the SDL website.
CU