Java Code Examples for java.awt.image.BufferedImage.getRaster()

The following are Jave code examples for showing how to use getRaster() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pdi   File: GiraImagem.java   View Source Code Vote up 7 votes
public BufferedImage girar(BufferedImage img, PosicoesDTO posicoes) {
	WritableRaster raster = img.getRaster();
	BufferedImage newImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
	WritableRaster rasterNewImage = newImage.getRaster();
	int pixels[] = new int[4];
	for (int i = posicoes.getX1(); i < posicoes.getX2(); i++) {
		for (int j = posicoes.getY1(); j < posicoes.getY2(); j++) {
			raster.getPixel(i, j, pixels);
			rasterNewImage.setPixel(i, img.getHeight() - j, pixels);
		}
	}

	newImage.setData(rasterNewImage);

	return newImage;

}
 
Example 2
Project: jmonet   File: FloydSteinberg.java   View Source Code Vote up 7 votes
/**
 * Converts a color cube matrix into a BufferedImage. See {@link #toColorCubeMatrix(BufferedImage)} for the
 * format details of the color cube matrix.
 *
 * @param matrix The matrix to convert into an image
 * @return The BufferedImage resulting from the given color cube matrix
 */
private static BufferedImage fromColorCubeMatrix(double[][][] matrix) {
    BufferedImage restored = new BufferedImage(matrix[0].length, matrix.length, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = restored.getRaster();

    for (int y = 0; y < matrix.length; y++) {
        for (int x = 0; x < matrix[y].length; x++) {
            double[] pixel = matrix[y][x];

            pixel[0] *= 255;
            pixel[1] *= 255;
            pixel[2] *= 255;
            pixel[3] = matrix[y][x][3];

            pixel[0] = pixel[0] < 0 ? 0 : pixel[0] > 255 ? 255 : pixel[0];
            pixel[1] = pixel[1] < 0 ? 0 : pixel[1] > 255 ? 255 : pixel[1];
            pixel[2] = pixel[2] < 0 ? 0 : pixel[2] > 255 ? 255 : pixel[2];

            raster.setPixel(x, y, pixel);
        }
    }

    return restored;
}
 
Example 3
Project: OpenJSharp   File: BufImgSurfaceData.java   View Source Code Vote up 6 votes
public static SurfaceData createDataBC(BufferedImage bImg,
                                       SurfaceType sType,
                                       int primaryBank) {
    ByteComponentRaster bcRaster =
        (ByteComponentRaster)bImg.getRaster();
    BufImgSurfaceData bisd =
        new BufImgSurfaceData(bcRaster.getDataBuffer(), bImg, sType);
    ColorModel cm = bImg.getColorModel();
    IndexColorModel icm = ((cm instanceof IndexColorModel)
                           ? (IndexColorModel) cm
                           : null);
    bisd.initRaster(bcRaster.getDataStorage(),
                    bcRaster.getDataOffset(primaryBank), 0,
                    bcRaster.getWidth(),
                    bcRaster.getHeight(),
                    bcRaster.getPixelStride(),
                    bcRaster.getScanlineStride(),
                    icm);
    return bisd;
}
 
Example 4
Project: OpenJSharp   File: EffectUtils.java   View Source Code Vote up 6 votes
/**
 * <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 5
Project: openjdk-jdk10   File: TransparencyTest.java   View Source Code Vote up 6 votes
protected static BufferedImage createIndexedImage(int w, int h,
                                                  IndexColorModel icm)
{
    BufferedImage img = new BufferedImage(w, h,
                                          BufferedImage.TYPE_BYTE_INDEXED,
                                          icm);

    int mapSize = icm.getMapSize();
    int width = w / mapSize;

    WritableRaster wr = img.getRaster();
    for (int i = 0; i < mapSize; i++) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < width; x++) {
                wr.setSample(i * width + x, y, 0, i);
            }
        }
    }
    return img;
}
 
Example 6
Project: openjdk-jdk10   File: EffectUtils.java   View Source Code Vote up 6 votes
/**
 * <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 7
Project: openjdk-jdk10   File: PaletteBuilder.java   View Source Code Vote up 6 votes
protected RenderedImage getIndexedImage() {
    IndexColorModel icm = getIndexColorModel();

    BufferedImage dst =
        new BufferedImage(src.getWidth(), src.getHeight(),
                          BufferedImage.TYPE_BYTE_INDEXED, icm);

    WritableRaster wr = dst.getRaster();
    for (int y =0; y < dst.getHeight(); y++) {
        for (int x = 0; x < dst.getWidth(); x++) {
            Color aColor = getSrcColor(x,y);
            wr.setSample(x, y, 0, findColorIndex(root, aColor));
        }
    }

    return dst;
}
 
Example 8
Project: GIFKR   File: ImageTools.java   View Source Code Vote up 6 votes
public static BufferedImage getChannel(BufferedImage img, int channel) {

		if(channel == 0xFFFF0000)
			channel = 0;
		else if(channel == 0xFF00FF00)
			channel = 1;
		else if(channel == 0xFF0000FF)
			channel = 2;

		WritableRaster r = img.getRaster();

		BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
		int[] nullArray = null;

		img2.getRaster().setSamples(0, 0, r.getWidth(), r.getHeight(), channel, r.getSamples(0, 0, r.getWidth(), r.getHeight(), channel, nullArray));
		return img2;

	}
 
Example 9
Project: OpenJSharp   File: EffectUtils.java   View Source Code Vote up 6 votes
/**
 * <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
 */
public static int[] getPixels(BufferedImage img,
                              int x, int y, int w, int h, int[] pixels) {
    if (w == 0 || h == 0) {
        return new int[0];
    }

    if (pixels == null) {
        pixels = new int[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_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        Raster raster = img.getRaster();
        return (int[]) raster.getDataElements(x, y, w, h, pixels);
    }

    // Unmanages the image
    return img.getRGB(x, y, w, h, pixels, 0, w);
}
 
Example 10
Project: pdi   File: PixelsUtils.java   View Source Code Vote up 6 votes
public static List<Integer> getPixeisVizinhos3x3(BufferedImage img, int i, int j, int tipoPixel) {
	List<Integer> pixeisVizinhos = new ArrayList<>();
	WritableRaster raster = img.getRaster();
	int pixels[] = new int[4];
	pixeisVizinhos.add(raster.getPixel(i - 1, j - 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i - 1, j, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i - 1, j + 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j - 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j + 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i + 1, j - 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i + 1, j, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i + 1, j + 1, pixels)[tipoPixel]);

	Collections.sort(pixeisVizinhos);

	return pixeisVizinhos;
}
 
Example 11
Project: jdk8u-jdk   File: BufImgSurfaceData.java   View Source Code Vote up 6 votes
public static SurfaceData createDataBC(BufferedImage bImg,
                                       SurfaceType sType,
                                       int primaryBank) {
    ByteComponentRaster bcRaster =
        (ByteComponentRaster)bImg.getRaster();
    BufImgSurfaceData bisd =
        new BufImgSurfaceData(bcRaster.getDataBuffer(), bImg, sType);
    ColorModel cm = bImg.getColorModel();
    IndexColorModel icm = ((cm instanceof IndexColorModel)
                           ? (IndexColorModel) cm
                           : null);
    bisd.initRaster(bcRaster.getDataStorage(),
                    bcRaster.getDataOffset(primaryBank), 0,
                    bcRaster.getWidth(),
                    bcRaster.getHeight(),
                    bcRaster.getPixelStride(),
                    bcRaster.getScanlineStride(),
                    icm);
    return bisd;
}
 
Example 12
Project: jdk8u-jdk   File: PaletteBuilder.java   View Source Code Vote up 6 votes
protected RenderedImage getIndexedImage() {
    IndexColorModel icm = getIndexColorModel();

    BufferedImage dst =
        new BufferedImage(src.getWidth(), src.getHeight(),
                          BufferedImage.TYPE_BYTE_INDEXED, icm);

    WritableRaster wr = dst.getRaster();
    for (int y =0; y < dst.getHeight(); y++) {
        for (int x = 0; x < dst.getWidth(); x++) {
            Color aColor = getSrcColor(x,y);
            wr.setSample(x, y, 0, findColorIndex(root, aColor));
        }
    }

    return dst;
}
 
Example 13
Project: pdi   File: PixelsUtils.java   View Source Code Vote up 5 votes
public static List<Integer> getPixeisVizinhos2x2(BufferedImage img, int i, int j, int tipoPixel) {
	List<Integer> pixeisVizinhos = new ArrayList<>();
	WritableRaster raster = img.getRaster();
	int pixels[] = new int[4];
	pixeisVizinhos.add(raster.getPixel(i - 1, j, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j - 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i, j + 1, pixels)[tipoPixel]);
	pixeisVizinhos.add(raster.getPixel(i + 1, j, pixels)[tipoPixel]);

	Collections.sort(pixeisVizinhos);

	return pixeisVizinhos;
}
 
Example 14
Project: sumo   File: Alos.java   View Source Code Vote up 5 votes
@Override
public void preloadLineTile(int y, int length, int band) {
	TIFF tiff=(TIFF)getImage(band);
	if (y < 0) {
           return;
       }
       preloadedInterval = new int[]{y, y + length};
       Rectangle rect = new Rectangle(0, y, tiff.xSize, length);


       rect=tiff.getBounds().intersection(rect);

       try {
       	BufferedImage bi=null;
       	try{
       			bi=tiff.read(0, rect);
       	}catch(Exception e){
       		logger.warn("Problem reading image POS x:"+0+ "  y: "+y +"   try to read again");
       		try {
   			    Thread.sleep(100);
   			} catch(InterruptedException exx) {
   			    Thread.currentThread().interrupt();
   			}
       		bi=tiff.read(0, rect);
       	}
       	WritableRaster raster=bi.getRaster();
       	preloadedData=(Short[])raster.getDataElements(0, 0, raster.getWidth(), raster.getHeight(), null);//tSamples(0, 0, raster.getWidth(), raster.getHeight(), 0, (short[]) null);
       } catch (Exception ex) {
           logger.error(ex.getMessage(),ex);
       }finally{
       }

}
 
Example 15
Project: AquamarineLake   File: DrawPanel.java   View Source Code Vote up 5 votes
public BufferedImage bufferedImage(Mat mat) {

        if (mat.height() > 0 && mat.width() > 0) {
            BufferedImage image = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR);
            WritableRaster raster = image.getRaster();
            DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
            byte[] data = dataBuffer.getData();
            mat.get(0, 0, data);
            return image;
        }
        return null;
    }
 
Example 16
Project: jaer   File: DavisFrameAviWriter.java   View Source Code Vote up 5 votes
@Override
    synchronized public void propertyChange(PropertyChangeEvent evt) {
        if ((aviOutputStream != null && isWriteEnabled())
                && (evt.getPropertyName() == AEFrameChipRenderer.EVENT_NEW_FRAME_AVAILBLE)
                && !chip.getAeViewer().isPaused()) {
            FloatBuffer frame = ((AEFrameChipRenderer)chip.getRenderer()).getPixmap();

            BufferedImage bufferedImage = new BufferedImage(chip.getSizeX(), chip.getSizeY(), BufferedImage.TYPE_3BYTE_BGR);
            WritableRaster raster = bufferedImage.getRaster();
            int sx = chip.getSizeX(), sy = chip.getSizeY();
            for (int y = 0; y < sy; y++) {
                for (int x = 0; x < sx; x++) {
                    int k = renderer.getPixMapIndex(x, y);
//                    bufferedImage.setRGB(x, y, (int) (frame[k] * 1024));
                    int yy = sy - y - 1;
                    int r = (int) (frame.get(k) * 255); // must flip image vertially according to java convention that image starts at upper left
                    int g = (int) (frame.get(k+1) * 255); // must flip image vertially according to java convention that image starts at upper left
                    int b = (int) (frame.get(k+2) * 255); // must flip image vertially according to java convention that image starts at upper left
                    raster.setSample(x, yy, 0, r);
                    raster.setSample(x, yy, 1, g);
                    raster.setSample(x, yy, 2, b);
                }
            }
            try {
                aviOutputStream.writeFrame(bufferedImage);
                int timestamp = renderer.getTimestampFrameEnd();
                writeTimecode(timestamp);
                incrementFramecountAndMaybeCloseOutput();

            } catch (IOException ex) {
                Logger.getLogger(DavisFrameAviWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (evt.getPropertyName() == AEInputStream.EVENT_REWIND) {
            doCloseFile();
        }
    }
 
Example 17
Project: Sikulix2tesseract   File: ImageUtil.java   View Source Code Vote up 5 votes
public static boolean isBlack(BufferedImage image, int x, int y) {
    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY) {
        WritableRaster raster = image.getRaster();
        int pixelRGBValue = raster.getSample(x, y, 0);
        if (pixelRGBValue == 0) {
            return true;
        } else {
            return false;
        }
    }

    int luminanceValue = 140;
    return isBlack(image, x, y, luminanceValue);
}
 
Example 18
Project: sumo   File: ZoomWindowLayer.java   View Source Code Vote up 4 votes
private BufferedImage createImage(GeoImageReader gir, int x, int y, int width, int height) {
    BufferedImage bufferedImage = new BufferedImage(width, height, gir.getType(true));

    //System.out.println(zoom);
    WritableRaster raster = bufferedImage.getRaster();

    // Put the pixels on the raster.
    //if (bands.length == 1) {
       // int band = band;
        nat = gir.readTile(x, y, width, height,band);
        raster.setPixels(0, 0, width, height, nat);

   /* } else {
        int b = 0;
        for (int band : bands) {
            gir.setBand(band);
            nat = gir.readTile(x, y, width, height);
            ///if (zoom == 1) {
            raster.setSamples(0, 0, width, height, b, nat);
            /*
             * for (int h = 0; h < height; h++) {
             *   int temp = h * width;
             *   for (int w = 0; w < width; w++) {
             *      raster.setSample(w, h, b, nat[temp + w]);
             *   }
             * }
             **/
            /*
             * } else {
             *       for (int h = 0; h < height; h++) {
             *          int temp = (int) (h * zoom) * (int) (width * zoom);
             *         for (int w = 0; w < width; w++) {
             *            try {
             *               raster.setSample(w, h, b, nat[temp + (int) (w * zoom)]);
             *          } catch (Exception e) {
             *               }
             *          }
             *      }
             *  }
             **/
        /*    b++;
            if (b > raster.getNumBands()) {
                break;
            }
        }

    }*/
    return bufferedImage;
}
 
Example 19
Project: GameResourceBot   File: Preprocessor.java   View Source Code Vote up 4 votes
public static List<File> cropMasked(BufferedImage image) throws IOException {
    // now we iterate over the rows and remove the masked areas since the
    // text we want to read is underneath it
    WritableRaster raster = image.getRaster();
    List<Integer> rowsToRemove = new ArrayList<>();
    for (int yy = 0; yy < image.getHeight(); yy++) {
        int masked = 0;
        for (int xx = 0; xx < image.getWidth(); xx++) {
            if (matchColor(getPixelValue(raster, xx, yy), Color.MAGENTA)) {
                masked++;
            }
        }
        float maskAmount = masked > 0 ? (float) masked / (float) image.getWidth() : 0;
        if (maskAmount > 0.02f) {
            rowsToRemove.add(yy);
        }
    }
    List<SubImage> subImages = extractSubimageBetweenMaskedRows(rowsToRemove, image.getHeight(), 50);
    List<File> result = new ArrayList<>(subImages.size() * 3);
    int i = 0;
    for (SubImage subImage : subImages) {
        BufferedImage cut = image.getSubimage(0, subImage.y, image.getWidth(), subImage.height);

        File tempFile;

        tempFile = File.createTempFile("subImage_" + i + "_c1", ".png", TMP_FILE_DIR);
        tempFile.deleteOnExit();
        ImageIO.write(cut.getSubimage(50, 0, 200, cut.getHeight()), "png", tempFile);
        result.add(tempFile);

        tempFile = File.createTempFile("subImage_" + i + "_c2", ".png", TMP_FILE_DIR);
        tempFile.deleteOnExit();
        ImageIO.write(cut.getSubimage(255, 0, 200, cut.getHeight()), "png", tempFile);
        result.add(tempFile);

        tempFile = File.createTempFile("subImage_" + i + "_c3", ".png", TMP_FILE_DIR);
        tempFile.deleteOnExit();
        ImageIO.write(cut.getSubimage(465, 0, 200, cut.getHeight()), "png", tempFile);
        result.add(tempFile);

        i++;
    }
    return result;
}
 
Example 20
Project: jdk8u-jdk   File: JPEGImageWriter.java   View Source Code Vote up 4 votes
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}