0

I am trying to make a program in Java that uses Perlin noise to make a black and white height map. I tried using the code from here to implement the noise. I used the code below to try to make the noise make a height map but what I get is something like this instead of getting a height map.

BufferedImage img;
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB );

int[] pixel = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

for(int i = 0; i < (WIDTH*HEIGHT); i++)
{
    pixel[i] = (int) perlin.PerlinNoise(i, i);   
} 
genpfault
  • 51,148
  • 11
  • 85
  • 139
Will_Opar
  • 41
  • 5

1 Answers1

2

You can use the BufferedImage.TYPE_BYTE_GRAY as follows: Create you noise-array of type int[] where the numbers in the array should be between [0,255]. Let's assume this pixel array is called pixelData, then the following should work

BufferedImage img = new BufferedImage(WIDTH, HEIGHT, 
                                     BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setPixels(0, 0, WIDTH, HEIGHT, pixelData);

File output = new File("image.jpg");
try {
  ImageIO.write(img, "jpg", output);
} catch (IOException e) {
  e.printStackTrace();
}

If you want to have a complete example, then please look at the following question: Issue with Perlin Noise in Java

Community
  • 1
  • 1
halirutan
  • 4,281
  • 18
  • 44
  • Is the permutation[] just random value's for each point you made up for the noise or do they have to be those values? – Will_Opar Jan 03 '14 at 03:38
  • You mean in the linked *Issue with Perlin Noise in Java* post? This is not my code! It is a reference implementation by Perlin and honestly, I don't know the answer. You have to read the publication. – halirutan Jan 03 '14 at 04:08