From b45098c71ca5dab7345e2b2f704ae6452a7b52cb Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Tue, 5 Jun 2012 15:18:09 +0200 Subject: [PATCH] filters: Add noise filter. --- demos/grinder/grinder.c | 25 ++++++++++++++++++++++++- include/filters/GP_Point.h | 6 ++++++ libs/filters/GP_Noise.gen.c.t | 34 ++++++++++++++++++++++++++++++++++ libs/filters/Makefile | 2 +- 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 libs/filters/GP_Noise.gen.c.t diff --git a/demos/grinder/grinder.c b/demos/grinder/grinder.c index abdf5464..1bc73600 100644 --- a/demos/grinder/grinder.c +++ b/demos/grinder/grinder.c @@ -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 * * * *****************************************************************************/ @@ -354,6 +354,28 @@ static GP_RetCode contrast(GP_Context **c, const char *params) return GP_ESUCCESS; } +/* noise */ +static struct param noise_params[] = { + {"ratio", PARAM_FLOAT, "noise", NULL, NULL}, + {NULL, 0, NULL, NULL, NULL} +}; + +static GP_RetCode noise(GP_Context **c, const char *params) +{ + float rat; + + if (param_parse(params, noise_params, "noise", param_err, &rat)) + return GP_EINVAL; + + GP_FILTER_PARAMS((*c)->pixel_type, filter_params); + + GP_FilterParamSetFloatAll(filter_params, rat); + + GP_FilterNoise(*c, *c, filter_params, progress_callback); + + return GP_ESUCCESS; +} + /* invert */ static struct param invert_params[] = { @@ -675,6 +697,7 @@ static struct filter filter_table[] = { {"contrast", "alter image contrast", contrast_params, contrast}, {"invert", "inverts image", invert_params, invert}, {"add_noise", "adds noise", add_noise_params, add_noise}, + {"noise", "adds noise", noise_params, noise}, {"blur", "gaussian blur", blur_params, blur}, {"dither", "dithers bitmap", dither_params, dither}, {"arithmetic", "arithmetic operation", arithmetic_params, arithmetic}, diff --git a/include/filters/GP_Point.h b/include/filters/GP_Point.h index 1ab37566..31e9747d 100644 --- a/include/filters/GP_Point.h +++ b/include/filters/GP_Point.h @@ -72,6 +72,12 @@ GP_Context *GP_FilterInvert(const GP_Context *src, GP_Context *dst, GP_ProgressCallback *callback); /* + * Noise filter. + */ +GP_Context *GP_FilterNoise(const GP_Context *src, GP_Context *dst, + GP_FilterParam ratio[], GP_ProgressCallback *callback); + +/* * Generic slow point filter. * * The filter_callback[] is expected to be filled with pointers diff --git a/libs/filters/GP_Noise.gen.c.t b/libs/filters/GP_Noise.gen.c.t new file mode 100644 index 00000000..7afca65b --- /dev/null +++ b/libs/filters/GP_Noise.gen.c.t @@ -0,0 +1,34 @@ +%% extends "filter.point.c.t" + +{% block descr %}Noise filter -- Adds noise to an image.{% endblock %} + +%% block body + +{{ filter_point_include() }} + +%% macro filter_op(chann_name, chann_size) +{{ chann_name }} = {{ chann_name }} + (random() % ({{ chann_name }}_max * 2)) - {{ chann_name }}_max; +{{ filter_clamp_val(chann_name, chann_size) }} +%% endmacro + +/* + * Generated noise filters. + */ +%% call(pt) filter_point_per_channel('Noise', 'GP_FilterParam ratio[]', filter_op) +{{ filter_params(pt, 'ratio', 'float ', '_rat', 'f') }} +%% for chann in pt.chanslist + int {{ chann[0] }}_max = {{ chann[0] }}_rat * {{ 2 ** chann[2] - 1}} + 0.5; +%% endfor +%% endcall + +/* + * Generated noise filters for pixels with one channel. + */ +%% call(ps) filter_point_per_bpp('Noise', 'GP_FilterParam ratio[]', filter_op) +{{ filter_param(ps, 'ratio', 'float ', '_rat', 'f') }} + int pix_max = pix_rat * {{ 2 ** ps.size - 1}} + 0.5; +%% endcall + +{{ filter_functions('Noise', 'GP_FilterParam ratio[]', 'ratio') }} + +%% endblock body diff --git a/libs/filters/Makefile b/libs/filters/Makefile index 4bed64af..49993aa7 100644 --- a/libs/filters/Makefile +++ b/libs/filters/Makefile @@ -3,7 +3,7 @@ TOPDIR=../.. STATS_FILTERS=GP_Histogram.gen.c POINT_FILTERS=GP_Contrast.gen.c GP_Brightness.gen.c GP_Invert.gen.c\ - GP_Point.gen.c + GP_Point.gen.c GP_Noise.gen.c ARITHMETIC_FILTERS=GP_Difference.gen.c GP_Addition.gen.c GP_Min.gen.c\ GP_Max.gen.c GP_Multiply.gen.c -- 2.11.4.GIT