Aaaaa988

Untitled

Mar 17th, 2021 (edited)
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.    
  37.     fread(&file_header, sizeof(file_header), 1, f1);
  38.    
  39.     cout << "-----FILE HEADER" << endl;
  40.     cout << "bfType - " << file_header.bfType << endl;
  41.     cout << "bfSize - " << file_header.bfSize << endl;
  42.     cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
  43.     cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
  44.     cout << "bfOffBits - " << file_header.bfOffbits << endl;
  45.    
  46.     fread(&map_info, sizeof(map_info), 1, f1);
  47.    
  48.     cout << "-----MAP INFO" << endl;
  49.     cout << "Size - " << map_info.Size << endl;
  50.     cout << "Width - " << map_info.Width << endl;
  51.     cout << "Height - " << map_info.Height << endl;
  52.     cout << "Planes - " << map_info.Planes << endl;
  53.     cout << "BitCount - " << map_info.BitCount << endl;
  54.     cout << "Compression - " << map_info.Compression << endl;
  55.     cout << "SizeImage - " << map_info.SizeImage << endl;
  56.     cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
  57.     cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
  58.     cout << "ClrUsed - " << map_info.ClrUsed << endl;
  59.     cout << "ClrImportant - " << map_info.ClrImportant << endl;
  60.    
  61.     if(map_info.BitCount == 8) {
  62.         FILE *f2 = fopen("pigvin256q.bmp", "wb");
  63.        
  64.         fwrite(&file_header, sizeof(file_header), 1, f2);
  65.         fwrite(&map_info, sizeof(map_info), 1, f2);
  66.        
  67.         int N = map_info.ClrUsed;
  68.         RGBquad BmiColors[N];
  69.        
  70.         fread(BmiColors, sizeof(RGBquad), N, f1);
  71.        
  72.         byte r, g, b, v;
  73.         for(int i = 0; i < N; i++) {
  74.             r = BmiColors[i].rgbRed;
  75.             g = BmiColors[i].rgbGreen;
  76.             b = BmiColors[i].rgbBlue;
  77.             v = (r + g + b) / 3;
  78.             BmiColors[i].rgbRed = v;
  79.             BmiColors[i].rgbGreen = v;
  80.             BmiColors[i].rgbBlue = v;
  81.         }
  82.         fwrite(BmiColors, sizeof(RGBquad), N, f2);
  83.        
  84.         byte *buffer = new byte[map_info.Width];
  85.         int n;
  86.         do {
  87.             n = fread(buffer, sizeof(byte), map_info.Width, f1);
  88.             fwrite(buffer, sizeof(byte), n, f2);
  89.         }while(n == map_info.Width);
  90.        
  91.         delete [] buffer;
  92.         fclose(f2);
  93.     }else
  94.         cout << "Unsupported image color type" << endl;
  95.    
  96.     fclose(f1);
  97.     return 0;
  98. }
  99.  
RAW Paste Data