Skip to content

Commit 2b6105c

Browse files
committed
updated option version to accept output name and added makefile
1 parent 21359c4 commit 2b6105c

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

modern/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
OPTS=-Ofast
2+
LIBS=-lpthread
3+
4+
all: mandelbrot
5+
6+
mandelbrot: mandelbrot_2.c
7+
$(CC) $(OPTS) -o $@ $^ $(LIBS)
8+
9+
.PHONY: clean distclean
10+
11+
clean:
12+
rm -f *~ *.o *# *.ppm
13+
14+
distclean: clean
15+
rm -f mandelbrot

modern/mandelbrot_2.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#error "Can't have more threads than pixel rows!"
2525
#endif
2626

27-
void set_pixel(unsigned char *img, int x, int y, unsigned char *color);
2827
unsigned char *create_image(int w, int h);
28+
void help(FILE *);
29+
void set_pixel(unsigned char *img, int x, int y, unsigned char *color);
2930
void write_ppm(char *name, int w, int h, unsigned char *img);
3031

3132
unsigned char **palette;
@@ -98,16 +99,19 @@ int main(int argc, char *argv[]) {
9899
struct timespec tspec;
99100
int py,i, opt;
100101
pthread_t *rowthreads;
101-
char opts[]="h:i:t:w:";
102+
char opts[]="h:i:o:t:w:?";
102103
struct option long_opts[] = {
103104
{ "height", 1, NULL, 'h'},
104105
{ "width", 1, NULL, 'w'},
105106
{ "iterations", 1, NULL, 'i'},
106107
{ "max_iter", 1, NULL, 'i'},
108+
{ "out", 1, NULL, 'o'},
107109
{ "threads", 1, NULL, 't'},
108110
{ "num_threads", 1, NULL, 't'},
111+
{ "help", 0, NULL, '?'},
109112
{ NULL, 0, NULL, '\0'}
110113
};
114+
char *outname="mandelbrot.ppm";
111115

112116
while((opt=getopt_long(argc, argv, opts, long_opts, NULL))!=-1) {
113117
switch(opt) {
@@ -143,6 +147,11 @@ int main(int argc, char *argv[]) {
143147
return -1;
144148
}
145149
break;
150+
case 'o':
151+
outname=optarg;
152+
break;
153+
default:
154+
help(stdout);
146155
}
147156
}
148157
if (num_threads>height) {
@@ -182,7 +191,7 @@ int main(int argc, char *argv[]) {
182191
rowthread(NULL);
183192
for(i=0; i<num_threads-1; i++) pthread_join(rowthreads[i], NULL);
184193

185-
write_ppm("mandelbrot.ppm", width, height, img);
194+
write_ppm(outname, width, height, img);
186195

187196
clock_gettime(CLOCK_REALTIME, &tspec);
188197
end = tspec.tv_sec+1e-9*tspec.tv_nsec;
@@ -215,3 +224,16 @@ void write_ppm(char *name, int w, int h, unsigned char *img) {
215224
fwrite(img, 3, w*h, out);
216225
fclose(out);
217226
}
227+
228+
void help(FILE *out) {
229+
fprintf(out, "Usage: mandelbrot <options>\n");
230+
fprintf(out, "\t-w\t--width\t\tImage width (%d)\n", width);
231+
fprintf(out, "\t-h\t--height\tImage height (%d)\n", height);
232+
fprintf(out, "\t-i\t--iterations\tMaximum number of iterations (%d)\n", max_iter);
233+
fprintf(out, "\t-t\t--threads\tNumber of threads (%d)\n", num_threads);
234+
fprintf(out, "\t-o\t--out\t\tOutput name (mandelbrot.ppm)\n");
235+
if (out==stdout) exit(0);
236+
exit(-1);
237+
}
238+
239+

0 commit comments

Comments
 (0)