summaryrefslogtreecommitdiff
path: root/libjava/java/awt/image/IndexColorModel.java
blob: 896d97f9372c44cf9e5b95737f0703e567d14b3f (plain)
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
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.awt.image;

import java.awt.Transparency;
import java.awt.color.ColorSpace;
import gnu.gcj.awt.Buffers;

/**
 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
 */
public class IndexColorModel extends ColorModel
{
  private byte[] r;
  private byte[] g;
  private byte[] b;
  private byte[] a;
  private int[] argb;
  private byte[] cmap;
  private int start;
  private int transparent;
  private int size;
  
  public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b)
  {
    super(bits, nArray(bits, 3),
	  ColorSpace.getInstance(ColorSpace.CS_sRGB),
	  false,  // no transparency
	  false,  // no premultiplied
	  Transparency.OPAQUE,
	  Buffers.smallestAppropriateTransferType(bits));
    this.r = r;
    this.g = g;
    this.b = b;
    this.size = size;
  }

  public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
			 int transparent)
  {
    super(bits, nArray(bits, 4), 
	  ColorSpace.getInstance(ColorSpace.CS_sRGB),
	  true,  // has transparency
	  false,
	  Transparency.BITMASK,
	  Buffers.smallestAppropriateTransferType(bits));
    this.r = r;
    this.g = g;
    this.b = b;
    this.transparent = transparent;
    this.size = size;
  }

  public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
			 byte[] a)
  {
    super(bits, nArray(bits, 4),
	  ColorSpace.getInstance(ColorSpace.CS_sRGB),
	  true,  // has transparency
	  false,
	  Transparency.BITMASK,
	  Buffers.smallestAppropriateTransferType(bits));
    this.r = r;
    this.g = g;
    this.b = b;
    this.a = a;
    this.size = size;
  }

  public IndexColorModel(int bits, int size, byte[] cmap, int start,
			 boolean hasAlpha)
  {
    super(bits, nArray(bits, hasAlpha ? 4 : 3),
	  ColorSpace.getInstance(ColorSpace.CS_sRGB),
	  hasAlpha,
	  false,
	  hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE,
	  Buffers.smallestAppropriateTransferType(bits));
    this.cmap = cmap;
    this.start = start;
    this.size = size;
  }

  public IndexColorModel(int bits, int size, byte[] cmap, int start,
			 boolean hasAlpha, int transparent,
			 int transferType)
  {
    super(bits, nArray(bits, hasAlpha ? 4 : 3),
	  ColorSpace.getInstance(ColorSpace.CS_sRGB),
	  hasAlpha,
	  false,
	  hasAlpha ? 
	  Transparency.TRANSLUCENT :
	  ((transparent < 0) ?
	   Transparency.OPAQUE :
	   Transparency.BITMASK),
	  transferType);
    this.cmap = cmap;
    this.start = start;
    this.size = size;
  }

  public final int getMapSize()
  {
    return size;
  }
  
  public final int getTransparentPixel()
  {
    return transparent;
  }

  public final void getReds(byte r[])
  {
    if (this.r == null) calcRGBArrays();
    System.arraycopy(this.r, 0, r, 0, getMapSize());
  }
  
  public final void getGreens(byte g[])
  {
    if (this.g == null) calcRGBArrays();
    System.arraycopy(this.g, 0, g, 0, getMapSize());
  }
  
  public final void getBlues(byte b[])
  {
    if (this.b == null) calcRGBArrays();
    System.arraycopy(this.b, 0, b, 0, getMapSize());
  }

  public final void getAlphas(byte a[])
  {
    if (this.a == null) calcAlphaArray();
    System.arraycopy(this.a, 0, a, 0, getMapSize());
  }

  public final void getRGBs(int rgb[])
  {
    if (this.argb == null) calcARGBArray();
    System.arraycopy(this.argb, 0, rgb, 0, getMapSize());
  }

  public int getRed(int pixel)
  {
    try
      {
	return r[pixel];
      }
    catch (NullPointerException npe)
      {
	calcRGBArrays();
	return r[pixel];
      }
  }

  public int getGreen(int pixel)
  {
    try
      {
	return g[pixel];
      }
    catch (NullPointerException npe)
      {
	calcRGBArrays();
	return g[pixel];
      }
  }

  public int getBlue(int pixel)
  {
    try
      {
	return b[pixel];
      }
    catch (NullPointerException npe)
      {
	calcRGBArrays();
	return b[pixel];
      }
  }
  
  public int getAlpha(int pixel)
  {
    try
      {
	return a[pixel];
      } 
    catch (NullPointerException npe)
      {
	calcAlphaArray();
	return a[pixel];
      }
  }

  private void calcRGBArrays() {
    int j=0;
    boolean hasAlpha = hasAlpha();
    r = new byte[size];
    g = new byte[size];
    b = new byte[size];
    if (hasAlpha) a = new byte[size];
    
    for (int i=0; i<size; i++)
      {
	r[i] = cmap[j++];
	g[i] = cmap[j++];
	b[i] = cmap[j++];
	if (hasAlpha()) a[i] = cmap[j++];
      }
  }

  private void calcAlphaArray()
  {
    int transparency = getTransparency();
    switch (transparency)
      {
      case Transparency.OPAQUE:
      case Transparency.BITMASK:
	a = nArray((byte) 255, size);
	if (transparency == Transparency.BITMASK)
	  a[transparent] = 0;
	break;
      case Transparency.TRANSLUCENT:
	calcRGBArrays();
      }
  }

  private void calcARGBArray()
  {
    int mapSize = getMapSize();
    argb = new int[mapSize];
    for (int p=0; p<mapSize; p++) argb[p] = getRGB(p);
  }
  
  public int getRed(Object inData)
  {
    return getRed(getPixelFromArray(inData));
  }

  public int getGreen(Object inData)
  {
    return getGreen(getPixelFromArray(inData));
  }

  public int getBlue(Object inData)
  {
    return getBlue(getPixelFromArray(inData));
  }
    
  public int getAlpha(Object inData)
  {
    return getAlpha(getPixelFromArray(inData));
  }

  public int getRGB(Object inData)
  {
    return getRGB(getPixelFromArray(inData));
  }

  public Object getDataElements(int rgb, Object pixel)
  {
    int av, rv, gv, bv;
    // using 8 bit values
    av = (rgb >>> 24) & 0xff;
    rv = (rgb >>> 16) & 0xff;
    gv = (rgb >>>  8) & 0xff;
    bv = (rgb >>>  0) & 0xff;
    
    int pixelValue = getPixelValue(av, rv, gv, bv);

    /* In this color model, the whole pixel fits in the first element
       of the array. */
    DataBuffer buffer = Buffers.createBuffer(transferType, pixel, 1);
    buffer.setElem(0, pixelValue);
    return Buffers.getData(buffer);
  }
    
  private int getPixelValue(int av, int rv, int gv, int bv)
  {
    if (r == null) calcRGBArrays();
    if (a == null) calcAlphaArray();
    
    int minDAlpha = 1<<8;
    int minDRGB = (1<<8)*(1<<8)*3;
    int pixelValue = -1;
    for (int i=0; i<size; i++)
      {
	int dAlpha = Math.abs(av-(a[i]&0xff));
	if (dAlpha > minDAlpha) continue;
	int dR = rv-(r[i]&0xff);
	int dG = gv-(g[i]&0xff);
	int dB = bv-(b[i]&0xff);
	int dRGB = dR*dR + dG*dG + dB*dB;
	
	if (dRGB >= minDRGB) continue;
	
	pixelValue = i;
	minDRGB = dRGB;
      }
    return pixelValue;
  }  

  public int[] getComponents(int pixel, int[] components, int offset)
  {
    int numComponents = getNumComponents();
    if (components == null) components = new int[offset + numComponents];
    components[offset++] = (r[pixel]&0xff);
    components[offset++] = (g[pixel]&0xff);
    components[offset++] = (b[pixel]&0xff);
    if (hasAlpha()) components[offset++] = (a[pixel]&0xff);
    return components;
  }
	
  public final int[] getComponents(Object pixel, int[] components,
				   int offset)
  {
    return getComponents(getPixelFromArray(pixel), components, offset);
  }
  
  public int getDataElement(int[] components, int offset)
  {
    int r = components[offset++];
    int g = components[offset++];
    int b = components[offset++];
    int a = hasAlpha() ? components[offset++] : 255;
    
    return getPixelValue(a, r, g, b);
  }
  
  public Object getDataElements(int[] components, int offset, Object pixel)
  {
    int pixelValue = getDataElement(components, offset);
    
    /* In this color model, the whole pixel fits in the first element
       of the array. */
    DataBuffer buffer = Buffers.createBuffer(transferType, pixel, 1);
    buffer.setElem(0, pixelValue);
    return Buffers.getData(buffer);
  }
    
  public SampleModel createCompatibleSampleModel(int w, int h)
  {
    int[] bandOffsets = {0};
    return new ComponentSampleModel(transferType, w, h,
				    1, // pixel stride
				    w, // scanline stride
				    bandOffsets);
  }
}