示例#1
0
文件: DDTImage.cs 项目: Weesals/ModHQ
        public static DDTImage Load(Stream file)
        {
            long beg = file.Position;
            var reader = new BinaryReader(file);
            string header = "";
            for (int h = 0; h < 3; ++h) header += reader.ReadChar();
            if (header != "RTS") throw new InvalidDataException();
            int version = (int)reader.ReadChar() - (int)'0';
            if (version == 3) {
                var img = new DDTImage();
                var serializer = new SerialReader(new BinaryReader(file));
                SerializeHeader(ref img.Header, serializer);

                int numColors = (img.Palette != null ? img.Palette.Length : 0);
                int[] paletteOffs = new int[5];
                if (img.Header.Format == ImageHeader.FormatE.Palette) {
                    // TODO: paletteOffs[img.Header.AlphaBits] = palette offset
                    serializer.Serialize(ref numColors);
                    int unknown02 = 0;
                    int palette15Off = 0;
                    serializer.Serialize(ref unknown02);
                    serializer.Serialize(ref paletteOffs[0]);       //16
                    serializer.Serialize(ref palette15Off);
                    serializer.Serialize(ref paletteOffs[1]);       //15b
                    serializer.Serialize(ref paletteOffs[4]);       //12
                }

                List<Tuple<int, int>> images = new List<Tuple<int, int>>();
                for (int l = 0; l < img.Header.MipLevels; ++l) {
                    int off = reader.ReadInt32();
                    int len = reader.ReadInt32();
                    images.Add(new Tuple<int,int>(off, len));
                }

                if (img.Header.Format == ImageHeader.FormatE.Palette) {
                    file.Seek(beg + paletteOffs[img.Header.AlphaBits], SeekOrigin.Begin);
                    byte[] paletteData = new byte[numColors * 2];
                    file.Read(paletteData, 0, paletteData.Length);

                    img.Palette = new Pixel[numColors];
                    switch (img.Header.AlphaBits) {
                        case 0: Convert565ToRGB(paletteData, 0, img.Palette); break;
                        case 1: Convert555ToRGB(paletteData, 0, img.Palette); break;
                        case 4: Convert444ToRGB(paletteData, 0, img.Palette); break;
                    }
                }

                file.Seek(beg + images[0].Item1, SeekOrigin.Begin);
                byte[] bytes = new byte[images[0].Item2];
                reader.Read(bytes, 0, bytes.Length);
                img.PixelData = bytes;
                return img;
            }
            return null;
        }
示例#2
0
文件: DDTImage.cs 项目: Weesals/ModHQ
        public static DDTImage Load(Stream file)
        {
            long   beg    = file.Position;
            var    reader = new BinaryReader(file);
            string header = "";

            for (int h = 0; h < 3; ++h)
            {
                header += reader.ReadChar();
            }
            if (header != "RTS")
            {
                throw new InvalidDataException();
            }
            int version = (int)reader.ReadChar() - (int)'0';

            if (version == 3)
            {
                var img        = new DDTImage();
                var serializer = new SerialReader(new BinaryReader(file));
                SerializeHeader(ref img.Header, serializer);

                int   numColors   = (img.Palette != null ? img.Palette.Length : 0);
                int[] paletteOffs = new int[5];
                if (img.Header.Format == ImageHeader.FormatE.Palette)
                {
                    // TODO: paletteOffs[img.Header.AlphaBits] = palette offset
                    serializer.Serialize(ref numColors);
                    int unknown02    = 0;
                    int palette15Off = 0;
                    serializer.Serialize(ref unknown02);
                    serializer.Serialize(ref paletteOffs[0]);       //16
                    serializer.Serialize(ref palette15Off);
                    serializer.Serialize(ref paletteOffs[1]);       //15b
                    serializer.Serialize(ref paletteOffs[4]);       //12
                }

                List <Tuple <int, int> > images = new List <Tuple <int, int> >();
                for (int l = 0; l < img.Header.MipLevels; ++l)
                {
                    int off = reader.ReadInt32();
                    int len = reader.ReadInt32();
                    images.Add(new Tuple <int, int>(off, len));
                }

                if (img.Header.Format == ImageHeader.FormatE.Palette)
                {
                    file.Seek(beg + paletteOffs[img.Header.AlphaBits], SeekOrigin.Begin);
                    byte[] paletteData = new byte[numColors * 2];
                    file.Read(paletteData, 0, paletteData.Length);

                    img.Palette = new Pixel[numColors];
                    switch (img.Header.AlphaBits)
                    {
                    case 0: Convert565ToRGB(paletteData, 0, img.Palette); break;

                    case 1: Convert555ToRGB(paletteData, 0, img.Palette); break;

                    case 4: Convert444ToRGB(paletteData, 0, img.Palette); break;
                    }
                }

                file.Seek(beg + images[0].Item1, SeekOrigin.Begin);
                byte[] bytes = new byte[images[0].Item2];
                reader.Read(bytes, 0, bytes.Length);
                img.PixelData = bytes;
                return(img);
            }
            return(null);
        }