-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.java
More file actions
139 lines (117 loc) · 4.85 KB
/
Image.java
File metadata and controls
139 lines (117 loc) · 4.85 KB
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
/* Name: Clara Fee, Julia Rieger
* File: Image.java
* Desc:
*
* Class to store one image with all RGB values
*
*/
public class Image {
int width;
int height;
MyColor[][] colorArray;
String filename;
public Image(MyColor[][] colorArray, int width, int height) {
this.colorArray = colorArray;
this.width = width;
this.height = height;
this.filename = "";
}
public void setFilename(String newName) {
this.filename = newName;
}
public String getFilename() {
return this.filename;
}
public MyColor[][] getColorArray() {
return this.colorArray;
}
public int getHeight() {
return this.height;
}
public int getWidth() {
return this.width;
}
public int getSize() {
return this.width * this.height;
}
public MyColor getStartPixel() {
return this.colorArray[0][0];
}
public MyColor getEndPixel() {
return this.colorArray[width-1][height-1];
}
public void shade(double shadeFactor) throws IllegalArgumentException {
if (shadeFactor == -1) {
for (int i = 0; i < this.getHeight(); i++) {
for (int j = 0; j < this.getWidth(); j++) {
colorArray[i][j].negative();
}
}
}
else {
if ((shadeFactor < 0) || (shadeFactor > 1)) {
throw new IllegalArgumentException("shadeFactor must be between 0 and 1!");
}
for (int i = 0; i < this.getHeight(); i++) {
for (int j = 0; j < this.getWidth(); j++) {
colorArray[i][j].shade(shadeFactor);
}
}
}
}
public Image edgeDetection(double[][] edgeDetectionFilter, int startI, int startJ, int endI, int endJ) {
double w1 = edgeDetectionFilter[0][0];
double w2 = edgeDetectionFilter[1][0];
double w3 = edgeDetectionFilter[2][0];
double w4 = edgeDetectionFilter[0][1];
double w5 = edgeDetectionFilter[1][1];
double w6 = edgeDetectionFilter[2][1];
double w7 = edgeDetectionFilter[0][2];
double w8 = edgeDetectionFilter[1][2];
double w9 = edgeDetectionFilter[2][2];
double newRed = 0;
double newBlue = 0;
double newGreen = 0;
MyColor bottomLeft;
MyColor left;
MyColor topLeft;
MyColor bottom;
MyColor center;
MyColor top;
MyColor bottomRight;
MyColor right;
MyColor topRight;
MyColor[][] colorArray = this.getColorArray();
MyColor[][] returnThisArr = new MyColor[colorArray.length][colorArray[0].length];
for (int i = startI; i < endI; i++) {
for (int j = startJ; j < endJ; j++) {
if (((i - 1) > startI) && ((i + 1) < endI) && ((j - 1) > startJ) && ((j + 1) < endJ)) {
topLeft = colorArray[i - 1][j - 1];
left = colorArray[i][j - 1];
bottomLeft = colorArray[i + 1][j - 1];
top = colorArray[i - 1][j];
center = colorArray[i][j];
bottom = colorArray[i + 1][j];
topRight = colorArray[i - 1][j + 1];
right = colorArray[i][j + 1];
bottomRight = colorArray[i + 1][j + 1];
newRed = Math.abs((topLeft.getR() * w1) + (top.getR() * w2) + (topRight.getR() * w3) + (left.getR() * w4) + (center.getR() * w5) + (right.getR() * w6) + (bottomLeft.getR() * w7) + (bottom.getR() * w8) + (bottomRight.getR() * w9));
newGreen = Math.abs((topLeft.getG() * w1) + (top.getG() * w2) + (topRight.getG() * w3) + (left.getG() * w4) + (center.getG() * w5) + (right.getG() * w6) + (bottomLeft.getG() * w7) + (bottom.getG() * w8) + (bottomRight.getG() * w9));
newBlue = Math.abs((topLeft.getB() * w1) + (top.getB() * w2) + (topRight.getB() * w3) + (left.getB() * w4) + (center.getB() * w5) + (right.getB() * w6) + (bottomLeft.getB() * w7) + (bottom.getB() * w8) + (bottomRight.getB() * w9));
if (newRed > 255) {
newRed = 255;
}
if (newGreen > 255) {
newGreen = 255;
}
if (newBlue > 255) {
newBlue = 255;
}
}
MyColor newColor = new MyColor((int) newRed, (int) newGreen, (int) newBlue, i, j);
returnThisArr[i][j] = newColor;
}
}
return new Image(returnThisArr, returnThisArr.length, returnThisArr[0].length);
}
}