Start the project by implementing jpeg read
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 09:34:23 +0000 (11:34 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 09:34:23 +0000 (11:34 +0200)
This commit is just a familiarization with libjpeg-turbo library
through a (potentailly buggy) implementation of jpeg to raw image.

.gitmodules [new file with mode: 0644]
solution/.gitignore [new file with mode: 0644]
solution/Libft [new submodule]
solution/Makefile [new file with mode: 0644]
solution/main.c [new file with mode: 0644]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..527c7c4
--- /dev/null
@@ -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 (file)
index 0000000..46ec5c6
--- /dev/null
@@ -0,0 +1,3 @@
+*.[oa]
+digiteq
+tags
diff --git a/solution/Libft b/solution/Libft
new file mode 160000 (submodule)
index 0000000..4585e84
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 4585e84ca7f988b520925803aeaf43373942a6f8
diff --git a/solution/Makefile b/solution/Makefile
new file mode 100644 (file)
index 0000000..87ffafe
--- /dev/null
@@ -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 (file)
index 0000000..2743a6f
--- /dev/null
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/02 08:44:53 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/04/02 11:29:04 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <unistd.h>
+#include <setjmp.h>
+
+#include <stdio.h>
+#include <jpeglib.h>
+
+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);
+}