add option to choose rendered image type

This commit is contained in:
Bruno BELANYI 2020-11-12 00:14:58 +01:00
parent df1521be7e
commit e3bbb9aee4
3 changed files with 38 additions and 3 deletions

View file

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "buddhabrot.h"
#include "image.h" #include "image.h"
#include "mandelbrot.h" #include "mandelbrot.h"
#include "options.h" #include "options.h"
@ -14,7 +15,15 @@ int main(int argc, char *argv[]) {
if (!image) if (!image)
err(EXIT_FAILURE, "could not allocate image"); err(EXIT_FAILURE, "could not allocate image");
switch (opt.render) {
case BUDDHABROT:
buddhabrot(image, opt.max_iter);
break;
case MANDELBROT:
mandelbrot(image, opt.max_iter); mandelbrot(image, opt.max_iter);
break;
}
print_ppm(image, opt.output); print_ppm(image, opt.output);
fclose(opt.output); fclose(opt.output);

View file

@ -2,20 +2,29 @@
#include <err.h> #include <err.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#define ARR_SIZE(Arr) (sizeof(Arr) / sizeof(*Arr))
static const char *methods[] = {
[MANDELBROT] = "mandelbrot",
[BUDDHABROT] = "buddhabrot",
};
struct options parse_options(int *argc, char **argv[]) { struct options parse_options(int *argc, char **argv[]) {
struct options opts = { struct options opts = {
.output = stdout, .output = stdout,
.w = 960, .w = 960,
.h = 540, .h = 540,
.max_iter = 100, .max_iter = 100,
.render = BUDDHABROT,
}; };
opterr = 0; // Deactivate error message from `getopt` opterr = 0; // Deactivate error message from `getopt`
int opt; int opt;
while ((opt = getopt(*argc, *argv, "h:m:o:w:")) != -1) { while ((opt = getopt(*argc, *argv, "h:m:o:r:w:")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
if (!sscanf(optarg, "%zu", &opts.h)) if (!sscanf(optarg, "%zu", &opts.h))
@ -29,13 +38,26 @@ struct options parse_options(int *argc, char **argv[]) {
if (!freopen(optarg, "w", opts.output)) if (!freopen(optarg, "w", opts.output))
err(EXIT_FAILURE, "could not open output file"); err(EXIT_FAILURE, "could not open output file");
break; break;
case 'r': {
size_t i;
for (i = 0; i < ARR_SIZE(methods); ++i)
if (!strcmp(methods[i], optarg)) {
opts.render = i;
break;
}
if (i == ARR_SIZE(methods))
errx(EXIT_FAILURE, "could not parse render");
break;
}
case 'w': case 'w':
if (!sscanf(optarg, "%zu", &opts.w)) if (!sscanf(optarg, "%zu", &opts.w))
errx(EXIT_FAILURE, "could not parse width"); errx(EXIT_FAILURE, "could not parse width");
break; break;
default: default:
fprintf(stderr, fprintf(stderr,
"Usage: %s [-o FILE] [-m MAX_ITER] [-h HEIGHT] [-w WIDTH]\n", "Usage: %s "
"[-o FILE] [-i MAX_ITER] [-h HEIGHT] [-w WIDTH] "
"[-r <buddhabrot|mandelbrot>]\n",
(*argv)[0]); (*argv)[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -9,6 +9,10 @@ struct options {
size_t w; size_t w;
size_t h; size_t h;
size_t max_iter; size_t max_iter;
enum {
BUDDHABROT,
MANDELBROT,
} render;
}; };
struct options parse_options(int *argc, char **argv[]); struct options parse_options(int *argc, char **argv[]);