1 #ifndef __NR_FILTER_UTILS_H__
2 #define __NR_FILTER_UTILS_H__
4 /** \file
5 * filter utils. Definition of functions needed by several filters.
6 *
7 * Authors:
8 * Jean-Rene Reinhard <jr@komite.net>
9 *
10 * Copyright (C) 2007 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "round.h"
17 namespace Inkscape {
18 namespace Filters {
20 /**
21 * Clamps an integer value to a value between 0 and 255. Needed by filters where
22 * rendering computations can lead to component values out of bound.
23 *
24 * \return 0 if the value is smaller than 0, 255 if it is greater 255, else v
25 * \param v the value to clamp
26 */
27 __attribute__ ((const))
28 inline int clamp(int const val) {
29 if (val < 0) return 0;
30 if (val > 255) return 255;
31 return val;
32 }
34 /**
35 * Clamps an integer value to a value between 0 and 255^3.
36 *
37 * \return 0 if the value is smaller than 0, 255^3 (16581375) if it is greater than 255^3, else v
38 * \param v the value to clamp
39 */
40 __attribute__ ((const))
41 inline int clamp3(int const val) {
42 if (val < 0) return 0;
43 if (val > 16581375) return 16581375;
44 return val;
45 }
47 /**
48 * Macro to use the clamp function with double inputs and unsigned char output
49 */
50 #define CLAMP_D_TO_U8(v) (unsigned char) clamp((int)round((v)))
52 /**
53 * Clamps an integer to a value between 0 and alpha. Useful when handling
54 * images with premultiplied alpha, as setting some of RGB channels
55 * to a value bigger than alpha confuses the alpha blending in Inkscape
56 * \return 0 if val is negative, alpha if val is bigger than alpha, val otherwise
57 * \param val the value to clamp
58 * \param alpha the maximum value to clamp to
59 */
60 __attribute__ ((const))
61 inline int clamp_alpha(int const val, int const alpha) {
62 if (val < 0) return 0;
63 if (val > alpha) return alpha;
64 return val;
65 }
67 /**
68 * Macro to use the clamp function with double inputs and unsigned char output
69 */
70 #define CLAMP_D_TO_U8_ALPHA(v,a) (unsigned char) clamp_alpha((int)round((v)),(a))
72 } /* namespace Filters */
73 } /* namespace Inkscape */
75 #endif /* __NR_FILTER_UTILS_H__ */
76 /*
77 Local Variables:
78 mode:c++
79 c-file-style:"stroustrup"
80 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
81 indent-tabs-mode:nil
82 fill-column:99
83 End:
84 */
85 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :