of 78/78
Data Structures & Algorithms Các thut toán nén dliu (Data Compression Algorithms) Nguyn Tri Tun Khoa CNTT – ĐH.KHTN.Tp.HCM Email: [email protected]

[Slide] Chuong 5 - Cac Thuat Toan Nen

  • View
    240

  • Download
    14

Embed Size (px)

Text of [Slide] Chuong 5 - Cac Thuat Toan Nen

Data Structures & Algorithms

Cc thut ton nn d liu (Data Compression Algorithms)Nguyn Tri Tun Khoa CNTT H.KHTN.Tp.HCM Email: [email protected]

Data CompressionGii thiu Gii thut nn RLE Gii thut nn Huffman

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

2

Gii thiuCc thut ng thng dng:Data Compression Lossless Compression Lossy Compression Encoding Decoding Run / Run Length RLE, Huffman, LZWWinter 2012 Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM 3

Gii thiu (tt)Mc ch ca nn d liu:Gim kch thc d liu:Khi lu tr Khi truyn d liu

Tng tnh bo mt

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

4

Gii thiu (tt)C 2 hnh thc nn:Nn bo ton thng tin (Lossless Compression):Khng mt mt thng tin nguyn thu Hiu sut nn khng cao: 10% - 60% Cc gii thut tiu biu: RLE, Arithmetic, Huffman, LZ77, LZ78,

Nn khng bo ton thng tin (Lossy Compression):Thng tin nguyn thy b mt mt Hiu sut nn cao 40% - 90% Cc gii thut tiu biu: JPEG, MP3, MP4,

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

5

Gii thiu (tt)Hiu sut nn (%):T l % kch thc d liu gim c sau khi p dng thut ton nn D (%) = (N M)/N*100 D: Hiu sut nn N: kch thc data trc khi nn M: kch thc data sau khi nn

Hiu sut nn ty thucPhng php nn c trng ca d liuWinter 2012 Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM 6

Gii thiu (tt)Nn tp tin:Dng khi cn Backup, Restore, d liu Dng cc thut ton nn bo ton thng tin Khng quan tm n nh dng (format) ca tp tin Cc phn mm: PKzip, WinZip, WinRar,

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

7

Data CompressionGii thiu Gii thut nn RLE Gii thut nn Huffman

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

8

Gii thut nn RLERLE = Run Length Encoding: m ho theo di lp li ca d liu tng Dng 1: RLE vi file *.PCX Dng 2: RLE vi file *.BMP Nhn xtWinter 2012 Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM 9

Gii thut nn RLE (tt)T tng:Hnh thc biu din thng tin d tha n gin: ng chy (run) l dy cc k t lp li lin tipng chy c biu din ngn gn: Khi di ng chy ln Tit kim ng k

V d:Data = AAAABBBBBBBBCCCCCCCCCCDEE (# 25 bytes) Datann = 4A8B10C1D2E (# 10 bytes)

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

10

Gii thut nn RLE (tt)T tng: (tt)Khi vn dng thc t, cn c bin php x l trnh trng hp phn tc dng i vi cc run c bit 1 k t X (# 1 bytes) 1X (# 2 bytes)

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

11

Gii thut nn RLE (tt)Dng 1: RLE vi file *.PCX1 1 0 0 1 1 0 1 0 1 0 0 0 0 0 1

Hai bit cao bat 11

n = 6 bit thap cho biet so lan lap

d = byte d lieu ke tiep c lap

Trng hp run bnh thng: AAAAAAAAAAAAA 13 A (biu din 2 bytes) 0xCD 0x41Winter 2012 Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM 12

Gii thut nn RLE (tt)RLE trong cu trc file *.PCX (tt)0 1 0 0 0 0 0 1

Hai bit cao khong bat

ay la byte d lieu (so lan lap = 1)

Trng hp run c bit: A A (biu din 1 byte) 0x41Winter 2012 Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM 13

Gii thut nn RLE (tt)RLE trong cu trc file *.PCX (tt)1 1 0 0 0 0 0 1 1 1 0 1 1 0 0 1

Hai bit cao bat 11

n = 6 bit thap cho biet so lan lap (= 1)

d = byte d lieu ke tiep c lap

Trng hp run c bit: 0xD9(217d)

1 0xD9 (biu din 2 bytes) 0xC1 0xD9

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

14

Gii thut nn RLE (tt)RLE trong cu trc file *.PCX (tt)u im:Ci t n gin Gim cc trng hp phn tc dng ca nhng run c bit

Khuyt im:Dng 6 bit biu din s ln lp ch th hin c chiu di max = 63 Cc on lp di s phi lu tr lp li Khng gii quyt c trng hp phn tc dng vi run c bit c m ASCII >= 192d

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

15

Gii thut nn RLE (tt)RLE trong cu trc file *.PCX (tt)

V sao dng 2 bits lm c hiu, m khng dng 1 bit ?

Winter 2012

Data Structures & Algorithms - Data Compression - Nguyen Tri Tuan, DH.KHTN Tp.HCM

16

Gii thut nn RLE (tt)#define MAX_RUNLENGTH 63 int PCXEncode_a_String(char *aString, int nLen, FILE *fEncode) { unsigned char cThis, cLast; int i; int nTotal = 0; // Tng s byte sau khi m ho int nRunCount = 1; // Chiu di ca 1 run cLast = *(aString); for (i=0; i