OSDN Git Service

気になるところを少し整理。
[gvonavish/GVONavish.git] / GVONavish / GVONavish / GVOImage.cpp
index 066a48f..8230e51 100644 (file)
@@ -39,7 +39,7 @@ namespace {
 
 bool GVOImage::stretchCopy( const GVOImage& src, uint32_t width, uint32_t height )
 {
-       if ( !createDIBImage( width, height ) ) {
+       if ( !createImage( width, height ) ) {
                return false;
        }
 
@@ -66,62 +66,60 @@ bool GVOImage::stretchCopy( const GVOImage& src, uint32_t width, uint32_t height
                ::BitBlt( hdcDst, 0, 0, m_size.cx, m_size.cy,
                        hdcSrc, 0, 0, SRCCOPY );
        }
+       m_pixelFormat = src.m_pixelFormat;
+
 
        ::RestoreDC( hdcSrc, -1 );
        ::DeleteDC( hdcSrc );
        ::RestoreDC( hdcDst, -1 );
        ::DeleteDC( hdcDst );
        ::ReleaseDC( NULL, hdc );
-       m_fileName = L"";
        return true;
 }
 
 
-bool GVOImage::createRGBAImage( int width, int height )
+bool GVOImage::createImage( int width, int height, GVOPixelFormat pixelFormat )
 {
        reset();
 
-       BITMAPV5HEADER bmih = { sizeof(bmih) };
-       bmih.bV5Compression = BI_BITFIELDS;
-       bmih.bV5BlueMask = 0xFF;
-       bmih.bV5GreenMask = 0xFF << 8;
-       bmih.bV5RedMask = 0xFF << 16;
-       bmih.bV5AlphaMask = 0xFF << 24;
-       bmih.bV5Width = width;
-       bmih.bV5Height = -height;
-       bmih.bV5Planes = 1;
-       bmih.bV5BitCount = 32;
-       bmih.bV5SizeImage = width * height * 4;
-       m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
-       if ( !m_hbmp ) {
+       uint32_t bitCount = 0;
+
+       if ( pixelFormat == k_GVOPixelFormat_RGB ) {
+               BITMAPINFOHEADER bmih = { sizeof(bmih) };
+               bmih.biWidth = width;
+               bmih.biHeight = -height;
+               bmih.biPlanes = 1;
+               bmih.biBitCount = 24;
+               bmih.biSizeImage = width * height * 3;
+               m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
+               bitCount = bmih.biBitCount;
+       }
+       else if ( pixelFormat == k_GVOPixelFormat_RGBA ) {
+               BITMAPV5HEADER bmih = { sizeof(bmih) };
+               bmih.bV5Compression = BI_BITFIELDS;
+               bmih.bV5BlueMask = 0xFF;
+               bmih.bV5GreenMask = 0xFF << 8;
+               bmih.bV5RedMask = 0xFF << 16;
+               bmih.bV5AlphaMask = 0xFF << 24;
+               bmih.bV5Width = width;
+               bmih.bV5Height = -height;
+               bmih.bV5Planes = 1;
+               bmih.bV5BitCount = 32;
+               bmih.bV5SizeImage = width * height * 4;
+               m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
+               bitCount = bmih.bV5BitCount;
+       }
+       else {
+               abort();
                return false;
        }
-       m_size.cx = width;
-       m_size.cy = height;
-       m_stride = width;
-       m_fileName = L"";
-       return true;
-}
-
-
-bool GVOImage::createDIBImage( int width, int height )
-{
-       reset();
-
-       BITMAPINFOHEADER bmih = { sizeof(bmih) };
-       bmih.biWidth = width;
-       bmih.biHeight = -height;
-       bmih.biPlanes = 1;
-       bmih.biBitCount = 24;
-       bmih.biSizeImage = width * height * 3;
-       m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
        if ( !m_hbmp ) {
                return false;
        }
        m_size.cx = width;
        m_size.cy = height;
-       m_stride = s_strideFromWidthAndBitsPerPixel( width, bmih.biBitCount );
-       m_fileName = L"";
+       m_pixelFormat = pixelFormat;
+       m_stride = s_strideFromWidthAndBitsPerPixel( width, bitCount );
        return true;
 }
 
@@ -134,13 +132,28 @@ bool GVOImage::loadFromFile( const std::wstring& fileName )
        HBITMAP hbmp = NULL;
        image.reset( Gdiplus::Bitmap::FromFile( fileName.c_str() ) );
        image->GetHBITMAP( Gdiplus::Color( 0, 0, 0 ), &hbmp );
+       Gdiplus::PixelFormat srcPixelFormat = image->GetPixelFormat();
        image.reset();
        if ( !hbmp ) {
                return false;
        }
+
+       GVOPixelFormat pixelFormat;
+
+       if ( srcPixelFormat & PixelFormatGDI ){
+               pixelFormat = k_GVOPixelFormat_RGB;
+       }
+       else if ( srcPixelFormat & (PixelFormatGDI | PixelFormatAlpha) ) {
+               pixelFormat = k_GVOPixelFormat_RGBA;
+       }
+       else {
+               abort();
+               return false;
+       }
+
        BITMAP bmp = { 0 };
        ::GetObject( hbmp, sizeof(bmp), &bmp );
-       if ( !createDIBImage( bmp.bmWidth, bmp.bmHeight ) ) {
+       if ( !createImage( bmp.bmWidth, bmp.bmHeight, pixelFormat ) ) {
                ::DeleteObject( hbmp );
                return false;
        }
@@ -150,18 +163,24 @@ bool GVOImage::loadFromFile( const std::wstring& fileName )
        ::GetBitmapBits( hbmp, buffer.size(), &buffer[0] );
        ::DeleteObject( hbmp );
 
-       switch ( bmp.bmBitsPixel ) {
-       case 24:
+       if ( pixelFormat == k_GVOPixelFormat_RGB ) {
+               switch ( bmp.bmBitsPixel ) {
+               case 24:
+                       ::memcpy( m_bits, &buffer[0], m_stride );
+                       break;
+               case 32:
+                       ::s_copyImage24From32( m_bits, &buffer[0], m_size.cx, m_size.cy );
+                       break;
+               default:
+                       return false;
+               }
+       }
+       else if ( pixelFormat == k_GVOPixelFormat_RGBA ) {
                ::memcpy( m_bits, &buffer[0], m_stride );
-               break;
-       case 32:
-               ::s_copyImage24From32( m_bits, &buffer[0], m_size.cx, m_size.cy );
-               break;
-       default:
+       }
+       else {
+               abort();
                return false;
        }
-
-       m_fileName = fileName;
-
        return true;
 }