From 91fbf1fcc4b96aca43a793da2bc364eccbc4816e Mon Sep 17 00:00:00 2001 From: Rich Geldreich Date: Thu, 19 Nov 2015 18:55:22 -0800 Subject: [PATCH] Fixing integer overflow problem, which can rarely cause serious artifacts. --- crnlib/crn_dxt1.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crnlib/crn_dxt1.cpp b/crnlib/crn_dxt1.cpp index 6ee75e6..34dcf94 100644 --- a/crnlib/crn_dxt1.cpp +++ b/crnlib/crn_dxt1.cpp @@ -112,9 +112,9 @@ namespace crnlib uint weight = m_unique_colors[i].m_weight; total_weight += weight; - tot_r += m_unique_colors[i].m_color.r * weight; - tot_g += m_unique_colors[i].m_color.g * weight; - tot_b += m_unique_colors[i].m_color.b * weight; + tot_r += m_unique_colors[i].m_color.r * static_cast(weight); + tot_g += m_unique_colors[i].m_color.g * static_cast(weight); + tot_b += m_unique_colors[i].m_color.b * static_cast(weight); } const uint half_total_weight = total_weight >> 1; @@ -494,7 +494,7 @@ namespace crnlib ll[2] = (ll[0]*2+ll[1])/3; ll[3] = (ll[0]+ll[1]*2)/3; - uint error_to_beat = 0; + uint64 error_to_beat = 0; uint min_color_weight = 0; uint max_color_weight = 0; for (uint i = 0; i < m_unique_colors.size(); i++) @@ -503,7 +503,7 @@ namespace crnlib uint w = m_unique_colors[i].m_weight; int delta = ll[m_best_solution.m_selectors[i]] - c; - error_to_beat += (int)w * (delta * delta); + error_to_beat += static_cast(w) * (delta * delta); if (c == min_color[comp_index]) min_color_weight += w; @@ -561,11 +561,11 @@ namespace crnlib tl[2] = (tl[0]*2+tl[1])/3; tl[3] = (tl[0]+tl[1]*2)/3; - uint trial_error = 0; + uint64 trial_error = 0; for (uint i = 0; i < m_unique_colors.size(); i++) { int delta = tl[m_best_solution.m_selectors[i]] - m_unique_colors[i].m_color[comp_index]; - trial_error += m_unique_colors[i].m_weight * (delta * delta); + trial_error += static_cast(m_unique_colors[i].m_weight) * (delta * delta); if (trial_error >= error_to_beat) break; } @@ -599,7 +599,7 @@ namespace crnlib for (uint i = 0; i < m_unique_colors.size(); i++) { int delta = tl[m_best_solution.m_selectors[i]] - m_unique_colors[i].m_color[comp_index]; - error_to_beat += m_unique_colors[i].m_weight * (delta * delta); + error_to_beat += static_cast(m_unique_colors[i].m_weight) * (delta * delta); } } // better @@ -1582,7 +1582,7 @@ namespace crnlib err = color_distance(true, c, colors[3], false); if (err < best_error) { best_error = err; best_color_index = 3; } - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break; @@ -1607,7 +1607,7 @@ namespace crnlib err = color_distance(false, c, colors[3], false); if (err < best_error) { best_error = err; best_color_index = 3; } - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break; @@ -1634,7 +1634,7 @@ namespace crnlib err = color_distance(true, c, colors[2], false); if (err < best_error) { best_error = err; best_color_index = 2; } - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break; @@ -1656,7 +1656,7 @@ namespace crnlib err = color_distance(false, c, colors[2], false); if (err < best_error) { best_error = err; best_color_index = 2; } - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break; @@ -1781,7 +1781,7 @@ namespace crnlib uint best_error = color_distance(m_perceptual, c, colors[best_color_index], false); - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break; @@ -1814,7 +1814,7 @@ namespace crnlib uint best_error = color_distance(m_perceptual, c, colors[best_color_index], false); - trial_error += best_error * m_unique_colors[unique_color_index].m_weight; + trial_error += best_error * static_cast(m_unique_colors[unique_color_index].m_weight); if (trial_error >= solution.m_error) break;