SDL 代码应该灰度化但只是正常加载图像

SDL code should greyscale but just loads the image normally

下面的代码应该加载图像,然后在 window 中对图像进行灰度缩放。相反,它只是加载图像。我在以 "for (int y = 0; y < image->h; y++)" 开头的循环中使用了 printf("hello") 但是控制台不显示 "hello",除非我删除了 SDL_Delay(20000) 这使得控制台打印它,但图像闪烁了一秒钟,我无法判断那是否是同一图像的灰度。

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include "SDL2/SDL_ttf.h"

SDL_Window *window = NULL;

SDL_Surface *windowSurface = NULL;

SDL_Surface *image = NULL;

SDL_Event *event = NULL;

SDL_Texture *texture = NULL;




int main(int argc, char *argv[])
{

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        perror("Cannot initialise SDL");
        SDL_Quit();
        return 1;
    }   
    else
    {   
        window = SDL_CreateWindow("Loading_image", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
        if(window == NULL)
            perror("Cannot load image");

        else
        {   
            windowSurface = SDL_GetWindowSurface(window);
            image = IMG_Load("image.bmp");

            if(image == NULL)
                perror("Cannot load image");

            else
                {
                    SDL_BlitSurface(image, NULL, windowSurface, NULL);
            }   

            SDL_UpdateWindowSurface(window);
            SDL_Delay(20000);
        }
    }

    SDL_UpdateTexture(texture, NULL, image->pixels, image->w * sizeof(Uint32));


    image = SDL_ConvertSurfaceFormat(image,SDL_PIXELFORMAT_ARGB8888,0);
    Uint32 * pixels = (Uint32 *)image->pixels; 

    int x = 0;
    int y = 0;

    for (int y = 0; y < image->h; y++)
    {    
        for (int x = 0; x < image->w; x++)
        {
            Uint32 pixel = pixels[y * image->w + x];
            Uint8 r=0,g=0,b=0;
            SDL_GetRGB(pixel, image->format, &r,&g,&b);
            Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
            SDL_MapRGB(image->format,v,v,v);

        }
    }  


    int quit = 0;
    while (!quit)  //This loop will loop until the conditions are met e.g. You quit the renderer//
        {
            SDL_WaitEvent(event);// waits for the event (quitting the renderer)//

            switch (event->type)
            {
                case SDL_QUIT: 
                quit = 1;  
                break; 
            }
        }

    SDL_FreeSurface(image);
    image = NULL;
    window = NULL;
    windowSurface = NULL;
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();

    return  0;
}

您的代码存在几个问题。主要是 SDL 细节,但也有一些与灰度转换有关的问题。 我删除了所有我能发现的不必要的东西,并通过评论对一些更改进行了注释。

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>

SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *image = NULL;
SDL_Event event; // You may want to use an object instead of a pointer
SDL_Texture *texture = NULL;

int main(int argc, char *argv[])
{
   if (SDL_Init(SDL_INIT_VIDEO) < 0)
   {
      perror("Cannot initialise SDL");
      SDL_Quit();
      return 1;
   }
   else
   {
      window = SDL_CreateWindow("Loading_image", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
      if (window == NULL)
         perror("Cannot load image"); // You may want to change this error message 

      else
      {
         windowSurface = SDL_GetWindowSurface(window);
         image = IMG_Load("image.bmp");

         if (image == NULL)
            perror("Cannot load image");

         // I removed the blitting code here, you basically don't need it here
         // Rather do it in the render loop below
      }
   }


   image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_ARGB8888, 0);
   Uint32 * pixels = (Uint32 *)image->pixels;

   int x = 0;
   int y = 0;

   for (int y = 0; y < image->h; y++)
   {
      for (int x = 0; x < image->w; x++)
      {
         Uint32 pixel = pixels[y * image->w + x];
         Uint8 r = 0, g = 0, b = 0;
         SDL_GetRGB(pixel, image->format, &r, &g, &b);
         Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
         pixel = SDL_MapRGB(image->format, v, v, v); // Get the return value which is the pixel value
         pixels[y * image->w + x] = pixel; // ...and assign it back to the pixels 
      }
   }

   int quit = 0;
   while (!quit)
   {
      while (SDL_PollEvent(&event)) // Continous checking for events
      {
         switch (event.type)
         {
         case SDL_QUIT:
            quit = 1;
            break;
         }
      }

      // "Render loop"
      SDL_BlitSurface(image, NULL, windowSurface, NULL);
      SDL_UpdateWindowSurface(window);
   }

   SDL_FreeSurface(image);
   image = NULL;
   window = NULL;
   windowSurface = NULL;
   SDL_DestroyWindow(window);
   IMG_Quit();
   SDL_Quit();

   return  0;
}