1+ import fs from "fs" ;
12import path from "path" ;
23
34import { checkImage , client , encodeImage , decodeImage } from "./setup" ;
@@ -23,4 +24,76 @@ describe("Image Bitmap", () => {
2324 const image = encodeImage ( result ) ;
2425 checkImage ( image , "snapshots/ref.png" ) ;
2526 } ) ;
27+ // The following tests exercise the React Native ArrayBuffer/TypedArray
28+ // overload of createImageBitmap, which is not part of the standard web API.
29+ it ( "createImageBitmap from ArrayBuffer" , async ( ) => {
30+ if ( client . OS === "web" ) {
31+ return ;
32+ }
33+ const pngBytes = Array . from (
34+ fs . readFileSync ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ,
35+ ) ;
36+ const expected = decodeImage ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ;
37+ const result = await client . eval (
38+ async ( { pngData } ) => {
39+ const bytes = new Uint8Array ( pngData ) ;
40+ const bmp = await createImageBitmap ( bytes . buffer ) ;
41+ return { width : bmp . width , height : bmp . height } ;
42+ } ,
43+ { pngData : pngBytes } ,
44+ ) ;
45+ expect ( result . width ) . toBe ( expected . width ) ;
46+ expect ( result . height ) . toBe ( expected . height ) ;
47+ } ) ;
48+ it ( "createImageBitmap from Uint8Array" , async ( ) => {
49+ if ( client . OS === "web" ) {
50+ return ;
51+ }
52+ const pngBytes = Array . from (
53+ fs . readFileSync ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ,
54+ ) ;
55+ const expected = decodeImage ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ;
56+ const result = await client . eval (
57+ async ( { pngData } ) => {
58+ const bytes = new Uint8Array ( pngData ) ;
59+ const bmp = await createImageBitmap ( bytes ) ;
60+ return { width : bmp . width , height : bmp . height } ;
61+ } ,
62+ { pngData : pngBytes } ,
63+ ) ;
64+ expect ( result . width ) . toBe ( expected . width ) ;
65+ expect ( result . height ) . toBe ( expected . height ) ;
66+ } ) ;
67+ it ( "createImageBitmap from Uint8Array subarray (byteOffset/byteLength)" , async ( ) => {
68+ if ( client . OS === "web" ) {
69+ return ;
70+ }
71+ const pngBytes = Array . from (
72+ fs . readFileSync ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ,
73+ ) ;
74+ const expected = decodeImage ( path . join ( __dirname , "./assets/Di-3d.png" ) ) ;
75+ const result = await client . eval (
76+ async ( { pngData } ) => {
77+ // Embed PNG bytes at an offset within a larger buffer
78+ const padding = 128 ;
79+ const totalLength = padding + pngData . length + padding ;
80+ const largeBuffer = new ArrayBuffer ( totalLength ) ;
81+ const fullView = new Uint8Array ( largeBuffer ) ;
82+ // Fill with garbage bytes
83+ fullView . fill ( 0xff ) ;
84+ // Copy PNG bytes into the middle
85+ const pngView = new Uint8Array ( largeBuffer , padding , pngData . length ) ;
86+ for ( let i = 0 ; i < pngData . length ; i ++ ) {
87+ pngView [ i ] = pngData [ i ] ;
88+ }
89+ // createImageBitmap must respect byteOffset/byteLength of the view,
90+ // not use the full underlying ArrayBuffer (which has garbage padding)
91+ const bmp = await createImageBitmap ( pngView ) ;
92+ return { width : bmp . width , height : bmp . height } ;
93+ } ,
94+ { pngData : pngBytes } ,
95+ ) ;
96+ expect ( result . width ) . toBe ( expected . width ) ;
97+ expect ( result . height ) . toBe ( expected . height ) ;
98+ } ) ;
2699} ) ;
0 commit comments