File: Palette.java

package info (click to toggle)
java-imaging-utilities 0.14.3-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,556 kB
  • sloc: java: 31,233; python: 71; xml: 31; makefile: 26; sh: 5
file content (239 lines) | stat: -rw-r--r-- 6,478 bytes parent folder | download | duplicates (5)
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
/*
 * Palette
 *
 * Copyright (c) 2000, 2001, 2002, 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.data;

import net.sourceforge.jiu.data.RGBIndex;

/**
 * This class represents a palette, a list of RGB colors.
 * An RGB color here has three int values for its red, green and blue
 * intensity.
 * Each intensity value must be larger than or equal to zero and
 * smaller than or equal to the maximum intensity value that can be
 * given to the constructor {@link #Palette(int, int)}.
 * This maximum value is typically 255.
 * Note that the number of entries in a palette is restricted only
 * by the element index type <code>int</code> so that palettes with
 * more than 256 entries are no problem.
 * When accessing (reading or writing) samples of this palette, use
 * the constants {@link #INDEX_RED}, {@link #INDEX_GREEN} and {@link #INDEX_BLUE} of
 * this class to define a color channel.
 * @author Marco Schmidt
 * @see net.sourceforge.jiu.data.PalettedImage
 */
public class Palette implements RGBIndex
{
	private int[][] data;
	private int numEntries;
	private int maxValue;

	/**
	 * Create a palette with the given number of entries and a maximum value
	 * for each sample.
	 * @param numEntries the number of entries to be accessible in this palette
	 * @param maxValue the maximum value to be allowed for each sample
	 */
	public Palette(int numEntries, int maxValue)
	{
		if (numEntries < 1)
		{
			throw new IllegalArgumentException("Error -- numEntries must be larger than 0.");
		}
		this.numEntries = numEntries;
		this.maxValue = maxValue;
		data = new int[3][];
		for (int i = 0; i < 3; i++)
		{
			data[i] = new int[numEntries];
		}
	}

	/**
	 * Create a palette with the given number of entries and a maximum value
	 * of <code>255</code>.
	 * @param numEntries the number of entries to be accessible in this palette
	 */
	public Palette(int numEntries)
	{
		this(numEntries, 255);
	}

	/**
	 * Creates a copy of this palette, allocating a new Palette object
	 * and copying each RGB triplet to the new palette.
	 * Then returns the new palette.
	 * Thus, a &quot;deep&quot; copy of this Palette object is created,
	 * not a &quot;shallow&quot; one.
	 *
	 * @return newly-created palette
	 */
	public Object clone()
	{
		Palette result = new Palette(getNumEntries(), getMaxValue());
		for (int i = 0; i < getNumEntries(); i++)
		{
			result.putSample(INDEX_RED, i, getSample(INDEX_RED, i));
			result.putSample(INDEX_GREEN, i, getSample(INDEX_GREEN, i));
			result.putSample(INDEX_BLUE, i, getSample(INDEX_BLUE, i));
		}
		return result;
	}

	/**
	 * Returns the amount of memory in bytes allocated for this palette.
	 *
	 */
	public long getAllocatedMemory()
	{
		long result = 0;
		if (data != null)
		{
			for (int i = 0; i < data.length; i++)
			{
				if (data[i] != null)
				{
					result += data[i].length;
				}
			}
		}
		return result;
	}

	/**
	 * Returns the maximum value allowed for a sample.
	 * @return the maximum sample value
	 */
	public int getMaxValue()
	{
		return maxValue;
	}

	/**
	 * Returns the number of entries in this palette.
	 * @return the number of entries in this palette
	 */
	public int getNumEntries()
	{
		return numEntries;
	}

	/**
	 * Returns one of the samples of this palette.
	 * @param entryIndex the index of the color to be addressed, must be from
	 *  <code>0</code> to <code>getNumEntries() - 1</code>
	 * @param channelIndex one of the three channels; must be {@link #INDEX_RED},
	 *  {@link #INDEX_GREEN} or {@link #INDEX_BLUE}
	 * @return the requested sample
	 */
	public int getSample(int channelIndex, int entryIndex)
	{
		try
		{
			return data[channelIndex][entryIndex];
		}
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			throw new IllegalArgumentException("Entry must be from 0 to " + (numEntries - 1) +
				", channel from 0 to 2.");
		}
	}

	/**
	 * Returns all samples of one channel as an int array.
	 * @param channelIndex index of the channel, one of the {@link RGBIndex} constants
	 * @return array with samples
	 */
	public int[] getSamples(int channelIndex)
	{
		if (channelIndex < 0 || channelIndex > 2)
		{
			throw new IllegalArgumentException("Invalid channel index, must be from 0 to 2.");
		}
		int[] result = new int[data[channelIndex].length];
		System.arraycopy(data[channelIndex], 0, result, 0, result.length);
		return result;
	}

	/**
	 * Checks if all entries of this palette are either black or white.
	 * An entry is black if all three intensitites (red, green and blue) are
	 * <code>0</code>, it is white if they are all equal to 
	 * {@link #getMaxValue()}.
	 * No particular order of entries (e.g. first color black, second white)
	 * is demanded and no specific number of entries (e.g. 2).
	 * This means that a palette is black and white if it contains ten entries
	 * that are all black.
	 *
	 * @return if the palette contains only the colors black and white
	 */
	public boolean isBlackAndWhite()
	{
		int i = 0;
		while (i < numEntries)
		{
			if (data[INDEX_RED][i] != data[INDEX_GREEN][i] ||
			    data[INDEX_GREEN][i] != data[INDEX_BLUE][i] ||
			    (data[INDEX_BLUE][i] != 0 && data[INDEX_BLUE][i] != maxValue))
			{
				return false;
			}
			i++;
		}
		return true;
	}

	/**
	 * Checks if this palette is gray, i.e., checks if all entries are
	 * gray. This is the case if for all entries red, green and blue
	 * have the same intensity.
	 * @return if the palette contains only shades of gray
	 */
	public boolean isGray()
	{
		int i = 0;
		while (i < numEntries)
		{
			if (data[INDEX_RED][i] != data[INDEX_GREEN][i] ||
			    data[INDEX_GREEN][i] != data[INDEX_BLUE][i])
			{
				return false;
			}
			i++;
		}
		return true;
	}

	public void put(int entryIndex, int red, int green, int blue)
	{
		putSample(INDEX_RED, entryIndex, red);
		putSample(INDEX_GREEN, entryIndex, green);
		putSample(INDEX_BLUE, entryIndex, blue);
	}

	/**
	 * Sets one sample of one color entry in the palette to a new value.
	 * @param channelIndex 
	 */
	public void putSample(int channelIndex, int entryIndex, int newValue)
	{
		if (newValue < 0 || newValue > maxValue)
		{
			throw new IllegalArgumentException("Value must be from 0 to " +
				maxValue + "; argument is " + newValue + ".");
		}
		try
		{
			data[channelIndex][entryIndex] = newValue;
		}
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			throw new IllegalArgumentException("Entry must be from 0 to " + (numEntries - 1) +
				", channel from 0 to 2.");
		}
	}
}