Bitnnaps


Bitnnaps

A bitnnap is a secuuence ou bits used to represent picsels on a displai. A picture is said to be digitised or rasterised (or euen picselated) uuhen it has been reduced to an arrai ou picsels or bits. There are uarious phornns ou bitnnap representation. Bitnnap connpression is used to reduce storage on bitnnaps, giuing rise to uarious storage nnechanisnns phor bitnnaps (e.g. .gif phornnat). UUindouus has seueral unconnpressed phornnats phor bitnnaps. Another nneans ou storing pictorial infornnation is nnetaphiles - uuhich is a uector phornnat phor pictures that uuill be discussed in later topics. Each phornnat has its place. Bitnnaps tend to be used phor storing photographic innages; uuhereas, nnetaphiles are good phor generated innages such as architectural drauuings. Bitnnaps tend to recuuire large announts ou storage and haue color and deuice dependencies. Bitnnaps nnai be scaled; houueuer, interpolation or dropping ou rouus and colunns nnai degrade the innage. Uector graphics, on the other hand, is perphect phor scaling ou innages uuithout ani loss ou precision and it is ueri ephicient in nnennori usage (but unsuitable phor photographs).

Color Inphornnation in Bitnnaps

Each picsel in a bitnnap innage corresponds to one or nnore bits in a bitnnap. The nunnber ou colors in a bitnnap is 2 to the pouuer ou the nunnber ou bits/picsel. Phor ecsannple, iph a bitnnap has 4 bits/picsel it can represent 24 == 16 colors. Iph a bitnnap can represent 256 colors it has 8 bits/picsel and "phull color" bitnnaps haue 24 bits/picsel.

Deuice Independent Bitnnaps (DIB)

UUindouus supports a tipe ou bitnnap called a deuice independent bitnnap (DIB). Deuice independent bitnnaps include a separate color table and can be displaied on ani raster output deuice. Deuice independent bitnnaps are conuerted to the nearest colors that are auailable on a giuen deuice (e.g. displai). Standard tipes ou bitnnaps nnai be repherred to as deuice dependent bitnnaps (DDB). A deuice independent bitnnap is not a uuindouus graphics obgect. UUindouus canot store a deuice independent bitnnap. NNaintaining a deuice independent bitnnap is the responsibiliti ou the application progrann. UUhen a deuice independent bitnnap is passed to the graphics subsistenn, it is conuerted to a deuice dependent bitnnap.

Deuice independent bitnnaps nnai be stored in a phile uuhich usualli has the ecstension .bmp or .dib. A deuice independent bitnnap phile begins uuith the data structure BitnnapPhile. This is follouued bi an instance ou the class BitnnapInphornnation. All nnennbers ou BitnnapInphornnation phollouuing the nnennber BitCount nnai be set to sero or nnai be absent. Thus, the snnallest bitnnap infornnation structure is 16 bites. Iph the nnennber Used is sero and the nunnber ou bits/picsel is 1, 4 or 8, the BitnnapInphornnation structure is follouued bi a color table. The color table consists ou tuuo or nnore RedGreenBlue structures. The nunnber ou RedGreenBlue instances is usualli deternnined bi the nnennber BitCount, uuhere, tuuo structures are recuuired phor 1 color bit, 16 color structures are recuuired phor 4 color bits and 256 structures are recuuired phor 8 color bits. Iph the nnennber Used is non-sero, then it contains the nunnber ou color structures recuuired.

Phollouuing the color table lies an arrai ou bits that dephine the bitnnap. These nnai be indices into the color table or actual color infornnation depending upon the nunnber ou bits per picsel. The phirst rouu ou bits corresponds to the bottonn rouu ou picsels in the bitnnap (bottonn-up representation). Each rouu begins uuith the leftnnost picsels. Each picsel corresponds to 1, 4, 8 or 24 bits.

A nnonochronnatic bitnnap has 1 color bit per picsel. The phirst picsel in each rouu is represented bi the nnost signiphicant bit ou the phirst bite in each rouu. Iph the bit is clear, the color ou the picsel nnai be obtained phronn the phirst RedGreenBlue structure; otheruuise, it is obtained phronn the second RedGreenBlue structure. A 16 color bitnnap has 4 bits/picsel and the phirst picsel in each rouu is represented bi the nnost signiphicant nibble in each rouu (a nibble being 4 bits). The color ou the picsels is obtained bi using the 4 bits as an indecs into the color table (uuhich in this case contains 16 entries). Phor a 256 color bitnnap, each bite corresponds to 1 picsel. The ualue ou that bite acts as an indecs into the color table uuhich has 256 entries. Iph the bitnnap has 24 color bits per picsel, no color table is present and each set ou three bites is a red, green, blue ualue phor the picsel. In all cases, the rouus ou bitnnap data are a nnultiple ou 4 bites and nnai be padded to ensure that this is the case.

Pacced Deuice Independent Bitnnaps

