Beautiful Code [18]
Raster operation 14: 1 1 1 0 S | D
Raster operation 15: 1 1 1 1 1
There are four possible combinations of source and destination pixels, and each raster operation does something different for those four combinations, so the total number is 24, or 16. Each of the 16 possible raster operations is identified by a number that corresponds to the pattern of resultant pixels shown in the table. The "Logical representation" column shows (in C syntax) the actual Boolean operation occurring between the source and destination pixels.
For example, in raster operation 12 (the most common), the source is simply transferred to the destination and in raster operation 14, the source is transferred to the destination only when the destination is black. Raster operation 10 leaves the destination the same regardless of the source. Raster operations 0 and 15 simply color the destination black and white, respectively, again independent of the source.
In a color graphics system, each pixel is generally 24-bits wide, with 8 bits each for the red, green, and blue primaries. If all bits are 0, the color is black; if all bits are 1, the color is white. The raster operations are applied to corresponding bits of the source and destination. With raster operation 14, for example, the source is displayed in destination areas that were initially colored black. Destination areas initially colored white will remain white. However, if a destination area is red and the source is blue, the result will be a combination of red and blue, or magenta. This is different from the monochrome example, but still entirely predictable.
In Windows, the raster operations for BitBlt and StretchBlt were complicated even further. Windows supported a graphical object called a pattern or brush, which was commonly used for filling enclosed areas. This pattern could be a solid color or a repetitive image, such as hash marks or bricks. To carry out this type of operation, BitBlt and StretchBlt performed a raster operation between the source, the destination, and a particular pattern. This pattern allowed the program to alter pixel bits of the source (perhaps inverting them or masking them) without regard to the destination.
Because the raster operation implemented by BitBlt and StretchBlt involved three objects—a source, destination, and pattern—it was called a ternary raster operation. There are 256 possible ternary raster operations, and BitBlt and StretchBlt supported every single one.
As in our earlier discussion, these 256 ternary raster operations are easier to comprehend if you begin by considering a monochrome graphics system. In addition to the source and destination, the pattern is also an array of 1-bit pixels; visualize the pattern overlaying a destination surface. Table 8-2 shows selections from the 256 ways in which the 0 and 1 pixels of the pattern, source, and destination can be combined.
Table 8-2. Ternary raster ops
Possible Combination
Input parameter Input value
Pattern (P): 1 1 1 1 0 0 0 0
Source (S): 1 1 0 0 1 1 0 0
Operation Output
Raster operation 0x00: 0 0 0 0 0 0 0 0
Raster operation 0x01: 0 0 0 0 0 0 0 1
Raster operation 0x02: 0 0 0 0 0 0 1 0
… …
Raster operation 0x60: 0 1 1 0 0 0 0 0
… …
Raster operation 0xFD: 1 1 1 1 1 1 0 1
Raster operation 0xFE: 1 1 1 1 1 1 1 0
Raster operation 0xFF: 1 1 1 1 1 1 1 1
This table shows sample inputs followed by the resulting destinations for 7 of the 256 possible ternary raster operations. Each of these raster operations can be identified by a one-byte hexadecimal number corresponding to the pattern of resultant destination bits shown in the table. For example, for raster operation 0x60, if the pattern pixel is 1 (white), the source pixel is 0 (black), and the destination pixel is 1, the destination will be 1 (white).
In the early versions of Windows, 15 of the total 256 raster operations were identified with names in both the documentation and the Windows header file that C programmers used. The first—where the destination is colored with all 0s regardless of