comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/RasterToPPM.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org