Ejemplo n.º 1
0
        /// <summary>
        /// Loads the default palettes from the specified archive.
        /// </summary>
        /// <param name="pattern">Pattern to check for (prefix string).</param>
        /// <param name="path">Folder path to read from.</param>
        /// <returns>Number of palettes loaded.</returns>
        public int LoadPalettes(string pattern, string path)
        {
            // Get Files
            string[] files = Directory.GetFiles(path, pattern + "*.PAL", SearchOption.TopDirectoryOnly);

            palettes.Clear();
            foreach (string file in files)
            {
                // Check for Palette
                if (Path.GetFileName(file).ToUpper().EndsWith(".PAL") && Path.GetFileName(file).ToUpper().StartsWith(pattern.ToUpper()))
                {
                    // Get Index
                    int index = Convert.ToInt32(
                        Path.GetFileNameWithoutExtension(file).Remove(0, pattern.Length));

                    // Get File from File
                    Palette256 palette = Palette256.FromFile(file);

                    // Add Palette
                    palettes.Add(index, palette);
                }
            }

            // Return Count
            return(palettes.Count);
        }
Ejemplo n.º 2
0
 public static Palette256 FromArchive(string file, bool ignoreCase, DATArchive archive)
 {
     if (!archive.Contains(file, ignoreCase))
     {
         return((Palette256)null);
     }
     return(Palette256.FromRawData(archive.ExtractFile(file, ignoreCase)));
 }
