Beispiel #1
0
        public static CTPKEntry FromFile(string filename, string foldername)
        {
            if (!File.Exists(filename))
                return new CTPKEntry();

            using (XmlTextReader reader = new XmlTextReader(filename))
            {
                reader.WhitespaceHandling = WhitespaceHandling.All;

                XmlSerializer serializer = new XmlSerializer(typeof(CTPKEntry));

                CTPKEntry entry = (CTPKEntry)serializer.Deserialize(reader);

                // Import image file
                entry._textureData = new Texture();

                var path = entry.FilePath;

                if (!String.IsNullOrWhiteSpace(foldername))
                    path = Path.Combine(foldername, path);

                var origbmp = Image.FromFile(path);

                var pixelSize = 3;
                var bmpPixelFormat = PixelFormat.Format24bppRgb;
                entry.Format = (int)TextureFormat.Rgb8;

                entry.HasAlpha = true;
                if (entry.HasAlpha)
                {
                    bmpPixelFormat = PixelFormat.Format32bppArgb;
                    entry.Format = (int)TextureFormat.Rgba8;
                    pixelSize = 4;
                }

                var bmp = new Bitmap(origbmp.Width, origbmp.Height, bmpPixelFormat);
                using (Graphics gr = Graphics.FromImage(bmp))
                {
                    gr.DrawImage(origbmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                }

                entry.Width = (ushort)bmp.Width;
                entry.Height = (ushort)bmp.Height;

                var scramble = new Texture().GetScrambledTextureData(bmp);
                var dataSize = bmp.Width * bmp.Height * pixelSize;

                entry.TextureRawData = new byte[dataSize];
                entry.TextureSize = (uint)dataSize;
                Array.Copy(scramble, entry.TextureRawData, dataSize);

                entry.FileTime = (uint)File.GetLastWriteTime(path).Ticks; // This is right exactly? Not sure, don't think it matters either

                var filenameData = Encoding.GetEncoding(932).GetBytes(entry.InternalFilePath);
                entry.FilenameHash = Crc32.Calculate(filenameData, filenameData.Length);

                return entry;
            }
        }
Beispiel #2
0
        public Bitmap GetBitmap()
        {
            if (TextureRawData.Length == 0)
            {
                return new Bitmap(0, 0);
            }

            _textureData = new Texture(Width, Height, (TextureFormat)Format, TextureRawData);
            return _textureData.GetBitmap();
        }