public void ConvertData() { if ((RawDataLocation != null) && (PaletteLocation != null) && (SaveFile != null)) { try { Color[] _palette = GetPalette(PaletteLocation); PixelLine[] _pixelData = GetDataBlock(_palette); Bitmap image = new Bitmap(ImageWidth, ImageHeight); for (int i = 0; i < _pixelData.Length; i++) { PixelLine pl = _pixelData[i]; for (int j = 0; j < pl.LineData.Length; j++) { image.SetPixel(j, i, pl.LineData[j]); } } if (_saveFile.Substring(_saveFile.Length - 3, 3) == "jpg") { image.Save(_saveFile, System.Drawing.Imaging.ImageFormat.Jpeg); } else { image.Save(_saveFile, System.Drawing.Imaging.ImageFormat.Gif); } } catch (Exception e) { Console.WriteLine(e); } } }
/// <summary> /// Fetches all pixels from the raw datafile and returns an array of PixelLine objects /// </summary> /// <param name="Palette">Color Palette used to decode the raw color coding</param> /// <returns></returns> private PixelLine[] GetDataBlock(Color[] Palette) { PixelLine[] pixelData = new PixelLine[_imgHeight]; //populate all lines for (int i = 0; i < _imgHeight; i++) { pixelData[i] = new PixelLine(_imgWidth); } try { using (StreamReader sr = new StreamReader(RawDataLocation)) { while (!sr.EndOfStream) { string data = sr.ReadLine(); data = data.Trim(); if (data != "") { string[] lineData = data.Split(':'); //pixel coordinates lineData[0] = lineData[0].Trim(new char[] { '(', ')' }); string[] pixelLineData = lineData[0].Split(','); int _lineNumber = Convert.ToInt32(pixelLineData[0]); int _startPixel = Convert.ToInt32(pixelLineData[1]); //trim spaces from data lineData[1] = lineData[1].Trim(); //trim off last comma lineData[1] = lineData[1].Substring(0, lineData[1].Length - 1); //split color codes string[] _helperString = lineData[1].Split(','); int[] _colorCodes = new int[_helperString.Length]; //convert color codes to colors using palette and put into the appropriate line for (int i = 0; i < _helperString.Length; i++) { int number = Convert.ToInt32(_helperString[i]); Color c = Palette[number]; pixelData[_lineNumber].AddPixelData(c, i + _startPixel); } } } } return(pixelData); } catch (Exception e) { Console.WriteLine(e); return(pixelData); } }