Search in sources :

Example 96 with SampleModel

use of java.awt.image.SampleModel in project TrakEM2 by trakem2.

the class ImageSaver method createImage.

public static final BufferedImage createImage(final int[] pixels, final int width, final int height, final DirectColorModel cm) {
    WritableRaster wr = cm.createCompatibleWritableRaster(1, 1);
    SampleModel sm = wr.getSampleModel().createCompatibleSampleModel(width, height);
    DataBuffer dataBuffer = new DataBufferInt(pixels, width * height, 0);
    WritableRaster rgbRaster = Raster.createWritableRaster(sm, dataBuffer, null);
    return new BufferedImage(cm, rgbRaster, cm == RGBA_PRE_COLOR_MODEL, null);
}
Also used : SampleModel(java.awt.image.SampleModel) WritableRaster(java.awt.image.WritableRaster) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 97 with SampleModel

use of java.awt.image.SampleModel in project javacv by bytedeco.

the class Java2DFrameConverter method copy.

public static void copy(Frame frame, BufferedImage bufferedImage, double gamma, boolean flipChannels, Rectangle roi) {
    Buffer in = frame.image[0];
    int bufferIndex = roi == null ? 0 : roi.y * frame.imageStride + roi.x * frame.imageChannels;
    SampleModel sm = bufferedImage.getSampleModel();
    Raster r = bufferedImage.getRaster();
    DataBuffer out = r.getDataBuffer();
    int x = -r.getSampleModelTranslateX();
    int y = -r.getSampleModelTranslateY();
    int step = sm.getWidth() * sm.getNumBands();
    int channels = sm.getNumBands();
    if (sm instanceof ComponentSampleModel) {
        step = ((ComponentSampleModel) sm).getScanlineStride();
        channels = ((ComponentSampleModel) sm).getPixelStride();
    } else if (sm instanceof SinglePixelPackedSampleModel) {
        step = ((SinglePixelPackedSampleModel) sm).getScanlineStride();
        channels = 1;
    } else if (sm instanceof MultiPixelPackedSampleModel) {
        step = ((MultiPixelPackedSampleModel) sm).getScanlineStride();
        // ??
        channels = ((MultiPixelPackedSampleModel) sm).getPixelBitStride() / 8;
    }
    int start = y * step + x * channels;
    if (out instanceof DataBufferByte) {
        byte[] a = ((DataBufferByte) out).getData();
        flipCopyWithGamma((ByteBuffer) in, bufferIndex, frame.imageStride, ByteBuffer.wrap(a), start, step, false, gamma, false, flipChannels ? channels : 0);
    } else if (out instanceof DataBufferDouble) {
        double[] a = ((DataBufferDouble) out).getData();
        flipCopyWithGamma((DoubleBuffer) in, bufferIndex, frame.imageStride, DoubleBuffer.wrap(a), start, step, gamma, false, flipChannels ? channels : 0);
    } else if (out instanceof DataBufferFloat) {
        float[] a = ((DataBufferFloat) out).getData();
        flipCopyWithGamma((FloatBuffer) in, bufferIndex, frame.imageStride, FloatBuffer.wrap(a), start, step, gamma, false, flipChannels ? channels : 0);
    } else if (out instanceof DataBufferInt) {
        int[] a = ((DataBufferInt) out).getData();
        int stride = frame.imageStride;
        if (in instanceof ByteBuffer) {
            in = ((ByteBuffer) in).order(flipChannels ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN).asIntBuffer();
            stride /= 4;
        }
        flipCopyWithGamma((IntBuffer) in, bufferIndex, stride, IntBuffer.wrap(a), start, step, gamma, false, flipChannels ? channels : 0);
    } else if (out instanceof DataBufferShort) {
        short[] a = ((DataBufferShort) out).getData();
        flipCopyWithGamma((ShortBuffer) in, bufferIndex, frame.imageStride, ShortBuffer.wrap(a), start, step, true, gamma, false, flipChannels ? channels : 0);
    } else if (out instanceof DataBufferUShort) {
        short[] a = ((DataBufferUShort) out).getData();
        flipCopyWithGamma((ShortBuffer) in, bufferIndex, frame.imageStride, ShortBuffer.wrap(a), start, step, false, gamma, false, flipChannels ? channels : 0);
    } else {
        assert false;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) ByteBuffer(java.nio.ByteBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) DoubleBuffer(java.nio.DoubleBuffer) DataBuffer(java.awt.image.DataBuffer) DoubleBuffer(java.nio.DoubleBuffer) DataBufferDouble(java.awt.image.DataBufferDouble) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) IntBuffer(java.nio.IntBuffer) DataBufferFloat(java.awt.image.DataBufferFloat) DataBufferUShort(java.awt.image.DataBufferUShort) ShortBuffer(java.nio.ShortBuffer) DataBuffer(java.awt.image.DataBuffer)

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