Skip to content

Commit 0c37d28

Browse files
authored
Merge pull request #543 from icefoxen/fix-pixel-format
Added a PixelFormatEnum::From<PixelFormat> implementation
2 parents 62e08b3 + 3a2cda8 commit 0c37d28

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/sdl2/pixels.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ impl PixelFormatEnum {
324324
}
325325
}
326326

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+
327339
impl FromPrimitive for PixelFormatEnum {
328340
fn from_i64(n: i64) -> Option<PixelFormatEnum> {
329341
use self::PixelFormatEnum::*;
@@ -371,3 +383,46 @@ impl FromPrimitive for PixelFormatEnum {
371383

372384
fn from_u64(n: u64) -> Option<PixelFormatEnum> { FromPrimitive::from_i64(n as i64) }
373385
}
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+
}

src/sdl2/surface.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ impl SurfaceRef {
224224
}
225225
}
226226

227+
pub fn pixel_format_enum(&self) -> pixels::PixelFormatEnum {
228+
pixels::PixelFormatEnum::from(self.pixel_format())
229+
}
230+
227231
/// Locks a surface so that the pixels can be directly accessed safely.
228232
pub fn with_lock<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
229233
unsafe {

0 commit comments

Comments
 (0)