• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "BC_Decoder.hpp"
16 
17 namespace {
18 static constexpr int BlockWidth = 4;
19 static constexpr int BlockHeight = 4;
20 
21 struct BC_color
22 {
decode__anona599d6700111::BC_color23 	void decode(unsigned char *dst, int x, int y, int dstW, int dstH, int dstPitch, int dstBpp, bool hasAlphaChannel, bool hasSeparateAlpha) const
24 	{
25 		Color c[4];
26 		c[0].extract565(c0);
27 		c[1].extract565(c1);
28 		if(hasSeparateAlpha || (c0 > c1))
29 		{
30 			c[2] = ((c[0] * 2) + c[1]) / 3;
31 			c[3] = ((c[1] * 2) + c[0]) / 3;
32 		}
33 		else
34 		{
35 			c[2] = (c[0] + c[1]) >> 1;
36 			if(hasAlphaChannel)
37 			{
38 				c[3].clearAlpha();
39 			}
40 		}
41 
42 		for(int j = 0; j < BlockHeight && (y + j) < dstH; j++)
43 		{
44 			int dstOffset = j * dstPitch;
45 			int idxOffset = j * BlockHeight;
46 			for(int i = 0; i < BlockWidth && (x + i) < dstW; i++, idxOffset++, dstOffset += dstBpp)
47 			{
48 				*reinterpret_cast<unsigned int *>(dst + dstOffset) = c[getIdx(idxOffset)].pack8888();
49 			}
50 		}
51 	}
52 
53 private:
54 	struct Color
55 	{
Color__anona599d6700111::BC_color::Color56 		Color()
57 		{
58 			c[0] = c[1] = c[2] = 0;
59 			c[3] = 0xFF000000;
60 		}
61 
extract565__anona599d6700111::BC_color::Color62 		void extract565(const unsigned int c565)
63 		{
64 			c[0] = ((c565 & 0x0000001F) << 3) | ((c565 & 0x0000001C) >> 2);
65 			c[1] = ((c565 & 0x000007E0) >> 3) | ((c565 & 0x00000600) >> 9);
66 			c[2] = ((c565 & 0x0000F800) >> 8) | ((c565 & 0x0000E000) >> 13);
67 		}
68 
pack8888__anona599d6700111::BC_color::Color69 		unsigned int pack8888() const
70 		{
71 			return ((c[2] & 0xFF) << 16) | ((c[1] & 0xFF) << 8) | (c[0] & 0xFF) | c[3];
72 		}
73 
clearAlpha__anona599d6700111::BC_color::Color74 		void clearAlpha()
75 		{
76 			c[3] = 0;
77 		}
78 
operator *__anona599d6700111::BC_color::Color79 		Color operator*(int factor) const
80 		{
81 			Color res;
82 			for(int i = 0; i < 4; ++i)
83 			{
84 				res.c[i] = c[i] * factor;
85 			}
86 			return res;
87 		}
88 
operator /__anona599d6700111::BC_color::Color89 		Color operator/(int factor) const
90 		{
91 			Color res;
92 			for(int i = 0; i < 4; ++i)
93 			{
94 				res.c[i] = c[i] / factor;
95 			}
96 			return res;
97 		}
98 
operator >>__anona599d6700111::BC_color::Color99 		Color operator>>(int shift) const
100 		{
101 			Color res;
102 			for(int i = 0; i < 4; ++i)
103 			{
104 				res.c[i] = c[i] >> shift;
105 			}
106 			return res;
107 		}
108 
operator +__anona599d6700111::BC_color::Color109 		Color operator+(Color const &obj) const
110 		{
111 			Color res;
112 			for(int i = 0; i < 4; ++i)
113 			{
114 				res.c[i] = c[i] + obj.c[i];
115 			}
116 			return res;
117 		}
118 
119 	private:
120 		int c[4];
121 	};
122 
getIdx__anona599d6700111::BC_color123 	unsigned int getIdx(int i) const
124 	{
125 		int offset = i << 1;  // 2 bytes per index
126 		return (idx & (0x3 << offset)) >> offset;
127 	}
128 
129 	unsigned short c0;
130 	unsigned short c1;
131 	unsigned int idx;
132 };
133 
134 struct BC_channel
135 {
decode__anona599d6700111::BC_channel136 	void decode(unsigned char *dst, int x, int y, int dstW, int dstH, int dstPitch, int dstBpp, int channel, bool isSigned) const
137 	{
138 		int c[8] = { 0 };
139 
140 		if(isSigned)
141 		{
142 			c[0] = static_cast<signed char>(data & 0xFF);
143 			c[1] = static_cast<signed char>((data & 0xFF00) >> 8);
144 		}
145 		else
146 		{
147 			c[0] = static_cast<unsigned char>(data & 0xFF);
148 			c[1] = static_cast<unsigned char>((data & 0xFF00) >> 8);
149 		}
150 
151 		if(c[0] > c[1])
152 		{
153 			for(int i = 2; i < 8; ++i)
154 			{
155 				c[i] = ((8 - i) * c[0] + (i - 1) * c[1]) / 7;
156 			}
157 		}
158 		else
159 		{
160 			for(int i = 2; i < 6; ++i)
161 			{
162 				c[i] = ((6 - i) * c[0] + (i - 1) * c[1]) / 5;
163 			}
164 			c[6] = isSigned ? -128 : 0;
165 			c[7] = isSigned ? 127 : 255;
166 		}
167 
168 		for(int j = 0; j < BlockHeight && (y + j) < dstH; j++)
169 		{
170 			for(int i = 0; i < BlockWidth && (x + i) < dstW; i++)
171 			{
172 				dst[channel + (i * dstBpp) + (j * dstPitch)] = static_cast<unsigned char>(c[getIdx((j * BlockHeight) + i)]);
173 			}
174 		}
175 	}
176 
177 private:
getIdx__anona599d6700111::BC_channel178 	unsigned char getIdx(int i) const
179 	{
180 		int offset = i * 3 + 16;
181 		return static_cast<unsigned char>((data & (0x7ull << offset)) >> offset);
182 	}
183 
184 	unsigned long long data;
185 };
186 
187 struct BC_alpha
188 {
decode__anona599d6700111::BC_alpha189 	void decode(unsigned char *dst, int x, int y, int dstW, int dstH, int dstPitch, int dstBpp) const
190 	{
191 		dst += 3;  // Write only to alpha (channel 3)
192 		for(int j = 0; j < BlockHeight && (y + j) < dstH; j++, dst += dstPitch)
193 		{
194 			unsigned char *dstRow = dst;
195 			for(int i = 0; i < BlockWidth && (x + i) < dstW; i++, dstRow += dstBpp)
196 			{
197 				*dstRow = getAlpha(j * BlockHeight + i);
198 			}
199 		}
200 	}
201 
202 private:
getAlpha__anona599d6700111::BC_alpha203 	unsigned char getAlpha(int i) const
204 	{
205 		int offset = i << 2;
206 		int alpha = (data & (0xFull << offset)) >> offset;
207 		return static_cast<unsigned char>(alpha | (alpha << 4));
208 	}
209 
210 	unsigned long long data;
211 };
212 }  // end namespace
213 
214 // Decodes 1 to 4 channel images to 8 bit output
Decode(const unsigned char * src,unsigned char * dst,int w,int h,int dstW,int dstH,int dstPitch,int dstBpp,int n,bool isNoAlphaU)215 bool BC_Decoder::Decode(const unsigned char *src, unsigned char *dst, int w, int h, int dstW, int dstH, int dstPitch, int dstBpp, int n, bool isNoAlphaU)
216 {
217 	static_assert(sizeof(BC_color) == 8, "BC_color must be 8 bytes");
218 	static_assert(sizeof(BC_channel) == 8, "BC_channel must be 8 bytes");
219 	static_assert(sizeof(BC_alpha) == 8, "BC_alpha must be 8 bytes");
220 
221 	const int dx = BlockWidth * dstBpp;
222 	const int dy = BlockHeight * dstPitch;
223 	const bool isAlpha = (n == 1) && !isNoAlphaU;
224 	const bool isSigned = ((n == 4) || (n == 5) || (n == 6)) && !isNoAlphaU;
225 
226 	switch(n)
227 	{
228 		case 1:  // BC1
229 		{
230 			const BC_color *color = reinterpret_cast<const BC_color *>(src);
231 			for(int y = 0; y < h; y += BlockHeight, dst += dy)
232 			{
233 				unsigned char *dstRow = dst;
234 				for(int x = 0; x < w; x += BlockWidth, ++color, dstRow += dx)
235 				{
236 					color->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, isAlpha, false);
237 				}
238 			}
239 		}
240 		break;
241 		case 2:  // BC2
242 		{
243 			const BC_alpha *alpha = reinterpret_cast<const BC_alpha *>(src);
244 			const BC_color *color = reinterpret_cast<const BC_color *>(src + 8);
245 			for(int y = 0; y < h; y += BlockHeight, dst += dy)
246 			{
247 				unsigned char *dstRow = dst;
248 				for(int x = 0; x < w; x += BlockWidth, alpha += 2, color += 2, dstRow += dx)
249 				{
250 					color->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, isAlpha, true);
251 					alpha->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp);
252 				}
253 			}
254 		}
255 		break;
256 		case 3:  // BC3
257 		{
258 			const BC_channel *alpha = reinterpret_cast<const BC_channel *>(src);
259 			const BC_color *color = reinterpret_cast<const BC_color *>(src + 8);
260 			for(int y = 0; y < h; y += BlockHeight, dst += dy)
261 			{
262 				unsigned char *dstRow = dst;
263 				for(int x = 0; x < w; x += BlockWidth, alpha += 2, color += 2, dstRow += dx)
264 				{
265 					color->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, isAlpha, true);
266 					alpha->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, 3, isSigned);
267 				}
268 			}
269 		}
270 		break;
271 		case 4:  // BC4
272 		{
273 			const BC_channel *red = reinterpret_cast<const BC_channel *>(src);
274 			for(int y = 0; y < h; y += BlockHeight, dst += dy)
275 			{
276 				unsigned char *dstRow = dst;
277 				for(int x = 0; x < w; x += BlockWidth, ++red, dstRow += dx)
278 				{
279 					red->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, 0, isSigned);
280 				}
281 			}
282 		}
283 		break;
284 		case 5:  // BC5
285 		{
286 			const BC_channel *red = reinterpret_cast<const BC_channel *>(src);
287 			const BC_channel *green = reinterpret_cast<const BC_channel *>(src + 8);
288 			for(int y = 0; y < h; y += BlockHeight, dst += dy)
289 			{
290 				unsigned char *dstRow = dst;
291 				for(int x = 0; x < w; x += BlockWidth, red += 2, green += 2, dstRow += dx)
292 				{
293 					red->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, 0, isSigned);
294 					green->decode(dstRow, x, y, dstW, dstH, dstPitch, dstBpp, 1, isSigned);
295 				}
296 			}
297 		}
298 		break;
299 		default:
300 			return false;
301 	}
302 
303 	return true;
304 }
305