Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Image FantasyRPG::FileFormats::EncodedImage::Load(std::ifstream& stream) {
- size_t length;
- stream.read(reinterpret_cast<char*>(&length), sizeof length);
- const auto identifier = new char[length];
- stream.read(identifier, length);
- if (!strcmp(identifier, "FantasyRPGEncodedImage")) {
- throw IncorrectFileFormat(std::string("Got incorrect file format. Was expecting FantasyRPGEncodedImage got: ") + identifier);
- }
- delete[] identifier;
- int width;
- int height;
- stream.read(reinterpret_cast<char*>(&width), sizeof(int));
- stream.read(reinterpret_cast<char*>(&height), sizeof(int));
- size_t colorCount;
- stream.read(reinterpret_cast<char*>(&colorCount), sizeof colorCount);
- auto cols = std::vector<Color>();
- for (auto i = 0; i < colorCount; i++) {
- Color col;
- stream.read(reinterpret_cast<char*>(&col), sizeof col);
- if (!VectorContainsItem(cols, col)) {
- cols.push_back(col);
- }
- }
- int size = 0;
- stream.read(reinterpret_cast<char*>(&size), sizeof size);
- auto colors = new Color[size];
- for (auto i = 0; i < size; i++) {
- int index;
- stream.read(reinterpret_cast<char*>(&index), sizeof index);
- colors[i] = cols[index];
- }
- return LoadImageEx(colors, width, height);
- }
- void FantasyRPG::FileFormats::EncodedImage::Save(std::fstream& stream, int width, int height, Color* colors) {
- const auto identifier = "FantasyRPGEncodedImage";
- auto length = strlen(identifier);
- stream.write(reinterpret_cast<char*>(&length), sizeof length);
- stream.write(identifier, length);
- stream.write(reinterpret_cast<char*>(&width), sizeof(int));
- stream.write(reinterpret_cast<char*>(&height), sizeof(int));
- auto cols = std::vector<Color>();
- for (auto i = 0; i < width * height; i++) {
- auto add = true;
- for (auto& c : cols) {
- if (c == colors[i]) {
- add = false;
- }
- }
- if (add) {
- cols.push_back(colors[i]);
- }
- }
- auto colorCount = cols.size();
- stream.write(reinterpret_cast<char*>(&colorCount), sizeof colorCount);
- for (auto &c : cols) {
- stream.write(reinterpret_cast<char*>(&c), sizeof c);
- }
- auto size = width * height;
- std::cout << colorCount << " colors and " << size << " pixels in the image" << std::endl;
- stream.write(reinterpret_cast<char*>(&size), sizeof size);
- auto j = 0;
- for (auto i = 0; i < size; i++) {
- j = 0;
- for (auto& c : cols) {
- if (c == colors[i]) {
- stream.write(reinterpret_cast<char*>(&j), sizeof j);
- break;
- }
- j++;
- }
- }
- stream.flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement