Tải bản đầy đủ - 0 (trang)
Chapter 8. On-the-Fly Code Generation for Image Processing

Chapter 8. On-the-Fly Code Generation for Image Processing

Tải bản đầy đủ - 0trang

Thison-the-flycodegenerationisnoteasy,soit'susually

restrictedtoveryparticularcircumstances.

Throughoutthischapter,wewilluseanexamplethatembodies

themostcommonreasonforusingon-the-flycodegeneration.

Inthisexample,atime-criticalsubroutinemustperformmany

repetitiveoperations.Anumberofgeneralizedparameters

comeintoplayduringtheexecutionoftheserepetitive

operations,andthesubroutinecouldrunalotfasterifwe

replacedthosegeneralizedparameterswithspecificvalues.We

can'treplacethoseparameterswhilewe'rewritingthe

subroutinebecausetheparametersaren'tknownuntilruntime,

andtheycanchangefromoneinvocationtothenext.However,

thesubroutineitselfcoulddothecodegenerationwhileit's

running.Inotherwords,thesubroutinecanexamineitsown

parametersatruntime,generatemoreefficientcode,andthen

executetheresultingcode.

Ifirststumbleduponthistechniquewhilewritingassembly

language.Ihadasubroutinethatperformedmanyrepetitive

operations.Atacrucialpoint,thesubroutinewouldexecute

eitherabitwiseANDoperationorabitwiseORoperation,

dependingonsomeothervaluethatremainedconstantduring

theseoperations.Theactualtestingofthisvaluetoperformthe

ANDorORoperationwasinsidetheloopandwasitselftaking

toomuchtime.Iconsideredsplittingtheroutineintotwo

entirelyseparateroutines,onewiththeANDoperationandone

withtheORoperation,untilIrealizedthatthesubroutinecould

beginbyexaminingthevalue,theninserttheactualANDorOR

machinecodeinstructionrightintotheexecutionstream.

Thetechniqueofon-the-flycodegenerationwasimplemented

inamuchlargerwayinthefirstreleaseofMicrosoftWindows

(version1.0),whichcameoutinNovember1985andhassince

cometohavesomemoderatesuccessinthepersonalcomputer

marketplace.Fromaprogrammer'sperspective,thefirst

versionofWindowsofferedroughly200functionsforcreating

graphicaluserinterfacesandfordisplayingvectorandraster



graphicsonboththescreenandprinterinafairlydeviceindependentmanner.

AmongthegraphicsfunctionsinWindows1.0wasonecalled

BitBlt,whichwasnamedafteraninstructionontheseminal

XeroxAltoandstoodforbitblocktransfer.Initsmostbasic

use,BitBltrenderedbitmapsonthescreenandprinter,butit

wasalsousedinternallyinWindowsfordisplayingmanyuser

interfaceobjects.Moregenerally,BitBlttransferredrectangular

arraysofpixelsfromasourcetoadestination.Arelated

functioncalledStretchBltcouldstretchorcompressthesource

pixelsintoalargerorsmallerdestinationrectangleduringthis

process.

IftheBitBltsourceisabitmap,andifthedestinationisthe

videodisplay,BitBltcopiesthepixelsfromthebitmaptothe

display,essentiallyrenderingthebitmaponthescreen.Ifthe

sourceisthedisplayandthedestinationisabitmap,BitBlt

copiespixelsfromthescreentothebitmap.Thebitmapimage

isthenacapturedscreenimage.

However,ifyou'rewritingaroutinelikeBitBlt,youmight

imagineincorporatingsomeextravalueandutilitythatgo

beyondthemeretransferofbits.Supposeyouwantanoption

thatwillinvertthepixelsasthey'retransferredfromthesource

tothedestination;blackpixelsbecomewhite,lightgray

becomesdarkgray,andgreenbecomesmagenta.

Andsupposeyouthendiscoverthatacolleaguewouldbe

overjoyedifBitBltcouldexaminethedestinationasit's

transferringpixels,andtransferpixelsfromthesourcetothe

destinationonlyifthedestinationpixelsateachparticularpoint

areblack.Thisfeaturewouldallowthedisplayof

nonrectangularimages.Forexample,ablack-filledcirclecould

bedrawnonthescreen,andthenBitBltwoulddisplayabitmap

onlywithinthatcircle.Andthensomebodyelserequestsan

optionthatcombinestheonesjustmentioned,inwhichBitBlt

invertsitssourcepixelswhenthedestinationisblack.



Asyoustartinvestigatingthesetypesofoptions,youmight

discoverawaytogeneralizethemall.Consideramonochrome

graphicssystem;everypixelisjustonebit,where0means

blackand1meanswhite.Insuchasystem,asourcebitmapis

anarrayof1-bitpixels,andthescreenisanarrayof1-bit

