Search in sources :

Example 11 with ColorSpace

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

the class ImageUtil method createColorModel.

/* XXX testing only
    public static void main(String[] args) {
        ImageTypeSpecifier bilevel =
            ImageTypeSpecifier.createIndexed(new byte[] {(byte)0, (byte)255},
                                             new byte[] {(byte)0, (byte)255},
                                             new byte[] {(byte)0, (byte)255},
                                             null, 1,
                                             DataBuffer.TYPE_BYTE);
        ImageTypeSpecifier gray =
            ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false);
        ImageTypeSpecifier grayAlpha =
            ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false,
                                               false);
        ImageTypeSpecifier rgb =
            ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                                 new int[] {0, 1, 2},
                                                 DataBuffer.TYPE_BYTE,
                                                 false,
                                                 false);
        ImageTypeSpecifier rgba =
            ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                                 new int[] {0, 1, 2, 3},
                                                 DataBuffer.TYPE_BYTE,
                                                 true,
                                                 false);
        ImageTypeSpecifier packed =
            ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                            0xff000000,
                                            0x00ff0000,
                                            0x0000ff00,
                                            0x000000ff,
                                            DataBuffer.TYPE_BYTE,
                                            false);

        SampleModel bandedSM =
            new java.awt.image.BandedSampleModel(DataBuffer.TYPE_BYTE,
                                                 1, 1, 15);

        System.out.println(createColorModel(bilevel.getSampleModel()));
        System.out.println(createColorModel(gray.getSampleModel()));
        System.out.println(createColorModel(grayAlpha.getSampleModel()));
        System.out.println(createColorModel(rgb.getSampleModel()));
        System.out.println(createColorModel(rgba.getSampleModel()));
        System.out.println(createColorModel(packed.getSampleModel()));
        System.out.println(createColorModel(bandedSM));
    }
    */
/**
     * Creates a <code>ColorModel</code> that may be used with the
     * specified <code>SampleModel</code>.  If a suitable
     * <code>ColorModel</code> cannot be found, this method returns
     * <code>null</code>.
     *
     * <p> Suitable <code>ColorModel</code>s are guaranteed to exist
     * for all instances of <code>ComponentSampleModel</code>.
     * For 1- and 3- banded <code>SampleModel</code>s, the returned
     * <code>ColorModel</code> will be opaque.  For 2- and 4-banded
     * <code>SampleModel</code>s, the output will use alpha transparency
     * which is not premultiplied.  1- and 2-banded data will use a
     * grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB
     * <code>ColorSpace</code>. Data with 5 or more bands will have a
     * <code>BogusColorSpace</code>.</p>
     *
     * <p>An instance of <code>DirectColorModel</code> will be created for
     * instances of <code>SinglePixelPackedSampleModel</code> with no more
     * than 4 bands.</p>
     *
     * <p>An instance of <code>IndexColorModel</code> will be created for
     * instances of <code>MultiPixelPackedSampleModel</code>. The colormap
     * will be a grayscale ramp with <code>1&nbsp;<<&nbsp;numberOfBits</code>
     * entries ranging from zero to at most 255.</p>
     *
     * @return An instance of <code>ColorModel</code> that is suitable for
     *         the supplied <code>SampleModel</code>, or <code>null</code>.
     *
     * @throws IllegalArgumentException  If <code>sampleModel</code> is
     *         <code>null</code>.
     */
