|
24 | 24 | #error "Can't have more threads than pixel rows!"
|
25 | 25 | #endif
|
26 | 26 |
|
27 |
| -void set_pixel(unsigned char *img, int x, int y, unsigned char *color); |
28 | 27 | 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); |
29 | 30 | void write_ppm(char *name, int w, int h, unsigned char *img);
|
30 | 31 |
|
31 | 32 | unsigned char **palette;
|
@@ -98,16 +99,19 @@ int main(int argc, char *argv[]) {
|
98 | 99 | struct timespec tspec;
|
99 | 100 | int py,i, opt;
|
100 | 101 | pthread_t *rowthreads;
|
101 |
| - char opts[]="h:i:t:w:"; |
| 102 | + char opts[]="h:i:o:t:w:?"; |
102 | 103 | struct option long_opts[] = {
|
103 | 104 | { "height", 1, NULL, 'h'},
|
104 | 105 | { "width", 1, NULL, 'w'},
|
105 | 106 | { "iterations", 1, NULL, 'i'},
|
106 | 107 | { "max_iter", 1, NULL, 'i'},
|
| 108 | + { "out", 1, NULL, 'o'}, |
107 | 109 | { "threads", 1, NULL, 't'},
|
108 | 110 | { "num_threads", 1, NULL, 't'},
|
| 111 | + { "help", 0, NULL, '?'}, |
109 | 112 | { NULL, 0, NULL, '\0'}
|
110 | 113 | };
|
| 114 | + char *outname="mandelbrot.ppm"; |
111 | 115 |
|
112 | 116 | while((opt=getopt_long(argc, argv, opts, long_opts, NULL))!=-1) {
|
113 | 117 | switch(opt) {
|
@@ -143,6 +147,11 @@ int main(int argc, char *argv[]) {
|
143 | 147 | return -1;
|
144 | 148 | }
|
145 | 149 | break;
|
| 150 | + case 'o': |
| 151 | + outname=optarg; |
| 152 | + break; |
| 153 | + default: |
| 154 | + help(stdout); |
146 | 155 | }
|
147 | 156 | }
|
148 | 157 | if (num_threads>height) {
|
@@ -182,7 +191,7 @@ int main(int argc, char *argv[]) {
|
182 | 191 | rowthread(NULL);
|
183 | 192 | for(i=0; i<num_threads-1; i++) pthread_join(rowthreads[i], NULL);
|
184 | 193 |
|
185 |
| - write_ppm("mandelbrot.ppm", width, height, img); |
| 194 | + write_ppm(outname, width, height, img); |
186 | 195 |
|
187 | 196 | clock_gettime(CLOCK_REALTIME, &tspec);
|
188 | 197 | 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) {
|
215 | 224 | fwrite(img, 3, w*h, out);
|
216 | 225 | fclose(out);
|
217 | 226 | }
|
| 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