Subversion Repositories Kolibri OS

Rev

Rev 7265 | Rev 7422 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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