TA的每日心情 | 开心 3 天前 |
---|
签到天数: 7 天 [LV.3]偶尔看看II
二级逆天
- 积分
- 1164
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <string.h>
typedef struct /**** BMP file header structure ****/
{
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} MyBITMAPFILEHEADER;
typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} MyBITMAPINFOHEADER;
typedef unsigned char BYTE;
typedef struct tagRGBQUAD{
BYTE rgbBlue;//蓝色的亮度(值范围为0-255)
BYTE rgbGreen;//绿色的亮度(值范围为0-255)
BYTE rgbRed;//红色的亮度(值范围为0-255)
BYTE rgbReserved;//保留,必须为0
}RGBQUAD;
void SaveBmp(const char *filename,unsigned char *rgbbuf,int width,int height)
{
MyBITMAPFILEHEADER bfh;
MyBITMAPINFOHEADER bih;
/* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
unsigned short bfType=0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = 2+sizeof(MyBITMAPFILEHEADER) + sizeof(MyBITMAPINFOHEADER)+width*height*3;
bfh.bfOffBits = 0x36;
bih.biSize = sizeof(MyBITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 5000;
bih.biYPelsPerMeter = 5000;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Could not write file\n");
return;
}
/*Write headers*/
fwrite(&bfType,sizeof(bfType),1,file);
fwrite(&bfh,sizeof(bfh),1, file);
fwrite(&bih,sizeof(bih),1, file);
fwrite(rgbbuf,width*height*3,1,file);
fclose(file);
}
int main()
{
unsigned char *buffer;
int length,imagesize;
int fd,width,height;
fd = open("k1.bmp", O_RDONLY);
if (fd == -1) {
printf("Unable to open file '\n");
return;
}
imagesize = lseek(fd, 0, SEEK_END);
printf("imgsize=%d \n",imagesize);
lseek(fd, 0, SEEK_SET);
buffer = malloc(imagesize);
if (buffer == NULL) {
printf("Unable to allocate memory for MJPEG image\n");
return;
}
read(fd,buffer, imagesize);
close(fd);
SaveBmp("test.bmp", buffer+0x36 ,640, 360);
return 0;
}
linux 下编译命令:
gcc bmp.c -o bmp
运行:
./bmp (运行前先将 k1.bmp copy到运行目录下, 运行后可发现有test.bmp 文件生成 ) |
|