Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // See the following specification for details on the ETC1 format: | 5 // See the following specification for details on the ETC1 format: |
| 6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8 _texture.txt | 6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8 _texture.txt |
| 7 | 7 |
| 8 #include "cc/resources/texture_compressor_etc1.h" | 8 #include "cc/resources/texture_compressor_etc1.h" |
| 9 | 9 |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 | 14 |
| 15 // Defining the following macro will cause the error metric function to weigh | 15 // Defining the following macro will cause the error metric function to weigh |
| 16 // each color channel differently depending on how the human eye can perceive | 16 // each color channel differently depending on how the human eye can perceive |
| 17 // them. This can give a slight improvement in image quality at the cost of a | 17 // them. This can give a slight improvement in image quality at the cost of a |
| 18 // performance hit. | 18 // performance hit. |
| 19 // #define USE_PERCEIVED_ERROR_METRIC | 19 // #define USE_PERCEIVED_ERROR_METRIC |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 template <typename T> | 23 // Constructs a color from a given base color and luminance value. |
| 24 inline T clamp(T val, T min, T max) { | 24 inline cc::Color MakeColor(const cc::Color& base, int16_t lum) { |
| 25 return val < min ? min : (val > max ? max : val); | |
| 26 } | |
| 27 | |
| 28 inline uint8_t round_to_5_bits(float val) { | |
| 29 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31); | |
| 30 } | |
| 31 | |
| 32 inline uint8_t round_to_4_bits(float val) { | |
| 33 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15); | |
| 34 } | |
| 35 | |
| 36 union Color { | |
| 37 struct BgraColorType { | |
| 38 uint8_t b; | |
| 39 uint8_t g; | |
| 40 uint8_t r; | |
| 41 uint8_t a; | |
| 42 } channels; | |
| 43 uint8_t components[4]; | |
| 44 uint32_t bits; | |
| 45 }; | |
| 46 | |
| 47 /* | |
| 48 * Codeword tables. | |
| 49 * See: Table 3.17.2 | |
| 50 */ | |
| 51 static const int16_t g_codeword_tables[8][4] = {{-8, -2, 2, 8}, | |
| 52 {-17, -5, 5, 17}, | |
| 53 {-29, -9, 9, 29}, | |
| 54 {-42, -13, 13, 42}, | |
| 55 {-60, -18, 18, 60}, | |
| 56 {-80, -24, 24, 80}, | |
| 57 {-106, -33, 33, 106}, | |
| 58 {-183, -47, 47, 183}}; | |
| 59 | |
| 60 /* | |
| 61 * Maps modifier indices to pixel index values. | |
| 62 * See: Table 3.17.3 | |
| 63 */ | |
| 64 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1}; | |
| 65 | |
| 66 /* | |
| 67 * The ETC1 specification index texels as follows: | |
| 68 * | |
| 69 * [a][e][i][m] [ 0][ 4][ 8][12] | |
| 70 * [b][f][j][n] <-> [ 1][ 5][ 9][13] | |
| 71 * [c][g][k][o] [ 2][ 6][10][14] | |
| 72 * [d][h][l][p] [ 3][ 7][11][15] | |
| 73 * | |
| 74 * However, when extracting sub blocks from BGRA data the natural array | |
| 75 * indexing order ends up different: | |
| 76 * | |
| 77 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m] | |
| 78 * [c][g][d][h] [b][f][j][n] | |
| 79 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o] | |
| 80 * [k][o][l][p] [d][h][l][p] | |
| 81 * | |
| 82 * In order to translate from the natural array indices in a sub block to the | |
| 83 * indices (number) used by specification and hardware we use this table. | |
| 84 */ | |
| 85 static const uint8_t g_idx_to_num[4][8] = { | |
| 86 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0. | |
| 87 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1. | |
| 88 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0. | |
| 89 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1. | |
| 90 }; | |
| 91 | |
| 92 inline void WriteColors444(uint8_t* block, | |
| 93 const Color& color0, | |
| 94 const Color& color1) { | |
| 95 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4); | |
| 96 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4); | |
| 97 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4); | |
| 98 } | |
| 99 | |
| 100 inline void WriteColors555(uint8_t* block, | |
| 101 const Color& color0, | |
| 102 const Color& color1) { | |
| 103 // Table for conversion to 3-bit two complement format. | |
| 104 static const uint8_t two_compl_trans_table[8] = { | |
| 105 4, // -4 (100b) | |
| 106 5, // -3 (101b) | |
| 107 6, // -2 (110b) | |
| 108 7, // -1 (111b) | |
| 109 0, // 0 (000b) | |
| 110 1, // 1 (001b) | |
| 111 2, // 2 (010b) | |
| 112 3, // 3 (011b) | |
| 113 }; | |
| 114 | |
| 115 int16_t delta_r = | |
| 116 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3); | |
| 117 int16_t delta_g = | |
| 118 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3); | |
| 119 int16_t delta_b = | |
| 120 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3); | |
| 121 DCHECK(delta_r >= -4 && delta_r <= 3); | |
| 122 DCHECK(delta_g >= -4 && delta_g <= 3); | |
| 123 DCHECK(delta_b >= -4 && delta_b <= 3); | |
| 124 | |
| 125 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4]; | |
| 126 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4]; | |
| 127 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4]; | |
| 128 } | |
| 129 | |
| 130 inline void WriteCodewordTable(uint8_t* block, | |
| 131 uint8_t sub_block_id, | |
| 132 uint8_t table) { | |
| 133 DCHECK_LT(sub_block_id, 2); | |
| 134 DCHECK_LT(table, 8); | |
| 135 | |
| 136 uint8_t shift = (2 + (3 - sub_block_id * 3)); | |
| 137 block[3] &= ~(0x07 << shift); | |
| 138 block[3] |= table << shift; | |
| 139 } | |
| 140 | |
| 141 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) { | |
| 142 block[4] |= pixel_data >> 24; | |
| 143 block[5] |= (pixel_data >> 16) & 0xff; | |
| 144 block[6] |= (pixel_data >> 8) & 0xff; | |
| 145 block[7] |= pixel_data & 0xff; | |
| 146 } | |
| 147 | |
| 148 inline void WriteFlip(uint8_t* block, bool flip) { | |
| 149 block[3] &= ~0x01; | |
| 150 block[3] |= static_cast<uint8_t>(flip); | |
| 151 } | |
| 152 | |
| 153 inline void WriteDiff(uint8_t* block, bool diff) { | |
| 154 block[3] &= ~0x02; | |
| 155 block[3] |= static_cast<uint8_t>(diff) << 1; | |
| 156 } | |
| 157 | |
| 158 /** | |
| 159 * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is | |
| 160 * expanded to BGR888 as it would be in hardware after decompression. The | |
| 161 * actual 444-bit data is available in the four most significant bits of each | |
| 162 * channel. | |
| 163 */ | |
| 164 inline Color MakeColor444(const float* bgr) { | |
| 165 uint8_t b4 = round_to_4_bits(bgr[0]); | |
| 166 uint8_t g4 = round_to_4_bits(bgr[1]); | |
| 167 uint8_t r4 = round_to_4_bits(bgr[2]); | |
| 168 Color bgr444; | |
| 169 bgr444.channels.b = (b4 << 4) | b4; | |
| 170 bgr444.channels.g = (g4 << 4) | g4; | |
| 171 bgr444.channels.r = (r4 << 4) | r4; | |
| 172 return bgr444; | |
| 173 } | |
| 174 | |
| 175 /** | |
| 176 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is | |
| 177 * expanded to BGR888 as it would be in hardware after decompression. The | |
| 178 * actual 555-bit data is available in the five most significant bits of each | |
| 179 * channel. | |
| 180 */ | |
| 181 inline Color MakeColor555(const float* bgr) { | |
| 182 uint8_t b5 = round_to_5_bits(bgr[0]); | |
| 183 uint8_t g5 = round_to_5_bits(bgr[1]); | |
| 184 uint8_t r5 = round_to_5_bits(bgr[2]); | |
| 185 Color bgr555; | |
| 186 bgr555.channels.b = (b5 << 3) | (b5 >> 2); | |
| 187 bgr555.channels.g = (g5 << 3) | (g5 >> 2); | |
| 188 bgr555.channels.r = (r5 << 3) | (r5 >> 2); | |
| 189 return bgr555; | |
| 190 } | |
| 191 | |
| 192 /** | |
| 193 * Constructs a color from a given base color and luminance value. | |
| 194 */ | |
| 195 inline Color MakeColor(const Color& base, int16_t lum) { | |
| 196 int b = static_cast<int>(base.channels.b) + lum; | 25 int b = static_cast<int>(base.channels.b) + lum; |
| 197 int g = static_cast<int>(base.channels.g) + lum; | 26 int g = static_cast<int>(base.channels.g) + lum; |
| 198 int r = static_cast<int>(base.channels.r) + lum; | 27 int r = static_cast<int>(base.channels.r) + lum; |
| 199 Color color; | 28 cc::Color color; |
| 200 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); | 29 color.channels.b = static_cast<uint8_t>(cc::clamp(b, 0, 255)); |
| 201 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); | 30 color.channels.g = static_cast<uint8_t>(cc::clamp(g, 0, 255)); |
| 202 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255)); | 31 color.channels.r = static_cast<uint8_t>(cc::clamp(r, 0, 255)); |
| 203 return color; | 32 return color; |
| 204 } | 33 } |
| 205 | 34 |
| 206 /** | 35 // Calculates the error metric for two colors. A small error signals that the |
| 207 * Calculates the error metric for two colors. A small error signals that the | 36 // colors are similar to each other, a large error the signals the opposite. |
| 208 * colors are similar to each other, a large error the signals the opposite. | 37 inline uint32_t GetColorError(const cc::Color& u, const cc::Color& v) { |
| 209 */ | |
| 210 inline uint32_t GetColorError(const Color& u, const Color& v) { | |
| 211 #ifdef USE_PERCEIVED_ERROR_METRIC | 38 #ifdef USE_PERCEIVED_ERROR_METRIC |
| 212 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; | 39 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; |
| 213 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; | 40 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; |
| 214 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; | 41 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; |
| 215 return static_cast<uint32_t>(0.299f * delta_b * delta_b + | 42 return static_cast<uint32_t>(0.299f * delta_b * delta_b + |
| 216 0.587f * delta_g * delta_g + | 43 0.587f * delta_g * delta_g + |
| 217 0.114f * delta_r * delta_r); | 44 0.114f * delta_r * delta_r); |
| 218 #else | 45 #else |
| 219 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; | 46 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; |
| 220 int delta_g = static_cast<int>(u.channels.g) - v.channels.g; | 47 int delta_g = static_cast<int>(u.channels.g) - v.channels.g; |
| 221 int delta_r = static_cast<int>(u.channels.r) - v.channels.r; | 48 int delta_r = static_cast<int>(u.channels.r) - v.channels.r; |
| 222 return delta_b * delta_b + delta_g * delta_g + delta_r * delta_r; | 49 return delta_b * delta_b + delta_g * delta_g + delta_r * delta_r; |
| 223 #endif | 50 #endif |
| 224 } | 51 } |
| 225 | 52 |
| 226 void GetAverageColor(const Color* src, float* avg_color) { | 53 void GetAverageColor(const cc::Color* src, float* avg_color) { |
| 227 uint32_t sum_b = 0, sum_g = 0, sum_r = 0; | 54 uint32_t sum_b = 0, sum_g = 0, sum_r = 0; |
| 228 | 55 |
| 229 for (unsigned int i = 0; i < 8; ++i) { | 56 for (unsigned int i = 0; i < 8; ++i) { |
| 230 sum_b += src[i].channels.b; | 57 sum_b += src[i].channels.b; |
| 231 sum_g += src[i].channels.g; | 58 sum_g += src[i].channels.g; |
| 232 sum_r += src[i].channels.r; | 59 sum_r += src[i].channels.r; |
| 233 } | 60 } |
| 234 | 61 |
| 235 const float kInv8 = 1.0f / 8.0f; | 62 const float kInv8 = 1.0f / 8.0f; |
| 236 avg_color[0] = static_cast<float>(sum_b) * kInv8; | 63 avg_color[0] = static_cast<float>(sum_b) * kInv8; |
| 237 avg_color[1] = static_cast<float>(sum_g) * kInv8; | 64 avg_color[1] = static_cast<float>(sum_g) * kInv8; |
| 238 avg_color[2] = static_cast<float>(sum_r) * kInv8; | 65 avg_color[2] = static_cast<float>(sum_r) * kInv8; |
| 239 } | 66 } |
| 240 | 67 |
| 241 void ComputeLuminance(uint8_t* block, | 68 void ComputeLuminance(uint8_t* block, |
| 242 const Color* src, | 69 const cc::Color* src, |
| 243 const Color& base, | 70 const cc::Color& base, |
| 244 int sub_block_id, | 71 int sub_block_id, |
| 245 const uint8_t* idx_to_num_tab) { | 72 const uint8_t* idx_to_num_tab) { |
| 246 uint32_t best_tbl_err = std::numeric_limits<uint32_t>::max(); | 73 uint32_t best_tbl_err = std::numeric_limits<uint32_t>::max(); |
| 247 uint8_t best_tbl_idx = 0; | 74 uint8_t best_tbl_idx = 0; |
| 248 uint8_t best_mod_idx[8][8]; // [table][texel] | 75 uint8_t best_mod_idx[8][8]; // [table][texel] |
| 249 | 76 |
| 250 // Try all codeword tables to find the one giving the best results for this | 77 // Try all codeword tables to find the one giving the best results for this |
| 251 // block. | 78 // block. |
| 252 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) { | 79 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) { |
| 253 // Pre-compute all the candidate colors; combinations of the base color and | 80 // Pre-compute all the candidate colors; combinations of the base color and |
| 254 // all available luminance values. | 81 // all available luminance values. |
| 255 Color candidate_color[4]; // [modifier] | 82 cc::Color candidate_color[4]; // [modifier] |
| 256 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { | 83 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { |
| 257 int16_t lum = g_codeword_tables[tbl_idx][mod_idx]; | 84 int16_t lum = cc::g_codeword_tables[tbl_idx][mod_idx]; |
| 258 candidate_color[mod_idx] = MakeColor(base, lum); | 85 candidate_color[mod_idx] = MakeColor(base, lum); |
| 259 } | 86 } |
| 260 | 87 |
| 261 uint32_t tbl_err = 0; | 88 uint32_t tbl_err = 0; |
| 262 | 89 |
| 263 for (unsigned int i = 0; i < 8; ++i) { | 90 for (unsigned int i = 0; i < 8; ++i) { |
| 264 // Try all modifiers in the current table to find which one gives the | 91 // Try all modifiers in the current table to find which one gives the |
| 265 // smallest error. | 92 // smallest error. |
| 266 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max(); | 93 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max(); |
| 267 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { | 94 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { |
| 268 const Color& color = candidate_color[mod_idx]; | 95 const cc::Color& color = candidate_color[mod_idx]; |
| 269 | 96 |
| 270 uint32_t mod_err = GetColorError(src[i], color); | 97 uint32_t mod_err = GetColorError(src[i], color); |
| 271 if (mod_err < best_mod_err) { | 98 if (mod_err < best_mod_err) { |
| 272 best_mod_idx[tbl_idx][i] = mod_idx; | 99 best_mod_idx[tbl_idx][i] = mod_idx; |
| 273 best_mod_err = mod_err; | 100 best_mod_err = mod_err; |
| 274 | 101 |
| 275 if (mod_err == 0) | 102 if (mod_err == 0) |
| 276 break; // We cannot do any better than this. | 103 break; // We cannot do any better than this. |
| 277 } | 104 } |
| 278 } | 105 } |
| 279 | 106 |
| 280 tbl_err += best_mod_err; | 107 tbl_err += best_mod_err; |
| 281 if (tbl_err > best_tbl_err) | 108 if (tbl_err > best_tbl_err) |
| 282 break; // We're already doing worse than the best table so skip. | 109 break; // We're already doing worse than the best table so skip. |
| 283 } | 110 } |
| 284 | 111 |
| 285 if (tbl_err < best_tbl_err) { | 112 if (tbl_err < best_tbl_err) { |
| 286 best_tbl_err = tbl_err; | 113 best_tbl_err = tbl_err; |
| 287 best_tbl_idx = tbl_idx; | 114 best_tbl_idx = tbl_idx; |
| 288 | 115 |
| 289 if (tbl_err == 0) | 116 if (tbl_err == 0) |
| 290 break; // We cannot do any better than this. | 117 break; // We cannot do any better than this. |
| 291 } | 118 } |
| 292 } | 119 } |
| 293 | 120 |
| 294 WriteCodewordTable(block, sub_block_id, best_tbl_idx); | 121 cc::WriteCodewordTable(block, sub_block_id, best_tbl_idx); |
| 295 | 122 |
| 296 uint32_t pix_data = 0; | 123 uint32_t pix_data = 0; |
| 297 | 124 |
| 298 for (unsigned int i = 0; i < 8; ++i) { | 125 for (unsigned int i = 0; i < 8; ++i) { |
| 299 uint8_t mod_idx = best_mod_idx[best_tbl_idx][i]; | 126 uint8_t mod_idx = best_mod_idx[best_tbl_idx][i]; |
| 300 uint8_t pix_idx = g_mod_to_pix[mod_idx]; | 127 uint8_t pix_idx = cc::g_mod_to_pix[mod_idx]; |
| 301 | 128 |
| 302 uint32_t lsb = pix_idx & 0x1; | 129 uint32_t lsb = pix_idx & 0x1; |
| 303 uint32_t msb = pix_idx >> 1; | 130 uint32_t msb = pix_idx >> 1; |
| 304 | 131 |
| 305 // Obtain the texel number as specified in the standard. | 132 // Obtain the texel number as specified in the standard. |
| 306 int texel_num = idx_to_num_tab[i]; | 133 int texel_num = idx_to_num_tab[i]; |
| 307 pix_data |= msb << (texel_num + 16); | 134 pix_data |= msb << (texel_num + 16); |
| 308 pix_data |= lsb << (texel_num); | 135 pix_data |= lsb << (texel_num); |
| 309 } | 136 } |
| 310 | 137 |
| 311 WritePixelData(block, pix_data); | 138 cc::WritePixelData(block, pix_data); |
| 312 } | 139 } |
| 313 | 140 |
| 314 /** | 141 /** |
| 315 * Tries to compress the block under the assumption that it's a single color | 142 * Tries to compress the block under the assumption that it's a single color |
| 316 * block. If it's not the function will bail out without writing anything to | 143 * block. If it's not the function will bail out without writing anything to |
| 317 * the destination buffer. | 144 * the destination buffer. |
| 318 */ | 145 */ |
| 319 bool TryCompressSolidBlock(uint8_t* dst, const Color* src) { | 146 bool TryCompressSolidBlock(uint8_t* dst, const cc::Color* src) { |
| 320 for (unsigned int i = 1; i < 16; ++i) { | 147 for (unsigned int i = 1; i < 16; ++i) { |
| 321 if (src[i].bits != src[0].bits) | 148 if (src[i].bits != src[0].bits) |
| 322 return false; | 149 return false; |
| 323 } | 150 } |
| 324 | 151 |
| 325 // Clear destination buffer so that we can "or" in the results. | 152 // Clear destination buffer so that we can "or" in the results. |
| 326 memset(dst, 0, 8); | 153 memset(dst, 0, 8); |
| 327 | 154 |
| 328 float src_color_float[3] = {static_cast<float>(src->channels.b), | 155 float src_color_float[3] = {static_cast<float>(src->channels.b), |
| 329 static_cast<float>(src->channels.g), | 156 static_cast<float>(src->channels.g), |
| 330 static_cast<float>(src->channels.r)}; | 157 static_cast<float>(src->channels.r)}; |
| 331 Color base = MakeColor555(src_color_float); | 158 cc::Color base = cc::MakeColor555(src_color_float); |
| 332 | 159 |
| 333 WriteDiff(dst, true); | 160 cc::WriteDiff(dst, true); |
| 334 WriteFlip(dst, false); | 161 cc::WriteFlip(dst, false); |
| 335 WriteColors555(dst, base, base); | 162 cc::WriteColors555(dst, base, base); |
| 336 | 163 |
| 337 uint8_t best_tbl_idx = 0; | 164 uint8_t best_tbl_idx = 0; |
| 338 uint8_t best_mod_idx = 0; | 165 uint8_t best_mod_idx = 0; |
| 339 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max(); | 166 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max(); |
| 340 | 167 |
| 341 // Try all codeword tables to find the one giving the best results for this | 168 // Try all codeword tables to find the one giving the best results for this |
| 342 // block. | 169 // block. |
| 343 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) { | 170 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) { |
| 344 // Try all modifiers in the current table to find which one gives the | 171 // Try all modifiers in the current table to find which one gives the |
| 345 // smallest error. | 172 // smallest error. |
| 346 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { | 173 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) { |
| 347 int16_t lum = g_codeword_tables[tbl_idx][mod_idx]; | 174 int16_t lum = cc::g_codeword_tables[tbl_idx][mod_idx]; |
| 348 const Color& color = MakeColor(base, lum); | 175 const cc::Color& color = MakeColor(base, lum); |
| 349 | 176 |
| 350 uint32_t mod_err = GetColorError(*src, color); | 177 uint32_t mod_err = GetColorError(*src, color); |
| 351 if (mod_err < best_mod_err) { | 178 if (mod_err < best_mod_err) { |
| 352 best_tbl_idx = tbl_idx; | 179 best_tbl_idx = tbl_idx; |
| 353 best_mod_idx = mod_idx; | 180 best_mod_idx = mod_idx; |
| 354 best_mod_err = mod_err; | 181 best_mod_err = mod_err; |
| 355 | 182 |
| 356 if (mod_err == 0) | 183 if (mod_err == 0) |
| 357 break; // We cannot do any better than this. | 184 break; // We cannot do any better than this. |
| 358 } | 185 } |
| 359 } | 186 } |
| 360 | 187 |
| 361 if (best_mod_err == 0) | 188 if (best_mod_err == 0) |
| 362 break; | 189 break; |
| 363 } | 190 } |
| 364 | 191 |
| 365 WriteCodewordTable(dst, 0, best_tbl_idx); | 192 cc::WriteCodewordTable(dst, 0, best_tbl_idx); |
| 366 WriteCodewordTable(dst, 1, best_tbl_idx); | 193 cc::WriteCodewordTable(dst, 1, best_tbl_idx); |
| 367 | 194 |
| 368 uint8_t pix_idx = g_mod_to_pix[best_mod_idx]; | 195 uint8_t pix_idx = cc::g_mod_to_pix[best_mod_idx]; |
| 369 uint32_t lsb = pix_idx & 0x1; | 196 uint32_t lsb = pix_idx & 0x1; |
| 370 uint32_t msb = pix_idx >> 1; | 197 uint32_t msb = pix_idx >> 1; |
| 371 | 198 |
| 372 uint32_t pix_data = 0; | 199 uint32_t pix_data = 0; |
| 373 for (unsigned int i = 0; i < 2; ++i) { | 200 for (unsigned int i = 0; i < 2; ++i) { |
| 374 for (unsigned int j = 0; j < 8; ++j) { | 201 for (unsigned int j = 0; j < 8; ++j) { |
| 375 // Obtain the texel number as specified in the standard. | 202 // Obtain the texel number as specified in the standard. |
| 376 int texel_num = g_idx_to_num[i][j]; | 203 int texel_num = cc::g_idx_to_num[i][j]; |
| 377 pix_data |= msb << (texel_num + 16); | 204 pix_data |= msb << (texel_num + 16); |
| 378 pix_data |= lsb << (texel_num); | 205 pix_data |= lsb << (texel_num); |
| 379 } | 206 } |
| 380 } | 207 } |
| 381 | 208 |
| 382 WritePixelData(dst, pix_data); | 209 cc::WritePixelData(dst, pix_data); |
| 383 return true; | 210 return true; |
| 384 } | 211 } |
| 385 | 212 |
| 386 void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) { | 213 void CompressBlock(uint8_t* dst, |
| 214 const cc::Color* ver_src, | |
| 215 const cc::Color* hor_src) { | |
| 387 if (TryCompressSolidBlock(dst, ver_src)) | 216 if (TryCompressSolidBlock(dst, ver_src)) |
| 388 return; | 217 return; |
| 389 | 218 |
| 390 const Color* sub_block_src[4] = {ver_src, ver_src + 8, hor_src, hor_src + 8}; | 219 const cc::Color* sub_block_src[4] = { |
| 220 ver_src, ver_src + 8, hor_src, hor_src + 8}; | |
| 391 | 221 |
| 392 Color sub_block_avg[4]; | 222 cc::Color sub_block_avg[4]; |
| 393 bool use_differential[2] = {true, true}; | 223 bool use_differential[2] = {true, true}; |
| 394 | 224 |
| 395 // Compute the average color for each sub block and determine if differential | 225 // Compute the average color for each sub block and determine if differential |
| 396 // coding can be used. | 226 // coding can be used. |
| 397 for (unsigned int i = 0, j = 1; i < 4; i += 2, j += 2) { | 227 for (unsigned int i = 0, j = 1; i < 4; i += 2, j += 2) { |
| 398 float avg_color_0[3]; | 228 float avg_color_0[3]; |
| 399 GetAverageColor(sub_block_src[i], avg_color_0); | 229 GetAverageColor(sub_block_src[i], avg_color_0); |
| 400 Color avg_color_555_0 = MakeColor555(avg_color_0); | 230 cc::Color avg_color_555_0 = cc::MakeColor555(avg_color_0); |
| 401 | 231 |
| 402 float avg_color_1[3]; | 232 float avg_color_1[3]; |
| 403 GetAverageColor(sub_block_src[j], avg_color_1); | 233 GetAverageColor(sub_block_src[j], avg_color_1); |
| 404 Color avg_color_555_1 = MakeColor555(avg_color_1); | 234 cc::Color avg_color_555_1 = cc::MakeColor555(avg_color_1); |
| 405 | 235 |
| 406 for (unsigned int light_idx = 0; light_idx < 3; ++light_idx) { | 236 for (unsigned int light_idx = 0; light_idx < 3; ++light_idx) { |
| 407 int u = avg_color_555_0.components[light_idx] >> 3; | 237 int u = avg_color_555_0.components[light_idx] >> 3; |
| 408 int v = avg_color_555_1.components[light_idx] >> 3; | 238 int v = avg_color_555_1.components[light_idx] >> 3; |
| 409 | 239 |
| 410 int component_diff = v - u; | 240 int component_diff = v - u; |
| 411 if (component_diff < -4 || component_diff > 3) { | 241 if (component_diff < -4 || component_diff > 3) { |
| 412 use_differential[i / 2] = false; | 242 use_differential[i / 2] = false; |
| 413 sub_block_avg[i] = MakeColor444(avg_color_0); | 243 sub_block_avg[i] = cc::MakeColor444(avg_color_0); |
| 414 sub_block_avg[j] = MakeColor444(avg_color_1); | 244 sub_block_avg[j] = cc::MakeColor444(avg_color_1); |
| 415 } else { | 245 } else { |
| 416 sub_block_avg[i] = avg_color_555_0; | 246 sub_block_avg[i] = avg_color_555_0; |
| 417 sub_block_avg[j] = avg_color_555_1; | 247 sub_block_avg[j] = avg_color_555_1; |
| 418 } | 248 } |
| 419 } | 249 } |
| 420 } | 250 } |
| 421 | 251 |
| 422 // Compute the error of each sub block before adjusting for luminance. These | 252 // Compute the error of each sub block before adjusting for luminance. These |
| 423 // error values are later used for determining if we should flip the sub | 253 // error values are later used for determining if we should flip the sub |
| 424 // block or not. | 254 // block or not. |
| 425 uint32_t sub_block_err[4] = {0}; | 255 uint32_t sub_block_err[4] = {0}; |
| 426 for (unsigned int i = 0; i < 4; ++i) { | 256 for (unsigned int i = 0; i < 4; ++i) { |
| 427 for (unsigned int j = 0; j < 8; ++j) { | 257 for (unsigned int j = 0; j < 8; ++j) { |
| 428 sub_block_err[i] += GetColorError(sub_block_avg[i], sub_block_src[i][j]); | 258 sub_block_err[i] += GetColorError(sub_block_avg[i], sub_block_src[i][j]); |
| 429 } | 259 } |
| 430 } | 260 } |
| 431 | 261 |
| 432 bool flip = | 262 bool flip = |
| 433 sub_block_err[2] + sub_block_err[3] < sub_block_err[0] + sub_block_err[1]; | 263 sub_block_err[2] + sub_block_err[3] < sub_block_err[0] + sub_block_err[1]; |
| 434 | 264 |
| 435 // Clear destination buffer so that we can "or" in the results. | 265 // Clear destination buffer so that we can "or" in the results. |
| 436 memset(dst, 0, 8); | 266 memset(dst, 0, 8); |
| 437 | 267 |
| 438 WriteDiff(dst, use_differential[!!flip]); | 268 cc::WriteDiff(dst, use_differential[!!flip]); |
| 439 WriteFlip(dst, flip); | 269 cc::WriteFlip(dst, flip); |
| 440 | 270 |
| 441 uint8_t sub_block_off_0 = flip ? 2 : 0; | 271 uint8_t sub_block_off_0 = flip ? 2 : 0; |
| 442 uint8_t sub_block_off_1 = sub_block_off_0 + 1; | 272 uint8_t sub_block_off_1 = sub_block_off_0 + 1; |
| 443 | 273 |
| 444 if (use_differential[!!flip]) { | 274 if (use_differential[!!flip]) { |
| 445 WriteColors555(dst, sub_block_avg[sub_block_off_0], | 275 cc::WriteColors555(dst, sub_block_avg[sub_block_off_0], |
| 446 sub_block_avg[sub_block_off_1]); | 276 sub_block_avg[sub_block_off_1]); |
| 447 } else { | 277 } else { |
| 448 WriteColors444(dst, sub_block_avg[sub_block_off_0], | 278 cc::WriteColors444(dst, sub_block_avg[sub_block_off_0], |
| 449 sub_block_avg[sub_block_off_1]); | 279 sub_block_avg[sub_block_off_1]); |
| 450 } | 280 } |
| 451 | 281 |
| 452 // Compute luminance for the first sub block. | 282 // Compute luminance for the first sub block. |
| 453 ComputeLuminance(dst, sub_block_src[sub_block_off_0], | 283 ComputeLuminance(dst, sub_block_src[sub_block_off_0], |
| 454 sub_block_avg[sub_block_off_0], 0, | 284 sub_block_avg[sub_block_off_0], 0, |
| 455 g_idx_to_num[sub_block_off_0]); | 285 cc::g_idx_to_num[sub_block_off_0]); |
| 456 // Compute luminance for the second sub block. | 286 // Compute luminance for the second sub block. |
| 457 ComputeLuminance(dst, sub_block_src[sub_block_off_1], | 287 ComputeLuminance(dst, sub_block_src[sub_block_off_1], |
| 458 sub_block_avg[sub_block_off_1], 1, | 288 sub_block_avg[sub_block_off_1], 1, |
| 459 g_idx_to_num[sub_block_off_1]); | 289 cc::g_idx_to_num[sub_block_off_1]); |
| 460 } | 290 } |
| 461 | 291 |
| 462 } // namespace | 292 } // namespace |
| 463 | 293 |
| 464 namespace cc { | 294 namespace cc { |
|
reveman
2015/05/07 14:24:35
nit: move this above "namespace {" on line 21 and
radu.velea
2015/05/07 15:53:45
Done.
| |
| 465 | 295 |
| 466 void TextureCompressorETC1::Compress(const uint8_t* src, | 296 void TextureCompressorETC1::Compress(const uint8_t* src, |
| 467 uint8_t* dst, | 297 uint8_t* dst, |
| 468 int width, | 298 int width, |
| 469 int height, | 299 int height, |
| 470 Quality quality) { | 300 Quality quality) { |
| 471 DCHECK(width >= 4 && (width & 3) == 0); | 301 DCHECK_GE(width, 4); |
| 472 DCHECK(height >= 4 && (height & 3) == 0); | 302 DCHECK_EQ((width & 3), 0); |
| 303 DCHECK_GE(height, 4); | |
| 304 DCHECK_EQ((height & 3), 0); | |
| 473 | 305 |
| 474 Color ver_blocks[16]; | 306 Color ver_blocks[16]; |
| 475 Color hor_blocks[16]; | 307 Color hor_blocks[16]; |
| 476 | 308 |
| 477 for (int y = 0; y < height; y += 4, src += width * 4 * 4) { | 309 for (int y = 0; y < height; y += 4, src += width * 4 * 4) { |
| 478 for (int x = 0; x < width; x += 4, dst += 8) { | 310 for (int x = 0; x < width; x += 4, dst += 8) { |
| 479 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4); | 311 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4); |
| 480 const Color* row1 = row0 + width; | 312 const Color* row1 = row0 + width; |
| 481 const Color* row2 = row1 + width; | 313 const Color* row2 = row1 + width; |
| 482 const Color* row3 = row2 + width; | 314 const Color* row3 = row2 + width; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 494 memcpy(hor_blocks + 4, row1, 16); | 326 memcpy(hor_blocks + 4, row1, 16); |
| 495 memcpy(hor_blocks + 8, row2, 16); | 327 memcpy(hor_blocks + 8, row2, 16); |
| 496 memcpy(hor_blocks + 12, row3, 16); | 328 memcpy(hor_blocks + 12, row3, 16); |
| 497 | 329 |
| 498 CompressBlock(dst, ver_blocks, hor_blocks); | 330 CompressBlock(dst, ver_blocks, hor_blocks); |
| 499 } | 331 } |
| 500 } | 332 } |
| 501 } | 333 } |
| 502 | 334 |
| 503 } // namespace cc | 335 } // namespace cc |
| OLD | NEW |