Refactor pixel handling functions from main.c
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 19 Jan 2024 13:58:48 +0000 (14:58 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 19 Jan 2024 13:58:48 +0000 (14:58 +0100)
Makefile
main.c
pixel.c [new file with mode: 0644]
pixel.h [new file with mode: 0644]

index ebb90c0bc232b67121a6b41777ccdd8b60cf7301..e37b34624c6cd8a4363a21f059db2f978b65b763 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ MLX := $(MLXDIR)libmlx_Linux.a
 LFTDIR := Libft/
 LFT := $(LFTDIR)libft.a
 
-SRCS := main.c complex.c color.c event_handling.c fractals.c
+SRCS := main.c complex.c color.c event_handling.c fractals.c pixel.c
 OBJS := $(SRCS:%.c=%.o)
 
 all : $(NAME)
diff --git a/main.c b/main.c
index aca56f01a71229f8593e0851a6720f7bfe387559..82a4fae938adfe392a26347acffa89d409931dee 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/19 13:44:02 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/19 13:58:45 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "color.h"
 #include "vect2.h"
 #include "fractals.h"
+#include "pixel.h"
 #include "fractol.h"
 #include "libft.h"
 #include <mlx.h>
 #include <math.h>
 #include <stdbool.h>
-#include <limits.h>
 #include <stdlib.h>
 #include <X11/keysym.h>
 #include <X11/X.h>
@@ -31,33 +31,6 @@ int  close_win(t_session *s)
        return (0);
 }
 
-void   *get_pixel(t_img *img, int x, int y)
-{
-       return (img->addr + y * img->bpl + x * img->bpp / CHAR_BIT);
-}
-
-void   ft_putpx_img(t_img *img, int x, int y, t_color c)
-{
-       char    *px_addr;
-
-       px_addr = get_pixel(img, x, y);
-       if (img->endian)
-       {
-               px_addr[0] = c.a;
-               px_addr[1] = c.r;
-               px_addr[2] = c.g;
-               px_addr[3] = c.b;
-       }
-       else
-       {
-               px_addr[3] = c.a;
-               px_addr[2] = c.r;
-               px_addr[1] = c.g;
-               px_addr[0] = c.b;
-       }
-       return ;
-}
-
 void   free_session(t_session *s)
 {
        mlx_destroy_display(s->mlx);
@@ -65,29 +38,6 @@ void free_session(t_session *s)
        return ;
 }
 
-t_color        get_img_pixel_color(t_img *img, int x, int y)
-{
-       char    *px_addr;
-       t_color c;
-
-       px_addr = get_pixel(img, x, y);
-       if (img->endian)
-       {
-               c.a = px_addr[0];
-               c.r = px_addr[1];
-               c.g = px_addr[2];
-               c.b = px_addr[3];
-       }
-       else
-       {
-               c.a = px_addr[3];
-               c.r = px_addr[2];
-               c.g = px_addr[1];
-               c.b = px_addr[0];
-       }
-       return (c);
-}
-
 t_color        get_color(t_session *s, int x, int y)
 {
        double  palette_param;
@@ -276,7 +226,7 @@ void        parse_args(int argc, char **argv, t_session *s)
        if (argc == 0)
                free(argv);
        s->set.man.detail = 1000;
-       s->set.man.color_stability = 1000;
+       s->set.man.color_stability = 100;
        s->img.width = 1000;
        s->img.height = 1000;
        s->img.undersample_max = 5;
diff --git a/pixel.c b/pixel.c
new file mode 100644 (file)
index 0000000..34e1796
--- /dev/null
+++ b/pixel.c
@@ -0,0 +1,66 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   pixel.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/01/19 13:50:28 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/01/19 13:58:30 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pixel.h"
+#include "color.h"
+#include "fractol.h"
+#include <limits.h>
+
+void   *get_pixel(t_img *img, int x, int y)
+{
+       return (img->addr + y * img->bpl + x * img->bpp / CHAR_BIT);
+}
+
+void   ft_putpx_img(t_img *img, int x, int y, t_color c)
+{
+       char    *px_addr;
+
+       px_addr = get_pixel(img, x, y);
+       if (img->endian)
+       {
+               px_addr[0] = c.a;
+               px_addr[1] = c.r;
+               px_addr[2] = c.g;
+               px_addr[3] = c.b;
+       }
+       else
+       {
+               px_addr[3] = c.a;
+               px_addr[2] = c.r;
+               px_addr[1] = c.g;
+               px_addr[0] = c.b;
+       }
+       return ;
+}
+
+t_color        get_img_pixel_color(t_img *img, int x, int y)
+{
+       char    *px_addr;
+       t_color c;
+
+       px_addr = get_pixel(img, x, y);
+       if (img->endian)
+       {
+               c.a = px_addr[0];
+               c.r = px_addr[1];
+               c.g = px_addr[2];
+               c.b = px_addr[3];
+       }
+       else
+       {
+               c.a = px_addr[3];
+               c.r = px_addr[2];
+               c.g = px_addr[1];
+               c.b = px_addr[0];
+       }
+       return (c);
+}
diff --git a/pixel.h b/pixel.h
new file mode 100644 (file)
index 0000000..58c6da9
--- /dev/null
+++ b/pixel.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   pixel.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/01/19 13:52:11 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/01/19 13:58:03 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef PIXEL_H
+# define PIXEL_H
+
+# include "color.h"
+# include "fractol.h"
+
+void   *get_pixel(t_img *img, int x, int y);
+void   ft_putpx_img(t_img *img, int x, int y, t_color c);
+t_color        get_img_pixel_color(t_img *img, int x, int y);
+
+#endif //PIXEL_H