1fractal

Untitled

May 7th, 2021
585
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Created by vfractal on 18/03/2021.
  3. //
  4.  
  5. #include "include/Imager.h"
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <utils/utils.h>
  10. #include <cmath>
  11. #include <cstring>
  12. #include <include/Logger.h>
  13. #include <sstream>
  14.  
  15. Imager::Imager(int width, int height, int unit_size, int scale) : size_x(width), size_y(height), unit_size(unit_size),
  16.                                                                   scale(scale) {
  17.     this->allocate(width, height, unit_size, scale);
  18.  
  19.     this->border_size = this->unit_size / 10;
  20.     this->isValid = true;
  21. }
  22.  
  23. Imager::Imager() {
  24.     this->isValid = false;
  25. }
  26.  
  27. void Imager::allocate(int width, int height) {
  28.     this->allocate(width, height, 1, 1);
  29. }
  30.  
  31. void Imager::allocate(int width, int height, int unit_size, int scale) {
  32.     int heightAlloc = height * scale + unit_size;
  33.     int widthAlloc = width * scale + unit_size;
  34.     this->image = std::vector<std::vector<u_char>>(
  35.             heightAlloc,
  36.             std::vector<u_char>(widthAlloc, BLACK)
  37.     );
  38. }
  39.  
  40.  
  41.  
  42. void Imager::drawCircle(int x, int y, int radius) {
  43.     this->drawCircle(x, y, radius, COLOR::DARKISH);
  44. }
  45.  
  46. void Imager::drawCircle(int x, int y, int radius, enum COLOR color) {
  47.     double PI = 3.141592653589793;
  48.     for (int i = 0; i < 90; i++) {
  49.         double s = sin(i * PI / 180);
  50.         double c = cos(i * PI / 180);
  51.         for (int ys = 0; ys <= s * radius; ys += 1) {
  52.             memset(&this->image[y + ys][x], color, sizeof(this->image[0][0]) * c * radius);
  53.             memset(&this->image[y + ys][x - c * radius + 1], color, sizeof(this->image[0][0]) * c * radius);
  54.             memset(&this->image[y - ys][x], color, sizeof(this->image[0][0]) * c * radius);
  55.             memset(&this->image[y - ys][x - c * radius + 1], color, sizeof(this->image[0][0]) * c * radius);
  56.         }
  57.     }
  58. }
  59.  
  60. void Imager::drawSquare(int X, int Y) {
  61.  
  62.     if (!this->isValid) return;
  63.  
  64.     X *= this->scale;
  65.     Y *= this->scale;
  66.  
  67.     //draw cell
  68.     this->drawRectangle(
  69.             X,
  70.             Y,
  71.             X + this->unit_size,
  72.             Y + this->unit_size,
  73.             LIGHT_GRAY
  74.     );
  75. //    draww cells border
  76.     this->drawRectangle(
  77.             X,
  78.             Y,
  79.             X + this->unit_size,
  80.             Y + this->border_size,
  81.             DARKISH
  82.     );
  83.     this->drawRectangle(
  84.             X,
  85.             Y,
  86.             X + this->border_size,
  87.             Y + this->unit_size,
  88.             DARKISH
  89.     );
  90.     this->drawRectangle(
  91.             X,
  92.             Y + this->unit_size - this->border_size,
  93.             X + this->unit_size,
  94.             Y + this->unit_size,
  95.             DARKISH
  96.     );
  97.     this->drawRectangle(
  98.             X + this->unit_size - this->border_size,
  99.             Y,
  100.             X + this->unit_size,
  101.             Y + this->unit_size,
  102.             DARKISH
  103.     );
  104.  
  105. }
  106.  
  107. void Imager::drawRectangle(int sx, int sy, int ex, int ey, enum COLOR color) {
  108.  
  109.     int height = ey - sy;
  110.     int width = ex - sx;
  111.  
  112.     int s_x, s_y;
  113.  
  114.  
  115.     s_x = (width > 0) ? sx : ex;
  116.     s_y = (height > 0) ? sy : ey;
  117.  
  118.     height = std::abs(height);
  119.     width = std::abs(width);
  120.  
  121.  
  122.     for (uint x = 0; x < width; x++) {
  123.         for (uint y = 0; y < height; y++) {
  124.             this->image[s_y + y][s_x + x] = color;
  125.         }
  126.     }
  127.  
  128. }
  129.  
  130. void Imager::save(std::string file) {
  131.     std::ofstream nfile;
  132.     std::ostringstream ss;
  133.     int count = 0;
  134.  
  135.     Logger::log((ss << this->image.size() << " X " << this->image[0].size() << " ==> "
  136.                     << this->image.size() * this->image[0].size() << " Pixels\n"));
  137.     Logger::log(ss << "Saving image to file [ " << file << " ]" << "\n");
  138.  
  139.     nfile.open(file, std::ios::trunc | std::ios::in);
  140.     Logger::log(ss << "File created [OK]" << std::endl);
  141.     Logger::log(ss << "Start writing" << std::endl);
  142.  
  143.     nfile << "P2" << "\n";
  144.     nfile << this->image[0].size() << " " << this->image.size() << "\n";
  145.     nfile << WHITE << "\n";
  146.  
  147.     for (const auto &i : this->image) {
  148.         for (const unsigned char &j : i) {
  149.             nfile << j << " ";
  150.         }
  151.         nfile << "\n";
  152.     }
  153.     Logger::log(ss << "Save [Ok]  " << std::endl);
  154. }
  155.  
  156.  
  157. void Imager::drawLine(int x, int y, int length, int width, int mode) {
  158.     this->drawLine(x, y, length, width, mode, WHITE);
  159. }
  160.  
  161. bool Imager::isConfigured() {
  162.     return this->isValid;
  163. }
  164.  
  165. int Imager::getScale() {
  166.     return this->scale;
  167. }
  168.  
  169. int Imager::getUnitSize() {
  170.     return this->unit_size;
  171. }
  172.  
  173. void Imager::drawLine(int x, int y, int length, int width, int mode, COLOR color) {
  174.     if ((mode & 1) == 0) {
  175.         this->drawRectangle(x - width / 2, y, x + width / 2, y + length, color);
  176.     } else {
  177.         this->drawRectangle(x, y - width / 2, x + length, y + width / 2, color);
  178.     }
  179.  
  180. }
  181.  
  182. void Imager::addz(int z_index, std::function<void()> function) {
  183.     this->z_container[z_index].emplace_back(function);
  184. }
  185.  
  186. void Imager::draw() {
  187.  
  188.     for (auto &layer : this->z_container) {
  189.         for (auto &invocable : layer.second) {
  190.             std::invoke(invocable);
  191.         }
  192.     }
  193.  
  194. }
  195.  
  196.  
RAW Paste Data