1 | ␊ |
2 | /* Graphic utility functions and data types␊ |
3 | * Prashant Vaibhav (C) 12/12/2008␊ |
4 | * Chameleon␊ |
5 | */␊ |
6 | ␊ |
7 | //Azi:include***␊ |
8 | //#include "boot.h" - included on "graphic_utils.h" (& graphics.h)...␊ |
9 | //#include "graphic_utils.h" - ... wich is included on graphics.h, that's included on gui.h :P␊ |
10 | #include "gui.h"␊ |
11 | ␊ |
12 | void blend( const pixmap_t *blendThis, // Source image␊ |
13 | pixmap_t *blendInto, // Dest image␊ |
14 | const position_t position) // Where to place the source image␊ |
15 | {␊ |
16 | uint16_t sx, sy, dx, dy;␊ |
17 | uint32_t dstrb, dstag, srcrb, srcag, drb, dag, rb, ag, alpha;␊ |
18 | ␉␊ |
19 | ␉uint16_t width = (blendThis->width + position.x < blendInto->width) ? blendThis->width: blendInto->width-position.x;␊ |
20 | ␉uint16_t height = (blendThis->height + position.y < blendInto->height) ? blendThis->height: blendInto->height-position.y;␊ |
21 | ␉␊ |
22 | for (dy = position.y, sy = 0; sy < height; dy++, sy++) {␊ |
23 | for (dx = position.x, sx = 0; sx < width; dx++, sx++) {␊ |
24 | alpha = (pixel(blendThis, sx, sy).ch.a);␊ |
25 | ␊ |
26 | ␉ /* Skip blending for fully transparent pixel */␊ |
27 | ␉ if (alpha == 0) continue;␊ |
28 | ␊ |
29 | ␉ /* For fully opaque pixel, there is no need to interpolate */␊ |
30 | ␉ if (alpha == 255) {␊ |
31 | ␉␉ pixel(blendInto, dx, dy).value = pixel(blendThis, sx, sy).value;␊ |
32 | ␉␉ continue;␊ |
33 | ␉ }␊ |
34 | ␊ |
35 | ␉ /* For semi-transparent pixels, do a full blend */␊ |
36 | ␉ //alpha++␊ |
37 | ␉␉ /* This is needed to spread the alpha over [0..256] instead of [0..255]␊ |
38 | ␉␉␉Boundary conditions were handled above */␊ |
39 | dstrb = pixel(blendInto, dx, dy).value & 0xFF00FF;␊ |
40 | dstag = (pixel(blendInto, dx, dy).value >> 8) & 0xFF00FF;␊ |
41 | srcrb = pixel(blendThis, sx, sy).value & 0xFF00FF;␊ |
42 | srcag = (pixel(blendThis, sx, sy).value >> 8) & 0xFF00FF;␊ |
43 | drb = srcrb - dstrb;␊ |
44 | dag = srcag - dstag;␊ |
45 | drb *= alpha; dag *= alpha;␊ |
46 | drb >>= 8; dag >>= 8;␊ |
47 | rb = (drb + dstrb) & 0x00FF00FF;␊ |
48 | ag = ((dag + dstag) << 8) & 0xFF00FF00;␊ |
49 | pixel(blendInto, dx, dy).value = (rb | ag);␊ |
50 | }␊ |
51 | }␊ |
52 | }␊ |
53 | ␊ |
54 | position_t centeredIn( const pixmap_t *background, const pixmap_t *toCenter )␊ |
55 | {␊ |
56 | position_t centered;␊ |
57 | centered.x = ( background->width - toCenter->width ) / 2;␊ |
58 | centered.y = ( background->height - toCenter->height ) / 2;␊ |
59 | return centered;␊ |
60 | }␊ |
61 | ␊ |
62 | position_t centeredAt( const pixmap_t *pixmap, const position_t center )␊ |
63 | {␊ |
64 | position_t topleft;␊ |
65 | topleft.x = center.x - (pixmap->width / 2);␊ |
66 | topleft.y = center.y - (pixmap->height / 2);␊ |
67 | return topleft;␊ |
68 | }␊ |
69 | ␊ |
70 | position_t pos(const uint16_t x, const uint16_t y) { position_t p; p.x = x; p.y = y; return p; }␊ |
71 | ␊ |
72 | void flipRB(pixmap_t *p)␊ |
73 | {␊ |
74 | ␉//if(testForQemu()) return;␊ |
75 | ␉␊ |
76 | ␉uint32_t x;␊ |
77 | register uint8_t tempB;␊ |
78 | ␉for (x = 0; x < (p->height) * (p->width) ; x++) {␊ |
79 | ␉␉tempB = (p->pixels[x]).ch.b;␊ |
80 | (p->pixels[x]).ch.b = (p->pixels[x]).ch.r;␊ |
81 | (p->pixels[x]).ch.r = tempB;␊ |
82 | ␉}␊ |
83 | }␊ |
84 | |