2010年1月18日月曜日

32bitDIB(5) ファイルに出力

■ファイルに出力
 そのまま出力する場合は簡単で、BITMAPFILEHEADER→BITMAPINFO→ピクセルデータと順番にファイルに書き込むだけです。
 32bitのビットマップを読み込めないアプリケーションがちょいちょいあるので使い勝手は微妙ですが。

bool saveBitmap( LPCTSTR fileName ) const
{
  //FileHeader用意
  BITMAPFILEHEADER bmpfh;
  bmpfh.bfOffBits = sizeof(bmpfh) + sizeof(m_bmi);
  bmpfh.bfSize = bmpfh.bfOffBits + getWidth()*getHeight()*sizeof(*m_pixel);
  bmpfh.bfType = ('M'<<8)+'B';
  bmpfh.bfReserved1 = bmpfh.bfReserved2 = 0;
 
  //ファイル作成
  HANDLE fh = CreateFile( fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  if ( fh == INVALID_HANDLE_VALUE )
  {
    return false;
  }
 
  DWORD dwSize;
  WriteFile( fh, &bmpfh, sizeof(BITMAPFILEHEADER), &dwSize, NULL );
  WriteFile( fh, &m_bmi, sizeof(m_bmi), &dwSize, NULL );
  WriteFile( fh, m_pixel, getWidth()*getHeight()*sizeof(*m_pixel), &dwSize, NULL );
  CloseHandle( fh );
 
  return true;
}




●サンプル

http://cid-8cd7cf5ea9fbca55.skydrive.live.com/self.aspx/Public/program/Sample5.cpp



#include <windows.h>
 
class DIB32
{
public:
  DIB32()
    : m_pixel( NULL )
  {
    ZeroMemory( &m_bmi, sizeof(m_bmi) );
  }
 
  ~DIB32()
  {
    release();
  }
 
  bool create( LONG width, LONG height )
  {
    if ( width <= 0 || height <= 0 ) return false;
    release();
 
    m_pixel = new DWORD[ width * height ];
    if ( !m_pixel ) return false;
 
    m_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    m_bmi.bmiHeader.biWidth = width;
    m_bmi.bmiHeader.biHeight = height;
    m_bmi.bmiHeader.biPlanes = 1;
    //32bit固定にする
    m_bmi.bmiHeader.biBitCount = 32;
    m_bmi.bmiHeader.biCompression = BI_RGB;
    //96dpiだと3780らしい。0の場合もあるとのこと
    m_bmi.bmiHeader.biXPelsPerMeter = 3780;
    m_bmi.bmiHeader.biYPelsPerMeter = 3780;
 
    return true;
  }
 
  bool create( LPCTSTR bitmapFile )
  {
    if ( !bitmapFile ) return false;
 
    HBITMAP hBmp = static_cast<HBITMAP>(
      LoadImage(NULL, bitmapFile, IMAGE_BITMAP,
      0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE) );
    if ( !hBmp ) return false;
 
    BITMAP bm = {0};
    GetObject( hBmp, sizeof(BITMAP), &bm );
 
    // create DC
    HDC hdc = CreateCompatibleDC( NULL );
    if ( !hdc )
    {
      DeleteDC( hdc );
      DeleteObject( hBmp );
      return false;
    }
 
    // create buf
    if ( !create( bm.bmWidth, bm.bmHeight ) )
    {
      DeleteDC( hdc );
      DeleteObject( hBmp );
      return false;
    }
 
    // copy
    GetDIBits( hdc, hBmp, 0, bm.bmHeight,m_pixel, &m_bmi,
      DIB_RGB_COLORS );
 
    DeleteDC( hdc );
    DeleteObject( hBmp );
 
    return true;
  }
 
  void release()
  {
    if ( m_pixel )
    {
      delete [] m_pixel;
      m_pixel = NULL;
    }
    ZeroMemory( &m_bmi, sizeof(m_bmi) );
  }
 
  bool render(
    HDC hdc,
    LONG destX, LONG destY,
    LONG destWidth, LONG destHeight,
    LONG srcX, LONG srcY,
    LONG srcWidth, LONG srcHeight ) const
  {
    return GDI_ERROR != StretchDIBits(
      hdc, destX, destY, destWidth, destHeight,
      srcX, srcY, srcWidth, srcHeight,
      m_pixel, &m_bmi, DIB_RGB_COLORS, SRCCOPY );
  }
 
 
  bool render(
    DIB32& dest,
    LONG destX, LONG destY,
    LONG width, LONG height,
    LONG srcX, LONG srcY ) const
  {
    if ( !m_pixel || !dest.m_pixel ) return false;
    if ( cliping(destX, destY, width, height, srcX, srcY, dest.getWidth(), dest.getHeight(), getWidth(), getHeight() ) )
    {
      DWORD transColor = *m_pixel;
      LPDWORD destLine = dest.m_pixel
        + ((dest.getHeight()-1)-destY)*dest.getWidth()
        + destX;
      LPDWORD srcLine = m_pixel
        + ((getHeight()-1)-srcY)*getWidth()
        + srcX;
      for (LONG y=0; y<height; ++y)
      {
        LPDWORD destPixel = destLine;
        LPDWORD srcPixel = srcLine;
        for (LONG x=0; x<width; ++x)
        {
          if ( (*srcPixel & 0xFFFFFF) != transColor )
          {
            *destPixel = *srcPixel;
          }
          destPixel++;
          srcPixel++;
        }
 
        destLine -= dest.getWidth();
        srcLine -= getWidth();
      }
 
      return true;
    }
 
    return false;
  }
 
  bool saveBitmap( LPCTSTR fileName ) const
  {
    //FileHeader用意
    BITMAPFILEHEADER bmpfh;
    bmpfh.bfOffBits = sizeof(bmpfh) + sizeof(m_bmi);
    bmpfh.bfSize = bmpfh.bfOffBits + getWidth()*getHeight()*sizeof(*m_pixel);
    bmpfh.bfType = ('M'<<8)+'B';
    bmpfh.bfReserved1 = bmpfh.bfReserved2 = 0;
 
    //ファイル作成
    HANDLE fh = CreateFile( fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
    if ( fh == INVALID_HANDLE_VALUE )
    {
      return false;
    }
 
    DWORD dwSize;
    WriteFile( fh, &bmpfh, sizeof(BITMAPFILEHEADER), &dwSize, NULL );
    WriteFile( fh, &m_bmi, sizeof(m_bmi), &dwSize, NULL );
    WriteFile( fh, m_pixel, getWidth()*getHeight()*sizeof(*m_pixel), &dwSize, NULL );
    CloseHandle( fh );
 
    return true;
  }
 
  LONG getWidth() const { return m_bmi.bmiHeader.biWidth; }
  LONG getHeight() const { return m_bmi.bmiHeader.biHeight; }
 
  static bool cliping(
    LONG& destX, LONG& destY,
    LONG& width, LONG& height,
    LONG& srcX, LONG& srcY,
    const LONG destWidth, const LONG destHeight,
    const LONG srcWidth, const LONG srcHeight )
  {
    // 左端クリッピング
    if ( destX < 0 ) { width += destX; srcX -= destX; destX = 0; }
    if ( srcX < 0 ) { width += srcX; destX -= srcX; srcX = 0; }
 
    // 右端
    if ( destX + width > destWidth ) { width -= ((destX+width)-destWidth); }
    if ( srcX + width > srcWidth ) { width -= ((srcX+width)-srcWidth); }
 
    // 上
    if ( destY < 0 ) { height += destY; srcY -= destY; destY = 0; }
    if ( srcY < 0 ) { height += srcY; destY -= srcY; srcY = 0; }
 
    // 下
    if ( destY + height > destHeight ) { height -= ((destY+height)-destHeight); }
    if ( srcY + height > srcHeight ) { height -= ((srcY+height)-srcHeight); }
 
    return ((width > 0) & (height > 0));
  }
 
private:
  LPDWORD m_pixel;
  BITMAPINFO m_bmi;
};
 
 
 
 
LRESULT CALLBACK wndProc(
  HWND hWnd,
  UINT msg,
  WPARAM wParam,
  LPARAM lParam )
{
  static DIB32 image;
  static DIB32 image2;
  switch (msg)
  {
  case WM_DESTROY:
    ShowWindow( hWnd, SW_HIDE );
    PostQuitMessage(0);
    break;
  case WM_CREATE:
    image.create( TEXT("image.bmp") );
    image2.create( TEXT("image2.bmp") );
    break;
  case WM_PAINT:
    {
      PAINTSTRUCT ps = {0};
      HDC hdc = BeginPaint( hWnd, &ps );
      image2.render( image, -50, -50, image2.getWidth(), image2.getHeight(), 0, 0 );
      image.render(
        hdc, 10, 10, image.getWidth(), image.getHeight(),
        0, 0, image.getWidth(), image.getHeight() );
      EndPaint( hWnd, &ps );
    }
    break;
  case WM_KEYDOWN:
    if ( wParam == VK_SPACE )
    {
      image.saveBitmap( TEXT("output.bmp") );
    }
    break;
  default:
    return DefWindowProc( hWnd, msg, wParam, lParam );
  }
 
  return 0;
}
 
 
 
 
int WINAPI WinMain(
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  PSTR lpCmdLine,
  int nCmdShow )
{
  LPCTSTR WINDOW_NAME = TEXT("sample");
 
  WNDCLASSEX wc;
  wc.style    = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc  = reinterpret_cast<WNDPROC>( wndProc );
  wc.cbClsExtra  = 0;
  wc.cbWndExtra  = 0;
  wc.cbSize    = sizeof( WNDCLASSEX );
  wc.hInstance  = hInstance;
  wc.hIcon    = NULL;
  wc.hIconSm    = NULL;
  wc.hCursor    = LoadCursor( NULL, IDC_ARROW );
  wc.hbrBackground= reinterpret_cast<HBRUSH>( GetStockObject(WHITE_BRUSH) );
  wc.lpszMenuName  = NULL;
  wc.lpszClassName= WINDOW_NAME;
  if ( !RegisterClassEx(&wc) ) return 0;
 
  LONG winWidth = 640
    + GetSystemMetrics(SM_CXEDGE)
    + GetSystemMetrics(SM_CXBORDER)
    + GetSystemMetrics(SM_CXDLGFRAME);
  LONG winHeight = 480
    + GetSystemMetrics(SM_CYEDGE)
    + GetSystemMetrics(SM_CYBORDER)
    + GetSystemMetrics(SM_CYDLGFRAME)
    + GetSystemMetrics(SM_CYCAPTION);
  HWND hWnd = CreateWindowEx(
    0, WINDOW_NAME, NULL, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME,
    CW_USEDEFAULT, CW_USEDEFAULT, winWidth, winHeight,
    NULL, NULL, hInstance, NULL);
  if ( !hWnd ) return -1;
 
  ShowWindow( hWnd, SW_SHOWNORMAL );
  UpdateWindow( hWnd );
 
  MSG msg;
  for (;;)
  {
    if ( !GetMessage(&msg, NULL, 0, 0) ) break;
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }
 
  
  UnregisterClass( WINDOW_NAME, hInstance );
  return msg.wParam;
}

 スペースキーを押すと、output.bmpをファイル出力します。

■関連記事:
生産がす: 32bitDIB(1) 作成と破棄
生産がす: 32bitDIB(2) 画像読み込み
生産がす: 32bitDIB(3) 塗りつぶし
生産がす: 32bitDIB(4) DIBに描画
生産がす: 32bitDIB(5) ファイルに出力
生産がす: 32bitDIB(6) 拡大縮小描画
生産がす: 32bitDIB(7) DIBSection
生産がす: DIB(8) - 直線描画
生産がす: DIB(9) - 回転描画
生産がす: DIB(10) - 三角形描画
生産がす: 日記ちゃん半透明合成
生産がす: 32bitDIBから無圧縮AVI2.0

0 件のコメント: