C SDL黑白转换中的预期标识符和未声明标识符错误
我正在尝试制作一个SDL图像转换器来制作黑白图像。到目前为止,我的代码运行良好,它从文件位置加载了一个图像,我所需要做的只是灰度转换,下面是黑白转换代码:C SDL黑白转换中的预期标识符和未声明标识符错误,c,sdl,C,Sdl,我正在尝试制作一个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
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;
}
下面似乎是发布的代码试图实现的
// 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;
}
}
//这假设transparance字节是高阶字节
int x=0;
int y=0;
Uint32*pPixel;
Uint8透明度;
uint8r;
uint8g;
uint8b;
Uint8灰色;
对于(int y=0;yh;y++)
{
对于(intx=0;xw;x++)
{
//获取像素的地址
像素=&(图像->像素[y][x]);
//提取单个颜色值
//可能需要使用系数:0.212671f、0.715160f、0.072169f
//而不是下面简单的shift+和
透明度=(*pPixel>>24)和0xFF;
r=(*pPixel>>16)和0xFF;
g=(*pPixel>>8)和0xFF;
b=(*pPixel>>0)和0xFF;
//计算灰度值
灰色=(Uint8)((Uint32)(r+g+b)/3)和0xFF;
//更新像素
*pPixel=(Uint32)透明仅供参考,您似乎缺少一组{
和}
在为内部嵌套循环发布的代码中。当然,除非您的意图是简单地声明Uint32*pixel
总共image->w
次,一个标识符名称在该代码中多次被过度使用)。啊,谢谢您!这有助于消除一些错误:)因为x
在最后一行中使用,这应该包含在内部循环的代码块中,否则该语句可能会破坏一个数组。您还缺少至少一个分号,并且我认为最上面的像素声明在逻辑上没有任何用处。而且我不知道image->&r
试图做什么。您真的打算使用双倍递增y
(一次在条件中,一次在循环的增量步骤中)
?是的:)谢谢!但是我当前的代码有什么错吗?现在我得到了*SDL.c:75:32:error:expected')'void SDL_GetRBG(pixel,image->format,&r,&g,&b)^SDL.c:75:19:注意:要匹配此“(“void SDL_GetRBG(pixel,image->format,&r,&g,&b)SDL.c:75:20:错误:函数定义void SDL_GetRBG(pixel,image->format,&r,&g,&b)中只允许不带类型的参数列表;错误代码出现问题的原因有很多。其他注释中已经介绍了一些原因(如内部for循环缺少大括号),但您仍然有SDL_GetRBG(它不存在,请使用SDL_GetRGB
),实际上没有将曲面格式传递给SDL_GetRGB
,并且在几个冲突的像素
声明之一中缺少分号。
// 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;
}
}