comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/RasterToPPM.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents d766fe2d917a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.raster;
2
3 import java.awt.Color;
4
5 import java.io.IOException;
6 import java.io.OutputStream;
7
8 /**
9 * Little helper class to write a Raster into an output stream
10 * as a Netpbm PPM file.
11 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
12 */
13 public class RasterToPPM
14 {
15 private RasterToPPM() {
16 }
17
18 /**
19 * Writes a Raster to a given stream as PPM.
20 * @param raster The raster to be written.
21 * @param palette The palette used to figure out the rgb values.
22 * @param out The stream to write into.
23 * @throws IOException Thrown if some error occurred during writing
24 * data to the output stream.
25 */
26 public static void writeToPPM(
27 Raster raster,
28 Palette palette,
29 OutputStream out
30 )
31 throws IOException
32 {
33 int W = raster.getWidth();
34 int H = raster.getHeight();
35 out.write(("P6\n" + W + " " + H + "\n255\n").getBytes("US-ASCII"));
36 double [] values = raster.getValues();
37 int pos = 0;
38 byte [] data = new byte[W*3];
39 int black = Color.BLACK.getRGB();
40 for (int i = 0; i < H; ++i) {
41 for (int j = 0; j < data.length; ++pos) {
42 Palette.Entry entry = palette.getEntry(values[pos]);
43 int rgb = entry == null
44 ? black
45 : entry.getColor().getRGB();
46 data[j++] = (byte)((rgb >> 16) & 0xff);
47 data[j++] = (byte)((rgb >> 8) & 0xff);
48 data[j++] = (byte)( rgb & 0xff);
49 }
50 out.write(data);
51 }
52 out.flush();
53 }
54 }
55 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org