public static int[,] LoadPNM(string imgf) { var textr = new StreamReader(imgf); string pnmformat = textr.ReadNextChunkDiscardComments(); if (pnmformat != "P1" && pnmformat != "P2") throw new Exception("PNM format "+pnmformat+" not supported"); int width = textr.ReadNextChunkDiscardComments().ToInt(); int height = textr.ReadNextChunkDiscardComments().ToInt(); int maxval = 0; if (pnmformat != "P1") maxval = textr.ReadNextChunkDiscardComments().ToInt(); //maxval = 255; // ignoring value written in file if (pnmformat == "P1") return LoadPBMData(textr, width, height); else if (pnmformat == "P2") return LoadPGMData(textr, width, height, maxval); throw new Exception("PNM format "+pnmformat+" didn't match supported"); }
public static int[,] LoadPGMData(StreamReader textr, int width, int height, int maxval) { int[,] data = new int[width, height]; int y = 0; int x = 0; while (true) { string nexts = textr.ReadNextChunkDiscardComments(); if (nexts == null) break; data[x,y] = nexts.ToInt(); ++x; if (x >= width) { ++y; x = 0; } } if (y != height || x != 0) { throw new Exception("specified dimensions not correct, have ("+x.ToString()+","+y.ToString()+") instead of ("+width.ToString()+","+height.ToString()+")"); } textr.Close(); return data; }