From a5f35c8236697c090110f3c00875dcec810655f6 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Sun, 3 Jun 2012 15:31:48 +0200 Subject: [PATCH] filters: Cleanup GP_FilterParams API a little. --- include/filters/GP_FilterParam.h | 77 ++++++++++++++++++++++++--------------- libs/filters/GP_FilterParam.c | 78 ++++++++++++++++++++++++++++++++++++++-- tests/SDL/showimage.c | 3 +- 3 files changed, 126 insertions(+), 32 deletions(-) diff --git a/include/filters/GP_FilterParam.h b/include/filters/GP_FilterParam.h index 892f166c..6520354d 100644 --- a/include/filters/GP_FilterParam.h +++ b/include/filters/GP_FilterParam.h @@ -16,7 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * - * Copyright (C) 2009-2011 Cyril Hrubis * + * Copyright (C) 2009-2012 Cyril Hrubis * * * *****************************************************************************/ @@ -46,14 +46,27 @@ typedef union GP_FilterParamVal { * Filter takes, empty channel name terminated, arrray of these as parameter. */ typedef struct GP_FilterParam { - //TODO: this must be >= for maximal channel name (now it's 2) + //TODO: this must be > than maximal channel name (now it's 2) char channel_name[2]; union GP_FilterParamVal val; } GP_FilterParam; /* - * Takes array of filter parameters and returns channel. + * Creates filter param structure large enough for given pixel_type. * + * The returned structure has initalized channel_names and terminator. + */ +GP_FilterParam *GP_FilterParamCreate(GP_PixelType pixel_type); + +/* + * Destroys filter param structure. + */ +void GP_FilterParamDestroy(GP_FilterParam *self); + +/* + * Takes array of filter parameters and returns filter parameter by a channel + * name. + * * Returns NULL if channel wasn't found. */ GP_FilterParam *GP_FilterParamChannel(GP_FilterParam params[], @@ -69,7 +82,7 @@ uint32_t GP_FilterParamChannels(GP_FilterParam params[]); * match. */ int GP_FilterParamCheckPixelType(GP_FilterParam params[], - GP_PixelType type); + GP_PixelType pixel_type); /* * Returns zero only if params have exactly same channels as array of @@ -85,53 +98,59 @@ int GP_FilterParamCheckChannels(GP_FilterParam params[], GP_FilterParam name[GP_PixelTypes[pixel_type].numchannels + 1]; \ GP_FilterParamInitChannels(name, pixel_type); -#define GP_FILTER_PARAMS_INT(pixel_type, name, val) \ - GP_FILTER_PARAMS(pixel_type, name) \ - GP_FilterParamSetIntAll(name, val); - -#define GP_FILTER_PARAMS_UINT(pixel_type, name, val) \ - GP_FILTER_PARAMS(pixel_type, name) \ - GP_FilterParamSetUIntAll(name, val); - -#define GP_FILTER_PARAMS_FLOAT(pixel_type, name, val) \ - GP_FILTER_PARAMS(pixel_type, name) \ - GP_FilterParamSetFloatAll(name, val); - -#define GP_FILTER_PARAMS_PTR(pixel_type, name, ptr) \ - GP_FILTER_PARAMS(pixel_type, name) \ - GP_FilterParamSetPtrAll(name, ptr); - /* * Initalize param names and terminator. * * Sets all values to 0. */ void GP_FilterParamInitChannels(GP_FilterParam params[], - GP_PixelType type); + GP_PixelType pixel_type); /* * Sets all values to integer value. */ -void GP_FilterParamSetIntAll(GP_FilterParam params[], - int32_t val); +void GP_FilterParamSetIntAll(GP_FilterParam params[], int32_t val); + +/* + * Sets integer value. Returns 0 if such value was found, non-zero otherwise. + */ +int GP_FilterParamSetInt(GP_FilterParam params[], const char *channel_name, + int32_t val); /* * Sets all values to float value. */ -void GP_FilterParamSetFloatAll(GP_FilterParam params[], - float val); +void GP_FilterParamSetFloatAll(GP_FilterParam params[], float val); + +/* + * Sets float value. Returns 0 if such value was found, non-zero otherwise. + */ +int GP_FilterParamSetFloat(GP_FilterParam params[], const char *channel_name, + float val); /* * Sets all values to unsigned integer value. */ -void GP_FilterParamSetUIntAll(GP_FilterParam params[], - uint32_t val); +void GP_FilterParamSetUIntAll(GP_FilterParam params[], uint32_t val); + +/* + * Sets unsigned integer value. Returns 0 if such value was found, non-zero + * otherwise. + */ +int GP_FilterParamSetUInt(GP_FilterParam params[], const char *channel_name, + uint32_t val); /* * Sets all values to pointer value. */ -void GP_FilterParamSetPtrAll(GP_FilterParam params[], - void *ptr); +void GP_FilterParamSetPtrAll(GP_FilterParam params[], void *ptr); + +/* + * Sets pointer value. Returns 0 if such value was found, non-zero otherwise. + */ +int GP_FilterParamSetPtr(GP_FilterParam params[], const char *channel_name, + void *ptr); + /* * Call free on all pointer values. */ diff --git a/libs/filters/GP_FilterParam.c b/libs/filters/GP_FilterParam.c index e968a4b2..e6c0f6b3 100644 --- a/libs/filters/GP_FilterParam.c +++ b/libs/filters/GP_FilterParam.c @@ -16,16 +16,38 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * - * Copyright (C) 2009-2011 Cyril Hrubis * + * Copyright (C) 2009-2012 Cyril Hrubis * * * *****************************************************************************/ #include -#include +#include "core/GP_Debug.h" #include "GP_FilterParam.h" +GP_FilterParam *GP_FilterParamCreate(GP_PixelType pixel_type) +{ + GP_FilterParam *ret; + + ret = malloc((GP_PixelTypes[pixel_type].numchannels + 1) + * sizeof(GP_FilterParam)); + + if (ret == NULL) { + GP_WARN("Malloc Failed"); + return NULL; + } + + GP_FilterParamInitChannels(ret, pixel_type); + + return ret; +} + +void GP_FilterParamDestroy(GP_FilterParam *self) +{ + free(self); +} + static unsigned int count_channels(GP_FilterParam params[]) { unsigned int i = 0; @@ -115,6 +137,19 @@ void GP_FilterParamSetIntAll(GP_FilterParam params[], params[i].val.i = val; } +int GP_FilterParamSetInt(GP_FilterParam params[], const char *channel_name, + int32_t val) +{ + GP_FilterParam *param; + param = GP_FilterParamChannel(params, channel_name); + + if (param == NULL) + return 1; + + param->val.i = val; + return 0; +} + void GP_FilterParamSetFloatAll(GP_FilterParam params[], float val) { @@ -124,6 +159,19 @@ void GP_FilterParamSetFloatAll(GP_FilterParam params[], params[i].val.f = val; } +int GP_FilterParamSetFloat(GP_FilterParam params[], const char *channel_name, + float val) +{ + GP_FilterParam *param; + param = GP_FilterParamChannel(params, channel_name); + + if (param == NULL) + return 1; + + param->val.f = val; + return 0; +} + void GP_FilterParamSetUIntAll(GP_FilterParam params[], uint32_t val) { @@ -133,6 +181,19 @@ void GP_FilterParamSetUIntAll(GP_FilterParam params[], params[i].val.ui = val; } +int GP_FilterParamSetUInt(GP_FilterParam params[], const char *channel_name, + uint32_t val) +{ + GP_FilterParam *param; + param = GP_FilterParamChannel(params, channel_name); + + if (param == NULL) + return 1; + + param->val.ui = val; + return 0; +} + void GP_FilterParamSetPtrAll(GP_FilterParam params[], void *ptr) { @@ -142,6 +203,19 @@ void GP_FilterParamSetPtrAll(GP_FilterParam params[], params[i].val.ptr = ptr; } +int GP_FilterParamSetPtr(GP_FilterParam params[], const char *channel_name, + void *ptr) +{ + GP_FilterParam *param; + param = GP_FilterParamChannel(params, channel_name); + + if (param == NULL) + return 1; + + param->val.ptr = ptr; + return 0; +} + void GP_FilterParamFreePtrAll(GP_FilterParam params[]) { unsigned int i; diff --git a/tests/SDL/showimage.c b/tests/SDL/showimage.c index 90bc393c..a63ba78e 100644 --- a/tests/SDL/showimage.c +++ b/tests/SDL/showimage.c @@ -51,7 +51,8 @@ void event_loop(void) case SDLK_DOWN: { brightness-=1; - GP_FILTER_PARAMS_INT(bitmap->pixel_type, param, brightness); + GP_FILTER_PARAMS(bitmap->pixel_type, param); + GP_FilterParamSetIntAll(param, brightness); res = GP_FilterBrightness(bitmap, NULL, param, NULL); printf("brightness = %i %ux%u\n", brightness, res->w, res->h); -- 2.11.4.GIT