Ejemplo n.º 3
0
        private static Palette256 LoadPalette(Stream stream)
        {
            stream.Seek(0L, SeekOrigin.Begin);
            BinaryReader binaryReader = new BinaryReader(stream);
            Palette256   palette256   = new Palette256();

            for (int index = 0; index < 256; ++index)
            {
                palette256.colors[index] = Color.FromArgb((int)binaryReader.ReadByte(), (int)binaryReader.ReadByte(), (int)binaryReader.ReadByte());
            }
            palette256.colors[0] = Color.FromArgb(0, palette256.colors[0]);
            return(palette256);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Loads the default palettes from the specified archive.
        /// </summary>
        /// <param name="pattern">Pattern to check for (prefix string).</param>
        /// <param name="archive">Data archive.</param>
        /// <returns>Number of palettes loaded.</returns>
        public int LoadPalettes(string pattern, DATArchive archive)
        {
            palettes.Clear();
            foreach (DATFileEntry file in archive.Files)
            {
                // Check for Palette
                if (file.Name.ToUpper().EndsWith(".PAL") && file.Name.ToUpper().StartsWith(pattern.ToUpper()))
                {
                    // Get Index
                    int index = Convert.ToInt32(
                        Path.GetFileNameWithoutExtension(file.Name).Remove(0, pattern.Length));

                    // Get File from Archive
                    Palette256 palette = Palette256.FromArchive(file.Name, archive);

                    // Add Palette
                    palettes.Add(index, palette);
                }
            }
            return(palettes.Count);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Internal function that loads a palette from a given data stream.
        /// </summary>
        /// <param name="stream">Data stream.</param>
        /// <returns>256 color palette.</returns>
        private static Palette256 LoadPalette(Stream stream)
        {
            // Create Binary Ready
            stream.Seek(0, SeekOrigin.Begin);
            BinaryReader reader = new BinaryReader(stream);

            // Create Palette
            Palette256 pal = new Palette256();

            #region Load Colors
            for (int i = 0; i < 256; i++)
            {
                // Get Color ByteS (RGB)
                pal.colors[i] = Color.FromArgb(
                    reader.ReadByte(),
                    reader.ReadByte(),
                    reader.ReadByte());
            }
            #endregion

            // Return Palette
            return(pal);
        }
Ejemplo n.º 6
0
 public int LoadPalettes(string pattern, string path)
 {
     string[] files = Directory.GetFiles(path, pattern + "*.PAL", SearchOption.TopDirectoryOnly);
     this.palettes.Clear();
     foreach (string str in files)
     {
         if (Path.GetFileName(str).ToUpper().EndsWith(".PAL") && Path.GetFileName(str).ToUpper().StartsWith(pattern.ToUpper()))
         {
             this.palettes.Add(Convert.ToInt32(Path.GetFileNameWithoutExtension(str).Remove(0, pattern.Length)), Palette256.FromFile(str));
         }
     }
     return(this.palettes.Count);
 }
Ejemplo n.º 7
0
 public int LoadPalettes(string pattern, DATArchive archive)
 {
     this.palettes.Clear();
     foreach (DATFileEntry file in archive.Files)
     {
         if (file.Name.ToUpper().EndsWith(".PAL") && file.Name.ToUpper().StartsWith(pattern.ToUpper()))
         {
             this.palettes.Add(Convert.ToInt32(Path.GetFileNameWithoutExtension(file.Name).Remove(0, pattern.Length)), Palette256.FromArchive(file.Name, archive));
         }
     }
     return(this.palettes.Count);
 }
Ejemplo n.º 8
0
        private unsafe static Bitmap SimpleRender(int width, int height, byte[] data, Palette256 palette, ImageType type)
        {
            Bitmap image = new Bitmap(width, height);

            BitmapData bmd = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, image.PixelFormat);

            for (int y = 0; y < bmd.Height; y++)
            {
                byte *row = (byte *)bmd.Scan0 + (y * bmd.Stride);

                for (int x = 0; x < bmd.Width; x++)
                {
                    int colorIndex = 0;
                    if (type == ImageType.EPF)
                    {
                        colorIndex = data[x * height + y];
                    }
                    else
                    {
                        colorIndex = data[y * width + x];
                    }

                    if (colorIndex == 0)
                    {
                        continue;
                    }

                    #region 32 Bit Render
                    if (bmd.PixelFormat == PixelFormat.Format32bppArgb)
                    {
                        row[x * 4]     = palette[colorIndex].B;
                        row[x * 4 + 1] = palette[colorIndex].G;
                        row[x * 4 + 2] = palette[colorIndex].R;
                        row[x * 4 + 3] = palette[colorIndex].A;
                    }
                    #endregion

                    #region 24 Bit Render
                    else if (bmd.PixelFormat == PixelFormat.Format24bppRgb)
                    {
                        row[x * 3]     = palette[colorIndex].B;
                        row[x * 3 + 1] = palette[colorIndex].G;
                        row[x * 3 + 2] = palette[colorIndex].R;
                    }
                    #endregion

                    #region 15 Bit Render
                    else if (bmd.PixelFormat == PixelFormat.Format16bppRgb555)
                    {
                        ushort colorWORD = (ushort)(((palette[colorIndex].R & 248) << 7) +
                                                    ((palette[colorIndex].G & 248) << 2) +
                                                    (palette[colorIndex].B >> 3));

                        row[x * 2]     = (byte)(colorWORD % 256);
                        row[x * 2 + 1] = (byte)(colorWORD / 256);
                    }
                    #endregion

                    #region 16 Bit Render
                    else if (bmd.PixelFormat == PixelFormat.Format16bppRgb565)
                    {
                        ushort colorWORD = (ushort)(((palette[colorIndex].R & 248) << 8)
                                                    + ((palette[colorIndex].G & 252) << 3) +
                                                    (palette[colorIndex].B >> 3));

                        row[x * 2]     = (byte)(colorWORD % 256);
                        row[x * 2 + 1] = (byte)(colorWORD / 256);
                    }
                    #endregion
                }
            }

            // Unlock Bits
            image.UnlockBits(bmd);

            // Flip Image
            if (type == ImageType.EPF)
            {
                image.RotateFlip(RotateFlipType.Rotate90FlipX);
            }

            // Return Bitmap
            return(image);
        }
Ejemplo n.º 9
0
 public unsafe static Bitmap RenderTile(byte[] tileData, Palette256 palette)
 {
     return(SimpleRender(Tileset.TileWidth, Tileset.TileHeight, tileData, palette, ImageType.Tile));
 }
Ejemplo n.º 10
0
 public unsafe static Bitmap RenderImage(MPFFrame mpf, Palette256 palette)
 {
     return(SimpleRender(mpf.Width, mpf.Height, mpf.RawData, palette, ImageType.MPF));
 }
Ejemplo n.º 11
0
 public unsafe static Bitmap RenderImage(HPFImage hpf, Palette256 palette)
 {
     return(SimpleRender(hpf.Width, hpf.Height, hpf.RawData, palette, ImageType.HPF));
 }
Ejemplo n.º 12
0
 public unsafe static Bitmap RenderImage(EPFFrame epf, Palette256 palette)
 {
     return(SimpleRender(epf.Width, epf.Height, epf.RawData, palette));
 }
Ejemplo n.º 13
0
 public static Palette256 FromRawData(byte[] data)
 {
     return(Palette256.LoadPalette((Stream) new MemoryStream(data)));
 }
Ejemplo n.º 14
0
 public static Palette256 FromFile(string file)
 {
     return(Palette256.LoadPalette((Stream) new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)));
 }
