java.awt.color.ColorSpace Java Examples

The following examples show how to use java.awt.color.ColorSpace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDColorSpace.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the (A)RGB equivalent of the given raster, using the given AWT color space
 * to perform the conversion.
 * @param raster the source raster
 * @param colorSpace the AWT
 * @return an (A)RGB buffered image
 */
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace)
{
    //
    // WARNING: this method is performance sensitive, modify with care!
    //

    // ICC Profile color transforms are only fast when performed using ColorConvertOp
    ColorModel colorModel = new ComponentColorModel(colorSpace,
        false, false, Transparency.OPAQUE, raster.getDataBuffer().getDataType());

    BufferedImage src = new BufferedImage(colorModel, raster, false, null);
    BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(),
                                           BufferedImage.TYPE_INT_RGB);
    ColorConvertOp op = new ColorConvertOp(null);
    op.filter(src, dest);
    return dest;
}
 
Example #2
Source File: D3DGraphicsConfig.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ColorModel getColorModel(int transparency) {
    switch (transparency) {
    case Transparency.OPAQUE:
        // REMIND: once the ColorModel spec is changed, this should be
        //         an opaque premultiplied DCM...
        return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    case Transparency.BITMASK:
        return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
    case Transparency.TRANSLUCENT:
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        return new DirectColorModel(cs, 32,
                                    0xff0000, 0xff00, 0xff, 0xff000000,
                                    true, DataBuffer.TYPE_INT);
    default:
        return null;
    }
}
 
Example #3
Source File: ImageUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns <code>true</code> if the given <code>ColorSpace</code> object is
 * an instance of <code>ICC_ColorSpace</code> but is not one of the standard
 * <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>.
 *
 * @param cs The <code>ColorSpace</code> to test.
 */
public static boolean isNonStandardICCColorSpace(ColorSpace cs) {
    boolean retval = false;

    try {
        // Check the standard ColorSpaces in decreasing order of
        // likelihood except check CS_PYCC last as in some JREs
        // PYCC.pf used not to be installed.
        retval =
            (cs instanceof ICC_ColorSpace) &&
            !(cs.isCS_sRGB() ||
              cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) ||
              cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) ||
              cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) ||
              cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)));
    } catch(IllegalArgumentException e) {
        // PYCC.pf not installed: ignore it - 'retval' is still 'false'.
    }

    return retval;
}
 
Example #4
Source File: JFIFMarkerSegment.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
JFIFExtensionMarkerSegment(BufferedImage thumbnail)
    throws IllegalThumbException {

    super(JPEG.APP0);
    ColorModel cm = thumbnail.getColorModel();
    int csType = cm.getColorSpace().getType();
    if (cm.hasAlpha()) {
        throw new IllegalThumbException();
    }
    if (cm instanceof IndexColorModel) {
        code = THUMB_PALETTE;
        thumb = new JFIFThumbPalette(thumbnail);
    } else if (csType == ColorSpace.TYPE_RGB) {
        code = THUMB_RGB;
        thumb = new JFIFThumbRGB(thumbnail);
    } else if (csType == ColorSpace.TYPE_GRAY) {
        code = THUMB_JPEG;
        thumb = new JFIFThumbJPEG(thumbnail);
    } else {
        throw new IllegalThumbException();
    }
}
 
Example #5
Source File: ComponentColorModel.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static int[] bitsArrayHelper(int[] origBits,
                                     int transferType,
                                     ColorSpace colorSpace,
                                     boolean hasAlpha) {
    switch(transferType) {
        case DataBuffer.TYPE_BYTE:
        case DataBuffer.TYPE_USHORT:
        case DataBuffer.TYPE_INT:
            if (origBits != null) {
                return origBits;
            }
            break;
        default:
            break;
    }
    int numBits = DataBuffer.getDataTypeSize(transferType);
    int numComponents = colorSpace.getNumComponents();
    if (hasAlpha) {
        ++numComponents;
    }
    int[] bits = new int[numComponents];
    for (int i = 0; i < numComponents; i++) {
        bits[i] = numBits;
    }
    return bits;
}
 
