sascha@495: package de.intevation.gnv.raster; sascha@495: sascha@779: import java.awt.Color; sascha@779: sascha@495: import java.io.IOException; sascha@495: import java.io.OutputStream; sascha@495: sascha@495: /** sascha@801: * Little helper class to write a Raster into an output stream sascha@801: * as a Netpbm PPM file. sascha@780: * @author Sascha L. Teichmann sascha@495: */ sascha@495: public class RasterToPPM sascha@495: { sascha@495: private RasterToPPM() { sascha@495: } sascha@495: sascha@801: /** sascha@801: * Writes a Raster to a given stream as PPM. sascha@801: * @param raster The raster to be written. sascha@801: * @param palette The palette used to figure out the rgb values. sascha@801: * @param out The stream to write into. sascha@801: * @throws IOException Thrown if some error occurred during writing sascha@801: * data to the output stream. sascha@801: */ sascha@801: public static void writeToPPM( sascha@801: Raster raster, sascha@801: Palette palette, sascha@801: OutputStream out sascha@801: ) 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 :