Libraryless. Click here for Pure Java version (2308L/15K).
1 | // from http://www.jhlabs.com/ip/blurring.html |
2 | |
3 | /* |
4 | Copyright 2006 Jerry Huxtable |
5 | |
6 | Licensed under the Apache License, Version 2.0 (the "License"); |
7 | you may not use this file except in compliance with the License. |
8 | You may obtain a copy of the License at |
9 | |
10 | http://www.apache.org/licenses/LICENSE-2.0 |
11 | |
12 | Unless required by applicable law or agreed to in writing, software |
13 | distributed under the License is distributed on an "AS IS" BASIS, |
14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | See the License for the specific language governing permissions and |
16 | limitations under the License. |
17 | */ |
18 | |
19 | sclass BoxBlurFilter extends AbstractBufferedImageOp implements IF1<BufferedImage> { |
20 | int hRadius; |
21 | int vRadius; |
22 | int iterations = 1; |
23 | |
24 | *() {} |
25 | *(int radius) { hRadius = vRadius = radius; } |
26 | *(int *hRadius, int *vRadius) {} |
27 | |
28 | public BufferedImage get(BufferedImage src) { |
29 | ret filter(src, null); |
30 | } |
31 | |
32 | public BufferedImage filter(BufferedImage src, BufferedImage dst) { |
33 | int width = src.getWidth(); |
34 | int height = src.getHeight(); |
35 | |
36 | if ( dst == null ) |
37 | dst = createCompatibleDestImage( src, null ); |
38 | |
39 | int[] inPixels = new int[width*height]; |
40 | int[] outPixels = new int[width*height]; |
41 | getRGB( src, 0, 0, width, height, inPixels ); |
42 | |
43 | for (int i = 0; i < iterations; i++ ) { |
44 | blur( inPixels, outPixels, width, height, hRadius ); |
45 | blur( outPixels, inPixels, height, width, vRadius ); |
46 | } |
47 | |
48 | setRGB( dst, 0, 0, width, height, inPixels ); |
49 | return dst; |
50 | } |
51 | |
52 | static void blur( int[] in, int[] out, int width, int height, int radius ) { |
53 | int widthMinus1 = width-1; |
54 | int tableSize = 2*radius+1; |
55 | int divide[] = new int[256*tableSize]; |
56 | |
57 | for ( int i = 0; i < 256*tableSize; i++ ) |
58 | divide[i] = i/tableSize; |
59 | |
60 | int inIndex = 0; |
61 | |
62 | for ( int y = 0; y < height; y++ ) { |
63 | int outIndex = y; |
64 | int ta = 0, tr = 0, tg = 0, tb = 0; |
65 | |
66 | for ( int i = -radius; i <= radius; i++ ) { |
67 | int rgb = in[inIndex + clamp(i, 0, width-1)]; |
68 | ta += (rgb >> 24) & 0xff; |
69 | tr += (rgb >> 16) & 0xff; |
70 | tg += (rgb >> 8) & 0xff; |
71 | tb += rgb & 0xff; |
72 | } |
73 | |
74 | for ( int x = 0; x < width; x++ ) { |
75 | out[ outIndex ] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb]; |
76 | |
77 | int i1 = x+radius+1; |
78 | if ( i1 > widthMinus1 ) |
79 | i1 = widthMinus1; |
80 | int i2 = x-radius; |
81 | if ( i2 < 0 ) |
82 | i2 = 0; |
83 | int rgb1 = in[inIndex+i1]; |
84 | int rgb2 = in[inIndex+i2]; |
85 | |
86 | ta += ((rgb1 >> 24) & 0xff)-((rgb2 >> 24) & 0xff); |
87 | tr += ((rgb1 & 0xff0000)-(rgb2 & 0xff0000)) >> 16; |
88 | tg += ((rgb1 & 0xff00)-(rgb2 & 0xff00)) >> 8; |
89 | tb += (rgb1 & 0xff)-(rgb2 & 0xff); |
90 | outIndex += height; |
91 | } |
92 | inIndex += width; |
93 | } |
94 | } |
95 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1018734 |
Snippet name: | BoxBlurFilter |
Eternal ID of this version: | #1018734/9 |
Text MD5: | 64c48b501edb6a3f15c13bec593bdeec |
Transpilation MD5: | 8c24f9c63a1c76f5eeb64a40bf6972c4 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-08-24 15:02:53 |
Source code size: | 3223 bytes / 95 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 445 / 1049 |
Version history: | 8 change(s) |
Referenced in: | [show references] |