22 * Represents an image data processor that converts raw image data to a specified pixel format.
33 * This could be turned into a transform stream and be used in the serial connection handler.
44 * See example here: https://github.com/mdn/dom-examples/blob/main/streams/png-transform-stream/png-transform-stream.js
5+ *
6+ * @author Sebastian Romero
57 */
68class ImageDataProcessor {
79 pixelFormatInfo = {
@@ -31,10 +33,7 @@ class ImageDataProcessor {
3133 * @param {number|null } width - The width of the image data processor. (Optional)
3234 * @param {number|null } height - The height of the image data processor. (Optional)
3335 */
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 ) {
3837 if ( mode ) this . setMode ( mode ) ;
3938 if ( width && height ) this . setResolution ( width , height ) ;
4039 }
@@ -151,13 +150,11 @@ class ImageDataProcessor {
151150 * Retrieves the image data from the given bytes by converting each pixel value.
152151 *
153152 * @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 .
155154 */
156155 getImageData ( bytes ) {
157156 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
161158
162159 for ( let row = 0 ; row < this . height ; row ++ ) {
163160 for ( let col = 0 ; col < this . width ; col ++ ) {
@@ -172,6 +169,6 @@ class ImageDataProcessor {
172169 dataContainer [ pixelIndex + 3 ] = 255 ; // Alpha channel (opacity)
173170 }
174171 }
175- return imageData ;
172+ return dataContainer ;
176173 }
177174 }
0 commit comments