]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/encoding/compression/lzw.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / encoding / compression / lzw.js
1 if(!dojo._hasResource["dojox.encoding.compression.lzw"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.encoding.compression.lzw"] = true;
3 dojo.provide("dojox.encoding.compression.lzw");
4 dojo.require("dojox.encoding.bits");
5
6 (function(){
7         var _bits = function(x){
8                 var w = 1;
9                 for(var v = 2; x >= v; v <<= 1, ++w);
10                 return w;
11         };
12
13         dojox.encoding.compression.lzw.Encoder = function(n){
14                 this.size = n;
15                 this.init();
16         };
17
18         dojo.extend(dojox.encoding.compression.lzw.Encoder, {
19                 init: function(){
20                         this.dict = {};
21                         for(var i = 0; i < this.size; ++i){
22                                 this.dict[String.fromCharCode(i)] = i;
23                         }
24                         this.width = _bits(this.code = this.size);
25                         this.p = "";
26                 },
27                 encode: function(value, stream){
28                         var c = String.fromCharCode(value), p = this.p + c, r = 0;
29                         // if already in the dictionary
30                         if(p in this.dict){
31                                 this.p = p;
32                                 return r;
33                         }
34                         stream.putBits(this.dict[this.p], this.width);
35                         // if we need to increase the code length
36                         if((this.code & (this.code + 1)) == 0){
37                                 stream.putBits(this.code++, r = this.width++);
38                         }
39                         // add new string
40                         this.dict[p] = this.code++;
41                         this.p = c;
42                         return r + this.width;
43                 },
44                 flush: function(stream){
45                         if(this.p.length == 0){
46                                 return 0;
47                         }
48                         stream.putBits(this.dict[this.p], this.width);
49                         this.p = "";
50                         return this.width;
51                 }
52         });
53
54         dojox.encoding.compression.lzw.Decoder = function(n){
55                 this.size = n;
56                 this.init();
57         };
58
59         dojo.extend(dojox.encoding.compression.lzw.Decoder, {
60                 init: function(){
61                         this.codes = new Array(this.size);
62                         for(var i = 0; i < this.size; ++i){
63                                 this.codes[i] = String.fromCharCode(i);
64                         }
65                         this.width = _bits(this.size);
66                         this.p = -1;
67                 },
68                 decode: function(stream){
69                         var c = stream.getBits(this.width), v;
70                         if(c < this.codes.length){
71                                 v = this.codes[c];
72                                 if(this.p >= 0){
73                                         this.codes.push(this.codes[this.p] + v.substr(0, 1));
74                                 }
75                         }else{
76                                 if((c & (c + 1)) == 0){
77                                         this.codes.push("");
78                                         ++this.width;
79                                         return "";
80                                 }
81                                 var x = this.codes[this.p];
82                                 v = x + x.substr(0, 1);
83                                 this.codes.push(v);
84                         }
85                         this.p = c;
86                         return v;
87                 }
88         });
89 })();
90
91 }