// read spf from file public static SPFFile FromFile(string pathToFile) { FileStream fs = new FileStream(pathToFile, FileMode.Open); SPFFile spfFile = FromStream(fs); return(spfFile); }
// read spf from stream public static SPFFile FromStream(Stream stream) { SPFFile spfFile = new SPFFile(); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); stream.Close(); BinaryReader br = new BinaryReader(new MemoryStream(buffer)); spfFile.signature = br.ReadChars(2); spfFile.width = br.ReadInt32(); spfFile.height = br.ReadInt32(); spfFile.stripCount = br.ReadInt32(); spfFile.strips = new SPFStrip[spfFile.stripCount]; for (int i = 0; i < spfFile.stripCount; i++) { int length; byte r, g, b, a; length = br.ReadInt32(); if (length < 0) { List <Color> rawColors = new List <Color>(); for (int j = 0; j < Math.Abs(length); j++) { r = br.ReadByte(); g = br.ReadByte(); b = br.ReadByte(); a = br.ReadByte(); rawColors.Add(Color.FromArgb(a, r, g, b)); } spfFile.strips[i] = new SPFStrip(length, rawColors.ToArray()); } else { r = br.ReadByte(); g = br.ReadByte(); b = br.ReadByte(); a = br.ReadByte(); spfFile.strips[i] = new SPFStrip(length, new Color[] { Color.FromArgb(a, r, g, b) }); } } br.Close(); return(spfFile); }
private void convertToSpfButton_Click(object sender, EventArgs e) { settings.spf = SPFFile.FromBitmap(settings.bit); ChangeLayout(); imageViewer.Invalidate(); stripCountLabel.Text = "Strip count: " + settings.spf.stripCount.ToString(); }
// private void LoadSPF() { settings.spf = SPFFile.FromFile(pathToFile); if (settings.spf == null) { MessageBox.Show(@"This file doesn't exists ¯\_(ツ)_/¯"); return; } else { settings.bit = settings.spf.GetImage(); ChangeLayout(); imageViewer.Invalidate(); stripCountLabel.Text = "Strip count: " + settings.spf.stripCount.ToString(); } }
// convert bitmap to spf public static SPFFile FromBitmap(Bitmap bit) { SPFFile spfFile = new SPFFile(); Color color; Color nextColor = Color.White; List <Color> rawColors = new List <Color>(); List <SPFStrip> strips = new List <SPFStrip>(); int offset = 0; // pixel offset int xx = 0, yy = 0; // pixel 2D coordinate int length = 1; // strip length int imagePixelNumber = bit.Width * bit.Height; // number of pixels // spfFile.signature = new char[] { 'S', 'P' }; spfFile.width = bit.Width; // width spfFile.height = bit.Height; // height int stripCount = 0; // algorithm for finding pixels with same color for forming strips while (offset < imagePixelNumber) { color = bit.GetPixel(xx, yy); // next color offset++; OffsetToCoordinates(ref xx, ref yy, offset, bit.Width); // while offset less that imagePixelNumber and previous pixel color equals to current pixel color while ((offset < imagePixelNumber) && (color == (nextColor = bit.GetPixel(xx, yy)))) { offset++; length++; OffsetToCoordinates(ref xx, ref yy, offset, bit.Width); } if (length == 1) { rawColors.Add(color); length = -1; // next color Color col = Color.White; while ((offset < imagePixelNumber) && (color != (col = bit.GetPixel(xx, yy)))) { rawColors.Add(col); color = col; length--; offset++; OffsetToCoordinates(ref xx, ref yy, offset, bit.Width); } strips.Add(new SPFStrip(length, rawColors.ToArray())); rawColors.Clear(); } else { strips.Add(new SPFStrip(length, new Color[] { color })); } stripCount++; length = 1; // length to default } spfFile.stripCount = stripCount; spfFile.strips = strips.ToArray(); spfFile.original = bit; return(spfFile); }