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++;
        }
    }
}
發佈了30 篇原創文章 · 獲贊 29 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章