Java Code Examples for java.awt.image.Raster#createWritableRaster()

The following examples show how to use java.awt.image.Raster#createWritableRaster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ICMColorDataTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void makeImage() {
    int scanLineBytes = WIDTH / PIXELS_IN_BYTE;
    if ((WIDTH & (PIXELS_IN_BYTE - 1)) != 0) {
        // Make sure all the pixels in a scan line fit
        scanLineBytes += 1;
    }

    byte[]     bits    = new byte[scanLineBytes * HEIGHT];
    DataBuffer dataBuf = new DataBufferByte(bits, bits.length, 0);
    SampleModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
                                                              WIDTH, HEIGHT, BITS_PER_PIXEL);
    WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuf, null);
    IndexColorModel indexModel = new IndexColorModel(2, 2, RED, GREEN, BLUE);
    BufferedImage bufImage = new BufferedImage(indexModel, raster,
                                               indexModel.isAlphaPremultiplied(), null);

    Graphics g = bufImage.getGraphics();
    g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
    g.dispose();
}
 
Example 2
Source File: SignedColorModel.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public WritableRaster createCompatibleWritableRaster(final int w,
	final int h)
{
	if (pixelBits == 16) {
		final int[] bandOffsets = new int[nChannels];
		for (int i = 0; i < nChannels; i++)
			bandOffsets[i] = i;

		final SampleModel m = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w,
			h, nChannels, w * nChannels, bandOffsets);
		final DataBuffer db = new DataBufferShort(w * h, nChannels);
		return Raster.createWritableRaster(m, db, null);
	}
	return helper.createCompatibleWritableRaster(w, h);
}
 
Example 3
Source File: TiledImageMock.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the tile at the given index without any verification. It is caller responsibility to verify if this
 * method is invoked in a consistent context (for example after a writable raster has been properly acquired).
 */
private WritableRaster tile(int tileX, int tileY) {
    if ((tileX -= minTileX) < 0 || tileX >= numXTiles ||
        (tileY -= minTileY) < 0 || tileY >= numYTiles)
    {
        throw new IndexOutOfBoundsException();
    }
    final int i = tileY * numXTiles + tileX;
    WritableRaster raster = tiles[i];
    if (raster == null) {
        tiles[i] = raster = Raster.createWritableRaster(sampleModel,
                new Point(tileX * tileWidth  + minX,
                          tileY * tileHeight + minY));
    }
    return raster;
}
 
Example 4
Source File: JFIFMarkerSegment.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    // read the palette
    byte [] palette = new byte [PALETTE_SIZE];
    float palettePart = ((float) PALETTE_SIZE) / getLength();
    readByteBuffer(iis,
                   palette,
                   reader,
                   palettePart,
                   0.0F);
    DataBufferByte buffer = new DataBufferByte(thumbWidth*thumbHeight);
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F-palettePart,
                   palettePart);
    iis.read();
    iis.reset();

    IndexColorModel cm = new IndexColorModel(8,
                                             256,
                                             palette,
                                             0,
                                             false);
    SampleModel sm = cm.createCompatibleSampleModel(thumbWidth,
                                                    thumbHeight);
    WritableRaster raster =
        Raster.createWritableRaster(sm, buffer, null);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}
 
Example 5
Source File: ColCvtAlpha.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    BufferedImage src
        = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);

    // Set src pixel values
    Color pelColor = new Color(100, 100, 100, 128);
    for (int i = 0; i < 10; i++) {
        src.setRGB(0, i, pelColor.getRGB());
    }

    ColorModel cm = new ComponentColorModel
        (ColorSpace.getInstance(ColorSpace.CS_GRAY),
         new int [] {8,8}, true,
         src.getColorModel().isAlphaPremultiplied(),
         Transparency.TRANSLUCENT,
         DataBuffer.TYPE_BYTE);

    SampleModel sm = new PixelInterleavedSampleModel
        (DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
         new int [] { 0, 1 });

    WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));

    BufferedImage dst =
        new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
    dst = dst.getSubimage(0, 0, 1, 10);

    ColorConvertOp op = new ColorConvertOp(null);

    op.filter(src, dst);

    for (int i = 0; i < 10; i++) {
        if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
            throw new RuntimeException(
                "Incorrect destination alpha value.");
        }
    }

}
 
