Skip to content

Commit aa385b4

Browse files
committed
define DitherMethod once
1 parent 0782797 commit aa385b4

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎src/codecs/gif.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ByteWriter } from '../util/bytes.ts';
2-
import { quantize, type DitherMethod } from './quantize.ts';
2+
import type { DitherMethod } from '../image.ts';
3+
import { quantize } from './quantize.ts';
34
import { MAX_PIXELS, PixelBudget, checkPixels } from '../util/limits.ts';
45

56
// ImageScript disposal modes (any, keep, previous, background) <-> GIF disposal methods (unspecified, none, background, previous)

‎src/codecs/quantize.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DitherMethod } from '../image.ts';
12

23
// perceptual channel weights for squared color distances
34
const WR = 3, WG = 4, WB = 2;
@@ -14,9 +15,9 @@ export interface QuantizeOptions {
1415
transparent?: boolean;
1516
}
1617

17-
/** The dithering methods of Image.quantize() and the GIF encoders */
18-
export const DITHER_METHODS = ['none', 'floyd-steinberg', 'atkinson', 'ordered'] as const;
19-
export type DitherMethod = typeof DITHER_METHODS[number];
18+
// every DitherMethod (a missing key is a type error)
19+
const DITHER: Record<DitherMethod, true> = { 'none': true, 'floyd-steinberg': true, 'atkinson': true, 'ordered': true };
20+
export const DITHER_METHODS = Object.keys(DITHER) as DitherMethod[];
2021

2122
/**
2223
* Checks palette options (`colors` from 2 to 256, a dither method, a strength 0-1) before anything changes, with the
@@ -185,7 +186,7 @@ const ATKINSON = [[1, 0, 1 / 8], [2, 0, 1 / 8], [-1, 1, 1 / 8], [0, 1, 1 / 8], [
185186
* the neighbors that come after it (serpentine: rows alternate direction), `'ordered'` offsets each pixel by a 4x4
186187
* Bayer threshold. `strength` (0-1) scales the spread error and the threshold offset; 0 changes nothing but rounding
187188
*/
188-
export function applyPalette(pixels: Uint8Array, width: number, height: number, palette: Uint8Array, dither: 'none' | 'floyd-steinberg' | 'atkinson' | 'ordered', strength: number): void {
189+
export function applyPalette(pixels: Uint8Array, width: number, height: number, palette: Uint8Array, dither: DitherMethod, strength: number): void {
189190
const entries = palette.length / 3;
190191
if (!entries) return;
191192
const nearest = nearestSearch(palette, entries);

0 commit comments

Comments
 (0)