Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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