Search in sources :

Example 36 with ColorSpace

use of java.awt.color.ColorSpace in project android by JetBrains.

the class KMeansColorExtractor method runClustering.

@Override
List<ExtractedColor> runClustering(HashMap<Integer, Integer> labToRgb, List<double[]> clusterInput, Clusterer.ProgressListener listener) {
    List<ExtractedColor> extractedColors;
    List<List<double[]>> clusters = new KMeansClusterer(myK).cluster(clusterInput.toArray(new double[clusterInput.size()][POINT_DIMENSION]));
    extractedColors = new ArrayList<>(clusters.size());
    ColorSpace labColorSpace = Colors.getLabColorSpace();
    for (List<double[]> cluster : clusters) {
        double[] color = cluster.get(0);
        float[] rgb = labColorSpace.toRGB(new float[] { (float) color[0], (float) color[1], (float) color[2] });
        int rgbInt = Math.round(rgb[0] * 255) << 16 | Math.round(rgb[1] * 255) << 8 | Math.round(rgb[2] * 255);
        extractedColors.add(new ExtractedColor(rgbInt, 1, null));
    }
    return extractedColors;
}
Also used : ColorSpace(java.awt.color.ColorSpace) List(java.util.List) ArrayList(java.util.ArrayList)

Example 37 with ColorSpace

use of java.awt.color.ColorSpace in project intellij-community by JetBrains.

the class MacColorPipette method captureScreen.

@Nullable
static BufferedImage captureScreen(@Nullable Window belowWindow, @NotNull Rectangle rect) {
    ID pool = Foundation.invoke("NSAutoreleasePool", "new");
    try {
        ID windowId = belowWindow != null ? MacUtil.findWindowFromJavaWindow(belowWindow) : null;
        Foundation.NSRect nsRect = new Foundation.NSRect(rect.x, rect.y, rect.width, rect.height);
        ID cgWindowId = windowId != null ? Foundation.invoke(windowId, "windowNumber") : ID.NIL;
        int windowListOptions = cgWindowId != null ? FoundationLibrary.kCGWindowListOptionOnScreenBelowWindow : FoundationLibrary.kCGWindowListOptionAll;
        int windowImageOptions = FoundationLibrary.kCGWindowImageNominalResolution;
        ID cgImageRef = Foundation.cgWindowListCreateImage(nsRect, windowListOptions, cgWindowId, windowImageOptions);
        ID bitmapRep = Foundation.invoke(Foundation.invoke("NSBitmapImageRep", "alloc"), "initWithCGImage:", cgImageRef);
        ID nsImage = Foundation.invoke(Foundation.invoke("NSImage", "alloc"), "init");
        Foundation.invoke(nsImage, "addRepresentation:", bitmapRep);
        ID data = Foundation.invoke(nsImage, "TIFFRepresentation");
        ID bytes = Foundation.invoke(data, "bytes");
        ID length = Foundation.invoke(data, "length");
        ByteBuffer byteBuffer = Native.getDirectByteBuffer(bytes.longValue(), length.longValue());
        Foundation.invoke(nsImage, "release");
        byte[] b = new byte[byteBuffer.remaining()];
        byteBuffer.get(b);
        BufferedImage result = ImageIO.read(new ByteArrayInputStream(b));
        if (result != null) {
            ColorSpace ics = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            ColorConvertOp cco = new ColorConvertOp(ics, null);
            return cco.filter(result, null);
        }
        return null;
    } catch (Throwable t) {
        LOG.error(t);
        return null;
    } finally {
        Foundation.invoke(pool, "release");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) Foundation(com.intellij.ui.mac.foundation.Foundation) ID(com.intellij.ui.mac.foundation.ID) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class CMMTests method getColorSpace.

protected static ColorSpace getColorSpace(TestEnvironment env) {
    ColorSpace cs;
    //(Boolean)env.getModifier(usePlatfromProfiles);
    Boolean usePlatfrom = true;
    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;
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) InputStream(java.io.InputStream) IOException(java.io.IOException) ICC_Profile(java.awt.color.ICC_Profile)

Example 39 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class RGBColorConvertTest method main.

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");
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 40 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class ITSDataType method main.

public static void main(String[] args) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    int[] bankIndices = new int[] { 1 };
    int[] bandOffsets = new int[] { 0 };
    // test createBanded()
    for (int i = 0; i < dataTypes.length; i++) {
        int dataType = dataTypes[i];
        try {
            ImageTypeSpecifier.createBanded(cs, bankIndices, bandOffsets, dataType, false, false);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("createBanded() test failed for " + "dataType = " + dataType);
        }
    }
    // test createInterleaved()
    for (int i = 0; i < dataTypes.length; i++) {
        int dataType = dataTypes[i];
        try {
            ImageTypeSpecifier.createInterleaved(cs, bandOffsets, dataType, false, false);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("createInterleaved() test failed " + "for dataType = " + dataType);
        }
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Aggregations

ColorSpace (java.awt.color.ColorSpace)74 BufferedImage (java.awt.image.BufferedImage)28 ColorModel (java.awt.image.ColorModel)28 ComponentColorModel (java.awt.image.ComponentColorModel)25 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)22 SampleModel (java.awt.image.SampleModel)15 IndexColorModel (java.awt.image.IndexColorModel)13 WritableRaster (java.awt.image.WritableRaster)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)8 DataBuffer (java.awt.image.DataBuffer)8 DirectColorModel (java.awt.image.DirectColorModel)8 Point (java.awt.Point)7 DataBufferByte (java.awt.image.DataBufferByte)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)4 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4