C++24位图转换成1位位图源码
时间: 2023-05-31 18:02:34 浏览: 101
vc++中的位图转换源代码
4星 · 用户满意度95%
以下是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. 释放分配的内存,关闭文件句柄。
阅读全文