Rev 7254 | Rev 7259 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
7254 | leency | 1 | #define MAX_CELL_SIZE 256 |
2 | |||
7257 | leency | 3 | ////////////////////////////////////////////////////////////////////////////////////// |
4 | // // |
||
5 | // DRAW PIXEL // |
||
6 | // // |
||
7 | ////////////////////////////////////////////////////////////////////////////////////// |
||
8 | |||
9 | //The 'draw[]' in the array which holds the states should we draw a pixel or not. |
||
10 | //Is need to decrese redraw when using some tools like line, rectangle and selection. |
||
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]; |
7151 | leency | 56 | dword img; |
7257 | leency | 57 | _pixel_state pixel_state; |
7155 | leency | 58 | void create(); |
7151 | leency | 59 | void set_pixel(); |
7254 | leency | 60 | void draw_line(); |
61 | void fill(); |
||
7156 | leency | 62 | void set_image(); |
7151 | leency | 63 | dword get_pixel(); |
7089 | leency | 64 | dword get_image(); |
7096 | leency | 65 | void move(); |
7150 | leency | 66 | }; |
7089 | leency | 67 | |
7155 | leency | 68 | void _image::create(int _rows, _columns) |
7089 | leency | 69 | { |
7155 | leency | 70 | int i; |
71 | rows = _rows; |
||
72 | columns = _columns; |
||
73 | for (i = 0; i < columns*rows; i++) mas[i]=0xBFCAD2; |
||
7257 | leency | 74 | pixel_state.set_sizes(rows, columns); |
7155 | leency | 75 | } |
76 | |||
77 | void _image::set_pixel(int _r, _c, _color) |
||
78 | { |
||
7151 | leency | 79 | mas[columns*_r + _c] = _color; |
7089 | leency | 80 | } |
81 | |||
7254 | leency | 82 | void _image::draw_line(int x1, int y1, int x2, int y2, dword color) { |
83 | int dx, dy, signX, signY, error, error2; |
||
84 | |||
85 | dx = x2 - x1; |
||
86 | |||
87 | if (dx < 0) |
||
88 | dx = -dx; |
||
89 | |||
90 | dy = y2 - y1; |
||
91 | |||
92 | if (dy < 0) |
||
93 | dy = -dy; |
||
94 | |||
95 | if (x1 < x2) |
||
96 | signX = 1; |
||
97 | else |
||
98 | signX = -1; |
||
99 | |||
100 | if (y1 < y2) |
||
101 | signY = 1; |
||
102 | else |
||
103 | signY = -1; |
||
104 | |||
105 | error = dx - dy; |
||
106 | |||
107 | set_pixel(y2, x2, color); |
||
108 | |||
109 | while((x1 != x2) || (y1 != y2)) |
||
110 | { |
||
111 | set_pixel(y1, x1, color); |
||
112 | |||
113 | error2 = error * 2; |
||
114 | |||
115 | if(error2 > calc(-dy)) |
||
116 | { |
||
117 | error -= dy; |
||
118 | x1 += signX; |
||
119 | } |
||
120 | |||
121 | if(error2 < dx) |
||
122 | { |
||
123 | error += dx; |
||
124 | y1 += signY; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | void _image::fill(int _r, _c, _color) |
||
130 | { |
||
131 | #define MARKED 6 |
||
132 | int r, c, i, restart; |
||
133 | |||
134 | dword old_color = get_pixel(_r, _c); |
||
135 | set_pixel(_r, _c, MARKED); |
||
136 | |||
137 | do { |
||
138 | restart=false; |
||
139 | for (r = 0; r < rows; r++) |
||
140 | for (c = 0; c < columns; c++) |
||
141 | { |
||
142 | IF (get_pixel(r,c) != old_color) continue; |
||
143 | IF (get_pixel(r,c) == MARKED) continue; |
||
144 | |||
145 | IF (c>0) && (get_pixel(r,c-1) == MARKED) set_pixel(r,c,MARKED); |
||
146 | IF (r>0) && (get_pixel(r-1,c) == MARKED) set_pixel(r,c,MARKED); |
||
147 | IF (c |
||
148 | IF (r |
||
149 | |||
150 | IF (get_pixel(r,c)==MARKED) restart=true; |
||
151 | } |
||
152 | }while(restart); |
||
153 | |||
154 | for (i=0; i |
||
155 | IF (mas[i]==MARKED) mas[i] = _color; |
||
156 | } |
||
157 | |||
7155 | leency | 158 | dword _image::get_pixel(int _r, _c) |
7089 | leency | 159 | { |
7151 | leency | 160 | return mas[columns*_r + _c]; |
7089 | leency | 161 | } |
162 | |||
7156 | leency | 163 | void _image::set_image(dword _inbuf) |
164 | { |
||
165 | dword i; |
||
166 | for (i = 0; i < columns*rows; i++;) |
||
167 | { |
||
7190 | leency | 168 | // mas[i] = ESDWORD[i*4+_inbuf] & 0x00FFFFFF; //for x32 bit color |
169 | mas[i] = ESDWORD[i*3+_inbuf] & 0xFFFFFF; |
||
7156 | leency | 170 | } |
171 | } |
||
172 | |||
7155 | leency | 173 | dword _image::get_image() |
7089 | leency | 174 | { |
175 | int r=0, c=0; |
||
176 | dword i; |
||
177 | |||
7151 | leency | 178 | free(img); |
179 | i = img = malloc(rows*columns*3); |
||
7089 | leency | 180 | |
181 | for (r = 0; r < rows; r++) |
||
182 | for (c = 0; c < columns; c++) |
||
183 | { |
||
7151 | leency | 184 | rgb.DwordToRgb(get_pixel(r,c)); |
7089 | leency | 185 | ESBYTE[i] = rgb.b; |
186 | ESBYTE[i+1] = rgb.g; |
||
187 | ESBYTE[i+2] = rgb.r; |
||
188 | i += 3; |
||
189 | } |
||
7151 | leency | 190 | return img; |
7089 | leency | 191 | } |
192 | |||
7096 | leency | 193 | enum { |
7151 | leency | 194 | MOVE_LEFT, |
195 | MOVE_RIGHT, |
||
196 | MOVE_UP, |
||
197 | MOVE_DOWN, |
||
198 | FLIP_VER, |
||
199 | FLIP_HOR, |
||
200 | ROTE |
||
7096 | leency | 201 | }; |
7155 | leency | 202 | void _image::move(int _direction) |
7096 | leency | 203 | { |
204 | int r, c; |
||
205 | dword first_element_data; |
||
206 | |||
7154 | leency | 207 | switch(_direction) |
7096 | leency | 208 | { |
7154 | leency | 209 | case MOVE_LEFT: |
210 | for (r = 0; r < rows; r++) |
||
211 | { |
||
212 | first_element_data = get_pixel(r, 0); |
||
213 | for (c = 0; c < columns-1; c++) set_pixel(r, c, get_pixel(r, c+1)); |
||
214 | set_pixel(r, columns-1, first_element_data); |
||
215 | } |
||
216 | break; |
||
217 | case MOVE_RIGHT: |
||
218 | for (r = 0; r < rows; r++) |
||
219 | { |
||
220 | first_element_data = get_pixel(r, columns-1); |
||
221 | for (c = columns-1; c > 0; c--) set_pixel(r, c, get_pixel(r, c-1)); |
||
222 | set_pixel(r, 0, first_element_data); |
||
223 | } |
||
224 | break; |
||
225 | case MOVE_UP: |
||
226 | for (c = 0; c < columns; c++) |
||
227 | { |
||
228 | first_element_data = get_pixel(0, c); |
||
229 | for (r = 0; r < rows-1; r++) set_pixel(r, c, get_pixel(r+1, c)); |
||
230 | set_pixel(rows-1, c, first_element_data); |
||
231 | } |
||
232 | break; |
||
233 | case MOVE_DOWN: |
||
234 | for (c = 0; c < columns; c++) |
||
235 | { |
||
236 | first_element_data = get_pixel(rows-1, c); |
||
237 | for (r = rows-1; r > 0; r--) set_pixel(r, c, get_pixel(r-1, c)); |
||
238 | set_pixel(0, c, first_element_data); |
||
239 | } |
||
240 | break; |
||
241 | case FLIP_HOR: |
||
242 | for (r = 0; r < rows; r++) |
||
243 | for (c = 0; c < columns/2; c++) { |
||
244 | first_element_data = get_pixel(r, c); |
||
245 | set_pixel(r, c, get_pixel(r, columns-c-1)); |
||
246 | set_pixel(r, columns-c-1, first_element_data); |
||
247 | } |
||
248 | break; |
||
249 | case FLIP_VER: |
||
250 | for (c = 0; c < columns; c++) |
||
251 | for (r = 0; r < rows/2; r++) { |
||
252 | first_element_data = get_pixel(r, c); |
||
253 | set_pixel(r, c, get_pixel(rows-r-1, c)); |
||
254 | set_pixel(rows-r-1, c, first_element_data); |
||
255 | } |
||
256 | break; |
||
7096 | leency | 257 | } |
258 | }>>>>>>>>>>>>>>>>>>>>>> |
||
7147 | leency | 259 |