Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. Image FantasyRPG::FileFormats::EncodedImage::Load(std::ifstream& stream) {
  2.     size_t length;
  3.  
  4.     stream.read(reinterpret_cast<char*>(&length), sizeof length);
  5.  
  6.     const auto identifier = new char[length];
  7.  
  8.     stream.read(identifier, length);
  9.  
  10.     if (!strcmp(identifier, "FantasyRPGEncodedImage")) {
  11.         throw IncorrectFileFormat(std::string("Got incorrect file format. Was expecting FantasyRPGEncodedImage got: ") + identifier);
  12.     }
  13.  
  14.     delete[] identifier;
  15.  
  16.     int width;
  17.     int height;
  18.  
  19.     stream.read(reinterpret_cast<char*>(&width), sizeof(int));
  20.  
  21.     stream.read(reinterpret_cast<char*>(&height), sizeof(int));
  22.  
  23.     size_t colorCount;
  24.  
  25.     stream.read(reinterpret_cast<char*>(&colorCount), sizeof colorCount);
  26.  
  27.     auto cols = std::vector<Color>();
  28.  
  29.     for (auto i = 0; i < colorCount; i++) {
  30.         Color col;
  31.         stream.read(reinterpret_cast<char*>(&col), sizeof col);
  32.         if (!VectorContainsItem(cols, col)) {
  33.             cols.push_back(col);
  34.         }
  35.     }
  36.  
  37.     int size = 0;
  38.  
  39.     stream.read(reinterpret_cast<char*>(&size), sizeof size);
  40.  
  41.     auto colors = new Color[size];
  42.  
  43.     for (auto i = 0; i < size; i++) {
  44.         int index;
  45.         stream.read(reinterpret_cast<char*>(&index), sizeof index);
  46.         colors[i] = cols[index];
  47.     }
  48.  
  49.     return LoadImageEx(colors, width, height);
  50. }
  51.  
  52. void FantasyRPG::FileFormats::EncodedImage::Save(std::fstream& stream, int width, int height, Color* colors) {
  53.     const auto identifier = "FantasyRPGEncodedImage";
  54.     auto length = strlen(identifier);
  55.  
  56.     stream.write(reinterpret_cast<char*>(&length), sizeof length);
  57.  
  58.     stream.write(identifier, length);
  59.  
  60.     stream.write(reinterpret_cast<char*>(&width), sizeof(int));
  61.  
  62.     stream.write(reinterpret_cast<char*>(&height), sizeof(int));
  63.  
  64.     auto cols = std::vector<Color>();
  65.  
  66.     for (auto i = 0; i < width * height; i++) {
  67.         auto add = true;
  68.         for (auto& c : cols) {
  69.             if (c == colors[i]) {
  70.                 add = false;
  71.             }
  72.         }
  73.         if (add) {
  74.             cols.push_back(colors[i]);
  75.         }
  76.     }
  77.  
  78.     auto colorCount = cols.size();
  79.  
  80.     stream.write(reinterpret_cast<char*>(&colorCount), sizeof colorCount);
  81.  
  82.     for (auto &c : cols) {
  83.         stream.write(reinterpret_cast<char*>(&c), sizeof c);
  84.     }
  85.  
  86.     auto size = width * height;
  87.  
  88.     std::cout << colorCount << " colors and " << size << " pixels in the image" << std::endl;
  89.  
  90.     stream.write(reinterpret_cast<char*>(&size), sizeof size);
  91.  
  92.     auto j = 0;
  93.  
  94.     for (auto i = 0; i < size; i++) {
  95.         j = 0;
  96.         for (auto& c : cols) {
  97.             if (c == colors[i]) {
  98.                 stream.write(reinterpret_cast<char*>(&j), sizeof j);
  99.                 break;
  100.             }
  101.             j++;
  102.         }
  103.     }
  104.  
  105.     stream.flush();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement