Rename GP_Context -> GP_Pixmap
[gfxprim.git] / pylib / gfxprim / filters / __init__.py
blobdae818d044e9851fb95fb81236caa737e8fcaf94
1 """
2 Module extending the Pixmap class with .filter submodule.
4 Use as in "import gfxprim.filters; pixmap_foo.filter.Resize(...)"
5 """
7 # Import the SWIG wrapper
8 from . import c_filters
10 def _init(module):
11 "Extend Pixmap with filters submodule"
13 from ..utils import extend, extend_submodule
14 from ..core import Pixmap as _pixmap
16 # New Pixmap submodule
17 class FiltersSubmodule(object):
18 def __init__(self, ctx):
19 self.ctx = ctx
21 _pixmap._submodules['filters'] = FiltersSubmodule
23 for name in ['Invert', 'InvertAlloc',
24 'Brightness', 'BrightnessAlloc',
25 'Contrast', 'ContrastAlloc',
26 'BrightnessContrast', 'BrightnessContrastAlloc',
27 'Posterize', 'PosterizeAlloc',
28 'Resize', 'ResizeAlloc',
29 'Rotate90', 'Rotate90Alloc',
30 'Rotate180', 'Rotate180Alloc',
31 'Rotate270', 'Rotate270Alloc',
32 'MirrorH', 'MirrorHAlloc',
33 'MirrorV', 'MirrorVAlloc',
34 'Addition', 'Multiply', 'Difference', 'Max', 'Min',
35 'GaussianBlur', 'GaussianBlurAlloc',
36 'GaussianBlurEx', 'GaussianBlurExAlloc',
37 'GaussianNoiseAdd', 'GaussianNoiseAddAlloc',
38 'GaussianNoiseAddEx', 'GaussianNoiseAddExAlloc',
39 'Laplace', 'LaplaceAlloc',
40 'EdgeSharpening', 'EdgeSharpeningAlloc',
41 'Median', 'MedianAlloc', 'MedianEx', 'MedianExAlloc',
42 'Sigma', 'SigmaAlloc', 'SigmaEx', 'SigmaExAlloc',
43 'FloydSteinberg', 'FloydSteinbergAlloc',
44 'HilbertPeano', 'HilbertPeanoAlloc',
45 'Sepia', 'SepiaAlloc', 'SepiaEx', 'SepiaExAlloc']:
46 extend_submodule(FiltersSubmodule, name, c_filters.__getattribute__('GP_Filter' + name))
48 def array_to_kern(kernel, kernel_div):
49 h = len(kernel)
50 w = len(kernel[0])
52 # Assert that array is matrix of numbers
53 for i in range(0, h):
54 assert(len(kernel[i]) == w)
56 for j in kernel[i]:
57 assert(isinstance(j, float) or isinstance(j, int))
59 # flatten the python array into C array
60 karr = c_filters.new_float_array(w * h)
62 for i in range(0, h):
63 for j in range(0, w):
64 c_filters.float_array_setitem(karr, i * w + j, kernel[i][j])
66 kern = c_filters.GP_FilterKernel2D(w, h, karr, kernel_div)
68 return kern
70 def array_del(kern):
71 c_filters.delete_float_array(kern.kernel)
73 def Convolution(src, dst, kernel, kernel_div, callback=None):
74 """
75 Convolution(src, dst, kernel, kernel_div, callback=None)
77 Bilinear convolution. The kernel is two dimensional array of coefficients,
78 kern_div is used to divide the kernel weigthed sum.
79 """
80 kern = array_to_kern(kernel, kernel_div)
81 ret = c_filters.GP_FilterConvolution(src, dst, kern, callback)
82 array_del(kern)
83 return ret
85 extend_submodule(FiltersSubmodule, 'Convolution', Convolution)
87 def ConvolutionEx(src, x_src, y_src, w_src, h_src, dst, x_dst, y_dst,
88 kernel, kernel_div, callback=None):
89 """
90 ConvolutionEx(src, x_src, y_src, w_src, h_src, dst, x_dst, y_dst,
91 kernel, kernel_div, callback=None)
93 Bilinear convolution. The kernel is two dimensional array of coefficients,
94 kern_div is used to divide the kernel weigthed sum.
95 """
96 kern = array_to_kern(kernel, kernel_div);
97 ret = c_filters.GP_FilterConvolutionEx(src, x_src, y_src,
98 w_src, h_src, dst, x_dst, y_dst,
99 kern, callback)
100 array_del(kern)
101 return ret
103 extend_submodule(FiltersSubmodule, 'ConvolutionEx', ConvolutionEx)
105 def ConvolutionAlloc(src, kernel, kernel_div, callback=None):
107 ConvolutionAlloc(src, kernel, kernel_div, callback=None)
109 Bilinear convolution. The kernel is two dimensional array of coefficients,
110 kern_div is used to divide the kernel weigthed sum.
112 kern = array_to_kern(kernel, kernel_div);
113 ret = c_filters.GP_FilterConvolutionAlloc(src, kern, callback)
114 array_del(kern)
115 return ret
117 extend_submodule(FiltersSubmodule, 'ConvolutionAlloc', ConvolutionAlloc)
119 def ConvolutionExAlloc(src, x_src, y_src, w_src, h_src,
120 kernel, kernel_div, callback=None):
122 ConvolutionExAlloc(src, x_src, y_src, w_src, h_src,
123 kernel, kernel_div, callback=None)
125 Bilinear convolution. The kernel is two dimensional array of coefficients,
126 kern_div is used to divide the kernel weigthed sum.
128 kern = array_to_kern(kernel, kernel_div);
129 ret = c_filters.GP_FilterConvolutionExAlloc(src, x_src, y_src,
130 w_src, h_src, kern, callback)
131 array_del(kern)
132 return ret
134 extend_submodule(FiltersSubmodule, 'ConvolutionExAlloc', ConvolutionExAlloc)
136 # Imports from the SWIG module
137 import re
138 def strip_GP_Filter(s):
139 return re.sub('^GP_Filter', '', s)
141 # Import functions from the SWIG module
142 from ..utils import import_members
143 import_members(c_filters, module, sub=strip_GP_Filter,
144 include=[
145 '^GP_Filter.*Alloc',
146 '^GP_Filter[A-Za-z0-9]*$',
149 module['Convolution'] = Convolution
150 module['ConvolutionAlloc'] = ConvolutionAlloc
151 module['ConvolutionEx'] = ConvolutionEx
152 module['ConvolutionExAlloc'] = ConvolutionExAlloc
156 # Special handling for weighted median parameters
158 def array_to_weights(weights):
159 h = len(weights)
160 w = len(weights[0])
162 # Assert that array is matrix of numbers
163 for i in range(0, h):
164 assert(len(weights[i]) == w)
166 for j in weights[i]:
167 assert(isinstance(j, int) and j >= 0)
169 # flatten the python array into C array
170 warr = c_filters.new_uint_array(w * h)
172 for i in range(0, h):
173 for j in range(0, w):
174 c_filters.uint_array_setitem(warr, i * w + j, weights[i][j])
176 ret = c_filters.GP_MedianWeights(w, h, warr)
178 return ret
180 def uint_arr_del(w):
181 c_filters.delete_uint_array(w.weights)
183 def WeightedMedian(src, dst, weights, callback=None):
185 WeightedMedian(src, dst, weights, callback=None)
187 Weighted variant of median filter. Weights is a two dimensional
188 array of positive integers that describe weights for neighbour pixels.
190 w = array_to_weights(weights)
191 ret = c_filters.GP_FilterWeightedMedian(src, dst, w, callback)
192 uint_arr_del(w)
193 return ret
195 # extend_submodule(FiltersSubmodule, 'WeightedMedian', WeightedMedian)
196 # module['WeightedMedian'] = WeightedMedian
198 def WeightedMedianAlloc(src, weights, callback=None):
200 WeightedMedianAlloc(src, weights, callback=None)
202 Weighted variant of median filter. Weights is a two dimensional
203 array of positive integers that describe weights for neighbour pixels.
205 w = array_to_weights(weights)
206 ret = c_filters.GP_FilterWeightedMedianAlloc(src, w, callback)
207 uint_arr_del(w)
208 return ret
210 # extend_submodule(FiltersSubmodule, 'WeightedMedianAlloc', WeightedMedianAlloc)
211 # module['WeightedMedianAlloc'] = WeightedMedianAlloc
213 _init(locals())
214 del _init