From e7b1d293bc06b3030d64528d3bc1db55b12bd6df Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 10 Mar 2020 22:38:32 +0100 Subject: [PATCH] Fixed Image writing out upside down --- src/graphics/image.rs | 14 +++++++++++--- src/graphics/mod.rs | 4 ---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/graphics/image.rs b/src/graphics/image.rs index 6e228624e..a9fb30a87 100644 --- a/src/graphics/image.rs +++ b/src/graphics/image.rs @@ -213,10 +213,18 @@ impl Image { let reader = gfx.factory.read_mapping(&dl_buffer)?; // intermediary buffer to avoid casting. - // Apparently this also at one point made the screenshot upside-down, - // but no longer? let mut data = Vec::with_capacity(self.width as usize * self.height as usize * 4); - data.extend(reader.into_iter().flatten()); + // Assuming OpenGL backend whose typical readback option (glReadPixels) has origin at bottom left. + // Image formats on the other hand usually deal with top right. + for y in (0..self.height as usize).rev() { + data.extend( + reader + .iter() + .skip(y * self.width as usize) + .take(self.width as usize) + .flatten(), + ); + } Ok(data) } diff --git a/src/graphics/mod.rs b/src/graphics/mod.rs index 2540c1162..e84d1bdec 100644 --- a/src/graphics/mod.rs +++ b/src/graphics/mod.rs @@ -504,9 +504,6 @@ pub fn present(ctx: &mut Context) -> GameResult<()> { /// Take a screenshot by outputting the current render surface /// (screen or selected canvas) to an `Image`. pub fn screenshot(ctx: &mut Context) -> GameResult { - // TODO LATER: This makes the screenshot upside-down form some reason... - // Probably because all our images are upside down, for coordinate reasons! - // How can we fix it? use gfx::memory::Bind; let debug_id = DebugId::get(ctx); @@ -1059,5 +1056,4 @@ mod tests { assert_relative_eq!(real, expected); } } - }