@li0ard/wsq 0.1.1 → 0.1.2
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.
- package/dist/const.js +3 -3
- package/dist/index.d.ts +3 -0
- package/dist/index.js +35 -33
- package/package.json +1 -1
package/dist/const.js
CHANGED
|
@@ -208,8 +208,8 @@ const buildWTree = (token, wtreelen, width, height) => {
|
|
|
208
208
|
token.wtree[19].leny = (token.wtree[15].leny + 1) / 2;
|
|
209
209
|
};
|
|
210
210
|
const wtree4 = (token, start1, start2, lenx, leny, x, y, stop1) => {
|
|
211
|
-
|
|
212
|
-
|
|
211
|
+
const p1 = start1, p2 = start2;
|
|
212
|
+
const evenx = lenx % 2, eveny = leny % 2;
|
|
213
213
|
if (!token.wtree)
|
|
214
214
|
return;
|
|
215
215
|
token.wtree[p1].x = x;
|
|
@@ -408,7 +408,7 @@ const qtree16 = (token, start, lenx, leny, x, y, rw, cl) => {
|
|
|
408
408
|
token.qtree[p + 15].leny = token.qtree[p + 10].leny;
|
|
409
409
|
};
|
|
410
410
|
const qtree4 = (token, start, lenx, leny, x, y) => {
|
|
411
|
-
|
|
411
|
+
const p = start, evenx = lenx % 2, eveny = leny % 2;
|
|
412
412
|
if (!token.qtree)
|
|
413
413
|
return;
|
|
414
414
|
token.qtree[p].x = x;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { BitmapWithMetadata } from "./bitmap.js";
|
|
2
|
+
export declare const wsqToRAW: (data: Uint8Array) => BitmapWithMetadata;
|
|
1
3
|
/**
|
|
2
4
|
* Convert Wavelet Scalar Quantization (`.wsq`) to PNG
|
|
3
5
|
* @param data WSQ buffer
|
|
4
6
|
*/
|
|
5
7
|
export declare const wsqToPNG: (data: Uint8Array) => Uint8Array;
|
|
8
|
+
export { BitmapWithMetadata } from "./bitmap.js";
|
package/dist/index.js
CHANGED
|
@@ -12,14 +12,14 @@ class WSQDecoder {
|
|
|
12
12
|
this.getCTableWSQ(dataInput, token, marker);
|
|
13
13
|
marker = this.getCMarkerWSQ(dataInput, TBLS_N_SOF);
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const frmHeaderWSQ = this.getCFrameHeaderWSQ(dataInput);
|
|
16
|
+
const width = frmHeaderWSQ.width;
|
|
17
|
+
const height = frmHeaderWSQ.height;
|
|
18
18
|
buildWSQTrees(token, width, height);
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const qdata = this.huffmanDecodeDataMem(dataInput, token, width * height);
|
|
20
|
+
const fdata = this.unquantize(token, qdata, width, height);
|
|
21
21
|
this.wsqReconstruct(token, fdata, width, height);
|
|
22
|
-
|
|
22
|
+
const cdata = this.convertImageToByte(fdata, width, height, frmHeaderWSQ.mShift, frmHeaderWSQ.rScale);
|
|
23
23
|
const nistcom = new Map();
|
|
24
24
|
const comments = [];
|
|
25
25
|
for (const comment of token.comments) {
|
|
@@ -68,7 +68,7 @@ class WSQDecoder {
|
|
|
68
68
|
return result;
|
|
69
69
|
}
|
|
70
70
|
convertImageToByte(img, width, height, mShift, rScale) {
|
|
71
|
-
|
|
71
|
+
const data = new Uint8Array(width * height);
|
|
72
72
|
let idx = 0;
|
|
73
73
|
for (let r = 0; r < height; r++) {
|
|
74
74
|
for (let c = 0; c < width; c++) {
|
|
@@ -93,10 +93,10 @@ class WSQDecoder {
|
|
|
93
93
|
throw new Error("ERROR: wsq_reconstruct : Lopass filter coefficients not defined");
|
|
94
94
|
if (token.tableDTT.hidef != 1)
|
|
95
95
|
throw new Error("ERROR: wsq_reconstruct : Hipass filter coefficients not defined");
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
const numPix = width * height;
|
|
97
|
+
const fdataTemp = new Float32Array(numPix);
|
|
98
98
|
for (let node = W_TREELEN - 1; node >= 0; node--) {
|
|
99
|
-
|
|
99
|
+
const fdataBse = (token.wtree[node].y * width) + token.wtree[node].x;
|
|
100
100
|
this.joinLets(fdataTemp, fdata, 0, fdataBse, token.wtree[node].lenx, token.wtree[node].leny, 1, width, token.tableDTT.hifilt, token.tableDTT.hisz, token.tableDTT.lofilt, token.tableDTT.losz, token.wtree[node].invcl);
|
|
101
101
|
this.joinLets(fdata, fdataTemp, fdataBse, 0, token.wtree[node].leny, token.wtree[node].lenx, width, 1, token.tableDTT.hifilt, token.tableDTT.hisz, token.tableDTT.lofilt, token.tableDTT.losz, token.wtree[node].invrw);
|
|
102
102
|
}
|
|
@@ -408,10 +408,10 @@ class WSQDecoder {
|
|
|
408
408
|
}
|
|
409
409
|
}
|
|
410
410
|
unquantize(token, sip, width, height) {
|
|
411
|
-
|
|
411
|
+
const fip = new Float32Array(width * height);
|
|
412
412
|
if (token.tableDQT.dqtDef != 1)
|
|
413
413
|
throw new Error("ERROR: unquantize : quantization table parameters not defined!");
|
|
414
|
-
|
|
414
|
+
const binCenter = token.tableDQT.binCenter;
|
|
415
415
|
let sptr = 0;
|
|
416
416
|
for (let cnt = 0; cnt < NUM_SUBBANDS; cnt++) {
|
|
417
417
|
if (token.tableDQT.qBin[cnt] == 0.0)
|
|
@@ -435,13 +435,13 @@ class WSQDecoder {
|
|
|
435
435
|
return fip;
|
|
436
436
|
}
|
|
437
437
|
huffmanDecodeDataMem(dataInput, token, size) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
438
|
+
const qdata = new Array(size);
|
|
439
|
+
const maxcode = new Array(MAX_HUFFBITS + 1);
|
|
440
|
+
const mincode = new Array(MAX_HUFFBITS + 1);
|
|
441
|
+
const valptr = new Array(MAX_HUFFBITS + 1);
|
|
442
|
+
const marker = new Ref(this.getCMarkerWSQ(dataInput, TBLS_N_SOB));
|
|
443
|
+
const bitCount = new Ref(0);
|
|
444
|
+
const nextByte = new Ref(0);
|
|
445
445
|
let hufftableId = 0;
|
|
446
446
|
let ip = 0;
|
|
447
447
|
let isPrematureEOF = false;
|
|
@@ -454,14 +454,14 @@ class WSQDecoder {
|
|
|
454
454
|
hufftableId = this.getCBlockHeader(dataInput);
|
|
455
455
|
if (token.tableDHT[hufftableId].tabdef != 1)
|
|
456
456
|
throw new Error("ERROR : huffmanDecodeDataMem : huffman table undefined.");
|
|
457
|
-
|
|
457
|
+
const hufftable = this.buildHuffsizes(token.tableDHT[hufftableId].huffbits, MAX_HUFFCOUNTS_WSQ);
|
|
458
458
|
this.buildHuffcodes(hufftable);
|
|
459
459
|
this.genDecodeTable(hufftable, maxcode, mincode, valptr, token.tableDHT[hufftableId].huffbits);
|
|
460
460
|
bitCount.value = 0;
|
|
461
461
|
marker.value = 0;
|
|
462
462
|
}
|
|
463
463
|
try {
|
|
464
|
-
|
|
464
|
+
const nodeptr = this.decodeDataMem(dataInput, mincode, maxcode, valptr, token.tableDHT[hufftableId].huffvalues, bitCount, marker, nextByte);
|
|
465
465
|
if (nodeptr == -1)
|
|
466
466
|
continue;
|
|
467
467
|
if (nodeptr > 0 && nodeptr <= 100) {
|
|
@@ -509,7 +509,7 @@ class WSQDecoder {
|
|
|
509
509
|
if (marker.value != 0)
|
|
510
510
|
return -1;
|
|
511
511
|
}
|
|
512
|
-
|
|
512
|
+
const inx2 = valptr[inx] + code - mincode[inx];
|
|
513
513
|
return huffvalues[inx2];
|
|
514
514
|
}
|
|
515
515
|
getCNextbitsWSQ(dataInput, marker, bitCount, bitsReq, nextByte) {
|
|
@@ -518,7 +518,7 @@ class WSQDecoder {
|
|
|
518
518
|
this.offset += 1;
|
|
519
519
|
bitCount.value = 8;
|
|
520
520
|
if (nextByte.value == 0xFF) {
|
|
521
|
-
|
|
521
|
+
const code2 = dataInput.getUint8(this.offset);
|
|
522
522
|
this.offset += 1;
|
|
523
523
|
if (code2 != 0x00 && bitsReq == 1) {
|
|
524
524
|
marker.value = (nextByte.value << 8) | code2;
|
|
@@ -565,7 +565,7 @@ class WSQDecoder {
|
|
|
565
565
|
}
|
|
566
566
|
buildHuffsizes(huffbits, maxHuffcounts) {
|
|
567
567
|
let numberOfCodes = 1;
|
|
568
|
-
|
|
568
|
+
const huffcodeTable = new Array(maxHuffcounts + 1);
|
|
569
569
|
let tempSize = 0;
|
|
570
570
|
for (let codeSize = 1; codeSize <= MAX_HUFFBITS; codeSize++) {
|
|
571
571
|
while (numberOfCodes <= huffbits[codeSize - 1]) {
|
|
@@ -603,12 +603,12 @@ class WSQDecoder {
|
|
|
603
603
|
getCBlockHeader(dataInput) {
|
|
604
604
|
dataInput.getUint16(this.offset);
|
|
605
605
|
this.offset += 2;
|
|
606
|
-
|
|
606
|
+
const value = dataInput.getUint8(this.offset);
|
|
607
607
|
this.offset += 1;
|
|
608
608
|
return value;
|
|
609
609
|
}
|
|
610
610
|
getCFrameHeaderWSQ(dataInput) {
|
|
611
|
-
|
|
611
|
+
const headerFrm = new HeaderFrm();
|
|
612
612
|
dataInput.getUint16(this.offset);
|
|
613
613
|
this.offset += 2;
|
|
614
614
|
headerFrm.black = dataInput.getUint8(this.offset);
|
|
@@ -688,7 +688,7 @@ class WSQDecoder {
|
|
|
688
688
|
}
|
|
689
689
|
}
|
|
690
690
|
getCHuffmanTableWSQ(dataInput, token) {
|
|
691
|
-
|
|
691
|
+
const firstHuffmanTable = this.getCHuffmanTable(dataInput, token, MAX_HUFFCOUNTS_WSQ, 0, true);
|
|
692
692
|
let tableId = firstHuffmanTable.tableId;
|
|
693
693
|
token.tableDHT[tableId].huffbits = firstHuffmanTable.huffbits.slice();
|
|
694
694
|
token.tableDHT[tableId].huffvalues = firstHuffmanTable.huffvalues.slice();
|
|
@@ -706,7 +706,7 @@ class WSQDecoder {
|
|
|
706
706
|
}
|
|
707
707
|
}
|
|
708
708
|
getCHuffmanTable(dataInput, token, maxHuffcounts, bytesLeft, readTableLen) {
|
|
709
|
-
|
|
709
|
+
const huffmanTable = new HuffmanTable();
|
|
710
710
|
if (readTableLen) {
|
|
711
711
|
huffmanTable.tableLen = dataInput.getUint16(this.offset);
|
|
712
712
|
this.offset += 2;
|
|
@@ -796,11 +796,11 @@ class WSQDecoder {
|
|
|
796
796
|
const aLofilt = new Float32Array(aSize);
|
|
797
797
|
aSize--;
|
|
798
798
|
for (let cnt = 0; cnt <= aSize; cnt++) {
|
|
799
|
-
|
|
799
|
+
const sign = dataInput.getUint8(this.offset);
|
|
800
800
|
this.offset += 1;
|
|
801
801
|
let scale = dataInput.getUint8(this.offset);
|
|
802
802
|
this.offset += 1;
|
|
803
|
-
|
|
803
|
+
const shrtDat = (dataInput.getInt32(this.offset) & 0xFFFFFFFF) >>> 0;
|
|
804
804
|
this.offset += 4;
|
|
805
805
|
aLofilt[cnt] = shrtDat;
|
|
806
806
|
while (scale > 0) {
|
|
@@ -826,11 +826,11 @@ class WSQDecoder {
|
|
|
826
826
|
const aHifilt = new Float32Array(aSize);
|
|
827
827
|
aSize--;
|
|
828
828
|
for (let cnt = 0; cnt <= aSize; cnt++) {
|
|
829
|
-
|
|
829
|
+
const sign = dataInput.getUint8(this.offset);
|
|
830
830
|
this.offset += 1;
|
|
831
831
|
let scale = dataInput.getUint8(this.offset);
|
|
832
832
|
this.offset += 1;
|
|
833
|
-
|
|
833
|
+
const shrtDat = (dataInput.getInt32(this.offset) & 0xFFFFFFFF) >>> 0;
|
|
834
834
|
this.offset += 4;
|
|
835
835
|
aHifilt[cnt] = shrtDat;
|
|
836
836
|
while (scale > 0) {
|
|
@@ -861,12 +861,13 @@ class WSQDecoder {
|
|
|
861
861
|
return num;
|
|
862
862
|
}
|
|
863
863
|
}
|
|
864
|
+
export const wsqToRAW = (data) => new WSQDecoder().decode(data);
|
|
864
865
|
/**
|
|
865
866
|
* Convert Wavelet Scalar Quantization (`.wsq`) to PNG
|
|
866
867
|
* @param data WSQ buffer
|
|
867
868
|
*/
|
|
868
869
|
export const wsqToPNG = (data) => {
|
|
869
|
-
const grayscale =
|
|
870
|
+
const grayscale = wsqToRAW(data);
|
|
870
871
|
const rgba = new Uint8Array(grayscale.width * grayscale.height * 4);
|
|
871
872
|
for (let i = 0; i < grayscale.width * grayscale.height; i++) {
|
|
872
873
|
rgba[i * 4 + 0] = grayscale.pixels[i];
|
|
@@ -876,3 +877,4 @@ export const wsqToPNG = (data) => {
|
|
|
876
877
|
}
|
|
877
878
|
return new Uint8Array(UPNG.encode([rgba.buffer], grayscale.width, grayscale.height, 0));
|
|
878
879
|
};
|
|
880
|
+
export { BitmapWithMetadata } from "./bitmap.js";
|