Aaaaa988

Untitled

Mar 17th, 2021 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <iostream>
  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. int main() {
  35.     FILE *f1 = fopen("pigvin256.bmp", "rb");
  36.     FILE *f2 = fopen("pigvin256q.bmp", "wb");
  37.    
  38.     fread(&file_header, sizeof(file_header), 1, f1);
  39.     fwrite(&file_header, sizeof(file_header), 1, f2);
  40.    
  41.     cout << "-----FILE HEADER" << endl;
  42.     cout << "bfType - " << file_header.bfType << endl;
  43.     cout << "bfSize - " << file_header.bfSize << endl;
  44.     cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
  45.     cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
  46.     cout << "bfOffBits - " << file_header.bfOffbits << endl;
  47.    
  48.     fread(&map_info, sizeof(map_info), 1, f1);
  49.    
  50.     cout << "-----MAP INFO" << endl;
  51.     cout << "Size - " << map_info.Size << endl;
  52.     cout << "Width - " << map_info.Width << endl;
  53.     cout << "Height - " << map_info.Height << endl;
  54.     cout << "Planes - " << map_info.Planes << endl;
  55.     cout << "BitCount - " << map_info.BitCount << endl;
  56.     cout << "Compression - " << map_info.Compression << endl;
  57.     cout << "SizeImage - " << map_info.SizeImage << endl;
  58.     cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
  59.     cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
  60.     cout << "ClrUsed - " << map_info.ClrUsed << endl;
  61.     cout << "ClrImportant - " << map_info.ClrImportant << endl;
  62.    
  63.     int width = map_info.Width;
  64.     int height = map_info.Height;
  65.    
  66.     map_info.Height = map_info.Width;
  67.     map_info.Width = height;
  68.    
  69.     fwrite(&map_info, sizeof(map_info), 1, f2);
  70.    
  71.     int border, padding = 0;
  72.     if(map_info.BitCount == 8 || map_info.BitCount == 4) {
  73.         int N = map_info.ClrUsed;
  74.         RGBquad BmiColors[N];
  75.    
  76.         fread(BmiColors, sizeof(RGBquad), N, f1);
  77.         fwrite(BmiColors, sizeof(RGBquad), N, f2);
  78.        
  79.         border = 1;
  80.     }else if(map_info.BitCount == 16)
  81.         border = 2;
  82.     else if(map_info.BitCount == 24)
  83.         border = 3;
  84.     else
  85.         cout << "Unsupported image color type" << endl;
  86.        
  87.     width *= border;
  88.    
  89.     if(width % 4)
  90.         padding = 4 - (width % 4);
  91.  
  92.     byte **buffer = new byte*[height];
  93.     for(int i = 0; i < height; i++) {
  94.         buffer[i] = new byte[width];
  95.         fread(buffer[i], 1, width, f1);
  96.         if(padding != 0)
  97.             fseek(f1, padding, SEEK_CUR);
  98.     }
  99.    
  100.     padding = 0;
  101.     if((height * 3) % 4)
  102.         padding = 4 - (height * 3) % 4;
  103.    
  104.     int b = 0;
  105.     for(int j = width - 1; j >= 0; j--) {
  106.         for(int i = 0; i < height; i++)
  107.             fwrite(&buffer[i][j], 1, 1, f2);
  108.    
  109.         //if(j < height)
  110.         if(padding != 0) {
  111.             fwrite(&b, padding, 1, f2);
  112.         }
  113.     }
  114.  
  115.     for(int i = 0; i < height; i++)
  116.         delete [] buffer[i];
  117.     delete [] buffer;
  118.    
  119.     fclose(f1);
  120.     fclose(f2);
  121.    
  122.     return 0;
  123. }
  124.  
Add Comment
Please, Sign In to add comment