Fixed integer overflow that caused incorrect quantization of large images,

or an image with a lot of a particular color.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4031 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-04-18 20:35:28 +00:00
parent f5161d833c
commit 92f86774ba
@@ -1,5 +1,5 @@
// //
// $Id: Quantize.java,v 1.4 2004/08/27 02:12:38 mdb Exp $ // $Id$
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -40,6 +40,7 @@ package com.threerings.media.image;
* - Made it work with image data with transparent pixels. * - Made it work with image data with transparent pixels.
* - Clarified documentation of the main method. * - Clarified documentation of the main method.
* - Changed the 'QUICK' constant to false for better quantization. * - Changed the 'QUICK' constant to false for better quantization.
* - Fixed an integer overflow that caused a bug quantizing large images.
* *
* <p><p> * <p><p>
* *
@@ -465,10 +466,10 @@ public class Quantize {
* characteristics for later averaging. * characteristics for later averaging.
*/ */
void reduction() { void reduction() {
int threshold = 1; long threshold = 1;
while (colors > max_colors) { while (colors > max_colors) {
colors = 0; colors = 0;
threshold = root.reduce(threshold, Integer.MAX_VALUE); threshold = root.reduce(threshold, Long.MAX_VALUE);
} }
} }
@@ -584,7 +585,7 @@ public class Quantize {
int mid_blue; int mid_blue;
// the pixel count for this node and all children // the pixel count for this node and all children
int number_pixels; long number_pixels;
// the pixel count for this node // the pixel count for this node
int unique; int unique;
@@ -603,7 +604,7 @@ public class Quantize {
this.id = 0; this.id = 0;
this.level = 0; this.level = 0;
this.number_pixels = Integer.MAX_VALUE; this.number_pixels = Long.MAX_VALUE;
this.mid_red = (MAX_RGB + 1) >> 1; this.mid_red = (MAX_RGB + 1) >> 1;
this.mid_green = (MAX_RGB + 1) >> 1; this.mid_green = (MAX_RGB + 1) >> 1;
@@ -673,7 +674,7 @@ public class Quantize {
* - figure out the color with the fewest pixels * - figure out the color with the fewest pixels
* - recalculate the total number of colors in the tree * - recalculate the total number of colors in the tree
*/ */
int reduce(int threshold, int next_threshold) { long reduce(long threshold, long next_threshold) {
if (nchild != 0) { if (nchild != 0) {
for (int id = 0; id < 8; id++) { for (int id = 0; id < 8; id++) {
if (child[id] != null) { if (child[id] != null) {