#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
/* 定义BYTE为一个字节的类型 */
typedef unsigned char BYTE;
/* 定义WORD为两个字节的类型 */
typedef unsigned short WORD;
/* 定义DWORD为四个字节的类型 */
typedef unsigned int DWORD;
/*************************** 位图标准信息(54字节,无调色板) ******************************************/
/* 位图文件头 (位图头的字节数 = 位图文件字节数 - 位图图像数据字节数)*/
#pragma pack(2)
typedef struct BMP_FILE_HEADER
{
WORD bType; // 文件标识符
DWORD bSize; // 文件的大小
WORD bReserved1; // 保留值,必须设置为0
WORD bReserved2; // 保留值,必须设置为0
DWORD bOffset; // 文件头的最后到图像数据位开始的偏移量
} BMPFILEHEADER; // 14 字节
//#pragma pack(pop)
/* 位图信息头 */
typedef struct BMP_INFO
{
DWORD bInfoSize; // 信息头的大小
DWORD bWidth; // 图像的宽度
DWORD bHeight; // 图像的高度
WORD bPlanes; // 图像的位面数
WORD bBitCount; // 每个像素的位数
DWORD bCompression; // 压缩类型
DWORD bmpImageSize; // 图像的大小,以字节为单位
DWORD bXPelsPerMeter; // 水平分辨率
DWORD bYPelsPerMeter; // 垂直分辨率
DWORD bClrUsed; // 使用的色彩数
DWORD bClrImportant; // 重要的颜色数
} BMPINF; // 40 字节
typedef struct BMP_COLOR
{
unsigned char* buf;
int width_x; //宽
int height_y; //高
int deepth; //像素位
}Bmpbase;
int main()
{
FILE *fp; // 定义一个文件指针
FILE *out;
BMPFILEHEADER bmpFileHeader; // 定义一个 BMP 文件头
BMPINF bmpInfo; // 定义一个 BMP 位图信息头
Bmpbase bmp; // 定义一个存储颜色信息
/* 打开原始图片 */
if((fp = fopen("photo.bmp", "rb")) == NULL) // fp = 0x00426aa0
{
printf("Cann't open the file!\n");
return 0;
}
//指向文件头
fseek(fp, 0, SEEK_SET);
// 读取文件头信息
/*
参数1: 读出数据的存放缓冲区
参数2: 读取多少个字节的数据
参数3: 读取几包这样的数据
参数4: 文件指针(源数据)
*/
fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp);
//指向位图信息头
fseek(fp, sizeof(BMPFILEHEADER), SEEK_SET);
//读位图信息头信息
fread(&bmpInfo, sizeof(BMPINF), 1, fp);
/* 将原始图片色彩信息存储到Bmpbase */
//获取图向宽、高、每个像素所占位数等信息
bmp.width_x = bmpInfo.bWidth;
bmp.height_y = bmpInfo.bHeight;
bmp.deepth= bmpInfo.bBitCount;
//定义变量,计算图像每行像素所占字节数
int lineByte = (bmp.width_x*bmp.deepth / 8 + 3) / 4 * 4;
//创建一定大小的数组空间
bmp.buf = (unsigned char *)malloc(lineByte*bmp.height_y);
//将颜色数据读入数组空间
fread(bmp.buf, 1, lineByte*bmp.height_y, fp);
fclose(fp);
/*
int i;
for(i = 0; i < lineByte*bmp.height_y; i++)
{
printf("%d", bmp.buf[i]);
}
*/
unsigned char color[lineByte*bmp.height_y/3][3];
int y;
for(y = 0; y < lineByte*bmp.height_y/3; y++)
{
color[y][0] = 0xBF;//B
color[y][1] = 0x6C;//G
color[y][2] = 0x00;//R
}
bmp.buf = &color[0][0];
//创建新图,并写入信息
//调色板时的大小,无则为0,有则另算
int colorTablesize = 0;
//带存储图像每行字节数
int lineByteOut = (bmp.width_x*bmp.deepth / 8 + 3) / 4 * 4;
//打开新文件
if((out = fopen("new.bmp", "wb")) == NULL)
{
printf("Cann't creat new file!\n");
return 0;
}
//申请位图文件头结构变量,填写文件头信息
BMPFILEHEADER fileHead;
fileHead.bType = 0x4D42; //bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bSize = sizeof(BMPFILEHEADER)+sizeof(BMPINF)+colorTablesize + lineByteOut*bmp.height_y;
fileHead.bReserved1 = 0;
fileHead.bReserved2 = 0;
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bOffset = 54 + colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BMPFILEHEADER), 1, fp);
//申请位图文件信息头结构变量,填写文件信息头信息
BMPINF infoHead;
infoHead.bInfoSize = 40;
infoHead.bWidth = bmp.width_x;
infoHead.bHeight = bmp.height_y;
infoHead.bPlanes = 1;
infoHead.bBitCount = bmp.deepth;
infoHead.bCompression = 0;
infoHead.bmpImageSize = lineByteOut*bmp.height_y;
infoHead.bXPelsPerMeter = 0;
infoHead.bYPelsPerMeter = 0;
infoHead.bClrUsed = 0;
infoHead.bClrImportant = 0;
//写位图信息头进文件
fwrite(&infoHead, sizeof(BMPINF), 1, fp);
//写颜色数据进文件
fwrite(bmp.buf, bmp.height_y*lineByteOut, 1, fp);
fclose(out);
return 0;
}
#include <stdlib.h>
#include<malloc.h>
/* 定义BYTE为一个字节的类型 */
typedef unsigned char BYTE;
/* 定义WORD为两个字节的类型 */
typedef unsigned short WORD;
/* 定义DWORD为四个字节的类型 */
typedef unsigned int DWORD;
/*************************** 位图标准信息(54字节,无调色板) ******************************************/
/* 位图文件头 (位图头的字节数 = 位图文件字节数 - 位图图像数据字节数)*/
#pragma pack(2)
typedef struct BMP_FILE_HEADER
{
WORD bType; // 文件标识符
DWORD bSize; // 文件的大小
WORD bReserved1; // 保留值,必须设置为0
WORD bReserved2; // 保留值,必须设置为0
DWORD bOffset; // 文件头的最后到图像数据位开始的偏移量
} BMPFILEHEADER; // 14 字节
//#pragma pack(pop)
/* 位图信息头 */
typedef struct BMP_INFO
{
DWORD bInfoSize; // 信息头的大小
DWORD bWidth; // 图像的宽度
DWORD bHeight; // 图像的高度
WORD bPlanes; // 图像的位面数
WORD bBitCount; // 每个像素的位数
DWORD bCompression; // 压缩类型
DWORD bmpImageSize; // 图像的大小,以字节为单位
DWORD bXPelsPerMeter; // 水平分辨率
DWORD bYPelsPerMeter; // 垂直分辨率
DWORD bClrUsed; // 使用的色彩数
DWORD bClrImportant; // 重要的颜色数
} BMPINF; // 40 字节
typedef struct BMP_COLOR
{
unsigned char* buf;
int width_x; //宽
int height_y; //高
int deepth; //像素位
}Bmpbase;
int main()
{
FILE *fp; // 定义一个文件指针
FILE *out;
BMPFILEHEADER bmpFileHeader; // 定义一个 BMP 文件头
BMPINF bmpInfo; // 定义一个 BMP 位图信息头
Bmpbase bmp; // 定义一个存储颜色信息
/* 打开原始图片 */
if((fp = fopen("photo.bmp", "rb")) == NULL) // fp = 0x00426aa0
{
printf("Cann't open the file!\n");
return 0;
}
//指向文件头
fseek(fp, 0, SEEK_SET);
// 读取文件头信息
/*
参数1: 读出数据的存放缓冲区
参数2: 读取多少个字节的数据
参数3: 读取几包这样的数据
参数4: 文件指针(源数据)
*/
fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp);
//指向位图信息头
fseek(fp, sizeof(BMPFILEHEADER), SEEK_SET);
//读位图信息头信息
fread(&bmpInfo, sizeof(BMPINF), 1, fp);
/* 将原始图片色彩信息存储到Bmpbase */
//获取图向宽、高、每个像素所占位数等信息
bmp.width_x = bmpInfo.bWidth;
bmp.height_y = bmpInfo.bHeight;
bmp.deepth= bmpInfo.bBitCount;
//定义变量,计算图像每行像素所占字节数
int lineByte = (bmp.width_x*bmp.deepth / 8 + 3) / 4 * 4;
//创建一定大小的数组空间
bmp.buf = (unsigned char *)malloc(lineByte*bmp.height_y);
//将颜色数据读入数组空间
fread(bmp.buf, 1, lineByte*bmp.height_y, fp);
fclose(fp);
/*
int i;
for(i = 0; i < lineByte*bmp.height_y; i++)
{
printf("%d", bmp.buf[i]);
}
*/
unsigned char color[lineByte*bmp.height_y/3][3];
int y;
for(y = 0; y < lineByte*bmp.height_y/3; y++)
{
color[y][0] = 0xBF;//B
color[y][1] = 0x6C;//G
color[y][2] = 0x00;//R
}
bmp.buf = &color[0][0];
//创建新图,并写入信息
//调色板时的大小,无则为0,有则另算
int colorTablesize = 0;
//带存储图像每行字节数
int lineByteOut = (bmp.width_x*bmp.deepth / 8 + 3) / 4 * 4;
//打开新文件
if((out = fopen("new.bmp", "wb")) == NULL)
{
printf("Cann't creat new file!\n");
return 0;
}
//申请位图文件头结构变量,填写文件头信息
BMPFILEHEADER fileHead;
fileHead.bType = 0x4D42; //bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bSize = sizeof(BMPFILEHEADER)+sizeof(BMPINF)+colorTablesize + lineByteOut*bmp.height_y;
fileHead.bReserved1 = 0;
fileHead.bReserved2 = 0;
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bOffset = 54 + colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BMPFILEHEADER), 1, fp);
//申请位图文件信息头结构变量,填写文件信息头信息
BMPINF infoHead;
infoHead.bInfoSize = 40;
infoHead.bWidth = bmp.width_x;
infoHead.bHeight = bmp.height_y;
infoHead.bPlanes = 1;
infoHead.bBitCount = bmp.deepth;
infoHead.bCompression = 0;
infoHead.bmpImageSize = lineByteOut*bmp.height_y;
infoHead.bXPelsPerMeter = 0;
infoHead.bYPelsPerMeter = 0;
infoHead.bClrUsed = 0;
infoHead.bClrImportant = 0;
//写位图信息头进文件
fwrite(&infoHead, sizeof(BMPINF), 1, fp);
//写颜色数据进文件
fwrite(bmp.buf, bmp.height_y*lineByteOut, 1, fp);
fclose(out);
return 0;
}