Java Code Examples for java.awt.image.Raster

The following examples show how to use java.awt.image.Raster. These examples are extracted from open source projects.
Example 1
Project: openjdk-8-source   File: BytePackedRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    // Check if we can use fast code
    if (!(inRaster instanceof BytePackedRaster) ||
        ((BytePackedRaster)inRaster).pixelBitStride != pixelBitStride) {
        super.setDataElements(x, y, inRaster);
        return;
    }

    int srcOffX = inRaster.getMinX();
    int srcOffY = inRaster.getMinY();
    int dstOffX = srcOffX + x;
    int dstOffY = srcOffY + y;
    int width = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height,
                    (BytePackedRaster)inRaster);
}
 
Example 2
Project: jdk8u_jdk   File: TexturePaintContext.java    License: GNU General Public License v2.0 6 votes vote down vote up
synchronized static WritableRaster makeByteRaster(Raster srcRas,
                                                  int w, int h)
{
    if (byteRasRef != null) {
        WritableRaster wr = (WritableRaster) byteRasRef.get();
        if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
            byteRasRef = null;
            return wr;
        }
    }
    // If we are going to cache this Raster, make it non-tiny
    if (w <= 32 && h <= 32) {
        w = h = 32;
    }
    return srcRas.createCompatibleWritableRaster(w, h);
}
 
Example 3
Project: openjdk-jdk9   File: ShortBandedRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 4
Project: TencentKona-8   File: ShortBandedRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 5
Project: jdk8u60   File: ColorPaintContext.java    License: GNU General Public License v2.0 6 votes vote down vote up
public synchronized Raster getRaster(int x, int y, int w, int h) {
    WritableRaster t = savedTile;

    if (t == null || w > t.getWidth() || h > t.getHeight()) {
        t = getColorModel().createCompatibleWritableRaster(w, h);
        IntegerComponentRaster icr = (IntegerComponentRaster) t;
        Arrays.fill(icr.getDataStorage(), color);
        // Note - markDirty is probably unnecessary since icr is brand new
        icr.markDirty();
        if (w <= 64 && h <= 64) {
            savedTile = t;
        }
    }

    return t;
}
 
Example 6
Project: orbit-image-analysis   File: Calibration.java    License: GNU General Public License v3.0 6 votes vote down vote up
public double getSlope(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
   
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
         for (int y=minY; y<maxY; y++) {
             double d1 = r1.getSampleDouble(x+u,y+v,0);
             if (d1> intensityThreshold) {
                 double d2 = r2.getSampleDouble(x, y, 0);
                 reg.addData(d2, d1);
             }
         }
     }

    double slope = reg.getSlope();
    double intercept = reg.getIntercept();
    logger.info("i,j: "+s+","+n+": "+ "slope: "+slope+" ; intercept: "+intercept);
    return slope;
}
 
Example 7
Project: openjdk-jdk9   File: RasterOpNullDestinationRasterTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {

        byte[][] data = new byte[1][10];
        ByteLookupTable lut = new ByteLookupTable(0, data);
        RasterOp op = new LookupOp(lut, null);

        int[] bandOffsets = {0};
        Point location = new Point(0, 0);
        DataBuffer db = new DataBufferByte(10 * 10);
        SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
                                                         10, 10, 1, 10,
                                                         bandOffsets);

        Raster src = Raster.createRaster(sm, db, location);

        op.filter(src, null); // this used to result in NullPointerException
    }
 
Example 8
Project: dsworkbench   File: MapRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Raster getRaster(int x, int y, int w, int h) {
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);

    int[] data = new int[w * h * 4];
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            double distance = mPoint.distance(x + i, y + j);
            double radius = mRadius.distance(0, 0);
            double ratio = distance / radius;
            if (ratio > 1.0) {
                ratio = 1.0;
            }

            int base = (j * w + i) * 4;
            data[base] = (int) (color1.getRed() + ratio * (color2.getRed() - color1.getRed()));
            data[base + 1] = (int) (color1.getGreen() + ratio * (color2.getGreen() - color1.getGreen()));
            data[base + 2] = (int) (color1.getBlue() + ratio * (color2.getBlue() - color1.getBlue()));
            data[base + 3] = (int) (color1.getAlpha() + ratio * (color2.getAlpha() - color1.getAlpha()));
        }
    }
    raster.setPixels(0, 0, w, h, data);

    return raster;
}
 
