-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockFinder.java
More file actions
152 lines (132 loc) · 3.36 KB
/
BlockFinder.java
File metadata and controls
152 lines (132 loc) · 3.36 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
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class BlockFinder {
BufferedImage bi;
ArrayList<Block> blocks = new ArrayList<Block>();
int width;
int height;
public static void main(String[] args) {
// TODO Auto-generated method stub291
if(args.length!=2){
System.out.println("USAGE: % java BlockFinder <bmp-file> <csv-file>");
return;
}
BlockFinder bf = new BlockFinder();
bf.readBmpFile(args[0]);
if(bf!=null){
int[] start = {0,0};
int[] end = {bf.width-1,bf.height-1};
int direction = 0;
bf.getBlocks(start,end,direction);
}
bf.outputBlocks2CSV(args[1]);
}
public void readBmpFile(String fname){
try {
bi = ImageIO.read(new File(fname));
width = this.bi.getWidth();
height = this.bi.getHeight();
} catch (Exception e) {
e.printStackTrace();
bi = null;
}
}
public void getBlocks(int[] start,int[] end,int direction){
int startno = this.getPositionNumber(start, direction);
int endno =this.getPositionNumber(end, direction);
for(int i=startno;i<=endno;i++){
int[] pos = getPosition(i,direction);
int color = this.bi.getRGB(pos[0], pos[1]);
if((color&0xFFFFFF)==0x00){
Block blk = getBlock(pos);
this.blocks.add(blk);
blk.printBlock();
}
}
}
public Block getBlock(int[] pos){
Block block = new Block();
block.x = pos[0]; block.y = pos[1];
int[] cur = new int[2];
cur[0]=pos[0]; cur[1]=pos[1];
block.width = 0;
block.height = 0;
//get width
while((this.bi.getRGB(cur[0], cur[1])&0xFFFF) == 0x00){
block.width++;
cur[0]++;
}
cur[0]=pos[0];
//get height
while((this.bi.getRGB(cur[0], cur[1])&0xFFFF) == 0x00){
block.height++;
cur[1]++;
}
if(block.width>=block.height){
block.orientation=0;
}else{
block.orientation=1;
}
//remove the block
for(int i=0;i<block.height;i++){
for(int j=0;j<block.width;j++){
cur[0]=pos[0]+j;
cur[1]=pos[1]+i;
this.bi.setRGB(cur[0], cur[1], -1);
}
}
return block;
}
public int[] getPosition(int no,int direction){
int[] pos = new int[2];
if(direction == 0){ // left to right
pos[1] = (int)((double)no/(double)this.width);
pos[0] = no%this.width;
}
else{ //top to bottom
pos[0] = (int)((double)no/(double)this.height);
pos[1] = no%this.height;
}
return pos;
}
public int getPositionNumber(int[] pos,int direction){
int no=0;
if(direction == 0){
no = pos[1] * width + pos[0];
}
else{
no = pos[0] * height + pos[1];
}
return no;
}
public int[] getColor(int col){
int[] rgb = new int[3];
rgb[0] = col & 0xFF;
rgb[1] = (col >>> 2) & 0xFF;
rgb[2] = (col >>> 4) & 0xFF;
return rgb;
}
public void outputBlocks2CSV(String filename){
try {
FileWriter fw = new FileWriter(filename, false);
PrintWriter pw = new PrintWriter(new BufferedWriter(fw));
pw.write("width,height\n");
pw.write(""+this.width+","+this.height+"\n\n");
pw.write("id,x,y,width,height,direction\n");
for(int i=0;i<this.blocks.size();i++){
Block b = this.blocks.get(i);
pw.write(i+","+b.toString()+"\n");
}
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}