@@ -78,11 +78,11 @@ <h2>Binary Animation Player</h2>
7878
7979 let animationTimer = null ;
8080 let currentBuffer = null ;
81- let meta = { w : 0 , h : 0 , frames : 0 , bpp : 0 } ;
81+ let meta = { headerSize : 0 , w : 0 , h : 0 , frames : 0 , bpp : 0 } ;
8282
8383 speedSlider . oninput = ( ) => {
8484 fpsVal . innerText = speedSlider . value ;
85- if ( currentBuffer ) startAnimation ( ) ; // Restart with new speed
85+ if ( currentBuffer ) startAnimation ( ) ;
8686 } ;
8787
8888 fileInput . onchange = async ( e ) => {
@@ -92,22 +92,34 @@ <h2>Binary Animation Player</h2>
9292 const buffer = await file . arrayBuffer ( ) ;
9393 const view = new DataView ( buffer ) ;
9494
95- // Header Parsing
96- const w = view . getUint8 ( 0 ) ;
97- const h = view . getUint8 ( 1 ) ;
98- const frames = view . getUint8 ( 2 ) ;
99- const reserved = view . getUint8 ( 3 ) ;
95+ const is332 = file . name . endsWith ( '.332' ) ;
96+ const is565 = file . name . endsWith ( '.565' ) ;
10097
98+ const isDeprecatedFormat = file . name . endsWith ( '.332' ) || file . name . endsWith ( '.565' ) ;
99+ const filesignature = String . fromCharCode (
100+ view . getUint8 ( 0 ) ,
101+ view . getUint8 ( 1 )
102+ ) ;
103+ const filetype = String . fromCharCode (
104+ view . getUint8 ( 2 ) ,
105+ view . getUint8 ( 3 )
106+ ) ;
107+
108+ const headerSize = isDeprecatedFormat ? 3 : 8 ;
109+ const w = isDeprecatedFormat ? view . getUint8 ( 0 ) : view . getUint16 ( 4 , true ) ;
110+ const h = isDeprecatedFormat ? view . getUint8 ( 1 ) : view . getUint16 ( 6 , true ) ;
101111 const pixelsPerFrame = w * h ;
102- const is565 = file . name . endsWith ( '.565' ) ;
103- const bpp = is565 ? 2 : 1 ;
112+ const bpp = is565 || filetype === 'b2' ? 2 : 1 ;
113+ const totalDataBytes = buffer . byteLength - headerSize ;
114+ const bytesPerFrame = pixelsPerFrame * bpp ;
115+ const frames = Math . floor ( totalDataBytes / bytesPerFrame ) ;
104116
105- meta = { w, h, frames, bpp, view } ;
117+ meta = { headerSize , w, h, frames, bpp, view } ;
106118 currentBuffer = view ;
107119
108120 status . innerHTML = `
109121 RES: ${ w } x${ h } | FRAMES: ${ frames } <br>
110- MODE: ${ bpp === 1 ? '8-bit RGB332' : '16-bit RGB565' }
122+ MODE: ${ isDeprecatedFormat ? 'Deprecated' : '' } ${ bpp === 1 ? '8-bit RGB332' : '16-bit RGB565' }
111123 ` ;
112124
113125 canvas . width = w ;
@@ -131,11 +143,11 @@ <h2>Binary Animation Player</h2>
131143 }
132144
133145 function renderFrame ( idx ) {
134- const { w, h, frames, bpp, view } = meta ;
146+ const { headerSize , w, h, frames, bpp, view } = meta ;
135147 const imageData = ctx . createImageData ( w , h ) ;
136148 const pixelsPerFrame = w * h ;
137- idx = idx % frames ; // Ensure frame index wraps around
138- const frameOffset = 3 + ( idx * pixelsPerFrame * bpp ) ;
149+ idx = idx % frames ;
150+ const frameOffset = headerSize + ( idx * pixelsPerFrame * bpp ) ;
139151
140152 for ( let i = 0 ; i < pixelsPerFrame ; i ++ ) {
141153 let r , g , b ;
@@ -152,7 +164,6 @@ <h2>Binary Animation Player</h2>
152164 b |= b << 2 ; // extend 0-3 range to 0-15
153165 b |= b << 4 ;
154166 } else {
155- // 16-bit RGB565 (Little Endian assumed)
156167 const word = view . getUint8 ( frameOffset + ( i * 2 ) ) << 8
157168 | view . getUint8 ( frameOffset + ( i * 2 ) + 1 ) ;
158169
0 commit comments