Beautiful Code [17]
The technique of on-the-fly code generation was implemented in a much larger way in the first release of Microsoft Windows (version 1.0), which came out in November 1985 and has since come to have some moderate success in the personal computer marketplace. From a programmer's perspective, the first version of Windows offered roughly 200 functions for creating graphical user interfaces and for displaying vector and raster graphics on both the screen and printer in a fairly device-independent manner.
Among the graphics functions in Windows 1.0 was one called BitBlt, which was named after an instruction on the seminal Xerox Alto and stood for bit block transfer. In its most basic use, BitBlt rendered bitmaps on the screen and printer, but it was also used internally in Windows for displaying many user interface objects. More generally, BitBlt transferred rectangular arrays of pixels from a source to a destination. A related function called StretchBlt could stretch or compress the source pixels into a larger or smaller destination rectangle during this process.
If the BitBlt source is a bitmap, and if the destination is the video display, BitBlt copies the pixels from the bitmap to the display, essentially rendering the bitmap on the screen. If the source is the display and the destination is a bitmap, BitBlt copies pixels from the screen to the bitmap. The bitmap image is then a captured screen image.
However, if you're writing a routine like BitBlt, you might imagine incorporating some extra value and utility that go beyond the mere transfer of bits. Suppose you want an option that will invert the pixels as they're transferred from the source to the destination; black pixels become white, light gray becomes dark gray, and green becomes magenta.
And suppose you then discover that a colleague would be overjoyed if BitBlt could examine the destination as it's transferring pixels, and transfer pixels from the source to the destination only if the destination pixels at each particular point are black. This feature would allow the display of nonrectangular images. For example, a black-filled circle could be drawn on the screen, and then BitBlt would display a bitmap only within that circle. And then somebody else requests an option that combines the ones just mentioned, in which BitBlt inverts its source pixels when the destination is black.
As you start investigating these types of options, you might discover a way to generalize them all. Consider a monochrome graphics system; every pixel is just one bit, where 0 means black and 1 means white. In such a system, a source bitmap is an array of 1-bit pixels, and the screen is an array of 1-bit pixels. The destination's color at a particular pixel location depends on the value of the source pixel (0 or 1) and the value of the destination pixel (0 or 1).
The result at the destination for any particular combination of source and destination pixels is called a raster operation or raster op, and there are 16 of them, as Table 8-1 illustrates.
Table 8-1. Basic raster ops
Possible combinations
Input parameter Input value
Source (S): 1 1 0 0
Destination (D): 1 0 1 0
Operation Output Logical representation
Raster operation 0: 0 0 0 0 0
Raster operation 1: 0 0 0 1 ~(S | D)
Raster operation 2: 0 0 1 0 ~S & D
Raster operation 3: 0 0 1 1 ~S
Raster operation 4: 0 1 0 0 S & ~D
Raster operation 5: 0 1 0 1 ~D
Raster operation 6: 0 1 1 0 S ^ D
Raster operation 7: 0 1 1 1 ~(S & D)
Raster operation 8: 1 0 0 0 S & D
Raster operation 9: 1 0 0 1 ~(S ^ D)
Raster operation 10: 1 0 1 0 D
Raster operation 11: 1 0 1 1 ~S | D
Raster operation 12: 1 1 0 0 S
Raster