@@ -324,6 +324,18 @@ impl PixelFormatEnum {
324
324
}
325
325
}
326
326
327
+ impl From < PixelFormat > for PixelFormatEnum {
328
+ fn from ( pf : PixelFormat ) -> PixelFormatEnum {
329
+ unsafe {
330
+ let ref sdl_pf = * pf. raw ;
331
+ match PixelFormatEnum :: from_u64 ( sdl_pf. format as u64 ) {
332
+ Some ( pfe) => pfe,
333
+ None => panic ! ( "Unknown pixel format: {:?}" , sdl_pf. format)
334
+ }
335
+ }
336
+ }
337
+ }
338
+
327
339
impl FromPrimitive for PixelFormatEnum {
328
340
fn from_i64 ( n : i64 ) -> Option < PixelFormatEnum > {
329
341
use self :: PixelFormatEnum :: * ;
@@ -371,3 +383,46 @@ impl FromPrimitive for PixelFormatEnum {
371
383
372
384
fn from_u64 ( n : u64 ) -> Option < PixelFormatEnum > { FromPrimitive :: from_i64 ( n as i64 ) }
373
385
}
386
+
387
+
388
+ // Just test a round-trip conversion from PixelFormat to
389
+ // PixelFormatEnum and back.
390
+ #[ test]
391
+ fn test_pixel_format_enum ( ) {
392
+ let pixel_formats = vec ! [
393
+ PixelFormatEnum :: RGB332 ,
394
+ PixelFormatEnum :: RGB444 , PixelFormatEnum :: RGB555 ,
395
+ PixelFormatEnum :: BGR555 , PixelFormatEnum :: ARGB4444 ,
396
+ PixelFormatEnum :: RGBA4444 , PixelFormatEnum :: ABGR4444 ,
397
+ PixelFormatEnum :: BGRA4444 , PixelFormatEnum :: ARGB1555 ,
398
+ PixelFormatEnum :: RGBA5551 , PixelFormatEnum :: ABGR1555 ,
399
+ PixelFormatEnum :: BGRA5551 , PixelFormatEnum :: RGB565 ,
400
+ PixelFormatEnum :: BGR565 ,
401
+ PixelFormatEnum :: RGB24 , PixelFormatEnum :: BGR24 ,
402
+ PixelFormatEnum :: RGB888 , PixelFormatEnum :: RGBX8888 ,
403
+ PixelFormatEnum :: BGR888 , PixelFormatEnum :: BGRX8888 ,
404
+ PixelFormatEnum :: ARGB8888 , PixelFormatEnum :: RGBA8888 ,
405
+ PixelFormatEnum :: ABGR8888 , PixelFormatEnum :: BGRA8888 ,
406
+ PixelFormatEnum :: ARGB2101010 ,
407
+ PixelFormatEnum :: YV12 , PixelFormatEnum :: IYUV ,
408
+ PixelFormatEnum :: YUY2 , PixelFormatEnum :: UYVY ,
409
+ PixelFormatEnum :: YVYU ,
410
+ PixelFormatEnum :: Index8 ,
411
+ // These don't seem to be supported;
412
+ // the round-trip
413
+ //PixelFormatEnum::Unknown, PixelFormatEnum::Index1LSB,
414
+ //PixelFormatEnum::Index1MSB, PixelFormatEnum::Index4LSB,
415
+ //PixelFormatEnum::Index4MSB
416
+ ] ;
417
+
418
+
419
+ let _sdl_context = :: sdl:: init ( ) . unwrap ( ) ;
420
+ for format in pixel_formats {
421
+ // If we don't support making a surface of a specific format,
422
+ // that's fine, just keep going the best we can.
423
+ if let Ok ( surf) = super :: surface:: Surface :: new ( 1 , 1 , format) {
424
+ let surf_format = surf. pixel_format ( ) ;
425
+ assert_eq ! ( PixelFormatEnum :: from( surf_format) , format) ;
426
+ }
427
+ }
428
+ }
0 commit comments