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

View File

@ -2,20 +2,29 @@
#include <err.h>
#include <stdlib.h>
#include <string.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 opts = {
.output = stdout,
.w = 960,
.h = 540,
.max_iter = 100,
.render = BUDDHABROT,
};
opterr = 0; // Deactivate error message from `getopt`
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) {
case '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))
err(EXIT_FAILURE, "could not open output file");
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':
if (!sscanf(optarg, "%zu", &opts.w))
errx(EXIT_FAILURE, "could not parse width");
break;
default:
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]);
exit(EXIT_FAILURE);
}

View File

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