star-cmap

Mapping values onto color ramps
git clone git://git.meso-star.fr/star-cmap.git
Log | Files | Refs | README | LICENSE

commit 56c892cbf90c59002d261711049c40b4a62e66b3
parent 3840630278fa20fc643366a4558f4fbef4849b95
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 20 Mar 2020 18:45:35 +0100

Test the builtin palettes

Diffstat:
Mcmake/CMakeLists.txt | 1+
Msrc/scmap.h | 1+
Msrc/scmap_palettes.c | 25+++++++++++++++++++------
Asrc/test_scmap_palettes.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 85 insertions(+), 6 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -73,6 +73,7 @@ if(NOT NO_TEST) new_test(test_scmap) new_test(test_scmap_fetch_color) + new_test(test_scmap_palettes) if(CMAKE_COMPILER_IS_GNUCC) target_link_libraries(test_scmap_fetch_color m) diff --git a/src/scmap.h b/src/scmap.h @@ -59,6 +59,7 @@ BEGIN_DECLS /* Builtin palettes */ SCMAP_API const struct scmap_palette scmap_palette_accent; SCMAP_API const struct scmap_palette scmap_palette_blues; +SCMAP_API const struct scmap_palette scmap_palette_brbg; /******************************************************************************* * Star-ColorMap API diff --git a/src/scmap_palettes.c b/src/scmap_palettes.c @@ -37,6 +37,17 @@ static const double blues[] = { 8.0/255.0, 69.0/255.0, 148.0/255.0 }; +static const double brbg[] = { + 140.0/255.0, 81.0/255.0, 10.0/255.0, + 191.0/255.0, 129.0/255.0, 45.0/255.0, + 223.0/255.0, 194.0/255.0, 125.0/255.0, + 246.0/255.0, 232.0/255.0, 195.0/255.0, + 199.0/255.0, 234.0/255.0, 229.0/255.0, + 128.0/255.0, 205.0/255.0, 193.0/255.0, + 53.0/255.0, 151.0/255.0, 143.0/255.0, + 1.0/255.0, 102.0/255.0, 94.0/255.0 +}; + static INLINE void get_color(const size_t icolor, double color[3], void* context) { @@ -47,10 +58,12 @@ get_color(const size_t icolor, double color[3], void* context) color[2] = colors[icolor*3+2]; } -const struct scmap_palette scmap_palette_accent = { - get_color, sizeof(accent)/sizeof(double[3]), (void*)accent -}; +#define DEFINE_PALETTE(Palette) \ + const struct scmap_palette scmap_palette_##Palette = { \ + get_color, sizeof(Palette)/sizeof(double[3]), (void*)Palette \ + } +DEFINE_PALETTE(accent); +DEFINE_PALETTE(blues); +DEFINE_PALETTE(brbg); +#undef DEFINE_PALETTE -const struct scmap_palette scmap_palette_blues = { - get_color, sizeof(blues)/sizeof(double[3]), (void*)blues -}; diff --git a/src/test_scmap_palettes.c b/src/test_scmap_palettes.c @@ -0,0 +1,64 @@ +/* Copyright (C) 2020 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#define MAP_WIDTH 240 +#define MAP_HEIGHT 16 + +#include "scmap.h" + +#include <rsys/image.h> +#include <rsys/mem_allocator.h> +#include <rsys/stretchy_array.h> + +int +main(int argc, char** argv) +{ + struct scmap* scmap = NULL; + double* colors = NULL; + struct image img; + size_t x, y; + size_t i; + (void)argc, (void)argv; + + CHK(scmap_create(NULL, NULL, 1, &scmap_palette_brbg, &scmap) == RES_OK); + CHK(colors = sa_add(colors, MAP_WIDTH*3)); + + FOR_EACH(i, 0, MAP_WIDTH) { + const double u = (double)i / (double)(MAP_WIDTH-1); + CHK(scmap_fetch_color(scmap, u, SCMAP_FILTER_LINEAR, colors+i*3) == RES_OK); + } + + CHK(image_init(&mem_default_allocator, &img) == RES_OK); + image_setup(&img, MAP_WIDTH, MAP_HEIGHT, + sizeof_image_format(IMAGE_RGB8)*MAP_WIDTH, IMAGE_RGB8, NULL); + + FOR_EACH(y, 0, MAP_HEIGHT) { + char* row = img.pixels + img.pitch * y; + FOR_EACH(x, 0, MAP_WIDTH) { + uint8_t* pix = (uint8_t*)(row + x*sizeof_image_format(img.format)); + pix[0] = (uint8_t)(colors[x*3+0] * 255 + 0.5/*round*/); + pix[1] = (uint8_t)(colors[x*3+1] * 255 + 0.5/*round*/); + pix[2] = (uint8_t)(colors[x*3+2] * 255 + 0.5/*round*/); + } + } + + CHK(image_write_ppm_stream(&img, 0, stdout) == RES_OK); + + sa_release(colors); + CHK(image_release(&img) == RES_OK); + CHK(scmap_ref_put(scmap) == RES_OK); + CHK(mem_allocated_size() == 0); + return 0; +}