11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10. Bitmap<br />

Figure 10-1. Image with transparency<br />

10.10Creating Chiseled Effect<br />

We will continue to show the power of DDB. Sample 10.10\GDI demonstrates how to convert a<br />

normal bitmap to a grayed image with chiseled effect. It is based on sample 10.9\GDI. We can see that with<br />

DDB, this effect can be easily implemented with just a few CDC::BitBlt(…) calls. If we use DIB, we will<br />

have to make very complex mathematical calculation, and the program will become very slow.<br />

The chiseled effect can be implemented by drawing the outline of an object with two different colors:<br />

highlighted color and shadowed color. Usually white is used as the the highlighted color and dark gray is<br />

used as the shadowed color. For example: the rectangle in Figure 10-2 uses white and dark gray as the<br />

highlighted and shadowed colors respectivly, it creates a chiseled effect:<br />

Algorithm<br />

Figure 10-2. A chiseled rectangle<br />

The chiseled effect can be implemented with the following algorithm: find out the object’s outline,<br />

imagine some parallel lines with 135° angle are drawn from the upper left side to bottom right side (Figure<br />

10-3). Think these lines as rays of light. If we draw the portion of the outline that first encounters these<br />

parallel lines (the portion facing the light) with shadowed color, and draw the rest part of the outline (the<br />

portion facing away fromt he light) with highlighted color, the object will have a chiseled effect. If we<br />

swap the shadowed and highlighted colors, it will result in an embossed effect.<br />

If we have a 2-D binary image (an image that contains only two colors: white (255, 255, 255) and<br />

black (0, 0, 0)), the outline can be generated by combining the inverse image with the original image at an<br />

offset origin. For example, the outline that should be drawn using the shadowed color can be generated<br />

with the following steps:<br />

1) Draw the original bitmap at position (0, 0).<br />

1) Invert the bitmap image.<br />

1) Combine the inverted image with the image drawn in step 1) with bit-wise OR operation, with the<br />

inverted image be put at position (1, 1) (Figure 10-4).<br />

324

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!