MiniGUI获取和设置BITMAP像素点

前言

最近有个需求,在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++;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值