C语言创建24位真彩色位图

本文档提供了使用C语言创建24位真彩色位图的方法,包括设置BITMAPINFOHEADER和BITMAPFILEHEADER结构体,以及位图数据的填充规则。源代码示例展示了如何生成一个640x480像素的位图文件。
摘要由CSDN通过智能技术生成
/* 功能: 创建一幅24位真彩色位图
** 作者: mayadong7349
** 参考: MSDN(Visual Studio 2005)、(百度百科:bmp) http://baike.baidu.com/view/7671.htm#2
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#define BMP_W 640L
#define BMP_H 480L

void SetBmpInfoHeader(PBITMAPINFOHEADER pbmpih)
{
	/* 14~17: 本结构体所占字节数, 固定值40 
	MSDN: Specifies the number of bytes required by the structure. 
	*/
	pbmpih->biSize          = 40;
	/* 18~21: 位图的宽度(以像素pixel为单位) 
	MSDN: Specifies the width of the bitmap, in pixels. 
	Windows 98/Me, Windows 2000/XP: If biCompression is BI_JPEG or BI_PNG, the biWidth member specifies 
	                                the width of the decompressed JPEG or PNG image file, respectively. 
	*/
	pbmpih->biWidth         = BMP_W;  
	/* 22~25: 位图的高度 
	MSDN: Specifies the height of the bitmap, in pixels. If biHeight is positive, the bitmap is a 
	bottom-up DIB and its origin is the lower-left corner. If biHeight is negative, the bitmap is 
	a top-down DIB and its origin is the upper-left corner. 
	If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or 
	BI_BITFIELDS. Top-down DIBs cannot be compressed. 
	Windows 98/Me, Windows 2000/XP: If biCompression is BI_JPEG or BI_PNG, the biHeight member specifies 
	                                the height of the decompressed JPEG or PNG image file, respectively. 
	*/
	pbmpih->biHeight        = BMP_H;  
	/* 26~27: 目标设备的级别, 固定值:1 
	MSDN: Specifies the number of planes for the target device. This value must be set to 1*/
	pbmpih->biPlanes        = 1;   
	/* 28~29: 一个像素所占bit数, 可以是:1(双色)、4(16色)、8(256色)
	、16(65536色)、24(真彩色2^24种颜色)、32(真彩色2^32种颜色),具体参阅MSDN。
	对于24位真彩色位图, 一个像素占用三个字节,即24 bits 
	MSDN: Specifies the number of bits-per-pixel. The biBitCount member of the BITMAPINFOHEADER
	      structure determines the number of bits that define each pixel and the maximum number 
		  of colors in the bitmap. This member must be one of the following values. 
    0: Windows 98/Me, Windows 2000/XP: The number of bits-per-pixel is specified or is implied 
	   by the JPEG or PNG format.  
    1: The bitmap is monochrome, and the bmiColors member of BITMAPINFO contains two 
好的,我可以帮你完成这个问题。首先,你需要了解真彩色位图的格式,这里我简单介绍一下: 真彩色位图是一种每个像素占据3个字节的图像格式,其中每个字节分别代表红、绿、蓝三个通道的亮度。因此,一幅真彩色位图的文件大小为图像宽度 × 图像高度 × 3。 接下来,我提供一个简单的C语言程序,可以在命令行输入参数,完成两幅真彩色位图图像的混合运算,并存储混合后的结果到新文件。程序中使用了Windows API函数,需要在Windows系统下编译运行。 ```c #include <windows.h> #include <stdio.h> int main(int argc, char *argv[]) { if (argc != 4) { printf("Usage: %s [input1.bmp] [input2.bmp] [output.bmp]\n", argv[0]); return 1; } // 读入输入图像1 HANDLE hFile1 = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile1 == INVALID_HANDLE_VALUE) { printf("Error: can't open file %s\n", argv[1]); return 1; } HANDLE hMapping1 = CreateFileMapping(hFile1, NULL, PAGE_READONLY, 0, 0, NULL); LPVOID lpFile1 = MapViewOfFile(hMapping1, FILE_MAP_READ, 0, 0, 0); BITMAPFILEHEADER *pBmpFileHeader1 = (BITMAPFILEHEADER *)lpFile1; BITMAPINFOHEADER *pBmpInfoHeader1 = (BITMAPINFOHEADER *)((char *)lpFile1 + sizeof(BITMAPFILEHEADER)); BYTE *pImage1 = (BYTE *)((char *)lpFile1 + pBmpFileHeader1->bfOffBits); // 读入输入图像2 HANDLE hFile2 = CreateFile(argv[2], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile2 == INVALID_HANDLE_VALUE) { printf("Error: can't open file %s\n", argv[2]); return 1; } HANDLE hMapping2 = CreateFileMapping(hFile2, NULL, PAGE_READONLY, 0, 0, NULL); LPVOID lpFile2 = MapViewOfFile(hMapping2, FILE_MAP_READ, 0, 0, 0); BITMAPFILEHEADER *pBmpFileHeader2 = (BITMAPFILEHEADER *)lpFile2; BITMAPINFOHEADER *pBmpInfoHeader2 = (BITMAPINFOHEADER *)((char *)lpFile2 + sizeof(BITMAPFILEHEADER)); BYTE *pImage2 = (BYTE *)((char *)lpFile2 + pBmpFileHeader2->bfOffBits); // 创建输出图像 HANDLE hFileOut = CreateFile(argv[3], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFileOut == INVALID_HANDLE_VALUE) { printf("Error: can't create file %s\n", argv[3]); return 1; } DWORD dwImageSize = pBmpInfoHeader1->biWidth * pBmpInfoHeader1->biHeight * 3; DWORD dwFileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwImageSize; BYTE *pImageOut = (BYTE *)malloc(dwImageSize); memset(pImageOut, 0, dwImageSize); BITMAPFILEHEADER bmpFileHeaderOut = { 'B', 'M', dwFileSize, 0, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) }; BITMAPINFOHEADER bmpInfoHeaderOut = *pBmpInfoHeader1; bmpInfoHeaderOut.biClrUsed = 0; bmpInfoHeaderOut.biSizeImage = dwImageSize; DWORD dwBytesWritten = 0; WriteFile(hFileOut, &bmpFileHeaderOut, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL); WriteFile(hFileOut, &bmpInfoHeaderOut, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL); // 图像混合 for (DWORD i = 0; i < dwImageSize; i++) { pImageOut[i] = (BYTE)((float)pImage1[i] * 0.5 + (float)pImage2[i] * 0.5); } // 写入输出图像 WriteFile(hFileOut, pImageOut, dwImageSize, &dwBytesWritten, NULL); // 释放资源 UnmapViewOfFile(lpFile1); CloseHandle(hMapping1); CloseHandle(hFile1); UnmapViewOfFile(lpFile2); CloseHandle(hMapping2); CloseHandle(hFile2); CloseHandle(hFileOut); free(pImageOut); printf("Image blending completed!\n"); return 0; } ``` 程序中使用了三个命令行参数,分别是输入图像1的文件名、输入图像2的文件名和输出图像的文件名。程序首先检查参数数量是否正确,然后打开输入图像1和输入图像2,读入图像数据。接着创建输出图像,进行图像混合,并将结果写入输出文件。最后释放资源,打印完成信息。 请注意,这只是一个简单的示例程序,可能需要根据具体的需求进行修改和优化。另外,由于涉及到文件读写和内存操作,程序可能存在一些潜在的风险,需要谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值