Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////
- // PGM.cs
- //
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace lab04
- {
- class PGM
- {
- private String name;
- private int width;
- private int height;
- private int maxPixelValue;
- private List<String> comments;
- private List<List<int>> pixels;
- public PGM()
- {
- this.Name = "";
- this.Width = 0;
- this.Height = 0;
- this.MaxPixelValue = 0;
- this.comments = new List<String>(0);
- this.pixels = new List<List<int>>(0);
- }
- public PGM(String name, int width, int height, int maxPixelValue)
- {
- this.Name = name;
- this.Width = width;
- this.Height = height;
- this.MaxPixelValue = maxPixelValue;
- pixels = new List<List<int>>(0);
- comments = new List<String>(0);
- }
- public void writeToFile(String fileName)
- {
- using (System.IO.StreamWriter outputFile = new StreamWriter(fileName))
- {
- outputFile.WriteLine(this.Name);
- foreach (string comment in comments)
- {
- outputFile.WriteLine(comment);
- }
- outputFile.WriteLine(this.Width + " " + this.Height);
- outputFile.WriteLine(this.MaxPixelValue);
- foreach (List<int> pixRow in pixels)
- {
- foreach (int pix in pixRow)
- {
- outputFile.Write(pix + "\t");
- }
- outputFile.Write(Environment.NewLine);
- }
- }
- }
- public List<String> getInputFromFile(String filePath)
- {
- string line;
- List<String> lines = new List<String>(0);
- System.IO.StreamReader file = new System.IO.StreamReader(filePath);
- while ((line = file.ReadLine()) != null)
- {
- lines.Add(line);
- }
- file.Close();
- return lines;
- }
- // public void generateHorizontalLines(int size, int width)
- // {
- // this.Name = "Horizontal Lines";
- // this.MaxPixelValue = 15;
- // this.Width = size;
- // this.Height = size;
- // bool flag = true;
- // for(int i = 0; i < size; i++)
- // {
- // List<int> pixelRow = new List<int>(0);
- // }
- // }
- public bool isComment(String line)
- {
- if (line[0].Equals('#'))
- {
- this.comments.Add(line);
- return true;
- }
- return false;
- }
- public void parseInput(List<String> lines)
- {
- List<String> pixelStrings = new List<String>(0);
- //String[] podzielone = filePath.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- lines.ForEach((line) =>
- {
- if (isComment(line)) { }
- else if (this.Name.Length < 1)
- {
- this.Name = line;
- }
- else if (this.Height < 1)
- {
- String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- this.Width = Convert.ToInt32(podzielone[0]);
- this.Height = Convert.ToInt32(podzielone[1]);
- }
- else if (this.MaxPixelValue < 1)
- {
- String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- this.MaxPixelValue = Convert.ToInt32(podzielone[0]);
- }
- else
- {
- pixelStrings.Add(line);
- }
- }
- );
- pixelStrings.ForEach((line) =>
- {
- String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- List<int> pixelRow = new List<int>(0);
- for(int i = 0; i < podzielone.Length; i++)
- {
- pixelRow.Add(Convert.ToInt32(podzielone[i]));
- }
- pixels.Add(pixelRow);
- });
- }
- public void addComment(String comment)
- {
- this.comments.Add("# " + comment);
- }
- public int Width { get => width; set => width = value; }
- public int Height { get => height; set => height = value; }
- public int MaxPixelValue { get => maxPixelValue; set => maxPixelValue = value; }
- public string Name { get => name; set => name = value; }
- public override string ToString()
- {
- StringBuilder stringBuilder = new StringBuilder();
- for(int i = 0; i<height; i++)
- {
- for(int j = 0; j<width; j++)
- {
- stringBuilder.Append(pixels[i][j]);
- stringBuilder.Append("\t");
- }
- stringBuilder.Append(Environment.NewLine);
- }
- stringBuilder.Append("Comments: ");
- foreach(string comment in comments)
- {
- stringBuilder.Append(Environment.NewLine);
- stringBuilder.Append(comment);
- }
- return stringBuilder.ToString();
- }
- }
- }
- ////////////////////////////////////////////////////////////
- // lab04 program class
- //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace lab04
- {
- class Program
- {
- static void Main(string[] args)
- {
- PGM pgm = new PGM();
- pgm.parseInput(pgm.getInputFromFile("pixels.pgm"));
- pgm.addComment("Losowy komentarzyk");
- pgm.writeToFile("pixelsSaved.pgm");
- Console.Out.Write(pgm.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement