Vous êtes sur la page 1sur 15

AlosslessapproachtobilevelandGreyscaleimageCompressionusing2dimensional

runlengthencodingalgorithm

SubhabrataBhattacharya
B.E.7thSemester,
ComputerScience&Engineering,
AsansolEngineeringCollege

Acknowledgments
I pay my sincere regards to Prof. Bhabatosh Chanda, Electronics and Communication Sciences Unit,
Computer and communication Sciences Division, Indian Statistical Institute, Kolkata; without whose
guidancetheprojectcouldneverbeenaccomplished.

References
DigitalImageProcessing,SecondEdition
RafaelC.GonzalezandRichardE.Woods
PearsonEducationInc.2002

SoftwareEnvironmentandtoolsused
TheentiresoftwareiswritteninANSICprogramminglanguageinGNU/Linux(kernel2.4.710)platform.
ThesoftwareiscompiledusingGnuCcompilerversion2.96andentirelyfreeforuseandredistributionas
longasthetermsandconditionsoftheGNUPublicLicensearesatisfied.

GeneralConstraints
[1]ThesoftwareiscurrentlydevelopedtoworkonlyinUnixbasedplatformsandcannotbeusedunder
Windowsbasedsystems.
[2]CurrentlythesoftwareisfullyfunctionalformonochromeWindowsbitmapfiles.Extendedsupportfor
OS/2bitmapsandotherfileformatsareunderdevelopment.
[3]Supportforcolorandgrayscaleimagesareunderdevelopment.

1.Introduction
Everyday,anenormousamountofinformationisstored,processedandtransmitteddigitally.Companies
providebusinessassociates,investorsandpotentialcustomerswithfinancialdata,annualreportsand
inventoryandproductinformationovertheInternet.Orderentryandtracking,twoofthemostbasic
onlinetransactionsareroutinelyperformedfromthecomfortofonesownhome.Becausemuchofthison
line information is graphical or pictorial in nature, the storage and communication requirements are
immense.Methodsofcompressingthedatapriortostorageand/ortransmissionareofsignificantpractical
andcommercialinterest.
Imagecompressionaddressestheproblemofreducingtheamountofdatarequiredtorepresentadigital
image. The underlying basis of the reduction process is the removal of redundant data. From a
mathematicalviewpoint,thisamountstotransforminga2Dpixelarrayintoastatisticallyuncorrelated
dataset.Thetransformationisappliedpriortostorageortransmissionoftheimage.Atsomelatertime,
thecompressedimageisdecompressedtoreconstructtheoriginalimageoranapproximationofit.
Image compression techniques broadly fall into two categories: information preserving (lossless) and
lossy.Theformerallowsanimagetobecompressedanddecompressedwithoutlosinginformationwhile
thelatterprovidehigherdegreesofdatareductionbutresultinalessthanperfectreproductionofthe
original image. Lossless image compression techniques are useful in image archiving. Lossy image
compressiontechniquesareusedintelevisionbroadcastandvideoconferencingwhereacertaindegreeof
errorisanacceptabletradeoffforincreasedcompressionperformance.Animagethatcanbecompressed
musthavedatathatarenonessentialandsimplyrestatethatwhichisalreadyknown.Thisiscalleddata
redundancy.Thetypeofdataredundancythatisbeingexploitedtocompressanimageinthiscontextis
interpixelredundancy.Becausethevalueofanypixelcanbepredictedfromthevalueofitsneighbours,
theinformationcarriedbytheindividualpixelsisrelativelysmall.Muchofthevisualcontributionofa
single pixel to an image is redundant; it could have been guessed on the basis of the values of its
neighbors. To reduce the interpixel redundancies in an image, the 2D pixel array normally used for
humanviewingandinterpretationmustbetransformedintoamoreefficient(butusuallynonvisual)
format. In case of lossless image compression, transformations of this type are reversible in nature
becausetheimagecanbereconstructedfromthetransformeddataset.
The algorithm implemented in the project is an improvement over the onedimensional runlength
algorithm. In the onedimensional algorithm the correlation between contiguous pixels in a row is
exploitedandthisinturnreducestheinterpixelredundancy.Whileinitsimprovedcounterpartthekey
toeliminationofinterpixelredundancyliesinexploitationofthecorrelationbetweentheadjacentrowsin
addition to the correlation that is already discussed. Although the method is complicated and time
resourceintensive;yetitensureshigherdegreeofcompression.Ithasbeenseenthatforanyrowofa
givenimage,therowsadjacenttoitaremoreorlessidenticali.e.thegrayleveldistributionofpixelsin
adjacentrowsofanimageissomewhatuniform.

2.WorkingMethodology
Theprojectbroadlyconsistsoftwosoftwares:theencoderandthedecoder.Inthiscontextwedescribe
eachofthemindetail.Boththeencoderanddecoderaresubdividedintodifferentmodulesandtheway
controlanddataflowfromonemoduletotheotherarebeingshowninthesubsequentschematicdiagrams
Encoder: Theencoderhasthefollowingmodules:Fileformatreader,Matrixgenerator,2Drunlength
encoder,ASCIIconverterandFileformatter

Fileformatreader:Thismoduleisresponsiblefortakinginputfromtheuserintheformofdifferent
imagefiles.Itisprogrammedtoreadthefileheaderandpassthenecessaryinformationliketheimage
resolution,colorcontentandalgorithmusedtoencodetheimageispassedtothenextmodule.Currently
ithasbeenprogrammedtoacceptonlyWindowsbitmapfiles(.bmp)withonlytwograylevels(black&
white) but the support for different file formats and multiple graylevel can be increased with minor
modificationsintheexistingsoftware.
Matrixgenerator:Onthebasisoftheinformationgatheredfromthepreviousmodule,itgeneratesatwo
dimensional array or matrix containing the graylevel value of each pixels of the image. Thus the
resolution of the image (width and height), color content and method used while encoding the image
becomesanabsolutemustforthismoduletogenerateanaccuratematrixthatmapseachofitselements
tothecorrespondingpositionaswellasthegraylevelvalueatthatpositionintheactualimage.

Equivalentbinarymatrix

A5x4monochromebitmapimage

Whiledealingwithcolororgrayscaleimagesthathavemorethanjust2colors,wefirstdecomposethe
graycodedimageintoaseriesofbinaryimages.Thisiscalledbitplanedecomposition.Thegraylevelsof
anmbitgrayscaleimagecanberepresentedintheformofthebase2polynomial:

am-12m-1 + am-22m-2 + + a121 + a020


Thecoefficientsofthepolynomial(ai)canonlyhavevalues0and1

gi = ai ai+1
gm-1 = am-1

0 im-2

26
0

22
0

Thusapixelwithgraylevel244canberepresentedinthebinaryformas:
27
1

25
0

24
1

23
0

21
0

Thecorrespondinggraycodeiscalculatedassfollows:
g81=a81i.e.g7=a7=1
g6=a6 a7=1 0=1
g5=a5 a6=0 0=0
g4=a4 a5=1 0=1
g3=a3 a4=0 1=1
g2=a2 a3=0 0=0
g1=a1 a2=0 0=0
g0=a0 a1=0 0=0

20
0

Hencegraycodecorrespondingtothepixelwithgraylevel244is11011000.Thegraycodedimageisnow
decomposedintom1bitplanes.Thisisdonetoavoidcomplexityinthebitplanesthatiscreateddueto
smallchangesingraylevelsoftheindividualpixels.
Thusforan8bitimage8separatebinarymatricesaregeneratedfromthegraycodesandthematrices
arepassedasinputtothenextmodule.
2Drunlengthencoder:Thismoduleisresponsiblefortheencoding.Eachrowfromthematrixthatisinput
from the previous module is encoded with the help of some predetermined code constraints. The 2D
runlengthencoding algorithm is discussed shortly in the next section. The matrix is converted into a
stringof0sand1sandthestringiswrittenintoatemporaryfile,whichispassedtothenextmodulefor
furtherprocessing.
ASCIIconverter:Itacceptsthefilecontainingthestringof0sand1sandtaking8charactersatatime,it
convertsitintoanequivalentASCIIcharactersandthestreamofASCIIcharactersarefurtherwritten
intoafile(whichis1/8thofthepreviousintermediatefile)andisbeingpassedtothefinalmoduleforlast
rites.
Fileformatter:Thisistheultimatemoduleoftheencodersoftware,whichgeneratesaheaderfromthefile
informationpreviouslyfetchedbythefileformatreader,andaddsthisheadertotheintermediateASCII
file.Thisfileisnowreadyforfuturestorage/transmissionanddecompressionprocess.
SchematicDiagramoftheEncoder:

DescriptionoftheAlgorithmused
The2dimensionalrunlengthcodingapproachadoptedforboththeCCITTGroup3and4standardsisa
linebylinemethodinwhichthepositionofeachblacktowhiteorwhitetoblackruntransitioniscoded
withrespecttothepositionofareferenceelementa0thatissituatedonthecurrentcodingline;the
referencelineforthefirstlineofeachnewimageisanimaginarywhiteline.Thebasiccodingprocessfora
singlescanlineisshownintheflowchartthatfollowsthissection.Achangingelementisdefinedasa
pixel whose value is different from that of the previous pixel on the same line. The most important
changingelementisa0(thereferenceelement),whichiseithersettothelocationofanimaginarywhite
changingelementtotheleftofthefirstpixelofeachnewcodinglineordeterminedfromtheprevious
codingmode.Aftera0islocated,a1isidentifiedasthelocationofthenextchangingelementtotheright
ofa0onthecurrentcodingline,a2asthenextchangingelementtotherightofa1onthesamecoding
line,b1asthechangingelementoftheoppositevalueof(a0)andtotherightofa0onthereference(or
previous)line,andb2asthenextchangingelementtotherightofb1onreferenceline.Ifanyofthese
changingelementsarenotdetected,theyaresettothelocationofanimaginarypixeltotherightofthe
lastpixelontheappropriateline.
Afteridentificationofthecurrentreferenceelementandassociatedchangingelements,twosimple
tests are performed to select one of the three possible coding modes: pass mode, horizontal mode or
verticalmode.Theinitialtest,whichcorrespondstothefirstbranchpointofthefollowingflowchart,
comparesthelocationofb2tothatofb1.Thesecondtest,whichcorrespondstothesecondbranchpointof
the flow chart, computes the distance (in pixels) between thelocations of a1 and b1 and compares it
against3.Dependingontheoutcomeofthesetests,oneofthethreeoutlinedcodingblocksoftheflow
chart is entered and the appropriate coding process is executed .A new reference element is then
established,aspertheflowchart,inpreparationforthenextcodingiteration.
Specificcodesaredefined,thataretobeusedforeachofthethreepossiblecodingmodes.Inpass
mode,whichspecificallyexcludesthecaseinwhichb2isdirectlyabovea1,onlythepassmodecodeword
0001isneeded.Thismodeidentifieswhiteorblackreferencelinerunsthatdonotoverlapthecurrent
whiteorblackcodinglineruns.Inhorizontalcodingmode,thedistancefroma0toa1anda1toa2mustbe
codedinaccordancewiththeterminatingandmakeupcodesdefinedbyCCITTandthenappendedtothe
horizontalmodecodeword001.Thisisindicatedbythenotation001+M(a0~a1)+M(a1~a2),wherea0
~a1anda1~a2bothdenotethedistancesfroma0toa1anda1toa2,respectively.
Forexample,considertheimageline

1723

1724

1725

1276

1727

1728

1729

1730

1731

1732

1733

1734

Hereweconsiderapairofadjacentrowsfromanimage.Thefirstchangingelementforeachnewlinei.e.
a0issettotheleftofthefirstpixelofthelinesoa0=0,a1=1etc.Likewiseintheseconditerationwe
havethenextchangingelementis1725becauseat1725atransitionfromacontinuouswhiteruntoblack
occurs.Finally,inverticalcodingmode,oneofthesixspecialvariablelengthcodesisassignedtothe
distancebetweena1andb1.
Tosummarizethevariouscodingmodescanbeshownas:

Mode
Pass
Horizontal
Vertical
A1belowb1(a1~b1=0)
A1onetotherightofb1(a1~b1=1)
A1twototherightofb1(a1~b1=2)
A1threetotherightofb1(a1~b1=3)
A1onetotheleftofb1(a1~b1=1)
A1twototheleftofb1(a1~b1=2)
A1threetotheleftofb1(a1~b1=3)
Extension

CodeWord
0001
001+M(a0~a1)+M(a1~a2)
1
011
000011
0000011
010
000010
0000010
0000001xxx

Theextensionmodecodewordatthebottomofthetableisusedtoenteranoptionalfacsimilecoding
mode.Forexample,the0000001111codewordisusedtoinitiateanuncompressedmodeoftransmission.
TherunlengthscanbeobtainedfromtheCCITTterminatingandmakeupcodetablesthataregivenas
follows.
CCITTTerminatingCodes
Run
Len
gth

White
Code
Word

Black
Code
Word

Run
Length

White
Code
Word

Black Code
Word

Run
Length

White Code
Word

Black Code
Word

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

110101
111
111
1000
1011
1100
1110
1111
10011
10100
111
1000
1000
11
110100
110101
101010
101011
100111
1100
1000
10111

110111
10
11
10
11
11
10
11
101
100
100
101
111
100
111
11000
10111
11000
1000
1100111
1101000
1101100

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

11
100
101000
101011
10011
100100
11000
10
11
11010
11011
10010
10011
10100
10101
10110
10111
101000
101001
101010
101011
101100

110111
101000
10111
11000
11001010
11001011
11001100
11001101
1101000
1101001
1101010
1101011
11010010
11010011
11010100
11010101
11010110
11010111
1101100
1101101
11011010
11011011

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

101101
100
101
1010
1011
1010010
1010011
1010100
1010101
100100
100101
1011000
1011001
1011010
1011011
1001010
1001011
110010
110011
110100

1010100
1010101
1010110
1010111
1100100
1100101
1010010
1010011
100100
110111
111000
100111
101000
1011000
1011001
101011
101100
1011010
1100110
1100111

CCITTMakeupCodes
Run
Length
64
128
192
256
320
384
448
512
576
640
704
768
832
896

White Code
Word
11011
10010
010111
0110111
00110110
00110111
01100100
01100101
01101000
01100111
011001100
011001101
011010010
011010011

BlackCodeWord
0000001111
000011001000
000011001001
000001011011
000000110011
000000110100
000000110101
0000001101100
0000001101101
0000001001010
0000001001011
0000001001100
0000001001101
0000001110010

Run
Length
960
1024
1088
1152
1216
1280
1344
1408
1472
1536
1600
1664
1728

White Code
Word
011010100
011010101
011010110
011010111
011011000
011011001
011011010
011011011
010011000
010011001
010011010
011000
010011011

BlackCodeWord
0000001110011
0000001110011
0000001110101
0000001110110
0000001110111
0000001010010
0000001010011
0000001010100
0000001010101
0000001011010
0000001011011
0000001100100
0000001100101

RunLength

CodeWord

RunLength

CodeWord

1792
1856
1920
1984
2048
2112
2176

00000001000
00000001100
00000001101
000000010011
000000010011
000000010100
000000010101

2240
2304
2368
2432
2496
2560

000000010110
000000010111
000000011100
000000011101
000000011110
000000011111

And finallythe uniquecodeword000000000001is used toindicate theendof eachcodinglineandis


concatenatedwitheachlineofcodedstrings.Thisendoflineindicatorisusedtosignalthefirstlineof
eachnewimage.Thuswiththenumberofendoflineindicatorswecaneasilyevaluatethenumberof
rowsintheimage.

FlowChartdepictingthe2Drunlengthcodingprocess

