#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;
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;
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)
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;
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;
}
}
}
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);