Example #1
0
        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());
        }
Example #2
0
        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());
        }
Example #3
0
        private static string ExtractDMI(string input, string outPath, string file)
        {
            var ser =
                new Serializer(new List<Type> {typeof (DmiImage), typeof (DMIState), typeof (DMIFrame), typeof (DMIImageData)});
            DmiImage dmi = null;
            try
            {
                dmi = new DmiImage(file);
            }
            catch (Exception e)
            {
                _log.Error("Error during extraction",e);
                return null;
            }
            // only parse power of two
            if (!IsPowerOfTwo(dmi.StateHeight) && !IsPowerOfTwo(dmi.StateWidth))
                return null;


            var oPath = Path.Combine(outPath, Path.GetDirectoryName(file.Replace(input + "\\", "")), dmi.DmiName);
            if (!Directory.Exists(oPath))
                Directory.CreateDirectory(oPath);


            using (var stream = File.Create(Path.Combine(oPath, Datafile)))
            {
                ser.Serialize(stream, dmi);
            }


            var stateIndex = 0;
            foreach (var dmiState in dmi.States)
            {
                var statePath = Path.Combine(oPath, stateIndex.ToString());
                if (!Directory.Exists(statePath))
                    Directory.CreateDirectory(statePath);
                int frameIndex = 0;
                foreach (var frame in dmiState.GetFrames())
                {
                    var framePath = Path.Combine(statePath, frameIndex.ToString());
                    if (!Directory.Exists(framePath))
                        Directory.CreateDirectory(framePath);
                    foreach (var image in frame.GetImages())
                    {
                        var imgPath = Path.Combine(framePath, image.Dir + ".png");
                        var bitmap = !use4X ? hqx.HqxSharp.Scale2(image.Bitmap) : hqx.HqxSharp.Scale4(image.Bitmap);
                        bitmap.Save(imgPath);
                    }
                    frameIndex++;
                }
                stateIndex++;
            }
            _log.InfoFormat("Extracted {0}",file);
            return Path.Combine(oPath, Datafile);
        }