pixel-data-js 0.0.3 → 0.2.0

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.
@@ -1,7 +1,17 @@
1
+ type RGBA = {
2
+ r: number;
3
+ g: number;
4
+ b: number;
5
+ a: number;
6
+ };
7
+ type Color32 = number & {
8
+ readonly __brandColor32: unique symbol;
9
+ };
10
+ type BlendColor32 = (src: Color32, dst: Color32) => Color32;
1
11
  type ImageDataLike = {
2
12
  width: number;
3
13
  height: number;
4
- data: Uint8ClampedArray;
14
+ data: Uint8ClampedArray<ArrayBuffer>;
5
15
  };
6
16
  type SerializedImageData = {
7
17
  width: number;
@@ -11,9 +21,204 @@ type SerializedImageData = {
11
21
  type Base64EncodedUInt8Array = string & {
12
22
  readonly __brandBase64UInt8Array: unique symbol;
13
23
  };
24
+ type Rect = {
25
+ x: number;
26
+ y: number;
27
+ w: number;
28
+ h: number;
29
+ };
30
+ declare enum MaskType {
31
+ ALPHA = 0,
32
+ BINARY = 1
33
+ }
34
+ interface BaseMaskData {
35
+ readonly width: number;
36
+ readonly height: number;
37
+ readonly data: Uint8Array;
38
+ }
39
+ interface AlphaMask extends BaseMaskData {
40
+ readonly type: MaskType.ALPHA;
41
+ }
42
+ interface BinaryMask extends BaseMaskData {
43
+ readonly type: MaskType.BINARY;
44
+ }
45
+ type AnyMask = AlphaMask | BinaryMask;
46
+
47
+ declare const sourceOverColor32: BlendColor32;
48
+ /**
49
+ * Screen: Lightens the destination (inverse of Multiply).
50
+ * Result = 1 - ((1 - Src) * (1 - Dst))
51
+ */
52
+ declare const screenColor32: BlendColor32;
53
+ /**
54
+ * Linear Dodge (Additive): Simply adds the source to the destination.
55
+ * Clamps at 255.
56
+ */
57
+ declare const linearDodgeColor32: BlendColor32;
58
+ /**
59
+ * Multiply: Darkens the destination based on the source color.
60
+ * Result = (Src * Dst) / 255
61
+ */
62
+ declare const multiplyColor32: BlendColor32;
63
+ /**
64
+ * Difference: Subtracts the darker color from the lighter color.
65
+ * Result = |Src - Dst|
66
+ */
67
+ declare const differenceColor32: BlendColor32;
68
+ /**
69
+ * Hard Light: Decides Multiply vs Screen based on SOURCE brightness.
70
+ * Acts like a harsh spotlight.
71
+ */
72
+ declare const hardLightColor32: BlendColor32;
73
+ /**
74
+ * Color Burn: Darkens the destination to reflect the source color.
75
+ * Intense saturation in the darks.
76
+ */
77
+ declare const colorBurnColor32: BlendColor32;
78
+ /**
79
+ * Overlay: The classic "Contrast" mode.
80
+ * Decides Multiply vs Screen based on DESTINATION brightness.
81
+ */
82
+ declare const overlayColor32: BlendColor32;
83
+ declare const COLOR_32_BLEND_MODES: {
84
+ sourceOver: BlendColor32;
85
+ screen: BlendColor32;
86
+ linearDodge: BlendColor32;
87
+ multiply: BlendColor32;
88
+ difference: BlendColor32;
89
+ overlay: BlendColor32;
90
+ hardLight: BlendColor32;
91
+ colorBurn: BlendColor32;
92
+ };
93
+
94
+ type BlendImageDataOptions = {
95
+ /**
96
+ * The x-coordinate in the destination image where the blend begins.
97
+ * @default 0
98
+ */
99
+ dx?: number;
100
+ /**
101
+ * The y-coordinate in the destination image where the blend begins.
102
+ * @default 0
103
+ */
104
+ dy?: number;
105
+ /**
106
+ * The x-coordinate of the top-left corner of the sub-rectangle
107
+ * of the source image to extract.
108
+ * @default 0
109
+ */
110
+ sx?: number;
111
+ /**
112
+ * The y-coordinate of the top-left corner of the sub-rectangle
113
+ * of the source image to extract.
114
+ * @default 0
115
+ */
116
+ sy?: number;
117
+ /**
118
+ * The width of the sub-rectangle of the source image to extract.
119
+ * Defaults to the full remaining width of the source.
120
+ */
121
+ sw?: number;
122
+ /**
123
+ * The height of the sub-rectangle of the source image to extract.
124
+ * Defaults to the full remaining height of the source.
125
+ */
126
+ sh?: number;
127
+ /**
128
+ * Overall layer opacity, typically ranging from 0.0 (transparent) to 1.0 (opaque).
129
+ * @default 1.0
130
+ */
131
+ opacity?: number;
132
+ /**
133
+ * Same as opacity but is 0-255 and faster when processing. If Present opacity is ignored.
134
+ * @default undefined
135
+ */
136
+ alpha?: number;
137
+ /**
138
+ * An optional alpha mask buffer.
139
+ * The values in this array (0-255) determine the intensity of the blend
140
+ * at each corresponding pixel.
141
+ */
142
+ mask?: AnyMask | null;
143
+ /**
144
+ * The specific blending function/algorithm to use for pixel math
145
+ * (e.g., Multiply, Screen, Overlay).
146
+ */
147
+ blendFn?: BlendColor32;
148
+ };
149
+ /**
150
+ * Blits source ImageData into a destination ImageData using 32-bit integer bitwise blending.
151
+ * This function bypasses standard Canvas API limitations by operating directly on
152
+ * Uint32Array views. It supports various blend modes, binary/alpha masking, and
153
+ * automatic clipping of both source and destination bounds.
154
+ * @example
155
+ * blendImageData32(ctx.getImageData(0,0,100,100), sprite, {
156
+ * blendFn: COLOR_32_BLEND_MODES.multiply,
157
+ * mask: brushMask,
158
+ * maskMode: MaskMode.ALPHA
159
+ * });
160
+ */
161
+ declare function blendImageData(dst: ImageDataLike, src: ImageDataLike, opts: BlendImageDataOptions): void;
162
+
163
+ type ApplyMaskOptions = {
164
+ /**
165
+ * The x-coordinate in the destination image where the mask begins.
166
+ * @default 0
167
+ */
168
+ dx?: number;
169
+ /**
170
+ * The y-coordinate in the destination image where the mask begins.
171
+ * @default 0
172
+ */
173
+ dy?: number;
174
+ /**
175
+ * The x-coordinate of the top-left corner of the sub-rectangle
176
+ * of the source image to extract.
177
+ * @default 0
178
+ */
179
+ sx?: number;
180
+ /**
181
+ * The y-coordinate of the top-left corner of the sub-rectangle
182
+ * of the source image to extract.
183
+ * @default 0
184
+ */
185
+ sy?: number;
186
+ /**
187
+ * The width of the sub-rectangle of the source image to extract.
188
+ * Defaults to the full remaining width of the source.
189
+ */
190
+ sw?: number;
191
+ /**
192
+ * The height of the sub-rectangle of the source image to extract.
193
+ * Defaults to the full remaining height of the source.
194
+ */
195
+ sh?: number;
196
+ };
197
+ /**
198
+ * Applies a binary (on/off) mask to an RGBA buffer.
199
+ * If mask value is 0, pixel becomes transparent.
200
+ */
201
+ declare function applyBinaryMask(dst: ImageDataLike, mask: BinaryMask, opts?: ApplyMaskOptions): ImageDataLike | undefined;
202
+ /**
203
+ * Applies a smooth alpha mask to an RGBA buffer.
204
+ * Multiplies existing Alpha by (maskValue / 255).
205
+ */
206
+ declare function applyAlphaMask(dst: ImageData, mask: AlphaMask, opts?: ApplyMaskOptions): void;
207
+
208
+ declare function makeImageDataColor32Adapter(imageData: ImageDataLike): {
209
+ inBounds: (x: number, y: number) => boolean;
210
+ imageData: ImageDataLike;
211
+ data32: Uint32Array<ArrayBuffer>;
212
+ setPixel: (x: number, y: number, color: Color32) => void;
213
+ getPixel: (x: number, y: number) => Color32 | undefined;
214
+ };
215
+ declare function extractPixelData(imageData: ImageDataLike, rect: Rect): Uint8ClampedArray;
216
+ declare function extractPixelData(imageData: ImageDataLike, x: number, y: number, w: number, h: number): Uint8ClampedArray;
217
+ declare function copyImageData({ data, width, height }: ImageDataLike): ImageData;
218
+ declare function copyImageDataLike({ data, width, height }: ImageDataLike): ImageDataLike;
14
219
 
15
220
  declare function base64EncodeArrayBuffer(buffer: ArrayBufferLike): Base64EncodedUInt8Array;
16
- declare function base64DecodeArrayBuffer(encoded: Base64EncodedUInt8Array): Uint8ClampedArray;
221
+ declare function base64DecodeArrayBuffer(encoded: Base64EncodedUInt8Array): Uint8ClampedArray<ArrayBuffer>;
17
222
  /**
18
223
  * Serialize for use in JSON. Pixel data is stored as base64 encoded string.
19
224
  */
@@ -23,4 +228,45 @@ declare function deserializeRawImageData<T extends SerializedImageData>(serializ
23
228
  declare function deserializeImageData<T extends SerializedImageData>(serialized: T): ImageData;
24
229
  declare function deserializeNullableImageData<T extends SerializedImageData | null>(serialized: T): T extends null ? null : ImageData;
25
230
 
26
- export { base64DecodeArrayBuffer, base64EncodeArrayBuffer, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, serializeImageData, serializeNullableImageData };
231
+ /**
232
+ * Packs RGBA into a 32-bit integer compatible with
233
+ * Little-Endian Uint32Array views on ImageData.
234
+ */
235
+ declare function packColor(r: number, g: number, b: number, a: number): Color32;
236
+ declare function packRGBA({ r, g, b, a }: RGBA): Color32;
237
+ declare const unpackRed: (packed: Color32) => number;
238
+ declare const unpackGreen: (packed: Color32) => number;
239
+ declare const unpackBlue: (packed: Color32) => number;
240
+ declare const unpackAlpha: (packed: Color32) => number;
241
+ declare function unpackColor(packed: Color32): RGBA;
242
+ declare function unpackColorTo(packed: Color32, scratch?: RGBA): RGBA;
243
+ declare function colorDistance(a: Color32, b: Color32): number;
244
+ /**
245
+ * Linearly interpolates between two 32-bit colors using a floating-point weight.
246
+ * * This is the preferred method for UI animations or scenarios where high
247
+ * precision is required. It uses the standard `a + t * (b - a)` formula
248
+ * for each channel.
249
+ * @param a - The starting color as a 32-bit integer (AABBGGRR).
250
+ * @param b - The target color as a 32-bit integer (AABBGGRR).
251
+ * @param t - The interpolation factor between 0.0 and 1.0.
252
+ * @returns The interpolated 32-bit color.
253
+ */
254
+ declare function lerpColor32(a: Color32, b: Color32, t: number): Color32;
255
+ /**
256
+ * Linearly interpolates between two 32-bit colors using integer fixed-point math.
257
+ * Highly optimized for image processing and real-time blitting. It processes
258
+ * channels in parallel using bitmasks (RB and GA pairs).
259
+ * @note Subject to a 1-bit drift (rounding down) due to fast bit-shift division.
260
+ * @param src - The source (foreground) color as a 32-bit integer.
261
+ * @param dst - The destination (background) color as a 32-bit integer.
262
+ * @param w - The blend weight as a byte value from 0 to 255. Where 0 is 100% dst and 255 is 100% src
263
+ * @returns The blended 32-bit color.
264
+ */ declare function lerpColor32Fast(src: Color32, dst: Color32, w: number): Color32;
265
+ declare function color32ToHex(color: Color32): string;
266
+ /**
267
+ * Converts a 32-bit integer (0xAABBGGRR) to a CSS rgba() string.
268
+ * Example: 0xFF0000FF -> "rgba(255,0,0,1)"
269
+ */
270
+ declare function color32ToCssRGBA(color: Color32): string;
271
+
272
+ export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, type BlendImageDataOptions, COLOR_32_BLEND_MODES, type Color32, type ImageDataLike, MaskType, type RGBA, type Rect, type SerializedImageData, applyAlphaMask, applyBinaryMask, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendImageData, color32ToCssRGBA, color32ToHex, colorBurnColor32, colorDistance, copyImageData, copyImageDataLike, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceColor32, extractPixelData, hardLightColor32, lerpColor32, lerpColor32Fast, linearDodgeColor32, makeImageDataColor32Adapter, multiplyColor32, overlayColor32, packColor, packRGBA, screenColor32, serializeImageData, serializeNullableImageData, sourceOverColor32, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed };