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/CHANGELOG.md +39 -0
- package/README.md +328 -55
- package/icon.png +0 -0
- package/lib/adapter-kit.js +289 -40
- package/lib/adapters/async.js +354 -140
- package/lib/adapters/cn.js +133 -68
- package/lib/adapters/openai.js +193 -143
- package/lib/adapters.js +3579 -695
- package/lib/client.js +6742 -828
- package/lib/comfy-workflow.js +100 -10
- package/lib/config-store.js +1628 -0
- package/lib/field-templates.js +385 -0
- package/lib/image-execution.js +58 -0
- package/lib/image-jobs.js +344 -0
- package/lib/image-queue.js +195 -0
- package/lib/image-request.js +830 -0
- package/lib/image-service.js +189 -0
- package/lib/image-worker.js +208 -0
- package/lib/index.js +1054 -967
- package/lib/localized-error.js +4 -0
- package/lib/model-rules.js +1034 -0
- package/lib/net.js +60 -25
- package/lib/output-profiles.js +1225 -0
- package/lib/png-scale.js +150 -68
- package/lib/prompt-profiles.js +196 -80
- package/lib/provider-operations.js +291 -0
- package/lib/proxy-dispatcher.js +11 -5
- package/lib/proxy.js +10 -8
- package/lib/request-auth.js +198 -0
- package/lib/retry.js +162 -0
- package/lib/settings-memory.js +117 -0
- package/lib/status.js +190 -0
- package/locale/messages.csv +206 -24
- package/package.json +3 -1
- package/scripts/release.mjs +79 -64
- package/skills/mo-image-editing/SKILL.md +36 -80
- package/skills/mo-image-generation/SKILL.md +62 -154
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
|
-
/**
|
|
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 (!
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
144
|
+
if (!isChunkType(buffer, offset + 4)) return null;
|
|
71
145
|
const start = offset + 8;
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
palette =
|
|
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 =
|
|
83
|
-
} else if (type === 'IDAT') {
|
|
84
|
-
idat.push(
|
|
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 =
|
|
164
|
+
offset = dataEnd + 4;
|
|
89
165
|
}
|
|
90
166
|
|
|
91
|
-
if (!
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
110
|
-
const
|
|
111
|
-
|
|
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
|
|
114
|
-
const
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (filter ===
|
|
119
|
-
else if (filter ===
|
|
120
|
-
else
|
|
121
|
-
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
|
234
|
+
return { width, height, rgba };
|
|
153
235
|
}
|
|
154
236
|
|
|
155
237
|
/** 面积平均缩放(不是最近邻,小图不会锯齿)。 */
|