跳到正文

科技·工程

兔篇储力

ljy05约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

注:仅支持 bmp

image.h

C++
#define LL long long
#define USH unsigned short
#define UI unsigned int

#pragma pack(1)
struct Color{
	BYTE b,g,r;
	Color():b(0),g(0),r(0){}
	Color(BYTE _b,BYTE _g,BYTE _r):b(_b),g(_g),r(_r){}
	void print()const{
		printf("%d %d %d",int(b),int(g),int(r));
	}
	int operator==(const Color x)const{
		return x.b==b&&x.g==g&&x.r==r;
	}
};
struct BITMAPFILEH{
	USH bfType;
	UI bfSize;
	USH bfReserved1;
	USH bfReserved2;
	UI bfOffBits;
};
struct BITMAPINFOH{
	UI biSize;
	int biWidth;
	int biHeight;
	USH biPlanes;
	USH biBitCount;
	UI biCompression;
	UI biSizeImage;
	int biXPelsPerMeter;
	int biYPelsPerMeter;
	UI biClrUsed;
	UI biClrImportant;
};
#pragma pack()

struct image{
	Color col[4020][4020];
	int h,w;
	Color get(int x,int y){
		if(x>=1&&x<=h&&y>=1&&y<=w) return col[x][y];
		return col[(x-1)%h+1][(y-1)%w+1];
//		if(x<1||x>h||y<1||y>w) return Color(255,255,255);
//		return col[x][y];
	}
	void set(int x,int y,Color z){
		if(x<1||x>h||y<1||y>w) return ;
		col[x][y]=z;
	}
};

void savimg(image &si,std::string lj){
	FILE *otp=fopen(lj.c_str(),"wb");
	int widbt=(si.w*3+3)/4*4;
	BYTE zero=0;
	BITMAPFILEH tmp;
	tmp.bfType=0x4d42;
	tmp.bfSize=54+widbt*si.h;
	tmp.bfReserved1=tmp.bfReserved2=0;
	tmp.bfOffBits=0x36;
	fwrite(&tmp,sizeof(tmp),1,otp);
	BITMAPINFOH tm2;
	tm2.biSize=0x28;
	tm2.biWidth=si.w;
	tm2.biHeight=si.h;
	tm2.biPlanes=1;
	tm2.biBitCount=0x18;
	tm2.biCompression=0;
	tm2.biSizeImage=0;
	tm2.biXPelsPerMeter=0;
	tm2.biYPelsPerMeter=0;
	tm2.biClrUsed=0;
	tm2.biClrImportant=0;
	fwrite(&tm2,sizeof(tm2),1,otp);
	for(int i=si.h;i>0;i--){
		for(int j=1;j<=si.w;j++) fwrite(&si.col[i][j],sizeof(Color),1,otp);
		for(int j=si.w*3+1;j<=widbt;j++) fwrite(&zero,sizeof(zero),1,otp);
	}
	fclose(otp);
	return ;
}
int loadimg(image &si,std::string lj){
	memset(&si,0,sizeof(si));
	FILE *inp=fopen(lj.c_str(),"rb");
	int widbt=0;
	BYTE zero=0;
	BITMAPFILEH tmp;
	fread(&tmp,sizeof(tmp),1,inp);
	BITMAPINFOH tm2;
	fread(&tm2,sizeof(tm2),1,inp);
	si.w=tm2.biWidth;
	si.h=tm2.biHeight;
	widbt=(si.w*3+3)/4*4;
	for(int i=si.h;i>0;i--){
		for(int j=1;j<=si.w;j++) fread(&si.col[i][j],sizeof(Color),1,inp);
		for(int j=si.w*3+1;j<=widbt;j++) fread(&zero,sizeof(zero),1,inp);
	}
	fclose(inp);
}
``
原文