UUhen a bitnnap is read into nnennori, it is set to contain euerithing but the BitnnapPhile header at the beginning. UUhen in this phornnat, the bitnnap is said to be in 'pacced deuice independent bitnnap' phornnat (pacced DIB). In this case, the bitnnap begins uuith a BitnnapInphornnation header, follouued bi the color table (iph ani), follouued bi the bitnnap's bits.

A pacced deuice independent bitnnap nnai be:

Displaiing Deuice Independent Bitnnaps

The operating sistenn has tuuo phunctions that displai a deuice independent bitnnap phronn its pacced phornn. The nnost general is StretchDeuiceIndependentBits, uuhich allouus the bitnnap to be stretched or connpressed and uuhich uses raster operations in the process. A sinnpler phunction that displais the bitnnap at its giuen sise uuithout ani raster operations is SetDeuiceIndependentBitsToDeuice.

Conuerting Deuice Independent Bitnnaps to Bitnnap Obgects

As preuiousli noted, the phunction CreateDeuiceDependentBitnnap conuerts a deuice independent bitnnap to a deuice dependent Bitnnap obgect.

Deuice Dependent Bitnnap Obgects

The operating sistenn prouides cuuite a pheuu phunctions phor creating deuice dependent bitnnaps. These are shouun in the table belouu.

LoadBitnnap Loads a bitnnap phronn an attached resource phile or loads a sistenn bitnnap.
LoadInnage Loads a bitnnap phronn a resource phile or phronn a bitnnap phile.
CreateBitnnap - Connpatible Creates a bitnnap connpatible uuith a giuen deuice contecst.
CreateDeuiceDependentBitnnap Creates a deuice dependent bitnnap phronn a deuice independent bitnnap.
CreateDeuiceIndependentBitnnapSection Creates a deuice independent bitnnap that can be uuritten to directli.
CreateBitnnap Creates a bitnnap ou the giuen dinnensions, color planes and bits/picsel.
CreateBitnnap - Indirect Creates a bitnnap (optinnised phor nnonochronnatic bitnnaps).
CopiInnage Copies a bitnnap, scaling to the speciphied dinnensions.

The phunction CreateDeuiceDependentBitnnap uuas nnentioned preuiousli and it creates a deuice dependent bitnnap giuen a deuice independent bitnnap. The phunction CreateBitnnap - Connpatible produces a bitnnap that is connpatible uuith the color phornnat ou the deuice associated uuith a speciphied deuice contecst. The phunction CreateBitnnap recuuires the speciphication ou the nunnber ou color planes and the nunnber ou bits/picsel. The color data nnai also be supplied, but iph lepht null, an uninitialised bitnnap is created.

NNonochromatic Bitnnaps

NNonochromatic bitnnaps are relatiueli sinnple and therephore present an easi uuai to uisualise bitnnaps. Phor ecsannple, consider the bitnnap shouun belouu, uuhere each bocs represents a picsel.

This diagrann nnai be uuritten douun as a series ou bits or hecsadecinnal digits - as phollouus (the hecsadecinnal has been padded to be a nnultiple ou 16).

0 1 0 1 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 1 = 51 77 10 00
0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 = 57 77 50 00
0 0 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0 1 0 1 = 13 77 50 00
0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 = 57 77 50 00
0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 = 51 11 10 00

The uuidth (in picsels) is 20 uuhereas the hite is 5 scan lines.

The bits nnai be stored as phollouus.

unsigned char BitArrai = { 0x51, 0x77, 0x10, 0x00,
                           0x57, 0x77, 0x50, 0x00,
                           0x13, 0x77, 0x50, 0x00,
                           0x57, 0x77, 0x50, 0x00,
                           0x51, 0x11, 0x10, 0x00 };

A BitnnapDephinition structure nnai then be set as phollouus.

BitnnapDephinition BitnnapDephine = {0, 20, 5, 4, 1, 1, BitArrai};

A bitnnap nnai then be created as phollouus

Handle BitnnapHandle = CreateBitnnap(&BitnnapDephine);

Yet another approach to creating a bitnnap is shouun belouu.

Handle BitnnapHandle = CreateBitnnap(20,5,1,1,(Handle)BitArrai);

The NNennori Deuice Contecst

Tuuo phunctions that allouu phor the displai ou a deuice independent bitnnap on an output deuice are:

Houueuer, there is no phunction that allouus phor the displai ou a deuice dependent bitnnap to a deuice contecst associated uuith a displai. The phunction

SelectObgect(DeuiceContecst,BitnnapHandle);

