forked from joshmarinacci/node-pureimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.ts
More file actions
192 lines (178 loc) · 4.88 KB
/
Copy pathbitmap.ts
File metadata and controls
192 lines (178 loc) · 4.88 KB
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
import { NAMED_COLORS, OPAQUE_BLACK } from "./named_colors.js";
import { Context } from "./context.js";
import { fromBytesBigEndian, getBytesBigEndian } from "./uint32.js";
/**
* The Bitmap class is used for direct pixel manipulation(for example setting a pixel colour,
* transparency etc). It also provides a factory method for creating new instances of
* {@link Context}
*/
export class Bitmap {
public width: number;
public height: number;
public data: Uint8Array;
private _context: Context;
/**
* Creates an instance of Bitmap.
* @param {number} w Width
* @param {number} h Height
* @memberof Bitmap
*/
constructor(w: number, h: number) {
this.width = Math.floor(w);
this.height = Math.floor(h);
this.data = new Uint8Array(w * h * 4);
const fillval = OPAQUE_BLACK;
for (let j = 0; j < h; j++) {
for (let i = 0; i < w; i++) {
this.setPixelRGBA(i, j, fillval);
}
}
this._context = new Context(this);
}
/** Calculate Index */
calculateIndex(
/** X position */
x: number,
/** Y position */
y: number,
) {
x = Math.floor(x);
y = Math.floor(y);
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return 0;
return (this.width * y + x) * 4;
}
/** Set the RGBA(Red, Green, Blue, Alpha) values on an individual pixel level */
setPixelRGBA(
/** X axis position */
x: number,
/** Y axis position */
y: number,
/** A hex representation of the RGBA value of the pixel. See {@link NAMED_COLORS} for examples */
rgba: number,
) {
this.validate_coords(x, y);
const i = this.calculateIndex(x, y);
const bytes = getBytesBigEndian(rgba);
this.data[i + 0] = bytes[0];
this.data[i + 1] = bytes[1];
this.data[i + 2] = bytes[2];
this.data[i + 3] = bytes[3];
}
/** Set the individual red, green, blue and alpha levels of an individual pixel */
setPixelRGBA_i(
/** X axis position */
x: number,
/** Y axis position */
y: number,
/** Red level */
r: number,
/** Green level */
g: number,
/** Blue level */
b: number,
/** Alpha level */
a: number,
) {
const i = this.calculateIndex(x, y);
this.data[i + 0] = r;
this.data[i + 1] = g;
this.data[i + 2] = b;
this.data[i + 3] = a;
}
/** Get the RGBA value of an individual pixel as a hexadecimal number(See {@link NAMED_COLORS} for examples) */
getPixelRGBA(
/** X axis position */
x: number,
/** Y axis position */
y: number,
) {
this.validate_coords(x, y);
const i = this.calculateIndex(x, y);
return fromBytesBigEndian(
this.data[i + 0],
this.data[i + 1],
this.data[i + 2],
this.data[i + 3],
);
}
getDebugPixelRGBA(
/** X axis position */
x: number,
/** Y axis position */
y: number,
) {
const i = this.calculateIndex(x, y);
const width = this.width;
const height = this.height;
const data = this.data;
console.dir({ i, width, height, data });
return fromBytesBigEndian(
this.data[i + 0],
this.data[i + 1],
this.data[i + 2],
this.data[i + 3],
);
}
/**
* Get Pixel RGBA Separate
*
* @ignore
*/
getPixelRGBA_separate(
/** X axis position */
x: number,
/** Y axis position */
y: number,
) {
const i = this.calculateIndex(x, y);
return this.data.slice(i, i + 4);
}
/**
* {@link Context} factory. Creates a new {@link Context} instance object for the current bitmap object
*/
getContext(type: string) {
if (type === "2d") {
return this._context;
}
return null;
}
_copySubBitmap(x: number, y: number, w: number, h: number) {
const dst = new Bitmap(w, h);
for (let i = 0; i < w; i++) {
for (let j = 0; j < h; j++) {
const indexA = this.calculateIndex(x + i, y + j);
const indexB = dst.calculateIndex(i, j);
for (let k = 0; k < 4; k++) {
dst.data[indexB + k] = this.data[indexA + k];
}
}
}
return dst;
}
_pasteSubBitmap(src: Bitmap, x: number, y: number) {
for (let i = 0; i < src.width; i++) {
for (let j = 0; j < src.height; j++) {
const indexA = this.calculateIndex(x + i, y + j);
const indexB = src.calculateIndex(i, j);
for (let k = 0; k < 4; k++) {
this.data[indexA + k] = src.data[indexB + k];
}
}
}
}
_isValidCoords(x, y) {
if (x < 0) return false;
if (y < 0) return false;
if (x >= this.width) return false;
if (y >= this.height) return false;
return true;
}
validate_coords(x, y) {
if (x < 0) throw new Error(`Invalid Index: x ${x} <0`);
if (y < 0) throw new Error(`Invalid Index: y ${y} <0`);
if (x >= this.width)
throw new Error(`Invalid Index: x ${x} >= width ${this.width}`);
if (y >= this.height)
throw new Error(`Invalid Index: y ${y} >= height ${this.height}`);
}
}