纯C语言实现解析单色位图文件获取颜色值

在绘制单色位图时,需要考虑字节对齐问题。字节对齐是指数据存储在内存中时按照多字节对齐的原则进行存放,以提高访问效率。

为了实现这个函数,可以按照以下步骤进行:

  1. 计算每行像素数据的实际占用字节数:每个像素占用1个BIT位,即1/8个字节。 

  2. 计算每行像素数据的补齐字节数:为了满足字节对齐要求,需要计算每行像素数据需要补齐的字节数。 

  3. 计算每行像素数据所需的总字节数:包括实际占用字节数和补齐字节数。 总字节数 = 实际占用字节数 + 补齐字节数

  4. 遍历行数和列数,根据索引计算出当前像素在pData数组中的位置: 像素位置 = 行索引 * 总字节数

  5. 根据列索引计算当前像素所在的BIT位在一个BYTE中的偏移量: 偏移量 = 7 - (列索引 % 8)

  6. 根据位运算的方式,将当前像素的值写入pData中的相应位置: if(pData[像素位置]  & 偏移量);

注意一点:标准的单色位图文件遵循从下至上、从左至右的方式扫描并存储

完整利用纯C语言解析单色位图文件获取颜色值的代码实现如下:

typedef char			int8_t;
typedef unsigned char	uint8_t;
typedef unsigned short	uint16_t;
typedef unsigned int	uint32_t;
typedef int				int32_t;

#pragma pack(push, 1) // 字节对齐设置为1字节
typedef struct {
    uint16_t bfType;
    uint32_t bfSize;
    uint16_t bfReserved1;
    uint16_t bfReserved2;
    uint32_t bfOffBits;
} BMPFileHeader;

typedef struct {
    uint32_t biSize;
    int32_t biWidth;
    int32_t biHeight;
    uint16_t biPlanes;
    uint16_t biBitCount;
    uint32_t biCompression;
    uint32_t biSizeImage;
    int32_t biXPelsPerMeter;
    int32_t biYPelsPerMeter;
    uint32_t biClrUsed;
    uint32_t biClrImportant;
} BMPInfoHeader;

#pragma pack(pop)

// 提取单色位图的颜色
void DrawBitmap8(CDC *pDC, const uint32_t x, const uint32_t y, const uint32_t w, const uint32_t h, const uint8_t *pData) 
{
	uint32_t	index = 0, bitOffset = 0, pixelByte = 0, pixelValue = 0;
	uint32_t	bytesPerLine = 0;
	uint32_t	row = 0, col = 0, startX = 0, startY = 0;

	// 单色位图对齐计算方法
	bytesPerLine = (w + 7) / 8;
	bytesPerLine += (bytesPerLine % sizeof(size_t)) ? sizeof(size_t) - bytesPerLine % sizeof(size_t) : 0;

	for (row = 0; row < h; row++)		// 先按行扫描
	{
        for (col = 0; col < w; col++)	// 再按列扫描
		{
            // 获取当前像素在 pData 中的索引
			index = bytesPerLine * row + col / 8;

            // 获取当前像素在字节中的位偏移
            bitOffset = 7 - (col % 8);

            // 获取当前像素值(字节)
            pixelByte = pData[index];
            
            // 获取当前像素值的位状态
            pixelValue = (pixelByte >> bitOffset) & 1;
            
			startX = x + col;
			startY = y + h - 1 - row;	// 单色位图文件是从下向上再按行扫描

            // 绘制像素
			if(!pixelValue)	// 黑色
				pDC->SetPixel(startX, startY, RGB(0, 0, 0));
			else
				pDC->SetPixel(startX, startY, RGB(0, 255, 0));
        }
    }
}

// 纯C语言解析单色BMP文件并绘制在xy位置
int32_t loadBitmap8(const int8_t *pFile, CDC *pDC, const uint32_t x, const uint32_t y)
{
	BMPFileHeader	fileHeader; 
	BMPInfoHeader	infoHeader;
	uint32_t		bytesPerLine = 0;
	uint8_t			*pixelData = NULL;
	FILE			*file = NULL;

	file = fopen(pFile, "rb");
	if (file == NULL) {
		printf("无法打开位图文件\n");
		return -1; 
	}

	fread(&fileHeader, sizeof(BMPFileHeader), 1, file);
	fread(&infoHeader, sizeof(BMPInfoHeader), 1, file);
	
	// 检查位图文件是否是单色位图
	if (infoHeader.biBitCount != 1) {
		printf("不支持的位图类型\n");
		fclose(file);
		return -1; 
	}
	
	// 根据位图信息计算行字节数和补齐字节数
	bytesPerLine = (infoHeader.biWidth + 7) / 8;
	bytesPerLine += (bytesPerLine % sizeof(size_t)) ? sizeof(size_t) - bytesPerLine % sizeof(size_t) : 0;
	
	// 分配像素数据内存
	pixelData = (uint8_t *)malloc(bytesPerLine * infoHeader.biHeight);
	if (pixelData == NULL) {
		printf("内存分配失败\n");
		fclose(file);
		return -1; 
	}
	
	// 读取像素数据
	fseek(file, fileHeader.bfOffBits, SEEK_SET);
	fread(pixelData, bytesPerLine * infoHeader.biHeight, 1, file);
	fclose(file);
	
	// 从x,y点开始绘制w,h的单色位图
	DrawBitmap8(pDC, x, y, infoHeader.biWidth, infoHeader.biHeight, pixelData);
	
	// 绘制完毕释放内存
    free(pixelData);
	pixelData = NULL;

    return 0;
}

运行效果如下:

注意:CDC这个类为MFC专用的绘图函数,请自行实现SetPixel这个函数即可,如有需要完整工程在评论区留邮箱即可!

猜你喜欢

转载自blog.csdn.net/wangningyu/article/details/134066290