commit 445422546278e8293c7585d62caa7608585d2311 Author: Basso Ricci Jacopo Date: Sun Mar 15 20:05:01 2026 +0100 Primo commit di questa repo, è un bel progettino molto semplice da funzioni esplicative diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a15e33d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ignoriamo le immagini +*.png +*.jpg +*.ppm + +# Ingoriamo i gli oggetti +*.o +*.x diff --git a/libImg.cpp b/libImg.cpp new file mode 100644 index 0000000..556d4f8 --- /dev/null +++ b/libImg.cpp @@ -0,0 +1,169 @@ +#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 è: "< +#include +#include +#include +#include +#include +#include +#include + +struct pixel { + unsigned char r, g, b; + + bool operator==(const pixel& o) const { return r == o.r && g == o.g && b == o.b; } + bool operator!=(const pixel& o) const { return !(*this == o); } + bool operator>=(const pixel& o) const { return r - o.r + g - o.g + b - o.b > 0; } + + float lum() const { return 0.2126f * r + 0.7152f * g + 0.0722f * b; } + float lumNorm() const { return lum() / 255.0f; } +}; + +using image = std::vector>; + +//Variabili importanti +inline std::mt19937 rng(10); +inline std::uniform_int_distribution dist(0, 255); +inline image errore(1, std::vector(1, {0, 0, 0})); + + +bool save(const std::string& path, const image& matrix); +image load(const std::string& path, int& w, int& h); + +bool images_equal(const image& a, const image& b); +unsigned long long countW(image &matrix); + +image randomImage(int w, int h); +void BWimage(image &matrix, float lim); +void Grayimage(image &matrix); +float BWAimage(image &matrix); +void invertImage(image &matrix); +void gaussianImage(image &matrix, int dim); \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b5a739f --- /dev/null +++ b/main.cpp @@ -0,0 +1,50 @@ +// magick malenia.jpg -define ppm:format=raw test.ppm +#include +#include "libImg.h" +using namespace std; + +const int WIDTH = 16 * 60; +const int HEIGHT = 9 * 60; + +int main () { + /* + image matrix = randomImage(WIDTH, HEIGHT); + + //BWimage(matrix, 0.70); + Grayimage(matrix); + + if (!save("output.ppm", matrix, WIDTH, HEIGHT)) { + return -1; + } + + + int height, width; + image caricato = load("output.ppm", width, height); + + if(images_equal(matrix, caricato)){ + std::cout << "Cazzo funziona"<