File: DespeckleFilter.java

package info (click to toggle)
libjhlabs-filters-java 2.0.235-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,132 kB
  • sloc: java: 16,740; xml: 17; sh: 11; makefile: 5
file content (119 lines) | stat: -rw-r--r-- 2,948 bytes parent folder | download | duplicates (3)
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
/*
Copyright 2006 Jerry Huxtable

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.jhlabs.image;

import java.awt.*;
import java.awt.image.*;

public class DespeckleFilter extends WholeImageFilter {

	public DespeckleFilter() {
	}

	protected short pepperAndSalt( short c, short v1, short v2 ) {
		if ( c < v1 )
			c++;
		if ( c < v2 )
			c++;
		if ( c > v1 )
			c--;
		if ( c > v2 )
			c--;
		return c;
	}
	
	protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
		int index = 0;
		short[][] r = new short[3][width];
		short[][] g = new short[3][width];
		short[][] b = new short[3][width];
		int[] outPixels = new int[width * height];

		for (int x = 0; x < width; x++) {
			int rgb = inPixels[x];
			r[1][x] = (short)((rgb >> 16) & 0xff);
			g[1][x] = (short)((rgb >> 8) & 0xff);
			b[1][x] = (short)(rgb & 0xff);
		}
		for (int y = 0; y < height; y++) {
			boolean yIn = y > 0 && y < height-1;
			int nextRowIndex = index+width;
			if ( y < height-1) {
				for (int x = 0; x < width; x++) {
					int rgb = inPixels[nextRowIndex++];
					r[2][x] = (short)((rgb >> 16) & 0xff);
					g[2][x] = (short)((rgb >> 8) & 0xff);
					b[2][x] = (short)(rgb & 0xff);
				}
			}
			for (int x = 0; x < width; x++) {
				boolean xIn = x > 0 && x < width-1;
				short or = r[1][x];
				short og = g[1][x];
				short ob = b[1][x];
				int w = x-1;
				int e = x+1;
				
				if ( yIn ) {
					or = pepperAndSalt( or, r[0][x], r[2][x] );
					og = pepperAndSalt( og, g[0][x], g[2][x] );
					ob = pepperAndSalt( ob, b[0][x], b[2][x] );
				}

				if ( xIn ) {
					or = pepperAndSalt( or, r[1][w], r[1][e] );
					og = pepperAndSalt( og, g[1][w], g[1][e] );
					ob = pepperAndSalt( ob, b[1][w], b[1][e] );
				}

				if ( yIn && xIn ) {
					or = pepperAndSalt( or, r[0][w], r[2][e] );
					og = pepperAndSalt( og, g[0][w], g[2][e] );
					ob = pepperAndSalt( ob, b[0][w], b[2][e] );

					or = pepperAndSalt( or, r[2][w], r[0][e] );
					og = pepperAndSalt( og, g[2][w], g[0][e] );
					ob = pepperAndSalt( ob, b[2][w], b[0][e] );
				}

				outPixels[index] = (inPixels[index] & 0xff000000) | (or << 16) | (og << 8) | ob;
				index++;
			}
			short[] t;
			t = r[0];
			r[0] = r[1];
			r[1] = r[2];
			r[2] = t;
			t = g[0];
			g[0] = g[1];
			g[1] = g[2];
			g[2] = t;
			t = b[0];
			b[0] = b[1];
			b[1] = b[2];
			b[2] = t;
		}
	
		return outPixels;
	}

	public String toString() {
		return "Blur/Despeckle...";
	}

}