1

私はsdlを使ってピクセルを操作しようとしていて、今それらを読み込もうとしています。以下は私のサンプルコードです。私がこれを印刷すると、このprintf("\npixelvalue is is : %d",MyPixel);ような値が得られます

11275780
11275776 
etc 

これらは16進形式ではないことは知っていますが、青い色だけを除外したいという操作方法はありますか? 第二に、操作後、新しい画像を生成する方法は?

#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

1

pointer の値を出力しています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 に答える