-
Notifications
You must be signed in to change notification settings - Fork 4
/
TextureDataFloat.ts
190 lines (178 loc) · 4.8 KB
/
TextureDataFloat.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
import {
Size,
Index,
FloatTypedArrayConstructor,
TypedArrayConstructor,
} from '../../types/CommonTypes';
import {ColorRgb} from '../math/ColorRgb';
import {ColorRgba} from '../math/ColorRgba';
export class TextureDataFloat {
private __data: Float32Array;
private __channels: number;
private __width: number;
private __height: number;
constructor(width: number, height: number, channels: number) {
this.__channels = channels;
this.__width = width;
this.__height = height;
this.__data = new Float32Array(width * height * channels);
}
resize(width: number, height: number, channels: number) {
this.__width = width;
this.__height = height;
this.__channels = channels;
this.__data = new Float32Array(
TextureDataFloat.transfer(
this.__data.buffer,
width * height * channels * 4
)
);
}
setPixelAtChannel(x: Index, y: Index, channelIdx: Index, value: number) {
this.__data[
y * this.__width * this.__channels + x * this.__channels + channelIdx
] = value;
}
get width() {
return this.__width;
}
get height() {
return this.__height;
}
get data() {
return this.__data;
}
getPixel(x: Index, y: Index, channelIdx: Index) {
return this.__data[
y * this.__width * this.__channels + x * this.__channels + channelIdx
];
}
getPixelAs(
x: Index,
y: Index,
channels: Size,
typeClass: typeof ColorRgb | typeof ColorRgba
) {
if (channels === 3) {
return new (typeClass as any)(
new Float32Array([
this.__data[
y * this.__width * this.__channels + x * this.__channels + 0
],
this.__data[
y * this.__width * this.__channels + x * this.__channels + 1
],
this.__data[
y * this.__width * this.__channels + x * this.__channels + 2
],
])
);
} else if (channels === 4) {
return new (typeClass as any)(
new Float32Array([
this.__data[
y * this.__width * this.__channels + x * this.__channels + 0
],
this.__data[
y * this.__width * this.__channels + x * this.__channels + 1
],
this.__data[
y * this.__width * this.__channels + x * this.__channels + 2
],
this.__data[
y * this.__width * this.__channels + x * this.__channels + 3
],
])
);
}
}
getPixelAsArray(x: Index, y: Index) {
const array = [];
for (let i = 0; i < this.__channels; i++) {
array.push(
this.__data[
y * this.__width * this.__channels + x * this.__channels + i
]
);
}
return array;
}
initialize(width: number, height: number, channels: number) {
this.__width = width;
this.__height = height;
this.__channels = channels;
this.__data = new Float32Array(width * height * channels);
}
static transfer(source: any, length: number) {
source = Object(source);
const dest = new ArrayBuffer(length);
if (!(source instanceof ArrayBuffer) || !(dest instanceof ArrayBuffer)) {
throw new TypeError(
'Source and destination must be ArrayBuffer instances'
);
}
if (dest.byteLength >= source.byteLength) {
let nextOffset = 0;
let leftBytes = source.byteLength;
const wordSizes = [8, 4, 2, 1];
wordSizes.forEach(_wordSize_ => {
if (leftBytes >= _wordSize_) {
const done = transferWith(
_wordSize_,
source,
dest,
nextOffset,
leftBytes
);
nextOffset = done.nextOffset;
leftBytes = done.leftBytes;
}
});
}
return dest;
function transferWith(
wordSize: number,
source: any,
dest: any,
nextOffset: number,
leftBytes: number
) {
let ViewClass: TypedArrayConstructor = Uint8Array;
switch (wordSize) {
case 8:
ViewClass = Float64Array;
break;
case 4:
ViewClass = Float32Array;
break;
case 2:
ViewClass = Uint16Array;
break;
case 1:
ViewClass = Uint8Array;
break;
default:
ViewClass = Uint8Array;
break;
}
const view_source = new ViewClass(
source,
nextOffset,
Math.trunc(leftBytes / wordSize)
);
const view_dest = new ViewClass(
dest,
nextOffset,
Math.trunc(leftBytes / wordSize)
);
for (let i = 0; i < view_dest.length; i++) {
view_dest[i] = view_source[i];
}
return {
nextOffset: view_source.byteOffset + view_source.byteLength,
leftBytes:
source.byteLength - (view_source.byteOffset + view_source.byteLength),
};
}
}
}