@@ -135,7 +135,7 @@ pub struct PixelFormat {
135135 pub raw : * ll:: SDL_PixelFormat
136136}
137137
138- #[ deriving( Eq , FromPrimitive ) ]
138+ #[ deriving( Eq , Show , FromPrimitive ) ]
139139pub 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