@woosh/meep-engine 2.126.75 → 2.127.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.
- package/build/bundle-worker-image-decoder.js +1 -1
- package/package.json +1 -1
- package/src/core/binary/platform_compute_endianness.d.ts.map +1 -1
- package/src/core/binary/platform_compute_endianness.js +5 -1
- package/src/core/color/hunt/xyz_to_hpe.d.ts +7 -0
- package/src/core/color/hunt/xyz_to_hpe.d.ts.map +1 -0
- package/src/core/color/hunt/xyz_to_hpe.js +15 -0
- package/src/core/color/xyz/rgb_to_xyz.d.ts +2 -0
- package/src/core/color/xyz/rgb_to_xyz.d.ts.map +1 -1
- package/src/core/color/xyz/rgb_to_xyz.js +2 -0
- package/src/core/color/xyz/xyz_to_rgb.d.ts +2 -0
- package/src/core/color/xyz/xyz_to_rgb.d.ts.map +1 -1
- package/src/core/color/xyz/xyz_to_rgb.js +2 -0
- package/src/core/math/idct_1d.d.ts +14 -0
- package/src/core/math/idct_1d.d.ts.map +1 -0
- package/src/core/math/idct_1d.js +69 -0
- package/src/core/math/idct_2d.d.ts +11 -0
- package/src/core/math/idct_2d.d.ts.map +1 -0
- package/src/core/math/idct_2d.js +22 -0
- package/src/core/model/DebouncedObservedBoolean.d.ts +1 -1
- package/src/core/model/DebouncedObservedBoolean.d.ts.map +1 -1
- package/src/core/model/object/objectKeyByValue.d.ts.map +1 -1
- package/src/core/model/object/objectKeyByValue.js +4 -0
- package/src/engine/asset/Asset.d.ts.map +1 -1
- package/src/engine/asset/Asset.js +2 -5
- package/src/engine/asset/AssetTransformer.js +1 -1
- package/src/engine/asset/loaders/image/codec/NativeImageDecoder.d.ts +20 -1
- package/src/engine/asset/loaders/image/codec/NativeImageDecoder.d.ts.map +1 -1
- package/src/engine/asset/loaders/image/codec/NativeImageDecoder.js +56 -13
- package/src/engine/asset/loaders/image/jpeg/JpegFrame.d.ts +42 -0
- package/src/engine/asset/loaders/image/jpeg/JpegFrame.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/JpegFrame.js +45 -0
- package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.d.ts +42 -0
- package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.js +48 -0
- package/src/engine/asset/loaders/image/jpeg/JpegImage.d.ts +60 -0
- package/src/engine/asset/loaders/image/jpeg/JpegImage.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/JpegImage.js +624 -0
- package/src/engine/asset/loaders/image/jpeg/NOTES.md +3 -0
- package/src/engine/asset/loaders/image/jpeg/buildComponentData.d.ts +7 -0
- package/src/engine/asset/loaders/image/jpeg/buildComponentData.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/buildComponentData.js +59 -0
- package/src/engine/asset/loaders/image/jpeg/buildHuffmanTable.d.ts +8 -0
- package/src/engine/asset/loaders/image/jpeg/buildHuffmanTable.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/buildHuffmanTable.js +46 -0
- package/src/engine/asset/loaders/image/jpeg/decodeScan.d.ts +17 -0
- package/src/engine/asset/loaders/image/jpeg/decodeScan.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/decodeScan.js +351 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.d.ts +23 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.js +214 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_float.d.ts +9 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_float.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/idct8x8_float.js +32 -0
- package/src/engine/asset/loaders/image/jpeg/jpeg_decode.d.ts +13 -0
- package/src/engine/asset/loaders/image/jpeg/jpeg_decode.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/jpeg/jpeg_decode.js +41 -0
- package/src/engine/asset/loaders/image/png/PNG.d.ts +18 -11
- package/src/engine/asset/loaders/image/png/PNG.d.ts.map +1 -1
- package/src/engine/asset/loaders/image/png/PNG.js +17 -22
- package/src/engine/asset/loaders/image/png/PNGReader.d.ts +71 -10
- package/src/engine/asset/loaders/image/png/PNGReader.d.ts.map +1 -1
- package/src/engine/asset/loaders/image/png/PNGReader.js +422 -101
- package/src/engine/ecs/EntityComponentDataset.d.ts.map +1 -1
- package/src/engine/ecs/EntityComponentDataset.js +2 -1
- package/src/engine/graphics/geometry/AttributeSpec.d.ts.map +1 -1
- package/src/engine/graphics/geometry/AttributeSpec.js +2 -1
- package/src/engine/graphics/render/frame_graph/RenderGraph.d.ts +50 -7
- package/src/engine/graphics/render/frame_graph/RenderGraph.d.ts.map +1 -1
- package/src/engine/graphics/render/frame_graph/RenderGraph.js +108 -8
- package/src/engine/graphics/render/frame_graph/RenderPassBuilder.d.ts +44 -0
- package/src/engine/graphics/render/frame_graph/RenderPassBuilder.d.ts.map +1 -0
- package/src/engine/graphics/render/frame_graph/{RenderGraphBuilder.js → RenderPassBuilder.js} +25 -6
- package/src/engine/graphics/render/frame_graph/RenderPassNode.d.ts +1 -0
- package/src/engine/graphics/render/frame_graph/RenderPassNode.d.ts.map +1 -1
- package/src/engine/graphics/render/frame_graph/RenderPassNode.js +1 -0
- package/src/engine/graphics/render/frame_graph/ResourceEntry.d.ts +8 -0
- package/src/engine/graphics/render/frame_graph/ResourceEntry.d.ts.map +1 -1
- package/src/engine/graphics/render/frame_graph/ResourceEntry.js +8 -0
- package/src/engine/graphics/texture/sampler/downloadSamplerAsPNG.d.ts.map +1 -1
- package/src/engine/graphics/texture/sampler/downloadSamplerAsPNG.js +6 -1
- package/src/engine/graphics/texture/sampler/sampler2d_compute_texel_value_conversion_scale_to_uint8.d.ts.map +1 -1
- package/src/engine/graphics/texture/sampler/sampler2d_compute_texel_value_conversion_scale_to_uint8.js +7 -2
- package/src/engine/graphics/texture/sampler/sampler2d_to_html_canvas.d.ts.map +1 -1
- package/src/engine/graphics/texture/sampler/sampler2d_to_html_canvas.js +12 -1
- package/src/engine/logging/elastic/ElasticSearchLogger.d.ts +1 -1
- package/src/engine/logging/elastic/ElasticSearchLogger.d.ts.map +1 -1
- package/src/engine/graphics/render/frame_graph/RenderGraphBuilder.d.ts +0 -33
- package/src/engine/graphics/render/frame_graph/RenderGraphBuilder.d.ts.map +0 -1
- package/src/engine/graphics/render/frame_graph/RenderPass.d.ts +0 -50
- package/src/engine/graphics/render/frame_graph/RenderPass.d.ts.map +0 -1
- package/src/engine/graphics/render/frame_graph/RenderPass.js +0 -74
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import { buildComponentData } from "./buildComponentData.js";
|
|
2
|
+
import { buildHuffmanTable } from "./buildHuffmanTable.js";
|
|
3
|
+
import { dctZigZag, decodeScan } from "./decodeScan.js";
|
|
4
|
+
import { JpegFrame } from "./JpegFrame.js";
|
|
5
|
+
import { JpegFrameComponent } from "./JpegFrameComponent.js";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/* -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
9
|
+
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
10
|
+
/*
|
|
11
|
+
Copyright 2025 Alex Goldring/ Company Named Ltd
|
|
12
|
+
|
|
13
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
you may not use this file except in compliance with the License.
|
|
15
|
+
You may obtain a copy of the License at
|
|
16
|
+
|
|
17
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
|
|
19
|
+
Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
See the License for the specific language governing permissions and
|
|
23
|
+
limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
// - The JPEG specification can be found in the ITU CCITT Recommendation T.81
|
|
27
|
+
// (www.w3.org/Graphics/JPEG/itu-t81.pdf)
|
|
28
|
+
// - The JFIF specification can be found in the JPEG File Interchange Format
|
|
29
|
+
// (www.w3.org/Graphics/JPEG/jfif3.pdf)
|
|
30
|
+
// - The Adobe Application-Specific JPEG markers in the Supporting the DCT Filters
|
|
31
|
+
// in PostScript Level 2, Technical Note #5116
|
|
32
|
+
// (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param {number} a
|
|
37
|
+
* @return {number}
|
|
38
|
+
*/
|
|
39
|
+
function clampTo8bit(a) {
|
|
40
|
+
return a < 0 ? 0 : a > 255 ? 255 : a;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class JpegImage {
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param {Uint8Array} data
|
|
48
|
+
*/
|
|
49
|
+
parse(data) {
|
|
50
|
+
let offset = 0;
|
|
51
|
+
|
|
52
|
+
function readUint16() {
|
|
53
|
+
const value = (data[offset] << 8) | data[offset + 1];
|
|
54
|
+
offset += 2;
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readDataBlock() {
|
|
59
|
+
const length = readUint16();
|
|
60
|
+
const array = data.subarray(offset, offset + length - 2);
|
|
61
|
+
offset += array.length;
|
|
62
|
+
return array;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param {JpegFrame} frame
|
|
68
|
+
*/
|
|
69
|
+
function prepareComponents(frame) {
|
|
70
|
+
// According to the JPEG standard, the sampling factor must be between 1 and 4
|
|
71
|
+
// See https://github.com/libjpeg-turbo/libjpeg-turbo/blob/9abeff46d87bd201a952e276f3e4339556a403a3/libjpeg.txt#L1138-L1146
|
|
72
|
+
let maxH = 1, maxV = 1;
|
|
73
|
+
let component, componentId;
|
|
74
|
+
for (componentId in frame.components) {
|
|
75
|
+
if (frame.components.hasOwnProperty(componentId)) {
|
|
76
|
+
component = frame.components[componentId];
|
|
77
|
+
if (maxH < component.h) maxH = component.h;
|
|
78
|
+
if (maxV < component.v) maxV = component.v;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / maxH);
|
|
82
|
+
const mcusPerColumn = Math.ceil(frame.scanLines / 8 / maxV);
|
|
83
|
+
for (componentId in frame.components) {
|
|
84
|
+
if (frame.components.hasOwnProperty(componentId)) {
|
|
85
|
+
component = frame.components[componentId];
|
|
86
|
+
const blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / maxH);
|
|
87
|
+
const blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / maxV);
|
|
88
|
+
const blocksPerLineForMcu = mcusPerLine * component.h;
|
|
89
|
+
const blocksPerColumnForMcu = mcusPerColumn * component.v;
|
|
90
|
+
const blocksToAllocate = blocksPerColumnForMcu * blocksPerLineForMcu;
|
|
91
|
+
const blocks = [];
|
|
92
|
+
|
|
93
|
+
// Each block is a Int32Array of length 64 (4 x 64 = 256 bytes)
|
|
94
|
+
|
|
95
|
+
for (let i = 0; i < blocksPerColumnForMcu; i++) {
|
|
96
|
+
const row = [];
|
|
97
|
+
for (let j = 0; j < blocksPerLineForMcu; j++)
|
|
98
|
+
row.push(new Int32Array(64));
|
|
99
|
+
blocks.push(row);
|
|
100
|
+
}
|
|
101
|
+
component.blocksPerLine = blocksPerLine;
|
|
102
|
+
component.blocksPerColumn = blocksPerColumn;
|
|
103
|
+
component.blocks = blocks;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
frame.maxH = maxH;
|
|
107
|
+
frame.maxV = maxV;
|
|
108
|
+
frame.mcusPerLine = mcusPerLine;
|
|
109
|
+
frame.mcusPerColumn = mcusPerColumn;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let jfif = null;
|
|
113
|
+
let adobe = null;
|
|
114
|
+
/**
|
|
115
|
+
* @type {JpegFrame}
|
|
116
|
+
*/
|
|
117
|
+
let frame;
|
|
118
|
+
let resetInterval;
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @type {Int32Array[]}
|
|
122
|
+
*/
|
|
123
|
+
const quantizationTables = []
|
|
124
|
+
/**
|
|
125
|
+
*
|
|
126
|
+
* @type {JpegFrame[]}
|
|
127
|
+
*/
|
|
128
|
+
const frames = [];
|
|
129
|
+
const huffmanTablesAC = [], huffmanTablesDC = [];
|
|
130
|
+
let fileMarker = readUint16();
|
|
131
|
+
let malformedDataOffset = -1;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @type {string[]}
|
|
136
|
+
*/
|
|
137
|
+
this.comments = [];
|
|
138
|
+
|
|
139
|
+
if (fileMarker !== 0xFFD8) { // SOI (Start of Image)
|
|
140
|
+
throw new Error("SOI not found");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fileMarker = readUint16();
|
|
144
|
+
while (fileMarker !== 0xFFD9) { // EOI (End of image)
|
|
145
|
+
let i, j;
|
|
146
|
+
switch (fileMarker) {
|
|
147
|
+
case 0xFF00:
|
|
148
|
+
break;
|
|
149
|
+
case 0xFFE0: // APP0 (Application Specific)
|
|
150
|
+
case 0xFFE1: // APP1
|
|
151
|
+
case 0xFFE2: // APP2
|
|
152
|
+
case 0xFFE3: // APP3
|
|
153
|
+
case 0xFFE4: // APP4
|
|
154
|
+
case 0xFFE5: // APP5
|
|
155
|
+
case 0xFFE6: // APP6
|
|
156
|
+
case 0xFFE7: // APP7
|
|
157
|
+
case 0xFFE8: // APP8
|
|
158
|
+
case 0xFFE9: // APP9
|
|
159
|
+
case 0xFFEA: // APP10
|
|
160
|
+
case 0xFFEB: // APP11
|
|
161
|
+
case 0xFFEC: // APP12
|
|
162
|
+
case 0xFFEC: // APP12
|
|
163
|
+
case 0xFFED: // APP13
|
|
164
|
+
case 0xFFEE: // APP14
|
|
165
|
+
case 0xFFEF: // APP15
|
|
166
|
+
case 0xFFFE: // COM (Comment)
|
|
167
|
+
const appData = readDataBlock();
|
|
168
|
+
|
|
169
|
+
if (fileMarker === 0xFFFE) {
|
|
170
|
+
const comment = String.fromCharCode.apply(null, appData);
|
|
171
|
+
this.comments.push(comment);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (fileMarker === 0xFFE0) {
|
|
175
|
+
if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
|
|
176
|
+
appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
|
|
177
|
+
jfif = {
|
|
178
|
+
version: { major: appData[5], minor: appData[6] },
|
|
179
|
+
densityUnits: appData[7],
|
|
180
|
+
xDensity: (appData[8] << 8) | appData[9],
|
|
181
|
+
yDensity: (appData[10] << 8) | appData[11],
|
|
182
|
+
thumbWidth: appData[12],
|
|
183
|
+
thumbHeight: appData[13],
|
|
184
|
+
thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// TODO APP1 - Exif
|
|
189
|
+
if (fileMarker === 0xFFE1) {
|
|
190
|
+
if (appData[0] === 0x45 &&
|
|
191
|
+
appData[1] === 0x78 &&
|
|
192
|
+
appData[2] === 0x69 &&
|
|
193
|
+
appData[3] === 0x66 &&
|
|
194
|
+
appData[4] === 0) { // 'EXIF\x00'
|
|
195
|
+
this.exifBuffer = appData.subarray(5, appData.length);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (fileMarker === 0xFFEE) {
|
|
200
|
+
if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
|
|
201
|
+
appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
|
|
202
|
+
adobe = {
|
|
203
|
+
version: appData[6],
|
|
204
|
+
flags0: (appData[7] << 8) | appData[8],
|
|
205
|
+
flags1: (appData[9] << 8) | appData[10],
|
|
206
|
+
transformCode: appData[11]
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
|
|
212
|
+
case 0xFFDB: // DQT (Define Quantization Tables)
|
|
213
|
+
const quantizationTablesLength = readUint16();
|
|
214
|
+
const quantizationTablesEnd = quantizationTablesLength + offset - 2;
|
|
215
|
+
while (offset < quantizationTablesEnd) {
|
|
216
|
+
|
|
217
|
+
const quantizationTableSpec = data[offset++];
|
|
218
|
+
const tableData = new Int32Array(64);
|
|
219
|
+
|
|
220
|
+
if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
|
|
221
|
+
|
|
222
|
+
for (j = 0; j < 64; j++) {
|
|
223
|
+
const z = dctZigZag[j];
|
|
224
|
+
tableData[z] = data[offset++];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
} else if ((quantizationTableSpec >> 4) === 1) { //16 bit
|
|
228
|
+
|
|
229
|
+
for (j = 0; j < 64; j++) {
|
|
230
|
+
const z = dctZigZag[j];
|
|
231
|
+
tableData[z] = readUint16();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
} else {
|
|
235
|
+
throw new Error("DQT: invalid table spec");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
quantizationTables[quantizationTableSpec & 15] = tableData;
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
|
|
242
|
+
case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
|
|
243
|
+
case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
|
|
244
|
+
case 0xFFC2: // SOF2 (Start of Frame, Progressive DCT)
|
|
245
|
+
readUint16(); // skip data length
|
|
246
|
+
frame = new JpegFrame();
|
|
247
|
+
frame.extended = (fileMarker === 0xFFC1);
|
|
248
|
+
frame.progressive = (fileMarker === 0xFFC2);
|
|
249
|
+
frame.precision = data[offset++];
|
|
250
|
+
frame.scanLines = readUint16();
|
|
251
|
+
frame.samplesPerLine = readUint16();
|
|
252
|
+
frame.components = {};
|
|
253
|
+
frame.componentsOrder = [];
|
|
254
|
+
|
|
255
|
+
let componentsCount = data[offset++], componentId;
|
|
256
|
+
for (i = 0; i < componentsCount; i++) {
|
|
257
|
+
componentId = data[offset];
|
|
258
|
+
const h = data[offset + 1] >> 4;
|
|
259
|
+
const v = data[offset + 1] & 15;
|
|
260
|
+
const qId = data[offset + 2];
|
|
261
|
+
|
|
262
|
+
if (h <= 0 || v <= 0) {
|
|
263
|
+
throw new Error('Invalid sampling factor, expected values above 0');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
frame.componentsOrder.push(componentId);
|
|
267
|
+
|
|
268
|
+
const component = new JpegFrameComponent();
|
|
269
|
+
component.h = h;
|
|
270
|
+
component.v = v;
|
|
271
|
+
component.quantizationIdx = qId;
|
|
272
|
+
|
|
273
|
+
frame.components[componentId] = component;
|
|
274
|
+
offset += 3;
|
|
275
|
+
}
|
|
276
|
+
prepareComponents(frame);
|
|
277
|
+
frames.push(frame);
|
|
278
|
+
break;
|
|
279
|
+
|
|
280
|
+
case 0xFFC4: // DHT (Define Huffman Tables)
|
|
281
|
+
const huffmanLength = readUint16();
|
|
282
|
+
const codeLengths = new Uint8Array(16);
|
|
283
|
+
for (i = 2; i < huffmanLength;) {
|
|
284
|
+
const huffmanTableSpec = data[offset++];
|
|
285
|
+
let codeLengthSum = 0;
|
|
286
|
+
|
|
287
|
+
for (j = 0; j < 16; j++, offset++) {
|
|
288
|
+
codeLengthSum += (codeLengths[j] = data[offset]);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const huffmanValues = new Uint8Array(codeLengthSum);
|
|
292
|
+
|
|
293
|
+
for (j = 0; j < codeLengthSum; j++, offset++) {
|
|
294
|
+
huffmanValues[j] = data[offset];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
i += 17 + codeLengthSum;
|
|
298
|
+
|
|
299
|
+
const huffman_table = buildHuffmanTable(codeLengths, huffmanValues);
|
|
300
|
+
|
|
301
|
+
if ((huffmanTableSpec >> 4) === 0) {
|
|
302
|
+
huffmanTablesDC[huffmanTableSpec & 15] = huffman_table;
|
|
303
|
+
} else {
|
|
304
|
+
huffmanTablesAC[huffmanTableSpec & 15] = huffman_table;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
break;
|
|
308
|
+
|
|
309
|
+
case 0xFFDD: // DRI (Define Restart Interval)
|
|
310
|
+
readUint16(); // skip data length
|
|
311
|
+
resetInterval = readUint16();
|
|
312
|
+
break;
|
|
313
|
+
|
|
314
|
+
case 0xFFDC: // Number of Lines marker
|
|
315
|
+
readUint16() // skip data length
|
|
316
|
+
readUint16() // Ignore this data since it represents the image height
|
|
317
|
+
break;
|
|
318
|
+
|
|
319
|
+
case 0xFFDA: // SOS (Start of Scan)
|
|
320
|
+
const scanLength = readUint16();
|
|
321
|
+
const selectorsCount = data[offset++];
|
|
322
|
+
/**
|
|
323
|
+
*
|
|
324
|
+
* @type {JpegFrameComponent[]}
|
|
325
|
+
*/
|
|
326
|
+
const components = [];
|
|
327
|
+
let component;
|
|
328
|
+
for (i = 0; i < selectorsCount; i++) {
|
|
329
|
+
component = frame.components[data[offset++]];
|
|
330
|
+
const tableSpec = data[offset++];
|
|
331
|
+
component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
|
|
332
|
+
component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
|
|
333
|
+
components.push(component);
|
|
334
|
+
}
|
|
335
|
+
const spectralStart = data[offset++];
|
|
336
|
+
const spectralEnd = data[offset++];
|
|
337
|
+
const successiveApproximation = data[offset++];
|
|
338
|
+
const processed = decodeScan(
|
|
339
|
+
data,
|
|
340
|
+
offset,
|
|
341
|
+
frame,
|
|
342
|
+
components,
|
|
343
|
+
resetInterval,
|
|
344
|
+
spectralStart,
|
|
345
|
+
spectralEnd,
|
|
346
|
+
successiveApproximation >> 4,
|
|
347
|
+
successiveApproximation & 15,
|
|
348
|
+
this.opts.tolerantDecoding
|
|
349
|
+
);
|
|
350
|
+
offset += processed;
|
|
351
|
+
break;
|
|
352
|
+
|
|
353
|
+
case 0xFFFF: // Fill bytes
|
|
354
|
+
if (data[offset] !== 0xFF) { // Avoid skipping a valid marker.
|
|
355
|
+
offset--;
|
|
356
|
+
}
|
|
357
|
+
break;
|
|
358
|
+
default:
|
|
359
|
+
if (data[offset - 3] === 0xFF &&
|
|
360
|
+
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
|
|
361
|
+
// could be incorrect encoding -- last 0xFF byte of the previous
|
|
362
|
+
// block was eaten by the encoder
|
|
363
|
+
offset -= 3;
|
|
364
|
+
break;
|
|
365
|
+
} else if (fileMarker === 0xE0 || fileMarker === 0xE1) {
|
|
366
|
+
// Recover from malformed APP1 markers popular in some phone models.
|
|
367
|
+
// See https://github.com/eugeneware/jpeg-js/issues/82
|
|
368
|
+
if (malformedDataOffset !== -1) {
|
|
369
|
+
throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
|
|
370
|
+
}
|
|
371
|
+
malformedDataOffset = offset - 1;
|
|
372
|
+
const nextOffset = readUint16();
|
|
373
|
+
if (data[offset + nextOffset - 2] === 0xFF) {
|
|
374
|
+
offset += nextOffset - 2;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
throw new Error("unknown JPEG marker " + fileMarker.toString(16));
|
|
379
|
+
}
|
|
380
|
+
fileMarker = readUint16();
|
|
381
|
+
}
|
|
382
|
+
if (frames.length !== 1)
|
|
383
|
+
throw new Error("only single frame JPEGs supported");
|
|
384
|
+
|
|
385
|
+
// set each frame's components quantization table
|
|
386
|
+
for (let i = 0; i < frames.length; i++) {
|
|
387
|
+
const cp = frames[i].components;
|
|
388
|
+
for (let j in cp) {
|
|
389
|
+
cp[j].quantizationTable = quantizationTables[cp[j].quantizationIdx];
|
|
390
|
+
delete cp[j].quantizationIdx;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
this.width = frame.samplesPerLine;
|
|
395
|
+
this.height = frame.scanLines;
|
|
396
|
+
this.jfif = jfif;
|
|
397
|
+
this.adobe = adobe;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
*
|
|
401
|
+
* @type {{lines:Uint8Array[], scaleX:number, scaleY:number}[]}
|
|
402
|
+
*/
|
|
403
|
+
this.components = [];
|
|
404
|
+
|
|
405
|
+
for (let i = 0; i < frame.componentsOrder.length; i++) {
|
|
406
|
+
const component = frame.components[frame.componentsOrder[i]];
|
|
407
|
+
this.components.push({
|
|
408
|
+
lines: buildComponentData(component),
|
|
409
|
+
scaleX: component.h / frame.maxH,
|
|
410
|
+
scaleY: component.v / frame.maxV
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
*
|
|
417
|
+
* @param {number} width
|
|
418
|
+
* @param {number} height
|
|
419
|
+
* @return {Uint8Array}
|
|
420
|
+
*/
|
|
421
|
+
getData(width, height) {
|
|
422
|
+
const scaleX = this.width / width;
|
|
423
|
+
const scaleY = this.height / height;
|
|
424
|
+
|
|
425
|
+
let component1, component2, component3, component4;
|
|
426
|
+
let component1Line, component2Line, component3Line, component4Line;
|
|
427
|
+
let x, y;
|
|
428
|
+
let offset = 0;
|
|
429
|
+
let Y, Cb, Cr, K, C, M, Ye, R, G, B;
|
|
430
|
+
let colorTransform;
|
|
431
|
+
const dataLength = width * height * this.components.length;
|
|
432
|
+
|
|
433
|
+
const data = new Uint8Array(dataLength);
|
|
434
|
+
switch (this.components.length) {
|
|
435
|
+
case 1:
|
|
436
|
+
component1 = this.components[0];
|
|
437
|
+
const lines = component1.lines;
|
|
438
|
+
for (y = 0; y < height; y++) {
|
|
439
|
+
component1Line = lines[0 | (y * component1.scaleY * scaleY)];
|
|
440
|
+
for (x = 0; x < width; x++) {
|
|
441
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
442
|
+
|
|
443
|
+
data[offset++] = Y;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
break;
|
|
447
|
+
case 2:
|
|
448
|
+
// PDF might compress two component data in custom colorspace
|
|
449
|
+
component1 = this.components[0];
|
|
450
|
+
component2 = this.components[1];
|
|
451
|
+
for (y = 0; y < height; y++) {
|
|
452
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
453
|
+
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
|
454
|
+
for (x = 0; x < width; x++) {
|
|
455
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
456
|
+
data[offset++] = Y;
|
|
457
|
+
Y = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
458
|
+
data[offset++] = Y;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
case 3:
|
|
463
|
+
// The default transform for three components is true
|
|
464
|
+
colorTransform = true;
|
|
465
|
+
// The adobe transform marker overrides any previous setting
|
|
466
|
+
if (this.adobe && this.adobe.transformCode)
|
|
467
|
+
colorTransform = true;
|
|
468
|
+
else if (typeof this.opts.colorTransform !== 'undefined') {
|
|
469
|
+
colorTransform = !!this.opts.colorTransform;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
component1 = this.components[0];
|
|
473
|
+
component2 = this.components[1];
|
|
474
|
+
component3 = this.components[2];
|
|
475
|
+
for (y = 0; y < height; y++) {
|
|
476
|
+
|
|
477
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
478
|
+
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
|
479
|
+
component3Line = component3.lines[0 | (y * component3.scaleY * scaleY)];
|
|
480
|
+
for (x = 0; x < width; x++) {
|
|
481
|
+
|
|
482
|
+
if (!colorTransform) {
|
|
483
|
+
R = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
484
|
+
G = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
485
|
+
B = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
486
|
+
} else {
|
|
487
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
488
|
+
Cb = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
489
|
+
Cr = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
490
|
+
|
|
491
|
+
R = clampTo8bit(Y + 1.402 * (Cr - 128));
|
|
492
|
+
G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
|
493
|
+
B = clampTo8bit(Y + 1.772 * (Cb - 128));
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
data[offset++] = R;
|
|
497
|
+
data[offset++] = G;
|
|
498
|
+
data[offset++] = B;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
case 4:
|
|
503
|
+
if (!this.adobe)
|
|
504
|
+
throw new Error('Unsupported color mode (4 components)');
|
|
505
|
+
// The default transform for four components is false
|
|
506
|
+
colorTransform = false;
|
|
507
|
+
// The adobe transform marker overrides any previous setting
|
|
508
|
+
if (this.adobe && this.adobe.transformCode)
|
|
509
|
+
colorTransform = true;
|
|
510
|
+
else if (typeof this.opts.colorTransform !== 'undefined')
|
|
511
|
+
colorTransform = !!this.opts.colorTransform;
|
|
512
|
+
|
|
513
|
+
component1 = this.components[0];
|
|
514
|
+
component2 = this.components[1];
|
|
515
|
+
component3 = this.components[2];
|
|
516
|
+
component4 = this.components[3];
|
|
517
|
+
for (y = 0; y < height; y++) {
|
|
518
|
+
const _y = y * scaleY;
|
|
519
|
+
|
|
520
|
+
component1Line = component1.lines[0 | (_y * component1.scaleY)];
|
|
521
|
+
component2Line = component2.lines[0 | (_y * component2.scaleY)];
|
|
522
|
+
component3Line = component3.lines[0 | (_y * component3.scaleY)];
|
|
523
|
+
component4Line = component4.lines[0 | (_y * component4.scaleY)];
|
|
524
|
+
for (x = 0; x < width; x++) {
|
|
525
|
+
const _x = x * scaleX;
|
|
526
|
+
|
|
527
|
+
if (!colorTransform) {
|
|
528
|
+
C = component1Line[0 | (_x * component1.scaleX)];
|
|
529
|
+
M = component2Line[0 | (_x * component2.scaleX)];
|
|
530
|
+
Ye = component3Line[0 | (_x * component3.scaleX)];
|
|
531
|
+
K = component4Line[0 | (_x * component4.scaleX)];
|
|
532
|
+
} else {
|
|
533
|
+
Y = component1Line[0 | (_x * component1.scaleX)];
|
|
534
|
+
Cb = component2Line[0 | (_x * component2.scaleX)];
|
|
535
|
+
Cr = component3Line[0 | (_x * component3.scaleX)];
|
|
536
|
+
K = component4Line[0 | (_x * component4.scaleX)];
|
|
537
|
+
|
|
538
|
+
C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
|
|
539
|
+
M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
|
540
|
+
Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
data[offset++] = 255 - C;
|
|
544
|
+
data[offset++] = 255 - M;
|
|
545
|
+
data[offset++] = 255 - Ye;
|
|
546
|
+
data[offset++] = 255 - K;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
break;
|
|
550
|
+
default:
|
|
551
|
+
throw new Error('Unsupported color mode');
|
|
552
|
+
}
|
|
553
|
+
return data;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
*
|
|
558
|
+
* @param {{data:Uint8Array, width:number, height:number}} imageData
|
|
559
|
+
* @param {boolean} [formatAsRGBA]
|
|
560
|
+
*/
|
|
561
|
+
copyToImageData(imageData, formatAsRGBA = false) {
|
|
562
|
+
const width = imageData.width, height = imageData.height;
|
|
563
|
+
const imageDataArray = imageData.data;
|
|
564
|
+
const data = this.getData(width, height);
|
|
565
|
+
let i = 0, j = 0, x, y;
|
|
566
|
+
let Y, K, C, M, R, G, B;
|
|
567
|
+
switch (this.components.length) {
|
|
568
|
+
case 1:
|
|
569
|
+
for (y = 0; y < height; y++) {
|
|
570
|
+
for (x = 0; x < width; x++) {
|
|
571
|
+
Y = data[i++];
|
|
572
|
+
|
|
573
|
+
imageDataArray[j++] = Y;
|
|
574
|
+
imageDataArray[j++] = Y;
|
|
575
|
+
imageDataArray[j++] = Y;
|
|
576
|
+
if (formatAsRGBA) {
|
|
577
|
+
imageDataArray[j++] = 255;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
break;
|
|
582
|
+
case 3:
|
|
583
|
+
for (y = 0; y < height; y++) {
|
|
584
|
+
for (x = 0; x < width; x++) {
|
|
585
|
+
R = data[i++];
|
|
586
|
+
G = data[i++];
|
|
587
|
+
B = data[i++];
|
|
588
|
+
|
|
589
|
+
imageDataArray[j++] = R;
|
|
590
|
+
imageDataArray[j++] = G;
|
|
591
|
+
imageDataArray[j++] = B;
|
|
592
|
+
if (formatAsRGBA) {
|
|
593
|
+
imageDataArray[j++] = 255;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
break;
|
|
598
|
+
case 4:
|
|
599
|
+
for (y = 0; y < height; y++) {
|
|
600
|
+
for (x = 0; x < width; x++) {
|
|
601
|
+
C = data[i++];
|
|
602
|
+
M = data[i++];
|
|
603
|
+
Y = data[i++];
|
|
604
|
+
K = data[i++];
|
|
605
|
+
|
|
606
|
+
R = 255 - clampTo8bit(C * (1 - K / 255) + K);
|
|
607
|
+
G = 255 - clampTo8bit(M * (1 - K / 255) + K);
|
|
608
|
+
B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
|
|
609
|
+
|
|
610
|
+
imageDataArray[j++] = R;
|
|
611
|
+
imageDataArray[j++] = G;
|
|
612
|
+
imageDataArray[j++] = B;
|
|
613
|
+
if (formatAsRGBA) {
|
|
614
|
+
imageDataArray[j++] = 255;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
break;
|
|
619
|
+
default:
|
|
620
|
+
throw new Error('Unsupported color mode');
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildComponentData.d.ts","sourceRoot":"","sources":["../../../../../../../src/engine/asset/loaders/image/jpeg/buildComponentData.js"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,mEAFY,UAAU,EAAE,CAqDvB"}
|