Java Code Examples for java.awt.image.ColorModel#equals()

The following examples show how to use java.awt.image.ColorModel#equals() . 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: IsToolkitUseTheMainScreen.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testHeadful() {
    GraphicsEnvironment ge
            = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration gc
            = ge.getDefaultScreenDevice().getDefaultConfiguration();
    Dimension gcSize = gc.getBounds().getSize();
    ColorModel gcCM = gc.getColorModel();

    Dimension toolkitSize = Toolkit.getDefaultToolkit().getScreenSize();
    ColorModel toolkitCM = Toolkit.getDefaultToolkit().getColorModel();

    if (!gcSize.equals(toolkitSize)) {
        System.err.println("Toolkit size = " + toolkitSize);
        System.err.println("GraphicsConfiguration size = " + gcSize);
        throw new RuntimeException("Incorrect size");
    }
    if (!gcCM.equals(toolkitCM)) {
        System.err.println("Toolkit color model = " + toolkitCM);
        System.err.println("GraphicsConfiguration color model = " + gcCM);
        throw new RuntimeException("Incorrect color model");
    }
}
 
Example 2
Source File: EqualsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    SimpleColorModel scm1 = new SimpleColorModel(3);
    SimpleColorModel scm2 = new SimpleColorModel(3);
    SimpleColorModel scm3 = new SimpleColorModel(8);
    ColorModel rgbcm = ColorModel.getRGBdefault();

    try {
        if (scm1.equals(scm2)) {
            throw new RuntimeException("Test 1 failed: " +
                                       "scm1 should not equal scm2");
        }

        if (scm1.equals(scm3)) {
            throw new RuntimeException("Test 2 failed: " +
                                       "scm1 should not equal scm3");
        }

        if (scm1.equals(rgbcm) || rgbcm.equals(scm1)) {
            throw new RuntimeException("Test 3 failed: " +
                                       "scm1 should not equal rgbcm");
        }
    } catch (Exception e) {
        throw new RuntimeException("Test failed: " + e);
    }
}
 
Example 3
Source File: GraphicsUtilities.java    From SVG-Android with Apache License 2.0 6 votes vote down vote up
private static BufferedImage toCompatibleImage(BufferedImage image) {
    if (isHeadless()) {
        return image;
    }

    ColorModel colorModel = image.getColorModel();
    if (colorModel != null && colorModel.equals(getGraphicsConfiguration().getColorModel())) {
        return image;
    }

    BufferedImage compatibleImage = getGraphicsConfiguration().createCompatibleImage(
            image.getWidth(), image.getHeight(), image.getTransparency());
    Graphics g = compatibleImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return compatibleImage;
}
 
Example 4
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates a buffered image possessing the given color model, from the
 * specified AWT image object. If the AWT image is already a buffered image
 * with the given color model, no new object is created.
 */
public static BufferedImage makeBuffered(final Image image,
	final ColorModel cm)
{
	if (cm == null) return makeBuffered(image);

	if (image instanceof BufferedImage) {
		final BufferedImage bi = (BufferedImage) image;
		if (cm.equals(bi.getColorModel())) return bi;
	}
	loadImage(image);
	final int w = image.getWidth(OBS), h = image.getHeight(OBS);
	final boolean alphaPremultiplied = cm.isAlphaPremultiplied();
	final WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
	final BufferedImage result = new BufferedImage(cm, raster,
		alphaPremultiplied, null);
	final Graphics2D g = result.createGraphics();
	g.drawImage(image, 0, 0, OBS);
	g.dispose();
	return result;
}
 
Example 5
Source File: ColorModelPatch.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if the given color models are equal. The {@link ColorModel} class
 * defines an {@code equals} method, but as of Java 6 that method does not compare every
 * attributes. For example it does not compare the color space and the transfer type, so
 * we have to compare them here.
 *
 * @param cm1  the first color model.
 * @param cm2  the second color model.
 * @return {@code true} if the two color models are equal.
 */
private static boolean equals(final ColorModel cm1, final ColorModel cm2) {
    if (cm1 == cm2) {
        return true;
    }
    if (cm1 != null && cm1.equals(cm2) &&
        cm1.getClass().equals(cm2.getClass()) &&
        cm1.getTransferType() == cm2.getTransferType() &&
        Objects.equals(cm1.getColorSpace(), cm2.getColorSpace()))
    {
        if (cm1 instanceof IndexColorModel) {
            final IndexColorModel icm1 = (IndexColorModel) cm1;
            final IndexColorModel icm2 = (IndexColorModel) cm2;
            final int size = icm1.getMapSize();
            if (icm2.getMapSize() == size &&
                icm1.getTransparentPixel() == icm2.getTransparentPixel() &&
                Objects.equals(icm1.getValidPixels(), icm2.getValidPixels()))
            {
                for (int i=0; i<size; i++) {
                    if (icm1.getRGB(i) != icm2.getRGB(i)) {
                        return false;
                    }
                }
            }
            if (cm1 instanceof MultiBandsIndexColorModel) {
                final MultiBandsIndexColorModel micm1 = (MultiBandsIndexColorModel) cm1;
                final MultiBandsIndexColorModel micm2 = (MultiBandsIndexColorModel) cm2;
                if (micm1.numBands != micm2.numBands || micm1.visibleBand != micm2.visibleBand) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}