@@ -11,6 +11,10 @@ pub enum PixelFormat {
1111 Bgr ,
1212 Rgba16 ,
1313 Rgb16 ,
14+ Rgba4444 ,
15+ Bgra16 ,
16+ Bgr16 ,
17+ Bgra4444 ,
1418 RgbaClut8 ,
1519 RgbxClut8 ,
1620 RgbClut8 ,
@@ -36,6 +40,10 @@ impl PixelFormat {
3640 PixelFormat :: Bgr => 24 ,
3741 PixelFormat :: Rgba16 => 16 ,
3842 PixelFormat :: Rgb16 => 16 ,
43+ PixelFormat :: Rgba4444 => 16 ,
44+ PixelFormat :: Bgra16 => 16 ,
45+ PixelFormat :: Bgr16 => 16 ,
46+ PixelFormat :: Bgra4444 => 16 ,
3947 PixelFormat :: RgbaClut8 => 8 ,
4048 PixelFormat :: RgbxClut8 => 8 ,
4149 PixelFormat :: RgbClut8 => 8 ,
@@ -75,6 +83,10 @@ impl Display for PixelFormat {
7583 Self :: Bgr => write ! ( f, "BGR" ) ,
7684 Self :: Rgba16 => write ! ( f, "RGBA5551" ) ,
7785 Self :: Rgb16 => write ! ( f, "RGB565" ) ,
86+ Self :: Rgba4444 => write ! ( f, "RGBA4444" ) ,
87+ Self :: Bgra16 => write ! ( f, "BGRA5551" ) ,
88+ Self :: Bgr16 => write ! ( f, "BGR565" ) ,
89+ Self :: Bgra4444 => write ! ( f, "BGRA4444" ) ,
7890 Self :: RgbaClut8 => write ! ( f, "RGBA clut8" ) ,
7991 Self :: RgbxClut8 => write ! ( f, "RGBX clut8" ) ,
8092 Self :: RgbClut8 => write ! ( f, "RGB clut8" ) ,
@@ -107,9 +119,14 @@ pub struct Frame {
107119 pub pixels : Box < [ Pixel ] >
108120}
109121
122+ fn bits_4_to_8 ( x : u8 ) -> u8 {
123+ x << 4 | ( x & 0xF )
124+ }
125+
110126fn bits_5_to_8 ( x : u8 ) -> u8 {
111127 x << 3 | ( x & 1 ) << 2 | ( x & 1 ) << 1 | ( x & 1 )
112128}
129+
113130fn bits_6_to_8 ( x : u8 ) -> u8 {
114131 x << 2 | ( x & 1 ) << 1 | ( x & 1 )
115132}
@@ -147,6 +164,51 @@ impl Frame {
147164 }
148165 }
149166
167+ pub fn from_bgra16 ( width : u32 , height : u32 , buf : & [ u8 ] ) -> Self {
168+ let needed_size = width * height * 2 ;
169+ assert ! ( buf. len( ) as u32 >= needed_size) ;
170+ let buf = & buf[ 0 ..needed_size as usize ] ;
171+ Self {
172+ width, height, og_fmt : PixelFormat :: Bgra16 ,
173+ pixels : buf. chunks_exact ( 2 ) . map ( |x| Pixel {
174+ r : bits_5_to_8 ( x[ 1 ] >> 3 ) ,
175+ g : bits_5_to_8 ( x[ 0 ] >> 5 | x[ 1 ] << 3 ) ,
176+ b : bits_5_to_8 ( x[ 0 ] ) ,
177+ a : if x[ 1 ] & 0x80 != 0 { 0xFF } else { 0 }
178+ } ) . collect ( )
179+ }
180+ }
181+
182+ pub fn from_rgba4444 ( width : u32 , height : u32 , buf : & [ u8 ] ) -> Self {
183+ let needed_size = width * height * 2 ;
184+ assert ! ( buf. len( ) as u32 >= needed_size) ;
185+ let buf = & buf[ 0 ..needed_size as usize ] ;
186+ Self {
187+ width, height, og_fmt : PixelFormat :: Rgba4444 ,
188+ pixels : buf. chunks_exact ( 2 ) . map ( |x| Pixel {
189+ r : bits_4_to_8 ( x[ 0 ] ) ,
190+ g : bits_4_to_8 ( x[ 0 ] >> 4 ) ,
191+ b : bits_4_to_8 ( x[ 1 ] ) ,
192+ a : bits_4_to_8 ( x[ 1 ] >> 4 )
193+ } ) . collect ( )
194+ }
195+ }
196+
197+ pub fn from_bgra4444 ( width : u32 , height : u32 , buf : & [ u8 ] ) -> Self {
198+ let needed_size = width * height * 2 ;
199+ assert ! ( buf. len( ) as u32 >= needed_size) ;
200+ let buf = & buf[ 0 ..needed_size as usize ] ;
201+ Self {
202+ width, height, og_fmt : PixelFormat :: Bgra4444 ,
203+ pixels : buf. chunks_exact ( 2 ) . map ( |x| Pixel {
204+ r : bits_4_to_8 ( x[ 1 ] ) ,
205+ g : bits_4_to_8 ( x[ 0 ] >> 4 ) ,
206+ b : bits_4_to_8 ( x[ 0 ] ) ,
207+ a : bits_4_to_8 ( x[ 1 ] >> 4 )
208+ } ) . collect ( )
209+ }
210+ }
211+
150212 pub fn from_rgb16 ( width : u32 , height : u32 , buf : & [ u8 ] ) -> Self {
151213 let needed_size = width * height * 2 ;
152214 assert ! ( buf. len( ) as u32 >= needed_size) ;
@@ -162,6 +224,21 @@ impl Frame {
162224 }
163225 }
164226
227+ pub fn from_bgr16 ( width : u32 , height : u32 , buf : & [ u8 ] ) -> Self {
228+ let needed_size = width * height * 2 ;
229+ assert ! ( buf. len( ) as u32 >= needed_size) ;
230+ let buf = & buf[ 0 ..needed_size as usize ] ;
231+ Self {
232+ width, height, og_fmt : PixelFormat :: Bgr16 ,
233+ pixels : buf. chunks_exact ( 2 ) . map ( |x| Pixel {
234+ r : bits_5_to_8 ( x[ 1 ] >> 3 ) ,
235+ g : bits_6_to_8 ( x[ 0 ] >> 5 | x[ 1 ] << 3 ) ,
236+ b : bits_5_to_8 ( x[ 0 ] ) ,
237+ a : 0xFF
238+ } ) . collect ( )
239+ }
240+ }
241+
165242 pub fn from_rgba_clut8 ( width : u32 , height : u32 , clut : & [ u8 ] , buf : & [ u8 ] ) -> Self {
166243 let needed_size = width * height;
167244 assert ! ( buf. len( ) as u32 >= needed_size) ;
0 commit comments