Search in sources :

Example 31 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method test24BitGray.

public static void test24BitGray() throws IOException {
    if (!isKakaduAvailable) {
        LOGGER.warning("Kakadu libs not found: test are skipped ");
        return;
    }
    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 24 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_INT);
    final int w = 512;
    final int h = 512;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final int[] bufferValues = new int[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (int) (j + i) * (16777216 /
        // 1024);
        bufferValues[j + (i * h)] = (int) (Math.random() * 16777215d);
    }
    DataBuffer imageBuffer = new DataBufferInt(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
    param.setSourceSubsampling(2, 3, 0, 0);
    write(outputFileName + "_gray24", bi, true, lossLessQuality);
    write(outputFileName + "_gray24", bi, false, lossLessQuality);
    write(outputFileName + "_gray24", bi, true, lossyQuality);
    write(outputFileName + "_gray24", bi, false, lossyQuality);
    write(outputFileName + "_JAI_gray24", bi, true, lossLessQuality, true);
    write(outputFileName + "_JAI_gray24", bi, false, lossLessQuality, true);
    write(outputFileName + "_JAI_subSampled_gray24", bi, true, lossyQuality, true, param);
    write(outputFileName + "_JAI_subSampled_gray24", bi, false, lossyQuality, true, param);
    LOGGER.info(writeOperations + " write operations performed");
}
Also used : SampleModel(java.awt.image.SampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 32 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class JP2KKakaduImageReader method getColorModel.

/**
 * Setup a proper <code>ColorModel</code>
 *
 * @return a color model.
 * @throws KduException
 */
private static ColorModel getColorModel(JP2KCodestreamProperties codestreamP) throws KduException {
    if (codestreamP.getColorModel() != null)
        return codestreamP.getColorModel();
    if (codestreamP.getColorModel() != null)
        return codestreamP.getColorModel();
    final int nComponents = codestreamP.getNumComponents();
    if (nComponents <= 4) {
        ColorSpace cs;
        if (nComponents > 2) {
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        } else {
            cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        }
        boolean hasAlpha = nComponents % 2 == 0;
        final int maxBitDepth = codestreamP.getMaxBitDepth();
        if (maxBitDepth <= 8) {
            codestreamP.setDataBufferType(DataBuffer.TYPE_BYTE);
        } else if (maxBitDepth <= 16) {
            if (codestreamP.isSigned())
                codestreamP.setDataBufferType(DataBuffer.TYPE_SHORT);
            else
                codestreamP.setDataBufferType(DataBuffer.TYPE_USHORT);
        } else if (maxBitDepth <= 32) {
            codestreamP.setDataBufferType(DataBuffer.TYPE_INT);
        }
        final int dataBufferType = codestreamP.getDataBufferType();
        if (dataBufferType != -1) {
            if (nComponents == 1 && (maxBitDepth == 1 || maxBitDepth == 2 || maxBitDepth == 4)) {
                codestreamP.setColorModel(ImageUtil.createColorModel(getSampleModel(codestreamP)));
            } else {
                codestreamP.setColorModel(new ComponentColorModel(cs, codestreamP.getBitsPerComponent(), hasAlpha, false, hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, dataBufferType));
            }
            return codestreamP.getColorModel();
        }
    }
    if (codestreamP.getSampleModel() == null)
        codestreamP.setSampleModel(getSampleModel(codestreamP));
    if (codestreamP.getSampleModel() == null)
        return null;
    return ImageUtil.createColorModel(codestreamP.getSampleModel());
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel)

Example 33 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class ImageIOUtilities method createColorModel.

/**
 * 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;
        if (dataType == DataBuffer.TYPE_SHORT)
            colorModel = new ComponentColorModel(colorSpace, sampleSize, hasAlpha, isAlphaPremultiplied, transparency, dataType);
        else
            colorModel = RasterFactory.createComponentColorModel(dataType, colorSpace, hasAlpha, isAlphaPremultiplied, transparency);
    } 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) BogusColorSpace(com.sun.imageio.plugins.common.BogusColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) 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) BogusColorSpace(com.sun.imageio.plugins.common.BogusColorSpace) IndexColorModel(java.awt.image.IndexColorModel)

Example 34 with ColorSpace

use of java.awt.color.ColorSpace in project wechat by dllwh.

the class OperateImageHelper method gray.

/**
 * 彩色转为黑白
 *
 * @param srcImageFile
 *            源图像地址
 * @param destImageFile
 *            目标图像地址
 */
public static final void gray(String srcImageFile, String destImageFile) {
    try {
        BufferedImage src = ImageIO.read(new File(srcImageFile));
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(cs, null);
        src = op.filter(src, null);
        ImageIO.write(src, "jpg", new File(destImageFile));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 35 with ColorSpace

use of java.awt.color.ColorSpace in project jmonkeyengine by jMonkeyEngine.

the class ImageToAwt method convert.

public static BufferedImage convert(Image image, boolean do16bit, boolean fullalpha, int mipLevel) {
    Format format = image.getFormat();
    DecodeParams p = params.get(image.getFormat());
    if (p == null)
        throw new UnsupportedOperationException();
    int width = image.getWidth();
    int height = image.getHeight();
    int level = mipLevel;
    while (--level >= 0) {
        width /= 2;
        height /= 2;
    }
    ByteBuffer buf = image.getData(0);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    BufferedImage out;
    boolean alpha = false;
    boolean luminance = false;
    boolean rgb = false;
    if (p.am != 0)
        alpha = true;
    if (p.rm != 0 && p.gm == 0 && p.bm == 0)
        luminance = true;
    else if (p.rm != 0 && p.gm != 0 && p.bm != 0)
        rgb = true;
    // alpha OR luminance but not both
    if ((alpha && !rgb && !luminance) || (luminance && !alpha && !rgb)) {
        out = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    } else if ((rgb && alpha) || (luminance && alpha)) {
        if (do16bit) {
            if (fullalpha) {
                ColorModel model = AWTLoader.AWT_RGBA4444;
                WritableRaster raster = model.createCompatibleWritableRaster(width, width);
                out = new BufferedImage(model, raster, false, null);
            } else {
                // RGB5_A1
                ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                int[] nBits = { 5, 5, 5, 1 };
                int[] bOffs = { 0, 1, 2, 3 };
                ColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.BITMASK, DataBuffer.TYPE_BYTE);
                WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 2, 2, bOffs, null);
                out = new BufferedImage(colorModel, raster, false, null);
            }
        } else {
            out = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        }
    } else {
        if (do16bit) {
            out = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_565_RGB);
        } else {
            out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }
    }
    int expansionA = 8 - Integer.bitCount(p.am);
    int expansionR = 8 - Integer.bitCount(p.rm);
    int expansionG = 8 - Integer.bitCount(p.gm);
    int expansionB = 8 - Integer.bitCount(p.bm);
    if (expansionR < 0) {
        expansionR = 0;
    }
    int mipPos = 0;
    for (int i = 0; i < mipLevel; i++) {
        mipPos += image.getMipMapSizes()[i];
    }
    int inputPixel;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int i = mipPos + (Ix(x, y, width) * p.bpp);
            inputPixel = (readPixel(buf, i, p.bpp) & p.im) >> p.is;
            int a = (inputPixel & p.am) >> p.as;
            int r = (inputPixel & p.rm) >> p.rs;
            int g = (inputPixel & p.gm) >> p.gs;
            int b = (inputPixel & p.bm) >> p.bs;
            r = r & 0xff;
            g = g & 0xff;
            b = b & 0xff;
            a = a & 0xff;
            a = a << expansionA;
            r = r << expansionR;
            g = g << expansionG;
            b = b << expansionB;
            if (luminance)
                b = g = r;
            if (!alpha)
                a = 0xff;
            int argb = (a << 24) | (r << 16) | (g << 8) | b;
            out.setRGB(x, y, argb);
        }
    }
    return out;
}
Also used : Format(com.jme3.texture.Image.Format) ColorSpace(java.awt.color.ColorSpace) ByteBuffer(java.nio.ByteBuffer)

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