#include "libImg.h" #define ASSERT_MSG(cond, msg) \ do { if (!(cond)) { std::cerr << msg << std::endl; assert(cond); } } while (0) // Salva un immagine in formato ppm P6 255 bool save(const std::string& path, const image& matrix){ std::ofstream f(path, std::ios::binary); if(!f){ std::cerr<<"Errore in save"<(&matrix[y][x]), 3); } } f.close(); std::cout << "Fine salvato"<> magic >> w >> h >> val; if(magic != "P6"){ std::cerr << " Magic Sbagliato"<(w)); for(int y = 0; y < h; y++){ for(int x = 0; x < w; x++){ f.read(reinterpret_cast(&matrix[y][x]), 3); if(f.gcount() != 3){ std::cerr << "Distanza sbagliata" <(w)); for(int y = 0; y < h; ++y){ for(int x = 0; x < w; ++x){ matrix[y][x] = { static_cast(dist(rng)), static_cast(dist(rng)), static_cast(dist(rng)) }; } } return matrix; } //converte un immagini in bianco e nero void BWimage(image &matrix, float lim){ ASSERT_MSG(lim >= 0 && lim <= 1, "Il valore limite deve essere tra 0-1"); const pixel t = { static_cast(lim*255), static_cast(lim*255), static_cast(lim*255) }; for(auto &i: matrix){ for(auto &p: i){ if(p >= t) p = {255,255,255}; else p = {0, 0, 0}; } } } //converte un immagine in scala di grigi void Grayimage(image &matrix){ for(auto &i: matrix){ for(auto &p: i){ short s = (short) p.lum(); p = { static_cast(s), static_cast(s), static_cast(s) }; } } } unsigned long long countW(image &matrix){ int ans = 0; pixel max = {255, 255, 255}; for(const auto &y:matrix){ for(const auto &p: y){ if(p == max)ans++; } } return ans; } // +- 0,25 0,125 ... float BWAimage(image &matrix){ image test; float lim = 0.5f; unsigned long long avg = matrix.size() * matrix[0].size() / 2; for(int i = 4; i <= 1<<6; i = i<<1){ test = matrix; BWimage(test, lim); if(countW(test) < avg){ lim -= 1.0f/i; } else{ lim += 1.0f/i; } // Ez debug // std::cout<<"Il lim รจ: "<