public static string GetMagicWord(PNMType ptype, PNMEncoding encoding) { if (ptype == PNMType.PGM && encoding == PNMEncoding.ASCIIEncoding) { return("P2"); } if (ptype == PNMType.PGM && encoding == PNMEncoding.BinaryEncoding) { return("P5"); } if (ptype == PNMType.PPM && encoding == PNMEncoding.ASCIIEncoding) { return("P3"); } if (ptype == PNMType.PPM && encoding == PNMEncoding.BinaryEncoding) { return("P6"); } if (ptype == PNMType.PBM && encoding == PNMEncoding.ASCIIEncoding) { return("P1"); } if (ptype == PNMType.PBM && encoding == PNMEncoding.BinaryEncoding) { throw new Exception("PBM files are only written in ASCII encoding."); } return("P5"); }
public static string GetMaxPixel(PNMType ptype) { switch (ptype) { case PNMType.PBM: return("-1"); // dun put in header } return("255"); }
public static IPNMWriter GetIPNMWriter(PNMType ptype) { IPNMWriter imWriter = null; switch (ptype) { case PNMType.PBM: imWriter = new PBMWriter(); break; case PNMType.PGM: imWriter = new PGMWriter(); break; case PNMType.PPM: imWriter = new PPMWriter(); break; } return(imWriter); }
public static void WritePNM(string FilePath, System.Drawing.Image im, PNMEncoding encoding, PNMType ptype) { FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None); IPNMDataWriter dw = PNMFactory.GetIPNMDataWriter(fs, encoding); if (dw == null) { throw new Exception ("Currently only Binary and ASCII encoding is supported"); } try { //write image header dw.WriteLine(PNMFactory.GetMagicWord(ptype, encoding)); dw.WriteLine(im.Width.ToString() + " " + im.Height.ToString()); if (ptype != PNMType.PBM) { dw.WriteLine("255"); } IPNMWriter imWriter = PNMFactory.GetIPNMWriter(ptype); imWriter.WriteImageData(dw, im); } catch { throw; } }