Skip to content

Commit 096594e

Browse files
author
PythonGermany
committed
Image generator added
1 parent 97c8536 commit 096594e

File tree

6 files changed

+156
-48
lines changed

6 files changed

+156
-48
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: rburgsta <[email protected]. +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2022/11/25 15:38:03 by rburgsta #+# #+# #
9-
# Updated: 2022/11/30 02:30:14 by rburgsta ### ########.fr #
9+
# Updated: 2022/12/02 10:42:45 by rburgsta ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -30,17 +30,23 @@ $(NAME): $(OBJ)
3030
make -C $(FT_PATH)
3131
cc $(FLAGS) -o $(NAME) $(OBJ) -L$(MLX_PATH) -lmlx42 -L$(FT_PATH) -lft -L$(GL_INC) -lglfw -ldl
3232

33+
gen : fract-gen.o fractals.o colors.o
34+
make -C $(FT_PATH)
35+
cc $(FLAGS) -o fract-gen $? -L$(FT_PATH) -lft
36+
3337
%.o: %.c
3438
cc -c $(FLAGS) -I$(MLX_INC) -I$(LIBFT_INC) $^
3539

3640
clean :
3741
make -C $(MLX_PATH) clean
3842
make -C $(FT_PATH) clean
3943
rm -f $(OBJ)
44+
rm -f fract-gen.o
4045

4146
fclean: clean
4247
make -C $(MLX_PATH) fclean
4348
make -C $(FT_PATH) fclean
4449
rm -f $(NAME)
50+
rm -f fract-gen
4551

4652
re: fclean all

colors.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ int range(int out_max, int in_max, int in_curr)
1919
return ((double)in_curr / in_max * out_max);
2020
}
2121

22-
uint32_t get_color(int r, int g, int b, int a)
22+
int get_color(int r, int g, int b, int a)
2323
{
2424
return ((r <<= 24) + (g <<= 16) + (b <<= 8) + (a & 0xFF));
2525
}
2626

27-
uint32_t tcr(int iter, int curr, int *cs)
27+
int tcr(int iter, int curr, int *cs)
2828
{
2929
int c;
3030
int fa;
@@ -39,7 +39,7 @@ uint32_t tcr(int iter, int curr, int *cs)
3939
if (c >= COLOR_MAX)
4040
c -= COLOR_MAX;
4141
if (*cs < 256 && curr < iter / 2 && c < 256 * ++fa)
42-
return (get_color(c, 0, 0, c));
42+
return (get_color(c, 0, 0, 255));
4343
if ((*cs >= 256 || curr >= iter / 2) && c < 256 * ++fa)
4444
return (get_color(255, 0, 255 - c, 255));
4545
if (c < 256 * ++fa)
@@ -52,25 +52,3 @@ uint32_t tcr(int iter, int curr, int *cs)
5252
return (get_color(0, 256 * fa - c - 1, 255, 255));
5353
return (get_color(c - 256 * (fa - 1), 0, 255, 255));
5454
}
55-
56-
// uint32_t lin_inter(uint32_t c1, uint32_t c2, double f)
57-
// {
58-
// uint8_t dr;
59-
// uint8_t dg;
60-
// uint8_t db;
61-
// uint8_t da;
62-
63-
// dr = (c2 - c1) << 24;
64-
// dg = (c2 - c1) << 16;
65-
// db = (c2 - c1) << 8;
66-
// da = (c2 - c1) & 0xFF;
67-
// return (get_color((c2 << 24) + dr * f, (c2 << 16) + dg * f,
68-
// (c2 << 8) + db * f, (c2 & 0xFF) + da * f));
69-
70-
// }
71-
72-
// uint32_t interpolate(int iter, double curr, int cs)
73-
// {
74-
// return (lin_inter(tcr(iter, curr, cs),
75-
// tcr(iter, curr + 1, cs), curr - (int)curr));
76-
// }

fract-gen.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include "fractol.h"
5+
#include "libft.h"
6+
7+
int chk_precision(char *str)
8+
{
9+
int i;
10+
int precision;
11+
12+
i = 0;
13+
precision = 0;
14+
if (str == NULL)
15+
return (1);
16+
if (str[i] == '-')
17+
i++;
18+
while (str[i] != '\0')
19+
{
20+
if (str[i] == '.' && precision)
21+
return (1);
22+
else if (str[i] == '.' && !precision)
23+
precision = 1;
24+
else if (!(str[i] >= '0' && str[i] <= '9'))
25+
return (1);
26+
i++;
27+
}
28+
return (0);
29+
}
30+
31+
double ft_precision_atoi(char *str)
32+
{
33+
double nb;
34+
int i;
35+
double precision;
36+
37+
nb = ft_atoi(str);
38+
precision = 0.1;
39+
i = 0;
40+
if (str[i] == '-')
41+
precision *= -1;
42+
while (str[i] && str[i] != '.')
43+
i++;
44+
if (str[i] == '.')
45+
{
46+
i++;
47+
while (str[i])
48+
{
49+
nb += precision * (str[i++] - '0');
50+
precision /= 10;
51+
}
52+
}
53+
return (nb);
54+
}
55+
56+
void populate_dta(t_dta *dta)
57+
{
58+
dta->scale = 2.5;
59+
dta->xs = -0.7;
60+
dta->ys = 0;
61+
dta->iter = 350;
62+
dta->cs = 25;
63+
dta->cs_active = 0;
64+
if (dta->argv[2][0] == 'j')
65+
{
66+
dta->re = ft_precision_atoi(dta->argv[3]);
67+
dta->im = ft_precision_atoi(dta->argv[4]);
68+
dta->f = &julia;
69+
}
70+
else if (dta->argv[2][0] == 'm')
71+
dta->f = &mandelbrot;
72+
else if (dta->argv[2][0] == 't')
73+
dta->f = &tricorn;
74+
}
75+
76+
void calculate_window(t_dta *dta)
77+
{
78+
FILE *file;
79+
int size;
80+
int i;
81+
int i2;
82+
int c;
83+
84+
file = fopen("test.ppm", "wb");
85+
size = ft_atoi(dta->argv[1]);
86+
fprintf(file, "P6\n%d\n%d\n255\n", size, size);
87+
i = 0;
88+
while (i < size)
89+
{
90+
i2 = 0;
91+
while (i2 < size)
92+
{
93+
dta->x = (i2 - size / 2.0) * dta->scale / size + dta->xs;
94+
dta->y = (size / 2.0 - i) * dta->scale / size + dta->ys;
95+
c = tcr(dta->iter, (*dta->f)(dta), &dta->cs);
96+
fwrite((unsigned char *)(&c) + 3, 1, 1, file);
97+
fwrite((unsigned char *)(&c) + 2, 1, 1, file);
98+
fwrite((unsigned char *)(&c) + 1, 1, 1, file);
99+
i2++;
100+
}
101+
if (i % 100 == 0)
102+
printf("Line: %i\n",i );
103+
i++;
104+
}
105+
fclose(file);
106+
}
107+
108+
int main(int argc, char **argv)
109+
{
110+
t_dta dta[1];
111+
112+
if (argc < 2)
113+
return (1);
114+
/*if (argc < 3 || strlen(argv[2]) != 1 || \
115+
(argv[2][0] != 'j' && argv[2][0] != 'm' && argv[2][0] != 't'))
116+
return (printf("Use: %s ['m' or \"j [double] [double]\" or 't']\n", \
117+
argv[0]), 1);
118+
if (argv[2][0] == 'j' && \
119+
(argc != 5 || chk_precision(argv[3]) || chk_precision(argv[4])))
120+
return (printf("Use: %s j [double] [double]\n", argv[0]), 1);
121+
else if (argc != 3 && (argv[2][0] == 'm' || argv[2][0] == 't'))
122+
return (printf("Use: %s %s\n", argv[0], argv[2]), 1);*/
123+
dta->argv = argv;
124+
populate_dta(dta);
125+
calculate_window(dta);
126+
return (0);
127+
}

fractol.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
# include "MLX42.h"
1717

18-
# define WIDTH 500
19-
# define HEIGHT 500
18+
# define WIDTH 400
19+
# define HEIGHT 400
2020

2121
typedef struct s_dta
2222
{
@@ -46,23 +46,22 @@ typedef struct s_coord
4646
} t_coord;
4747

4848
//Fractals
49-
int julia(t_dta *dta);
50-
int mandelbrot(t_dta *dta);
51-
int tricorn(t_dta *dta);
49+
int julia(t_dta *dta);
50+
int mandelbrot(t_dta *dta);
51+
int tricorn(t_dta *dta);
5252

5353
//Colors
54-
uint32_t get_color(int r, int g, int b, int a);
55-
uint32_t tcr(int iter, int curr, int *cs);
56-
//uint32_t interpolate(int iter, double curr, int cs);
54+
int get_color(int r, int g, int b, int a);
55+
int tcr(int iter, int curr, int *cs);
5756

5857
//Hooks
59-
void key_hook(void *dta);
60-
void scroll_hook(double xdelta, double ydelta, void *param);
61-
void mouse_hook(mouse_key_t bt, action_t ac, \
58+
void key_hook(void *dta);
59+
void scroll_hook(double xdelta, double ydelta, void *param);
60+
void mouse_hook(mouse_key_t bt, action_t ac, \
6261
modifier_key_t md, void *dta);
6362

6463
//Main
65-
void populate_dta(t_dta *dta);
66-
void calculate_window(t_dta *dta);
64+
void populate_dta(t_dta *dta);
65+
void calculate_window(t_dta *dta);
6766

6867
#endif

hooks.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ void key_hook(void *dta)
6464
int msec = diff * 1000 / CLOCKS_PER_SEC;
6565
ms_g += msec%1000;
6666
i++;
67-
printf("%d milliseconds, %d average\n\
68-
Scale: %.20f, %d iter\n\
69-
Size of set (current window view = %fmm): %fkm\n\
70-
Xparam %f, Yparam %f\n\
71-
Cshift: %d\n\n", \
67+
printf("%d milliseconds, %d average\nScale: %.15f, %d iter\nSize of set (current window view = %fmm): %fkm\nXparam %f, Yparam %f\nCshift: %d\nXshift: %.15f\nYshift: %.15f\n\n",\
7268
msec%1000, ms_g / i, t->scale, t->iter, (float)WIDTH / 100, \
73-
4 / t->scale / 1000000 * (WIDTH / 100), t->re, t->im, t->cs);
69+
4 / t->scale / 1000000 * (WIDTH / 100), t->re, t->im, t->cs, \
70+
t->xs, t->ys);
7471

7572
}
7673

main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
/* ::: :::::::: */
44
/* main.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: rburgsta <rburgsta@student.42.fr> +#+ +:+ +#+ */
6+
/* By: rburgsta <rburgsta@student.42heilbronn. +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/11/27 16:31:38 by rburgsta #+# #+# */
9-
/* Updated: 2022/12/01 12:14:41 by rburgsta ### ########.fr */
9+
/* Updated: 2022/12/02 21:54:39 by rburgsta ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "fractol.h"
1414
#include "libft.h"
15+
#include <stdio.h>
1516

1617
int chk_precision(char *str)
1718
{
@@ -96,7 +97,7 @@ void calculate_window(t_dta *dta)
9697
dta->x = (i2 - WIDTH / 2.0) * dta->scale / WIDTH + dta->xs;
9798
dta->y = (HEIGHT / 2.0 - i) * dta->scale / HEIGHT + dta->ys;
9899
mlx_put_pixel(dta->img, i2++, i, \
99-
tcr(dta->iter, (*dta->f)(dta), &dta->cs));
100+
tcr(dta->iter, (*dta->f)(dta), &dta->cs));
100101
}
101102
i++;
102103
}

0 commit comments

Comments
 (0)