dwv 0.35.0-beta.9 → 0.35.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.
@@ -1,1756 +0,0 @@
1
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jpeg = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
- /*
3
- * Copyright (C) 2015 Michael Martinez
4
- * Changes: Added support for selection values 2-7, fixed minor bugs &
5
- * warnings, split into multiple class files, and general clean up.
6
- *
7
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
8
- */
9
-
10
- /*
11
- * Copyright (C) Helmut Dersch
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining a copy
14
- * of this software and associated documentation files (the "Software"), to deal
15
- * in the Software without restriction, including without limitation the rights
16
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
- * copies of the Software, and to permit persons to whom the Software is
18
- * furnished to do so, subject to the following conditions:
19
-
20
- * The above copyright notice and this permission notice shall be included in
21
- * all copies or substantial portions of the Software.
22
-
23
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
- * THE SOFTWARE.
30
- */
31
-
32
- /*jslint browser: true, node: true */
33
- /*global require, module */
34
-
35
- "use strict";
36
-
37
- /*** Imports ***/
38
- var jpeg = jpeg || {};
39
- jpeg.lossless = jpeg.lossless || {};
40
-
41
-
42
- /*** Constructor ***/
43
- jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || function () {
44
- this.hSamp = 0; // Horizontal sampling factor
45
- this.quantTableSel = 0; // Quantization table destination selector
46
- this.vSamp = 0; // Vertical
47
- };
48
-
49
-
50
- /*** Exports ***/
51
-
52
- var moduleType = typeof module;
53
- if ((moduleType !== 'undefined') && module.exports) {
54
- module.exports = jpeg.lossless.ComponentSpec;
55
- }
56
-
57
- },{}],2:[function(require,module,exports){
58
- /*
59
- * Copyright (C) 2015 Michael Martinez
60
- * Changes: Added support for selection values 2-7, fixed minor bugs &
61
- * warnings, split into multiple class files, and general clean up.
62
- *
63
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
64
- */
65
-
66
- /*
67
- * Copyright (C) Helmut Dersch
68
- *
69
- * Permission is hereby granted, free of charge, to any person obtaining a copy
70
- * of this software and associated documentation files (the "Software"), to deal
71
- * in the Software without restriction, including without limitation the rights
72
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
73
- * copies of the Software, and to permit persons to whom the Software is
74
- * furnished to do so, subject to the following conditions:
75
-
76
- * The above copyright notice and this permission notice shall be included in
77
- * all copies or substantial portions of the Software.
78
-
79
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
81
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
82
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
83
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
84
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
85
- * THE SOFTWARE.
86
- */
87
-
88
- /*jslint browser: true, node: true */
89
- /*global require, module */
90
-
91
- "use strict";
92
-
93
- /*** Imports ***/
94
- var jpeg = jpeg || {};
95
- jpeg.lossless = jpeg.lossless || {};
96
-
97
-
98
- /*** Constructor ***/
99
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || function (data, offset, length) {
100
- // Note: DataView is much slower than Int8Array
101
- // this.buffer = new DataView(data, offset, length);
102
- this.buffer = new Uint8Array(data, offset, length);
103
- this.index = 0;
104
- };
105
-
106
-
107
-
108
- jpeg.lossless.DataStream.prototype.get16 = function () {
109
- // var value = this.buffer.getUint16(this.index, false);
110
- var value = (this.buffer[this.index] << 8) + this.buffer[this.index + 1]; // DataView is big-endian by default
111
- this.index += 2;
112
- return value;
113
- };
114
-
115
-
116
-
117
- jpeg.lossless.DataStream.prototype.get8 = function () {
118
- // var value = this.buffer.getUint8(this.index);
119
- var value = this.buffer[this.index];
120
- this.index += 1;
121
- return value;
122
- };
123
-
124
-
125
- /*** Exports ***/
126
-
127
- var moduleType = typeof module;
128
- if ((moduleType !== 'undefined') && module.exports) {
129
- module.exports = jpeg.lossless.DataStream;
130
- }
131
-
132
- },{}],3:[function(require,module,exports){
133
- /*
134
- * Copyright (C) 2015 Michael Martinez
135
- * Changes: Added support for selection values 2-7, fixed minor bugs &
136
- * warnings, split into multiple class files, and general clean up.
137
- *
138
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
139
- */
140
-
141
- /*
142
- * Copyright (C) Helmut Dersch
143
- *
144
- * Permission is hereby granted, free of charge, to any person obtaining a copy
145
- * of this software and associated documentation files (the "Software"), to deal
146
- * in the Software without restriction, including without limitation the rights
147
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
148
- * copies of the Software, and to permit persons to whom the Software is
149
- * furnished to do so, subject to the following conditions:
150
-
151
- * The above copyright notice and this permission notice shall be included in
152
- * all copies or substantial portions of the Software.
153
-
154
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
156
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
157
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
158
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
159
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
160
- * THE SOFTWARE.
161
- */
162
-
163
- /*jslint browser: true, node: true */
164
- /*global require, module */
165
-
166
- "use strict";
167
-
168
- /*** Imports ***/
169
- var jpeg = jpeg || {};
170
- jpeg.lossless = jpeg.lossless || {};
171
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
172
- jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || ((typeof require !== 'undefined') ? require('./huffman-table.js') : null);
173
- jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || ((typeof require !== 'undefined') ? require('./quantization-table.js') : null);
174
- jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || ((typeof require !== 'undefined') ? require('./scan-header.js') : null);
175
- jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || ((typeof require !== 'undefined') ? require('./frame-header.js') : null);
176
- jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null);
177
-
178
-
179
- /*** Constructor ***/
180
-
181
- /**
182
- * The Decoder constructor.
183
- * @property {number} xDim - size of x dimension
184
- * @property {number} yDim - size of y dimension
185
- * @property {number} numComp - number of components
186
- * @property {number} numBytes - number of bytes per component
187
- * @type {Function}
188
- */
189
- jpeg.lossless.Decoder = jpeg.lossless.Decoder || function (buffer, numBytes) {
190
- this.buffer = buffer;
191
- this.frame = new jpeg.lossless.FrameHeader();
192
- this.huffTable = new jpeg.lossless.HuffmanTable();
193
- this.quantTable = new jpeg.lossless.QuantizationTable();
194
- this.scan = new jpeg.lossless.ScanHeader();
195
- this.DU = jpeg.lossless.Utils.createArray(10, 4, 64); // at most 10 data units in a MCU, at most 4 data units in one component
196
- this.HuffTab = jpeg.lossless.Utils.createArray(4, 2, 50 * 256);
197
- this.IDCT_Source = [];
198
- this.nBlock = []; // number of blocks in the i-th Comp in a scan
199
- this.acTab = jpeg.lossless.Utils.createArray(10, 1); // ac HuffTab for the i-th Comp in a scan
200
- this.dcTab = jpeg.lossless.Utils.createArray(10, 1); // dc HuffTab for the i-th Comp in a scan
201
- this.qTab = jpeg.lossless.Utils.createArray(10, 1); // quantization table for the i-th Comp in a scan
202
- this.marker = 0;
203
- this.markerIndex = 0;
204
- this.numComp = 0;
205
- this.restartInterval = 0;
206
- this.selection = 0;
207
- this.xDim = 0;
208
- this.yDim = 0;
209
- this.xLoc = 0;
210
- this.yLoc = 0;
211
- this.numBytes = 0;
212
- this.outputData = null;
213
- this.restarting = false;
214
- this.mask = 0;
215
-
216
- if (typeof numBytes !== "undefined") {
217
- this.numBytes = numBytes;
218
- }
219
- };
220
-
221
-
222
- /*** Static Pseudo-constants ***/
223
-
224
- jpeg.lossless.Decoder.IDCT_P = [0, 5, 40, 16, 45, 2, 7, 42, 21, 56, 8, 61, 18, 47, 1, 4, 41, 23, 58, 13, 32, 24, 37, 10, 63, 17, 44, 3, 6, 43, 20,
225
- 57, 15, 34, 29, 48, 53, 26, 39, 9, 60, 19, 46, 22, 59, 12, 33, 31, 50, 55, 25, 36, 11, 62, 14, 35, 28, 49, 52, 27, 38, 30, 51, 54];
226
- jpeg.lossless.Decoder.TABLE = [0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53,
227
- 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63];
228
- jpeg.lossless.Decoder.MAX_HUFFMAN_SUBTREE = 50;
229
- jpeg.lossless.Decoder.MSB = 0x80000000;
230
- jpeg.lossless.Decoder.RESTART_MARKER_BEGIN = 0xFFD0;
231
- jpeg.lossless.Decoder.RESTART_MARKER_END = 0xFFD7;
232
-
233
- /*** Prototype Methods ***/
234
-
235
- /**
236
- * Returns decompressed data.
237
- * @param {ArrayBuffer} buffer
238
- * @param {number} [offset]
239
- * @param {number} [length]
240
- * @returns {ArrayBufer}
241
- */
242
- jpeg.lossless.Decoder.prototype.decompress = function (buffer, offset, length) {
243
- return this.decode(buffer, offset, length).buffer;
244
- };
245
-
246
-
247
-
248
- jpeg.lossless.Decoder.prototype.decode = function (buffer, offset, length, numBytes) {
249
- /*jslint bitwise: true */
250
-
251
- var current, scanNum = 0, pred = [], i, compN, temp = [], index = [], mcuNum;
252
-
253
- if (typeof buffer !== "undefined") {
254
- this.buffer = buffer;
255
- }
256
-
257
- if (typeof numBytes !== "undefined") {
258
- this.numBytes = numBytes;
259
- }
260
-
261
- this.stream = new jpeg.lossless.DataStream(this.buffer, offset, length);
262
- this.buffer = null;
263
-
264
- this.xLoc = 0;
265
- this.yLoc = 0;
266
- current = this.stream.get16();
267
-
268
- if (current !== 0xFFD8) { // SOI
269
- throw new Error("Not a JPEG file");
270
- }
271
-
272
- current = this.stream.get16();
273
-
274
- while ((((current >> 4) !== 0x0FFC) || (current === 0xFFC4))) { // SOF 0~15
275
- switch (current) {
276
- case 0xFFC4: // DHT
277
- this.huffTable.read(this.stream, this.HuffTab);
278
- break;
279
- case 0xFFCC: // DAC
280
- throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)");
281
- case 0xFFDB:
282
- this.quantTable.read(this.stream, jpeg.lossless.Decoder.TABLE);
283
- break;
284
- case 0xFFDD:
285
- this.restartInterval = this.readNumber();
286
- break;
287
- case 0xFFE0:
288
- case 0xFFE1:
289
- case 0xFFE2:
290
- case 0xFFE3:
291
- case 0xFFE4:
292
- case 0xFFE5:
293
- case 0xFFE6:
294
- case 0xFFE7:
295
- case 0xFFE8:
296
- case 0xFFE9:
297
- case 0xFFEA:
298
- case 0xFFEB:
299
- case 0xFFEC:
300
- case 0xFFED:
301
- case 0xFFEE:
302
- case 0xFFEF:
303
- this.readApp();
304
- break;
305
- case 0xFFFE:
306
- this.readComment();
307
- break;
308
- default:
309
- if ((current >> 8) !== 0xFF) {
310
- throw new Error("ERROR: format throw new IOException! (decode)");
311
- }
312
- }
313
-
314
- current = this.stream.get16();
315
- }
316
-
317
- if ((current < 0xFFC0) || (current > 0xFFC7)) {
318
- throw new Error("ERROR: could not handle arithmetic code!");
319
- }
320
-
321
- this.frame.read(this.stream);
322
- current = this.stream.get16();
323
-
324
- do {
325
- while (current !== 0x0FFDA) { // SOS
326
- switch (current) {
327
- case 0xFFC4: // DHT
328
- this.huffTable.read(this.stream, this.HuffTab);
329
- break;
330
- case 0xFFCC: // DAC
331
- throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)");
332
- case 0xFFDB:
333
- this.quantTable.read(this.stream, jpeg.lossless.Decoder.TABLE);
334
- break;
335
- case 0xFFDD:
336
- this.restartInterval = this.readNumber();
337
- break;
338
- case 0xFFE0:
339
- case 0xFFE1:
340
- case 0xFFE2:
341
- case 0xFFE3:
342
- case 0xFFE4:
343
- case 0xFFE5:
344
- case 0xFFE6:
345
- case 0xFFE7:
346
- case 0xFFE8:
347
- case 0xFFE9:
348
- case 0xFFEA:
349
- case 0xFFEB:
350
- case 0xFFEC:
351
- case 0xFFED:
352
- case 0xFFEE:
353
- case 0xFFEF:
354
- this.readApp();
355
- break;
356
- case 0xFFFE:
357
- this.readComment();
358
- break;
359
- default:
360
- if ((current >> 8) !== 0xFF) {
361
- throw new Error("ERROR: format throw new IOException! (Parser.decode)");
362
- }
363
- }
364
-
365
- current = this.stream.get16();
366
- }
367
-
368
- this.precision = this.frame.precision;
369
- this.components = this.frame.components;
370
-
371
- if (!this.numBytes) {
372
- this.numBytes = parseInt(Math.ceil(this.precision / 8));
373
- }
374
-
375
- if (this.numBytes == 1) {
376
- this.mask = 0xFF;
377
- } else {
378
- this.mask = 0xFFFF;
379
- }
380
-
381
- this.scan.read(this.stream);
382
- this.numComp = this.scan.numComp;
383
- this.selection = this.scan.selection;
384
-
385
- if (this.numBytes === 1) {
386
- if (this.numComp === 3) {
387
- this.getter = this.getValueRGB;
388
- this.setter = this.setValueRGB;
389
- this.output = this.outputRGB;
390
- } else {
391
- this.getter = this.getValue8;
392
- this.setter = this.setValue8;
393
- this.output = this.outputSingle;
394
- }
395
- } else {
396
- this.getter = this.getValue16;
397
- this.setter = this.setValue16;
398
- this.output = this.outputSingle;
399
- }
400
-
401
- switch (this.selection) {
402
- case 2:
403
- this.selector = this.select2;
404
- break;
405
- case 3:
406
- this.selector = this.select3;
407
- break;
408
- case 4:
409
- this.selector = this.select4;
410
- break;
411
- case 5:
412
- this.selector = this.select5;
413
- break;
414
- case 6:
415
- this.selector = this.select6;
416
- break;
417
- case 7:
418
- this.selector = this.select7;
419
- break;
420
- default:
421
- this.selector = this.select1;
422
- break;
423
- }
424
-
425
- this.scanComps = this.scan.components;
426
- this.quantTables = this.quantTable.quantTables;
427
-
428
- for (i = 0; i < this.numComp; i+=1) {
429
- compN = this.scanComps[i].scanCompSel;
430
- this.qTab[i] = this.quantTables[this.components[compN].quantTableSel];
431
- this.nBlock[i] = this.components[compN].vSamp * this.components[compN].hSamp;
432
- this.dcTab[i] = this.HuffTab[this.scanComps[i].dcTabSel][0];
433
- this.acTab[i] = this.HuffTab[this.scanComps[i].acTabSel][1];
434
- }
435
-
436
- this.xDim = this.frame.dimX;
437
- this.yDim = this.frame.dimY;
438
- if (this.numBytes == 1) {
439
- this.outputData = new Uint8Array(new ArrayBuffer(this.xDim * this.yDim * this.numBytes * this.numComp));
440
- } else {
441
- this.outputData = new Uint16Array(new ArrayBuffer(this.xDim * this.yDim * this.numBytes * this.numComp));
442
- }
443
-
444
- scanNum+=1;
445
-
446
- while (true) { // Decode one scan
447
- temp[0] = 0;
448
- index[0] = 0;
449
-
450
- for (i = 0; i < 10; i+=1) {
451
- pred[i] = (1 << (this.precision - 1));
452
- }
453
-
454
- if (this.restartInterval === 0) {
455
- current = this.decodeUnit(pred, temp, index);
456
-
457
- while ((current === 0) && ((this.xLoc < this.xDim) && (this.yLoc < this.yDim))) {
458
- this.output(pred);
459
- current = this.decodeUnit(pred, temp, index);
460
- }
461
-
462
- break; //current=MARKER
463
- }
464
-
465
- for (mcuNum = 0; mcuNum < this.restartInterval; mcuNum+=1) {
466
- this.restarting = (mcuNum == 0);
467
- current = this.decodeUnit(pred, temp, index);
468
- this.output(pred);
469
-
470
- if (current !== 0) {
471
- break;
472
- }
473
- }
474
-
475
- if (current === 0) {
476
- if (this.markerIndex !== 0) {
477
- current = (0xFF00 | this.marker);
478
- this.markerIndex = 0;
479
- } else {
480
- current = this.stream.get16();
481
- }
482
- }
483
-
484
- if (!((current >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) &&
485
- (current <= jpeg.lossless.Decoder.RESTART_MARKER_END))) {
486
- break; //current=MARKER
487
- }
488
- }
489
-
490
- if ((current === 0xFFDC) && (scanNum === 1)) { //DNL
491
- this.readNumber();
492
- current = this.stream.get16();
493
- }
494
- } while ((current !== 0xFFD9) && ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) && (scanNum === 0));
495
-
496
- return this.outputData;
497
- };
498
-
499
-
500
-
501
- jpeg.lossless.Decoder.prototype.decodeUnit = function (prev, temp, index) {
502
- if (this.numComp == 1) {
503
- return this.decodeSingle(prev, temp, index);
504
- } else if (this.numComp == 3) {
505
- return this.decodeRGB(prev, temp, index);
506
- } else {
507
- return -1;
508
- }
509
- };
510
-
511
-
512
-
513
- jpeg.lossless.Decoder.prototype.select1 = function (compOffset) {
514
- return this.getPreviousX(compOffset);
515
- };
516
-
517
-
518
-
519
- jpeg.lossless.Decoder.prototype.select2 = function (compOffset) {
520
- return this.getPreviousY(compOffset);
521
- };
522
-
523
-
524
-
525
- jpeg.lossless.Decoder.prototype.select3 = function (compOffset) {
526
- return this.getPreviousXY(compOffset);
527
- };
528
-
529
-
530
-
531
- jpeg.lossless.Decoder.prototype.select4 = function (compOffset) {
532
- return (this.getPreviousX(compOffset) + this.getPreviousY(compOffset)) - this.getPreviousXY(compOffset);
533
- };
534
-
535
-
536
-
537
- jpeg.lossless.Decoder.prototype.select5 = function (compOffset) {
538
- return this.getPreviousX(compOffset) + ((this.getPreviousY(compOffset) - this.getPreviousXY(compOffset)) >> 1);
539
- };
540
-
541
-
542
-
543
- jpeg.lossless.Decoder.prototype.select6 = function (compOffset) {
544
- return this.getPreviousY(compOffset) + ((this.getPreviousX(compOffset) - this.getPreviousXY(compOffset)) >> 1);
545
- };
546
-
547
-
548
-
549
- jpeg.lossless.Decoder.prototype.select7 = function (compOffset) {
550
- return ((this.getPreviousX(compOffset) + this.getPreviousY(compOffset)) / 2);
551
- };
552
-
553
-
554
-
555
- jpeg.lossless.Decoder.prototype.decodeRGB = function (prev, temp, index) {
556
- /*jslint bitwise: true */
557
-
558
- var value, actab, dctab, qtab, ctrC, i, k, j;
559
-
560
- prev[0] = this.selector(0);
561
- prev[1] = this.selector(1);
562
- prev[2] = this.selector(2);
563
-
564
- for (ctrC = 0; ctrC < this.numComp; ctrC+=1) {
565
- qtab = this.qTab[ctrC];
566
- actab = this.acTab[ctrC];
567
- dctab = this.dcTab[ctrC];
568
- for (i = 0; i < this.nBlock[ctrC]; i+=1) {
569
- for (k = 0; k < this.IDCT_Source.length; k+=1) {
570
- this.IDCT_Source[k] = 0;
571
- }
572
-
573
- value = this.getHuffmanValue(dctab, temp, index);
574
-
575
- if (value >= 0xFF00) {
576
- return value;
577
- }
578
-
579
- prev[ctrC] = this.IDCT_Source[0] = prev[ctrC] + this.getn(index, value, temp, index);
580
- this.IDCT_Source[0] *= qtab[0];
581
-
582
- for (j = 1; j < 64; j+=1) {
583
- value = this.getHuffmanValue(actab, temp, index);
584
-
585
- if (value >= 0xFF00) {
586
- return value;
587
- }
588
-
589
- j += (value >> 4);
590
-
591
- if ((value & 0x0F) === 0) {
592
- if ((value >> 4) === 0) {
593
- break;
594
- }
595
- } else {
596
- this.IDCT_Source[jpeg.lossless.Decoder.IDCT_P[j]] = this.getn(index, value & 0x0F, temp, index) * qtab[j];
597
- }
598
- }
599
- }
600
- }
601
-
602
- return 0;
603
- };
604
-
605
-
606
-
607
- jpeg.lossless.Decoder.prototype.decodeSingle = function (prev, temp, index) {
608
- /*jslint bitwise: true */
609
-
610
- var value, i, n, nRestart;
611
-
612
- if (this.restarting) {
613
- this.restarting = false;
614
- prev[0] = (1 << (this.frame.precision - 1));
615
- } else {
616
- prev[0] = this.selector();
617
- }
618
-
619
- for (i = 0; i < this.nBlock[0]; i+=1) {
620
- value = this.getHuffmanValue(this.dcTab[0], temp, index);
621
- if (value >= 0xFF00) {
622
- return value;
623
- }
624
-
625
- n = this.getn(prev, value, temp, index);
626
- nRestart = (n >> 8);
627
-
628
- if ((nRestart >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) && (nRestart <= jpeg.lossless.Decoder.RESTART_MARKER_END)) {
629
- return nRestart;
630
- }
631
-
632
- prev[0] += n;
633
- }
634
-
635
- return 0;
636
- };
637
-
638
-
639
-
640
- // Huffman table for fast search: (HuffTab) 8-bit Look up table 2-layer search architecture, 1st-layer represent 256 node (8 bits) if codeword-length > 8
641
- // bits, then the entry of 1st-layer = (# of 2nd-layer table) | MSB and it is stored in the 2nd-layer Size of tables in each layer are 256.
642
- // HuffTab[*][*][0-256] is always the only 1st-layer table.
643
- //
644
- // An entry can be: (1) (# of 2nd-layer table) | MSB , for code length > 8 in 1st-layer (2) (Code length) << 8 | HuffVal
645
- //
646
- // HuffmanValue(table HuffTab[x][y] (ex) HuffmanValue(HuffTab[1][0],...)
647
- // ):
648
- // return: Huffman Value of table
649
- // 0xFF?? if it receives a MARKER
650
- // Parameter: table HuffTab[x][y] (ex) HuffmanValue(HuffTab[1][0],...)
651
- // temp temp storage for remainded bits
652
- // index index to bit of temp
653
- // in FILE pointer
654
- // Effect:
655
- // temp store new remainded bits
656
- // index change to new index
657
- // in change to new position
658
- // NOTE:
659
- // Initial by temp=0; index=0;
660
- // NOTE: (explain temp and index)
661
- // temp: is always in the form at calling time or returning time
662
- // | byte 4 | byte 3 | byte 2 | byte 1 |
663
- // | 0 | 0 | 00000000 | 00000??? | if not a MARKER
664
- // ^index=3 (from 0 to 15)
665
- // 321
666
- // NOTE (marker and marker_index):
667
- // If get a MARKER from 'in', marker=the low-byte of the MARKER
668
- // and marker_index=9
669
- // If marker_index=9 then index is always > 8, or HuffmanValue()
670
- // will not be called
671
- jpeg.lossless.Decoder.prototype.getHuffmanValue = function (table, temp, index) {
672
- /*jslint bitwise: true */
673
-
674
- var code, input, mask;
675
- mask = 0xFFFF;
676
-
677
- if (index[0] < 8) {
678
- temp[0] <<= 8;
679
- input = this.stream.get8();
680
- if (input === 0xFF) {
681
- this.marker = this.stream.get8();
682
- if (this.marker !== 0) {
683
- this.markerIndex = 9;
684
- }
685
- }
686
- temp[0] |= input;
687
- } else {
688
- index[0] -= 8;
689
- }
690
-
691
- code = table[temp[0] >> index[0]];
692
-
693
- if ((code & jpeg.lossless.Decoder.MSB) !== 0) {
694
- if (this.markerIndex !== 0) {
695
- this.markerIndex = 0;
696
- return 0xFF00 | this.marker;
697
- }
698
-
699
- temp[0] &= (mask >> (16 - index[0]));
700
- temp[0] <<= 8;
701
- input = this.stream.get8();
702
-
703
- if (input === 0xFF) {
704
- this.marker = this.stream.get8();
705
- if (this.marker !== 0) {
706
- this.markerIndex = 9;
707
- }
708
- }
709
-
710
- temp[0] |= input;
711
- code = table[((code & 0xFF) * 256) + (temp[0] >> index[0])];
712
- index[0] += 8;
713
- }
714
-
715
- index[0] += 8 - (code >> 8);
716
-
717
- if (index[0] < 0) {
718
- throw new Error("index=" + index[0] + " temp=" + temp[0] + " code=" + code + " in HuffmanValue()");
719
- }
720
-
721
- if (index[0] < this.markerIndex) {
722
- this.markerIndex = 0;
723
- return 0xFF00 | this.marker;
724
- }
725
-
726
- temp[0] &= (mask >> (16 - index[0]));
727
- return code & 0xFF;
728
- };
729
-
730
-
731
-
732
- jpeg.lossless.Decoder.prototype.getn = function (PRED, n, temp, index) {
733
- /*jslint bitwise: true */
734
-
735
- var result, one, n_one, mask, input;
736
- one = 1;
737
- n_one = -1;
738
- mask = 0xFFFF;
739
-
740
- if (n === 0) {
741
- return 0;
742
- }
743
-
744
- if (n === 16) {
745
- if (PRED[0] >= 0) {
746
- return -32768;
747
- } else {
748
- return 32768;
749
- }
750
- }
751
-
752
- index[0] -= n;
753
-
754
- if (index[0] >= 0) {
755
- if ((index[0] < this.markerIndex) && !this.isLastPixel()) { // this was corrupting the last pixel in some cases
756
- this.markerIndex = 0;
757
- return (0xFF00 | this.marker) << 8;
758
- }
759
-
760
- result = temp[0] >> index[0];
761
- temp[0] &= (mask >> (16 - index[0]));
762
- } else {
763
- temp[0] <<= 8;
764
- input = this.stream.get8();
765
-
766
- if (input === 0xFF) {
767
- this.marker = this.stream.get8();
768
- if (this.marker !== 0) {
769
- this.markerIndex = 9;
770
- }
771
- }
772
-
773
- temp[0] |= input;
774
- index[0] += 8;
775
-
776
- if (index[0] < 0) {
777
- if (this.markerIndex !== 0) {
778
- this.markerIndex = 0;
779
- return (0xFF00 | this.marker) << 8;
780
- }
781
-
782
- temp[0] <<= 8;
783
- input = this.stream.get8();
784
-
785
- if (input === 0xFF) {
786
- this.marker = this.stream.get8();
787
- if (this.marker !== 0) {
788
- this.markerIndex = 9;
789
- }
790
- }
791
-
792
- temp[0] |= input;
793
- index[0] += 8;
794
- }
795
-
796
- if (index[0] < 0) {
797
- throw new Error("index=" + index[0] + " in getn()");
798
- }
799
-
800
- if (index[0] < this.markerIndex) {
801
- this.markerIndex = 0;
802
- return (0xFF00 | this.marker) << 8;
803
- }
804
-
805
- result = temp[0] >> index[0];
806
- temp[0] &= (mask >> (16 - index[0]));
807
- }
808
-
809
- if (result < (one << (n - 1))) {
810
- result += (n_one << n) + 1;
811
- }
812
-
813
- return result;
814
- };
815
-
816
-
817
-
818
- jpeg.lossless.Decoder.prototype.getPreviousX = function (compOffset) {
819
- /*jslint bitwise: true */
820
-
821
- if (this.xLoc > 0) {
822
- return this.getter((((this.yLoc * this.xDim) + this.xLoc) - 1), compOffset);
823
- } else if (this.yLoc > 0) {
824
- return this.getPreviousY(compOffset);
825
- } else {
826
- return (1 << (this.frame.precision - 1));
827
- }
828
- };
829
-
830
-
831
-
832
- jpeg.lossless.Decoder.prototype.getPreviousXY = function (compOffset) {
833
- /*jslint bitwise: true */
834
-
835
- if ((this.xLoc > 0) && (this.yLoc > 0)) {
836
- return this.getter(((((this.yLoc - 1) * this.xDim) + this.xLoc) - 1), compOffset);
837
- } else {
838
- return this.getPreviousY(compOffset);
839
- }
840
- };
841
-
842
-
843
-
844
- jpeg.lossless.Decoder.prototype.getPreviousY = function (compOffset) {
845
- /*jslint bitwise: true */
846
-
847
- if (this.yLoc > 0) {
848
- return this.getter((((this.yLoc - 1) * this.xDim) + this.xLoc), compOffset);
849
- } else {
850
- return this.getPreviousX(compOffset);
851
- }
852
- };
853
-
854
-
855
-
856
- jpeg.lossless.Decoder.prototype.isLastPixel = function () {
857
- return (this.xLoc === (this.xDim - 1)) && (this.yLoc === (this.yDim - 1));
858
- };
859
-
860
-
861
-
862
- jpeg.lossless.Decoder.prototype.outputSingle = function (PRED) {
863
- if ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) {
864
- this.setter((((this.yLoc * this.xDim) + this.xLoc)), this.mask & PRED[0]);
865
-
866
- this.xLoc+=1;
867
-
868
- if (this.xLoc >= this.xDim) {
869
- this.yLoc+=1;
870
- this.xLoc = 0;
871
- }
872
- }
873
- };
874
-
875
-
876
-
877
- jpeg.lossless.Decoder.prototype.outputRGB = function (PRED) {
878
- var offset = ((this.yLoc * this.xDim) + this.xLoc);
879
-
880
- if ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) {
881
- this.setter(offset, PRED[0], 0);
882
- this.setter(offset, PRED[1], 1);
883
- this.setter(offset, PRED[2], 2);
884
-
885
- this.xLoc+=1;
886
-
887
- if (this.xLoc >= this.xDim) {
888
- this.yLoc+=1;
889
- this.xLoc = 0;
890
- }
891
- }
892
- };
893
-
894
- jpeg.lossless.Decoder.prototype.setValue8 = function (index, val) {
895
- this.outputData[index] = val;
896
- };
897
-
898
- jpeg.lossless.Decoder.prototype.getValue8 = function (index) {
899
- return this.outputData[index]; // mask should not be necessary because outputData is either Int8Array or Int16Array
900
- };
901
-
902
- var littleEndian = (function() {
903
- var buffer = new ArrayBuffer(2);
904
- new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
905
- // Int16Array uses the platform's endianness.
906
- return new Int16Array(buffer)[0] === 256;
907
- })();
908
-
909
- if (littleEndian) {
910
- // just reading from an array is fine then. Int16Array will use platform endianness.
911
- jpeg.lossless.Decoder.prototype.setValue16 = jpeg.lossless.Decoder.prototype.setValue8;
912
- jpeg.lossless.Decoder.prototype.getValue16 = jpeg.lossless.Decoder.prototype.getValue8;
913
- }
914
- else {
915
- // If platform is big-endian, we will need to convert to little-endian
916
- jpeg.lossless.Decoder.prototype.setValue16 = function (index, val) {
917
- this.outputData[index] = ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
918
- };
919
-
920
- jpeg.lossless.Decoder.prototype.getValue16 = function (index) {
921
- var val = this.outputData[index];
922
- return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
923
- };
924
- }
925
-
926
- jpeg.lossless.Decoder.prototype.setValueRGB = function (index, val, compOffset) {
927
- // this.outputData.setUint8(index * 3 + compOffset, val);
928
- this.outputData[index * 3 + compOffset] = val;
929
- };
930
-
931
- jpeg.lossless.Decoder.prototype.getValueRGB = function (index, compOffset) {
932
- // return this.outputData.getUint8(index * 3 + compOffset);
933
- return this.outputData[index * 3 + compOffset];
934
- };
935
-
936
-
937
-
938
- jpeg.lossless.Decoder.prototype.readApp = function() {
939
- var count = 0, length = this.stream.get16();
940
- count += 2;
941
-
942
- while (count < length) {
943
- this.stream.get8();
944
- count+=1;
945
- }
946
-
947
- return length;
948
- };
949
-
950
-
951
-
952
- jpeg.lossless.Decoder.prototype.readComment = function () {
953
- var sb = "", count = 0, length;
954
-
955
- length = this.stream.get16();
956
- count += 2;
957
-
958
- while (count < length) {
959
- sb += this.stream.get8();
960
- count+=1;
961
- }
962
-
963
- return sb;
964
- };
965
-
966
-
967
-
968
- jpeg.lossless.Decoder.prototype.readNumber = function() {
969
- var Ld = this.stream.get16();
970
-
971
- if (Ld !== 4) {
972
- throw new Error("ERROR: Define number format throw new IOException [Ld!=4]");
973
- }
974
-
975
- return this.stream.get16();
976
- };
977
-
978
-
979
-
980
- /*** Exports ***/
981
-
982
- var moduleType = typeof module;
983
- if ((moduleType !== 'undefined') && module.exports) {
984
- module.exports = jpeg.lossless.Decoder;
985
- }
986
-
987
- },{"./data-stream.js":2,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-header.js":9,"./utils.js":10}],4:[function(require,module,exports){
988
- /*
989
- * Copyright (C) 2015 Michael Martinez
990
- * Changes: Added support for selection values 2-7, fixed minor bugs &
991
- * warnings, split into multiple class files, and general clean up.
992
- *
993
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
994
- */
995
-
996
- /*
997
- * Copyright (C) Helmut Dersch
998
- *
999
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1000
- * of this software and associated documentation files (the "Software"), to deal
1001
- * in the Software without restriction, including without limitation the rights
1002
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1003
- * copies of the Software, and to permit persons to whom the Software is
1004
- * furnished to do so, subject to the following conditions:
1005
-
1006
- * The above copyright notice and this permission notice shall be included in
1007
- * all copies or substantial portions of the Software.
1008
-
1009
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1010
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1011
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1012
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1013
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1014
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1015
- * THE SOFTWARE.
1016
- */
1017
-
1018
- /*jslint browser: true, node: true */
1019
- /*global require, module */
1020
-
1021
- "use strict";
1022
-
1023
- /*** Imports ***/
1024
- var jpeg = jpeg || {};
1025
- jpeg.lossless = jpeg.lossless || {};
1026
- jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || ((typeof require !== 'undefined') ? require('./component-spec.js') : null);
1027
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
1028
-
1029
-
1030
- /*** Constructor ***/
1031
- jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || function () {
1032
- this.components = []; // Components
1033
- this.dimX = 0; // Number of samples per line
1034
- this.dimY = 0; // Number of lines
1035
- this.numComp = 0; // Number of component in the frame
1036
- this.precision = 0; // Sample Precision (from the original image)
1037
- };
1038
-
1039
-
1040
-
1041
- /*** Prototype Methods ***/
1042
-
1043
- jpeg.lossless.FrameHeader.prototype.read = function (data) {
1044
- /*jslint bitwise: true */
1045
-
1046
- var count = 0, length, i, c, temp;
1047
-
1048
- length = data.get16();
1049
- count += 2;
1050
-
1051
- this.precision = data.get8();
1052
- count+=1;
1053
-
1054
- this.dimY = data.get16();
1055
- count += 2;
1056
-
1057
- this.dimX = data.get16();
1058
- count += 2;
1059
-
1060
- this.numComp = data.get8();
1061
- count+=1;
1062
- for (i = 1; i <= this.numComp; i+=1) {
1063
- if (count > length) {
1064
- throw new Error("ERROR: frame format error");
1065
- }
1066
-
1067
- c = data.get8();
1068
- count+=1;
1069
-
1070
- if (count >= length) {
1071
- throw new Error("ERROR: frame format error [c>=Lf]");
1072
- }
1073
-
1074
- temp = data.get8();
1075
- count+=1;
1076
-
1077
- if (!this.components[c]) {
1078
- this.components[c] = new jpeg.lossless.ComponentSpec();
1079
- }
1080
-
1081
- this.components[c].hSamp = temp >> 4;
1082
- this.components[c].vSamp = temp & 0x0F;
1083
- this.components[c].quantTableSel = data.get8();
1084
- count+=1;
1085
- }
1086
-
1087
- if (count !== length) {
1088
- throw new Error("ERROR: frame format error [Lf!=count]");
1089
- }
1090
-
1091
- return 1;
1092
- };
1093
-
1094
-
1095
- /*** Exports ***/
1096
-
1097
- var moduleType = typeof module;
1098
- if ((moduleType !== 'undefined') && module.exports) {
1099
- module.exports = jpeg.lossless.FrameHeader;
1100
- }
1101
-
1102
- },{"./component-spec.js":1,"./data-stream.js":2}],5:[function(require,module,exports){
1103
- /*
1104
- * Copyright (C) 2015 Michael Martinez
1105
- * Changes: Added support for selection values 2-7, fixed minor bugs &
1106
- * warnings, split into multiple class files, and general clean up.
1107
- *
1108
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
1109
- */
1110
-
1111
- /*
1112
- * Copyright (C) Helmut Dersch
1113
- *
1114
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1115
- * of this software and associated documentation files (the "Software"), to deal
1116
- * in the Software without restriction, including without limitation the rights
1117
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1118
- * copies of the Software, and to permit persons to whom the Software is
1119
- * furnished to do so, subject to the following conditions:
1120
-
1121
- * The above copyright notice and this permission notice shall be included in
1122
- * all copies or substantial portions of the Software.
1123
-
1124
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1125
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1126
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1127
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1128
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1129
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1130
- * THE SOFTWARE.
1131
- */
1132
-
1133
- /*jslint browser: true, node: true */
1134
- /*global require, module */
1135
-
1136
- "use strict";
1137
-
1138
- /*** Imports ***/
1139
- var jpeg = jpeg || {};
1140
- jpeg.lossless = jpeg.lossless || {};
1141
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
1142
- jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null);
1143
-
1144
-
1145
- /*** Constructor ***/
1146
- jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || function () {
1147
- this.l = jpeg.lossless.Utils.createArray(4, 2, 16);
1148
- this.th = [];
1149
- this.v = jpeg.lossless.Utils.createArray(4, 2, 16, 200);
1150
- this.tc = jpeg.lossless.Utils.createArray(4, 2);
1151
-
1152
- this.tc[0][0] = 0;
1153
- this.tc[1][0] = 0;
1154
- this.tc[2][0] = 0;
1155
- this.tc[3][0] = 0;
1156
- this.tc[0][1] = 0;
1157
- this.tc[1][1] = 0;
1158
- this.tc[2][1] = 0;
1159
- this.tc[3][1] = 0;
1160
- this.th[0] = 0;
1161
- this.th[1] = 0;
1162
- this.th[2] = 0;
1163
- this.th[3] = 0;
1164
- };
1165
-
1166
-
1167
-
1168
- /*** Static Pseudo-constants ***/
1169
-
1170
- jpeg.lossless.HuffmanTable.MSB = 0x80000000;
1171
-
1172
-
1173
- /*** Prototype Methods ***/
1174
-
1175
- jpeg.lossless.HuffmanTable.prototype.read = function(data, HuffTab) {
1176
- /*jslint bitwise: true */
1177
-
1178
- var count = 0, length, temp, t, c, i, j;
1179
-
1180
- length = data.get16();
1181
- count += 2;
1182
-
1183
- while (count < length) {
1184
- temp = data.get8();
1185
- count+=1;
1186
- t = temp & 0x0F;
1187
- if (t > 3) {
1188
- throw new Error("ERROR: Huffman table ID > 3");
1189
- }
1190
-
1191
- c = temp >> 4;
1192
- if (c > 2) {
1193
- throw new Error("ERROR: Huffman table [Table class > 2 ]");
1194
- }
1195
-
1196
- this.th[t] = 1;
1197
- this.tc[t][c] = 1;
1198
-
1199
- for (i = 0; i < 16; i+=1) {
1200
- this.l[t][c][i] = data.get8();
1201
- count+=1;
1202
- }
1203
-
1204
- for (i = 0; i < 16; i+=1) {
1205
- for (j = 0; j < this.l[t][c][i]; j+=1) {
1206
- if (count > length) {
1207
- throw new Error("ERROR: Huffman table format error [count>Lh]");
1208
- }
1209
-
1210
- this.v[t][c][i][j] = data.get8();
1211
- count+=1;
1212
- }
1213
- }
1214
- }
1215
-
1216
- if (count !== length) {
1217
- throw new Error("ERROR: Huffman table format error [count!=Lf]");
1218
- }
1219
-
1220
- for (i = 0; i < 4; i+=1) {
1221
- for (j = 0; j < 2; j+=1) {
1222
- if (this.tc[i][j] !== 0) {
1223
- this.buildHuffTable(HuffTab[i][j], this.l[i][j], this.v[i][j]);
1224
- }
1225
- }
1226
- }
1227
-
1228
- return 1;
1229
- };
1230
-
1231
-
1232
-
1233
- // Build_HuffTab()
1234
- // Parameter: t table ID
1235
- // c table class ( 0 for DC, 1 for AC )
1236
- // L[i] # of codewords which length is i
1237
- // V[i][j] Huffman Value (length=i)
1238
- // Effect:
1239
- // build up HuffTab[t][c] using L and V.
1240
- jpeg.lossless.HuffmanTable.prototype.buildHuffTable = function(tab, L, V) {
1241
- /*jslint bitwise: true */
1242
-
1243
- var currentTable, temp, k, i, j, n;
1244
- temp = 256;
1245
- k = 0;
1246
-
1247
- for (i = 0; i < 8; i+=1) { // i+1 is Code length
1248
- for (j = 0; j < L[i]; j+=1) {
1249
- for (n = 0; n < (temp >> (i + 1)); n+=1) {
1250
- tab[k] = V[i][j] | ((i + 1) << 8);
1251
- k+=1;
1252
- }
1253
- }
1254
- }
1255
-
1256
- for (i = 1; k < 256; i+=1, k+=1) {
1257
- tab[k] = i | jpeg.lossless.HuffmanTable.MSB;
1258
- }
1259
-
1260
- currentTable = 1;
1261
- k = 0;
1262
-
1263
- for (i = 8; i < 16; i+=1) { // i+1 is Code length
1264
- for (j = 0; j < L[i]; j+=1) {
1265
- for (n = 0; n < (temp >> (i - 7)); n+=1) {
1266
- tab[(currentTable * 256) + k] = V[i][j] | ((i + 1) << 8);
1267
- k+=1;
1268
- }
1269
-
1270
- if (k >= 256) {
1271
- if (k > 256) {
1272
- throw new Error("ERROR: Huffman table error(1)!");
1273
- }
1274
-
1275
- k = 0;
1276
- currentTable+=1;
1277
- }
1278
- }
1279
- }
1280
- };
1281
-
1282
-
1283
- /*** Exports ***/
1284
-
1285
- var moduleType = typeof module;
1286
- if ((moduleType !== 'undefined') && module.exports) {
1287
- module.exports = jpeg.lossless.HuffmanTable;
1288
- }
1289
-
1290
- },{"./data-stream.js":2,"./utils.js":10}],6:[function(require,module,exports){
1291
- /*jslint browser: true, node: true */
1292
- /*global require, module */
1293
-
1294
- "use strict";
1295
-
1296
- /*** Imports ****/
1297
-
1298
- /**
1299
- * jpeg
1300
- * @type {*|{}}
1301
- */
1302
- var jpeg = jpeg || {};
1303
-
1304
- /**
1305
- * jpeg.lossless
1306
- * @type {*|{}}
1307
- */
1308
- jpeg.lossless = jpeg.lossless || {};
1309
-
1310
-
1311
- jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || ((typeof require !== 'undefined') ? require('./component-spec.js') : null);
1312
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
1313
- jpeg.lossless.Decoder = jpeg.lossless.Decoder || ((typeof require !== 'undefined') ? require('./decoder.js') : null);
1314
- jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || ((typeof require !== 'undefined') ? require('./frame-header.js') : null);
1315
- jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || ((typeof require !== 'undefined') ? require('./huffman-table.js') : null);
1316
- jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || ((typeof require !== 'undefined') ? require('./quantization-table.js') : null);
1317
- jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || ((typeof require !== 'undefined') ? require('./scan-component.js') : null);
1318
- jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || ((typeof require !== 'undefined') ? require('./scan-header.js') : null);
1319
- jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null);
1320
-
1321
-
1322
- /*** Exports ***/
1323
- var moduleType = typeof module;
1324
- if ((moduleType !== 'undefined') && module.exports) {
1325
- module.exports = jpeg;
1326
- }
1327
-
1328
- },{"./component-spec.js":1,"./data-stream.js":2,"./decoder.js":3,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-component.js":8,"./scan-header.js":9,"./utils.js":10}],7:[function(require,module,exports){
1329
- /*
1330
- * Copyright (C) 2015 Michael Martinez
1331
- * Changes: Added support for selection values 2-7, fixed minor bugs &
1332
- * warnings, split into multiple class files, and general clean up.
1333
- *
1334
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
1335
- */
1336
-
1337
- /*
1338
- * Copyright (C) Helmut Dersch
1339
- *
1340
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1341
- * of this software and associated documentation files (the "Software"), to deal
1342
- * in the Software without restriction, including without limitation the rights
1343
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1344
- * copies of the Software, and to permit persons to whom the Software is
1345
- * furnished to do so, subject to the following conditions:
1346
-
1347
- * The above copyright notice and this permission notice shall be included in
1348
- * all copies or substantial portions of the Software.
1349
-
1350
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1351
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1352
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1353
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1354
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1355
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1356
- * THE SOFTWARE.
1357
- */
1358
-
1359
- /*jslint browser: true, node: true */
1360
- /*global require, module */
1361
-
1362
- "use strict";
1363
-
1364
- /*** Imports ***/
1365
- var jpeg = jpeg || {};
1366
- jpeg.lossless = jpeg.lossless || {};
1367
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
1368
- jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null);
1369
-
1370
-
1371
- /*** Constructor ***/
1372
- jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || function () {
1373
- this.precision = []; // Quantization precision 8 or 16
1374
- this.tq = []; // 1: this table is presented
1375
- this.quantTables = jpeg.lossless.Utils.createArray(4, 64); // Tables
1376
-
1377
- this.tq[0] = 0;
1378
- this.tq[1] = 0;
1379
- this.tq[2] = 0;
1380
- this.tq[3] = 0;
1381
- };
1382
-
1383
-
1384
-
1385
- /*** Static Methods ***/
1386
-
1387
- jpeg.lossless.QuantizationTable.enhanceQuantizationTable = function(qtab, table) {
1388
- /*jslint bitwise: true */
1389
-
1390
- var i;
1391
-
1392
- for (i = 0; i < 8; i+=1) {
1393
- qtab[table[(0 * 8) + i]] *= 90;
1394
- qtab[table[(4 * 8) + i]] *= 90;
1395
- qtab[table[(2 * 8) + i]] *= 118;
1396
- qtab[table[(6 * 8) + i]] *= 49;
1397
- qtab[table[(5 * 8) + i]] *= 71;
1398
- qtab[table[(1 * 8) + i]] *= 126;
1399
- qtab[table[(7 * 8) + i]] *= 25;
1400
- qtab[table[(3 * 8) + i]] *= 106;
1401
- }
1402
-
1403
- for (i = 0; i < 8; i+=1) {
1404
- qtab[table[0 + (8 * i)]] *= 90;
1405
- qtab[table[4 + (8 * i)]] *= 90;
1406
- qtab[table[2 + (8 * i)]] *= 118;
1407
- qtab[table[6 + (8 * i)]] *= 49;
1408
- qtab[table[5 + (8 * i)]] *= 71;
1409
- qtab[table[1 + (8 * i)]] *= 126;
1410
- qtab[table[7 + (8 * i)]] *= 25;
1411
- qtab[table[3 + (8 * i)]] *= 106;
1412
- }
1413
-
1414
- for (i = 0; i < 64; i+=1) {
1415
- qtab[i] >>= 6;
1416
- }
1417
- };
1418
-
1419
-
1420
- /*** Prototype Methods ***/
1421
-
1422
- jpeg.lossless.QuantizationTable.prototype.read = function (data, table) {
1423
- /*jslint bitwise: true */
1424
-
1425
- var count = 0, length, temp, t, i;
1426
-
1427
- length = data.get16();
1428
- count += 2;
1429
-
1430
- while (count < length) {
1431
- temp = data.get8();
1432
- count+=1;
1433
- t = temp & 0x0F;
1434
-
1435
- if (t > 3) {
1436
- throw new Error("ERROR: Quantization table ID > 3");
1437
- }
1438
-
1439
- this.precision[t] = temp >> 4;
1440
-
1441
- if (this.precision[t] === 0) {
1442
- this.precision[t] = 8;
1443
- } else if (this.precision[t] === 1) {
1444
- this.precision[t] = 16;
1445
- } else {
1446
- throw new Error("ERROR: Quantization table precision error");
1447
- }
1448
-
1449
- this.tq[t] = 1;
1450
-
1451
- if (this.precision[t] === 8) {
1452
- for (i = 0; i < 64; i+=1) {
1453
- if (count > length) {
1454
- throw new Error("ERROR: Quantization table format error");
1455
- }
1456
-
1457
- this.quantTables[t][i] = data.get8();
1458
- count+=1;
1459
- }
1460
-
1461
- jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table);
1462
- } else {
1463
- for (i = 0; i < 64; i+=1) {
1464
- if (count > length) {
1465
- throw new Error("ERROR: Quantization table format error");
1466
- }
1467
-
1468
- this.quantTables[t][i] = data.get16();
1469
- count += 2;
1470
- }
1471
-
1472
- jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table);
1473
- }
1474
- }
1475
-
1476
- if (count !== length) {
1477
- throw new Error("ERROR: Quantization table error [count!=Lq]");
1478
- }
1479
-
1480
- return 1;
1481
- };
1482
-
1483
-
1484
-
1485
- /*** Exports ***/
1486
-
1487
- var moduleType = typeof module;
1488
- if ((moduleType !== 'undefined') && module.exports) {
1489
- module.exports = jpeg.lossless.QuantizationTable;
1490
- }
1491
-
1492
- },{"./data-stream.js":2,"./utils.js":10}],8:[function(require,module,exports){
1493
- /*
1494
- * Copyright (C) 2015 Michael Martinez
1495
- * Changes: Added support for selection values 2-7, fixed minor bugs &
1496
- * warnings, split into multiple class files, and general clean up.
1497
- *
1498
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
1499
- */
1500
-
1501
- /*
1502
- * Copyright (C) Helmut Dersch
1503
- *
1504
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1505
- * of this software and associated documentation files (the "Software"), to deal
1506
- * in the Software without restriction, including without limitation the rights
1507
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1508
- * copies of the Software, and to permit persons to whom the Software is
1509
- * furnished to do so, subject to the following conditions:
1510
-
1511
- * The above copyright notice and this permission notice shall be included in
1512
- * all copies or substantial portions of the Software.
1513
-
1514
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1516
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1517
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1518
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1519
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1520
- * THE SOFTWARE.
1521
- */
1522
-
1523
- /*jslint browser: true, node: true */
1524
- /*global require, module */
1525
-
1526
- "use strict";
1527
-
1528
- /*** Imports ***/
1529
- var jpeg = jpeg || {};
1530
- jpeg.lossless = jpeg.lossless || {};
1531
-
1532
-
1533
- /*** Constructor ***/
1534
- jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || function () {
1535
- this.acTabSel = 0; // AC table selector
1536
- this.dcTabSel = 0; // DC table selector
1537
- this.scanCompSel = 0; // Scan component selector
1538
- };
1539
-
1540
-
1541
-
1542
- /*** Exports ***/
1543
-
1544
- var moduleType = typeof module;
1545
- if ((moduleType !== 'undefined') && module.exports) {
1546
- module.exports = jpeg.lossless.ScanComponent;
1547
- }
1548
-
1549
- },{}],9:[function(require,module,exports){
1550
- /*
1551
- * Copyright (C) 2015 Michael Martinez
1552
- * Changes: Added support for selection values 2-7, fixed minor bugs &
1553
- * warnings, split into multiple class files, and general clean up.
1554
- *
1555
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
1556
- */
1557
-
1558
- /*
1559
- * Copyright (C) Helmut Dersch
1560
- *
1561
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1562
- * of this software and associated documentation files (the "Software"), to deal
1563
- * in the Software without restriction, including without limitation the rights
1564
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1565
- * copies of the Software, and to permit persons to whom the Software is
1566
- * furnished to do so, subject to the following conditions:
1567
-
1568
- * The above copyright notice and this permission notice shall be included in
1569
- * all copies or substantial portions of the Software.
1570
-
1571
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1572
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1573
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1574
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1575
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1576
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1577
- * THE SOFTWARE.
1578
- */
1579
-
1580
- /*jslint browser: true, node: true */
1581
- /*global require, module */
1582
-
1583
- "use strict";
1584
-
1585
- /*** Imports ***/
1586
- var jpeg = jpeg || {};
1587
- jpeg.lossless = jpeg.lossless || {};
1588
- jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null);
1589
- jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || ((typeof require !== 'undefined') ? require('./scan-component.js') : null);
1590
-
1591
-
1592
- /*** Constructor ***/
1593
- jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || function () {
1594
- this.ah = 0;
1595
- this.al = 0;
1596
- this.numComp = 0; // Number of components in the scan
1597
- this.selection = 0; // Start of spectral or predictor selection
1598
- this.spectralEnd = 0; // End of spectral selection
1599
- this.components = [];
1600
- };
1601
-
1602
-
1603
- /*** Prototype Methods ***/
1604
-
1605
- jpeg.lossless.ScanHeader.prototype.read = function(data) {
1606
- /*jslint bitwise: true */
1607
-
1608
- var count = 0, length, i, temp;
1609
-
1610
- length = data.get16();
1611
- count += 2;
1612
-
1613
- this.numComp = data.get8();
1614
- count+=1;
1615
-
1616
- for (i = 0; i < this.numComp; i+=1) {
1617
- this.components[i] = new jpeg.lossless.ScanComponent();
1618
-
1619
- if (count > length) {
1620
- throw new Error("ERROR: scan header format error");
1621
- }
1622
-
1623
- this.components[i].scanCompSel = data.get8();
1624
- count+=1;
1625
-
1626
- temp = data.get8();
1627
- count+=1;
1628
-
1629
- this.components[i].dcTabSel = (temp >> 4);
1630
- this.components[i].acTabSel = (temp & 0x0F);
1631
- }
1632
-
1633
- this.selection = data.get8();
1634
- count+=1;
1635
-
1636
- this.spectralEnd = data.get8();
1637
- count+=1;
1638
-
1639
- temp = data.get8();
1640
- this.ah = (temp >> 4);
1641
- this.al = (temp & 0x0F);
1642
- count+=1;
1643
-
1644
- if (count !== length) {
1645
- throw new Error("ERROR: scan header format error [count!=Ns]");
1646
- }
1647
-
1648
- return 1;
1649
- };
1650
-
1651
-
1652
-
1653
- /*** Exports ***/
1654
-
1655
- var moduleType = typeof module;
1656
- if ((moduleType !== 'undefined') && module.exports) {
1657
- module.exports = jpeg.lossless.ScanHeader;
1658
- }
1659
-
1660
- },{"./data-stream.js":2,"./scan-component.js":8}],10:[function(require,module,exports){
1661
- /*
1662
- * Copyright (C) 2015 Michael Martinez
1663
- * Changes: Added support for selection values 2-7, fixed minor bugs &
1664
- * warnings, split into multiple class files, and general clean up.
1665
- *
1666
- * 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT.
1667
- */
1668
-
1669
- /*
1670
- * Copyright (C) Helmut Dersch
1671
- *
1672
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1673
- * of this software and associated documentation files (the "Software"), to deal
1674
- * in the Software without restriction, including without limitation the rights
1675
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1676
- * copies of the Software, and to permit persons to whom the Software is
1677
- * furnished to do so, subject to the following conditions:
1678
-
1679
- * The above copyright notice and this permission notice shall be included in
1680
- * all copies or substantial portions of the Software.
1681
-
1682
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1683
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1684
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1685
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1686
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1687
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1688
- * THE SOFTWARE.
1689
- */
1690
-
1691
- /*jslint browser: true, node: true */
1692
- /*global require, module */
1693
-
1694
- "use strict";
1695
-
1696
- /*** Imports ***/
1697
- var jpeg = jpeg || {};
1698
- jpeg.lossless = jpeg.lossless || {};
1699
-
1700
-
1701
- /*** Constructor ***/
1702
- jpeg.lossless.Utils = jpeg.lossless.Utils || {};
1703
-
1704
-
1705
- /*** Static methods ***/
1706
-
1707
- // http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript
1708
- jpeg.lossless.Utils.createArray = function (length) {
1709
- var arr = new Array(length || 0),
1710
- i = length;
1711
-
1712
- if (arguments.length > 1) {
1713
- var args = Array.prototype.slice.call(arguments, 1);
1714
- while(i--) arr[length-1 - i] = jpeg.lossless.Utils.createArray.apply(this, args);
1715
- }
1716
-
1717
- return arr;
1718
- };
1719
-
1720
-
1721
- // http://stackoverflow.com/questions/18638900/javascript-crc32
1722
- jpeg.lossless.Utils.makeCRCTable = function(){
1723
- var c;
1724
- var crcTable = [];
1725
- for(var n =0; n < 256; n++){
1726
- c = n;
1727
- for(var k =0; k < 8; k++){
1728
- c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
1729
- }
1730
- crcTable[n] = c;
1731
- }
1732
- return crcTable;
1733
- };
1734
-
1735
- jpeg.lossless.Utils.crc32 = function(dataView) {
1736
- var uint8view = new Uint8Array(dataView.buffer);
1737
- var crcTable = jpeg.lossless.Utils.crcTable || (jpeg.lossless.Utils.crcTable = jpeg.lossless.Utils.makeCRCTable());
1738
- var crc = 0 ^ (-1);
1739
-
1740
- for (var i = 0; i < uint8view.length; i++ ) {
1741
- crc = (crc >>> 8) ^ crcTable[(crc ^ uint8view[i]) & 0xFF];
1742
- }
1743
-
1744
- return (crc ^ (-1)) >>> 0;
1745
- };
1746
-
1747
-
1748
- /*** Exports ***/
1749
-
1750
- var moduleType = typeof module;
1751
- if ((moduleType !== 'undefined') && module.exports) {
1752
- module.exports = jpeg.lossless.Utils;
1753
- }
1754
-
1755
- },{}]},{},[6])(6)
1756
- });