0

I have a class to separate and join image in tiles. It works fine when the sides of the tile correspond to the dimensions of the image, i.e. height 250, tile height 25. But when it's not it doesn't create smaller tiles at the borders as it should.

Where would be the problem to properly create the border tiles smaller than the rest?

Constructor:

public EdgeBufferedImage(BufferedImage image, int w, int h){
    this.sourceImg = image;

    this.setCol((int)Math.ceil(image.getWidth()/(double)w));
    this.setRow((int)Math.ceil(image.getHeight()/(double)h));

    this.setWidth(image.getWidth());
    this.setHeight(image.getHeight());

    this.setTilew(w);
    this.setTileh(h);
    this.setMatImg(new BufferedImage[row][col]);
}

Methods: Image tiling

public void createSmallImages() {

    int rows = getRow();
    int columns = getCol();
    int smallWidth = getTilew();
    int smallHeight = getTileh();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {

            if (j == columns - 1) smallWidth = getWidth() - (getTilew() * j);           
            if (i == rows - 1) smallHeight = getHeight() - (getTileh() * i);

            matImg[i][j] = getSourceImg().getSubimage(j * smallWidth, i
                    * smallHeight, smallWidth, smallHeight);
        }
        smallWidth = getTilew();
        smallHeight = getTileh();   
    }   
}

Image joining

public void joinTiles(){

    int rows = getRow();
    int columns = getCol();
    int smallWidth = getTilew();
    int smallHeight = getTileh();

    BufferedImage comb = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) comb.getGraphics();

    g.setColor(Color.RED);

    for (int row = 0; row < rows; row++){
        for (int col = 0; col < columns; col++){

            BufferedImage piece = getMatImg()[row][col];

            if (col == columns - 1) smallWidth = getWidth() - (getTilew() * col);           
            if (row == rows - 1) smallHeight = getHeight() - (getTileh() * row);

            g.drawImage(piece, col * smallWidth, row * smallHeight, smallWidth, smallHeight, null);
            g.drawRect(col * smallWidth, row * smallHeight, smallWidth, smallHeight);

        }
        smallWidth = getTilew();
        smallHeight = getTileh();
    }
    g.dispose();

    setSourceImg(comb); 
}

Original image is 512*512

Image with whole tiles(256*128)

Image with another tile size (256*100)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • If you add print statements or break points to your code, can you verify that the correct values are passed to `BufferedImage#getSubimage(x, y, w, h)`? – MTCoster Nov 15 '18 at 17:55
  • 1
    Now that I look closely, when I change to the smaller width/height, I still multiply it by the row/col position. So I should first multiply the original value and add it the pad, correct? – Santiago Rosales Nov 15 '18 at 17:59
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Nov 15 '18 at 23:37

0 Answers0