Improve the program to use the whole color range
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 6 May 2024 20:04:08 +0000 (22:04 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 6 May 2024 20:04:08 +0000 (22:04 +0200)
Also change the type that holds pair count to typedef for easier
upgrade. Default type is now unsigned long long.

main.c

diff --git a/main.c b/main.c
index c3df925c92f63a6668312674c7bd9dd5deedcd94..48d6bc7e3720dd456404e6c9290f3584115a4eb1 100644 (file)
--- 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);