Computers & GeosciencesVol. 18, No. 9, pp. 1213-1253,
19920098-3004/92 $5.00 + 0.00 Printed in Great Britain. All rights
reservedCopyright 1992Pergamon Press Ltd
COMPRESSIONOFDIGITIZEDMAPIMAGES DAVID A.SOUTHARD
TheMITRECorporationE073, 202BurlingtonRoad,Bedford,MA01730-1420,
U.S.A. (ReceivedI0February1992; accepted30April1992) Abstract--An
efficient way to storeandretrieve digitized chartimages using
alossy vector quantization
0/Q)compressiontechniqueispresented.AVQcodebookislocatedusingthefastpairwisenearest
neighbor(PNN)clustering algorithm.Ak-dtree datastructureisused for
efficient image compression. Some uniquefeatures of this
approachare that compressed files use a predictable amountof
storage, and
canbedecompressedquicklyfordisplayonequipmentrangingfromportablepersonalcomputersto
advancedgraphicsworkstations.Themethodalsoappliestopanchromaticandmultispectralsatellite
imagery. In this application, digitized chart images with
compression ratios of 24: 1 exhibited good quality.
Incertainapplications,even highercompressionratiosarefeasible.
KeyWords:Digital mapping,Image compression, Vector
quantization,Clustering algorithms, k-dtree. INTRODUCTION
Thisinvestigationinto image compression techniques wasmot i vat
edbytechnicalissuessurroundingthe designof anaut omat
edmissionplanningprogram termedtheAirForceMissionSupport System( AF
MSS)(Southard, 1992).Thedisplayof digitized
navigationalchartsiscentralt othefunctionof this system.
TheDefenseMappi ngAgency(DMA), supplies digitizedchartsoncompact
diskread-onlymemory (CDROM) media.Thesegeoreferenceddigitalim-
agesaretermedEqualArc-secondRasterChart(ARC)DigitizedRasterGraphics(
ADRG) (Defense Mappi ngAgency,1989).Itwasestimatedt hat AF MSS
wouldneedon-line about 200 gigahytes
(GB)ofADRGdata,initsrawform.Imagecompressionis onestrategyt hat
willbeusedtoreducethiscostly
storagerequirement.Thetechniquesaredescribed
andevaluated,andasetofalgorithmst hat meetsthe
goalssetbyAFMSSarepresented.AnANSI C
sourcecodeforthesealgorithmsisprovided. Applications
Themethodsselectedhavebroadapplicabilityto manyaut omat edmapping,
charting,andgeodesy systems.Digitizedchartscanreplacepaperchartsin
manydisplayapplications.Digitalmapping,remote
sensingandanalysis,computer-aideddesignfor civil
engineering,architecture,andenvironmentalanaly- sis,global
positioning andnavigation,and geographi- cali nformat i
onsystems(GIS),aresomeexamples.
Digitizedchartimagesareusefulinmilitarymission planningandcommand,
control,andcommuni- cations(C 3)systems. Imagecompressiontechniques
Compressiontechniquescanbeclassifiedintotwo br
oadcategories:lossless,andlossy.Losslessalgor-
ithmsincludesuchwell-knowntechniquesasrun- length coding, quadtree
coding, Huffman coding, and theLempei-Zivalgorithm,whichisusedbythe
UNI Xcompr essutility(Welch,1984).Alossless
algorithmreconstructscompresseddat aexactly toits
originalstate.Thedegree ofcompression dependson thecontentof
thedata.Ontypicalchart images,
losslesstechniquescanachieveuptoabout a2 :
1compressionratio(theratiooftheoriginalimagesize
tothecompressedimagesize).Tablelshowsthe compressionof
selectedchartandsatelliteimages usingrun-length,Huffman, andLempel
-Ai zcoding. I nsomesituations,however,thesecodingschemes
increasedthesizeof theimages. Lossy
algorithmsadmithighercompressionratios.
Lossyalgorithmsincludedifferentialpulsecode modul at i on(DPCM),
vector quantization(VQ),and thediscretecosinet
ransform(DCT),whichisthe centraltechniqueinthepopul arJ PEGandMPEG
standards(RabbaniandJones,1991).Lossyalgor-
ithmswilldegradetheoriginalimagesomewhat,
dependingontheimagecontent,theamount
ofcompression,andthepropertiesofthealgorithm.In
manysituations,however,somedegradationisper-
missible.Thelossmaybevisuallyimperceptible.
Theclassificationandcompressiontechniquest hatare presentedare
appropriatefor display andanalysis of manytypesof
scientificandengineeringimages. Thelossycharacterof
thesealgorithms,however, wouldmaketheminappropriateforarchivaldat a
storage,orforsituationst hat requirecompleteaccu-
racy.IntheseinstancesIosslessalgorithmsmust be employed.For
example,DigitalTerrainElevation Dat
a(DTED)suppliedbytheDMAforhillyterrain in
centralGermanycanbecompressedtoanaverage compressionratioof
1.7usingHuffmancoding,and 3.4 using Lempel-Ziv coding.It would be
inappropri- atetousealossytechnique,becausethiswould 1213
1214D.A.SOUTHARD Table1.Compressionratiosforlossless compressionof
selected images Lossless compressionmethod
ImageSizeRunlengthHuffmanLempel-Ziv ONC5125120.971.060.99 TPC*1000
x10000.981.101.00 TPC500x5000.971.251.27 JOG1024 10241.021.121.12
TLM512x5120.981.111.06 SPOTt2391x22491.011.711.93
SPOT:~2391x22490.981.040.99 Unlessindicated,imageshavebeen
spatiallyfilteredandsubsampled. *RawADRGimage. tRawpanchromatic
image,withlittle contrast.
~Processedwithcontraststretch,andspatiallyfilteredtoremoveimaging
artifacts. compr omi sethecharact erof thedat a. Arelief- shadedi m
a g e of thesedat a, however,canbecom- pressed successfully using
the met hods described here. V e c t o r q u a n t i z a t i o
nVect orquant i zat i onisageneralmet hodt hat is
usedextensivelyforappl i cat i onsinspeechcoding,i magecoding,
segment at i on, classification,andrec- ogni t i onprobl ems.
Therearemanyformsof VQ,as wellashybri dsof VQwithot her
techniques(Abut ,1990;Rabbani andJones,1991).SeveralformsofVQ, al
ongwithot her compressi onschemes,have beenpr oposedforcompressi
ngdi gi t alchart s(Barad andMart i nez, 1989;Jai si mhaandothers,
1989; Lohr enzandothers, 1990;Pot l apal l i andothers, 1989;Pot l
apal l i , Barad, andMart i nez, 1989).Inthis work, severalcompet i
ngVQal gori t hmsweretested, eval uat i ngt hemwithanemphasi soni
magequal i t y andprocessi ng speed,andthe best techniquesforthis
appl i cat i onwereapplied.I n VQ, ani mage is di vi ded i nt
osmall units,t ermed v e c t o r s . I nthiscontext, thet ermv e c
t o r simply refers toann-t upl e; t hereisnogeomet ri ci nt erpret
at i on.Thevect orscompri seagr oupof adj acent i mage pixels.Typi
cal l y, avect orisasmallrect angul ar gr oupi ngof f r
om2to25pixels.Suppose, fora moment , t hat wehaveapredefinedlistof
vectors, namedt hec o d e b o o k . Tocompress, t heent ry is
deter- mi nedinthecodebookt hat most closelymat ches eachvect
orfromthei nput image.Inot her words,eachi magevect orisclassifiedi
nt oacat egoryrep- resentedbyacodebookentry. Theindexof each ent r
yissavedinthecompressedimage.Todecom- press,eachindexisusedtol ook-
upavect orinthe codebookandreassembl etheimage.Vectorquan- t i zat
i onhasseveralsalientcharacteristicst hat dis- t i ngui shi t f r
omtheot her compressi onal gori t
hms,andwhichmakeVQideallysuitedforaut omat ed mappi ngsystems: A s
y m m e t r i c . Near l yallthecomput at i onassoci- at
edwithcompressi onanddecompressi onfalls on thesideofcompressi on.
Imagereconst ruct i onisa fastt abl el ook- upoperat i on. Most ot
hercom- pressional gori t hmstendtorequireabout asmuch comput
etimefordecompressi onasforcom- pression(cf.Rabbani andJones,1991).
E f f i c i e n t . Thecompressi onstagecompri sestwo steps:
codebook devel opment ,andthe compressi on
itself.Oncethereisacodebookforaclassofimages,suchasforapar t i cul
ar t ypeof chart , the codebookcanbereusedtocompressanyi magein t
hat class.Al t houghcodebookdevel opment isin- tensive comput at i
onal l y,compressi on is simple and quick. P r e d i c t a b l e .
Thecompressi onrat i odoesnotchange from i mage toimage.The size
ofthe vectors andthelengthof t hecodebookdet ermi net he amount of
compression. Thispr oper t ysimpli- fiesdiskspacemanagement
onaheavilyl oaded system. F l e x i b l e . Thedecompressi on
canbet ai l ored tothe localresources,rangi ngfromaPCwithaSuper-
VGAcard, toasupergraphicswor kst at i onwitha hi gh-resol ut i
ondisplay. Thesamecompressedfile
canbedecompressedforagray-scaledisplay, an 8-bit (256colors) l ook-
upt abl edisplay,or a24-bit RGB(16.8mi l l i oncolors)display. Thi
sadapt a-bi l i t y requires onl y t hat we modi f y the VQl ook-
up t abl easappr opr i at e. Thereisnoneedtocompress
theimagedifferentlyforeachsi t uat i on.R a n d o m - a c c e s s .
Manyal gori t hmsrequiredecom- pressiontocont i
nuefrombeginningtoendina predet ermi nedway.Themet hoddescri bedcan
begin atany poi ntinthe compressedimage, andgo inanydirection.F i l
t e r i n gDigitizedchart imagesmaycont ai ntwointerest- ing,but
undesi rabl e, artifacts.Chart susuallyare pr i nt edusingonl
yafewcol orsof ink.Int ermedi at e shadesarereproducedwiththehal
f-t onepri nt i ng process,inwhichar r aysof tinydot sof
inkreplicate i nt ermedi at etonesof color. Whendigitizedwithan opt
i cal scanningprocess, thehal f-t onedareas appears t i p p l e d .
Thestipplecanbestbedescribedasa Compressionof digitized
mapimages1215 7Y vqntvqoo.pvq,xp@ vq' .p vq,n,,@ Figure1.Overview
of vectorquantizationimage compression.
salt-and-peppertextureinthebackgroundareas.The cart
ographeroriginallyintendedtheseareastol ook likeaconst ant shadeof
color. Thesecondartifactisaninteractionof thehalf- tone
printingscreens withthe implicit arrayofoptical
samples,whichproducesamoir# patterninthese
areas.Thiseffectisunderst oodasabeatingof the spatialfrequencyof
thepixelarrayagainstthoseofthehalf-tonescreens.Themoir6patterncanbe
distinct. I f eitherof theseartifactsarepresent,theimage
shouldbefilteredbeforecompression.Byfiltering
beforecompression,stippleandmoir6canbeelimi-
nated,andthelegibilityof thedecompressedimage
increased.Filteringmaybeaccomplishedbyreplac-
ingeachpixelbyaweightedaverageofpixelsinits immediatenei ghborhood,
asdiscussedbyPratt (1991)orbyGonzalezandWintz(1987).Eight
differentfiltersweretested,andthebestresultswere
obtainedwithaseparable,piecewisecubicfilterofMitchellandNetravali(1988).Aseparablefilter
responsefunctionintwodimensionsisF( x, y) = f (x)f (y),
wherexandyrepresentthehorizontaland
verticaldistancesfromasamplepoint,measuredin
pixelintervalsfromthesamplepoint.Tofilter,the
weightedsumofallpixelst hat fallwithinthefilter's
radiusiscalculated,wheretheweightsaredeter-
minedbythefilterresponsefunction,thenare
normalizedbydividingbythesumof theweights.
Separablefilterscanbeusedbot hforsubsampling
andinterpolatingimages.Because they are calculated
usinganalyticalfunctions,eitherintegralornon-
integralsamplingfatorscanbeused.Theequation
forMitchellandNetravali' sfamilyof cubicfilters is: whereB andCare
parameterst hat define aparticular
cubicspline.MitchellandNetravalirecommended thevalues(B,C)=(1, !)
3"Overview ofVQcompression
Figure1illustratestheimagecompressionpro-
cedure.Theimageisconvertedfromthedistribution
mediatothelocalfilesystem.Afterfiltering,if
needed,arepresentativesubset ofvectors is extracted,
andassembledintoatrainingimage.Thecodebook developmentprogram,
vqinit,analyzesthetraining imaget odetermineacodebook.
Then,theprogram vqeompusesthecodebook tocompressoneormore
imagesfromtheclassrepresentedbythetraining
image.Asanoptionalseparatestep,vqinitgenerates
acolortable,andvqcmapusesthecolortableto color-mapthecodebook.
Thedecompressionpro- gram,vqexp(VQ-expand),takesthecodebookand the
compressed image,andassemblesan image file. A displayprogram,
specifictothelocalenvironment,
readstheimage,andforcolor-mappedimages,also
readsthecolortable,andwritestheimagetothe comput erdisplay.
CODEBOOKDEVELOPMENT Thekey toVQis the methodby which acodebook
isobtained.Thecodebookdeterminesthequalityofthereconstructedimage.Thecodebookislocatedby
aprocesstermedclustering. Oneormoretraining
imagesisanalyzedtolocateacodebookt hat will
minimizeadistortionmeasureforthecompressed
image.Thetrainingimagesrepresenttheclassofimagest
obecompressedwiththecodebook. The 1 ((12-9B -6C) l xl3 +( - 18 +12B
+6C) l xl2 +(6 -2B) f ( x) =g ~ ( - B -6C)1xl3 +(6B+30C)lxl2+ ( -
12B-48C)lxl+(8B+24C) ifIxl n o d e . s p l i t . i n d e x =-1; t r
e e - > n o d e . s p l i t . l e f t =N U L L ;t r e e - > n
o d e . s p i l t . r i g h t =N U L L ;b r e a k ;d e f a u l t :f
p r i n t f (s t d e r r ,e x i t (E R R O R 1; }r e t u r n (t r e
e 1; /*i n i t i a l i z e s p l l t n o d e * /E r r o r i s n e w
n o d e : u n k n o w n n o d e t y p e \ n " ); / *. . . . . . . .
. . . . . . . . . . . . . . * //*n e w c l u s t e r */ / * . . . .
. . . . . . . . . . . . . . . . . . * /* n e w c l u s t e r (/*a l
l o c a t e a n d I n l t an e w c l u s t e r * /l o n g h u m ,
/*n u m b e r o f v e c t o r s a c c u m u l a t e d */ l o n g e
r r , /*t o t a l e r r o r I n c l u s t e r */ s h o r t * v e c
t o r )/*p o i n t e r t o b l o c k o f s i z e c l u s t e r _ s
l z e * /C l u s t e r *c;/*p o i n t e r t o n e w c l u s t e r *
/c=( C l u s t e r *)m y a l l o c (s l z e o f ( C l u s t e r ) )
;c - > n =h u m ;c - > e r r o r = e r r ;c - > a r r a y
= ( s h o r t *)m y a l l o c (c l u s t e r _ s l z e *s l z e o f
(s h o r t )1; m e m c p y (c - > a r r a y , v e c t o r , c l
u s t e r _ s l z e *s i z e o f (s h o r t )1; n _ c l u s t e r s
+ + ;r e t u r n (c1; / *. . . . . . . . . . . . . . . . . . . . .
. * //*m a t c h */ I *. . . . . . . . . . . . . . . . . . . . . .
* I# d e f i n e N O T _ F O U N D (-11 l o n g m a t c h ( K D T r
e es h o r t{r e g i s t e rr e g i s t e r* t r e e ,* v e c t o r
) /*s e a r c h b u c k e t f o r m a t c h l n g c l u s t e r *
//*p o i n t e r t o b u c k e t n o d e * //*p o i n t e r t o v e
c t o r t o m a t c h */ i; J ;/*o u t e r l o o p c o u n t e r :
c l u s t e r i n d e x */ /*i n n e r l o o p c o u n t e r : v e
c t o r e l e m e n t i n d e x */ f o r (I=0;inode.bucket.count;i
+ + ) {f o r (J=0;J n o d e . b u c k e t . c l u s t e r [ i] -
> a r r a y [ J] v e c t o r [ J])b r e a k ;) i f (J= = c l u s
t e r _ s l z e )r e t u r n (i); )r e t u r n ( N O T _ F O U N D
); I = / *. . . . . . . . . . . . . . . . . . . . . . * // * s p l
i t * // * . . . . . . . . . . . . . . . . . . . . . . *
/Compressionof digitizedmapimages1231 K D T r e e{* s p l i t ( K D
T r e e * t r e e ) /*s p l i t ab u c k e t i n t o t w o p i e c
e s */ r e g i s t e r i;/*l o o p v a r i a b l e */ r e g i s t e
r il;/*l e f t b r a n c h c l u s t e r c o u n t e r */ r e g i s
t e r Jr;/*r i g h t b r a n c h c l u s t e r c o u n t e r */ l o
n g m a x i n d e x ; /*m a x r a n g e o r d i n a t e i n d e x *
/l o n g m a x r a n g e ; /*m a x o r d i n a t e r a n g e */ K D
T r e e * n e w t r e e ; /*n e w t r e e n o d e */ l o n g r a n
g e ; /*c u r r e n t o r d i n a t e r a n g e * /l o n g t h r e
s h o l d ; /*s p l i t t h r e s h o l d */ / *. . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . * //*F i n d t h e o r d i n a t e w i t h t h
e g r e a t e s t v a r i a n c e * //, . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . ,/ m a x i n d e x =0; m a x r a n g e =t r e e - >
n o d e . b u c k e t . m a x [ 0]-t r e e - > n o d e . b u c k
e t . m i n [ 0]; f o r (i=1;i n o d e . b u c k e t . m a x [ i]-t
r e e - > n o d e . b u c k e t . m l n [ i]; i f (r a n g e
>m a x r a n g e ) {m a x i n d e x =i; m a x r a n g e =r a n g
e ;})1". . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . * //*I f c l u s t
e r s a r e i d e n t i c a l , c o n d e n s e i n t o o n e */
/*c l u s t e r a n d r e t u r n i m m e d i a t e l y */ l *. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . " 1i f ( m a x r a n g e = = 0)
{f o r (i=1;i n o d e . b u c k e t . c o u n t ;i + + ) {t r e e -
> n o d e . b u c k e t . c l u s t e r [ 0] - > n + =t r e e
- > n o d e . b u c k e t . c l u s t e r [ i]->n; t r e e -
> n o d e . b u c k e t . c l u s t e r [ 0] - > e r r o r +
=t r e e - > n o d e . b u c k e t . c l u s t e r [ i] - > e
r r o r ;f r e e c l u s t e r (t r e e - > n o d e . b u c k e
t . c l u s t e r [ i]); )t r e e - > n o d e . b u c k e t . c
o u n t =I; r e t u r n (t r e e ); )/ *. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . * //*O t h e r w i s e , a l l o c a t e an e w s p l i
t n o d e , * //*a n d t w o n e w b u c k e t n o d e s , i n i t
i a l i z e * // *. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . * /t h r
e s h o l d =(t r e e - > n o d e . b u c k e t . m a x [ m a x
i n d e x ]+ t r e e - > n o d e . b u c k e t . m i n [ m a x i
n d e x ])/2; n e w t r e e =n e w n o d e (S P L I T ); n e w t r
e e - > n o d e . s p l l t . l n d e x =m a x l n d e x ;n e w
t r e e - > n o d e . s p l i t . t h r e s h =t h r e s h o l d
;n e w t r e e - > n o d e . s p l i t . l e f t =n e w n o d e
(B U C K E T ); n e w t r e e - > n o d e . s p l l t . r i g h
t =n e w n o d e (B U C K E T ); / *. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . * //*P a r t i t i o n s e x i s t i n g c l u s t e r s t
o l e f t o r r i g h t */ /*b r a n c h e s a c c o r d i n g t o
t h r e s h o l d t e s t . */ / *. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . * /i l =i r =0; f o r (i=0;i n o d e . b u c k e t . c o u n
t ;i + + ) {i f (t r e e - > n o d e . b u c k e t . c l u s t e
r [ i] - > a r r a y [ m a x i n d e x ]> t h r e s h o l d )
{n e w t r e e - > n o d e . s p l i t . r l g h t - > n o d
e . b u c k e t . c l u s t e r [ J r + + ]= n e w c l u s t e r (
t r e e - > n o d e . b u c k e t . c l u s t e r [ i]->n, t
r e e - > n o d e . b u c k e t . c l u s t e r [ i] - > e r
r o r ,t r e e - > n o d e . b u c k e t . c l u s t e r [ i] -
> a r r a y ); )e l s e{n e w t r e e - > n o d e . s p l i t
. l e f t - > n o d e . b u c k e t . c l u s t e r [ i f + + ]=
n e w c l u s t e r ( t r e e - > n o d e . b u c k e t . c l u
s t e r [ i]->n, 1232D,A.S O U T H A R Dt r e e - > n o d e .
b u c k e t . c l u s t e r [ i] - > e r r o r ,t r e e - > n
o d e . b u c k e t . c l u s t e r [ i] - > a r r a y ); }}n e
w t r e e - > n o d e . s p l i t . l e f t - > n o d e . b u
c k e t . c o u n t =il; n e w t r e e - > n o d e . s p l l t .
r l g h t - > n o d e . b u c k e t . c o u n t =Jr; / * . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . * /I*R e s e t t h e m a x / m l n
a r r a y s f o r b o t h b u c k e t s * // * . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . * /s e t m a x m i n ( n e w t r e e - > n o
d e . s p l i t . l e f t ); s e t m a x m i n ( n e w t r e e -
> n o d e . s p l i t . r i g h t ); I *. . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . * I/*D e - a l l o c a t e b u c k e t n o d e , a
n d */ /*r e t u r n n e w s p l i t n o d e . */ I *. . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . , if r e e n o d e (t r e e ); r e t u r
n ( n e w t r e e ); / *. . . . . . . . . . . . . . . . . . . . . .
* //*a p p e n d */ / *. . . . . . . . . . . . . . . . . . . . . .
* /v o i d a p p e n d ( K D T r e el o n gl o n gs h o r t{ * t r
e e ,n u m ,err, * v e c t o r ) r e g i s t e r i;/*n e w c l u s
t e r i n d e x */ i=t r e e - > n o d e . b u c k e t . c o u n
t + + ;t r e e - > n o d e . b u c k e t . c l u s t e r [ i]=n
e w c l u s t e r ( n u m , err,v e c t o r ); i f (i= = 0)/*b u c
k e t w a s e m p t y */ ( )e l s e( /*a p p e n d o n e c l u s t
e r t o b u c k e t */ /*p o i n t e r t o b u c k e t n o d e */
/*n u m b e r o f v e c t o r s r e p r e s e n t e d */ /*m a x s
q u a r e d e r r o r f o r c l u s t e r */ /*v e c t o r t o b e
a p p e n d e d */ s e t m a x m i n (t r e e );I*i n i t i a l i z
e m a x l m l n a r r a y s *I /*u p d a t e m a x / m l n a r r a
y s */ u p d m a x m i n (t r e e - > n o d e . b u c k e t . m
a x , t r e e - > n o d e . b u c k e t . m i n ,v e c t o r );
/ *. . . . . . . . . . . . . . . . . . . . . . * //*s e t m a x m i
n */ / * . . . . . . . . . . . . . . . . . . . . . . * /v o i d s e
t m a x m l n (I*s e t t h e m a x / m l n a r r a y s i n ab u c k
e t *I K D T r e e * t r e e )/*p o i n t e r t o B u c k e t t y p
e t r e e n o d e */ { r e g i s t e r i;/*l o o p c o u n t e r o
v e r c l u s t e r s i n b u c k e t */ i f (t r e e - > n o d
e . b u c k e t . c o u n t >0) { m e m c p y (t r e e - > n
o d e . b u c k e t . m a x ,t r e e - > n o d e . b u c k e t .
c l u s t e r [ 0] - > a r r a y ,c l u s t e r _ s i z e *s l z
e o f (s h o r t )); m e m c p y (t r e e - > n o d e . b u c k
e t . m i n ,t r e e - > n o d e . b u c k e t . c l u s t e r [
0] - > a r r a y ,c l u s t e r _ s i z e *s l z e o f ( s h o r
t )); f o r (i=i;i n o d e . b u c k e t . c o u n t ;i + + ) { u p
d m a x m i n (t r e e - > n o d e . b u c k e t . m a x ,t r e
e - > n o d e . b u c k e t . m i n ,t r e e - > n o d e . b
u c k e t . c l u s t e r [ i] - > a r r a y ); ) / *. . . . . .
. . . . . . . . . . . . . . . . * //*u p d m a x m l n */ I *. . .
. . . . . . . . . . . . . . . . . . . * Iv o i d u p d m a x m l n
( s h o r t * m a x ,s h o r t * m i n ,s h o r t * v e c t o r )
/*u p d a t e t h e m a x / m i n a r r a y s i n ab u c k e t */
/*a r r a y o f m a x v a l u e s */ /*a r r a y o f m i n v a l u
e s */ /*n e w c l u s t e r v e c t o r */ Compressionof
digitizedmapimages1233 r e g i s t e r i ; / * l o o p i n g c o u
n t e r * /f o r (i=0;ic o d e b o o k _ l e n ) ( t r e e =b a l a
n c e (t r e e ); c a n d i d a t e s =( C a n d i d a t e *)m y a
l l o c (nb u c k e t s * s i z e o f ( C a n d i d a t e ) ) ;nc a
n d l d a t e s =f i n d _ c a n d i d a t e s (t r e e , c a n d i
d a t e s )- c a n d i d a t e s ;q s o r t (c a n d i d a t e s ,
n _ c a n d l d a t e s , s i z e o f (C a n d i d a t e ), c o m p
a r e _ c a n d l d a t e s ); n _ c a n d i d a t e s =n _ c l u s
t e r s -c o d e b o o k _ l e n n o d e . s p l l t . r l g h t ;i
f (t l - , t y p e = = B U C K E T & & t l - > n o d e .
b u c k e t . c o u n t n o d e . b u c k e t . c l u s t e r [ i];
t r =i n s e r t (t r , c - > n , c - , e r r o r , c - > a r
r a y ); ) t r e e - > n o d e . s p l l t . r l g h t =N U L L
;f r e e n o d e (t r e e ); t r e e =t r ;) e l s e i f (t r -
> t y p e = = B U C K E T & & t r - > n o d e . b u c
k e t . c o u n t n , c - > e r r o r , c - > a r r a y ); }
t r e e - > n o d e . s p i l t . l e f t =N U L L ;f r e e n o
d e (t r e e ); t r e e =tl; /*r e c u r s i v e d e s c e n t o f
t r e e */ tree->node.spilt.left t r e e - > n o d e . s p l
l t . r l g h t} } r e t u r n (t r e e ); =b a l a n c e (tl); =b
a l a n c e (t r ); / *. . . . . . . . . . . . . . . . . . . . . .
* //*f r e e n o d e */ I* . . . . . . . . . . . . . . . . . . . .
. .*i v o i d f r e e n o d e (/*f r e e m e m o r y a l l o c a t
e d f o r K D T r e e */ K D T r e e * t r e e )/*p o i n e r tot r
e e */ { r e g i s t e r i;/*l o o p i n g c o u n t e r */ i f (t
r e e ]=N U L L )s w i t c h (t r e e - > t y p e ) { c a s e B
U C K E T :f o r (i=0;i n o d e . b u c k e t . c o u n t ;i + + )
{ f r e e c l u s t e r (t r e e - > n o d e . b u c k e t . c l
u s t e r [ i]); ) f r e e (t r e e - > n o d e . b u c k e t .
c l u s t e r ); f r e e (t r e e - > n o d e . b u c k e t . m
a x ); f r e e (t r e e - > n o d e . b u c k e t . m l n ); ' f
r e e (tree); n _ b u c k e t s - - ;b r e a k ;c a s e S P L I T
:f r e e n o d e (t r e e - > n o d e . s p l i t . l e f t ); f
r e e n o d e (t r e e - > n o d e . s p i l t . r i g h t ); f
r e e (t r e e ); b r e a k ;d e f a u l t :f p r l n t f (s t d e
r r , ' E r r o r i n f r e e n o d e : u n k n o w n n o d e t y p
e \ n " ); e x i t ( E R R O R ); ) / *. . . . . . . . . . . . . .
. . . . . . . . * //*f r e e c l u s t e r * /i, . . . . . . . . .
. . . . . . . . . . . . .,i v o i d f r e e c l u s t e r ( C l u s
t e r * c l u s t e r ) { f r e e (c l u s t e r - > a r r a y
); f r e e (c l u s t e r ); n _ c l u s t e r s - - ;) / *. . . .
. . . . . . . . . . . . . . . . . . * //*f i n d _ c a n d l d a t
e s */ /* . . . . . . . . . . . . . . . . . . . . . .* /C a n d i d
a t e * f l n d c a n d l d a t e s ( K D T r e e * t r e e ,C a n
d i d a t e * c a n d ) { /*f r e e ac l u s t e r a r r a y * //*p
o i n t e r t o c l u s t e r */ /*f i n d c a n d i d a t e c l u
s t e r p a i r s */ /*t r e e t o s e a r c h */ /*p o i n t e r i
n t o c a n d i d a t e a r r a y */ i f (t r e e = = N U L L ) ( f
p r i n t f (s t d e r r , ' E r r o r i n f l n d _ c a n d i d a
t e s : n u l l t r e e p o l n t e r \ n " ); e x i t ( E R R O R
); ) s w i t c h (t r e e - > t y p e ) { c a s e S P L I T :c a
n d =f l n d _ c a n d l d a t e s (t r e e - > n o d e . s p i
l t . l e f t , c a n d ); c a n d =f l n d _ c a n d i d a t e s
(t r e e - > n o d e . s p i l t . r i g h t , c a n d ); b r e
a k ;c a s e B U C K E T :i f (t r e e - > n o d e . b u c k e t
. c o u n t >i) { c a n d - > p t r =t r e e ;Compressionof
digitized mapimages 1235 c a n d - > e r r =c a n d i d a t e (t
r e e , & c a n d - > n o l , & c a n d - > n o 2 );
c a n d + + ;)b r e a k ;d e f a u l t :f p r l n t f (s t d e r r
, " E r r o r i n f l n d _ c a n d l d a t e s : u n k n o w n t r
e e t y p e \ n " ); e x i t (E R R O R ); )r e t u r n (c a n d
);I*r e t u r n p o i n t e r t o n e x t e m p t y s l o t * //*c
a n d i d a t e */ / * . . . . . . . . . . . . . . . . . . . . . .
* /l o n g c a n d i d a t e (/*f i n d i n d e x o f c a n d i d a
t e f o r b u c k e t * /K D T r e e * t r e e , /*b u c k e t n o
d e o f t r e e * /l o n g * m i n i , /*i n d e x o f f i r s t i
n c a n d i d a t e p a i r * /l o n g * m l n J )/*i n d e x o f s
e c o n d i n c a n d i d a t e p a i r * I{r e g i s t e r C l u s
t e r **c;/*c l u s t e r p o i n t e r a r r a y * /r e g i s t e
r i;/*l o o p i n g i n d e x * /r e g i s t e r J;/*l o o p i n g
i n d e x * /r e g i s t e r i;/*l o o p i n g l i m i t * /r e g i
s t e r 11;/*l o o p i n g l i m i t -1* /l o n g m l n e r r ; /*m
i n i m u m e r r o r e n c o u n t e r e d * /l o n g e r r o r ;
/*c u r r e n t e r r o r */ c=t r e e - > n o d e . b u c k e t
. c l u s t e r ; /*p o i n t e r t o c l u s t e r */ 1=t r e e -
> n o d e . b u c k e t . c o u n t ; /*l e n g t h o f c l u s
t e r */ i i =1-I;/*l e n g t h m i n u s o n e */ m l n e r r =L O
N G _ M A X ; /*ag i a n t n u m b e r * /f o r (i=0;i# i n c l
u d e < l i m l t s . h ># i n c l u d e < m a t h . h
>/*s t a n d a r d I/Of u n c t i o n d e f i n i t i o n s */
/*c o n t a i n s MAX_INT,etc.*I /*m a t h f u n c t i o n s l o g
(),etc.*/ / *. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. * //*M A N I F E S T C O N S T A N T S */ / *. . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . * /# d e f i n e B I T S 8 # d
e f i n e B U C K E T S I Z E 8 # d e f i n e E R R O R (-i) # d e
f i n e F A L S E 0 # d e f l n e M I N A R G S 3 # d e f i n e N O
R M A L 0 # d e f i n e T R U E (]FALSE) I*n u m b e r ofb i t s p
e r b y t e */ I*n u m b e r o f v e c t o r s p e r b u c k e t */
/*e r r o r r e t u r n code*/ /*logicalF A L S E */ /*mln.n u m b
e r ofa r g u m e n t s r e q u i r e d */ /*normalr e t u r n
code*/ /*logicalT R U E */ / *. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . * //*D A T A S T R U C T U R E S */ /*V e c t o r
Q u a n t i z a t l o n C o d e b o o k */ I *. . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . * It y p e d e f u n s i g n e d
c h a r uchar;I*ensureu n s i g n e d for p o r t a b i l i t y *I
t y p e d e f s t r u c tl o n g index; u c h a r *vector; }Code;
/*s t r u c t u r e d e f i n i n g asinglecode*/ /*i n d e x
ofthec o d e */ /*p o i n t e r tocodev e c t o r */ t y p e d e f
s t r u c t{ l o n g num; C o d e *book; C o d e B o o k ;/*s t r u
c t u r e d e f i n i n g ab o o k ofc o d e s */ /*n u m b e r
ofcodev e c t o r s inc o d e b o o k */ /*p o i n t e r toac o l l
e c t i o n ofC o d e s */ # d e f i n e CODE(J) # d e f i n e V A
L U E ( J , I )( c o d e b o o k . b o o k [ J ] . v e c t o r
)(CODE(J)[I]) / *. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . * //*S t r u c t u r e s forK - d treeu s e d inP N N s e a r
c h a l g o r i t h m */ /, . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . .*i t y p e d e f e n u m { BUCKET,S P L I T ) t y p
e d e f s t r u c t s p i l t _ n o d e{ l o n g disc; l o n g
part; s t r u c t k d _ n o d e *left; s t r u c t k d _ n o d e
*right; )Split; t y p e d e f s t r u c t k d _ D o d e{ No d e T y
p e type; u n i o n{ Splitsplit; c o d e B o o k bucket; }node;
NodeType; /*s t r u c t u r e ford e c i s i o n n o d e s */ /*i n
d e x ofd i s c r i m i n a t o r c o o r d i n a t e */ /*p a r t
i t i o n v a l u e ford i s c r i m i n a t o r */ /*for v e c t o
r s t h r e s h o l d */ /*s t r u c t u r e forK - D treen o d e s
*/ /*oneoftwot y p e s */ /*d e c i s i o n n o d e *I /*d a t a n
o d e *I }KDTree; / * . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . * //*F U N C T I O N P R O T O T Y P E S */ / * . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . * /l o n g b o b ( u
c h a r *); Compressionof digitized mapimages 1239 K D T r e el o n
gint l o n gl o n gv o i dl o n gK D T r e eK D T r e eC o d eC o d
e B o o kl o n gC o d e B o o kC o d e B o o kl o n g*build__tree(
C o d e B o o k ); b w b ( u c h a r *); c m p r ( C o d e *,C o d
e *); d i s c r i m ( C o d e B o o k ); d l s t ( long,u c h a r
*,u c h a r *); get__args( int,char**); m e d i a n o f ( long,C o
d e B o o k ); * n e w b u c k e t ( C o d e B o o k ); * n e w s p
l l t ( long,long,K D T r e e *,K D T r e e *); * m a t c h ( u c h
a r *,K D T r e e *); r d _ c o d e s ( F I L E *); s e a r c h ( u
c h a r *,K D T r e e *); s p l i t _ l e f t ( long,C o d e B o o
k ); s p l i t _ r l g h t ( long,C o d e B o o k ); s p r e a d o
f ( long,C o d e B o o k ); / *. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . r . . . . . . . .
. . . . . . . . . . * //*G L O B A L D A T A */ / *. . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . * /l o n gl o n gF I L Eu c
h a rC o d el o n gl o n gl o n gl o n gl o n gu c h a rl o n gb l
o c k _ h e i g h t ; I*h e i g h t ofv e c t o r b l o c k */ b l
o c k _ w l d t h ; /*w i d t h ofv e c t o r b l o c k */
*codef;/*c o d e b o o k inputfiles t r e a m */ *lower;/*lowerb o
u n d s fors e a r c h */ *matched;/*p o i n t e r tom a t c h e d
c o d e */ nearest;/ * i n t e r i m n e a r e s t c o d e m a t c
h i n d e x */ n_cols;/*n u m b e r ofc o l u m n s ininputi m a g
e */ n_rows;/*n u m b e r ofrowsininputimage*/ p i x e l _ d e p t
h ; /*sizeofp i x e l */ serno;/*c o d e b o o k serialn u m b e r
*/ *upper;/*u p p e r b o u n d s fors e a r c h */ v e c t o r
slze;/*sizeofcodev e c t o r */ / *. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . * // *P R O G R A M * / I *. . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . * Im a i n ( ( /*VQc o m p
r e s s i o n */ intargc,/*a r g u m e n t count*/ c h a r
*argv[])/*a r g u m e n t v e c t o r *I C o d e B o o k
codebook;/*thec o d e b o o k */ l o n g code_width;/*w i d t h inb
y t e s ofav e c t o r b l o c k */ u c h a r *cvq;/*(uchar)c o m p
r e s s e d r o w b u f f e r */ r e g i s t e r l o n g i;/*v e c
t o r b l o c k r o w i n d e x */ u c h a r
**image;/*rowsofinputimage*/ r e g i s t e r l o n g J;/*v e c t o
r b l o c k c o l u m n i n d e x */ r e g i s t e r l o n g k;/*p
i x e l b y t e (depth)i n d e x */ r e g i s t e r l o n g I;/*p l
x e l b y t e (depth)i n d e x */ l o n g n_bytes;/*b y t e s
ininputimager o w */ r e g i s t e r u c h a r *pl;/*p o i n t e r
intoi m a g e b u f f e r */ r e g i s t e r u c h a r *pv;/*p o i
n t e r intov e c t o r b u f f e r */ r e g i s t e r
short*pvq;/*p o i n t e r intoc o m p r e s s e d r o w b u f f e r
*/ s h o r t *svq;/*(short)c o m p r e s s e d r o w b u f f e r */
K D T r e e *tree;/*thek - d s e a r c h tree*/ u c h a r *v;/*v e
c t o r b u f f e r */ l o n g x;/*ximagei n d e x */ l o n g
xsize;/*xsizeofc o m p r e s s e d image*/ l o n g y;/*yimagei n d
e x */ l o n g yslze;/*ysizeofc o m p r e s s e d image*/ l o n g
zsize;/*zsize(depth)ofC o m p r e s s e d i m a g e */ / *. . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *
//*getc o m p r e s s i o n p a r a m e t e r s */ I * . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *
Ig e t _ a r g s ( a r g c , a r g v );/*c o m m a n d - l l n e a
r g u m e n t s */ c o d e b o o k = r d _ c o d e s ( c o d e f
);/*c o d e b o o k */ t r e e =b u i l d _ t r e e ( c o d e b o o
k );/*m a k e as e a r c h tree*/ / * . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . * //*c a l c u l a t
e d e r i v e d p a r a m e t e r s */ I *. . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . * Ix s l z e =n _
c o l s /b l o c k _ w l d t h ; /*c o m p r e s s e d c o l u m n
s */ y s l z e =n _ r o w s /block__height;/*c o m p r e s s e d
rows*/ zsize=( log( ( d o u b l e ) c o d e b o o k . n u m )/l o g
(2.0)+BITS-1)/BITS; /*b y t e s p e r c o d e */ n _ b y t e s =p l
x e l _ d e p t h *n_cols;/*sizeofi m a g e r o w b u f f e r */ c
o d e _ w l d t h =p l x e l _ d e p t h *b l o c k _ w l d t h ;
/*b y t e w i d t h ofab l o c k */ I *. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . * I/*w r i t e o u
t p u t fileh e a d e r i n f o */ / * . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . * /1240D. A. S o u
T ~c h a rf w r l t e ( & e e r n o , s i z e o f (l o n g
),i,s t d o u t );/*s e r i a l n u m b e r * /f w r l t e ( &
x s l z e , s l z e o f (l o n g ),I,s t d o u t );/*n u m b e r o
f c o l u m n s * /f w r l t e ( & y s l z e , s l z e o f (l o
n g ),1,s t d o u t );/*n u m b e r o f r o w s * /f w r i t e
(& z s l z e , s l z e o f (l o n g ),1,s t d o u t );/*b y t e
s p e r c o d e */ / * . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . * /I*a l l o c a t e b u f f e r m
e m o r y *I I *. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . * Iv=( u c h a r *)m a l l o c ( v e c t o
r _ s i z e *s l z e o f ( u c h a r ) ) ;u p p e r =( u c h a r
*)m a l l o c ( v e c t o r _ s l z e *s l z e o f ( u c h a r ) )
;l o w e r =( u c h a r *)m a l l o c ( v e c t o r _ s l z e *s l
z e o f ( u c h a r ) ) ;8 v q =( s h o r t *)m a l l o c ( x s i z
e *s i z e o f ( s h o r t ) ) ;c v q =( u c h a r *)m a l l o c (
x s l z e *s l z e o f ( u c h a r ) ) ;i m a g e =( u c h a r **)m
a l l o c ( b l o c k _ h e l g h t *s l z e o f ( u c h a r *)); f
o r (i=0;i v e c t o r [ k e y ]); /*f i n d i n d e x o f m e d
i a n v a l u e */ I*d i s c r i m i n a t o r c o o r d i n a t e
i n d e x */ /*p o i n t e r t o c o d e b o o k * //*i n d e x o f
m e d i a n */ k e y =d;I*s o r t o n k e y i n d e x e d b y d i s
c r i m . *I q s o r t (c o d e b o o k . b o o k , c o d e b o o k
. n u m , s l z e o f ( C o d e ),c m p r ); m=c o d e b o o k . n
u m /2+i;/*n o m i n a l i n d e x o f m e d i a n */ w h i l e (
m>0& & V A L U E ( m,d)= = V A L U E ( m-I,d)) {m - - ;
/*d e c r e m e n t w h i l e v a l u e s a r e e q u a l */ )r e t
u r n ( m-1);/*-i=n o s p l i t p o s s i b l e */ / *. . . . . . .
. . . . . . . . . . . . . . . * //*n e w s p l l t */ I *. . . . .
. . . . . . . . . . . . . . . . . * IK D T r e e * n e w s p l l t
( l o n g d, l o n g p, K D T r e e * l e f t ,K D T r e e * r i g
h t ) {K D T r e e * t r e e ;t r e et r e e - > t y p et r e e
- > n o d e . s p l i t . d l s ct r e e - > n o d e . s p i
l t . p a r tt r e e - > n o d e . s p i l t . l e f tt r e e -
> n o d e . s p l l t . r l g h tr e t u r n (t r e e ); /*r e t
u r n s n o n - t e r m i n a l k - d t r e e n o d e */ /*d i s c
r i m i n a t o r i n d e x */ /*p a r t i t i o n v a l u e f o r
d i s c r i m i n a t o r */ /*l e f t s u b - t r e e */ /*r i g h
t s u b - t r e e * //*p o i n t e r t o n e w k - d t r e e n o d
e */ =( K D T r e e *)m a l l o c (8 i z e o f (* t r e e )); =S P
L I T ;=d; =p; =l e f t ;=r i g h t ;/ *. . . . . . . . . . . . . .
. . . . . . . . * //*s p i l t _ l e f t */ / *. . . . . . . . . .
. . . . . . . . . . . . * /C o d e B o o k s p i l t _ l e f t (/*r
e t u r n l e f t p a r t i t i o n */ l o n g pl,/*i n d e x o f p
a r t i t i o n * /1244D. A. SOUTHAgD C o d e B o o k c o d e b o o
k )/*c o d e b o o k */ C o d e B o o k l e f t ; /*t h e l e f t p
a r t i t i o n */ l e f t . n u m =p i +i; l e f t . b o o k =c o
d e b o o k . b o o k ;r e t u r n (l e f t ); /*n u m b e r o f c
o d e s */ /*p o i n t e r t o c o d e s * //*s p l l t r l g h t
*/ / *. . . . . . . . . . . . . . . . . . . . . . * /C o d e B o o
k s p i l t _ r i g h t ( l o n g pl, C o d e B o o k c o d e b o o
k ) {C o d e B o o k r i g h t ;/*r e t u r n r i g h t p a r t i t
i o n */ /*i n d e x o f p a r t i t i o n */ /*c o d e b o o k */
/*t h e r i g h t p a r t i t i o n */ r l g h t . n u m =c o d e b
o o k . n u m -p l -I; r l g h t . b o o k =c o d e b o o k . b o o
k +p i +I; r e t u r n (r i g h t ); /*n u m b e r o f c o d e s *
//*p o i n t e r t o c o d e s */ / *. . . . . . . . . . . . . . .
. . . . . . . * //*s e a r c h */ / *. . . . . . . . . . . . . . .
. . . . . . . * /l o n g s e a r c h ( u c h a r v e c t o r [ ] ,K
D T r e e * t r e e ) {i f (t r e e - > t y p e = = B U C K E T
) {)e l s e{/*r e c u r s l v e s e a r c h t h r u k - d tree*/
/*v e c t o r t o b e m a t c h e d */ /*t r e e t o b e t r a v e
r s e d * //*c h e c k a l l c o d e s i n b u c k e t */ r e g i s
t e r C o d e *c;/* r e g i s t e r i;/* r e g i s t e r J;/* r e g
i s t e r m a t c h e s ; / *r e g i s t e r sum;/* c o d e p o i n
t e r */ c o d e l o o p i n d e x */ c o o r d i n a t e l o o p i
n d e x * /l o g i c a l p r e d i c a t e */ p a r t i a l d i s t
a n c e s u m */ c=t r e e - > n o d e . b u c k e t . b o o k
;f o r (i=0;i n o d e . b u c k e t . n u m ;i++,c + + ) {s u m =d
i s t (0,v e c t o r , c - > v e c t o r ); f o r (J=1; ( m a t
c h e s =s u m n o d e . s p l i t . d l s c ;p=t r e e - > n o
d e . s p i l t . p a r t ;i f ( v e c t o r [ d]< = p) {/*g e t
d i s c r i m i n a t o r i n d e x */ /*g e t p a r t i t i o n v
a l u e */ /*s e a r c h l e f t s u b t r e e */ t e m p =u p p e
r [ d]; u p p e r [ d]=p; /*s a v e u p p e r b o u n d */ /*n e w
u p p e r b o u n d */ d o n e =s e a r c h ( v e c t o r , t r e e
- > n o d e . s p l i t . l e f t ); i f ( d o n e )r e t u r n
d o n e ;u p p e r [ d]=t e m p ;t e m p =l o w e r [ d]; l o w e r
[ d]=p; /*r e s t o r e u p p e r b o u n d */ /*s a v e l o w e r
b o u n d * //*n e w l o w e r b o u n d * /i f ( b o b ( v e c t o
r ))/*b o u n d s o v e r l a p b a l l ? */ {d o n e =s e a r c h
( v e c t o r , t r e e - > n o d e . s p l i t . r l g h t ); i
f ( d o n e )r e t u r n d o n e ;)Compressionof
digitizedmapimages1245 } e l s e{ l o w e r [ d]=t e m p ;t e m p
=l o w e r [ d]; l o w e r [ d]=p; d o n e =s e a r c h ( v e c t o
r ,i f ( d o n e )r e t u r n d o n e ;l o w e r [ d]=t e m p ;t e
m p =u p p e r [ d]; u p p e r [ d]=p; i f ( b o b ( v e c t o r ))
{ /*r e s t o r e l o w e r b o u n d */ /*s e a r c h r i g h t s
u b t r e e */ /*s a v e l o w e r b o u n d */ /*n e w l o w e r b
o u n d * /t r e e - > n o d e . s p l i t . r i g h t ); /*r e
s t o r e l o w e r b o u n d */ /*s a v e u p p e r b o u n d */
/*n e w u p p e r b o u n d */ /*b o u n d s o v e r l a p b a l l
? * /d o n e =s e a r c h ( v e c t o r , t r e e - > n o d e .
s p i l t . l e f t ); i f ( d o n e )r e t u r n d o n e ;) u p p
e r [ d]=t e m p ; /*r e s t o r e u p p e r b o u n d */ } ) r e t
u r n ( b w b ( v e c t o r )); / *. . . . . . . . . . . . . . . .
. . . . . . * //*m a t c h * //* . . . . . . . . . . . . . . . . .
. . . . .*/ v e c t o r [ ] ,* t r e e ) i; C o d e * m a t c h ( u
c h a rK D T r e e{ r e g i s t e rm a t c h e d =N U L L ;n e a r
e s t =I N T _ M A X ;f o r (i=0;iProgram L~ting F I L E : v q e x
p . cP U R P O S E : D e c o m p r e s s ( e x p a n d ) V Q c o m
p r e s s e d i m a g e f i l e s .U S A G E : v q e x p < c o d
e b o o k > [ i n p u t . v q ] [ o u t p u t . i m a g e ]A R G
U M E N T S :c o d e b o o k : n a m e o f c o d e b o o k file,c r
e a t e d b y v q i n i ti n p u t . v q = n a m e o f V Q - c o m
p r e s s e d i m a g e f i l e ( s t d i n )[o u t p u t . i m a g
e :n a m e o f d e c o m p r e s s e d i m a g e f i l e ( s t d o
u t )L A N G U A G E : A N S I C,M I P S Cc o m p i l e r v e r s i
o nA U T H O R = D a v i d A.S o u t h a r d , T h e M I T R E C o
r p o r a t i o nC R E A T E D : 12N o v 1 9 9 1N O T E S / W A R N
I N G S :/*s t a n d a r d I / O l i b r a r y d e f i n i n t i o
n s */ /*M A N I F E S T C O N S T A N T S */ /* . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . */ # d e f i n e E R R O R (-i)/*e r r o r r
e t u r n s t a t u s c o d e * /# d e f i n e M I N A R G S 1/*v a
l i d m i n i m u m n u m b e r o f a r g u m e n t s */ # d e f i
n e N O R M A L 0/*n o r m a l r e t u r n s t a t u s c o d e */
I* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . *I /*F U N C T I O N P
R O T O T P Y E S */ I* . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *I
t y p e d e f u n s i g n e d c h a r u c h a r ; /*e n s u r e u n
s l g n e d f o r p o r t a b i l i t y */ v o i d g e t _ a r g s
(i n t , c h a r **); u c h a r * r d _ c o d e s (F I L E *);
Compressionof digitizedmapimages1247 / *. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . * !/*G L O B A L V A R I A B L E S */ ! *. . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . 1l o n g c o d e b o o k _ l e n g t h
; /*n u m b e r ofcodesinc o d e b o o k *! F I L E *coder;/*filep
o i n t e r forthec o d e b o o k */ l o n g b l o c k _ h e i g h
t ; !*v e c t o r b l o c k h e i g h t *! l o n g b l o c k _ w l
d t h ; !*v e c t o r b l o c k w i d t h *! l o n g p i x e l _ d
e p t h ; /*b y t e s p e r pixel*/ l o n g serial;/*serialn u m b
e r ofc o d e b o o k */ l *. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
" 1/*P R O G R A M */ ! *. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *
!1"V Q d e c o m p r e s s i o n */ intargc,/*argumentcount*! c h a
r * * a r g v )!*argumentlist*/ u c h a r "*codebook;/*c o d e b o
o k ford e c o m p r e s s i o n *! l o n g c o d e _ w l d t h ;
/*w i d t h ofonev e c t o r r o w */ l o n g c o d e
81ze;/*numberofb y t e s inav e c t o r */ r e g i s t e r i;/*v e
c t o r b l o c k r o w i n d e x *! u c h a r **image;/*a r r a y
ofp o i n t e r s toi m a g e rows*/ r e g i s t e r J;/*v e c t o
r b l o c k c o l u m n i n d e x */ r e g i s t e r k;/*v e c t o
r b l o c k b y t e i n d e x */ r e g i s t e r i;/*i n d e x
intoimagerows*/ l o n g n_cols;!*n u m b e r ofcolsine x p a n d e
d i m a g e */ r e g i s t e r u c h a r *pi;!*p o i n t e r
intoani m a g e r o w */ r e g i s t e r u c h a r *pv;!*p o i n t
e r intoacodev e c t o r */ l o n g serno;/*c o m p r e s s e d
images e r i a l n u m b e r */ short*svq;/*(short)a r r a y ofc o
d e s b u f f e r *! u c h a r *cvq;/*(uchar)a r r a y ofc o d e s
b u f f e r */ l o n g x;!*c o m p r e s s e d imagec o l u m n i n
d e x */ l o n g xslze;/*n u m b e r ofcolsinc o m p r e s s e d
image*/ l o n g y;/*c o m p r e s s e d imager o w i n d e x */ l o
n g ysize;/*numberofrowsinc o m p r e s s e d i m a g e */ l o n g
zsize;!*n u m b e r ofb y t e s p e r c o d e i n d e x */ m a i n
( {/ *. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . * //*R e a d a r g u m e n t s a n d
c o d e b o o k s */ / *. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . * !g e t _ a r g s
( argc,a r g v );/*c o m m a n d - l l n e a r g u m e n t s */ c o
d e b o o k =rd__codes( c o d e f );/*getc o d e b o o k fromfile*/
1 ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . * l/*R e a d c o m p r e s s e d imageh
e a d e r info*! / *. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . * !f r e a d (
&serno,s i z e o f ( l o n g ),i,stdin);/*serialn u m b e r *!
f r e a d ( &xsize,s l z e o f ( l o n g ),1,stdin);/*n u m b e
r ofc o l u m n s */ f r e a d ( &yslze,s i z e o f ( l o n g
),1,s t d l n );!*n u m b e r ofrows*! f r e a d ( &zsize,s i z
e o f ( l o n g ),1,stdin);!*b y t e s p e r code*! if( s e r n o
I=serial)/*v e r i f y serialn u m b e r s */ { f p r i n t f (
stderr, %s:c o d e b o o k i n c o n s i s t e n t w i t h c o m p
r e s s e d imagelkn', argv[0]); exit( E R R O R ); ) l *. . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . * l!*A l l o c a t e b u f f e r m e m o r y "1 l *.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . * ln _ c o l s =x s i z e
*block_.width;/*bytesa c r o s s c o l u m n */ c o d e _ w l d t h
=b l o c k _ w l d t h *p i x e 1 _ d e p t h ; /*b y t e s a c r o
s s b l o c k */ c o d e _ s l z e =c o d e _ w l d t h *b l o c k
_ h e i g h t ; !*b y t e s e n t i r e b l o c k */ i m a g e
=(uchar**)m a l l o c (b l o c k _ h e l g h t *sizeof(uchar*));
for( i=0;i