Skip to content

Commit 1fc2ae8

Browse files
committed
Merge pull request #77 from andelf/refactor-texture-and-pixelformat
Implement Renderer::read_pixels, Texture::lock, Texture::unlock.
2 parents 03f2ebf + 942615a commit 1fc2ae8

File tree

4 files changed

+288
-67
lines changed

4 files changed

+288
-67
lines changed

src/sdl2/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ pub mod render;
3131
pub mod rwops;
3232
pub mod sdl;
3333
pub mod audio;
34+
pub mod version;

src/sdl2/pixels.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub struct PixelFormat {
135135
pub raw: *ll::SDL_PixelFormat
136136
}
137137

138-
#[deriving(Eq, FromPrimitive)]
138+
#[deriving(Eq, Show, FromPrimitive)]
139139
pub enum PixelFormatFlag {
140140
Unknown = ll::SDL_PIXELFORMAT_UNKNOWN as int,
141141
Index1LSB = ll::SDL_PIXELFORMAT_INDEX1LSB as int,
@@ -174,3 +174,58 @@ pub enum PixelFormatFlag {
174174
UYVY = ll::SDL_PIXELFORMAT_UYVY as int,
175175
YVYU = ll::SDL_PIXELFORMAT_YVYU as int
176176
}
177+
178+
impl PixelFormatFlag {
179+
pub fn byte_size_of_pixels(&self, num_of_pixels: uint) -> uint {
180+
match *self {
181+
RGB332
182+
=> num_of_pixels * 1,
183+
RGB444 | RGB555 | BGR555 | ARGB4444 | RGBA4444 | ABGR4444 |
184+
BGRA4444 | ARGB1555 | RGBA5551 | ABGR1555 | BGRA5551 | RGB565 |
185+
BGR565
186+
=> num_of_pixels * 2,
187+
RGB24 | BGR24
188+
=> num_of_pixels * 3,
189+
RGB888 | RGBX8888 | BGR888 | BGRX8888 | ARGB8888 | RGBA8888 |
190+
ABGR8888 | BGRA8888 | ARGB2101010
191+
=> num_of_pixels * 4,
192+
// YUV formats
193+
// FIXME: rounding error here?
194+
YV12 | IYUV
195+
=> num_of_pixels / 2 * 3,
196+
YUY2 | UYVY | YVYU
197+
=> num_of_pixels * 2,
198+
// Unsupported formats
199+
Index8
200+
=> num_of_pixels * 1,
201+
Unknown | Index1LSB | Index1MSB | Index4LSB | Index4MSB
202+
=> fail!("not supported format: {}", *self),
203+
}
204+
}
205+
206+
pub fn byte_size_per_pixel(&self) -> uint {
207+
match *self {
208+
RGB332
209+
=> 1,
210+
RGB444 | RGB555 | BGR555 | ARGB4444 | RGBA4444 | ABGR4444 |
211+
BGRA4444 | ARGB1555 | RGBA5551 | ABGR1555 | BGRA5551 | RGB565 |
212+
BGR565
213+
=> 2,
214+
RGB24 | BGR24
215+
=> 3,
216+
RGB888 | RGBX8888 | BGR888 | BGRX8888 | ARGB8888 | RGBA8888 |
217+
ABGR8888 | BGRA8888 | ARGB2101010
218+
=> 4,
219+
// YUV formats
220+
YV12 | IYUV
221+
=> 2,
222+
YUY2 | UYVY | YVYU
223+
=> 2,
224+
// Unsupported formats
225+
Index8
226+
=> 1,
227+
Unknown | Index1LSB | Index1MSB | Index4LSB | Index4MSB
228+
=> fail!("not supported format: {}", *self),
229+
}
230+
}
231+
}

0 commit comments

Comments
 (0)