Example #6
Source File: ComponentColorModel.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private static int[] bitsArrayHelper(int[] origBits,
                                     int transferType,
                                     ColorSpace colorSpace,
                                     boolean hasAlpha) {
    switch(transferType) {
        case DataBuffer.TYPE_BYTE:
        case DataBuffer.TYPE_USHORT:
        case DataBuffer.TYPE_INT:
            if (origBits != null) {
                return origBits;
            }
            break;
        default:
            break;
    }
    int numBits = DataBuffer.getDataTypeSize(transferType);
    int numComponents = colorSpace.getNumComponents();
    if (hasAlpha) {
        ++numComponents;
    }
    int[] bits = new int[numComponents];
    for (int i = 0; i < numComponents; i++) {
        bits[i] = numBits;
    }
    return bits;
}
 
Example #7
Source File: DataConversionTests.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Context(TestEnvironment env, Result result, ColorSpace cs) {
    this.cs = cs;
    this.env = env;
    this.res = result;

    numComponents = cs.getNumComponents();

    val = new float[numComponents];

    for (int i = 0; i < numComponents; i++) {
        float min = cs.getMinValue(i);
        float max = cs.getMaxValue(i);

        val[i] = 0.5f * (max - min);
    }

    rgb = new float[]{0.5f, 0.5f, 0.5f};
    cie = new float[]{0.5f, 0.5f, 0.5f};
}
 
Example #8
Source File: Color.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a <code>float</code> array containing only the color
 * components of the <code>Color</code> in the
 * <code>ColorSpace</code> specified by the <code>cspace</code>
 * parameter. If <code>compArray</code> is <code>null</code>, an array
 * with length equal to the number of components in
 * <code>cspace</code> is created for the return value.  Otherwise,
 * <code>compArray</code> must have at least this length, and it is
 * filled in with the components and returned.
 * @param cspace a specified <code>ColorSpace</code>
 * @param compArray an array that this method fills with the color
 *          components of this <code>Color</code> in the specified
 *          <code>ColorSpace</code>
 * @return the color components in a <code>float</code> array.
 */
public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
    if (cs == null) {
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    }
    float f[];
    if (fvalue == null) {
        f = new float[3];
        f[0] = ((float)getRed())/255f;
        f[1] = ((float)getGreen())/255f;
        f[2] = ((float)getBlue())/255f;
    } else {
        f = fvalue;
    }
    float tmp[] = cs.toCIEXYZ(f);
    float tmpout[] = cspace.fromCIEXYZ(tmp);
    if (compArray == null) {
        return tmpout;
    }
    for (int i = 0 ; i < tmpout.length ; i++) {
        compArray[i] = tmpout[i];
    }
    return compArray;
}
 
Example #9
Source File: BitmapResource.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * @param buf
 * @return DataImage
 */
protected DataImage get18PlaneImage(MemBuffer buf) {
	// create the color model
	ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
	int[] nBits = { 8, 8, 8 };
	int[] bOffs = { 2, 1, 0 };
	ColorModel colorModel =
		new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE,
			DataBuffer.TYPE_BYTE);
	int w = getWidth();
	int h = getHeight();
	WritableRaster raster =
		Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w * 3, 3, bOffs, null);

	// create the image

	BufferedImage image =
		new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);

	byte[] dbuf = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
	getPixelData(buf, dbuf);
	return new BitmapDataImage(image);
}
 
Example #10
Source File: IncorrectManagedImageSourceOffset.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the custom buffered image, which mostly identical to
 * BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
 * This means that the raster will have gaps, between the rows.
 */
private static BufferedImage makeCustomManagedBI() {
    int w = 511, h = 255;
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
                                                    Transparency.OPAQUE,
                                                    DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
                                           w * 3 + 2, 3, bOffs, null);
    BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
    SunWritableRaster.makeTrackable(raster.getDataBuffer());
    SunWritableRaster.markDirty(bi);
    return bi;
}
 
