已知像素写成BMP文件

 void CMapEditDlg::OnButtonSave()
{
//  FILE  *pFilein;
//  if ((pFilein = fopen("D://abc.img", "ab+")) == NULL)
//     {
//       printf("cannot open file");
//       exit(0);
//     }
//  for (int nRow = 0; nRow < 138; nRow++)
//  {
//   for (int nCol = 0; nCol < 156; nCol++)
//   {
//    fwrite(&m_col[nCol][nRow],sizeof(COLORREF),1,pFilein);
//   }
//  }
//  fclose(pFilein);
 SaveToBMP("D://abc.img");
 MessageBox("OK");
//  char caption[100];
//  //»ñÈ¡Ò¶ÃæÄÚÈÝ
//  strcpy(caption,csMapName);
//  
//  char stype[100];
//  strcpy(stype,csMapStyle);
//  char texture[100];
//  CString csTemp = _T("");
//  csTemp.Format(_T("%d"),m_nFloorNum);
//  strcpy(texture,csTemp);
//  char music[100];
//  strcpy(music,csMapMusic);
// //  int height = m_nHeight;
// //  int width = m_nWidth;
// //  int t = 0;
// //  int mymap[143];
// //
// //
// //  for (int i = 0; i < width; i++)
// //  {
// //   for (int j = 0; j < height; j++)
// //   {
// //    mymap[t] = t;
// //    t++;
// //   }
// //  }
// // csTemp = m_csMapPath +
// // str = "C://"+m_cCaption+".map";
//  //**************************************************************************
//     if ((pFilein = fopen(csTemp, "ab+")) == NULL)
//     {
//       printf("cannot open file");
//       exit(0);
//     }
//     FILE *stream = tmpfile();//´´½¨ÁÙʱÎļþ
//     fwrite(&caption, 100, 1, stream); // ½«ÄÚÈÝдÈëÁÙʱÎļþ
//  fwrite(&stype, 100, 1, stream);
//  fwrite(&texture, 100, 1, stream);
//  fwrite(&music, 100, 1, stream);
//  m_progress.SetPos(1);
//  int i = 2;
//  for (int nRow = 0; nRow < 11; nRow++)
//  {
//   for (int nCol = 0; nCol < 13; nCol++)
//   {
//    fwrite(&m_stMapObj[nRow][nCol],16,1,stream);
//    i++;
//    int n = static_cast<int>((static_cast<float>(i)/static_cast<float>(286))*100);
//    m_progress.SetPos(n);
//   }
//  }
//  rewind(stream);//ÒÆÖÁÎļþÍ·
//  //¼ÓÃÜÎļþ
//
//  char ch;
//  ch=fgetc(stream);
//  CString s = _T("passwordfsdfasdfasdf");
//  char *psw = s.GetBuffer(0);
//  int j = 0;
//   i = 0;
//  while(psw[++i]);
//  int z = 0;
//  while(!feof(stream))
//  {
//   ch=ch^psw[j>=i?j=0:j++];
//   
//   fputc(ch,pFilein);
//   ch=fgetc(stream);
//   z++;
//   int n= 50 + static_cast<int>((static_cast<float>(z)/static_cast<float>(5376))*100);
//   m_progress.SetPos(n);
//  }
//  _rmtmp();//ɾ³ýÁÙʱÎļþ
//     fclose(pFilein);
//   m_progress.ShowWindow(FALSE);
//  m_progress.SetPos(0);
}
void CMapEditDlg::ReadMap()
{
 FILE *pFileout;  // Îļþ²Ù×÷Ö¸Õë
 char caption[100] = "";
 char stype[100] = "";
 char texture[100] = "";
 char music[100] = "";
 int t = 0;
 CString str;
 // ÒÔ¶þ½øÖÆ·½Ê½´ò¿ªuser.datÎļþ
 CFileDialog dlg(TRUE,"*.map",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"Map Files (*.map)|*.map|",NULL);
 if (dlg.DoModal() == IDOK)
 {
  str = dlg.GetPathName();
 }
 else
 {
  return;
 }
 if ((pFileout = fopen(str, "rb")) == NULL)
 {
  printf("cannot open file");
  exit(0);
 }
 // ³É¿é´ÓÎļþÖжÁÈ¡
 FILE *stream = tmpfile();//´´½¨ÁÙʱÎļþ
 //½âÃÜÎļþ
 char ch;

 ch=fgetc(pFileout);
 CString s = _T("passwordfsdfasdfasdf");
 char *psw = s.GetBuffer(0);
 int a,b;
 b = a = 0;
 while(psw[++a]);
 int z = 0;
 while(!feof(pFileout))
 {
  ch=ch^psw[b>=a?b=0:b++];
  fputc(ch,stream);
  ch=fgetc(pFileout);
  z++;
  int n= static_cast<int>((static_cast<float>(z)/static_cast<float>(5376))*100);
  m_progress.SetPos(n);
 }
 fclose(pFileout);
 rewind(stream);//ÒÆÖÁÎļþÍ·
 fread(&caption, 100, 1, stream);
 fread(&stype, 100, 1, stream);
 fread(&texture, 100, 1, stream);
 fread(&music, 100, 1, stream);
 int i = 0;
 for (int nRow = 0; nRow < 11; nRow++)
 {
  for (int nCol = 0; nCol < 13; nCol++)
  {
   fwrite(&m_stMapObj[nRow][nCol],16,1,stream);
   i++;
   int n = 50 + static_cast<int>((static_cast<float>(i)/static_cast<float>(286))*100);
   m_progress.SetPos(n);
  }
 }
 _rmtmp();//ɾ³ýÁÙʱÎļþ
 m_progress.ShowWindow(FALSE);
 m_progress.SetPos(0);
}
typedef struct RGB_VALUE
{
 int nRed;
 int nGreen;
 int nBule;
}RGBVALUE;
RGBVALUE GetRGBValue(COLORREF color)
{
 RGBVALUE stRGB;
 stRGB.nRed = color&RGB(255,0,0);
 stRGB.nGreen = (color&RGB(0,255,0)) >> 8;
 stRGB.nBule = (color&RGB(0,0,255)) >> 16;
 return stRGB;

}
BOOL CMapEditDlg::SaveToBMP( char* fileName )
{
 int nPixelFormat = 0;
 nPixelFormat = ::GetDeviceCaps(hSmDC,BITSPIXEL);
//  DDSURFACEDESC2 ddsd;
//  memset( &ddsd, 0, sizeof(DDSURFACEDESC2) );
//  ddsd.dwSize = sizeof(DDSURFACEDESC2);
//  m_lpDDS->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
 int m_iWidth = 156;
 int m_iHeight = 138;
// BYTE* p = (BYTE*)ddsd.lpSurface;

 // ´ò¿ªÎļþ
 FILE* fp;
 if( (fp=fopen(fileName, "wb")) != NULL )
 {
  // ±£´æÎļþÍ·
  BITMAPFILEHEADER FileHeader;
  FileHeader.bfType = 'MB';// Not BM
  FileHeader.bfSize = m_iWidth * m_iHeight * 3 + 0x36;
  FileHeader.bfReserved1 = 0;
  FileHeader.bfReserved2 = 0;
  FileHeader.bfOffBits = 0x36;
  fwrite(&FileHeader, sizeof(BITMAPFILEHEADER), 1, fp);

  // ±£´æÎļþÐÅÏ¢
  BITMAPINFOHEADER Header;
  Header.biSize = sizeof(BITMAPINFOHEADER); // ½á¹¹µÄ´óС
  Header.biWidth = m_iWidth;     // ¿í
  Header.biHeight = -m_iHeight;       // ¸ß
  Header.biPlanes = 1;      // ¹Ì¶¨
  Header.biBitCount = 24;      // ÑÕÉ«Êý
  Header.biCompression = BI_RGB;    // ÊÇ·ñѹËõ
  Header.biSizeImage = m_iWidth * m_iHeight * 3; // ͼƬµÄ´óС
  Header.biXPelsPerMeter = 0;
  Header.biYPelsPerMeter = 0;
  Header.biClrUsed = 0;
  Header.biClrImportant = 0;
  fwrite(&Header, Header.biSize, 1, fp);

  // дÈë¾ßÌåÄÚÈÝ(´ÓÏÂÏòÉÏ´æ·Å)
//  fseek(fp, 0x36, SEEK_SET);

//   if( nPixelFormat == 32 )
//   {
//    int iPitch = ((4-ddsd.dwWidth&3)&3);
//    p += (m_iHeight - 1) * ddsd.lPitch;
   for(int i=0; i<m_iHeight; i++)
   {
    for(int j=0; j<m_iWidth; j++)
    {
     RGBVALUE rgb;
     rgb = GetRGBValue(m_col[j][i]);
     fputc( rgb.nBule, fp); // b
     fputc( rgb.nGreen, fp); // g
     fputc( rgb.nRed, fp); // r
//      if( j==m_iWidth-1 && i==0 )
//       break;
//      p += 4;
    }
//     if( i==0 )
//      p -= (ddsd.lPitch<<1)-(iPitch<<2) - 4;
//     else
//      p -= (ddsd.lPitch<<1)-(iPitch<<2);
   }
//   }
//   else if( nPixelFormat == 16 )
//   {
// //    int iPitch = ((2-ddsd.dwWidth&1)&1);
// //    p += (m_iHeight - 1) * ddsd.lPitch;
//    for(int i=0; i<m_iHeight; i++)
//    {
//     for(int j=0; j<m_iWidth; j++)
//     {
//      fputc( GetB(p[0]),       fp);    // b
//      fputc( GetG(p[0],p[1]),  fp); // g
//      fputc( GetR(p[1]),       fp);    // r
// //      if( j==m_iWidth-1 && i==0 )
// //       break;
// //      p += 2;
//     }
// //     if( i==0 )
// //      p -= (ddsd.lPitch<<1)-(iPitch<<1) - 2;
// //     else
// //      p -= (ddsd.lPitch<<1)-(iPitch<<1); // Ö¸Õëתµ½ÉÏÒ»ÐеĿªÊ¼
//    }
//  }

  fclose(fp);
 }
//  m_lpDDS->Unlock( NULL );
 return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值