Blame view

src/collembola/model/CollemUtils.java 1.12 KB
89f70c1ec   glaville   import current mc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  package collembola.model;
  
  import java.awt.color.ColorSpace;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Random;
  
  import javax.imageio.ImageIO;
  
  public class CollemUtils {
  	
  	public static void savePicture(int[] grid, int width, String prefix, int index) {
  		HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>();
  		Random rng = new Random();
  		
  		final int height = grid.length / width;
  		//System.out.println(Arrays.toString(grid));
  		BufferedImage img = new BufferedImage(width, height, ColorSpace.TYPE_RGB);
  		
  		for (int i = 0; i < height; i++) {
  			for (int j = 0; j < width; j++) {
  				final int id = grid[i * width + j];
  				
  				if (! colorMap.containsKey(id)) {
  					colorMap.put(id, rng.nextInt());
  				}
  				
  				img.setRGB(j, height - 1 - i, colorMap.get(id));
  			}
  		}
  		
  		try {
  			System.out.println("Writing data/" + prefix + "-" + index + ".png ...");
  			ImageIO.write(img, "png", new File("data/" + prefix + "-" + index + ".png"));
  		} catch (IOException e) {
  			throw new RuntimeException("Error saving picture", e);
  		}
  		
  	}
  	
  }