Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/RasterToPPM.java Fri Sep 28 12:14:00 2012 +0200 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2010 by Intevation GmbH + * + * This program is free software under the LGPL (>=v2.1) + * Read the file LGPL.txt coming with the software for details + * or visit http://www.gnu.org/licenses/ if it does not exist. + */ + +package de.intevation.gnv.raster; + +import java.awt.Color; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Little helper class to write a Raster into an output stream + * as a Netpbm PPM file. + * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> + */ +public class RasterToPPM +{ + private RasterToPPM() { + } + + /** + * Writes a Raster to a given stream as PPM. + * @param raster The raster to be written. + * @param palette The palette used to figure out the rgb values. + * @param out The stream to write into. + * @throws IOException Thrown if some error occurred during writing + * data to the output stream. + */ + public static void writeToPPM( + Raster raster, + Palette palette, + OutputStream out + ) + throws IOException + { + int W = raster.getWidth(); + int H = raster.getHeight(); + out.write(("P6\n" + W + " " + H + "\n255\n").getBytes("US-ASCII")); + double [] values = raster.getValues(); + int pos = 0; + byte [] data = new byte[W*3]; + int black = Color.BLACK.getRGB(); + for (int i = 0; i < H; ++i) { + for (int j = 0; j < data.length; ++pos) { + Palette.Entry entry = palette.getEntry(values[pos]); + int rgb = entry == null + ? black + : entry.getColor().getRGB(); + data[j++] = (byte)((rgb >> 16) & 0xff); + data[j++] = (byte)((rgb >> 8) & 0xff); + data[j++] = (byte)( rgb & 0xff); + } + out.write(data); + } + out.flush(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :