c++读取BMP文件(24位)
2008-01-22 14:37
405 查看
#include "StdAfx.h"
#include "stdio.h"
#include "memory.h"
#include "stdlib.h"
#define PIXPLINE 320
typedef struct tagRGBQUAD{ //定义每个像素的数据类型
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
} RGBQUAD;
int bmp_read(unsigned char *image, int xsize, int ysize, char *filename){
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) {
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
void clonebmp(unsigned char *image,int xsize,int ysize)
{
bmp_read(image, xsize, ysize, ".//1");
bmp_write(image, xsize, ysize, ".//2");
}
/**//****************************************************************************
* 名称:youwritetobmp()
* 功能:写入bmp文件
* 入口参数:RGBQUAD *pixarr ---- 要写入的像素数组指针, int xsize ---- 图像宽度, int ysize ---- 图像高度, char *filename --图像名称
* 出口参数:无
* 返回值:-1:错误 ;0:正确
****************************************************************************/
int youwritetobmp(RGBQUAD *pixarr, int xsize, int ysize, char *filename){
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
int i;
int j;
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width;
if(!(xsize%4)) width=xsize;
else width= xsize+(4-xsize%4); //如不是4的倍数,则转换成4的倍数
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
RGBQUAD zero = {0,0,0}; //不足字节,用zero填充
for(j=0;j<ysize;j++){
if(!(xsize%4)){
for(i=0;i<xsize;i++){
fwrite(pixarr+i, sizeof(RGBQUAD),1, fp);
}
}
else
{
for(i=0;i<xsize;i++){
fwrite(pixarr+i, sizeof(RGBQUAD),1, fp);
}
for(i=xsize;i<xsize+(4-xsize%4);i++){
fwrite(&zero, sizeof(RGBQUAD),1, fp);
}
}
}
fclose(fp);
return 0;
}
int main()
{
unsigned char *image;
int xsize = 320;
int ysize = 163;
RGBQUAD pixarray[PIXPLINE]; //一行像素值数组
image = (unsigned char *)malloc((size_t)xsize * ysize * 3);
if (image == NULL)
return -1;
clonebmp(image,xsize,ysize); //功能一:实现一副图像的拷贝
//youwritetobmp(pixarray,xsize,ysize,"yourimage");//功能二:实现像素的精确绘制,像素值在pixarray数组中
free(image);
return 0;
}
#include "stdio.h"
#include "memory.h"
#include "stdlib.h"
#define PIXPLINE 320
typedef struct tagRGBQUAD{ //定义每个像素的数据类型
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
} RGBQUAD;
int bmp_read(unsigned char *image, int xsize, int ysize, char *filename){
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) {
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
void clonebmp(unsigned char *image,int xsize,int ysize)
{
bmp_read(image, xsize, ysize, ".//1");
bmp_write(image, xsize, ysize, ".//2");
}
/**//****************************************************************************
* 名称:youwritetobmp()
* 功能:写入bmp文件
* 入口参数:RGBQUAD *pixarr ---- 要写入的像素数组指针, int xsize ---- 图像宽度, int ysize ---- 图像高度, char *filename --图像名称
* 出口参数:无
* 返回值:-1:错误 ;0:正确
****************************************************************************/
int youwritetobmp(RGBQUAD *pixarr, int xsize, int ysize, char *filename){
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
int i;
int j;
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width;
if(!(xsize%4)) width=xsize;
else width= xsize+(4-xsize%4); //如不是4的倍数,则转换成4的倍数
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
RGBQUAD zero = {0,0,0}; //不足字节,用zero填充
for(j=0;j<ysize;j++){
if(!(xsize%4)){
for(i=0;i<xsize;i++){
fwrite(pixarr+i, sizeof(RGBQUAD),1, fp);
}
}
else
{
for(i=0;i<xsize;i++){
fwrite(pixarr+i, sizeof(RGBQUAD),1, fp);
}
for(i=xsize;i<xsize+(4-xsize%4);i++){
fwrite(&zero, sizeof(RGBQUAD),1, fp);
}
}
}
fclose(fp);
return 0;
}
int main()
{
unsigned char *image;
int xsize = 320;
int ysize = 163;
RGBQUAD pixarray[PIXPLINE]; //一行像素值数组
image = (unsigned char *)malloc((size_t)xsize * ysize * 3);
if (image == NULL)
return -1;
clonebmp(image,xsize,ysize); //功能一:实现一副图像的拷贝
//youwritetobmp(pixarray,xsize,ysize,"yourimage");//功能二:实现像素的精确绘制,像素值在pixarray数组中
free(image);
return 0;
}
相关文章推荐
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- bmp位图文件:读取、写入、24位真彩转8位灰度、灰度图的二值化
- opengl读取24位BMP文件为纹理并处理黑色背景为透明
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- c++读取24位BMP图像并处理
- C++读取BMP文件
- C++读取、旋转和保存bmp图像文件编程实现
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 从文件夹中读取子文件夹内带有指定后缀如.bmp文件的C++实现,并读取子文件夹后缀数字
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现(转载)
- C++读取、旋转和保存bmp图像文件编程实现
- C++读取、旋转和保存bmp图像文件编程实现
- C++读取与保存bmp图片文件编程实现
- C++ 读取配置文件的 ofstream和ifstream详细用法
- [转载]VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等
- C++ 读取文件内容至当前文件长度的字符串中
- C/C++读取文件
- 使用ifstream和getline读取文件内容[c++]
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组