commit 33597e0c60566d93600729a1701d3cd6495c4c40
parent 1103b0fa523e93202dc1c33ecdf0eee01dcdab90
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 20 Mar 2020 17:15:11 +0100
Implement the scmap_fetch_color function
Diffstat:
3 files changed, 96 insertions(+), 2 deletions(-)
diff --git a/src/scmap.c b/src/scmap.c
@@ -175,6 +175,51 @@ error:
goto exit;
}
+static INLINE void
+fetch_color_nearest
+ (const struct scmap* scmap,
+ const size_t icol,
+ const double u,
+ double color[3])
+{
+ const size_t i = u < 0.5 ? icol*3 : (icol + 1)*3;
+ ASSERT(scmap && color && u >= 0 && u <1);
+ ASSERT(i/3 < darray_double_size_get(&scmap->palette)/3);
+ color[0] = darray_double_cdata_get(&scmap->palette)[i+0];
+ color[1] = darray_double_cdata_get(&scmap->palette)[i+1];
+ color[2] = darray_double_cdata_get(&scmap->palette)[i+2];
+}
+
+static INLINE void
+fetch_color_linear
+ (const struct scmap* scmap,
+ const size_t icol,
+ const double u,
+ double color[3])
+{
+ const size_t i = icol*3;
+ ASSERT(scmap && color && u >= 0 && u <1);
+ ASSERT(i/3 < darray_double_size_get(&scmap->palette)/3);
+
+ if(u == 0) {
+ color[0] = darray_double_cdata_get(&scmap->palette)[i+0];
+ color[1] = darray_double_cdata_get(&scmap->palette)[i+1];
+ color[2] = darray_double_cdata_get(&scmap->palette)[i+2];
+ } else {
+ const size_t j = (icol+1)*3;
+ const double* col0;
+ const double* col1;
+ ASSERT(j/3 < darray_double_size_get(&scmap->palette)/3);
+
+ col0 = darray_double_cdata_get(&scmap->palette) + i;
+ col1 = darray_double_cdata_get(&scmap->palette) + j;
+
+ color[0] = u * (col1[0] - col0[0]) + col0[0];
+ color[1] = u * (col1[1] - col0[1]) + col0[1];
+ color[2] = u * (col1[2] - col0[2]) + col0[2];
+ }
+}
+
static void
release_scmap(ref_T* ref)
{
@@ -270,3 +315,52 @@ scmap_ref_put(struct scmap* scmap)
return RES_OK;
}
+res_T
+scmap_fetch_color
+ (const struct scmap* scmap,
+ const double value,
+ const enum scmap_filter filter,
+ double color[3])
+{
+ size_t ncolors;
+ size_t icol;
+ double cell;
+ double icell;
+ double u;
+ res_T res = RES_BAD_ARG;
+
+ if(!scmap || !color) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ if(value < 0 || value > 1) {
+ log_err(scmap, "%s: the submitted value must be in [0, 1] -- value = %g.\n",
+ FUNC_NAME, value);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ ncolors = darray_double_size_get(&scmap->palette)/3;
+ cell = value * (double)(ncolors-1);
+
+ u = modf(cell, &icell);
+ icol = (size_t)icell;
+ ASSERT(icol < ncolors - 1);
+
+ switch(filter) {
+ case SCMAP_FILTER_NEAREST:
+ fetch_color_nearest(scmap, icol, u, color);
+ break;
+ case SCMAP_FILTER_LINEAR:
+ fetch_color_linear(scmap, icol, u, color);
+ break;
+ default: FATAL("Unreachable code.\n"); break;
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
diff --git a/src/scmap.h b/src/scmap.h
@@ -33,7 +33,7 @@
#ifndef NDEBUG
#define SCMAP(Func) ASSERT(scmap_ ## Func == RES_OK)
#else
- #define SCMA(Func) scmap_ ## Func
+ #define SCMAP(Func) scmap_ ## Func
#endif
enum scmap_filter {
diff --git a/src/test_scmap.c b/src/test_scmap.c
@@ -104,5 +104,5 @@ main(int argc, char** argv)
logger_release(&logger);
CHK(mem_allocated_size() == 0);
-return 0;
+ return 0;
}