/** * File: Noise.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/Noise.hh" #include "utils/safeJsonParse.hh" namespace ty { Noise::Noise() : gen_(rd_()) , dist_(0, UCMAX) { } unsigned char Noise::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> Noise::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] = UCMAX; } return image; } ColorRange Noise::rangeClamp(const ColorRange& colors) { const auto& [left, right] = colors; const int uCharMaxInt = UCMAX; /// 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 { cl, cr }; } std::vector<Setting> Noise::getSettings() { return { { "leftRed" , &(noiseData_['r'].first) }, { "rightRed" , &(noiseData_['r'].second) }, { "leftGreen" , &(noiseData_['g'].first) }, { "rightGreen", &(noiseData_['g'].second) }, { "leftBlue" , &(noiseData_['b'].first) }, { "rightBlue" , &(noiseData_['b'].second) }, }; } void Noise::init(const json& j) { Generator::init(j); const auto settings = getSettings(); applyJson(j, settings); for (const auto c : { 'r', 'g', 'b' }) noiseData_[c] = rangeClamp(noiseData_[c]); } } /// ty