Subversion Repositories Kolibri OS

Rev

Rev 7422 | Rev 7977 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7422 leency 1
#define MAX_CELL_SIZE 256
7254 leency 2
 
7257 leency 3
//////////////////////////////////////////////////////////////////////////////////////
4
//                                                                                  //
5
//                                  DRAW PIXEL                                      //
6
//                                                                                  //
7
//////////////////////////////////////////////////////////////////////////////////////
8
 
7259 leency 9
//The 'draw[]' is the array which holds the states should we draw a pixel or not.
10
//Is need to decrease redraw when using some tools like line, rectangle and selection.
7257 leency 11
 
12
struct _pixel_state
13
{
14
	unsigned image_rows, image_columns;
15
	bool draw[MAX_CELL_SIZE*MAX_CELL_SIZE];
16
	void set_drawable_state();
17
	bool is_drawable();
18
	void reset_and_set_all_drawable();
19
	void set_sizes();
20
} pixel_state;
21
 
22
void _pixel_state::set_drawable_state(int _r, _c, _state)
23
{
24
	draw[image_columns*_r + _c] = _state;
25
}
26
 
27
bool _pixel_state::is_drawable(int _r, _c)
28
{
29
	return draw[image_columns*_r + _c];
30
}
31
 
32
void _pixel_state::reset_and_set_all_drawable()
33
{
34
	int i;
35
	for (i = 0; i < image_columns*image_rows; i++) draw[i]=true;
36
}
37
 
38
void _pixel_state::set_sizes(dword _r, _c)
39
{
40
	image_rows = _r;
41
	image_columns = _c;
42
}
43
 
44
//////////////////////////////////////////////////////////////////////////////////////
45
//                                                                                  //
46
//                                      IMAGE                                       //
47
//                                                                                  //
48
//////////////////////////////////////////////////////////////////////////////////////
49
 
50
//This stucture determines basic actions that can be done with image (icon).
51
 
7155 leency 52
struct _image
7089 leency 53
{
54
	unsigned rows, columns;
7254 leency 55
	dword mas[MAX_CELL_SIZE*MAX_CELL_SIZE];
7444 leency 56
	dword mas_copy[MAX_CELL_SIZE*MAX_CELL_SIZE];
7151 leency 57
	dword img;
7257 leency 58
	_pixel_state pixel_state;
7155 leency 59
	void create();
7151 leency 60
	void set_pixel();
7254 leency 61
	void draw_line();
62
	void fill();
7156 leency 63
	void set_image();
7151 leency 64
	dword get_pixel();
7089 leency 65
	dword get_image();
7265 leency 66
	dword get_image_with_replaced_color();
7096 leency 67
	void move();
7150 leency 68
};
7089 leency 69
 
7155 leency 70
void _image::create(int _rows, _columns)
7089 leency 71
{
7155 leency 72
	int i;
73
	rows = _rows;
74
	columns = _columns;
75
	for (i = 0; i < columns*rows; i++) mas[i]=0xBFCAD2;
7257 leency 76
	pixel_state.set_sizes(rows, columns);
7155 leency 77
}
78
 
79
void _image::set_pixel(int _r, _c, _color)
80
{
7151 leency 81
	mas[columns*_r + _c] = _color;
7089 leency 82
}
83
 
7254 leency 84
void _image::draw_line(int x1, int y1, int x2, int y2, dword color) {
85
	int dx, dy, signX, signY, error, error2;
86
 
87
	dx = x2 - x1;
88
 
89
	if (dx < 0)
90
		dx = -dx;
91
 
92
	dy = y2 - y1;
93
 
94
	if (dy < 0)
95
		dy = -dy;
96
 
97
	if (x1 < x2)
98
		signX = 1;
99
	else
100
		signX = -1;
101
 
102
	if (y1 < y2)
103
		signY = 1;
104
	else
105
		signY = -1;
106
 
107
	error = dx - dy;
108
 
109
	set_pixel(y2, x2, color);
110
 
111
	while((x1 != x2) || (y1 != y2))
112
	{
113
		set_pixel(y1, x1, color);
114
 
115
		error2 = error * 2;
116
 
117
		if(error2 > calc(-dy))
118
		{
119
			error -= dy;
120
			x1 += signX;
121
		}
122
 
123
		if(error2 < dx)
124
		{
125
			error += dx;
126
			y1 += signY;
127
		}
128
	}
129
}
130
 
