-
Notifications
You must be signed in to change notification settings - Fork 45
/
README
242 lines (185 loc) · 9.3 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
Canvas filters
--------------
This library implements a few image processing filters using the canvas element.
The filters operate on ImageData objects. The filters do not modify the
source ImageData.
Based on http://www.html5rocks.com/en/tutorials/canvas/imagefilters/
Smoke tests online at http://fhtr.org/canvasfilters/
LICENSE
-------
MIT
API Documentation
-----------------
Filters : {
//
// Convenience functions
//
// filterImage applies a filter function to an image or canvas element.
// Arguments from the third onwards are passed as extra arguments to the filter function.
ImageData filterImage(Function filter, Image_or_Canvas image, Filter_arguments var_args, ...)
// getPixels returns the ImageData object for an image or a canvas element.
ImageData getPixels(Image_or_Canvas img)
// toCanvas returns a new canvas filled with the given ImageData object.
Canvas toCanvas(ImageData pixels)
// getCanvas creates a canvas of the wanted dimensions
Canvas getCanvas(int width, int height)
// createImageData creates an ImageData object of the wanted dimensions
ImageData createImageData(int width, int height)
// createImageData creates an ImageData-like object backed by a Float32Array
// of the wanted dimensions
ImageDataFloat32 createImageDataFloat32(int width, int height)
// bilinearSample bilinearly samples the image at the given coordinates.
// The result is computed by linear blending of the four pixels around x,y.
[r,g,b,a] bilinearSample(ImageData pixels, float x, float y)
//
// Distort filters
//
// identity returns a copy of the ImageData
ImageData identity(ImageData pixels)
// horizontalFlip flips the image left-right
ImageData horizontalFlip(ImageData pixels)
// verticalFlip flips the image upside down
ImageData verticalFlip(ImageData pixels)
// distortSine distorts the image by pinching / punching it by the given amount.
// The distort amounts should be between -0.5 and 0.5.
ImageData distortSine(ImageData pixels, float xAmount, float yAmount)
//
// Color filters
//
// luminance converts the image to grayscale using the CIE luminance
// (0.2126*r + 0.7152*g + 0.0722*b)
ImageData luminance(ImageData pixels)
// grayscale converts the image to grayscale using
// (0.3*r + 0.59*g + 0.11*b)
ImageData grayscale(ImageData pixels)
// grayscaleAvg converts the image to grayscale using
// (r+g+b) / 3
ImageData grayscaleAvg(ImageData pixels)
// threshold converts the image to a two-color image with
// pixels brighter than or equal to the threshold value rendered white and
// pixels darker than the threshold rendered black
// The filter uses grayscale to compute the value of a pixel.
// (0.3*r + 0.59*g + 0.11*b)
ImageData threshold(ImageData pixels, int threshold)
// invert inverts the RGB channels of the image.
// The inverted version of a pixel is [255-r, 255-g, 255-b, a]
ImageData invert(ImageData pixels)
// invert inverts the RGB channels of the image.
// The inverted version of a pixel is [255-r, 255-g, 255-b, a]
ImageData invert(ImageData pixels)
// brightnessContrast adjusts the brightness and contrast of the image.
// The brightness value ranges between -1 .. 1, with 0 being neutral.
// The contrast value ranges between 0 .. 127, with 1 being neutral.
ImageData brightnessContrast(ImageData pixels, float brightness, float contrast)
// applyLUT applies a color lookup table to the image.
// The lookup table is an object of form
// {r:Uint8[256], g:Uint8[256], b:Uint8[256], a:Uint8[256]}
// Result pixel values are calculated by looking up the current value from
// the corresponding lookup table: [lut.r[r], lut.g[g], lut.b[b], lut.a[a]]
ImageData applyLUT(ImageData pixels, LookUpTable lut)
//
// Convolution filters
//
// convolve convolves the image using the weights array as a square
// row-major convolution matrix.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageData convolve(ImageData pixels, Array weights, bool opaque)
// horizontalConvolve convolves the image using a horizontal weights vector.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageData horizontalConvolve(ImageData pixels, Array weights, bool opaque)
// verticalConvolve convolves the image using a vertical weights vector.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageData verticalConvolve(ImageData pixels, Array weights, bool opaque)
// separableConvolve convolves the image using vertically and horizontally
// using the supplied vectors. Faster than convolve for separable kernels.
ImageData separableConvolve(ImageData pixels,
Array horizWeights,
Array vertWeights,
bool opaque)
// convolveFloat32 is a version of convolve that operates on ImageData-like
// objects with a Float32Array storing the pixels
// {width:int, height:int, data:Float32Array}.
// Useful when you need a high value range or negative values in pixels.
ImageDataFloat32 convolveFloat32(ImageData pixels, Array weights, bool opaque)
// horizontalConvolveFloat32 convolves the image using a horizontal weights
// vector.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageDataFloat32 horizontalConvolveFloat32(ImageData pixels,
Array weights,
bool opaque)
// verticalConvolveFloat32 convolves the image using a vertical weights
// vector.
// Returns a ImageDataFloat32.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageDataFloat32 verticalConvolveFloat32(ImageData pixels,
Array weights,
bool opaque)
// separableConvolveFloat32 convolves the image using vertically and
// horizontally using the supplied vectors. Faster than convolve for separable
// kernels.
// Returns a ImageDataFloat32.
// If the opaque argument is set to true the result image will have
// an opaque alpha channel.
ImageDataFloat32 separableConvolveFloat32(ImageData pixels,
Array horizWeights,
Array vertWeights,
bool opaque)
//
// Pre-defined convolution filters
//
// gaussianBlur applies a gaussian blur kernel of the wanted diameter on the image.
ImageData gaussianBlur(ImageData pixels, float diameter)
// laplace applies a Laplace edge detection kernel on the image.
ImageData laplace(ImageData pixels)
// sobel applies a Sobel filter on the image.
// This filter is purely for looks, the red channel encodes absolute vertical
// gradient and the green channel absolute horizontal gradient.
ImageData sobel(ImageData pixels)
// sobelVectors computes the signed horizontal and vertical gradients of the image
// and returns the array of resulting 2-vectors, packed tightly into a Float32Array
Float32Vec2ImageData sobelVectors(ImageData pixels)
// sobelVerticalGradient computes the signed vertical gradient of the image
ImageDataFloat32 sobelVerticalGradient(ImageData pixels)
// sobelHorizontalGradient computes the signed horizontal gradient of the image
ImageDataFloat32 sobelHorizontalGradient(ImageData pixels)
//
// Blend operations
//
// darkenBlend blends b on top of a, replacing a with b whenever b is darker.
// The filter operates on a per-channel basis, the result pixels
// are computed as [min(a.r, b.r), min(a.g, b.g), min(a.b, b.b), alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData darkenBlend(ImageData a, ImageData b)
// lightenBlend blends b on top of a, replacing a with b whenever b is lighter.
// The filter operates on a per-channel basis, the result pixels
// are computed as [max(a.r, b.r), max(a.g, b.g), max(a.b, b.b), alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData lightenBlend(ImageData a, ImageData b)
// addBlend blends b on top of a, adding b's values to a.
// [a.r+b.r, a.g+b.g, a.b+b.b, alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData addBlend(ImageData a, ImageData b)
// subBlend blends b on top of a, subtracting b's values to a.
// [a.r-(255-b.r), a.g-(255-b.g), a.b-(255-b.b), alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData subBlend(ImageData a, ImageData b)
// multiplyBlend blends b on top of a, multiplying b with a.
// [a.r*b.r/255, a.g*b.g/255, a.b*b.b/255, alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData multiplyBlend(ImageData a, ImageData b)
// screenBlend blends b on top of a with the screen blend mode.
// Makes a brighter by an amount determined by b.
// [255 - (255 - b.c)*(255 - a.c)/255, ..., alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData screenBlend(ImageData a, ImageData b)
// differenceBlend blends b on top of a by taking the absolute difference
// between the images.
// [Math.abs(a.c-b.c), alpha(a.a, b.a)]
// where alpha(a, b) = a + (255-a)*b/255.
ImageData differenceBlend(ImageData a, ImageData b)
}