ThumbnailPalette.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "ThumbnailPalette.h"
  2. #include "CoverImage.h"
  3. #include <algorithm>
  4. #include <array>
  5. #include <cmath>
  6. #include <vector>
  7. using namespace WallpaperEngine::Media;
  8. namespace {
  9. constexpr int kBucketBits = 4;
  10. constexpr int kBucketsPerChannel = 1 << kBucketBits;
  11. // colors closer than this (euclidean distance in 0-1 RGB) count as the same color
  12. constexpr float kMinDistinctDistance = 0.28f;
  13. struct Bucket {
  14. uint32_t count = 0;
  15. glm::vec3 sum = { 0.0f, 0.0f, 0.0f };
  16. [[nodiscard]] glm::vec3 average () const { return sum / static_cast<float> (count); }
  17. };
  18. float luminance (const glm::vec3& color) {
  19. const auto linear = [] (float channel) {
  20. return channel <= 0.03928f ? channel / 12.92f : std::pow ((channel + 0.055f) / 1.055f, 2.4f);
  21. };
  22. return 0.2126f * linear (color.r) + 0.7152f * linear (color.g) + 0.0722f * linear (color.b);
  23. }
  24. float contrastRatio (const glm::vec3& a, const glm::vec3& b) {
  25. const float la = luminance (a);
  26. const float lb = luminance (b);
  27. return (std::max (la, lb) + 0.05f) / (std::min (la, lb) + 0.05f);
  28. }
  29. } // namespace
  30. ThumbnailPalette WallpaperEngine::Media::computeThumbnailPalette (const uint8_t* rgba, size_t width, size_t height) {
  31. ThumbnailPalette palette;
  32. if (rgba == nullptr || width == 0 || height == 0) {
  33. return palette;
  34. }
  35. std::array<Bucket, kBucketsPerChannel * kBucketsPerChannel * kBucketsPerChannel> buckets;
  36. // looking at every pixel of a 3000px cover is pointless, a ~96x96 grid is plenty
  37. const size_t stepX = std::max<size_t> (1, width / 96);
  38. const size_t stepY = std::max<size_t> (1, height / 96);
  39. for (size_t y = 0; y < height; y += stepY) {
  40. for (size_t x = 0; x < width; x += stepX) {
  41. const uint8_t* pixel = rgba + (y * width + x) * 4;
  42. if (pixel[3] < 128) {
  43. continue;
  44. }
  45. const size_t index = ((static_cast<size_t> (pixel[0]) >> (8 - kBucketBits)) * kBucketsPerChannel
  46. + (static_cast<size_t> (pixel[1]) >> (8 - kBucketBits)))
  47. * kBucketsPerChannel
  48. + (static_cast<size_t> (pixel[2]) >> (8 - kBucketBits));
  49. buckets[index].count++;
  50. buckets[index].sum += glm::vec3 (pixel[0], pixel[1], pixel[2]) / 255.0f;
  51. }
  52. }
  53. std::vector<const Bucket*> ordered;
  54. for (const auto& bucket : buckets) {
  55. if (bucket.count > 0) {
  56. ordered.push_back (&bucket);
  57. }
  58. }
  59. if (ordered.empty ()) {
  60. return palette;
  61. }
  62. std::ranges::sort (ordered, [] (const Bucket* a, const Bucket* b) { return a->count > b->count; });
  63. std::vector<glm::vec3> picked;
  64. for (const Bucket* bucket : ordered) {
  65. const glm::vec3 color = bucket->average ();
  66. const bool distinct = std::ranges::all_of (picked, [&color] (const glm::vec3& other) {
  67. return glm::distance (color, other) >= kMinDistinctDistance;
  68. });
  69. if (distinct) {
  70. picked.push_back (color);
  71. }
  72. if (picked.size () == 3) {
  73. break;
  74. }
  75. }
  76. // flat covers only have one or two distinct colors, reuse what there is
  77. while (picked.size () < 3) {
  78. picked.push_back (picked.back ());
  79. }
  80. palette.primary = picked[0];
  81. palette.secondary = picked[1];
  82. palette.tertiary = picked[2];
  83. const glm::vec3 white (1.0f);
  84. const glm::vec3 black (0.0f);
  85. palette.highContrast
  86. = contrastRatio (palette.primary, white) >= contrastRatio (palette.primary, black) ? white : black;
  87. palette.text = palette.highContrast;
  88. // a color from the cover itself is nicer than plain black/white as long as it is readable
  89. float bestContrast = 4.5f;
  90. for (const glm::vec3& candidate : { palette.secondary, palette.tertiary }) {
  91. const float contrast = contrastRatio (palette.primary, candidate);
  92. if (contrast > bestContrast) {
  93. bestContrast = contrast;
  94. palette.text = candidate;
  95. }
  96. }
  97. return palette;
  98. }
  99. ThumbnailPalette WallpaperEngine::Media::loadThumbnailPalette (const std::string& path) {
  100. const auto image = loadCoverImage (path);
  101. if (!image.has_value ()) {
  102. return {};
  103. }
  104. return computeThumbnailPalette (
  105. image->rgba.data (), static_cast<size_t> (image->width), static_cast<size_t> (image->height)
  106. );
  107. }