Example #11
Source File: PageDrawer.java    From sambox with Apache License 2.0 6 votes vote down vote up
private BufferedImage create2ByteGrayAlphaImage(int width, int height)
{
    /**
     * gray + alpha
     */
    int[] bandOffsets = new int[] { 1, 0 };
    int bands = bandOffsets.length;

    /**
     * Color Model usesd for raw GRAY + ALPHA
     */
    final ColorModel CM_GRAY_ALPHA = new ComponentColorModel(
            ColorSpace.getInstance(ColorSpace.CS_GRAY), true, false,
            Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);

    // Init data buffer of type byte
    DataBuffer buffer = new DataBufferByte(width * height * bands);

    // Wrap the data buffer in a raster
    WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height,
            width * bands, bands, bandOffsets, new Point(0, 0));

    // Create a custom BufferedImage with the raster and a suitable color model
    return new BufferedImage(CM_GRAY_ALPHA, raster, false, null);
}
 
Example #12
Source File: JSliderOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Icon getIcon ()
{
    // TODO Use that to get the state (-> highlight or not)
    TransitionAwareUI transitionAwareUI = (TransitionAwareUI) slider.getUI();
    StateTransitionTracker stateTransitionTracker = transitionAwareUI.getTransitionTracker();
    // stateTransitionTracker.getModelStateInfo().getCurrModelState();

    final Icon icon = super.getIcon();
    final BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    final Graphics iconGraphics = image.createGraphics();
    icon.paintIcon(slider, iconGraphics, 0, 0);
    // Make it brighter (very simple approach)
    final RescaleOp rescaleOp = new RescaleOp(2.0f, 50, null);
    rescaleOp.filter(image, image);

    ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    op.filter(image, image);

    return new ImageIcon(image);
}
 
Example #13
Source File: CMMTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected static ColorSpace getColorSpace(TestEnvironment env) {
    ColorSpace cs;
    Boolean usePlatfrom = true; //(Boolean)env.getModifier(usePlatfromProfiles);

    int cs_code = env.getIntValue(csList);
    if (usePlatfrom) {
        cs = ColorSpace.getInstance(cs_code);
    } else {
        String resource = "profiles/";
        switch (cs_code) {
            case ColorSpace.CS_CIEXYZ:
                resource += "CIEXYZ.pf";
                break;
            case ColorSpace.CS_GRAY:
                resource += "GRAY.pf";
                break;
            case ColorSpace.CS_LINEAR_RGB:
                resource += "LINEAR_RGB.pf";
                break;
            case ColorSpace.CS_PYCC:
                resource += "PYCC.pf";
                break;
            case ColorSpace.CS_sRGB:
                resource += "sRGB.pf";
                break;
            default:
                throw new RuntimeException("Unknown color space: " + cs_code);
        }

        try {
            InputStream is = CMMTests.class.getResourceAsStream(resource);
            ICC_Profile p = ICC_Profile.getInstance(is);

            cs = new ICC_ColorSpace(p);
        } catch (IOException e) {
            throw new RuntimeException("Unable load profile from resource " + resource, e);
        }
    }
    return cs;
}
 
Example #14
Source File: X11GraphicsConfig.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static ComponentColorModel createABGRCCM() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8, 8};
    int[] bOffs = {3, 2, 1, 0};
    return new ComponentColorModel(cs, nBits, true, true,
                                   Transparency.TRANSLUCENT,
                                   DataBuffer.TYPE_BYTE);
}
 
Example #15
Source File: Color.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a color in the specified <code>ColorSpace</code>
 * with the color components specified in the <code>float</code>
 * array and the specified alpha.  The number of components is
 * determined by the type of the <code>ColorSpace</code>.  For
 * example, RGB requires 3 components, but CMYK requires 4
 * components.
 * @param cspace the <code>ColorSpace</code> to be used to
 *                  interpret the components
 * @param components an arbitrary number of color components
 *                      that is compatible with the <code>ColorSpace</code>
 * @param alpha alpha value
 * @throws IllegalArgumentException if any of the values in the
 *         <code>components</code> array or <code>alpha</code> is
 *         outside of the range 0.0 to 1.0
 * @see #getComponents
 * @see #getColorComponents
 */
