@@ -298,3 +298,228 @@ fn image_to_rgba_pixels(image: &Image, flip_x: bool, flip_y: bool, rect: Rect) -
298
298
_ => None ,
299
299
}
300
300
}
301
+
302
+ #[ cfg( feature = "custom_cursor" ) ]
303
+ #[ cfg( test) ]
304
+ mod tests {
305
+ use bevy_asset:: RenderAssetUsages ;
306
+ use bevy_image:: Image ;
307
+ use bevy_math:: Rect ;
308
+ use bevy_math:: Vec2 ;
309
+ use wgpu_types:: { Extent3d , TextureDimension } ;
310
+
311
+ use super :: * ;
312
+
313
+ fn create_image_rgba8 ( data : & [ u8 ] ) -> Image {
314
+ Image :: new (
315
+ Extent3d {
316
+ width : 3 ,
317
+ height : 3 ,
318
+ depth_or_array_layers : 1 ,
319
+ } ,
320
+ TextureDimension :: D2 ,
321
+ data. to_vec ( ) ,
322
+ TextureFormat :: Rgba8UnormSrgb ,
323
+ RenderAssetUsages :: default ( ) ,
324
+ )
325
+ }
326
+
327
+ macro_rules! test_image_to_rgba_pixels {
328
+ ( $name: ident, $flip_x: expr, $flip_y: expr, $rect: expr, $expected: expr) => {
329
+ #[ test]
330
+ fn $name( ) {
331
+ let image_data: & [ u8 ] = & [
332
+ // Row 1: Red, Green, Blue
333
+ 255 , 0 , 0 , 255 , // Red
334
+ 0 , 255 , 0 , 255 , // Green
335
+ 0 , 0 , 255 , 255 , // Blue
336
+ // Row 2: Yellow, Cyan, Magenta
337
+ 255 , 255 , 0 , 255 , // Yellow
338
+ 0 , 255 , 255 , 255 , // Cyan
339
+ 255 , 0 , 255 , 255 , // Magenta
340
+ // Row 3: White, Gray, Black
341
+ 255 , 255 , 255 , 255 , // White
342
+ 128 , 128 , 128 , 255 , // Gray
343
+ 0 , 0 , 0 , 255 , // Black
344
+ ] ;
345
+
346
+ // RGBA8 test
347
+ {
348
+ let image = create_image_rgba8( image_data) ;
349
+ let rect = $rect;
350
+ let result = image_to_rgba_pixels( & image, $flip_x, $flip_y, rect) ;
351
+ assert_eq!( result, Some ( $expected. to_vec( ) ) ) ;
352
+ }
353
+ }
354
+ } ;
355
+ }
356
+
357
+ test_image_to_rgba_pixels ! (
358
+ no_flip_full_image,
359
+ false ,
360
+ false ,
361
+ Rect {
362
+ min: Vec2 :: ZERO ,
363
+ max: Vec2 :: new( 3.0 , 3.0 )
364
+ } ,
365
+ [
366
+ // Row 1: Red, Green, Blue
367
+ 255 , 0 , 0 , 255 , // Red
368
+ 0 , 255 , 0 , 255 , // Green
369
+ 0 , 0 , 255 , 255 , // Blue
370
+ // Row 2: Yellow, Cyan, Magenta
371
+ 255 , 255 , 0 , 255 , // Yellow
372
+ 0 , 255 , 255 , 255 , // Cyan
373
+ 255 , 0 , 255 , 255 , // Magenta
374
+ // Row 3: White, Gray, Black
375
+ 255 , 255 , 255 , 255 , // White
376
+ 128 , 128 , 128 , 255 , // Gray
377
+ 0 , 0 , 0 , 255 , // Black
378
+ ]
379
+ ) ;
380
+
381
+ test_image_to_rgba_pixels ! (
382
+ flip_x_full_image,
383
+ true ,
384
+ false ,
385
+ Rect {
386
+ min: Vec2 :: ZERO ,
387
+ max: Vec2 :: new( 3.0 , 3.0 )
388
+ } ,
389
+ [
390
+ // Row 1 flipped: Blue, Green, Red
391
+ 0 , 0 , 255 , 255 , // Blue
392
+ 0 , 255 , 0 , 255 , // Green
393
+ 255 , 0 , 0 , 255 , // Red
394
+ // Row 2 flipped: Magenta, Cyan, Yellow
395
+ 255 , 0 , 255 , 255 , // Magenta
396
+ 0 , 255 , 255 , 255 , // Cyan
397
+ 255 , 255 , 0 , 255 , // Yellow
398
+ // Row 3 flipped: Black, Gray, White
399
+ 0 , 0 , 0 , 255 , // Black
400
+ 128 , 128 , 128 , 255 , // Gray
401
+ 255 , 255 , 255 , 255 , // White
402
+ ]
403
+ ) ;
404
+
405
+ test_image_to_rgba_pixels ! (
406
+ flip_y_full_image,
407
+ false ,
408
+ true ,
409
+ Rect {
410
+ min: Vec2 :: ZERO ,
411
+ max: Vec2 :: new( 3.0 , 3.0 )
412
+ } ,
413
+ [
414
+ // Row 3: White, Gray, Black
415
+ 255 , 255 , 255 , 255 , // White
416
+ 128 , 128 , 128 , 255 , // Gray
417
+ 0 , 0 , 0 , 255 , // Black
418
+ // Row 2: Yellow, Cyan, Magenta
419
+ 255 , 255 , 0 , 255 , // Yellow
420
+ 0 , 255 , 255 , 255 , // Cyan
421
+ 255 , 0 , 255 , 255 , // Magenta
422
+ // Row 1: Red, Green, Blue
423
+ 255 , 0 , 0 , 255 , // Red
424
+ 0 , 255 , 0 , 255 , // Green
425
+ 0 , 0 , 255 , 255 , // Blue
426
+ ]
427
+ ) ;
428
+
429
+ test_image_to_rgba_pixels ! (
430
+ flip_both_full_image,
431
+ true ,
432
+ true ,
433
+ Rect {
434
+ min: Vec2 :: ZERO ,
435
+ max: Vec2 :: new( 3.0 , 3.0 )
436
+ } ,
437
+ [
438
+ // Row 3 flipped: Black, Gray, White
439
+ 0 , 0 , 0 , 255 , // Black
440
+ 128 , 128 , 128 , 255 , // Gray
441
+ 255 , 255 , 255 , 255 , // White
442
+ // Row 2 flipped: Magenta, Cyan, Yellow
443
+ 255 , 0 , 255 , 255 , // Magenta
444
+ 0 , 255 , 255 , 255 , // Cyan
445
+ 255 , 255 , 0 , 255 , // Yellow
446
+ // Row 1 flipped: Blue, Green, Red
447
+ 0 , 0 , 255 , 255 , // Blue
448
+ 0 , 255 , 0 , 255 , // Green
449
+ 255 , 0 , 0 , 255 , // Red
450
+ ]
451
+ ) ;
452
+
453
+ test_image_to_rgba_pixels ! (
454
+ no_flip_rect,
455
+ false ,
456
+ false ,
457
+ Rect {
458
+ min: Vec2 :: new( 1.0 , 1.0 ) ,
459
+ max: Vec2 :: new( 3.0 , 3.0 )
460
+ } ,
461
+ [
462
+ // Only includes part of the original image (sub-rectangle)
463
+ // Row 2, columns 2-3: Cyan, Magenta
464
+ 0 , 255 , 255 , 255 , // Cyan
465
+ 255 , 0 , 255 , 255 , // Magenta
466
+ // Row 3, columns 2-3: Gray, Black
467
+ 128 , 128 , 128 , 255 , // Gray
468
+ 0 , 0 , 0 , 255 , // Black
469
+ ]
470
+ ) ;
471
+
472
+ test_image_to_rgba_pixels ! (
473
+ flip_x_rect,
474
+ true ,
475
+ false ,
476
+ Rect {
477
+ min: Vec2 :: new( 1.0 , 1.0 ) ,
478
+ max: Vec2 :: new( 3.0 , 3.0 )
479
+ } ,
480
+ [
481
+ // Row 2 flipped: Magenta, Cyan
482
+ 255 , 0 , 255 , 255 , // Magenta
483
+ 0 , 255 , 255 , 255 , // Cyan
484
+ // Row 3 flipped: Black, Gray
485
+ 0 , 0 , 0 , 255 , // Black
486
+ 128 , 128 , 128 , 255 , // Gray
487
+ ]
488
+ ) ;
489
+
490
+ test_image_to_rgba_pixels ! (
491
+ flip_y_rect,
492
+ false ,
493
+ true ,
494
+ Rect {
495
+ min: Vec2 :: new( 1.0 , 1.0 ) ,
496
+ max: Vec2 :: new( 3.0 , 3.0 )
497
+ } ,
498
+ [
499
+ // Row 3 first: Gray, Black
500
+ 128 , 128 , 128 , 255 , // Gray
501
+ 0 , 0 , 0 , 255 , // Black
502
+ // Row 2 second: Cyan, Magenta
503
+ 0 , 255 , 255 , 255 , // Cyan
504
+ 255 , 0 , 255 , 255 , // Magenta
505
+ ]
506
+ ) ;
507
+
508
+ test_image_to_rgba_pixels ! (
509
+ flip_both_rect,
510
+ true ,
511
+ true ,
512
+ Rect {
513
+ min: Vec2 :: new( 1.0 , 1.0 ) ,
514
+ max: Vec2 :: new( 3.0 , 3.0 )
515
+ } ,
516
+ [
517
+ // Row 3 flipped: Black, Gray
518
+ 0 , 0 , 0 , 255 , // Black
519
+ 128 , 128 , 128 , 255 , // Gray
520
+ // Row 2 flipped: Magenta, Cyan
521
+ 255 , 0 , 255 , 255 , // Magenta
522
+ 0 , 255 , 255 , 255 , // Cyan
523
+ ]
524
+ ) ;
525
+ }
0 commit comments