Skip to content

Commit 73d888d

Browse files
committed
check the gain map scale and the dither method with the shared helpers
1 parent c17f4ca commit 73d888d

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎src/codecs/quantize.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DitherMethod } from '../image.ts';
2+
import { oneOf } from '../util/options.ts';
23

34
// perceptual channel weights for squared color distances
45
const WR = 3, WG = 4, WB = 2;
@@ -27,11 +28,13 @@ export function paletteOptions(colors: number | undefined, dither: unknown, dith
2728
let count: number | undefined;
2829
if (colors !== undefined) {
2930
count = Number(colors);
31+
// eslint-disable-next-line no-restricted-syntax -- the palette size is only checked here
3032
if (!Number.isInteger(count) || count < 2 || count > 256) throw new RangeError(`colors must be an integer from 2 to 256, not ${colors}`);
3133
}
32-
if (!(DITHER_METHODS as readonly unknown[]).includes(dither)) throw new TypeError(`dither must be one of ${DITHER_METHODS.map(name => `'${name}'`).join(', ')}, not ${String(dither)}`);
34+
oneOf(dither, DITHER_METHODS, 'dither');
3335
const strength = Number(ditherStrength);
3436
if (!Number.isFinite(strength)) throw new RangeError(`Invalid ditherStrength: ${ditherStrength}`);
37+
// eslint-disable-next-line no-restricted-syntax -- the dither strength is only checked here
3538
if (strength < 0 || strength > 1) throw new RangeError(`ditherStrength must be 0-1, not ${ditherStrength}`);
3639
return { colors: count, dither: dither as DitherMethod, strength };
3740
}

‎src/image.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { encode as utf8 } from './util/utf8.ts';
2929
import { resample, BOX, TRIANGLE, CUBIC, type Region } from './util/resample.ts';
3030
import { gaussianBlur, boxBlurRGBA, premultiply, unpremultiply } from './util/blur.ts';
3131
import { Colors, wrapHue, hue2rgb } from './colors.ts';
32-
import { isOptions, checkOptions, finite, oneOf, pixelLimit, checkThreads, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from './util/options.ts';
32+
import { isOptions, checkOptions, finite, oneOf, pixelLimit, checkThreads, gainMapScale, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from './util/options.ts';
3333
import { MAX_PIXELS, LimitError, decoding } from './util/limits.ts';
3434
import { GainMap } from './gainmap.ts';
3535
import * as gain from './util/gain.ts';
@@ -655,6 +655,7 @@ export interface ColorToAlphaOptions {
655655

656656
// OpenType features as tags and values (tags are 4 printable ASCII characters)
657657
function featureList(features: Record<string, boolean | number>): Array<[string, number]> {
658+
// eslint-disable-next-line no-restricted-syntax -- the OpenType features are only used by the text functions
658659
if (typeof features !== 'object' || features === null) throw new TypeError('Features must be an object');
659660
return Object.entries(features).map(([tag, value]) => {
660661
if (!/^[\x20-\x7e]{4}$/.test(tag)) throw new RangeError(`Invalid OpenType feature tag: ${JSON.stringify(tag)}`);
@@ -2461,8 +2462,7 @@ export class Image {
24612462
const { quality: mapQuality, scale } = typeof gainMapOption === 'object' && gainMapOption !== null ? gainMapOption : {} as GainMapEmbedOptions;
24622463
let width: number | undefined, height: number | undefined;
24632464
if (scale !== undefined) {
2464-
const s = finite(scale, 'gain map scale');
2465-
if (!(s > 0 && s <= 1)) throw new RangeError(`The gain map scale must be above 0 and at most 1, not ${s}`);
2465+
const s = gainMapScale(scale);
24662466
width = Math.max(1, Math.round(this.width * s));
24672467
height = Math.max(1, Math.round(this.height * s));
24682468
}
@@ -2573,8 +2573,7 @@ export class Image {
25732573
const mapQuality = Math.max(1, checkQuality(quality, 'gain map quality'));
25742574
let map = gainMap.image;
25752575
if (scale !== undefined) {
2576-
const s = finite(scale, 'gain map scale');
2577-
if (!(s > 0 && s <= 1)) throw new RangeError(`The gain map scale must be above 0 and at most 1, not ${s}`);
2576+
const s = gainMapScale(scale);
25782577
const width = Math.max(1, Math.round(this.width * s)), height = Math.max(1, Math.round(this.height * s));
25792578
if (width !== map.width || height !== map.height) map = map.clone().resize(width, height);
25802579
}
@@ -2769,12 +2768,14 @@ export class Image {
27692768
const mode = givenWidth !== undefined ? 'width' : givenHeight !== undefined ? 'height' : 'scale';
27702769
const size = finite(givenWidth ?? givenHeight ?? scale ?? 1, `SVG ${mode}`);
27712770
if (mode === 'scale' && size <= 0)
2771+
// eslint-disable-next-line no-restricted-syntax -- the SVG size is only checked here
27722772
throw new RangeError('SVG scale must be > 0');
27732773
if (mode !== 'scale' && size < 1)
27742774
throw new RangeError(`SVG ${mode} must be >= 1`);
27752775

27762776
// the images given by href, checked before anything is decoded
27772777
if (typeof givenImages !== 'object' || givenImages === null || Array.isArray(givenImages) || givenImages instanceof Map)
2778+
// eslint-disable-next-line no-restricted-syntax -- the images of an SVG are only checked here
27782779
throw new TypeError('SVG images must be an object of hrefs to image data');
27792780
const given = new Map<string, BinaryData | Image>();
27802781
for (const href of Object.keys(givenImages)) {

‎src/util/options.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ export function pixelLimit(value: number, name = 'maxPixels'): number {
9999
return Number(value);
100100
}
101101

102+
/** The `scale` of a gain map: above 0 and at most 1 (1 is the size of the image) */
103+
export function gainMapScale(value: number): number {
104+
const scale = finite(value, 'gain map scale');
105+
if (!(scale > 0 && scale <= 1)) throw new RangeError(`The gain map scale must be above 0 and at most 1, not ${scale}`);
106+
return scale;
107+
}
108+
102109
/** The `threads` option: a whole number of at least 1 */
103110
export function checkThreads(threads: number): void {
104111
if (!Number.isInteger(threads) || threads < 1) throw new RangeError(`threads must be a positive integer, not ${threads}`);

0 commit comments

Comments
 (0)