1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
package ij.process;
import java.awt.*;
import java.awt.image.*;
import ij.*; //??
/** Converts an RGB image to 8-bit index color using Heckbert's median-cut
color quantization algorithm. Based on median.c by Anton Kruger from the
September, 1994 issue of Dr. Dobbs Journal.
*/
public class MedianCut {
static final int MAXCOLORS = 256; // maximum # of output colors
static final int HSIZE = 32768; // size of image histogram
private int[] hist; // RGB histogram and reverse color lookup table
private int[] histPtr; // points to colors in "hist"
private Cube[] list; // list of cubes
private int[] pixels32;
private int width, height;
private IndexColorModel cm;
public MedianCut(int[] pixels, int width, int height) {
int color16;
pixels32 = pixels;
this.width = width;
this.height = height;
//build 32x32x32 RGB histogram
IJ.showProgress(0.3);
IJ.showStatus("Building 32x32x32 RGB histogram");
hist = new int[HSIZE];
for (int i=0; i<width*height; i++) {
color16 = rgb(pixels32[i]);
hist[color16]++;
}
}
public MedianCut(ColorProcessor ip) {
this((int[])ip.getPixels(), ip.getWidth(), ip.getHeight());
}
int getColorCount() {
int count = 0;
for (int i=0; i<HSIZE; i++)
if (hist[i]>0) count++;
return count;
}
Color getModalColor() {
int max=0;
int c = 0;
for (int i=0; i<HSIZE; i++)
if (hist[i]>max) {
max = hist[i];
c = i;
}
return new Color(red(c), green(c), blue(c));
}
// Convert from 24-bit to 15-bit color
private final int rgb(int c) {
int r = (c&0xf80000)>>19;
int g = (c&0xf800)>>6;
int b = (c&0xf8)<<7;
return b | g | r;
}
// Get red component of a 15-bit color
private final int red(int x) {
return (x&31)<<3;
}
// Get green component of a 15-bit color
private final int green(int x) {
return (x>>2)&0xf8;
}
// Get blue component of a 15-bit color
private final int blue(int x) {
return (x>>7)&0xf8;
}
/** Uses Heckbert's median-cut algorithm to divide the color space defined by
"hist" into "maxcubes" cubes. The centroids (average value) of each cube
are are used to create a color table. "hist" is then updated to function
as an inverse color map that is used to generate an 8-bit image. */
public Image convert(int maxcubes) {
ImageProcessor ip = convertToByte(maxcubes);
return ip.createImage();
}
/** This is a version of convert that returns a ByteProcessor. */
public ImageProcessor convertToByte(int maxcubes) {
int lr, lg, lb;
int i, median, color;
int count;
int k, level, ncubes, splitpos;
int num, width;
int longdim=0; //longest dimension of cube
Cube cube, cubeA, cubeB;
// Create initial cube
IJ.showStatus("Median cut");
list = new Cube[MAXCOLORS];
histPtr = new int[HSIZE];
ncubes = 0;
cube = new Cube();
for (i=0,color=0; i<=HSIZE-1; i++) {
if (hist[i] != 0) {
histPtr[color++] = i;
cube.count = cube.count + hist[i];
}
}
cube.lower = 0; cube.upper = color-1;
cube.level = 0;
Shrink(cube);
list[ncubes++] = cube;
//Main loop
while (ncubes < maxcubes) {
// Search the list of cubes for next cube to split, the lowest level cube
level = 255; splitpos = -1;
for (k=0; k<=ncubes-1; k++) {
if (list[k].lower == list[k].upper)
; // single color; cannot be split
else if (list[k].level < level) {
level = list[k].level;
splitpos = k;
}
}
if (splitpos == -1) // no more cubes to split
break;
// Find longest dimension of this cube
cube = list[splitpos];
lr = cube.rmax - cube.rmin;
lg = cube.gmax - cube.gmin;
lb = cube.bmax - cube.bmin;
if (lr >= lg && lr >= lb) longdim = 0;
if (lg >= lr && lg >= lb) longdim = 1;
if (lb >= lr && lb >= lg) longdim = 2;
// Sort along "longdim"
reorderColors(histPtr, cube.lower, cube.upper, longdim);
quickSort(histPtr, cube.lower, cube.upper);
restoreColorOrder(histPtr, cube.lower, cube.upper, longdim);
// Find median
count = 0;
for (i=cube.lower;i<=cube.upper-1;i++) {
if (count >= cube.count/2) break;
color = histPtr[i];
count = count + hist[color];
}
median = i;
// Now split "cube" at the median and add the two new
// cubes to the list of cubes.
cubeA = new Cube();
cubeA.lower = cube.lower;
cubeA.upper = median-1;
cubeA.count = count;
cubeA.level = cube.level + 1;
Shrink(cubeA);
list[splitpos] = cubeA; // add in old slot
cubeB = new Cube();
cubeB.lower = median;
cubeB.upper = cube.upper;
cubeB.count = cube.count - count;
cubeB.level = cube.level + 1;
Shrink(cubeB);
list[ncubes++] = cubeB; // add in new slot */
if (ncubes%15==0)
IJ.showProgress(0.3 + (0.6*ncubes)/maxcubes);
}
// We have enough cubes, or we have split all we can. Now
// compute the color map, the inverse color map, and return
// an 8-bit image.
IJ.showProgress(0.9);
makeInverseMap(hist, ncubes);
IJ.showProgress(0.95);
return makeImage();
}
void Shrink(Cube cube) {
// Encloses "cube" with a tight-fitting cube by updating the
// (rmin,gmin,bmin) and (rmax,gmax,bmax) members of "cube".
int r, g, b;
int color;
int rmin, rmax, gmin, gmax, bmin, bmax;
rmin = 255; rmax = 0;
gmin = 255; gmax = 0;
bmin = 255; bmax = 0;
for (int i=cube.lower; i<=cube.upper; i++) {
color = histPtr[i];
r = red(color);
g = green(color);
b = blue(color);
if (r > rmax) rmax = r;
if (r < rmin) rmin = r;
if (g > gmax) gmax = g;
if (g < gmin) gmin = g;
if (b > bmax) bmax = b;
if (b < bmin) bmin = b;
}
cube.rmin = rmin; cube.rmax = rmax;
cube.gmin = gmin; cube.gmax = gmax;
cube.bmin = bmin; cube.bmax = bmax;
}
void makeInverseMap(int[] hist, int ncubes) {
// For each cube in the list of cubes, computes the centroid
// (average value) of the colors enclosed by that cube, and
// then loads the centroids in the color map. Next loads
// "hist" with indices into the color map
int r, g, b;
int color;
float rsum, gsum, bsum;
Cube cube;
byte[] rLUT = new byte[256];
byte[] gLUT = new byte[256];
byte[] bLUT = new byte[256];
IJ.showStatus("Making inverse map");
for (int k=0; k<=ncubes-1; k++) {
cube = list[k];
rsum = gsum = bsum = (float)0.0;
for (int i=cube.lower; i<=cube.upper; i++) {
color = histPtr[i];
r = red(color);
rsum += (float)r*(float)hist[color];
g = green(color);
gsum += (float)g*(float)hist[color];
b = blue(color);
bsum += (float)b*(float)hist[color];
}
// Update the color map
r = (int)(rsum/(float)cube.count);
g = (int)(gsum/(float)cube.count);
b = (int)(bsum/(float)cube.count);
if (r==248 && g==248 && b==248)
r=g=b=255; // Restore white (255,255,255)
rLUT[k] = (byte)r;
gLUT[k] = (byte)g;
bLUT[k] = (byte)b;
}
cm = new IndexColorModel(8, ncubes, rLUT, gLUT, bLUT);
// For each color in each cube, load the corre-
// sponding slot in "hist" with the centroid of the cube.
for (int k=0; k<=ncubes-1; k++) {
cube = list[k];
for (int i=cube.lower; i<=cube.upper; i++) {
color = histPtr[i];
hist[color] = k;
}
}
}
void reorderColors(int[] a, int lo, int hi, int longDim) {
// Change the ordering of the 5-bit colors in each word of int[]
// so we can sort on the 'longDim' color
int c, r, g, b;
switch (longDim) {
case 0: //red
for (int i=lo; i<=hi; i++) {
c = a[i];
r = c & 31;
a[i] = (r<<10) | (c>>5);
}
break;
case 1: //green
for (int i=lo; i<=hi; i++) {
c = a[i];
r = c & 31;
g = (c>>5) & 31;
b = c>>10;
a[i] = (g<<10) | (b<<5) | r;
}
break;
case 2: //blue; already in the needed order
break;
}
}
void restoreColorOrder(int[] a, int lo, int hi, int longDim) {
// Restore the 5-bit colors to the original order
int c, r, g, b;
switch (longDim){
case 0: //red
for (int i=lo; i<=hi; i++) {
c = a[i];
r = c >> 10;
a[i] = ((c&1023)<<5) | r;
}
break;
case 1: //green
for (int i=lo; i<=hi; i++) {
c = a[i];
r = c & 31;
g = c>>10;
b = (c>>5) & 31;
a[i] = (b<<10) | (g<<5) | r;
}
break;
case 2: //blue
break;
}
}
void quickSort(int a[], int lo0, int hi0) {
// Based on the QuickSort method by James Gosling from Sun's SortDemo applet
int lo = lo0;
int hi = hi0;
int mid, t;
if ( hi0 > lo0) {
mid = a[ ( lo0 + hi0 ) / 2 ];
while( lo <= hi ) {
while( ( lo < hi0 ) && ( a[lo] < mid ) )
++lo;
while( ( hi > lo0 ) && ( a[hi] > mid ) )
--hi;
if( lo <= hi ) {
t = a[lo];
a[lo] = a[hi];
a[hi] = t;
++lo;
--hi;
}
}
if( lo0 < hi )
quickSort( a, lo0, hi );
if( lo < hi0 )
quickSort( a, lo, hi0 );
}
}
ImageProcessor makeImage() {
// Generate 8-bit image
Image img8;
byte[] pixels8;
int color16;
IJ.showStatus("Creating 8-bit image");
pixels8 = new byte[width*height];
for (int i=0; i<width*height; i++) {
color16 = rgb(pixels32[i]);
pixels8[i] = (byte)hist[color16];
}
ImageProcessor ip = new ByteProcessor(width, height, pixels8, cm);
IJ.showProgress(1.0);
return ip;
}
} //class MedianCut
class Cube { // structure for a cube in color space
int lower; // one corner's index in histogram
int upper; // another corner's index in histogram
int count; // cube's histogram count
int level; // cube's level
int rmin, rmax;
int gmin, gmax;
int bmin, bmax;
Cube() {
count = 0;
}
public String toString() {
String s = "lower=" + lower + " upper=" + upper;
s = s + " count=" + count + " level=" + level;
s = s + " rmin=" + rmin + " rmax=" + rmax;
s = s + " gmin=" + gmin + " gmax=" + gmax;
s = s + " bmin=" + bmin + " bmax=" + bmax;
return s;
}
}
|