@@ -98,8 +98,8 @@ Deno.test("PNG Encoding - Basic RGB", async () => {
9898
9999 assertEquals ( result , 1 , "PNG encoding should succeed" ) ;
100100
101- // Verify output size is reasonable
102- const outputSize = new Uint32Array ( Module . HEAPU32 . buffer , outputSizePtr >> 2 , 1 ) [ 0 ] ;
101+ // Verify output size is reasonable (fix memory access)
102+ const outputSize = Module . HEAPU32 [ outputSizePtr >> 2 ] ;
103103 assert ( outputSize > 50 , `PNG output should be reasonable size, got ${ outputSize } ` ) ;
104104 assert ( outputSize < 1000 , `PNG output should not be too large, got ${ outputSize } ` ) ;
105105
@@ -123,32 +123,46 @@ Deno.test("PNG Decoding - Valid PNG data", async () => {
123123 // Initialize PNG
124124 assertEquals ( Module . _png_wasm_init ( ) , 1 ) ;
125125
126- // Minimal valid 1x1 RGB PNG (known working)
127- const validPNG = new Uint8Array ( [
128- 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A , // PNG signature
129- 0x00 , 0x00 , 0x00 , 0x0D , // IHDR length
130- 0x49 , 0x48 , 0x44 , 0x52 , // IHDR chunk type
131- 0x00 , 0x00 , 0x00 , 0x01 , // Width: 1
132- 0x00 , 0x00 , 0x00 , 0x01 , // Height: 1
133- 0x08 , 0x02 , 0x00 , 0x00 , 0x00 , // 8-bit RGB
134- 0x90 , 0x77 , 0x53 , 0xDE , // IHDR CRC
135- 0x00 , 0x00 , 0x00 , 0x0C , // IDAT length
136- 0x49 , 0x44 , 0x41 , 0x54 , // IDAT chunk type
137- 0x08 , 0x99 , 0x01 , 0x01 , 0x00 , 0x00 , 0x00 , 0xFF , 0xFF , 0x00 , 0x00 , 0x00 , // Compressed data
138- 0x02 , 0x00 , 0x01 , 0x00 , // IDAT CRC
139- 0x00 , 0x00 , 0x00 , 0x00 , // IEND length
140- 0x49 , 0x45 , 0x4E , 0x44 , // IEND chunk type
141- 0xAE , 0x42 , 0x60 , 0x82 // IEND CRC
142- ] ) ;
126+ // Instead of using hardcoded PNG with potentially invalid compression data,
127+ // create a valid PNG by encoding first, then decoding it
128+ const testImageData = new Uint8Array ( [ 255 , 0 , 0 ] ) ; // 1x1 red pixel (RGB)
129+ const width = 1 , height = 1 , channels = 3 ;
143130
144- // Allocate memory
145- const pngDataPtr = Module . _malloc ( validPNG . length ) ;
131+ // Allocate memory for encoding
132+ const imagePtr = Module . _malloc ( testImageData . length ) ;
146133 const outputBufferPtr = Module . _malloc ( 4 ) ;
147- const widthPtr = Module . _malloc ( 4 ) ;
148- const heightPtr = Module . _malloc ( 4 ) ;
149- const channelsPtr = Module . _malloc ( 4 ) ;
150- const bitDepthPtr = Module . _malloc ( 4 ) ;
151- const colorTypePtr = Module . _malloc ( 4 ) ;
134+ const outputSizePtr = Module . _malloc ( 4 ) ;
135+
136+ try {
137+ // Copy test data
138+ const imageView = new Uint8Array ( Module . HEAPU8 . buffer , imagePtr , testImageData . length ) ;
139+ imageView . set ( testImageData ) ;
140+
141+ // Encode to create valid PNG
142+ const encodeResult = Module . _png_wasm_encode_buffer (
143+ imagePtr , width , height , channels ,
144+ outputBufferPtr , outputSizePtr
145+ ) ;
146+
147+ assertEquals ( encodeResult , 1 , "PNG encoding should succeed" ) ;
148+
149+ // Get encoded PNG
150+ const pngSize = Module . HEAPU32 [ outputSizePtr >> 2 ] ;
151+ const encodedPngDataPtr = Module . HEAPU32 [ outputBufferPtr >> 2 ] ;
152+ const validPNG = new Uint8Array ( pngSize ) ;
153+ validPNG . set ( Module . HEAPU8 . subarray ( encodedPngDataPtr , encodedPngDataPtr + pngSize ) ) ;
154+
155+ // Free encoding memory
156+ Module . _free ( encodedPngDataPtr ) ;
157+
158+ // Now allocate memory for decoding
159+ const pngDataPtr = Module . _malloc ( validPNG . length ) ;
160+ const decodeOutputBufferPtr = Module . _malloc ( 4 ) ;
161+ const widthPtr = Module . _malloc ( 4 ) ;
162+ const heightPtr = Module . _malloc ( 4 ) ;
163+ const channelsPtr = Module . _malloc ( 4 ) ;
164+ const bitDepthPtr = Module . _malloc ( 4 ) ;
165+ const colorTypePtr = Module . _malloc ( 4 ) ;
152166
153167 try {
154168 // Copy PNG data to WASM memory
@@ -157,34 +171,41 @@ Deno.test("PNG Decoding - Valid PNG data", async () => {
157171
158172 // Decode PNG
159173 const result = Module . _png_wasm_decode_buffer (
160- pngDataPtr , validPNG . length , outputBufferPtr ,
174+ pngDataPtr , validPNG . length , decodeOutputBufferPtr ,
161175 widthPtr , heightPtr , channelsPtr , bitDepthPtr , colorTypePtr
162176 ) ;
163177
164178 if ( result === 1 ) {
165- // Success case
166- const width = new Uint32Array ( Module . HEAPU32 . buffer , widthPtr >> 2 , 1 ) [ 0 ] ;
167- const height = new Uint32Array ( Module . HEAPU32 . buffer , heightPtr >> 2 , 1 ) [ 0 ] ;
168- const channels = new Uint32Array ( Module . HEAPU32 . buffer , channelsPtr >> 2 , 1 ) [ 0 ] ;
169-
170- assertEquals ( width , 1 , "Decoded width should be 1" ) ;
171- assertEquals ( height , 1 , "Decoded height should be 1" ) ;
172- assertEquals ( channels , 3 , "Decoded channels should be 3 (RGB)" ) ;
179+ // Success case - fix memory access patterns
180+ const decodedWidth = Module . HEAPU32 [ widthPtr >> 2 ] ;
181+ const decodedHeight = Module . HEAPU32 [ heightPtr >> 2 ] ;
182+ const decodedChannels = Module . HEAPU32 [ channelsPtr >> 2 ] ;
183+
184+ assertEquals ( decodedWidth , 1 , "Decoded width should be 1" ) ;
185+ assertEquals ( decodedHeight , 1 , "Decoded height should be 1" ) ;
186+ assertEquals ( decodedChannels , 3 , "Decoded channels should be 3 (RGB)" ) ;
173187 } else {
174188 // If decoding fails, we need to investigate the IDAT compression issue
175189 throw new Error ( "PNG decoding failed - IDAT compression issue needs to be resolved" ) ;
176190 }
177191
178192 } finally {
179- // Cleanup
193+ // Cleanup decoding memory
180194 Module . _free ( pngDataPtr ) ;
181- Module . _free ( outputBufferPtr ) ;
195+ Module . _free ( decodeOutputBufferPtr ) ;
182196 Module . _free ( widthPtr ) ;
183197 Module . _free ( heightPtr ) ;
184198 Module . _free ( channelsPtr ) ;
185199 Module . _free ( bitDepthPtr ) ;
186200 Module . _free ( colorTypePtr ) ;
187201 }
202+
203+ } finally {
204+ // Cleanup encoding memory
205+ Module . _free ( imagePtr ) ;
206+ Module . _free ( outputBufferPtr ) ;
207+ Module . _free ( outputSizePtr ) ;
208+ }
188209} ) ;
189210
190211Deno . test ( "Performance - Memory allocation speed" , async ( ) => {
0 commit comments