From 8a40e589b816b62c5c173d524cc93190c9173884 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 6 May 2024 22:04:08 +0200 Subject: [PATCH] Improve the program to use the whole color range Also change the type that holds pair count to typedef for easier upgrade. Default type is now unsigned long long. --- main.c | 56 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index c3df925..48d6bc7 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,8 @@ #define BUFFER_SIZE 100 +typedef unsigned long long t_count; + static const char *default_out = "default.pgm"; static const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; @@ -49,21 +51,21 @@ void print_pgm_header(int fd) return ; } -int mat_max(t_mat *mat) +t_count mat_max(t_mat *mat) { size_t i; size_t j; - int tmp; - int max; + t_count tmp; + t_count max; i = 0; - max = 0; + max = *(t_count *)ft_mat_access(mat, 0, 0); while (i < 256) { j = 0; while (j < 256) { - tmp = *(int *)ft_mat_access(mat, i, j); + tmp = *(t_count *)ft_mat_access(mat, i, j); if (tmp > max) max = tmp; ++j; @@ -73,12 +75,37 @@ int mat_max(t_mat *mat) return (max); } +t_count mat_min(t_mat *mat) +{ + size_t i; + size_t j; + t_count tmp; + t_count min; + + i = 0; + min = *(t_count *)ft_mat_access(mat, 0, 0); + while (i < 256) + { + j = 0; + while (j < 256) + { + tmp = *(t_count *)ft_mat_access(mat, i, j); + if (tmp < min) + min = tmp; + ++j; + } + ++i; + } + return (min); +} + void print_image(int fd, t_mat *mat) { - size_t i; - size_t j; - int cell; - const int max = mat_max(mat); + size_t i; + size_t j; + t_count cell; + const t_count max = mat_max(mat); + const t_count min = mat_min(mat); i = 0; while (i < 256) @@ -86,8 +113,8 @@ void print_image(int fd, t_mat *mat) j = 0; while (j < 256) { - cell = *(int *)ft_mat_access(mat, i, j); - ft_dprintf(fd, "%c", (char)(log(cell + 1) / log(max + 1) * 255)); + cell = *(t_count *)ft_mat_access(mat, i, j); + ft_dprintf(fd, "%c", (char)(log(cell - min + 1) / log(max - min + 1) * 255)); ++j; } ++i; @@ -101,14 +128,14 @@ void file_processing(int in_fd, int out_fd) unsigned char buffer[BUFFER_SIZE]; t_mat matrix; - if (ft_mat_init(&matrix, sizeof(int)) != success || ft_mat_zeros(&matrix, 256, 256) != success) + if (ft_mat_init(&matrix, sizeof(t_count)) != success || ft_mat_zeros(&matrix, 256, 256) != success) return ; while (read(in_fd, buffer, BUFFER_SIZE) > 0) { i = 1; while (i < BUFFER_SIZE) { - ++*(int *)ft_mat_access(&matrix, buffer[i - 1], buffer[i]); + ++*(t_count *)ft_mat_access(&matrix, buffer[i - 1], buffer[i]); ++i; } } @@ -139,7 +166,10 @@ int main(int argc, char **argv) } res = open_files(argc, argv, &in_fd, &out_fd); if (res != 0) + { + ft_printf("There was a problem opening a file.\n"); return (res); + } file_processing(in_fd, out_fd); close(in_fd); close(out_fd); -- 2.30.2