imagescript 3.20.2 → 4.0.0-rc.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/README.md +21 -12
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +24 -26
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +274 -110
- package/dist/codecs/gif.js +11 -9
- package/dist/codecs/hdr.d.ts +1 -1
- package/dist/codecs/jpeg.js +13 -13
- package/dist/codecs/jxl/headers.js +2 -2
- package/dist/codecs/jxl.js +5 -5
- package/dist/codecs/netpbm.js +7 -7
- package/dist/codecs/png.js +15 -15
- package/dist/codecs/quantize.js +34 -8
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/webp.js +3 -3
- package/dist/colors.d.ts +8 -7
- package/dist/colors.js +13 -3
- package/dist/frame.d.ts +93 -102
- package/dist/frame.js +89 -62
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +73 -49
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +287 -732
- package/dist/image.js +255 -404
- package/dist/index.d.ts +15 -25
- package/dist/index.js +15 -14
- package/dist/render/svg.js +7 -2
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +9 -9
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/options.js +20 -6
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import { BinaryData } from './util/mem.js';
|
|
2
2
|
import { Image, DecodeOptions } from './image.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export { Image, ResizeModes, SVGScaleModes } from './image.js';
|
|
3
|
+
import { Animation } from './animation.js';
|
|
4
|
+
export { Image } from './image.js';
|
|
6
5
|
export { Colors } from './colors.js';
|
|
7
|
-
export type { Color, ColorFunction, ResizeMode,
|
|
8
|
-
export { Frame
|
|
9
|
-
export type {
|
|
6
|
+
export type { Color, ColorFunction, ResizeMode, ResizeFit, TextStyleOptions, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, TGAEncodeOptions, GIFEncodeOptions, DitherMethod, GainMapEmbedOptions, RenderSVGOptions, RenderTextOptions, ShadowOptions, OutlineOptions, QuantizeOptions, TransformOptions, TransformMode, AffineTransform, GradientOptions, LinearGradientOptions, RadialGradientOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
7
|
+
export { Frame } from './frame.js';
|
|
8
|
+
export type { FrameDisposalMode, FrameOptions } from './frame.js';
|
|
10
9
|
export { GainMap } from './gainmap.js';
|
|
11
|
-
export type { GainMapOptions, GainMapEncodeOptions } from './gainmap.js';
|
|
12
|
-
export { GIF } from './gif.js';
|
|
10
|
+
export type { GainMapOptions, GainMapEncodeOptions, GainMapChannelValues } from './gainmap.js';
|
|
13
11
|
export { Animation } from './animation.js';
|
|
14
|
-
export type { AnimationOptions,
|
|
12
|
+
export type { AnimationOptions, AnimationCanvasOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFAnimationEncodeOptions, APNGEncodeOptions } from './animation.js';
|
|
15
13
|
export { AnimationEncoder } from './animation-encoder.js';
|
|
16
|
-
export type { AnimationEncoderOptions, WebPAnimationEncoderOptions, GIFAnimationEncoderOptions, APNGAnimationEncoderOptions } from './animation-encoder.js';
|
|
17
|
-
export { TextLayout } from './text-layout.js';
|
|
18
14
|
export type { TextLayoutOptions, WrapStyle, HorizontalAlign, VerticalAlign } from './text-layout.js';
|
|
19
15
|
export { ImageType } from './image-type.js';
|
|
20
16
|
export type { ImageTypeName } from './image-type.js';
|
|
@@ -22,18 +18,12 @@ export type { ToneMapping } from './codecs/hdr.js';
|
|
|
22
18
|
export type { BinaryData } from './util/mem.js';
|
|
23
19
|
export type { Metadata } from './metadata.js';
|
|
24
20
|
/**
|
|
25
|
-
* Decodes any supported image
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
21
|
+
* Decodes any supported image. A file with more than one frame or an explicit animation marker (including a one-frame
|
|
22
|
+
* APNG or animated WebP) is returned as an {@link Animation}; otherwise it is an {@link Image}. A one-frame animation
|
|
23
|
+
* keeps its timing, offsets and loop metadata. Static formats (PNG, JPEG, AVIF, ...) are Images, and AVIF image
|
|
24
|
+
* sequences decode to their first frame, as {@link Image.decode} does. The options are passed on to
|
|
25
|
+
* {@link Animation.decode} or {@link Image.decode}.
|
|
26
|
+
* @param input The image data
|
|
27
|
+
* @param options See {@link DecodeOptions}
|
|
30
28
|
*/
|
|
31
|
-
export declare function decode(
|
|
32
|
-
/**
|
|
33
|
-
* Decodes any supported image: GIFs are returned as {@link GIF} (an {@link Animation}), animated WebPs as
|
|
34
|
-
* {@link Animation}, everything else (including APNGs, as their default image) as {@link Image}.
|
|
35
|
-
* {@link Animation.decode} decodes the frames of GIFs, APNGs and WebPs
|
|
36
|
-
* @param data The image data
|
|
37
|
-
* @param onlyExtractFirstFrame Whether to end animation decoding after the first frame
|
|
38
|
-
*/
|
|
39
|
-
export declare function decode(data: BinaryData, onlyExtractFirstFrame?: boolean): Promise<GIF | Animation | Image>;
|
|
29
|
+
export declare function decode(input: BinaryData, options?: DecodeOptions): Promise<Image | Animation>;
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import { Image } from "./image.js";
|
|
2
|
-
import { GIF } from "./gif.js";
|
|
3
2
|
import { Animation } from "./animation.js";
|
|
4
|
-
import {
|
|
3
|
+
import { checkOptions } from "./util/options.js";
|
|
4
|
+
import * as png from "./codecs/png.js";
|
|
5
5
|
import * as webp from "./codecs/webp.js";
|
|
6
6
|
import { view } from "./util/mem.js";
|
|
7
7
|
import { ImageType } from "./image-type.js";
|
|
8
|
-
export { Image
|
|
8
|
+
export { Image } from "./image.js";
|
|
9
9
|
export { Colors } from "./colors.js";
|
|
10
|
-
export { Frame
|
|
10
|
+
export { Frame } from "./frame.js";
|
|
11
11
|
export { GainMap } from "./gainmap.js";
|
|
12
|
-
export { GIF } from "./gif.js";
|
|
13
12
|
export { Animation } from "./animation.js";
|
|
14
13
|
export { AnimationEncoder } from "./animation-encoder.js";
|
|
15
|
-
export { TextLayout } from "./text-layout.js";
|
|
16
14
|
export { ImageType } from "./image-type.js";
|
|
17
|
-
export function decode(
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
return
|
|
24
|
-
|
|
15
|
+
export async function decode(input, options = {}) {
|
|
16
|
+
checkOptions(options, 'decode(data, { maxPixels: 1e8 })');
|
|
17
|
+
const bytes = view(input);
|
|
18
|
+
const type = ImageType.getType(bytes);
|
|
19
|
+
const animated = type === 'gif' || type === 'jxl' || (type === 'webp' && webp.isAnimated(bytes)) || (type === 'png' && png.isAnimated(bytes));
|
|
20
|
+
if (!animated)
|
|
21
|
+
return Image.decode(input, options);
|
|
22
|
+
const animation = await Animation.decode(input, options);
|
|
23
|
+
if (animation.length !== 1 || Animation.__is_animated__(animation))
|
|
24
|
+
return animation;
|
|
25
|
+
return animation.frames[0].image;
|
|
25
26
|
}
|
package/dist/render/svg.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* <style> sheets (type, class and id selectors), solid colors, linear and radial gradients, patterns, strokes
|
|
5
5
|
* (joins, caps, dashes), markers, opacity, clip paths, luminance masks, images in data URIs (rasters decoded by
|
|
6
6
|
* the caller, SVGs and SVGZs) and filters (all primitives, see filter.ts).
|
|
7
|
-
* Not supported:
|
|
7
|
+
* Text is supported with supplied or embedded fonts. Not supported: animation, scripting, foreignObject or automatic
|
|
8
|
+
* loading of external files and URLs; images can be embedded as data URIs or supplied by the caller.
|
|
8
9
|
* How patterns are rendered (a tile in device resolution, its filtering) and their reference cycles broken, and how
|
|
9
10
|
* images are fitted and sampled follows resvg/usvg, the markers (their placement and orientation,
|
|
10
11
|
* crates/usvg/src/parser/marker.rs) are ported from usvg (https://github.com/linebender/resvg; Copyright 2017 the
|
|
@@ -20,6 +21,7 @@ import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLe
|
|
|
20
21
|
import { rasterizeGlyph } from "./text.js";
|
|
21
22
|
import { renderPaintGlyph } from "./colr.js";
|
|
22
23
|
import { decodeSync as decodePNG } from "../codecs/png.js";
|
|
24
|
+
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
23
25
|
const UNSUPPORTED = new Set(['filter', 'pattern', 'marker', 'foreignObject', 'script', 'style', 'title', 'desc', 'metadata',
|
|
24
26
|
'defs', 'clipPath', 'mask', 'linearGradient', 'radialGradient', 'stop', 'symbol', 'font', 'font-face', 'animate', 'animateTransform', 'animateMotion', 'set']);
|
|
25
27
|
const INHERITED = new Set(['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-width', 'stroke-opacity', 'stroke-linecap', 'stroke-linejoin',
|
|
@@ -51,7 +53,7 @@ const DEFAULTS = {
|
|
|
51
53
|
};
|
|
52
54
|
const SAMPLES = 16;
|
|
53
55
|
const OFFSETS = Float64Array.from({ length: SAMPLES }, (_, s) => (s + 0.5) / SAMPLES);
|
|
54
|
-
export function render(source, mode, size, images = new Map(), fonts = []) {
|
|
56
|
+
export function render(source, mode, size, images = new Map(), fonts = [], maxPixels = MAX_PIXELS) {
|
|
55
57
|
if (typeof source !== 'string')
|
|
56
58
|
source = decodeUTF8(source);
|
|
57
59
|
const root = parseXML(source).children.find(node => node.name === 'svg' && node.svg);
|
|
@@ -78,6 +80,9 @@ export function render(source, mode, size, images = new Map(), fonts = []) {
|
|
|
78
80
|
}
|
|
79
81
|
outWidth = Math.max(1, outWidth);
|
|
80
82
|
outHeight = Math.max(1, outHeight);
|
|
83
|
+
if (!Number.isSafeInteger(outWidth) || !Number.isSafeInteger(outHeight))
|
|
84
|
+
throw new RangeError(`SVG output dimensions are too large: ${outWidth}x${outHeight}`);
|
|
85
|
+
checkPixels(outWidth, outHeight, maxPixels);
|
|
81
86
|
const canvas = new Layer(outWidth, outHeight);
|
|
82
87
|
doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
|
|
83
88
|
return { width: outWidth, height: outHeight, pixels: canvas.toRGBA() };
|
package/dist/text-layout.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type WrapStyle = 'word' | 'char';
|
|
|
4
4
|
export type HorizontalAlign = 'left' | 'center' | 'right';
|
|
5
5
|
/** Which lines to keep when the text is cut off at `maxHeight` */
|
|
6
6
|
export type VerticalAlign = 'top' | 'middle' | 'bottom';
|
|
7
|
-
/**
|
|
7
|
+
/** The layout options of {@link Image.renderText} (part of {@link RenderTextOptions}): wrapping, cutting off and alignment */
|
|
8
8
|
export interface TextLayoutOptions {
|
|
9
9
|
/** Longer lines are wrapped. Default: `Infinity` */
|
|
10
10
|
maxWidth?: number;
|
|
@@ -12,40 +12,10 @@ export interface TextLayoutOptions {
|
|
|
12
12
|
maxHeight?: number;
|
|
13
13
|
/** Whether to wrap between words or at any character. Default: `'word'` */
|
|
14
14
|
wrapStyle?: WrapStyle;
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
horizontalAlign?: HorizontalAlign | VerticalAlign;
|
|
21
|
-
/**
|
|
22
|
-
* Which lines to keep when cutting off at `maxHeight`. Default: `'top'`
|
|
23
|
-
* (before 3.0 this was called horizontalAlign, the values of the other axis (`'left'`, `'center'`, `'right'`) are
|
|
24
|
-
* accepted for compatibility, which will be removed in 4.0)
|
|
25
|
-
*/
|
|
26
|
-
verticalAlign?: VerticalAlign | HorizontalAlign;
|
|
15
|
+
/** Alignment of the lines relative to the widest line: `'left'`, `'center'` or `'right'`. Default: `'left'` */
|
|
16
|
+
horizontalAlign?: HorizontalAlign;
|
|
17
|
+
/** Which lines to keep when cutting off at `maxHeight`: `'top'`, `'middle'` or `'bottom'`. Default: `'top'` */
|
|
18
|
+
verticalAlign?: VerticalAlign;
|
|
27
19
|
/** Whether to break lines at newline characters. Default: `true` */
|
|
28
20
|
wrapHardBreaks?: boolean;
|
|
29
21
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Layout options for {@link Image.renderText}
|
|
32
|
-
*/
|
|
33
|
-
export declare class TextLayout {
|
|
34
|
-
/** Longer lines are wrapped */
|
|
35
|
-
maxWidth: number;
|
|
36
|
-
/** Lines that don't fit are cut off */
|
|
37
|
-
maxHeight: number;
|
|
38
|
-
/** Whether to wrap between words or at any character */
|
|
39
|
-
wrapStyle: WrapStyle;
|
|
40
|
-
/** Alignment of the lines relative to the widest line */
|
|
41
|
-
horizontalAlign: HorizontalAlign;
|
|
42
|
-
/** Which lines to keep when cutting off at `maxHeight` */
|
|
43
|
-
verticalAlign: VerticalAlign;
|
|
44
|
-
/** Whether to break lines at newline characters */
|
|
45
|
-
wrapHardBreaks: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Creates a layout, missing options take their defaults
|
|
48
|
-
* @param options See {@link TextLayoutOptions}
|
|
49
|
-
*/
|
|
50
|
-
constructor(options?: TextLayoutOptions);
|
|
51
|
-
}
|
package/dist/text-layout.js
CHANGED
|
@@ -18,15 +18,15 @@ export class TextLayout {
|
|
|
18
18
|
this.wrapStyle = wrapStyle || 'word';
|
|
19
19
|
if (!['word', 'char'].includes(this.wrapStyle))
|
|
20
20
|
throw new RangeError('Invalid wrapStyle');
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
if (horizontalAlign !== undefined) {
|
|
22
|
+
if (!HORIZONTAL.includes(horizontalAlign))
|
|
23
|
+
throw new TypeError(`horizontalAlign must be 'left', 'center' or 'right', not '${horizontalAlign}'`);
|
|
24
|
+
this.horizontalAlign = horizontalAlign;
|
|
25
|
+
}
|
|
26
|
+
if (verticalAlign !== undefined) {
|
|
27
|
+
if (!VERTICAL.includes(verticalAlign))
|
|
28
|
+
throw new TypeError(`verticalAlign must be 'top', 'middle' or 'bottom', not '${verticalAlign}'`);
|
|
29
|
+
this.verticalAlign = verticalAlign;
|
|
30
30
|
}
|
|
31
31
|
this.wrapHardBreaks = typeof wrapHardBreaks === 'undefined' ? true : wrapHardBreaks;
|
|
32
32
|
if (typeof this.wrapHardBreaks !== 'boolean')
|
package/dist/util/gain.js
CHANGED
|
@@ -3,7 +3,7 @@ export const LINEAR = new Float32Array(256).map((_, v) => (v /= 255) <= 0.04045
|
|
|
3
3
|
export const encodeSRGB = (v) => 255 * (v <= 0.0031308 ? 12.92 * Math.max(0, v) : 1.055 * Math.min(1, v) ** (1 / 2.4) - 0.055);
|
|
4
4
|
const LUMA = [0.2126, 0.7152, 0.0722];
|
|
5
5
|
const gainTables = (map) => [0, 1, 2].map(c => {
|
|
6
|
-
const min = map.
|
|
6
|
+
const min = map.__min__[c], max = map.__max__[c], gamma = map.__gamma__[c];
|
|
7
7
|
return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
|
|
8
8
|
});
|
|
9
9
|
export function decodeGains(map) {
|
|
@@ -18,7 +18,7 @@ export function decodeGains(map) {
|
|
|
18
18
|
return gains;
|
|
19
19
|
}
|
|
20
20
|
export function encodeGains(map, gains, width, height, single) {
|
|
21
|
-
const min = map.
|
|
21
|
+
const min = map.__min__.slice(), max = map.__max__.slice();
|
|
22
22
|
for (let g = 0; g < gains.length; g += 3)
|
|
23
23
|
for (let c = 0; c < 3; c++) {
|
|
24
24
|
const v = gains[g + c];
|
|
@@ -31,17 +31,17 @@ export function encodeGains(map, gains, width, height, single) {
|
|
|
31
31
|
const lo = Math.min(...min), hi = Math.max(...max);
|
|
32
32
|
min.fill(lo);
|
|
33
33
|
max.fill(hi);
|
|
34
|
-
const gamma = map.
|
|
35
|
-
map.
|
|
34
|
+
const gamma = map.__gamma__[0];
|
|
35
|
+
map.__gamma__ = [gamma, gamma, gamma];
|
|
36
36
|
}
|
|
37
|
-
map.
|
|
38
|
-
map.
|
|
37
|
+
map.__min__ = min;
|
|
38
|
+
map.__max__ = max;
|
|
39
39
|
const image = map.image.width === width && map.image.height === height ? map.image : new Image(width, height);
|
|
40
40
|
const out = image.bitmap;
|
|
41
41
|
for (let i = 0, g = 0; i < out.length; i += 4, g += 3) {
|
|
42
42
|
for (let c = 0; c < 3; c++) {
|
|
43
43
|
const t = max[c] > min[c] ? (gains[g + c] - min[c]) / (max[c] - min[c]) : 0;
|
|
44
|
-
out[i + c] = 255 * Math.min(1, Math.max(0, t)) ** map.
|
|
44
|
+
out[i + c] = 255 * Math.min(1, Math.max(0, t)) ** map.__gamma__[c];
|
|
45
45
|
}
|
|
46
46
|
out[i + 3] = 255;
|
|
47
47
|
}
|
|
@@ -61,8 +61,8 @@ export function neutralMap(like, likeWidth, width, height) {
|
|
|
61
61
|
bitmap[i + 3] = 255;
|
|
62
62
|
}
|
|
63
63
|
map.image = image;
|
|
64
|
-
map.
|
|
65
|
-
map.
|
|
64
|
+
map.__min__ = map.__min__.map(v => Math.min(v, 0));
|
|
65
|
+
map.__max__ = map.__max__.map(v => Math.max(v, 0));
|
|
66
66
|
return map;
|
|
67
67
|
}
|
|
68
68
|
const sample = (gains, width, height, x, y, out) => {
|
|
@@ -80,7 +80,7 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
|
|
|
80
80
|
const mapWidth = map.width, mapHeight = map.height;
|
|
81
81
|
const sx = mapWidth / width, sy = mapHeight / height;
|
|
82
82
|
const oS = map.offsetSDR, oH = map.offsetHDR;
|
|
83
|
-
const lo = Math.min(0, ...map.
|
|
83
|
+
const lo = Math.min(0, ...map.__min__), hi = Math.max(0, ...map.__max__, extraMax);
|
|
84
84
|
const S = [0, 0, 0], H = [0, 0, 0], sumS = [0, 0, 0], sumH = [0, 0, 0];
|
|
85
85
|
const i0 = Math.max(0, Math.floor(left * sx)), i1 = Math.min(mapWidth, Math.ceil(right * sx));
|
|
86
86
|
const j0 = Math.max(0, Math.floor(top * sy)), j1 = Math.min(mapHeight, Math.ceil(bottom * sy));
|
|
@@ -125,7 +125,7 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
|
|
|
125
125
|
next[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
const {
|
|
128
|
+
const { __min__: min, __max__: max, gamma } = map;
|
|
129
129
|
let fits = !single || (min[0] === min[1] && min[1] === min[2] && max[0] === max[1] && max[1] === max[2] && gamma[0] === gamma[1] && gamma[1] === gamma[2]);
|
|
130
130
|
for (let g = 0; fits && g < next.length; g += 3)
|
|
131
131
|
for (let c = 0; c < 3; c++)
|
|
@@ -217,7 +217,7 @@ export function recolor(map, before, apply, exact) {
|
|
|
217
217
|
let changedPeak = 1;
|
|
218
218
|
for (let g = 0; g < changed.length; g++)
|
|
219
219
|
changedPeak = Math.max(changedPeak, changed[g]);
|
|
220
|
-
const lo = Math.min(0, ...map.
|
|
220
|
+
const lo = Math.min(0, ...map.__min__), hi = Math.max(Math.log2(Math.max(peak, changedPeak)), ...map.__max__);
|
|
221
221
|
const single = map.channels === 1;
|
|
222
222
|
for (let g = 0; g < gains.length; g += 3) {
|
|
223
223
|
if (single) {
|
package/dist/util/mem.d.ts
CHANGED
package/dist/util/options.js
CHANGED
|
@@ -1,12 +1,32 @@
|
|
|
1
1
|
export function isOptions(value) {
|
|
2
2
|
return typeof value === 'object' && value !== null && !ArrayBuffer.isView(value);
|
|
3
3
|
}
|
|
4
|
+
export function checkOptions(value, usage) {
|
|
5
|
+
if (value !== undefined && !isOptions(value))
|
|
6
|
+
throw new TypeError(`${usage.split('(')[0]}() takes an options object, like ${usage}, not ${value === null ? 'null' : typeof value}`);
|
|
7
|
+
}
|
|
4
8
|
export function finite(value, name) {
|
|
5
9
|
const number = Number(value);
|
|
6
10
|
if (!Number.isFinite(number))
|
|
7
11
|
throw new RangeError(`Invalid ${name}: ${value}`);
|
|
8
12
|
return number;
|
|
9
13
|
}
|
|
14
|
+
export function loopCount(value, name = 'loop count') {
|
|
15
|
+
if (typeof value !== 'number')
|
|
16
|
+
throw new TypeError(`${name} must be a number (Infinity or an integer from 0), not ${value === null ? 'null' : typeof value}`);
|
|
17
|
+
if (value !== Infinity && (!Number.isInteger(value) || value < 0))
|
|
18
|
+
throw new RangeError(`${name} must be Infinity or an integer from 0, not ${value}`);
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
export const toPlays = (count) => count === Infinity ? 0 : count + 1;
|
|
22
|
+
export const fromPlays = (plays) => plays === 0 ? Infinity : plays - 1;
|
|
23
|
+
const LOOP_LIMITS = { GIF: 65535, WebP: 65534, APNG: 2 ** 31 - 2 };
|
|
24
|
+
export function fitLoops(count, format) {
|
|
25
|
+
const max = LOOP_LIMITS[format];
|
|
26
|
+
if (count !== Infinity && count > max)
|
|
27
|
+
throw new RangeError(`refuse loop counts that don't fit the format: ${format} loop counts go up to ${max}, not ${count}`);
|
|
28
|
+
return count;
|
|
29
|
+
}
|
|
10
30
|
export function quality(value, name = 'quality') {
|
|
11
31
|
const q = finite(value, name);
|
|
12
32
|
if (q < 0 || q > 100)
|
|
@@ -20,9 +40,3 @@ export function effort(value, max) {
|
|
|
20
40
|
}
|
|
21
41
|
export const compressionLevel = (value) => Math.min(9, Math.max(0, ~~finite(value, 'compression level')));
|
|
22
42
|
export const color = (value, name = 'color') => finite(value, name);
|
|
23
|
-
const LOOP_LIMITS = { GIF: 65535, WebP: 65534, APNG: 2 ** 31 - 2 };
|
|
24
|
-
export function fitLoops(loopCount, format) {
|
|
25
|
-
if (loopCount !== Infinity && loopCount > LOOP_LIMITS[format])
|
|
26
|
-
throw new RangeError(`${format} stores loop counts up to ${LOOP_LIMITS[format]} (or Infinity to loop forever), not ${loopCount}`);
|
|
27
|
-
return loopCount;
|
|
28
|
-
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '
|
|
1
|
+
export const VERSION = '4.0.0-rc.1';
|
package/package.json
CHANGED
package/dist/gif.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { BinaryData } from './util/mem.js';
|
|
2
|
-
import { Frame } from './frame.js';
|
|
3
|
-
import { Animation, AnimationDecodeOptions, AnimationOptions, GIFEncodeOptions } from './animation.js';
|
|
4
|
-
/**
|
|
5
|
-
* Represents a GIF image as an array of frames, an {@link Animation} whose `encode()` and `decode()` work with GIFs.
|
|
6
|
-
* Array methods like map and filter return plain arrays.
|
|
7
|
-
* @deprecated Use {@link Animation}, which works the same and also handles APNG: `new Animation(frames, loopCount)`,
|
|
8
|
-
* {@link Animation.decode} and {@link Animation.encodeGIF}. `GIF` will be removed in 4.0
|
|
9
|
-
* @group Deprecated
|
|
10
|
-
*/
|
|
11
|
-
export declare class GIF extends Animation {
|
|
12
|
-
/**
|
|
13
|
-
* Creates a new GIF image
|
|
14
|
-
* @param frames The frames to create the GIF from
|
|
15
|
-
* @param loopCountOrOptions How many times to repeat the GIF (`Infinity` = forever, 0 = play once), or see
|
|
16
|
-
* {@link AnimationOptions}
|
|
17
|
-
*/
|
|
18
|
-
constructor(frames: Frame[], loopCountOrOptions?: number | AnimationOptions);
|
|
19
|
-
/** A short description for debugging, e.g. `GIF<64x64x1000ms>` */
|
|
20
|
-
toString(): string;
|
|
21
|
-
/**
|
|
22
|
-
* Encodes the GIF
|
|
23
|
-
* @param qualityOrOptions 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above), or
|
|
24
|
-
* see {@link GIFEncodeOptions}
|
|
25
|
-
* @deprecated Use {@link Animation.encodeGIF}
|
|
26
|
-
*/
|
|
27
|
-
encode(qualityOrOptions?: number | GIFEncodeOptions): Promise<Uint8Array>;
|
|
28
|
-
/**
|
|
29
|
-
* Decodes a GIF into fully composited frames
|
|
30
|
-
* @param input The binary data to decode
|
|
31
|
-
* @param onlyFirstFrameOrOptions Whether to end GIF decoding after the first frame, or see
|
|
32
|
-
* {@link AnimationDecodeOptions}
|
|
33
|
-
* @deprecated Use {@link Animation.decode}, which decodes APNGs too
|
|
34
|
-
*/
|
|
35
|
-
static decode(input: BinaryData, onlyFirstFrameOrOptions?: boolean | AnimationDecodeOptions): Promise<GIF>;
|
|
36
|
-
}
|
package/dist/gif.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { view } from "./util/mem.js";
|
|
2
|
-
import { ImageType } from "./image-type.js";
|
|
3
|
-
import { Animation } from "./animation.js";
|
|
4
|
-
import { isOptions } from "./util/options.js";
|
|
5
|
-
export class GIF extends Animation {
|
|
6
|
-
constructor(frames, loopCountOrOptions) {
|
|
7
|
-
super(frames, loopCountOrOptions);
|
|
8
|
-
}
|
|
9
|
-
toString() {
|
|
10
|
-
return `GIF<${this.width}x${this.height}x${this.duration}ms>`;
|
|
11
|
-
}
|
|
12
|
-
async encode(qualityOrOptions) {
|
|
13
|
-
return this.encodeGIF(qualityOrOptions);
|
|
14
|
-
}
|
|
15
|
-
static async decode(input, onlyFirstFrameOrOptions = false) {
|
|
16
|
-
const { onlyFirstFrame = false, maxPixels } = isOptions(onlyFirstFrameOrOptions) ? onlyFirstFrameOrOptions : { onlyFirstFrame: onlyFirstFrameOrOptions };
|
|
17
|
-
const data = view(input);
|
|
18
|
-
if (!ImageType.isGIF(new DataView(data.buffer, data.byteOffset, data.byteLength)))
|
|
19
|
-
throw new Error('Unsupported image type');
|
|
20
|
-
return Animation.__fromGIF__(data, onlyFirstFrame, GIF, maxPixels);
|
|
21
|
-
}
|
|
22
|
-
}
|
package/dist/util/rotate.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { littleEndian, isOpaque, premultiply8, unpremultiply8 } from "./alpha.js";
|
|
2
|
-
function span(start, step, limit, from, to) {
|
|
3
|
-
if (step === 0)
|
|
4
|
-
return start >= 0 && start < limit ? [from, to] : [from, from];
|
|
5
|
-
const a = -start / step, b = (limit - start) / step;
|
|
6
|
-
const lo = Math.floor(Math.min(a, b)) - 1, hi = Math.ceil(Math.max(a, b)) + 1;
|
|
7
|
-
return [Math.max(from, lo), Math.max(Math.max(from, lo), Math.min(to, hi))];
|
|
8
|
-
}
|
|
9
|
-
export function rotateBilinear(src, width, height, out, outWidth, outHeight, sin, cos) {
|
|
10
|
-
if (!littleEndian)
|
|
11
|
-
return false;
|
|
12
|
-
let bitmap = src;
|
|
13
|
-
if (!isOpaque(src))
|
|
14
|
-
premultiply8(src, bitmap = new Uint8ClampedArray(src.length));
|
|
15
|
-
const pixels = new Int32Array(bitmap.buffer, bitmap.byteOffset, width * height);
|
|
16
|
-
const stride = width + 2;
|
|
17
|
-
const padded = new Int32Array(stride * (height + 2));
|
|
18
|
-
for (let y = 0; y < height; y++)
|
|
19
|
-
padded.set(pixels.subarray(y * width, (y + 1) * width), (y + 1) * stride + 1);
|
|
20
|
-
const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
|
|
21
|
-
const x0 = 0.5 - outWidth / 2, cx = width / 2 + 0.5, cy = height / 2 + 0.5;
|
|
22
|
-
for (let oy = 0; oy < outHeight; oy++) {
|
|
23
|
-
const dy = oy + 0.5 - outHeight / 2;
|
|
24
|
-
const sx0 = x0 * cos - dy * sin + cx, sy0 = x0 * sin + dy * cos + cy;
|
|
25
|
-
const [xFrom, xTo] = span(sx0, cos, width + 1, 0, outWidth);
|
|
26
|
-
const [from, to] = span(sy0, sin, height + 1, xFrom, xTo);
|
|
27
|
-
let o = oy * outWidth + from, sx = sx0 + from * cos, sy = sy0 + from * sin;
|
|
28
|
-
for (let ox = from; ox < to; ox++, o++, sx += cos, sy += sin) {
|
|
29
|
-
if (!(sx >= 0 && sy >= 0 && sx < width + 1 && sy < height + 1))
|
|
30
|
-
continue;
|
|
31
|
-
const ix = sx | 0, iy = sy | 0;
|
|
32
|
-
const fx = (sx - ix) * 256 + 0.5 | 0, fy = (sy - iy) * 256 + 0.5 | 0;
|
|
33
|
-
const w11 = fx * fy + 128 >> 8, w10 = fx - w11, w01 = fy - w11, w00 = 256 - fx - fy + w11;
|
|
34
|
-
const i = iy * stride + ix;
|
|
35
|
-
const p00 = padded[i], p10 = padded[i + 1], p01 = padded[i + stride], p11 = padded[i + stride + 1];
|
|
36
|
-
const rb = Math.imul(p00 & 0xff00ff, w00) + Math.imul(p10 & 0xff00ff, w10) + Math.imul(p01 & 0xff00ff, w01) + Math.imul(p11 & 0xff00ff, w11) + 0x800080;
|
|
37
|
-
const ga = Math.imul(p00 >>> 8 & 0xff00ff, w00) + Math.imul(p10 >>> 8 & 0xff00ff, w10) + Math.imul(p01 >>> 8 & 0xff00ff, w01) + Math.imul(p11 >>> 8 & 0xff00ff, w11) + 0x800080;
|
|
38
|
-
result[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
unpremultiply8(out);
|
|
42
|
-
return true;
|
|
43
|
-
}
|