imagescript 3.6.0 → 3.7.0
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/README.md +4 -4
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/dist/codecs/av1/encode.js +47 -23
- package/dist/codecs/jxl/acs-enc.js +294 -0
- package/dist/codecs/jxl/aq-enc.js +176 -0
- package/dist/codecs/jxl/bit-writer.js +46 -0
- package/dist/codecs/jxl/color.js +61 -1
- package/dist/codecs/jxl/dct.js +7 -7
- package/dist/codecs/jxl/entropy-enc.js +589 -0
- package/dist/codecs/jxl/entropy.js +1 -1
- package/dist/codecs/jxl/fast-lossless.js +2 -107
- package/dist/codecs/jxl/fdct.js +1114 -0
- package/dist/codecs/jxl/filters.js +49 -2
- package/dist/codecs/jxl/frame.js +4 -1
- package/dist/codecs/jxl/headers-enc.js +60 -0
- package/dist/codecs/jxl/idct.js +804 -2
- package/dist/codecs/jxl/modular-enc.js +100 -0
- package/dist/codecs/jxl/modular.js +3 -2
- package/dist/codecs/jxl/quant.js +1 -0
- package/dist/codecs/jxl/transforms-enc.js +188 -0
- package/dist/codecs/jxl/vardct-enc.js +397 -0
- package/dist/codecs/jxl/vardct.js +6 -6
- package/dist/codecs/jxl.js +3 -2
- package/dist/codecs/vp8l.js +356 -58
- package/dist/image.d.ts +23 -7
- package/dist/image.js +11 -2
- package/dist/index.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/codecs/vp8l.js
CHANGED
|
@@ -729,7 +729,7 @@ function prefixEncode(value) {
|
|
|
729
729
|
const second = d >> h - 1 & 1;
|
|
730
730
|
return { symbol: 2 * h + second, bits: h - 1, extra: d & (1 << h - 1) - 1 };
|
|
731
731
|
}
|
|
732
|
-
function tokenize(pixels, width
|
|
732
|
+
function tokenize(pixels, width) {
|
|
733
733
|
const n = pixels.length;
|
|
734
734
|
const ops = new Uint8Array(n), values = new Uint32Array(n), distances = new Uint32Array(n);
|
|
735
735
|
let count = 0;
|
|
@@ -746,12 +746,15 @@ function tokenize(pixels, width, cacheBits) {
|
|
|
746
746
|
const hashes = new Int32Array(n);
|
|
747
747
|
for (let i = 0; i + 1 < n; i++)
|
|
748
748
|
hashes[i] = Math.imul(Math.imul(pixels[i], 0x1e35a7bd) ^ pixels[i + 1], 0x9e3779b1) >>> 32 - HASH_BITS;
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
749
|
+
let inserted = 0;
|
|
750
|
+
const insertUpTo = (end) => {
|
|
751
|
+
for (; inserted < end && inserted + 1 < n; inserted++) {
|
|
752
|
+
const h = hashes[inserted];
|
|
753
|
+
chain[inserted] = head[h];
|
|
754
|
+
head[h] = inserted;
|
|
755
|
+
}
|
|
756
|
+
if (inserted < end)
|
|
757
|
+
inserted = end;
|
|
755
758
|
};
|
|
756
759
|
const matchLength = (i, j) => {
|
|
757
760
|
const max = Math.min(MAX_LENGTH, n - i);
|
|
@@ -760,11 +763,10 @@ function tokenize(pixels, width, cacheBits) {
|
|
|
760
763
|
l++;
|
|
761
764
|
return l;
|
|
762
765
|
};
|
|
763
|
-
|
|
764
|
-
const
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
let bestLength = 0, bestDistance = 0;
|
|
766
|
+
let bestLength = 0, bestDistance = 0;
|
|
767
|
+
const find = (i) => {
|
|
768
|
+
bestLength = 0;
|
|
769
|
+
bestDistance = 0;
|
|
768
770
|
if (i >= 1) {
|
|
769
771
|
const l = matchLength(i, i - 1);
|
|
770
772
|
if (l > bestLength) {
|
|
@@ -792,44 +794,56 @@ function tokenize(pixels, width, cacheBits) {
|
|
|
792
794
|
break;
|
|
793
795
|
}
|
|
794
796
|
}
|
|
797
|
+
};
|
|
798
|
+
for (let i = 0; i < n;) {
|
|
799
|
+
insertUpTo(i);
|
|
800
|
+
find(i);
|
|
801
|
+
if (bestLength >= MIN_LENGTH && bestLength < MAX_LENGTH && i + 1 < n) {
|
|
802
|
+
const length = bestLength, distance = bestDistance;
|
|
803
|
+
insertUpTo(i + 1);
|
|
804
|
+
find(i + 1);
|
|
805
|
+
if (bestLength > length + 1) {
|
|
806
|
+
ops[count] = 0;
|
|
807
|
+
values[count++] = pixels[i++];
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
bestLength = length;
|
|
811
|
+
bestDistance = distance;
|
|
812
|
+
}
|
|
795
813
|
if (bestLength >= MIN_LENGTH) {
|
|
796
814
|
ops[count] = 1;
|
|
797
815
|
values[count] = bestLength;
|
|
798
816
|
distances[count++] = distanceCode(bestDistance);
|
|
799
|
-
for (let k = 0; k < bestLength; k++)
|
|
800
|
-
insert(i + k);
|
|
801
817
|
i += bestLength;
|
|
802
818
|
continue;
|
|
803
819
|
}
|
|
804
|
-
const argb = pixels[i];
|
|
805
|
-
if (cache) {
|
|
806
|
-
for (; cached < i; cached++)
|
|
807
|
-
cache[Math.imul(pixels[cached], 0x1e35a7bd) >>> cacheShift] = pixels[cached];
|
|
808
|
-
const key = Math.imul(argb, 0x1e35a7bd) >>> cacheShift;
|
|
809
|
-
if (cache[key] === argb) {
|
|
810
|
-
ops[count] = 2;
|
|
811
|
-
values[count++] = key;
|
|
812
|
-
insert(i++);
|
|
813
|
-
continue;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
820
|
ops[count] = 0;
|
|
817
|
-
values[count++] =
|
|
818
|
-
insert(i++);
|
|
821
|
+
values[count++] = pixels[i++];
|
|
819
822
|
}
|
|
820
823
|
return { ops, values, distances, count };
|
|
821
824
|
}
|
|
822
|
-
function
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
825
|
+
function withCache(stream, pixels, bits) {
|
|
826
|
+
if (!bits)
|
|
827
|
+
return stream;
|
|
828
|
+
const { ops, values, distances, count } = stream;
|
|
829
|
+
const out = { ops: ops.slice(0, count), values: values.slice(0, count), distances, count };
|
|
830
|
+
const cache = new Uint32Array(1 << bits), shift = 32 - bits;
|
|
831
|
+
for (let k = 0, i = 0; k < count; k++) {
|
|
832
|
+
if (ops[k] === 1) {
|
|
833
|
+
for (const end = i + values[k]; i < end; i++)
|
|
834
|
+
cache[Math.imul(pixels[i], 0x1e35a7bd) >>> shift] = pixels[i];
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
const argb = pixels[i++], key = Math.imul(argb, 0x1e35a7bd) >>> shift;
|
|
838
|
+
if (cache[key] === argb) {
|
|
839
|
+
out.ops[k] = 2;
|
|
840
|
+
out.values[k] = key;
|
|
841
|
+
}
|
|
842
|
+
cache[key] = argb;
|
|
828
843
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
w.write(0, 1);
|
|
844
|
+
return out;
|
|
845
|
+
}
|
|
846
|
+
function histograms(stream, cacheBits) {
|
|
833
847
|
const greenSize = NUM_LITERAL_CODES + NUM_LENGTH_CODES + (cacheBits ? 1 << cacheBits : 0);
|
|
834
848
|
const counts = [new Int32Array(greenSize), new Int32Array(256), new Int32Array(256), new Int32Array(256), new Int32Array(NUM_DISTANCE_CODES)];
|
|
835
849
|
const { ops, values, distances, count } = stream;
|
|
@@ -848,24 +862,293 @@ function writeImage(w, pixels, width, isMain) {
|
|
|
848
862
|
else
|
|
849
863
|
counts[0][NUM_LITERAL_CODES + NUM_LENGTH_CODES + v]++;
|
|
850
864
|
}
|
|
851
|
-
|
|
852
|
-
|
|
865
|
+
return counts;
|
|
866
|
+
}
|
|
867
|
+
function bestCacheBits(stream, pixels) {
|
|
868
|
+
const { ops, values, count } = stream;
|
|
869
|
+
const caches = Array.from({ length: 11 }, (_, b) => new Uint32Array(1 << b));
|
|
870
|
+
const literals = Array.from({ length: 11 }, () => [0, 1, 2, 3].map(() => new Float64Array(256)));
|
|
871
|
+
const hits = Array.from({ length: 11 }, (_, b) => new Float64Array(1 << b));
|
|
872
|
+
for (let k = 0, i = 0; k < count; k++) {
|
|
873
|
+
if (ops[k] === 1) {
|
|
874
|
+
for (const end = i + values[k]; i < end; i++) {
|
|
875
|
+
const p = pixels[i], h = Math.imul(p, 0x1e35a7bd);
|
|
876
|
+
for (let b = 1; b <= 10; b++)
|
|
877
|
+
caches[b][h >>> 32 - b] = p;
|
|
878
|
+
}
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
const p = pixels[i++], h = Math.imul(p, 0x1e35a7bd);
|
|
882
|
+
const l0 = literals[0];
|
|
883
|
+
l0[0][p >> 8 & 255]++;
|
|
884
|
+
l0[1][p >> 16 & 255]++;
|
|
885
|
+
l0[2][p & 255]++;
|
|
886
|
+
l0[3][p >>> 24]++;
|
|
887
|
+
for (let b = 1; b <= 10; b++) {
|
|
888
|
+
const key = h >>> 32 - b, cache = caches[b];
|
|
889
|
+
if (cache[key] === p)
|
|
890
|
+
hits[b][key]++;
|
|
891
|
+
else {
|
|
892
|
+
const l = literals[b];
|
|
893
|
+
l[0][p >> 8 & 255]++;
|
|
894
|
+
l[1][p >> 16 & 255]++;
|
|
895
|
+
l[2][p & 255]++;
|
|
896
|
+
l[3][p >>> 24]++;
|
|
897
|
+
cache[key] = p;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
const bitsOf = (h, total) => {
|
|
902
|
+
let bits = 0;
|
|
903
|
+
for (let i = 0; i < h.length; i++)
|
|
904
|
+
if (h[i])
|
|
905
|
+
bits += 5 - h[i] * Math.log2(h[i] / total);
|
|
906
|
+
return bits;
|
|
907
|
+
};
|
|
908
|
+
let best = 0, bestBits = Infinity;
|
|
909
|
+
for (let b = 0; b <= 10; b++) {
|
|
910
|
+
const l = literals[b];
|
|
911
|
+
let n = 0;
|
|
912
|
+
for (let i = 0; i < 256; i++)
|
|
913
|
+
n += l[0][i];
|
|
914
|
+
let hitCount = 0;
|
|
915
|
+
for (let i = 0; i < hits[b].length; i++)
|
|
916
|
+
hitCount += hits[b][i];
|
|
917
|
+
const green = n + hitCount;
|
|
918
|
+
let bits = bitsOf(l[1], n) + bitsOf(l[2], n) + bitsOf(l[3], n);
|
|
919
|
+
for (let i = 0; i < 256; i++)
|
|
920
|
+
if (l[0][i])
|
|
921
|
+
bits += 5 - l[0][i] * Math.log2(l[0][i] / green);
|
|
922
|
+
if (b)
|
|
923
|
+
for (let i = 0; i < hits[b].length; i++)
|
|
924
|
+
if (hits[b][i])
|
|
925
|
+
bits += 5 - hits[b][i] * Math.log2(hits[b][i] / green);
|
|
926
|
+
if (bits < bestBits) {
|
|
927
|
+
bestBits = bits;
|
|
928
|
+
best = b;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
return best;
|
|
932
|
+
}
|
|
933
|
+
function entropyBits(counts) {
|
|
934
|
+
let bits = 0;
|
|
935
|
+
for (const c of counts) {
|
|
936
|
+
let total = 0;
|
|
937
|
+
for (let i = 0; i < c.length; i++)
|
|
938
|
+
total += c[i];
|
|
939
|
+
for (let i = 0; i < c.length; i++)
|
|
940
|
+
if (c[i])
|
|
941
|
+
bits += 5 - c[i] * Math.log2(c[i] / total);
|
|
942
|
+
}
|
|
943
|
+
return bits;
|
|
944
|
+
}
|
|
945
|
+
function tokenSymbols(stream, k, out) {
|
|
946
|
+
const v = stream.values[k], op = stream.ops[k];
|
|
947
|
+
out[1] = out[2] = out[3] = out[4] = -1;
|
|
948
|
+
if (op === 0) {
|
|
949
|
+
out[0] = v >> 8 & 255;
|
|
950
|
+
out[1] = v >> 16 & 255;
|
|
951
|
+
out[2] = v & 255;
|
|
952
|
+
out[3] = v >>> 24;
|
|
953
|
+
}
|
|
954
|
+
else if (op === 1) {
|
|
955
|
+
out[0] = NUM_LITERAL_CODES + prefixEncode(v).symbol;
|
|
956
|
+
out[4] = prefixEncode(stream.distances[k]).symbol;
|
|
957
|
+
}
|
|
958
|
+
else
|
|
959
|
+
out[0] = NUM_LITERAL_CODES + NUM_LENGTH_CODES + v;
|
|
960
|
+
}
|
|
961
|
+
function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
962
|
+
const bw = subSize(width, bits), bh = subSize(height, bits), nb = bw * bh;
|
|
963
|
+
const greenSize = NUM_LITERAL_CODES + NUM_LENGTH_CODES + (cacheBits ? 1 << cacheBits : 0);
|
|
964
|
+
const sizes = [greenSize, 256, 256, 256, NUM_DISTANCE_CODES], offsets = [0, greenSize, greenSize + 256, greenSize + 512, greenSize + 768];
|
|
965
|
+
const total = greenSize + 768 + NUM_DISTANCE_CODES;
|
|
966
|
+
const syms = new Int32Array(5), keys = [];
|
|
967
|
+
for (let k = 0, i = 0; k < stream.count; k++) {
|
|
968
|
+
const x = i % width, y = (i / width) | 0, blk = (y >> bits) * bw + (x >> bits);
|
|
969
|
+
tokenSymbols(stream, k, syms);
|
|
970
|
+
for (let c = 0; c < 5; c++)
|
|
971
|
+
if (syms[c] >= 0)
|
|
972
|
+
keys.push(blk * total + offsets[c] + syms[c]);
|
|
973
|
+
i += stream.ops[k] === 1 ? stream.values[k] : 1;
|
|
974
|
+
}
|
|
975
|
+
const sorted = Int32Array.from(keys).sort();
|
|
976
|
+
const starts = new Int32Array(nb + 1), sparse = [];
|
|
977
|
+
let blk = 0;
|
|
978
|
+
for (let e = 0; e < sorted.length;) {
|
|
979
|
+
const key = sorted[e], b = Math.floor(key / total);
|
|
980
|
+
while (blk < b)
|
|
981
|
+
starts[++blk] = sparse.length;
|
|
982
|
+
let n = 0;
|
|
983
|
+
while (e < sorted.length && sorted[e] === key) {
|
|
984
|
+
n++;
|
|
985
|
+
e++;
|
|
986
|
+
}
|
|
987
|
+
sparse.push(key - b * total, n);
|
|
988
|
+
}
|
|
989
|
+
while (blk < nb)
|
|
990
|
+
starts[++blk] = sparse.length;
|
|
991
|
+
const entries = Float64Array.from(sparse);
|
|
992
|
+
const logs = (cl) => {
|
|
993
|
+
const t = totals(cl), out = new Float64Array(total);
|
|
994
|
+
for (let c = 0; c < 5; c++) {
|
|
995
|
+
const o = offsets[c], d = t[c] + 0.5 * sizes[c];
|
|
996
|
+
for (let s = 0; s < sizes[c]; s++)
|
|
997
|
+
out[o + s] = -Math.log2((cl[o + s] + 0.5) / d);
|
|
998
|
+
}
|
|
999
|
+
return out;
|
|
1000
|
+
};
|
|
1001
|
+
const cost = (blk, log) => {
|
|
1002
|
+
let bitsSum = 0;
|
|
1003
|
+
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1004
|
+
bitsSum += entries[e + 1] * log[entries[e]];
|
|
1005
|
+
return bitsSum;
|
|
1006
|
+
};
|
|
1007
|
+
const totals = (cl) => Float64Array.from(offsets, (o, c) => {
|
|
1008
|
+
let t = 0;
|
|
1009
|
+
for (let s = 0; s < sizes[c]; s++)
|
|
1010
|
+
t += cl[o + s];
|
|
1011
|
+
return t;
|
|
1012
|
+
});
|
|
1013
|
+
const clusterBits = (cl) => {
|
|
1014
|
+
let b = 0;
|
|
1015
|
+
const t = totals(cl);
|
|
1016
|
+
for (let c = 0; c < 5; c++) {
|
|
1017
|
+
for (let s = 0; s < sizes[c]; s++) {
|
|
1018
|
+
const n = cl[offsets[c] + s];
|
|
1019
|
+
if (n)
|
|
1020
|
+
b += 5 - n * Math.log2(n / t[c]);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
return b;
|
|
1024
|
+
};
|
|
1025
|
+
const used = Array.from({ length: nb }, (_, i) => i).filter(i => starts[i + 1] > starts[i]);
|
|
1026
|
+
const single = new Float64Array(total);
|
|
1027
|
+
for (const blk of used)
|
|
1028
|
+
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1029
|
+
single[entries[e]] += entries[e + 1];
|
|
1030
|
+
let bestBits = clusterBits(single), best = null;
|
|
1031
|
+
const assign = new Int32Array(nb);
|
|
1032
|
+
for (const k of [4, 8, 16]) {
|
|
1033
|
+
if (k > used.length)
|
|
1034
|
+
break;
|
|
1035
|
+
const blockHistogram = (blk) => {
|
|
1036
|
+
const h = new Float64Array(total);
|
|
1037
|
+
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1038
|
+
h[entries[e]] = entries[e + 1];
|
|
1039
|
+
return h;
|
|
1040
|
+
};
|
|
1041
|
+
const centers = [blockHistogram(used[0])];
|
|
1042
|
+
const dist = new Float64Array(nb).fill(Infinity);
|
|
1043
|
+
while (centers.length < k) {
|
|
1044
|
+
const log = logs(centers[centers.length - 1]);
|
|
1045
|
+
let far = -1;
|
|
1046
|
+
for (const blk of used) {
|
|
1047
|
+
dist[blk] = Math.min(dist[blk], cost(blk, log));
|
|
1048
|
+
if (far < 0 || dist[blk] > dist[far])
|
|
1049
|
+
far = blk;
|
|
1050
|
+
}
|
|
1051
|
+
centers.push(blockHistogram(far));
|
|
1052
|
+
}
|
|
1053
|
+
let clusters = centers;
|
|
1054
|
+
for (let iter = 0; iter < 3; iter++) {
|
|
1055
|
+
const ls = clusters.map(logs);
|
|
1056
|
+
for (const blk of used) {
|
|
1057
|
+
let bc = 0, bv = Infinity;
|
|
1058
|
+
for (let j = 0; j < ls.length; j++) {
|
|
1059
|
+
const v = cost(blk, ls[j]);
|
|
1060
|
+
if (v < bv) {
|
|
1061
|
+
bv = v;
|
|
1062
|
+
bc = j;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
assign[blk] = bc;
|
|
1066
|
+
}
|
|
1067
|
+
clusters = clusters.map(() => new Float64Array(total));
|
|
1068
|
+
for (const blk of used) {
|
|
1069
|
+
const cl = clusters[assign[blk]];
|
|
1070
|
+
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1071
|
+
cl[entries[e]] += entries[e + 1];
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
const remap = new Int32Array(k).fill(-1), kept = [];
|
|
1075
|
+
for (const blk of used)
|
|
1076
|
+
if (remap[assign[blk]] < 0) {
|
|
1077
|
+
remap[assign[blk]] = kept.length;
|
|
1078
|
+
kept.push(clusters[assign[blk]]);
|
|
1079
|
+
}
|
|
1080
|
+
const groups = new Int32Array(nb);
|
|
1081
|
+
for (const blk of used)
|
|
1082
|
+
groups[blk] = remap[assign[blk]];
|
|
1083
|
+
const b = kept.reduce((sum, cl) => sum + clusterBits(cl), 0) + nb;
|
|
1084
|
+
if (b < bestBits) {
|
|
1085
|
+
bestBits = b;
|
|
1086
|
+
best = [groups, kept];
|
|
1087
|
+
}
|
|
1088
|
+
else
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
if (!best)
|
|
1092
|
+
return null;
|
|
1093
|
+
const [groups, kept] = best;
|
|
1094
|
+
return [groups, kept.map(cl => sizes.map((size, c) => Int32Array.from(cl.subarray(offsets[c], offsets[c] + size)))), bestBits];
|
|
1095
|
+
}
|
|
1096
|
+
function writeImage(w, pixels, width, isMain) {
|
|
1097
|
+
const tokens = tokenize(pixels, width);
|
|
1098
|
+
const cacheBits = isMain ? bestCacheBits(tokens, pixels) : 0;
|
|
1099
|
+
const stream = withCache(tokens, pixels, cacheBits), counts = histograms(stream, cacheBits);
|
|
1100
|
+
const best = entropyBits(counts);
|
|
1101
|
+
const height = pixels.length / width;
|
|
1102
|
+
let groups = null, groupCounts = [counts], prefixBits = 0;
|
|
1103
|
+
if (isMain && pixels.length > 4096) {
|
|
1104
|
+
const r = prefixGroups(stream, width, height, cacheBits, 4);
|
|
1105
|
+
if (r && r[2] < best) {
|
|
1106
|
+
[groups, groupCounts] = r;
|
|
1107
|
+
prefixBits = 4;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
if (cacheBits) {
|
|
1111
|
+
w.write(1, 1);
|
|
1112
|
+
w.write(cacheBits, 4);
|
|
1113
|
+
}
|
|
1114
|
+
else
|
|
1115
|
+
w.write(0, 1);
|
|
1116
|
+
if (isMain) {
|
|
1117
|
+
w.write(groups ? 1 : 0, 1);
|
|
1118
|
+
if (groups) {
|
|
1119
|
+
w.write(prefixBits - 2, 3);
|
|
1120
|
+
writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
const { ops, values, distances, count } = stream;
|
|
1124
|
+
const codes = groupCounts.map(g => g.map(c => writeCode(w, c)));
|
|
1125
|
+
const bw = subSize(width, prefixBits);
|
|
1126
|
+
let group = codes[0];
|
|
1127
|
+
for (let k = 0, i = 0; k < count; k++) {
|
|
1128
|
+
if (groups) {
|
|
1129
|
+
const x = i % width, y = (i / width) | 0;
|
|
1130
|
+
group = codes[groups[(y >> prefixBits) * bw + (x >> prefixBits)]];
|
|
1131
|
+
}
|
|
853
1132
|
const v = values[k];
|
|
854
1133
|
if (ops[k] === 0) {
|
|
855
|
-
writeSymbol(w,
|
|
856
|
-
writeSymbol(w,
|
|
857
|
-
writeSymbol(w,
|
|
858
|
-
writeSymbol(w,
|
|
1134
|
+
writeSymbol(w, group[0], v >> 8 & 255);
|
|
1135
|
+
writeSymbol(w, group[1], v >> 16 & 255);
|
|
1136
|
+
writeSymbol(w, group[2], v & 255);
|
|
1137
|
+
writeSymbol(w, group[3], v >>> 24);
|
|
1138
|
+
i++;
|
|
859
1139
|
}
|
|
860
1140
|
else if (ops[k] === 1) {
|
|
861
1141
|
const length = prefixEncode(v), distance = prefixEncode(distances[k]);
|
|
862
|
-
writeSymbol(w,
|
|
1142
|
+
writeSymbol(w, group[0], NUM_LITERAL_CODES + length.symbol);
|
|
863
1143
|
w.write(length.extra, length.bits);
|
|
864
|
-
writeSymbol(w,
|
|
1144
|
+
writeSymbol(w, group[4], distance.symbol);
|
|
865
1145
|
w.write(distance.extra, distance.bits);
|
|
1146
|
+
i += v;
|
|
1147
|
+
}
|
|
1148
|
+
else {
|
|
1149
|
+
writeSymbol(w, group[0], NUM_LITERAL_CODES + NUM_LENGTH_CODES + v);
|
|
1150
|
+
i++;
|
|
866
1151
|
}
|
|
867
|
-
else
|
|
868
|
-
writeSymbol(w, codes[0], NUM_LITERAL_CODES + NUM_LENGTH_CODES + v);
|
|
869
1152
|
}
|
|
870
1153
|
}
|
|
871
1154
|
const subPixels = (a, b) => ((a | 0x00ff00ff) - (b & 0xff00ff00) & 0xff00ff00 | (a | 0xff00ff00) - (b & 0x00ff00ff) & 0x00ff00ff) >>> 0;
|
|
@@ -916,6 +1199,19 @@ function applyPredictor(p, width, height, bits) {
|
|
|
916
1199
|
export function encode(argb, width, height) {
|
|
917
1200
|
if (width < 1 || height < 1 || width > 16384 || height > 16384)
|
|
918
1201
|
throw new RangeError('VP8L: dimensions must be 1-16384');
|
|
1202
|
+
const colors = new Map();
|
|
1203
|
+
for (let i = 0; i < argb.length && colors.size <= 256; i++)
|
|
1204
|
+
if (!colors.has(argb[i]))
|
|
1205
|
+
colors.set(argb[i], colors.size);
|
|
1206
|
+
if (colors.size > 256)
|
|
1207
|
+
return encodeWith(argb, width, height, null);
|
|
1208
|
+
const indexed = encodeWith(argb, width, height, colors);
|
|
1209
|
+
if (colors.size <= 16)
|
|
1210
|
+
return indexed;
|
|
1211
|
+
const predicted = encodeWith(argb, width, height, null);
|
|
1212
|
+
return predicted.length < indexed.length ? predicted : indexed;
|
|
1213
|
+
}
|
|
1214
|
+
function encodeWith(argb, width, height, colors) {
|
|
919
1215
|
const w = new BitWriter();
|
|
920
1216
|
let hasAlpha = false;
|
|
921
1217
|
for (let i = 0; i < argb.length; i++)
|
|
@@ -928,12 +1224,8 @@ export function encode(argb, width, height) {
|
|
|
928
1224
|
w.write(height - 1, 14);
|
|
929
1225
|
w.write(hasAlpha ? 1 : 0, 1);
|
|
930
1226
|
w.write(0, 3);
|
|
931
|
-
const colors = new Map();
|
|
932
|
-
for (let i = 0; i < argb.length && colors.size <= 256; i++)
|
|
933
|
-
if (!colors.has(argb[i]))
|
|
934
|
-
colors.set(argb[i], colors.size);
|
|
935
1227
|
let pixels, codedWidth = width;
|
|
936
|
-
if (colors
|
|
1228
|
+
if (colors) {
|
|
937
1229
|
const palette = Uint32Array.from([...colors.keys()].sort((a, b) => a - b));
|
|
938
1230
|
palette.forEach((c, i) => colors.set(c, i));
|
|
939
1231
|
const bits = palette.length <= 2 ? 3 : palette.length <= 4 ? 2 : palette.length <= 16 ? 1 : 0;
|
|
@@ -955,11 +1247,17 @@ export function encode(argb, width, height) {
|
|
|
955
1247
|
}
|
|
956
1248
|
else {
|
|
957
1249
|
pixels = argb.slice();
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1250
|
+
let redBlue = false;
|
|
1251
|
+
for (let i = 0; i < pixels.length && !redBlue; i++)
|
|
1252
|
+
if (pixels[i] & 0x00ff00ff)
|
|
1253
|
+
redBlue = true;
|
|
1254
|
+
if (redBlue) {
|
|
1255
|
+
w.write(1, 1);
|
|
1256
|
+
w.write(2, 2);
|
|
1257
|
+
for (let i = 0; i < pixels.length; i++) {
|
|
1258
|
+
const p = pixels[i], green = p >> 8 & 255;
|
|
1259
|
+
pixels[i] = (p & 0xff00ff00 | (p | 0x0000ff00) - (green << 16 | green) & 0x00ff00ff) >>> 0;
|
|
1260
|
+
}
|
|
963
1261
|
}
|
|
964
1262
|
const bits = 4;
|
|
965
1263
|
w.write(1, 1);
|
package/dist/image.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ export interface WebPEncodeOptions {
|
|
|
82
82
|
/** Metadata to include, see {@link Metadata} */
|
|
83
83
|
metadata?: Metadata;
|
|
84
84
|
}
|
|
85
|
-
/** Options of {@link Image.encodeTGA}
|
|
85
|
+
/** Options of {@link Image.encodeTGA} */
|
|
86
86
|
export interface EncodeOptions {
|
|
87
87
|
/** Metadata to include, see {@link Metadata} */
|
|
88
88
|
metadata?: Metadata;
|
|
@@ -132,6 +132,21 @@ export interface DecodeOptions {
|
|
|
132
132
|
*/
|
|
133
133
|
toneMapping?: ToneMapping;
|
|
134
134
|
}
|
|
135
|
+
/** Options of {@link Image.encodeJXL} */
|
|
136
|
+
export interface JXLEncodeOptions {
|
|
137
|
+
/**
|
|
138
|
+
* Lossy compression (0-100, like cjxl's `-q`: 90 is visually lossless, 100 lossless): higher is better and larger.
|
|
139
|
+
* Without it the image is encoded losslessly
|
|
140
|
+
*/
|
|
141
|
+
quality?: number;
|
|
142
|
+
/**
|
|
143
|
+
* How hard lossy compression searches for the best transforms: 0-2, faster to smaller. 0 uses 8x8 DCTs only (like
|
|
144
|
+
* `cjxl -e 3`), 1 and 2 search their sizes like `cjxl -e 5` (2 tries more of the small ones). Default: `1`
|
|
145
|
+
*/
|
|
146
|
+
effort?: number;
|
|
147
|
+
/** Metadata to include, see {@link Metadata} */
|
|
148
|
+
metadata?: Metadata;
|
|
149
|
+
}
|
|
135
150
|
/** Options of {@link Image.encodeAVIF} */
|
|
136
151
|
export interface AVIFEncodeOptions {
|
|
137
152
|
/** 0-100, like avifenc's: higher is better and larger, 100 is lossless. Default: `60` */
|
|
@@ -773,13 +788,14 @@ export declare class Image {
|
|
|
773
788
|
*/
|
|
774
789
|
encodeAVIF({ quality, alphaQuality, subsampling, effort, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
775
790
|
/**
|
|
776
|
-
* Encodes the image as
|
|
777
|
-
* or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511
|
|
778
|
-
* black, else YCoCg
|
|
779
|
-
*
|
|
780
|
-
*
|
|
791
|
+
* Encodes the image as JPEG XL: losslessly (the default), the codestream byte for byte like libjxl's `cjxl -e 1` on
|
|
792
|
+
* CPUs with AVX2 or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511
|
|
793
|
+
* colors besides black, else YCoCg; greyscale if the image is), or lossy with a quality (like `cjxl -q`: XYB,
|
|
794
|
+
* varblocks of libjxl's search, chroma from luma, the edge preserving filter). Alpha (if the image has
|
|
795
|
+
* transparency) is lossless either way. The codestream is in the container, with the metadata as Exif
|
|
796
|
+
* @param options See {@link JXLEncodeOptions}
|
|
781
797
|
*/
|
|
782
|
-
encodeJXL({ metadata }?:
|
|
798
|
+
encodeJXL({ quality, effort, metadata }?: JXLEncodeOptions): Promise<Uint8Array>;
|
|
783
799
|
/**
|
|
784
800
|
* Encodes the image as the smallest fitting Netpbm format: PBM for black and white images, PGM for greyscale ones,
|
|
785
801
|
* PPM for others, PAM if the image has transparency
|
package/dist/image.js
CHANGED
|
@@ -1029,8 +1029,17 @@ export class Image {
|
|
|
1029
1029
|
const exif = exifData(resolveMetadata(metadata));
|
|
1030
1030
|
return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif });
|
|
1031
1031
|
}
|
|
1032
|
-
async encodeJXL({ metadata = {} } = {}) {
|
|
1033
|
-
|
|
1032
|
+
async encodeJXL({ quality, effort = 1, metadata = {} } = {}) {
|
|
1033
|
+
if (!Number.isInteger(effort) || effort < 0 || effort > 2)
|
|
1034
|
+
throw new RangeError(`effort must be an integer 0-2, not ${effort}`);
|
|
1035
|
+
let distance = 0;
|
|
1036
|
+
if (quality !== undefined) {
|
|
1037
|
+
const q = finite(quality, 'quality');
|
|
1038
|
+
if (q < 0 || q > 100)
|
|
1039
|
+
throw new RangeError(`quality must be 0-100, not ${q}`);
|
|
1040
|
+
distance = q >= 100 ? 0 : q >= 30 ? 0.1 + (100 - q) * 0.09 : 53 / 3000 * q * q - 23 / 20 * q + 25;
|
|
1041
|
+
}
|
|
1042
|
+
return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)), distance, effort);
|
|
1034
1043
|
}
|
|
1035
1044
|
async encodePNM() {
|
|
1036
1045
|
return netpbm.encode(this.__bytes__(), this.width, this.height);
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Image, DecodeOptions } from './image.js';
|
|
|
3
3
|
import { GIF } from './gif.js';
|
|
4
4
|
import { Animation, AnimationDecodeOptions } from './animation.js';
|
|
5
5
|
export { Image } from './image.js';
|
|
6
|
-
export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
6
|
+
export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
7
7
|
export { Frame } from './frame.js';
|
|
8
8
|
export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
|
|
9
9
|
export { GIF } from './gif.js';
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '3.
|
|
1
|
+
export const VERSION = '3.7.0';
|