📄 savebmpfile.cpp
字号:
// bmpmaker.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#if 0 //already defined in <wingdi.h>
#ifndef DWORD
#define DWORD int
#endif
#ifndef BYTE
#define BYTE unsigned char
#endif
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER { /* bmfh */
short bfType;
DWORD bfSize;
short bfReserved1;
short bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER { /* bmih */
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
short biPlanes;
short biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct tagRGBQUAD { /* rgbq */
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
//typedef struct tagBITMAPINFO { /* bmi */
// BITMAPINFOHEADER bmiHeader;
// RGBQUAD bmiColors[1];
//} BITMAPINFO;
#pragma pack()
#endif //already defined in <wingdi.h>
int savetobmpfile(unsigned char *buf,
unsigned int len,
unsigned int width,
unsigned int height,
unsigned int bpp,
const char *path)
{
BITMAPFILEHEADER h1;
BITMAPINFOHEADER h2;
RGBQUAD rgb;
int i;
unsigned int t;
unsigned char *buf_file = new unsigned char[len];
memcpy((void *) &h1.bfType, "BM", 2);
h1.bfSize = sizeof(h1) + sizeof(h2) + sizeof(rgb) * (1 << bpp) + width * height;
h1.bfReserved1 = 0;
h1.bfReserved2 = 0;
h1.bfOffBits = sizeof(h1) + sizeof(h2) + sizeof(rgb) * (1 << bpp);
h2.biSize = sizeof(h2);
h2.biWidth = width;
h2.biHeight = height;
h2.biPlanes = 1;
h2.biBitCount = bpp;
h2.biCompression = 0;
h2.biSizeImage = (width * height) * ((bpp + 7) / 8);
h2.biXPelsPerMeter = 0;
h2.biYPelsPerMeter = 0;
h2.biClrUsed = 0;
h2.biClrImportant = 0;
FILE *fp;
fp = fopen(path, "wb");
if (!fp)
{
return -1;
}
fwrite((char *) (&h1), sizeof(h1), 1, fp);
fwrite(&h2, sizeof(h2), 1, fp);
/* FIXME: how to fill the palette if bpp > 8 ? */
for (i = 0; i < (1 << bpp); i++)
{
rgb.rgbRed = i;
rgb.rgbBlue = i;
rgb.rgbGreen = i;
rgb.rgbReserved = 0;
fwrite(&rgb, sizeof(rgb), 1, fp);
}
for (t=0; t<480;t++)
{
for (i=0;i<640;i++)
{
buf_file[t*640+i] = (unsigned char)buf[(480-t-1)*640+i];
}
}
fwrite(buf_file, len, 1, fp);
delete(buf_file);
fclose(fp);
return 0;
}
int readfrombmpfile(unsigned char *buf,
const char *path)
{
int t,i;
FILE *fp;
unsigned char *buf_file = new unsigned char[640*480];
fp = fopen(path, "r");
if (!fp)
{
return -1;
}
fseek(fp,sizeof(BITMAPFILEHEADER),SEEK_SET);
fseek(fp,sizeof(BITMAPINFOHEADER),SEEK_CUR);
fseek(fp,(1<<8)*sizeof(RGBQUAD),SEEK_CUR);
fread(buf_file,sizeof(unsigned char),640*480,fp);
for (t=0; t<480;t++)
{
for (i=0;i<640;i++)
{
buf[t*640+i] = (unsigned char)buf_file[(480-t-1)*640+i];
}
}
fclose(fp);
delete(buf_file);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -