1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 
13 namespace Antmicro.Renode.Backends.Display
14 {
15     public enum PixelFormat
16     {
17         A4,
18         L4,
19         A8,
20         L8,
21         AL44,
22         AL88,
23         RGB565,
24         BGR565,
25         BGR888,
26         RGB888,
27         ARGB1555,
28         ARGB4444,
29         RGBA4444,
30         ABGR4444,
31         BGRA4444,
32         XRGB4444,
33         RGBX4444,
34         XBGR4444,
35         BGRX4444,
36         BGRA8888,
37         RGBA8888,
38         ABGR8888,
39         ARGB8888,
40         BGRX8888,
41         RGBX8888,
42         XRGB8888,
43         XBGR8888
44     }
45 
46     public enum ColorType
47     {
48         R,
49         G,
50         B,
51         A,
52         X,
53         L
54     }
55 
56     public static class PixelFormatExtensions
57     {
PixelFormatExtensions()58         static PixelFormatExtensions()
59         {
60             var values = Enum.GetValues(typeof(PixelFormat));
61             depths = new int[values.Length];
62             for(var i = 0; i < depths.Length; i++)
63             {
64                 // here we check if pixel format enum value has proper value
65                 // i.e. calculated from the position in enum (not set explicitly)
66                 var value = (int)values.GetValue(i);
67                 if(value != i)
68                 {
69                     throw new ArgumentException(string.Format("Unexpected pixel format value: {0}", (PixelFormat)value));
70                 }
71                 depths[value] = GetColorsLengths((PixelFormat)value).Sum(x => x.Value) / 8;
72             }
73         }
74 
75         /// <summary>
76         /// Returns a number of bytes needed to encode the color.
77         /// </summary>
78         /// <param name="format">Color format.</param>
GetColorDepth(this PixelFormat format)79         public static int GetColorDepth(this PixelFormat format)
80         {
81             if(format < 0 || (int)format >= depths.Length)
82             {
83                 throw new ArgumentException(string.Format("Unsupported pixel format: {0}", format));
84             }
85 
86             return depths[(int)format];
87         }
88 
89         /// <summary>
90         /// Calculates number of bits needed to encode each color channel.
91         /// </summary>
92         /// <param name="format">Color format</param>
GetColorsLengths(this PixelFormat format)93         public static Dictionary<ColorType, byte> GetColorsLengths(this PixelFormat format)
94         {
95             var bits = new Dictionary<ColorType, byte>();
96 
97             var offset = 0;
98             var formatAsCharArray = format.ToString().ToCharArray();
99             var firstNumberPosition = formatAsCharArray.Length / 2;
100 
101             while (offset < firstNumberPosition)
102             {
103                 ColorType colorType;
104                 if(!Enum.TryParse(formatAsCharArray[offset].ToString(), out colorType))
105                 {
106                     throw new ArgumentException();
107                 }
108 
109                 bits.Add(colorType, (byte)(formatAsCharArray[firstNumberPosition + offset] - '0'));
110                 offset++;
111             }
112 
113             return bits;
114         }
115 
116         private static int[] depths;
117     }
118 }
119 
120