Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by vfractal on 18/03/2021.
- //
- #include "include/Imager.h"
- #include <iostream>
- #include <fstream>
- #include <utils/utils.h>
- #include <cmath>
- #include <cstring>
- #include <include/Logger.h>
- #include <sstream>
- Imager::Imager(int width, int height, int unit_size, int scale) : size_x(width), size_y(height), unit_size(unit_size),
- scale(scale) {
- this->allocate(width, height, unit_size, scale);
- this->border_size = this->unit_size / 10;
- this->isValid = true;
- }
- Imager::Imager() {
- this->isValid = false;
- }
- void Imager::allocate(int width, int height) {
- this->allocate(width, height, 1, 1);
- }
- void Imager::allocate(int width, int height, int unit_size, int scale) {
- int heightAlloc = height * scale + unit_size;
- int widthAlloc = width * scale + unit_size;
- this->image = std::vector<std::vector<u_char>>(
- heightAlloc,
- std::vector<u_char>(widthAlloc, BLACK)
- );
- }
- void Imager::drawCircle(int x, int y, int radius) {
- this->drawCircle(x, y, radius, COLOR::DARKISH);
- }
- void Imager::drawCircle(int x, int y, int radius, enum COLOR color) {
- double PI = 3.141592653589793;
- for (int i = 0; i < 90; i++) {
- double s = sin(i * PI / 180);
- double c = cos(i * PI / 180);
- for (int ys = 0; ys <= s * radius; ys += 1) {
- memset(&this->image[y + ys][x], color, sizeof(this->image[0][0]) * c * radius);
- memset(&this->image[y + ys][x - c * radius + 1], color, sizeof(this->image[0][0]) * c * radius);
- memset(&this->image[y - ys][x], color, sizeof(this->image[0][0]) * c * radius);
- memset(&this->image[y - ys][x - c * radius + 1], color, sizeof(this->image[0][0]) * c * radius);
- }
- }
- }
- void Imager::drawSquare(int X, int Y) {
- if (!this->isValid) return;
- X *= this->scale;
- Y *= this->scale;
- //draw cell
- this->drawRectangle(
- X,
- Y,
- X + this->unit_size,
- Y + this->unit_size,
- LIGHT_GRAY
- );
- // draww cells border
- this->drawRectangle(
- X,
- Y,
- X + this->unit_size,
- Y + this->border_size,
- DARKISH
- );
- this->drawRectangle(
- X,
- Y,
- X + this->border_size,
- Y + this->unit_size,
- DARKISH
- );
- this->drawRectangle(
- X,
- Y + this->unit_size - this->border_size,
- X + this->unit_size,
- Y + this->unit_size,
- DARKISH
- );
- this->drawRectangle(
- X + this->unit_size - this->border_size,
- Y,
- X + this->unit_size,
- Y + this->unit_size,
- DARKISH
- );
- }
- void Imager::drawRectangle(int sx, int sy, int ex, int ey, enum COLOR color) {
- int height = ey - sy;
- int width = ex - sx;
- int s_x, s_y;
- s_x = (width > 0) ? sx : ex;
- s_y = (height > 0) ? sy : ey;
- height = std::abs(height);
- width = std::abs(width);
- for (uint x = 0; x < width; x++) {
- for (uint y = 0; y < height; y++) {
- this->image[s_y + y][s_x + x] = color;
- }
- }
- }
- void Imager::save(std::string file) {
- std::ofstream nfile;
- std::ostringstream ss;
- int count = 0;
- Logger::log((ss << this->image.size() << " X " << this->image[0].size() << " ==> "
- << this->image.size() * this->image[0].size() << " Pixels\n"));
- Logger::log(ss << "Saving image to file [ " << file << " ]" << "\n");
- nfile.open(file, std::ios::trunc | std::ios::in);
- Logger::log(ss << "File created [OK]" << std::endl);
- Logger::log(ss << "Start writing" << std::endl);
- nfile << "P2" << "\n";
- nfile << this->image[0].size() << " " << this->image.size() << "\n";
- nfile << WHITE << "\n";
- for (const auto &i : this->image) {
- for (const unsigned char &j : i) {
- nfile << j << " ";
- }
- nfile << "\n";
- }
- Logger::log(ss << "Save [Ok] " << std::endl);
- }
- void Imager::drawLine(int x, int y, int length, int width, int mode) {
- this->drawLine(x, y, length, width, mode, WHITE);
- }
- bool Imager::isConfigured() {
- return this->isValid;
- }
- int Imager::getScale() {
- return this->scale;
- }
- int Imager::getUnitSize() {
- return this->unit_size;
- }
- void Imager::drawLine(int x, int y, int length, int width, int mode, COLOR color) {
- if ((mode & 1) == 0) {
- this->drawRectangle(x - width / 2, y, x + width / 2, y + length, color);
- } else {
- this->drawRectangle(x, y - width / 2, x + length, y + width / 2, color);
- }
- }
- void Imager::addz(int z_index, std::function<void()> function) {
- this->z_container[z_index].emplace_back(function);
- }
- void Imager::draw() {
- for (auto &layer : this->z_container) {
- for (auto &invocable : layer.second) {
- std::invoke(invocable);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement