From 35a54d05f0abd23a8aaa7f6bd5f0d9fe54d9266b Mon Sep 17 00:00:00 2001 From: Maik Herbers <maik.herbers@stud.tu-darmstadt.de> Date: Wed, 11 Jan 2023 15:31:59 +0100 Subject: [PATCH] Change colour scheme selection, add viridis colour map. * include/viridis_lut.h: New file. * include/colour.h (colour_type): New enum. (colour_function_t): New typedef. (get_colour_mono): Add `colour_type' parameter. (get_colour_cividis): Rename to ... (get_colour_cmap): ... this and add `colour_type' parameter. * src/colour.c (get_colour_mono): Update parameters here. (get_colour_cividis): Rename to ... (get_colour_cmap): ... this and update parameters. * src/main.c (init_options): Switch `monochromo' parameter to `type' to indicate colour. (fill): Use `colour_function_t' typedef. (main): Switch from option `-m' to `-t <type>' to select colour scheme. --- include/colour.h | 12 +- include/viridis_lut.h | 272 ++++++++++++++++++++++++++++++++++++++++++ src/colour.c | 14 ++- src/main.c | 33 +++-- 4 files changed, 314 insertions(+), 17 deletions(-) create mode 100644 include/viridis_lut.h diff --git a/include/colour.h b/include/colour.h index 9ea65ad..97a72d1 100644 --- a/include/colour.h +++ b/include/colour.h @@ -4,5 +4,13 @@ #include "common.h" -void get_colour_mono(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps); -void get_colour_cividis(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps); +enum colour_type { + colour_type_mono, + colour_type_cividis, + colour_type_viridis, +}; + +typedef void (*colour_function_t)(struct rgba_pix *, const mpc_t, struct temporaries, enum colour_type type); + +void get_colour_mono(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps, enum colour_type type); +void get_colour_cmap(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps, enum colour_type type); diff --git a/include/viridis_lut.h b/include/viridis_lut.h new file mode 100644 index 0000000..2f466d5 --- /dev/null +++ b/include/viridis_lut.h @@ -0,0 +1,272 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ + +#pragma once + +#include "common.h" + +/* + * Generated from matplotlib by + * import matplotlib as mpl + * cmap = mpl.cm.get_cmap('viridis') + * to_hex = lambda val: f"0x{int(round(255 * val)):02x}" + * for i in range(cmap.N): + * print("{{ {}, {}, {}, {} }},".format(*map(to_hex, cmap(i)))) + */ +struct rgba_pix viridis_lut[] = { + { 0x44, 0x01, 0x54, 0xff }, + { 0x44, 0x02, 0x56, 0xff }, + { 0x45, 0x04, 0x57, 0xff }, + { 0x45, 0x05, 0x59, 0xff }, + { 0x46, 0x07, 0x5a, 0xff }, + { 0x46, 0x08, 0x5c, 0xff }, + { 0x46, 0x0a, 0x5d, 0xff }, + { 0x46, 0x0b, 0x5e, 0xff }, + { 0x47, 0x0d, 0x60, 0xff }, + { 0x47, 0x0e, 0x61, 0xff }, + { 0x47, 0x10, 0x63, 0xff }, + { 0x47, 0x11, 0x64, 0xff }, + { 0x47, 0x13, 0x65, 0xff }, + { 0x48, 0x14, 0x67, 0xff }, + { 0x48, 0x16, 0x68, 0xff }, + { 0x48, 0x17, 0x69, 0xff }, + { 0x48, 0x18, 0x6a, 0xff }, + { 0x48, 0x1a, 0x6c, 0xff }, + { 0x48, 0x1b, 0x6d, 0xff }, + { 0x48, 0x1c, 0x6e, 0xff }, + { 0x48, 0x1d, 0x6f, 0xff }, + { 0x48, 0x1f, 0x70, 0xff }, + { 0x48, 0x20, 0x71, 0xff }, + { 0x48, 0x21, 0x73, 0xff }, + { 0x48, 0x23, 0x74, 0xff }, + { 0x48, 0x24, 0x75, 0xff }, + { 0x48, 0x25, 0x76, 0xff }, + { 0x48, 0x26, 0x77, 0xff }, + { 0x48, 0x28, 0x78, 0xff }, + { 0x48, 0x29, 0x79, 0xff }, + { 0x47, 0x2a, 0x7a, 0xff }, + { 0x47, 0x2c, 0x7a, 0xff }, + { 0x47, 0x2d, 0x7b, 0xff }, + { 0x47, 0x2e, 0x7c, 0xff }, + { 0x47, 0x2f, 0x7d, 0xff }, + { 0x46, 0x30, 0x7e, 0xff }, + { 0x46, 0x32, 0x7e, 0xff }, + { 0x46, 0x33, 0x7f, 0xff }, + { 0x46, 0x34, 0x80, 0xff }, + { 0x45, 0x35, 0x81, 0xff }, + { 0x45, 0x37, 0x81, 0xff }, + { 0x45, 0x38, 0x82, 0xff }, + { 0x44, 0x39, 0x83, 0xff }, + { 0x44, 0x3a, 0x83, 0xff }, + { 0x44, 0x3b, 0x84, 0xff }, + { 0x43, 0x3d, 0x84, 0xff }, + { 0x43, 0x3e, 0x85, 0xff }, + { 0x42, 0x3f, 0x85, 0xff }, + { 0x42, 0x40, 0x86, 0xff }, + { 0x42, 0x41, 0x86, 0xff }, + { 0x41, 0x42, 0x87, 0xff }, + { 0x41, 0x44, 0x87, 0xff }, + { 0x40, 0x45, 0x88, 0xff }, + { 0x40, 0x46, 0x88, 0xff }, + { 0x3f, 0x47, 0x88, 0xff }, + { 0x3f, 0x48, 0x89, 0xff }, + { 0x3e, 0x49, 0x89, 0xff }, + { 0x3e, 0x4a, 0x89, 0xff }, + { 0x3e, 0x4c, 0x8a, 0xff }, + { 0x3d, 0x4d, 0x8a, 0xff }, + { 0x3d, 0x4e, 0x8a, 0xff }, + { 0x3c, 0x4f, 0x8a, 0xff }, + { 0x3c, 0x50, 0x8b, 0xff }, + { 0x3b, 0x51, 0x8b, 0xff }, + { 0x3b, 0x52, 0x8b, 0xff }, + { 0x3a, 0x53, 0x8b, 0xff }, + { 0x3a, 0x54, 0x8c, 0xff }, + { 0x39, 0x55, 0x8c, 0xff }, + { 0x39, 0x56, 0x8c, 0xff }, + { 0x38, 0x58, 0x8c, 0xff }, + { 0x38, 0x59, 0x8c, 0xff }, + { 0x37, 0x5a, 0x8c, 0xff }, + { 0x37, 0x5b, 0x8d, 0xff }, + { 0x36, 0x5c, 0x8d, 0xff }, + { 0x36, 0x5d, 0x8d, 0xff }, + { 0x35, 0x5e, 0x8d, 0xff }, + { 0x35, 0x5f, 0x8d, 0xff }, + { 0x34, 0x60, 0x8d, 0xff }, + { 0x34, 0x61, 0x8d, 0xff }, + { 0x33, 0x62, 0x8d, 0xff }, + { 0x33, 0x63, 0x8d, 0xff }, + { 0x32, 0x64, 0x8e, 0xff }, + { 0x32, 0x65, 0x8e, 0xff }, + { 0x31, 0x66, 0x8e, 0xff }, + { 0x31, 0x67, 0x8e, 0xff }, + { 0x31, 0x68, 0x8e, 0xff }, + { 0x30, 0x69, 0x8e, 0xff }, + { 0x30, 0x6a, 0x8e, 0xff }, + { 0x2f, 0x6b, 0x8e, 0xff }, + { 0x2f, 0x6c, 0x8e, 0xff }, + { 0x2e, 0x6d, 0x8e, 0xff }, + { 0x2e, 0x6e, 0x8e, 0xff }, + { 0x2e, 0x6f, 0x8e, 0xff }, + { 0x2d, 0x70, 0x8e, 0xff }, + { 0x2d, 0x71, 0x8e, 0xff }, + { 0x2c, 0x71, 0x8e, 0xff }, + { 0x2c, 0x72, 0x8e, 0xff }, + { 0x2c, 0x73, 0x8e, 0xff }, + { 0x2b, 0x74, 0x8e, 0xff }, + { 0x2b, 0x75, 0x8e, 0xff }, + { 0x2a, 0x76, 0x8e, 0xff }, + { 0x2a, 0x77, 0x8e, 0xff }, + { 0x2a, 0x78, 0x8e, 0xff }, + { 0x29, 0x79, 0x8e, 0xff }, + { 0x29, 0x7a, 0x8e, 0xff }, + { 0x29, 0x7b, 0x8e, 0xff }, + { 0x28, 0x7c, 0x8e, 0xff }, + { 0x28, 0x7d, 0x8e, 0xff }, + { 0x27, 0x7e, 0x8e, 0xff }, + { 0x27, 0x7f, 0x8e, 0xff }, + { 0x27, 0x80, 0x8e, 0xff }, + { 0x26, 0x81, 0x8e, 0xff }, + { 0x26, 0x82, 0x8e, 0xff }, + { 0x26, 0x82, 0x8e, 0xff }, + { 0x25, 0x83, 0x8e, 0xff }, + { 0x25, 0x84, 0x8e, 0xff }, + { 0x25, 0x85, 0x8e, 0xff }, + { 0x24, 0x86, 0x8e, 0xff }, + { 0x24, 0x87, 0x8e, 0xff }, + { 0x23, 0x88, 0x8e, 0xff }, + { 0x23, 0x89, 0x8e, 0xff }, + { 0x23, 0x8a, 0x8d, 0xff }, + { 0x22, 0x8b, 0x8d, 0xff }, + { 0x22, 0x8c, 0x8d, 0xff }, + { 0x22, 0x8d, 0x8d, 0xff }, + { 0x21, 0x8e, 0x8d, 0xff }, + { 0x21, 0x8f, 0x8d, 0xff }, + { 0x21, 0x90, 0x8d, 0xff }, + { 0x21, 0x91, 0x8c, 0xff }, + { 0x20, 0x92, 0x8c, 0xff }, + { 0x20, 0x92, 0x8c, 0xff }, + { 0x20, 0x93, 0x8c, 0xff }, + { 0x1f, 0x94, 0x8c, 0xff }, + { 0x1f, 0x95, 0x8b, 0xff }, + { 0x1f, 0x96, 0x8b, 0xff }, + { 0x1f, 0x97, 0x8b, 0xff }, + { 0x1f, 0x98, 0x8b, 0xff }, + { 0x1f, 0x99, 0x8a, 0xff }, + { 0x1f, 0x9a, 0x8a, 0xff }, + { 0x1e, 0x9b, 0x8a, 0xff }, + { 0x1e, 0x9c, 0x89, 0xff }, + { 0x1e, 0x9d, 0x89, 0xff }, + { 0x1f, 0x9e, 0x89, 0xff }, + { 0x1f, 0x9f, 0x88, 0xff }, + { 0x1f, 0xa0, 0x88, 0xff }, + { 0x1f, 0xa1, 0x88, 0xff }, + { 0x1f, 0xa1, 0x87, 0xff }, + { 0x1f, 0xa2, 0x87, 0xff }, + { 0x20, 0xa3, 0x86, 0xff }, + { 0x20, 0xa4, 0x86, 0xff }, + { 0x21, 0xa5, 0x85, 0xff }, + { 0x21, 0xa6, 0x85, 0xff }, + { 0x22, 0xa7, 0x85, 0xff }, + { 0x22, 0xa8, 0x84, 0xff }, + { 0x23, 0xa9, 0x83, 0xff }, + { 0x24, 0xaa, 0x83, 0xff }, + { 0x25, 0xab, 0x82, 0xff }, + { 0x25, 0xac, 0x82, 0xff }, + { 0x26, 0xad, 0x81, 0xff }, + { 0x27, 0xad, 0x81, 0xff }, + { 0x28, 0xae, 0x80, 0xff }, + { 0x29, 0xaf, 0x7f, 0xff }, + { 0x2a, 0xb0, 0x7f, 0xff }, + { 0x2c, 0xb1, 0x7e, 0xff }, + { 0x2d, 0xb2, 0x7d, 0xff }, + { 0x2e, 0xb3, 0x7c, 0xff }, + { 0x2f, 0xb4, 0x7c, 0xff }, + { 0x31, 0xb5, 0x7b, 0xff }, + { 0x32, 0xb6, 0x7a, 0xff }, + { 0x34, 0xb6, 0x79, 0xff }, + { 0x35, 0xb7, 0x79, 0xff }, + { 0x37, 0xb8, 0x78, 0xff }, + { 0x38, 0xb9, 0x77, 0xff }, + { 0x3a, 0xba, 0x76, 0xff }, + { 0x3b, 0xbb, 0x75, 0xff }, + { 0x3d, 0xbc, 0x74, 0xff }, + { 0x3f, 0xbc, 0x73, 0xff }, + { 0x40, 0xbd, 0x72, 0xff }, + { 0x42, 0xbe, 0x71, 0xff }, + { 0x44, 0xbf, 0x70, 0xff }, + { 0x46, 0xc0, 0x6f, 0xff }, + { 0x48, 0xc1, 0x6e, 0xff }, + { 0x4a, 0xc1, 0x6d, 0xff }, + { 0x4c, 0xc2, 0x6c, 0xff }, + { 0x4e, 0xc3, 0x6b, 0xff }, + { 0x50, 0xc4, 0x6a, 0xff }, + { 0x52, 0xc5, 0x69, 0xff }, + { 0x54, 0xc5, 0x68, 0xff }, + { 0x56, 0xc6, 0x67, 0xff }, + { 0x58, 0xc7, 0x65, 0xff }, + { 0x5a, 0xc8, 0x64, 0xff }, + { 0x5c, 0xc8, 0x63, 0xff }, + { 0x5e, 0xc9, 0x62, 0xff }, + { 0x60, 0xca, 0x60, 0xff }, + { 0x63, 0xcb, 0x5f, 0xff }, + { 0x65, 0xcb, 0x5e, 0xff }, + { 0x67, 0xcc, 0x5c, 0xff }, + { 0x69, 0xcd, 0x5b, 0xff }, + { 0x6c, 0xcd, 0x5a, 0xff }, + { 0x6e, 0xce, 0x58, 0xff }, + { 0x70, 0xcf, 0x57, 0xff }, + { 0x73, 0xd0, 0x56, 0xff }, + { 0x75, 0xd0, 0x54, 0xff }, + { 0x77, 0xd1, 0x53, 0xff }, + { 0x7a, 0xd1, 0x51, 0xff }, + { 0x7c, 0xd2, 0x50, 0xff }, + { 0x7f, 0xd3, 0x4e, 0xff }, + { 0x81, 0xd3, 0x4d, 0xff }, + { 0x84, 0xd4, 0x4b, 0xff }, + { 0x86, 0xd5, 0x49, 0xff }, + { 0x89, 0xd5, 0x48, 0xff }, + { 0x8b, 0xd6, 0x46, 0xff }, + { 0x8e, 0xd6, 0x45, 0xff }, + { 0x90, 0xd7, 0x43, 0xff }, + { 0x93, 0xd7, 0x41, 0xff }, + { 0x95, 0xd8, 0x40, 0xff }, + { 0x98, 0xd8, 0x3e, 0xff }, + { 0x9b, 0xd9, 0x3c, 0xff }, + { 0x9d, 0xd9, 0x3b, 0xff }, + { 0xa0, 0xda, 0x39, 0xff }, + { 0xa2, 0xda, 0x37, 0xff }, + { 0xa5, 0xdb, 0x36, 0xff }, + { 0xa8, 0xdb, 0x34, 0xff }, + { 0xaa, 0xdc, 0x32, 0xff }, + { 0xad, 0xdc, 0x30, 0xff }, + { 0xb0, 0xdd, 0x2f, 0xff }, + { 0xb2, 0xdd, 0x2d, 0xff }, + { 0xb5, 0xde, 0x2b, 0xff }, + { 0xb8, 0xde, 0x29, 0xff }, + { 0xba, 0xde, 0x28, 0xff }, + { 0xbd, 0xdf, 0x26, 0xff }, + { 0xc0, 0xdf, 0x25, 0xff }, + { 0xc2, 0xdf, 0x23, 0xff }, + { 0xc5, 0xe0, 0x21, 0xff }, + { 0xc8, 0xe0, 0x20, 0xff }, + { 0xca, 0xe1, 0x1f, 0xff }, + { 0xcd, 0xe1, 0x1d, 0xff }, + { 0xd0, 0xe1, 0x1c, 0xff }, + { 0xd2, 0xe2, 0x1b, 0xff }, + { 0xd5, 0xe2, 0x1a, 0xff }, + { 0xd8, 0xe2, 0x19, 0xff }, + { 0xda, 0xe3, 0x19, 0xff }, + { 0xdd, 0xe3, 0x18, 0xff }, + { 0xdf, 0xe3, 0x18, 0xff }, + { 0xe2, 0xe4, 0x18, 0xff }, + { 0xe5, 0xe4, 0x19, 0xff }, + { 0xe7, 0xe4, 0x19, 0xff }, + { 0xea, 0xe5, 0x1a, 0xff }, + { 0xec, 0xe5, 0x1b, 0xff }, + { 0xef, 0xe5, 0x1c, 0xff }, + { 0xf1, 0xe5, 0x1d, 0xff }, + { 0xf4, 0xe6, 0x1e, 0xff }, + { 0xf6, 0xe6, 0x20, 0xff }, + { 0xf8, 0xe6, 0x21, 0xff }, + { 0xfb, 0xe7, 0x23, 0xff }, + { 0xfd, 0xe7, 0x25, 0xff }, +}; diff --git a/src/colour.c b/src/colour.c index 4feed3e..2d0ea22 100644 --- a/src/colour.c +++ b/src/colour.c @@ -3,9 +3,13 @@ #include "colour.h" #include "cividis_lut.h" +#include "viridis_lut.h" -void get_colour_mono(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps) +void get_colour_mono(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps, enum colour_type type) { + // only needed so that the signature matches + (void)type; + /* * Use * 1/π * arctan(log(|val|^α + 1)) ∈ [0, 1] @@ -40,15 +44,17 @@ inline static struct rgba_pix interpolate(double t, struct rgba_pix a, struct rg }; } -void get_colour_cividis(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps) +void get_colour_cmap(struct rgba_pix *colour, const mpc_t val, struct temporaries tmps, enum colour_type type) { + struct rgba_pix *lut = (type == colour_type_cividis ? cividis_lut : viridis_lut); + mpc_arg(tmps.mpfr, val, MPFR_RNDN); mpfr_div(tmps.mpfr, tmps.mpfr, tmps.pi, MPFR_RNDN); mpfr_mul_ui(tmps.mpfr, tmps.mpfr, 256, MPFR_RNDN); uint8_t idx = mpfr_get_ui(tmps.mpfr, MPFR_RNDZ); if (idx == 0xff) { - *colour = cividis_lut[idx]; + *colour = lut[idx]; return; } @@ -56,5 +62,5 @@ void get_colour_cividis(struct rgba_pix *colour, const mpc_t val, struct tempora mpfr_frac(tmps.mpfr, tmps.mpfr, MPFR_RNDN); double t = mpfr_get_d(tmps.mpfr, MPFR_RNDN); - *colour = interpolate(t, cividis_lut[idx], cividis_lut[idx + 1]); + *colour = interpolate(t, lut[idx], lut[idx + 1]); } diff --git a/src/main.c b/src/main.c index c49bcba..ed0cf19 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include <ctype.h> #include <errno.h> +#include <string.h> #include <fcntl.h> #include <unistd.h> @@ -19,9 +20,9 @@ #define POLY_SIZE 20 struct options { - bool monochrome; uint64_t prec; uint64_t resolution; + enum colour_type colour_type; mpc_t offset; mpc_t x_step; @@ -38,13 +39,13 @@ static inline void get_pos(const struct options opts, mpc_t z, uint64_t i, uint6 mpc_add(z, z, tmps.mpc, MPFR_RNDN); } -struct options init_options(bool monochrome, uint64_t prec, uint64_t resolution) +struct options init_options(enum colour_type type, uint64_t prec, uint64_t resolution) { struct options opts = { 0 }; - opts.monochrome = monochrome; opts.prec = prec; opts.resolution = resolution; + opts.colour_type = type; mpc_init2(opts.offset, prec); mpc_init2(opts.x_step, prec); @@ -210,7 +211,7 @@ void destroy_polynomial(struct polynomial p) void fill(struct pic pic, struct options opts, const struct polynomial p) { - void (*get_colour)(struct rgba_pix *, const mpc_t, struct temporaries) = opts.monochrome ? &get_colour_mono : &get_colour_cividis; + colour_function_t get_colour = (opts.colour_type == colour_type_mono ? &get_colour_mono : &get_colour_cmap); #pragma omp parallel { struct temporaries tmps = init_temporaries(opts.prec); @@ -232,7 +233,7 @@ void fill(struct pic pic, struct options opts, const struct polynomial p) } horner_eval(val, z, p); - get_colour(pic.pix + i * opts.resolution + j, val, tmps); + get_colour(pic.pix + i * opts.resolution + j, val, tmps, opts.colour_type); } } @@ -254,7 +255,7 @@ void print_help(const char *exe) "Options:\n" " -h show this help message\n" " -V show version information\n" - " -m use monochrome\n" + " -t TYPE set colour type, one of mono, cividis, viridis\n" " -p PREC set precision\n" " -c CENTER set center\n" " -r RADIUS set radius\n" @@ -294,20 +295,30 @@ int main(int argc, char **argv) const char *out_file = "out.png"; const char *coefficient_file = ""; - bool monochrome = false; + enum colour_type colour_type = colour_type_cividis; struct options opts; while (true) { - switch (getopt(argc, argv, "+hVmp:c:r:R:o:")) { + switch (getopt(argc, argv, "+hVt:p:c:r:R:o:")) { case -1: goto escape; case 'h': print_help(argv[0]); return EXIT_SUCCESS; - case 'm': - monochrome = true; + case 't': + if (strcmp("mono", optarg) == 0) { + colour_type = colour_type_mono; + } else if (strcmp("cividis", optarg) == 0) { + colour_type = colour_type_cividis; + } else if (strcmp("viridis", optarg) == 0) { + colour_type = colour_type_viridis; + } else { + printf("invalid colour type `%s'\n", optarg); + + return EXIT_FAILURE; + } break; case 'p': @@ -367,7 +378,7 @@ escape: mpc_init2(tmp, prec); mpc_init2(center, prec); - opts = init_options(monochrome, prec, resolution); + opts = init_options(colour_type, prec, resolution); if (mpc_set_str(center, center_str, 10, MPFR_RNDN)) { printf("invalid center argument `%s'\n", center_str); -- GitLab