spice-html5-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. data/COPYING +674 -0
  2. data/COPYING.LESSER +165 -0
  3. data/LICENSE.txt +15 -0
  4. data/README.md +35 -0
  5. data/lib/spice-html5-rails/version.rb +7 -0
  6. data/lib/spice-html5-rails.rb +10 -0
  7. data/vendor/assets/javascripts/spice-html5.js +25 -0
  8. data/vendor/assets/javascripts/spiceHTML5/atKeynames.js +183 -0
  9. data/vendor/assets/javascripts/spiceHTML5/bitmap.js +51 -0
  10. data/vendor/assets/javascripts/spiceHTML5/cursor.js +92 -0
  11. data/vendor/assets/javascripts/spiceHTML5/display.js +806 -0
  12. data/vendor/assets/javascripts/spiceHTML5/enums.js +282 -0
  13. data/vendor/assets/javascripts/spiceHTML5/inputs.js +271 -0
  14. data/vendor/assets/javascripts/spiceHTML5/lz.js +166 -0
  15. data/vendor/assets/javascripts/spiceHTML5/main.js +177 -0
  16. data/vendor/assets/javascripts/spiceHTML5/png.js +256 -0
  17. data/vendor/assets/javascripts/spiceHTML5/quic.js +1335 -0
  18. data/vendor/assets/javascripts/spiceHTML5/spiceconn.js +455 -0
  19. data/vendor/assets/javascripts/spiceHTML5/spicedataview.js +96 -0
  20. data/vendor/assets/javascripts/spiceHTML5/spicemsg.js +883 -0
  21. data/vendor/assets/javascripts/spiceHTML5/spicetype.js +480 -0
  22. data/vendor/assets/javascripts/spiceHTML5/thirdparty/jsbn.js +589 -0
  23. data/vendor/assets/javascripts/spiceHTML5/thirdparty/prng4.js +79 -0
  24. data/vendor/assets/javascripts/spiceHTML5/thirdparty/rng.js +102 -0
  25. data/vendor/assets/javascripts/spiceHTML5/thirdparty/rsa.js +146 -0
  26. data/vendor/assets/javascripts/spiceHTML5/thirdparty/sha1.js +346 -0
  27. data/vendor/assets/javascripts/spiceHTML5/ticket.js +250 -0
  28. data/vendor/assets/javascripts/spiceHTML5/utils.js +261 -0
  29. data/vendor/assets/javascripts/spiceHTML5/wire.js +123 -0
  30. metadata +108 -0
