/** * File: NoiseGen.cc * Copyright (C) 2025 Tyler Triplett * License: GNU GPL 3.0 or later <https://www.gnu.org/licenses/gpl-3.0.html> * * This is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include <generators/NoiseGen.hh> #include <utils/safeJsonParse.hh> namespace ty { NoiseGen::NoiseGen(const int& w, const int& h) : BaseGen(w, h) , gen_(rd_()) , dist_(0, uCharMax_) { } NoiseGen::~NoiseGen() { } unsigned char NoiseGen::getRandom(const char& c) { const auto& [l, r] = noiseData_[c]; const auto random = dist_(gen_, decltype(dist_)::param_type(l, r)); return random; } std::vector<unsigned char> NoiseGen::runGen() { const int pixels = width_ * height_; const int size = pixels * 4; std::vector<unsigned char> image(size); for (int i = 0; i < pixels; i++) { image[i * 4] = getRandom('r'); image[i * 4 + 1] = getRandom('g'); image[i * 4 + 2] = getRandom('b'); image[i * 4 + 3] = uCharMax_; } return image; } ColorRange NoiseGen::rangeClamp(const int& left, const int& right) { const int uCharMaxInt = uCharMax_; /// needed for clamp /// mod 256 wrapping would feel strange const auto cr = std::clamp(right, 0, uCharMaxInt); const auto cl = std::min(cr, std::clamp(left, 0, uCharMaxInt)); return std::make_pair(cl, cr); } void NoiseGen::init(const json& j) { const std::vector<std::pair<std::string, char>> colors = { {"Red", 'r'}, {"Green", 'g'}, {"Blue", 'b'}, }; for (const auto& cm : colors) { const auto& [color, i] = cm; const auto left = getJsonValues<int>(j, std::string("left") + color); const auto right = getJsonValues<int>(j, std::string("right") + color); if (left && right) noiseData_[i] = rangeClamp(*left, *right); } } } /// ty