Decoder
The decoder has the following modules: File header reader, Information extractor, ASCII to binary
converterLinedecoder,Matrixgenerator,Matrixtoimagefileconverter
Fileheaderreader:Thismoduleisprogrammedtoreadthefileheader.Itvalidatestheinput(inthiscase,
thefile)tobeusedbythesubsequentmodules.Italsosendstherawheaderdatatothenextmodulefor
furtherprocessingandusage.
Information extractor: It accepts the rawheader that is being passed to it and converts it to useful
informationthatisrequiredbysubsequentmodulesintheentiredecodingprocess.Numbersofcolumns
intheimage,colorsusedarebeingpassedtothemodulesworkinginthenextstage.

ASCIItobinaryconverter:ThismoduleacceptsrawASCIIdatastreamwithouttheheader.EachASCII
characterisconvertedintoitsbinaryequivalent(aclusterofeight1sand0s).Thusastreamof1sand0s
isgeneratedwhichstartsandendswithanendoflineindicator,000000000001.Theendoflinecount
givesthenumberofrowsintheimage.
Linedecoder:Thismoduleisresponsibleforextractinganencodedstringdelimitedbytwoendofline
indicatorsanddecodingit.Thecodewordsareseparatedfromeachotherbyexhaustivelookupinthe
codetablesthroughefficientsearchmechanismandappropriatevaluesofa0,a1anda2arecalculated
fromthem.Thesevaluesarepassedtothenextmodule.
Matrix generator: It accepts the value of a0, a1 and a2 as being passed by the previous module and
developsindividualrowsofthematrix.Forexample,foreachrowa0isinitiallysetto0.a1iscalculated
fromthemodeofcodingasdepictedfromthecodewordbytheLinedecoder.Sostartingfroma0tothe
positionjustleftofa1arefilledwithwhite.a1onwardswillbefilledwiththecoloroppositeofa0i.e.black
andsoon.
Theabovetwomodulesneedtoworksimultaneouslytobuildthematrix.
Matrixtoimagefileconverter:Thematrixthatisgeneratedisbeingpassedtotheultimatestageofthe
decoder,whichisresponsiblefortheconvertingthematrixintoabitmapfileformatoraformatthatis
easilyreadablebyanyimageviewersoftware.
SchematicDiagramoftheDecoder

StatisticalComparison
HereastandardWindowsbitmapimageofresolution800x600,savedundertheWindowsmonochrome
bitmapschemeissavedusingotherpopularencodingalgorithmandtheresultsaregivenasfollows:

Algorithm/Fileformatused

SizeinKB

TypeofCompression

OriginalImage

58.594

BMP(WindowsBitmap)

58.654

Lossless

JPEG

126.51

Lossy

GIF(GraphicsInterchangeFormat)

16.025

Lossless

PNG(PortableNetworkGraphics)

9.067

Lossless

TIFF(TaggedImageFileFormat)
PBM(PortableBitMap)

58.871
58.627

Lossless
Lossless

TDR

13.969

Lossless

ThestudyrevealsthatthecompressionachievedintheTDRfileformat(asgeneratedbytheencoder
software),implementedthrough2dimensionalrunlengthcodingalgorithmissignificantlyhigherthan
mostofthepopularencodingtechniques.
Thesameprocedurewasappliedwith10othermonochromebitmapfilesandanaveragecompressionratio
of5:1wasobserved.
The technique described in the above context can prove to be highly effective to store and transfer
documents. Large documents that are saved as image files like this can be easily transmitted over a
networkbecauseoftheirlessfilesize.
Imagesamplesonwhichthealgorithmistested:
a)Figure1(doc1.bmp)

b)Figure2(doc2.bmp)

c)Figure3(doc3.bmp)

RelatedScreenshots
a) TheEncoder

b)Decoder

Scopeofimprovement
Apartfromtheconstraintsthatarebeingdiscussedearlierthereareafewareaswhereperformanceofthe
softwarecanbeimproved.Sincethealgorithmimplementedthroughthesoftwareisnotanoptimalone,
thereisalwaysascopeofincreasedcompressionbyuseofgeneraldatacompressionalgorithmsoverit.

Vous aimerez peut-être aussi