Advertisement
Aaaaa988

Untitled

Mar 20th, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <graphics.h>
  3. using namespace std;
  4.  
  5. struct FileHeader {
  6.     WORD bfType;
  7.     DWORD bfSize;
  8.     WORD bfReserved1;
  9.     WORD bfReserved2;
  10.     DWORD bfOffbits;
  11. } file_header;
  12.  
  13. struct MAPINFO {
  14.     DWORD Size;
  15.     DWORD Width;
  16.     DWORD Height;
  17.     WORD Planes;
  18.     WORD BitCount;
  19.     DWORD Compression;
  20.     DWORD SizeImage;
  21.     long XPelsPerMeter;
  22.     long YPelsPerMeter;
  23.     DWORD ClrUsed;
  24.     DWORD ClrImportant;
  25. } map_info;
  26.  
  27. struct RGBquad {
  28.     byte rgbRed;
  29.     byte rgbGreen;
  30.     byte rgbBlue;
  31.     byte rgbReserved;
  32. };
  33.  
  34. byte setBit(byte buffer, byte ch, int index) {
  35.     byte mask = 0xFE;
  36.     byte r = (ch >> index) & 0x01;
  37.  
  38.     buffer &= mask;
  39.     buffer |= r;
  40.     return buffer;
  41. }
  42.  
  43. byte getBit(byte buffer, byte ch, int index) {
  44.     byte r = (buffer & 0x01);
  45.     byte mask = 0xFF;
  46.     mask ^= (1 << index);
  47.    
  48.     ch &= mask;
  49.     ch |= (r << index);
  50.     return ch;
  51. }
  52.  
  53. int main() {
  54.     FILE *f1 = fopen("image_tc.bmp", "rb");
  55.     FILE *f2 = fopen("file.txt", "rb");
  56.     bool isWrite = false;
  57.     if(f1 != NULL) {
  58.         if(f2 != NULL) {
  59.             fread(&file_header, sizeof(file_header), 1, f1);
  60.            
  61.             cout << "-----FILE HEADER" << endl;
  62.             cout << "bfType - " << file_header.bfType << endl;
  63.             cout << "bfSize - " << file_header.bfSize << endl;
  64.             cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
  65.             cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
  66.             cout << "bfOffBits - " << file_header.bfOffbits << endl;
  67.            
  68.             fread(&map_info, sizeof(map_info), 1, f1);
  69.            
  70.             cout << "-----MAP INFO" << endl;
  71.             cout << "Size - " << map_info.Size << endl;
  72.             cout << "Width - " << map_info.Width << endl;
  73.             cout << "Height - " << map_info.Height << endl;
  74.             cout << "Planes - " << map_info.Planes << endl;
  75.             cout << "BitCount - " << map_info.BitCount << endl;
  76.             cout << "Compression - " << map_info.Compression << endl;
  77.             cout << "SizeImage - " << map_info.SizeImage << endl;
  78.             cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
  79.             cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
  80.             cout << "ClrUsed - " << map_info.ClrUsed << endl;
  81.             cout << "ClrImportant - " << map_info.ClrImportant << endl;
  82.            
  83.             if(map_info.BitCount == 24) {
  84.                 FILE *f3 = fopen("output.bmp", "wb");
  85.                 fwrite(&file_header, sizeof(file_header), 1, f3);
  86.                 fwrite(&map_info, sizeof(map_info), 1, f3);
  87.                
  88.                 int width = map_info.Width * 3;
  89.                 int padding = 0;
  90.                 RGBquad rgb;
  91.                
  92.                 if(width % 4)
  93.                     padding = 4 - (width % 4);
  94.                
  95.                 byte *buffer = new byte[width], ch;
  96.                 int n;
  97.                 do {
  98.                     n = fread(buffer, 1, width, f1);
  99.                     for(int i = 0; i < n; i += 8) {
  100.                         if(fread(&ch, 1, 1, f2)) {
  101.                             for(int j = 0; j < 8; j++)
  102.                                 buffer[i + j] = setBit(buffer[i + j], ch, j);
  103.                         }else
  104.                             break;
  105.                     }  
  106.                    
  107.                     fwrite(buffer, sizeof(byte), n, f3);
  108.                     if(padding != 0) {
  109.                         fread(&rgb, padding, 1, f1);
  110.                         fwrite(&rgb, padding, 1, f3);
  111.                     }
  112.                 }while(n == width);
  113.                
  114.                 delete [] buffer;
  115.                 fclose(f3);
  116.                 isWrite = true;
  117.             }else
  118.                 cout << "Unsupported image color type" << endl;
  119.                
  120.             fclose(f2);
  121.         }else
  122.             cout << "Could not open text file" << endl;
  123.        
  124.         fclose(f1);
  125.     }else
  126.         cout << "Could not open bmp file" << endl;
  127.        
  128.     if(isWrite) {
  129.         f1 = fopen("output.bmp", "rb");
  130.         if(f1 != NULL) {
  131.             f2 = fopen("outputFile.txt", "wb");
  132.             fread(&file_header, sizeof(file_header), 1, f1);
  133.             fread(&map_info, sizeof(map_info), 1, f1);
  134.            
  135.             int width = map_info.Width * 3;
  136.             int padding = 0;
  137.            
  138.             if(width % 4)
  139.                 padding = 4 - (width % 4);
  140.                
  141.             byte *buffer = new byte[width], ch;
  142.             int n;
  143.             do {
  144.                 n = fread(buffer, 1, width, f1);
  145.                 for(int i = 0; i < n; i += 8) {
  146.                     for(int j = 0; j < 8; j++)
  147.                         ch = getBit(buffer[i + j], ch, j);
  148.                     fwrite(&ch, 1, 1, f2);
  149.                 }
  150.                
  151.                 if(padding != 0)
  152.                     fseek(f1, padding, SEEK_CUR);
  153.             }while(n == width);
  154.            
  155.             delete [] buffer;
  156.             fclose(f2);
  157.         }else
  158.             cout << "Could not open bmp file" << endl;
  159.        
  160.         fclose(f1);
  161.     }
  162.    
  163.     return 0;
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement