Search in sources :

Example 91 with SampleModel

use of java.awt.image.SampleModel in project bioformats by openmicroscopy.

the class UnsignedIntColorModel method createCompatibleWritableRaster.

/* @see java.awt.image.ColorModel#createCompatibleWritableRaster(int, int) */
@Override
public WritableRaster createCompatibleWritableRaster(int w, int h) {
    int[] bandOffsets = new int[nChannels];
    for (int i = 0; i < nChannels; i++) bandOffsets[i] = i;
    SampleModel m = new ComponentSampleModel(DataBuffer.TYPE_INT, w, h, nChannels, w * nChannels, bandOffsets);
    DataBuffer db = new DataBufferInt(w * h, nChannels);
    return Raster.createWritableRaster(m, db, null);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBuffer(java.awt.image.DataBuffer)

Example 92 with SampleModel

use of java.awt.image.SampleModel in project quick-media by liuyueyi.

the class PNGImageEncoder method encode.

/**
 * This method encodes a <code>RenderedImage</code> into PNG.
 * The stream into which the PNG is dumped is not closed at
 * the end of the operation, this should be done if needed
 * by the caller of this method.
 */
public void encode(RenderedImage im) throws IOException {
    this.image = im;
    this.width = image.getWidth();
    this.height = image.getHeight();
    SampleModel sampleModel = image.getSampleModel();
    int[] sampleSize = sampleModel.getSampleSize();
    // Set bitDepth to a sentinel value
    this.bitDepth = -1;
    this.bitShift = 0;
    // Allow user to override the bit depth of gray images
    if (param instanceof PNGEncodeParam.Gray) {
        PNGEncodeParam.Gray paramg = (PNGEncodeParam.Gray) param;
        if (paramg.isBitDepthSet()) {
            this.bitDepth = paramg.getBitDepth();
        }
        if (paramg.isBitShiftSet()) {
            this.bitShift = paramg.getBitShift();
        }
    }
    // Get bit depth from image if not set in param
    if (this.bitDepth == -1) {
        // Get bit depth from channel 0 of the image
        this.bitDepth = sampleSize[0];
        // Ensure all channels have the same bit depth
        for (int i = 1; i < sampleSize.length; i++) {
            if (sampleSize[i] != bitDepth) {
                throw new RuntimeException();
            }
        }
        // Round bit depth up to a power of 2
        if (bitDepth > 2 && bitDepth < 4) {
            bitDepth = 4;
        } else if (bitDepth > 4 && bitDepth < 8) {
            bitDepth = 8;
        } else if (bitDepth > 8 && bitDepth < 16) {
            bitDepth = 16;
        } else if (bitDepth > 16) {
            throw new RuntimeException();
        }
    }
    this.numBands = sampleModel.getNumBands();
    this.bpp = numBands * ((bitDepth == 16) ? 2 : 1);
    ColorModel colorModel = image.getColorModel();
    if (colorModel instanceof IndexColorModel) {
        if (bitDepth < 1 || bitDepth > 8) {
            throw new RuntimeException();
        }
        if (sampleModel.getNumBands() != 1) {
            throw new RuntimeException();
        }
        IndexColorModel icm = (IndexColorModel) colorModel;
        int size = icm.getMapSize();
        redPalette = new byte[size];
        greenPalette = new byte[size];
        bluePalette = new byte[size];
        alphaPalette = new byte[size];
        icm.getReds(redPalette);
        icm.getGreens(greenPalette);
        icm.getBlues(bluePalette);
        icm.getAlphas(alphaPalette);
        this.bpp = 1;
        if (param == null) {
            param = createGrayParam(redPalette, greenPalette, bluePalette, alphaPalette);
        }
        // If param is still null, it can't be expressed as gray
        if (param == null) {
            param = new PNGEncodeParam.Palette();
        }
        if (param instanceof PNGEncodeParam.Palette) {
            // If palette not set in param, create one from the ColorModel.
            PNGEncodeParam.Palette parami = (PNGEncodeParam.Palette) param;
            if (parami.isPaletteSet()) {
                int[] palette = parami.getPalette();
                size = palette.length / 3;
                int index = 0;
                for (int i = 0; i < size; i++) {
                    redPalette[i] = (byte) palette[index++];
                    greenPalette[i] = (byte) palette[index++];
                    bluePalette[i] = (byte) palette[index++];
                    alphaPalette[i] = (byte) 255;
                }
            }
            this.colorType = PNG_COLOR_PALETTE;
        } else if (param instanceof PNGEncodeParam.Gray) {
            redPalette = greenPalette = bluePalette = alphaPalette = null;
            this.colorType = PNG_COLOR_GRAY;
        } else {
            throw new RuntimeException();
        }
    } else if (numBands == 1) {
        if (param == null) {
            param = new PNGEncodeParam.Gray();
        }
        this.colorType = PNG_COLOR_GRAY;
    } else if (numBands == 2) {
        if (param == null) {
            param = new PNGEncodeParam.Gray();
        }
        if (param.isTransparencySet()) {
            skipAlpha = true;
            numBands = 1;
            if ((sampleSize[0] == 8) && (bitDepth < 8)) {
                compressGray = true;
            }
            bpp = (bitDepth == 16) ? 2 : 1;
            this.colorType = PNG_COLOR_GRAY;
        } else {
            if (this.bitDepth < 8) {
                this.bitDepth = 8;
            }
            this.colorType = PNG_COLOR_GRAY_ALPHA;
        }
    } else if (numBands == 3) {
        if (param == null) {
            param = new PNGEncodeParam.RGB();
        }
        this.colorType = PNG_COLOR_RGB;
    } else if (numBands == 4) {
        if (param == null) {
            param = new PNGEncodeParam.RGB();
        }
        if (param.isTransparencySet()) {
            skipAlpha = true;
            numBands = 3;
            bpp = (bitDepth == 16) ? 6 : 3;
            this.colorType = PNG_COLOR_RGB;
        } else {
            this.colorType = PNG_COLOR_RGB_ALPHA;
        }
    }
    interlace = param.getInterlacing();
    writeMagic();
    writeIHDR();
    writeCHRM();
    writeGAMA();
    writeICCP();
    writeSBIT();
    writeSRGB();
    writePLTE();
    writeHIST();
    writeTRNS();
    writeBKGD();
    writePHYS();
    writeSPLT();
    writeTIME();
    writeTEXT();
    writeZTXT();
    writePrivateChunks();
    writeIDAT();
    writeIEND();
    dataOutput.flush();
    dataOutput.close();
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 93 with SampleModel

use of java.awt.image.SampleModel in project quick-media by liuyueyi.

the class TIFFTranscoderImageIOWriteAdapter method writeImage.

/**
 * @throws TranscoderException
 * @see org.apache.batik.transcoder.image.TIFFTranscoder.WriteAdapter#writeImage(TIFFTranscoder, java.awt.image.BufferedImage, org.apache.batik.transcoder.TranscoderOutput)
 */
public void writeImage(TIFFTranscoder transcoder, BufferedImage img, TranscoderOutput output) throws TranscoderException {
    TranscodingHints hints = transcoder.getTranscodingHints();
    ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor("image/tiff");
    ImageWriterParams params = new ImageWriterParams();
    float PixSzMM = transcoder.getUserAgent().getPixelUnitToMillimeter();
    int PixSzInch = (int) (25.4 / PixSzMM + 0.5);
    params.setResolution(PixSzInch);
    if (hints.containsKey(TIFFTranscoder.KEY_COMPRESSION_METHOD)) {
        String method = (String) hints.get(TIFFTranscoder.KEY_COMPRESSION_METHOD);
        // Values set here as defined in TIFFImageWriteParam of JAI Image I/O Tools
        if ("packbits".equals(method)) {
            params.setCompressionMethod("PackBits");
        } else if ("deflate".equals(method)) {
            params.setCompressionMethod("Deflate");
        } else if ("lzw".equals(method)) {
            params.setCompressionMethod("LZW");
        } else if ("jpeg".equals(method)) {
            params.setCompressionMethod("JPEG");
        } else {
        // nop
        }
    }
    try {
        OutputStream ostream = output.getOutputStream();
        int w = img.getWidth();
        int h = img.getHeight();
        SinglePixelPackedSampleModel sppsm;
        sppsm = (SinglePixelPackedSampleModel) img.getSampleModel();
        int bands = sppsm.getNumBands();
        int[] off = new int[bands];
        for (int i = 0; i < bands; i++) off[i] = i;
        SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
        RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
        writer.writeImage(rimg, ostream, params);
        ostream.flush();
    } catch (IOException ex) {
        throw new TranscoderException(ex);
    }
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) OutputStream(java.io.OutputStream) ImageWriter(org.apache.batik.ext.awt.image.spi.ImageWriter) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) IOException(java.io.IOException) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) TranscodingHints(org.apache.batik.transcoder.TranscodingHints) ImageWriterParams(org.apache.batik.ext.awt.image.spi.ImageWriterParams) FormatRed(org.apache.batik.ext.awt.image.rendered.FormatRed) TranscoderException(org.apache.batik.transcoder.TranscoderException) RenderedImage(java.awt.image.RenderedImage)

Example 94 with SampleModel

use of java.awt.image.SampleModel in project quick-media by liuyueyi.

the class PNGEncodeParam method getDefaultEncodeParam.

/**
 * Returns an instance of <code>PNGEncodeParam.Palette</code>,
 * <code>PNGEncodeParam.Gray</code>, or
 * <code>PNGEncodeParam.RGB</code> appropriate for encoding
 * the given image.
 *
 * <p> If the image has an <code>IndexColorModel</code>, an
 * instance of <code>PNGEncodeParam.Palette</code> is returned.
 * Otherwise, if the image has 1 or 2 bands an instance of
 * <code>PNGEncodeParam.Gray</code> is returned.  In all other
 * cases an instance of <code>PNGEncodeParam.RGB</code> is
 * returned.
 *
 * <p> Note that this method does not provide any guarantee that
 * the given image will be successfully encoded by the PNG
 * encoder, as it only performs a very superficial analysis of
 * the image structure.
 */
public static PNGEncodeParam getDefaultEncodeParam(RenderedImage im) {
    ColorModel colorModel = im.getColorModel();
    if (colorModel instanceof IndexColorModel) {
        return new PNGEncodeParam.Palette();
    }
    SampleModel sampleModel = im.getSampleModel();
    int numBands = sampleModel.getNumBands();
    if (numBands == 1 || numBands == 2) {
        return new PNGEncodeParam.Gray();
    } else {
        return new PNGEncodeParam.RGB();
    }
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 95 with SampleModel

use of java.awt.image.SampleModel in project quick-media by liuyueyi.

the class SimpleRenderedImage method getData.

/**
 * Returns an arbitrary rectangular region of the RenderedImage
 * in a Raster.  The rectangle of interest will be clipped against
 * the image bounds.
 *
 * <p> The returned Raster is semantically a copy.  This means
 * that updates to the source image will not be reflected in the
 * returned Raster.  For non-writable (immutable) source images,
 * the returned value may be a reference to the image's internal
 * data.  The returned Raster should be considered non-writable;
 * any attempt to alter its pixel data (such as by casting it to
 * WritableRaster or obtaining and modifying its DataBuffer) may
 * result in undefined behavior.  The copyData method should be
 * used if the returned Raster is to be modified.
 *
 * @param bounds the region of the RenderedImage to be returned.
 */
public Raster getData(Rectangle bounds) {
    int startX = XToTileX(bounds.x);
    int startY = YToTileY(bounds.y);
    int endX = XToTileX(bounds.x + bounds.width - 1);
    int endY = YToTileY(bounds.y + bounds.height - 1);
    Raster tile;
    if ((startX == endX) && (startY == endY)) {
        tile = getTile(startX, startY);
        return tile.createChild(bounds.x, bounds.y, bounds.width, bounds.height, bounds.x, bounds.y, null);
    } else {
        // Create a WritableRaster of the desired size
        SampleModel sm = sampleModel.createCompatibleSampleModel(bounds.width, bounds.height);
        // Translate it
        WritableRaster dest = Raster.createWritableRaster(sm, bounds.getLocation());
        for (int j = startY; j <= endY; j++) {
            for (int i = startX; i <= endX; i++) {
                tile = getTile(i, j);
                Rectangle intersectRect = bounds.intersection(tile.getBounds());
                Raster liveRaster = tile.createChild(intersectRect.x, intersectRect.y, intersectRect.width, intersectRect.height, intersectRect.x, intersectRect.y, null);
                dest.setDataElements(0, 0, liveRaster);
            }
        }
        return dest;
    }
}
Also used : SampleModel(java.awt.image.SampleModel) WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) Point(java.awt.Point)

Aggregations

SampleModel (java.awt.image.SampleModel)97 ColorModel (java.awt.image.ColorModel)43 ComponentSampleModel (java.awt.image.ComponentSampleModel)38 Point (java.awt.Point)33 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)33 DataBuffer (java.awt.image.DataBuffer)32 Rectangle (java.awt.Rectangle)30 IndexColorModel (java.awt.image.IndexColorModel)29 BufferedImage (java.awt.image.BufferedImage)27 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)26 WritableRaster (java.awt.image.WritableRaster)26 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)23 DataBufferByte (java.awt.image.DataBufferByte)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ComponentColorModel (java.awt.image.ComponentColorModel)17 DataBufferInt (java.awt.image.DataBufferInt)17 ColorSpace (java.awt.color.ColorSpace)15 Raster (java.awt.image.Raster)15 DataBufferUShort (java.awt.image.DataBufferUShort)13 BandedSampleModel (java.awt.image.BandedSampleModel)12