Advertisement
Guest User

DF2csv

a guest
Jun 13th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.awt.image.BufferedImage;
  6.  
  7. import javax.imageio.ImageIO;
  8.  
  9. public class Main {
  10.  
  11.    
  12.     static File png;
  13.     static File csv;
  14.     static BufferedImage img= null;
  15.    
  16.     static int gridWidth = 1;
  17.     static int gridHeight = 1;
  18.    
  19.     Main(){};
  20.    
  21.     /**
  22.      * @param args pngname;csvname;RGB as Hex;sign;gridWidth;gridHeight
  23.      */
  24.     public static void main(String[] args) {
  25.         if (args[0].equals("help")){
  26.             System.out.println("A simple Program to convert a png into an csv, which then can be imported into quickfort, which can be used to design a Fortress in Dwarf Fortress" +
  27.                     "\n\n" +
  28.                     "The Number of Arguments must be 6 or 4:\n" +
  29.                     "pngpath: path to the png \n" +
  30.                     "csvname: path to the csv that should be generated \n" +
  31.                     "RGB_as_6Hex: the colour which should be used to determinate the place of the sign. Must consist of 6 hexnumbers \n" +
  32.                     "sign: The letter, or sign which will be used. f.e. d for dig \n" +
  33.                     "gridWidth: How many pixels should be jumped horizontally. 1 means every pixel \n" +
  34.                     "gridHeight: hom many pixels should be jumped vertically. 1 means every pixel\n" +
  35.                     "alpha gets ignored, so care that it has a completely different colour");
  36.             System.exit(0);
  37.         }
  38.         if ((args.length > 6) || (args.length != 4 && args.length != 6))
  39.         {
  40.             System.out.println("Wrong Number of Arguments!\n" +
  41.                     "Must be 6:\n" +
  42.                     " pngname csvname RGB_as_6Hex sign gridWidth gridHeight\n" +
  43.                     "or 4: \n" +
  44.                     " pngname csvname RGB_as_6Hex sign \n" +
  45.                     "  grid is set to 1\n" +
  46.                     "write help as argument for help" );
  47.             System.exit(2);
  48.             }
  49.         if (args.length == 6){
  50.             if ((args[4].equals("0")) || (args[5].equals("0"))){
  51.                 System.out.println("gridsize isn't allowed to be zero. \n" +
  52.                         "write help as argument for help");
  53.                 System.exit(3);
  54.             }
  55.             gridWidth = Integer.valueOf(args[4]);
  56.             gridHeight = Integer.valueOf(args[5]);
  57.         }      
  58.         if (args[2].length() > 6){
  59.             System.out.println("Colour Bitmap too long, Max 6 HexNumbers \n" +
  60.                     "White = FFFFFF\n" +
  61.                     "Black = 000000\n" +
  62.                     "Red = FF0000\n" +
  63.                     "Green = 00FF00\n" +
  64.                     "Blue = 0000FF");
  65.             System.exit(2);}
  66.        
  67.     try{
  68.         png = new File(args[0]);
  69.         if (!png.exists()){
  70.             System.out.println("Error: pngFile doesn't exists");           
  71.             System.exit(1);
  72.         }
  73.         csv = new File(args[1]);
  74.         if (!csv.exists()){
  75.             csv.createNewFile();   
  76.         };
  77.        
  78.         //png read
  79.         img = ImageIO.read(png);
  80.         BufferedWriter out =new BufferedWriter( new FileWriter(csv));      
  81.         int col = Integer.valueOf(args[2],16) & 0xFFFFFF;
  82. //      String cox = "000000";
  83. //      int col = Integer.valueOf(cox, 16);
  84.        
  85.         for(int h = 0; h < img.getHeight();h+=gridHeight){
  86.         for(int w = 0; w < img.getWidth();w += gridWidth){
  87.             String s = "";
  88. //          int g = img.getRGB(w, h) ;
  89.             if( (img.getRGB(w, h) & 0x00FFFFFF) == col)
  90.             {
  91.                 s =""+args[3];
  92.             }
  93.             out.write(s);                          
  94.             out.write(';');
  95.             }
  96.         out.write("\n");   
  97.         }
  98.         out.close();
  99.         System.out.println("Size:\n" +
  100.                 "Heigh:" + (double)img.getHeight()/gridHeight + "\n" +
  101.                 "Width:" + (double)img.getWidth()/gridWidth);
  102.        
  103.         System.out.println("Done...");
  104.         System.exit(0);
  105.         // TODO Auto-generated method stub
  106.     } catch (IOException e) { e.printStackTrace();     
  107.        
  108.     }
  109.    
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement