How to split bitmap byte array into 4 byte array to create 4 image?

abc abc 351 Reputation points
2021-02-11T11:11:28.973+00:00

Hi,

I got bitmap bit array using CreateDIBSection().How to split bitmap byte array into 4 byte array to create 4 image?

Developer technologies C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-02-13T14:17:32.557+00:00

    I did a test with 4 DIB Sections, but as said in other threads, it is simpler with GDI+ or WIC =>

    67753-dibsections.jpg

    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, L"E:\\Butterfly_Blue.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);  
    DIBSECTION ds;  
    GetObject(hBitmap, sizeof(ds), &ds);  
      
    BYTE* pBits = (BYTE*)ds.dsBm.bmBits;  
    BYTE* pScan = pBits;  
    int nWidth = ds.dsBm.bmWidth;  
    int nHeight = ds.dsBm.bmHeight;  
    long cbScan = ((nWidth * 24 + 31) & ~31) / 8;  
      
    SIZE sz;  
    sz.cx = nWidth / 2;  
    sz.cy = nHeight / 2;  
      
    BYTE* pbBitmap1;  
    HBITMAP hBitmap1 = NULL;  
    HRESULT hr = CreateNBitHBITMAP(NULL, &sz, (void**)&pbBitmap1, &hBitmap1, 24);  
      
    BYTE* pbBitmap2;  
    HBITMAP hBitmap2 = NULL;  
    hr = CreateNBitHBITMAP(NULL, &sz, (void**)&pbBitmap2, &hBitmap2, 24);  
      
    BYTE* pbBitmap3;  
    HBITMAP hBitmap3 = NULL;  
    hr = CreateNBitHBITMAP(NULL, &sz, (void**)&pbBitmap3, &hBitmap3, 24);  
      
    BYTE* pbBitmap4;  
    HBITMAP hBitmap4 = NULL;  
    hr = CreateNBitHBITMAP(NULL, &sz, (void**)&pbBitmap4, &hBitmap4, 24);  
      
    BYTE* pScan1 = pbBitmap1;  
    long cbScan1 = ((nWidth/2 * 24 + 31) & ~31) / 8;  
      
    BYTE* pScan2 = pbBitmap2;  
    long cbScan2 = ((nWidth/2 * 24 + 31) & ~31) / 8;  
      
    BYTE* pScan3 = pbBitmap3;  
    long cbScan3 = ((nWidth/2 * 24 + 31) & ~31) / 8;  
      
    BYTE* pScan4 = pbBitmap4;  
    long cbScan4 = ((nWidth/2 * 24 + 31) & ~31) / 8;  
      
    for (int nY = 0; nY < nHeight; ++nY)  
    {  
    	for (int nX = 0; nX < nWidth; ++nX)  
    	{  
    		// Top Left  
    		if (nY > nHeight / 2 && nX < nWidth / 2)  
    		{  
    			pScan1[0] = pScan[0];  
    			pScan1[1] = pScan[1];  
    			pScan1[2] = pScan[2];  
    			// Test Red BGR  
    			// pScan1[2] = 255;  
    			pScan1 += 3;  
    		}  
      
    		// Top Right  
    		if (nY > nHeight / 2 && nX > nWidth / 2)  
    		{  
    			pScan2[0] = pScan[0];  
    			pScan2[1] = pScan[1];  
    			pScan2[2] = pScan[2];  
    			// Test Green BGR  
    			// pScan2[1] = 255;  
    			pScan2 += 3;  
    		}  
      
    		// Bottom Left  
    		if (nY < nHeight / 2 && nX < nWidth / 2)  
    		{  
    			pScan3[0] = pScan[0];  
    			pScan3[1] = pScan[1];  
    			pScan3[2] = pScan[2];  
    			// Test Blue BGR  
    			// pScan3[0] = 255;  
    			pScan3 += 3;  
    		}  
      
    		// Bottom Right  
    		if (nY < nHeight / 2 && nX > nWidth / 2)  
    		{  
    			pScan4[0] = pScan[0];  
    			pScan4[1] = pScan[1];  
    			pScan4[2] = pScan[2];  
    			// Test Yellow BGR  
    			/* pScan4[1] = 255;  
    			pScan4[2] = 255; */  
    			pScan4 += 3;  
    		}  
      
    		// Next pixel  
    		pScan += 3;  
    	}  
    	// Next scan line  
    	pBits += cbScan;  
    	pScan = pBits;  
      
    	// Top  
    	if (nY > nHeight / 2)  
    	{  
    		pbBitmap1 += cbScan1;  
    		pScan1 = pbBitmap1;  
      
    		pbBitmap2 += cbScan2;  
    		pScan2 = pbBitmap2;  
    	}  
      
    	// Bottom  
    	if (nY < nHeight / 2)  
    	{  
    		pbBitmap3 += cbScan3;  
    		pScan3 = pbBitmap3;  
      
    		pbBitmap4 += cbScan4;  
    		pScan4 = pbBitmap4;  
    	}					  
    }  
      
    // Test display on screen  
    HDC hDC = GetDC(NULL);  
    HDC hDCMem = CreateCompatibleDC(hDC);  
    int nX = 200, nY = 200, nSpace = 50;  
      
    HBITMAP hBitmapOld;  
    hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);  
    BitBlt(hDC, nX, nY, nWidth, nHeight, hDCMem, 0, 0, SRCCOPY);  
    SelectObject(hDCMem, hBitmapOld);  
    DeleteObject(hBitmap);  
      
    hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap1);  
    BitBlt(hDC, nX + nWidth + nSpace, nY, nWidth / 2, nHeight / 2, hDCMem, 0, 0, SRCCOPY);  
    SelectObject(hDCMem, hBitmapOld);  
    DeleteObject(hBitmap1);  
      
    hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap2);  
    BitBlt(hDC, nX + nWidth + nSpace + nWidth/2 + nSpace, nY, nWidth / 2, nHeight / 2, hDCMem, 0, 0, SRCCOPY);  
    SelectObject(hDCMem, hBitmapOld);  
    DeleteObject(hBitmap2);  
      
    hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap3);  
    BitBlt(hDC, nX + nWidth + nSpace, nY + nHeight/2 + nSpace, nWidth / 2, nHeight / 2, hDCMem, 0, 0, SRCCOPY);  
    SelectObject(hDCMem, hBitmapOld);  
    DeleteObject(hBitmap3);  
      
    hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap4);  
    BitBlt(hDC, nX + nWidth + nSpace + nWidth / 2 + nSpace, nY +  nHeight / 2 + nSpace, nWidth / 2, nHeight / 2, hDCMem, 0, 0, SRCCOPY);  
    SelectObject(hDCMem, hBitmapOld);  
    DeleteObject(hBitmap4);  
      
    DeleteDC(hDCMem);  
    ReleaseDC(NULL, hDC);  
    

    Utility Function :

    // Adapted from MS SDK  
    HRESULT CreateNBitHBITMAP(HDC hDC, const SIZE* psize, void** ppvBits, HBITMAP* phBitmap, WORD nBitCount)  
    {  
    	*phBitmap = NULL;  
    	BITMAPINFO bmi = { 0 };  
    	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
    	bmi.bmiHeader.biWidth = psize->cx;  
    	bmi.bmiHeader.biHeight = psize->cy;  
    	bmi.bmiHeader.biPlanes = 1;  
    	bmi.bmiHeader.biBitCount = nBitCount;  
    	bmi.bmiHeader.biCompression = BI_RGB;  
    	HDC hDCUsed = hDC ? hDC : GetDC(NULL);  
    	if (hDCUsed)  
    	{  
    		*phBitmap = CreateDIBSection(hDCUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);  
    		if (hDC != hDCUsed)  
    		{  
    			ReleaseDC(NULL, hDCUsed);  
    		}  
    	}  
    	return (NULL == *phBitmap) ? E_OUTOFMEMORY : S_OK;  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.