dsh-mo-image 0.6.0 → 1.0.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/lib/png-scale.js CHANGED
@@ -10,6 +10,8 @@ import zlib from 'node:zlib';
10
10
 
11
11
  const PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
12
12
  const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
13
+ const MAX_PIXELS = 32 * 1024 * 1024;
14
+ const MAX_DIMENSION = 0x7fffffff;
13
15
 
14
16
  const CRC_TABLE = (() => {
15
17
  const table = new Uint32Array(256);
@@ -52,104 +54,184 @@ export function isPng(buffer) {
52
54
  return true;
53
55
  }
54
56
 
55
- /** 解成 { width, height, rgba };不支持就返回 null。 */
57
+ /** PNG 允许的色深/颜色类型组合;解码器自己只实现 8-bit。 */
58
+ function validColorFormat(bitDepth, colorType) {
59
+ if (colorType === 0) return bitDepth === 1 || bitDepth === 2 || bitDepth === 4 || bitDepth === 8 || bitDepth === 16;
60
+ if (colorType === 3) return bitDepth === 1 || bitDepth === 2 || bitDepth === 4 || bitDepth === 8;
61
+ if (colorType === 2 || colorType === 4 || colorType === 6) return bitDepth === 8 || bitDepth === 16;
62
+ return false;
63
+ }
64
+
65
+ function positiveDimension(value) {
66
+ return Number.isSafeInteger(value) && value > 0 && value <= MAX_DIMENSION;
67
+ }
68
+
69
+ /**
70
+ * 只读签名和 IHDR(含 CRC),不碰像素数据。
71
+ *
72
+ * 宽高是后面所有分配和循环的依据,所以必须在解码前把长度、边界和字段合法性验光;
73
+ * adapter-kit 的 imageSizeOf/looksLikeImage 也复用这里,保证两边对「合法头」的定义一致。
74
+ */
75
+ export function pngHeaderOf(buffer) {
76
+ if (!Buffer.isBuffer(buffer) || buffer.length < 33 || !isPng(buffer)) return null;
77
+ if (buffer.readUInt32BE(8) !== 13) return null;
78
+ if (buffer.toString('ascii', 12, 16) !== 'IHDR') return null;
79
+ if (crc32(buffer.subarray(12, 29)) !== buffer.readUInt32BE(29)) return null;
80
+ const width = buffer.readUInt32BE(16);
81
+ const height = buffer.readUInt32BE(20);
82
+ if (!positiveDimension(width) || !positiveDimension(height)) return null;
83
+ const bitDepth = buffer[24];
84
+ const colorType = buffer[25];
85
+ const compression = buffer[26];
86
+ const filter = buffer[27];
87
+ const interlace = buffer[28];
88
+ if (!validColorFormat(bitDepth, colorType)) return null;
89
+ if (compression !== 0 || filter !== 0 || (interlace !== 0 && interlace !== 1)) return null;
90
+ return {
91
+ width: width,
92
+ height: height,
93
+ bitDepth: bitDepth,
94
+ colorType: colorType,
95
+ compression: compression,
96
+ filter: filter,
97
+ interlace: interlace
98
+ };
99
+ }
100
+
101
+ /** 像素预算用除法比较:两个 32 位宽高直接相乘会先超过安全整数。 */
102
+ function pixelCount(width, height) {
103
+ if (!positiveDimension(width) || !positiveDimension(height)) return 0;
104
+ if (width > Math.floor(MAX_PIXELS / height)) return 0;
105
+ const pixels = width * height;
106
+ return Number.isSafeInteger(pixels) && Number.isSafeInteger(pixels * 4) ? pixels : 0;
107
+ }
108
+
109
+ function isChunkType(buffer, offset) {
110
+ for (let i = 0; i < 4; i++) {
111
+ const code = buffer[offset + i];
112
+ if (!((code >= 0x41 && code <= 0x5a) || (code >= 0x61 && code <= 0x7a))) return false;
113
+ }
114
+ return true;
115
+ }
116
+
117
+ function isCriticalChunk(buffer, offset) {
118
+ const code = buffer[offset];
119
+ return code >= 0x41 && code <= 0x5a;
120
+ }
121
+
122
+ /** 解成 { width, height, rgba };不支持或结构不合法就返回 null。 */
56
123
  export function decodePng(buffer) {
57
- if (!isPng(buffer)) return null;
58
- let offset = 8;
59
- let width = 0;
60
- let height = 0;
61
- let bitDepth = 0;
62
- let colorType = -1;
63
- let interlace = 0;
124
+ if (!Buffer.isBuffer(buffer)) return null;
125
+ const header = pngHeaderOf(buffer);
126
+ if (!header) return null;
127
+ if (header.bitDepth !== 8 || header.interlace !== 0) return null;
128
+ const channels = CHANNELS[header.colorType];
129
+ if (!channels) return null;
130
+ const width = header.width;
131
+ const height = header.height;
132
+ const colorType = header.colorType;
133
+ const pixels = pixelCount(width, height);
134
+ if (!pixels) return null;
135
+
136
+ let offset = 33;
64
137
  let palette = null;
65
138
  let transparency = null;
139
+ let sawIend = false;
66
140
  const idat = [];
67
141
 
68
142
  while (offset + 8 <= buffer.length) {
69
143
  const length = buffer.readUInt32BE(offset);
70
- const type = buffer.toString('ascii', offset + 4, offset + 8);
144
+ if (!isChunkType(buffer, offset + 4)) return null;
71
145
  const start = offset + 8;
72
- const data = buffer.subarray(start, start + length);
73
- if (type === 'IHDR') {
74
- width = data.readUInt32BE(0);
75
- height = data.readUInt32BE(4);
76
- bitDepth = data[8];
77
- colorType = data[9];
78
- interlace = data[12];
79
- } else if (type === 'PLTE') {
80
- palette = Buffer.from(data);
146
+ // 块长度必须连同尾部 CRC 落在 buffer 内,否则后面的读取会越界。
147
+ if (length > buffer.length - start - 4) return null;
148
+ const dataEnd = start + length;
149
+ if (isCriticalChunk(buffer, offset + 4)
150
+ && crc32(buffer.subarray(offset + 4, dataEnd)) !== buffer.readUInt32BE(dataEnd)) return null;
151
+ const type = buffer.toString('ascii', offset + 4, offset + 8);
152
+ if (type === 'IHDR') return null;
153
+ if (type === 'PLTE') {
154
+ palette = buffer.subarray(start, dataEnd);
81
155
  } else if (type === 'tRNS') {
82
- transparency = Buffer.from(data);
83
- } else if (type === 'IDAT') {
84
- idat.push(Buffer.from(data));
156
+ transparency = buffer.subarray(start, dataEnd);
157
+ } else if (type === 'IDAT' && length) {
158
+ idat.push(buffer.subarray(start, dataEnd));
85
159
  } else if (type === 'IEND') {
160
+ if (length !== 0) return null;
161
+ sawIend = true;
86
162
  break;
87
163
  }
88
- offset = start + length + 4;
164
+ offset = dataEnd + 4;
89
165
  }
90
166
 
91
- if (!width || !height || bitDepth !== 8 || interlace !== 0) return null;
92
- const channels = CHANNELS[colorType];
93
- if (!channels) return null;
167
+ if (!sawIend || idat.length === 0) return null;
94
168
  if (colorType === 3 && !palette) return null;
95
169
 
170
+ const stride = width * channels;
171
+ const rowSize = stride + 1;
172
+ const expected = height * rowSize;
173
+ if (!Number.isSafeInteger(expected) || expected <= 0) return null;
96
174
  let raw;
97
175
  try {
98
- raw = zlib.inflateSync(Buffer.concat(idat));
176
+ // 只解到期望长度:压缩炸弹会在超出时抛错,而不是先占满内存。
177
+ raw = zlib.inflateSync(idat.length === 1 ? idat[0] : Buffer.concat(idat), { maxOutputLength: expected });
99
178
  } catch (_error) {
100
179
  return null;
101
180
  }
181
+ if (!raw || raw.length !== expected) return null;
102
182
 
103
- const stride = width * channels;
104
- if (raw.length < height * (stride + 1)) return null;
105
- const pixels = Buffer.alloc(height * stride);
106
-
107
- let pos = 0;
183
+ // 在解压缓冲区内还原扫描线。过滤器只依赖当前行左侧和上一行,不需要第二份像素副本。
108
184
  for (let y = 0; y < height; y++) {
109
- const filter = raw[pos++];
110
- const rowStart = y * stride;
111
- const prevStart = rowStart - stride;
185
+ const rowStart = y * rowSize + 1;
186
+ const filter = raw[rowStart - 1];
187
+ if (filter === 0) continue;
188
+ if (filter > 4) return null;
189
+ const prevStart = rowStart - rowSize;
112
190
  for (let x = 0; x < stride; x++) {
113
- const value = raw[pos++];
114
- const a = x >= channels ? pixels[rowStart + x - channels] : 0;
115
- const b = y > 0 ? pixels[prevStart + x] : 0;
116
- const c = (x >= channels && y > 0) ? pixels[prevStart + x - channels] : 0;
117
- let out;
118
- if (filter === 0) out = value;
119
- else if (filter === 1) out = value + a;
120
- else if (filter === 2) out = value + b;
121
- else if (filter === 3) out = value + ((a + b) >> 1);
122
- else if (filter === 4) out = value + paeth(a, b, c);
123
- else return null;
124
- pixels[rowStart + x] = out & 0xff;
191
+ const a = x >= channels ? raw[rowStart + x - channels] : 0;
192
+ const b = y > 0 ? raw[prevStart + x] : 0;
193
+ const c = x >= channels && y > 0 ? raw[prevStart + x - channels] : 0;
194
+ let prediction;
195
+ if (filter === 1) prediction = a;
196
+ else if (filter === 2) prediction = b;
197
+ else if (filter === 3) prediction = (a + b) >> 1;
198
+ else prediction = paeth(a, b, c);
199
+ raw[rowStart + x] = (raw[rowStart + x] + prediction) & 0xff;
125
200
  }
126
201
  }
127
202
 
128
- const rgba = new Uint8Array(width * height * 4);
129
- for (let i = 0; i < width * height; i++) {
130
- const src = i * channels;
131
- const dst = i * 4;
132
- if (colorType === 0) {
133
- const v = pixels[src];
134
- rgba[dst] = v; rgba[dst + 1] = v; rgba[dst + 2] = v; rgba[dst + 3] = 255;
135
- } else if (colorType === 2) {
136
- rgba[dst] = pixels[src]; rgba[dst + 1] = pixels[src + 1]; rgba[dst + 2] = pixels[src + 2]; rgba[dst + 3] = 255;
137
- } else if (colorType === 3) {
138
- const index = pixels[src];
139
- const at = index * 3;
140
- rgba[dst] = palette[at] || 0;
141
- rgba[dst + 1] = palette[at + 1] || 0;
142
- rgba[dst + 2] = palette[at + 2] || 0;
143
- rgba[dst + 3] = transparency && index < transparency.length ? transparency[index] : 255;
144
- } else if (colorType === 4) {
145
- const v = pixels[src];
146
- rgba[dst] = v; rgba[dst + 1] = v; rgba[dst + 2] = v; rgba[dst + 3] = pixels[src + 1];
147
- } else {
148
- rgba[dst] = pixels[src]; rgba[dst + 1] = pixels[src + 1]; rgba[dst + 2] = pixels[src + 2]; rgba[dst + 3] = pixels[src + 3];
203
+ if (colorType === 6) {
204
+ // RGBA 只需移除每行的过滤字节,复用整个解压缓冲区。
205
+ for (let y = 0; y < height; y++) {
206
+ const rowStart = y * rowSize + 1;
207
+ raw.copyWithin(y * stride, rowStart, rowStart + stride);
208
+ }
209
+ return { width, height, rgba: new Uint8Array(raw.buffer, raw.byteOffset, pixels * 4) };
210
+ }
211
+
212
+ const rgba = new Uint8Array(pixels * 4);
213
+ let dst = 0;
214
+ for (let y = 0; y < height; y++) {
215
+ const rowStart = y * rowSize + 1;
216
+ for (let src = rowStart; src < rowStart + stride; src += channels, dst += 4) {
217
+ if (colorType === 0 || colorType === 4) {
218
+ const gray = raw[src];
219
+ rgba[dst] = gray; rgba[dst + 1] = gray; rgba[dst + 2] = gray;
220
+ rgba[dst + 3] = colorType === 4 ? raw[src + 1] : 255;
221
+ } else if (colorType === 2) {
222
+ rgba[dst] = raw[src]; rgba[dst + 1] = raw[src + 1]; rgba[dst + 2] = raw[src + 2]; rgba[dst + 3] = 255;
223
+ } else {
224
+ const index = raw[src];
225
+ const at = index * 3;
226
+ rgba[dst] = palette[at] || 0;
227
+ rgba[dst + 1] = palette[at + 1] || 0;
228
+ rgba[dst + 2] = palette[at + 2] || 0;
229
+ rgba[dst + 3] = transparency && index < transparency.length ? transparency[index] : 255;
230
+ }
149
231
  }
150
232
  }
151
233
 
152
- return { width: width, height: height, rgba: rgba };
234
+ return { width, height, rgba };
153
235
  }
154
236
 
155
237
  /** 面积平均缩放(不是最近邻,小图不会锯齿)。 */