@crabcrop/url 0.9.6 → 0.9.7

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/dist/explain.js CHANGED
@@ -13,9 +13,11 @@ export const FILTER_DESCRIPTIONS = {
13
13
  blurfaces: "blur detected faces (needs a face model on the server)",
14
14
  brighten: "brighten by the given amount (-255..255)",
15
15
  contrast: "contrast in percent, -100..100 (0 is unchanged, -100 is flat)",
16
+ gamma: "gamma correction with divisor g, 0 < g <= 10: each channel becomes 255 * (v / 255)^(1 / g) (1 is unchanged, above 1 brightens midtones)",
16
17
  huerotate: "rotate the hue by the given degrees",
17
18
  rotate: "rotate by the given angle in degrees; the canvas expands to fit",
18
19
  quality: "output quality 1..100, or `auto` for perceptual targeting",
20
+ palette: "write PNG output as an indexed (palette) image with at most this many colors, 2..256; ignored by the other formats",
19
21
  format: "force the output format",
20
22
  fill: "the background color other operations pad with",
21
23
  facepad: "how much a face crop expands around the face",
@@ -35,7 +37,7 @@ export const FILTER_DESCRIPTIONS = {
35
37
  curve: "tone curve: remap the named channel (rgb, r, g, or b) through 2 to 16 x,y control points, linearly interpolated, ends clamped",
36
38
  convolution: "convolve with a 3x3 or 5x5 kernel: 9 or 25 row-major values (plain decimals, -1024..1024), then a normalize flag (0 or 1); edges replicate",
37
39
  noise: "add monochrome film grain: one deterministic noise value per pixel on all channels, strength 0..100 percent of full scale (0 is a no-op)",
38
- trim: "trim uniform borders",
40
+ trim: "trim uniform borders; the five-argument form (tolerance,left,top,right,bottom) also removes that many pixels per side after the tolerance pass",
39
41
  dpr: "device pixel ratio the sizes are multiplied by",
40
42
  watermark: "overlay a watermark image",
41
43
  max_bytes: "cap the output size, lowering quality to fit",
@@ -166,8 +168,18 @@ function validateFilter(name, args) {
166
168
  return args === null ? null : bad;
167
169
  }
168
170
  switch (name) {
169
- case "trim":
170
- return args === null || isIntIn(args, 0, 255) ? null : bad;
171
+ case "trim": {
172
+ if (args === null)
173
+ return null;
174
+ const parts = args.split(",");
175
+ if (parts.length === 1)
176
+ return isIntIn(parts[0], 0, 255) ? null : bad;
177
+ return parts.length === 5 &&
178
+ isIntIn(parts[0], 0, 255) &&
179
+ parts.slice(1).every((p) => isIntIn(p, 0, 4294967295))
180
+ ? null
181
+ : bad;
182
+ }
171
183
  case "blur":
172
184
  case "sharpen":
173
185
  return args !== null && isFloat(args) && Number(args) > 0 ? null : bad;
@@ -179,12 +191,18 @@ function validateFilter(name, args) {
179
191
  return args !== null && isFloat(args) && Number(args) >= -100 && Number(args) <= 100
180
192
  ? null
181
193
  : bad;
194
+ case "gamma":
195
+ return args !== null && isFloat(args) && Number(args) > 0 && Number(args) <= 10
196
+ ? null
197
+ : bad;
182
198
  case "saturation":
183
199
  return args !== null && isFloat(args) && Number(args) >= 0 && Number(args) <= 255
184
200
  ? null
185
201
  : bad;
186
202
  case "quality":
187
203
  return args === "auto" || (args !== null && isIntIn(args, 1, 100)) ? null : bad;
204
+ case "palette":
205
+ return args !== null && isIntIn(args, 2, 256) ? null : bad;
188
206
  case "format":
189
207
  return args !== null && FORMAT_NAMES.has(args) ? null : bad;
190
208
  case "rgb": {
@@ -599,6 +617,34 @@ function explainSegment(seg, nowSeconds) {
599
617
  }
600
618
  return { kind: "operation", title: "Focal rectangle", detail: `set the focal rectangle ${rest} for the next crop (left x top : right x bottom)` };
601
619
  }
620
+ if (seg.startsWith("focalpoint:")) {
621
+ const rest = seg.slice(11);
622
+ const colon = rest.indexOf(":");
623
+ const point = colon === -1 ? rest : rest.slice(0, colon);
624
+ const zoomText = colon === -1 ? null : rest.slice(colon + 1);
625
+ const xy = point.split("x");
626
+ const ok = xy.length === 2 &&
627
+ isFloat(xy[0]) &&
628
+ isFloat(xy[1]) &&
629
+ Number(xy[0]) >= 0 &&
630
+ Number(xy[0]) <= 1 &&
631
+ Number(xy[1]) >= 0 &&
632
+ Number(xy[1]) <= 1 &&
633
+ (zoomText === null ||
634
+ (isFloat(zoomText) && Number(zoomText) > 0 && Number(zoomText) <= 100));
635
+ if (!ok) {
636
+ return {
637
+ kind: "operation",
638
+ title: "Focal point",
639
+ error: `malformed focalpoint segment: ${seg} (want focalpoint:<x>x<y>[:<zoom>] with x and y in 0..1 and zoom in (0, 100])`,
640
+ };
641
+ }
642
+ return {
643
+ kind: "operation",
644
+ title: "Focal point",
645
+ detail: `set the focal point ${point} at zoom ${zoomText ?? "1"} for the next crop (fractions of the image dimensions; zoom above 1 tightens the crop around the point)`,
646
+ };
647
+ }
602
648
  if (seg.startsWith("crop:")) {
603
649
  const rest = seg.slice(5);
604
650
  const idx = rest.indexOf(":");
@@ -75,6 +75,8 @@ export declare const ops: {
75
75
  cropRect(left: number, top: number, right: number, bottom: number): string;
76
76
  /** `focal:10x20:110x220` (left x top : right x bottom); steers the next crop. */
77
77
  focal(left: number, top: number, right: number, bottom: number): string;
78
+ /** `focalpoint:0.75x0.4:2` (fractions of the image dimensions, zoom optional); steers the next crop. */
79
+ focalPoint(x: number, y: number, zoom?: number): string;
78
80
  /** `pad:300x200`, optionally `:blur` or a RRGGBB fill color. */
79
81
  pad(width: number, height: number, fill?: "blur" | string): string;
80
82
  /** `preset:thumb`; expands server-side. */
@@ -120,6 +122,8 @@ export declare const filter: {
120
122
  brighten(amount: number): string;
121
123
  /** Percent contrast: -100..100 (0 unchanged, -100 flat). */
122
124
  contrast(percent: number): string;
125
+ /** Gamma correction divisor: 0 < g <= 10 (1 unchanged; above 1 brightens midtones). */
126
+ gamma(value: number): string;
123
127
  /** Rotate the hue by integer degrees. */
124
128
  hueRotate(degrees: number): string;
125
129
  /** Saturation factor: 0..255 (0 grayscale, 1 identity). */
@@ -161,8 +165,12 @@ export declare const filter: {
161
165
  rotate(angle: number): string;
162
166
  /** Trim uniform borders; tolerance 0..255, default 10. */
163
167
  trim(tolerance?: number): string;
168
+ /** Trim uniform borders, then remove pixels per side (left, top, right, bottom). */
169
+ trimSides(tolerance: number, left: number, top: number, right: number, bottom: number): string;
164
170
  /** Encoding quality 1..100, or `"auto"` for perceptual targeting. */
165
171
  quality(value: number | "auto"): string;
172
+ /** Indexed PNG output with at most this many colors, 2..256; other formats ignore it. */
173
+ palette(colors: number): string;
166
174
  format(format: ImageFormat): string;
167
175
  /** The background color other operations pad with (RRGGBB). */
168
176
  fill(color: string): string;
@@ -169,6 +169,19 @@ export const ops = {
169
169
  }
170
170
  return `focal:${left}x${top}:${right}x${bottom}`;
171
171
  },
172
+ /** `focalpoint:0.75x0.4:2` (fractions of the image dimensions, zoom optional); steers the next crop. */
173
+ focalPoint(x, y, zoom = 1) {
174
+ const frac = (what, v) => {
175
+ if (!Number.isFinite(v) || v < 0 || v > 1) {
176
+ fail(what, v, "a fraction from 0 to 1");
177
+ }
178
+ return v;
179
+ };
180
+ if (!Number.isFinite(zoom) || zoom <= 0 || zoom > 100) {
181
+ fail("focalpoint zoom", zoom, "a number above 0 and at most 100");
182
+ }
183
+ return `focalpoint:${num(frac("focalpoint x", x))}x${num(frac("focalpoint y", y))}:${num(zoom)}`;
184
+ },
172
185
  /** `pad:300x200`, optionally `:blur` or a RRGGBB fill color. */
173
186
  pad(width, height, fill) {
174
187
  const box = pixelBox("pad", width, height);
@@ -267,6 +280,13 @@ export const filter = {
267
280
  contrast(percent) {
268
281
  return `contrast(${num(floatIn("contrast", percent, -100, 100))})`;
269
282
  },
283
+ /** Gamma correction divisor: 0 < g <= 10 (1 unchanged; above 1 brightens midtones). */
284
+ gamma(value) {
285
+ if (!Number.isFinite(value) || value <= 0 || value > 10) {
286
+ fail("gamma", value, "a number above 0 and at most 10");
287
+ }
288
+ return `gamma(${num(value)})`;
289
+ },
270
290
  /** Rotate the hue by integer degrees. */
271
291
  hueRotate(degrees) {
272
292
  return `huerotate(${intIn("huerotate degrees", degrees, -2147483648, 2147483647)})`;
@@ -361,12 +381,21 @@ export const filter = {
361
381
  trim(tolerance = 10) {
362
382
  return `trim(${intIn("trim tolerance", tolerance, 0, 255)})`;
363
383
  },
384
+ /** Trim uniform borders, then remove pixels per side (left, top, right, bottom). */
385
+ trimSides(tolerance, left, top, right, bottom) {
386
+ const side = (what, v) => intIn(what, v, 0, 4294967295);
387
+ return `trim(${intIn("trim tolerance", tolerance, 0, 255)},${side("trim left", left)},${side("trim top", top)},${side("trim right", right)},${side("trim bottom", bottom)})`;
388
+ },
364
389
  /** Encoding quality 1..100, or `"auto"` for perceptual targeting. */
365
390
  quality(value) {
366
391
  if (value === "auto")
367
392
  return "quality(auto)";
368
393
  return `quality(${intIn("quality", value, 1, 100)})`;
369
394
  },
395
+ /** Indexed PNG output with at most this many colors, 2..256; other formats ignore it. */
396
+ palette(colors) {
397
+ return `palette(${intIn("palette colors", colors, 2, 256)})`;
398
+ },
370
399
  format(format) {
371
400
  if (!IMAGE_FORMATS.includes(format)) {
372
401
  fail("format", format, `one of ${IMAGE_FORMATS.join(", ")}`);
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "test": "node --test"
22
22
  },
23
23
  "type": "module",
24
- "version": "0.9.6",
24
+ "version": "0.9.7",
25
25
  "devDependencies": {
26
26
  "@types/node": "^26.6.5",
27
27
  "typescript": "^7.0.2"