public Color(ColorSpace cspace, float components[], float alpha) {
    boolean rangeError = false;
    String badComponentString = "";
    int n = cspace.getNumComponents();
    fvalue = new float[n];
    for (int i = 0; i < n; i++) {
        if (components[i] < 0.0 || components[i] > 1.0) {
            rangeError = true;
            badComponentString = badComponentString + "Component " + i
                                 + " ";
        } else {
            fvalue[i] = components[i];
        }
    }
    if (alpha < 0.0 || alpha > 1.0) {
        rangeError = true;
        badComponentString = badComponentString + "Alpha";
    } else {
        falpha = alpha;
    }
    if (rangeError) {
        throw new IllegalArgumentException(
            "Color parameter outside of expected range: " +
            badComponentString);
    }
    frgbvalue = cspace.toRGB(fvalue);
    cs = cspace;
    value = ((((int)(falpha*255)) & 0xFF) << 24) |
            ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
            ((((int)(frgbvalue[1]*255)) & 0xFF) << 8)  |
            ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
}
 
Example #16
Source File: CMMTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void init() {
    cmmRoot = new Group("cmm", "Color Management Benchmarks");
    cmmRoot.setTabbed();

    cmmOptRoot = new Group(cmmRoot, "opts", "General Options");

    /*
    usePlatfromProfiles =
            new Option.Enable(cmmOptRoot, "csPlatfrom",
                    "Use Platfrom Profiles", false);
    */
    int[] colorspaces = new int[] {
        ColorSpace.CS_sRGB,
        ColorSpace.CS_GRAY,
        ColorSpace.CS_LINEAR_RGB,
        ColorSpace.CS_CIEXYZ
    };

    String[] csNames = new String[]{
        "CS_sRGB",
        "CS_GRAY",
        "CS_LINEAR_RGB",
        "CS_CIEXYZ"
    };

    csList = new Option.IntList(cmmOptRoot,
            "profiles", "Color Profiles",
            colorspaces, csNames, csNames, 0x8);

    ColorConversionTests.init();
    ProfileTests.init();
}
 
Example #17
Source File: RenderToCustomBufferTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createCustomBuffer() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
    WritableRaster wr = cm.createCompatibleWritableRaster(width, height);

    return new BufferedImage(cm, wr, false, null);
}
 
Example #18
Source File: AlphaTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    ColorConvertOp op = new ColorConvertOp(cs, null);
    // create source image filled with an opaque color
    BufferedImage src = createSrc();
    int srcAlpha = getAlpha(src);

    System.out.printf("Src alpha: 0x%02x\n", srcAlpha);

    // create clear (transparent black) destination image
    BufferedImage dst = createDst();
    int dstAlpha = getAlpha(dst);
    System.out.printf("Dst alpha: 0x%02x\n", dstAlpha);


    dst = op.filter(src, dst);
    dstAlpha = getAlpha(dst);
    // we expect that destination image is opaque
    // i.e. alpha is transferred from source to
    // the destination
    System.out.printf("Result alpha: 0x%02x\n", dstAlpha);

    if (srcAlpha != dstAlpha) {
        throw new RuntimeException("Test failed!");
    }
    System.out.println("Test passed");
}
 
Example #19
Source File: JPEG.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static ColorSpace getYCC() {
    if (!yccInited) {
        try {
            YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
        } catch (IllegalArgumentException e) {
            // PYCC.pf may not always be installed
        } finally {
            yccInited = true;
        }
    }
    return YCC;
}
 
Example #20
Source File: DaisyFilter.java    From scrimage with Apache License 2.0 5 votes vote down vote up
private BufferedImage getGrayScale(BufferedImage bi) {
    // the following code is by David Ekholm from the GrayscaleFilter
    final Graphics2D g = bi.createGraphics();
    final RenderingHints rhs = g.getRenderingHints();

    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    final ColorConvertOp theOp = new ColorConvertOp(cs, rhs);

    final BufferedImage dstImg = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    theOp.filter(bi, dstImg);

    return dstImg;
}
 