Example 9
Project: beast-mcmc   File: KMLRenderer.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
public BufferedImage render(int width, int height) {
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    render(image);

    Raster raster = image.getData();

    lattice = new int[width][height];
    int[] pixel = new int[4];
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            raster.getPixel(i, j, pixel);
            if (colorDistanceSquared(pixel, shapeColor) < colorDistanceSquared(pixel, background)) {
                lattice[i][j] = 1;
            } else {
                lattice[i][j] = 0;
            }
        }
    }

    return image;
}
 
Example 10
Project: scifio   File: SignedColorModel.java    License: 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 11
Project: Java8CN   File: EffectUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
 * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
 * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
 * image.</p>
 *
 * @param img    the source image
 * @param x      the x location at which to start grabbing pixels
 * @param y      the y location at which to start grabbing pixels
 * @param w      the width of the rectangle of pixels to grab
 * @param h      the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static byte[] getPixels(BufferedImage img,
                               int x, int y, int w, int h, byte[] pixels) {
    if (w == 0 || h == 0) {
        return new byte[0];
    }

    if (pixels == null) {
        pixels = new byte[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        Raster raster = img.getRaster();
        return (byte[]) raster.getDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 12
Project: Bytecoder   File: ShortBandedRaster.java    License: Apache License 2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 13
Project: jdk8u-jdk   File: ShortComponentRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortComponentRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 14
Project: dragonwell8_jdk   File: ShortInterleavedRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      REMIND: Do something faster!
//      if (inRaster instanceof ShortInterleavedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 15
Project: dawnsci   File: AWTImageUtils.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get datasets from an image
 * @param image
 * @param keepBitWidth if true, then use signed primitives of same bit width for possibly unsigned data
 * @return array of datasets
 */
static public Dataset[] makeDatasets(final BufferedImage image, boolean keepBitWidth) {
	// make raster from buffered image
	final Raster ras = image.getData();
	final SampleModel sm = ras.getSampleModel();
	int[] dtype = getDTypeFromImage(sm, keepBitWidth);

	final int bands = ras.getNumBands();
	Dataset[] data = new Dataset[bands];

	createDatasets(ras, data, dtype[0]);
	if (dtype[1] == 1) {
		for (int i = 0; i < bands; i++) {
			tagIntForShortDataset(data[i]);
		}
	}
	return data;
}
 
Example 16
Project: osp   File: IntegerRaster.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Constructs IntegerRaster with the given size.
 * @param _nrow the number of rows
 * @param _ncol the number of columns
 */
public IntegerRaster(int _nrow, int _ncol) {
  nrow = _nrow;
  ncol = _ncol;
  dimension = new Dimension(ncol, nrow);
  int size = nrow*ncol;
  ComponentColorModel ccm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8, 8, 8}, false, // hasAlpha
    false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
  BandedSampleModel csm = new BandedSampleModel(DataBuffer.TYPE_BYTE, ncol, nrow, ncol, new int[] {0, 1, 2}, new int[] {0, 0, 0});
  rgbData = new byte[3][size];
  DataBuffer databuffer = new DataBufferByte(rgbData, size);
  WritableRaster raster = Raster.createWritableRaster(csm, databuffer, new Point(0, 0));
  image = new BufferedImage(ccm, raster, false, null);
  // col in x direction, row in y direction
  xmin = 0;
  xmax = ncol;
  ymin = nrow;
  ymax = 0; // zero is on top
}
 
Example 17
Project: jdk8u_jdk   File: ShortInterleavedRaster.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      REMIND: Do something faster!
//      if (inRaster instanceof ShortInterleavedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
 
Example 18
Project: jdk8u-jdk   File: ImageTests.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
    RasterOp op = ictx.rasterOp;
    Raster src = ictx.rasSrc;
    WritableRaster dst = ictx.rasDst;
    if (ictx.touchSrc) {
        Graphics gSrc = ictx.bufSrc.getGraphics();
        do {
            gSrc.fillRect(0, 0, 1, 1);
            op.filter(src, dst);
        } while (--numReps > 0);
    } else {
        do {
            op.filter(src, dst);
        } while (--numReps > 0);
    }
}
 
Example 19
Project: jdk8u60   File: TexturePaintContext.java    License: GNU General Public License v2.0 6 votes vote down vote up
synchronized static WritableRaster makeByteRaster(Raster srcRas,
                                                  int w, int h)
{
    if (byteRasRef != null) {
        WritableRaster wr = (WritableRaster) byteRasRef.get();
        if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
            byteRasRef = null;
            return wr;
        }
    }
    // If we are going to cache this Raster, make it non-tiny
    if (w <= 32 && h <= 32) {
        w = h = 32;
    }
    return srcRas.createCompatibleWritableRaster(w, h);
}
 
Example 20
Project: jdk-1.7-annotated   File: MultipleGradientPaintContext.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Took this cacheRaster code from GradientPaint. It appears to recycle
 * rasters for use by any other instance, as long as they are sufficiently
 * large.
 */
private static synchronized void putCachedRaster(ColorModel cm,
                                                 Raster ras)
{
    if (cached != null) {
        Raster cras = (Raster) cached.get();
        if (cras != null) {
            int cw = cras.getWidth();
            int ch = cras.getHeight();
            int iw = ras.getWidth();
            int ih = ras.getHeight();
            if (cw >= iw && ch >= ih) {
                return;
            }
            if (cw * ch >= iw * ih) {
                return;
            }
        }
    }
    cachedModel = cm;
    cached = new WeakReference<Raster>(ras);
}
 
Example 21
Project: orbit-image-analysis   File: DLSegment.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static Raster flipRaster(Raster r) {
    int w = r.getWidth();
    int h = r.getHeight();
    WritableRaster rf = r.createCompatibleWritableRaster(r.getMinX(),r.getMinY(), w,h);
    int[] p = new int[w*3];
    for (int y=r.getMinY(); y<r.getMinY()+h; y++) {
        p = r.getPixels(r.getMinX(),y,w,1,p);
        rf.setPixels(r.getMinX(),r.getMinY()+h-(y-r.getMinY())-1,w,1,p);
    }
    return rf;
}
 
Example 22
Project: Canova   File: ImageLoader.java    License: Apache License 2.0 5 votes vote down vote up
public int[][] fromFile(File file) throws IOException {
    BufferedImage image = ImageIO.read(file);
    if (height > 0 && width > 0)
        image = toBufferedImage(image.getScaledInstance(height, width, Image.SCALE_SMOOTH));
    Raster raster = image.getData();
    int w = raster.getWidth(), h = raster.getHeight();
    int[][] ret = new int[w][h];
    for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
            ret[i][j] = raster.getSample(i, j, 0);

    return ret;
}
 
Example 23
Project: openjdk-8   File: SingleArrayTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void doTest(Raster src, WritableRaster dst) {
    System.out.println("Test for raster:" + src);
    try {
        dst = op.filter(src, dst);
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
Example 24
Project: openjdk-8-source   File: SingleArrayTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void doTest(Raster src, WritableRaster dst) {
    System.out.println("Test for raster:" + src);
    try {
        dst = op.filter(src, dst);
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
Example 25
Project: healthcare-dicom-dicomweb-adapter   File: J2KImageReader.java    License: Apache License 2.0 5 votes vote down vote up
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    checkIndex(imageIndex);
    processImageStarted(imageIndex);

    if (param == null) {
        param = getDefaultReadParam();
    }
    param = new J2KImageReadParamJava(param);

    if (!ignoreMetadata) {
        imageMetadata = new J2KMetadata();
        iis.seek(streamPosition0);
        readState = new J2KReadState(iis,
                                     (J2KImageReadParamJava)param,
                                     imageMetadata,
                                     this);
    } else {
        iis.seek(streamPosition0);
        readState = new J2KReadState(iis,
                                     (J2KImageReadParamJava)param,
                                     this);
    }

    Raster ras = readState.readAsRaster();
    if (abortRequested())
        processReadAborted();
    else
        processImageComplete();
    return ras;
}
 
Example 26
Project: openjdk-8-source   File: Win32ColorModel24.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 27
Project: jfreesane   File: SaneImage.java    License: Apache License 2.0 5 votes vote down vote up
private BufferedImage decodeSingleBitGrayscaleImage(DataBuffer buffer) {
  WritableRaster raster = Raster.createPackedRaster(buffer, width, height, 1, new Point(0, 0));
  return new BufferedImage(
      new IndexColorModel(
          1,
          2,
          new byte[] {(byte) 0xff, 0},
          new byte[] {(byte) 0xff, 0},
          new byte[] {(byte) 0xff, 0}),
      raster,
      false,
      null);
}
 
Example 28
Project: openjdk-jdk8u   File: Win32ColorModel24.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 29
Project: openjdk-8-source   File: PixelTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object context, int numReps) {
    Raster ras = ((Context) context).ras;
    Object elemdata = ((Context) context).elemdata;
    do {
        ras.getDataElements(numReps&7, 0, elemdata);
    } while (--numReps > 0);
}
 
Example 30
Project: jdk8u-dev-jdk   File: TexturePaintContext.java    License: GNU General Public License v2.0 5 votes vote down vote up
synchronized static void dropRaster(ColorModel cm, Raster outRas) {
    if (outRas == null) {
        return;
    }
    if (xrgbmodel == cm) {
        xrgbRasRef = new WeakReference<>(outRas);
    } else if (argbmodel == cm) {
        argbRasRef = new WeakReference<>(outRas);
    }
}
 
Example 31
Project: jdk8u-jdk   File: MultipleGradientPaintContext.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 32
Project: openjdk-8   File: ShortInterleavedRaster.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 33
Project: openjdk-jdk9   File: ImageRepresentation.java    License: GNU General Public License v2.0 5 votes vote down vote up
public BufferedImage getOpaqueRGBImage() {
    if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) {
        int w = bimage.getWidth();
        int h = bimage.getHeight();
        int size = w * h;

        // Note that we steal the data array here, but only for reading...
        DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer();
        int[] pixels = SunWritableRaster.stealData(db, 0);

        for (int i = 0; i < size; i++) {
            if ((pixels[i] >>> 24) != 0xff) {
                return bimage;
            }
        }

        ColorModel opModel = new DirectColorModel(24,
                                                  0x00ff0000,
                                                  0x0000ff00,
                                                  0x000000ff);

        int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff};
        WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w,
                                                            bandmasks,
                                                            null);

        try {
            BufferedImage opImage = createImage(opModel, opRaster,
                                                false, null);
            return opImage;
        } catch (Exception e) {
            return bimage;
        }
    }
    return bimage;
}
 
Example 34
Project: jdk8u_jdk   File: BMPCompressionTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(final BufferedImage in, final BufferedImage out) {

            final int width = in.getWidth();
            int height = in.getHeight();
            if (out.getWidth() != width || out.getHeight() != height) {
                throw new RuntimeException("Dimensions changed!");
            }

            Raster oldras = in.getRaster();
            ColorModel oldcm = in.getColorModel();
            Raster newras = out.getRaster();
            ColorModel newcm = out.getColorModel();

            for (int j = 0; j < height; j++) {
                for (int i = 0; i < width; i++) {
                    Object oldpixel = oldras.getDataElements(i, j, null);
                    int oldrgb = oldcm.getRGB(oldpixel);
                    int oldalpha = oldcm.getAlpha(oldpixel);

                    Object newpixel = newras.getDataElements(i, j, null);
                    int newrgb = newcm.getRGB(newpixel);
                    int newalpha = newcm.getAlpha(newpixel);

                    if (newrgb != oldrgb ||
                        newalpha != oldalpha) {
                        // showDiff(in, out);
                        throw new RuntimeException("Pixels differ at " + i +
                                                   ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
                    }
                }
            }
            return true;
        }
 
Example 35
Project: openjdk-jdk9   File: BMPSubsamplingTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel colorModel =
        new ComponentColorModel(cs, nBits,
                                false, false,
                                Transparency.OPAQUE,
                                DataBuffer.TYPE_BYTE);
    WritableRaster raster =
        Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                       w, h,
                                       w*3, 3,
                                       bOffs, null);
    return new BufferedImage(colorModel, raster, false, null);
}
 
Example 36
Project: TencentKona-8   File: ColCvtAlpha.java    License: 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 37
Project: openjdk-jdk9   File: ByteInterleavedRaster.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void setRect(int dx, int dy, Raster srcRaster) {
    if (!(srcRaster instanceof ByteInterleavedRaster)) {
        super.setRect(dx, dy, srcRaster);
        return;
    }

    int width  = srcRaster.getWidth();
    int height = srcRaster.getHeight();
    int srcOffX = srcRaster.getMinX();
    int srcOffY = srcRaster.getMinY();
    int dstOffX = dx+srcOffX;
    int dstOffY = dy+srcOffY;

    // Clip to this raster
    if (dstOffX < this.minX) {
        int skipX = minX - dstOffX;
        width -= skipX;
        srcOffX += skipX;
        dstOffX = this.minX;
    }
    if (dstOffY < this.minY) {
        int skipY = this.minY - dstOffY;
        height -= skipY;
        srcOffY += skipY;
        dstOffY = this.minY;
    }
    if (dstOffX+width > this.maxX) {
        width = this.maxX - dstOffX;
    }
    if (dstOffY+height > this.maxY) {
        height = this.maxY - dstOffY;
    }

    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height, srcRaster);
}
 
Example 38
Project: Bytecoder   File: TexturePaintContext.java    License: Apache License 2.0 5 votes vote down vote up
static synchronized void dropRaster(ColorModel cm, Raster outRas) {
    if (outRas == null) {
        return;
    }
    if (xrgbmodel == cm) {
        xrgbRasRef = new WeakReference<>(outRas);
    } else if (argbmodel == cm) {
        argbRasRef = new WeakReference<>(outRas);
    }
}
 
Example 39
Project: dragonwell8_jdk   File: GIFImageWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void writeRows(RenderedImage image, LZWCompressor compressor,
                       int sx, int sdx, int sy, int sdy, int sw,
                       int dy, int ddy, int dw, int dh,
                       int numRowsWritten, int progressReportRowPeriod)
  throws IOException {
    if (DEBUG) System.out.println("Writing unoptimized");

    int[] sbuf = new int[sw];
    byte[] dbuf = new byte[dw];

    Raster raster =
        image.getNumXTiles() == 1 && image.getNumYTiles() == 1 ?
        image.getTile(0, 0) : image.getData();
    for (int y = dy; y < dh; y += ddy) {
        if (numRowsWritten % progressReportRowPeriod == 0) {
            if (abortRequested()) {
                processWriteAborted();
                return;
            }
            processImageProgress((numRowsWritten*100.0F)/dh);
        }

        raster.getSamples(sx, sy, sw, 1, 0, sbuf);
        for (int i = 0, j = 0; i < dw; i++, j += sdx) {
            dbuf[i] = (byte)sbuf[j];
        }
        compressor.compress(dbuf, 0, dw);
        numRowsWritten++;
        sy += sdy;
    }
}
 
Example 40
Project: jdk8u-jdk   File: ColCvtAlpha.java    License: 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.");
        }
    }

}