Skip to content

Commit ab91c18

Browse files
committed
Extract proper graph_foo.c. Depend on .o files.
1 parent c6aa973 commit ab91c18

8 files changed

+191
-123
lines changed

GNUmakefile

+28-7
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,36 @@ else
8282
${SILENT}touch .has_mpc
8383
endif
8484

85-
${THE_TESTS}: .has_check ${THE_LIBRARY} check_mandelbrot.c
86-
${SILENT}${CC} ${CFLAGS} ${CHECK_CFLAGS} -o ${THE_TESTS} check_mandelbrot.c ${THE_LIBRARY} ${LIBS} ${CAIRO_LIBS} ${GD_LIBS} ${IMLIB2_LIBS} ${MPC_LIBS} ${CHECK_LIBS}
85+
check_mandelbrot.o: .has_check mandelbrot.h mandelbrot.c
86+
${SILENT}${CC} ${CFLAGS} ${CHECK_CFLAGS} -c check_mandelbrot.c
8787

88-
${THE_LIBRARY}: .has_cairo .has_gd .has_imlib2 .has_mpc graph_cairo.h graph_gd.h graph_imlib2.h graph.h graph.c mandelbrot.h mandelbrot.c mandelbrot_mpc.c
89-
${SILENT}${CC} ${CFLAGS} ${CAIRO_CFLAGS} ${GD_CFLAGS} ${IMLIB2_CFLAGS} -c graph.c
88+
graph.o: graph.h graph.c
89+
${SILENT}${CC} ${CFLAGS} -c graph.c
90+
91+
graph_cairo.o: .has_cairo graph.h graph_cairo.h graph_cairo.c
92+
${SILENT}${CC} ${CFLAGS} ${CAIRO_CFLAGS} -c graph_cairo.c
93+
94+
graph_gd.o: .has_gd graph.h graph_gd.h graph_gd.c
95+
${SILENT}${CC} ${CFLAGS} ${GD_CFLAGS} -c graph_gd.c
96+
97+
graph_imlib2.o: .has_imlib2 graph.h graph_imlib2.h graph_imlib2.c
98+
${SILENT}${CC} ${CFLAGS} ${IMLIB2_CFLAGS} -c graph_imlib2.c
99+
100+
main.o: mandelbrot.h main.c
101+
${SILENT}${CC} ${CFLAGS} -c main.c
102+
103+
mandelbrot.o: mandelbrot.h mandelbrot.c
90104
${SILENT}${CC} ${CFLAGS} -c mandelbrot.c
105+
106+
mandelbrot_mpc.o: .has_mpc mandelbrot.h mandelbrot_mpc.c
91107
${SILENT}${CC} ${CFLAGS} ${MPC_CFLAGS} -c mandelbrot_mpc.c
92-
${SILENT}ar rc ${THE_LIBRARY} graph.o mandelbrot.o mandelbrot_mpc.o
108+
109+
${THE_TESTS}: ${THE_LIBRARY} check_mandelbrot.o
110+
${SILENT}${CC} ${LDFLAGS} ${THE_LIBRARY} ${LIBS} ${CAIRO_LIBS} ${GD_LIBS} ${IMLIB2_LIBS} ${MPC_LIBS} ${CHECK_LIBS} check_mandelbrot.o -o ${THE_TESTS}
111+
112+
${THE_LIBRARY}: graph.o graph_cairo.o graph_gd.o graph_imlib2.o mandelbrot.o mandelbrot_mpc.o
113+
${SILENT}ar rc ${THE_LIBRARY} graph.o graph_cairo.o graph_gd.o graph_imlib2.o mandelbrot.o mandelbrot_mpc.o
93114
${SILENT}ranlib ${THE_LIBRARY}
94115

95-
${THE_PROGRAM}: ${THE_LIBRARY} mandelbrot.h main.c
96-
${SILENT}${CC} ${CFLAGS} -o ${THE_PROGRAM} main.c ${THE_LIBRARY} ${LIBS} ${CAIRO_LIBS} ${GD_LIBS} ${IMLIB2_LIBS} ${MPC_LIBS}
116+
${THE_PROGRAM}: ${THE_LIBRARY} main.o
117+
${SILENT}${CC} ${LDFLAGS} ${THE_LIBRARY} ${LIBS} ${CAIRO_LIBS} ${GD_LIBS} ${IMLIB2_LIBS} ${MPC_LIBS} main.o -o ${THE_PROGRAM}

graph.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#ifndef _GRAPH_H_
2+
#define _GRAPH_H_
3+
14
#include <complex.h>
5+
#include <stddef.h>
26

37
typedef struct extreme_coordinates {
48
complex double lower_left;
@@ -30,3 +34,5 @@ void graph_destroy(const graph_t);
3034
complex double graph_get_coordinates(graph_t, const size_t, const size_t);
3135
void graph_set_pixel(const graph_t, const size_t, const size_t, const size_t);
3236
void graph_write(const graph_t, const char *);
37+
38+
#endif

graph_cairo.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <cairo/cairo.h>
2+
#include "graph_cairo.h"
3+
4+
void
5+
graph_backend_cairo_create(graph_t *graph)
6+
{
7+
graph->image = cairo_create(
8+
cairo_image_surface_create(CAIRO_FORMAT_RGB24,
9+
graph->width, graph->height));
10+
cairo_set_line_width(graph->image, 0.1);
11+
}
12+
13+
void
14+
graph_backend_cairo_set_pixel(const graph_t graph,
15+
const size_t horizontal, const size_t vertical,
16+
const size_t colormap_entry)
17+
{
18+
cairo_rectangle(graph.image, horizontal, vertical, 1, 1);
19+
cairo_set_source_rgb(graph.image,
20+
graph.colormap[colormap_entry][0] / 255.0,
21+
graph.colormap[colormap_entry][1] / 255.0,
22+
graph.colormap[colormap_entry][2] / 255.0);
23+
cairo_fill(graph.image);
24+
}
25+
26+
void
27+
graph_backend_cairo_write(const graph_t graph, const char *outputfile)
28+
{
29+
cairo_surface_write_to_png(cairo_get_target(graph.image), outputfile);
30+
}
31+
32+
void
33+
graph_backend_cairo_destroy(const graph_t graph)
34+
{
35+
cairo_surface_destroy(cairo_get_target(graph.image));
36+
cairo_destroy(graph.image);
37+
}
38+
39+
graph_backend_t graph_backend_cairo = {
40+
"cairo",
41+
graph_backend_cairo_create,
42+
graph_backend_cairo_set_pixel,
43+
graph_backend_cairo_write,
44+
graph_backend_cairo_destroy,
45+
};

graph_cairo.h

+7-39
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
1-
#include <cairo/cairo.h>
1+
#include "graph.h"
22

3-
static void
4-
graph_backend_cairo_create(graph_t *graph)
5-
{
6-
graph->image = cairo_create(
7-
cairo_image_surface_create(CAIRO_FORMAT_RGB24,
8-
graph->width, graph->height));
9-
cairo_set_line_width(graph->image, 0.1);
10-
}
3+
void graph_backend_cairo_create(graph_t *);
114

12-
static void
13-
graph_backend_cairo_set_pixel(const graph_t graph,
14-
const size_t horizontal, const size_t vertical,
15-
const size_t colormap_entry)
16-
{
17-
cairo_rectangle(graph.image, horizontal, vertical, 1, 1);
18-
cairo_set_source_rgb(graph.image,
19-
graph.colormap[colormap_entry][0] / 255.0,
20-
graph.colormap[colormap_entry][1] / 255.0,
21-
graph.colormap[colormap_entry][2] / 255.0);
22-
cairo_fill(graph.image);
23-
}
5+
void graph_backend_cairo_set_pixel(const graph_t,
6+
const size_t, const size_t, const size_t);
247

25-
static void
26-
graph_backend_cairo_write(const graph_t graph, const char *outputfile)
27-
{
28-
cairo_surface_write_to_png(cairo_get_target(graph.image), outputfile);
29-
}
8+
void graph_backend_cairo_write(const graph_t, const char *);
309

31-
static void
32-
graph_backend_cairo_destroy(const graph_t graph)
33-
{
34-
cairo_surface_destroy(cairo_get_target(graph.image));
35-
cairo_destroy(graph.image);
36-
}
10+
void graph_backend_cairo_destroy(const graph_t);
3711

38-
static graph_backend_t graph_backend_cairo = {
39-
"cairo",
40-
graph_backend_cairo_create,
41-
graph_backend_cairo_set_pixel,
42-
graph_backend_cairo_write,
43-
graph_backend_cairo_destroy,
44-
};
12+
graph_backend_t graph_backend_cairo;

graph_gd.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <gd.h>
2+
#include "graph_gd.h"
3+
4+
void
5+
graph_backend_gd_create(graph_t *graph)
6+
{
7+
graph->image = gdImageCreate(graph->width, graph->height);
8+
for (size_t i = 0; i < NUM_COLORS; i++) {
9+
gdImageColorAllocate(graph->image,
10+
graph->colormap[i][0],
11+
graph->colormap[i][1],
12+
graph->colormap[i][2]);
13+
}
14+
}
15+
16+
void
17+
graph_backend_gd_set_pixel(const graph_t graph,
18+
const size_t horizontal, const size_t vertical,
19+
const size_t colormap_entry)
20+
{
21+
gdImageSetPixel(graph.image, horizontal, vertical, colormap_entry);
22+
}
23+
24+
void
25+
graph_backend_gd_write(const graph_t graph, const char *outputfile)
26+
{
27+
FILE *pngout = fopen(outputfile, "wb");
28+
gdImagePng(graph.image, pngout);
29+
fclose(pngout);
30+
}
31+
32+
void
33+
graph_backend_gd_destroy(const graph_t graph)
34+
{
35+
gdImageDestroy(graph.image);
36+
}
37+
38+
graph_backend_t graph_backend_gd = {
39+
"gd",
40+
graph_backend_gd_create,
41+
graph_backend_gd_set_pixel,
42+
graph_backend_gd_write,
43+
graph_backend_gd_destroy,
44+
};
45+

graph_gd.h

+7-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
1-
#include <gd.h>
1+
#include "graph.h"
22

3-
static void
4-
graph_backend_gd_create(graph_t *graph)
5-
{
6-
graph->image = gdImageCreate(graph->width, graph->height);
7-
for (size_t i = 0; i < NUM_COLORS; i++) {
8-
gdImageColorAllocate(graph->image,
9-
graph->colormap[i][0],
10-
graph->colormap[i][1],
11-
graph->colormap[i][2]);
12-
}
13-
}
3+
void graph_backend_gd_create(graph_t *);
144

15-
static void
16-
graph_backend_gd_set_pixel(const graph_t graph,
17-
const size_t horizontal, const size_t vertical,
18-
const size_t colormap_entry)
19-
{
20-
gdImageSetPixel(graph.image, horizontal, vertical, colormap_entry);
21-
}
5+
void graph_backend_gd_set_pixel(const graph_t,
6+
const size_t, const size_t, const size_t);
227

23-
static void
24-
graph_backend_gd_write(const graph_t graph, const char *outputfile)
25-
{
26-
FILE *pngout = fopen(outputfile, "wb");
27-
gdImagePng(graph.image, pngout);
28-
fclose(pngout);
29-
}
8+
void graph_backend_gd_write(const graph_t, const char *);
309

31-
static void
32-
graph_backend_gd_destroy(const graph_t graph)
33-
{
34-
gdImageDestroy(graph.image);
35-
}
10+
void graph_backend_gd_destroy(const graph_t);
3611

37-
static graph_backend_t graph_backend_gd = {
38-
"gd",
39-
graph_backend_gd_create,
40-
graph_backend_gd_set_pixel,
41-
graph_backend_gd_write,
42-
graph_backend_gd_destroy,
43-
};
12+
graph_backend_t graph_backend_gd;

graph_imlib2.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <Imlib2.h>
2+
#include "graph_imlib2.h"
3+
4+
void
5+
graph_backend_imlib2_create(graph_t *graph)
6+
{
7+
graph->image = imlib_create_image(graph->width, graph->height);
8+
imlib_context_set_image(graph->image);
9+
}
10+
11+
void
12+
graph_backend_imlib2_set_pixel(const graph_t graph,
13+
const size_t horizontal, const size_t vertical,
14+
const size_t colormap_entry)
15+
{
16+
imlib_context_set_color(
17+
graph.colormap[colormap_entry][0],
18+
graph.colormap[colormap_entry][1],
19+
graph.colormap[colormap_entry][2],
20+
255);
21+
imlib_image_fill_rectangle(horizontal, vertical, 1, 1);
22+
}
23+
24+
void
25+
graph_backend_imlib2_write(const graph_t graph, const char *outputfile)
26+
{
27+
(void)graph;
28+
imlib_image_set_format("png");
29+
imlib_save_image(outputfile);
30+
}
31+
32+
void
33+
graph_backend_imlib2_destroy(const graph_t graph)
34+
{
35+
(void)graph;
36+
imlib_free_image();
37+
}
38+
39+
graph_backend_t graph_backend_imlib2 = {
40+
"imlib2",
41+
graph_backend_imlib2_create,
42+
graph_backend_imlib2_set_pixel,
43+
graph_backend_imlib2_write,
44+
graph_backend_imlib2_destroy,
45+
};
46+

graph_imlib2.h

+7-39
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
1-
#include <Imlib2.h>
1+
#include "graph.h"
22

3-
static void
4-
graph_backend_imlib2_create(graph_t *graph)
5-
{
6-
graph->image = imlib_create_image(graph->width, graph->height);
7-
imlib_context_set_image(graph->image);
8-
}
3+
void graph_backend_imlib2_create(graph_t *);
94

10-
static void
11-
graph_backend_imlib2_set_pixel(const graph_t graph,
12-
const size_t horizontal, const size_t vertical,
13-
const size_t colormap_entry)
14-
{
15-
imlib_context_set_color(
16-
graph.colormap[colormap_entry][0],
17-
graph.colormap[colormap_entry][1],
18-
graph.colormap[colormap_entry][2],
19-
255);
20-
imlib_image_fill_rectangle(horizontal, vertical, 1, 1);
21-
}
5+
void graph_backend_imlib2_set_pixel(const graph_t,
6+
const size_t, const size_t, const size_t);
227

23-
static void
24-
graph_backend_imlib2_write(const graph_t graph, const char *outputfile)
25-
{
26-
(void)graph;
27-
imlib_image_set_format("png");
28-
imlib_save_image(outputfile);
29-
}
8+
void graph_backend_imlib2_write(const graph_t, const char *);
309

31-
static void
32-
graph_backend_imlib2_destroy(const graph_t graph)
33-
{
34-
(void)graph;
35-
imlib_free_image();
36-
}
10+
void graph_backend_imlib2_destroy(const graph_t);
3711

38-
static graph_backend_t graph_backend_imlib2 = {
39-
"imlib2",
40-
graph_backend_imlib2_create,
41-
graph_backend_imlib2_set_pixel,
42-
graph_backend_imlib2_write,
43-
graph_backend_imlib2_destroy,
44-
};
12+
graph_backend_t graph_backend_imlib2;

0 commit comments

Comments
 (0)