前言
最近有个需求,在MiniGUI中使用FillBoxWithBitmap加载一张图片之后,需要获取到每一个像素点并重新设置像素点,因此做一下笔记
1. get pixel
需要一个结构体来存RGBA数据
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
static void getPixel(PBITMAP src) {
RGBQUAD srcdib[src->bmWidth * src->bmHeight];
int x, y, point = 0;
Uint8 *srcrow;
Uint32 pixel;
/* 循环获取像素点 */
for (y = 0; y < src->bmHeight; y++) {
for (x = 0; x < src->bmWidth; x++) {
/* 得到像素点的地址 */
srcrow = (Uint8 *) src->bmBits + y * src->bmPitch
+ x * src->bmBytesPerPixel;
pixel = *((Uint32 *) (srcrow));
/* 这是MiniGUI中根据Pixel转成RGBA的函数 */
Pixel2RGBA(HDC_SCREEN, pixel, &srcdib[point].rgbRed,
&srcdib[point].rgbGreen, &srcdib[point].rgbBlue,
&srcdib[point].rgbReserved);
/* 打印看看对不对 */
printf("%d %d %d %d\n", srcdib[point].rgbReserved,
srcdib[point].rgbRed, srcdib[point].rgbGreen,
srcdib[point].rgbBlue);
/* 记录点的位置 */
point++;
}
}
}
2. set pixel
static void setPixel(PBITMAP src, PBITMAP dstbmp) {
/* 这里根据源图片重新构造一个PBITMAP对象 */
dstbmp->bmType = src->bmType;
dstbmp->bmBitsPerPixel = src->bmBitsPerPixel;
dstbmp->bmBytesPerPixel = src->bmBytesPerPixel;
dstbmp->bmAlpha = src->bmAlpha;
dstbmp->bmColorKey = src->bmColorKey;
#ifdef _FOR_MONOBITMAP
dstbmp->bmColorRep = src->bmColorRep;
#endif
dstbmp->bmAlphaMask = src->bmAlphaMask;
dstbmp->bmAlphaPitch = src->bmAlphaPitch;
dstbmp->bmWidth = src->bmWidth;
dstbmp->bmHeight = src->bmHeight;
dstbmp->bmPitch = src->bmPitch;
dstbmp->bmBits = malloc(src->bmWidth * src->bmHeight * src->bmBytesPerPixel);
/* dstdib存的是自己需要的每个像素点的值,根据需要自己赋值 */
RGBQUAD dstdib[src->bmWidth * src->bmHeight];
int x, y, point = 0;
Uint32 pixel;
/* 设置每一个像素点的值 */
for (y = 0; y < src->bmHeight; y++) {
for (x = 0; x < src->bmWidth; x++) {
pixel = RGBA2Pixel(HDC_SCREEN, dstdib[point].rgbRed,
dstdib[point].rgbGreen, dstdib[point].rgbBlue,
dstdib[point].rgbReserved);
/* 打印看看像素是否正常 */
printf("%d %d %d %d\n", dstdib[point].rgbReserved,
dstdib[point].rgbRed, dstdib[point].rgbGreen,
dstdib[point].rgbBlue);
printf("pixel=%x\n", pixel);
/* MiniGUI根据pixel设置像素点的函数 */
SetPixelInBitmap(dstbmp, x, y, pixel);
/* 记录点的位置 */
point++;
}
}
}