Ejemplo n.º 15
0
        public unsafe Bitmap Render(int frameIndex)
        {
            // Create Bitmap
            Bitmap image = new Bitmap(width, height);

            // Lock Bits
            BitmapData bmd = image.LockBits(
                new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.WriteOnly,
                image.PixelFormat);

            MPFFrame   frame = frames[frameIndex];
            Palette256 pal   = Palette256.FromArchive(palette, DATArchive.Hades);

            // Render Image
            for (int y = 0; y < frame.Height; y++)
            {
                byte *row = (byte *)bmd.Scan0 + ((y + frame.Top) * bmd.Stride);

                for (int x = 0; x < frame.Width; x++)
                {
                    int xIndex     = x + frame.Left;
                    int colorIndex = frame.RawData[y * frame.Width + x];

                    if (colorIndex > 0)
                    {
                        #region 32 Bit Render
                        if (bmd.PixelFormat == PixelFormat.Format32bppArgb)
                        {
                            row[xIndex * 4]     = pal[colorIndex].B;
                            row[xIndex * 4 + 1] = pal[colorIndex].G;
                            row[xIndex * 4 + 2] = pal[colorIndex].R;
                            row[xIndex * 4 + 3] = pal[colorIndex].A;
                        }
                        #endregion

                        #region 24 Bit Render
                        else if (bmd.PixelFormat == PixelFormat.Format24bppRgb)
                        {
                            row[xIndex * 3]     = pal[colorIndex].B;
                            row[xIndex * 3 + 1] = pal[colorIndex].G;
                            row[xIndex * 3 + 2] = pal[colorIndex].R;
                        }
                        #endregion

                        #region 15 Bit Render
                        else if (bmd.PixelFormat == PixelFormat.Format16bppRgb555)
                        {
                            // Get 15-Bit Color
                            ushort colorWORD = (ushort)(((pal[colorIndex].R & 248) << 7) +
                                                        ((pal[colorIndex].G & 248) << 2) +
                                                        (pal[colorIndex].B >> 3));

                            row[xIndex * 2]     = (byte)(colorWORD % 256);
                            row[xIndex * 2 + 1] = (byte)(colorWORD / 256);
                        }
                        #endregion

                        #region 16 Bit Render
                        else if (bmd.PixelFormat == PixelFormat.Format16bppRgb565)
                        {
                            // Get 16-Bit Color
                            ushort colorWORD = (ushort)(((pal[colorIndex].R & 248) << 8)
                                                        + ((pal[colorIndex].G & 252) << 3) +
                                                        (pal[colorIndex].B >> 3));

                            row[xIndex * 2]     = (byte)(colorWORD % 256);
                            row[xIndex * 2 + 1] = (byte)(colorWORD / 256);
                        }
                        #endregion
                    }
                }
            }

            // Unlock Bits
            image.UnlockBits(bmd);

            // Return Bitmap
            return(image);
        }