diff options
Diffstat (limited to 'source/blender/imbuf/intern/dds/ColorBlock.cpp')
-rw-r--r-- | source/blender/imbuf/intern/dds/ColorBlock.cpp | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/source/blender/imbuf/intern/dds/ColorBlock.cpp b/source/blender/imbuf/intern/dds/ColorBlock.cpp index 63997f93c8c..0199d15ada7 100644 --- a/source/blender/imbuf/intern/dds/ColorBlock.cpp +++ b/source/blender/imbuf/intern/dds/ColorBlock.cpp @@ -39,13 +39,13 @@ #include <Common.h> // Get approximate luminance. - inline static unsigned int colorLuminance(Color32 c) + inline static uint colorLuminance(Color32 c) { return c.r + c.g + c.b; } // Get the euclidean distance between the given colors. - inline static unsigned int colorDistance(Color32 c0, Color32 c1) + inline static uint colorDistance(Color32 c0, Color32 c1) { return (c0.r - c1.r) * (c0.r - c1.r) + (c0.g - c1.g) * (c0.g - c1.g) + (c0.b - c1.b) * (c0.b - c1.b); } @@ -59,22 +59,22 @@ ColorBlock::ColorBlock() /// Init the color block with the contents of the given block. ColorBlock::ColorBlock(const ColorBlock & block) { - for(unsigned int i = 0; i < 16; i++) { + for(uint i = 0; i < 16; i++) { color(i) = block.color(i); } } /// Initialize this color block. -ColorBlock::ColorBlock(const Image * img, unsigned int x, unsigned int y) +ColorBlock::ColorBlock(const Image * img, uint x, uint y) { init(img, x, y); } -void ColorBlock::init(const Image * img, unsigned int x, unsigned int y) +void ColorBlock::init(const Image * img, uint x, uint y) { - const unsigned int bw = min(img->width() - x, 4U); - const unsigned int bh = min(img->height() - y, 4U); + const uint bw = min(img->width() - x, 4U); + const uint bh = min(img->height() - y, 4U); static int remainder[] = { 0, 0, 0, 0, @@ -86,10 +86,10 @@ void ColorBlock::init(const Image * img, unsigned int x, unsigned int y) // Blocks that are smaller than 4x4 are handled by repeating the pixels. // @@ Thats only correct when block size is 1, 2 or 4, but not with 3. :( - for(unsigned int i = 0; i < 4; i++) { + for(uint i = 0; i < 4; i++) { //const int by = i % bh; const int by = remainder[(bh - 1) * 4 + i]; - for(unsigned int e = 0; e < 4; e++) { + for(uint e = 0; e < 4; e++) { //const int bx = e % bw; const int bx = remainder[(bw - 1) * 4 + e]; color(e, i) = img->pixel(x + bx, y + by); @@ -111,7 +111,7 @@ void ColorBlock::splatX() { for(int i = 0; i < 16; i++) { - unsigned char x = m_color[i].r; + uint8 x = m_color[i].r; m_color[i] = Color32(x, x, x, x); } } @@ -120,16 +120,16 @@ void ColorBlock::splatY() { for(int i = 0; i < 16; i++) { - unsigned char y = m_color[i].g; + uint8 y = m_color[i].g; m_color[i] = Color32(y, y, y, y); } } /// Count number of unique colors in this color block. -unsigned int ColorBlock::countUniqueColors() const +uint ColorBlock::countUniqueColors() const { - unsigned int count = 0; + uint count = 0; // @@ This does not have to be o(n^2) for(int i = 0; i < 16; i++) @@ -152,17 +152,27 @@ unsigned int ColorBlock::countUniqueColors() const /// Get average color of the block. Color32 ColorBlock::averageColor() const { - unsigned int r, g, b, a; + uint r, g, b, a; r = g = b = a = 0; - for(unsigned int i = 0; i < 16; i++) { + for(uint i = 0; i < 16; i++) { r += m_color[i].r; g += m_color[i].g; b += m_color[i].b; a += m_color[i].a; } - return Color32((unsigned char)(r / 16), (unsigned char)(g / 16), (unsigned char)(b / 16), (unsigned char)(a / 16)); + return Color32(uint8(r / 16), uint8(g / 16), uint8(b / 16), uint8(a / 16)); +} + +/// Return true if the block is not fully opaque. +bool ColorBlock::hasAlpha() const +{ + for (uint i = 0; i < 16; i++) + { + if (m_color[i].a != 255) return true; + } + return false; } @@ -170,11 +180,11 @@ Color32 ColorBlock::averageColor() const void ColorBlock::diameterRange(Color32 * start, Color32 * end) const { Color32 c0, c1; - unsigned int best_dist = 0; + uint best_dist = 0; for(int i = 0; i < 16; i++) { for (int j = i+1; j < 16; j++) { - unsigned int dist = colorDistance(m_color[i], m_color[j]); + uint dist = colorDistance(m_color[i], m_color[j]); if( dist > best_dist ) { best_dist = dist; c0 = m_color[i]; @@ -191,13 +201,13 @@ void ColorBlock::diameterRange(Color32 * start, Color32 * end) const void ColorBlock::luminanceRange(Color32 * start, Color32 * end) const { Color32 minColor, maxColor; - unsigned int minLuminance, maxLuminance; + uint minLuminance, maxLuminance; maxLuminance = minLuminance = colorLuminance(m_color[0]); - for(unsigned int i = 1; i < 16; i++) + for(uint i = 1; i < 16; i++) { - unsigned int luminance = colorLuminance(m_color[i]); + uint luminance = colorLuminance(m_color[i]); if (luminance > maxLuminance) { maxLuminance = luminance; @@ -219,7 +229,7 @@ void ColorBlock::boundsRange(Color32 * start, Color32 * end) const Color32 minColor(255, 255, 255); Color32 maxColor(0, 0, 0); - for(unsigned int i = 0; i < 16; i++) + for(uint i = 0; i < 16; i++) { if (m_color[i].r < minColor.r) { minColor.r = m_color[i].r; } if (m_color[i].g < minColor.g) { minColor.g = m_color[i].g; } @@ -253,7 +263,7 @@ void ColorBlock::boundsRangeAlpha(Color32 * start, Color32 * end) const Color32 minColor(255, 255, 255, 255); Color32 maxColor(0, 0, 0, 0); - for(unsigned int i = 0; i < 16; i++) + for(uint i = 0; i < 16; i++) { if (m_color[i].r < minColor.r) { minColor.r = m_color[i].r; } if (m_color[i].g < minColor.g) { minColor.g = m_color[i].g; } @@ -290,11 +300,11 @@ void ColorBlock::boundsRangeAlpha(Color32 * start, Color32 * end) const void ColorBlock::sortColorsByAbsoluteValue() { // Dummy selection sort. - for( unsigned int a = 0; a < 16; a++ ) { - unsigned int max = a; + for( uint a = 0; a < 16; a++ ) { + uint max = a; Color16 cmax(m_color[a]); - for( unsigned int b = a+1; b < 16; b++ ) { + for( uint b = a+1; b < 16; b++ ) { Color16 cb(m_color[b]); if( cb.u > cmax.u ) { @@ -302,9 +312,6 @@ void ColorBlock::sortColorsByAbsoluteValue() cmax = cb; } } - Color32 tmp; - swap( m_color[a], m_color[max], tmp ); + swap( m_color[a], m_color[max] ); } } - - |