Auxiliary class in digital image processing: CDib class

Source: Internet
Author: User

The CDIB class is an auxiliary basic class used in digital image processing. It provides operations on Dib, including loading bitmap, obtaining bitmap information, creating a color palette, and drawing bitmaps.

The Code is as follows:

 

// Dib. h: interface for the CDIB class.
//
//////////////////////////////////////// //////////////////////////////

# If! Defined (afx_dib_h1_ac952c3a_9b6b_4319_8d6e_e7f509348a881_encoded _)
# Define afx_dib_h1_ac952c3a_9b6b_4319_8d6e_e7f509348a881_encoded _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000

# Define PalVersion 0x300 // color palette version

Class CDib: public CObject
{
Public:
CDib ();
Virtual ~ CDib ();
// Operations
Public:
// Function declaration used to operate DIB
BOOL DrawDib (HDC, LPRECT, HGLOBAL, LPRECT, CPalette *); // display bitmap
BOOL ConstructPalette (HGLOBAL, CPalette *); // constructs the logical palette.
LPSTR GetBits (LPSTR); // The entry address for retrieving bitmap data
DWORD GetWidth (LPSTR); // retrieves the bitmap width.
DWORD GetHeight (LPSTR); // retrieves the height of a bitmap.
WORD GetPalSize (LPSTR); // obtain the color palette size.
WORD GetColorNum (LPSTR); // obtain the number of colors contained in the bitmap.
WORD GetBitCount (LPSTR); // retrieves the color depth of a bitmap.
HGLOBAL CopyObject (HGLOBAL); // used to copy bitmap objects

BOOL SaveFile (HGLOBAL, CFile &); // stores the bitmap as a file
HGLOBAL LoadFile (CFile &); // loads a bitmap from a file

// When processing an image, the byte width of the bitmap must be a multiple of 4,
// We designed the GetRequireWidth function to handle this special case.
Int getreqbytewidth (INT); // Number of converted bytes getrequirebytewidth
Long getrectwidth (lpcrect); // obtain the area width.
Long getrectheight (lpcrect); // gets the height of the Region
Public:
Void clearmemory ();
Void initmembers ();
Public:
Lpbitmapinfo lpbminfo; // pointer to the bitmapinfo Structure
Lpbitmapinfoheader lpbmihrd; // pointer to the bitmapinfoheader Structure
BITMAPFILEHEADER bmfHeader; // BITMAPFILEHEADER Structure
LPSTR lpdib; // pointer to DIB
LPSTR lpDIBBits; // DIB pixel pointer
DWORD dwDIBSize; // DIB size

HGLOBAL m_hDib; // The DIB object handle.
 
RGBQUAD * lpRgbQuag; // pointer to the color table
};

# Endif //! Defined (afx_dib_h1_ac952c3a_9b6b_4319_8d6e_e7f509348a881_encoded _)

 

// MyDib. cpp: implementation of the CDib class.
//
//////////////////////////////////////// //////////////////////////////

# Include "stdafx. h"
# Include "Dib. h"

# Include <math. h>

# Ifdef _ DEBUG
# Undef THIS_FILE
Static char THIS_FILE [] =__ FILE __;
# Define new DEBUG_NEW
# Endif

/*
* Dib header flag (string "BM ")
*/
# Define DIB_MARKER (WORD) ('M' <8) | 'B ')
//////////////////////////////////////// //////////////////////////////
// Construction/Destruction
//////////////////////////////////////// //////////////////////////////
CDib: CDib ()
{
InitMembers ();
}

CDib ::~ CDib ()
{
ClearMemory ();
}

/*************************************** **********************************
* Function name: DrawDib ()
* Parameter description:
* HDC hDC: output device DC
* LPRECT lpDCRect: draws a rectangular area.
* Hglobal hdib, dib object handle
* Lprect lpdibrect, output region of Dib
* Cpalette * ppal, pointer of the palette
* Function type: bool
* Function: This function is mainly used to draw Dib objects.
**************************************** ********************************/

