MCPcopy Create free account
hub / github.com/BohemiaInteractive/CWR / ComputeAverage

Function ComputeAverage

engine/Poseidon/Graphics/Textures/JpgImport.cpp:82–100  ·  view source on GitHub ↗

Box-average color of an RGBA8 buffer. Used as ITextureSource::GetAverageColor().

Source from the content-addressed store, hash-verified

80
81// Box-average color of an RGBA8 buffer. Used as ITextureSource::GetAverageColor().
82PackedColor 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

Callers 1

InitFromMemoryMethod · 0.85

Calls 1

PackedColorClass · 0.50

Tested by

no test coverage detected