public static final ColorModel createColorModel(SampleModel sampleModel) {
    // Check the parameter.
    if (sampleModel == null) {
        throw new IllegalArgumentException("sampleModel == null!");
    }
    // Get the data type.
    int dataType = sampleModel.getDataType();
    // Check the data type
    switch(dataType) {
        case DataBuffer.TYPE_BYTE:
        case DataBuffer.TYPE_USHORT:
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_INT:
        case DataBuffer.TYPE_FLOAT:
        case DataBuffer.TYPE_DOUBLE:
            break;
        default:
            // Return null for other types.
            return null;
    }
    // The return variable.
    ColorModel colorModel = null;
    // Get the sample size.
    int[] sampleSize = sampleModel.getSampleSize();
    // Create a Component ColorModel.
    if (sampleModel instanceof ComponentSampleModel) {
        // Get the number of bands.
        int numBands = sampleModel.getNumBands();
        // Determine the color space.
        ColorSpace colorSpace = null;
        if (numBands <= 2) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        } else if (numBands <= 4) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        } else {
            colorSpace = new BogusColorSpace(numBands);
        }
        boolean hasAlpha = (numBands == 2) || (numBands == 4);
        boolean isAlphaPremultiplied = false;
        int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
        colorModel = new ComponentColorModel(colorSpace, sampleSize, hasAlpha, isAlphaPremultiplied, transparency, dataType);
    } else if (sampleModel.getNumBands() <= 4 && sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        int[] bitMasks = sppsm.getBitMasks();
        int rmask = 0;
        int gmask = 0;
        int bmask = 0;
        int amask = 0;
        int numBands = bitMasks.length;
        if (numBands <= 2) {
            rmask = gmask = bmask = bitMasks[0];
            if (numBands == 2) {
                amask = bitMasks[1];
            }
        } else {
            rmask = bitMasks[0];
            gmask = bitMasks[1];
            bmask = bitMasks[2];
            if (numBands == 4) {
                amask = bitMasks[3];
            }
        }
        int bits = 0;
        for (int i = 0; i < sampleSize.length; i++) {
            bits += sampleSize[i];
        }
        return new DirectColorModel(bits, rmask, gmask, bmask, amask);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        // Load the colormap with a ramp.
        int bitsPerSample = sampleSize[0];
        int numEntries = 1 << bitsPerSample;
        byte[] map = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            map[i] = (byte) (i * 255 / (numEntries - 1));
        }
        colorModel = new IndexColorModel(bitsPerSample, numEntries, map, map, map);
    }
    return colorModel;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 12 with ColorSpace

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

the class JPEGImageWriter method getDestCSType.

private int getDestCSType(ImageTypeSpecifier destType) {
    ColorModel cm = destType.getColorModel();
    boolean alpha = cm.hasAlpha();
    ColorSpace cs = cm.getColorSpace();
    int retval = JPEG.JCS_UNKNOWN;
    switch(cs.getType()) {
        case ColorSpace.TYPE_GRAY:
            retval = JPEG.JCS_GRAYSCALE;
            break;
        case ColorSpace.TYPE_RGB:
            if (alpha) {
                retval = JPEG.JCS_RGBA;
            } else {
                retval = JPEG.JCS_RGB;
            }
            break;
        case ColorSpace.TYPE_YCbCr:
            if (alpha) {
                retval = JPEG.JCS_YCbCrA;
            } else {
                retval = JPEG.JCS_YCbCr;
            }
            break;
        case ColorSpace.TYPE_3CLR:
            if (cs == JPEG.JCS.getYCC()) {
                if (alpha) {
                    retval = JPEG.JCS_YCCA;
                } else {
                    retval = JPEG.JCS_YCC;
                }
            }
        case ColorSpace.TYPE_CMYK:
            retval = JPEG.JCS_CMYK;
            break;
    }
    return retval;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 13 with ColorSpace

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

the class PNGImageReader method getImageTypes.

public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IIOException {
    if (imageIndex != 0) {
        throw new IndexOutOfBoundsException("imageIndex != 0!");
    }
    readHeader();
    ArrayList<ImageTypeSpecifier> l = new ArrayList<ImageTypeSpecifier>(1);
    ColorSpace rgb;
    ColorSpace gray;
    int[] bandOffsets;
    int bitDepth = metadata.IHDR_bitDepth;
    int colorType = metadata.IHDR_colorType;
    int dataType;
    if (bitDepth <= 8) {
        dataType = DataBuffer.TYPE_BYTE;
    } else {
        dataType = DataBuffer.TYPE_USHORT;
    }
    switch(colorType) {
        case PNG_COLOR_GRAY:
            // Packed grayscale
            l.add(ImageTypeSpecifier.createGrayscale(bitDepth, dataType, false));
            break;
        case PNG_COLOR_RGB:
            if (bitDepth == 8) {
                // some standard types of buffered images
                // which can be used as destination
                l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR));
                l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB));
                l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_BGR));
            }
            // Component R, G, B
            rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            bandOffsets = new int[3];
            bandOffsets[0] = 0;
            bandOffsets[1] = 1;
            bandOffsets[2] = 2;
            l.add(ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, false, false));
            break;
        case PNG_COLOR_PALETTE:
            // Need tRNS chunk
            readMetadata();
            /*
             * The PLTE chunk spec says:
             *
             * The number of palette entries must not exceed the range that
             * can be represented in the image bit depth (for example, 2^4 = 16
             * for a bit depth of 4). It is permissible to have fewer entries
             * than the bit depth would allow. In that case, any out-of-range
             * pixel value found in the image data is an error.
             *
             * http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.PLTE
             *
             * Consequently, the case when the palette length is smaller than
             * 2^bitDepth is legal in the view of PNG spec.
             *
             * However the spec of createIndexed() method demands the exact
             * equality of the palette lengh and number of possible palette
             * entries (2^bitDepth).
             *
             * {@link javax.imageio.ImageTypeSpecifier.html#createIndexed}
             *
             * In order to avoid this contradiction we need to extend the
             * palette arrays to the limit defined by the bitDepth.
             */
            int plength = 1 << bitDepth;
            byte[] red = metadata.PLTE_red;
            byte[] green = metadata.PLTE_green;
            byte[] blue = metadata.PLTE_blue;
            if (metadata.PLTE_red.length < plength) {
                red = Arrays.copyOf(metadata.PLTE_red, plength);
                Arrays.fill(red, metadata.PLTE_red.length, plength, metadata.PLTE_red[metadata.PLTE_red.length - 1]);
                green = Arrays.copyOf(metadata.PLTE_green, plength);
                Arrays.fill(green, metadata.PLTE_green.length, plength, metadata.PLTE_green[metadata.PLTE_green.length - 1]);
                blue = Arrays.copyOf(metadata.PLTE_blue, plength);
                Arrays.fill(blue, metadata.PLTE_blue.length, plength, metadata.PLTE_blue[metadata.PLTE_blue.length - 1]);
            }
            // Alpha from tRNS chunk may have fewer entries than
            // the RGB LUTs from the PLTE chunk; if so, pad with
            // 255.
            byte[] alpha = null;
            if (metadata.tRNS_present && (metadata.tRNS_alpha != null)) {
                if (metadata.tRNS_alpha.length == red.length) {
                    alpha = metadata.tRNS_alpha;
                } else {
                    alpha = Arrays.copyOf(metadata.tRNS_alpha, red.length);
                    Arrays.fill(alpha, metadata.tRNS_alpha.length, red.length, (byte) 255);
                }
            }
            l.add(ImageTypeSpecifier.createIndexed(red, green, blue, alpha, bitDepth, DataBuffer.TYPE_BYTE));
            break;
        case PNG_COLOR_GRAY_ALPHA:
            // Component G, A
            gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            bandOffsets = new int[2];
            bandOffsets[0] = 0;
            bandOffsets[1] = 1;
            l.add(ImageTypeSpecifier.createInterleaved(gray, bandOffsets, dataType, true, false));
            break;
        case PNG_COLOR_RGB_ALPHA:
            if (bitDepth == 8) {
                // some standard types of buffered images
                // wich can be used as destination
                l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR));
                l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB));
            }
            // Component R, G, B, A (non-premultiplied)
            rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            bandOffsets = new int[4];
            bandOffsets[0] = 0;
            bandOffsets[1] = 1;
            bandOffsets[2] = 2;
            bandOffsets[3] = 3;
            l.add(ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, true, false));
            break;
        default:
            break;
    }
    return l.iterator();
}
Also used : ColorSpace(java.awt.color.ColorSpace) ArrayList(java.util.ArrayList) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Point(java.awt.Point)

