1

我正在尝试使用 sdl 操作像素并设法现在读取它们。下面是我的示例代码。当我打印 I thisprintf("\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;    
}
4

3 回答 3

2

使用SDL_MapRGBSDL_MapRGBA对颜色进行分类。SDL 将根据表面格式为您过滤掉它。

像这样:

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

SDL_GetRGB(rawpixel, surface->format, &red, &green, &blue);
于 2013-09-04T19:43:40.520 回答
1

您正在打印指针的值MyPixel。要获取值,您必须取消引用指向像素值的指针,如下所示:*MyPixel

然后 printf 看起来像这样:

printf("\npixelvalue is : %d and the address of that pixel is: %p\n",*MyPixel , MyPixel);

其他错误:

  1. 您的 for 循环不正确。您应该从 0 循环到小于宽度或高度,否则您将读取未初始化的内存。

  2. 你没有锁定表面。尽管您只是在读取像素并且没有任何问题,但它仍然是不正确的。

  3. image如果指针在您已经使用指针之后出现,请测试正确性。在初始化之后立即进行测试。

于 2013-09-05T13:15:06.030 回答
0

如果我没记错的话,我使用sdl_gfx进行像素操作。

它还包含画圆、椭圆等功能。

于 2013-09-04T18:44:25.027 回答