@@ -0,0 +1,1335 @@
1
+ /*"use strict";*/
2
+ /* use strict is commented out because it results in a 5x slowdone in chrome */
3
+ /*
4
+ * Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
5
+ * Copyright (C) 2012 by Aric Stewart <aric@codeweavers.com>
6
+ *
7
+ * This file is part of spice-html5.
8
+ *
9
+ * spice-html5 is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * spice-html5 is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public License
20
+ * along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
21
+ */
22
+
23
+ var encoder;
24
+
25
+ var QUIC_IMAGE_TYPE_INVALID = 0;
26
+ var QUIC_IMAGE_TYPE_GRAY = 1;
27
+ var QUIC_IMAGE_TYPE_RGB16 = 2;
28
+ var QUIC_IMAGE_TYPE_RGB24 = 3;
29
+ var QUIC_IMAGE_TYPE_RGB32 = 4;
30
+ var QUIC_IMAGE_TYPE_RGBA = 5;
31
+ var DEFevol = 3;
32
+ var DEFwmimax = 6;
33
+ var DEFwminext = 2048;
34
+ var need_init = true;
35
+ var DEFmaxclen = 26;
36
+ var evol = DEFevol;
37
+ var wmimax = DEFwmimax;
38
+ var wminext = DEFwminext;
39
+ var family_5bpc = { nGRcodewords:[0,0,0,0,0,0,0,0],
40
+ notGRcwlen:[0,0,0,0,0,0,0,0],
41
+ notGRprefixmask:[0,0,0,0,0,0,0,0],
42
+ notGRsuffixlen:[0,0,0,0,0,0,0,0],
43
+ xlatU2L:[0,0,0,0,0,0,0,0],
44
+ xlatL2U:[0,0,0,0,0,0,0,0]
45
+ };
46
+ var family_8bpc = { nGRcodewords:[0,0,0,0,0,0,0,0],
47
+ notGRcwlen:[0,0,0,0,0,0,0,0],
48
+ notGRprefixmask:[0,0,0,0,0,0,0,0],
49
+ notGRsuffixlen:[0,0,0,0,0,0,0,0],
50
+ xlatU2L:[0,0,0,0,0,0,0,0],
51
+ xlatL2U:[0,0,0,0,0,0,0,0]
52
+ };
53
+ var bppmask = [ 0x00000000,
54
+ 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
55
+ 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
56
+ 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
57
+ 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
58
+ 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
59
+ 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
60
+ 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
61
+ 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff];
62
+
63
+ var zeroLUT = [];
64
+
65
+ var besttrigtab = [
66
+ [ 550, 900, 800, 700, 500, 350, 300, 200, 180, 180, 160],
67
+ [ 110, 550, 900, 800, 550, 400, 350, 250, 140, 160, 140],
68
+ [ 100, 120, 550, 900, 700, 500, 400, 300, 220, 250, 160]];
69
+
70
+ var J = [ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6,
71
+ 7, 7, 8, 9, 10, 11, 12, 13, 14, 15];
72
+
73
+ var lzeroes = [
74
+ 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3,
75
+ 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
77
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79
+ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84
+ 0, 0, 0, 0, 0, 0];
85
+
86
+ var tabrand_chaos = [
87
+ 0x02c57542, 0x35427717, 0x2f5a2153, 0x9244f155, 0x7bd26d07, 0x354c6052,
88
+ 0x57329b28, 0x2993868e, 0x6cd8808c, 0x147b46e0, 0x99db66af, 0xe32b4cac,
89
+ 0x1b671264, 0x9d433486, 0x62a4c192, 0x06089a4b, 0x9e3dce44, 0xdaabee13,
90
+ 0x222425ea, 0xa46f331d, 0xcd589250, 0x8bb81d7f, 0xc8b736b9, 0x35948d33,
91
+ 0xd7ac7fd0, 0x5fbe2803, 0x2cfbc105, 0x013dbc4e, 0x7a37820f, 0x39f88e9e,
92
+ 0xedd58794, 0xc5076689, 0xfcada5a4, 0x64c2f46d, 0xb3ba3243, 0x8974b4f9,
93
+ 0x5a05aebd, 0x20afcd00, 0x39e2b008, 0x88a18a45, 0x600bde29, 0xf3971ace,
94
+ 0xf37b0a6b, 0x7041495b, 0x70b707ab, 0x06beffbb, 0x4206051f, 0xe13c4ee3,
95
+ 0xc1a78327, 0x91aa067c, 0x8295f72a, 0x732917a6, 0x1d871b4d, 0x4048f136,
96
+ 0xf1840e7e, 0x6a6048c1, 0x696cb71a, 0x7ff501c3, 0x0fc6310b, 0x57e0f83d,
97
+ 0x8cc26e74, 0x11a525a2, 0x946934c7, 0x7cd888f0, 0x8f9d8604, 0x4f86e73b,
98
+ 0x04520316, 0xdeeea20c, 0xf1def496, 0x67687288, 0xf540c5b2, 0x22401484,
99
+ 0x3478658a, 0xc2385746, 0x01979c2c, 0x5dad73c8, 0x0321f58b, 0xf0fedbee,
100
+ 0x92826ddf, 0x284bec73, 0x5b1a1975, 0x03df1e11, 0x20963e01, 0xa17cf12b,
101
+ 0x740d776e, 0xa7a6bf3c, 0x01b5cce4, 0x1118aa76, 0xfc6fac0a, 0xce927e9b,
102
+ 0x00bf2567, 0x806f216c, 0xbca69056, 0x795bd3e9, 0xc9dc4557, 0x8929b6c2,
103
+ 0x789d52ec, 0x3f3fbf40, 0xb9197368, 0xa38c15b5, 0xc3b44fa8, 0xca8333b0,
104
+ 0xb7e8d590, 0xbe807feb, 0xbf5f8360, 0xd99e2f5c, 0x372928e1, 0x7c757c4c,
105
+ 0x0db5b154, 0xc01ede02, 0x1fc86e78, 0x1f3985be, 0xb4805c77, 0x00c880fa,
106
+ 0x974c1b12, 0x35ab0214, 0xb2dc840d, 0x5b00ae37, 0xd313b026, 0xb260969d,
107
+ 0x7f4c8879, 0x1734c4d3, 0x49068631, 0xb9f6a021, 0x6b863e6f, 0xcee5debf,
108
+ 0x29f8c9fb, 0x53dd6880, 0x72b61223, 0x1f67a9fd, 0x0a0f6993, 0x13e59119,
109
+ 0x11cca12e, 0xfe6b6766, 0x16b6effc, 0x97918fc4, 0xc2b8a563, 0x94f2f741,
110
+ 0x0bfa8c9a, 0xd1537ae8, 0xc1da349c, 0x873c60ca, 0x95005b85, 0x9b5c080e,
111
+ 0xbc8abbd9, 0xe1eab1d2, 0x6dac9070, 0x4ea9ebf1, 0xe0cf30d4, 0x1ef5bd7b,
112
+ 0xd161043e, 0x5d2fa2e2, 0xff5d3cae, 0x86ed9f87, 0x2aa1daa1, 0xbd731a34,
113
+ 0x9e8f4b22, 0xb1c2c67a, 0xc21758c9, 0xa182215d, 0xccb01948, 0x8d168df7,
114
+ 0x04238cfe, 0x368c3dbc, 0x0aeadca5, 0xbad21c24, 0x0a71fee5, 0x9fc5d872,
115
+ 0x54c152c6, 0xfc329483, 0x6783384a, 0xeddb3e1c, 0x65f90e30, 0x884ad098,
116
+ 0xce81675a, 0x4b372f7d, 0x68bf9a39, 0x43445f1e, 0x40f8d8cb, 0x90d5acb6,
117
+ 0x4cd07282, 0x349eeb06, 0x0c9d5332, 0x520b24ef, 0x80020447, 0x67976491,
118
+ 0x2f931ca3, 0xfe9b0535, 0xfcd30220, 0x61a9e6cc, 0xa487d8d7, 0x3f7c5dd1,
119
+ 0x7d0127c5, 0x48f51d15, 0x60dea871, 0xc9a91cb7, 0x58b53bb3, 0x9d5e0b2d,
120
+ 0x624a78b4, 0x30dbee1b, 0x9bdf22e7, 0x1df5c299, 0x2d5643a7, 0xf4dd35ff,
121
+ 0x03ca8fd6, 0x53b47ed8, 0x6f2c19aa, 0xfeb0c1f4, 0x49e54438, 0x2f2577e6,
122
+ 0xbf876969, 0x72440ea9, 0xfa0bafb8, 0x74f5b3a0, 0x7dd357cd, 0x89ce1358,
123
+ 0x6ef2cdda, 0x1e7767f3, 0xa6be9fdb, 0x4f5f88f8, 0xba994a3a, 0x08ca6b65,
124
+ 0xe0893818, 0x9e00a16a, 0xf42bfc8f, 0x9972eedc, 0x749c8b51, 0x32c05f5e,
125
+ 0xd706805f, 0x6bfbb7cf, 0xd9210a10, 0x31a1db97, 0x923a9559, 0x37a7a1f6,
126
+ 0x059f8861, 0xca493e62, 0x65157e81, 0x8f6467dd, 0xab85ff9f, 0x9331aff2,
127
+ 0x8616b9f5, 0xedbd5695, 0xee7e29b1, 0x313ac44f, 0xb903112f, 0x432ef649,
128
+ 0xdc0a36c0, 0x61cf2bba, 0x81474925, 0xa8b6c7ad, 0xee5931de, 0xb2f8158d,
129
+ 0x59fb7409, 0x2e3dfaed, 0x9af25a3f, 0xe1fed4d5 ];
130
+
131
+ var rgb32_pixel_pad = 3;
132
+ var rgb32_pixel_r = 2;
133
+ var rgb32_pixel_g = 1;
134
+ var rgb32_pixel_b = 0;
135
+ var rgb32_pixel_size = 4;
136
+
137
+ /* Helper Functions */
138
+
139
+ function ceil_log_2(val)
140
+ {
141
+ if (val === 1)
142
+ return 0;
143
+
144
+ var result = 1;
145
+ val -= 1;
146
+ while (val = val >>> 1)
147
+ result++;
148
+
149
+ return result;
150
+ }
151
+
152
+ function family_init(family, bpc, limit)
153
+ {
154
+ var l;
155
+ for (l = 0; l < bpc; l++)
156
+ {
157
+ var altprefixlen, altcodewords;
158
+ altprefixlen = limit - bpc;
159
+ if (altprefixlen > bppmask[bpc - l])
160
+ altprefixlen = bppmask[bpc - l];
161
+
162
+ altcodewords = bppmask[bpc] + 1 - (altprefixlen << l);
163
+ family.nGRcodewords[l] = (altprefixlen << l);
164
+ family.notGRcwlen[l] = altprefixlen + ceil_log_2(altcodewords);
165
+ family.notGRprefixmask[l] = bppmask[32 - altprefixlen]>>>0;
166
+ family.notGRsuffixlen[l] = ceil_log_2(altcodewords);
167
+ }
168
+
169
+ /* decorelate_init */
170
+ var pixelbitmask = bppmask[bpc];
171
+ var pixelbitmaskshr = pixelbitmask >>> 1;
172
+ var s;
173
+ for (s = 0; s <= pixelbitmask; s++) {
174
+ if (s <= pixelbitmaskshr) {
175
+ family.xlatU2L[s] = s << 1;
176
+ } else {
177
+ family.xlatU2L[s] = ((pixelbitmask - s) << 1) + 1;
178
+ }
179
+ }
180
+
181
+ /* corelate_init */
182
+ for (s = 0; s <= pixelbitmask; s++) {
183
+ if (s & 0x01) {
184
+ family.xlatL2U[s] = pixelbitmask - (s >>> 1);
185
+ } else {
186
+ family.xlatL2U[s] = (s >>> 1);
187
+ }
188
+ }
189
+ }
190
+
191
+ function quic_image_bpc(type)
192
+ {
193
+ switch (type) {
194
+ case QUIC_IMAGE_TYPE_GRAY:
195
+ return 8;
196
+ case QUIC_IMAGE_TYPE_RGB16:
197
+ return 5;
198
+ case QUIC_IMAGE_TYPE_RGB24:
199
+ return 8;
200
+ case QUIC_IMAGE_TYPE_RGB32:
201
+ return 8;
202
+ case QUIC_IMAGE_TYPE_RGBA:
203
+ return 8;
204
+ case QUIC_IMAGE_TYPE_INVALID:
205
+ default:
206
+ console.log("quic: bad image type\n");
207
+ return 0;
208
+ }
209
+ }
210
+
211
+ function cnt_l_zeroes(bits)
212
+ {
213
+ if (bits & 0xff800000) {
214
+ return lzeroes[bits >>> 24];
215
+ } else if (bits & 0xffff8000) {
216
+ return 8 + lzeroes[(bits >>> 16) & 0x000000ff];
217
+ } else if (bits & 0xffffff80) {
218
+ return 16 + lzeroes[(bits >>> 8) & 0x000000ff];
219
+ } else {
220
+ return 24 + lzeroes[bits & 0x000000ff];
221
+ }
222
+ }
223
+
224
+ function golomb_decoding_8bpc(l, bits)
225
+ {
226
+ var rc;
227
+ var cwlen;
228
+
229
+ if (bits < 0 || bits > family_8bpc.notGRprefixmask[l])
230
+ {
231
+ var zeroprefix = cnt_l_zeroes(bits);
232
+ cwlen = zeroprefix + 1 + l;
233
+ rc = (zeroprefix << l) | (bits >> (32-cwlen)) & bppmask[l];
234
+ }
235
+ else
236
+ {
237
+ cwlen = family_8bpc.notGRcwlen[l];
238
+ rc = family_8bpc.nGRcodewords[l] + ((bits >> (32-cwlen)) & bppmask[family_8bpc.notGRsuffixlen[l]]);
239
+ }
240
+ return {'codewordlen':cwlen, 'rc':rc};
241
+ }
242
+
243
+ function golomb_code_len_8bpc(n, l)
244
+ {
245
+ if (n < family_8bpc.nGRcodewords[l]) {
246
+ return (n >>> l) + 1 + l;
247
+ } else {
248
+ return family_8bpc.notGRcwlen[l];
249
+ }
250
+ }
251
+
252
+ function QuicModel(bpc)
253
+ {
254
+ var bstart;
255
+ var bend = 0;
256
+
257
+ this.levels = 0x1 << bpc;
258
+ this.n_buckets_ptrs = 0;
259
+
260
+ switch (evol) {
261
+ case 1:
262
+ this.repfirst = 3;
263
+ this.firstsize = 1;
264
+ this.repnext = 2;
265
+ this.mulsize = 2;
266
+ break;
267
+ case 3:
268
+ this.repfirst = 1;
269
+ this.firstsize = 1;
270
+ this.repnext = 1;
271
+ this.mulsize = 2;
272
+ break;
273
+ case 5:
274
+ this.repfirst = 1;
275
+ this.firstsize = 1;
276
+ this.repnext = 1;
277
+ this.mulsize = 4;
278
+ break;
279
+ case 0:
280
+ case 2:
281
+ case 4:
282
+ console.log("quic: findmodelparams(): evol value obsolete!!!\n");
283
+ default:
284
+ console.log("quic: findmodelparams(): evol out of range!!!\n");
285
+ }
286
+
287
+ this.n_buckets = 0;
288
+ var repcntr = this.repfirst + 1;
289
+ var bsize = this.firstsize;
290
+
291
+ do {
292
+ if (this.n_buckets) {
293
+ bstart = bend + 1;
294
+ } else {
295
+ bstart = 0;
296
+ }
297
+
298
+ if (!--repcntr) {
299
+ repcntr = this.repnext;
300
+ bsize *= this.mulsize;
301
+ }
302
+
303
+ bend = bstart + bsize - 1;
304
+ if (bend + bsize >= this.levels) {
305
+ bend = this.levels - 1;
306
+ }
307
+
308
+ if (!this.n_buckets_ptrs) {
309
+ this.n_buckets_ptrs = this.levels;
310
+ }
311
+
312
+ (this.n_buckets)++;
313
+ } while (bend < this.levels - 1);
314
+ }
315
+
316
+ QuicModel.prototype = {
317
+ n_buckets : 0,
318
+ n_buckets_ptrs : 0,
319
+ repfirst : 0,
320
+ firstsize : 0,
321
+ repnext : 0,
322
+ mulsize : 0,
323
+ levels :0
324
+ }
325
+
326
+ function QuicBucket()
327
+ {
328
+ this.counters = [0,0,0,0,0,0,0,0];
329
+ }
330
+
331
+ QuicBucket.prototype = {
332
+ bestcode: 0,
333
+
334
+ reste : function (bpp)
335
+ {
336
+ this.bestcode = bpp;
337
+ this.counters = [0,0,0,0,0,0,0,0];
338
+ },
339
+
340
+ update_model_8bpc : function (state, curval, bpp)
341
+ {
342
+ var i;
343
+
344
+ var bestcode = bpp - 1;
345
+ var bestcodelen = (this.counters[bestcode] += golomb_code_len_8bpc(curval, bestcode));
346
+
347
+ for (i = bpp - 2; i >= 0; i--) {
348
+ var ithcodelen = (this.counters[i] += golomb_code_len_8bpc(curval, i));
349
+
350
+ if (ithcodelen < bestcodelen) {
351
+ bestcode = i;
352
+ bestcodelen = ithcodelen;
353
+ }
354
+ }
355
+
356
+ this.bestcode = bestcode;
357
+
358
+ if (bestcodelen > state.wm_trigger) {
359
+ for (i = 0; i < bpp; i++) {
360
+ this.counters[i] = this.counters[i] >>> 1;
361
+ }
362
+ }
363
+ }
364
+ }
365
+
366
+ function QuicFamilyStat()
367
+ {
368
+ this.buckets_ptrs = [];
369
+ this.buckets_buf = [];
370
+ }
371
+
372
+ QuicFamilyStat.prototype = {
373
+
374
+ fill_model_structures : function(model)
375
+ {
376
+ var bstart;
377
+ var bend = 0;
378
+ var bnumber = 0;
379
+
380
+ var repcntr = model.repfirst + 1;
381
+ var bsize = model.firstsize;
382
+
383
+ do {
384
+ if (bnumber) {
385
+ bstart = bend + 1;
386
+ } else {
387
+ bstart = 0;
388
+ }
389
+
390
+ if (!--repcntr) {
391
+ repcntr = model.repnext;
392
+ bsize *= model.mulsize;
393
+ }
394
+
395
+ bend = bstart + bsize - 1;
396
+ if (bend + bsize >= model.levels) {
397
+ bend = model.levels - 1;
398
+ }
399
+
400
+ this.buckets_buf[bnumber] = new QuicBucket;
401
+
402
+ var i;
403
+ for (i = bstart; i <= bend; i++) {
404
+ this.buckets_ptrs[i] = this.buckets_buf[bnumber];
405
+ }
406
+
407
+ bnumber++;
408
+ } while (bend < model.levels - 1);
409
+ return true;
410
+ }
411
+ }
412
+
413
+ function QuicChannel(model_8bpc, model_5bpc)
414
+ {
415
+ this.state = new CommonState;
416
+ this.family_stat_8bpc = new QuicFamilyStat;
417
+ this.family_stat_5bpc = new QuicFamilyStat;
418
+ this.correlate_row = { zero: 0 , row:[] };
419
+ this.model_8bpc = model_8bpc;
420
+ this.model_5bpc = model_5bpc;
421
+ this.buckets_ptrs = [];
422
+
423
+ if (!this.family_stat_8bpc.fill_model_structures(this.model_8bpc))
424
+ return undefined;
425
+
426
+ if (!this.family_stat_5bpc.fill_model_structures(this.model_5bpc))
427
+ return undefined;
428
+ }
429
+
430
+ QuicChannel.prototype = {
431
+
432
+ reste : function (bpc)
433
+ {
434
+ var j;
435
+ this.correlate_row = { zero: 0 , row: []};
436
+
437
+ if (bpc == 8) {
438
+ for (j = 0; j < this.model_8bpc.n_buckets; j++)
439
+ this.family_stat_8bpc.buckets_buf[j].reste(7);
440
+ this.buckets_ptrs = this.family_stat_8bpc.buckets_ptrs;
441
+ } else if (bpc == 5) {
442
+ for (j = 0; j < this.model_5bpc.n_buckets; j++)
443
+ this.family_stat_8bpc.buckets_buf[j].reste(4);
444
+ this.buckets_ptrs = this.family_stat_5bpc.buckets_ptrs;
445
+ } else {
446
+ console.log("quic: %s: bad bpc %d\n", __FUNCTION__, bpc);
447
+ return false;
448
+ }
449
+
450
+ this.state.reste();
451
+ return true;
452
+ }
453
+ }
454
+
455
+ function CommonState()
456
+ {
457
+ }
458
+
459
+ CommonState.prototype = {
460
+ waitcnt: 0,
461
+ tabrand_seed: 0xff,
462
+ wm_trigger: 0,
463
+ wmidx: 0,
464
+ wmileft: wminext,
465
+ melcstate: 0,
466
+ melclen: 0,
467
+ melcorder: 0,
468
+
469
+ set_wm_trigger : function()
470
+ {
471
+ var wm = this.wmidx;
472
+ if (wm > 10) {
473
+ wm = 10;
474
+ }
475
+
476
+ this.wm_trigger = besttrigtab[Math.floor(evol / 2)][wm];
477
+ },
478
+
479
+ reste : function()
480
+ {
481
+ this.waitcnt = 0;
482
+ this.tabrand_seed = 0x0ff;
483
+ this.wmidx = 0;
484
+ this.wmileft = wminext;
485
+
486
+ this.set_wm_trigger();
487
+
488
+ this.melcstate = 0;
489
+ this.melclen = J[0];
490
+ this.melcorder = 1 << this.melclen;
491
+ },
492
+
493
+ tabrand : function()
494
+ {
495
+ this.tabrand_seed++;
496
+ return tabrand_chaos[this.tabrand_seed & 0x0ff];
497
+ }
498
+ }
499
+
500
+
501
+ function QuicEncoder()
502
+ {
503
+ this.rgb_state = new CommonState;
504
+ this.model_8bpc = new QuicModel(8);
505
+ this.model_5bpc = new QuicModel(5);
506
+ this.channels = [];
507
+
508
+ var i;
509
+ for (i = 0; i < 4; i++) {
510
+ this.channels[i] = new QuicChannel(this.model_8bpc, this.model_5bpc);
511
+ if (!this.channels[i])
512
+ {
513
+ console.log("quic: failed to create channel");
514
+ return undefined;
515
+ }
516
+ }
517
+ }
518
+
519
+ QuicEncoder.prototype = {
520
+ type: 0,
521
+ width: 0,
522
+ height: 0,
523
+ io_idx: 0,
524
+ io_available_bits: 0,
525
+ io_word: 0,
526
+ io_next_word: 0,
527
+ io_now: 0,
528
+ io_end: 0,
529
+ rows_completed: 0,
530
+ };
531
+
532
+ QuicEncoder.prototype.reste = function(io_ptr)
533
+ {
534
+ this.rgb_state.reste();
535
+
536
+ this.io_now = io_ptr;
537
+ this.io_end = this.io_now.length;
538
+ this.io_idx = 0;
539
+ this.rows_completed = 0;
540
+ return true;
541
+ }
542
+
543
+ QuicEncoder.prototype.read_io_word = function()
544
+ {
545
+ if (this.io_idx >= this.io_end)
546
+ throw("quic: out of data");
547
+ this.io_next_word = this.io_now[this.io_idx++] | this.io_now[this.io_idx++]<<8 | this.io_now[this.io_idx++]<<16 | this.io_now[this.io_idx++]<<24;
548
+ }
549
+
550
+ QuicEncoder.prototype.decode_eatbits = function (len)
551
+ {
552
+ this.io_word = this.io_word << len;
553
+
554
+ var delta = (this.io_available_bits - len);
555
+ if (delta >= 0)
556
+ {
557
+ this.io_available_bits = delta;
558
+ this.io_word |= this.io_next_word >>> this.io_available_bits;
559
+ }
560
+ else
561
+ {
562
+ delta = -1 * delta;
563
+ this.io_word |= this.io_next_word << delta;
564
+ this.read_io_word();
565
+ this.io_available_bits = 32 - delta;
566
+ this.io_word |= this.io_next_word >>> this.io_available_bits;
567
+ }
568
+ }
569
+
570
+ QuicEncoder.prototype.decode_eat32bits = function()
571
+ {
572
+ this.decode_eatbits(16);
573
+ this.decode_eatbits(16);
574
+ }
575
+
576
+ QuicEncoder.prototype.reste_channels = function(bpc)
577
+ {
578
+ var i;
579
+
580
+ for (i = 0; i < 4; i++)
581
+ if (!this.channels[i].reste(bpc))
582
+ return false;
583
+ return true;
584
+ }
585
+
586
+ QuicEncoder.prototype.quic_decode_begin = function(io_ptr)
587
+ {
588
+ if (!this.reste(io_ptr)) {
589
+ return false;
590
+ }
591
+
592
+ this.io_idx = 0;
593
+ this.io_next_word = this.io_now[this.io_idx++] | this.io_now[this.io_idx++]<<8 | this.io_now[this.io_idx++]<<16 | this.io_now[this.io_idx++]<<24;
594
+ this.io_word = this.io_next_word;
595
+ this.io_available_bits = 0;
596
+
597
+ var magic = this.io_word;
598
+ this.decode_eat32bits();
599
+ if (magic != 0x43495551) /*QUIC*/ {
600
+ console.log("quic: bad magic "+magic.toString(16));
601
+ return false;
602
+ }
603
+
604
+ var version = this.io_word;
605
+ this.decode_eat32bits();
606
+ if (version != ((0 << 16) | (0 & 0xffff))) {
607
+ console.log("quic: bad version "+version.toString(16));
608
+ return false;
609
+ }
610
+
611
+ this.type = this.io_word;
612
+ this.decode_eat32bits();
613
+
614
+ this.width = this.io_word;
615
+ this.decode_eat32bits();
616
+
617
+ this.height = this.io_word;
618
+ this.decode_eat32bits();
619
+
620
+ var bpc = quic_image_bpc(this.type);
621
+
622
+ if (!this.reste_channels(bpc))
623
+ return false;
624
+
625
+ return true;
626
+ }
627
+
628
+ QuicEncoder.prototype.quic_rgb32_uncompress_row0_seg = function (i, cur_row, end,
629
+ waitmask, bpc, bpc_mask)
630
+ {
631
+ var stopidx;
632
+ var n_channels = 3;
633
+ var c;
634
+ var a;
635
+
636
+ if (!i) {
637
+ cur_row[rgb32_pixel_pad] = 0;
638
+ c = 0;
639
+ do
640
+ {
641
+ a = golomb_decoding_8bpc(this.channels[c].buckets_ptrs[this.channels[c].correlate_row.zero].bestcode, this.io_word);
642
+ this.channels[c].correlate_row.row[0] = a.rc;
643
+ cur_row[2-c] = (family_8bpc.xlatL2U[a.rc]&0xFF);
644
+ this.decode_eatbits(a.codewordlen);
645
+ } while (++c < n_channels);
646
+
647
+ if (this.rgb_state.waitcnt) {
648
+ --this.rgb_state.waitcnt;
649
+ } else {
650
+ this.rgb_state.waitcnt = (this.rgb_state.tabrand() & waitmask);
651
+ c = 0;
652
+ do
653
+ {
654
+ this.channels[c].buckets_ptrs[this.channels[c].correlate_row.zero].update_model_8bpc(this.rgb_state, this.channels[c].correlate_row.row[0], bpc);
655
+ } while (++c < n_channels);
656
+ }
657
+ stopidx = ++i + this.rgb_state.waitcnt;
658
+ } else {
659
+ stopidx = i + this.rgb_state.waitcnt;
660
+ }
661
+
662
+ while (stopidx < end) {
663
+ for (; i <= stopidx; i++) {
664
+ cur_row[(i* rgb32_pixel_size)+rgb32_pixel_pad] = 0;
665
+ c = 0;
666
+ do
667
+ {
668
+ a = golomb_decoding_8bpc(this.channels[c].buckets_ptrs[this.channels[c].correlate_row.row[i - 1]].bestcode, this.io_word);
669
+ this.channels[c].correlate_row.row[i] = a.rc;
670
+ cur_row[(i* rgb32_pixel_size)+(2-c)] = (family_8bpc.xlatL2U[a.rc] + cur_row[((i-1) * rgb32_pixel_size) + (2-c)]) & bpc_mask;
671
+ this.decode_eatbits(a.codewordlen);
672
+ } while (++c < n_channels);
673
+ }
674
+ c = 0;
675
+ do
676
+ {
677
+ this.channels[c].buckets_ptrs[this.channels[c].correlate_row.row[stopidx - 1]].update_model_8bpc(this.rgb_state, this.channels[c].correlate_row.row[stopidx], bpc);
678
+ } while (++c < n_channels);
679
+ stopidx = i + (this.rgb_state.tabrand() & waitmask);
680
+ }
681
+
682
+ for (; i < end; i++) {
683
+ cur_row[(i* rgb32_pixel_size)+rgb32_pixel_pad] = 0;
684
+ c = 0;
685
+ do
686
+ {
687
+ a = golomb_decoding_8bpc(this.channels[c].buckets_ptrs[this.channels[c].correlate_row.row[i - 1]].bestcode, this.io_word);
688
+ this.channels[c].correlate_row.row[i] = a.rc;
689
+ cur_row[(i* rgb32_pixel_size)+(2-c)] = (family_8bpc.xlatL2U[a.rc] + cur_row[((i-1) * rgb32_pixel_size) + (2-c)]) & bpc_mask;
690
+ this.decode_eatbits(a.codewordlen);
691
+ } while (++c < n_channels);
692
+ }
693
+ this.rgb_state.waitcnt = stopidx - end;
694
+ }
695
+
696
+ QuicEncoder.prototype.quic_rgb32_uncompress_row0 = function (cur_row)
697
+ {
698
+ var bpc = 8;
699
+ var bpc_mask = 0xff;
700
+ var pos = 0;
701
+ var width = this.width;
702
+
703
+ while ((wmimax > this.rgb_state.wmidx) && (this.rgb_state.wmileft <= width)) {
704
+ if (this.rgb_state.wmileft) {
705
+ this.quic_rgb32_uncompress_row0_seg(pos, cur_row,
706
+ pos + this.rgb_state.wmileft,
707
+ bppmask[this.rgb_state.wmidx],
708
+ bpc, bpc_mask);
709
+ pos += this.rgb_state.wmileft;
710
+ width -= this.rgb_state.wmileft;
711
+ }
712
+
713
+ this.rgb_state.wmidx++;
714
+ this.rgb_state.set_wm_trigger();
715
+ this.rgb_state.wmileft = wminext;
716
+ }
717
+
718
+ if (width) {
719
+ this.quic_rgb32_uncompress_row0_seg(pos, cur_row, pos + width,
720
+ bppmask[this.rgb_state.wmidx], bpc, bpc_mask);
721
+ if (wmimax > this.rgb_state.wmidx) {
722
+ this.rgb_state.wmileft -= width;
723
+ }
724
+ }
725
+ }
726
+
727
+ QuicEncoder.prototype.quic_rgb32_uncompress_row_seg = function( prev_row, cur_row, i, end, bpc, bpc_mask)
728
+ {
729
+ var n_channels = 3;
730
+ var waitmask = bppmask[this.rgb_state.wmidx];
731
+
732
+ var a;
733
+ var run_index = 0;
734
+ var stopidx = 0;
735
+ var run_end = 0;
736
+ var c;
737
+
738
+ if (!i)
739
+ {
740
+ cur_row[rgb32_pixel_pad] = 0;
741
+
742
+ c = 0;
743
+ do {
744
+ a = golomb_decoding_8bpc(this.channels[c].buckets_ptrs[this.channels[c].correlate_row.zero].bestcode, this.io_word);
745
+ this.channels[c].correlate_row.row[0] = a.rc;
746
+ cur_row[2-c] = (family_8bpc.xlatL2U[this.channels[c].correlate_row.row[0]] + prev_row[2-c]) & bpc_mask;
747
+ this.decode_eatbits(a.codewordlen);
748
+ } while (++c < n_channels);
749
+
750
+ if (this.rgb_state.waitcnt) {
751
+ --this.rgb_state.waitcnt;
752
+ } else {
753
+ this.rgb_state.waitcnt = (this.rgb_state.tabrand() & waitmask);
754
+ c = 0;
755
+ do {
756
+ this.channels[c].buckets_ptrs[this.channels[c].correlate_row.zero].update_model_8bpc(this.rgb_state, this.channels[c].correlate_row.row[0], bpc);
757
+ } while (++c < n_channels);
758
+ }
759
+ stopidx = ++i + this.rgb_state.waitcnt;
760
+ } else {
761
+ stopidx = i + this.rgb_state.waitcnt;
762
+ }
763
+ for (;;) {
764
+ var rc = 0;
765
+ while (stopidx < end && !rc) {
766
+ for (; i <= stopidx && !rc; i++) {
767
+ var pixel = i * rgb32_pixel_size;
768
+ var pixelm1 = (i-1) * rgb32_pixel_size;
769
+ var pixelm2 = (i-2) * rgb32_pixel_size;
770
+
771
+ if ( prev_row[pixelm1+rgb32_pixel_r] == prev_row[pixel+rgb32_pixel_r] && prev_row[pixelm1+rgb32_pixel_g] == prev_row[pixel+rgb32_pixel_g] && prev_row[pixelm1 + rgb32_pixel_b] == prev_row[pixel+rgb32_pixel_b])
772
+ {
773
+ if (run_index != i && i > 2 && (cur_row[pixelm1+rgb32_pixel_r] == cur_row[pixelm2+rgb32_pixel_r] && cur_row[pixelm1+rgb32_pixel_g] == cur_row[pixelm2+rgb32_pixel_g] && cur_row[pixelm1+rgb32_pixel_b] == cur_row[pixelm2+rgb32_pixel_b]))
774
+ {
775
+ /* do run */
776
+ this.rgb_state.waitcnt = stopidx - i;
777
+ run_index = i;
778
+ run_end = i + this.decode_run(this.rgb_state);
779
+
780
+ for (; i < run_end; i++) {
781
+ var pixel = i * rgb32_pixel_size;
782
+ var pixelm1 = (i-1) * rgb32_pixel_size;
783
+ cur_row[pixel+rgb32_pixel_pad] = 0;
784
+ cur_row[pixel+rgb32_pixel_r] = cur_row[pixelm1+rgb32_pixel_r];
785
+ cur_row[pixel+rgb32_pixel_g] = cur_row[pixelm1+rgb32_pixel_g];
786
+ cur_row[pixel+rgb32_pixel_b] = cur_row[pixelm1+rgb32_pixel_b];
787
+ }
788
+
789
+ if (i == end) {
790
+ return;
791
+ }
792
+ else
793
+ {
794
+ stopidx = i + this.rgb_state.waitcnt;
795
+ rc = 1;
796
+ break;
797
+ }
798
+ }
799
+ }
800
+
801
+ c = 0;
802
+ cur_row[pixel+rgb32_pixel_pad] = 0;
803
+ do {
804
+ var cc = this.channels[c];
805
+ var cr = cc.correlate_row;
806
+
807
+ a = golomb_decoding_8bpc(cc.buckets_ptrs[cr.row[i-1]].bestcode, this.io_word);
808
+ cr.row[i] = a.rc;
809
+ cur_row[pixel+(2-c)] = (family_8bpc.xlatL2U[a.rc] + ((cur_row[pixelm1+(2-c)] + prev_row[pixel+(2-c)]) >> 1)) & bpc_mask;
810
+ this.decode_eatbits(a.codewordlen);
811
+ } while (++c < n_channels);
812
+ }
813
+ if (rc)
814
+ break;
815
+
816
+ c = 0;
817
+ do {
818
+ this.channels[c].buckets_ptrs[this.channels[c].correlate_row.row[stopidx - 1]].update_model_8bpc(this.rgb_state, this.channels[c].correlate_row.row[stopidx], bpc);
819
+ } while (++c < n_channels);
820
+
821
+ stopidx = i + (this.rgb_state.tabrand() & waitmask);
822
+ }
823
+
824
+ for (; i < end && !rc; i++) {
825
+ var pixel = i * rgb32_pixel_size;
826
+ var pixelm1 = (i-1) * rgb32_pixel_size;
827
+ var pixelm2 = (i-2) * rgb32_pixel_size;
828
+
829
+ if (prev_row[pixelm1+rgb32_pixel_r] == prev_row[pixel+rgb32_pixel_r] && prev_row[pixelm1+rgb32_pixel_g] == prev_row[pixel+rgb32_pixel_g] && prev_row[pixelm1+rgb32_pixel_b] == prev_row[pixel+rgb32_pixel_b])
830
+ {
831
+ if (run_index != i && i > 2 && (cur_row[pixelm1+rgb32_pixel_r] == cur_row[pixelm2+rgb32_pixel_r] && cur_row[pixelm1+rgb32_pixel_g] == cur_row[pixelm2+rgb32_pixel_g] && cur_row[pixelm1+rgb32_pixel_b] == cur_row[pixelm2+rgb32_pixel_b]))
832
+ {
833
+ /* do run */
834
+ this.rgb_state.waitcnt = stopidx - i;
835
+ run_index = i;
836
+ run_end = i + this.decode_run(this.rgb_state);
837
+
838
+ for (; i < run_end; i++) {
839
+ var pixel = i * rgb32_pixel_size;
840
+ var pixelm1 = (i-1) * rgb32_pixel_size;
841
+ cur_row[pixel+rgb32_pixel_pad] = 0;
842
+ cur_row[pixel+rgb32_pixel_r] = cur_row[pixelm1+rgb32_pixel_r];
843
+ cur_row[pixel+rgb32_pixel_g] = cur_row[pixelm1+rgb32_pixel_g];
844
+ cur_row[pixel+rgb32_pixel_b] = cur_row[pixelm1+rgb32_pixel_b];
845
+ }
846
+
847
+ if (i == end) {
848
+ return;
849
+ }
850
+ else
851
+ {
852
+ stopidx = i + this.rgb_state.waitcnt;
853
+ rc = 1;
854
+ break;
855
+ }
856
+ }
857
+ }
858
+
859
+ cur_row[pixel+rgb32_pixel_pad] = 0;
860
+ c = 0;
861
+ do
862
+ {
863
+ a = golomb_decoding_8bpc(this.channels[c].buckets_ptrs[this.channels[c].correlate_row.row[i-1]].bestcode, this.io_word);
864
+ this.channels[c].correlate_row.row[i] = a.rc;
865
+ cur_row[pixel+(2-c)] = (family_8bpc.xlatL2U[a.rc] + ((cur_row[pixelm1+(2-c)] + prev_row[pixel+(2-c)]) >> 1)) & bpc_mask;
866
+ this.decode_eatbits(a.codewordlen);
867
+ } while (++c < n_channels);
868
+ }
869
+
870
+ if (!rc)
871
+ {
872
+ this.rgb_state.waitcnt = stopidx - end;
873
+ return;
874
+ }
875
+ }
876
+ }
877
+
878
+ QuicEncoder.prototype.decode_run = function(state)
879
+ {
880
+ var runlen = 0;
881
+
882
+ do {
883
+ var hits;
884
+ var x = (~(this.io_word >>> 24)>>>0)&0xff;
885
+ var temp = zeroLUT[x];
886
+
887
+ for (hits = 1; hits <= temp; hits++) {
888
+ runlen += state.melcorder;
889
+
890
+ if (state.melcstate < 32) {
891
+ state.melclen = J[++state.melcstate];
892
+ state.melcorder = (1 << state.melclen);
893
+ }
894
+ }
895
+ if (temp != 8) {
896
+ this.decode_eatbits(temp + 1);
897
+
898
+ break;
899
+ }
900
+ this.decode_eatbits(8);
901
+ } while (true);
902
+
903
+ if (state.melclen) {
904
+ runlen += this.io_word >>> (32 - state.melclen);
905
+ this.decode_eatbits(state.melclen);
906
+ }
907
+
908
+ if (state.melcstate) {
909
+ state.melclen = J[--state.melcstate];
910
+ state.melcorder = (1 << state.melclen);
911
+ }
912
+
913
+ return runlen;
914
+ }
915
+
916
+ QuicEncoder.prototype.quic_rgb32_uncompress_row = function (prev_row, cur_row)
917
+ {
918
+ var bpc = 8;
919
+ var bpc_mask = 0xff;
920
+ var pos = 0;
921
+ var width = this.width;
922
+
923
+ while ((wmimax > this.rgb_state.wmidx) && (this.rgb_state.wmileft <= width)) {
924
+ if (this.rgb_state.wmileft) {
925
+ this.quic_rgb32_uncompress_row_seg(prev_row, cur_row, pos,
926
+ pos + this.rgb_state.wmileft, bpc, bpc_mask);
927
+ pos += this.rgb_state.wmileft;
928
+ width -= this.rgb_state.wmileft;
929
+ }
930
+
931
+ this.rgb_state.wmidx++;
932
+ this.rgb_state.set_wm_trigger();
933
+ this.rgb_state.wmileft = wminext;
934
+ }
935
+
936
+ if (width) {
937
+ this.quic_rgb32_uncompress_row_seg(prev_row, cur_row, pos,
938
+ pos + width, bpc, bpc_mask);
939
+ if (wmimax > this.rgb_state.wmidx) {
940
+ this.rgb_state.wmileft -= width;
941
+ }
942
+ }
943
+ }
944
+
945
+ QuicEncoder.prototype.quic_four_uncompress_row0_seg = function (channel, i,
946
+ correlate_row, cur_row, end, waitmask,
947
+ bpc, bpc_mask)
948
+ {
949
+ var stopidx;
950
+ var a;
951
+
952
+ if (i == 0) {
953
+ a = golomb_decoding_8bpc(channel.buckets_ptrs[correlate_row.zero].bestcode, this.io_word);
954
+ correlate_row.row[0] = a.rc;
955
+ cur_row[rgb32_pixel_pad] = family_8bpc.xlatL2U[a.rc];
956
+ this.decode_eatbits(a.codewordlen);
957
+
958
+ if (channel.state.waitcnt) {
959
+ --channel.state.waitcnt;
960
+ } else {
961
+ channel.state.waitcnt = (channel.state.tabrand() & waitmask);
962
+ channel.buckets_ptrs[correlate_row.zero].update_model_8bpc(channel.state, correlate_row.row[0], bpc);
963
+ }
964
+ stopidx = ++i + channel.state.waitcnt;
965
+ } else {
966
+ stopidx = i + channel.state.waitcnt;
967
+ }
968
+
969
+ while (stopidx < end) {
970
+ var pbucket;
971
+
972
+ for (; i <= stopidx; i++) {
973
+ pbucket = channel.buckets_ptrs[correlate_row.row[i - 1]];
974
+
975
+ a = golomb_decoding_8bpc(pbucket.bestcode, this.io_word);
976
+ correlate_row.row[i] = a.rc;
977
+ cur_row[(i*rgb32_pixel_size)+rgb32_pixel_pad] = (family_8bpc.xlatL2U[a.rc] + cur_row[((i-1)*rgb32_pixel_size)+rgb32_pixel_pad]) & bpc_mask;
978
+ this.decode_eatbits(a.codewordlen);
979
+ }
980
+
981
+ pbucket.update_model_8bpc(channel.state, correlate_row.row[stopidx], bpc);
982
+
983
+ stopidx = i + (channel.state.tabrand() & waitmask);
984
+ }
985
+
986
+ for (; i < end; i++) {
987
+ a = golomb_decoding_8bpc(channel.buckets_ptrs[correlate_row.row[i-1]].bestcode, this.io_word);
988
+
989
+ correlate_row.row[i] = a.rc;
990
+ cur_row[(i*rgb32_pixel_size)+rgb32_pixel_pad] = (family_8bpc.xlatL2U[a.rc] + cur_row[((i-1)*rgb32_pixel_size)+rgb32_pixel_pad]) & bpc_mask;
991
+ this.decode_eatbits(a.codewordlen);
992
+ }
993
+ channel.state.waitcnt = stopidx - end;
994
+ }
995
+
996
+ QuicEncoder.prototype.quic_four_uncompress_row0 = function(channel, cur_row)
997
+ {
998
+ var bpc = 8;
999
+ var bpc_mask = 0xff;
1000
+ var correlate_row = channel.correlate_row;
1001
+ var pos = 0;
1002
+ var width = this.width;
1003
+
1004
+ while ((wmimax > channel.state.wmidx) && (channel.state.wmileft <= width)) {
1005
+ if (channel.state.wmileft) {
1006
+ this.quic_four_uncompress_row0_seg(channel, pos, correlate_row, cur_row,
1007
+ pos + channel.state.wmileft, bppmask[channel.state.wmidx],
1008
+ bpc, bpc_mask);
1009
+ pos += channel.state.wmileft;
1010
+ width -= channel.state.wmileft;
1011
+ }
1012
+
1013
+ channel.state.wmidx++;
1014
+ channel.state.set_wm_trigger();
1015
+ channel.state.wmileft = wminext;
1016
+ }
1017
+
1018
+ if (width) {
1019
+ this.quic_four_uncompress_row0_seg(channel, pos, correlate_row, cur_row, pos + width,
1020
+ bppmask[channel.state.wmidx], bpc, bpc_mask);
1021
+ if (wmimax > channel.state.wmidx) {
1022
+ channel.state.wmileft -= width;
1023
+ }
1024
+ }
1025
+ }
1026
+
1027
+ QuicEncoder.prototype.quic_four_uncompress_row_seg = function (channel,
1028
+ correlate_row, prev_row, cur_row, i,
1029
+ end, bpc, bpc_mask)
1030
+ {
1031
+ var waitmask = bppmask[channel.state.wmidx];
1032
+ var stopidx;
1033
+
1034
+ var run_index = 0;
1035
+ var run_end;
1036
+
1037
+ var a;
1038
+
1039
+ if (i == 0) {
1040
+ a = golomb_decoding_8bpc(channel.buckets_ptrs[correlate_row.zero].bestcode, this.io_word);
1041
+
1042
+ correlate_row.row[0] = a.rc
1043
+ cur_row[rgb32_pixel_pad] = (family_8bpc.xlatL2U[a.rc] + prev_row[rgb32_pixel_pad]) & bpc_mask;
1044
+ this.decode_eatbits(a.codewordlen);
1045
+
1046
+ if (channel.state.waitcnt) {
1047
+ --channel.state.waitcnt;
1048
+ } else {
1049
+ channel.state.waitcnt = (channel.state.tabrand() & waitmask);
1050
+ channel.buckets_ptrs[correlate_row.zero].update_model_8bpc(channel.state, correlate_row.row[0], bpc);
1051
+ }
1052
+ stopidx = ++i + channel.state.waitcnt;
1053
+ } else {
1054
+ stopidx = i + channel.state.waitcnt;
1055
+ }
1056
+ for (;;) {
1057
+ var rc = 0;
1058
+ while (stopidx < end && !rc) {
1059
+ var pbucket;
1060
+ for (; i <= stopidx && !rc; i++) {
1061
+ var pixel = i * rgb32_pixel_size;
1062
+ var pixelm1 = (i-1) * rgb32_pixel_size;
1063
+ var pixelm2 = (i-2) * rgb32_pixel_size;
1064
+
1065
+ if (prev_row[pixelm1+rgb32_pixel_pad] == prev_row[pixel+rgb32_pixel_pad])
1066
+ {
1067
+ if (run_index != i && i > 2 && cur_row[pixelm1+rgb32_pixel_pad] == cur_row[pixelm2+rgb32_pixel_pad])
1068
+ {
1069
+ /* do run */
1070
+ channel.state.waitcnt = stopidx - i;
1071
+ run_index = i;
1072
+
1073
+ run_end = i + this.decode_run(channel.state);
1074
+
1075
+ for (; i < run_end; i++) {
1076
+ var pixel = i * rgb32_pixel_size;
1077
+ var pixelm1 = (i-1) * rgb32_pixel_size;
1078
+ cur_row[pixel+rgb32_pixel_pad] = cur_row[pixelm1+rgb32_pixel_pad];
1079
+ }
1080
+
1081
+ if (i == end) {
1082
+ return;
1083
+ }
1084
+ else
1085
+ {
1086
+ stopidx = i + channel.state.waitcnt;
1087
+ rc = 1;
1088
+ break;
1089
+ }
1090
+ }
1091
+ }
1092
+
1093
+ pbucket = channel.buckets_ptrs[correlate_row.row[i - 1]];
1094
+ a = golomb_decoding_8bpc(pbucket.bestcode, this.io_word);
1095
+ correlate_row.row[i] = a.rc
1096
+ cur_row[pixel+rgb32_pixel_pad] = (family_8bpc.xlatL2U[a.rc] + ((cur_row[pixelm1+rgb32_pixel_pad] + prev_row[pixel+rgb32_pixel_pad]) >> 1)) & bpc_mask;
1097
+ this.decode_eatbits(a.codewordlen);
1098
+ }
1099
+ if (rc)
1100
+ break;
1101
+
1102
+ pbucket.update_model_8bpc(channel.state, correlate_row.row[stopidx], bpc);
1103
+
1104
+ stopidx = i + (channel.state.tabrand() & waitmask);
1105
+ }
1106
+
1107
+ for (; i < end && !rc; i++) {
1108
+ var pixel = i * rgb32_pixel_size;
1109
+ var pixelm1 = (i-1) * rgb32_pixel_size;
1110
+ var pixelm2 = (i-2) * rgb32_pixel_size;
1111
+ if (prev_row[pixelm1+rgb32_pixel_pad] == prev_row[pixel+rgb32_pixel_pad])
1112
+ {
1113
+ if (run_index != i && i > 2 && cur_row[pixelm1+rgb32_pixel_pad] == cur_row[pixelm2+rgb32_pixel_pad])
1114
+ {
1115
+ /* do run */
1116
+ channel.state.waitcnt = stopidx - i;
1117
+ run_index = i;
1118
+
1119
+ run_end = i + this.decode_run(channel.state);
1120
+
1121
+ for (; i < run_end; i++) {
1122
+ var pixel = i * rgb32_pixel_size;
1123
+ var pixelm1 = (i-1) * rgb32_pixel_size;
1124
+ cur_row[pixel+rgb32_pixel_pad] = cur_row[pixelm1+rgb32_pixel_pad];
1125
+ }
1126
+
1127
+ if (i == end) {
1128
+ return;
1129
+ }
1130
+ else
1131
+ {
1132
+ stopidx = i + channel.state.waitcnt;
1133
+ rc = 1;
1134
+ break;
1135
+ }
1136
+ }
1137
+ }
1138
+
1139
+ a = golomb_decoding_8bpc(channel.buckets_ptrs[correlate_row.row[i-1]].bestcode, this.io_word);
1140
+ correlate_row.row[i] = a.rc;
1141
+ cur_row[pixel+rgb32_pixel_pad] = (family_8bpc.xlatL2U[a.rc] + ((cur_row[pixelm1+rgb32_pixel_pad] + prev_row[pixel+rgb32_pixel_pad]) >> 1)) & bpc_mask;
1142
+ this.decode_eatbits(a.codewordlen);
1143
+ }
1144
+
1145
+ if (!rc)
1146
+ {
1147
+ channel.state.waitcnt = stopidx - end;
1148
+ return;
1149
+ }
1150
+ }
1151
+ }
1152
+
1153
+ QuicEncoder.prototype.quic_four_uncompress_row = function(channel, prev_row,
1154
+ cur_row)
1155
+ {
1156
+ var bpc = 8;
1157
+ var bpc_mask = 0xff;
1158
+ var correlate_row = channel.correlate_row;
1159
+ var pos = 0;
1160
+ var width = this.width;
1161
+
1162
+ while ((wmimax > channel.state.wmidx) && (channel.state.wmileft <= width)) {
1163
+ if (channel.state.wmileft) {
1164
+ this.quic_four_uncompress_row_seg(channel, correlate_row, prev_row, cur_row, pos,
1165
+ pos + channel.state.wmileft, bpc, bpc_mask);
1166
+ pos += channel.state.wmileft;
1167
+ width -= channel.state.wmileft;
1168
+ }
1169
+
1170
+ channel.state.wmidx++;
1171
+ channel.state.set_wm_trigger();
1172
+ channel.state.wmileft = wminext;
1173
+ }
1174
+
1175
+ if (width) {
1176
+ this.quic_four_uncompress_row_seg(channel, correlate_row, prev_row, cur_row, pos,
1177
+ pos + width, bpc, bpc_mask);
1178
+ if (wmimax > channel.state.wmidx) {
1179
+ channel.state.wmileft -= width;
1180
+ }
1181
+ }
1182
+ }
1183
+
1184
+ /* We need to be generating rgb32 or rgba */
1185
+ QuicEncoder.prototype.quic_decode = function(buf, stride)
1186
+ {
1187
+ var row;
1188
+
1189
+ switch (this.type)
1190
+ {
1191
+ case QUIC_IMAGE_TYPE_RGB32:
1192
+ case QUIC_IMAGE_TYPE_RGB24:
1193
+ this.channels[0].correlate_row.zero = 0;
1194
+ this.channels[1].correlate_row.zero = 0;
1195
+ this.channels[2].correlate_row.zero = 0;
1196
+ this.quic_rgb32_uncompress_row0(buf);
1197
+
1198
+ this.rows_completed++;
1199
+ for (row = 1; row < this.height; row++)
1200
+ {
1201
+ var prev = buf;
1202
+ buf = prev.subarray(stride);
1203
+ this.channels[0].correlate_row.zero = this.channels[0].correlate_row.row[0];
1204
+ this.channels[1].correlate_row.zero = this.channels[1].correlate_row.row[0];
1205
+ this.channels[2].correlate_row.zero = this.channels[2].correlate_row.row[0];
1206
+ this.quic_rgb32_uncompress_row(prev, buf);
1207
+ this.rows_completed++;
1208
+ };
1209
+ break;
1210
+ case QUIC_IMAGE_TYPE_RGB16:
1211
+ console.log("quic: unsupported output format\n");
1212
+ return false;
1213
+ break;
1214
+ case QUIC_IMAGE_TYPE_RGBA:
1215
+ this.channels[0].correlate_row.zero = 0;
1216
+ this.channels[1].correlate_row.zero = 0;
1217
+ this.channels[2].correlate_row.zero = 0;
1218
+ this.quic_rgb32_uncompress_row0(buf);
1219
+
1220
+ this.channels[3].correlate_row.zero = 0;
1221
+ this.quic_four_uncompress_row0(this.channels[3], buf);
1222
+
1223
+ this.rows_completed++;
1224
+ for (row = 1; row < this.height; row++) {
1225
+ var prev = buf;
1226
+ buf = prev.subarray(stride);
1227
+
1228
+ this.channels[0].correlate_row.zero = this.channels[0].correlate_row.row[0];
1229
+ this.channels[1].correlate_row.zero = this.channels[1].correlate_row.row[0];
1230
+ this.channels[2].correlate_row.zero = this.channels[2].correlate_row.row[0];
1231
+ this.quic_rgb32_uncompress_row(prev, buf);
1232
+
1233
+ this.channels[3].correlate_row.zero = this.channels[3].correlate_row.row[0];
1234
+ this.quic_four_uncompress_row(encoder.channels[3], prev, buf);
1235
+ this.rows_completed++;
1236
+ }
1237
+ break;
1238
+
1239
+ case QUIC_IMAGE_TYPE_GRAY:
1240
+ console.log("quic: unsupported output format\n");
1241
+ return false;
1242
+ break;
1243
+
1244
+ case QUIC_IMAGE_TYPE_INVALID:
1245
+ default:
1246
+ console.log("quic: bad image type\n");
1247
+ return false;
1248
+ }
1249
+ return true;
1250
+ }
1251
+
1252
+ QuicEncoder.prototype.simple_quic_decode = function(buf)
1253
+ {
1254
+ var stride = 4; /* FIXME - proper stride calc please */
1255
+ if (!this.quic_decode_begin(buf))
1256
+ return undefined;
1257
+ if (this.type != QUIC_IMAGE_TYPE_RGB32 && this.type != QUIC_IMAGE_TYPE_RGB24
1258
+ && this.type != QUIC_IMAGE_TYPE_RGBA)
1259
+ return undefined;
1260
+ var out = new Uint8Array(this.width*this.height*4);
1261
+ out[0] = 69;
1262
+ if (this.quic_decode( out, (this.width * stride)))
1263
+ return out;
1264
+ return undefined;
1265
+ }
1266
+
1267
+ function SpiceQuic()
1268
+ {
1269
+ }
1270
+
1271
+ SpiceQuic.prototype =
1272
+ {
1273
+ from_dv: function(dv, at, mb)
1274
+ {
1275
+ if (!encoder)
1276
+ throw("quic: no quic encoder");
1277
+ this.data_size = dv.getUint32(at, true);
1278
+ at += 4;
1279
+ var buf = new Uint8Array(mb.slice(at));
1280
+ this.outptr = encoder.simple_quic_decode(buf);
1281
+ if (this.outptr)
1282
+ {
1283
+ this.type = encoder.type;
1284
+ this.width = encoder.width;
1285
+ this.height = encoder.height;
1286
+ }
1287
+ at += buf.length;
1288
+ return at;
1289
+ },
1290
+ }
1291
+
1292
+ function convert_spice_quic_to_web(context, spice_quic)
1293
+ {
1294
+ var ret = context.createImageData(spice_quic.width, spice_quic.height);
1295
+ var i;
1296
+ for (i = 0; i < (ret.width * ret.height * 4); i+=4)
1297
+ {
1298
+ ret.data[i + 0] = spice_quic.outptr[i + 2];
1299
+ ret.data[i + 1] = spice_quic.outptr[i + 1];
1300
+ ret.data[i + 2] = spice_quic.outptr[i + 0];
1301
+ if (spice_quic.type !== QUIC_IMAGE_TYPE_RGBA)
1302
+ ret.data[i + 3] = 255;
1303
+ else
1304
+ ret.data[i + 3] = 255 - spice_quic.outptr[i + 3];
1305
+ }
1306
+ return ret;
1307
+ }
1308
+
1309
+ /* Module initialization */
1310
+ if (need_init)
1311
+ {
1312
+ need_init = false;
1313
+
1314
+ family_init(family_8bpc, 8, DEFmaxclen);
1315
+ family_init(family_5bpc, 5, DEFmaxclen);
1316
+ /* init_zeroLUT */
1317
+ var i, j, k, l;
1318
+
1319
+ j = k = 1;
1320
+ l = 8;
1321
+ for (i = 0; i < 256; ++i) {
1322
+ zeroLUT[i] = l;
1323
+ --k;
1324
+ if (k == 0) {
1325
+ k = j;
1326
+ --l;
1327
+ j *= 2;
1328
+ }
1329
+ }
1330
+
1331
+ encoder = new QuicEncoder;
1332
+
1333
+ if (!encoder)
1334
+ throw("quic: failed to create encoder");
1335
+ }