From 9d09c6b4c581f0ffeb988c4cf5ce2ebf6781a639 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Wed, 11 Mar 2015 13:23:12 +0100 Subject: [PATCH] fosphor/gl_cmap_gen: Add new function to generate GL colormap from a PNG Signed-off-by: Sylvain Munaut --- CMakeLists.txt | 6 +++ lib/CMakeLists.txt | 6 +++ lib/fosphor/Makefile | 4 +- lib/fosphor/gl_cmap_gen.c | 101 ++++++++++++++++++++++++++++++++++++++ lib/fosphor/gl_cmap_gen.h | 1 + 5 files changed, 116 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index da049f8..f2ebef3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,6 +130,8 @@ if (Qt5_FOUND) endforeach(module) endif (Qt5_FOUND) +find_package(PNG 1.6.19) + ######################################################################## # Find gnuradio build dependencies ######################################################################## @@ -165,6 +167,10 @@ endif(DOXYGEN_FOUND) include(GrComponent) +GR_REGISTER_COMPONENT("PNG" ENABLE_PNG + PNG_FOUND +) + GR_REGISTER_COMPONENT("Python" ENABLE_PYTHON PYTHONLIBS_FOUND pybind11_FOUND ) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 4c1117a..51c3694 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -85,6 +85,12 @@ if(ENABLE_QT) target_link_libraries(gnuradio-fosphor ${Qt5_LIBRARIES}) endif(ENABLE_QT) +if(ENABLE_PNG) + add_definitions(-DENABLE_PNG) + target_include_directories(gnuradio-fosphor PRIVATE ${PNG_INCLUDE_DIRS}) + target_link_libraries(gnuradio-fosphor ${PNG_LIBRARIES}) +endif(ENABLE_PNG) + set_target_properties(gnuradio-fosphor PROPERTIES DEFINE_SYMBOL "gnuradio_fosphor_EXPORTS") if(APPLE) diff --git a/lib/fosphor/Makefile b/lib/fosphor/Makefile index a8fb3bb..051afd4 100644 --- a/lib/fosphor/Makefile +++ b/lib/fosphor/Makefile @@ -1,7 +1,7 @@ UNAME=$(shell uname) CC=gcc -CFLAGS=-Wall -Werror -O2 `pkg-config freetype2 glfw3 --cflags` -g -LDLIBS=`pkg-config freetype2 glfw3 --libs` -lm +CFLAGS=-Wall -Werror -O2 `pkg-config freetype2 glfw3 libpng --cflags` -g +LDLIBS=`pkg-config freetype2 glfw3 libpng --libs` -lm ifneq ($(AMDAPPSDKROOT), ) CFLAGS+=-I$(AMDAPPSDKROOT)/include endif diff --git a/lib/fosphor/gl_cmap_gen.c b/lib/fosphor/gl_cmap_gen.c index 59df6c3..c0be4ff 100644 --- a/lib/fosphor/gl_cmap_gen.c +++ b/lib/fosphor/gl_cmap_gen.c @@ -15,10 +15,14 @@ * \brief OpenGL color map generators */ +#include #include +#include +#include #include #include "gl_cmap_gen.h" +#include "resource.h" static void @@ -116,6 +120,33 @@ _set_rgba_from_hsv(uint32_t *rgba, float h, float s, float v) } +static inline uint32_t +_rgba_interpolate(uint32_t *rgba, int sz, int p, int N) +{ + int pos_i = (p * (sz-1)) / (N-1); + int pos_f = (p * (sz-1)) - (pos_i * (N-1)); + uint32_t vl, vh, vf = 0; + int i; + + if (pos_f == 0) + return rgba[pos_i]; + + vl = rgba[pos_i]; + vh = rgba[pos_i+1]; + + for (i=0; i<4; i++) + { + uint32_t nv = + ((vl >> (8 * i)) & 0xff) * ((N-1) - pos_f) + + ((vh >> (8 * i)) & 0xff) * pos_f; + + vf |= ((nv / (N-1)) & 0xff) << (8 * i); + } + + return vf; +} + + int fosphor_gl_cmap_histogram(uint32_t *rgba, int N, void *arg) { @@ -167,4 +198,74 @@ fosphor_gl_cmap_waterfall(uint32_t *rgba, int N, void *arg) return 0; } + +#ifdef ENABLE_PNG +#include +int +fosphor_gl_cmap_png(uint32_t *rgba, int N, void *arg) +{ + const char *rsrc_name = arg; + png_image img; + const void *png_data = NULL; + void *png_rgba = NULL; + int png_len, i, rv; + + /* Grab the file */ + png_data = resource_get(rsrc_name, &png_len); + if (!png_data) + return -ENOENT; + + /* Read PNG */ + memset(&img, 0x00, sizeof(img)); + img.version = PNG_IMAGE_VERSION; + + rv = png_image_begin_read_from_memory(&img, png_data, png_len); + if (!rv) { + rv = -EINVAL; + goto error; + } + + img.format = PNG_FORMAT_RGBA; + + png_rgba = malloc(sizeof(uint32_t) * img.width * img.height); + if (!png_rgba) { + rv = -ENOMEM; + goto error; + } + + rv = png_image_finish_read(&img, + NULL, /* background */ + png_rgba, /* buffer */ + sizeof(uint32_t) * img.width, /* row_stride */ + NULL /* colormap */ + ); + if (!rv) { + rv = -EINVAL; + goto error; + } + + /* Interpolate the PNG to the requested linear scale */ + for (i=0; i