-
Notifications
You must be signed in to change notification settings - Fork 12
/
Matrix.pde
194 lines (161 loc) · 5.12 KB
/
Matrix.pde
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
class Matrix {
ArrayList frames = new ArrayList();
public int rad = 70;
int border = 10;
public int rows = 0;
public int cols = 0;
public PixelColor current_color;
Frame copy_frame;
int SCALE = 1;
int current_frame_nr;
Matrix(int cols, int rows ) {
this.cols = cols; //X
this.rows = rows; //Y
this.current_color = new PixelColor();
add_frame();
}
public int width() {
return cols * rad;
}
public int height() {
return rows * rad;
}
public PGraphics current_frame_image() {
return this.current_frame_image(rad, border);
}
public PGraphics current_frame_image(int draw_rad, int draw_border) {
return this.current_frame().draw_full(draw_rad, draw_border);
}
public boolean click(int x, int y, boolean dragged) {
if( x < 0 || y < 0) return false; //25 pixel right and bottom for row editing
if( x > this.width() + 25 || y > this.height() + 25) return false; //25 pixel right and bottom for row editing
PixelColor pc = this.current_frame().update(x / rad, y / rad, current_color, !dragged);
if( pc == null ) return false;
current_color = pc.clone();
return true;
}
int num_frames() {
return frames.size();
}
Frame next_frame() {
current_frame_nr = (current_frame_nr + 1 ) % num_frames();
return current_frame();
}
Frame previous_frame() {
current_frame_nr = ( current_frame_nr == 0 ) ? num_frames() - 1 : current_frame_nr - 1;
return current_frame();
}
Frame first_frame() {
current_frame_nr = 0;
return current_frame();
}
/* +++++++++++++++ DATA STRUCTURE +++++++++++++++ */
Frame current_frame() {
return frame(current_frame_nr);
}
Frame frame(int f) {
try {
return (Frame) frames.get(f);
}
catch(Exception e ) {
return (Frame) frames.get(0);
}
}
void set_pixel(int f, int x, int y, PixelColor pc) {
frame(f).set_colored_pixel(x, y, pc);
}
/* +++++++++++++++ FRAME +++++++++++++++ */
Frame copy_frame() {
copy_frame = current_frame().clone();
return current_frame();
}
/* +++++++++++++++ FRAME +++++++++++++++ */
Frame paste_frame() {
if( copy_frame != null) frames.set(current_frame_nr, copy_frame.clone()); // better use set_pixel here!?
return current_frame();
}
Frame add_frame() {
if(!frames.isEmpty()) current_frame_nr++;
frames.add(current_frame_nr, new Frame(this.cols, this.rows)); //init first frame
return current_frame();
}
Frame delete_frame() {
matrix.copy_frame();
if(this.num_frames() > 1) {
frames.remove(current_frame_nr);
current_frame_nr = current_frame_nr % num_frames();
}
return current_frame();
}
/* +++++++++++++++ FILE +++++++++++++++ */
void save_to_file(String savePath) {
if( match(savePath, ".bmp") == null ) savePath += ".bmp";
int height = (int) Math.sqrt(this.num_frames());
while( this.num_frames() % height != 0) {
height--;
}
int width = this.num_frames() / height;
PImage output = createImage( width * this.cols, height * this.rows, RGB);
for(int h = 0; h < height; h++) {
for(int w = 0; w < width; w++) {
Frame frame = this.frame(h*width + w);
for(int y = 0; y < frame.rows; y++) {
for(int x = 0; x < frame.cols; x++) {
output.set(x + frame.cols * w, y + frame.rows * h, frame.get_pixel(x,y).get_color() );
}
}
}
}
output.save(savePath); //TODO add scaling??
println("SAVED to " + savePath);
}
Matrix load_mtx(String loadPath) {
PixelColor pc;
Matrix matrix = new Matrix(this.cols, this.rows); //actually we have to read values from File!
Frame frame = matrix.current_frame();
BufferedReader reader = createReader(loadPath);
String line = "";
while( line != null ) {
try {
line = reader.readLine();
if(line != null && line.length() > 0) {
String[] str = line.split(",");
for(int y = 0; y < frame.rows; y++) {
//invert matrix
pc = new PixelColor(~Integer.parseInt(str[y*3]), ~Integer.parseInt(str[y*3 + 1]), ~Integer.parseInt(str[y*3 + 2]) );
frame.set_row(7-y, pc );
}
frame = matrix.add_frame();
}
}
catch (IOException e) {
e.printStackTrace();
return matrix;
}
}
matrix.delete_frame();
return matrix;
}
Matrix load_bmp(String loadPath) {
Matrix matrix = new Matrix(this.cols, this.rows);
PImage input = loadImage(loadPath);
input.loadPixels();
int width = input.width / this.cols / SCALE;
int height = input.height / this.rows / SCALE;
Frame frame = matrix.current_frame();
for(int h = 0; h < height; h++) {
for(int w = 0; w < width; w++) {
for(int y = 0; y < frame.rows; y++) {
for(int x = 0; x < frame.cols; x++) {
int off = h * width * frame.cols * frame.rows + w * frame.cols + y * (frame.cols * width) + x ;
color c = input.pixels[off * SCALE];
frame.get_pixel(x, y).set_color(c);
}
}
frame = matrix.add_frame();
}
}
matrix.delete_frame();
return matrix;
}
}