2
2
* Represents an image data processor that converts raw image data to a specified pixel format.
3
3
* This could be turned into a transform stream and be used in the serial connection handler.
4
4
* See example here: https://github.com/mdn/dom-examples/blob/main/streams/png-transform-stream/png-transform-stream.js
5
+ *
6
+ * @author Sebastian Romero
5
7
*/
6
8
class ImageDataProcessor {
7
9
pixelFormatInfo = {
@@ -31,10 +33,7 @@ class ImageDataProcessor {
31
33
* @param {number|null } width - The width of the image data processor. (Optional)
32
34
* @param {number|null } height - The height of the image data processor. (Optional)
33
35
*/
34
- constructor ( context , mode = null , width = null , height = null ) {
35
- this . context = context ;
36
- this . canvas = context . canvas ;
37
-
36
+ constructor ( mode = null , width = null , height = null ) {
38
37
if ( mode ) this . setMode ( mode ) ;
39
38
if ( width && height ) this . setResolution ( width , height ) ;
40
39
}
@@ -151,13 +150,11 @@ class ImageDataProcessor {
151
150
* Retrieves the image data from the given bytes by converting each pixel value.
152
151
*
153
152
* @param {Uint8Array } bytes - The raw byte array containing the image data.
154
- * @returns {ImageData } The image data object .
153
+ * @returns {Uint8ClampedArray } The image data as a Uint8ClampedArray containing RGBA values .
155
154
*/
156
155
getImageData ( bytes ) {
157
156
const BYTES_PER_ROW = this . width * this . bytesPerPixel ;
158
-
159
- const imageData = this . context . createImageData ( this . width , this . height ) ;
160
- const dataContainer = imageData . data ;
157
+ const dataContainer = new Uint8ClampedArray ( this . width * this . height * 4 ) ; // 4 channels: R, G, B, A
161
158
162
159
for ( let row = 0 ; row < this . height ; row ++ ) {
163
160
for ( let col = 0 ; col < this . width ; col ++ ) {
@@ -172,6 +169,6 @@ class ImageDataProcessor {
172
169
dataContainer [ pixelIndex + 3 ] = 255 ; // Alpha channel (opacity)
173
170
}
174
171
}
175
- return imageData ;
172
+ return dataContainer ;
176
173
}
177
174
}
0 commit comments