1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "pixelformat.h" |
22 | #include "StringMap.h" |
23 | |
24 | namespace love |
25 | { |
26 | |
27 | static StringMap<PixelFormat, PIXELFORMAT_MAX_ENUM>::Entry formatEntries[] = |
28 | { |
29 | { "unknown" , PIXELFORMAT_UNKNOWN }, |
30 | |
31 | { "normal" , PIXELFORMAT_NORMAL }, |
32 | { "hdr" , PIXELFORMAT_HDR }, |
33 | |
34 | { "r8" , PIXELFORMAT_R8 }, |
35 | { "rg8" , PIXELFORMAT_RG8 }, |
36 | { "rgba8" , PIXELFORMAT_RGBA8 }, |
37 | { "srgba8" , PIXELFORMAT_sRGBA8 }, |
38 | { "r16" , PIXELFORMAT_R16 }, |
39 | { "rg16" , PIXELFORMAT_RG16 }, |
40 | { "rgba16" , PIXELFORMAT_RGBA16 }, |
41 | { "r16f" , PIXELFORMAT_R16F }, |
42 | { "rg16f" , PIXELFORMAT_RG16F }, |
43 | { "rgba16f" , PIXELFORMAT_RGBA16F }, |
44 | { "r32f" , PIXELFORMAT_R32F }, |
45 | { "rg32f" , PIXELFORMAT_RG32F }, |
46 | { "rgba32f" , PIXELFORMAT_RGBA32F }, |
47 | |
48 | { "la8" , PIXELFORMAT_LA8 }, |
49 | |
50 | { "rgba4" , PIXELFORMAT_RGBA4 }, |
51 | { "rgb5a1" , PIXELFORMAT_RGB5A1 }, |
52 | { "rgb565" , PIXELFORMAT_RGB565 }, |
53 | { "rgb10a2" , PIXELFORMAT_RGB10A2 }, |
54 | { "rg11b10f" , PIXELFORMAT_RG11B10F }, |
55 | |
56 | { "stencil8" , PIXELFORMAT_STENCIL8 }, |
57 | { "depth16" , PIXELFORMAT_DEPTH16 }, |
58 | { "depth24" , PIXELFORMAT_DEPTH24 }, |
59 | { "depth32f" , PIXELFORMAT_DEPTH32F }, |
60 | { "depth24stencil8" , PIXELFORMAT_DEPTH24_STENCIL8 }, |
61 | { "depth32fstencil8" , PIXELFORMAT_DEPTH32F_STENCIL8 }, |
62 | |
63 | { "DXT1" , PIXELFORMAT_DXT1 }, |
64 | { "DXT3" , PIXELFORMAT_DXT3 }, |
65 | { "DXT5" , PIXELFORMAT_DXT5 }, |
66 | { "BC4" , PIXELFORMAT_BC4 }, |
67 | { "BC4s" , PIXELFORMAT_BC4s }, |
68 | { "BC5" , PIXELFORMAT_BC5 }, |
69 | { "BC5s" , PIXELFORMAT_BC5s }, |
70 | { "BC6h" , PIXELFORMAT_BC6H }, |
71 | { "BC6hs" , PIXELFORMAT_BC6Hs }, |
72 | { "BC7" , PIXELFORMAT_BC7 }, |
73 | { "PVR1rgb2" , PIXELFORMAT_PVR1_RGB2 }, |
74 | { "PVR1rgb4" , PIXELFORMAT_PVR1_RGB4 }, |
75 | { "PVR1rgba2" , PIXELFORMAT_PVR1_RGBA2 }, |
76 | { "PVR1rgba4" , PIXELFORMAT_PVR1_RGBA4 }, |
77 | { "ETC1" , PIXELFORMAT_ETC1 }, |
78 | { "ETC2rgb" , PIXELFORMAT_ETC2_RGB }, |
79 | { "ETC2rgba" , PIXELFORMAT_ETC2_RGBA }, |
80 | { "ETC2rgba1" , PIXELFORMAT_ETC2_RGBA1 }, |
81 | { "EACr" , PIXELFORMAT_EAC_R }, |
82 | { "EACrs" , PIXELFORMAT_EAC_Rs }, |
83 | { "EACrg" , PIXELFORMAT_EAC_RG }, |
84 | { "EACrgs" , PIXELFORMAT_EAC_RGs }, |
85 | { "ASTC4x4" , PIXELFORMAT_ASTC_4x4 }, |
86 | { "ASTC5x4" , PIXELFORMAT_ASTC_5x4 }, |
87 | { "ASTC5x5" , PIXELFORMAT_ASTC_5x5 }, |
88 | { "ASTC6x5" , PIXELFORMAT_ASTC_6x5 }, |
89 | { "ASTC6x6" , PIXELFORMAT_ASTC_6x6 }, |
90 | { "ASTC8x5" , PIXELFORMAT_ASTC_8x5 }, |
91 | { "ASTC8x6" , PIXELFORMAT_ASTC_8x6 }, |
92 | { "ASTC8x8" , PIXELFORMAT_ASTC_8x8 }, |
93 | { "ASTC10x5" , PIXELFORMAT_ASTC_10x5 }, |
94 | { "ASTC10x6" , PIXELFORMAT_ASTC_10x6 }, |
95 | { "ASTC10x8" , PIXELFORMAT_ASTC_10x8 }, |
96 | { "ASTC10x10" , PIXELFORMAT_ASTC_10x10 }, |
97 | { "ASTC12x10" , PIXELFORMAT_ASTC_12x10 }, |
98 | { "ASTC12x12" , PIXELFORMAT_ASTC_12x12 }, |
99 | }; |
100 | |
101 | static_assert(sizeof(formatEntries) / sizeof(formatEntries[0]) == (size_t) PIXELFORMAT_MAX_ENUM, "pixel format string map is missing entries!" ); |
102 | |
103 | static StringMap<PixelFormat, PIXELFORMAT_MAX_ENUM> formats(formatEntries, sizeof(formatEntries)); |
104 | |
105 | bool getConstant(const char *in, PixelFormat &out) |
106 | { |
107 | return formats.find(in, out); |
108 | } |
109 | |
110 | bool getConstant(PixelFormat in, const char *&out) |
111 | { |
112 | return formats.find(in, out); |
113 | } |
114 | |
115 | bool isPixelFormatCompressed(PixelFormat format) |
116 | { |
117 | // I'm lazy |
118 | int iformat = (int) format; |
119 | return iformat >= (int) PIXELFORMAT_DXT1 && iformat < (int) PIXELFORMAT_MAX_ENUM; |
120 | } |
121 | |
122 | bool isPixelFormatDepthStencil(PixelFormat format) |
123 | { |
124 | int iformat = (int) format; |
125 | return iformat >= (int) PIXELFORMAT_STENCIL8 && iformat <= (int) PIXELFORMAT_DEPTH32F_STENCIL8; |
126 | } |
127 | |
128 | bool isPixelFormatDepth(PixelFormat format) |
129 | { |
130 | int iformat = (int) format; |
131 | return iformat >= (int) PIXELFORMAT_DEPTH16 && iformat <= (int) PIXELFORMAT_DEPTH32F_STENCIL8; |
132 | } |
133 | |
134 | bool isPixelFormatStencil(PixelFormat format) |
135 | { |
136 | return format == PIXELFORMAT_STENCIL8 || format == PIXELFORMAT_DEPTH24_STENCIL8 || format == PIXELFORMAT_DEPTH32F_STENCIL8; |
137 | } |
138 | |
139 | size_t getPixelFormatSize(PixelFormat format) |
140 | { |
141 | switch (format) |
142 | { |
143 | case PIXELFORMAT_R8: |
144 | case PIXELFORMAT_STENCIL8: |
145 | return 1; |
146 | case PIXELFORMAT_RG8: |
147 | case PIXELFORMAT_R16: |
148 | case PIXELFORMAT_R16F: |
149 | case PIXELFORMAT_LA8: |
150 | case PIXELFORMAT_RGBA4: |
151 | case PIXELFORMAT_RGB5A1: |
152 | case PIXELFORMAT_RGB565: |
153 | case PIXELFORMAT_DEPTH16: |
154 | return 2; |
155 | case PIXELFORMAT_RGBA8: |
156 | case PIXELFORMAT_sRGBA8: |
157 | case PIXELFORMAT_RG16: |
158 | case PIXELFORMAT_RG16F: |
159 | case PIXELFORMAT_R32F: |
160 | case PIXELFORMAT_RGB10A2: |
161 | case PIXELFORMAT_RG11B10F: |
162 | case PIXELFORMAT_DEPTH24: |
163 | case PIXELFORMAT_DEPTH32F: |
164 | case PIXELFORMAT_DEPTH24_STENCIL8: |
165 | return 4; |
166 | case PIXELFORMAT_RGBA16: |
167 | case PIXELFORMAT_RGBA16F: |
168 | case PIXELFORMAT_RG32F: |
169 | case PIXELFORMAT_DEPTH32F_STENCIL8: |
170 | return 8; |
171 | case PIXELFORMAT_RGBA32F: |
172 | return 16; |
173 | default: |
174 | // TODO: compressed formats |
175 | return 0; |
176 | } |
177 | } |
178 | |
179 | int getPixelFormatColorComponents(PixelFormat format) |
180 | { |
181 | switch (format) |
182 | { |
183 | case PIXELFORMAT_R8: |
184 | case PIXELFORMAT_R16: |
185 | case PIXELFORMAT_R16F: |
186 | case PIXELFORMAT_R32F: |
187 | return 1; |
188 | case PIXELFORMAT_RG8: |
189 | case PIXELFORMAT_RG16: |
190 | case PIXELFORMAT_RG16F: |
191 | case PIXELFORMAT_RG32F: |
192 | case PIXELFORMAT_LA8: |
193 | return 2; |
194 | case PIXELFORMAT_RGB565: |
195 | case PIXELFORMAT_RG11B10F: |
196 | return 3; |
197 | case PIXELFORMAT_RGBA8: |
198 | case PIXELFORMAT_sRGBA8: |
199 | case PIXELFORMAT_RGBA16: |
200 | case PIXELFORMAT_RGBA16F: |
201 | case PIXELFORMAT_RGBA32F: |
202 | case PIXELFORMAT_RGBA4: |
203 | case PIXELFORMAT_RGB5A1: |
204 | case PIXELFORMAT_RGB10A2: |
205 | return 4; |
206 | default: |
207 | return 0; |
208 | } |
209 | } |
210 | |
211 | } // love |
212 | |