Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / encoding / compression / lzw.js @ 9

History | View | Annotate | Download (1.77 KB)

1
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6

    
7

    
8
if(!dojo._hasResource["dojox.encoding.compression.lzw"]){
9
dojo._hasResource["dojox.encoding.compression.lzw"]=true;
10
dojo.provide("dojox.encoding.compression.lzw");
11
dojo.require("dojox.encoding.bits");
12
(function(){
13
var _1=function(x){
14
var w=1;
15
for(var v=2;x>=v;v<<=1,++w){
16
}
17
return w;
18
};
19
dojox.encoding.compression.lzw.Encoder=function(n){
20
this.size=n;
21
this.init();
22
};
23
dojo.extend(dojox.encoding.compression.lzw.Encoder,{init:function(){
24
this.dict={};
25
for(var i=0;i<this.size;++i){
26
this.dict[String.fromCharCode(i)]=i;
27
}
28
this.width=_1(this.code=this.size);
29
this.p="";
30
},encode:function(_2,_3){
31
var c=String.fromCharCode(_2),p=this.p+c,r=0;
32
if(p in this.dict){
33
this.p=p;
34
return r;
35
}
36
_3.putBits(this.dict[this.p],this.width);
37
if((this.code&(this.code+1))==0){
38
_3.putBits(this.code++,r=this.width++);
39
}
40
this.dict[p]=this.code++;
41
this.p=c;
42
return r+this.width;
43
},flush:function(_4){
44
if(this.p.length==0){
45
return 0;
46
}
47
_4.putBits(this.dict[this.p],this.width);
48
this.p="";
49
return this.width;
50
}});
51
dojox.encoding.compression.lzw.Decoder=function(n){
52
this.size=n;
53
this.init();
54
};
55
dojo.extend(dojox.encoding.compression.lzw.Decoder,{init:function(){
56
this.codes=new Array(this.size);
57
for(var i=0;i<this.size;++i){
58
this.codes[i]=String.fromCharCode(i);
59
}
60
this.width=_1(this.size);
61
this.p=-1;
62
},decode:function(_5){
63
var c=_5.getBits(this.width),v;
64
if(c<this.codes.length){
65
v=this.codes[c];
66
if(this.p>=0){
67
this.codes.push(this.codes[this.p]+v.substr(0,1));
68
}
69
}else{
70
if((c&(c+1))==0){
71
this.codes.push("");
72
++this.width;
73
return "";
74
}
75
var x=this.codes[this.p];
76
v=x+x.substr(0,1);
77
this.codes.push(v);
78
}
79
this.p=c;
80
return v;
81
}});
82
})();
83
}