c语言生成Bmp图像

代码如下:

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#pragma pack(2)//必须得写,否则sizeof得不到正确的结果

typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;
typedef long    LONG;
typedef struct {
	WORD    bfType;
	DWORD   bfSize;
	WORD    bfReserved1;
	WORD    bfReserved2;
	DWORD   bfOffBits;
} BITMAPFILEHEADER;

typedef struct {
	DWORD      biSize;
	LONG       biWidth;
	LONG       biHeight;
	WORD       biPlanes;
	WORD       biBitCount;
	DWORD      biCompression;
	DWORD      biSizeImage;
	LONG       biXPelsPerMeter;
	LONG       biYPelsPerMeter;
	DWORD      biClrUsed;
	DWORD      biClrImportant;
} BITMAPINFOHEADER;

void saveBitmapData(int w, int h, unsigned char* pData, int nDatasize)
{

	// Define BMP Size
	const int height = w;
	const int width = h;
	const int size = nDatasize;
	double x, y;
	int index;

	// Part.1 Create Bitmap File Header
	BITMAPFILEHEADER fileHeader;

	fileHeader.bfType = 0x4D42;
	fileHeader.bfReserved1 = 0;
	fileHeader.bfReserved2 = 0;
	fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
	fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	// Part.2 Create Bitmap Info Header
	BITMAPINFOHEADER bitmapHeader = { 0 };

	bitmapHeader.biSize = sizeof(BITMAPINFOHEADER);
	bitmapHeader.biHeight = -height;
	bitmapHeader.biWidth = width;
	bitmapHeader.biPlanes = 1;
	bitmapHeader.biBitCount = 32;
	bitmapHeader.biSizeImage = size;
	bitmapHeader.biCompression = 0; //BI_RGB


	// Write to file
	FILE* output = fopen("output.bmp", "wb");

	if (output == NULL)
	{
		printf("Cannot open file!\n");
	}
	else
	{
		fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, output);
		fwrite(&bitmapHeader, sizeof(BITMAPINFOHEADER), 1, output);
		fwrite(pData, size, 1, output);
		fclose(output);
	}
}

void saveBitmap()
{
	// Define BMP Size
	const int height = 600;
	const int width = 800;
	const int size = height * width * 3;
	double x, y;
	int index;

	// Part.1 Create Bitmap File Header
	BITMAPFILEHEADER fileHeader;

	fileHeader.bfType = 0x4D42;
	fileHeader.bfReserved1 = 0;
	fileHeader.bfReserved2 = 0;
	fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
	fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	// Part.2 Create Bitmap Info Header
	BITMAPINFOHEADER bitmapHeader = { 0 };

	bitmapHeader.biSize = sizeof(BITMAPINFOHEADER);
	bitmapHeader.biHeight = height;
	bitmapHeader.biWidth = width;
	bitmapHeader.biPlanes = 3;
	bitmapHeader.biBitCount = 24;
	bitmapHeader.biSizeImage = size;
	bitmapHeader.biCompression = 0; //BI_RGB

	// Part.3 Create Data
	BYTE* bits = (BYTE*)malloc(size);

	// Clear
	memset(bits, 0xFF, size);

	// Sin Graph
	for (x = 0; x < 800; x += 0.5)
	{
		y = sin(x / 100.0) * 200 + 300;
		index = (int)y * 800 * 3 + (int)x * 3;

		bits[index + 0] = 255; // Blue
		bits[index + 1] = 0;   // Green
		bits[index + 2] = 0;   // Red
	}

	// Write to file
	FILE* output = fopen("output.bmp", "wb");

	if (output == NULL)
	{
		printf("Cannot open file!\n");
	}
	else
	{
		fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, output);
		fwrite(&bitmapHeader, sizeof(BITMAPINFOHEADER), 1, output);
		fwrite(bits, size, 1, output);
		fclose(output);
	}
}

int main(int argc, const char** argv)
{
	saveBitmap();
	return 0;
}

生成consoleApp输出图像如下:

 

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言可以通过以下步骤实现对BMP图像添加高斯噪声: 1.首先,需要引入一个随机数生成器函数来生成高斯噪声。可以使用标准C库中的rand()函数来生成一个0到RAND_MAX(通常是32767)之间的随机整数。可以将其归一化为0到1之间的浮点数,并使用均值为0,标准差为sigma的高斯分布函数生成高斯噪声值。 float gaussNoise(float sigma) { float u1, u2, v1, v2, s; do { u1 = rand() / ((float)RAND_MAX); u2 = rand() / ((float)RAND_MAX); v1 = 2 * u1 - 1; v2 = 2 * u2 - 1; s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); float mul = sqrt(-2.0 * log(s) / s); return v2 * mul * sigma; } 2.读取BMP图像的像素数据。可以使用图像处理库,如OpenCV来读取BMP图像的像素数据。 unsigned char* imageData; // 存储图像像素数据的指针 int width, height; // 图像的宽度和高度 int channels; // 图像的通道数 // 使用OpenCV读取BMP图像,将像素数据存储在imageData指针中 // 获取图像的宽度、高度和通道数 3.遍历图像的每个像素,并为每个像素添加高斯噪声。 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { for (int c = 0; c < channels; c++) { float noise = gaussNoise(sigma); // 根据设定的标准差生成高斯噪声 imageData[(i * width + j) * channels + c] += (unsigned char)noise; } } } 4.将处理后的像素数据保存为新的BMP图像。同样可以使用图像处理库来保存处理后的像素数据为BMP图像。 // 使用OpenCV保存像素数据为BMP图像 // 释放imageData的内存 这样,通过以上步骤,利用C语言实现了对BMP图像添加高斯噪声的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_21239475

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值