Aaaaa988

Untitled

Mar 20th, 2021
497
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. typedef struct TPCXHeaderStruct {
  7.     unsigned char ID;
  8.     unsigned char Version;
  9.     unsigned char Coding;
  10.     unsigned char BitPerPixel;
  11.     unsigned short XMin;
  12.     unsigned short YMin;
  13.     unsigned short XMax;
  14.     unsigned short YMax;
  15.     unsigned short HRes;
  16.     unsigned short VRes;
  17.     unsigned char Palette[16][3];
  18.     unsigned char Reserved;
  19.     unsigned char Planes;
  20.     unsigned short BytePerLine;
  21.     unsigned short PaletteInfo;
  22.     unsigned short HScreenSize;
  23.     unsigned short VScreenSize;
  24.     unsigned char Filler[54];
  25. } TPCXHeader;
  26.  
  27. typedef struct TPaletteStruct {
  28.     unsigned char Red;
  29.     unsigned char Green;
  30.     unsigned char Blue;
  31. } TPalette;
  32.  
  33. int main() {
  34.     FILE *f = fopen("image_256.pcx", "rb");
  35.     if(f != NULL) {
  36.         TPCXHeader PCXHeader;
  37.         fread(&PCXHeader, sizeof(TPCXHeader), 1, f);
  38.        
  39.         cout << "-----TPCX Header" << endl;
  40.         cout << "ID - " << (int)PCXHeader.ID << endl;
  41.         cout << "Version - " << (int)PCXHeader.Version << endl;
  42.         cout << "Coding - " << (int)PCXHeader.Coding << endl;
  43.         cout << "BitPerPixel - " << (int)PCXHeader.BitPerPixel << endl;
  44.         cout << "XMin - " << PCXHeader.XMin << endl;
  45.         cout << "YMin - " << PCXHeader.YMin << endl;
  46.         cout << "XMax - " << PCXHeader.XMax << endl;
  47.         cout << "YMax - " << PCXHeader.YMax << endl;
  48.         cout << "HRes - " << PCXHeader.HRes << endl;
  49.         cout << "VRes - " << PCXHeader.VRes << endl;
  50.         cout << "Palette - " << sizeof(PCXHeader.Palette) << endl;
  51.         cout << "Reserved - " << (int)PCXHeader.Reserved << endl;
  52.         cout << "Planes - " << (int)PCXHeader.Planes << endl;
  53.         cout << "BytePerLine - " << PCXHeader.BytePerLine << endl;
  54.         cout << "PaletteInfo - " << PCXHeader.PaletteInfo << endl;
  55.         cout << "HScreenSize - " << PCXHeader.HScreenSize << endl;
  56.         cout << "VScreenSize - " << PCXHeader.VScreenSize << endl;
  57.         cout << "Filler - " << sizeof(PCXHeader.Filler) << endl;
  58.        
  59.         int XSize = PCXHeader.XMax - PCXHeader.XMin + 1;
  60.         int YSize = PCXHeader.YMax - PCXHeader.YMin + 1;
  61.         int TotalBytes = PCXHeader.Planes * PCXHeader.BytePerLine;
  62.         resize(XSize, YSize);
  63.        
  64.         cout << "TotalBytes - " << TotalBytes << endl;
  65.         cout << "XSize - " << XSize << endl;
  66.         cout << "YSize - " << YSize << endl;
  67.        
  68.         byte buffer;
  69.         if(PCXHeader.Version == 5 || PCXHeader.BitPerPixel == 8) {
  70.             fseek(f, -769, SEEK_END);
  71.             fread(&buffer, 1, 1, f);
  72.            
  73.             if(buffer == 12) {
  74.                 TPalette PCXPalette[256];
  75.                 fread(PCXPalette, sizeof(PCXPalette), 1, f);
  76.                 fseek(f, sizeof(TPCXHeader), SEEK_SET);
  77.                
  78.                 int count, x = 0, y = 0;
  79.                 //int planes = 0;
  80.                 while(fread(&buffer, 1, 1, f)) {
  81.                     if((buffer & 0xC0) == 0xC0) {
  82.                         count = (buffer & 0x3F);
  83.                         fread(&buffer, 1, 1, f);
  84.                     }else
  85.                         count = 1;
  86.                        
  87.                     do {
  88.                         putpixel(x, y, RGB(PCXPalette[buffer].Red, PCXPalette[buffer].Green, PCXPalette[buffer].Blue));
  89.                        
  90.                         if(x >= TotalBytes)
  91.                             x = 0, y++;
  92.                            
  93.                         if(y >= YSize) {
  94.                         //  planes++;
  95.                         //  if(planes != PCXHeader.Planes)
  96.                         //      x = 0, y = 0;
  97.                         //  else
  98.                                 break;
  99.                         //  cout << "planes - " << planes << endl;
  100.                         }
  101.                        
  102.                         x++;
  103.                     }while(--count);
  104.                 }
  105.             }
  106.         }else
  107.             cout << "File does not contain palette" << endl;
  108.     }else
  109.         cout << "Could not open pcx file" << endl;
  110.    
  111.     fclose(f);
  112.     return 0;
  113. }
  114.  
RAW Paste Data