Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
RearrangeTiles.java
Go to the documentation of this file.
1 package micropolisj.build_tool;
2 
3 import java.awt.*;
4 import java.awt.image.BufferedImage;
5 import java.io.*;
6 import java.util.*;
7 import javax.imageio.*;
8 import javax.swing.ImageIcon;
9 
14 public class RearrangeTiles
15 {
16  static final int DEST_COLUMNS = 16;
17  static final int TILE_SIZE = 16;
18 
19  public static void main(String [] args)
20  throws Exception
21  {
22  File inputFile = new File(args[0]);
23  File outputFile = new File(args[1]);
24 
25  // read in the image
26  Image srcImage = new ImageIcon(inputFile.toString()).getImage();
27  int srcCols = srcImage.getWidth(null) / TILE_SIZE;
28  int srcRows = srcImage.getHeight(null) / TILE_SIZE;
29  int ntiles = srcRows * srcCols;
30 
31  // actually assemble the image
32  int destCols = DEST_COLUMNS;
33  int destRows = (ntiles + destCols-1) / destCols;
34  BufferedImage buf = new BufferedImage(TILE_SIZE*DEST_COLUMNS,TILE_SIZE*destRows,BufferedImage.TYPE_INT_RGB);
35  Graphics2D gr = buf.createGraphics();
36 
37  for (int i = 0; i < ntiles; i++) {
38 
39  int srcRow = i / srcCols;
40  int srcCol = i % srcCols;
41 
42  int destRow = i / destCols;
43  int destCol = i % destCols;
44 
45  gr.drawImage(
46  srcImage,
47  destCol*TILE_SIZE, destRow*TILE_SIZE,
48  (destCol+1)*TILE_SIZE,(destRow+1)*TILE_SIZE,
49  srcCol*TILE_SIZE, srcRow*TILE_SIZE,
50  (srcCol+1)*TILE_SIZE, (srcRow+1)*TILE_SIZE,
51  null);
52  }
53 
54  ImageIO.write(buf, "png", outputFile);
55  }
56 }