Example #21
Source File: ComponentColorModel.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static int bitsHelper(int transferType,
                              ColorSpace colorSpace,
                              boolean hasAlpha) {
    int numBits = DataBuffer.getDataTypeSize(transferType);
    int numComponents = colorSpace.getNumComponents();
    if (hasAlpha) {
        ++numComponents;
    }
    return numBits * numComponents;
}
 
Example #22
Source File: Color.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a color in the specified {@code ColorSpace}
 * with the color components specified in the {@code float}
 * array and the specified alpha.  The number of components is
 * determined by the type of the {@code ColorSpace}.  For
 * example, RGB requires 3 components, but CMYK requires 4
 * components.
 * @param cspace the {@code ColorSpace} to be used to
 *                  interpret the components
 * @param components an arbitrary number of color components
 *                      that is compatible with the {@code ColorSpace}
 * @param alpha alpha value
 * @throws IllegalArgumentException if any of the values in the
 *         {@code components} array or {@code alpha} is
 *         outside of the range 0.0 to 1.0
 * @see #getComponents
 * @see #getColorComponents
 */
public Color(ColorSpace cspace, float components[], float alpha) {
    boolean rangeError = false;
    String badComponentString = "";
    int n = cspace.getNumComponents();
    fvalue = new float[n];
    for (int i = 0; i < n; i++) {
        if (components[i] < 0.0 || components[i] > 1.0) {
            rangeError = true;
            badComponentString = badComponentString + "Component " + i
                                 + " ";
        } else {
            fvalue[i] = components[i];
        }
    }
    if (alpha < 0.0 || alpha > 1.0) {
        rangeError = true;
        badComponentString = badComponentString + "Alpha";
    } else {
        falpha = alpha;
    }
    if (rangeError) {
        throw new IllegalArgumentException(
            "Color parameter outside of expected range: " +
            badComponentString);
    }
    frgbvalue = cspace.toRGB(fvalue);
    cs = cspace;
    value = ((((int)(falpha*255)) & 0xFF) << 24) |
            ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
            ((((int)(frgbvalue[1]*255)) & 0xFF) << 8)  |
            ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
}
 
Example #23
Source File: Color.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a <code>float</code> array containing the color and alpha
 * components of the <code>Color</code>, in the
 * <code>ColorSpace</code> specified by the <code>cspace</code>
 * parameter.  If <code>compArray</code> is <code>null</code>, an
 * array with length equal to the number of components in
 * <code>cspace</code> plus one is created for the return value.
 * Otherwise, <code>compArray</code> must have at least this
 * length, and it is filled in with the components and returned.
 * @param cspace a specified <code>ColorSpace</code>
 * @param compArray an array that this method fills with the
 *          color and alpha components of this <code>Color</code> in
 *          the specified <code>ColorSpace</code> and returns
 * @return the color and alpha components in a <code>float</code>
 *          array.
 */
public float[] getComponents(ColorSpace cspace, float[] compArray) {
    if (cs == null) {
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    }
    float f[];
    if (fvalue == null) {
        f = new float[3];
        f[0] = ((float)getRed())/255f;
        f[1] = ((float)getGreen())/255f;
        f[2] = ((float)getBlue())/255f;
    } else {
        f = fvalue;
    }
    float tmp[] = cs.toCIEXYZ(f);
    float tmpout[] = cspace.fromCIEXYZ(tmp);
    if (compArray == null) {
        compArray = new float[tmpout.length + 1];
    }
    for (int i = 0 ; i < tmpout.length ; i++) {
        compArray[i] = tmpout[i];
    }
    if (fvalue == null) {
        compArray[tmpout.length] = ((float)getAlpha())/255f;
    } else {
        compArray[tmpout.length] = falpha;
    }
    return compArray;
}
 