Example 14 with ColorSpace

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

the class RenderToCustomBufferTest method createCustomBuffer.

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);
}
Also used : ColorSpace(java.awt.color.ColorSpace) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Example 15 with ColorSpace

use of java.awt.color.ColorSpace in project BoofCV by lessthanoptimal.

the class ConvertBufferedImage method extractBuffered.

/**
 * <p>
 * Creates a new BufferedImage that internally uses the same data as the provided
 * GrayU8.  The returned BufferedImage will be of type TYPE_BYTE_GRAY.
 * </p>
 * <p/>
 * <p>
 * NOTE: This only works on images which are not subimages!
 * </p>
 *
 * @param img Input image who's data will be wrapped by the returned BufferedImage.
 * @return BufferedImage which shared data with the input image.
 */
public static BufferedImage extractBuffered(GrayU8 img) {
    if (img.isSubimage())
        throw new IllegalArgumentException("Sub-images are not supported for this operation");
    final int width = img.width;
    final int height = img.height;
    // wrap the byte array
    DataBuffer bufferByte = new DataBufferByte(img.data, width * height, 0);
    ColorModel colorModel;
    int[] bOffs = new int[] { 0 };
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    int[] nBits = { 8 };
    colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    // Create a raster using the sample model and data buffer
    WritableRaster raster = Raster.createInterleavedRaster(bufferByte, width, height, img.stride, 1, bOffs, new Point(0, 0));
    // Combine the color model and raster into a buffered image
    return new BufferedImage(colorModel, raster, false, null);
}
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