Java BufferedImage Clip subimage(BufferedImage src, int x, int y, int w, int h)

Here you can find the source of subimage(BufferedImage src, int x, int y, int w, int h)

Description

Returns a subimage of the given main image.

License

LGPL

Parameter

Parameter Description
src the source image.
x the x coordinate
y the y coordinate
w the width
h the height

Return

the extracted sub-image

Declaration

@SuppressWarnings("unused")
public static BufferedImage subimage(BufferedImage src, int x, int y, int w, int h) 

Method Source Code


//package com.java2s;
/*/*  w  w  w  .j a  v  a  2  s.com*/
 * Copyright 2008-2011, David Karnok 
 * The file is part of the Open Imperium Galactica project.
 * 
 * The code should be distributed under the LGPL license.
 * See http://www.gnu.org/licenses/lgpl.html for details.
 */

import java.awt.image.BufferedImage;

public class Main {
    /** Use subimage or separate image. */
    private static final boolean SUBIMAGE = false;

    /**
     * Returns a subimage of the given main image. If the sub-image tends to be
     * smaller in area then the original image, a new buffered image is returned
     * instead of the shared sub image
     * 
     * @param src
     *            the source image.
     * @param x
     *            the x coordinate
     * @param y
     *            the y coordinate
     * @param w
     *            the width
     * @param h
     *            the height
     * @return the extracted sub-image
     */
    @SuppressWarnings("unused")
    public static BufferedImage subimage(BufferedImage src, int x, int y, int w, int h) {
        if (!SUBIMAGE /* && w * h * 16 < src.getWidth() * src.getHeight() */) {
            return newSubimage(src, x, y, w, h);
        }
        return src.getSubimage(x, y, w, h);
    }

    /**
     * Returns an independent subimage of the given main image. copying data
     * from the original image.
     * 
     * @param src
     *            the source image.
     * @param x
     *            the x coordinate
     * @param y
     *            the y coordinate
     * @param w
     *            the width
     * @param h
     *            the height
     * @return the extracted sub-image
     */
    public static BufferedImage newSubimage(BufferedImage src, int x, int y, int w, int h) {
        BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int[] tmp = new int[w * h];
        src.getRGB(x, y, w, h, tmp, 0, w);
        bimg.setRGB(0, 0, w, h, tmp, 0, w);
        return bimg;
    }
}

Related

  1. clip(BufferedImage image, int[] ul, int[] lr)
  2. subImage(BufferedImage image, int x, int y, int width, int height)