File: OctTreeQuantizer.java

package info (click to toggle)
libjhlabs-filters-java 2.0.235-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,132 kB
  • sloc: java: 16,740; xml: 17; sh: 11; makefile: 5
file content (273 lines) | stat: -rw-r--r-- 6,641 bytes parent folder | download | duplicates (3)
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
/*
Copyright 2006 Jerry Huxtable

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.jhlabs.image;

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;

/**
 * An image Quantizer based on the Octree algorithm. This is a very basic implementation
 * at present and could be much improved by picking the nodes to reduce more carefully 
 * (i.e. not completely at random) when I get the time.
 */
public class OctTreeQuantizer implements Quantizer {

	/**
	 * The greatest depth the tree is allowed to reach
	 */
	final static int MAX_LEVEL = 5;

	/**
	 * An Octtree node.
	 */
	class OctTreeNode {
		int children;
		int level;
		OctTreeNode parent;
		OctTreeNode leaf[] = new OctTreeNode[8];
		boolean isLeaf;
		int count;
		int	totalRed;
		int	totalGreen;
		int	totalBlue;
		int index;
		
		/**
		 * A debugging method which prints the tree out.
		 */
		public void list(PrintStream s, int level) {
			for (int i = 0; i < level; i++)
				System.out.print(' ');
			if (count == 0)
				System.out.println(index+": count="+count);
			else
				System.out.println(index+": count="+count+" red="+(totalRed/count)+" green="+(totalGreen/count)+" blue="+(totalBlue/count));
			for (int i = 0; i < 8; i++)
				if (leaf[i] != null)
					leaf[i].list(s, level+2);
		}
	}

	private int nodes = 0;
	private OctTreeNode root;
	private int reduceColors;
	private int maximumColors;
	private int colors = 0;
	private Vector[] colorList;
	
	public OctTreeQuantizer() {
		setup(256);
		colorList = new Vector[MAX_LEVEL+1];
		for (int i = 0; i < MAX_LEVEL+1; i++)
			colorList[i] = new Vector();
		root = new OctTreeNode();
	}

	/**
	 * Initialize the quantizer. This should be called before adding any pixels.
	 * @param numColors the number of colors we're quantizing to.
	 */
	public void setup(int numColors) {
		maximumColors = numColors;
		reduceColors = Math.max(512, numColors * 2);
	}
	
	/**
	 * Add pixels to the quantizer.
	 * @param pixels the array of ARGB pixels
	 * @param offset the offset into the array
	 * @param count the count of pixels
	 */
	public void addPixels(int[] pixels, int offset, int count) {
		for (int i = 0; i < count; i++) {
			insertColor(pixels[i+offset]);
			if (colors > reduceColors)
				reduceTree(reduceColors);
		}
	}	

	public int getIndexForColor(int rgb) {
		int red = (rgb >> 16) & 0xff;
		int green = (rgb >> 8) & 0xff;
		int blue = rgb & 0xff;

		OctTreeNode node = root;

		for (int level = 0; level <= MAX_LEVEL; level++) {
			OctTreeNode child;
			int bit = 0x80 >> level;

			int index = 0;
			if ((red & bit) != 0)
				index += 4;
			if ((green & bit) != 0)
				index += 2;
			if ((blue & bit) != 0)
				index += 1;

			child = node.leaf[index];

			if (child == null)
				return node.index;
			else if (child.isLeaf)
				return child.index;
			else
				node = child;
		}
		System.out.println("getIndexForColor failed");
		return 0;
	}

	private void insertColor(int rgb) {
		int red = (rgb >> 16) & 0xff;
		int green = (rgb >> 8) & 0xff;
		int blue = rgb & 0xff;

		OctTreeNode node = root;

//		System.out.println("insertColor="+Integer.toHexString(rgb));
		for (int level = 0; level <= MAX_LEVEL; level++) {
			OctTreeNode child;
			int bit = 0x80 >> level;

			int index = 0;
			if ((red & bit) != 0)
				index += 4;
			if ((green & bit) != 0)
				index += 2;
			if ((blue & bit) != 0)
				index += 1;

			child = node.leaf[index];

			if (child == null) {
				node.children++;

				child = new OctTreeNode();
				child.parent = node;
				node.leaf[index] = child;
				node.isLeaf = false;
				nodes++;
				colorList[level].addElement(child);

				if (level == MAX_LEVEL) {
					child.isLeaf = true;
					child.count = 1;
					child.totalRed = red;
					child.totalGreen = green;
					child.totalBlue = blue;
					child.level = level;
					colors++;
					return;
				}

				node = child;
			} else if (child.isLeaf) {
				child.count++;
				child.totalRed += red;
				child.totalGreen += green;
				child.totalBlue += blue;
				return;
			} else
				node = child;
		}
		System.out.println("insertColor failed");
	}

	private void reduceTree(int numColors) {
		for (int level = MAX_LEVEL-1; level >= 0; level--) {
			Vector v = colorList[level];
			if (v != null && v.size() > 0) {
				for (int j = 0; j < v.size(); j++) {
					OctTreeNode node = (OctTreeNode)v.elementAt(j);
					if (node.children > 0) {
						for (int i = 0; i < 8; i++) {
							OctTreeNode child = node.leaf[i];
							if (child != null) {
								if (!child.isLeaf)
									System.out.println("not a leaf!");
								node.count += child.count;
								node.totalRed += child.totalRed;
								node.totalGreen += child.totalGreen;
								node.totalBlue += child.totalBlue;
								node.leaf[i] = null;
								node.children--;
								colors--;
								nodes--;
								colorList[level+1].removeElement(child);
							}
						}
						node.isLeaf = true;
						colors++;
						if (colors <= numColors)
							return;
					}
				}
			}
		}

		System.out.println("Unable to reduce the OctTree");
	}

	public int[] buildColorTable() {
		int[] table = new int[colors];
		buildColorTable(root, table, 0);
		return table;
	}
	
	/**
	 * A quick way to use the quantizer. Just create a table the right size and pass in the pixels.
	 */
	public void buildColorTable(int[] inPixels, int[] table) {
		int count = inPixels.length;
		maximumColors = table.length;
		for (int i = 0; i < count; i++) {
			insertColor(inPixels[i]);
			if (colors > reduceColors)
				reduceTree(reduceColors);
		}
		if (colors > maximumColors)
			reduceTree(maximumColors);
		buildColorTable(root, table, 0);
	}

	private int buildColorTable(OctTreeNode node, int[] table, int index) {
		if (colors > maximumColors)
			reduceTree(maximumColors);

		if (node.isLeaf) {
			int count = node.count;
			table[index] = 0xff000000 |
				((node.totalRed/count) << 16) |
				((node.totalGreen/count) << 8) |
				node.totalBlue/count;
			node.index = index++;
		} else {
			for (int i = 0; i < 8; i++) {
				if (node.leaf[i] != null) {
					node.index = index;
					index = buildColorTable(node.leaf[i], table, index);
				}
			}
		}
		return index;
	}
	
}