2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.image;
41 import gnu.java.awt.Buffers;
43 import java.awt.Point;
44 import java.awt.Transparency;
45 import java.awt.color.ColorSpace;
46 import java.util.Arrays;
49 * A color model operates with colors in several formats:
52 * <li>normalized: component samples are in range [0.0, 1.0].</li>
54 * <li>color model pixel value: all the color component samples for a
55 * sigle pixel packed/encoded in a way natural for the color
58 * <li>color model pixel int value: only makes sense if the natural
59 * encoding of a single pixel can fit in a single int value.</li>
61 * <li>array of transferType containing a single pixel: the pixel is
62 * encoded in the natural way of the color model, taking up as many
63 * array elements as needed.</li>
65 * <li>sRGB pixel int value: a pixel in sRGB color space, encoded in
66 * default 0xAARRGGBB format, assumed not alpha premultiplied.</li>
68 * <li>single [0, 255] scaled int samples from default sRGB color
69 * space. These are always assumed to be alpha non-premultiplied.</li>
71 * <li>arrays of unnormalized component samples of single pixel: these
72 * samples are scaled and multiplied according to the color model, but
73 * is otherwise not packed or encoded. Each element of the array is one
74 * separate component sample. The color model only operate on the
75 * components from one pixel at a time, but using offsets, allows
76 * manipulation of arrays that contain the components of more than one
81 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
82 * @author C. Brian Jones (cbj@gnu.org)
84 public abstract class ColorModel implements Transparency
86 protected int pixel_bits;
87 protected int transferType;
93 boolean isAlphaPremultiplied;
96 * The standard color model for the common sRGB.
98 private static final ColorModel S_RGB_MODEL = new SRGBColorModel();
100 static int[] nArray(int value, int times)
102 int[] array = new int[times];
103 java.util.Arrays.fill(array, value);
107 static byte[] nArray(byte value, int times)
109 byte[] array = new byte[times];
110 java.util.Arrays.fill(array, value);
115 * Constructs the default color model. The default color model
116 * can be obtained by calling <code>getRGBdefault</code> of this
118 * @param bits the number of bits wide used for bit size of pixel values
120 public ColorModel(int bits)
122 this(bits * 4, // total bits, sRGB, four channels
123 nArray(bits, 4), // bits for each channel
124 ColorSpace.getInstance(ColorSpace.CS_sRGB), // sRGB
126 false, // not premultiplied
128 Buffers.smallestAppropriateTransferType(bits * 4));
132 * Constructs a ColorModel that translates pixel values to
133 * color/alpha components.
135 * @exception IllegalArgumentException If the length of the bit array is less
136 * than the number of color or alpha components in this ColorModel, or if the
137 * transparency is not a valid value, or if the sum of the number of bits in
138 * bits is less than 1 or if any of the elements in bits is less than 0.
140 protected ColorModel(int pixel_bits, int[] bits, ColorSpace cspace,
141 boolean hasAlpha, boolean isAlphaPremultiplied,
142 int transparency, int transferType)
145 for (int i = 0; i < bits.length; i++)
148 throw new IllegalArgumentException ();
150 bits_sum |= bits [i];
153 if ((bits.length < cspace.getNumComponents())
155 throw new IllegalArgumentException ();
157 this.pixel_bits = pixel_bits;
159 this.cspace = cspace;
160 this.hasAlpha = hasAlpha;
161 this.isAlphaPremultiplied = isAlphaPremultiplied;
162 this.transparency = transparency;
163 this.transferType = transferType;
166 public void finalize()
172 * Returns the default color model which in Sun's case is an instance
173 * of <code>DirectColorModel</code>.
175 public static ColorModel getRGBdefault()
180 public final boolean hasAlpha()
185 public final boolean isAlphaPremultiplied()
187 return isAlphaPremultiplied;
191 * Get get number of bits wide used for the bit size of pixel values
193 public int getPixelSize()
198 public int getComponentSize(int componentIdx)
200 return bits[componentIdx];
203 public int[] getComponentSize()
208 public int getTransparency()
213 public int getNumComponents()
215 return getNumColorComponents() + (hasAlpha ? 1 : 0);
218 public int getNumColorComponents()
220 return cspace.getNumComponents();
224 * Converts pixel value to sRGB and extract red int sample scaled
227 * @param pixel pixel value that will be interpreted according to
228 * the color model, (assumed alpha premultiplied if color model says
231 * @return red sample scaled to range [0, 255], from default color
232 * space sRGB, alpha non-premultiplied.
234 public abstract int getRed(int pixel);
237 * Converts pixel value to sRGB and extract green int sample
238 * scaled to range [0, 255].
242 public abstract int getGreen(int pixel);
245 * Converts pixel value to sRGB and extract blue int sample
246 * scaled to range [0, 255].
250 public abstract int getBlue(int pixel);
253 * Extract alpha int sample from pixel value, scaled to [0, 255].
255 * @param pixel pixel value that will be interpreted according to
258 * @return alpha sample, scaled to range [0, 255].
260 public abstract int getAlpha(int pixel);
263 * Converts a pixel int value of the color space of the color
264 * model to a sRGB pixel int value.
266 * This method is typically overriden in subclasses to provide a
267 * more efficient implementation.
269 * @param pixel pixel value that will be interpreted according to
272 * @return a pixel in sRGB color space, encoded in default
273 * 0xAARRGGBB format. */
274 public int getRGB(int pixel)
277 ((getAlpha(pixel) & 0xff) << 24) |
278 (( getRed(pixel) & 0xff) << 16) |
279 ((getGreen(pixel) & 0xff) << 8) |
280 (( getBlue(pixel) & 0xff) << 0);
285 * In this color model we know that the whole pixel value will
286 * always be contained within the first element of the pixel
289 final int getPixelFromArray(Object inData) {
291 Buffers.createBufferFromData(transferType, inData, 1);
292 Object da = Buffers.getData(data);
294 return data.getElem(0);
298 * Converts pixel in the given array to sRGB and extract blue int
299 * sample scaled to range [0-255].
301 * This method is typically overriden in subclasses to provide a
302 * more efficient implementation.
304 * @param inData array of transferType containing a single pixel. The
305 * pixel should be encoded in the natural way of the color model.
307 public int getRed(Object inData)
309 return getRed(getPixelFromArray(inData));
313 * @see #getRed(Object)
315 public int getGreen(Object inData)
317 return getGreen(getPixelFromArray(inData));
321 * @see #getRed(Object)
323 public int getBlue(Object inData) {
324 return getBlue(getPixelFromArray(inData));
328 * @see #getRed(Object)
330 public int getAlpha(Object inData) {
331 return getAlpha(getPixelFromArray(inData));
335 * Converts a pixel in the given array of the color space of the
336 * color model to an sRGB pixel int value.
338 * <p>This method performs the inverse function of
339 * <code>getDataElements(int rgb, Object pixel)</code>.
340 * I.e. <code>(rgb == cm.getRGB(cm.getDataElements(rgb,
343 * @param inData array of transferType containing a single pixel. The
344 * pixel should be encoded in the natural way of the color model.
346 * @return a pixel in sRGB color space, encoded in default
349 * @see #getDataElements(int, Object)
351 public int getRGB(Object inData)
354 ((getAlpha(inData) & 0xff) << 24) |
355 (( getRed(inData) & 0xff) << 16) |
356 ((getGreen(inData) & 0xff) << 8) |
357 (( getBlue(inData) & 0xff) << 0);
361 * Converts an sRGB pixel int value to an array containing a
362 * single pixel of the color space of the color model.
364 * <p>This method performs the inverse function of
365 * <code>getRGB(Object inData)</code>.
367 * Outline of conversion process:
371 * <li>Convert rgb to normalized [0.0, 1.0] sRGB values.</li>
373 * <li>Convert to color space components using fromRGB in
376 * <li>If color model has alpha and should be premultiplied,
377 * multiply color space components with alpha value</li>
379 * <li>Scale the components to the correct number of bits.</li>
381 * <li>Arrange the components in the output array</li>
385 * @param rgb The color to be converted to dataElements. A pixel
386 * in sRGB color space, encoded in default 0xAARRGGBB format,
387 * assumed not alpha premultiplied.
389 * @param pixel to avoid needless creation of arrays, an array to
390 * use to return the pixel can be given. If null, a suitable array
393 * @return An array of transferType values representing the color,
394 * in the color model format. The color model defines whether the
396 * @see #getRGB(Object)
398 public Object getDataElements(int rgb, Object pixel)
400 // subclasses has to implement this method.
401 throw new UnsupportedOperationException();
405 * Fills an array with the unnormalized component samples from a
406 * pixel value. I.e. decompose the pixel, but not perform any
409 * This method is typically overriden in subclasses to provide a
410 * more efficient implementation.
412 * @param pixel pixel value encoded according to the color model.
414 * @return arrays of unnormalized component samples of single
415 * pixel. The scale and multiplication state of the samples are
416 * according to the color model. Each component sample is stored
417 * as a separate element in the array.
419 public int[] getComponents(int pixel, int[] components, int offset)
421 // subclasses has to implement this method.
422 throw new UnsupportedOperationException();
426 * Fills an array with the unnormalized component samples from an
427 * array of transferType containing a single pixel. I.e. decompose
428 * the pixel, but not perform any color conversion.
430 * This method is typically overriden in subclasses to provide a
431 * more efficient implementation.
433 * @param pixel an array of transferType containing a single pixel. The
434 * pixel should be encoded in the natural way of the color model. If
435 * this argument is not an array, as expected, a {@link ClassCastException}
437 * @param components an array that will be filled with the color component
438 * of the pixel. If this is null, a new array will be allocated
439 * @param offset index into the components array at which the result
442 * @return arrays of unnormalized component samples of single
443 * pixel. The scale and multiplication state of the samples are
444 * according to the color model. Each component sample is stored
445 * as a separate element in the array.
447 public int[] getComponents(Object pixel, int[] components, int offset)
449 // subclasses has to implement this method.
450 throw new UnsupportedOperationException();
454 * Convert normalized components to unnormalized components.
456 public int[] getUnnormalizedComponents(float[] normComponents,
461 int numComponents = getNumComponents();
462 if (components == null)
464 components = new int[offset + numComponents];
467 for (int i=0; i<numComponents; i++)
469 float in = normComponents[normOffset++];
470 int out = (int) (in * ((1<<getComponentSize(i)) - 1));
471 components[offset++] = out;
477 * Convert unnormalized components to normalized components.
479 public float[] getNormalizedComponents(int[] components,
481 float[] normComponents,
484 int numComponents = getNumComponents();
485 if (normComponents == null)
487 normComponents = new float[normOffset + numComponents];
490 for (int i=0; i<numComponents; i++)
492 float in = components[offset++];
493 float out = in / ((1<<getComponentSize(i)) - 1);
494 normComponents[normOffset++] = out;
496 return normComponents;
500 * Convert unnormalized components to normalized components.
504 public float[] getNormalizedComponents (Object pixel,
505 float[] normComponents,
508 int[] components = getComponents(pixel, null, 0);
509 return getNormalizedComponents(components, 0, normComponents, normOffset);
513 * Converts the unnormalized component samples from an array to a
514 * pixel value. I.e. composes the pixel from component samples, but
515 * does not perform any color conversion or scaling of the samples.
517 * This method performs the inverse function of
518 * <code>getComponents(int pixel, int[] components,
519 * int offset)</code>. I.e.
521 * <code>(pixel == cm.getDataElement(cm.getComponents(pixel, null,
524 * This method is overriden in subclasses since this abstract class throws
525 * UnsupportedOperationException().
527 * @param components Array of unnormalized component samples of single
528 * pixel. The scale and multiplication state of the samples are according
529 * to the color model. Each component sample is stored as a separate element
531 * @param offset Position of the first value of the pixel in components.
533 * @return pixel value encoded according to the color model.
535 public int getDataElement(int[] components, int offset)
537 // subclasses have to implement this method.
538 throw new UnsupportedOperationException();
542 * Converts the normalized component samples from an array to a pixel
543 * value. I.e. composes the pixel from component samples, but does not
544 * perform any color conversion or scaling of the samples.
546 * This method is typically overriden in subclasses to provide a
547 * more efficient implementation. The method provided by this abstract
548 * class converts the components to unnormalized form and returns
549 * getDataElement(int[], int).
551 * @param components Array of normalized component samples of single pixel.
552 * The scale and multiplication state of the samples are according to the
553 * color model. Each component sample is stored as a separate element in the
555 * @param offset Position of the first value of the pixel in components.
557 * @return pixel value encoded according to the color model.
560 public int getDataElement (float[] components, int offset)
563 getDataElement(getUnnormalizedComponents(components, offset, null, 0),
567 public Object getDataElements(int[] components, int offset, Object obj)
569 // subclasses have to implement this method.
570 throw new UnsupportedOperationException();
574 * Converts the normalized component samples from an array to an array of
575 * TransferType values. I.e. composes the pixel from component samples, but
576 * does not perform any color conversion or scaling of the samples.
578 * If obj is null, a new array of TransferType is allocated and returned.
579 * Otherwise the results are stored in obj and obj is returned. If obj is
580 * not long enough, ArrayIndexOutOfBounds is thrown. If obj is not an array
581 * of primitives, ClassCastException is thrown.
583 * This method is typically overriden in subclasses to provide a
584 * more efficient implementation. The method provided by this abstract
585 * class converts the components to unnormalized form and returns
586 * getDataElement(int[], int, Object).
588 * @param components Array of normalized component samples of single pixel.
589 * The scale and multiplication state of the samples are according to the
590 * color model. Each component sample is stored as a separate element in the
592 * @param offset Position of the first value of the pixel in components.
593 * @param obj Array of TransferType or null.
595 * @return pixel value encoded according to the color model.
596 * @throws ArrayIndexOutOfBoundsException
597 * @throws ClassCastException
600 public Object getDataElements(float[] components, int offset, Object obj)
603 getDataElements(getUnnormalizedComponents(components, offset, null, 0),
607 public boolean equals(Object obj)
609 if (!(obj instanceof ColorModel)) return false;
611 ColorModel o = (ColorModel) obj;
613 (pixel_bits == o.pixel_bits) &&
614 (transferType == o.transferType) &&
615 (transparency == o.transparency) &&
616 (hasAlpha == o.hasAlpha) &&
617 (isAlphaPremultiplied == o.isAlphaPremultiplied) &&
618 Arrays.equals(bits, o.bits) &&
619 (cspace.equals(o.cspace));
622 public final ColorSpace getColorSpace()
627 public ColorModel coerceData(WritableRaster raster,
628 boolean isAlphaPremultiplied)
630 // This method should always be overridden, but is not abstract.
631 throw new UnsupportedOperationException();
634 void coerceDataWorker(WritableRaster raster,
635 boolean isAlphaPremultiplied)
637 int w = raster.getWidth();
638 int h = raster.getHeight();
639 int x = raster.getMinX();
640 int y = raster.getMinY();
642 int numColors = getNumColorComponents();
643 int numComponents = getNumComponents();
644 int alphaScale = (1 << getComponentSize(numColors)) - 1;
645 double[] pixels = raster.getPixels(x, y, w, h, (double[]) null);
647 for (int i = 0; i < size; i++)
649 double alpha = pixels[i * numComponents + numColors] / alphaScale;
650 for (int c = 0; c < numColors; c++)
652 int offset = i * numComponents + c;
653 if (isAlphaPremultiplied)
654 pixels[offset] = Math.round(pixels[offset] * alpha);
656 pixels[offset] = Math.round(pixels[offset] / alpha);
660 raster.setPixels(0, 0, w, h, pixels);
664 * Checks if the given raster has a compatible data-layout (SampleModel).
665 * @param raster The Raster to test.
666 * @return true if raster is compatible.
668 public boolean isCompatibleRaster(Raster raster)
670 SampleModel sampleModel = raster.getSampleModel();
671 return isCompatibleSampleModel(sampleModel);
674 // Typically overridden
675 public WritableRaster createCompatibleWritableRaster(int w, int h)
677 return new WritableRaster(createCompatibleSampleModel(w, h),
681 // Typically overridden
682 public SampleModel createCompatibleSampleModel(int w, int h)
684 throw new UnsupportedOperationException();
687 // Typically overridden
688 public boolean isCompatibleSampleModel(SampleModel sm)
690 return sm.getTransferType() == transferType;
693 public final int getTransferType ()
699 * Subclasses must override this method if it is possible for the
700 * color model to have an alpha channel.
702 * @return null, as per JDK 1.3 doc. Subclasses will only return
703 * null if no alpha raster exists.
705 public WritableRaster getAlphaRaster(WritableRaster raster)
709 /* It is a mystery to me why we couldn't use the following code...
712 if (!hasAlpha()) return null;
714 SampleModel sm = raster.getSampleModel();
715 int[] alphaBand = { sm.getNumBands() - 1 };
716 SampleModel alphaModel = sm.createSubsetSampleModel(alphaBand);
717 DataBuffer buffer = raster.getDataBuffer();
718 Point origin = new Point(0, 0);
719 return Raster.createWritableRaster(alphaModel, buffer, origin);
722 ...here, and avoided overriding the method in subclasses,
723 but the Sun docs state that this method always will return
724 null, and that overriding is required. Oh, well.
730 return "pixel_bits=" + pixel_bits +
731 ", cspace=" + cspace +
732 ", transferType=" + transferType +
733 ", transparency=" + transparency +
734 ", hasAlpha=" + hasAlpha +
735 ", isAlphaPremultiplied=" + isAlphaPremultiplied;
738 public String toString()
740 return getClass().getName() + "[" + stringParam() + "]";
744 * A color model optimized for standard sRGB.
746 private static class SRGBColorModel
747 extends DirectColorModel
752 super(32,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
755 public int getAlpha(Object inData)
757 return ((((int[]) inData)[0]) >> 24) & 0xFF;
760 public int getBlue(Object inData)
762 return ((((int[]) inData)[0])) & 0xFF;
765 public int getGreen(Object inData)
767 return ((((int[]) inData)[0]) >> 8) & 0xFF;
770 public int getRed(Object inData)
772 return ((((int[]) inData)[0]) >> 16) & 0xFF;
775 public int getRGB(Object inData)
777 return ((int[]) inData)[0];
780 public Object getDataElements(int rgb, Object pixel)
784 pixel = new int[]{rgb};
788 ((int[]) pixel)[0] = rgb;