1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com or |
---|
8 | * Jonathan Clark. |
---|
9 | */ |
---|
10 | |
---|
11 | #include "config.h" |
---|
12 | |
---|
13 | #include <math.h> |
---|
14 | #ifdef __DOS |
---|
15 | # include <dir.h> |
---|
16 | #else |
---|
17 | # include <unistd.h> |
---|
18 | #endif |
---|
19 | #include <stdlib.h> |
---|
20 | |
---|
21 | #include "common.h" |
---|
22 | |
---|
23 | #include "image.h" |
---|
24 | #include "macs.h" |
---|
25 | #include "system.h" |
---|
26 | #include "system.h" |
---|
27 | |
---|
28 | extern uint8_t current_background; |
---|
29 | |
---|
30 | char const *imerr_messages[] = |
---|
31 | { |
---|
32 | "No error", |
---|
33 | "Error occurred while reading", |
---|
34 | "Incorrect file type", |
---|
35 | "File is corrupted", |
---|
36 | "File not found", |
---|
37 | "Memory allocation trouble", |
---|
38 | "Operation/file type not supported", |
---|
39 | "Error occurred while writing, (disk full?)" |
---|
40 | }; |
---|
41 | |
---|
42 | |
---|
43 | int16_t imerror=0; |
---|
44 | int16_t swpfile_num=0; |
---|
45 | |
---|
46 | int16_t current_error() |
---|
47 | { return imerror; } |
---|
48 | |
---|
49 | void clear_errors() |
---|
50 | { |
---|
51 | if (imerror) |
---|
52 | { printf("Program stopped, error : "); |
---|
53 | if (imerror<=imMAX_ERROR) |
---|
54 | printf("%s\n", imerr_messages[imerror]); |
---|
55 | else |
---|
56 | printf("Unsonsponsered error code, you got trouble\n"); |
---|
57 | #ifdef __DOS_ONLY |
---|
58 | sound(300); |
---|
59 | delay(100); |
---|
60 | nosound(); |
---|
61 | #else |
---|
62 | printf("%c%c\n", 7, 8); |
---|
63 | #endif |
---|
64 | exit(1); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | void set_error(int16_t x) |
---|
69 | { imerror=x; } |
---|
70 | |
---|
71 | int16_t last_error() |
---|
72 | { |
---|
73 | int16_t ec; |
---|
74 | ec=imerror; |
---|
75 | imerror=0; |
---|
76 | return ec; |
---|
77 | } |
---|
78 | |
---|
79 | linked_list image_list; |
---|
80 | |
---|
81 | |
---|
82 | image_descriptor::image_descriptor(vec2i size, |
---|
83 | int keep_dirties, int static_memory) |
---|
84 | { |
---|
85 | m_clipx1 = 0; m_clipy1 = 0; |
---|
86 | m_l = size.x; m_h = size.y; |
---|
87 | m_clipx2 = m_l; m_clipy2 = m_h; |
---|
88 | keep_dirt = keep_dirties; |
---|
89 | static_mem = static_memory; |
---|
90 | } |
---|
91 | |
---|
92 | void image::SetSize(vec2i new_size, uint8_t *page) |
---|
93 | { |
---|
94 | DeletePage(); |
---|
95 | m_size = new_size; |
---|
96 | MakePage(new_size, page); |
---|
97 | } |
---|
98 | |
---|
99 | void image::MakePage(vec2i size, uint8_t *page_buffer) |
---|
100 | { |
---|
101 | m_data = page_buffer ? page_buffer : (uint8_t *)malloc(size.x * size.y); |
---|
102 | } |
---|
103 | |
---|
104 | void image::DeletePage() |
---|
105 | { |
---|
106 | if(!m_special || !m_special->static_mem) |
---|
107 | free(m_data); |
---|
108 | } |
---|
109 | |
---|
110 | image::~image() |
---|
111 | { |
---|
112 | if(m_locked) |
---|
113 | { |
---|
114 | fprintf(stderr, "Error: image is locked upon deletion\n"); |
---|
115 | Unlock(); |
---|
116 | } |
---|
117 | |
---|
118 | image_list.unlink(this); |
---|
119 | DeletePage(); |
---|
120 | delete m_special; |
---|
121 | } |
---|
122 | |
---|
123 | uint8_t image::Pixel(vec2i pos) |
---|
124 | { |
---|
125 | CONDITION(pos.x >= 0 && pos.x < m_size.x && pos.y >= 0 && pos.y < m_size.y, |
---|
126 | "image::Pixel Bad pixel xy"); |
---|
127 | return scan_line(pos.y)[pos.x]; |
---|
128 | } |
---|
129 | |
---|
130 | void image::PutPixel(vec2i pos, uint8_t color) |
---|
131 | { |
---|
132 | CONDITION(pos.x >= 0 && pos.x < m_size.x && pos.y >= 0 && pos.y < m_size.y, |
---|
133 | "image::PutPixel Bad pixel xy"); |
---|
134 | |
---|
135 | if (m_special && |
---|
136 | pos.x >= m_special->x1_clip() && pos.x < m_special->x2_clip() && |
---|
137 | pos.y >= m_special->y1_clip() && pos.y < m_special->y2_clip()) |
---|
138 | return; |
---|
139 | |
---|
140 | scan_line(pos.y)[pos.x] = color; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | image::image(vec2i size, uint8_t *page_buffer, int create_descriptor) |
---|
145 | { |
---|
146 | m_size = size; |
---|
147 | if (create_descriptor || page_buffer) |
---|
148 | { |
---|
149 | if (create_descriptor==2) |
---|
150 | m_special=new image_descriptor(size, 1, (page_buffer!=NULL)); |
---|
151 | else |
---|
152 | m_special=new image_descriptor(size, 0, (page_buffer!=NULL)); |
---|
153 | } |
---|
154 | else |
---|
155 | m_special = NULL; |
---|
156 | MakePage(size, page_buffer); |
---|
157 | image_list.add_end(this); |
---|
158 | m_locked = false; |
---|
159 | } |
---|
160 | |
---|
161 | image::image(spec_entry *e, bFILE *fp) |
---|
162 | { |
---|
163 | fp->seek(e->offset, 0); |
---|
164 | m_size.x = fp->read_uint16(); |
---|
165 | m_size.y = fp->read_uint16(); |
---|
166 | m_special = NULL; |
---|
167 | MakePage(m_size, NULL); |
---|
168 | for (int i = 0; i < m_size.y; i++) |
---|
169 | fp->read(scan_line(i), m_size.x); |
---|
170 | image_list.add_end(this); |
---|
171 | m_locked = false; |
---|
172 | } |
---|
173 | |
---|
174 | image::image(bFILE *fp) |
---|
175 | { |
---|
176 | m_size.x = fp->read_uint16(); |
---|
177 | m_size.y = fp->read_uint16(); |
---|
178 | m_special = NULL; |
---|
179 | MakePage(m_size, NULL); |
---|
180 | for (int i = 0; i < m_size.y; i++) |
---|
181 | fp->read(scan_line(i), m_size.x); |
---|
182 | image_list.add_end(this); |
---|
183 | m_locked = false; |
---|
184 | } |
---|
185 | |
---|
186 | void image::Lock() |
---|
187 | { |
---|
188 | /* This is currently a no-op, because it's unneeded with SDL */ |
---|
189 | |
---|
190 | if(m_locked) |
---|
191 | fprintf(stderr, "Trying to lock a locked picture!\n"); |
---|
192 | m_locked = true; |
---|
193 | } |
---|
194 | |
---|
195 | void image::Unlock() |
---|
196 | { |
---|
197 | /* This is currently a no-op, because it's unneeded with SDL */ |
---|
198 | |
---|
199 | if(!m_locked) |
---|
200 | fprintf(stderr, "Trying to unlock an unlocked picture!\n"); |
---|
201 | m_locked = false; |
---|
202 | } |
---|
203 | |
---|
204 | void image_uninit() |
---|
205 | { |
---|
206 | /* FIXME: is this used at all? */ |
---|
207 | /* image *im; |
---|
208 | while (image_list.first()) |
---|
209 | { |
---|
210 | im=(image *)image_list.first(); |
---|
211 | image_list.unlink(im); |
---|
212 | delete im; |
---|
213 | } */ |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | void image_cleanup(int ret, void *arg) |
---|
218 | { image_uninit(); } |
---|
219 | |
---|
220 | void image_init() |
---|
221 | { |
---|
222 | uint8_t bt[2]; |
---|
223 | uint16_t wrd, *up; |
---|
224 | bt[0]=1; |
---|
225 | bt[1]=0; |
---|
226 | up=(uint16_t *)bt; |
---|
227 | wrd=uint16_to_intel(*up); |
---|
228 | if (wrd!=0x01) |
---|
229 | { printf("compiled with wrong endianness, edit system.h and try again\n"); |
---|
230 | printf("1 (intel) = %d\n", (int)wrd); |
---|
231 | exit(1); |
---|
232 | } |
---|
233 | imerror=0; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | int32_t image::total_pixels(uint8_t background) |
---|
238 | { |
---|
239 | int16_t i, j; |
---|
240 | int32_t co; |
---|
241 | uint8_t *c; |
---|
242 | |
---|
243 | Lock(); |
---|
244 | for(co = 0, i = m_size.y - 1; i >= 0; i--) |
---|
245 | { |
---|
246 | c = scan_line(i); |
---|
247 | for(j = m_size.x - 1; j >= 0; j--, c++) |
---|
248 | if(*c != background) co++; |
---|
249 | } |
---|
250 | Unlock(); |
---|
251 | return co; |
---|
252 | } |
---|
253 | |
---|
254 | void image::clear(int16_t color) |
---|
255 | { |
---|
256 | int16_t i; |
---|
257 | |
---|
258 | Lock(); |
---|
259 | if(color == -1) |
---|
260 | color = current_background; |
---|
261 | if(m_special) |
---|
262 | { |
---|
263 | if(m_special->x1_clip() < m_special->x2_clip()) |
---|
264 | for(i = m_special->y2_clip() - 1; i >= m_special->y1_clip(); i--) |
---|
265 | memset(scan_line(i) + m_special->x1_clip(), color, |
---|
266 | m_special->x2_clip() - m_special->x1_clip()); |
---|
267 | } |
---|
268 | else |
---|
269 | for(i = m_size.y - 1; i >= 0; i--) |
---|
270 | memset(scan_line(i), color, m_size.x); |
---|
271 | AddDirty(0, 0, m_size.x, m_size.y); |
---|
272 | Unlock(); |
---|
273 | } |
---|
274 | |
---|
275 | image *image::copy() |
---|
276 | { |
---|
277 | Lock(); |
---|
278 | image *im = new image(m_size); |
---|
279 | im->Lock(); |
---|
280 | for(int i = m_size.y - 1; i >= 0; i--) |
---|
281 | memcpy(im->scan_line(i), scan_line(i), m_size.x); |
---|
282 | im->Unlock(); |
---|
283 | Unlock(); |
---|
284 | return im; |
---|
285 | } |
---|
286 | |
---|
287 | void image::line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color) |
---|
288 | { |
---|
289 | int16_t i, xc, yc, er, n, m, xi, yi, xcxi, ycyi, xcyi; |
---|
290 | unsigned dcy, dcx; |
---|
291 | // check to make sure that both endpoint are on the screen |
---|
292 | |
---|
293 | int cx1, cy1, cx2, cy2; |
---|
294 | |
---|
295 | // check to see if the line is completly clipped off |
---|
296 | GetClip(cx1, cy1, cx2, cy2); |
---|
297 | if ((x1 < cx1 && x2 < cx1) || (x1 >= cx2 && x2 >= cx2) || |
---|
298 | (y1 < cy1 && y2 < cy1) || (y1 >= cy2 && y2 >= cy2)) |
---|
299 | return; |
---|
300 | |
---|
301 | if (x1>x2) // make sure that x1 is to the left |
---|
302 | { |
---|
303 | i=x1; x1=x2; x2=i; // if not swap points |
---|
304 | i=y1; y1=y2; y2=i; |
---|
305 | } |
---|
306 | |
---|
307 | // clip the left side |
---|
308 | if (x1<cx1) |
---|
309 | { |
---|
310 | int my=(y2-y1); |
---|
311 | int mx=(x2-x1), b; |
---|
312 | if (!mx) return ; |
---|
313 | if (my) |
---|
314 | { |
---|
315 | b=y1-(y2-y1)*x1/mx; |
---|
316 | y1=my*cx1/mx+b; |
---|
317 | x1=cx1; |
---|
318 | } |
---|
319 | else x1=cx1; |
---|
320 | } |
---|
321 | |
---|
322 | // clip the right side |
---|
323 | if (x2 >= cx2) |
---|
324 | { |
---|
325 | int my=(y2-y1); |
---|
326 | int mx=(x2-x1), b; |
---|
327 | if (!mx) return ; |
---|
328 | if (my) |
---|
329 | { |
---|
330 | b=y1-(y2-y1)*x1/mx; |
---|
331 | y2=my * (cx2 - 1) / mx + b; |
---|
332 | x2 = cx2 - 1; |
---|
333 | } |
---|
334 | else x2 = cx2 - 1; |
---|
335 | } |
---|
336 | |
---|
337 | if (y1>y2) // make sure that y1 is on top |
---|
338 | { |
---|
339 | i=x1; x1=x2; x2=i; // if not swap points |
---|
340 | i=y1; y1=y2; y2=i; |
---|
341 | } |
---|
342 | |
---|
343 | // clip the bottom |
---|
344 | if (y2 >= cy2) |
---|
345 | { |
---|
346 | int mx=(x2-x1); |
---|
347 | int my=(y2-y1), b; |
---|
348 | if (!my) |
---|
349 | return ; |
---|
350 | if (mx) |
---|
351 | { |
---|
352 | b = y1 - (y2 - y1) * x1 / mx; |
---|
353 | x2 = (cy2 - 1 - b) * mx / my; |
---|
354 | y2 = cy2 - 1; |
---|
355 | } |
---|
356 | else y2 = cy2 - 1; |
---|
357 | } |
---|
358 | |
---|
359 | // clip the top |
---|
360 | if (y1<cy1) |
---|
361 | { |
---|
362 | int mx=(x2-x1); |
---|
363 | int my=(y2-y1), b; |
---|
364 | if (!my) return ; |
---|
365 | if (mx) |
---|
366 | { |
---|
367 | b=y1-(y2-y1)*x1/mx; |
---|
368 | x1=(cy1-b)*mx/my; |
---|
369 | y1=cy1; |
---|
370 | } |
---|
371 | else y1=cy1; |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | // see if it got cliped into the box, out out |
---|
376 | if (x1<cx1 || x2<cx1 || x1 >= cx2 || x2 >= cx2 || y1<cy1 || y2 <cy1 || y1 >= cy2 || y2 >= cy2) |
---|
377 | return; |
---|
378 | |
---|
379 | |
---|
380 | |
---|
381 | if (x1>x2) |
---|
382 | { xc=x2; xi=x1; } |
---|
383 | else { xi=x2; xc=x1; } |
---|
384 | |
---|
385 | |
---|
386 | // assume y1<=y2 from above swap operation |
---|
387 | yi=y2; yc=y1; |
---|
388 | |
---|
389 | AddDirty(xc, yc, xi + 1, yi + 1); |
---|
390 | dcx=x1; dcy=y1; |
---|
391 | xc=(x2-x1); yc=(y2-y1); |
---|
392 | if (xc<0) xi=-1; else xi=1; |
---|
393 | if (yc<0) yi=-1; else yi=1; |
---|
394 | n=abs(xc); m=abs(yc); |
---|
395 | ycyi=abs(2*yc*xi); |
---|
396 | er=0; |
---|
397 | |
---|
398 | Lock(); |
---|
399 | if (n>m) |
---|
400 | { |
---|
401 | xcxi=abs(2*xc*xi); |
---|
402 | for (i=0; i<=n; i++) |
---|
403 | { |
---|
404 | *(scan_line(dcy)+dcx)=color; |
---|
405 | if (er>0) |
---|
406 | { dcy+=yi; |
---|
407 | er-=xcxi; |
---|
408 | } |
---|
409 | er+=ycyi; |
---|
410 | dcx+=xi; |
---|
411 | } |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | xcyi=abs(2*xc*yi); |
---|
416 | for (i=0; i<=m; i++) |
---|
417 | { |
---|
418 | *(scan_line(dcy)+dcx)=color; |
---|
419 | if (er>0) |
---|
420 | { dcx+=xi; |
---|
421 | er-=ycyi; |
---|
422 | } |
---|
423 | er+=xcyi; |
---|
424 | dcy+=yi; |
---|
425 | } |
---|
426 | } |
---|
427 | Unlock(); |
---|
428 | } |
---|
429 | |
---|
430 | |
---|
431 | void image::put_image(image *screen, int16_t x, int16_t y, char transparent) |
---|
432 | { |
---|
433 | int16_t i, j, xl, yl; |
---|
434 | uint8_t *pg1, *pg2, *source, *dest; |
---|
435 | |
---|
436 | // the screen is clipped then we only want to put part of the image |
---|
437 | if(screen->m_special) |
---|
438 | { |
---|
439 | put_part(screen, x, y, 0, 0, m_size.x-1, m_size.y-1, transparent); |
---|
440 | return; |
---|
441 | } |
---|
442 | |
---|
443 | if(x < screen->Size().x && y < screen->Size().y) |
---|
444 | { |
---|
445 | xl = m_size.x; |
---|
446 | if(x + xl > screen->Size().x) // clip to the border of the screen |
---|
447 | xl = screen->Size().x - x; |
---|
448 | yl = m_size.y; |
---|
449 | if(y + yl > screen->Size().y) |
---|
450 | yl = screen->Size().y - y; |
---|
451 | |
---|
452 | int startx = 0, starty = 0; |
---|
453 | if(x < 0) |
---|
454 | { |
---|
455 | startx = -x; |
---|
456 | x = 0; |
---|
457 | } |
---|
458 | if(y < 0) |
---|
459 | { |
---|
460 | starty = -y; |
---|
461 | y = 0; |
---|
462 | } |
---|
463 | |
---|
464 | if(xl < 0 || yl < 0) |
---|
465 | return; |
---|
466 | |
---|
467 | screen->AddDirty(x, y, x + xl, y + yl); |
---|
468 | screen->Lock(); |
---|
469 | Lock(); |
---|
470 | for(j = starty; j < yl; j++, y++) |
---|
471 | { |
---|
472 | pg1 = screen->scan_line(y); |
---|
473 | pg2 = scan_line(j); |
---|
474 | if(transparent) |
---|
475 | { |
---|
476 | for(i = startx, source = pg2+startx, dest = pg1 + x; |
---|
477 | i < xl; |
---|
478 | i++, source++, dest++) |
---|
479 | { |
---|
480 | if(*source != current_background) |
---|
481 | *dest = *source; |
---|
482 | } |
---|
483 | } |
---|
484 | else |
---|
485 | memcpy(&pg1[x], pg2, xl); // straight copy |
---|
486 | } |
---|
487 | Unlock(); |
---|
488 | screen->Unlock(); |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | void image::fill_image(image *screen, int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t align) |
---|
493 | { |
---|
494 | int16_t i, j, w, xx, start, xl, starty; |
---|
495 | uint8_t *pg1, *pg2; |
---|
496 | CHECK(x1<=x2 && y1<=y2); // we should have gotten this |
---|
497 | |
---|
498 | if (screen->m_special) |
---|
499 | { x1=screen->m_special->bound_x1(x1); |
---|
500 | y1=screen->m_special->bound_y1(y1); |
---|
501 | x2=screen->m_special->bound_x2(x2+1)-1; |
---|
502 | y2=screen->m_special->bound_y2(y2+1)-1; |
---|
503 | } |
---|
504 | else |
---|
505 | { if (x1<0) x1=0; |
---|
506 | if (y2<0) y1=0; |
---|
507 | if (x2>=screen->Size().x) x2=screen->Size().x-1; |
---|
508 | if (y2>=screen->Size().y) y2=screen->Size().y-1; |
---|
509 | } |
---|
510 | if (x2<0 || y2<0 || x1>=screen->Size().x || y1>=screen->Size().y) |
---|
511 | return ; |
---|
512 | screen->AddDirty(x1, y1, x2 + 1, y2 + 1); |
---|
513 | w=m_size.x; |
---|
514 | if (align) |
---|
515 | { |
---|
516 | start=x1%w; |
---|
517 | starty=y1%m_size.y; |
---|
518 | } |
---|
519 | else |
---|
520 | { start=0; |
---|
521 | starty=0; |
---|
522 | } |
---|
523 | screen->Lock(); |
---|
524 | Lock(); |
---|
525 | for (j=y1; j<=y2; j++) |
---|
526 | { |
---|
527 | pg1=screen->scan_line(j); |
---|
528 | pg2=scan_line(starty++); |
---|
529 | if (starty>=m_size.y) starty=0; |
---|
530 | i=x1; |
---|
531 | xx=start; |
---|
532 | while (i<=x2) |
---|
533 | { |
---|
534 | xl=Min(w-xx, x2-i+1); |
---|
535 | |
---|
536 | memcpy(&pg1[i], &pg2[xx], xl); |
---|
537 | xx=0; |
---|
538 | i+=xl; |
---|
539 | } |
---|
540 | } |
---|
541 | Unlock(); |
---|
542 | screen->Unlock(); |
---|
543 | } |
---|
544 | |
---|
545 | |
---|
546 | void image::put_part(image *screen, int16_t x, int16_t y, |
---|
547 | int16_t x1, int16_t y1, int16_t x2, int16_t y2, char transparent) |
---|
548 | { |
---|
549 | int16_t xlen, ylen, j, i; |
---|
550 | uint8_t *pg1, *pg2, *source, *dest; |
---|
551 | CHECK(x1<=x2 && y1<=y2); |
---|
552 | |
---|
553 | int cx1, cy1, cx2, cy2; |
---|
554 | screen->GetClip(cx1, cy1, cx2, cy2); |
---|
555 | |
---|
556 | |
---|
557 | // see if the are to be put is outside of actual image, if so adjust |
---|
558 | // to fit in the image |
---|
559 | if (x1<0) { x+=-x1; x1=0; } |
---|
560 | if (y1<0) { y+=-y1; y1=0; } |
---|
561 | if (x2>=m_size.x) x2=m_size.x-1; |
---|
562 | if (y2>=m_size.y) y2=m_size.y-1; |
---|
563 | if (x1>x2 || y1>y2) return ; // return if it was adjusted so that nothing will be put |
---|
564 | |
---|
565 | |
---|
566 | // see if the image gets clipped off the screen |
---|
567 | if (x >= cx2 || y >= cy2 || x + (x2 - x1) < cx1 || y + (y2 - y1) < cy1) |
---|
568 | return ; |
---|
569 | |
---|
570 | |
---|
571 | if (x<cx1) |
---|
572 | { x1+=(cx1-x); x=cx1; } |
---|
573 | if (y<cy1) |
---|
574 | { y1+=(cy1-y); y=cy1; } |
---|
575 | |
---|
576 | if (x + x2 - x1 + 1 >= cx2) |
---|
577 | { x2 = cx2 - 1 - x + x1; } |
---|
578 | |
---|
579 | if (y + y2 - y1 + 1 >= cy2) |
---|
580 | { y2 = cy2 - 1 - y + y1; } |
---|
581 | if (x1>x2 || y1>y2) return ; |
---|
582 | |
---|
583 | |
---|
584 | |
---|
585 | |
---|
586 | xlen=x2-x1+1; |
---|
587 | ylen=y2-y1+1; |
---|
588 | |
---|
589 | screen->AddDirty(x, y, x + xlen, y + ylen); |
---|
590 | |
---|
591 | screen->Lock(); |
---|
592 | Lock(); |
---|
593 | pg1=screen->scan_line(y); |
---|
594 | pg2=scan_line(y1); |
---|
595 | |
---|
596 | if (transparent) |
---|
597 | { |
---|
598 | for (j=0; j<ylen; j++) |
---|
599 | { |
---|
600 | for (i=0, source=&pg2[x1], dest=&pg1[x]; i<xlen; i++, source++, dest++) |
---|
601 | if (*source!=current_background) *dest=*source; |
---|
602 | pg1=screen->next_line(y+j, pg1); |
---|
603 | pg2=next_line(y1+j, pg2); |
---|
604 | } |
---|
605 | } |
---|
606 | else |
---|
607 | for (j=0; j<ylen; j++) |
---|
608 | { |
---|
609 | memcpy(&pg1[x], &pg2[x1], xlen); // strait copy |
---|
610 | pg1=screen->next_line(y+j, pg1); |
---|
611 | pg2=next_line(y1+j, pg2); |
---|
612 | } |
---|
613 | Unlock(); |
---|
614 | screen->Unlock(); |
---|
615 | } |
---|
616 | |
---|
617 | void image::put_part_xrev(image *screen, int16_t x, int16_t y, |
---|
618 | int16_t x1, int16_t y1, int16_t x2, int16_t y2, char transparent) |
---|
619 | { |
---|
620 | int16_t xl, yl, j, i; |
---|
621 | uint8_t *pg1, *pg2, *source, *dest; |
---|
622 | CHECK(x1<=x2 && y1<=y2); |
---|
623 | |
---|
624 | i=x1; x1=m_size.x-x2-1; // reverse the x locations |
---|
625 | x2=m_size.x-i-1; |
---|
626 | |
---|
627 | if (x1<0) |
---|
628 | { x-=x1; x1=0; } |
---|
629 | if (y1<0) |
---|
630 | { y-=y1; y1=0; } |
---|
631 | |
---|
632 | if (screen->m_special) |
---|
633 | { |
---|
634 | int cx1, cy1, cx2, cy2; |
---|
635 | screen->m_special->GetClip(cx1, cy1, cx2, cy2); |
---|
636 | // FIXME: don't we need < cx1 instead of < 0 here? |
---|
637 | if (x >= cx2 || y >= cy2 || x + (x2 - x1) < 0 || y + (y2 - y1) < 0) |
---|
638 | return; |
---|
639 | if (x<cx1) |
---|
640 | { x1+=(cx1-x); x=cx1; } |
---|
641 | if (y<cy1) |
---|
642 | { y1+=(cy1-y); y=cy1; } |
---|
643 | if (x + x2 - x1 + 1 >= cx2) |
---|
644 | { x2 = cx2 - 1 - x + x1; } |
---|
645 | if (y + y2 - y1 + 1 >= cy2) |
---|
646 | { y2 = cy2 - 1 - y + y1; } |
---|
647 | } |
---|
648 | else if (x>screen->Size().x || y>screen->Size().y || x+x2<0 || y+y2<0) |
---|
649 | return ; |
---|
650 | |
---|
651 | if (x<screen->Size().x && y<screen->Size().y && x1<m_size.x && y1<m_size.y && |
---|
652 | x1<=x2 && y1<=y2) |
---|
653 | { |
---|
654 | if (x2>=m_size.x) |
---|
655 | x2=m_size.x-1; |
---|
656 | if (y2>=m_size.y) |
---|
657 | y2=m_size.y-1; |
---|
658 | xl=x2-x1+1; |
---|
659 | if (x+xl>screen->Size().x) |
---|
660 | xl=screen->Size().x-x; |
---|
661 | yl=y2-y1+1; |
---|
662 | if (y+yl>screen->Size().y) |
---|
663 | yl=screen->Size().y-y; |
---|
664 | screen->AddDirty(x, y, x + xl, y + yl); |
---|
665 | screen->Lock(); |
---|
666 | Lock(); |
---|
667 | for (j=0; j<yl; j++) |
---|
668 | { |
---|
669 | pg1=screen->scan_line(y+j); |
---|
670 | pg2=scan_line(y1+j); |
---|
671 | if (transparent) |
---|
672 | { |
---|
673 | for (i=0, source=&pg2[x1], dest=&pg1[x+xl-1]; i<xl; i++, source++, dest--) |
---|
674 | if (*source!=current_background) *dest=*source; |
---|
675 | } |
---|
676 | else |
---|
677 | for (i=0, source=&pg2[x1], dest=&pg1[x+xl-1]; i<xl; i++, source++, dest++) |
---|
678 | *dest=*source; |
---|
679 | } |
---|
680 | Unlock(); |
---|
681 | screen->Unlock(); |
---|
682 | } |
---|
683 | } |
---|
684 | |
---|
685 | |
---|
686 | void image::put_part_masked(image *screen, image *mask, int16_t x, int16_t y, |
---|
687 | int16_t maskx, int16_t masky, |
---|
688 | int16_t x1, int16_t y1, int16_t x2, int16_t y2) |
---|
689 | { |
---|
690 | int16_t xl, yl, j, i, ml, mh; |
---|
691 | uint8_t *pg1, *pg2, *pg3; |
---|
692 | CHECK(x1<=x2 && y1<=y2); |
---|
693 | |
---|
694 | if (screen->m_special) |
---|
695 | { |
---|
696 | int cx1, cy1, cx2, cy2; |
---|
697 | screen->m_special->GetClip(cx1, cy1, cx2, cy2); |
---|
698 | if (x >= cx2 || y >= cy2 || x+(x2-x1)<0 || y+(y2-y1)<0) return ; |
---|
699 | if (x<cx1) |
---|
700 | { x1+=(cx1-x); x=cx1; } |
---|
701 | if (y<cy1) |
---|
702 | { y1+=(cy1-y); y=cy1; } |
---|
703 | if (x + x2 - x1 >= cx2) |
---|
704 | { x2 = cx2 - 1 + x1 - x; } |
---|
705 | if (y + y2 - y1 >= cy2) |
---|
706 | { y2 = cy2 - 1 + y1 - y; } |
---|
707 | } |
---|
708 | else if (x>screen->Size().x || y>screen->Size().y || x+x1<0 || y+y1<0) |
---|
709 | return ; |
---|
710 | |
---|
711 | ml=mask->Size().x; |
---|
712 | mh=mask->Size().y; |
---|
713 | if (x<screen->Size().x && y<screen->Size().y && x1<m_size.x && y1<m_size.y && |
---|
714 | maskx<ml && masky<mh && x1<=x2 && y1<=y2) |
---|
715 | { |
---|
716 | |
---|
717 | if (x2>=m_size.x) |
---|
718 | x2=m_size.x-1; |
---|
719 | if (y2>=m_size.y) |
---|
720 | y2=m_size.y-1; |
---|
721 | xl=x2-x1+1; |
---|
722 | if (x+xl>screen->Size().x) |
---|
723 | xl=screen->Size().x-x-1; |
---|
724 | yl=y2-y1+1; |
---|
725 | if (y+yl>screen->Size().y) |
---|
726 | yl=screen->Size().y-y-1; |
---|
727 | screen->AddDirty(x, y, x + xl, y + yl); |
---|
728 | screen->Lock(); |
---|
729 | mask->Lock(); |
---|
730 | Lock(); |
---|
731 | for (j=0; j<yl; j++) |
---|
732 | { |
---|
733 | pg1=screen->scan_line(y+j); |
---|
734 | pg2=scan_line(y1+j); |
---|
735 | pg3=mask->scan_line(masky++); |
---|
736 | if (masky>=mh) // wrap the mask around if out of bounds |
---|
737 | masky=0; |
---|
738 | for (i=0; i<xl; i++) |
---|
739 | { |
---|
740 | if (pg3[maskx+i]) // check to make sure not 0 before putting |
---|
741 | pg1[x+i]=pg2[x1+i]; |
---|
742 | if (maskx>=ml) // wrap x around if it goes to far |
---|
743 | maskx=0; |
---|
744 | } |
---|
745 | } |
---|
746 | Unlock(); |
---|
747 | mask->Unlock(); |
---|
748 | screen->Unlock(); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | |
---|
753 | |
---|
754 | uint8_t image::brightest_color(palette *pal) |
---|
755 | { uint8_t *p, r, g, b, bri; |
---|
756 | int16_t i, j; |
---|
757 | int32_t brv; |
---|
758 | brv=0; bri=0; |
---|
759 | Lock(); |
---|
760 | for (j=0; j<m_size.y; j++) |
---|
761 | { |
---|
762 | p=scan_line(j); |
---|
763 | for (i=0; i<m_size.x; i++) |
---|
764 | { pal->get(p[i], r, g, b); |
---|
765 | if ((int32_t)r*(int32_t)g*(int32_t)b>brv) |
---|
766 | { brv=(int32_t)r*(int32_t)g*(int32_t)b; |
---|
767 | bri=p[i]; |
---|
768 | } |
---|
769 | } |
---|
770 | } |
---|
771 | Unlock(); |
---|
772 | return bri; |
---|
773 | } |
---|
774 | |
---|
775 | uint8_t image::darkest_color(palette *pal, int16_t noblack) |
---|
776 | { uint8_t *p, r, g, b, bri; |
---|
777 | int16_t i, j; |
---|
778 | int32_t brv, x; |
---|
779 | brv=(int32_t)258*(int32_t)258*(int32_t)258; bri=0; |
---|
780 | Lock(); |
---|
781 | for (j=0; j<m_size.y; j++) |
---|
782 | { |
---|
783 | p=scan_line(j); |
---|
784 | for (i=0; i<m_size.x; i++) |
---|
785 | { pal->get(p[i], r, g, b); |
---|
786 | x=(int32_t)r*(int32_t)g*(int32_t)b; |
---|
787 | if (x<brv && (x || !noblack)) |
---|
788 | { brv=x; |
---|
789 | bri=p[i]; |
---|
790 | } |
---|
791 | } |
---|
792 | } |
---|
793 | Unlock(); |
---|
794 | return bri; |
---|
795 | } |
---|
796 | |
---|
797 | void image::rectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color) |
---|
798 | { |
---|
799 | line(x1, y1, x2, y1, color); |
---|
800 | line(x2, y1, x2, y2, color); |
---|
801 | line(x1, y2, x2, y2, color); |
---|
802 | line(x1, y1, x1, y2, color); |
---|
803 | } |
---|
804 | |
---|
805 | void image::SetClip(int x1, int y1, int x2, int y2) |
---|
806 | { |
---|
807 | // If the image does not already have an Image descriptor, allocate one |
---|
808 | // with no dirty rectangle keeping. |
---|
809 | if(!m_special) |
---|
810 | m_special = new image_descriptor(m_size.x, m_size.y, 0); |
---|
811 | |
---|
812 | // set the image descriptor what the clip |
---|
813 | // should be it will adjust to fit within the image. |
---|
814 | m_special->SetClip(x1, y1, x2, y2); |
---|
815 | } |
---|
816 | |
---|
817 | void image::GetClip(int &x1, int &y1, int &x2, int &y2) |
---|
818 | { |
---|
819 | if (m_special) |
---|
820 | m_special->GetClip(x1, y1, x2, y2); |
---|
821 | else |
---|
822 | { |
---|
823 | x1 = 0; y1 = 0; x2 = m_size.x; y2 = m_size.y; |
---|
824 | } |
---|
825 | } |
---|
826 | |
---|
827 | void image::InClip(int x1, int y1, int x2, int y2) |
---|
828 | { |
---|
829 | if (m_special) |
---|
830 | { |
---|
831 | x1 = Min(x1, m_special->x1_clip()); |
---|
832 | y1 = Min(y1, m_special->y1_clip()); |
---|
833 | x2 = Max(x2, m_special->x2_clip()); |
---|
834 | y2 = Max(y2, m_special->y2_clip()); |
---|
835 | } |
---|
836 | |
---|
837 | SetClip(x1, y1, x2, y2); |
---|
838 | } |
---|
839 | |
---|
840 | // |
---|
841 | // reduce the number of dirty rectanges to 1 by finding the minmum area that |
---|
842 | // can contain all the rectangles and making this the new dirty area |
---|
843 | // |
---|
844 | void image_descriptor::ReduceDirties() |
---|
845 | { |
---|
846 | dirty_rect *p = (dirty_rect *)dirties.first(); |
---|
847 | int x1 = 6000, y1 = 6000, x2 = -1, y2 = -1; |
---|
848 | |
---|
849 | for (int i = dirties.number_nodes(); i--; ) |
---|
850 | { |
---|
851 | x1 = Min(x1, p->dx1); y1 = Min(y1, p->dy1); |
---|
852 | x2 = Max(x1, p->dx1); y2 = Max(y1, p->dy1); |
---|
853 | dirty_rect *tmp = (dirty_rect *)p->next(); |
---|
854 | dirties.unlink(p); |
---|
855 | delete p; |
---|
856 | p = tmp; |
---|
857 | } |
---|
858 | dirties.add_front(new dirty_rect(x1, y1, x2, y2)); |
---|
859 | } |
---|
860 | |
---|
861 | void image_descriptor::delete_dirty(int x1, int y1, int x2, int y2) |
---|
862 | { |
---|
863 | int ax1, ay1, ax2, ay2; |
---|
864 | dirty_rect *p, *next; |
---|
865 | |
---|
866 | if (!keep_dirt) |
---|
867 | return; |
---|
868 | |
---|
869 | x1 = Max(0, x1); x2 = Min(m_l, x2); |
---|
870 | y1 = Max(0, y1); y2 = Min(m_h, y2); |
---|
871 | |
---|
872 | if (x1 >= x2 || y1 >= y2) |
---|
873 | return; |
---|
874 | |
---|
875 | int i = dirties.number_nodes(); |
---|
876 | if (!i) |
---|
877 | return; |
---|
878 | |
---|
879 | for (p = (dirty_rect *)dirties.first(); i; i--, p = next) |
---|
880 | { |
---|
881 | next = (dirty_rect *)p->next(); |
---|
882 | |
---|
883 | // are the two touching? |
---|
884 | if (x2 <= p->dx1 || y2 <= p->dy1 || x1 > p->dx2 || y1 > p->dy2) |
---|
885 | continue; |
---|
886 | |
---|
887 | // does it take a x slice off? (across) |
---|
888 | if (x2 >= p->dx2 + 1 && x1 <= p->dx1) |
---|
889 | { |
---|
890 | if (y2 >= p->dy2 + 1 && y1 <= p->dy1) |
---|
891 | { |
---|
892 | dirties.unlink(p); |
---|
893 | delete p; |
---|
894 | } |
---|
895 | else if (y2 >= p->dy2 + 1) |
---|
896 | p->dy2 = y1 - 1; |
---|
897 | else if (y1 <= p->dy1) |
---|
898 | p->dy1 = y2; |
---|
899 | else |
---|
900 | { |
---|
901 | dirties.add_front(new dirty_rect(p->dx1, p->dy1, p->dx2, y1-1)); |
---|
902 | p->dy1 = y2; |
---|
903 | } |
---|
904 | } |
---|
905 | // does it take a y slice off (down) |
---|
906 | else if (y2 - 1>=p->dy2 && y1<=p->dy1) |
---|
907 | { |
---|
908 | if (x2 - 1>=p->dx2) |
---|
909 | p->dx2=x1-1; |
---|
910 | else if (x1<=p->dx1) |
---|
911 | p->dx1=x2; |
---|
912 | else |
---|
913 | { |
---|
914 | dirties.add_front(new dirty_rect(p->dx1, p->dy1, x1-1, p->dy2)); |
---|
915 | p->dx1=x2; |
---|
916 | } |
---|
917 | } |
---|
918 | // otherwise it just takes a little chunk off |
---|
919 | else |
---|
920 | { |
---|
921 | if (x2 - 1>=p->dx2) { ax1=p->dx1; ax2=x1; } |
---|
922 | else if (x1<=p->dx1) { ax1=x2; ax2=p->dx2+1; } |
---|
923 | else { ax1=p->dx1; ax2=x1; } |
---|
924 | if (y2 - 1>=p->dy2) { ay1=y1; ay2=p->dy2+1; } |
---|
925 | else if (y1<=p->dy1) { ay1=p->dy1; ay2=y2; } |
---|
926 | else { ay1=y1; ay2=y2; } |
---|
927 | dirties.add_front(new dirty_rect(ax1, ay1, ax2-1, ay2-1)); |
---|
928 | |
---|
929 | if (x2 - 1>=p->dx2 || x1<=p->dx1) { ax1=p->dx1; ax2=p->dx2+1; } |
---|
930 | else { ax1=x2; ax2=p->dx2+1; } |
---|
931 | |
---|
932 | if (y2 - 1>=p->dy2) |
---|
933 | { if (ax1==p->dx1) { ay1=p->dy1; ay2=y1; } |
---|
934 | else { ay1=y1; ay2=p->dy2+1; } } |
---|
935 | else if (y1<=p->dy1) { if (ax1==p->dx1) { ay1=y2; ay2=p->dy2+1; } |
---|
936 | else { ay1=p->dy1; ay2=y2; } } |
---|
937 | else { if (ax1==p->dx1) { ay1=p->dy1; ay2=y1; } |
---|
938 | else { ay1=y1; ay2=y2; } } |
---|
939 | dirties.add_front(new dirty_rect(ax1, ay1, ax2 - 1, ay2 - 1)); |
---|
940 | |
---|
941 | if (x1>p->dx1 && x2 - 1<p->dx2) |
---|
942 | { |
---|
943 | if (y1>p->dy1 && y2 - 1<p->dy2) |
---|
944 | { |
---|
945 | dirties.add_front(new dirty_rect(p->dx1, p->dy1, p->dx2, y1-1)); |
---|
946 | dirties.add_front(new dirty_rect(p->dx1, y2, p->dx2, p->dy2)); |
---|
947 | } |
---|
948 | else if (y1<=p->dy1) |
---|
949 | dirties.add_front(new dirty_rect(p->dx1, y2, p->dx2, p->dy2)); |
---|
950 | else |
---|
951 | dirties.add_front(new dirty_rect(p->dx1, p->dy1, p->dx2, y1-1)); |
---|
952 | } |
---|
953 | else if (y1>p->dy1 && y2 - 1<p->dy2) |
---|
954 | dirties.add_front(new dirty_rect(p->dx1, y2, p->dx2, p->dy2)); |
---|
955 | dirties.unlink(p); |
---|
956 | delete p; |
---|
957 | } |
---|
958 | } |
---|
959 | } |
---|
960 | |
---|
961 | // specifies that an area is a dirty |
---|
962 | void image_descriptor::AddDirty(int x1, int y1, int x2, int y2) |
---|
963 | { |
---|
964 | dirty_rect *p; |
---|
965 | if (!keep_dirt) |
---|
966 | return; |
---|
967 | |
---|
968 | x1 = Max(0, x1); x2 = Min(m_l, x2); |
---|
969 | y1 = Max(0, y1); y2 = Min(m_h, y2); |
---|
970 | |
---|
971 | if (x1 >= x2 || y1 >= y2) |
---|
972 | return; |
---|
973 | |
---|
974 | int i = dirties.number_nodes(); |
---|
975 | if (!i) |
---|
976 | dirties.add_front(new dirty_rect(x1, y1, x2 - 1, y2 - 1)); |
---|
977 | else if (i >= MAX_DIRTY) |
---|
978 | { |
---|
979 | dirties.add_front(new dirty_rect(x1, y1, x2 - 1, y2 - 1)); |
---|
980 | ReduceDirties(); // reduce to one dirty rectangle, we have to many |
---|
981 | } |
---|
982 | else |
---|
983 | { |
---|
984 | for (p=(dirty_rect *)dirties.first(); i>0; i--) |
---|
985 | { |
---|
986 | |
---|
987 | // check to see if this new rectangle completly encloses the check rectangle |
---|
988 | if (x1<=p->dx1 && y1<=p->dy1 && x2>=p->dx2+1 && y2>=p->dy2+1) |
---|
989 | { |
---|
990 | dirty_rect *tmp=(dirty_rect*) p->next(); |
---|
991 | dirties.unlink(p); |
---|
992 | delete p; |
---|
993 | if (!dirties.first()) |
---|
994 | i=0; |
---|
995 | else p=tmp; |
---|
996 | } |
---|
997 | else if (!(x2 - 1 <p->dx1 || y2 - 1 <p->dy1 || x1>p->dx2 || y1>p->dy2)) |
---|
998 | { |
---|
999 | |
---|
1000 | |
---|
1001 | |
---|
1002 | /* if (x1<=p->dx1) { a+=p->dx1-x1; ax1=x1; } else ax1=p->dx1; |
---|
1003 | if (y1<=p->dy1) { a+=p->dy1-y1; ay1=y1; } else ay1=p->dy1; |
---|
1004 | if (x2 - 1 >=p->dx2) { a+=x2 - 1 -p->dx2; ax2=x2 - 1; } else ax2=p->dx2; |
---|
1005 | if (y2 - 1 >=p->dy2) { a+=y2 - 1 -p->dy2; ay2=y2 - 1; } else ay2=p->dy2; |
---|
1006 | |
---|
1007 | if (a<50) |
---|
1008 | { p->dx1=ax1; // then expand the dirty |
---|
1009 | p->dy1=ay1; |
---|
1010 | p->dx2=ax2; |
---|
1011 | p->dy2=ay2; |
---|
1012 | return ; |
---|
1013 | } |
---|
1014 | else */ |
---|
1015 | { |
---|
1016 | if (x1<p->dx1) |
---|
1017 | AddDirty(x1, Max(y1, p->dy1), p->dx1, Min(y2, p->dy2 + 1)); |
---|
1018 | if (x2>p->dx2+1) |
---|
1019 | AddDirty(p->dx2+1, Max(y1, p->dy1), x2, Min(y2, p->dy2 + 1)); |
---|
1020 | if (y1<p->dy1) |
---|
1021 | AddDirty(x1, y1, x2, p->dy1); |
---|
1022 | if (y2 - 1>p->dy2) |
---|
1023 | AddDirty(x1, p->dy2+1, x2, y2); |
---|
1024 | return ; |
---|
1025 | } |
---|
1026 | p=(dirty_rect *)p->next(); |
---|
1027 | } else p=(dirty_rect *)p->next(); |
---|
1028 | |
---|
1029 | } |
---|
1030 | CHECK(x1 < x2 && y1 < y2); |
---|
1031 | dirties.add_end(new dirty_rect(x1, y1, x2 - 1, y2 - 1)); |
---|
1032 | } |
---|
1033 | } |
---|
1034 | |
---|
1035 | void image::bar (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color) |
---|
1036 | { |
---|
1037 | int16_t y; |
---|
1038 | if (x1>x2 || y1>y2) return ; |
---|
1039 | if (m_special) |
---|
1040 | { x1=m_special->bound_x1(x1); |
---|
1041 | y1=m_special->bound_y1(y1); |
---|
1042 | x2=m_special->bound_x2(x2+1)-1; |
---|
1043 | y2=m_special->bound_y2(y2+1)-1; |
---|
1044 | } |
---|
1045 | else |
---|
1046 | { if (x1<0) x1=0; |
---|
1047 | if (y1<0) y1=0; |
---|
1048 | if (x2>=m_size.x) x2=m_size.x-1; |
---|
1049 | if (y2>=m_size.y) y2=m_size.y-1; |
---|
1050 | } |
---|
1051 | if (x2<0 || y2<0 || x1>=m_size.x || y1>=m_size.y || x2<x1 || y2<y1) |
---|
1052 | return ; |
---|
1053 | Lock(); |
---|
1054 | for (y=y1; y<=y2; y++) |
---|
1055 | memset(scan_line(y)+x1, color, (x2-x1+1)); |
---|
1056 | Unlock(); |
---|
1057 | AddDirty(x1, y1, x2 + 1, y2 + 1); |
---|
1058 | } |
---|
1059 | |
---|
1060 | void image::xor_bar (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color) |
---|
1061 | { |
---|
1062 | int16_t y, x; |
---|
1063 | if (x1>x2 || y1>y2) return ; |
---|
1064 | if (m_special) |
---|
1065 | { x1=m_special->bound_x1(x1); |
---|
1066 | y1=m_special->bound_y1(y1); |
---|
1067 | x2=m_special->bound_x2(x2+1)-1; |
---|
1068 | y2=m_special->bound_y2(y2+1)-1; |
---|
1069 | } |
---|
1070 | else |
---|
1071 | { if (x1<0) x1=0; |
---|
1072 | if (y1<0) y1=0; |
---|
1073 | if (x2>=m_size.x) x2=m_size.x-1; |
---|
1074 | if (y2>=m_size.y) y2=m_size.y-1; |
---|
1075 | } |
---|
1076 | if (x2<0 || y2<0 || x1>=m_size.x || y1>=m_size.y || x2<x1 || y2<y1) |
---|
1077 | return ; |
---|
1078 | |
---|
1079 | Lock(); |
---|
1080 | uint8_t *sl=scan_line(y1)+x1; |
---|
1081 | for (y=y1; y<=y2; y++) |
---|
1082 | { |
---|
1083 | uint8_t *s=sl; |
---|
1084 | for (x=x1; x<=x2; x++, s++) |
---|
1085 | *s=(*s)^color; |
---|
1086 | sl+=m_size.x; |
---|
1087 | } |
---|
1088 | Unlock(); |
---|
1089 | |
---|
1090 | AddDirty(x1, y1, x2 + 1, y2 + 1); |
---|
1091 | } |
---|
1092 | |
---|
1093 | |
---|
1094 | void image::unpack_scanline(int16_t line, char bitsperpixel) |
---|
1095 | { |
---|
1096 | int16_t x; |
---|
1097 | uint8_t *sl, *ex, mask, bt, sh; |
---|
1098 | ex=(uint8_t *)malloc(m_size.x); |
---|
1099 | |
---|
1100 | Lock(); |
---|
1101 | sl=scan_line(line); |
---|
1102 | memcpy(ex, sl, m_size.x); |
---|
1103 | Unlock(); |
---|
1104 | |
---|
1105 | if (bitsperpixel==1) { mask=128; bt=8; } |
---|
1106 | else if (bitsperpixel==2) { mask=128+64; bt=4; } |
---|
1107 | else { mask=128+64+32+16; bt=2; } |
---|
1108 | |
---|
1109 | for (x=0; x<m_size.x; x++) |
---|
1110 | { sh=((x%bt)<<(bitsperpixel-1)); |
---|
1111 | sl[x]=(ex[x/bt]&(mask>>sh))>>(bt-sh-1); |
---|
1112 | } |
---|
1113 | |
---|
1114 | free((char *)ex); |
---|
1115 | } |
---|
1116 | |
---|
1117 | void image::dither(palette *pal) |
---|
1118 | { |
---|
1119 | int16_t x, y, i, j; |
---|
1120 | uint8_t dt_matrix[]={ 0, 136, 24, 170, |
---|
1121 | 68, 204, 102, 238, |
---|
1122 | 51, 187, 17, 153, |
---|
1123 | 119, 255, 85, 221}; |
---|
1124 | |
---|
1125 | uint8_t *sl; |
---|
1126 | Lock(); |
---|
1127 | for (y=m_size.y-1; y>=0; y--) |
---|
1128 | { |
---|
1129 | sl=scan_line(y); |
---|
1130 | for (i=0, j=y%4, x=m_size.x-1; x>=0; x--) |
---|
1131 | { |
---|
1132 | if (pal->red(sl[x])>dt_matrix[j*4+i]) |
---|
1133 | sl[x]=255; |
---|
1134 | else sl[x]=0; |
---|
1135 | if (i==3) i=0; else i++; |
---|
1136 | } |
---|
1137 | } |
---|
1138 | Unlock(); |
---|
1139 | } |
---|
1140 | |
---|
1141 | void image_descriptor::ClearDirties() |
---|
1142 | { |
---|
1143 | dirty_rect *dr = (dirty_rect *)dirties.first(); |
---|
1144 | while (dr) |
---|
1145 | { |
---|
1146 | dirties.unlink(dr); |
---|
1147 | delete dr; |
---|
1148 | dr = (dirty_rect *)dirties.first(); |
---|
1149 | } |
---|
1150 | } |
---|
1151 | |
---|
1152 | void image::Scale(vec2i new_size) |
---|
1153 | { |
---|
1154 | vec2i old_size = m_size; |
---|
1155 | uint8_t *im = (uint8_t *)malloc(old_size.x * old_size.y); |
---|
1156 | Lock(); |
---|
1157 | memcpy(im, scan_line(0), old_size.x * old_size.y); |
---|
1158 | |
---|
1159 | DeletePage(); |
---|
1160 | MakePage(new_size, NULL); |
---|
1161 | m_size = new_size; // set the new height and width |
---|
1162 | |
---|
1163 | uint8_t *sl1, *sl2; |
---|
1164 | int y, y2, x2; |
---|
1165 | double yc, xc, yd, xd; |
---|
1166 | |
---|
1167 | yc = (double)old_size.y / (double)new_size.y; |
---|
1168 | xc = (double)old_size.x / (double)new_size.x; |
---|
1169 | for (y2 = 0, yd = 0; y2 < new_size.y; yd += yc, y2++) |
---|
1170 | { |
---|
1171 | y = (int)yd; |
---|
1172 | sl1 = im + y * old_size.x; |
---|
1173 | sl2 = scan_line(y2); |
---|
1174 | for (xd = 0, x2 = 0; x2 < new_size.x; xd += xc, x2++) |
---|
1175 | sl2[x2] = sl1[(int)xd]; |
---|
1176 | } |
---|
1177 | free(im); |
---|
1178 | if (m_special) |
---|
1179 | m_special->Resize(new_size); |
---|
1180 | Unlock(); |
---|
1181 | } |
---|
1182 | |
---|
1183 | void image::scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t xd, int16_t yd) |
---|
1184 | { |
---|
1185 | CHECK(x1>=0 && y1>=0 && x1<x2 && y1<y2 && x2<m_size.x && y2<m_size.y); |
---|
1186 | if (m_special) |
---|
1187 | { |
---|
1188 | int cx1, cy1, cx2, cy2; |
---|
1189 | m_special->GetClip(cx1, cy1, cx2, cy2); |
---|
1190 | x1=Max(x1, cx1); y1=Max(cy1, y1); x2=Min(x2, cx2 - 1); y2=Min(y2, cy2 - 1); |
---|
1191 | } |
---|
1192 | int16_t xsrc, ysrc, xdst, ydst, xtot=x2-x1-abs(xd)+1, ytot, xt; |
---|
1193 | uint8_t *src, *dst; |
---|
1194 | if (xd<0) { xsrc=x1-xd; xdst=x1; } else { xsrc=x2-xd; xdst=x2; } |
---|
1195 | if (yd<0) { ysrc=y1-yd; ydst=y1; } else { ysrc=y2-yd; ydst=y2; } |
---|
1196 | for (ytot=y2-y1-abs(yd)+1; ytot; ytot--) |
---|
1197 | { src=scan_line(ysrc)+xsrc; |
---|
1198 | dst=scan_line(ydst)+xdst; |
---|
1199 | if (xd<0) |
---|
1200 | for (xt=xtot; xt; xt--) |
---|
1201 | *(dst++)=*(src++); |
---|
1202 | else for (xt=xtot; xt; xt--) |
---|
1203 | *(dst--)=*(src--); |
---|
1204 | if (yd<0) { ysrc++; ydst++; } else { ysrc--; ydst--; } |
---|
1205 | } |
---|
1206 | AddDirty(x1, y1, x2 + 1, y2 + 1); |
---|
1207 | } |
---|
1208 | |
---|
1209 | |
---|
1210 | image *image::create_smooth(int16_t smoothness) |
---|
1211 | { |
---|
1212 | int16_t i, j, k, l, t, d; |
---|
1213 | image *im; |
---|
1214 | CHECK(smoothness>=0); |
---|
1215 | if (!smoothness) return NULL; |
---|
1216 | d=smoothness*2+1; |
---|
1217 | d=d*d; |
---|
1218 | im=new image(m_size); |
---|
1219 | for (i=0; i<m_size.x; i++) |
---|
1220 | for (j=0; j<m_size.y; j++) |
---|
1221 | { |
---|
1222 | for (t=0, k=-smoothness; k<=smoothness; k++) |
---|
1223 | for (l=-smoothness; l<=smoothness; l++) |
---|
1224 | if (i+k>smoothness && i+k<m_size.x-smoothness && j+l<m_size.y-smoothness && j+l>smoothness) |
---|
1225 | t+=Pixel(vec2i(i+k, j+l)); |
---|
1226 | else t+=Pixel(vec2i(i, j)); |
---|
1227 | im->PutPixel(vec2i(i, j), t/d); |
---|
1228 | } |
---|
1229 | return im; |
---|
1230 | } |
---|
1231 | |
---|
1232 | void image::widget_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
---|
1233 | uint8_t light, uint8_t med, uint8_t dark) |
---|
1234 | { |
---|
1235 | line(x1, y1, x2, y1, light); |
---|
1236 | line(x1, y1, x1, y2, light); |
---|
1237 | line(x2, y1+1, x2, y2, dark); |
---|
1238 | line(x1+1, y2, x2-1, y2, dark); |
---|
1239 | bar(x1+1, y1+1, x2-1, y2-1, med); |
---|
1240 | } |
---|
1241 | |
---|
1242 | class fill_rec |
---|
1243 | { |
---|
1244 | public : |
---|
1245 | int16_t x, y; |
---|
1246 | fill_rec *last; |
---|
1247 | fill_rec(int16_t X, int16_t Y, fill_rec *Last) |
---|
1248 | { x=X; y=Y; last=Last; } |
---|
1249 | } ; |
---|
1250 | |
---|
1251 | void image::flood_fill(int16_t x, int16_t y, uint8_t color) |
---|
1252 | { |
---|
1253 | uint8_t *sl, *above, *below; |
---|
1254 | fill_rec *recs=NULL, *r; |
---|
1255 | uint8_t fcolor; |
---|
1256 | Lock(); |
---|
1257 | sl=scan_line(y); |
---|
1258 | fcolor=sl[x]; |
---|
1259 | if (fcolor==color) return ; |
---|
1260 | do |
---|
1261 | { |
---|
1262 | if (recs) |
---|
1263 | { r=recs; |
---|
1264 | recs=recs->last; |
---|
1265 | x=r->x; y=r->y; |
---|
1266 | delete r; |
---|
1267 | } |
---|
1268 | sl=scan_line(y); |
---|
1269 | if (sl[x]==fcolor) |
---|
1270 | { |
---|
1271 | while (sl[x]==fcolor && x>0) x--; |
---|
1272 | if (sl[x]!=fcolor) x++; |
---|
1273 | if (y>0) |
---|
1274 | { |
---|
1275 | above=scan_line(y-1); |
---|
1276 | if (above[x]==fcolor) |
---|
1277 | { r=new fill_rec(x, y-1, recs); |
---|
1278 | recs=r; |
---|
1279 | } |
---|
1280 | } |
---|
1281 | if (y<m_size.y-1) |
---|
1282 | { |
---|
1283 | above=scan_line(y+1); |
---|
1284 | if (above[x]==fcolor) |
---|
1285 | { r=new fill_rec(x, y+1, recs); |
---|
1286 | recs=r; |
---|
1287 | } |
---|
1288 | } |
---|
1289 | |
---|
1290 | |
---|
1291 | |
---|
1292 | do |
---|
1293 | { |
---|
1294 | sl[x]=color; |
---|
1295 | if (y>0) |
---|
1296 | { above=scan_line(y-1); |
---|
1297 | if (x>0 && above[x-1]!=fcolor && above[x]==fcolor) |
---|
1298 | { r=new fill_rec(x, y-1, recs); |
---|
1299 | recs=r; |
---|
1300 | } |
---|
1301 | } |
---|
1302 | if (y<m_size.y-1) |
---|
1303 | { below=scan_line(y+1); |
---|
1304 | if (x>0 && below[x-1]!=fcolor && below[x]==fcolor) |
---|
1305 | { r=new fill_rec(x, y+1, recs); |
---|
1306 | recs=r; |
---|
1307 | } |
---|
1308 | } |
---|
1309 | x++; |
---|
1310 | } while (sl[x]==fcolor && x<m_size.x); |
---|
1311 | x--; |
---|
1312 | if (y>0) |
---|
1313 | { |
---|
1314 | above=scan_line(y-1); |
---|
1315 | if (above[x]==fcolor) |
---|
1316 | { r=new fill_rec(x, y-1, recs); |
---|
1317 | recs=r; |
---|
1318 | } |
---|
1319 | } |
---|
1320 | if (y<m_size.y-1) |
---|
1321 | { |
---|
1322 | above=scan_line(y+1); |
---|
1323 | if (above[x]==fcolor) |
---|
1324 | { r=new fill_rec(x, y+1, recs); |
---|
1325 | recs=r; |
---|
1326 | } |
---|
1327 | } |
---|
1328 | } |
---|
1329 | } while (recs); |
---|
1330 | Unlock(); |
---|
1331 | } |
---|
1332 | |
---|
1333 | |
---|
1334 | #define LED_L 5 |
---|
1335 | #define LED_H 5 |
---|
1336 | void image::burn_led(int16_t x, int16_t y, int32_t num, int16_t color, int16_t scale) |
---|
1337 | { |
---|
1338 | char st[100]; |
---|
1339 | int16_t ledx[]={ 1, 2, 1, 2, 3, 3, 3, 3, 1, 2, 0, 0, 0, 0}; |
---|
1340 | int16_t ledy[]={ 3, 3, 0, 0, 1, 2, 4, 6, 7, 7, 4, 6, 1, 2}; |
---|
1341 | |
---|
1342 | int16_t dig[]={ 2+4+8+16+32+64, 4+8, 2+4+1+32+16, 2+4+1+8+16, 64+1+4+8, |
---|
1343 | 2+64+1+8+16, 64+32+1+8+16, 2+4+8, 1+2+4+8+16+32+64, 64+2+4+1+8, 1}; |
---|
1344 | int16_t xx, yy, zz; |
---|
1345 | sprintf(st, "%8ld", (long int)num); |
---|
1346 | for (xx=0; xx<8; xx++) |
---|
1347 | { |
---|
1348 | if (st[xx]!=' ') |
---|
1349 | { |
---|
1350 | if (st[xx]=='-') |
---|
1351 | zz=10; |
---|
1352 | else |
---|
1353 | zz=st[xx]-'0'; |
---|
1354 | for (yy=0; yy<7; yy++) |
---|
1355 | if ((1<<yy)&dig[zz]) |
---|
1356 | line(x+ledx[yy*2]*scale, y+ledy[yy*2]*scale, x+ledx[yy*2+1]*scale, |
---|
1357 | y+ledy[yy*2+1]*scale, color); |
---|
1358 | } |
---|
1359 | x+=6*scale; |
---|
1360 | } |
---|
1361 | } |
---|
1362 | |
---|
1363 | uint8_t dither_matrix[]={ 0, 136, 24, 170, |
---|
1364 | 68, 204, 102, 238, |
---|
1365 | 51, 187, 17, 153, |
---|
1366 | 119, 255, 85, 221}; |
---|
1367 | |
---|
1368 | image *image::copy_part_dithered (int16_t x1, int16_t y1, int16_t x2, int16_t y2) |
---|
1369 | { |
---|
1370 | int x, y, cx1, cy1, cx2, cy2, ry, rx, bo, dity, ditx; |
---|
1371 | image *ret; |
---|
1372 | uint8_t *sl1, *sl2; |
---|
1373 | GetClip(cx1, cy1, cx2, cy2); |
---|
1374 | if (y1<cy1) y1=cy1; |
---|
1375 | if (x1<cx1) x1=cx1; |
---|
1376 | if (y2>cy2 - 1) y2=cy2 - 1; |
---|
1377 | if (x2>cx2 - 1) x2=cx2 - 1; |
---|
1378 | CHECK(x2>=x1 && y2>=y1); |
---|
1379 | if (x2<x1 || y2<y1) return NULL; |
---|
1380 | ret=new image(vec2i((x2-x1+8)/8, (y2-y1+1))); |
---|
1381 | if (!last_loaded()) |
---|
1382 | ret->clear(); |
---|
1383 | else |
---|
1384 | { |
---|
1385 | ret->Lock(); |
---|
1386 | Lock(); |
---|
1387 | for (y=y1, ry=0, dity=(y1%4)*4; y<=y2; y++, ry++) |
---|
1388 | { |
---|
1389 | sl1=ret->scan_line(ry); // sl1 is the scan linefo the return image |
---|
1390 | sl2=scan_line(y); // sl2 is the orginal image scan line |
---|
1391 | memset(sl1, 0, (x2-x1+8)/8); |
---|
1392 | for (bo=7, rx=0, x=x1, ditx=x1%4; x<=x2; x++) |
---|
1393 | { |
---|
1394 | if (last_loaded()->red(sl2[x])>dither_matrix[ditx+dity]) |
---|
1395 | sl1[rx]|=1<<bo; |
---|
1396 | if (bo!=0) |
---|
1397 | bo--; |
---|
1398 | else |
---|
1399 | { |
---|
1400 | rx++; |
---|
1401 | bo=7; |
---|
1402 | } |
---|
1403 | ditx+=1; if (ditx>3) ditx=0; |
---|
1404 | } |
---|
1405 | dity+=4; if (dity>12) dity=0; |
---|
1406 | } |
---|
1407 | Unlock(); |
---|
1408 | ret->Unlock(); |
---|
1409 | } |
---|
1410 | return ret; |
---|
1411 | } |
---|
1412 | |
---|
1413 | void image::flip_x() |
---|
1414 | { |
---|
1415 | uint8_t *rev=(uint8_t *)malloc(m_size.x), *sl; |
---|
1416 | CONDITION(rev, "memory allocation"); |
---|
1417 | int y, x, i; |
---|
1418 | |
---|
1419 | /* FIXME: Abuse Win32 uses RestoreSurface() here instead of locking */ |
---|
1420 | Lock(); |
---|
1421 | for (y=0; y<m_size.y; y++) |
---|
1422 | { sl=scan_line(y); |
---|
1423 | for (i=0, x=m_size.x-1; x>=0; x--, i++) |
---|
1424 | rev[i]=sl[x]; |
---|
1425 | memcpy(sl, rev, m_size.x); |
---|
1426 | } |
---|
1427 | Unlock(); |
---|
1428 | free(rev); |
---|
1429 | } |
---|
1430 | |
---|
1431 | void image::flip_y() |
---|
1432 | { |
---|
1433 | uint8_t *rev=(uint8_t *)malloc(m_size.x), *sl; |
---|
1434 | CONDITION(rev, "memory allocation"); |
---|
1435 | int y; |
---|
1436 | |
---|
1437 | /* FIXME: Abuse Win32 uses RestoreSurface() here instead of locking */ |
---|
1438 | Lock(); |
---|
1439 | for (y=0; y<m_size.y/2; y++) |
---|
1440 | { sl=scan_line(y); |
---|
1441 | memcpy(rev, sl, m_size.x); |
---|
1442 | memcpy(sl, scan_line(m_size.y-y-1), m_size.x); |
---|
1443 | memcpy(scan_line(m_size.y-y-1), rev, m_size.x); |
---|
1444 | } |
---|
1445 | Unlock(); |
---|
1446 | free(rev); |
---|
1447 | } |
---|
1448 | |
---|
1449 | void image::make_color(uint8_t color) |
---|
1450 | { |
---|
1451 | uint8_t *sl; |
---|
1452 | int y, x; |
---|
1453 | Lock(); |
---|
1454 | for (y=0; y<m_size.y; y++) |
---|
1455 | { |
---|
1456 | sl=scan_line(y); |
---|
1457 | for (x=m_size.x; x; x--, sl++) |
---|
1458 | if (*sl) |
---|
1459 | *sl=color; |
---|
1460 | } |
---|
1461 | Unlock(); |
---|
1462 | } |
---|