C++ 24位图转换成1位位图代码实现
时间: 2023-05-30 20:03:40 浏览: 77
以下是24位图转换成1位位图的代码实现(使用Python语言):
```python
from PIL import Image
# 打开24位图
im = Image.open('input_image.bmp')
# 转换为灰度图
gray_im = im.convert('L')
# 获取图像大小
width, height = gray_im.size
# 创建新的1位位图
new_im = Image.new('1', (width, height), 255)
# 设置阈值
threshold = 128
# 遍历每个像素点,将灰度值大于阈值的设为白色,否则设为黑色
for x in range(width):
for y in range(height):
if gray_im.getpixel((x, y)) > threshold:
new_im.putpixel((x, y), 255)
else:
new_im.putpixel((x, y), 0)
# 保存新图像
new_im.save('output_image.bmp')
```
上述代码中,我们使用Pillow库打开24位图,并将其转换为灰度图。然后,我们创建一个与原图像大小相同的1位位图,并遍历每个像素点。如果该像素的灰度值大于阈值,则将其设为白色,否则设为黑色。最后,我们保存新的1位位图。
相关问题
C++24位图转换成1位位图源码
以下是C语言实现24位图转换成1位位图的源代码:
```c
#include<stdio.h>
#include<stdlib.h>
#pragma pack(2)
typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
}BITMAPFILEHEADER;
#pragma pack(2)
typedef struct tagBITMAPINFOHEADER
{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
typedef struct tagRGBTRIPLE
{
unsigned char rgbtBlue;
unsigned char rgbtGreen;
unsigned char rgbtRed;
}RGBTRIPLE;
int main(int argc,char *argv[])
{
if(argc!=3)
{
printf("Usage:%s <input_file> <output_file>\n",argv[0]);
return -1;
}
char *input_file=argv[1];
char *output_file=argv[2];
FILE *fp_in=fopen(input_file,"rb");
if(fp_in==NULL)
{
printf("Open input file error!\n");
return -1;
}
BITMAPFILEHEADER file_header;
BITMAPINFOHEADER info_header;
RGBTRIPLE *p_rgb_buffer=NULL;//24位色图像缓冲区
unsigned char *p_mono_buffer=NULL;//1位色图像缓冲区
fread(&file_header,sizeof(BITMAPFILEHEADER),1,fp_in);
fread(&info_header,sizeof(BITMAPINFOHEADER),1,fp_in);
//判断位图类型是否为24位色图像
if(info_header.biBitCount!=24)
{
printf("Input file is not a 24-bit color bitmap!\n");
fclose(fp_in);
return -1;
}
//计算24位色图像每行的字节数
int row_size=(info_header.biWidth*info_header.biBitCount+31)/32*4;
//分配24位色图像缓冲区
p_rgb_buffer=(RGBTRIPLE*)malloc(row_size*info_header.biHeight);
if(p_rgb_buffer==NULL)
{
printf("Allocate memory error!\n");
fclose(fp_in);
return -1;
}
//读取24位色图像数据
fseek(fp_in,file_header.bfOffBits,SEEK_SET);
fread(p_rgb_buffer,row_size*info_header.biHeight,1,fp_in);
//计算1位色图像每行的字节数
int mono_row_size=(info_header.biWidth+31)/32*4;
//分配1位色图像缓冲区
p_mono_buffer=(unsigned char*)malloc(mono_row_size*info_header.biHeight);
if(p_mono_buffer==NULL)
{
printf("Allocate memory error!\n");
fclose(fp_in);
free(p_rgb_buffer);
return -1;
}
//将24位色图像数据转换成1位色图像数据
for(int i=0;i<info_header.biHeight;i++)
{
for(int j=0;j<info_header.biWidth;j++)
{
int index=(i*mono_row_size+j/8);
p_mono_buffer[index]|=((p_rgb_buffer[i*row_size+j].rgbtRed>128)?(1<<(7-j%8)):0);
}
}
//将1位色图像数据写入输出文件
FILE *fp_out=fopen(output_file,"wb");
if(fp_out==NULL)
{
printf("Open output file error!\n");
fclose(fp_in);
free(p_rgb_buffer);
free(p_mono_buffer);
return -1;
}
fwrite(&file_header,sizeof(BITMAPFILEHEADER),1,fp_out);
fwrite(&info_header,sizeof(BITMAPINFOHEADER),1,fp_out);
fwrite(p_mono_buffer,mono_row_size*info_header.biHeight,1,fp_out);
fclose(fp_in);
fclose(fp_out);
free(p_rgb_buffer);
free(p_mono_buffer);
return 0;
}
```
该程序实现了以下功能:
1. 读取24位色位图文件头和信息头信息。
2. 分配24位色图像缓冲区,并读取24位色图像数据。
3. 分配1位色图像缓冲区,将24位色图像数据转换成1位色图像数据。
4. 将1位色图像数据写入输出文件中。
5. 释放分配的内存,关闭文件句柄。
MFC 24位图转换成1位位图代码实现
以下是将 MFC 24 位图转换成 1 位位图的代码实现:
1. 定义函数 Convert24To1Bit,接收两个参数:24 位位图源文件路径和 1 位位图目标文件路径。
2. 在函数内部,使用 CImage 类加载 24 位位图源文件,获取其宽度和高度,并创建一个同样大小的 1 位位图 CImage 对象。
3. 使用 GetPixel 函数遍历 24 位位图的每一个像素点,计算出其灰度值,并将其转换成 0 或 1 的二进制值,存储到一个 1 位位图的像素数组中。
4. 使用 SetPixel 函数将每个像素点的二进制值写入到 1 位位图对象中。
5. 最后使用 Save 函数将 1 位位图对象保存到目标文件路径中。
以下是具体实现代码:
```c++
void Convert24To1Bit(LPCTSTR szSrcPath, LPCTSTR szDstPath)
{
CImage imgSrc;
imgSrc.Load(szSrcPath); // 加载 24 位位图
int nWidth = imgSrc.GetWidth();
int nHeight = imgSrc.GetHeight();
CImage imgDst;
imgDst.Create(nWidth, nHeight, 1); // 创建 1 位位图
BYTE* pSrcData = (BYTE*)imgSrc.GetBits();
BYTE* pDstData = (BYTE*)imgDst.GetBits();
int nSrcPitch = imgSrc.GetPitch();
int nDstPitch = imgDst.GetPitch();
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
COLORREF clr = *(COLORREF*)(pSrcData + y * nSrcPitch + x * 3);
BYTE gray = (BYTE)(GetRValue(clr) * 0.299 + GetGValue(clr) * 0.587 + GetBValue(clr) * 0.114);
BYTE bit = gray > 128 ? 1 : 0;
*(pDstData + y * nDstPitch + x / 8) |= (bit << (7 - x % 8));
}
}
imgDst.Save(szDstPath, Gdiplus::ImageFormatBMP); // 保存 1 位位图
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)