Beispiel #1
0
        public void Save(string fileName)
        {
            var header = new KPALHeader
            {
                version       = Version,
                headerSize    = 0x1C,
                redBitDepth   = RedDepth,
                greenBitDepth = GreenDepth,
                blueBitDepth  = BlueDepth,
                alphaBitDepth = AlphaDepth,
                colorCount    = Palette.Count
            };
            var stream = File.Create(fileName);

            var buffer = new byte[4];

            using (var bw = new BinaryWriter(stream))
            {
                stream.Position = header.headerSize;
                foreach (var c in Palette)
                {
                    var value = 0;
                    value |= ChangeBitDepth(c.R, RedDepth) << (AlphaDepth + BlueDepth + GreenDepth);
                    value |= ChangeBitDepth(c.G, GreenDepth) << (AlphaDepth + BlueDepth);
                    value |= ChangeBitDepth(c.B, BlueDepth) << AlphaDepth;
                    value |= ChangeBitDepth(c.A, AlphaDepth);

                    BinaryPrimitives.WriteInt32LittleEndian(buffer, value);
                    bw.Write(buffer);
                }

                header.dataSize = (int)(stream.Length - header.headerSize);

                stream.Position = 0;
                bw.Write(Encoding.ASCII.GetBytes(header.magic));
                BinaryPrimitives.WriteInt32LittleEndian(buffer, header.version);
                bw.Write(buffer);
                bw.Write(Encoding.ASCII.GetBytes(header.devMagic));
                BinaryPrimitives.WriteInt32LittleEndian(buffer, header.headerSize);
                bw.Write(buffer);
                BinaryPrimitives.WriteInt32LittleEndian(buffer, header.dataSize);
                bw.Write(buffer);
                bw.Write(header.redBitDepth);
                bw.Write(header.greenBitDepth);
                bw.Write(header.blueBitDepth);
                bw.Write(header.alphaBitDepth);
                BinaryPrimitives.WriteInt32LittleEndian(buffer, header.colorCount);
                bw.Write(buffer);
            }
        }
Beispiel #2
0
        public void Save(string fileName)
        {
            var header = new KPALHeader
            {
                version       = Version,
                headerSize    = 0x1C,
                redBitDepth   = RedDepth,
                greenBitDepth = GreenDepth,
                blueBitDepth  = BlueDepth,
                alphaBitDepth = AlphaDepth,
                colorCount    = Palette.Count
            };
            var stream = File.Create(fileName);

            using (var bw = new BinaryWriter(stream))
            {
                stream.Position = header.headerSize;
                foreach (var c in Palette)
                {
                    int value = 0;
                    value |= Convert.ChangeBitDepth(c.R, 8, RedDepth) << (AlphaDepth + BlueDepth + GreenDepth);
                    value |= Convert.ChangeBitDepth(c.G, 8, GreenDepth) << (AlphaDepth + BlueDepth);
                    value |= Convert.ChangeBitDepth(c.B, 8, BlueDepth) << AlphaDepth;
                    value |= Convert.ChangeBitDepth(c.A, 8, AlphaDepth);

                    bw.Write(Convert.ToByteArray(value, 4, ByteOrder.LittleEndian));
                }

                header.dataSize = (int)(stream.Length - header.headerSize);

                stream.Position = 0;
                bw.Write(Encoding.ASCII.GetBytes(header.magic));
                bw.Write(Convert.ToByteArray(header.version, 4, ByteOrder.LittleEndian));
                bw.Write(Encoding.ASCII.GetBytes(header.devMagic));
                bw.Write(Convert.ToByteArray(header.headerSize, 4, ByteOrder.LittleEndian));
                bw.Write(Convert.ToByteArray(header.dataSize, 4, ByteOrder.LittleEndian));
                bw.Write(header.redBitDepth);
                bw.Write(header.greenBitDepth);
                bw.Write(header.blueBitDepth);
                bw.Write(header.alphaBitDepth);
                bw.Write(Convert.ToByteArray(header.colorCount, 4, ByteOrder.LittleEndian));
            }
        }