Skip to content

Commit 943f2df

Browse files
committed
Added arithmetic operations on images. Added Skeletization. Small fixes
1 parent f4fd70a commit 943f2df

16 files changed

+298
-30
lines changed

src/graphic/filter/Algorithms/AbstractMatrixAlgorithm.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public abstract class AbstractMatrixAlgorithm implements Algorithm {
11+
public abstract class AbstractMatrixAlgorithm extends Algorithm {
1212

1313
BufferedImage inImage;
1414

1515
@Override
1616
public BufferedImage execute(BufferedImage inputImage) {
17-
inImage = inputImage;
17+
inImage = copyImage(inputImage);
1818
for (int i = 1; i < inImage.getWidth() - 1; i++) {
1919
for (int j = 1; j < inImage.getHeight() - 1; j++) {
2020
inputImage.setRGB(i, j, getOutputRGB(i, j));
2121
}
2222
}
23-
return inImage;
23+
return inputImage;
2424
}
2525

2626
private int getOutputRGB(int x, int y) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package graphic.filter.Algorithms;
2+
3+
import java.awt.Color;
4+
import java.awt.image.BufferedImage;
5+
6+
public class AddImagesAlgorithm extends Algorithm {
7+
8+
BufferedImage inImage;
9+
10+
public BufferedImage execute(BufferedImage inputImage,
11+
BufferedImage inputImage2) {
12+
inImage = copyImage(inputImage);
13+
Color color;
14+
Color color2;
15+
for (int i = 1; i < inImage.getWidth() - 1; i++) {
16+
for (int j = 1; j < inImage.getHeight() - 1; j++) {
17+
color = new Color(inputImage.getRGB(i, j));
18+
color2 = new Color(inputImage2.getRGB(i, j));
19+
int newRed = color.getRed() + color2.getRed();
20+
int newGreen = color.getGreen() + color2.getGreen();
21+
int newBlue = color.getBlue() + color2.getBlue();
22+
Color newColor = new Color(getValidColorValue(newRed),
23+
getValidColorValue(newGreen),
24+
getValidColorValue(newBlue));
25+
inImage.setRGB(i, j, newColor.getRGB());
26+
}
27+
}
28+
return inImage;
29+
}
30+
31+
private int getValidColorValue(int colorValue) {
32+
if (colorValue < 0)
33+
return 0;
34+
else if (colorValue > 255)
35+
return 255;
36+
return colorValue;
37+
}
38+
39+
@Override
40+
public String getName() {
41+
return "Add images";
42+
}
43+
44+
@Override
45+
public BufferedImage execute(BufferedImage inputImage) {
46+
// TODO Auto-generated method stub
47+
return null;
48+
}
49+
50+
}
+12-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package graphic.filter.Algorithms;
22

33
import java.awt.image.BufferedImage;
4+
import java.awt.image.ColorModel;
5+
import java.awt.image.WritableRaster;
46

57
/**
68
*
79
* @author mmskrzyp
810
*
911
*/
10-
public interface Algorithm {
12+
public abstract class Algorithm {
1113

12-
public BufferedImage execute(BufferedImage inputImage);
14+
public abstract BufferedImage execute(BufferedImage inputImage);
1315

14-
public String getName();
16+
public abstract String getName();
17+
18+
public BufferedImage copyImage(BufferedImage bufferedImage) {
19+
ColorModel cm = bufferedImage.getColorModel();
20+
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
21+
WritableRaster raster = bufferedImage.copyData(null);
22+
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
23+
}
1524

1625
}

src/graphic/filter/Algorithms/AveragingFilterAlgorithm.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public class AveragingFilterAlgorithm implements Algorithm {
11+
public class AveragingFilterAlgorithm extends Algorithm {
1212

1313
BufferedImage inImage;
1414

1515
@Override
1616
public BufferedImage execute(BufferedImage inputImage) {
17-
inImage = inputImage;
17+
inImage = copyImage(inputImage);
1818
for (int i = 1; i < inImage.getWidth() - 1; i++) {
1919
for (int j = 1; j < inImage.getHeight() - 1; j++) {
20-
inImage.setRGB(i, j, getOutputRGB(i, j));
20+
inputImage.setRGB(i, j, getOutputRGB(i, j));
2121
}
2222
}
23-
return inImage;
23+
return inputImage;
2424
}
2525

2626
private int getOutputRGB(int x, int y) {

src/graphic/filter/Algorithms/BinaryzationFilterAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public class BinaryzationFilterAlgorithm implements Algorithm {
11+
public class BinaryzationFilterAlgorithm extends Algorithm {
1212

1313
@Override
1414
public BufferedImage execute(BufferedImage inputImage) {

src/graphic/filter/Algorithms/BlueChannelAlgotithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.awt.Color;
44
import java.awt.image.BufferedImage;
55

6-
public class BlueChannelAlgotithm implements Algorithm {
6+
public class BlueChannelAlgotithm extends Algorithm {
77

88
@Override
99
public BufferedImage execute(BufferedImage inputImage) {

src/graphic/filter/Algorithms/GreenChannelAlgotithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.awt.Color;
44
import java.awt.image.BufferedImage;
55

6-
public class GreenChannelAlgotithm implements Algorithm {
6+
public class GreenChannelAlgotithm extends Algorithm {
77

88
@Override
99
public BufferedImage execute(BufferedImage inputImage) {

src/graphic/filter/Algorithms/LaplacianFilterAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public double[][] getFactors() {
1414

1515
@Override
1616
public String getName() {
17-
return "Laplacian Filter (Sharpening)";
17+
return "Laplacian Filter";
1818
}
1919

2020
}

src/graphic/filter/Algorithms/MaximizingFilterAlgorithm.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public class MaximizingFilterAlgorithm implements Algorithm {
11+
public class MaximizingFilterAlgorithm extends Algorithm {
1212

1313
BufferedImage inImage;
1414

1515
@Override
1616
public BufferedImage execute(BufferedImage inputImage) {
17-
inImage = inputImage;
17+
inImage = copyImage(inputImage);
1818
for (int i = 1; i < inImage.getWidth() - 1; i++) {
1919
for (int j = 1; j < inImage.getHeight() - 1; j++) {
20-
inputImage.setRGB(i, j, getOutputRGB(i, j));
20+
inImage.setRGB(i, j, getOutputRGB(inputImage, i, j));
2121
}
2222
}
2323
return inImage;
2424
}
2525

26-
private int getOutputRGB(int x, int y) {
26+
private int getOutputRGB(BufferedImage inputImage, int x, int y) {
2727
int maxRed = 0, maxGreen = 0, maxBlue = 0;
2828
Color color;
2929
for (int i = 0; i < 3; i++) {
3030
for (int j = 0; j < 3; j++) {
31-
color = new Color(inImage.getRGB(x + i - 1, y + j - 1));
31+
color = new Color(inputImage.getRGB(x + i - 1, y + j - 1));
3232
if (maxRed < color.getRed())
3333
maxRed = color.getRed();
3434
if (maxGreen < color.getGreen())

src/graphic/filter/Algorithms/MinimazingFilterAlgorithm.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
import java.awt.Color;
44
import java.awt.image.BufferedImage;
55

6-
public class MinimazingFilterAlgorithm implements Algorithm {
6+
public class MinimazingFilterAlgorithm extends Algorithm {
77

8-
BufferedImage inImage;
8+
private BufferedImage inImage;
99

1010
@Override
1111
public BufferedImage execute(BufferedImage inputImage) {
12-
inImage = inputImage;
12+
inImage = copyImage(inputImage);
1313
for (int i = 1; i < inImage.getWidth() - 1; i++) {
1414
for (int j = 1; j < inImage.getHeight() - 1; j++) {
15-
inputImage.setRGB(i, j, getOutputRGB(i, j));
15+
inImage.setRGB(i, j, getOutputRGB(inputImage, i, j));
1616
}
1717
}
1818
return inImage;
1919
}
2020

21-
private int getOutputRGB(int x, int y) {
21+
private int getOutputRGB(BufferedImage inputImage, int x, int y) {
2222
int minRed = 255, minGreen = 255, minBlue = 255;
2323
Color color;
2424
for (int i = 0; i < 3; i++) {
2525
for (int j = 0; j < 3; j++) {
26-
color = new Color(inImage.getRGB(x + i - 1, y + j - 1));
26+
color = new Color(inputImage.getRGB(x + i - 1, y + j - 1));
2727
if (minRed > color.getRed())
2828
minRed = color.getRed();
2929
if (minGreen > color.getGreen())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package graphic.filter.Algorithms;
2+
3+
import java.awt.Color;
4+
import java.awt.image.BufferedImage;
5+
6+
public class MultiplyImagesAlgorithm extends Algorithm {
7+
8+
BufferedImage inImage;
9+
10+
public BufferedImage execute(BufferedImage inputImage,
11+
BufferedImage inputImage2) {
12+
inImage = copyImage(inputImage);
13+
Color color;
14+
Color color2;
15+
for (int i = 1; i < inImage.getWidth() - 1; i++) {
16+
for (int j = 1; j < inImage.getHeight() - 1; j++) {
17+
color = new Color(inputImage.getRGB(i, j));
18+
color2 = new Color(inputImage2.getRGB(i, j));
19+
int newRed = color.getRed() * color2.getRed();
20+
int newGreen = color.getGreen() * color2.getGreen();
21+
int newBlue = color.getBlue() * color2.getBlue();
22+
Color newColor = new Color(getValidColorValue(newRed),
23+
getValidColorValue(newGreen),
24+
getValidColorValue(newBlue));
25+
inImage.setRGB(i, j, newColor.getRGB());
26+
}
27+
}
28+
return inImage;
29+
}
30+
31+
private int getValidColorValue(int colorValue) {
32+
if (colorValue < 0)
33+
return 0;
34+
else if (colorValue > 255)
35+
return 255;
36+
return colorValue;
37+
}
38+
39+
@Override
40+
public String getName() {
41+
return "Multiply images";
42+
}
43+
44+
@Override
45+
public BufferedImage execute(BufferedImage inputImage) {
46+
// TODO Auto-generated method stub
47+
return null;
48+
}
49+
50+
}

src/graphic/filter/Algorithms/RedChannelAlgotithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public class RedChannelAlgotithm implements Algorithm {
11+
public class RedChannelAlgotithm extends Algorithm {
1212

1313
@Override
1414
public BufferedImage execute(BufferedImage inputImage) {

src/graphic/filter/Algorithms/RevertColorsFilterAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author mmskrzyp
99
*
1010
*/
11-
public class RevertColorsFilterAlgorithm implements Algorithm {
11+
public class RevertColorsFilterAlgorithm extends Algorithm {
1212

1313
@Override
1414
public BufferedImage execute(BufferedImage inputImage) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package graphic.filter.Algorithms;
2+
3+
public class SharpeningFilterAlgorithm extends AbstractMatrixAlgorithm {
4+
5+
@Override
6+
public double[][] getFactors() {
7+
return new double[][] { { -1, 0, -1 }, { 0, 5, 0 }, { -1, 0, -1 } };
8+
}
9+
10+
@Override
11+
public String getName() {
12+
return "Sharpening Filter";
13+
}
14+
15+
}

0 commit comments

Comments
 (0)