HRESULT BitmapUtil::LoadIt(LPCTSTR pszFileName) { ULONG_PTR dwToken = 0; Gdiplus::GdiplusStartupInput input; Gdiplus::GdiplusStartupOutput output; Gdiplus::Status status = Gdiplus::GdiplusStartup( &dwToken, &input, &output ); if( status != Gdiplus::Ok ) { return E_FAIL; } HRESULT hr = E_FAIL; { Gdiplus::Bitmap bmSrc( (CT2W)pszFileName ); if( bmSrc.GetLastStatus() == Gdiplus::Ok ) { hr = MyCreateFromGdiplusBitmap( bmSrc ); } } Gdiplus::GdiplusShutdown( dwToken ); return hr; }
void Convert( const char* pSrc, const char* pDst, RawImgFormat eImgFormat = Raw_RGB_Unknown ) { CInitGDIPlus initGDIPlus; initGDIPlus.Init(); { Gdiplus::Bitmap bmSrc( (CT2W)pSrc ); if( bmSrc.GetLastStatus() != Gdiplus::Ok ) { throw "Load Image File Failed!"; } m_nHeight = bmSrc.GetHeight(); m_nWidth = bmSrc.GetWidth(); Gdiplus::PixelFormat eSrcPixelFormat = bmSrc.GetPixelFormat(); if ( eSrcPixelFormat != PixelFormat24bppRGB && eSrcPixelFormat != PixelFormat32bppARGB ) { throw "Unport Image Format!"; } UINT nBPP = 32; DWORD dwFlags = 0; Gdiplus::PixelFormat eDestPixelFormat = PixelFormat32bppRGB; if( eSrcPixelFormat&PixelFormatGDI ) { nBPP = Gdiplus::GetPixelFormatSize( eSrcPixelFormat ); eDestPixelFormat = eSrcPixelFormat; } if( Gdiplus::IsAlphaPixelFormat( eSrcPixelFormat ) ) { nBPP = 32; dwFlags |= 0x01;//createAlphaChannel; eDestPixelFormat = PixelFormat32bppARGB; } m_pDst = new BYTE[nBPP/8*m_nHeight*m_nWidth]; //BOOL bSuccess = Create( bmSrc.GetWidth(), bmSrc.GetHeight(), nBPP, dwFlags ); //if( !bSuccess ) //{ // return( E_FAIL ); //} USES_ATL_SAFE_ALLOCA; Gdiplus::ColorPalette* pPalette = NULL; if( Gdiplus::IsIndexedPixelFormat( eSrcPixelFormat ) ) { //assert(false); throw "Unsuport Indexed Pixel!"; //UINT nPaletteSize = bmSrc.GetPaletteSize(); //pPalette = static_cast< Gdiplus::ColorPalette* >( _ATL_SAFE_ALLOCA(nPaletteSize, _ATL_SAFE_ALLOCA_DEF_THRESHOLD) ); //if( pPalette == NULL ) // return E_OUTOFMEMORY; //bmSrc.GetPalette( pPalette, nPaletteSize ); //RGBQUAD argbPalette[256]; //ATLASSERT( (pPalette->Count > 0) && (pPalette->Count <= 256) ); //for( UINT iColor = 0; iColor < pPalette->Count; iColor++ ) //{ // Gdiplus::ARGB color = pPalette->Entries[iColor]; // argbPalette[iColor].rgbRed = (BYTE)( (color>>RED_SHIFT) & 0xff ); // argbPalette[iColor].rgbGreen = (BYTE)( (color>>GREEN_SHIFT) & 0xff ); // argbPalette[iColor].rgbBlue = (BYTE)( (color>>BLUE_SHIFT) & 0xff ); // argbPalette[iColor].rgbReserved = 0; //} //SetColorTable( 0, pPalette->Count, argbPalette ); } if( eDestPixelFormat == eSrcPixelFormat ) { // The pixel formats are identical, so just memcpy the rows. Gdiplus::BitmapData data; Gdiplus::Rect rect( 0, 0, bmSrc.GetWidth(), bmSrc.GetHeight() ); if(bmSrc.LockBits( &rect, Gdiplus::ImageLockModeRead, eSrcPixelFormat, &data )!=Gdiplus::Ok) { throw "bmSrc.LockBits E_OUTOFMEMORY"; //return E_OUTOFMEMORY; } size_t nBytesPerRow = AtlAlignUp( nBPP*bmSrc.GetWidth(), 8 )/8; BYTE* pbDestRow = m_pDst;//static_cast< BYTE* >( GetBits() ); BYTE* pbSrcRow = static_cast< BYTE* >( data.Scan0 ); for( int y = 0; y < bmSrc.GetHeight(); y++ ) { Checked::memcpy_s(pbDestRow, nBytesPerRow, pbSrcRow, nBytesPerRow); pbDestRow += nBytesPerRow;//GetPitch(); pbSrcRow += data.Stride; } bmSrc.UnlockBits( &data ); } else { size_t nBytesPerRow = AtlAlignUp( nBPP*bmSrc.GetWidth(), 8 )/8; // Let GDI+ work its magic Gdiplus::Bitmap bmDest( bmSrc.GetWidth(), bmSrc.GetHeight(), nBytesPerRow, // GetPitch(), eDestPixelFormat, m_pDst); //static_cast< BYTE* >( GetBits() ) ); Gdiplus::Graphics gDest( &bmDest ); gDest.DrawImage( &bmSrc, 0, 0 ); } } initGDIPlus.Unit(); };