Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <fstream>
- #include <memory>
- #include <utility>
- #pragma pack(push,1)
- struct BMP_FILE_HEADER {
- uint16_t bfType;
- uint32_t bfSize;
- uint16_t bfReserved1;
- uint16_t bfReserved2;
- uint32_t bfOffBits;
- };
- struct BITMAPCOREHEADER {
- uint32_t bcSize;
- uint16_t bcWidth;
- uint16_t bcHeight;
- uint16_t bcPlanes;
- uint16_t bcBitCount;
- };
- struct BITMAPINFOHEADER {
- uint32_t bcSize;
- 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;
- };
- #pragma pack(pop)
- template<typename T>
- void readFile(std::ifstream& file, T* rv) {
- file.read(reinterpret_cast<char*>(rv), sizeof(T));
- }
- template<typename T>
- void writeFile(std::ofstream& file, T* rv) {
- file.write(reinterpret_cast<char*>(rv), sizeof(T));
- }
- void makeBmp(std::ifstream& inf, std::ofstream& outf, int32_t width, int32_t height) {
- const uint32_t size = width*2*height*3;
- BMP_FILE_HEADER writeFileHeader {
- 0x4D42,
- size + sizeof(BMP_FILE_HEADER) + sizeof(BITMAPINFOHEADER),
- 0,
- 0,
- sizeof(BMP_FILE_HEADER) + sizeof(BITMAPINFOHEADER)
- };
- writeFile(outf, &writeFileHeader);
- BITMAPINFOHEADER writeHeader {
- sizeof(BITMAPINFOHEADER),
- width * 2,
- height,
- 1,
- 24,
- 0,
- size,
- 0,
- 0,
- 0,
- 0
- };
- writeFile(outf, &writeHeader);
- std::unique_ptr<char[]> in_line{new char[width*3]};
- std::unique_ptr<char[]> out_line{new char[width*2*3]};
- for(auto cHeight = 0; cHeight <= height; ++cHeight) {
- inf.read(in_line.get(), width*3);
- memcpy(out_line.get(), in_line.get(), width*3);
- char* in = in_line.get();
- char* out = out_line.get() + width*3;
- for(auto i = 0; i < width; ++i) {
- out[i*3] = in[(width - i)*3 - 3];
- out[i*3 + 1] = in[(width - i)*3 - 2];
- out[i*3 + 2] = in[(width - i)*3 - 1];
- }
- outf.write(out_line.get(), width*2*3);
- }
- outf.close();
- }
- int main() {
- std::ifstream in("A:\\files\\123.bmp", std::ios::binary);
- std::ofstream out("A:\\files\\1234.bmp", std::ios::binary);
- if(!in.is_open()) {
- return 1;
- }
- BMP_FILE_HEADER fileHeader;
- readFile(in, &fileHeader);
- if(fileHeader.bfType != 0x4D42) {
- return 1;
- }
- uint32_t size;
- readFile(in, &size);
- in.seekg(0x0E);
- switch(size) {
- case 12: {
- BITMAPCOREHEADER header;
- readFile(in, &header);
- if(header.bcBitCount != 24) {
- return 1;
- }
- in.seekg(fileHeader.bfOffBits);
- makeBmp(in, out, header.bcWidth, header.bcHeight);
- }
- default: {
- BITMAPINFOHEADER header;
- readFile(in, &header);
- if(header.biCompression != 0 || header.biClrUsed != 0 || header.biBitCount != 24) {
- return 1;
- }
- in.seekg(fileHeader.bfOffBits);
- makeBmp(in, out, header.biWidth, header.biHeight);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement