SDL2逐像素读取PNG24
SDL2 read PNG24 pixel by pixel
我有一个getpixel
函数,给定一个曲面,读取给定像素的r, g, b和alpha值:
void getpixel(SDL_Surface *surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
Uint8 red, green, blue, alpha;
SDL_GetRGBA(*p, surface->format, &red, &green, &blue, &alpha);
cout << (int)red << " " << (int)green << " " << (int)blue << " " << (int)alpha << endl;
}
我用Phosothop"Save for Web"保存了一个PNG图像,我选择PNG24作为格式。问题是这个函数只读取红色的值,并且总是将alpha读取为0。我尝试强制格式如下:
SDL_Surface* temp = IMG_Load(png_file_path.c_str());
SDL_Surface* image = SDL_ConvertSurfaceFormat(temp, SDL_PIXELFORMAT_RGBA8888, 0);
SDL_FreeSurface(temp);
通过这样做,它只读取alpha值。如何在SDL2中逐像素读取PNG ?
SDL_GetRGBA(*p, surface->format, &red, &green, &blue, &alpha);
尝试从*p
中提取类型为Uint8
的值。它只有一个字节,所以是的-它只会是红色或alpha取决于像素格式。SDL_GetRGBA
期望Uint32
,所以调用应该是例如SDL_GetRGBA(*(Uint32*)p, surface->format, &red, &green, &blue, &alpha);
(它只对32bpp格式有效-如果不是这种情况,您应该将surface转换为32位,或者memcpy
或BytesPerPixel
的像素数据量,否则结果将不正确)
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- SDL2逐像素读取PNG24