2 4 8位BMP转24位BMP(代码)

发布时间:2016-12-7 16:34:43 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"2 4 8位BMP转24位BMP(代码)",主要涉及到2 4 8位BMP转24位BMP(代码)方面的内容,对于2 4 8位BMP转24位BMP(代码)感兴趣的同学可以参考一下。

//#include "stdafx.h" #include <math.h> #define WIDTHBYTE(bit) (( ( (bit)+31)/32 )*4) #define _BITS_PER_PIXEL_24 24 BOOL ConvertBmp4ToBmp24(const char* pSrcFile,const char* pDesFile) { FILE* fpSrc = fopen(pSrcFile,"rb"); if(fpSrc == NULL) return FALSE; // Read BITMAPFILEHEADER info BITMAPFILEHEADER bfh; fread(&bfh, 1, sizeof(BITMAPFILEHEADER), fpSrc); // Read BITMAPINFOHEADER info BITMAPINFOHEADER bih; fread(&bih, 1, sizeof(BITMAPINFOHEADER), fpSrc); const int iPitch = WIDTHBYTE(bih.biBitCount*bih.biWidth); if(bih.biBitCount == 4) //16 色 { //转换为 24 位 long _bpp = (_BITS_PER_PIXEL_24 >> 3); long _pitch = bih.biWidth * _bpp; while ((_pitch & 3) != 0) _pitch++; FILE* fpDes = fopen(pDesFile,"wb"); if(fpDes == NULL) { fclose(fpSrc); return FALSE; } //24 bmp header BITMAPFILEHEADER bfh24 = bfh; BITMAPINFOHEADER bih24 = bih; DWORD dwSize = _pitch * bih.biHeight; bfh24.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwSize; bfh24.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bih24.biBitCount = _BITS_PER_PIXEL_24; fwrite(&bfh24,sizeof(BITMAPFILEHEADER),1,fpDes); fwrite(&bih24,sizeof(BITMAPINFOHEADER),1,fpDes); //end // Calculate palette entries int iPaletteEntries = bih.biClrUsed; if (iPaletteEntries == 0) iPaletteEntries = 16; RGBQUAD lpPalette[256] = {0}; // Read palette info fread(lpPalette, iPaletteEntries, sizeof(RGBQUAD), fpSrc); //Progress // CProgressDlg dlg;//进度条 // dlg.Create(); // dlg.SetStep(1); // dlg.SetPos(0); // dlg.SetWindowText(_T("位图格式转换中...")); //分批读取数据 200 row const int oncerow = 200; const int times = bih.biHeight/oncerow; for(int i = 0;i<times;i++) //需要处理多少次 { //dlg.SetPos( (1.0*i/times)*100); const DWORD dwSize = iPitch * oncerow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * oncerow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<oncerow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) //像素个数 { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; //lpData[dwSrcTotalOffset] //第 j 个像素点 //const int bitoffidx = 7 - j%8; //前 4 个字节的值 //const int opreatenum = pow(2,bitoffidx); //int statevalue = (lpData[dwSrcTotalOffset]&opreatenum) >> bitoffidx; int statevalue = 0; if(j%2 == 0) { statevalue = (lpData[dwSrcTotalOffset] & 0xf0) >> 4; } else { statevalue = lpData[dwSrcTotalOffset] & 0x0f; } BYTE red = lpPalette[statevalue].rgbRed; BYTE green = lpPalette[statevalue].rgbGreen; BYTE blue = lpPalette[statevalue].rgbBlue; // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset = (j+1)/2; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } //剩余不够 200 行的 const int leftrow = bih.biHeight%oncerow; //dlg.SetPos(100); if(leftrow > 0) { const DWORD dwSize = iPitch * leftrow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * leftrow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<leftrow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; //star //const int bitoffidx = 7-j%8; //const int opreatenum = pow(2,bitoffidx); //int statevalue = (lpData[dwSrcTotalOffset]&opreatenum) >> bitoffidx; int statevalue = 0; if(j%2 == 0) { statevalue = (lpData[dwSrcTotalOffset] & 0xf0) >> 4; } else { statevalue = lpData[dwSrcTotalOffset] & 0x0f; } BYTE red = lpPalette[statevalue].rgbRed; BYTE green = lpPalette[statevalue].rgbGreen; BYTE blue = lpPalette[statevalue].rgbBlue; //end // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset = (j+1)/2; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } fclose(fpSrc); fclose(fpDes); return TRUE; } else { fclose(fpSrc); return FALSE; } } BOOL ConvertBmp1ToBmp24(const char* pSrcFile,const char* pDesFile) { FILE* fpSrc = fopen(pSrcFile,"rb"); if(fpSrc == NULL) return FALSE; // Read BITMAPFILEHEADER info BITMAPFILEHEADER bfh; fread(&bfh, 1, sizeof(BITMAPFILEHEADER), fpSrc); // Read BITMAPINFOHEADER info BITMAPINFOHEADER bih; fread(&bih, 1, sizeof(BITMAPINFOHEADER), fpSrc); const int iPitch = WIDTHBYTE(bih.biBitCount*bih.biWidth); if(bih.biBitCount == 1) //黑白图 { //转换为 24 位 long _bpp = (_BITS_PER_PIXEL_24 >> 3); long _pitch = bih.biWidth * _bpp; while ((_pitch & 3) != 0) _pitch++; FILE* fpDes = fopen(pDesFile,"wb"); if(fpDes == NULL) { fclose(fpSrc); return FALSE; } //24 bmp header BITMAPFILEHEADER bfh24 = bfh; BITMAPINFOHEADER bih24 = bih; DWORD dwSize = _pitch * bih.biHeight; bfh24.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwSize; bfh24.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bih24.biBitCount = _BITS_PER_PIXEL_24; fwrite(&bfh24,sizeof(BITMAPFILEHEADER),1,fpDes); fwrite(&bih24,sizeof(BITMAPINFOHEADER),1,fpDes); //end // Calculate palette entries int iPaletteEntries = bih.biClrUsed; if (iPaletteEntries == 0) iPaletteEntries = 2; RGBQUAD lpPalette[256] = {0}; // Read palette info fread(lpPalette, iPaletteEntries, sizeof(RGBQUAD), fpSrc); // //Progress // CProgressDlg dlg;//进度条 // dlg.Create(); // dlg.SetStep(1); // dlg.SetPos(0); // dlg.SetWindowText(_T("位图格式转换中...")); //分批读取数据 200 row const int oncerow = 200; const int times = bih.biHeight/oncerow; for(int i = 0;i<times;i++) //需要处理多少次 { //dlg.SetPos( (1.0*i/times)*100); const DWORD dwSize = iPitch * oncerow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * oncerow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<oncerow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) //像素个数 { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; //lpData[dwSrcTotalOffset] //第 j 个像素点 const int bitoffidx = 7 - j%8; const int opreatenum = pow(2,bitoffidx); int statevalue = (lpData[dwSrcTotalOffset]&opreatenum) >> bitoffidx; BYTE red = lpPalette[statevalue].rgbRed; BYTE green = lpPalette[statevalue].rgbGreen; BYTE blue = lpPalette[statevalue].rgbBlue; // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset = (j+1)/8; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } //剩余不够 200 行的 const int leftrow = bih.biHeight%oncerow; //dlg.SetPos(100); if(leftrow > 0) { const DWORD dwSize = iPitch * leftrow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * leftrow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<leftrow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; //star const int bitoffidx = 7-j%8; const int opreatenum = pow(2,bitoffidx); int statevalue = (lpData[dwSrcTotalOffset]&opreatenum) >> bitoffidx; BYTE red = lpPalette[statevalue].rgbRed; BYTE green = lpPalette[statevalue].rgbGreen; BYTE blue = lpPalette[statevalue].rgbBlue; //end // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset = (j+1)/8; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } fclose(fpSrc); fclose(fpDes); return TRUE; } else { fclose(fpSrc); return FALSE; } } BOOL ConvertBmp8ToBmp24(const char* pSrcFile,const char* pDesFile) { FILE* fpSrc = fopen(pSrcFile,"rb"); if(fpSrc == NULL) return FALSE; // Read BITMAPFILEHEADER info BITMAPFILEHEADER bfh; fread(&bfh, 1, sizeof(BITMAPFILEHEADER), fpSrc); // Read BITMAPINFOHEADER info BITMAPINFOHEADER bih; fread(&bih, 1, sizeof(BITMAPINFOHEADER), fpSrc); // Calculate pitch const int iBpp = bih.biBitCount >> 3; int iPitch = iBpp * bih.biWidth; while ((iPitch & 3) != 0) iPitch++; if (bih.biBitCount == 8) { //转换为 24 位 long _bpp = (_BITS_PER_PIXEL_24 >> 3); long _pitch = bih.biWidth * _bpp; while ((_pitch & 3) != 0) _pitch++; FILE* fpDes = fopen(pDesFile,"wb"); if(fpDes == NULL) { fclose(fpSrc); return FALSE; } //24 bmp header BITMAPFILEHEADER bfh24 = bfh; BITMAPINFOHEADER bih24 = bih; DWORD dwSize = _pitch * bih.biHeight; bfh24.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwSize; bfh24.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bih24.biBitCount = _BITS_PER_PIXEL_24; fwrite(&bfh24,sizeof(BITMAPFILEHEADER),1,fpDes); fwrite(&bih24,sizeof(BITMAPINFOHEADER),1,fpDes); //end // Calculate palette entries int iPaletteEntries = bih.biClrUsed; if (iPaletteEntries == 0) iPaletteEntries = 256; RGBQUAD lpPalette[256] = {0}; // Read palette info fread(lpPalette, iPaletteEntries, sizeof(RGBQUAD), fpSrc); //Progress // CProgressDlg dlg;//进度条 // dlg.Create(); // dlg.SetStep(1); // dlg.SetPos(0); // dlg.SetWindowText(_T("位图格式转换中...")); //分批读取数据 200 row const int oncerow = 200; const int times = bih.biHeight/oncerow; for(int i = 0;i<times;i++) //需要处理多少次 { //dlg.SetPos( (1.0*i/times)*100); const DWORD dwSize = iPitch * oncerow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * oncerow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<oncerow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; BYTE red = lpPalette[lpData[dwSrcTotalOffset]].rgbRed; BYTE green = lpPalette[lpData[dwSrcTotalOffset]].rgbGreen; BYTE blue = lpPalette[lpData[dwSrcTotalOffset]].rgbBlue; // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset += iBpp; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } //剩余不够 200 行的 const int leftrow = bih.biHeight%oncerow; //dlg.SetPos(100); if(leftrow > 0) { const DWORD dwSize = iPitch * leftrow; LPBYTE lpData = (LPBYTE)malloc(dwSize*sizeof(BYTE)); fread(lpData, dwSize, sizeof(BYTE), fpSrc); // Create temporary bitmap const DWORD dw24Size = _pitch * leftrow; LPBYTE lp24Data = (LPBYTE)malloc(dw24Size*sizeof(BYTE)); // Convert bitmap DWORD dwDstHorizontalOffset; DWORD dwDstVerticalOffset = 0; DWORD dwDstTotalOffset; DWORD dwSrcHorizontalOffset; DWORD dwSrcVerticalOffset = 0; DWORD dwSrcTotalOffset; for (long i=0; i<leftrow; i++) //每次转换 200 行 { dwDstHorizontalOffset = 0; dwSrcHorizontalOffset = 0; for (long j=0; j<bih.biWidth; j++) { // Update destination total offset dwDstTotalOffset = dwDstVerticalOffset + dwDstHorizontalOffset; // Update source total offset dwSrcTotalOffset = dwSrcVerticalOffset + dwSrcHorizontalOffset; BYTE red = lpPalette[lpData[dwSrcTotalOffset]].rgbRed; BYTE green = lpPalette[lpData[dwSrcTotalOffset]].rgbGreen; BYTE blue = lpPalette[lpData[dwSrcTotalOffset]].rgbBlue; // Update bitmap lp24Data[dwDstTotalOffset+2] = red; lp24Data[dwDstTotalOffset+1] = green; lp24Data[dwDstTotalOffset] = blue; // Update destination horizontal offset dwDstHorizontalOffset += _bpp; // Update source horizontal offset dwSrcHorizontalOffset += iBpp; } // Update destination vertical offset dwDstVerticalOffset += _pitch; // Update source vertical offset dwSrcVerticalOffset += iPitch; } fwrite(lp24Data,sizeof(BYTE),dw24Size,fpDes); delete[] lp24Data; } fclose(fpSrc); fclose(fpDes); return TRUE; } else { fclose(fpSrc); return FALSE; } }转载 :http://download.csdn.net/detail/ccxbb/6022487

上一篇:Opencv 多通道矩阵的访问
下一篇:POJ 3352 Road Construction / 边双连通分量

相关文章

相关评论