sascha@495: package de.intevation.gnv.raster; sascha@495: sascha@495: import java.io.IOException; sascha@495: import java.io.OutputStream; sascha@495: sascha@495: import java.awt.Color; sascha@495: sascha@495: /** sascha@495: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@495: */ sascha@495: public class RasterToPPM sascha@495: { sascha@495: private RasterToPPM() { sascha@495: } sascha@495: sascha@495: public static void writeToPPM(Raster raster, Palette palette, OutputStream out) sascha@495: throws IOException sascha@495: { sascha@495: int W = raster.getWidth(); sascha@495: int H = raster.getHeight(); sascha@495: out.write(("P6\n" + W + " " + H + "\n255\n").getBytes("US-ASCII")); sascha@495: double [] values = raster.getValues(); sascha@495: int pos = 0; sascha@495: byte [] data = new byte[W*3]; sascha@495: int black = Color.BLACK.getRGB(); sascha@495: for (int i = 0; i < H; ++i) { sascha@495: for (int j = 0; j < data.length; ++pos) { sascha@495: Palette.Entry entry = palette.getEntry(values[pos]); sascha@495: int rgb = entry == null sascha@495: ? black sascha@495: : entry.getColor().getRGB(); sascha@495: data[j++] = (byte)((rgb >> 16) & 0xff); sascha@495: data[j++] = (byte)((rgb >> 8) & 0xff); sascha@495: data[j++] = (byte)( rgb & 0xff); sascha@495: } sascha@495: out.write(data); sascha@495: } sascha@495: out.flush(); sascha@495: } sascha@495: } sascha@495: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :