Search in sources :

Example 76 with ColorModel

use of java.awt.image.ColorModel in project imageio-ext by geosolutions-it.

the class TIFFDecompressor method getRawImageTypeSpecifier.

/**
 * A utility method that returns an
 * <code>ImageTypeSpecifier</code> suitable for decoding an image
 * with the given parameters.
 *
 * @param photometricInterpretation the value of the
 * <code>PhotometricInterpretation</code> field.
 * @param compression the value of the <code>Compression</code> field.
 * @param samplesPerPixel the value of the
 * <code>SamplesPerPixel</code> field.
 * @param bitsPerSample the value of the <code>BitsPerSample</code> field.
 * @param sampleFormat the value of the <code>SampleFormat</code> field.
 * @param extraSamples the value of the <code>ExtraSamples</code> field.
 * @param colorMap the value of the <code>ColorMap</code> field.
 *
 * @return a suitable <code>ImageTypeSpecifier</code>, or
 * <code>null</code> if it is not possible to create one.
 */
public static ImageTypeSpecifier getRawImageTypeSpecifier(int photometricInterpretation, int compression, int samplesPerPixel, int[] bitsPerSample, int[] sampleFormat, int[] extraSamples, char[] colorMap) {
    if (DEBUG) {
        System.out.println("\n ---- samplesPerPixel = " + samplesPerPixel + "\n ---- bitsPerSample[0] = " + bitsPerSample[0] + "\n ---- sampleFormat[0] = " + sampleFormat[0]);
    }
    // 1, 2, 4, 8, or 16 bit grayscale or indexed images
    if (samplesPerPixel == 1 && (bitsPerSample[0] == 1 || bitsPerSample[0] == 2 || bitsPerSample[0] == 4 || bitsPerSample[0] == 8 || bitsPerSample[0] == 16)) {
        if (colorMap == null) {
            // Grayscale
            boolean isSigned = (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER);
            int dataType;
            if (bitsPerSample[0] <= 8) {
                dataType = DataBuffer.TYPE_BYTE;
            } else {
                dataType = sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
            }
            return ImageTypeSpecifier.createGrayscale(bitsPerSample[0], dataType, isSigned);
        } else {
            // Indexed
            int mapSize = 1 << bitsPerSample[0];
            byte[] redLut = new byte[mapSize];
            byte[] greenLut = new byte[mapSize];
            byte[] blueLut = new byte[mapSize];
            byte[] alphaLut = null;
            int idx = 0;
            for (int i = 0; i < mapSize; i++) {
                redLut[i] = (byte) ((colorMap[i] * 255) / 65535);
                greenLut[i] = (byte) ((colorMap[mapSize + i] * 255) / 65535);
                blueLut[i] = (byte) ((colorMap[2 * mapSize + i] * 255) / 65535);
            }
            int dataType = bitsPerSample[0] <= 8 ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT;
            return ImageTypeSpecifier.createIndexed(redLut, greenLut, blueLut, alphaLut, bitsPerSample[0], dataType);
        }
    }
    // 8-bit gray-alpha
    if (samplesPerPixel == 2 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8) {
        int dataType = DataBuffer.TYPE_BYTE;
        boolean alphaPremultiplied = false;
        if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
            alphaPremultiplied = true;
        }
        // System.out.println("alphaPremultiplied = "+alphaPremultiplied);//XXX
        return ImageTypeSpecifier.createGrayscale(8, dataType, false, alphaPremultiplied);
    }
    // 16-bit gray-alpha
    if (samplesPerPixel == 2 && bitsPerSample[0] == 16 && bitsPerSample[1] == 16) {
        int dataType = sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
        boolean alphaPremultiplied = false;
        if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
            alphaPremultiplied = true;
        }
        // System.out.println("alphaPremultiplied = "+alphaPremultiplied);//XXX
        boolean isSigned = dataType == DataBuffer.TYPE_SHORT;
        return ImageTypeSpecifier.createGrayscale(16, dataType, isSigned, alphaPremultiplied);
    }
    ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    // 8-bit RGB
    if (samplesPerPixel == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) {
        int[] bandOffsets = new int[3];
        bandOffsets[0] = 0;
        bandOffsets[1] = 1;
        bandOffsets[2] = 2;
        int dataType = DataBuffer.TYPE_BYTE;
        ColorSpace theColorSpace;
        if ((photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_Y_CB_CR && compression != BaselineTIFFTagSet.COMPRESSION_JPEG && compression != BaselineTIFFTagSet.COMPRESSION_OLD_JPEG) || photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CIELAB) {
            theColorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
        } else {
            theColorSpace = rgb;
        }
        return ImageTypeSpecifier.createInterleaved(theColorSpace, bandOffsets, dataType, false, false);
    }
    // 8-bit RGBA
    if (samplesPerPixel == 4 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8 && bitsPerSample[3] == 8) {
        int[] bandOffsets = new int[4];
        bandOffsets[0] = 0;
        bandOffsets[1] = 1;
        bandOffsets[2] = 2;
        bandOffsets[3] = 3;
        int dataType = DataBuffer.TYPE_BYTE;
        ColorSpace theColorSpace;
        boolean hasAlpha;
        boolean alphaPremultiplied = false;
        if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CMYK) {
            theColorSpace = SimpleCMYKColorSpace.getInstance();
            hasAlpha = false;
        } else {
            theColorSpace = rgb;
            hasAlpha = true;
            if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                alphaPremultiplied = true;
            }
        }
        return ImageTypeSpecifier.createInterleaved(theColorSpace, bandOffsets, dataType, hasAlpha, alphaPremultiplied);
    }
    // 16-bit RGB
    if (samplesPerPixel == 3 && bitsPerSample[0] == 16 && bitsPerSample[1] == 16 && bitsPerSample[2] == 16) {
        int[] bandOffsets = new int[3];
        bandOffsets[0] = 0;
        bandOffsets[1] = 1;
        bandOffsets[2] = 2;
        int dataType = sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
        return ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, false, false);
    }
    // 16-bit RGBA
    if (samplesPerPixel == 4 && bitsPerSample[0] == 16 && bitsPerSample[1] == 16 && bitsPerSample[2] == 16 && bitsPerSample[3] == 16) {
        int[] bandOffsets = new int[4];
        bandOffsets[0] = 0;
        bandOffsets[1] = 1;
        bandOffsets[2] = 2;
        bandOffsets[3] = 3;
        int dataType = sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
        boolean alphaPremultiplied = false;
        if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
            alphaPremultiplied = true;
        }
        return ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, true, alphaPremultiplied);
    }
    // in more than 1 channel
    if ((photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CMYK) && (bitsPerSample[0] == 1 || bitsPerSample[0] == 2 || bitsPerSample[0] == 4)) {
        ColorSpace cs = null;
        if (samplesPerPixel == 4)
            cs = SimpleCMYKColorSpace.getInstance();
        else
            cs = new BogusColorSpace(samplesPerPixel);
        // By specifying the bits per sample the color values
        // will scale on display
        ColorModel cm = new ComponentColorModel(cs, bitsPerSample, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        return new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1));
    }
    // Compute bits per pixel.
    int totalBits = 0;
    for (int i = 0; i < bitsPerSample.length; i++) {
        totalBits += bitsPerSample[i];
    }
    // Packed: 3- or 4-band, 8- or 16-bit.
    if ((samplesPerPixel == 3 || samplesPerPixel == 4) && (totalBits == 8 || totalBits == 16)) {
        int redMask = createMask(bitsPerSample, 0);
        int greenMask = createMask(bitsPerSample, 1);
        int blueMask = createMask(bitsPerSample, 2);
        int alphaMask = (samplesPerPixel == 4) ? createMask(bitsPerSample, 3) : 0;
        int transferType = totalBits == 8 ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT;
        boolean alphaPremultiplied = false;
        if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
            alphaPremultiplied = true;
        }
        return ImageTypeSpecifier.createPacked(rgb, redMask, greenMask, blueMask, alphaMask, transferType, alphaPremultiplied);
    }
    // Generic components with 8X bits per sample.
    if (bitsPerSample[0] % 8 == 0) {
        // Check whether all bands have same bit depth.
        boolean allSameBitDepth = true;
        for (int i = 1; i < bitsPerSample.length; i++) {
            if (bitsPerSample[i] != bitsPerSample[i - 1]) {
                allSameBitDepth = false;
                break;
            }
        }
        // Proceed if all bands have same bit depth.
        if (allSameBitDepth) {
            // Determine the data type.
            int dataType = -1;
            boolean isDataTypeSet = false;
            switch(bitsPerSample[0]) {
                case 8:
                    if (sampleFormat[0] != BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
                        // Ignore whether signed or unsigned:
                        // treat all as unsigned.
                        dataType = DataBuffer.TYPE_BYTE;
                        isDataTypeSet = true;
                    }
                    break;
                case 16:
                    if (sampleFormat[0] != BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
                        if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER) {
                            dataType = DataBuffer.TYPE_SHORT;
                        } else {
                            dataType = DataBuffer.TYPE_USHORT;
                        }
                        isDataTypeSet = true;
                    }
                    break;
                case 32:
                    if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
                        dataType = DataBuffer.TYPE_FLOAT;
                    } else {
                        dataType = DataBuffer.TYPE_INT;
                    }
                    isDataTypeSet = true;
                    break;
                case 64:
                    if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
                        dataType = DataBuffer.TYPE_DOUBLE;
                    } else
                        throw new IllegalStateException("Unsupported sample format for 64 bits data.");
                    isDataTypeSet = true;
                    break;
            }
            if (isDataTypeSet) {
                // Create the SampleModel.
                SampleModel sm = createInterleavedSM(dataType, samplesPerPixel);
                // Create the ColorModel.
                ColorModel cm;
                if (samplesPerPixel >= 1 && samplesPerPixel <= 4 && (dataType == DataBuffer.TYPE_INT || dataType == DataBuffer.TYPE_FLOAT)) {
                    // Handle the 32-bit cases for 1-4 bands.
                    ColorSpace cs = samplesPerPixel <= 2 ? ColorSpace.getInstance(ColorSpace.CS_GRAY) : rgb;
                    boolean hasAlpha = ((samplesPerPixel % 2) == 0);
                    boolean alphaPremultiplied = false;
                    if (hasAlpha && extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                        alphaPremultiplied = true;
                    }
                    cm = createComponentCM(cs, samplesPerPixel, dataType, hasAlpha, alphaPremultiplied);
                } else {
                    ColorSpace cs = new BogusColorSpace(samplesPerPixel);
                    cm = createComponentCM(cs, samplesPerPixel, dataType, // hasAlpha
                    false, // alphaPremultiplied
                    false);
                }
                // System.out.println(cm); // XXX
                return new ImageTypeSpecifier(cm, sm);
            }
        }
    }
    if (colorMap == null && sampleFormat[0] != BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
        // Determine size of largest sample.
        int maxBitsPerSample = 0;
        for (int i = 0; i < bitsPerSample.length; i++) {
            if (bitsPerSample[i] > maxBitsPerSample) {
                maxBitsPerSample = bitsPerSample[i];
            }
        }
        // Determine whether data are signed.
        boolean isSigned = (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER);
        // Grayscale
        if (samplesPerPixel == 1) {
            int dataType = getDataTypeFromNumBits(maxBitsPerSample, isSigned);
            return ImageTypeSpecifier.createGrayscale(maxBitsPerSample, dataType, isSigned);
        }
        // Gray-alpha
        if (samplesPerPixel == 2) {
            boolean alphaPremultiplied = false;
            if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                alphaPremultiplied = true;
            }
            int dataType = getDataTypeFromNumBits(maxBitsPerSample, isSigned);
            return ImageTypeSpecifier.createGrayscale(maxBitsPerSample, dataType, false, alphaPremultiplied);
        }
        if (samplesPerPixel == 3 || samplesPerPixel == 4) {
            if (totalBits <= 32 && !isSigned) {
                // Packed RGB or RGBA
                int redMask = createMask(bitsPerSample, 0);
                int greenMask = createMask(bitsPerSample, 1);
                int blueMask = createMask(bitsPerSample, 2);
                int alphaMask = (samplesPerPixel == 4) ? createMask(bitsPerSample, 3) : 0;
                int transferType = getDataTypeFromNumBits(totalBits, false);
                boolean alphaPremultiplied = false;
                if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                    alphaPremultiplied = true;
                }
                return ImageTypeSpecifier.createPacked(rgb, redMask, greenMask, blueMask, alphaMask, transferType, alphaPremultiplied);
            } else if (samplesPerPixel == 3) {
                // Interleaved RGB
                int[] bandOffsets = new int[] { 0, 1, 2 };
                int dataType = getDataTypeFromNumBits(maxBitsPerSample, isSigned);
                return ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, false, false);
            } else if (samplesPerPixel == 4) {
                // Interleaved RGBA
                int[] bandOffsets = new int[] { 0, 1, 2, 3 };
                int dataType = getDataTypeFromNumBits(maxBitsPerSample, isSigned);
                boolean alphaPremultiplied = false;
                if (extraSamples != null && extraSamples[0] == BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                    alphaPremultiplied = true;
                }
                return ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, dataType, true, alphaPremultiplied);
            }
        } else {
            // Arbitrary Interleaved.
            int dataType = getDataTypeFromNumBits(maxBitsPerSample, isSigned);
            SampleModel sm = createInterleavedSM(dataType, samplesPerPixel);
            ColorSpace cs = new BogusColorSpace(samplesPerPixel);
            ColorModel cm = createComponentCM(cs, samplesPerPixel, dataType, // hasAlpha
            false, // alphaPremultiplied
            false);
            return new ImageTypeSpecifier(cm, sm);
        }
    }
    if (DEBUG) {
        System.out.println("\nNo raw ITS available:");
        System.out.println("photometricInterpretation = " + photometricInterpretation);
        System.out.println("compression = " + compression);
        System.out.println("samplesPerPixel = " + samplesPerPixel);
        if (bitsPerSample != null) {
            for (int i = 0; i < bitsPerSample.length; i++) {
                System.out.println("bitsPerSample[" + i + "] = " + (int) bitsPerSample[i]);
            }
        }
        if (sampleFormat != null) {
            for (int i = 0; i < sampleFormat.length; i++) {
                System.out.println("sampleFormat[" + i + "] = " + (int) sampleFormat[i]);
            }
        }
        if (extraSamples != null) {
            for (int i = 0; i < extraSamples.length; i++) {
                System.out.println("extraSamples[" + i + "] = " + (int) extraSamples[i]);
            }
        }
        System.out.println("colorMap = " + colorMap);
        if (colorMap != null) {
            System.out.println("colorMap.length = " + colorMap.length);
        }
        throw new RuntimeException("Unable to create an ImageTypeSpecifier");
    }
    return null;
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) BogusColorSpace(com.sun.media.imageioimpl.common.BogusColorSpace) SimpleCMYKColorSpace(com.sun.media.imageioimpl.common.SimpleCMYKColorSpace) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) BogusColorSpace(com.sun.media.imageioimpl.common.BogusColorSpace) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 77 with ColorModel

use of java.awt.image.ColorModel in project imageio-ext by geosolutions-it.

the class TIFFDecompressor method createComponentCM.

/**
 * Create a <code>ComponentColorModel</code> for use in creating
 * an <code>ImageTypeSpecifier</code>.
 */
// This code was copied from javax.imageio.ImageTypeSpecifier and
// modified to support floating point data.
static ColorModel createComponentCM(ColorSpace colorSpace, int numBands, int dataType, boolean hasAlpha, boolean isAlphaPremultiplied) {
    int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
    ColorModel colorModel;
    if (dataType == DataBuffer.TYPE_FLOAT || dataType == DataBuffer.TYPE_DOUBLE) {
        colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, dataType);
    } else {
        int[] numBits = new int[numBands];
        int bits;
        if (dataType == DataBuffer.TYPE_BYTE) {
            bits = 8;
        } else if (dataType == DataBuffer.TYPE_SHORT || dataType == DataBuffer.TYPE_USHORT) {
            bits = 16;
        } else if (dataType == DataBuffer.TYPE_INT) {
            bits = 32;
        } else {
            throw new IllegalArgumentException("dataType = " + dataType);
        }
        for (int i = 0; i < numBands; i++) {
            numBits[i] = bits;
        }
        colorModel = new ComponentColorModel(colorSpace, numBits, hasAlpha, isAlphaPremultiplied, transparency, dataType);
    }
    return colorModel;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel)

Example 78 with ColorModel

use of java.awt.image.ColorModel in project imageio-ext by geosolutions-it.

the class PNGWriter method writePNG.

public RenderedImage writePNG(RenderedImage image, OutputStream outStream, float quality, FilterType filterType, Map<String, String> text) throws Exception {
    // compute the compression level similarly to what the Clib code does
    int level = Math.round(9 * (1f - quality));
    // get the optimal scanline provider for this image
    RenderedImage original = image;
    ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
    if (scanlines == null) {
        throw new IllegalArgumentException("Could not find a scanline extractor for " + original);
    }
    // encode using the PNGJ library and the GeoServer own scanline providers
    ColorModel colorModel = image.getColorModel();
    boolean indexed = colorModel instanceof IndexColorModel;
    ImageInfo ii = getImageInfo(image, scanlines, colorModel, indexed);
    PngWriter pw = new PngWriter(outStream, ii);
    pw.setShouldCloseStream(false);
    try {
        pw.setCompLevel(level);
        pw.setFilterType(filterType);
        ChunksListForWrite chunkList = pw.getChunksList();
        PngMetadata metadata = pw.getMetadata();
        if (indexed) {
            IndexColorModel icm = (IndexColorModel) colorModel;
            PngChunkPLTE palette = metadata.createPLTEChunk();
            int ncolors = icm.getMapSize();
            palette.setNentries(ncolors);
            for (int i = 0; i < ncolors; i++) {
                final int red = icm.getRed(i);
                final int green = icm.getGreen(i);
                final int blue = icm.getBlue(i);
                palette.setEntry(i, red, green, blue);
            }
            if (icm.hasAlpha()) {
                PngChunkTRNS transparent = new PngChunkTRNS(ii);
                int[] alpha = new int[ncolors];
                for (int i = 0; i < ncolors; i++) {
                    final int a = icm.getAlpha(i);
                    alpha[i] = a;
                }
                transparent.setPalletteAlpha(alpha);
                chunkList.queue(transparent);
            }
        }
        if (text != null && !text.isEmpty()) {
            Iterator<Entry<String, String>> entrySetIterator = text.entrySet().iterator();
            while (entrySetIterator.hasNext()) {
                Entry<String, String> entrySet = entrySetIterator.next();
                metadata.setText(entrySet.getKey(), entrySet.getValue(), true, false);
            }
        }
        // write out the actual image lines
        for (int row = 0; row < image.getHeight(); row++) {
            pw.writeRow(scanlines);
        }
        pw.end();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to encode the PNG", e);
        throw e;
    } finally {
        pw.close();
    }
    return image;
}
Also used : PngWriter(ar.com.hjg.pngj.PngWriter) PngMetadata(ar.com.hjg.pngj.chunks.PngMetadata) Entry(java.util.Map.Entry) ChunksListForWrite(ar.com.hjg.pngj.chunks.ChunksListForWrite) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) RenderedImage(java.awt.image.RenderedImage) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) IndexColorModel(java.awt.image.IndexColorModel)

Example 79 with ColorModel

use of java.awt.image.ColorModel in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method test8BitGray.

public static void test8BitGray() 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[] { 8 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    final int w = 128;
    final int h = 128;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final byte[] bufferValues = new byte[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (4096 /
        // 1024));
        bufferValues[j + (i * h)] = (byte) (Math.random() * 255d);
    }
    DataBuffer imageBuffer = new DataBufferByte(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    write(outputFileName + "_gray8", bi, true, lossLessQuality);
    write(outputFileName + "_gray8", bi, false, lossLessQuality);
    write(outputFileName + "_gray8", bi, true, lossyQuality);
    write(outputFileName + "_gray8", bi, false, lossyQuality);
    write(outputFileName + "_JAI_gray8", bi, true, lossLessQuality, true);
    write(outputFileName + "_JAI_gray8", bi, false, lossLessQuality, true);
    // write(outputFileName + "_JAI_gray12", bi, true, lossyQuality, true);
    // write(outputFileName + "_JAI_gray12", bi, false, lossyQuality, true);
    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) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 80 with ColorModel

use of java.awt.image.ColorModel in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method test12BitGray.

public static void test12BitGray() 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[] { 12 }, 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) * (4096 /
        // 1024));
        bufferValues[j + (i * h)] = (short) (Math.random() * 4095d);
    }
    DataBuffer imageBuffer = new DataBufferUShort(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    write(outputFileName + "_gray12", bi, true, lossLessQuality);
    write(outputFileName + "_gray12", bi, false, lossLessQuality);
    write(outputFileName + "_gray12", bi, true, lossyQuality);
    write(outputFileName + "_gray12", bi, false, lossyQuality);
    write(outputFileName + "_JAI_gray12", bi, true, lossLessQuality, true);
    write(outputFileName + "_JAI_gray12", bi, false, lossLessQuality, true);
    // write(outputFileName + "_JAI_gray12", bi, true, lossyQuality, true);
    // write(outputFileName + "_JAI_gray12", bi, false, lossyQuality, true);
    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)

Aggregations

ColorModel (java.awt.image.ColorModel)188 BufferedImage (java.awt.image.BufferedImage)92 IndexColorModel (java.awt.image.IndexColorModel)80 WritableRaster (java.awt.image.WritableRaster)57 ComponentColorModel (java.awt.image.ComponentColorModel)47 SampleModel (java.awt.image.SampleModel)40 DirectColorModel (java.awt.image.DirectColorModel)39 ColorSpace (java.awt.color.ColorSpace)28 Raster (java.awt.image.Raster)24 Rectangle (java.awt.Rectangle)21 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)20 Point (java.awt.Point)15 ComponentSampleModel (java.awt.image.ComponentSampleModel)15 Graphics2D (java.awt.Graphics2D)13 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)13 IOException (java.io.IOException)13 ColorConvertOp (java.awt.image.ColorConvertOp)12 DataBuffer (java.awt.image.DataBuffer)12 DataBufferInt (java.awt.image.DataBufferInt)11 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)11