/// 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; } noiseGen::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::loadJson(const json& j) { for (const auto& cm : colorMap_) { const auto& color = cm.first; const auto& c = cm.second; const auto left = getJsonValues<int>(j, std::string("left") + color); const auto right = getJsonValues<int>(j, std::string("right") + color); if (left && right) { noiseData_[c] = rangeClamp(*left, *right); } } } } /// ty