2,275
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include "stdlib.h"
#include "math.h"
#include <string>
#include <iostream>
using namespace std;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef long LONG;
typedef struct tagBITMAPFILEHEADER
{
//WORD bfType; //0x4d42 BM
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagNITMAPINFOHEADER
{
DWORD biSize;
LONG biWidth; //位图宽度
LONG biHeight; //位图高度
WORD biPlanes;
WORD biBitCount; //图像的位数
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsperMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagIMAGEDATA
{
BYTE blue;
BYTE green;
BYTE red;
}IMAGEDATA;
typedef struct tagRGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBQUAD;
BITMAPFILEHEADER file_head;
BITMAPINFOHEADER info_head;
int main()
{
char name[100];
WORD image_type;
cout<<"input the name of image"<<endl;
cin>>name;
IMAGEDATA * image_data;
IMAGEDATA * temp_data;
FILE * f1;
FILE * f2;
f1 = fopen(name,"rb");
fread(&image_type,sizeof(WORD),1,f1);
if(image_type!=0x4d42)
{
cout<<"this is not a bmp file"<<endl;
return 0;
}
fread(&file_head,sizeof(BITMAPFILEHEADER),1,f1);
fread(&info_head,sizeof(BITMAPINFOHEADER),1,f1);
if(info_head.biBitCount!=24)
{
cout<<"this is not a 24-bit image"<<endl;
return 0;
}
int width,height;
int midY,midX;
WORD biBitCount = info_head.biBitCount;
width = info_head.biWidth;
height = info_head.biHeight;
midY = height/2-1;
midX = width/2-1;
int total_size = width*height;
image_data = new IMAGEDATA[total_size]; //像素矩阵
int skipnum = (4-((width*sizeof(IMAGEDATA))%4))%4; //需要去除的填充字节数
temp_data = new IMAGEDATA [skipnum]; //去除填充字节的临时数组
for(int i = 0;i<height;i++)
{
for(int j=0;j<width;j++)
{
fread(image_data+(i*width+j),sizeof(IMAGEDATA),1,f1); //像素数组,按像素偏移
}
fread(temp_data,skipnum,1,f1); //去除填充字节
}
//fread(image_data,width*sizeof(IMAGEDATA)+3/4*4,height,f1);
/*
for(int j= 0;j<midY;j++)
{
for(int i = 0;i<midX;i++)
{
IMAGEDATA temp = image_data[(j+midY+1)*width+(i)];
image_data[(j+midY+1)*width+(i)]= image_data[i+j*width];
image_data[i+j*width]=temp;
}
}
*/
int addnum =( 4-((width*sizeof(BYTE))%4))%4;
BYTE * e_image_data = new BYTE[width*height];
for(int i =0;i<height;i++)
{
for(int j =0;j<width;j++)
{
int blue = image_data[i*width+j].blue;
int green = image_data[i*width+j].green;
int red = image_data[i*width+j].red;
e_image_data[i*width+j] = (BYTE)(0.1+0.587* (float)green+0.299*(float)red+0.114*(float)blue);
//cout<<e_image_data[i*width+j]<<endl;
}
}
fclose(f1);
//cout<<file_head.bfSize<<' '<<54+width*height*sizeof(IMAGEDATA)+skipnum*height;
file_head.bfSize =54+1024+height*width+(height*addnum);
cout<<addnum<<endl;
file_head.bfOffBits=54+1024;
info_head.biBitCount = 8;
//info_head.biSizeImage = width*height+height*addnum;
f2 = fopen("res3.bmp","a");
WORD file_type = 0x4d42;
fwrite(&file_type,sizeof(WORD),1,f2);
fwrite(&file_head,sizeof(BITMAPFILEHEADER),1,f2);
fwrite(&info_head,sizeof(BITMAPINFOHEADER),1,f2);
//fwrite(image_data,width*sizeof(IMAGEDATA)+3/4*4,height,f2);
RGBQUAD colortable[256];
for(int i=0;i<256;i++)
{
colortable[i].rgbBlue = i;
colortable[i].rgbGreen = i;
colortable[i].rgbRed = i;
colortable[i].rgbReserved = 0;
}
fwrite(colortable,sizeof(RGBQUAD)*256,1,f2);
BYTE * empty = new BYTE[addnum]; //添加用的数组
for(int i=0;i<addnum;i++)
empty[i]=0;
for(int i=0;i<height;i++)
{
for(int j =0;j<width;j++)
{
fwrite(&(e_image_data[i*width+j]),sizeof(BYTE),1,f2);
}
fwrite(empty,addnum,1,f2); //添加字节
}
fclose(f2);
return 0;
}