我正在尝试制作一个SDL图像转换器来制作黑白图像。到目前为止,我的代码运行正常,它从文件位置加载了一个图像,我需要做的只是在灰度转换中,这里是黑白转换代码:
int x = 0;
int y = 0;
Uint32 pixel = pixels[y * image->w + x];
for (int y = 0; y++ < image->h; y++)
{
for (int x = 0; x < image->w; x++)
Uint32 *pixel = (Uint32*)image->pixels
Uint8 r=0,g=0,b=0;
SDL_GetRBG(image->&r,&g,&b);
Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
Uint32 pixel = SDL_MapRGB(image->format,v,v,v);
pixels[y * image->w + x] = pixel;
}
我收到以下错误:
SDL.c:73:3: error: expected expression
Uint32 *pixel = (Uint32*)image->pixels
SDL.c:75:3: warning: implicit declaration of function 'SDL_GetRBG' is invalid in C99 [-Wimplicit-function-declaration]
SDL_GetRBG(image->&r,&g,&b);
SDL.c:75:21: error: expected identifier
SDL_GetRBG(image->&r,&g,&b);
^
SDL.c:75:22: error: use of undeclared identifier 'r'
SDL_GetRBG(image->&r,&g,&b);
SDL.c:76:25: error: use of undeclared identifier 'r'
Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
SDL.c:76:41: error: use of undeclared identifier 'g'
Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
SDL.c:76:41: error: use of undeclared identifier 'g'
Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
SDL.c:76:41: error: use of undeclared identifier 'g'
Uint8 v = 0.212671f * r + 0.715160f * g + 0.072169f * b;
答案 0 :(得分:0)
以下似乎是发布的代码试图完成的内容
// this assumes the transparancy byte is the high order byte
int x = 0;
int y = 0;
Uint32 *pPixel;
Uint8 transparancy;
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 gray;
for (int y = 0; y < image->h; y++)
{
for (int x = 0; x < image->w; x++)
{
// get address of pixel
pPixel = &(image->pixels[y][x]);
// extract individual color values
// may want to use the factors: 0.212671f, 0.715160f, 0.072169f
// rather than the following simple shift+and
transparancy = (*pPixel >>24)&0xFF;
r = (*pPixel >>16)&0xFF;
g = (*pPixel >> 8)&0xFF;
b = (*pPixel >> 0)&0xFF;
// calculate gray value
gray = (Uint8)( (Uint32)(r+g+b)/3 )&0xFF;
// update the pixel
*pPixel = (Uint32)transparancy << 24 |
(Uint32)gray<<16 |
(Uint32)gray<<8 |
(Uint32)gray;
}
}