--- /dev/null
+CC := gcc
+CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic
+
+RM := rm -f
+
+EXT_LIBS := m
+EXT_LINKS := $(addprefix -l, $(EXT_LIBS))
+
+SUBPROJECTS := Libft
+
+INCDIR := .
+INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+INCLUDE := $(addprefix -I, $(INCDIR))
+
+SRCDIR := .
+
+SOURCES := main.c \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.c=.o)
+
+NAME := consebit
+
+all : $(NAME)
+
+debug : CFLAGS += -g
+debug : $(NAME)
+
+$(NAME) : $(OBJECTS) Libft/libft.a
+ $(CC) $(CFLAGS) -o $@ $^ $(EXT_LINKS)
+
+Libft/libft.a : | Libft/Makefile
+ifneq (,$(findstring debug, $(MAKECMDGOALS)))
+ $(MAKE) -C Libft debug
+else
+ $(MAKE) -C Libft
+endif
+
+%.o : %.c | Libft/Makefile
+ $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+%/Makefile :
+ git submodule update --init $($@%/Makefile=%)
+
+clean :
+ $(RM) $(OBJECTS)
+
+fclean : clean
+ $(MAKE) -C Libft/ fclean
+ $(RM) $(NAME)
+
+re : fclean
+ $(MAKE) all
--- /dev/null
+#include "libft.h"
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <math.h>
+
+#define BUFFER_SIZE 100
+
+static const char *default_out = "default.pgm";
+static const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+
+int open_files(int argc, char **argv, int *in_fd, int *out_fd)
+{
+ char *line;
+
+ if (argc == 3)
+ *out_fd = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, mode);
+ else
+ *out_fd = open(default_out, O_WRONLY | O_CREAT, mode);
+ while (*out_fd < 0 && errno == EEXIST)
+ {
+ ft_printf("The file %s already exist. Do you want to overwrite? (y or [n]) ", argv[2]);
+ line = get_next_line(STDIN_FILENO);
+ if (!ft_strcmp(line, "\n") || !ft_strcmp(line, "n\n"))
+ {
+ free(line);
+ return (1);
+ }
+ else if (!ft_strcmp(line, "y\n"))
+ {
+ *out_fd = open(argv[2], O_WRONLY | O_CREAT, mode);
+ free(line);
+ if (*out_fd < 0)
+ return (2);
+ }
+ }
+ *in_fd = open(argv[1], O_RDONLY);
+ if (*in_fd >= 0)
+ return (0);
+ close(*out_fd);
+ return (2);
+}
+
+void print_pgm_header(int fd)
+{
+ ft_dprintf(fd, "P5\n256 256\n255\n");
+ return ;
+}
+
+int mat_max(t_mat *mat)
+{
+ size_t i;
+ size_t j;
+ int tmp;
+ int max;
+
+ i = 0;
+ max = 0;
+ while (i < 256)
+ {
+ j = 0;
+ while (j < 256)
+ {
+ tmp = *(int *)ft_mat_access(mat, i, j);
+ if (tmp > max)
+ max = tmp;
+ ++j;
+ }
+ ++i;
+ }
+ return (max);
+}
+
+void print_image(int fd, t_mat *mat)
+{
+ size_t i;
+ size_t j;
+ int cell;
+ const int max = mat_max(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));
+ ++j;
+ }
+ ++i;
+ }
+ return ;
+}
+
+void file_processing(int in_fd, int out_fd)
+{
+ size_t i;
+ unsigned char buffer[BUFFER_SIZE];
+ t_mat matrix;
+
+ if (ft_mat_init(&matrix, sizeof(int)) != 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]);
+ ++i;
+ }
+ }
+ print_pgm_header(out_fd);
+ print_image(out_fd, &matrix);
+ ft_mat_free(&matrix, NULL);
+ return ;
+}
+
+void print_help(void)
+{
+ ft_printf("This is the consebit tool by Lukáš Jiřiště\n"
+ "This tool plots frequency of pairs of consecutive bits.\n\n"
+ "consebit <input_file> [<output_file>]\n");
+ return ;
+}
+
+int main(int argc, char **argv)
+{
+ int in_fd;
+ int out_fd;
+ int res;
+
+ if (argc < 2 || argc > 3)
+ {
+ print_help();
+ return (1);
+ }
+ res = open_files(argc, argv, &in_fd, &out_fd);
+ if (res != 0)
+ return (res);
+ file_processing(in_fd, out_fd);
+ close(in_fd);
+ close(out_fd);
+ return (0);
+}