hjhj

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.5 kB
1
Indexable
Never
#include <iostream>

#include <fstream>

using namespace std;

struct bmpheader {
	char Signature[2];
	int FileSize;
	int Reserved;
	int DataOffSet;
};

struct DIB {
	int dibSize;
	int width;
	int height;
	short planes;
	short bpp;
	int compression;
	int imageSize;
	int XpixelsperM;
	int YpixelsperM;
	int colorsUsed;
	int importantColor;
};

struct bmpImage {
	bmpheader header;
	DIB dib;
	char* pDIBReserved;
	char* pImageData;
};

int readBMP(const char* filename, bmpImage bmp)
{
	ifstream fin(filename, ios::in | ios::binary);
	if (!fin)
		return 0;
	fin.seekg(0, ios::beg);
	fin.read((char*)&bmp.header, 14);
	fin.read((char*)&bmp.dib, 40);
	if (bmp.dib.dibSize > 40) {
		int d = bmp.dib.dibSize - 40;
		bmp.pDIBReserved = new char[d];
		fin.read(bmp.pDIBReserved, d);
	}
	bmp.pImageData = new char[bmp.dib.imageSize];
	fin.read(bmp.pImageData, bmp.dib.imageSize);
	return 0;
}

int writeBMP(const char* filename, bmpImage bmp)
{
	ofstream fout(filename, ios::out | ios::binary);
	if (!fout)
		return 0;
	fout.write((char*)&bmp.header, 14);
	fout.write((char*)&bmp.dib, 40);
	if (bmp.dib.dibSize > 40) {
		int d = bmp.dib.dibSize - 40;
		bmp.pDIBReserved = new char[d];
		fout.write(bmp.pDIBReserved, d);
	}
	fout.write(bmp.pImageData, bmp.dib.imageSize);
	return 0;
}

int main()
{
	bmpImage srcImage, dstImage;
	readBMP("input.bmp", srcImage);
	writeBMP("output.bmp", srcImage);
	return 0;
}