Image processing SDL代码应该是灰度的,但只是正常加载图像
下面的代码应加载图像,然后在窗口中对图像进行灰度缩放。相反,它只是加载图像。我在循环中使用了printf(“hello”),以“for(int y=0;yImage processing SDL代码应该是灰度的,但只是正常加载图像,image-processing,sdl-2,grayscale,Image Processing,Sdl 2,Grayscale,下面的代码应加载图像,然后在窗口中对图像进行灰度缩放。相反,它只是加载图像。我在循环中使用了printf(“hello”),以“for(int y=0;yh;y++)”开头。但是控制台不会显示“hello”,除非我删除了使控制台打印它的SDL_延迟(20000),但是图像会闪烁一秒钟,我无法判断这是否是同一图像的灰度 #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <stdio.h> #in
#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;
}
#包括
#包括
#包括
#包括“SDL2/SDL_ttf.h”
SDL_Window*Window=NULL;
SDL_Surface*windowSurface=NULL;
SDL_表面*图像=空;
SDL_事件*事件=NULL;
SDL_纹理*纹理=空;
int main(int argc,char*argv[])
{
if(SDL_Init(SDL_Init_视频)<0)
{
perror(“无法初始化SDL”);
SDL_退出();
返回1;
}
其他的
{
window=SDL_CreateWindow(“加载图像”,SDL_WINDOWPOS_居中,SDL_WINDOWPOS_居中,640480,显示SDL_window|SDL_window_可调整大小);
如果(窗口==NULL)
perror(“无法加载图像”);
其他的
{
windowSurface=SDL_GetWindowSurface(窗口);
image=IMG_Load(“image.bmp”);
if(image==NULL)
perror(“无法加载图像”);
其他的
{
SDL_BlitSurface(图像,空,窗口表面,空);
}
SDL_更新内表面(窗口);
SDL_延迟(20000);
}
}
SDL_UpdateTexture(纹理,空,图像->像素,图像->w*sizeof(Uint32));
图像=SDL_ConvertSurfaceFormat(图像,SDL_像素格式_argb888,0);
Uint32*像素=(Uint32*)图像->像素;
int x=0;
int y=0;
对于(int y=0;yh;y++)
{
对于(intx=0;xw;x++)
{
Uint32像素=像素[y*图像->w+x];
Uint8 r=0,g=0,b=0;
SDL_GetRGB(像素,图像->格式,&r,&g,&b);
Uint8 v=0.212671f*r+0.715160f*g+0.072169f*b;
SDL_MapRGB(图像->格式,v,v,v);
}
}
int-quit=0;
while(!quit)//此循环将循环,直到满足条件,例如退出渲染器//
{
SDL_WaitEvent(event);//等待事件(退出渲染器)//
开关(事件->类型)
{
案例SDL_退出:
退出=1;
打破
}
}
SDL_自由曲面(图像);
image=NULL;
窗口=空;
windowSurface=NULL;
SDL_窗口(窗口);
IMG_Quit();
SDL_退出();
返回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;
}
#包括
#包括
#包括
SDL_Window*Window=NULL;
SDL_Surface*windowSurface=NULL;
SDL_表面*图像=空;
SDL_事件事件;//您可能希望使用对象而不是指针
SDL_纹理*纹理=空;
int main(int argc,char*argv[])
{
if(SDL_Init(SDL_Init_视频)<0)
{
perror(“无法初始化SDL”);
SDL_退出();
返回1;
}
其他的
{
window=SDL_CreateWindow(“加载图像”,SDL_WINDOWPOS_居中,SDL_WINDOWPOS_居中,640480,显示SDL_window|SDL_window_可调整大小);
如果(窗口==NULL)
perror(“无法加载图像”);//您可能需要更改此错误消息
其他的
{
windowSurface=SDL_GetWindowSurface(窗口);
image=IMG_Load(“image.bmp”);
if(image==NULL)
perror(“无法加载图像”);
//我删除了这里的闪电代码,你基本上不需要它
//而是在下面的渲染循环中执行
}
}
图像=SDL_ConvertSurfaceFormat(图像,SDL_像素格式_argb888,0);
Uint32*像素=(Uint32*)图像->像素;
int x=0;
int y=0;
对于(int y=0;yh;y++)
{
对于(intx=0;xw;x++)
{
Uint32像素=像素[y*图像->w+x];
Uint8 r=0,g=0,b=0;
SDL_GetRGB(像素,图像->格式,&r,&g,&b);
Uint8 v=0.212671f*r+0.715160f*g+0.072169f*b;
pixel=SDL_MapRGB(image->format,v,v,v);//获取作为像素值的返回值
像素[y*图像->w+x]=像素;//…并将其分配回像素
}
}
int-quit=0;
而(!退出)
{
while(SDL_polleevent(&event))//持续检查事件
{
开关(事件类型)
{
案例SDL_退出:
退出=1;
打破
}
}
//“渲染循环”
SDL_BlitSurface(图像,空,窗口表面,空);
SDL_更新内表面(窗口);
}
SDL_自由曲面(图像);
image=NULL;
窗口=空;
windowSurface=NULL;
SDL_窗口(窗口);
IMG_Quit();
SDL_退出();
返回0;
}