Dt Compression Reduce the size of dt. Reduces storge spce nd hence storge cost. Compression rtio = originl dt size/compressed dt size Reduces time to retrieve nd trnsmit dt.
Lossless And Lossy Compression compresseddt = compress(originldt) decompresseddt = decompress(compresseddt) When originldt = decompresseddt, the compression is lossless. When originldt!= decompresseddt, the compression is lossy.
Lossless And Lossy Compression Lossy compressors generlly otin much higher compression rtios thn do lossless compressors. Sy vs.. Lossless compression is essentil in pplictions such s text file compression. Lossy compression is cceptle in mny imging pplictions. In video trnsmission, slight loss in the trnsmitted video is not noticed y the humn eye.
Text Compression Lossless compression is essentil. Populr text compressors such s zip nd Unix s compress re sed on the LZW (Lempel-Ziv-Welch) method.
LZW Compression Chrcter sequences in the originl text re replced y codes tht re dynmiclly determined. The code tle is not encoded into the The code tle is not encoded into the compressed text, ecuse it my e reconstructed from the compressed text during decompression.
LZW Compression Assume the letters in the text re limited to {, }. In prctice, the lphet my e the 56 chrcter ASCII set. The chrcters in the lphet re ssigned code numers eginning t. The initil code tle is: code
LZW Compression code Originl text = Compression is done y scnning the originl text from left to right. Find longest prefix p for which there is code in the code tle. Represent p y its code pcode nd ssign the next ville code numer to pc, where c is the next chrcter in the text tht is to e compressed.
LZW Compression Originl text = p = pcode = c = code Represent y nd enter into the code tle. Compressed text =
LZW Compression code Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
LZW Compression code 4 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
LZW Compression code 4 5 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
code LZW Compression Originl text = Compressed text = p = pcode = c = 4 5 6 Represent y nd enter into the code tle. Compressed text =
code LZW Compression Originl text = Compressed text = p = pcode = c = 4 5 6 7 Represent y nd enter into the code tle. Compressed text =
code LZW Compression Originl text = Compressed text = p = pcode = 5 c = 4 5 6 7 8 Represent y 5 nd enter into the code tle. Compressed text = 5
code LZW Compression 4 5 Originl text = Compressed text = 5 p = pcode = 8 c = 6 7 8 9 Represent y 8 nd enter into the code tle. Compressed text = 58
code LZW Compression 4 5 Originl text = Compressed text = 58 p = pcode = 8 c = null Represent y 8. Compressed text = 588 6 7 8 9
Code Tle Representtion code 4 5 6 7 8 9 Dictionry. Pirs re (, element) = (,code). Opertions re : find() nd insert(, code) Limit numer of codes to. Use hsh tle. Convert vrile length s into fixed length s. Ech hs the form pc, where the string p is tht is lredy in the tle. Replce pc with (pcode)c.
Code Tle Representtion code 4 5 6 7 8 9 code 4 5 6 7 8 9 5 8
LZW Decompression code Originl text = Compressed text = 588 Convert codes to text from left to right. represents. Decompressed text = pcode = nd p =.
LZW Decompression code Originl text = Compressed text = 588 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
LZW Decompression code Originl text = Compressed text = 588 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
LZW Decompression code Originl text = Compressed text = 588 represents Decompressed text =. pcode = nd p =. 4 lstp = followed y first chrcter of p is entered into the code tle.
LZW Decompression code Originl text = Compressed text = 588 represents Decompressed text =. pcode = nd p =. 4 5 lstp = followed y first chrcter of p is entered into the code tle.
code LZW Decompression 4 5 Originl text = Compressed text = 588 represents 6 Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
code LZW Decompression 4 5 Originl text = Compressed text = 588 5 represents 6 7 Decompressed text =. pcode = 5 nd p =. lstp = followed y first chrcter of p is entered into the code tle.
code LZW Decompression 4 5 Originl text = Compressed text = 588 8 represents??? 6 7 When code is not in the tle, its is lstp followed y first chrcter of lstp. lstp = So 8 represents. 8
code LZW Decompression 4 5 Originl text = Compressed text = 588 8 represents 6 7 8 9 Decompressed text =. pcode = 8 nd p =. lstp = followed y first chrcter of p is entered into the code tle.
code 4 5 6 7 Code Tle Representtion 8 9 Dictionry. Pirs re (, element) = (code, wht the code represents) = (code, codekey). Opertions re : find() nd insert(, code) Keys re integers,,, Use D rry codetle. codetle[code] = codekey. Ech code hs the form pc, where the string p is code tht is lredy in the tle. Replce pc with (pcode)c.
Time Complexity Compression. O(n) expected time, where n is the length of the text tht is eing compressed. Decompression. O(n) time, where n is the length of the decompressed text.
Prepre to e confused
How to pck it codes into ytes. void output(long pcode). {// Output 8 its, sve rest in leftover.. int c, d; 4. if (itsleftover) 5. {// hlf yte remins from efore MASK = 5 () EXCESS = 4 BYTE_SIZE = 8 MASK = 55 () 6. Tke 8 right its from -it numer d = int(pcode & MASK); // right ByteSize its 7. c = int((leftover << EXCESS) (pcode >> BYTE_SIZE)); 8. out.put(c); Comine leftover its with 4 left its from new -it numer 9. out.put(d);. itsleftover = flse;. }. else. {// no its remin from efore 4. leftover = pcode & MASK; // right EXCESS its 5. c = int(pcode >> EXCESS); 6. out.put(c); 7. itsleftover = true; 8. } 9.} Chop 4 right its off -it numer nd sve for lter Output 8 left its
Compression - Initilizing. hshchins<long, int> h(divisor);. for (int i = ; i < ALPHA; i++). h.insert(pirtype(i, i)); 4. int codesused = ALPHA; 5. Note tht is going to e something like 65 nd not s suggested y the erlier exmple.
Compression. int c = in.get(); // first chrcter of input file. if (c!= EOF) {// input file is not empty. long pcode = c; // prefix code 4. while ((c = in.get())!= EOF) {// process chrcter c 5. long thekey = (pcode << BYTE_SIZE) + c; 6. // see if code for thekey is in the dictionry 7. pirtype* thepir = h.find(thekey); 8. if (thepir == NULL) {// thekey is not in the tle 9. output(pcode);. if (codesused < MAX_CODES) // crete new code. h.insert(pirtype((pcode << BYTE_SIZE) c, codesused++));. pcode = c;. } else pcode = thepir->second; // thekey is in tle 4. } 5. 6. // output lst code(s) 7. output(pcode); 8. if (itsleftover) 9. out.put(leftover << EXCESS);
Decompression. typedef pir<int, chr> pirtype;. pirtype ht[max_codes]; text(p) = text(q)c. void output(int code) 4. {// Output string corresponding to code. 5. size = -; 6. while (code >= ALPHA) 7. {// suffix in dictionry 8. s[++size] = ht[code].second; 9. code = ht[code].first;. }. s[++size] = code; // code < ALPHA.. // decompressed string is s[size]... s[] 4. for (int i = size; i >= ; i--) 5. out.put(s[i]); 6.}
Decompression. ool getcode(int& code) Turning ytes into -it codes. {// Put next code in compressed file into code.. // Return flse if no more codes. 4. int c, d; 5. if ((c = in.get()) == EOF) 6. return flse; // no more codes 7. 8. // see if ny left over its from efore 9. // if yes, conctente with left over 4 its. if (itsleftover). code = (leftover << BYTE_SIZE) c;. else. {// no left over its, need four more its 4. // to complete code 5. d = in.get(); // nother 8 its 6. code = (c << EXCESS) (d >> EXCESS); EXCESS = 4 7. leftover = d & MASK; // sve 4 its MASK = 5 () 8. } 9. itsleftover =!itsleftover;. return true;.}
Decompression. if (getcode(pcode)) {// file is not empty. s[] = pcode; // chrcter for pcode. out.put(s[]); // output string for pcode 4. size = ; //s[size] is first chrcter of lst string 5. 6. while(getcode(ccode)) {// there is nother code 7. if (ccode < codesused) {// ccode is defined 8. output(ccode); 9. if (codesused < MAX_CODES) {// crete new code. ht[codesused].first = pcode;. ht[codesused++].second = s[size];. }. } else {// specil cse, undefined code 4. ht[codesused].first = pcode; 5. ht[codesused++].second = s[size]; 6. output(ccode); 7. } 8. pcode = ccode; 9. }.}