-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.java
181 lines (151 loc) · 5.08 KB
/
Image.java
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
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.commons.io.FilenameUtils;
/*******************************************
*
* @author Goirad
*
*
*Utility class to modularize the images that I'm analyzing,
*provides functionality for quickly reading color data and others.
*
*Also acts to store stuff like where the file currently is,
*as well as a unique ID that doesn't change after filename changes.
*/
public class Image {
private static int totalImages = 0; //total # of instances of this class
private double percentR, totalR; //data for each of red green and blue
private double percentG, totalG;
private double percentB, totalB;
private double H;
private double S;
private double L;
private int width, height; //size of image in pixels
private int ID; //unique ID
private File file; //current location on disk
private boolean hasAlpha; //whether the image has alpha channels
/*****************************************************
* @param inputFile the location of the image
*
* Constructor takes input file and tries to read it as an image, extracts data
*/
public Image(File inputFile) {
file = inputFile;
BufferedImage img = null;
try{
img = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
get2D(img);
ID = totalImages; //labels instance and increments counter
totalImages++;
img = null; //dumps potentially huge file
}
/*************************
* Converts image to user readable string
*/
public String toString(){
return "R " + file + " " + percentR + " G" + percentG + " B" + percentB + " -" + ID + "." + getType();
}
/*************************
* returns the type (jpg, png, etc)
* @return the file type
*/
public String getType(){
return FilenameUtils.getExtension(file.getPath());
}
/*************************
* returns the average brightness of the whole image, scaled for better quality
* @return the average brightness of the whole image
*/
public double getBrightness(){
return (percentR*.21 + percentG*.71 + percentB*.08)/(2.55*getArea());
}
/*************************
* @return the percent of the total image that is red
*/
public double getPercentR() {
return percentR;
}
/*************************
* @return the percent of the total image that is green
*/
public double getPercentG() {
return percentG;
}
/*************************
* @return the percent of the total image that is blye
*/
public double getPercentB() {
return percentB;
}
/*************************
* @return the width of the image in pixels
*/
public int getWidth() {
return width;
}
/*************************
* @return the height of the image in pixels
*/
public int getHeight() {
return height;
}
/*************************
* @return the image's unique identifier
*/
public int getID() {
return ID;
}
/*************************
* @return the current file the image is stored as
*/
public File getFile() {
return file;
}
/*************************
* @return the total number of pixels in the image
*/
public double getArea(){
return width*height;
}
/*************************
* This is where the magic happens, the image is processed and the color data extracted.
*/
private void get2D(BufferedImage img) {
final byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
width = img.getWidth();
height = img.getHeight();
hasAlpha = img.getAlphaRaster() != null;
if (hasAlpha) {
final int pixelLength = 4;
for (int pixel = 0; pixel < pixels.length; pixel += pixelLength) {
totalB += ((int) pixels[pixel + 1] & 0xff); // blue
totalB += (((int) pixels[pixel + 2] & 0xff)); // green
totalB += (((int) pixels[pixel + 3] & 0xff)); // red
}
} else {
final int pixelLength = 3;
for (int pixel = 0; pixel < pixels.length; pixel += pixelLength) {
totalB += ((int) pixels[pixel] & 0xff); // blue
totalG += (((int) pixels[pixel + 1] & 0xff)); // green
totalR += (((int) pixels[pixel + 2] & 0xff)); // red
}
}
percentR = totalR/2.55/getArea(); //converts totals to percents
percentG = totalG/2.55/getArea();
percentB = totalB/2.55/getArea();
}
public double getH() {
return H;
}
public double getS() {
return S;
}
public double getL() {
return L;
}
}