From d45aeadacbc4256c73e2f2c8c1276fac72ad00c0 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 4 Apr 2024 11:42:07 +0200 Subject: [PATCH] Implement jpeg error handling, fix leaks --- solution/digiteq.h | 12 +++++++++++- solution/images.c | 18 +++++++++++------- solution/jpeg_handling.c | 22 +++++++++++++++++++--- solution/main.c | 12 ++++++++---- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/solution/digiteq.h b/solution/digiteq.h index 01d7719..873eda5 100644 --- a/solution/digiteq.h +++ b/solution/digiteq.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 10:14:39 by ljiriste #+# #+# */ -/* Updated: 2024/04/04 10:36:18 by ljiriste ### ########.fr */ +/* Updated: 2024/04/04 11:40:32 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,20 @@ # define DIGITEQ_H # include "libft.h" +# include + +# include +# include # define EMOJI_TRESHOLD 255 # define FRAME_THICKNESS 2 +typedef struct s_jpeg_error +{ + struct jpeg_error_mgr common; + jmp_buf setjmp_buffer; +} t_jpeg_error; + typedef struct s_mlx_session { void *mlx; diff --git a/solution/images.c b/solution/images.c index b274d4a..1938f8d 100644 --- a/solution/images.c +++ b/solution/images.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 10:29:22 by ljiriste #+# #+# */ -/* Updated: 2024/04/04 10:31:33 by ljiriste ### ########.fr */ +/* Updated: 2024/04/04 11:30:16 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,12 +73,16 @@ int open_images(t_graphics *graphics, char **argv) graphics->background.img = mlx_jpeg_file_to_image(graphics->mlx_ses.mlx, argv[1], &graphics->background.width, &graphics->background.height); - graphics->emoji.img = mlx_xpm_file_to_image(graphics->mlx_ses.mlx, - "emoji.xpm", &graphics->emoji.width, &graphics->emoji.height); - graphics->red_frame = create_frame_img(graphics->mlx_ses.mlx, - graphics->emoji.width, graphics->emoji.height, 0x80FF0000); - graphics->green_frame = create_frame_img(graphics->mlx_ses.mlx, - graphics->emoji.width, graphics->emoji.height, 0x8000FF00); + graphics->emoji.img = mlx_jpeg_file_to_image(graphics->mlx_ses.mlx, + argv[2], &graphics->emoji.width, + &graphics->emoji.height); + if (graphics->emoji.img) + { + graphics->red_frame = create_frame_img(graphics->mlx_ses.mlx, + graphics->emoji.width, graphics->emoji.height, 0x80FF0000); + graphics->green_frame = create_frame_img(graphics->mlx_ses.mlx, + graphics->emoji.width, graphics->emoji.height, 0x8000FF00); + } res = graphics->background.img && graphics->red_frame.img && graphics->green_frame.img && graphics->emoji.img; if (res) diff --git a/solution/jpeg_handling.c b/solution/jpeg_handling.c index 589b0c8..6d86060 100644 --- a/solution/jpeg_handling.c +++ b/solution/jpeg_handling.c @@ -6,13 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 10:21:40 by ljiriste #+# #+# */ -/* Updated: 2024/04/04 10:52:14 by ljiriste ### ########.fr */ +/* Updated: 2024/04/04 11:40:47 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "digiteq.h" #include #include +#include #include #include @@ -61,13 +62,18 @@ static void flip_alpha(t_mlx_data *img) return ; } +static void my_error_exit(j_common_ptr cinfo) +{ + longjmp(((t_jpeg_error *)cinfo->err)->setjmp_buffer, 1); +} + // This function is written as to have // the same signature as mlx_png_file_to_image void *mlx_jpeg_file_to_image(void *mlx_ptr, char *filename, int *width, int *height) { struct jpeg_decompress_struct cinfo; - struct jpeg_error_mgr jerr; + t_jpeg_error jerr; FILE *source; t_mlx_data img; @@ -77,7 +83,17 @@ void *mlx_jpeg_file_to_image(void *mlx_ptr, char *filename, ft_dprintf(STDERR_FILENO, "Cannot open %s\n", filename); return (NULL); } - cinfo.err = jpeg_std_error(&jerr); + cinfo.err = jpeg_std_error(&jerr.common); + jerr.common.error_exit = my_error_exit; + if (setjmp(jerr.setjmp_buffer)) + { + fclose(source); + ft_dprintf(STDERR_FILENO, "%s: ", filename); + cinfo.err->output_message((j_common_ptr)(&cinfo)); + ft_dprintf(STDERR_FILENO, "\n"); + jpeg_destroy_decompress(&cinfo); + return (NULL); + } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, source); jpeg_read_header(&cinfo, TRUE); diff --git a/solution/main.c b/solution/main.c index 6dceb66..cd6de12 100644 --- a/solution/main.c +++ b/solution/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/02 08:44:53 by ljiriste #+# #+# */ -/* Updated: 2024/04/04 10:58:21 by ljiriste ### ########.fr */ +/* Updated: 2024/04/04 11:33:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,10 @@ int init_state(t_state *state) state->graph.mlx_ses.mlx_win = NULL; state->pos.x = 0; state->pos.y = 0; + state->graph.background.img = NULL; + state->graph.emoji.img = NULL; + state->graph.red_frame.img = NULL; + state->graph.green_frame.img = NULL; return (ft_vec_init(&state->found, sizeof(t_position)) != success || !state->graph.mlx_ses.mlx); } @@ -48,11 +52,11 @@ void display(char **argv) int main(int argc, char **argv) { - if (argc != 2) + if (argc != 3) { ft_printf("Wrong invocation, it should be done as follows:\n" - "\t%s source_jpeg\n", - argv[0]); + "\t%s source_jpeg target_jpeg\n", + argv[0]); return (1); } display(argv); -- 2.30.2