Beispiel #1
0
        public bool DrawPalImage(int imageIndex)
        {
            if (ImagesPal == null || ImagesPal.Count <= imageIndex)
            {
                return(false);
            }
            if (imageIndex < 0 || imageIndex >= ImagesPal.Count)
            {
                return(false);
            }

            SpriteImagePal sprImg = ImagesPal[imageIndex];

            if (FileHeader.Version >= 0x201 && sprImg.Decoded == false)
            {
                sprImg.Data    = RLE.Decode(sprImg.Data);
                sprImg.Decoded = true;
            }
            if (sprImg.Data == null || sprImg.Data.Length == 0 || sprImg.Width < 1 || sprImg.Height < 1)
            {
                return(false);
            }

            sprImg.Image = new Bitmap(sprImg.Width, sprImg.Height);
            var fb = new FastBitmap(sprImg.Image);

            fb.LockImage();
            for (var x = 0; x < sprImg.Width; x++)
            {
                for (var y = 0; y < sprImg.Height; y++)
                {
                    var index = (x + (y * sprImg.Width));
                    if (index >= sprImg.Data.Length)
                    {
                        fb.SetPixel(x, y, Color.Transparent);
                        continue;
                    }
                    fb.SetPixel(x, y, Palette[sprImg.Data[index]]);
                }
            }
            fb.UnlockImage();

            return(true);
        }
Beispiel #2
0
        public void AddImagePal(Bitmap image, int position)
        {
            var img = new SpriteImagePal {
                Width   = (ushort)image.Width,
                Height  = (ushort)image.Height,
                Image   = image.Clone() as Bitmap,
                Decoded = true
            };

            img.Size = (ushort)(img.Width * img.Height);
            img.Data = new byte[img.Size];

            // build data
            for (var x = 0; x < img.Width; x++)
            {
                for (var y = 0; y < img.Height; y++)
                {
                    var c = image.GetPixel(x, y);
                    var i = Palette.IndexOf(c);
                    if (i == -1)
                    {
                        continue; // @TODO: color not found?
                    }
                    img.Data[(x + (y * img.Width))] = (byte)i;
                }
            }

            if (position >= ImagesPal.Count)
            {
                ImagesPal.Add(img);
            }
            else
            {
                ImagesPal[position].Image = null; // force redraw
                ImagesPal.Insert(position, img);
            }
        }
Beispiel #3
0
        protected override bool ReadInternal()
        {
            if (base.ReadInternal() == false) {
                return false;
            }

            ImagesPal = new List<SpriteImagePal>();
            ImagesRgba = new List<SpriteImageRgba>();
            Palette = new PalFormat();

            if (FileHeader.Version.Major > 2) {
                // Unsupported version
                return false;
            }

            int imgPalCount = Reader.ReadUInt16();
            int imgRgbaCount = 0;
            if (FileHeader.Version >= 0x201) {
                imgRgbaCount = Reader.ReadUInt16();
            }

            // Images - Palette \\
            for (var i = 0; i < imgPalCount; i++) {
                var imgPal = new SpriteImagePal {
                    Width = Reader.ReadUInt16(),
                    Height = Reader.ReadUInt16()
                };
                if (FileHeader.Version >= 0x201) {
                    imgPal.Size = Reader.ReadUInt16();
                } else {
                    imgPal.Size = (ushort)(imgPal.Width * imgPal.Height);
                }
                imgPal.Data = Reader.ReadBytes(imgPal.Size);

                ImagesPal.Add(imgPal);
            }

            // Images - RGBA \\
            for (int i = 0; i < imgRgbaCount; i++) {
                var imgRgba = new SpriteImageRgba {
                    Width = Reader.ReadUInt16(),
                    Height = Reader.ReadUInt16()
                };

                int size = (imgRgba.Width * imgRgba.Height * 4);
                imgRgba.Data = Reader.ReadBytes(size);

                ImagesRgba.Add(imgRgba);
            }

            // Palette \\
            Reader.BaseStream.Position = (Reader.BaseStream.Length - (4 * PalFormat.ColorCount));

            Palette.Read(Reader.BaseStream);

            Flush();
            return true;
        }
Beispiel #4
0
        public void AddImagePal(Bitmap image, int position)
        {
            var img = new SpriteImagePal {
                Width = (ushort)image.Width,
                Height = (ushort)image.Height,
                Image = image.Clone() as Bitmap,
                Decoded = true
            };
            img.Size = (ushort)(img.Width * img.Height);
            img.Data = new byte[img.Size];

            // build data
            for (var x = 0; x < img.Width; x++) {
                for (var y = 0; y < img.Height; y++) {
                    var c = image.GetPixel(x, y);
                    var i = Palette.IndexOf(c);
                    if (i == -1) {
                        continue; // @TODO: color not found?
                    }
                    img.Data[(x + (y * img.Width))] = (byte)i;
                }
            }

            if (position >= ImagesPal.Count) {
                ImagesPal.Add(img);
            } else {
                ImagesPal[position].Image = null; // force redraw
                ImagesPal.Insert(position, img);
            }
        }
Beispiel #5
0
        protected override bool ReadInternal()
        {
            if (base.ReadInternal() == false)
            {
                return(false);
            }

            ImagesPal  = new List <SpriteImagePal>();
            ImagesRgba = new List <SpriteImageRgba>();
            Palette    = new PalFormat();

            if (FileHeader.Version.Major > 2)
            {
                // Unsupported version
                return(false);
            }

            int imgPalCount  = Reader.ReadUInt16();
            int imgRgbaCount = 0;

            if (FileHeader.Version >= 0x201)
            {
                imgRgbaCount = Reader.ReadUInt16();
            }

            // Images - Palette \\
            for (var i = 0; i < imgPalCount; i++)
            {
                var imgPal = new SpriteImagePal {
                    Width  = Reader.ReadUInt16(),
                    Height = Reader.ReadUInt16()
                };
                if (FileHeader.Version >= 0x201)
                {
                    imgPal.Size = Reader.ReadUInt16();
                }
                else
                {
                    imgPal.Size = (ushort)(imgPal.Width * imgPal.Height);
                }
                imgPal.Data = Reader.ReadBytes(imgPal.Size);

                ImagesPal.Add(imgPal);
            }

            // Images - RGBA \\
            for (int i = 0; i < imgRgbaCount; i++)
            {
                var imgRgba = new SpriteImageRgba {
                    Width  = Reader.ReadUInt16(),
                    Height = Reader.ReadUInt16()
                };

                int size = (imgRgba.Width * imgRgba.Height * 4);
                imgRgba.Data = Reader.ReadBytes(size);

                ImagesRgba.Add(imgRgba);
            }

            // Palette \\
            Reader.BaseStream.Position = (Reader.BaseStream.Length - (4 * PalFormat.ColorCount));

            Palette.Read(Reader.BaseStream);

            Flush();
            return(true);
        }