/
api.ts
344 lines (316 loc) · 7.29 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import type {
Fn,
Fn2,
IObjectOf,
NumericArray,
Type,
TypedArray,
} from "@thi.ng/api";
/**
* ABGR 8bit lane/channel IDs
*/
export enum Lane {
ALPHA = 0,
BLUE = 1,
GREEN = 2,
RED = 3,
}
/**
* Wrap behaviors (currently unused)
*/
export enum Wrap {
NONE,
U,
V,
UV,
}
/**
* Filtered access types (currently unused)
*/
export enum Filter {
NEAREST,
LINEAR,
}
export type UintType = Type.U8 | Type.U16 | Type.U32;
/**
* Blend function (for packed integers) given to IBlend implementations.
*/
export type BlendFnInt = Fn2<number, number, number>;
export type BlendFnFloat = (
out: NumericArray | null,
src: NumericArray,
dest: NumericArray
) => NumericArray;
/**
* Color channel getter. Returns 0-based channel value (regardless of
* shift/bit position)
*/
export type ChannelGetter<T> = Fn<T, number>;
/**
* Color channel setter. Takes current full pixel value and 0-based
* channel value to set (regardless of shift/bit position). Returns
* updated pixel value.
*/
export type ChannelSetter<T> = Fn2<T, number, T>;
export interface IABGRConvert<T> {
/**
* Converts given ABGR value into internal pixel format.
*/
fromABGR: (x: number, out?: T) => T;
/**
* Converts given internal pixel format value to packed ABGR.
*/
toABGR: Fn<T, number>;
}
export interface PackedChannelSpec {
/**
* Channel size in bits (1-8)
*/
size: number;
/**
* Related ABGR lane this channel is mapped from/to. Only used if
* parent format uses auto-generated {@link IABGRConvert} implementation
* (i.e. only if no-user defined converters are given to
* {@link PackedFormatSpec}).
*/
lane?: Lane;
}
export interface PackedChannel {
/**
* Channel size in bits (1-8)
*/
size: number;
/**
* Bit shift offset (in bits, not shifted value)
*/
shift: number;
/**
* Shift from ABGR channel offset
*/
abgrShift: number;
/**
* 0-based channel bit mask (WRT parent format)
*/
mask0: number;
/**
* Aligned bit mask (WRT parent format)
*/
maskA: number;
/**
* Original channel/lane ID in ABGR
*/
lane: Lane;
/**
* Int value accessor
*/
int: ChannelGetter<number>;
/**
* Int value accessor
*/
setInt: ChannelSetter<number>;
/**
* Normalized float accessor
*/
float: ChannelGetter<number>;
/**
* Normalized float accessor
*/
setFloat: ChannelSetter<number>;
/**
* Applies ordered dithering to given channel value.
*/
dither: (
mat: BayerMatrix,
steps: number,
x: number,
y: number,
val: number
) => number;
}
/**
* Format configuration passed to {@link defPackedFormat}.
*/
export interface PackedFormatSpec extends Partial<IABGRConvert<number>> {
/**
* Storage / typed array type
*/
type: UintType;
/**
* Number of actual used bits (must be <= `size`)
*/
size: number;
/**
* Number of alpha channel bits. MUST be given if format uses alpha
* channel.
*
* @defaultValue 0
*/
alpha?: number;
/**
* Individual channel configurations
*/
channels: PackedChannelSpec[];
}
/**
* Compiled format object returned by {@link defPackedFormat}.
*/
export interface PackedFormat extends IABGRConvert<number> {
type: UintType;
size: number;
alpha: number;
channels: PackedChannel[];
// internal marker only
readonly __packed: true;
}
export interface FloatFormatSpec {
alpha?: boolean;
gray?: boolean;
channels: Lane[];
}
export interface FloatFormat extends IABGRConvert<NumericArray> {
alpha: boolean;
gray: boolean;
size: number;
shift: IObjectOf<number>;
channels: Lane[];
// internal marker only
readonly __float: true;
}
export interface CanvasContext {
canvas: HTMLCanvasElement;
ctx: CanvasRenderingContext2D;
}
export interface RawPixelBuffer extends CanvasContext {
img: ImageData;
pixels: Uint32Array;
}
export interface IPixelBuffer<T extends TypedArray, P> {
width: number;
height: number;
pixels: T;
/**
* Returns pixel value at given position. If pos is outside the
* defined region, returns a suitable zero value.
*
* @param x -
* @param y -
*/
getAt(x: number, y: number): P;
/**
* Writes pixel value at given position. Has no effect if outside of
* the defined region.
*
* @param x -
* @param y -
* @param col -
*/
setAt(x: number, y: number, col: P): this;
/**
* Extracts region as new pixel buffer in same format.
*
* @param x -
* @param y -
* @param width -
* @param height -
*/
getRegion(
x: number,
y: number,
width: number,
height: number
): IPixelBuffer<T, P>;
}
export interface IBlit<T extends TypedArray, P> {
/**
* Blits pixels into given `dest` pixel buffer, using provided
* options. If `dest` buffer is smaller than source buffer, only the
* top-left region will be written.
*
* Destination MUST be of same format as original. No conversion is
* performed.
*
* @param dest -
* @param opts -
*/
blit(dest: IPixelBuffer<T, P>, opts?: Partial<BlitOpts>): void;
/**
* Converts and blits pixels into given canvas at position `x`, `y`
* (0,0 by default). If canvas is smaller than source buffer, only
* the top-left region will be written.
*/
blitCanvas(canvas: HTMLCanvasElement, x?: number, y?: number): void;
}
export interface IBlend<F, T extends TypedArray, P> {
/**
* Uses given `op` function to blend / compose pixels of this buffer
* with those of `dest` and writes results into `dest`. Supports
* same options as `blit()`.
*
* @param op -
* @param dest -
* @param opts -
*/
blend(op: F, dest: IPixelBuffer<T, P>, opts?: Partial<BlitOpts>): void;
}
export interface IInvert {
invert(): this;
}
export interface IColorChannel<T extends TypedArray, C> {
/**
* Returns new pixel buffer of selected color channel. The extracted
* values are a copy of the original.
*/
getChannel(id: C): IPixelBuffer<T, number>;
/**
* Replaces selected color channel with values from given pixel
* buffer, which MUST be of same size as target.
*
* @param id -
* @param buf -
*/
setChannel(id: C, buf: IPixelBuffer<T, number> | number): this;
}
export interface BlitOpts {
/**
* Destination X position (top-left)
*
* @defaultValue 0
*/
dx: number;
/**
* Destination Y position (top-left)
*
* @defaultValue 0
*/
dy: number;
/**
* Source X position (top-left)
*
* @defaultValue 0
*/
sx: number;
/**
* Source Y position (top-left)
*
* @defaultValue 0
*/
sy: number;
/**
* Source region width
*
* @defaultValue buffer width
*/
w: number;
/**
* Source region width
*
* @defaultValue buffer height
*/
h: number;
}
export type BayerSize = 1 | 2 | 4 | 8 | 16 | 32 | 64;
export interface BayerMatrix {
mat: number[][];
invSize: number;
mask: number;
}