| 63 | } |
| 64 | |
| 65 | static int FitCodes(uint8_t const* tile, uint8_t const* codes, uint8_t* indices) |
| 66 | { |
| 67 | // fit each alpha value to the codebook |
| 68 | int err = 0; |
| 69 | for (int i = 0; i < 16; ++i) |
| 70 | { |
| 71 | // find the least error and corresponding index |
| 72 | int value = (int)(tile[i]); |
| 73 | int least = INT_MAX; |
| 74 | int index = 0; |
| 75 | for (int j = 0; j < 8; ++j) |
| 76 | { |
| 77 | // get the squared error from this code |
| 78 | int dist = (int)value - (int)codes[j]; |
| 79 | dist *= dist; |
| 80 | |
| 81 | // compare with the best so far |
| 82 | if (dist < least) |
| 83 | { |
| 84 | least = dist; |
| 85 | index = j; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // save this index and accumulate the error |
| 90 | indices[i] = (uint8_t)index; |
| 91 | err += least; |
| 92 | } |
| 93 | |
| 94 | // return the total error |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | static void WriteAlphaBlock(int alpha0, int alpha1, uint8_t const* indices, void* block) |
| 99 | { |