Exemplo n.º 1
0
    public static NCLR_s BitmapToPalette(string bitmap, int paletteIndex = 0)
    {
        NCLR_s       _s     = new NCLR_s();
        BinaryReader reader = new BinaryReader(File.OpenRead(bitmap));

        if (new string(reader.ReadChars(2)) != "BM")
        {
            _s.header.id = "RLCN".ToCharArray();
        }
        _s.header.endianess        = 0xfeff;
        _s.header.constant         = 0x100;
        _s.header.header_size      = 0x10;
        _s.header.nSection         = 1;
        reader.BaseStream.Position = 0x1cL;
        ushort num = reader.ReadUInt16();

        if (num == 4)
        {
            _s.pltt.depth = ColorDepth.Depth4Bit;
        }
        else if (num == 8)
        {
            _s.pltt.depth = ColorDepth.Depth8Bit;
        }
        else
        {
            Stream stream1 = reader.BaseStream;
            stream1.Position += 0x10L;
        }
        _s.pltt.nColors = reader.ReadUInt32();
        if (_s.pltt.nColors == 0)
        {
            _s.pltt.nColors = (uint)((num == 4) ? 0x10 : 0x100);
        }
        Stream baseStream = reader.BaseStream;

        baseStream.Position += 4L;
        _s.pltt.palettes     = new NTFP[paletteIndex + 1];
        _s.pltt.palettes[paletteIndex].colors = new Color[_s.pltt.nColors];
        for (int i = 0; i < _s.pltt.nColors; i++)
        {
            byte[] buffer = reader.ReadBytes(4);
            _s.pltt.palettes[paletteIndex].colors[i] = Color.FromArgb(buffer[2], buffer[1], buffer[0]);
        }
        byte[] bytes = Convertir.ColorToBGR555(_s.pltt.palettes[paletteIndex].colors);
        _s.pltt.palettes[paletteIndex].colors = Convertir.BGR555(bytes);
        _s.pltt.ID            = "TTLP".ToCharArray();
        _s.pltt.paletteLength = _s.pltt.nColors * 2;
        _s.pltt.unknown1      = 0;
        _s.pltt.length        = _s.pltt.paletteLength + 0x18;
        _s.header.file_size   = _s.pltt.length + _s.header.header_size;
        reader.Close();
        return(_s);
    }
Exemplo n.º 2
0
    public static void Escribir(NCLR_s paleta, string fileout)
    {
        BinaryWriter writer = new BinaryWriter(File.OpenWrite(fileout));

        writer.Write(paleta.header.id);
        writer.Write(paleta.header.endianess);
        writer.Write(paleta.header.constant);
        writer.Write(paleta.header.file_size);
        writer.Write(paleta.header.header_size);
        writer.Write(paleta.header.nSection);
        writer.Write(paleta.pltt.ID);
        writer.Write(paleta.pltt.length);
        writer.Write((paleta.pltt.depth == ColorDepth.Depth4Bit) ? ((ushort)3) : ((ushort)4));
        writer.Write((ushort)0);
        writer.Write((uint)0);
        writer.Write(paleta.pltt.paletteLength);
        writer.Write(0x10);
        for (int i = 0; i < paleta.pltt.palettes.Length; i++)
        {
            writer.Write(Convertir.ColorToBGR555(paleta.pltt.palettes[i].colors));
        }
        writer.Flush();
        writer.Close();
    }