source: Dev/trunk/src/client/dojox/encoding/compression/lzw.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 1.9 KB
Line 
1define([
2        "dojo/_base/lang",      // dojo.extend
3        "../bits"
4], function(lang, bits) {
5
6        var lzw = lang.getObject("dojox.encoding.compression.lzw", true);
7
8        var _bits = function(x){
9                var w = 1;
10                for(var v = 2; x >= v; v <<= 1, ++w);
11                return w;
12        };
13
14        lzw.Encoder = function(n){
15                this.size = n;
16                this.init();
17        };
18
19        lang.extend(lzw.Encoder, {
20                init: function(){
21                        this.dict = {};
22                        for(var i = 0; i < this.size; ++i){
23                                this.dict[String.fromCharCode(i)] = i;
24                        }
25                        this.width = _bits(this.code = this.size);
26                        this.p = "";
27                },
28                encode: function(value, stream){
29                        var c = String.fromCharCode(value), p = this.p + c, r = 0;
30                        // if already in the dictionary
31                        if(p in this.dict){
32                                this.p = p;
33                                return r;
34                        }
35                        stream.putBits(this.dict[this.p], this.width);
36                        // if we need to increase the code length
37                        if((this.code & (this.code + 1)) == 0){
38                                stream.putBits(this.code++, r = this.width++);
39                        }
40                        // add new string
41                        this.dict[p] = this.code++;
42                        this.p = c;
43                        return r + this.width;
44                },
45                flush: function(stream){
46                        if(this.p.length == 0){
47                                return 0;
48                        }
49                        stream.putBits(this.dict[this.p], this.width);
50                        this.p = "";
51                        return this.width;
52                }
53        });
54
55        lzw.Decoder = function(n){
56                this.size = n;
57                this.init();
58        };
59
60        lang.extend(lzw.Decoder, {
61                init: function(){
62                        this.codes = new Array(this.size);
63                        for(var i = 0; i < this.size; ++i){
64                                this.codes[i] = String.fromCharCode(i);
65                        }
66                        this.width = _bits(this.size);
67                        this.p = -1;
68                },
69                decode: function(stream){
70                        var c = stream.getBits(this.width), v;
71                        if(c < this.codes.length){
72                                v = this.codes[c];
73                                if(this.p >= 0){
74                                        this.codes.push(this.codes[this.p] + v.substr(0, 1));
75                                }
76                        }else{
77                                if((c & (c + 1)) == 0){
78                                        this.codes.push("");
79                                        ++this.width;
80                                        return "";
81                                }
82                                var x = this.codes[this.p];
83                                v = x + x.substr(0, 1);
84                                this.codes.push(v);
85                        }
86                        this.p = c;
87                        return v;
88                }
89        });
90
91        return lzw;
92});
Note: See TracBrowser for help on using the repository browser.