pixels.Thedestination'scolorataparticularpixellocation

dependsonthevalueofthesourcepixel(0or1)andthevalue

ofthedestinationpixel(0or1).

Theresultatthedestinationforanyparticularcombinationof

sourceanddestinationpixelsiscalledarasteroperationor

rasterop,andthereare16ofthem,asTable8-1illustrates.

Table8-1.Basicrasterops

Possiblecombinations

Inputparameter



Inputvalue



Source(S):



1100







Destination(D):



1010







Operation



Output



Logicalrepresentation



Rasteroperation0:



0000



0



Rasteroperation1:



0001



~(S|D)



Rasteroperation2:



0010



~S&D



Rasteroperation3:



0011



~S



Rasteroperation4:



0100



S&~D



Rasteroperation5:



0101



~D



Rasteroperation6:



0110



S^D



Rasteroperation7:



0111



~(S&D)



Rasteroperation8:



1000



S&D



Rasteroperation9:



1001



~(S^D)



Rasteroperation10: 1010



D



Rasteroperation11: 1011



~S|D



Rasteroperation12: 1100



S



Rasteroperation13: 1101



S|~D



Rasteroperation14: 1110



S|D



Rasteroperation15: 1111



1



Therearefourpossiblecombinationsofsourceanddestination

pixels,andeachrasteroperationdoessomethingdifferentfor

thosefourcombinations,sothetotalnumberis24,or16.Each

ofthe16possiblerasteroperationsisidentifiedbyanumber

thatcorrespondstothepatternofresultantpixelsshowninthe

table.The"Logicalrepresentation"columnshows(inCsyntax)

theactualBooleanoperationoccurringbetweenthesourceand

destinationpixels.

Forexample,inrasteroperation12(themostcommon),the

sourceissimplytransferredtothedestinationandinraster

operation14,thesourceistransferredtothedestinationonly

whenthedestinationisblack.Rasteroperation10leavesthe

destinationthesameregardlessofthesource.Raster

operations0and15simplycolorthedestinationblackand

white,respectively,againindependentofthesource.

Inacolorgraphicssystem,eachpixelisgenerally24-bitswide,

with8bitseachforthered,green,andblueprimaries.Ifall

bitsare0,thecolorisblack;ifallbitsare1,thecoloriswhite.

Therasteroperationsareappliedtocorrespondingbitsofthe

sourceanddestination.Withrasteroperation14,forexample,

thesourceisdisplayedindestinationareasthatwereinitially

coloredblack.Destinationareasinitiallycoloredwhitewill

remainwhite.However,ifadestinationareaisredandthe

sourceisblue,theresultwillbeacombinationofredandblue,

ormagenta.Thisisdifferentfromthemonochromeexample,



butstillentirelypredictable.

InWindows,therasteroperationsforBitBltandStretchBltwere

complicatedevenfurther.Windowssupportedagraphicalobject

calledapatternorbrush,whichwascommonlyusedforfilling

enclosedareas.Thispatterncouldbeasolidcolorora

repetitiveimage,suchashashmarksorbricks.Tocarryoutthis

typeofoperation,BitBltandStretchBltperformedaraster

operationbetweenthesource,thedestination,andaparticular

pattern.Thispatternallowedtheprogramtoalterpixelbitsof

thesource(perhapsinvertingthemormaskingthem)without

regardtothedestination.

BecausetherasteroperationimplementedbyBitBltand

StretchBltinvolvedthreeobjects—asource,destination,and

pattern—itwascalledaternaryrasteroperation.Thereare256

possibleternaryrasteroperations,andBitBltandStretchBlt

supportedeverysingleone.

Asinourearlierdiscussion,these256ternaryrasteroperations

areeasiertocomprehendifyoubeginbyconsideringa

monochromegraphicssystem.Inadditiontothesourceand

destination,thepatternisalsoanarrayof1-bitpixels;visualize

thepatternoverlayingadestinationsurface.Table8-2shows

selectionsfromthe256waysinwhichthe0and1pixelsofthe

pattern,source,anddestinationcanbecombined.

Table8-2.Ternaryrasterops

PossibleCombination

Inputparameter



Inputvalue



Pattern(P):



11110000



Source(S):



11001100



Operation



Output



Rasteroperation0x00:



00000000



Rasteroperation0x01:



00000001



Rasteroperation0x02:



00000010











Rasteroperation0x60:



01100000











Rasteroperation0xFD:



11111101



Rasteroperation0xFE:



11111110



Rasteroperation0xFF:



11111111



Thistableshowssampleinputsfollowedbytheresulting

destinationsfor7ofthe256possibleternaryrasteroperations.

Eachoftheserasteroperationscanbeidentifiedbyaone-byte

hexadecimalnumbercorrespondingtothepatternofresultant

destinationbitsshowninthetable.Forexample,forraster

operation0x60,ifthepatternpixelis1(white),thesourcepixel

is0(black),andthedestinationpixelis1,thedestinationwill

be1(white).

IntheearlyversionsofWindows,15ofthetotal256raster

operationswereidentifiedwithnamesinboththe

documentationandtheWindowsheaderfilethatC

programmersused.Thefirst—wherethedestinationiscolored

withall0sregardlessofthepattern,source,anddestination—

wasknownasBLACKNESS;thelastwascalledWHITENESS.

TheWindowsprogrammingreferenceidentifiedall256raster

operationsbythebitwiseBooleanoperationtheyperformed,

expressedinreversePolishnotation.Forexample,raster

operation0x60correspondstotheBooleanoperationPDSxa.

ThismeansthatanExclusive-OR(x)operationisperformed



betweenthedestination(D)andthesource(S),andtheresult

iscombinedwiththepattern(P)inabitwiseANDoperation(a).

Incolorsystems,thesameBooleanoperationisperformed

amongthecolorbitsofthesource,destination,andpattern.As

ofthiswriting,theserasteroperationsaredocumentedonline

athttp://msdn.microsoft.com/library/enus/gdi/pantdraw_6n77.asp.

Someoftheserasteroperationsarequiteusefulincertain

circumstances.Forexample,youmightwanttoinvertthe

destinationpixelsthatcorrespondtoareaswhereabrushis

black,butdisplayasourcebitmapinareaswherethebrushis

white.That'srasteroperation0xC5.Ofcourse,manyofthe256

possibilitieshavelittlepracticaluse,andIsuspectthatmostof

themhaveneverbeenusedoutsideofdemonstrationor

exploratorycode.Still,thesenseofcompletenessand

versatilityisquitesatisfying.

IfwewereimplementingthisversatileBitBltfunctionourselves,

howwouldwedoit?Assumethatit's1985andwe'reusingthe

Cprogramminglanguage.Forillustrativepurposes,let'salso

assumethatwe'redealingwithaone-byte-per-pixelgrayshade

graphicssystem,andthatthesource,destination,andpattern

canbeaccessedthroughtwo-dimensionalarraysnamedS,D,

andP.Thatis,eachofthesevariablesisapointertoa

collectionofbytepointers,andeachbytepointerpointstothe

beginningofahorizontalrowofpixels,sothatS[y][x]

accessesthebyteatrowyandcolumnx.Thewidthandheight

oftheareayou'reworkingwithisstoredincxandcy(thisisa

traditionalWindowsprogrammingvariable-namingconvention:

thecstandsforcount,sothesevariablesindicateacountofx

andyvalues,orwidthandheight).Theropvariablestoresa

rasteroperationcodefrom0to255.

Here'ssomesimpleCcodetoimplementusingBitBlt.Aswitch

statementusesroptodeterminewhichoperationisperformed

tocalculatethepixelvaluesinthedestination.Only3ofthe



256rasteroperationsareshownhere,butyougetthegeneral

idea:







































