Java BufferedImage Split splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown)

Here you can find the source of splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown)

Description

Creates subimages for each tile in the specified source image.

License

Open Source License

Parameter

Parameter Description
imageWithTiles the source image, divided into tiles along the x and y direction. Should have a size that is a multiple of the number of tiles in each direction, so that all tiles get the same size.
numberOfTilesAcross number of columns of tiles in the source image
numberOfTilesDown number of rows of tiles in the source image

Return

an array with the tiles, ordered by rows from left to right.

Declaration

public static BufferedImage[] splitImageIntoTiles(final BufferedImage imageWithTiles,
        final int numberOfTilesAcross, final int numberOfTilesDown) 

Method Source Code

//package com.java2s;
/*//  www . j  a  v  a2s.c  om
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Creates subimages for each tile in the specified source image.
     * The returned subimages share the same image data with the source image.
     *
     * @param imageWithTiles      the source image, divided into tiles along the x and y direction.
     *                            Should have a size that is a multiple of the number of tiles in each direction, so that all tiles get the same size.
     * @param numberOfTilesAcross number of columns of tiles in the source image
     * @param numberOfTilesDown   number of rows of tiles in the source image
     *
     * @return an array with the tiles, ordered by rows from left to right.
     */
    public static BufferedImage[] splitImageIntoTiles(final BufferedImage imageWithTiles,
            final int numberOfTilesAcross, final int numberOfTilesDown) {
        final int tileWidth = imageWithTiles.getWidth() / numberOfTilesAcross;
        final int tileHeight = imageWithTiles.getHeight() / numberOfTilesDown;
        final int totalNumberOfTiles = numberOfTilesDown * numberOfTilesAcross;

        final BufferedImage[] tiles = new BufferedImage[totalNumberOfTiles];
        for (int y = 0; y < numberOfTilesDown; y++) {
            for (int x = 0; x < numberOfTilesAcross; x++) {
                tiles[x + y * numberOfTilesAcross] = imageWithTiles.getSubimage(x * tileWidth, y * tileHeight,
                        tileWidth, tileHeight);
            }
        }

        return tiles;
    }
}

Related

  1. splitImage(BufferedImage image, int rows, int cols)
  2. splitImage(BufferedImage img, int cols, int rows)
  3. splitImage(BufferedImage img, int rows, int cols)
  4. splitImage(final Image img, final int rows, final int cols)
  5. splitImage2D(BufferedImage img, int cols, int rows)
  6. splitVertically(BufferedImage top, int elements)
  7. tileStretchPaint(Graphics g, JComponent comp, BufferedImage img, Insets ins)