如何在SDL_surface中设置像素
How to set a pixel in a SDL_surface?
我需要在此页面中使用以下函数。SDL_表面结构定义为
typedef struct SDL_Surface {
Uint32 flags; /* Read-only */
SDL_PixelFormat *format; /* Read-only */
int w, h; /* Read-only */
Uint16 pitch; /* Read-only */
void *pixels; /* Read-write */
SDL_Rect clip_rect; /* Read-only */
int refcount; /* Read-mostly */
} SDL_Surface;
功能是:
void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
Uint8 *target_pixel = (Uint8 *)surface->pixels + y * surface->pitch + x * 4;
*(Uint32 *)target_pixel = pixel;
}
在这里我几乎没有什么疑问,可能是由于缺乏真实的画面。
- 为什么我们需要将
surface->pitch
乘以y
,将x
乘以4
- 先将
target_pixel
声明为8-bit integer pointer
,然后再将其强制转换为32-bit integer pointer
的必要性是什么 set_pixel
函数返回后,target_pixel
如何保留pixel
值
- 由于每个像素的大小为4(表面使用
Uint32
值像素),但计算是在Uint8
中进行的。4
很难看,请参见下文 - 使地址计算以字节为单位
- 由于要写入的像素实际上是32位的,因此指针必须是32位才能进行单次写入
由于曲面的pitch
字段是以字节为单位的,因此计算必须以字节为单元。
这是一个(没有我最初的尝试那么激进)重写:
void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels
+ y * surface->pitch
+ x * surface->format->BytesPerPixel);
*target_pixel = pixel;
}
请注意我们如何使用surface->format->BytesPerPixel
来将4
因子化。魔术常数不是个好主意。还要注意,上面假设曲面实际上使用了32位像素。
您可以使用以下代码:
unsigned char* pixels = (unsigned char*)surface -> pixels;
pixels[4 * (y * surface -> w + x) + c] = 255;
x
是您想要的点的x,y
是点的y,c
显示您想要的信息:
c=0
对应蓝色
c=1
对应绿色
c=2
对应红色
c=3
对应于α(不透明度)
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 在OpenCV中设置像素值(奇怪的结果)
- 将垫子的每个像素设置为某个值,如果它低于一个值?
- 使用视觉C++设置"Windows Form Application"中的像素值
- 如何在MFC中设置对话框的默认大小(以像素为单位)
- 在Nvidia NPP ImageCPU对象中设置像素值
- 调用 cvSet2D() 来设置图像的像素值返回值 0
- 在数组中设置像素颜色
- 如何将像素设置为cv::Mat对象中的值
- C++openGL-获取和设置像素的函数
- 如何在SDL_surface中设置像素
- 通过 C/C++ 使用 JPEG 像素格式在 v4l2 中设置/获取相机 JPEG 压缩质量
- 使用 BMP 作为高度图.如何访问像素颜色以设置高度
- 设置像素函数 [C++]
- 获取并设置像素的颜色
- 无法将大小 QMainWindow 设置为小于 200x100 像素
- 如何在Qimage中设置像素并保存更改
- 在Gdk::Pixbuf上获取像素值,使用Gdk::Cairo设置像素值
- OpenCV:如何设置像素的alpha透明度
- 如何在c++中设置像素