00001
00002
00003
00004
00005
00006
00007
00008 #include "StdAfx.h"
00009 #include "BitmapUtil.h"
00010
00011 #include "DecoratorUtil.h"
00012 #include "DecoratorLibResource.h"
00013 #include "DecoratorDefs.h"
00014 #include <io.h>
00015
00016 namespace DecoratorSDK
00017 {
00018
00019
00020
00021
00022
00023
00024
00025 RefCoord::RefCoord()
00026 : m_eType( CRP_BEGIN ), m_lPos( 0 )
00027 {
00028 }
00029
00030 RefCoord::RefCoord( ECoordRefPoint eType, long lPos )
00031 : m_eType( eType ), m_lPos( lPos )
00032 {
00033 }
00034
00035 RefCoord::RefCoord( const RefCoord& rc )
00036 : m_eType( rc.m_eType ), m_lPos( rc.m_lPos )
00037 {
00038 }
00039
00040 RefCoord& RefCoord::operator=( const RefCoord& rc )
00041 {
00042 if ( this != &rc ) {
00043 m_eType = rc.m_eType;
00044 m_lPos = rc.m_lPos;
00045 }
00046 return *this;
00047 }
00048
00049 long RefCoord::getPos() const
00050 {
00051 return m_lPos;
00052 }
00053
00054 ECoordRefPoint RefCoord::getRef() const
00055 {
00056 return m_eType;
00057 }
00058
00059 long RefCoord::calcPos( long lLength )
00060 {
00061 long lResult = 0;
00062 switch ( m_eType ) {
00063 case CRP_BEGIN :
00064 lResult = m_lPos;
00065 break;
00066 case CRP_END :
00067 lResult = lLength - m_lPos;
00068 break;
00069 case CRP_CENTER :
00070 lResult = lLength / 2 + m_lPos;
00071 break;
00072 }
00073 lResult = ( lResult < 0 ) ? 0 : lResult;
00074 return ( lResult > lLength ) ? lLength : lResult;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084 RefPoint::RefPoint()
00085 {
00086 }
00087
00088 RefPoint::RefPoint( const RefCoord& rcX, const RefCoord& rcY )
00089 : m_rcX( rcX ), m_rcY( rcY )
00090 {
00091 }
00092
00093 RefPoint::RefPoint( const RefPoint& rp )
00094 : m_rcX( rp.m_rcX ), m_rcY( rp.m_rcY )
00095 {
00096 }
00097
00098 RefPoint& RefPoint::operator=( const RefPoint rp )
00099 {
00100 if ( this != &rp ) {
00101 m_rcX = rp.m_rcX;
00102 m_rcY = rp.m_rcY;
00103 }
00104 return *this;
00105 }
00106
00107 RefCoord RefPoint::getX() const
00108 {
00109 return m_rcX;
00110 }
00111
00112 RefCoord RefPoint::getY() const
00113 {
00114 return m_rcY;
00115 }
00116
00117 CPoint RefPoint::calcPoint( long lWidth, long lHeight )
00118 {
00119 return CPoint( m_rcX.calcPos( lWidth ), m_rcY.calcPos( lHeight ) );
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 BitmapBase::BitmapBase( const CString& strName )
00129 : m_lWidth( 0 ), m_lHeight( 0 ), m_bHasTransparentColor( false ),
00130 m_crTransparentColor( 0x0 ), m_bHasBackgroundColor( false ), m_crBackgroundColor( 0x0 )
00131 {
00132 setName( strName );
00133 }
00134
00135 BitmapBase::BitmapBase( const CString& strName, COLORREF crColor, bool bIsTransparent )
00136 : m_lWidth( 0 ), m_lHeight( 0 ), m_bHasTransparentColor( bIsTransparent ),
00137 m_crTransparentColor( ( bIsTransparent ) ? crColor : 0x0 ), m_bHasBackgroundColor( ! bIsTransparent), m_crBackgroundColor( ( ! bIsTransparent ) ? crColor : 0x0 )
00138 {
00139 setName( strName );
00140 }
00141
00142 BitmapBase::BitmapBase( const CString& strName, COLORREF crTransparentColor, COLORREF crBackgroundColor )
00143 : m_lWidth( 0 ), m_lHeight( 0 ), m_bHasTransparentColor( true ),
00144 m_crTransparentColor( crTransparentColor ), m_bHasBackgroundColor( true ), m_crBackgroundColor( crBackgroundColor )
00145 {
00146 setName( strName );
00147 }
00148
00149 BitmapBase::BitmapBase( const CString& strName, COLORREF crTransparentColor, COLORREF crGrayedColor, bool isMasked )
00150 : m_lWidth( 0 ), m_lHeight( 0 ), m_bHasTransparentColor( true ),
00151 m_crTransparentColor( crTransparentColor ), m_bHasBackgroundColor( false ), m_bHasGrayedColor( true ),
00152 m_crGrayedColor( crGrayedColor )
00153 {
00154 setName( strName );
00155 }
00156
00157 BitmapBase::~BitmapBase()
00158 {
00159 }
00160
00161 CString BitmapBase::getName() const
00162 {
00163 return m_strName;
00164 }
00165
00166 long BitmapBase::getWidth() const
00167 {
00168 return m_lWidth;
00169 }
00170
00171 long BitmapBase::getHeight() const
00172 {
00173 return m_lHeight;
00174 }
00175
00176 bool BitmapBase::hasTransparentColor() const
00177 {
00178 return m_bHasTransparentColor;
00179 }
00180
00181 bool BitmapBase::hasBackgroundColor() const
00182 {
00183 return m_bHasBackgroundColor;
00184 }
00185
00186 COLORREF BitmapBase::getTransparentColor() const
00187 {
00188 return m_crTransparentColor;
00189 }
00190
00191 COLORREF BitmapBase::getBackgroundColor() const
00192 {
00193 return m_crBackgroundColor;
00194 }
00195
00196 void BitmapBase::draw( Gdiplus::Graphics* gdip, CDC* pDC, const CRect& cRect, const TileVector& vecTiles,
00197 DWORD dwModifierFlags ) const
00198 {
00199 for ( unsigned long i = 0 ; i < vecTiles.size() ; i++ ) {
00200
00201 CPoint srcTopLeft = vecTiles[ i ].getPartTopLeft().calcPoint( m_lWidth, m_lHeight );
00202 CPoint srcBottomRight = vecTiles[ i ].getPartBottomRight().calcPoint( m_lWidth, m_lHeight );
00203 long lSrcWidth = srcBottomRight.x - srcTopLeft.x;
00204 long lSrcHeight = srcBottomRight.y - srcTopLeft.y;
00205
00206 CPoint dstTopLeft = vecTiles[ i ].getTopLeft().calcPoint( cRect.Width(), cRect.Height() ) + cRect.TopLeft();
00207 CPoint dstBottomRight = vecTiles[ i ].getBottomRight().calcPoint( cRect.Width(), cRect.Height() ) + cRect.TopLeft();
00208 long lDstWidth = dstBottomRight.x - dstTopLeft.x;
00209 long lDstHeight = dstBottomRight.y - dstTopLeft.y;
00210
00211 long lHNumber = 1;
00212 long lHMod = 0;
00213 if ( vecTiles[ i ].getHorizontalFill() == FT_TILE ) {
00214 lHMod = lDstWidth % lSrcWidth;
00215 lHNumber = lDstWidth / lSrcWidth + ( ( ! lHMod ) ? 0 : 1 );
00216 }
00217
00218 long lVNumber = 1;
00219 long lVMod = 0;
00220 if ( vecTiles[ i ].getVerticalFill() == FT_TILE ) {
00221 lVMod = lDstHeight % lSrcHeight;
00222 lVNumber = lDstHeight / lSrcHeight + ( ( ! lVMod ) ? 0 : 1 );
00223 }
00224
00225 for ( long h = 0 ; h < lHNumber ; h++ ) {
00226
00227 long lSrcRight = srcBottomRight.x;
00228 long lDstLeft = dstTopLeft.x;
00229 long lDstRight = 0;
00230 switch ( vecTiles[ i ].getHorizontalFill() ) {
00231 case FT_NONE :
00232 lDstRight = dstTopLeft.x + lSrcWidth;
00233 break;
00234 case FT_STRETCH :
00235 lDstRight = dstBottomRight.x;
00236 break;
00237 case FT_TILE :
00238 if ( h == lHNumber - 1 && ! lHMod )
00239 lSrcRight = srcTopLeft.x + lHMod;
00240 lDstLeft += h * lSrcWidth;
00241 lDstRight = ( h == lHNumber - 1 && ! lHMod ) ? dstBottomRight.x : lDstLeft + lSrcWidth;
00242 break;
00243 }
00244
00245 for ( long v = 0 ; v < lVNumber ; v++ ) {
00246
00247 long lSrcBottom = srcBottomRight.y;
00248 long lDstTop = dstTopLeft.y;
00249 long lDstBottom = 0;
00250 switch ( vecTiles[ i ].getVerticalFill() ) {
00251 case FT_NONE :
00252 lDstBottom = dstTopLeft.y + lSrcHeight;
00253 break;
00254 case FT_STRETCH :
00255 lDstBottom = dstBottomRight.y;
00256 break;
00257 case FT_TILE :
00258 if ( v == lVNumber - 1 && ! lVMod )
00259 lSrcBottom = srcTopLeft.y + lVMod;
00260 lDstTop += v * lSrcHeight;
00261 lDstBottom = ( v == lVNumber - 1 && ! lVMod ) ? dstBottomRight.y : lDstTop + lSrcHeight;
00262 break;
00263 }
00264
00265 draw( gdip, pDC, CRect( srcTopLeft.x, srcTopLeft.y, lSrcRight, lSrcBottom ),
00266 CRect( lDstLeft, lDstTop, lDstRight, lDstBottom ), SRCCOPY, dwModifierFlags );
00267 }
00268 }
00269 }
00270 if ( m_bHasBackgroundColor ) {
00271 if (gdip != NULL) {
00272
00273 } else {
00274 CDC dcMemory;
00275 dcMemory.CreateCompatibleDC( pDC );
00276 CBitmap bmp;
00277 bmp.CreateCompatibleBitmap( pDC, cRect.Width(), cRect.Height() );
00278 dcMemory.SelectObject( &bmp );
00279 dcMemory.FillSolidRect( CRect( 0, 0, cRect.Width(), cRect.Height() ), m_crBackgroundColor );
00280 CPen pen;
00281 pen.CreatePen( PS_SOLID, 6, m_crBackgroundColor );
00282 dcMemory.SelectObject( &pen );
00283 dcMemory.MoveTo( 0 , 0 );
00284 dcMemory.LineTo( 60, 60 );
00285 pDC->StretchBlt( cRect.left, cRect.top, cRect.Width(), cRect.Height(), &dcMemory, 0, 0, cRect.Width(), cRect.Height(), SRCAND );
00286 }
00287 }
00288 }
00289
00290 void BitmapBase::setSize( long lWidth, long lHeight )
00291 {
00292 m_lWidth = lWidth;
00293 m_lHeight = lHeight;
00294 }
00295
00296 void BitmapBase::setName( const CString& strName )
00297 {
00298 int iPos = strName.ReverseFind( '\\' );
00299 int iPos2 = strName.ReverseFind( '/' );
00300 iPos = max( iPos, iPos2 );
00301 m_strName = ( iPos == -1 ) ? strName : strName.Right( strName.GetLength() - iPos - 1 );
00302 }
00303
00304 Gdiplus::ColorMatrix BitmapBase::GetGreyFadeMatrix(COLORREF greyColor) const
00305 {
00306 float fadeFactor = 0.3f * GetRValue(greyColor) + 0.59f * GetGValue(greyColor) + 0.11f * GetBValue(greyColor);
00307 Gdiplus::ColorMatrix greyFadeColorMatrix = {
00308 0.3f / fadeFactor, 0.0f, 0.0f, 0.0f, 0.0f,
00309 0.0f, 0.59f / fadeFactor, 0.0f, 0.0f, 0.0f,
00310 0.0f, 0.0f, 0.11f / fadeFactor, 0.0f, 0.0f,
00311 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
00312 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
00313 };
00314 return greyFadeColorMatrix;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324 BitmapDIB::BitmapDIB( const CString& strName )
00325 : BitmapBase( strName ), m_hDIB( NULL )
00326 {
00327 load( strName );
00328 }
00329
00330 BitmapDIB::BitmapDIB( const CString& strName, COLORREF crColor, bool bIsTransparent )
00331 : BitmapBase( strName , crColor, bIsTransparent ), m_hDIB( NULL )
00332 {
00333 load( strName );
00334 }
00335
00336 BitmapDIB::BitmapDIB( const CString& strName, COLORREF crTransparentColor, COLORREF crBackgroundColor )
00337 : BitmapBase( strName, crTransparentColor, crBackgroundColor ), m_hDIB( NULL )
00338 {
00339 load( strName );
00340 }
00341
00342 BitmapDIB::~BitmapDIB()
00343 {
00344 if ( isInitialized() ) {
00345 ::GlobalUnlock( (HGLOBAL) m_hDIB );
00346 ::GlobalFree( (HGLOBAL) m_hDIB );
00347 }
00348 }
00349
00350 void BitmapDIB::draw( Gdiplus::Graphics* gdip, CDC* pDC, const CRect& srcRect, const CRect& dstRect,
00351 DWORD dwOpCode, DWORD dwModifierFlags ) const
00352 {
00353 if ( isInitialized() ) {
00354 CRect srcRectCopy = srcRect;
00355 CRect dstRectCopy = dstRect;
00356 if (gdip != NULL) {
00357 LPSTR lpDIBHdr = (LPSTR)GlobalLock(m_hDIB);
00358 LPSTR lpDIBBits = FindDIBBits(lpDIBHdr);
00359 Gdiplus::Bitmap bitmap((LPBITMAPINFO)lpDIBHdr, lpDIBBits);
00360
00361 bool bGray = (dwModifierFlags & MF_GREYED) != 0 && m_bHasGrayedColor;
00362 Gdiplus::ImageAttributes imgAttribs;
00363 if ((dwModifierFlags & MF_TRANSPARENT) != 0 && m_bHasTransparentColor) {
00364 Gdiplus::Color transparentColor(GetRValue(m_crTransparentColor), GetGValue(m_crTransparentColor), GetBValue(m_crTransparentColor));
00365 imgAttribs.SetColorKey(transparentColor, transparentColor);
00366 }
00367 if (bGray) {
00368 Gdiplus::ColorMatrix greyFadeColorMatrix = GetGreyFadeMatrix(m_crGrayedColor);
00369 imgAttribs.SetColorMatrix(&greyFadeColorMatrix);
00370 }
00371 Gdiplus::Rect destRect(dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
00372 gdip->DrawImage(&bitmap, destRect, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(),
00373 Gdiplus::UnitPixel, &imgAttribs);
00374 GlobalUnlock(m_hDIB);
00375 } else {
00376 ::PaintDIB( pDC->m_hDC, &dstRectCopy, m_hDIB, &srcRectCopy, NULL );
00377 }
00378 }
00379 }
00380
00381 HBITMAP BitmapDIB::DIBToBitmap(HDIB hDIB, HPALETTE hPal)
00382 {
00383 LPSTR lpDIBHdr, lpDIBBits;
00384 HBITMAP hBitmap;
00385 HDC hDC;
00386 HPALETTE hOldPal = NULL;
00387
00388 if (!hDIB)
00389 return NULL;
00390
00391 lpDIBHdr = (LPSTR)GlobalLock(hDIB);
00392
00393 lpDIBBits = FindDIBBits(lpDIBHdr);
00394
00395 hDC = GetDC(NULL);
00396 if (!hDC)
00397 {
00398 GlobalUnlock(hDIB);
00399 return NULL;
00400 }
00401
00402 if (hPal)
00403 hOldPal = SelectPalette(hDC, hPal, FALSE);
00404 RealizePalette(hDC);
00405
00406 hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpDIBHdr, CBM_INIT,
00407 lpDIBBits, (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS);
00408
00409 if (hOldPal)
00410 SelectPalette(hDC, hOldPal, FALSE);
00411
00412 ReleaseDC(NULL, hDC);
00413 GlobalUnlock(hDIB);
00414
00415 return hBitmap;
00416 }
00417
00418 void BitmapDIB::load( const CString& strName )
00419 {
00420 if ( ! m_hDIB ) {
00421 if ( ! getFacilities().arePathesValid() )
00422 return;
00423 std::vector<CString> vecPathes = getFacilities().getPathes();
00424
00425 CString strFName = strName;
00426
00427 CFile fDIB;
00428 CFileException fexDIB;
00429 for ( unsigned int i = 0 ; i < vecPathes.size() ; i++ ) {
00430 if ( fDIB.Open( vecPathes[ i ] + strFName, CFile::modeRead | CFile::shareDenyWrite, &fexDIB ) ) {
00431 TRY {
00432 m_hDIB = ::ReadDIBFile( fDIB );
00433 fDIB.Close();
00434 if ( m_hDIB ) {
00435 LPSTR lpDIB = (LPSTR) ::GlobalLock( (HGLOBAL) m_hDIB );
00436 setSize( ::DIBWidth( lpDIB ), ::DIBHeight( lpDIB) );
00437 ::GlobalUnlock( (HGLOBAL) m_hDIB );
00438 return;
00439 }
00440 }
00441 CATCH (CFileException, eLoad) {
00442 fDIB.Abort();
00443 m_hDIB = NULL;
00444 AfxMessageBox( "Error Reading Bitmap File: " + vecPathes[ i ] + strFName, MB_OK | MB_ICONSTOP );
00445 return;
00446 } END_CATCH
00447 }
00448 }
00449 }
00450 }
00451
00452 bool BitmapDIB::isInitialized() const
00453 {
00454 return m_hDIB != NULL;
00455 }
00456
00457
00458
00459
00460
00461
00462
00463 BitmapMasked::BitmapMasked( const CString& strName, COLORREF crTransparentColor, COLORREF crGrayColor ):
00464 BitmapBase ( strName, crTransparentColor, crGrayColor, true ),
00465 m_pBits (NULL),
00466 m_pBMI (NULL),
00467 m_pPalette (NULL)
00468 {
00469 CFile bitmapFile(strName, CFile::modeRead);
00470 Read( bitmapFile );
00471
00472 setSize( getWidth(), getHeight());
00473 }
00474
00475 BitmapMasked::BitmapMasked( UINT nResID, COLORREF crTransparentColor, COLORREF crGrayColor ):
00476 BitmapBase ( "" , crTransparentColor, crGrayColor, true ),
00477 m_pBits (NULL),
00478 m_pBMI (NULL),
00479 m_pPalette (NULL)
00480 {
00481 TCHAR chBuffer[10];
00482 _ultot( nResID, chBuffer, 10 );
00483 setName(chBuffer);
00484 ReadFromResource( nResID );
00485
00486 setSize( getWidth(), getHeight());
00487 }
00488
00489 BitmapMasked::~BitmapMasked()
00490 {
00491 Free();
00492 }
00493
00494 bool BitmapMasked::isInitialized() const
00495 {
00496 return m_pBMI != NULL;
00497 }
00498
00499 void BitmapMasked::draw( Gdiplus::Graphics* gdip, CDC* pDC, const CRect& srcRect, const CRect& dstRect,
00500 DWORD dwOpCode, DWORD dwModifierFlags ) const
00501 {
00502 if ( isInitialized() ) {
00503 bool bGray = (dwModifierFlags & MF_GREYED) != 0 && m_bHasGrayedColor;
00504 if ((dwModifierFlags & MF_TRANSPARENT) != 0 && m_bHasTransparentColor)
00505 drawTransparent (gdip, pDC, srcRect, dstRect, m_crTransparentColor, bGray, m_crGrayedColor);
00506 else
00507 draw (gdip, pDC, srcRect, dstRect, bGray, m_crGrayedColor);
00508 }
00509 }
00510
00511 long BitmapMasked::getWidth() const
00512 {
00513 if (!m_pBMI)
00514 return 0;
00515
00516
00517 return m_pBMI->bmiHeader.biWidth;
00518 }
00519
00520 long BitmapMasked::getHeight() const
00521 {
00522 if (!m_pBMI)
00523 return 0;
00524
00525
00526 return m_pBMI->bmiHeader.biHeight;
00527 }
00528
00529 DWORD BitmapMasked::Read(CFile& file, BOOL bFromResource)
00530 {
00531 DWORD dwReadBytes = 0;
00532 DWORD dwLength = (DWORD) file.GetLength();
00533 ASSERT ( dwLength > 0 );
00534
00535
00536 Free();
00537
00538 BITMAPFILEHEADER bmfHeader;
00539 BITMAPINFOHEADER bmiHeader;
00540
00541 if( !bFromResource )
00542 {
00543
00544 if( (dwReadBytes = file.Read((LPSTR)&bmfHeader, BMIF_SIZE)) != BMIF_SIZE)
00545 return 0;
00546 if(bmfHeader.bfType != DIB_HEADER_MARKER)
00547 return 0;
00548 }
00549
00550
00551 if( file.Read( &bmiHeader, BMIH_SIZE ) != BMIH_SIZE )
00552 return 0;
00553 dwReadBytes += BMIH_SIZE;
00554
00555 DWORD dwPalSize = NumColors( bmiHeader ) * sizeof RGBQUAD;
00556 m_pBMI = (LPBITMAPINFO) new BYTE[BMIH_SIZE + dwPalSize];
00557 if (m_pBMI == NULL)
00558 return 0;
00559 memcpy( m_pBMI, &bmiHeader, BMIH_SIZE );
00560
00561 if( file.Read( m_pBMI->bmiColors, dwPalSize ) != dwPalSize )
00562 return 0;
00563 dwReadBytes += dwPalSize;
00564 CreatePalette();
00565
00566
00567 m_pBits = new BYTE[ dwLength - dwReadBytes ];
00568 if (m_pBits == NULL)
00569 return 0;
00570
00571 if (file.Read( m_pBits, dwLength - dwReadBytes ) != (dwLength - dwReadBytes))
00572 {
00573 delete m_pBMI;
00574 m_pBMI = NULL;
00575
00576 delete m_pBits;
00577 m_pBits = NULL;
00578
00579 return 0;
00580 }
00581 dwReadBytes = dwLength;
00582
00583 return dwReadBytes;
00584 }
00585
00586 DWORD BitmapMasked::ReadFromResource(UINT nResID)
00587 {
00588 DWORD dwResult = 0;
00589 HMODULE hModule = AfxFindResourceHandle(MAKEINTRESOURCE(nResID), RT_BITMAP);
00590 ASSERT(hModule != NULL);
00591
00592 HRSRC hbmres = FindResource(hModule, MAKEINTRESOURCE(nResID), RT_BITMAP );
00593 ASSERT(hbmres != NULL);
00594 CMemFile file;
00595 HGLOBAL hGlob;
00596
00597 if (hbmres)
00598 {
00599 DWORD dwResSize = SizeofResource( hModule, hbmres );
00600 ASSERT( dwResSize > 0 );
00601 file.Attach( (LPBYTE)LockResource( hGlob = LoadResource(hModule, hbmres) ), dwResSize );
00602 dwResult = Read(file, TRUE);
00603 ASSERT( dwResult == dwResSize );
00604 file.Detach();
00605 DeleteObject( hGlob );
00606 }
00607
00608 return dwResult;
00609 }
00610
00611 WORD BitmapMasked::NumColors( BITMAPINFOHEADER& bmiHeader ) const
00612 {
00613 if ( bmiHeader.biClrUsed != 0)
00614 return (WORD)bmiHeader.biClrUsed;
00615
00616 switch ( bmiHeader.biBitCount )
00617 {
00618 case 1:
00619 return 2;
00620 case 4:
00621 return 16;
00622 case 8:
00623 return 256;
00624 default:
00625 ASSERT(false);
00626 case 24:
00627 case 32:
00628 return 0;
00629 }
00630 }
00631
00632 void BitmapMasked::Free()
00633 {
00634
00635 if(m_pBMI)
00636 {
00637 delete m_pBMI;
00638 m_pBMI = NULL;
00639 }
00640
00641 if(m_pBits)
00642 {
00643 delete m_pBits;
00644 m_pBits = NULL;
00645 }
00646 if(m_pPalette)
00647 {
00648 m_pPalette->DeleteObject();
00649 delete m_pPalette;
00650 m_pPalette = NULL;
00651 }
00652 }
00653
00654 BOOL BitmapMasked::CreatePalette()
00655 {
00656 if ( !isInitialized() )
00657 return FALSE;
00658
00659
00660 WORD wNumColors = NumColors( m_pBMI->bmiHeader );
00661 BOOL bResult = TRUE;
00662
00663 if (wNumColors != 0)
00664 {
00665
00666 LPLOGPALETTE pLogPal = (LPLOGPALETTE) new BYTE[ sizeof(LOGPALETTE) +
00667 sizeof(PALETTEENTRY)*wNumColors ];
00668
00669
00670 if( pLogPal == 0 )
00671 return FALSE;
00672
00673
00674 pLogPal->palVersion = PALVERSION;
00675 pLogPal->palNumEntries = wNumColors;
00676
00677 for (WORD i = 0; i < wNumColors; i++)
00678 {
00679 pLogPal->palPalEntry[i].peRed = m_pBMI->bmiColors[i].rgbRed;
00680 pLogPal->palPalEntry[i].peGreen = m_pBMI->bmiColors[i].rgbGreen;
00681 pLogPal->palPalEntry[i].peBlue = m_pBMI->bmiColors[i].rgbBlue;
00682 pLogPal->palPalEntry[i].peFlags = 0;
00683 }
00684
00685
00686 if (m_pPalette)
00687 {
00688 m_pPalette->DeleteObject();
00689 delete m_pPalette;
00690 }
00691
00692 m_pPalette = new CPalette;
00693 bResult = m_pPalette->CreatePalette( pLogPal );
00694 delete pLogPal;
00695 }
00696
00697 return bResult;
00698 }
00699
00700 void BitmapMasked::draw(Gdiplus::Graphics* gdip, CDC *pDC, const CRect& srcRect, const CRect &dstRect,
00701 bool bGray, COLORREF grayColor) const
00702 {
00703 if ( !isInitialized() )
00704 return;
00705
00706 if (gdip != NULL) {
00707 Gdiplus::Bitmap bitmap(m_pBMI, m_pBits);
00708 Gdiplus::ImageAttributes imgAttribs;
00709 if (bGray) {
00710 Gdiplus::ColorMatrix greyFadeColorMatrix = GetGreyFadeMatrix(grayColor);
00711 imgAttribs.SetColorMatrix(&greyFadeColorMatrix);
00712 }
00713 Gdiplus::Rect destRect(dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
00714 gdip->DrawImage(&bitmap, destRect, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(),
00715 Gdiplus::UnitPixel, &imgAttribs);
00716 } else {
00717 CPalette* pOldPal = NULL;
00718
00719
00720 if (m_pPalette != NULL)
00721 {
00722
00723
00724 pOldPal = pDC->SelectPalette( m_pPalette, TRUE);
00725 }
00726
00727
00728 pDC->SetStretchBltMode( COLORONCOLOR );
00729
00730
00731 ::StretchDIBits(pDC->m_hDC,
00732 dstRect.left,
00733 dstRect.top,
00734 dstRect.Width(),
00735 dstRect.Height(),
00736 0,
00737 0,
00738 getWidth(),
00739 getHeight(),
00740 m_pBits,
00741 m_pBMI,
00742 DIB_RGB_COLORS,
00743 SRCCOPY);
00744
00745
00746 if (pOldPal != NULL)
00747 {
00748 pDC->SelectPalette( pOldPal, TRUE);
00749 }
00750 }
00751 }
00752
00753 void BitmapMasked::drawTransparent(Gdiplus::Graphics* gdip, CDC *pDC, const CRect& srcRect, const CRect &dstRect,
00754 COLORREF clrTransparency, bool bGray, COLORREF grayColor) const
00755 {
00756 if ( !isInitialized() )
00757 return;
00758
00759 if (gdip != NULL) {
00760 Gdiplus::Bitmap bitmap(m_pBMI, m_pBits);
00761 Gdiplus::Rect destRect(dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
00762 Gdiplus::ImageAttributes imgAttribs;
00763 Gdiplus::Color transparentColor(GetRValue(clrTransparency), GetGValue(clrTransparency), GetBValue(clrTransparency));
00764 imgAttribs.SetColorKey(transparentColor, transparentColor);
00765 if (bGray) {
00766 Gdiplus::ColorMatrix greyFadeColorMatrix = GetGreyFadeMatrix(grayColor);
00767 imgAttribs.SetColorMatrix(&greyFadeColorMatrix);
00768 }
00769 gdip->DrawImage(&bitmap, destRect, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(),
00770 Gdiplus::UnitPixel, &imgAttribs);
00771 } else {
00772
00773
00774
00775 CDC dcImage;
00776 dcImage.CreateCompatibleDC (pDC);
00777 CBitmap bmImage;
00778 bmImage.CreateCompatibleBitmap(pDC, dstRect.Width(), dstRect.Height());
00779
00780 CBitmap* pOldBitmapImage = dcImage.SelectObject(&bmImage);
00781
00782
00783 CPalette* pOldPal = NULL;
00784
00785 if (m_pPalette != NULL)
00786 {
00787
00788
00789 pOldPal = dcImage.SelectPalette( m_pPalette, TRUE);
00790 }
00791
00792
00793 dcImage.SetStretchBltMode( COLORONCOLOR );
00794
00795
00796 ::StretchDIBits(dcImage.m_hDC,
00797 0,
00798 0,
00799 dstRect.Width(),
00800 dstRect.Height(),
00801 0,
00802 0,
00803 getWidth(),
00804 getHeight(),
00805 m_pBits,
00806 m_pBMI,
00807 DIB_RGB_COLORS,
00808 SRCCOPY);
00809
00810
00811
00812 if (pOldPal != NULL)
00813 {
00814 dcImage.SelectPalette( pOldPal, TRUE);
00815 }
00816
00817
00818
00819
00820 CDC dcAnd;
00821 dcAnd.CreateCompatibleDC (pDC);
00822
00823 CBitmap bitmapAnd;
00824 bitmapAnd.CreateBitmap (dstRect.Width(), dstRect.Height(), 1, 1, NULL);
00825 CBitmap* pOldBitmapAnd = dcAnd.SelectObject (&bitmapAnd);
00826
00827 dcImage.SetBkColor (clrTransparency);
00828 dcAnd.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcImage, 0, 0,
00829 SRCCOPY);
00830
00831
00832
00833
00834 CDC dcXor;
00835 dcXor.CreateCompatibleDC (pDC);
00836
00837 CBitmap bitmapXor;
00838 bitmapXor.CreateCompatibleBitmap (&dcImage, dstRect.Width(), dstRect.Height());
00839 CBitmap* pOldBitmapXor = dcXor.SelectObject (&bitmapXor);
00840
00841 dcXor.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcImage, 0, 0,
00842 SRCCOPY);
00843
00844 dcXor.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcAnd, 0, 0,
00845 0x220326);
00846
00847
00848
00849
00850
00851 CDC dcTemp;
00852 dcTemp.CreateCompatibleDC (pDC);
00853
00854 CBitmap bitmapTemp;
00855 bitmapTemp.CreateCompatibleBitmap (&dcImage, dstRect.Width(), dstRect.Height());
00856 CBitmap* pOldBitmapTemp = dcTemp.SelectObject (&bitmapTemp);
00857
00858 dcTemp.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), pDC, dstRect.left, dstRect.top, SRCCOPY);
00859
00860
00861
00862
00863
00864 if (bGray) {
00865 dcTemp.SetTextColor(grayColor);
00866 dcTemp.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcAnd, 0, 0, SRCAND);
00867 }
00868 else {
00869 dcTemp.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcAnd, 0, 0, SRCAND);
00870
00871 dcTemp.BitBlt (0, 0, dstRect.Width(), dstRect.Height(), &dcXor, 0, 0, SRCINVERT);
00872 }
00873
00874
00875
00876
00877 pDC->BitBlt (dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height(), &dcTemp, 0, 0, SRCCOPY);
00878
00879
00880
00881
00882 dcTemp.SelectObject (pOldBitmapTemp);
00883 dcXor.SelectObject (pOldBitmapXor);
00884 dcAnd.SelectObject (pOldBitmapAnd);
00885 dcImage.SelectObject (pOldBitmapImage);
00886 }
00887 }
00888
00889
00890
00891
00892
00893
00894
00895
00896 BitmapGen::BitmapGen( const CString& strName )
00897 : BitmapBase( strName ), m_pImage( NULL )
00898 {
00899 load( strName );
00900 }
00901
00902 BitmapGen::BitmapGen( const CString& strName, COLORREF crColor, bool bIsTransparent )
00903 : BitmapBase( strName, crColor, bIsTransparent ), m_pImage( NULL )
00904 {
00905 load( strName );
00906 }
00907
00908 BitmapGen::BitmapGen( const CString& strName, COLORREF crTransparentColor, COLORREF crBackgroundColor )
00909 : BitmapBase( strName, crTransparentColor, crBackgroundColor ), m_pImage( NULL )
00910 {
00911 load( strName );
00912 }
00913
00914 BitmapGen::~BitmapGen()
00915 {
00916 if( m_pImage)
00917 {
00918 delete m_pImage;
00919 m_pImage = NULL;
00920 }
00921 }
00922
00923 void BitmapGen::draw( Gdiplus::Graphics* gdip, CDC* pDC, const CRect& srcRect, const CRect& dstRect,
00924 DWORD dwOpCode, DWORD dwModifierFlags ) const
00925 {
00926 ASSERT(m_pImage);
00927 Gdiplus::Graphics* gdip2 = gdip;
00928 if (gdip == NULL)
00929 gdip2 = getFacilities().getGraphics();
00930 Gdiplus::Status st = Gdiplus::Win32Error;
00931 int srcWidth = srcRect.Width();
00932 int srcHeight = srcRect.Height();
00933 if (m_pImage->GetType() == Gdiplus::ImageTypeMetafile) {
00934 srcWidth = m_pImage->GetWidth();
00935 srcHeight = m_pImage->GetHeight();
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 bool bGray = (dwModifierFlags & MF_GREYED) != 0 && m_bHasGrayedColor;
00974 Gdiplus::ImageAttributes imgAttribs;
00975 if ((dwModifierFlags & MF_TRANSPARENT) != 0 && m_bHasTransparentColor) {
00976 Gdiplus::Color transparentColor(GetRValue(m_crTransparentColor), GetGValue(m_crTransparentColor), GetBValue(m_crTransparentColor));
00977 imgAttribs.SetColorKey(transparentColor, transparentColor);
00978 }
00979 if (bGray) {
00980 Gdiplus::ColorMatrix greyFadeColorMatrix = GetGreyFadeMatrix(m_crGrayedColor);
00981 imgAttribs.SetColorMatrix(&greyFadeColorMatrix);
00982 }
00983 Gdiplus::Rect destRect(dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
00984 st = gdip2->DrawImage(m_pImage, destRect, srcRect.left, srcRect.top, srcWidth, srcHeight,
00985 Gdiplus::UnitPixel, &imgAttribs);
00986 if (st == Gdiplus::Win32Error)
00987 gdip2->FillRectangle(&Gdiplus::SolidBrush(Gdiplus::Color::Red), dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
00988 }
00989
00990 void BitmapGen::load( const CString& strName )
00991 {
00992 if ( ! m_pImage ) {
00993 if ( ! getFacilities().arePathesValid() )
00994 return;
00995 std::vector<CString> vecPathes = getFacilities().getPathes();
00996
00997 bool success = false;
00998 CString strFName = strName;
00999
01000 for ( unsigned int i = 0 ; !success && i < vecPathes.size() ; i++ )
01001 {
01002 CString imageFileName = vecPathes[ i ] + strFName;
01003 bool fileExists = (_taccess(imageFileName, 0) == 0);
01004 if (fileExists) {
01005 m_pImage = Gdiplus::Image::FromFile( CStringW( (LPCTSTR) imageFileName));
01006 if( m_pImage && m_pImage->GetLastStatus() == Gdiplus::Ok)
01007 {
01008 UINT widthToSet = m_pImage->GetWidth();
01009 UINT heightToSet = m_pImage->GetHeight();
01010 ASSERT( widthToSet > 0);
01011 ASSERT( heightToSet > 0);
01012 if (m_pImage->GetType() == Gdiplus::ImageTypeMetafile) {
01013 int primaryScreenWidth = GetSystemMetrics(SM_CXSCREEN);
01014 int primaryScreenHeight = GetSystemMetrics(SM_CYSCREEN);
01015 if (widthToSet > 0.9 * primaryScreenWidth ||
01016 heightToSet > 0.9 * primaryScreenHeight)
01017 {
01018
01019
01020 widthToSet = WIDTH_MODEL;
01021 heightToSet = HEIGHT_MODEL;
01022 }
01023 #ifdef _DEBUG
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058 #endif
01059 }
01060 setSize( widthToSet, heightToSet );
01061 success = true;
01062 }
01063 }
01064 }
01065
01066 if( !success && m_pImage)
01067 {
01068 delete m_pImage;
01069 m_pImage = NULL;
01070 }
01071 }
01072 }
01073
01074 bool BitmapGen::isInitialized() const
01075 {
01076 return m_pImage != NULL;
01077 }
01078
01079
01080
01081
01082
01083
01084
01085 BitmapRES::BitmapRES( UINT uiID )
01086 : BitmapBase( createResString( uiID ) ), m_pBitmap( NULL )
01087 {
01088 load( uiID );
01089 }
01090
01091 BitmapRES::BitmapRES( UINT uiID, COLORREF crColor, bool bIsTransparent )
01092 : BitmapBase( createResString( uiID ), crColor, bIsTransparent ), m_pBitmap( NULL )
01093 {
01094 load( uiID );
01095 }
01096
01097 BitmapRES::BitmapRES( UINT uiID, COLORREF crTransparentColor, COLORREF crBackgroundColor )
01098 : BitmapBase( createResString( uiID ), crTransparentColor, crBackgroundColor ), m_pBitmap( NULL )
01099 {
01100 load( uiID );
01101 }
01102
01103 BitmapRES::~BitmapRES()
01104 {
01105 delete m_pBitmap;
01106 m_pBitmap = NULL;
01107 }
01108
01109 void BitmapRES::draw( Gdiplus::Graphics* gdip, CDC* pDC, const CRect& srcRect, const CRect& dstRect,
01110 DWORD dwOpCode, DWORD dwModifierFlags ) const
01111 {
01112 Gdiplus::Graphics* gdip2 = gdip;
01113 if (gdip == NULL)
01114 gdip2 = getFacilities().getGraphics();
01115 bool bGray = (dwModifierFlags & MF_GREYED) != 0 && m_bHasGrayedColor;
01116 Gdiplus::ImageAttributes imgAttribs;
01117 if ((dwModifierFlags & MF_TRANSPARENT) != 0 && m_bHasTransparentColor) {
01118 Gdiplus::Color transparentColor(GetRValue(m_crTransparentColor), GetGValue(m_crTransparentColor), GetBValue(m_crTransparentColor));
01119 imgAttribs.SetColorKey(transparentColor, transparentColor);
01120 }
01121 if (bGray) {
01122 Gdiplus::ColorMatrix greyFadeColorMatrix = GetGreyFadeMatrix(m_crGrayedColor);
01123 imgAttribs.SetColorMatrix(&greyFadeColorMatrix);
01124 }
01125 Gdiplus::Rect destRect(dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height());
01126 gdip2->DrawImage(m_pBitmap, destRect, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(),
01127 Gdiplus::UnitPixel, &imgAttribs);
01128 }
01129
01130
01131 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
01132 #define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
01133
01134 void BitmapRES::load( UINT uiID )
01135 {
01136
01137 m_pBitmap = new Gdiplus::Bitmap(HINST_THISCOMPONENT, (WCHAR*) MAKEINTRESOURCE( uiID));
01138 if (m_pBitmap->GetLastStatus() != Gdiplus::Ok) {
01139 ASSERT(false);
01140 }
01141
01142 setSize(m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
01143 }
01144
01145 bool BitmapRES::isInitialized() const
01146 {
01147 return m_pBitmap != NULL;
01148 }
01149
01150
01151
01152
01153
01154
01155
01156 BackgroundTile::BackgroundTile()
01157 : m_rpPartTopLeft( RefPoint() ), m_rpPartBottomRight( RefPoint( RefCoord( CRP_END, 0 ), RefCoord( CRP_END, 0 ) ) ),
01158 m_rpTopLeft( RefPoint() ), m_rpBottomRight( RefPoint( RefCoord( CRP_END, 0 ), RefCoord( CRP_END, 0 ) ) ),
01159 m_eFillVertical( FT_NONE ), m_eFillHorizontal( FT_NONE )
01160 {
01161 }
01162
01163 BackgroundTile::BackgroundTile( const RefPoint& rpPTL, const RefPoint& rpPBR, const RefPoint& rpTL, const RefPoint& rpBR, EFillType eFH, EFillType eFV )
01164 : m_rpPartTopLeft( rpPTL ), m_rpPartBottomRight( rpPBR ), m_rpTopLeft( rpTL ), m_rpBottomRight( rpBR ), m_eFillVertical( eFV ), m_eFillHorizontal( eFH )
01165 {
01166 }
01167
01168 BackgroundTile::BackgroundTile( const BackgroundTile& bt )
01169 : m_rpPartTopLeft( bt.m_rpPartTopLeft ), m_rpPartBottomRight( bt.m_rpPartBottomRight ), m_rpTopLeft( bt.m_rpTopLeft ),
01170 m_rpBottomRight( bt.m_rpBottomRight ), m_eFillVertical( bt.m_eFillVertical ), m_eFillHorizontal( bt.m_eFillHorizontal )
01171 {
01172 }
01173
01174 BackgroundTile& BackgroundTile::operator=( const BackgroundTile& bt )
01175 {
01176 if ( this != &bt ) {
01177 m_rpPartTopLeft = bt.m_rpPartTopLeft;
01178 m_rpPartBottomRight = bt.m_rpPartBottomRight;
01179 m_rpTopLeft = bt.m_rpTopLeft;
01180 m_rpBottomRight = bt.m_rpBottomRight;
01181 m_eFillVertical = bt.m_eFillVertical;
01182 m_eFillHorizontal = bt.m_eFillHorizontal;
01183 }
01184 return *this;
01185 }
01186
01187 RefPoint BackgroundTile::getPartTopLeft() const
01188 {
01189 return m_rpPartTopLeft;
01190 }
01191
01192 RefPoint BackgroundTile::getPartBottomRight() const
01193 {
01194 return m_rpPartBottomRight;
01195 }
01196
01197 RefPoint BackgroundTile::getTopLeft() const
01198 {
01199 return m_rpTopLeft;
01200 }
01201
01202 RefPoint BackgroundTile::getBottomRight() const
01203 {
01204 return m_rpBottomRight;
01205 }
01206
01207 EFillType BackgroundTile::getHorizontalFill() const
01208 {
01209 return m_eFillHorizontal;
01210 }
01211
01212 EFillType BackgroundTile::getVerticalFill() const
01213 {
01214 return m_eFillVertical;
01215 }
01216
01217 };