Bool CDIB: drawdib (HDC,
Lprect lpdcrect,
Hglobal hdib,
Lprect lpdibrect,
Cpalette * ppal)
{
Bool bsuccess = false; // mark of successful re-painting
Hpalette holdpal = NULL; // previous palette
If (hdib = NULL) // determines whether the dib object is valid.
{
Return FALSE; // No, return
}
Lpdib = (LPSTR): GlobalLock (HGLOBAL) hDIB); // lock DIB
LpDIBBits = GetBits (lpdib); // locate the start position of the DIB image pixel
If (pPal! = NULL) // obtain the DIB palette and select it in the device Environment
{
HPALETTE hPal = (HPALETTE) pPal-> m_hObject;
HOldPal =: SelectPalette (hDC, hPal, TRUE );
}
: SetStretchBltMode (hDC, COLORONCOLOR); // you can specify the display mode.
BSuccess =: StretchDIBits (hDC, // device environment handle
LpDCRect-> left, // target X coordinate
LpDCRect-> top, // target Y coordinate
GetRectWidth (lpDCRect), // target width
GetRectHeight (lpDCRect), // target height
LpDIBRect-> left, // source X coordinate
LpDIBRect-> top, // source Y coordinate
GetRectWidth (lpDIBRect), // Source width
GetRectHeight (lpDIBRect), // source height
LpDIBBits, // pointer to the dib Pixel
(LPBITMAPINFO) lpdib, // pointer to the bitmap Information Structure
DIB_RGB_COLORS, // number of colors used
SRCCOPY); // grating Operation Type
: GlobalUnlock (hDIB); // unlock
If (hOldPal! = NULL) // restore the system palette
{
: SelectPalette (hDC, hOldPal, TRUE );
}
Return bSuccess;
}

/*************************************** **********************************
* Function name: ConstructPalette (HGLOBAL hDIB, CPalette * pPal)
* Function parameters:
* HGLOBAL hDIB, DIB object handle
* CPalette * pPal, pointer of the palette
* Function type: BOOL
* Function Description: This function creates a logical color palette Based on DIB.
**************************************** ********************************/

BOOL CDib: ConstructPalette (HGLOBAL hDIB, CPalette * pPal)
{
 
HANDLE hLogPal; // HANDLE of the logical color palette
Int iLoop; // Loop Variable
BOOL bSuccess = FALSE; // creation result
If (hDIB = NULL) // determines whether the DIB object is valid.
{
Return FALSE; // return FALSE
}
Lpdib = (LPSTR): GlobalLock (HGLOBAL) hDIB); // lock DIB
Lpbminfo = (LPBITMAPINFO) lpdib;
Long wNumColors = GetColorNum (lpdib); // gets the number of colors in the DIB color table.
If (wNumColors! = 0)
{
HLogPal =: GlobalAlloc (GHND, sizeof (LOGPALETTE) // allocate memory to the logical palette
+ Sizeof (PALETTEENTRY)
* WNumColors );
If (hLogPal = 0) // exit if the request fails.
{
: GlobalUnlock (HGLOBAL) hDIB); // unlock
Return FALSE;
}
LPLOGPALETTE lpPal = (LPLOGPALETTE): GlobalLock (HGLOBAL) hLogPal );

LpPal-> palVersion = PalVersion; // you can specify the color palette version.
LpPal-> palNumEntries = (WORD) wNumColors; // you can specify the number of colors.
For (iLoop = 0; iLoop <(int) wNumColors; iLoop ++) // read the color palette
{
LpPal-> palPalEntry [iLoop]. peRed = lpbminfo-> bmiColors [iLoop]. rgbRed; // read the primary color components.
LpPal-> palPalEntry [iLoop]. peGreen = lpbminfo-> bmiColors [iLoop]. rgbGreen;
LpPal-> palPalEntry [iLoop]. peBlue = lpbminfo-> bmiColors [iLoop]. rgbBlue;
LpPal-> palPalEntry [iLoop]. peFlags = 0; // Reserved Bit
}
BSuccess = pPal-> CreatePalette (lpPal); // create a palette based on the Logical palette and return a pointer
: GlobalUnlock (HGLOBAL) hLogPal); // unlock
: GlobalFree (HGLOBAL) hLogPal); // release the logical palette
}
: GlobalUnlock (HGLOBAL) hDIB); // unlock
Return bSuccess; // return the result
}

/*************************************** **********************************
* Function name: GetBits (LPSTR lpdib)
* Function parameters:
* LPSTR lpdib, pointer to DIB object
* Function type: LPSTR
* Function: calculates the starting position of the DIB pixel and returns a pointer to it.
**************************************** ********************************/

LPSTR CDib: GetBits (LPSTR lpdib)
{
Return (lpdib + (LPBITMAPINFOHEADER) lpdib)-> biSize + GetPalSize (lpdib ));
// Return (lpdib + * (LPDWORD) lpdib + GetPalSize (lpdib ));
}

/*************************************** **********************************
* Function name: GetWidth (LPSTR lpdib)
* Function parameters:
* LPSTR lpdib, pointer to DIB object
* Function type: DWORD
* Function: This function returns the image width in DIB.
**************************************** ********************************/

DWORD CDib: GetWidth (LPSTR lpdib)
{
Return (LPBITMAPINFOHEADER) lpdib)-> biWidth; // returns the DIB width.
}

/*************************************** **********************************
* Function name: GetHeight (LPSTR lpdib)
* Function parameters:
* LPSTR lpdib, pointer to DIB object
* Function type: DWORD
* Function: This function returns the Image Height in DIB.
**************************************** ********************************/