131
void _image::fill(int _r, _c, _color)
132
{
133
	#define MARKED 6
134
	int r, c, i, restart;
135
 
136
	dword old_color = get_pixel(_r, _c);
137
	set_pixel(_r, _c, MARKED);
138
 
139
	do {
140
		restart=false;
141
		for (r = 0; r < rows; r++)
142
			for (c = 0; c < columns; c++)
143
			{
144
				IF (get_pixel(r,c) != old_color) continue;
145
				IF (get_pixel(r,c) == MARKED) continue;
146
 
147
				IF (c>0)               && (get_pixel(r,c-1) == MARKED) set_pixel(r,c,MARKED);
148
				IF (r>0)               && (get_pixel(r-1,c) == MARKED) set_pixel(r,c,MARKED);
149
				IF (c
150
				IF (r
151
 
152
				IF (get_pixel(r,c)==MARKED) restart=true;
153
			}
154
	}while(restart);
155
 
156
	for (i=0; i
157
			IF (mas[i]==MARKED) mas[i] = _color;
158
}
159
 
7155 leency 160
dword _image::get_pixel(int _r, _c)
7089 leency 161
{
7151 leency 162
	return mas[columns*_r + _c];
7089 leency 163
}
164
 
7156 leency 165
void _image::set_image(dword _inbuf)
166
{
167
	dword i;
168
	for (i = 0; i < columns*rows; i++;)
169
	{
7190 leency 170
		// mas[i] = ESDWORD[i*4+_inbuf] & 0x00FFFFFF; //for x32 bit color
171
		mas[i] = ESDWORD[i*3+_inbuf] & 0xFFFFFF;
7156 leency 172
	}
173
}
174
 
7155 leency 175
dword _image::get_image()
7089 leency 176
{
177
	int r=0, c=0;
178
	dword i;
179
 
7151 leency 180
	free(img);
181
	i = img = malloc(rows*columns*3);
7089 leency 182
 
183
	for (r = 0; r < rows; r++)
184
		for (c = 0; c < columns; c++)
185
		{
7151 leency 186
			rgb.DwordToRgb(get_pixel(r,c));
7089 leency 187
			ESBYTE[i] = rgb.b;
188
			ESBYTE[i+1] = rgb.g;
189
			ESBYTE[i+2] = rgb.r;
190
			i += 3;
191
		}
7151 leency 192
	return img;
7089 leency 193
}
194
 
7265 leency 195
dword _image::get_image_with_replaced_color(dword _col_from, _col_to)
196
{
197
	int r=0, c=0;
198
	dword i;
199
	dword cur_pixel;
200
 
201
	free(img);
202
	i = img = malloc(rows*columns*3);
203
 
204
	for (r = 0; r < rows; r++)
205
		for (c = 0; c < columns; c++)
206
		{
207
			cur_pixel = get_pixel(r,c);
208
			if (cur_pixel == _col_from) cur_pixel = _col_to;
209
			rgb.DwordToRgb(cur_pixel);
210
			ESBYTE[i] = rgb.b;
211
			ESBYTE[i+1] = rgb.g;
212
			ESBYTE[i+2] = rgb.r;
213
			i += 3;
214
		}
215
	return img;
216
}
217
 
7096 leency 218
enum {
7151 leency 219
	MOVE_LEFT,
220
	MOVE_RIGHT,
221
	MOVE_UP,
222
	MOVE_DOWN,
223
	FLIP_VER,
224
	FLIP_HOR,
7444 leency 225
	ROTATE_LEFT,
226
	ROTATE_RIGHT
7096 leency 227
};
7155 leency 228
void _image::move(int _direction)
7096 leency 229
{
230
	int r, c;
231
	dword first_element_data;
232
 
7154 leency 233
	switch(_direction)
7096 leency 234
	{
7154 leency 235
		case MOVE_LEFT:
236
				for (r = 0; r < rows; r++)
237
				{
238
					first_element_data = get_pixel(r, 0);
239
					for (c = 0; c < columns-1; c++) set_pixel(r, c, get_pixel(r, c+1));
240
					set_pixel(r, columns-1, first_element_data);
241
				}
242
				break;
243
		case MOVE_RIGHT:
244
				for (r = 0; r < rows; r++)
245
				{
246
					first_element_data = get_pixel(r, columns-1);
247
					for (c = columns-1; c > 0; c--) set_pixel(r, c, get_pixel(r, c-1));
248
					set_pixel(r, 0, first_element_data);
249
				}
250
				break;
251
		case MOVE_UP:
252
				for (c = 0; c < columns; c++)
253
				{
254
					first_element_data = get_pixel(0, c);
255
					for (r = 0; r < rows-1; r++) set_pixel(r, c, get_pixel(r+1, c));
256
					set_pixel(rows-1, c, first_element_data);
257
				}
258
				break;
259
		case MOVE_DOWN:
260
				for (c = 0; c < columns; c++)
261
				{
262
					first_element_data = get_pixel(rows-1, c);
263
					for (r = rows-1; r > 0; r--) set_pixel(r, c, get_pixel(r-1, c));
264
					set_pixel(0, c, first_element_data);
265
				}
266
				break;
267
		case FLIP_HOR:
268
				for (r = 0; r < rows; r++)
269
					for (c = 0; c < columns/2; c++) {
270
						first_element_data = get_pixel(r, c);
271
						set_pixel(r, c, get_pixel(r, columns-c-1));
272
						set_pixel(r, columns-c-1, first_element_data);
273
					}
274
				break;
275
		case FLIP_VER:
276
				for (c = 0; c < columns; c++)
277
					for (r = 0; r < rows/2; r++) {
278
						first_element_data = get_pixel(r, c);
279
						set_pixel(r, c, get_pixel(rows-r-1, c));
280
						set_pixel(rows-r-1, c, first_element_data);
281
					}
7444 leency 282
				break;
283
		case ROTATE_LEFT:
284
				//slow but the code is simple
285
				//need to rewrite in case of big images support
286
				move(ROTATE_RIGHT);
287
				move(ROTATE_RIGHT);
288
				move(ROTATE_RIGHT);
7154 leency 289
				break;
7444 leency 290
		case ROTATE_RIGHT:
291
				if (columns!=rows) {
292
					notify("Sorry, rotate is implemented for square canvaces only!");
293
					break;
294
				}
295
 
296
				for (r=0; r
297
					mas_copy[r] = mas[r];
298
				}
299
 
300
				for (c = 0; c < columns; c++)
301
					for (r = 0; r < rows; r++) {
302
						set_pixel(c, rows-r-1, mas_copy[columns*r + c]);
303
					}
304
 
305
				columns >< rows;
306
				break;
7096 leency 307
	}
308
}
7147 leency 309
 
7444 leency 310
/*
311
1234
312
5678
313
90AB
7147 leency 314
 
7444 leency 315
951
316
 
317
A
318
B
7147 leency 319
 
7444 leency 320
*/
7147 leency 321