@@ -25,9 +25,8 @@ let config = {
25
25
26
26
const imageWidth = 320 ; // Adjust this value based on your bitmap width
27
27
const imageHeight = 240 ; // Adjust this value based on your bitmap height
28
- const bytesPerPixel = 2 ; // Adjust this value based on your bitmap format
29
- // const mode = 'RGB565'; // Adjust this value based on your bitmap format
30
- const mode = 'GRAYSCALE' ; // Adjust this value based on your bitmap format
28
+ const mode = 'RGB565' ; // Adjust this value based on your bitmap format
29
+ // const mode = 'GRAYSCALE'; // Adjust this value based on your bitmap format
31
30
const totalBytes = imageWidth * imageHeight * config [ mode ] . bytesPerPixel ;
32
31
33
32
// Set the buffer size to the total bytes. This allows to read the entire bitmap in one go.
@@ -167,22 +166,21 @@ function renderBitmap(bytes, width, height) {
167
166
canvas . height = height ;
168
167
const bytesPerPixel = config [ mode ] . bytesPerPixel ;
169
168
const BYTES_PER_ROW = width * bytesPerPixel ;
170
- const BYTES_PER_COL = height * bytesPerPixel ;
171
169
172
170
const imageData = ctx . createImageData ( canvas . width , canvas . height ) ;
173
- const data = imageData . data ;
171
+ const dataContainer = imageData . data ;
174
172
175
- for ( let row = 0 ; row < BYTES_PER_ROW ; row ++ ) {
176
- for ( let col = 0 ; col < BYTES_PER_COL ; col ++ ) {
177
- const dataIndex = ( row * BYTES_PER_ROW ) + ( col * bytesPerPixel ) ;
178
- const pixelValue = getPixelValue ( bytes , dataIndex , bytesPerPixel ) ;
173
+ for ( let row = 0 ; row < height ; row ++ ) {
174
+ for ( let col = 0 ; col < width ; col ++ ) {
175
+ const sourceDataIndex = ( row * BYTES_PER_ROW ) + ( col * bytesPerPixel ) ;
176
+ const pixelValue = getPixelValue ( bytes , sourceDataIndex , bytesPerPixel ) ;
179
177
const [ r , g , b ] = config [ mode ] . convert ( pixelValue ) ;
180
178
181
- const idx = ( ( row * width ) + col ) * 4 ;
182
- data [ idx ] = r ; // Red channel
183
- data [ idx + 1 ] = g ; // Green channel
184
- data [ idx + 2 ] = b ; // Blue channel
185
- data [ idx + 3 ] = 255 ; // Alpha channel (opacity)
179
+ const pixelIndex = ( ( row * width ) + col ) * 4 ;
180
+ dataContainer [ pixelIndex ] = r ; // Red channel
181
+ dataContainer [ pixelIndex + 1 ] = g ; // Green channel
182
+ dataContainer [ pixelIndex + 2 ] = b ; // Blue channel
183
+ dataContainer [ pixelIndex + 3 ] = 255 ; // Alpha channel (opacity)
186
184
}
187
185
}
188
186
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
0 commit comments