DIBCPP

xiaoxiao2021-03-06  85

#include "stdafx.h" #include "dib.h"

Implement_dynamic (CDIB, CGDIObject);

////// CDIB //// éè ± ¸þ¹þ¹þ¹þ¹ »í1/4àà (° æ ± 3 0¹ ¹¹ þ¹:» í// éè ± ¸þ¹þ¹þ¹þ¹ »í4μä''/2 ¨, ïô/3/3/3/4, ¶áèë , ± £ IR, 2¶ × 1/2 î »í4//// ° æ ± 3/4ëùóð: þþþ, ,þþ þþþþþþ:: þþ

// ¹¹¹¹¹¹o ¯êýcdib :: cdib (): cgdiobject () {}

// ¹oo ¯êýcdib :: ~ cdib () {deleteObject ();}

///// Createdib (int CX, int CY, uint ibitcount, const void * lpbits) /// íê3¹¹|Äü: // '' 1//////// êäë2îêý: // í1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/4ïñê íèè cx // í1/1/1/1////////1///////////////////////////////////////////////////>

// ''/2 ¨dibî »í1/1/4bool cdib :: Createdib (int Cx, int CY, uint ibitcount, const void * lpbits) {assert (ibitcount == 1) || (ibitcount == 4) || (ibitcount = = 8) || (ibitcount == 16) || (ibitcount == 24) || (ibitcount == 32));

// Create a BitmapinfoHeader Structure to described the dib // '' ¨ðå í í = SizeOf (BitmapInfoHeader); // 256 * sizeof (rgbquad); BitmapInfo * PBMI; BYTE * PBYTE;

Switch (ibitcount) {casse 8: isize = 4 * 4; Break; Case 1: Case 4: Case 16: Case 24: Case 32: Break; default: Break;

PBYTE = new byte [isize]; pBMI = (BitmapInfo *) PBYTE; MEMSET (PBMI, 0, ISIZE);

pBMI-> bmiHeader.biSize = sizeof (BITMAPINFOHEADER); pBMI-> bmiHeader.biWidth = cx; pBMI-> bmiHeader.biHeight = cy; pBMI-> bmiHeader.biPlanes = 1; pBMI-> bmiHeader.biBitCount = ibitcount; pBMI- > bmiheader.bicompression = bi_rgb; // to use this flag.

Bool Bret = Createdibindirect (PBMI, LPBITS); Delete [] Pbyte; Return (BRET);}

///// Createdibindirect (lpbitmapinfo pbmi, const void * lpbits) // // íê3¹¹|Äü: // ''/// ¨ä ²2îêê:: // î »í4ðåï ¢ 1/2¹¹¹¹¹¹õõ PBMI / / Í1/1/////////// ø2îêý: // êç · ñ3¹ ¹ ¹ ¹ ¹d »í1///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Ô¶ôó if (m_hobject! = Null) {deleteObject ();

. // Create the DIB section //''½¨ CDC * pDC = new CDC; pDC-> CreateCompatibleDC (NULL); LPVOID pBits; HBITMAP hDIB = :: CreateDIBSection (pDC-> GetSafeHdc (), pBMI, DIB_RGB_COLORS, & pBits , NULL, 0); DELETE PDC;

Assert (HDIB); ASSERT (PBITS); Attach (HDIB);

// ¿ 1/2 ± 'dïññý4 ý s¾ setdibbits (GetWidthbytes () * GetHeight (), LPBITS;

Return True;}

//// // setDibits (dword dwcount, const void * lpbits) // // íê3¹¹|Äü: // éèöãí1///// éèöí1///////// éèöã1//////// éèöã1//////// éèöã¼1/////// éèöã1///////// éèöã¼1 ««ýý¾,, 1/2« lpbits μÄêý¾¾¾¿ 1/2 ± öáí/1/1//// êäë2îêý: // î »í1/1/1ýý¾¾'' óð¡¡¡ DWCOUNT // í1/1/1/4ïñýý4ý lpbits //// · μ »Ø2îêý: // ¿ 1/2 ±'μÄêý4ý' óð¡¡ ////

// Set DIB's bits // ÉèÖÃͼÏñÊý¾ÝDWORD CDIB :: SetDIBBits (DWORD dwCount, const void * lpBits) {if (lpBits = NULL!) {LPVOID pBits = GetBits (); memcpy (pBits, lpBits, dwCount); return dwCount;} Return 0;}

/// // getDibbits (dWord dwcount, lpvoid lpbits) /// íê3¹¹|Äü: // μãμ½¹¹1/1///// è¹û lpbits ªªª Õ, ôò · μ »Ø 12/// èç¹û lpbits 2» ªªª Õ, // ô½1/2 «í1/1/1/1¼ñý¾¾¿¿ 1/2 ± áá ¸ãõõõ, // 2 ¢ · μ »Øí1/1ññýý¾¾ööõõ; /// ääë2îêý: // ¿ 1/2 ±'μÄî »í //1/1ý¾/4ý'ó¡¡¡¡ DWCOUNT // í1/1/1/1/1/1/4 ý l¼ lpbits //// · μ »Ø2îêý: // í4ïïñýý4ýöõõõ ///////////

// Get DIB's bits // μÃμ½Í¼ÏñÊý¾ÝLPVOID CDIB :: GetDIBBits (DWORD dwCount, LPVOID lpBits) {LPVOID pBits = GetBits (); if (lpBits = NULL!) {Memcpy (lpBits, pBits, dwCount); return pBits;} else { Return PBITS;}} //// // Operator = (CDIB & COPY) //// íê3¹¹|Äü: // öøôô¸ ¸3öμ · û /// äë2îêý: // ªªª 1/2 ±'μÄî »í1/1/////// · μ» Ø2îêý: // ðííí¼ïñêý¾/////////

// ÖØÔظ³Öμ · ûCDIB & CDIB :: operator = (CDIB & copy) {DIBSECTION DibSection; // μÃμ½ÔͼÏñÐÅÏ ¢ copy.GetDibSection (& DibSection); int nSize = DibSection.dsBmih.biClrUsed * sizeof (RGBQUAD) sizeof (BITMAPINFOHEADER);

// éêçëðíí¼¼ñåå ¢ í Äú'æ Byte * pbyte = new byte [nsize]; // ¿ 1/2 ±'ðåï ¢ Memcpy (Pbyte, & (Dibsection.dsbmih), Sizeof (BitmapInfoHead);

CDC * PDC = Copy.getdc (); // μãå μ :: getDibcolortable (PDC-> getsafehdc (), 0, dibsection.dsbmih.bized, (rgbquad *) (pbyte sizeof (bitmapInfoHead)) Copy.ReleaseDC (PDC);

// '' '1/2 ¨ðâî »í1/4 bitmapinfo * pBMI = (BitmapInfo *) PBYTE; CREATEDIBISDIRECT (PBMI);

// ¿ 1/2 ± 'dïðå ¢ int NTOTALSIZE = Copy.GetWidthbytes () * Copy.getHEight (); memcpy (GetBits (), Copy.getBits (), NTOTALSIZE

DELETE [] PBYTE; RETURN (* this);

//// // setpalette (uint ustartindex, uint centries, const rgbquad * pcolors) /// íê3¹¹|Äü: // éèö μ ÷ é «° å //// êäë2îêý: // μ ÷ é é« «¿ ªª1/4 ÷ ÷ ý uStartIndex // μ ÷ é «° åèë¿ Ú Centries // õõ «ê¾/4ý pcolors //// · μ» Ø2îêý: // þþ ////

// Set the color table in the DIB section.// ÉèÖÃμ ÷ É «° åvoid CDIB :: SetPalette (UINT uStartIndex, UINT cEntries, CONST RGBQUAD * pColors) {HDC hdc = :: CreateCompatibleDC (NULL); HBITMAP hOld = ( Hbitmap :: SelectObject (HDC, M_HObject);

