Aaaaa988

Untitled

Mar 17th, 2021
481
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define MAXCOLORTC
  2. #include "wingraph.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. struct FileHeader {
  7.     WORD bfType;
  8.     DWORD bfSize;
  9.     WORD bfReserved1;
  10.     WORD bfReserved2;
  11.     DWORD bfOffbits;
  12. } file_header;
  13.  
  14. struct MAPINFO {
  15.     DWORD Size;
  16.     DWORD Width;
  17.     DWORD Height;
  18.     WORD Planes;
  19.     WORD BitCount;
  20.     DWORD Compression;
  21.     DWORD SizeImage;
  22.     long XPelsPerMeter;
  23.     long YPelsPerMeter;
  24.     DWORD ClrUsed;
  25.     DWORD ClrImportant;
  26. } map_info;
  27.  
  28. struct RGBquad {
  29.     byte rgbRed;
  30.     byte rgbGreen;
  31.     byte rgbBlue;
  32.     byte rgbReserved;
  33. };
  34.  
  35. int main() {
  36.     FILE *f1 = fopen("image_tc.bmp", "rb");
  37.    
  38.     fread(&file_header, sizeof(file_header), 1, f1);
  39.    
  40.     cout << "-----FILE HEADER" << endl;
  41.     cout << "bfType - " << file_header.bfType << endl;
  42.     cout << "bfSize - " << file_header.bfSize << endl;
  43.     cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
  44.     cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
  45.     cout << "bfOffBits - " << file_header.bfOffbits << endl;
  46.    
  47.     fread(&map_info, sizeof(map_info), 1, f1);
  48.    
  49.     cout << "-----MAP INFO" << endl;
  50.     cout << "Size - " << map_info.Size << endl;
  51.     cout << "Width - " << map_info.Width << endl;
  52.     cout << "Height - " << map_info.Height << endl;
  53.     cout << "Planes - " << map_info.Planes << endl;
  54.     cout << "BitCount - " << map_info.BitCount << endl;
  55.     cout << "Compression - " << map_info.Compression << endl;
  56.     cout << "SizeImage - " << map_info.SizeImage << endl;
  57.     cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
  58.     cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
  59.     cout << "ClrUsed - " << map_info.ClrUsed << endl;
  60.     cout << "ClrImportant - " << map_info.ClrImportant << endl;
  61.    
  62.     int width = map_info.Width;
  63.     int height = map_info.Height;
  64.     resize(width, height);
  65.    
  66.     if(map_info.BitCount == 4) {
  67.         int N = map_info.ClrUsed;
  68.         RGBquad BmiColors[N];
  69.    
  70.         fread(BmiColors, sizeof(RGBquad), N, f1);
  71.        
  72.         byte *buffer = new byte[width];
  73.        
  74.         int padding = 0;
  75.         if(width % 4)
  76.             padding = 4 - (width % 4);
  77.        
  78.         int n, line = 0;
  79.         do {
  80.             n = fread(buffer, 1, width, f1);
  81.             for(int i = 0, k = 0; i < n; i++, k += 2) {
  82.                 byte pixel1 = buffer[i] & 0xf;
  83.                 byte pixel2 = buffer[i] & 0x0f;
  84.                
  85.                 putpixel(k, height - line, RGB((int)BmiColors[pixel1].rgbBlue,
  86.                     (int)BmiColors[pixel1].rgbGreen, (int)BmiColors[pixel1].rgbRed));
  87.                 putpixel(k + 1, height - line, (int)RGB(BmiColors[pixel2].rgbBlue,
  88.                     (int)BmiColors[pixel2].rgbGreen, (int)BmiColors[pixel2].rgbRed));
  89.             }
  90.             if(padding)
  91.                 fseek(f1, padding, SEEK_CUR);
  92.             line++;
  93.         }while(n == width);
  94.        
  95.         delete [] buffer;
  96.     }else if(map_info.BitCount == 8) {
  97.         int N = map_info.ClrUsed;
  98.         RGBquad BmiColors[N];
  99.    
  100.         fread(BmiColors, sizeof(RGBquad), N, f1);
  101.        
  102.         byte *buffer = new byte[width];
  103.         int n, line = 0;
  104.         do {
  105.             n = fread(buffer, 1, width, f1);
  106.             for(int i = 0; i < n; i++)
  107.                 putpixel(i, height - line, RGB(BmiColors[buffer[i]].rgbBlue,
  108.                     BmiColors[buffer[i]].rgbGreen, BmiColors[buffer[i]].rgbRed));  
  109.             line++;
  110.         }while(n == width);
  111.        
  112.         delete [] buffer;
  113.     }else if(map_info.BitCount == 24) {
  114.         width *= 3;
  115.        
  116.         int padding = 0;
  117.         RGBquad rgb;
  118.         if(width % 4)
  119.             padding = 4 - (width % 4);
  120.        
  121.         byte *buffer = new byte[width];
  122.         int n, line = 0;
  123.         do {
  124.             n = fread(buffer, 1, width, f1);
  125.             for(int i = 0, col = 0; i < n; i += 3, col++) {
  126.                 putpixel(col, height - line, RGB(buffer[i + 2],
  127.                     buffer[i + 1], buffer[i]));
  128.             }
  129.             line++;
  130.            
  131.             if(padding != 0)
  132.                 fread(&rgb, padding, 1, f1);
  133.         }while(n == width);
  134.        
  135.         delete [] buffer;
  136.     }else
  137.         cout << "Unsupported image color type" << endl;
  138.    
  139.     fclose(f1);
  140.     return 0;
  141. }
  142.  
RAW Paste Data