我正在尝试使用sdl操作pixel并设法读取它们。下面是我的示例代码。当我打印这个printf("\npixelvalue is is : %d",MyPixel);时,我得到这样的值

11275780
11275776
etc

我知道这些不是十六进制形式,但如何操作说,我想过滤掉只是蓝色的颜色?二是在操作后如何生成新的图像?
#include "SDL.h"

int main( int argc, char* argv[] )
{
  SDL_Surface *screen, *image;
  SDL_Event event;
  Uint8 *keys;
  int done = 0;

  if (SDL_Init(SDL_INIT_VIDEO) == -1)
  {
    printf("Can't init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);
  SDL_WM_SetCaption("sample1", "app.ico");

  /* obtain the SDL surfance of the video card */
  screen = SDL_SetVideoMode(640, 480, 24, SDL_HWSURFACE);
  if (screen == NULL)
  {
    printf("Can't set video mode: %s\n", SDL_GetError());
    exit(1);
  }
  printf("Loading here");

  /* load BMP file */
  image = SDL_LoadBMP("testa.bmp");
  Uint32* pixels = (Uint32*)image->pixels;
  int width = image->w;
  int height = image->h;
  printf("Widts is : %d",image->w);

  for(int iH = 1; iH<=height; iH++)
    for(int iW = 1; iW<=width; iW++)
    {
      printf("\nIh is : %d",iH);
      printf("\nIw is : %d",iW);
      Uint32* MyPixel = pixels + ( (iH-1) + image->w ) + iW;
      printf("\npixelvalue is  is : %d",MyPixel);
    }

  if (image == NULL) {
    printf("Can't load image of tux: %s\n", SDL_GetError());
    exit(1);
  }

  /* Blit image to the video surface */
  SDL_BlitSurface(image, NULL, screen, NULL);
  SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);

  /* free the image if it is no longer needed */
  SDL_FreeSurface(image);

  /* process the keyboard event */
  while (!done)
  {
    // Poll input queue, run keyboard loop
    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )
      {
        done = 1;
        break;
      }
    }
    keys = SDL_GetKeyState(NULL);
    if (keys[SDLK_q])
    {
      done = 1;
    }
    // Release CPU for others
    SDL_Delay(100);
  }
  // Release memeory and Quit SDL
  SDL_FreeSurface(screen);
  SDL_Quit();
  return 0;
}

最佳答案

使用SDL_MapRGBSDL_MapRGBA对颜色进行排序。SDL将根据surface格式为您过滤掉它。
就像这样:

Uint32 rawpixel = getpixel(surface, x, y);
Uint8 red, green, blue;

SDL_GetRGB(rawpixel, surface->format, &red, &green, &blue);

09-09 18:17