Пример #1
0
        public static void ConvertR8ToPng(string[] args)
        {
            var srcImage = new R8Reader(File.OpenRead(args[1]));
            var shadowIndex = new int[] { };
            if (args.Contains("--noshadow"))
            {
                Array.Resize(ref shadowIndex, shadowIndex.Length + 1);
                shadowIndex[shadowIndex.Length - 1] = 3;
            }

            var palette = Palette.Load(args[2], shadowIndex);
            var startFrame = int.Parse(args[3]);
            var endFrame = int.Parse(args[4]) + 1;
            var filename = args[5];
            var frameCount = endFrame - startFrame;

            var frame = srcImage[startFrame];
            var bitmap = new Bitmap(frame.FrameSize.Width * frameCount, frame.FrameSize.Height, PixelFormat.Format8bppIndexed);
            bitmap.Palette = palette.AsSystemPalette();

            frame = srcImage[startFrame];

            if (args.Contains("--tileset"))
            {
                int f = 0;
                var tileset = new Bitmap(frame.FrameSize.Width * 20, frame.FrameSize.Height * 40, PixelFormat.Format8bppIndexed);
                tileset.Palette = palette.AsSystemPalette();

                for (int h = 0; h < 40; h++)
                {
                    for (int w = 0; w < 20; w++)
                    {
                        if (h * 20 + w < frameCount)
                        {
                            Console.WriteLine(f);
                            frame = srcImage[f];

                            var data = tileset.LockBits(new Rectangle(w * frame.Size.Width, h * frame.Size.Height, frame.Size.Width, frame.Size.Height),
                                ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

                            for (var i = 0; i < frame.Size.Height; i++)
                                Marshal.Copy(frame.Image, i * frame.Size.Width,
                                    new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Size.Width);

                            tileset.UnlockBits(data);
                            f++;
                        }
                    }
                }

                bitmap = tileset;
            }

            bitmap.Save(filename + ".png");
            Console.WriteLine(filename + ".png saved");
        }
Пример #2
0
        Sprite[] LoadSprites(string filename)
        {
            // TODO: Cleanly abstract file type detection
            if (filename.ToLower().EndsWith("r8"))
            {
                var r8 = new R8Reader(FileSystem.Open(filename));
                return r8.Select(a => SheetBuilder.Add(a.Image, a.Size, a.Offset)).ToArray();
            }

            BinaryReader reader = new BinaryReader(FileSystem.OpenWithExts(filename, exts));

            var ImageCount = reader.ReadUInt16();
            if (ImageCount == 0)
            {
                var shp = new ShpTSReader(FileSystem.OpenWithExts(filename, exts));
                return shp.Select(a => SheetBuilder.Add(a.Image, shp.Size)).ToArray();
            }
            else
            {
                var shp = new ShpReader(FileSystem.OpenWithExts(filename, exts));
                return shp.Frames.Select(a => SheetBuilder.Add(a.Image, shp.Size)).ToArray();
            }
        }
Пример #3
0
        public static void ConvertR8ToPng(string[] args)
        {
            var srcImage = new R8Reader(File.OpenRead(args[1]));
            var shouldRemap = args.Contains("--transparent");
            var palette = Palette.Load(args[2], shouldRemap);
            var startFrame = int.Parse(args[3]);
            var endFrame = int.Parse(args[4]) + 1;
            var filename = args[5];
            var FrameCount = endFrame - startFrame;

            var frame = srcImage[startFrame];
            var bitmap = new Bitmap(frame.FrameWidth * FrameCount, frame.FrameHeight, PixelFormat.Format8bppIndexed);
            bitmap.Palette = palette.AsSystemPalette();

            int OffsetX = 0;
            int OffsetY = 0;

            int x = 0;

            if (args.Contains("--vehicle")) //complex resorting to RA/CnC compatible frame order
            {
                endFrame = endFrame - (FrameCount / 2);
                startFrame--;
                for (int f = endFrame; f > startFrame; f--)
                {
                    frame = srcImage[f];

                    OffsetX = frame.FrameWidth/2 - frame.OffsetX;
                    OffsetY = frame.FrameHeight/2 - frame.OffsetY;

                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;
                }
                endFrame = endFrame + (FrameCount / 2) - 1;
                startFrame = startFrame + (FrameCount / 2) + 1;
                for (int f = endFrame; f > startFrame; f--)
                {
                    frame = srcImage[f];

                    OffsetX = frame.FrameWidth/2 - frame.OffsetX;
                    OffsetY = frame.FrameHeight/2 - frame.OffsetY;

                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;
                }
            }
            else
            {
                for (int f = startFrame; f < endFrame; f++)
                {
                    frame = srcImage[f];

                    if (args.Contains("--infrantry"))
                    {
                        OffsetX = frame.FrameWidth/2 - frame.Width/2;
                        OffsetY = frame.FrameHeight/2 - frame.Height/2;
                    }
                    else if (args.Contains("--projectile"))
                    {
                        OffsetX = frame.FrameWidth/2 - frame.OffsetX;
                        OffsetY = frame.FrameHeight/2 - frame.OffsetY;
                    }
                    else if (args.Contains("--building"))
                    {
                        if (frame.OffsetX < 0) { frame.OffsetX = 0 - frame.OffsetX; }
                        if (frame.OffsetY < 0) { frame.OffsetY = 0 - frame.OffsetY; }
                        OffsetX = 0 + frame.OffsetX;
                        OffsetY = frame.FrameHeight - frame.OffsetY;
                    }
                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;
                }
            }
            bitmap.Save(filename+".png");
            Console.WriteLine(filename+".png saved");
        }
Пример #4
0
        public static void ConvertR8ToPng(string[] args)
        {
            var srcImage = new R8Reader(File.OpenRead(args[1]));
            var shouldRemap = args.Contains("--transparent");
            var palette = Palette.Load(args[2], shouldRemap);
            var startFrame = int.Parse(args[3]);
            var endFrame = int.Parse(args[4]) + 1;
            var filename = args[5];
            var FrameCount = endFrame - startFrame;

            var frame = srcImage[startFrame];
            var bitmap = new Bitmap(frame.FrameWidth * FrameCount, frame.FrameHeight, PixelFormat.Format8bppIndexed);
            bitmap.Palette = palette.AsSystemPalette();

            int OffsetX = 0;
            int OffsetY = 0;

            int x = 0;

            frame = srcImage[startFrame];

            //TODO: This is not enough as the run and shoot animation are next to each other for each sequence in RA/CnC.
            if (args.Contains("--infantry")) //resorting to RA/CnC compatible counter-clockwise frame order
            {
                endFrame = startFrame-1;
                for (int e = 8; e < FrameCount+1; e=e+8) //assuming 8 facings each animation set
                {

                    for (int f = startFrame+e-1; f > endFrame; f--)
                    {
                        OffsetX = frame.FrameWidth/2 - frame.Width/2;
                        OffsetY = frame.FrameHeight/2 - frame.Height/2;

                        Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                        Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                        var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                            PixelFormat.Format8bppIndexed);

                        for (var i = 0; i < frame.Height; i++)
                            Marshal.Copy(frame.Image, i * frame.Width,
                                new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                        bitmap.UnlockBits(data);

                        x += frame.FrameWidth;

                        frame = srcImage[f];
                        Console.WriteLine("f: {0}", f);
                    }
                    endFrame = startFrame+e-1;
                    frame = srcImage[startFrame+e];
                    Console.WriteLine("e: {0}", e);
                    Console.WriteLine("FrameCount: {0}", FrameCount);
                }
            }
            else if (args.Contains("--vehicle")) //resorting to RA/CnC compatible counter-clockwise frame order
            {
                frame = srcImage[startFrame];

                for (int f = endFrame-1; f > startFrame-1; f--)
                {
                    OffsetX = frame.FrameWidth/2 - frame.OffsetX;
                    OffsetY = frame.FrameHeight/2 - frame.OffsetY;

                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;

                    frame = srcImage[f];
                }
            }
            else if (args.Contains("--wall")) //complex resorting to RA/CnC compatible frame order
            {
                int[] D2kBrikFrameOrder = {1, 4, 2, 12, 5, 6, 16, 9, 3, 13, 7, 8, 14, 10, 11, 15, 17, 20, 18, 28, 21, 22, 32, 25, 19, 29, 23, 24, 30, 26, 27, 31};
                foreach (int o in D2kBrikFrameOrder)
                {
                    int f = startFrame -1 + o;

                    frame = srcImage[f];

                    if (frame.OffsetX < 0) { frame.OffsetX = 0 - frame.OffsetX; }
                    if (frame.OffsetY < 0) { frame.OffsetY = 0 - frame.OffsetY; }
                    OffsetX = 0 + frame.OffsetX;
                    OffsetY = frame.FrameHeight - frame.OffsetY;
                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;
                }
            }
            else if (args.Contains("--tileset"))
            {
                int f = 0;
                var tileset = new Bitmap(frame.FrameWidth * 20, frame.FrameHeight * 40, PixelFormat.Format8bppIndexed);
                tileset.Palette = palette.AsSystemPalette();

                for (int h = 0; h < 40; h++)
                {
                    for (int w = 0; w < 20; w++)
                    {
                        if (h * 20 + w < FrameCount)
                        {
                            Console.WriteLine(f);
                            frame = srcImage[f];

                            var data = tileset.LockBits(new Rectangle(w * frame.Width, h * frame.Height, frame.Width, frame.Height),
                                ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

                            for (var i = 0; i < frame.Height; i++)
                                Marshal.Copy(frame.Image, i * frame.Width,
                                    new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                            tileset.UnlockBits(data);
                            f++;
                        }
                    }
                }
                bitmap = tileset;
            }
            else
            {
                for (int f = startFrame; f < endFrame; f++)
                {
                    frame = srcImage[f];
                    if (args.Contains("--infantrydeath"))
                    {
                        OffsetX = frame.FrameWidth/2 - frame.Width/2;
                        OffsetY = frame.FrameHeight/2 - frame.Height/2;
                    }
                    else if (args.Contains("--projectile"))
                    {
                        OffsetX = frame.FrameWidth/2 - frame.OffsetX;
                        OffsetY = frame.FrameHeight/2 - frame.OffsetY;
                    }
                    else if (args.Contains("--building"))
                    {
                        if (frame.OffsetX < 0) { frame.OffsetX = 0 - frame.OffsetX; }
                        if (frame.OffsetY < 0) { frame.OffsetY = 0 - frame.OffsetY; }
                        OffsetX = 0 + frame.OffsetX;
                        OffsetY = frame.FrameHeight - frame.OffsetY;
                    }
                    Console.WriteLine("calculated OffsetX: {0}", OffsetX);
                    Console.WriteLine("calculated OffsetY: {0}", OffsetY);

                    var data = bitmap.LockBits(new Rectangle(x+OffsetX, 0+OffsetY, frame.Width, frame.Height), ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);

                    for (var i = 0; i < frame.Height; i++)
                        Marshal.Copy(frame.Image, i * frame.Width,
                            new IntPtr(data.Scan0.ToInt64() + i * data.Stride), frame.Width);

                    bitmap.UnlockBits(data);

                    x += frame.FrameWidth;
                }
            }
            bitmap.Save(filename+".png");
            Console.WriteLine(filename+".png saved");
        }