public static void Create(DmiImage dmi, string path) { var builder = new StringBuilder("# BEGIN DMI\n"); builder.Append("version = 4.0\n"); builder.Append("\twidth = " + dmi.StateWidth + "\n"); builder.Append("\theight = " + dmi.StateHeight + "\n"); var totalImages = dmi.States.Sum(x => x.GetFrames().Sum(y => y.GetImages().Count)); var xY = Math.Min(10, totalImages); var totalWidth = (dmi.StateWidth * xY); var totalHeight = dmi.StateHeight * (int)Math.Ceiling(totalImages / (float)xY); int pixelX = 0; int pixelY = totalHeight - 1; var img = new FreeImageBitmap(totalWidth, totalHeight, PixelFormat.Format32bppPArgb); img.FillBackground(Color.FromArgb(0, 0, 0, 0)); foreach (var state in dmi.States) { builder.AppendFormat("state = \"{0}\"\n", state.Name); builder.AppendFormat("\tdirs = {0}\n", state.Dir); builder.AppendFormat("\tframes = {0}\n", state.Frames); if (state.HasDelay) { builder.AppendFormat("\tdelay = {0}\n", state.GetDelayString); } if (state.Rewind > 0) { builder.AppendFormat("\trewind = {0}\n", state.Rewind); } foreach (var frame in state.GetFrames()) { foreach (var image in frame.GetImages()) { for (int x = 0; x < dmi.StateWidth; x++) { for (int y = 0; y < dmi.StateHeight; y++) { img.SetPixel(pixelX + x, pixelY - y, image.Bitmap.GetPixel(x, y)); } } pixelX += dmi.StateWidth; if (pixelX >= totalWidth) { pixelY -= dmi.StateHeight; pixelX = 0; } } } } builder.AppendLine("# END DMI"); if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } img.Save(path, FREE_IMAGE_FORMAT.FIF_PNG, FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION); // Work around because FREEIMAGE saves metatags as unicode. AddMetadata(path, "Description", builder.ToString()); }
public static void Create(DmiImage dmi, string path) { var builder = new StringBuilder("# BEGIN DMI\n"); builder.Append("version = 4.0\n"); builder.Append("\twidth = " + dmi.StateWidth + "\n"); builder.Append("\theight = " + dmi.StateHeight + "\n"); var totalImages = dmi.States.Sum(x => x.GetFrames().Sum(y => y.GetImages().Count)); var xY = Math.Min(10, totalImages); var totalWidth = (dmi.StateWidth * xY); var totalHeight = dmi.StateHeight * (int)Math.Ceiling(totalImages / (float)xY); int pixelX = 0; int pixelY = totalHeight - 1; var img = new FreeImageBitmap(totalWidth, totalHeight, PixelFormat.Format32bppPArgb); img.FillBackground(Color.FromArgb(0, 0, 0, 0)); foreach (var state in dmi.States) { builder.AppendFormat("state = \"{0}\"\n", state.Name); builder.AppendFormat("\tdirs = {0}\n", state.Dir); builder.AppendFormat("\tframes = {0}\n", state.Frames); if (state.HasDelay) builder.AppendFormat("\tdelay = {0}\n", state.GetDelayString); if (state.Rewind > 0) builder.AppendFormat("\trewind = {0}\n", state.Rewind); foreach (var frame in state.GetFrames()) { foreach (var image in frame.GetImages()) { for (int x = 0; x < dmi.StateWidth; x++) { for (int y = 0; y < dmi.StateHeight; y++) { img.SetPixel(pixelX + x, pixelY - y, image.Bitmap.GetPixel(x, y)); } } pixelX += dmi.StateWidth; if (pixelX >= totalWidth) { pixelY -= dmi.StateHeight; pixelX = 0; } } } } builder.AppendLine("# END DMI"); if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); img.Save(path, FREE_IMAGE_FORMAT.FIF_PNG, FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION); // Work around because FREEIMAGE saves metatags as unicode. AddMetadata(path, "Description", builder.ToString()); }
public unsafe void GetSetPixel() { Random rand = new Random(); FreeImageBitmap fib = new FreeImageBitmap(2, 1, PixelFormat.Format1bppIndexed); Palette palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(16, 1, PixelFormat.Format4bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(256, 1, PixelFormat.Format8bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format16bppRgb555); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.That(Math.Abs(orgColor.B - newColor.B) <= 8); Assert.That(Math.Abs(orgColor.G - newColor.G) <= 8); Assert.That(Math.Abs(orgColor.R - newColor.R) <= 8); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format24bppRgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format32bppArgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); Assert.AreEqual(orgColor.A, newColor.A); } fib.Dispose(); }