24真彩图转4位位图

 

一定要注意数据对齐方式%%%%%%%%%%%%%

BOOL CPhotoDlg::Convert24To4(LPCTSTR lpszSrcFile, LPCTSTR lpszDestFile)
{
 FILE*fp;
 fp = fopen("aa.txt", "wr");
 BITMAPFILEHEADER bmHdr;
 BITMAPINFOHEADER bmInfo;
 HANDLE hFile, hNewFile;
 DWORD dwByteWritten = 0;

 //打开源文件句柄
 hFile = CreateFile(lpszSrcFile,
  GENERIC_READ,
  FILE_SHARE_READ,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  NULL);

 if(hFile == INVALID_HANDLE_VALUE)
  return FALSE;
 hNewFile = CreateFile(lpszDestFile,
  GENERIC_READ|GENERIC_WRITE,
  FILE_SHARE_READ|FILE_SHARE_WRITE,
  NULL,
  CREATE_ALWAYS,
  FILE_ATTRIBUTE_NORMAL,
  NULL);
 if(hNewFile == INVALID_HANDLE_VALUE)
 {
  CloseHandle(hFile);
  return false;
 }
 //读取原文件bmp头和文件悉尼市
 ReadFile(hFile, &bmHdr, sizeof(bmHdr), &dwByteWritten, NULL);
 ReadFile(hFile, &bmInfo, sizeof(bmInfo), &dwByteWritten, NULL);
 //只处理24位为压缩图像
 if(bmInfo.biBitCount != 24 || bmInfo.biCompression != 0)
 {
  CloseHandle(hNewFile);
  CloseHandle(hFile);
  DeleteFile(lpszDestFile);
  return false;
 }
 //计算图像数据大小
 DWORD dwOldSize = bmInfo.biSizeImage;
 if(dwOldSize == 0)
 {
  dwOldSize = bmHdr.bfSize - sizeof(bmHdr) - sizeof(bmInfo);
 }
 /*
 long wid = bmInfo.biWidth % 4;
 if(wid > 0)
 {
  wid = 4 - wid;
 }
 wid += bmInfo.biWidth;
 */
 long wid = bmInfo.biWidth;
 long lOldWidth = (bmInfo.biWidth * 3 +3)/4*4;
 long lNewWidth = ((bmInfo.biWidth+1)/2 +3)/4*4;
 DWORD dwNewSize;
 dwNewSize = lNewWidth *bmInfo.biHeight; // 转换后图片的大小
 //读取原始数据
 unsigned char *pBuffer = NULL;
 pBuffer = new unsigned char[dwOldSize];
 if(pBuffer == NULL)
 {
  CloseHandle(hNewFile);
  CloseHandle(hFile);
  DeleteFile(lpszDestFile);
  return false;
 }
 ReadFile(hFile, pBuffer, dwOldSize, &dwByteWritten, NULL);
 fwrite(pBuffer, dwOldSize, 1, fp);
 //fclose(fp);
 unsigned char *pNew = new unsigned char [dwNewSize];
 unsigned char color = 0;
 DWORD dwIndex = 0, dwOldIndex = 0;
 int i, j, k;
 long pos = 0;

 unsigned char R, G, B;
 //
 for(j = 0; j < bmInfo.biHeight; j++)
 {
  pos = lOldWidth * j;
  for(i = 0; i< lOldWidth; i = i+6)
  {
   B = pBuffer[pos+i];
   G = pBuffer[pos+i+1];
   R = pBuffer[pos+i+2];
   unsigned char maxcolor = 0.299*R+0.587*G+0.114*B;
   maxcolor /= 17;
   B = pBuffer[pos+i+3];
   G = pBuffer[pos+i+4];
   R = pBuffer[pos+i+5];
   unsigned char maxcolor2 = 0.299*R+0.587*G+0.114*B;
   maxcolor2 /= 17;
   pNew[j*lNewWidth + i/6] = (maxcolor<<4)|maxcolor2;
  }
 }
 fwrite("------------------", sizeof("------------------"), 1, fp);
 fwrite(pNew, dwNewSize, 1, fp);
 fclose(fp);
 /
 //把结果保存到新文件
 //修改属性
 bmHdr.bfSize = sizeof(bmHdr)+sizeof(bmInfo)+sizeof(RGBQUAD)*16 + dwNewSize;
 bmHdr.bfOffBits = bmHdr.bfSize - dwNewSize;
 bmInfo.biBitCount = 4;
 bmInfo.biSizeImage = dwNewSize;

 //创建调色板
 RGBQUAD pa[16];
 unsigned char c;
 for(i = 0; i < 16; i++)
 {
  c = i * 17;
  pa[i].rgbBlue =c;
  pa[i].rgbGreen = c;
  pa[i].rgbRed= c;
  pa[i].rgbReserved = 0;
 }
 //BMP头
 WriteFile(hNewFile, &bmHdr, sizeof(bmHdr), &dwByteWritten, NULL);
 WriteFile(hNewFile, &bmInfo, sizeof(bmInfo), &dwByteWritten, NULL);
 WriteFile(hNewFile, pa, sizeof(RGBQUAD)*16, &dwByteWritten, NULL);
 WriteFile(hNewFile, pNew, dwNewSize, &dwByteWritten, NULL);
 delete []pBuffer;
 delete []pNew;
 CloseHandle(hNewFile);
 CloseHandle(hFile);
 return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值