A collection of color values.
Inheritance: Asset
Exemplo n.º 1
0
        /// <summary>
        /// Read a <see cref="PaletteAsset"/> from the <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="name"></param>
        /// <param name="reader"></param>
        /// <param name="count"></param>
        /// <param name="max"></param>
        /// <param name="transparentIndex"></param>
        /// <param name="leftPadding">Colors that are added to the start of the palette, if non-null.</param>
        /// <param name="rightPadding">Colors that are added to the end of the palette, if non-null.</param>
        /// <returns></returns>
        public static PaletteAsset ReadRgb(AssetManager manager, string name, BinaryReader reader, int count, int max, int transparentIndex = -1, IEnumerable <Color> leftPadding = null, IEnumerable <Color> rightPadding = null)
        {
            PaletteAsset palette = new PaletteAsset(manager, name);

            if (leftPadding != null)
            {
                foreach (Color color in leftPadding)
                {
                    palette.ColorsMutable.Add(color);
                }
            }

            for (int index = 0; index < count; index++)
            {
                int red   = Math.Min(255, reader.ReadByte() * 255 / max);
                int green = Math.Min(255, reader.ReadByte() * 255 / max);
                int blue  = Math.Min(255, reader.ReadByte() * 255 / max);

                palette.ColorsMutable.Add(index == transparentIndex ? Color.Transparent : Color.FromArgb(red, green, blue));
            }

            if (rightPadding != null)
            {
                foreach (Color color in rightPadding)
                {
                    palette.ColorsMutable.Add(color);
                }
            }

            return(palette);
        }
Exemplo n.º 2
0
        void Load(BinaryReader reader, int width, int height, PaletteAsset palette, byte format, int[] aux = null, int auxOffset = 0)
        {
            int[] indices = new int[width * height];
            ushort dataSize;

            switch (format) {
                case 4: // Uncompressed
                    dataSize = reader.ReadUInt16();
                    if (width * height != dataSize)
                        throw new InvalidDataException();
                    reader.ReadBytesAsInt32(indices, 0, width * height);
                    break;

                case 6: // 5-bit RLE
                    dataSize = reader.ReadUInt16();
                    LoadRLE(indices, reader, width, height, 5, aux, auxOffset);
                    break;

                case 8: // 4-bit RLE
                    if (aux == null)
                        throw new NotImplementedException("Need to load auxiliary palette.");
                    dataSize = reader.ReadUInt16();
                    LoadRLE(indices, reader, width, height, 4, aux, auxOffset);
                    break;

                case 10: // 4-bit aux palette indices.
                    throw new NotImplementedException("Need to load auxiliary palette.");

                default:
                    throw new NotImplementedException("Unknown graphic format " + format + ".");
            }

            Setup(palette, width, height, indices);
        }
Exemplo n.º 3
0
 /// <summary>Setup the texture.</summary>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 protected void Setup(PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
 {
     if (pitch < 0)
     {
         pitch = width;
     }
     Indices = indices;
     Palette = palette;
     Pitch   = pitch;
     Update(width, height);
 }
