Java BufferedImage Operation recombine(BufferedImage[][] blocks)

Here you can find the source of recombine(BufferedImage[][] blocks)

Description

Handles recombining a two-dimensional array of image blocks back into a single image.

License

Open Source License

Parameter

Parameter Description
blocks - the blocks to be combined.

Return

a new image with all blocks combined.

Declaration

public static BufferedImage recombine(BufferedImage[][] blocks) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics;

import java.awt.image.BufferedImage;

public class Main {
    /**//from  ww  w .j  a  v  a2s  . c o m
     * Handles recombining a two-dimensional array of image blocks back into a
     * single image.
     *
     * @param blocks - the blocks to be combined.
     * @return a new image with all blocks combined.
     */
    public static BufferedImage recombine(BufferedImage[][] blocks) {
        int type = 0;
        int totalWidth = 0;
        int totalHeight = 0;
        int blockWidth = 0;
        int blockHeight = 0;

        // Scan over all blocks to get the total width and height of the image.
        for (int row = 0; row < blocks.length; row++) {
            for (int col = 0; col < blocks[row].length; col++) {
                BufferedImage block = blocks[row][col];

                // Get block size and image type information for the first block
                // only.
                if (row == 0 && col == 0) {
                    type = BufferedImage.TYPE_INT_ARGB; // block.getType();
                    blockWidth = block.getWidth();
                    blockHeight = block.getHeight();
                }
                // Sum all of the block widths for the first row.
                if (row == 0) {
                    totalWidth += block.getWidth();
                }
                // Sum all of the block height for the first column.
                if (col == 0) {
                    totalHeight += block.getHeight();
                }
            }
        }

        // Create a new image with the size of the calculated blocks.
        BufferedImage img = new BufferedImage(totalWidth, totalHeight, type);
        // Get the graphics from the new image so that it can be drawn on.
        Graphics g = img.getGraphics();

        // Loop over all the image blocks and write them to the new output
        // image.
        for (int row = 0; row < blocks.length; row++) {
            for (int col = 0; col < blocks[row].length; col++) {
                BufferedImage block = blocks[row][col];
                int x = col * blockWidth;
                int y = row * blockHeight;
                int w = block.getWidth();
                int h = block.getHeight();
                g.drawImage(block, x, y, w, h, null);
            }
        }

        // Release the graphics context.
        g.dispose();

        return img;
    }
}

Related

  1. palettize(BufferedImage img, int maxEntries)
  2. performRescaleOperation(BufferedImage image, float scale, float offset)
  3. prepareModel(BufferedImage model)
  4. recolor(BufferedImage img, int newColor)
  5. recolor(BufferedImage src, Color sc)
  6. reorientImage(BufferedImage image, boolean yAxisFlipNeeded, int cwRotationNeeded)
  7. repairImage(final BufferedImage bfi, final List order)
  8. resampleImage(BufferedImage image, int height)
  9. resampleWithAffineTransformOp(BufferedImage srcImage, double sx, double sy)