Example #24
Source File: Color.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a color in the specified <code>ColorSpace</code>
 * with the color components specified in the <code>float</code>
 * array and the specified alpha.  The number of components is
 * determined by the type of the <code>ColorSpace</code>.  For
 * example, RGB requires 3 components, but CMYK requires 4
 * components.
 * @param cspace the <code>ColorSpace</code> to be used to
 *                  interpret the components
 * @param components an arbitrary number of color components
 *                      that is compatible with the <code>ColorSpace</code>
 * @param alpha alpha value
 * @throws IllegalArgumentException if any of the values in the
 *         <code>components</code> array or <code>alpha</code> is
 *         outside of the range 0.0 to 1.0
 * @see #getComponents
 * @see #getColorComponents
 */
public Color(ColorSpace cspace, float components[], float alpha) {
    boolean rangeError = false;
    String badComponentString = "";
    int n = cspace.getNumComponents();
    fvalue = new float[n];
    for (int i = 0; i < n; i++) {
        if (components[i] < 0.0 || components[i] > 1.0) {
            rangeError = true;
            badComponentString = badComponentString + "Component " + i
                                 + " ";
        } else {
            fvalue[i] = components[i];
        }
    }
    if (alpha < 0.0 || alpha > 1.0) {
        rangeError = true;
        badComponentString = badComponentString + "Alpha";
    } else {
        falpha = alpha;
    }
    if (rangeError) {
        throw new IllegalArgumentException(
            "Color parameter outside of expected range: " +
            badComponentString);
    }
    frgbvalue = cspace.toRGB(fvalue);
    cs = cspace;
    value = ((((int)(falpha*255)) & 0xFF) << 24) |
            ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
            ((((int)(frgbvalue[1]*255)) & 0xFF) << 8)  |
            ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
}
 
Example #25
Source File: RGBColorConvertTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) {
    BufferedImage src =
        new BufferedImage(256,3,BufferedImage.TYPE_INT_RGB);
    BufferedImage dst =
        new BufferedImage(256,3,BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < 256; i++) {
        src.setRGB(i,0,i);
        src.setRGB(i,1,i << 8);
        src.setRGB(i,2,i << 16);
    }

    ColorSpace srcColorSpace = src.getColorModel().getColorSpace();

    ColorConvertOp op = new ColorConvertOp(srcColorSpace, srcColorSpace,
                                           null);
    op.filter(src, dst);

    int errCount = 0;
    for (int i = 0; i < src.getWidth(); i++) {
        for (int j = 0; j < src.getHeight(); j++) {
            int scol = src.getRGB(i,j);
            int dcol = dst.getRGB(i,j);
            if (scol != dcol) {
                System.err.println("(" + i + "," + j + ") : " +
                                   Integer.toHexString(scol) + "!=" +
                                   Integer.toHexString(dcol));
                errCount++;
            }
        }
    }

    if (errCount > 0) {
        throw new RuntimeException(errCount + " pixels are changed by " +
                                   "transform between the same ICC color " +
                                   "spaces");
    }
}
 
Example #26
Source File: RtfTransferableData.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int[] getAdjustedColorComponents(Color color) {
  if (SystemInfo.isMac) {
    // on Mac OS color components are expected in Apple's 'Generic RGB' color space
    ColorSpace genericRgbSpace = MacOSApplicationProvider.getInstance().getGenericRgbColorSpace();
    if (genericRgbSpace != null) {
      float[] components = genericRgbSpace.fromRGB(color.getRGBColorComponents(null));
      return new int[] {
              colorComponentFloatToInt(components[0]),
              colorComponentFloatToInt(components[1]),
              colorComponentFloatToInt(components[2])
      };
    }
  }
  return new int[] {color.getRed(), color.getGreen(), color.getBlue()};
}
 
Example #27
Source File: Color.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a color in the specified <code>ColorSpace</code>
 * with the color components specified in the <code>float</code>
 * array and the specified alpha.  The number of components is
 * determined by the type of the <code>ColorSpace</code>.  For
 * example, RGB requires 3 components, but CMYK requires 4
 * components.
 * @param cspace the <code>ColorSpace</code> to be used to
 *                  interpret the components
 * @param components an arbitrary number of color components
 *                      that is compatible with the <code>ColorSpace</code>
 * @param alpha alpha value
 * @throws IllegalArgumentException if any of the values in the
 *         <code>components</code> array or <code>alpha</code> is
 *         outside of the range 0.0 to 1.0
 * @see #getComponents
 * @see #getColorComponents
 */
