-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColouringHelper.java
More file actions
196 lines (171 loc) · 6.04 KB
/
ColouringHelper.java
File metadata and controls
196 lines (171 loc) · 6.04 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
4-Colouring Program
Usage: java Colouring input.png outputname.png
Should 4-colour reasonably high resolution line art such that no adjacent sections of the image have the same colour.
Input must be a PNG.
Nicholas Kobald 16/04/15
*/
import java.awt.Color;
import javax.imageio.ImageIO;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Point;
import java.util.*;
public abstract class ColouringHelper extends ColouringBase {
protected int numSections; //number of sections
protected int [] sectionColors; //map section colours
public PNGCanvas p; //canvas we're doing most of our work on
int [][] visited;
int [][] partitions;
protected int maxSectionSize;
protected int [][] outlineArray;
protected boolean Vis(int x, int y){
if((x<0) || x>= WIDTH || y<0 || y >= WIDTH) return true;
if(visited[x][y] == 1) return true;
else return false;
}
protected void setVis(int x, int y){
if((x<0) || x>=p.width || y<0 || y>=p.height) return;
visited[x][y] = 1;
}
//populate outlineArray to have -1 at all (x,y) that PNG canvas has a black cell.
protected void fillOutLineArray(){
for(int i = 0; i<WIDTH;i++){
for(int j = 0; j<HEIGHT;j++){
if(p.GetPixel(i, j).equals(BLACK)) outlineArray[i][j] = -1;
}
}
}
/* Make all pixels black or white. */
protected void convertToBinary() {
for(int i =0;i<p.width;i++){
for(int j =0;j<p.height;j++){
if(p.intensity(i, j) > 200) p.SetPixel(i, j, WHITE); //white
else p.SetPixel(i, j, BLACK);
}
}
}
/* Move values onto PNG canvas */
protected void PopCanv(int [][] c){
for(int i = 0; i<c.length;i++){
for(int j = 0;j<c[0].length;j++){
p.SetPixel(i, j, new Color(c[i][j], c[i][j], c[i][j]));
}
}
}
protected void getLines(){
for(int i = 0; i<WIDTH; i++){
for(int j =0; j<HEIGHT; j++){
if(outlineArray[i][j] == -1) partitions[i][j] = -1;
}
}
}
protected int findSection(int x, int y){
for(int i = 0; true; i++){
if(getPartVal(x+i, y) != -2 && getPartVal(x+i, y) != -1 ) return getPartVal(x+i, y);
if(getPartVal(x-i, y) != -2 && getPartVal(x-i, y) != -1 ) return getPartVal(x-i, y);
if(getPartVal(x, y+i) != -2 && getPartVal(x, y+i) != -1 ) return getPartVal(x, y+i);
if(getPartVal(x, y-i) != -2 && getPartVal(x, y-i) != -1 ) return getPartVal(x, y-i);
}
}
protected int getPartVal(int x, int y){
if((x<0) || x>=WIDTH || y<0 || y>=HEIGHT) return -2;
return partitions[x][y];
}
protected void PartitionImage(){
visited = new int[WIDTH][HEIGHT];
int sectionId = -1;
for(int i = 0; i<WIDTH;i++)
for(int j = 0; j<HEIGHT; j++){
if(visited[i][j] == 0 && !(p.GetPixel(i,j).equals(BLACK))) {
getPartition(i, j, ++sectionId, visited);
}
visited[i][j] = 1;
}
this.numSections = sectionId+1;
getLines();
}
protected void getPartition(int x, int y, int sectionID, int [][] visited) {
Queue<Point> bfsQue = new LinkedList<Point>();
Color target = p.GetPixel(x, y);
Point temp;
int count = 0;
bfsQue.add(new Point(x,y));
while(bfsQue.peek() != null){
temp = bfsQue.poll();
count++;
x = temp.x;
y = temp.y;
visited[x][y] = 1;
//System.out.println("in call number" + sectionID + " currently at " + x + ", " + y);
partitions[x][y] = sectionID;
if(x+1 <= p.width && target.equals(p.GetPixel(x+1, y)) && !Vis(x+1, y)) {
setVis(x+1, y);
bfsQue.add(new Point(x+1, y));
}
if(x != 0 && target.equals(p.GetPixel(x-1, y))&& !Vis(x-1, y)) {
setVis(x-1, y);
bfsQue.add(new Point(x-1, y));
}
if((y!=0 && target.equals(p.GetPixel(x, y-1)) && !Vis(x, y-1))){
setVis(x, y-1);
bfsQue.add(new Point(x, y-1));
}
if((y+1 <= HEIGHT && target.equals(p.GetPixel(x, y+1)) && !Vis(x, y+1))) {
setVis(x, y+1);
bfsQue.add(new Point(x, y+1));
}
}
if(count>this.maxSectionSize)
this.maxSectionSize = count;
}
/* Prints an error message and exits (intended for user errors) */
protected static void ErrorExit(String errorMessage, Object... formatArgs){
System.err.printf("ERROR: " + errorMessage + "\n",formatArgs);
System.exit(0);
}
/* Throws a runtime error (intended for logic errors) */
protected static void ErrorAbort(String errorMessage, Object... formatArgs){
throw new Error(String.format(errorMessage,formatArgs));
}
protected static Color[][] load_image(String image_filename){
BufferedImage inputImage = null;
try{
System.err.printf("Reading image from %s\n",image_filename);
inputImage = ImageIO.read(new File(image_filename));
} catch(java.io.IOException e){
ErrorExit("Unable to open %s: %s\n",image_filename,e.getMessage());
}
int width = inputImage.getWidth();
int height = inputImage.getHeight();
Color[][] imagePixels = new Color[width][height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
imagePixels[x][y] = new Color(inputImage.getRGB(x,y));
return imagePixels;
}
protected static int[][] ColoursToIntensities(Color[][] inputPixels){
int width = inputPixels.length;
int height = inputPixels[0].length;
int[][] intensities = new int[width][height];
for (int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
intensities[x][y] = (inputPixels[x][y].getRed()+inputPixels[x][y].getGreen()+inputPixels[x][y].getBlue())/3;
return intensities;
}
protected static void save_image(Color[][] imagePixels, String image_filename){
int width = imagePixels.length;
int height = imagePixels[0].length;
BufferedImage outputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
outputImage.setRGB(x,y,imagePixels[x][y].getRGB());
try{
ImageIO.write(outputImage, "png", new File(image_filename));
}catch(java.io.IOException e){
ErrorExit("Unable to write %s: %s\n",image_filename,e.getMessage());
}
System.err.printf("Wrote a %d by %d image\n",width,height);
}
}