@crabcrop/mcp 0.1.0 → 0.10.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 +12 -5
- package/dist/index.js +83 -34
- package/node_modules/@crabcrop/url/README.md +51 -7
- package/node_modules/@crabcrop/url/dist/bytes.d.ts +21 -0
- package/node_modules/@crabcrop/url/dist/bytes.js +86 -0
- package/node_modules/@crabcrop/url/dist/crypto/aes-gcm.d.ts +21 -0
- package/node_modules/@crabcrop/url/dist/crypto/aes-gcm.js +248 -0
- package/node_modules/@crabcrop/url/dist/crypto/node.d.ts +11 -0
- package/node_modules/@crabcrop/url/dist/crypto/node.js +41 -0
- package/node_modules/@crabcrop/url/dist/crypto/portable.d.ts +14 -0
- package/node_modules/@crabcrop/url/dist/crypto/portable.js +60 -0
- package/node_modules/@crabcrop/url/dist/crypto/sha256.d.ts +14 -0
- package/node_modules/@crabcrop/url/dist/crypto/sha256.js +131 -0
- package/node_modules/@crabcrop/url/dist/explain.d.ts +56 -8
- package/node_modules/@crabcrop/url/dist/explain.js +497 -50
- package/node_modules/@crabcrop/url/dist/index.d.ts +83 -5
- package/node_modules/@crabcrop/url/dist/index.js +201 -26
- package/node_modules/@crabcrop/url/dist/limits.d.ts +9 -0
- package/node_modules/@crabcrop/url/dist/limits.js +9 -0
- package/node_modules/@crabcrop/url/dist/operations.d.ts +114 -19
- package/node_modules/@crabcrop/url/dist/operations.js +343 -50
- package/node_modules/@crabcrop/url/package.json +12 -4
- package/package.json +1 -1
- package/server.json +1 -1
|
@@ -9,12 +9,24 @@
|
|
|
9
9
|
* pipeline and the equivalent hand-written string produce
|
|
10
10
|
* byte-identical URLs. Validation throws at build time, like
|
|
11
11
|
* `buildSrcSet` does for widths: a value the server would 400
|
|
12
|
-
* never gets signed.
|
|
12
|
+
* never gets signed. The closed sets (formats, aligns, anchors,
|
|
13
|
+
* modes, factors, shapes) and the class-name checks come from
|
|
14
|
+
* the explainer (issue #482), so the builders and the
|
|
15
|
+
* explainer read one copy of them. The numeric bounds are
|
|
16
|
+
* still written on each side: the builders check numbers, the
|
|
17
|
+
* explainer checks their spellings.
|
|
13
18
|
*/
|
|
14
|
-
|
|
19
|
+
import { BLEND_MODES, classFoldKey, CROP_ALIGNS, CURVE_CHANNELS, DPR_FACTORS, FLIP_AXES, IMAGE_FORMATS, isClassName, isOneOf, JPEG_SUBSAMPLINGS, MASK_SHAPES, MAX_OBJECTS_CLASSES, QUALITY_FORMATS, STUDIO_BACKDROPS, STUDIO_MODES, STUDIO_SHADOWS, TEXT_ANCHORS, UPSCALE_FACTORS, } from "./explain.js";
|
|
20
|
+
import { MAX_DIMENSION } from "./limits.js";
|
|
15
21
|
function fail(what, value, rule) {
|
|
16
22
|
throw new Error(`${what} must be ${rule}, got ${JSON.stringify(value)}`);
|
|
17
23
|
}
|
|
24
|
+
/** A closed set spelled for an error message: `2 or 4`, `1, 1.5, 2, or 3`. */
|
|
25
|
+
function eitherOf(values, quote = false) {
|
|
26
|
+
const words = values.map((v) => (quote ? `"${v}"` : String(v)));
|
|
27
|
+
const last = words.pop();
|
|
28
|
+
return words.length === 1 ? `${words[0]} or ${last}` : `${words.join(", ")}, or ${last}`;
|
|
29
|
+
}
|
|
18
30
|
function intIn(what, value, min, max) {
|
|
19
31
|
if (!Number.isSafeInteger(value) || value < min || value > max) {
|
|
20
32
|
fail(what, value, `an integer from ${min} to ${max}`);
|
|
@@ -28,19 +40,92 @@ function floatIn(what, value, min, max) {
|
|
|
28
40
|
return value;
|
|
29
41
|
}
|
|
30
42
|
function positiveFloat(what, value) {
|
|
31
|
-
|
|
43
|
+
// The server reads a 32-bit float: past its range the value
|
|
44
|
+
// is infinite there, and a positive value below it is zero.
|
|
45
|
+
const f32 = Math.fround(value);
|
|
46
|
+
if (!Number.isFinite(f32) || f32 <= 0) {
|
|
32
47
|
fail(what, value, "a positive number");
|
|
33
48
|
}
|
|
34
49
|
return value;
|
|
35
50
|
}
|
|
51
|
+
/** Spell `digits * 10^exponent` in plain decimal, no exponent. */
|
|
52
|
+
function plainDecimal(digits, exponent) {
|
|
53
|
+
const text = digits.toString();
|
|
54
|
+
if (exponent >= 0)
|
|
55
|
+
return text + "0".repeat(exponent);
|
|
56
|
+
const padded = text.padStart(1 - exponent, "0");
|
|
57
|
+
const whole = padded.slice(0, padded.length + exponent);
|
|
58
|
+
const fraction = padded.slice(padded.length + exponent).replace(/0+$/, "");
|
|
59
|
+
return fraction ? `${whole}.${fraction}` : whole;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The canonical spelling of a 32-bit float parameter (issue
|
|
63
|
+
* #472). The server parses these arguments as `f32` and
|
|
64
|
+
* prints them back as Rust's float `Display` does: the
|
|
65
|
+
* shortest decimal that reads back as the same f32 (the
|
|
66
|
+
* closer of two, a tie going up), never an exponent. So the
|
|
67
|
+
* double `0.1 + 0.2` spells `0.3`, the form the server keys
|
|
68
|
+
* its cache on, not its 17-digit double spelling. The search
|
|
69
|
+
* is exact, on the float's binary value in BigInt
|
|
70
|
+
* arithmetic, so no double rounding can pick a spelling Rust
|
|
71
|
+
* would not.
|
|
72
|
+
*/
|
|
73
|
+
function num(value) {
|
|
74
|
+
const f = Math.fround(value);
|
|
75
|
+
if (!Number.isFinite(f)) {
|
|
76
|
+
throw new Error(`${value} is outside the 32-bit float range the server parses`);
|
|
77
|
+
}
|
|
78
|
+
if (f === 0)
|
|
79
|
+
return "0";
|
|
80
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
81
|
+
view.setFloat32(0, Math.abs(f));
|
|
82
|
+
const bits = view.getUint32(0);
|
|
83
|
+
const biased = bits >>> 23;
|
|
84
|
+
const fraction = bits & 0x7fffff;
|
|
85
|
+
const mantissa = BigInt(biased === 0 ? fraction : fraction | 0x800000);
|
|
86
|
+
// In units of a quarter ulp, 2^unit: the float sits at
|
|
87
|
+
// 4 * mantissa and reads back from any decimal strictly
|
|
88
|
+
// between the midpoints to its neighbors, which lie half as
|
|
89
|
+
// far below at a power of two (the binade below is finer).
|
|
90
|
+
// A decimal exactly on a midpoint rounds to the even
|
|
91
|
+
// mantissa, so the ends count only for an even one.
|
|
92
|
+
const unit = (biased === 0 ? 1 : biased) - 152;
|
|
93
|
+
const center = 4n * mantissa;
|
|
94
|
+
const low = fraction === 0 && biased > 1 ? center - 1n : center - 2n;
|
|
95
|
+
const high = center + 2n;
|
|
96
|
+
const inclusive = mantissa % 2n === 0n;
|
|
97
|
+
const sign = f < 0 ? "-" : "";
|
|
98
|
+
// From one decade above the value down, the first decimal
|
|
99
|
+
// exponent with a candidate in the interval gives the
|
|
100
|
+
// shortest spelling. Only the two neighbors of the value at
|
|
101
|
+
// that exponent can qualify, and an f32 needs at most nine
|
|
102
|
+
// significant digits, so the loop is short.
|
|
103
|
+
for (let k = Math.floor(Math.log10(Math.abs(f))) + 2;; k--) {
|
|
104
|
+
// Compare digits * 10^k with units * 2^unit as integers.
|
|
105
|
+
const binary = (unit > 0 ? 2n ** BigInt(unit) : 1n) * (k < 0 ? 10n ** BigInt(-k) : 1n);
|
|
106
|
+
const step = (unit < 0 ? 2n ** BigInt(-unit) : 1n) * (k > 0 ? 10n ** BigInt(k) : 1n);
|
|
107
|
+
const at = center * binary;
|
|
108
|
+
const below = (at / step) * step;
|
|
109
|
+
const above = below + step;
|
|
110
|
+
const lowEnd = low * binary;
|
|
111
|
+
const highEnd = high * binary;
|
|
112
|
+
const downFits = inclusive ? below >= lowEnd : below > lowEnd;
|
|
113
|
+
const upFits = inclusive ? above <= highEnd : above < highEnd;
|
|
114
|
+
if (downFits || upFits) {
|
|
115
|
+
const down = downFits && (!upFits || 2n * (at - below) < step);
|
|
116
|
+
return sign + plainDecimal((down ? below : above) / step, k);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
36
120
|
/**
|
|
37
|
-
* The canonical
|
|
121
|
+
* The canonical spelling of a 64-bit float parameter (the
|
|
122
|
+
* convolution kernel's values): integers without a fraction,
|
|
38
123
|
* other values in plain decimal (the grammar takes no
|
|
39
124
|
* exponent forms). JavaScript's shortest round-trip spelling
|
|
40
125
|
* agrees with the server's for the magnitudes the grammar
|
|
41
126
|
* allows; exponent spellings are expanded to plain decimal.
|
|
42
127
|
*/
|
|
43
|
-
function
|
|
128
|
+
function kernelNum(value) {
|
|
44
129
|
if (Number.isSafeInteger(value))
|
|
45
130
|
return String(value);
|
|
46
131
|
const s = String(value);
|
|
@@ -89,28 +174,13 @@ function encodeArg(text) {
|
|
|
89
174
|
}
|
|
90
175
|
function className(what, name) {
|
|
91
176
|
const trimmed = name.trim();
|
|
92
|
-
if (trimmed
|
|
93
|
-
|
|
94
|
-
!/^[\p{L}\p{N} _.\-/]+$/u.test(trimmed)) {
|
|
95
|
-
fail(what, name, "1 to 64 characters of letters, digits, space, `_`, `.`, `-`, `/`");
|
|
177
|
+
if (!isClassName(trimmed)) {
|
|
178
|
+
fail(what, name, "1 to 64 UTF-8 bytes of letters, digits, space, `_`, `.`, `-`, `/`");
|
|
96
179
|
}
|
|
97
|
-
|
|
180
|
+
// The server trims the name before it validates and keys
|
|
181
|
+
// on it, so the trimmed name is the canonical one.
|
|
182
|
+
return encodeArg(trimmed);
|
|
98
183
|
}
|
|
99
|
-
const CROP_ALIGNS = ["center", "smart", "face", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"];
|
|
100
|
-
const ANCHORS = [
|
|
101
|
-
"top-left",
|
|
102
|
-
"top-center",
|
|
103
|
-
"top-right",
|
|
104
|
-
"center-left",
|
|
105
|
-
"center",
|
|
106
|
-
"center-right",
|
|
107
|
-
"bottom-left",
|
|
108
|
-
"bottom-center",
|
|
109
|
-
"bottom-right",
|
|
110
|
-
];
|
|
111
|
-
const BLEND_MODES = ["normal", "multiply", "screen", "overlay"];
|
|
112
|
-
const CURVE_CHANNELS = ["rgb", "r", "g", "b"];
|
|
113
|
-
const IMAGE_FORMATS = ["jpeg", "jpg", "png", "webp", "jxl", "avif", "gif", "mp4", "webm"];
|
|
114
184
|
/** Operation segments, one typed call per segment kind. */
|
|
115
185
|
export const ops = {
|
|
116
186
|
/** `300x200`; each side a Dimension (0 proportional, negative flips). */
|
|
@@ -136,12 +206,54 @@ export const ops = {
|
|
|
136
206
|
},
|
|
137
207
|
/** `crop:300x200` or `crop:300x200:smart`; an exact pixel box. */
|
|
138
208
|
crop(width, height, align = "center") {
|
|
139
|
-
if (!CROP_ALIGNS
|
|
209
|
+
if (!isOneOf(CROP_ALIGNS, align)) {
|
|
140
210
|
fail("crop align", align, `one of ${CROP_ALIGNS.join(", ")}`);
|
|
141
211
|
}
|
|
142
212
|
const box = pixelBox("crop", width, height);
|
|
143
213
|
return align === "center" ? `crop:${box}` : `crop:${box}:${align}`;
|
|
144
214
|
},
|
|
215
|
+
/**
|
|
216
|
+
* `crop:300x200:objects(person,car):1.5`; crops to the
|
|
217
|
+
* union of every detection of the named classes, the
|
|
218
|
+
* union box scaled about its center by `scale` (1 is the
|
|
219
|
+
* tight box; the crop stays centered on the union).
|
|
220
|
+
* Needs a detection model configured on the server; when
|
|
221
|
+
* none of the classes is detected the render falls back
|
|
222
|
+
* to the plain center crop of the same size. Duplicate
|
|
223
|
+
* classes (ASCII case-insensitive, as the server compares
|
|
224
|
+
* them: `E` repeats `e`, `É` does not repeat `é`) are
|
|
225
|
+
* spelled once, as the server's canonical form spells
|
|
226
|
+
* them; at most 32 distinct classes, and a 33rd throws,
|
|
227
|
+
* since the server rejects it with `invalid_crop`.
|
|
228
|
+
*/
|
|
229
|
+
cropObjects(width, height, classes, scale = 1) {
|
|
230
|
+
if (!Array.isArray(classes) || classes.length < 1) {
|
|
231
|
+
fail("crop objects classes", classes, "an array of 1 to 32 distinct class names");
|
|
232
|
+
}
|
|
233
|
+
// The cap counts distinct names, as the server's parser
|
|
234
|
+
// does (issue #408): repeats fold first and cost nothing.
|
|
235
|
+
const seen = new Set();
|
|
236
|
+
const names = [];
|
|
237
|
+
for (const name of classes) {
|
|
238
|
+
const encoded = className("crop objects class", name);
|
|
239
|
+
const key = classFoldKey(name.trim());
|
|
240
|
+
if (!seen.has(key)) {
|
|
241
|
+
if (names.length === MAX_OBJECTS_CLASSES) {
|
|
242
|
+
fail("crop objects classes", classes, "an array of 1 to 32 distinct class names");
|
|
243
|
+
}
|
|
244
|
+
seen.add(key);
|
|
245
|
+
names.push(encoded);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Checked as the f32 the server reads (as are zoom, blur
|
|
249
|
+
// and gamma below): a positive double that rounds to f32
|
|
250
|
+
// zero would otherwise be spelled `0`, which it refuses.
|
|
251
|
+
if (!Number.isFinite(scale) || Math.fround(scale) <= 0 || scale > 100) {
|
|
252
|
+
fail("crop objects scale", scale, "a number above 0 and at most 100");
|
|
253
|
+
}
|
|
254
|
+
const box = pixelBox("crop", width, height);
|
|
255
|
+
return `crop:${box}:objects(${names.join(",")}):${num(scale)}`;
|
|
256
|
+
},
|
|
145
257
|
/** `crop-rect:10x20:110x220` (left x top : right x bottom). */
|
|
146
258
|
cropRect(left, top, right, bottom) {
|
|
147
259
|
intIn("crop-rect left", left, 0, 4294967295);
|
|
@@ -164,6 +276,19 @@ export const ops = {
|
|
|
164
276
|
}
|
|
165
277
|
return `focal:${left}x${top}:${right}x${bottom}`;
|
|
166
278
|
},
|
|
279
|
+
/** `focalpoint:0.75x0.4:2` (fractions of the image dimensions, zoom optional); steers the next crop. */
|
|
280
|
+
focalPoint(x, y, zoom = 1) {
|
|
281
|
+
const frac = (what, v) => {
|
|
282
|
+
if (!Number.isFinite(v) || v < 0 || v > 1) {
|
|
283
|
+
fail(what, v, "a fraction from 0 to 1");
|
|
284
|
+
}
|
|
285
|
+
return v;
|
|
286
|
+
};
|
|
287
|
+
if (!Number.isFinite(zoom) || Math.fround(zoom) <= 0 || zoom > 100) {
|
|
288
|
+
fail("focalpoint zoom", zoom, "a number above 0 and at most 100");
|
|
289
|
+
}
|
|
290
|
+
return `focalpoint:${num(frac("focalpoint x", x))}x${num(frac("focalpoint y", y))}:${num(zoom)}`;
|
|
291
|
+
},
|
|
167
292
|
/** `pad:300x200`, optionally `:blur` or a RRGGBB fill color. */
|
|
168
293
|
pad(width, height, fill) {
|
|
169
294
|
const box = pixelBox("pad", width, height);
|
|
@@ -175,8 +300,12 @@ export const ops = {
|
|
|
175
300
|
},
|
|
176
301
|
/** `preset:thumb`; expands server-side. */
|
|
177
302
|
preset(name) {
|
|
178
|
-
|
|
179
|
-
|
|
303
|
+
// The server can only configure names from this
|
|
304
|
+
// charset (its preset file validation), so anything
|
|
305
|
+
// outside it is a reference that can only ever answer
|
|
306
|
+
// unknown_preset; refuse it here instead.
|
|
307
|
+
if (!/^[A-Za-z0-9_-]+$/.test(name)) {
|
|
308
|
+
fail("preset name", name, "letters, digits, `_`, `-` (the server's preset name charset)");
|
|
180
309
|
}
|
|
181
310
|
return `preset:${name}`;
|
|
182
311
|
},
|
|
@@ -227,6 +356,9 @@ export const filter = {
|
|
|
227
356
|
equalize() {
|
|
228
357
|
return "equalize";
|
|
229
358
|
},
|
|
359
|
+
enhance() {
|
|
360
|
+
return "enhance";
|
|
361
|
+
},
|
|
230
362
|
removeBg() {
|
|
231
363
|
return "removebg";
|
|
232
364
|
},
|
|
@@ -242,9 +374,13 @@ export const filter = {
|
|
|
242
374
|
pixelateFaces() {
|
|
243
375
|
return "pixelatefaces";
|
|
244
376
|
},
|
|
245
|
-
/** Gaussian blur by sigma
|
|
377
|
+
/** Gaussian blur by sigma: positive, at most the server's 512 cap. */
|
|
246
378
|
blur(sigma) {
|
|
247
|
-
|
|
379
|
+
// The server also refuses a subnormal f32 sigma.
|
|
380
|
+
if (!Number.isFinite(sigma) || Math.fround(sigma) < 2 ** -126 || sigma > 512) {
|
|
381
|
+
fail("blur sigma", sigma, "a positive number up to 512");
|
|
382
|
+
}
|
|
383
|
+
return `blur(${num(sigma)})`;
|
|
248
384
|
},
|
|
249
385
|
/** Unsharp-mask strength (positive). */
|
|
250
386
|
sharpen(amount) {
|
|
@@ -258,6 +394,28 @@ export const filter = {
|
|
|
258
394
|
contrast(percent) {
|
|
259
395
|
return `contrast(${num(floatIn("contrast", percent, -100, 100))})`;
|
|
260
396
|
},
|
|
397
|
+
/** Gamma correction divisor: 0 < g <= 10 (1 unchanged; above 1 brightens midtones). */
|
|
398
|
+
gamma(value) {
|
|
399
|
+
if (!Number.isFinite(value) || Math.fround(value) <= 0 || value > 10) {
|
|
400
|
+
fail("gamma", value, "a number above 0 and at most 10");
|
|
401
|
+
}
|
|
402
|
+
return `gamma(${num(value)})`;
|
|
403
|
+
},
|
|
404
|
+
/** Exposure in stops: -10..10 (0 unchanged; +1 doubles the light, -1 halves it). */
|
|
405
|
+
exposure(stops) {
|
|
406
|
+
if (!Number.isFinite(stops) || stops < -10 || stops > 10) {
|
|
407
|
+
fail("exposure", stops, "a number between -10 and 10");
|
|
408
|
+
}
|
|
409
|
+
return `exposure(${num(stops)})`;
|
|
410
|
+
},
|
|
411
|
+
/** Highlights and shadows: two percents, each -100..100 (0 unchanged). */
|
|
412
|
+
highlightsShadows(highlights, shadows) {
|
|
413
|
+
return `highlights_shadows(${num(floatIn("highlights_shadows highlights", highlights, -100, 100))},${num(floatIn("highlights_shadows shadows", shadows, -100, 100))})`;
|
|
414
|
+
},
|
|
415
|
+
/** Vibrance percent: -100..100 (0 unchanged; muted colors move most, vivid colors are protected). */
|
|
416
|
+
vibrance(percent) {
|
|
417
|
+
return `vibrance(${num(floatIn("vibrance", percent, -100, 100))})`;
|
|
418
|
+
},
|
|
261
419
|
/** Rotate the hue by integer degrees. */
|
|
262
420
|
hueRotate(degrees) {
|
|
263
421
|
return `huerotate(${intIn("huerotate degrees", degrees, -2147483648, 2147483647)})`;
|
|
@@ -278,7 +436,7 @@ export const filter = {
|
|
|
278
436
|
* nearest end value.
|
|
279
437
|
*/
|
|
280
438
|
curve(channel, points) {
|
|
281
|
-
if (!CURVE_CHANNELS
|
|
439
|
+
if (!isOneOf(CURVE_CHANNELS, channel)) {
|
|
282
440
|
fail("curve channel", channel, `one of ${CURVE_CHANNELS.join(", ")}`);
|
|
283
441
|
}
|
|
284
442
|
if (points.length < 2 || points.length > 16) {
|
|
@@ -312,7 +470,7 @@ export const filter = {
|
|
|
312
470
|
}
|
|
313
471
|
for (const v of values) {
|
|
314
472
|
floatIn("convolution kernel value", v, -1024, 1024);
|
|
315
|
-
if (!/^-?\d+(\.\d+)?$/.test(
|
|
473
|
+
if (!/^-?\d+(\.\d+)?$/.test(kernelNum(v))) {
|
|
316
474
|
fail("convolution kernel value", v, "a plain decimal (the grammar takes no exponent forms)");
|
|
317
475
|
}
|
|
318
476
|
}
|
|
@@ -323,7 +481,7 @@ export const filter = {
|
|
|
323
481
|
fail("convolution normalize", normalize, "a boolean or 0/1");
|
|
324
482
|
}
|
|
325
483
|
const flag = normalize === true || normalize === 1 ? 1 : 0;
|
|
326
|
-
return `convolution(${values.map(
|
|
484
|
+
return `convolution(${values.map(kernelNum).join(",")},${flag})`;
|
|
327
485
|
},
|
|
328
486
|
/**
|
|
329
487
|
* Film grain: one noise value per pixel, added equally
|
|
@@ -352,17 +510,102 @@ export const filter = {
|
|
|
352
510
|
trim(tolerance = 10) {
|
|
353
511
|
return `trim(${intIn("trim tolerance", tolerance, 0, 255)})`;
|
|
354
512
|
},
|
|
513
|
+
/** Trim uniform borders, then remove pixels per side (left, top, right, bottom). */
|
|
514
|
+
trimSides(tolerance, left, top, right, bottom) {
|
|
515
|
+
const side = (what, v) => intIn(what, v, 0, 4294967295);
|
|
516
|
+
return `trim(${intIn("trim tolerance", tolerance, 0, 255)},${side("trim left", left)},${side("trim top", top)},${side("trim right", right)},${side("trim bottom", bottom)})`;
|
|
517
|
+
},
|
|
355
518
|
/** Encoding quality 1..100, or `"auto"` for perceptual targeting. */
|
|
356
519
|
quality(value) {
|
|
357
520
|
if (value === "auto")
|
|
358
521
|
return "quality(auto)";
|
|
359
522
|
return `quality(${intIn("quality", value, 1, 100)})`;
|
|
360
523
|
},
|
|
361
|
-
|
|
362
|
-
|
|
524
|
+
/**
|
|
525
|
+
* Per-format quality in one URL: a map of format to value
|
|
526
|
+
* (1..100 or "auto"), plus an optional fallback for
|
|
527
|
+
* formats with no entry. Resolved by the server after
|
|
528
|
+
* format negotiation; entries spell in canonical
|
|
529
|
+
* (format-name) order with the fallback first.
|
|
530
|
+
*/
|
|
531
|
+
qualityPerFormat(entries, fallback) {
|
|
532
|
+
const names = Object.keys(entries);
|
|
533
|
+
if (names.length === 0 && fallback === undefined) {
|
|
534
|
+
fail("quality", "{}", "at least one entry or a fallback");
|
|
535
|
+
}
|
|
536
|
+
const parts = [];
|
|
537
|
+
if (fallback !== undefined) {
|
|
538
|
+
parts.push(fallback === "auto" ? "auto" : String(intIn("quality", fallback, 1, 100)));
|
|
539
|
+
}
|
|
540
|
+
for (const name of names.sort()) {
|
|
541
|
+
if (!isOneOf(QUALITY_FORMATS, name)) {
|
|
542
|
+
fail("quality", name, `one of ${QUALITY_FORMATS.join(", ")}`);
|
|
543
|
+
}
|
|
544
|
+
const value = entries[name];
|
|
545
|
+
parts.push(`${name}:${value === "auto" ? "auto" : intIn("quality", value, 1, 100)}`);
|
|
546
|
+
}
|
|
547
|
+
return `quality(${parts.join(",")})`;
|
|
548
|
+
},
|
|
549
|
+
/** Indexed PNG output with at most this many colors, 2..256; other formats ignore it. */
|
|
550
|
+
palette(colors) {
|
|
551
|
+
return `palette(${intIn("palette colors", colors, 2, 256)})`;
|
|
552
|
+
},
|
|
553
|
+
/**
|
|
554
|
+
* Output format, with its encoder options (issue #319):
|
|
555
|
+
* `progressive` and `subsampling` (444, 422, or 420) for
|
|
556
|
+
* JPEG, `interlace` for PNG, `method` (0..6) and `sns`
|
|
557
|
+
* (smart subsampling, 0..100) for WebP. An option its
|
|
558
|
+
* format cannot honour throws here, like the server's 400.
|
|
559
|
+
*/
|
|
560
|
+
format(format, options = {}) {
|
|
561
|
+
if (!isOneOf(IMAGE_FORMATS, format)) {
|
|
363
562
|
fail("format", format, `one of ${IMAGE_FORMATS.join(", ")}`);
|
|
364
563
|
}
|
|
365
|
-
|
|
564
|
+
// The output segment is the canonical spelling: the
|
|
565
|
+
// `jpg` input alias spells as `jpeg`, like the server's
|
|
566
|
+
// canonical form.
|
|
567
|
+
const canonical = format === "jpg" ? "jpeg" : format;
|
|
568
|
+
const args = [canonical];
|
|
569
|
+
const used = (key) => options[key] !== undefined;
|
|
570
|
+
if (format === "jpeg" || format === "jpg") {
|
|
571
|
+
if (used("interlace") || used("method") || used("sns")) {
|
|
572
|
+
fail("format", JSON.stringify(options), "jpeg takes progressive and subsampling only");
|
|
573
|
+
}
|
|
574
|
+
if (options.progressive)
|
|
575
|
+
args.push("progressive");
|
|
576
|
+
if (options.subsampling !== undefined) {
|
|
577
|
+
if (!isOneOf(JPEG_SUBSAMPLINGS, options.subsampling)) {
|
|
578
|
+
fail("subsampling", options.subsampling, `one of ${JPEG_SUBSAMPLINGS.join(", ")}`);
|
|
579
|
+
}
|
|
580
|
+
args.push(`subsampling:${options.subsampling}`);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
else if (format === "png") {
|
|
584
|
+
if (used("progressive") || used("subsampling") || used("method") || used("sns")) {
|
|
585
|
+
fail("format", JSON.stringify(options), "png takes interlace only");
|
|
586
|
+
}
|
|
587
|
+
if (options.interlace)
|
|
588
|
+
args.push("interlace");
|
|
589
|
+
}
|
|
590
|
+
else if (format === "webp") {
|
|
591
|
+
if (used("progressive") || used("subsampling") || used("interlace")) {
|
|
592
|
+
fail("format", JSON.stringify(options), "webp takes method and sns only");
|
|
593
|
+
}
|
|
594
|
+
if (options.method !== undefined) {
|
|
595
|
+
args.push(`method:${intIn("method", options.method, 0, 6)}`);
|
|
596
|
+
}
|
|
597
|
+
if (options.sns !== undefined) {
|
|
598
|
+
args.push(`sns:${intIn("sns", options.sns, 0, 100)}`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (used("progressive") ||
|
|
602
|
+
used("subsampling") ||
|
|
603
|
+
used("interlace") ||
|
|
604
|
+
used("method") ||
|
|
605
|
+
used("sns")) {
|
|
606
|
+
fail("format", JSON.stringify(options), `${format} takes no encoder options`);
|
|
607
|
+
}
|
|
608
|
+
return `format(${args.join(",")})`;
|
|
366
609
|
},
|
|
367
610
|
/** The background color other operations pad with (RRGGBB). */
|
|
368
611
|
fill(color) {
|
|
@@ -372,17 +615,59 @@ export const filter = {
|
|
|
372
615
|
facePad(amount) {
|
|
373
616
|
return `facepad(${num(floatIn("facepad", amount, 0, 3))})`;
|
|
374
617
|
},
|
|
618
|
+
/**
|
|
619
|
+
* Studio mode: `studio(product)` composites the subject
|
|
620
|
+
* the server's segmentation model finds onto a backdrop
|
|
621
|
+
* with a synthesized contact shadow, scaled and centered
|
|
622
|
+
* to the pad margin; `studio(scene)` keeps the
|
|
623
|
+
* photograph's scene, reframing it on the detected
|
|
624
|
+
* subject and correcting light and color. See
|
|
625
|
+
* {@link StudioOptions} for the named arguments.
|
|
626
|
+
* Arguments spelled at their defaults (shadow `soft`,
|
|
627
|
+
* pad 10) are omitted, matching the server's canonical
|
|
628
|
+
* spelling, so `studio("product", { pad: 10 })` and
|
|
629
|
+
* `studio("product")` are the same string.
|
|
630
|
+
*/
|
|
631
|
+
studio(mode, options = {}) {
|
|
632
|
+
if (!isOneOf(STUDIO_MODES, mode)) {
|
|
633
|
+
fail("studio mode", mode, `one of ${STUDIO_MODES.join(", ")}`);
|
|
634
|
+
}
|
|
635
|
+
const { bg, shadow, pad } = options;
|
|
636
|
+
if (mode === "scene" && (bg !== undefined || shadow !== undefined)) {
|
|
637
|
+
fail("studio scene options", options, "pad only: bg and shadow are product arguments");
|
|
638
|
+
}
|
|
639
|
+
const parts = [mode];
|
|
640
|
+
if (mode === "product") {
|
|
641
|
+
if (bg !== undefined) {
|
|
642
|
+
const value = isOneOf(STUDIO_BACKDROPS, bg) ? bg : hex6("studio bg", bg);
|
|
643
|
+
parts.push(`bg:${value}`);
|
|
644
|
+
}
|
|
645
|
+
if (shadow !== undefined) {
|
|
646
|
+
if (!isOneOf(STUDIO_SHADOWS, shadow)) {
|
|
647
|
+
fail("studio shadow", shadow, `one of ${STUDIO_SHADOWS.join(", ")}`);
|
|
648
|
+
}
|
|
649
|
+
if (shadow !== "soft")
|
|
650
|
+
parts.push(`shadow:${shadow}`);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (pad !== undefined) {
|
|
654
|
+
intIn("studio pad", pad, 0, 40);
|
|
655
|
+
if (pad !== 10)
|
|
656
|
+
parts.push(`pad:${pad}`);
|
|
657
|
+
}
|
|
658
|
+
return `studio(${parts.join(",")})`;
|
|
659
|
+
},
|
|
375
660
|
/** AI upscale by the given factor. */
|
|
376
661
|
upscale(factor) {
|
|
377
|
-
if (
|
|
378
|
-
fail("upscale factor", factor,
|
|
662
|
+
if (!isOneOf(UPSCALE_FACTORS, factor)) {
|
|
663
|
+
fail("upscale factor", factor, eitherOf(UPSCALE_FACTORS));
|
|
379
664
|
}
|
|
380
665
|
return `upscale(${factor})`;
|
|
381
666
|
},
|
|
382
667
|
/** Device pixel ratio the sizes are multiplied by. */
|
|
383
668
|
dpr(factor) {
|
|
384
|
-
if (!
|
|
385
|
-
fail("dpr", factor,
|
|
669
|
+
if (!isOneOf(DPR_FACTORS, factor)) {
|
|
670
|
+
fail("dpr", factor, eitherOf(DPR_FACTORS));
|
|
386
671
|
}
|
|
387
672
|
return `dpr(${num(factor)})`;
|
|
388
673
|
},
|
|
@@ -400,13 +685,13 @@ export const filter = {
|
|
|
400
685
|
intIn("radius", c, 0, MAX_DIMENSION);
|
|
401
686
|
return `radius(${corners.join(",")})`;
|
|
402
687
|
},
|
|
403
|
-
/** Paint an opaque border inside the frame (RRGGBB). */
|
|
688
|
+
/** Paint an opaque border inside the frame (RRGGBB); a width of 0 is a no-op. */
|
|
404
689
|
border(width, color) {
|
|
405
|
-
return `border(${intIn("border width", width,
|
|
690
|
+
return `border(${intIn("border width", width, 0, MAX_DIMENSION)},${hex6("border color", color)})`;
|
|
406
691
|
},
|
|
407
692
|
mask(shape) {
|
|
408
|
-
if (
|
|
409
|
-
fail("mask shape", shape,
|
|
693
|
+
if (!isOneOf(MASK_SHAPES, shape)) {
|
|
694
|
+
fail("mask shape", shape, eitherOf(MASK_SHAPES, true));
|
|
410
695
|
}
|
|
411
696
|
return `mask(${shape})`;
|
|
412
697
|
},
|
|
@@ -416,13 +701,13 @@ export const filter = {
|
|
|
416
701
|
monochrome(color) {
|
|
417
702
|
return `monochrome(${hex6("monochrome color", color)})`;
|
|
418
703
|
},
|
|
419
|
-
/** Mosaic block size in pixels: 1..
|
|
704
|
+
/** Mosaic block size in pixels: 1..{@link MAX_DIMENSION}. */
|
|
420
705
|
pixelate(blockSize) {
|
|
421
706
|
return `pixelate(${intIn("pixelate size", blockSize, 1, MAX_DIMENSION)})`;
|
|
422
707
|
},
|
|
423
708
|
flip(axis) {
|
|
424
|
-
if (
|
|
425
|
-
fail("flip axis", axis,
|
|
709
|
+
if (!isOneOf(FLIP_AXES, axis)) {
|
|
710
|
+
fail("flip axis", axis, eitherOf(FLIP_AXES, true));
|
|
426
711
|
}
|
|
427
712
|
return `flip(${axis})`;
|
|
428
713
|
},
|
|
@@ -444,7 +729,7 @@ export const filter = {
|
|
|
444
729
|
intIn("overlay x", x, -2147483648, 2147483647);
|
|
445
730
|
intIn("overlay y", y, -2147483648, 2147483647);
|
|
446
731
|
intIn("overlay alpha", alpha, 0, 100);
|
|
447
|
-
if (!BLEND_MODES
|
|
732
|
+
if (!isOneOf(BLEND_MODES, blend)) {
|
|
448
733
|
fail("overlay blend", blend, `one of ${BLEND_MODES.join(", ")}`);
|
|
449
734
|
}
|
|
450
735
|
if (url === "")
|
|
@@ -457,8 +742,8 @@ export const filter = {
|
|
|
457
742
|
* canonical spelling is always eight lowercase hex digits.
|
|
458
743
|
*/
|
|
459
744
|
text(content, size, color, anchor) {
|
|
460
|
-
if (!
|
|
461
|
-
fail("text anchor", anchor, `one of ${
|
|
745
|
+
if (!isOneOf(TEXT_ANCHORS, anchor)) {
|
|
746
|
+
fail("text anchor", anchor, `one of ${TEXT_ANCHORS.join(", ")}`);
|
|
462
747
|
}
|
|
463
748
|
if (content === "")
|
|
464
749
|
fail("text", content, "a non-empty label");
|
|
@@ -473,6 +758,10 @@ export const filter = {
|
|
|
473
758
|
pixelateClass(name) {
|
|
474
759
|
return `pixelateclass(${className("pixelateclass name", name)})`;
|
|
475
760
|
},
|
|
761
|
+
/** Recolor every detected object of the named class to the given RRGGBB color. */
|
|
762
|
+
recolorClass(name, color) {
|
|
763
|
+
return `recolorclass(${className("recolorclass name", name)},${hex6("recolorclass color", color)})`;
|
|
764
|
+
},
|
|
476
765
|
/** Blur the rectangle (x, y, w, h) in pixels. */
|
|
477
766
|
blurRegion(x, y, w, h) {
|
|
478
767
|
return `blur_region(${region(x, y, w, h)})`;
|
|
@@ -481,6 +770,10 @@ export const filter = {
|
|
|
481
770
|
pixelateRegion(x, y, w, h) {
|
|
482
771
|
return `pixelate_region(${region(x, y, w, h)})`;
|
|
483
772
|
},
|
|
773
|
+
/** Erase the rectangle (x, y, w, h) in pixels and inpaint it (AI). */
|
|
774
|
+
removeRegion(x, y, w, h) {
|
|
775
|
+
return `remove_region(${region(x, y, w, h)})`;
|
|
776
|
+
},
|
|
484
777
|
};
|
|
485
778
|
function region(x, y, w, h) {
|
|
486
779
|
intIn("region x", x, 0, MAX_DIMENSION);
|
|
@@ -13,18 +13,26 @@
|
|
|
13
13
|
"name": "@crabcrop/url",
|
|
14
14
|
"keywords": ["image", "url", "signing", "hmac", "cdn", "crabcrop"],
|
|
15
15
|
"files": ["dist"],
|
|
16
|
+
"imports": {
|
|
17
|
+
"#crypto": {
|
|
18
|
+
"browser": "./dist/crypto/portable.js",
|
|
19
|
+
"node": "./dist/crypto/node.js",
|
|
20
|
+
"default": "./dist/crypto/portable.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
16
23
|
"publishConfig": { "access": "public" },
|
|
17
24
|
"repository": { "type": "git", "url": "git+https://github.com/dayvson/crabcrop.git", "directory": "sdks/typescript" },
|
|
18
25
|
"scripts": {
|
|
19
|
-
"build": "tsc",
|
|
26
|
+
"build": "tsc && tsc -p tsconfig.browser.json",
|
|
20
27
|
"prepack": "npm run build",
|
|
21
|
-
"
|
|
28
|
+
"pretest": "npm run build && tsc -p tsconfig.test.json",
|
|
29
|
+
"test": "node --test && node --conditions=browser --test"
|
|
22
30
|
},
|
|
23
31
|
"type": "module",
|
|
24
|
-
"version": "0.
|
|
32
|
+
"version": "0.10.0",
|
|
25
33
|
"devDependencies": {
|
|
26
34
|
"@types/node": "^26.6.5",
|
|
27
|
-
"typescript": "^
|
|
35
|
+
"typescript": "^7.0.2"
|
|
28
36
|
},
|
|
29
37
|
"main": "./dist/index.js",
|
|
30
38
|
"types": "./dist/index.d.ts"
|
package/package.json
CHANGED
package/server.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"$schema":"https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json","description":"Sign and explain crabcrop image URLs, fetch processed-image metadata, and purge cached results.","name":"io.github.crabcrop/crabcrop","packages":[{"environmentVariables":[{"description":"Base URL of the crabcrop server the tools act on (for example https://images.example.com).","format":"string","isRequired":false,"isSecret":false,"name":"CRABCROP_BASE"},{"description":"The server's signing secret, used by sign_url and purge_result. Keep it in the environment, not in tool arguments.","format":"string","isRequired":false,"isSecret":true,"name":"CRABCROP_SECRET"}],"identifier":"@crabcrop/mcp","registryBaseUrl":"https://registry.npmjs.org","registryType":"npm","runTimeHint":"npx","transport":{"type":"stdio"},"version":"0.
|
|
1
|
+
{"$schema":"https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json","description":"Sign and explain crabcrop image URLs, fetch processed-image metadata, and purge cached results.","name":"io.github.crabcrop/crabcrop","packages":[{"environmentVariables":[{"description":"Base URL of the crabcrop server the tools act on (for example https://images.example.com).","format":"string","isRequired":false,"isSecret":false,"name":"CRABCROP_BASE"},{"description":"The server's signing secret, used by sign_url and purge_result. Keep it in the environment, not in tool arguments.","format":"string","isRequired":false,"isSecret":true,"name":"CRABCROP_SECRET"}],"identifier":"@crabcrop/mcp","registryBaseUrl":"https://registry.npmjs.org","registryType":"npm","runTimeHint":"npx","transport":{"type":"stdio"},"version":"0.10.0"}],"repository":{"source":"github","url":"https://github.com/dayvson/crabcrop"},"version":"0.10.0"}
|