Advertisement
Aaaaa988

Untitled

Mar 17th, 2021
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.51 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, file_header_logo;
  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, map_info_logo;
  26.  
  27. struct RGBquad {
  28.     byte rgbRed;
  29.     byte rgbGreen;
  30.     byte rgbBlue;
  31.     byte rgbReserved;
  32. };
  33.  
  34. int main() {
  35.     FILE *f1 = fopen("image_tc.bmp", "rb");
  36.     FILE *f2 = fopen("image_small_tc.bmp", "rb");
  37.     FILE *f3 = fopen("output.bmp", "wb");
  38.    
  39.     fread(&file_header, sizeof(file_header), 1, f1);
  40.     fwrite(&file_header, sizeof(file_header), 1, f3);
  41.     fread(&file_header_logo, sizeof(file_header_logo), 1, f2);
  42.    
  43.     cout << "-----FILE HEADER" << endl;
  44.     cout << "bfType - " << file_header.bfType << endl;
  45.     cout << "bfSize - " << file_header.bfSize << endl;
  46.     cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
  47.     cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
  48.     cout << "bfOffBits - " << file_header.bfOffbits << endl;
  49.    
  50.     fread(&map_info, sizeof(map_info), 1, f1);
  51.     fwrite(&map_info, sizeof(map_info), 1, f3);
  52.     fread(&map_info_logo, sizeof(map_info_logo), 1, f2);
  53.    
  54.     cout << "-----MAP INFO" << endl;
  55.     cout << "Size - " << map_info.Size << endl;
  56.     cout << "Width - " << map_info.Width << endl;
  57.     cout << "Height - " << map_info.Height << endl;
  58.     cout << "Planes - " << map_info.Planes << endl;
  59.     cout << "BitCount - " << map_info.BitCount << endl;
  60.     cout << "Compression - " << map_info.Compression << endl;
  61.     cout << "SizeImage - " << map_info.SizeImage << endl;
  62.     cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
  63.     cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
  64.     cout << "ClrUsed - " << map_info.ClrUsed << endl;
  65.     cout << "ClrImportant - " << map_info.ClrImportant << endl;
  66.    
  67.     int width = map_info.Width;
  68.     int height = map_info.Height;
  69.    
  70.     if(map_info.BitCount == 4) {
  71. //      int N = map_info.ClrUsed;
  72. //      RGBquad BmiColors[N];
  73. // 
  74. //      fread(BmiColors, sizeof(RGBquad), N, f1);
  75. //     
  76. //      byte *buffer = new byte[width];
  77. //      int n, line = 0;
  78. //      do {
  79. //          n = fread(buffer, 1, width, f1);
  80. //          for(int i = 0; i < n; i += 2) {
  81. //              byte pixel1 = buffer[i] & 0xf;
  82. //              byte pixel2 = buffer[i] & 0x0f;
  83. //             
  84. //              putpixel(i, height - line, RGB((int)BmiColors[pixel1].rgbBlue,
  85. //                  (int)BmiColors[pixel1].rgbGreen, (int)BmiColors[pixel1].rgbRed));
  86. //                 
  87. //              putpixel(i + 1, height - line, (int)RGB(BmiColors[pixel2].rgbBlue,
  88. //                  (int)BmiColors[pixel2].rgbGreen, (int)BmiColors[pixel2].rgbRed));
  89. //          }
  90. //          line++;
  91. //      }while(n == width);
  92. //     
  93. //      delete [] buffer;
  94.     }else if(map_info.BitCount == 8) {
  95.         int N = map_info.ClrUsed;
  96.         RGBquad BmiColors[N], BmiColorsLogo[N];
  97.    
  98.         fread(BmiColors, sizeof(RGBquad), N, f1);
  99.         fread(BmiColorsLogo, sizeof(RGBquad), N, f2);
  100.        
  101.         byte **bufferInp = new byte*[height];
  102.         for(int i = 0; i < height; i++) {
  103.             bufferInp[i] = new byte[width];
  104.             fread(bufferInp[i], sizeof(byte), width, f1);
  105.         }
  106.        
  107.         byte **bufferOut = new byte*[map_info_logo.Height];
  108.         for(int i = 0; i < map_info_logo.Height; i++) {
  109.             bufferOut[i] = new byte[map_info_logo.Width];
  110.             fread(bufferOut[i], sizeof(byte), map_info_logo.Width, f2);
  111.         }
  112.        
  113.         for(int i = 0; i < height; i++) {
  114.             for(int j = 0; j < width; j++) {
  115.                 if(i < map_info_logo.Height){
  116.                     if(j < map_info_logo.Width) {
  117.                         bufferInp[i][j] = bufferOut[i][j];
  118.                        
  119.                         BmiColors[bufferInp[i][j]].rgbRed = BmiColorsLogo[bufferOut[i][j]].rgbRed;
  120.                         BmiColors[bufferInp[i][j]].rgbGreen = BmiColorsLogo[bufferOut[i][j]].rgbGreen;
  121.                         BmiColors[bufferInp[i][j]].rgbBlue = BmiColorsLogo[bufferOut[i][j]].rgbBlue;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.        
  127.         fwrite(BmiColors, sizeof(RGBquad), N, f3);
  128.        
  129.         for(int i = 0; i < height; i++) {
  130.             fwrite(bufferInp[i], sizeof(byte), width, f3);
  131.         }
  132.        
  133.         for(int i = 0; i < height; i++)
  134.             delete[] bufferInp[i];
  135.         delete [] bufferInp;
  136.        
  137.         for(int i = 0; i < map_info_logo.Height; i++)
  138.             delete [] bufferOut[i];
  139.         delete[] bufferOut;
  140.     }else if(map_info.BitCount == 24) {
  141.         width *= 3;
  142.        
  143.         int padding = 0;
  144.         RGBquad rgb, rgbLogo;
  145.         if(width % 4)
  146.             padding = 4 - (width % 4);
  147.            
  148.         int paddingLogo = 0;
  149.         if((map_info_logo.Width * 3) % 4)
  150.             paddingLogo = 4 - ((map_info_logo.Width * 3) % 4);
  151.        
  152.         bool firstVal = false;
  153.         byte *buffer = new byte[width];
  154.         byte *bufferOut = new byte[map_info_logo.Width * 3];
  155.         int n, line = 0, nl, val;
  156.         do {
  157.             n = fread(buffer, 1, width, f1);
  158.             if(line < map_info_logo.Height) {
  159.                 nl = fread(bufferOut, 1, map_info_logo.Width * 3, f2);
  160.                
  161.                 for(int i = 0, col = 0; i < n; i += 3, col++) {
  162.                     if(!firstVal) {
  163.                         val = (bufferOut[i] + bufferOut[i + 1] + bufferOut[i + 2]);
  164.                         firstVal = true;
  165.                     }
  166.                    
  167.                     if(i < map_info_logo.Width * 3) {
  168.                         if((bufferOut[i] + bufferOut[i + 1] + bufferOut[i + 2]) != val) {
  169.                             buffer[i] = bufferOut[i];
  170.                             buffer[i + 1] = bufferOut[i + 1];
  171.                             buffer[i + 2] = bufferOut[i + 2];
  172.                         }
  173.                        
  174.                     }
  175.                 }
  176.                
  177.                 if(paddingLogo)
  178.                     fread(&rgbLogo, paddingLogo, 1, f2);
  179.                    
  180.             }
  181.             line++;
  182.             fwrite(buffer, sizeof(byte), n, f3);
  183.             if(padding != 0)
  184.                 fread(&rgb, padding, 1, f1);
  185.         }while(n == width);
  186.        
  187.         delete [] buffer;
  188.         delete [] bufferOut;
  189.     }else
  190.         cout << "Unsupported image color type" << endl;
  191.    
  192.     fclose(f1);
  193.     fclose(f2);
  194.     fclose(f3);
  195.     return 0;
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement