Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <graphics.h>
- using namespace std;
- struct FileHeader {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffbits;
- } file_header;
- struct MAPINFO {
- DWORD Size;
- DWORD Width;
- DWORD Height;
- WORD Planes;
- WORD BitCount;
- DWORD Compression;
- DWORD SizeImage;
- long XPelsPerMeter;
- long YPelsPerMeter;
- DWORD ClrUsed;
- DWORD ClrImportant;
- } map_info;
- struct RGBquad {
- byte rgbRed;
- byte rgbGreen;
- byte rgbBlue;
- byte rgbReserved;
- };
- byte setBit(byte buffer, byte ch, int index) {
- byte mask = 0xFE;
- byte r = (ch >> index) & 0x01;
- buffer &= mask;
- buffer |= r;
- return buffer;
- }
- byte getBit(byte buffer, byte ch, int index) {
- byte r = (buffer & 0x01);
- byte mask = 0xFF;
- mask ^= (1 << index);
- ch &= mask;
- ch |= (r << index);
- return ch;
- }
- int main() {
- FILE *f1 = fopen("image_tc.bmp", "rb");
- FILE *f2 = fopen("file.txt", "rb");
- bool isWrite = false;
- if(f1 != NULL) {
- if(f2 != NULL) {
- fread(&file_header, sizeof(file_header), 1, f1);
- cout << "-----FILE HEADER" << endl;
- cout << "bfType - " << file_header.bfType << endl;
- cout << "bfSize - " << file_header.bfSize << endl;
- cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
- cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
- cout << "bfOffBits - " << file_header.bfOffbits << endl;
- fread(&map_info, sizeof(map_info), 1, f1);
- cout << "-----MAP INFO" << endl;
- cout << "Size - " << map_info.Size << endl;
- cout << "Width - " << map_info.Width << endl;
- cout << "Height - " << map_info.Height << endl;
- cout << "Planes - " << map_info.Planes << endl;
- cout << "BitCount - " << map_info.BitCount << endl;
- cout << "Compression - " << map_info.Compression << endl;
- cout << "SizeImage - " << map_info.SizeImage << endl;
- cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
- cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
- cout << "ClrUsed - " << map_info.ClrUsed << endl;
- cout << "ClrImportant - " << map_info.ClrImportant << endl;
- if(map_info.BitCount == 24) {
- FILE *f3 = fopen("output.bmp", "wb");
- fwrite(&file_header, sizeof(file_header), 1, f3);
- fwrite(&map_info, sizeof(map_info), 1, f3);
- int width = map_info.Width * 3;
- int padding = 0;
- RGBquad rgb;
- if(width % 4)
- padding = 4 - (width % 4);
- byte *buffer = new byte[width], ch;
- int n;
- do {
- n = fread(buffer, 1, width, f1);
- for(int i = 0; i < n; i += 8) {
- if(fread(&ch, 1, 1, f2)) {
- for(int j = 0; j < 8; j++)
- buffer[i + j] = setBit(buffer[i + j], ch, j);
- }else
- break;
- }
- fwrite(buffer, sizeof(byte), n, f3);
- if(padding != 0) {
- fread(&rgb, padding, 1, f1);
- fwrite(&rgb, padding, 1, f3);
- }
- }while(n == width);
- delete [] buffer;
- fclose(f3);
- isWrite = true;
- }else
- cout << "Unsupported image color type" << endl;
- fclose(f2);
- }else
- cout << "Could not open text file" << endl;
- fclose(f1);
- }else
- cout << "Could not open bmp file" << endl;
- if(isWrite) {
- f1 = fopen("output.bmp", "rb");
- if(f1 != NULL) {
- f2 = fopen("outputFile.txt", "wb");
- fread(&file_header, sizeof(file_header), 1, f1);
- fread(&map_info, sizeof(map_info), 1, f1);
- int width = map_info.Width * 3;
- int padding = 0;
- if(width % 4)
- padding = 4 - (width % 4);
- byte *buffer = new byte[width], ch;
- int n;
- do {
- n = fread(buffer, 1, width, f1);
- for(int i = 0; i < n; i += 8) {
- for(int j = 0; j < 8; j++)
- ch = getBit(buffer[i + j], ch, j);
- fwrite(&ch, 1, 1, f2);
- }
- if(padding != 0)
- fseek(f1, padding, SEEK_CUR);
- }while(n == width);
- delete [] buffer;
- fclose(f2);
- }else
- cout << "Could not open bmp file" << endl;
- fclose(f1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement