-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoAdjust.java
More file actions
41 lines (31 loc) · 1.28 KB
/
AutoAdjust.java
File metadata and controls
41 lines (31 loc) · 1.28 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
public class AutoAdjust {
public static final float BLACK = 32;
public static final float GREY = 63.5f;
public static final float WHITE = 95;
public static String[] adjust(String[] input)
{
float minPixel = Float.MAX_VALUE;
float maxPixel = 0;
for (String row : input) {
for (float pixel : row.getBytes()) {
minPixel = Math.min(minPixel, pixel);
maxPixel = Math.max(maxPixel, pixel);
}
}
float brightness = GREY - ((maxPixel + minPixel) / 2f);
float contrast = (WHITE - BLACK) / (maxPixel - minPixel);
String[] output = new String[input.length];
for (int row = 0; row < input.length; row++) {
byte[] inputPixels = input[row].getBytes();
char[] outputPixels = new char[inputPixels.length];
for (int col=0; col < inputPixels.length; col++) {
outputPixels[col] = adjustPixel(inputPixels[col], brightness, contrast);
}
output[row] = new String(outputPixels);
}
return output;
}
private static char adjustPixel(byte inputPixel, float brightness, float contrast) {
return (char) (Math.round(((float) inputPixel + brightness - GREY) * contrast) + GREY);
}
}