スポンサーサイト

0
    この広告は60日以上更新がないブログに表示されております。
    新しい記事を書くことで広告を消すことができます。
    • 2011.03.05 Saturday
    • -
    • -
    • -
    • by スポンサードリンク

    簡単な画像を扱うクラスの実装4 画像の読み込みと書き込み

    0
      // ヘッダーファイルの追加
      #include

      /****************************************************************************/

      // 画像の読み込み
      bool CBmp::Read(const char *path)
      {
      // 画像の廃棄
      Destroy();

      // ファイル情報格納領域
      char fhead[32];

      // ファイル情報オフセット
      int foffset[8] = {0,2,6,8,10,0,0,0};

      // 読み込むファイルを開く
      ifstream is(path,ios::in | ios::binary);

      // ファイル情報の読み込み
      is.read((char *)fhead,14);

      // 画像ファイルか確認
      unsigned short *us = (unsigned short *)((char *)fhead);
      if(*us != 0x4d42)
      return false;

      // 画像情報の読み込み
      is.read(m_Header,BMPHEADERSIZE);

      // ビット情報格納領域のサイズを計算
      unsigned long *ul = (unsigned long *)((char *)m_Header + m_Offset.i);
      *ul = ScanBytes(GetWidth(),GetDepth()) * GetHeight();

      // パレット情報の読み込み
      int coentries = GetColorEntries();
      if(GetColorEntries())
      is.read((char *)m_Header + BMPHEADERSIZE,GetColorEntries() * 4);

      // ビット情報格納領域の確保
      if((m_Bits = new char[*ul]) == 0)
      {
      Destroy();
      return false;
      }

      // ビット情報読み込み
      is.read((char *)m_Bits,*ul);

      return IsOK();
      }


      /****************************************************************************/

      // 画像の書き出し
      bool CBmp::Write(const char *path)
      {
      // 書き込む画像の確認
      if(!IsOK())
      return false;

      // ファイル情報格納領域
      char fhead[32];

      // ファイル情報オフセット
      int foffset[8] = {0,2,6,8,10,0,0,0};

      // ファイル情報格納
      unsigned short *us;
      unsigned long *ul;

      int i = 0;

      // ファイルタイプ
      us = (unsigned short *)((char *)fhead + foffset[i++]);
      *us = 0x4d42;

      // ファイルのサイズ
      ul = (unsigned long *)((char *)fhead + foffset[i++]);
      *ul = 14 + BMPHEADERSIZE + GetColorEntries() * 4 + ScanBytes(GetWidth(),GetDepth()) * GetHeight();

      // 未使用情報
      us = (unsigned short *)((char *)fhead + foffset[i++]);
      *us = 0;
      us = (unsigned short *)((char *)fhead + foffset[i++]);
      *us = 0;

      // ビット情報までのオフセット
      ul = (unsigned long *)((char *)fhead + foffset[i++]);
      *ul = 14 + BMPHEADERSIZE + GetColorEntries() * 4;

      // 書き込むファイルを開く
      ofstream os(path,ios::out | ios::binary);

      // ファイル情報書き込み
      os.write((char *)fhead,14);

      // ヘッダー格納領域書き込み
      os.write((char *)m_Header,BMPHEADERSIZE);

      // パレット情報の書き込み
      if(GetColorEntries())
      os.write((char *)m_Header + BMPHEADERSIZE,GetColorEntries() * 4);

      // ビット情報格納領域書き込み
      os.write((char *)m_Bits,ScanBytes(GetWidth(),GetDepth()) * GetHeight());

      return true;
      }

      /****************************************************************************/


      JUGEMテーマ:C言語




      PR

      calendar

      S M T W T F S
          123
      45678910
      11121314151617
      18192021222324
      252627282930 
      << September 2011 >>

      ランキング

      selected entries

      categories

      archives

      recommend

      links

      profile

      書いた記事数:5 最後に更新した日:2011/02/28

      search this site.

      others

      mobile

      qrcode

      powered

      無料ブログ作成サービス JUGEM