Example 6
Source File: SimpleRenderedImage.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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.
 */
@Override
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;
  }
}
 
Example 7
Source File: IncorrectSampleMaskTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void doTest(int dataType) {
    int maxSize = DataBuffer.getDataTypeSize(dataType);
    System.out.println("Type size: " + maxSize);

    int theMask = (int)(1L << (maxSize + 2)) - 1;
    System.out.printf("theMask=%x\n", theMask);

    SinglePixelPackedSampleModel sm =
        new SinglePixelPackedSampleModel(dataType, w, h,
                                         new int[] { theMask });


    int[] sampleSize = sm.getSampleSize();
    for (int s : sampleSize) {
        if (s > maxSize) {
            throw new RuntimeException("Test failed: sample size is too big:" + s);
        }
    }

    System.out.println("Test medialib...");
    DataBuffer buf = createDataBuffer(dataType);

    WritableRaster wr = Raster.createWritableRaster(sm, buf, null);

    op.filter(wr, null);
    System.out.println("Test PASSED.");
}
 
Example 8
Source File: JFIFMarkerSegment.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    // read the palette
    byte [] palette = new byte [PALETTE_SIZE];
    float palettePart = ((float) PALETTE_SIZE) / getLength();
    readByteBuffer(iis,
                   palette,
                   reader,
                   palettePart,
                   0.0F);
    DataBufferByte buffer = new DataBufferByte(thumbWidth*thumbHeight);
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F-palettePart,
                   palettePart);
    iis.read();
    iis.reset();

    IndexColorModel cm = new IndexColorModel(8,
                                             256,
                                             palette,
                                             0,
                                             false);
    SampleModel sm = cm.createCompatibleSampleModel(thumbWidth,
                                                    thumbHeight);
    WritableRaster raster =
        Raster.createWritableRaster(sm, buffer, null);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}
 
Example 9
Source File: JFIFMarkerSegment.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    // read the palette
    byte [] palette = new byte [PALETTE_SIZE];
    float palettePart = ((float) PALETTE_SIZE) / getLength();
    readByteBuffer(iis,
                   palette,
                   reader,
                   palettePart,
                   0.0F);
    DataBufferByte buffer = new DataBufferByte(thumbWidth*thumbHeight);
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F-palettePart,
                   palettePart);
    iis.read();
    iis.reset();

    IndexColorModel cm = new IndexColorModel(8,
                                             256,
                                             palette,
                                             0,
                                             false);
    SampleModel sm = cm.createCompatibleSampleModel(thumbWidth,
                                                    thumbHeight);
    WritableRaster raster =
        Raster.createWritableRaster(sm, buffer, null);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}
 
Example 10
Source File: ImageSaver.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
static public 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);
}
 
Example 11
Source File: IncorrectSampleMaskTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void doTest(int dataType) {
    int maxSize = DataBuffer.getDataTypeSize(dataType);
    System.out.println("Type size: " + maxSize);

    int theMask = (int)(1L << (maxSize + 2)) - 1;
    System.out.printf("theMask=%x\n", theMask);

    SinglePixelPackedSampleModel sm =
        new SinglePixelPackedSampleModel(dataType, w, h,
                                         new int[] { theMask });


    int[] sampleSize = sm.getSampleSize();
    for (int s : sampleSize) {
        if (s > maxSize) {
            throw new RuntimeException("Test failed: sample size is too big:" + s);
        }
    }

    System.out.println("Test medialib...");
    DataBuffer buf = createDataBuffer(dataType);

    WritableRaster wr = Raster.createWritableRaster(sm, buf, null);

    op.filter(wr, null);
    System.out.println("Test PASSED.");
}
 
Example 12
Source File: ColCvtAlpha.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    BufferedImage src
        = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);

    // Set src pixel values
    Color pelColor = new Color(100, 100, 100, 128);
    for (int i = 0; i < 10; i++) {
        src.setRGB(0, i, pelColor.getRGB());
    }

    ColorModel cm = new ComponentColorModel
        (ColorSpace.getInstance(ColorSpace.CS_GRAY),
         new int [] {8,8}, true,
         src.getColorModel().isAlphaPremultiplied(),
         Transparency.TRANSLUCENT,
         DataBuffer.TYPE_BYTE);

    SampleModel sm = new PixelInterleavedSampleModel
        (DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
         new int [] { 0, 1 });

    WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));

    BufferedImage dst =
        new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
    dst = dst.getSubimage(0, 0, 1, 10);

    ColorConvertOp op = new ColorConvertOp(null);

    op.filter(src, dst);

    for (int i = 0; i < 10; i++) {
        if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
            throw new RuntimeException(
                "Incorrect destination alpha value.");
        }
    }

}
 
Example 13
Source File: JFIFMarkerSegment.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    // read the palette
    byte [] palette = new byte [PALETTE_SIZE];
    float palettePart = ((float) PALETTE_SIZE) / getLength();
    readByteBuffer(iis,
                   palette,
                   reader,
                   palettePart,
                   0.0F);
    DataBufferByte buffer = new DataBufferByte(thumbWidth*thumbHeight);
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F-palettePart,
                   palettePart);
    iis.read();
    iis.reset();

    IndexColorModel cm = new IndexColorModel(8,
                                             256,
                                             palette,
                                             0,
                                             false);
    SampleModel sm = cm.createCompatibleSampleModel(thumbWidth,
                                                    thumbHeight);
    WritableRaster raster =
        Raster.createWritableRaster(sm, buffer, null);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}
 
Example 14
Source File: ImageTypeSpecifier.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 15
Source File: ImageTypeSpecifier.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 16
Source File: ImageTypeSpecifier.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 17
Source File: ImageTypeSpecifier.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 18
Source File: ImageTypeSpecifier.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 19
Source File: ImageTypeSpecifier.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a <code>BufferedImage</code> with a given width and
 * height according to the specification embodied in this object.
 *
 * @param width the desired width of the returned
 * <code>BufferedImage</code>.
 * @param height the desired height of the returned
 * <code>BufferedImage</code>.
 *
 * @return a new <code>BufferedImage</code>
 *
 * @exception IllegalArgumentException if either <code>width</code> or
 * <code>height</code> are negative or zero.
 * @exception IllegalArgumentException if the product of
 * <code>width</code> and <code>height</code> is greater than
 * <code>Integer.MAX_VALUE</code>, or if the number of array
 * elements needed to store the image is greater than
 * <code>Integer.MAX_VALUE</code>.
 */
public BufferedImage createBufferedImage(int width, int height) {
    try {
        SampleModel sampleModel = getSampleModel(width, height);
        WritableRaster raster =
            Raster.createWritableRaster(sampleModel,
                                        new Point(0, 0));
        return new BufferedImage(colorModel, raster,
                                 colorModel.isAlphaPremultiplied(),
                                 new Hashtable());
    } catch (NegativeArraySizeException e) {
        // Exception most likely thrown from a DataBuffer constructor
        throw new IllegalArgumentException
            ("Array size > Integer.MAX_VALUE!");
    }
}
 
Example 20
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 3 votes vote down vote up
/**
 * 生成对应指定像素的ARGB模式BufferedImage
 * 
 * @param pixels
 * @param w
 * @param h
 * @param pixelSize
 * @return
 */
public static BufferedImage newAwtARGBImage(int[] pixels, int w, int h, int pixelSize) {
	DataBuffer dataBuffer = new DataBufferInt(pixels, pixelSize);
	SampleModel sample = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h,
			new int[] { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 });
	WritableRaster raster = Raster.createWritableRaster(sample, dataBuffer, new Point(0, 0));
	return new BufferedImage(COLOR_MODEL_ARGB, raster, true, null);
}