DWORD CDib: GetHeight (LPSTR lpdib)
{
Return (LPBITMAPINFOHEADER) lpdib)-> biHeight; // returns the DIB height.

}
/*************************************** **********************************
* Function name: GetPalSize (LPSTR lpdib)
* Function parameters:
* LPSTR lpdib, pointer to DIB object
* Function type: WORD
* Function: This function returns the size of the palette in DIB.
**************************************** ********************************/

WORD CDib: GetPalSize (LPSTR lpdib)
{
Return (WORD) (GetColorNum (lpdib) * sizeof (RGBQUAD); // calculate the size of the palette in DIB
}

/*************************************** **********************************
* Function name: GetColorNum (LPSTR lpdib)
* Function parameters:
* LPSTR lpdib, pointer to DIB object
* Function type: WORD
* Function: This function returns the number of colors in the DIB palette.
**************************************** ********************************/
Word CDIB: getcolornum (lpstr lpdib)
{
Long dwclrused = (lpbitmapinfoheader) lpdib)-> biclrused; // read the dwclrused Value
If (dwclrused! = 0)
{
Return (Word) dwclrused; // This value is returned if dwclrused is not 0.
}
Word wbitcount = (lpbitmapinfoheader) lpdib)-> bibitcount; // read the bibitcount Value
Switch (wbitcount) // calculate the number of colors based on the number of pixels
{
Case 1:
Return 2;

Case 4:
Return 16;

Case 8:
Return 256;

Default:
Return 0;
}
}

/*************************************** **********************************
* Function name: getbitcount (lpstr lpdib)
* Function parameters:
* Lpstr lpdib, pointer to Dib object
* Function type: Word
* Function: This function returns dibbitcount.
**************************************** ********************************/
Word CDIB: getbitcount (lpstr lpdib)
{
Return (lpbitmapinfoheader) lpdib)-> bibitcount; // return Bit Width
}

/*************************************** **********************************
* Function name: copyobject (hglobal hglob)
* Function parameters:
* Hglobal hglob, memory region to be copied
* Function type: hglobal
* Function: This function copies the specified memory area.
**************************************** ********************************/

HGLOBAL CDib: CopyObject (HGLOBAL hGlob)
{
If (hGlob = NULL)
Return NULL;
DWORD dwLen =: GlobalSize (HGLOBAL) hGlob); // gets the size of the specified memory area
HGLOBAL hTemp =: GlobalAlloc (GHND, dwLen); // allocate new memory space
If (hTemp! = NULL) // determine whether the allocation is successful
{
Void * lpTemp =: GlobalLock (HGLOBAL) hTemp); // lock
Void * lp =: GlobalLock (HGLOBAL) hGlob );
Memcpy (lpTemp, lp, dwLen); // copy
: GlobalUnlock (hTemp); // unlock
: GlobalUnlock (hGlob );
}
Return htemp;
}

/*************************************** **********************************
* Function name: SaveFile (hglobal hdib, cfile & file)
* Function parameters:
* Hglobal hdib, The DIB to save
* Cfile & file: Save the cfile file.
* Function type: bool
* Function: saves the specified Dib object to the specified cfile.
**************************************** *********************************/

Bool CDIB: SaveFile (hglobal hdib, cfile & file)
{
If (hdib = NULL)
{
Return false; // If DiB is null, false is returned.
}
Lpbmihrd = (lpbitmapinfoheader): globallock (hglobal) hdib); // read the bitmapinfo structure and lock
If (lpbmihrd = NULL)
{
Return false; // if it is null, false is returned.
}
Bmfheader. bftype = dib_marker; // fill the file header
Dwdibsize = * (lpdword) lpbmihrd + getpalsize (lpstr) lpbmihrd); // File Header size + color table size
DWORD dwbmbitssize; // pixel size
Dwbmbitssize = getreqbytewidth (lpbmihrd-> biwidth) * (DWORD) lpbmihrd-> bibitcount) * lpbmihrd-> biheight; // The size is width * height.
Dwdibsize + = dwbmbitssize; // The size after calculation when the number of bytes in each row of DiB is a multiple of 4
Lpbmihrd-> bisizeimage = dwbmbitssize; // update bisizeimage
Bmfheader. bfsize = dwdibsize + sizeof (bitmapfileheader); // File Size
Bmfheader. bfreserved1 = 0; // two reserved words
Bmfheader. bfreserved2 = 0;
BmfHeader. bfOffBits = (DWORD) sizeof (BITMAPFILEHEADER) + lpbmihrd-> biSize // calculates the offset bfOffBits
+ GetPalSize (LPSTR) lpbmihrd );
TRY
{
File. Write (& bmfHeader, sizeof (BITMAPFILEHEADER); // Write the file header
File. WriteHuge (lpbmihrd, dwDIBSize); // write the DIB header and pixel
}
CATCH (CFileException, e)
{
: GlobalUnlock (HGLOBAL) hDib); // unlock
THROW_LAST (); // throw an exception
}
END_CATCH
: GlobalUnlock (HGLOBAL) hDib); // unlock
Return TRUE; // return TRUE
}

/*************************************** **********************************
* Function name: LoadFile (CFile & file)
* Function parameters:
* CFile & file: to read and obtain the CFile file
* Function type: HGLOBAL
* Function: Read the DIB object in the specified file to the specified memory area.
**************************************** *********************************/

HGLOBAL CDib: LoadFile (CFile & file)
{

DWORD dwFileSize;
 
DwFileSize = file. GetLength (); // get the file size
If (file. Read (LPSTR) & bmfHeader, sizeof (bmfHeader ))! = Sizeof (bmfHeader) // read the DIB File Header
{
Return NULL; // returns NULL if the size is inconsistent.
}
If (bmfHeader. bfType! = DIB_MARKER) // determines whether it is a DIB object.
{
Return NULL; // if not, return NULL.
}
M_hDib = (HGLOBAL): GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, dwFileSize-sizeof (BITMAPFILEHEADER); // allocate DIB memory
If (m_hDib = 0)
{
Return NULL; // allocation failed, return NULL
}
//////////////////////////////////////// /////////////////////////////////
// Assign values to CDib member variables
Lpdib = (LPSTR): GlobalLock (HGLOBAL) m_hDib); // lock
Lpbminfo = (BITMAPINFO *) lpdib;
Lpbmihrd = (BITMAPINFOHEADER *) lpdib;
LpRgbQuag = (RGBQUAD *) (lpdib + lpbmihrd-> biSize );
Int m_numberOfColors = GetColorNum (LPSTR) lpbmihrd );
If (lpbmihrd-> biClrUsed = 0)
Lpbmihrd-> biclrused = m_numberofcolors;
DWORD colortablesize = m_numberofcolors * sizeof (rgbquad );
Lpdibbits = lpdib + lpbmihrd-> bisize + colortablesize;
//////////////////////////////////////// /////////////////////////////////
 
If (file. readhuge (lpdib, dwfilesize-sizeof (bitmapfileheader ))! = // Read Pixel
Dwfilesize-sizeof (bitmapfileheader) // inconsistent size
{
: Globalunlock (hglobal) m_hdib); // unlock
: Globalfree (hglobal) m_hdib); // release the memory.
Return NULL;
}
: Globalunlock (hglobal) m_hdib); // unlock
Return m_hdib; // returns the dib handle.
}

/*************************************** **********************************
* Function name: GetReqByteWidth (int bits)
* Function parameters:
* Int bits, number of digits
* Function type: int
* Function: obtain the number of bytes in the row. The value must be a multiple of 4.
**************************************** *********************************/
Int CDib: GetReqByteWidth (int bits)
{
Int getBytes = (bits + 31)/32*4;
Return getBytes;
}

/*************************************** **********************************
* Function name: GetRectWidth (LPCRECT lpRect)
* Function parameters:
* LPCRECT lpRect: pointer to the rectangle area
* Function type: long
* Function: obtains the width of a rectangle.
**************************************** *********************************/

Long CDIB: getrectwidth (lpcrect lprect)
{
Long nwidth = lprect-> right-lprect-> left;
Return nwidth;
}
/*************************************** **********************************
* Function name: getrectheight (lpcrect lprect)
* Function parameters:
* Lpcrect lprect: pointer to the rectangle area
* Function type: Long
* Function: obtains the height of a rectangle.
**************************************** *********************************/
Long CDIB: getrectheight (lpcrect lprect)
{
Long nheight = lprect-> bottom-lprect-> top;
Return nheight;
}

/*************************************** **********************************
* Function name: InitMembers ()
* Function type: void
* Function: Initialize the member variables of the class.
**************************************** *********************************/
Void CDib: InitMembers ()
{
M_hDib = NULL;
Lpbmihrd = NULL; // pointer to the BITMAPINFO Structure
Lpdib = NULL;
LpDIBBits = NULL;
DwDIBSize = 0;
LpRgbQuag = NULL;
}

/*************************************** **********************************
* Function name: ClearMemory ()
* Function type: void
* Function: reset the member variables of the class.
**************************************** *********************************/
Void CDib: ClearMemory ()
{
If (m_hDib! = NULL)
: GlobalFree (m_hDib );
Lpbmihrd = NULL; // pointer to the BITMAPINFO Structure
Lpdib = NULL;
DwDIBSize = 0;
LpRgbQuag = NULL;
DwDIBSize = 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.