:: setDibcolortable (HDC, UstartIndex, Centries, Pcolors); :: SelectObject (HDC, HOLD); :: DeleteObject (HDC);

/// // setPalette (cpalette * ppal) // // íê3¹¹|Äü: // éèöã ÷ é «° å //// éäë2îêý: // μ ÷ é« ½ ¹¹¹¹¹¹õ ppal //// · μ »Ø2îêý: // þþ ////// éèöμ ÷ é« ° åvoid cdib :: setPalette (cpalette * ppal) {assert (ppal);

// Get the colors from the Palette Int IColors = 0; PPAL-> GetObject (SizeOf (Icolors), & Icolors); Assert (Icolors> 0); Paletteentry * Ppe = New Paletteentry [ICOLORS]; PPAL-> getPaletteentries (0, Icolors, PPEs;

// build a Table of rgbquads rgbquad * prgb = new rgbquad [iColors]; assert (prg); for (int i = 0; i

SetPalette (0, ICOLORS, PRGB);

Delete [] PRGB; Delete [] PPE;

//// // Getdc (void) /// íê3¹¹|Äü: // μãμ½½îîî »í1///// êääë2îêý: // þþ //// · μ» ø2îêý: /// óëî »í4ï¹¹¹¹èè ± ¸ ¸ Ö õõõ ////

// μã½½îî »í1/1/1¼¹¹¹¹μè cdc * cdib :: getdc (void) {cdibdc * PDC = new cdibdc; if (pdc == null) return (null); PDC-> CreateCompatibleDC (NULL); PDC-> m_hold = Hbitmap :: SelectObject (PDC-> getsafehdc (), getsafehandle ());

Return (PDC);

//// // ReleaseDC (CDC * PDC) // // íê3¹¹|Äü: // μãî½îî »í1//// óäë2îêý: // óëîî» í1//// · μ »Ø2îêý: / / Êç · ñ3¹¹| ////

// êí · μμ½½μäîî »í1/1/1/4ï¹¹¹μèè ¸Bool CDIB :: ReleaseDC (CDC * PDC) {Assert (PDC! = NULL); if (PDC-> iskindof (Runtime_Class (CDIBDC))) {Delete PDC; Return (TRUE); } Return (false);

#ifdef _debugvoid cdib :: dump (cdumpcontext & dc) const {cgdiobject :: dump (dc);

IF (m_hobject == null) return;

Bitmap BM; Verify (SizeOf (BM), & BM)); DC << "bm.bmtype =" << bm.bmtype; DC << "/nbm.bmheight =" << bm.bmHeight; DC << "/nbm.bmwidth =" << bm.bmwidth; DC << "/NBM.BMWIDTHBYTES =" << bm.bmwidthbytes; DC << "/nbm.bmplanes =" << bm.bmplanes; DC << "/ NBM.BMBITSPIXEL = "<< bm.bmbitspixel; DC <<" / n ";} # ENDIF

//// // loadbmp (lpcstr filename) //// íê3¹¹|Äü: // ¶áèëí¼1// êäë2îêêý: // »í4îî///// μ μ û / ///////// μ μ FileName //////// μ μ μ FileName //////// μ μ FileName /////// μ» ø2îêý: // êç · ñ3 ý¹| (-1ªª2 »3¹¹|) ////

// ¶áèëí/1/1/1ñää1/1int cdib :: loadbmp (lpcstr filename) {// ¿ ªªä //4 þ cfile file (filename, cfile :: moderad | cfile :: sharedenynne);

Word bfType; dword bfsize; // ¶áèëîä1/// ¶áèîîä1//// ¶áëîîî1//// áëîîîîþ (& bftype, sizeof (word)); File.Read (& bfsize, sizeof (dword)); if (bfsize <= 0) {file.close (); return -1;} // êç · ñBMPîä1/4 þ IF (bftype! = ((Word) 'm' << 8) 'b')) {file.close (); return -1;}

