bmpfile

news/2024/7/15 17:28:13 标签: c语言, c++, 图论

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include “bmpfile.h”

#pragma pack(2)
typedef struct {
unsigned char bfType[2];
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BMPFileHeader;

#pragma pack(2)
typedef struct {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BMPInfoHeader;

typedef struct {
BMPFileHeader fileHeader;
BMPInfoHeader infoHeader;
int stride;
unsigned char *imageData;
} BMPImage;

void* bmpfile_create(int w, int h) {
BMPImage *bmpImage = malloc(sizeof(BMPImage));
if ( !bmpImage->imageData )
printf(" BMP image creation failed\n ");
bmpImage->fileHeader.bfType[0] = ‘B’;
bmpImage->fileHeader.bfType[1] = ‘M’;
bmpImage->fileHeader.bfSize = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + w * h * 3;
bmpImage->fileHeader.bfReserved1 = 0;
bmpImage->fileHeader.bfReserved2 = 0;
bmpImage->fileHeader.bfOffBits = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
bmpImage->infoHeader.biSize = sizeof(BMPInfoHeader);
bmpImage->infoHeader.biWidth = w;
bmpImage->infoHeader.biHeight = h;
bmpImage->infoHeader.biPlanes = 1;
bmpImage->infoHeader.biBitCount = 24;
bmpImage->infoHeader.biCompression = 0;
bmpImage->infoHeader.biSizeImage = w * h * 3;
bmpImage->infoHeader.biXPelsPerMeter = 0;
bmpImage->infoHeader.biYPelsPerMeter = 0;
bmpImage->infoHeader.biClrUsed = 0;
bmpImage->infoHeader.biClrImportant = 0;
bmpImage->stride = (((bmpImage->infoHeader.biWidth * bmpImage->infoHeader.biBitCount) + 31) / 32) * 4;
bmpImage->imageData = malloc(bmpImage->stride * bmpImage->infoHeader.biHeight * 3);
return bmpImage;
}

void bmpfile_destroy(void *ctx) {
BMPImage bmpImage = (BMPImage)ctx;
free(bmpImage->imageData);
free(bmpImage);
}

void* bmpfile_load(char *file) {
FILE *fp = fopen(file, “rb”);
if (fp == NULL) {
return NULL;
}
BMPImage *bmpImage = malloc(sizeof(BMPImage));
fread(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fread(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
bmpImage->imageData = malloc(bmpImage->infoHeader.biSizeImage);
fseek(fp, bmpImage->fileHeader.bfOffBits, SEEK_SET);
fread(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return bmpImage;
}

int bmpfile_save(void *ctx, char *file) {
BMPImage bmpImage = (BMPImage)ctx;
FILE *fp = fopen(file, “wb”);
if (fp == NULL) {
return -1;
}
fwrite(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fwrite(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
fwrite(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return 0;
}

void bmpfile_pixel(void *ctx, int x, int y, int c) {
if (ctx == NULL)
return;
BMPImage bmpImage = (BMPImage)ctx;
if ( x < 0 || x >= bmpImage->infoHeader.biWidth || y < 0 || y >= bmpImage->infoHeader.biHeight )
return;
int index = (bmpImage->infoHeader.biHeight - y - 1) * bmpImage->stride + x;// bmpImage->infoHeader.biWidth + x;//按照反的方向存储(从左向右,从下向上)图片的最左下角的像素是首先被存储在bmp文件中的,所以这里代码的最后是加+x
bmpImage->imageData[index * 3 + 0] = c & 0xFF;
bmpImage->imageData[index * 3 + 1] = (c >> 8) & 0xFF;
bmpImage->imageData[index * 3 + 2] = (c >> 16) & 0xFF;
}

int main() {
srand((unsigned)time(NULL));

int width = 400;
int height = 300;
void *ctx = bmpfile_create(width, height);

int i;
for (i = 0; i < 1000; i++) {
    int x = rand() % width;
    int y = rand() % height;
    int r = rand() % 256;
    int g = rand() % 256;
    int b = rand() % 256;
    int color = r | (g << 8) | (b << 16);
    bmpfile_pixel(ctx, x, y, color);
}

bmpfile_save(ctx, "test.bmp");
bmpfile_destroy(ctx);

return 0;

}


http://www.niftyadmin.cn/n/287200.html

相关文章

Chrome 113 发布,默认启用 WebGPU

经过四周时间开发&#xff0c;Chrome 113 稳定版正式推出&#xff0c;新版本包括用于视频电话会议的 AV1 视频编码、WebGPU&#xff0c;以及其他增强功能。 默认启用 WebGPU WebGPU 是用于 Web 的新 API&#xff0c;它使用了现代的硬件功能&#xff0c;允许在 GPU 上进行渲染和…

尚融宝29-提现和还款

目录 一、提现 &#xff08;一&#xff09;需求 &#xff08;二&#xff09;前端 &#xff08;三&#xff09;后端 1、提现接口 2、回调接口 二、还款 &#xff08;一&#xff09;需求 &#xff08;二&#xff09;前端 &#xff08;三&#xff09;后端 1、还款接口 …

19. Unity - 2D游戏开发小记02 --- 伪透视图、2D物体碰撞、瓦片地图碰撞、素材缩放平铺

1. 伪视图 在2D游戏开发当中,当角色移动时,会发生物体与物体之间的前后遮挡。2D视图中的前后关系是由 Y 轴决定,y 值越小物体越靠前。unity的渲染应开启根据 y 值的大小进行渲染才能保证正确的遮挡效果,在菜单栏Editor–>project setting --> Graphic中按照下图方式…

Ubuntu连接Xshell

Ubuntu连接Xshell 1、安装ssh&#xff0c;开启服务 1、安装ssh sudo apt-get install openssl-server 2、启动ssh服务 /etc/init.d/ssh start 3、修改文件&#xff0c;允许远程登陆 sudo vi /etc/ssh/sshd_config PermitRootLogin prohibit-password #默认为禁止登录 PermitR…

C语言循环语句

C语言是一种高级编程语言&#xff0c;广泛应用于系统开发、游戏开发、网络编程和嵌入式系统等多个领域。循环语句是C语言中非常基础和重要的知识点之一&#xff0c;可以实现程序的循环执行某些操作&#xff0c;包括处理很多特定的数据&#xff0c;直到指定条件以满足退出执行的…

CTFshow misc PNG隐写入门赛

目录 One PieNG 1 One PieNG 2 One PieNG 3 One PieNG 4 One PieNG 5 One PieNG 6 One PieNG 7 One PieNG 8 One PieNG 9 One PieNG 10 One PieNG 11 One PieNG 12 One PieNG 13 One PieNG 14 One PieNG 15 One PieNG 16 One PieNG 17 One PieNG 18 One Pie…

kafka topic 发送消息到 hbase

目录 一、配置kafka信息 二、配置hbase信息&#xff0c;连接hbase数据库 需要先在hbase创建对应的命令空间和table Put对象&#xff0c;按照不同需求&#xff0c;从文件中截取对应字段 三、完整代码 一、配置kafka信息 Properties properties new Properties();propertie…

压测工具Jmeter

腾讯下载地址&#xff1a;https://mirrors.cloud.tencent.com/apache/jmeter/ index of /apache/jmeter/binaries/xxx.zip 这是windows系统使用 index of /apache/jmeter/binaries/xxx.tgz 这是linux系统使用 安装需要java环境 进到解压文件的bin目录下&#xff0c;双击 jmeter…