/// <summary> /// Returns an instance of System.Drawing.Image which represents visually this CAlphaSprite instance. /// A lossless conversion takes place. /// </summary> /// <returns></returns> public Image ToImage() { Bitmap bmp = new Bitmap(1, 1); if (this.Height > 0 && this.Width > 0) { bmp = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565); bmp.MakeTransparent(); int lc = 0; foreach (Line l in this.Lines) { int sc = 0; int sco = 0; foreach (Segment s in l.Segments) { sco += (int)s.Offset; for (int i = 0; i < s.PixCount; i++) { int x = sco; int y = lc; Color color = CAlphaSpriteHelper.GetColorFrom16bit(s.Pixels[i], s.Offset); try { /* Try Catch reason: * Some segments will overflow the boundaries of the sprite.. * This used to crash the old editor, but a try catch solves it. * I'll investigate the reason later, but there seems to be absolutely no data loss.*/ bmp.SetPixel(x, y, color); } catch { } sco++; } sc++; } lc++; } } return(bmp); }
/// <summary> /// Initializes a new instance of CAlphaSprite from the given System.Drawing.Image /// Warning: Lossy conversions take place. /// DarkEden Alpha Sprites have 2 color channels (5 and 6 bits) and 1 alpha channel (5 bits) /// whereas System.Drawing.Image is 32-bit in RGBA /// Colors are approximated. /// </summary> /// <param name="srcimg"></param> public CAlphaSprite(Image srcimg) { this.BodyLength = 0; this.Width = (UInt16)srcimg.Width; this.Height = (UInt16)srcimg.Height; this.LineLengths = new UInt16[this.Height]; Bitmap srcbmp = new Bitmap(srcimg); this.Lines = new Line[srcimg.Height]; for (int y = 0; y < srcimg.Height; y++) { this.Lines[y] = new Line(); UInt16 llength = 1; List <Segment> lsegs = new List <Segment>(); for (int x = 0; x < srcimg.Width;) { Segment seg = new Segment(); llength += 2; List <UInt16> pixels = new List <UInt16>(); byte offs = 0; while (srcbmp.GetPixel(x, y).A == 0) { x++; if (x >= srcimg.Width) { break; } else { offs += 1; } } seg.Offset = offs; if (x >= srcimg.Width) { llength -= 2; break; } Color c = srcbmp.GetPixel(x, y); while (c.A > 0) { pixels.Add(CAlphaSpriteHelper.Get16bitFromColor(c)); llength += 2; x++; if (x >= srcimg.Width) { break; } c = srcbmp.GetPixel(x, y); } seg.PixCount = (byte)pixels.Count; seg.Pixels = pixels.ToArray(); lsegs.Add(seg); } this.LineLengths[y] = llength; this.BodyLength += llength; this.Lines[y].SegmentCount = (byte)lsegs.Count; this.Lines[y].Segments = lsegs.ToArray(); } this.ByteCount = this.GetBytes().Length; }