DWORD BFOFFBITS; // õõμ1/2 î »í //1¼ý¾¾¾ææðê1/1/1ýý¾¾ææðê1/1//4« òæ2 ¢ ááë File.seek (2 * SizeOf (Word), cfile :: current); File.Read (& bfffbits, sizeof (dword);

LPVOID Dibbuf; dibbuf = (lpvoid) New char [bfsize];

IF (dibbuf == null) {file.close (); return -1;}

// ¶ÁÈëÎ »Í¼Êý¾Ý file.ReadHuge (DibBuf, bfSize); file.Close (); LPBITMAPINFO pBMI; int size = bfOffBits - 14; pBMI = (LPBITMAPINFO) new char [sizeof (BITMAPINFOHEADER) 256 * sizeof (RGBQUAD) ];

// éú3éðå ¢ í í · Memcpy (pbmi, dibbuf, size); lpvoid lpbits = (lp4tr) Dibbuf size);

// '' '1/2 ¨î »í1/4 createDibindirect (PBMI, LPBITS);

Delete pbmi; delete dibbuf; returnif 1;

//// // GetColorused () /// íê3¹¹|Äü: // μ½½½¹¹¹¹μä :õ «êý //// êäþ2îêêý: // þþþ ////// ø2îêý: // õ «õ« êý ////// / μÃμ½Ê¹ÓÃμÄÑÕÉ «Êýint CDIB :: GetColorUsed () {LPBITMAPINFOHEADER pBMIH; DIBSECTION DibSection; GetDibSection (& DibSection); pBMIH = & DibSection.dsBmih; return pBMIH-> biClrUsed;}

/// / / getpalette () /// íê3¹¹|Äü: // μã½½¹¹¹¹¹: ÷ ÷ «° å //// øäë²2îêý: // þþþ //// · μ» ø2îêý: // μ μ é «° õõõõ, óíêó|êí · å///

// μ ã μ «¹ ¹ μ ÷ é« cc ÷ c c {l = l = =;;;;;;;;;;;;;

PLOGPAL-> PALVERSION = 0x300; PLOGPAL-> PALNUMENTRIES = 256;

HDC HDC = GetSafeHDC (); RGBQUAD PrGb [256]; :: getDibcolortable (HDC, 0, 256, PrGB); for (int i = 0; i <256; i ) {plogpal-> palpalent [i]. Pred = prgb [i] .rgbred; plogpal-> palpalent [i] .pegreen = prg [i] .RGBGreen; PLOGPAL-> PALPALENTRY [i] .peblue = prg [i] .RGBBLUE; PLOGPAL-> PALPALENTRY [i] .peflags = 0;} cpalette * ppal = null; ppal = new cpalette; ppal-> createpalette (PLOGPAL);

DELETE PLOGPAL; RETURN PPAL;

/// / / getBitmapinfo (void) //// íê3¹¹|Äü: // μã½½½ »í4ðå ¢ //// øäë²2îêý: // þþþ /////////////////// μ» ø2îêý: // î »í4ðå ¢ ¸ ¸ ¸ Õõ, óãíê|êíí å ////

// μÃμ½Î »Í¼ÐÅÏ ¢ LPBITMAPINFO CDIB :: GetBitmapInfo (void) {DIBSECTION DibSection; GetDibSection (& DibSection); int nSize = DibSection.dsBmih.biClrUsed * sizeof (RGBQUAD) sizeof (BITMAPINFOHEADER); BYTE * pByte = new BYTE [nSize ]; Memcpy (Pbyte, & (Dibsection.dsbmih), Sizeof (BitMapInfoHead); CDC * PDC = getdc () ;: getDibcolortable (PDC-> getsafehdc (), 0, dibsection.dsbmih.birrused, (rgbquad *) (Pbyte Sizeof (BitmapInfoHead)); ReleaseDC (PDC); BitmapInfo * PBMI = (BitmapInfo *) PBYTE; RETURN (PBMI);} ///// SaveBMP (LPCSTR FileName) // // íê3é ¹ ¹|Äü: // ± £'æî »í4îä1///// êäë2îþûý: // îäþ/// · μ» Ø2îêý: // êç · ñ3¹¹| ////////

// ± £'æîîä1/// ±int cdib :: SaveBMP (lpcstr filename) {BitmapfileHeader HDR;

// ' ªªä //4 þ cfile file (filename, cfile :: modewrite | cfile :: motecreate);

// îä1/1 = (Word) 'm' << 8) 'b'; lpbitmapinfo pbi = getBitmapInfo (); PbitmapInfoHeader PBIH = (PbitmapInfoHeader) PBI;

Hdr.bfsize = (dword) (Sizeof (BitmapfileHeader) PBIH-> Bisize PBIH-> BICLRUSED * SIZEOF (RGBQUAD) PBIH-> BisizeImage);

Hdr.bfreserved1 = 0; hdr.bfreserved2 = 0;

// compute the offset to the array of color indices = (dword) sizeof (bitmapfileHead) PBIH-> Bisize PBIH-> biclrused * sizeof (rgbquad); // ð'èëîîä1///// ð'èîîîþ1//// ð'èîîäþþþí · File.write ((((((((((((((((((((((((((((((((((((( LPVOID) & HDR, SIZEOF (BitmapfileHeader);

// ð'èëðåï ¢ í · File.write ((LPVOID) PBIH, SIZEOF (BitmapInfoHeader) PBIH-> BICLRUSED * SIZEOF (RGBQUAD));

. // Copy the array of color indices into the .BMP file // Ð'ÈëÊý¾Ý int nTotal = pbih-> biSizeImage; LPVOID lpBits = GetBits (); file.WriteHuge (lpBits, nTotal); // ¹Ø ± ÕÎļþ file. CLOSE (); delete PBI;

Return 1;}

/// // Capturedib (CWND * PWND, Const CRECT & CAPRECT) //// íê3¹¹|Äü: // 2¶ × 1//// 2¶ × 1//// 2 Úí4ïó //// êäë2îêý: // '° Õö õõ PWND / / / 2 × 1/2 ä'óð¡¡ CapRect //// · μ »Ø2îêý: // êç · ñ3¹¹| ////

// 2¶ × 1/// 2 '° ¿ Úí/1/4ïóbool cdib :: Capturedib (CWND * PWND, Const CRECT & CAPRECT) {BOOL RET = FALSE

IF (PWND == NULL) RETURN FALSE;

CDC * PPLAYDC = PWND-> getDC ();

IF (PPLAYDC == Null) Return False;

CRECT RECT; if (CapRect.isRectempty ()) PWND-> getClientRect (Rect); else Rect = CapRect;

// μ ã μ½ «« ê u uint nbitcount = pplaydc-> getDevicecaps (bitspixel);

// '' ¨î »í //1/1/1///// ' PPLAYDC); RETURN FALSE;

PWND-> ShowWindow (sw_show); // 2¶ × 1/2 IF (pcopydc-> bitblt (0, 0, Rect.width (), Rect.Height (), PPLAYDC, 0, 0, SRCCOPY) RET = True; ReleaseDC (PCOPYDC);

PWND-> ReleaseDC (PPLAYDC); Return Ret;}

///// CDIBDC //// éè ± ¸þ¹þ¹þ¹þ¹ »í4éè ±àà (° æ ± 3¹¹) //// íêé¹ ¹ ¹þ¹þ¹þ¹þ¹þ¹þ¹» í4μÄï¹¹¹ªª /// ï ± 3/4ëùóð: þþþ, ôøö//////

Implement_dynamic (CDIBDC, CDC);

Cdibdc :: cdibdc () {m_hold = NULL;}

CDIBDC :: ~ cdibdc () {if (m_hold! = Null) {:: selectObject (getsafehdc (), m_hold);}}}

转载请注明原文地址:https://www.9cbs.com/read-121777.html

New Post(0)