Search in sources :

Example 66 with ColorSpace

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

the class NITFReader method getImageTypes.

@Override
public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOException {
    checkIndex(imageIndex);
    List<ImageTypeSpecifier> l = new ArrayList<ImageTypeSpecifier>();
    try {
        ImageSubheader subheader = record.getImages()[imageIndex].getSubheader();
        String irep = subheader.getImageRepresentation().getStringData().trim();
        String pvType = subheader.getPixelValueType().getStringData().trim();
        int bandCount = subheader.getBandCount();
        int nbpp = subheader.getNumBitsPerPixel().getIntData();
        // if (NITFUtils.isCompressed(record, imageIndex))
        // {
        // throw new NotImplementedException(
        // "Only uncompressed imagery is currently supported");
        // }
        int nBytes = ((nbpp - 1) / 8) + 1;
        if (nBytes == 1 || nBytes == 2 || (nBytes == 4 && pvType.equals("R")) || (nBytes == 8 && pvType.equals("R"))) {
            if (nBytes == 1 && bandCount == 3 && irep.equals("RGB")) {
                ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                int[] bandOffsets = new int[3];
                for (int i = 0; i < bandOffsets.length; ++i) bandOffsets[i] = i;
                l.add(ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, DataBuffer.TYPE_BYTE, false, false));
            }
            l.add(ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false));
        } else {
            throw new UnsupportedOperationException("Support for pixels of size " + nbpp + " bytes has not been implemented yet");
        }
    } catch (NITFException e) {
        LOGGER.severe(e.getLocalizedMessage());
    }
    return l.iterator();
}
Also used : ImageSubheader(nitf.ImageSubheader) ColorSpace(java.awt.color.ColorSpace) NITFException(nitf.NITFException) ArrayList(java.util.ArrayList) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Point(java.awt.Point)

Example 67 with ColorSpace

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

the class JP2KKakaduWriteTest method testReducedMemory.

public static void testReducedMemory() throws IOException {
    if (!isKakaduAvailable) {
        LOGGER.warning("Kakadu libs not found: test are skipped ");
        return;
    }
    System.setProperty(JP2KKakaduImageWriter.MAX_BUFFER_SIZE_KEY, "64K");
    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 16 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    final int w = 512;
    final int h = 512;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final short[] bufferValues = new short[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (65536 /
        // 1024));
        bufferValues[j + (i * h)] = (short) (Math.random() * 65535);
    }
    DataBuffer imageBuffer = new DataBufferUShort(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    write(outputFileName + "_RM", bi, true, lossLessQuality);
    write(outputFileName + "_RM", bi, false, lossLessQuality);
    write(outputFileName + "_RM", bi, true, lossyQuality);
    write(outputFileName + "_RM", bi, false, lossyQuality);
    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) DataBufferUShort(java.awt.image.DataBufferUShort) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 68 with ColorSpace

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

the class JP2KKakaduWriteTest method test16BitGray.

public static void test16BitGray() 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[] { 16 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    final int w = 512;
    final int h = 512;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final short[] bufferValues = new short[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (65536 /
        // 1024));
        bufferValues[j + (i * h)] = (short) (Math.random() * 65535);
    }
    DataBuffer imageBuffer = new DataBufferUShort(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 + "_gray16", bi, true, lossLessQuality);
    write(outputFileName + "_gray16", bi, false, lossLessQuality);
    write(outputFileName + "_gray16", bi, true, lossyQuality);
    write(outputFileName + "_gray16", bi, false, lossyQuality);
    write(outputFileName + "_JAI_gray16", bi, true, lossLessQuality, true);
    write(outputFileName + "_JAI_gray16", bi, false, lossLessQuality, true);
    write(outputFileName + "_JAI_subSampled_gray16", bi, true, lossyQuality, true, param);
    write(outputFileName + "_JAI_subSampled_gray16", 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) DataBufferUShort(java.awt.image.DataBufferUShort) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 69 with ColorSpace

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

the class TIFFBaseJPEGCompressor method encode.

public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
    if (this.JPEGWriter == null) {
        throw new IIOException("JPEG writer has not been initialized!");
    }
    if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
        throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
    }
    // Set the stream.
    ImageOutputStream ios;
    // usingCodecLib && !writeAbbreviatedStream
    long initialStreamPosition;
    if (usingCodecLib && !writeAbbreviatedStream) {
        ios = stream;
        initialStreamPosition = stream.getStreamPosition();
    } else {
        // is using a stream on the native side which cannot be reset.
        if (baos == null) {
            baos = new IIOByteArrayOutputStream();
        } else {
            baos.reset();
        }
        ios = new MemoryCacheImageOutputStream(baos);
        initialStreamPosition = 0L;
    }
    JPEGWriter.setOutput(ios);
    // Create a DataBuffer.
    DataBufferByte dbb;
    if (off == 0 || usingCodecLib) {
        dbb = new DataBufferByte(b, b.length);
    } else {
        // 
        // Workaround for bug in core Java Image I/O JPEG
        // ImageWriter which cannot handle non-zero offsets.
        // 
        int bytesPerSegment = scanlineStride * height;
        byte[] btmp = new byte[bytesPerSegment];
        System.arraycopy(b, off, btmp, 0, bytesPerSegment);
        dbb = new DataBufferByte(btmp, bytesPerSegment);
        off = 0;
    }
    // Set up the ColorSpace.
    int[] offsets;
    ColorSpace cs;
    if (bitsPerSample.length == 3) {
        offsets = new int[] { off, off + 1, off + 2 };
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    } else {
        offsets = new int[] { off };
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    }
    // Create the ColorModel.
    ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    // Create the SampleModel.
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
    // Create the WritableRaster.
    WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
    // Create the BufferedImage.
    BufferedImage bi = new BufferedImage(cm, wras, false, null);
    // Get the pruned JPEG image metadata (may be null).
    IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
    // Compress the image into the output stream.
    int compDataLength;
    if (usingCodecLib && !writeAbbreviatedStream) {
        // Write complete JPEG stream
        JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
    } else {
        if (writeAbbreviatedStream) {
            // Write abbreviated JPEG stream
            // First write the tables-only data.
            JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
            ios.flush();
            // Rewind to the beginning of the byte array.
            baos.reset();
            // Write the abbreviated image data.
            IIOImage image = new IIOImage(bi, null, imageMetadata);
            JPEGWriter.writeToSequence(image, JPEGParam);
            JPEGWriter.endWriteSequence();
        } else {
            // Write complete JPEG stream
            JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        }
        compDataLength = baos.size();
        baos.writeTo(stream);
        baos.reset();
    }
    return compDataLength;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 70 with ColorSpace

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

the class JP2KKakaduImageReader method parseBoxes.

/**
 * Get basic image properties by querying several JP2Boxes. Then, properly
 * set the ColorModel of the input object.
 *
 * @param codestreamP
 */
private void parseBoxes(JP2KCodestreamProperties codestreamP) {
    if (isRawSource)
        return;
    short numComp = 1;
    byte[] bitDepths = null;
    byte[] maps = null;
    int bitDepth = -1;
    ICC_Profile profile = null;
    int colorSpaceType = -1;
    // //
    // 
    // ImageHeader Box
    // 
    // //
    final ImageHeaderBox ihBox = (ImageHeaderBox) getJp2Box(ImageHeaderBox.BOX_TYPE);
    if (ihBox != null) {
        numComp = ihBox.getNumComponents();
        bitDepth = ihBox.getBitDepth();
    }
    // //
    // 
    // ColorSpecification Box
    // 
    // //
    final ColorSpecificationBox csBox = (ColorSpecificationBox) getJp2Box(ColorSpecificationBox.BOX_TYPE);
    if (csBox != null) {
        profile = csBox.getICCProfile();
        colorSpaceType = csBox.getEnumeratedColorSpace();
    }
    // //
    // 
    // ComponentMapping Box
    // 
    // //
    final ComponentMappingBox cmBox = (ComponentMappingBox) getJp2Box(ComponentMappingBox.BOX_TYPE);
    if (cmBox != null) {
        maps = cmBox.getComponentAssociation();
    }
    // //
    // 
    // Palette Box
    // 
    // //
    final PaletteBox palBox = (PaletteBox) getJp2Box(PaletteBox.BOX_TYPE);
    if (palBox != null) {
        byte[][] lookUpTable = palBox.getLUT();
        if (lookUpTable != null && numComp == 1) {
            int tableComps = lookUpTable.length;
            int maxDepth = 1 + (bitDepth & 0x7F);
            if (maps == null) {
                maps = new byte[tableComps];
                for (int i = 0; i < tableComps; i++) maps[i] = (byte) i;
            }
            if (tableComps == 3) {
                codestreamP.setColorModel(new IndexColorModel(maxDepth, lookUpTable[0].length, lookUpTable[maps[0]], lookUpTable[maps[1]], lookUpTable[maps[2]]));
                return;
            } else if (tableComps == 4) {
                codestreamP.setColorModel(new IndexColorModel(maxDepth, lookUpTable[0].length, lookUpTable[maps[0]], lookUpTable[maps[1]], lookUpTable[maps[2]], lookUpTable[maps[3]]));
                return;
            }
        }
    }
    // //
    // 
    // BitsPerComponent Box
    // 
    // //
    final BitsPerComponentBox bpcBox = (BitsPerComponentBox) getJp2Box(BitsPerComponentBox.BOX_TYPE);
    if (bpcBox != null) {
        bitDepths = bpcBox.getBitDepth();
    }
    // //
    // 
    // ChannelDefinition Box
    // 
    // //
    final ChannelDefinitionBox chBox = (ChannelDefinitionBox) getJp2Box(ChannelDefinitionBox.BOX_TYPE);
    if (chBox != null) {
        final short[] channels = chBox.getChannel();
        final short[] associations = chBox.getAssociation();
        final int[] cType = chBox.getTypes();
        boolean hasAlpha = false;
        final int alphaChannel = numComp - 1;
        for (int i = 0; i < channels.length; i++) {
            if (cType[i] == 1 && channels[i] == alphaChannel)
                hasAlpha = true;
        }
        boolean[] isPremultiplied = new boolean[] { false };
        if (hasAlpha) {
            isPremultiplied = new boolean[alphaChannel];
            for (int i = 0; i < alphaChannel; i++) isPremultiplied[i] = false;
            for (int i = 0; i < channels.length; i++) {
                if (cType[i] == 2)
                    isPremultiplied[associations[i] - 1] = true;
            }
            for (int i = 1; i < alphaChannel; i++) isPremultiplied[0] &= isPremultiplied[i];
        }
        ColorSpace cs = null;
        // RGBN Workaround
        if (associations.length == 4) /* && associations[0]==1 && associations[1]==2 && associations[2]==3*/
        {
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            hasAlpha = true;
        } else if (profile != null)
            cs = new ICC_ColorSpace(profile);
        else if (colorSpaceType == ColorSpecificationBox.ECS_sRGB)
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        else if (colorSpaceType == ColorSpecificationBox.ECS_GRAY)
            cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        else if (colorSpaceType == ColorSpecificationBox.ECS_YCC)
            cs = ColorSpace.getInstance(ColorSpace.CS_PYCC);
        else
            LOGGER.warning("JP2 type only handle sRGB, GRAY and YCC Profiles");
        // TODO: Check these settings
        int[] bits = new int[numComp];
        for (int i = 0; i < numComp; i++) if (bitDepths != null)
            bits[i] = (bitDepths[i] & 0x7F) + 1;
        else
            bits[i] = (bitDepth & 0x7F) + 1;
        int maxBitDepth = 1 + (bitDepth & 0x7F);
        boolean isSigned = (bitDepth & 0x80) == 0x80;
        if (bitDepths != null)
            isSigned = (bitDepths[0] & 0x80) == 0x80;
        if (bitDepths != null)
            for (int i = 0; i < numComp; i++) if (bits[i] > maxBitDepth)
                maxBitDepth = bits[i];
        int type = -1;
        if (maxBitDepth <= 8)
            type = DataBuffer.TYPE_BYTE;
        else if (maxBitDepth <= 16)
            type = isSigned ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
        else if (maxBitDepth <= 32)
            type = DataBuffer.TYPE_INT;
        if (type == -1)
            return;
        if (cs != null) {
            codestreamP.setColorModel(new ComponentColorModel(cs, bits, hasAlpha, isPremultiplied[0], hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, type));
        }
    }
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) PaletteBox(it.geosolutions.imageio.plugins.jp2k.box.PaletteBox) ColorSpecificationBox(it.geosolutions.imageio.plugins.jp2k.box.ColorSpecificationBox) ChannelDefinitionBox(it.geosolutions.imageio.plugins.jp2k.box.ChannelDefinitionBox) BitsPerComponentBox(it.geosolutions.imageio.plugins.jp2k.box.BitsPerComponentBox) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ImageHeaderBox(it.geosolutions.imageio.plugins.jp2k.box.ImageHeaderBox) ComponentMappingBox(it.geosolutions.imageio.plugins.jp2k.box.ComponentMappingBox) ICC_Profile(java.awt.color.ICC_Profile) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

ColorSpace (java.awt.color.ColorSpace)86 ColorModel (java.awt.image.ColorModel)33 BufferedImage (java.awt.image.BufferedImage)30 ComponentColorModel (java.awt.image.ComponentColorModel)29 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)24 IndexColorModel (java.awt.image.IndexColorModel)17 SampleModel (java.awt.image.SampleModel)17 WritableRaster (java.awt.image.WritableRaster)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)10 DirectColorModel (java.awt.image.DirectColorModel)10 Point (java.awt.Point)8 DataBuffer (java.awt.image.DataBuffer)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)8 DataBufferByte (java.awt.image.DataBufferByte)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Test (org.junit.Test)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4