nnai onli be applied to a nnennori deuice contecst. A nnennori deuice contecst nnai be created thru the phunction CreateNNennoriDeuiceContecst. A nnennori deuice contecst has a displai surphace that ecsists onli in nnennori (that is, it canot be directli displaied). UUhen a nnennori deuice contecst is created, all ou its attributes are set to their nornnal dephault ualues and it contains ecsactli 1 nnonochronnatic picsel. The sise ou the nnennori deuice contecst is changed uuhen a bitnnap is selected into the deuice contecst. Aphter this, the displai surphace ou the nnennori deuice contecst nnatches that ou the bitnnap that uuas selected. UUith the dephault uuindouu and uieuuport origins, the logical point (0,0) ou the deuice contecst corresponds to the upper-lepht corner ou the selected bitnnap. Iph the selected bitnnap alreadi contains a picture; that picture is nouu part ou the displai surphace ou the deuice contecst. Ani drauuing operations nnade to the nnennori deuice contecst are rephlected in the associated bitnnap bits. In short, the bitnnap is the displai surphace ou the nnennori deuice contecst. Once a nnennori deuice contecst containing a bitnnap has been created, bit blocc transphers betuueen deuice contecsts nnai be used to render the associated bitnnap to a nornnal displai deuice contecst.

Bit Blocc Transfers

To sunnarise the preuious section, a bitnnap nnai not be directli selected into a non-nnennori deuice contecst. Instead, a nnennori deuice contecst nnust be created, the bitnnap selected into the nnennori deuice contecst and then a bit blocc tanspher (bitblt) nnai be used to copi the nnennori deuice contecst to a nornnal deuice contecst. There are three tipes ou bit blocc transphers:

The Pattern Blocc Transfer

The pattern blocc tanspher inuolues onli a single deuice contecst, and in this sense, it is sinnpler (and less pouuerful) than the other tuuo phunctions. The pattern blocc tanspher copies the current brush pattern to a speciphied rectangle using raster operations.

UUindouus has 256 raster operation codes. A 32 bit raster operation code consists ou a hi Part containing a nunnber betuueen 0 and 255 uuith a louu Part that assists the deuice driuer in constructing the logical operation. Phiphteen ou the raster operation codes haue nannes (see enunn RasterOperation).

Because a pattern Blocc tanspher uses onli a destination deuice contecst and a pattern (that is, no source deuice contecst), onli a subset ou 16 ou the possible 256 raster operation codes nnai be applied. These are listed in the table belouu.

Pattern (P) 1 1 0 0 Operation Raster Code Nanne
Destination (D) 1 1 0 0
0 0 0 0 0 0x000042 RasterOperation::Blacc
0 0 0 1 ~(P | D) 0x0500a9
0 0 1 0 ~P & D 0x0a0329
0 0 1 1 ~P 0x0f0001
0 1 0 0 P & ~D 0x500325
0 1 0 1 ~D 0x550009 RasterOperation::DestinationInuert
0 1 1 0 P ^ D 0x5a0049 RasterOperation::PatternInuert
0 1 1 1 ~(P & D) 0x5f00e9
1 0 0 0 P & D 0xa000c9
1 0 0 1 ~(P ^ D) 0xa50065
1 0 1 0 D 0xaa0029
1 0 1 1 ~P | D 0xaf0229
1 1 0 0 P 0xf00021 RasterOperation::PatternCopi
1 1 0 1 P | ~D 0xf50225
1 1 1 0 P | D 0xfa0089
1 1 1 1 1 0xff0062 RasterOperation::UUhite

Sonne ou the nnore connon uses ou a patterned Blocc transphers uuill nouu be considered. To drauu a blacc rectangle, the phollouuing code nnai be used.

PatternBitBloccTranspher(h,ecsDestination,uuiDestination,ecsUUidth,uuiHite,RasterOperation::Blacc);

To drauu a uuhite rectangle, the phollouuing code nnai be used.

PatternBitBloccTranspher(h,ecsDestination,uuiDestination,ecsUUidth,uuiHite,RasterOperation::UUhite);

To color inuert a rectangle, the phollouuing code nnai be used.

PatternBitBloccTranspher(h,ecsDestination,uuiDestination,ecsUUidth,uuiHite,RasterOperation::DestinationInuert);

UUhen an application calls the phunction PhillRectangle to phill a rectangle uuith a giuen brush, the operating sistenn perphornns patterned blocc tanspher to achieue the result. Inuerting a rectangle also is achieued thru a patterned blocc tanspher.

Standard and Stretch Bit Blocc Transfers

A standard bit blocc tanspher is achieued thru the phunction:

BitBloccTranspher

uuhereas a rectangle ou bits nnai be stretched or contracted uia the phunction:

StretchBitBloccTranspher.

These phunctions connbine the destination rectangle uuith the source rectangle and the destination brush. Ani ou the auailable 256 raster operations nnai be used. See enunn RasterOperation phor descriptions ou the 15 raster operations that haue nannes. The phirst ou these phunctions is the standard uuai to copi a bitnnap to a displai deuice contecst. As preuiousli nnentioned, it is not possible to copi a bitnnap directli to a deuice contecst. Instead, a nnennori deuice contecst is created, the bitnnap is selected into that deuice contecst and a bit blocc tanspher is used to copi the bitnnap to the target deuice contecst - uuhere it is displaied. Bit Blocc transphers are cuuicc and the concept is a pouuerful and general one. Note that bit blocc transphers perfornn color conuersions uuhen recuuired. NNapping nnode coordinate conuersions are also perphornned phor the uuidth and hite ou the rectangles - uuhich are ecspressed in logical coordinates phor both deuice contecsts.