Exemplo n.º 4
0
        internal Graphic(GraphicArchive archive, AssetLoader loader, int index, int length, int width, int height, PaletteAsset palette, int[] aux = null, int auxOffset = 0)
            : base(archive, loader)
        {
            BinaryReader reader = loader.Reader;

            Name = index.ToString();
            if (length == 0)
                return;

            if (width >= 0 && height >= 0) {
                int[] indices = reader.ReadBytesAsInt32(width * height);
                Setup(palette, width, height, indices);
            } else {
                byte format = reader.ReadByte();
                width = reader.ReadByte();
                height = reader.ReadByte();
                Load(reader, width, height, palette, format, aux, auxOffset);
            }

            Center = new Vector2i(width / 2 - 4, height - 4);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Read a <see cref="PaletteAsset"/> from the <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="name"></param>
        /// <param name="reader"></param>
        /// <param name="count"></param>
        /// <param name="max"></param>
        /// <param name="transparentIndex"></param>
        /// <param name="leftPadding">Colors that are added to the start of the palette, if non-null.</param>
        /// <param name="rightPadding">Colors that are added to the end of the palette, if non-null.</param>
        /// <returns></returns>
        public static PaletteAsset ReadRgb(AssetManager manager, string name, BinaryReader reader, int count, int max, int transparentIndex = -1, IEnumerable<Color> leftPadding = null, IEnumerable<Color> rightPadding = null)
        {
            PaletteAsset palette = new PaletteAsset(manager, name);

            if (leftPadding != null)
                foreach (Color color in leftPadding)
                    palette.ColorsMutable.Add(color);

            for (int index = 0; index < count; index++) {
                int red = Math.Min(255, reader.ReadByte() * 255 / max);
                int green = Math.Min(255, reader.ReadByte() * 255 / max);
                int blue = Math.Min(255, reader.ReadByte() * 255 / max);

                palette.ColorsMutable.Add(index == transparentIndex ? Color.Transparent : Color.FromArgb(red, green, blue));
            }

            if (rightPadding != null)
                foreach (Color color in rightPadding)
                    palette.ColorsMutable.Add(color);

            return palette;
        }
Exemplo n.º 6
0
 /// <summary>Initialise the texture.</summary>
 /// <param name="manager"></param>
 /// <param name="name"></param>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 public IndexedTextureAsset(AssetManager manager, string name, PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
     : this(manager, name)
 {
     Setup(palette, width, height, indices, pitch);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Initialise the texture.
 /// </summary>
 /// <param name="manager">The asset manager in use.</param>
 /// <param name="loader">The asset loader state.</param>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 public IndexedTextureAsset(AssetLoader loader, PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
     : this(loader) {
     Setup(palette, width, height, indices, pitch);
 }
Exemplo n.º 8
0
        void ReadIconAndTitle(int iconAndTitleOffset)
        {
            if (iconAndTitleOffset == 0)
                return;

            // Read the palette.
            Reader.BaseStream.Position = iconAndTitleOffset + 0x220;
            Color[] colors = new Color[16];
            for (int index = 0; index < 16; index++)
                Reader.ReadNDSColor(forceTransparent: index == 0);
            PaletteAsset palette = new PaletteAsset(Manager, "Icon palette", colors);

            // Read the icon.
            Reader.BaseStream.Position = iconAndTitleOffset + 0x20;
            Vector2i iconSize = new Vector2i(32, 32);
            Vector2i tileSize = iconSize / 8;
            int[] indices = new int[iconSize.X * iconSize.Y];

            for (Vector2i tile = Vector2i.Zero; tile.Y < tileSize.Y; tile.Y++) {
                for (tile.X = 0; tile.X < tileSize.X; tile.X++) {
                    for (Vector2i pixel = Vector2i.Zero; pixel.Y < 8; pixel.Y++) {
                        for (pixel.X = 0; pixel.X < 4; pixel.X++) {
                            int x = pixel.X + tile.X * 4;
                            int y = pixel.Y + tile.Y * 8;
                            int offset = x + y * iconSize.X;
                            byte pair = Reader.ReadByte();

                            indices[offset + 0] = pair & 0x0F;
                            indices[offset + 1] = pair >> 4;
                        }
                    }
                }
            }

            Icon = new IndexedTextureAsset(Manager, "Icon", palette, iconSize.X, iconSize.Y, indices);

            // Read the title.
            Name = Reader.ReadStringzAt(iconAndTitleOffset + 0x240, Encoding.Unicode);
            TitleEnglish = Reader.ReadStringzAt(iconAndTitleOffset + 0x340, Encoding.Unicode);
            TitleFrench = Reader.ReadStringzAt(iconAndTitleOffset + 0x440, Encoding.Unicode);
            TitleGerman = Reader.ReadStringzAt(iconAndTitleOffset + 0x540, Encoding.Unicode);
            TitleItalian = Reader.ReadStringzAt(iconAndTitleOffset + 0x640, Encoding.Unicode);
            TitleSpanish = Reader.ReadStringzAt(iconAndTitleOffset + 0x740, Encoding.Unicode);
        }
Exemplo n.º 9
0
        internal Palette(AssetLoader loader)
            : base(loader)
        {
            BinaryReader reader = loader.Reader;

            Unknowns.ReadBytes(reader, 25); // Offset 0-24
            int firstIndex = reader.ReadByte(); // Offset 25-25
            Unknowns.ReadBytes(reader, 3); // Offset 26-28
            int count = reader.ReadUInt16(); // Offset 29-30
            Unknowns.ReadBytes(reader, 1); // Offset 31-31
            byte format = reader.ReadByte(); // Offset 32-32
            Unknowns.ReadBytes(reader, 4); // Offset 33-36
            // Offset 37

            PaletteColor[] colors = new PaletteColor[count];
            Colors = new ReadOnlyCollection<PaletteColor>(colors);
            FlatColors = new Codex<Color>(256);
            for (int index = 0; index < firstIndex; index++)
                FlatColors.Add(Color.Purple);
            for (int index = 0; index < count; index++) {
                PaletteColor color;

                switch (format) {
                    case 0: // Variable (byte used, byte red, byte green, byte blue)
                        color = new PaletteColor(reader.ReadByte() != 0, reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
                        break;

                    case 1: // Constant (byte red, byte green, byte blue)
                        color = new PaletteColor(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
                        break;

                    default:
                        throw new NotImplementedException();
                }

                colors[index] = color;
                FlatColors.Add(color);
            }

            PaletteAsset = new PaletteAsset(Manager, Name ?? "Palette", FlatColors);
        }
Exemplo n.º 10
0
 /// <summary>Initialise the texture.</summary>
 /// <param name="manager"></param>
 /// <param name="name"></param>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 public IndexedTextureAsset(AssetManager manager, string name, PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
     : this(manager, name)
 {
     Setup(palette, width, height, indices, pitch);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initialise the texture.
 /// </summary>
 /// <param name="manager">The asset manager in use.</param>
 /// <param name="loader">The asset loader state.</param>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 public IndexedTextureAsset(AssetLoader loader, PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
     : this(loader)
 {
     Setup(palette, width, height, indices, pitch);
 }
Exemplo n.º 12
0
 /// <summary>Setup the texture.</summary>
 /// <param name="palette"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="indices"></param>
 /// <param name="pitch"></param>
 protected void Setup(PaletteAsset palette, int width, int height, int[] indices, int pitch = -1)
 {
     if (pitch < 0)
         pitch = width;
     Indices = indices;
     Palette = palette;
     Pitch = pitch;
     Update(width, height);
 }