private float Maximize(Box cube, int dir, int first, int last, int cut[],
    			long whole_r, long whole_g, long whole_b, long whole_w) {
    		long half_r, half_g, half_b, half_w;
    		long base_r, base_g, base_b, base_w;
    		int i;
    		float temp, max;

    		base_r = Bottom(cube, dir, mr);
    		base_g = Bottom(cube, dir, mg);
    		base_b = Bottom(cube, dir, mb);
    		base_w = Bottom(cube, dir, wt);
    		
    		max = 0.0f;
    		cut[0] = -1;
    	
    		for(i = first; i < last; ++i){
    			half_r = base_r + Top(cube, dir, i, mr);
    			half_g = base_g + Top(cube, dir, i, mg);
    			half_b = base_b + Top(cube, dir, i, mb);
    			half_w = base_w + Top(cube, dir, i, wt);
    			/* now half_x is sum over lower half of box, if split at i */
    			if (half_w == 0) /* subbox could be empty of pixels! */
    				continue;    /* never split into an empty box */
    			temp = (half_r*half_r + half_g*half_g +	half_b*half_b)/(float)half_w;
    			half_r = whole_r - half_r;
    			half_g = whole_g - half_g;
    			half_b = whole_b - half_b;
    			half_w = whole_w - half_w;
    			if (half_w == 0) /* subbox could be empty of pixels! */
    				continue; /* never split into an empty box */
    			temp += (half_r*half_r + half_g*half_g + half_b*half_b)/(float)half_w;
    			
    			if (temp > max) { max = temp; cut[0] = i;}
    		}
    		
    		return max;
    	}
Java port of
C Implementation of Wu's Color Quantizer (v. 2)
(see Graphics Gems vol. II, pp. 126-133)
Author:	Xiaolin Wu
Dept. of Computer Science
Univ. of Western Ontario
London, Ontario N6A 5B7
wu@csd.uwo.ca

Algorithm: Greedy orthogonal bipartition of RGB space for variance
minimization aided by inclusion-exclusion tricks.
For speed no nearest neighbor search is done. Slightly
better performance can be expected by more sophisticated
but more expensive versions.

The author thanks Tom Lane at Tom_Lane@G.GP.CS.CMU.EDU for much of
additional documentation and a cure to a previous bug.

Free to distribute, comments and suggestions are appreciated.
subs/gif-revert/src/main/java/com/engineer/gif/revert/lib/AnimatedGIFWriter.java