OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_RESOURCES_TEXTURE_COMPRESSOR_UTIL_H_ | |
6 #define CC_RESOURCES_TEXTURE_COMPRESSOR_UTIL_H_ | |
7 | |
8 namespace cc { | |
9 namespace texture_compress { | |
christiank
2015/04/29 12:25:46
We don't use the texture_compress namespace any lo
| |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "base/logging.h" | |
13 | |
14 template <typename T> | |
15 inline T clamp(T val, T min, T max) { | |
16 return val < min ? min : (val > max ? max : val); | |
17 } | |
18 | |
19 inline uint8_t round_to_5_bits(float val) { | |
20 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31); | |
21 } | |
22 | |
23 inline uint8_t round_to_4_bits(float val) { | |
24 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15); | |
25 } | |
26 | |
27 union Color { | |
28 struct BgraColorType { | |
29 uint8_t b; | |
30 uint8_t g; | |
31 uint8_t r; | |
32 uint8_t a; | |
33 } channels; | |
34 uint8_t components[4]; | |
35 uint32_t bits; | |
36 }; | |
37 | |
38 /* | |
39 * Codeword tables. | |
40 * See: Table 3.17.2 | |
41 */ | |
42 ALIGNAS(16) static const int16_t g_codeword_tables[8][4] = { | |
43 {-8, -2, 2, 8}, | |
44 {-17, -5, 5, 17}, | |
45 {-29, -9, 9, 29}, | |
46 {-42, -13, 13, 42}, | |
47 {-60, -18, 18, 60}, | |
48 {-80, -24, 24, 80}, | |
49 {-106, -33, 33, 106}, | |
50 {-183, -47, 47, 183}}; | |
51 | |
52 /* | |
53 * Maps modifier indices to pixel index values. | |
54 * See: Table 3.17.3 | |
55 */ | |
56 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1}; | |
57 | |
58 /* | |
59 * The ETC1 specification index texels as follows: | |
60 * | |
61 * [a][e][i][m] [ 0][ 4][ 8][12] | |
62 * [b][f][j][n] <-> [ 1][ 5][ 9][13] | |
63 * [c][g][k][o] [ 2][ 6][10][14] | |
64 * [d][h][l][p] [ 3][ 7][11][15] | |
65 * | |
66 * [ 0][ 1][ 2][ 3] [ 0][ 4][ 4][ 5] | |
67 * [ 4][ 5][ 6][ 7] <-> [ 8][ 9][12][13] | |
68 * [ 8][ 9][10][11] [ 2][ 3][ 6][ 7] | |
69 * [12][13][14][15] [10][11][14][15] | |
70 * | |
71 * However, when extracting sub blocks from BGRA data the natural array | |
72 * indexing order ends up different: | |
73 * | |
74 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m] | |
75 * [c][g][d][h] [b][f][j][n] | |
76 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o] | |
77 * [k][o][l][p] [d][h][l][p] | |
78 * | |
79 * In order to translate from the natural array indices in a sub block to the | |
80 * indices (number) used by specification and hardware we use this table. | |
81 */ | |
82 static const uint8_t g_idx_to_num[4][8] = { | |
83 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0. | |
84 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1. | |
85 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0. | |
86 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1. | |
87 }; | |
88 | |
89 inline void WriteColors444(uint8_t* block, | |
90 const Color& color0, | |
91 const Color& color1) { | |
92 /* for BGRA textures */ | |
93 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4); | |
94 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4); | |
95 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4); | |
96 } | |
97 | |
98 inline void WriteColors555(uint8_t* block, | |
99 const Color& color0, | |
100 const Color& color1) { | |
101 // Table for conversion to 3-bit two complement format. | |
102 static const uint8_t two_compl_trans_table[8] = { | |
103 4, // -4 (100b) | |
104 5, // -3 (101b) | |
105 6, // -2 (110b) | |
106 7, // -1 (111b) | |
107 0, // 0 (000b) | |
108 1, // 1 (001b) | |
109 2, // 2 (010b) | |
110 3, // 3 (011b) | |
111 }; | |
112 | |
113 int16_t delta_r = | |
114 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3); | |
115 int16_t delta_g = | |
116 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3); | |
117 int16_t delta_b = | |
118 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3); | |
119 DCHECK(delta_r >= -4 && delta_r <= 3); | |
120 DCHECK(delta_g >= -4 && delta_g <= 3); | |
121 DCHECK(delta_b >= -4 && delta_b <= 3); | |
122 | |
123 /* For BGRA textures */ | |
124 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4]; | |
125 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4]; | |
126 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4]; | |
127 } | |
128 | |
129 inline void WriteCodewordTable(uint8_t* block, | |
130 uint8_t sub_block_id, | |
131 uint8_t table) { | |
132 DCHECK_LT(sub_block_id, 2); | |
133 DCHECK_LT(table, 8); | |
134 | |
135 uint8_t shift = (2 + (3 - sub_block_id * 3)); | |
136 block[3] &= ~(0x07 << shift); | |
137 block[3] |= table << shift; | |
138 } | |
139 | |
140 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) { | |
141 block[4] |= pixel_data >> 24; | |
142 block[5] |= (pixel_data >> 16) & 0xff; | |
143 block[6] |= (pixel_data >> 8) & 0xff; | |
144 block[7] |= pixel_data & 0xff; | |
145 } | |
146 | |
147 inline void WriteFlip(uint8_t* block, bool flip) { | |
148 block[3] &= ~0x01; | |
149 block[3] |= static_cast<uint8_t>(flip); | |
150 } | |
151 | |
152 inline void WriteDiff(uint8_t* block, bool diff) { | |
153 block[3] &= ~0x02; | |
154 block[3] |= static_cast<uint8_t>(diff) << 1; | |
155 } | |
156 | |
157 /** | |
158 * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is | |
159 * expanded to BGR888 as it would be in hardware after decompression. The | |
160 * actual 444-bit data is available in the four most significant bits of each | |
161 * channel. | |
162 */ | |
163 inline Color MakeColor444(const float* bgr) { | |
164 uint8_t b4 = round_to_4_bits(bgr[0]); | |
165 uint8_t g4 = round_to_4_bits(bgr[1]); | |
166 uint8_t r4 = round_to_4_bits(bgr[2]); | |
167 Color bgr444; | |
168 bgr444.channels.b = (b4 << 4) | b4; | |
169 bgr444.channels.g = (g4 << 4) | g4; | |
170 bgr444.channels.r = (r4 << 4) | r4; | |
171 bgr444.channels.a = | |
172 0x44; /* added to distinguish between expanded 555 and 444 colors */ | |
173 return bgr444; | |
174 } | |
175 | |
176 /** | |
177 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is | |
178 * expanded to BGR888 as it would be in hardware after decompression. The | |
179 * actual 555-bit data is available in the five most significant bits of each | |
180 * channel. | |
181 */ | |
182 inline Color MakeColor555(const float* bgr) { | |
183 uint8_t b5 = round_to_5_bits(bgr[0]); | |
184 uint8_t g5 = round_to_5_bits(bgr[1]); | |
185 uint8_t r5 = round_to_5_bits(bgr[2]); | |
186 Color bgr555; | |
187 bgr555.channels.b = (b5 << 3) | (b5 >> 2); | |
188 bgr555.channels.g = (g5 << 3) | (g5 >> 2); | |
189 bgr555.channels.r = (r5 << 3) | (r5 >> 2); | |
190 bgr555.channels.a = | |
191 0x55; /* added to distinguish between expanded 555 and 444 colors */ | |
192 return bgr555; | |
193 } | |
194 | |
195 /** | |
196 * Constructs a color from a given base color and luminance value. | |
197 */ | |
198 inline Color MakeColor(const Color& base, int16_t lum) { | |
199 int b = static_cast<int>(base.channels.b) + lum; | |
200 int g = static_cast<int>(base.channels.g) + lum; | |
201 int r = static_cast<int>(base.channels.r) + lum; | |
202 Color color; | |
203 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); | |
204 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); | |
205 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255)); | |
206 return color; | |
207 } | |
208 | |
209 /** | |
210 * Calculates the error metric for two colors. A small error signals that the | |
211 * colors are similar to each other, a large error the signals the opposite. | |
212 */ | |
213 inline uint32_t GetColorError(const Color& u, const Color& v) { | |
214 #ifdef USE_PERCEIVED_ERROR_METRIC | |
215 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; | |
216 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; | |
217 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; | |
218 return static_cast<uint32_t>(0.299f * delta_b * delta_b + | |
219 0.587f * delta_g * delta_g + | |
220 0.114f * delta_r * delta_r); | |
221 #else | |
222 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; | |
223 int delta_g = static_cast<int>(u.channels.g) - v.channels.g; | |
224 int delta_r = static_cast<int>(u.channels.r) - v.channels.r; | |
225 return delta_b * delta_b + delta_g * delta_g + delta_r * delta_r; | |
226 #endif | |
227 } | |
228 | |
229 } // namespace texture_compress | |
230 } // namespace cc | |
231 | |
232 #endif // CC_RESOURCES_TEXTURE_COMPRESSOR_UTIL_H_ | |
OLD | NEW |