public Color(ColorSpace cspace, float components[], float alpha) {
    boolean rangeError = false;
    String badComponentString = "";
    int n = cspace.getNumComponents();
    fvalue = new float[n];
    for (int i = 0; i < n; i++) {
        if (components[i] < 0.0 || components[i] > 1.0) {
            rangeError = true;
            badComponentString = badComponentString + "Component " + i
                                 + " ";
        } else {
            fvalue[i] = components[i];
        }
    }
    if (alpha < 0.0 || alpha > 1.0) {
        rangeError = true;
        badComponentString = badComponentString + "Alpha";
    } else {
        falpha = alpha;
    }
    if (rangeError) {
        throw new IllegalArgumentException(
            "Color parameter outside of expected range: " +
            badComponentString);
    }
    frgbvalue = cspace.toRGB(fvalue);
    cs = cspace;
    value = ((((int)(falpha*255)) & 0xFF) << 24) |
            ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
            ((((int)(frgbvalue[1]*255)) & 0xFF) << 8)  |
            ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
}
 
Example #28
Source File: Color.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a <code>float</code> array containing the color and alpha
 * components of the <code>Color</code>, in the
 * <code>ColorSpace</code> specified by the <code>cspace</code>
 * parameter.  If <code>compArray</code> is <code>null</code>, an
 * array with length equal to the number of components in
 * <code>cspace</code> plus one is created for the return value.
 * Otherwise, <code>compArray</code> must have at least this
 * length, and it is filled in with the components and returned.
 * @param cspace a specified <code>ColorSpace</code>
 * @param compArray an array that this method fills with the
 *          color and alpha components of this <code>Color</code> in
 *          the specified <code>ColorSpace</code> and returns
 * @return the color and alpha components in a <code>float</code>
 *          array.
 */
public float[] getComponents(ColorSpace cspace, float[] compArray) {
    if (cs == null) {
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    }
    float f[];
    if (fvalue == null) {
        f = new float[3];
        f[0] = ((float)getRed())/255f;
        f[1] = ((float)getGreen())/255f;
        f[2] = ((float)getBlue())/255f;
    } else {
        f = fvalue;
    }
    float tmp[] = cs.toCIEXYZ(f);
    float tmpout[] = cspace.fromCIEXYZ(tmp);
    if (compArray == null) {
        compArray = new float[tmpout.length + 1];
    }
    for (int i = 0 ; i < tmpout.length ; i++) {
        compArray[i] = tmpout[i];
    }
    if (fvalue == null) {
        compArray[tmpout.length] = ((float)getAlpha())/255f;
    } else {
        compArray[tmpout.length] = falpha;
    }
    return compArray;
}
 
Example #29
Source File: ColConvTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static String getCSName(int cs) {
    switch(cs) {
        case ColorSpace.CS_GRAY:
            return "CS_GRAY";
        case ColorSpace.CS_CIEXYZ:
            return "CS_CIEXYZ";
        case ColorSpace.CS_LINEAR_RGB:
            return "CS_LINEAR_RGB";
        case ColorSpace.CS_PYCC:
            return "CS_PYCC";
        case ColorSpace.CS_sRGB:
            return "CS_sRGB";
    }
    return "UNKNOWN";
}
 
Example #30
Source File: JPEG.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given <code>ColorSpace</code>
 * object is an instance of ICC_ColorSpace but is not one of the
 * standard <code>ColorSpaces</code> returned by
 * <code>ColorSpace.getInstance()</code>.
 */
static boolean isNonStandardICC(ColorSpace cs) {
    boolean retval = false;
    if ((cs instanceof ICC_ColorSpace)
        && (!cs.isCS_sRGB())
        && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
        && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
        && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
        && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
        ) {
        retval = true;
    }
    return retval;
}