for(y=0;y
for(x=0;x
{

switch(rop)

{

case0x00:

D[y][x]=0x00;

break;

...

case0x60:

D[y][x]=(D[y][x]^S[y][x])&P[y][x];

break;

...

case0xFF:

D[y][x]=0xFF;

break;

}

}



Thiscertainlyisprettycode,whichmeansthatit'snicetolook

atandcertainlycrystalclearinitsintentionsandfunctionality.

Butbeautifulcodeitisnot,becausebeautifulcodeisalso

satisfyingwhenyourunit.

Thiscodeisactuallyadisasterbecauseitdealswithbitmaps,

andbitmapscanbehuge.Thesedays,bitmapsthatcomeout

ofinexpensivedigitalcamerascanhavemillionsofpixels.Do

youreallywantthatswitchstatementtobeinsidetherowand

columnloops?

Shouldtheswitchlogicbeexecutedforeachandeverypixel?

Probablynot.Movingtheloopsinsideeachcasecertainly

cluttersupthecode,butnowatleastithasafightingchanceof



reasonableperformance:











































switch(rop)

{

case0x00:

for(y=0;y
for(x=0;x
D[y][x]=0x00;

break;

...

case0x60:

for(y=0;y
for(x=0;x
D[y][x]=(D[y][x]^S[y][x])&P[y][x];

break;

...

case0xFF:

for(y=0;y
for(x=0;x
D[y][x]=0xFF;

break;

}



Ofcourse,ifitreallywere1985andyouwerewritingWindows,

youwouldn'tevenbedoingitinC.EarlyWindowsapplications

weremostlywritteninC,butWindowsitselfwaswrittenin

8086assemblylanguage.

ForsomethingasimportanttoWindowsasBitBlt,anevenmore

radicalsolutionwasrequired—somethingevenfasterthan

assemblylanguage,asincredibleasthatmayseem.The

MicrosoftprogrammerswhoimplementedBitBltwerequite

proudofwhattheyhaddone,andthoseofuslearningWindows

programminginthemid-1980swereequallyimpressedwhen

theybraggedoftheirachievement.

TheBitBltfunctionactuallycontainedaminicompilerofsorts.



Basedontherasteroperation(aswellasthegraphicsformat,

thenumberofbitsperpixel,andthesizeofthearea),the

BitBltfunctionassembled8086machinecodeinstructionson

thestackintheformofasubroutineandthenexecutedit.This

makeshiftmachinecoderoutineloopedthroughallthepixels

andperformedtherequestedrasteroperation.

ItwastheperfectsolutiontoimplementingBitBltandits256

rasteroperations.Althoughthisminicompilerrequiredabitof

overheadtoputthemachinecodetogetheronthestack,the

per-pixelprocessingwasasfastaspossible,andthat'swhatis

mostimportantwhenworkingwithbitmaps.Moreover,the

BitBltcodeinWindowswasprobablymuchshorterthanitwould

havebeenhaditcontainedexplicitcodeforall256raster

operations.

Itwasevenpossibletogetalittleglimpseintotheworkingsof

thisBitBltminicompilerbyexaminingthedocumentationofthe

ternaryrasteroperations.Forexample,therasteroperation

identifiedbythenumber0x60implementsaBooleanoperation

ofPDSxa.WhencallingBitBlt,youactuallysupplya32-bitraster

operationcode,whichisdocumentedas0x00600365forthis

operation.Noticethe0x60byteembeddedinthatnumber,but

alsotakenotethatthebottomtwobytesformthenumber

0x0365.

Therasteroperationwiththeresultof11110110or0xF6has

theBooleanoperationPDSxo,whichisverysimilartoPDSxa

exceptthatitperformsanORoperationratherthanAND.The

complete32-bitrasteroperationcodepassedtotheBitBlt

functionis0x00F70265.Thebottomtwobytesformthenumber

0x0265,whichisveryclosetothe0x0365ofPDSxa.Ifyou

examinemoreofthese32-bitrasteroperationcodes,it

becomesveryobviousthattherasteroperationcodeitself

servesasatemplateofsortsfortheBitBltminicompilerto

assemblethepropermachinecode.ThattechniquesavesBitBlt

boththememoryandtimerequiredtousealookuptable.



Ofcourse,Windows1.0wascreatedover20yearsago.We

haveallmovedon,andsohasWindowsitself.Thesedaysmy

preferredprogramminglanguageisneitherassemblylanguage

norC,butC#.Iusuallywritewhat'scalledmanagedcodethat

runsundertheMicrosoft.NETFramework.TheC#compiler

turnsmysourcecodeintoprocessor-independentIntermediate

Language(oftenreferredtoasMicrosoftIntermediate

Language,orMSIL).Onlylater,whentheprogramisrun,does

the.NETCommonLanguageRun-timeuseajust-in-time

compilertoconvertthatIntermediateLanguageintomachine

codeappropriatefortheruntimeprocessor.

Andyetdigitalimageprocessingofallsortsstillcriesoutfor

unusualapproachestocoding.Whenworkingwithmillionsof

pixels,theper-pixelprocessinghastobefast,fast,fast.For

commercialproducts,youwillprobablywanttohirean

assemblylanguageprogrammerorsomeonewhoknowshowto

targettheGraphicsProcessingUnit(GPU)foundonvideo

boards.Evenforcasualornoncommercialsoftware,youwill

probablywantsomethingfasterthanthenormalhigh-level

languageloop.

Iwasrecentlyremindedoftheon-the-flycodegenerationofthe

originalWindowsBitBltfunctionwhileexperimentingwithsome

C#codetoimplementdigitalimagefilters,alsocalledimage

filtersordigitalfilters.Rasteroperationssuchasthose

implementedintheWindowsBitBltandStretchBltfunctions

applyonlytocorrespondingpixelsofasource,destination,and

pattern.Digitalfilterstakesurroundingpixelsintoaccount.You

applyaparticulardigitalfiltertoabitmaptochangeitinsome

way,perhapstosharpentheedgesorevenblurtheoverall

image.Ablurfilter,forexample,averagesagroupof

surroundingpixelstocalculateadestinationpixel.

Simpledigitalimagefiltersareoftenimplementedassmall

arraysofnumbers.Thesearraysareusuallysquareandhave

anoddnumberofrowsandcolumns.Figure8-1showsasimple

example.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 8. On-the-Fly Code Generation for Image Processing

Tải bản đầy đủ ngay(0 tr)

×