basisu_global_selector_palette_helpers.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // basiu_global_selector_palette_helpers.cpp
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_global_selector_palette_helpers.h"
  16. namespace basisu
  17. {
  18. uint64_t etc1_global_selector_codebook_find_best_entry(const basist::etc1_global_selector_codebook &codebook,
  19. uint32_t num_src_pixel_blocks, const pixel_block *pSrc_pixel_blocks, const etc_block *pBlock_endpoints,
  20. uint32_t &palette_index, basist::etc1_global_palette_entry_modifier &palette_modifier,
  21. bool perceptual, uint32_t max_pal_entries, uint32_t max_modifiers)
  22. {
  23. uint64_t best_err = UINT64_MAX;
  24. uint32_t best_pal_index = 0;
  25. basist::etc1_global_palette_entry_modifier best_pal_modifier;
  26. if (!max_pal_entries)
  27. max_pal_entries = codebook.size();
  28. if (!max_modifiers)
  29. max_modifiers = basist::etc1_global_palette_entry_modifier::cTotalValues;
  30. for (uint32_t pal_index = 0; pal_index < max_pal_entries; pal_index++)
  31. {
  32. for (uint32_t mod_index = 0; mod_index < max_modifiers; mod_index++)
  33. {
  34. const basist::etc1_global_palette_entry_modifier pal_modifier(mod_index);
  35. const basist::etc1_selector_palette_entry pal_entry(codebook.get_entry(pal_index, pal_modifier));
  36. uint64_t trial_err = 0;
  37. for (uint32_t block_index = 0; block_index < num_src_pixel_blocks; block_index++)
  38. {
  39. etc_block trial_block(pBlock_endpoints[block_index]);
  40. for (uint32_t y = 0; y < 4; y++)
  41. for (uint32_t x = 0; x < 4; x++)
  42. trial_block.set_selector(x, y, pal_entry(x, y));
  43. trial_err += trial_block.evaluate_etc1_error(reinterpret_cast<const basisu::color_rgba *>(pSrc_pixel_blocks[block_index].get_ptr()), perceptual);
  44. if (trial_err >= best_err)
  45. break;
  46. }
  47. if (trial_err < best_err)
  48. {
  49. best_err = trial_err;
  50. best_pal_index = pal_index;
  51. best_pal_modifier = pal_modifier;
  52. }
  53. } // mod_index
  54. } // pal_index
  55. palette_index = best_pal_index;
  56. palette_modifier = best_pal_modifier;
  57. return best_err;
  58. }
  59. } // namespace basisu