Esempio n. 1
0
 public static void Main(string[] args)
 {
     using (var input = File.OpenRead("AtlasFlying.cfs"))
     {
         var cfs = new SpriteFile();
         cfs.Deserialize(input);
     }
 }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            bool showHelp = false;
            bool dontFixSpecialColors = false;

            var options = new OptionSet()
            {
                {
                    "ncf|no-color-fix",
                    "don't fix special colors (such as shadows, lights)",
                    v => dontFixSpecialColors = v != null
                    },
                {
                    "h|help",
                    "show this message and exit",
                    v => showHelp = v != null
                    },
            };

            List<string> extras;

            try
            {
                extras = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("{0}: ", GetExecutableName());
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
                return;
            }

            if (extras.Count < 1 || extras.Count > 2 || showHelp == true)
            {
                Console.WriteLine("Usage: {0} [OPTIONS]+ input_cfs [output_png]", GetExecutableName());
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            string inputPath = extras[0];
            string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, ".png");

            SpriteFile sprite;
            using (var input = File.OpenRead(inputPath))
            {
                sprite = new SpriteFile();
                sprite.Deserialize(input);
            }

            var bitmap = new Bitmap(
                sprite.Width * sprite.ColumnCount,
                sprite.Height * sprite.RowCount,
                PixelFormat.Format8bppIndexed);

            var palette = bitmap.Palette;
            var shadowIndex = 256 - sprite.ShadowCount;
            var lightIndex = shadowIndex - sprite.LightCount;

            for (int i = 0; i < 256; i++)
            {
                var color = sprite.Palette[i];

                var r = (int)((color >> 16) & 0xFF);
                var g = (int)((color >> 8) & 0xFF);
                var b = (int)((color >> 0) & 0xFF);
                //var a = (int)((color >> 24) & 0xFF);

                int a;

                if (i == 0)
                {
                    // transparent pixel
                    a = 0;
                }
                else if (sprite.ShadowCount > 0 && i >= shadowIndex)
                {
                    if (dontFixSpecialColors == false)
                    {
                        // make shadows black+alpha
                        a = 64 + (((i - shadowIndex) + 1) * 16);
                        r = g = b = 0;
                    }
                    else
                    {
                        a = 255;
                    }
                }
                else if (sprite.LightCount > 0 && i >= lightIndex)
                {
                    if (dontFixSpecialColors == false)
                    {
                        // make lights white+alpha
                        a = 64 + (((i - lightIndex) + 1) * 4);
                        r = g = b = 255;
                    }
                    else
                    {
                        a = 255;
                    }
                }
                    /*else if (i > sprite.MaxSolidIndex)
                {
                    a = 0;
                }*/
                else
                {
                    a = 255;
                }

                palette.Entries[i] = Color.FromArgb(a, r, g, b);
            }
            bitmap.Palette = palette;

            for (int i = 0, y = 0; y < sprite.Height * sprite.RowCount; y += sprite.Height)
            {
                for (int x = 0; x < sprite.Width * sprite.ColumnCount; x += sprite.Width)
                {
                    var frame = sprite.Frames[i++];

                    if (frame.Width == 0 ||
                        frame.Height == 0)
                    {
                        continue;
                    }

                    var area = new Rectangle(
                        x + frame.X,
                        y + frame.Y,
                        frame.Width,
                        frame.Height);

                    var data = bitmap.LockBits(area, ImageLockMode.WriteOnly, bitmap.PixelFormat);
                    var scan = data.Scan0;
                    for (int o = 0; o < frame.Height * frame.Width; o += frame.Width)
                    {
                        Marshal.Copy(frame.Pixels, o, scan, frame.Width);
                        scan += data.Stride;
                    }
                    bitmap.UnlockBits(data);
                }
            }

            bitmap.Save(outputPath, ImageFormat.Png);
        }