From: Lukas Jiriste Date: Tue, 2 Apr 2024 09:34:23 +0000 (+0200) Subject: Start the project by implementing jpeg read X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=3898103791bdb7f2e846ddaa95e3255d5604fc66;p=Digiteq.git Start the project by implementing jpeg read This commit is just a familiarization with libjpeg-turbo library through a (potentailly buggy) implementation of jpeg to raw image. --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..527c7c4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "solution/Libft"] + path = solution/Libft + url = git://ljiriste.work/Libft diff --git a/solution/.gitignore b/solution/.gitignore new file mode 100644 index 0000000..46ec5c6 --- /dev/null +++ b/solution/.gitignore @@ -0,0 +1,3 @@ +*.[oa] +digiteq +tags diff --git a/solution/Libft b/solution/Libft new file mode 160000 index 0000000..4585e84 --- /dev/null +++ b/solution/Libft @@ -0,0 +1 @@ +Subproject commit 4585e84ca7f988b520925803aeaf43373942a6f8 diff --git a/solution/Makefile b/solution/Makefile new file mode 100644 index 0000000..87ffafe --- /dev/null +++ b/solution/Makefile @@ -0,0 +1,54 @@ +CC := gcc +CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic + +RM := rm -f + +SUBPROJECTS := Libft + +INCDIR := . +INCDIR += $(addsuffix /inc, $(SUBPROJECTS)); +INCLUDE := $(addprefix -I, $(INCDIR)) + +LIBS := jpeg + +LIBSFLAGS := $(addprefix -l, $(LIBS)) + +SRCDIR := . + +SOURCES := main.c \ + +SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) + +OBJECTS := $(SOURCES:.c=.o) + +NAME := digiteq + +all : $(NAME) + +debug : CFLAGS += -g +debug : $(NAME) + +$(NAME) : $(OBJECTS) Libft/libft.a + $(CC) $(CFLAGS) -o $@ $^ $(LIBSFLAGS) + +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 + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/solution/main.c b/solution/main.c new file mode 100644 index 0000000..2743a6f --- /dev/null +++ b/solution/main.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 08:44:53 by ljiriste #+# #+# */ +/* Updated: 2024/04/02 11:29:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +#include +#include + +typedef struct s_rgb +{ + unsigned char r; + unsigned char g; + unsigned char b; +} t_rgb; + +// Assumes 3 chanels +int read_jpeg(char *filename, t_mat *image) +{ + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + JSAMPROW buffer; + FILE *source; + + source = fopen(filename, "rb"); + if (!source) + { + ft_dprintf(STDERR_FILENO, "Can't open %s\n", filename); + return (1); + } + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo, source); + jpeg_read_header(&cinfo, TRUE); + jpeg_start_decompress(&cinfo); + ft_mat_zeros(image, cinfo.output_height, cinfo.output_width); + while (cinfo.output_scanline < cinfo.output_height) + { + buffer = ft_mat_access(image, cinfo.output_scanline, 0); + jpeg_read_scanlines(&cinfo, &buffer, 1); + } + jpeg_finish_decompress(&cinfo); + jpeg_destroy_decompress(&cinfo); + fclose(source); + return (0); +} + +int main(int argc, char **argv) +{ + t_mat image; + + if (argc != 2) + return (1); + ft_mat_init(&image, sizeof(t_rgb)); + read_jpeg(argv[1], &image); + ft_mat_free(&image, NULL); + return (0); +}