Box-average color of an RGBA8 buffer. Used as ITextureSource::GetAverageColor().
| 80 | |
| 81 | // Box-average color of an RGBA8 buffer. Used as ITextureSource::GetAverageColor(). |
| 82 | PackedColor ComputeAverage(const uint8_t* rgba, int w, int h) |
| 83 | { |
| 84 | uint64_t sumR = 0, sumG = 0, sumB = 0, sumA = 0; |
| 85 | const size_t n = static_cast<size_t>(w) * static_cast<size_t>(h); |
| 86 | for (size_t i = 0; i < n; ++i) |
| 87 | { |
| 88 | sumR += rgba[i * 4 + 0]; |
| 89 | sumG += rgba[i * 4 + 1]; |
| 90 | sumB += rgba[i * 4 + 2]; |
| 91 | sumA += rgba[i * 4 + 3]; |
| 92 | } |
| 93 | if (n == 0) |
| 94 | return PackedBlack; |
| 95 | uint8_t r = static_cast<uint8_t>(sumR / n); |
| 96 | uint8_t g = static_cast<uint8_t>(sumG / n); |
| 97 | uint8_t b = static_cast<uint8_t>(sumB / n); |
| 98 | uint8_t a = static_cast<uint8_t>(sumA / n); |
| 99 | return PackedColor(r, g, b, a); |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 |
no test coverage detected