예제 #1
0
        public void OpenFile(string filename, Stream stream)
        {
            CurrentFile = new TEXFile(stream);

            var EArgs = new FileOpenedEventArgs(filename);

            EArgs.Platform = ((TEXFile.Platform)CurrentFile.File.Header.Platform).ToString("F");
            EArgs.Format   = ((TEXFile.PixelFormat)CurrentFile.File.Header.PixelFormat).ToString("F");
            EArgs.Mipmaps  = CurrentFile.File.Header.NumMips.ToString();
            EArgs.PreCave  = CurrentFile.IsPreCaveUpdate();
            EArgs.TexType  = EnumHelper <TEXFile.TextureType> .GetEnumDescription(((TEXFile.TextureType)CurrentFile.File.Header.TextureType).ToString());

            var mipmap = CurrentFile.GetMainMipmap();

            EArgs.Size = mipmap.Width + "x" + mipmap.Height;

            OnOpenFile(EArgs);

            byte[] argbData;

            switch ((TEXFile.PixelFormat)CurrentFile.File.Header.PixelFormat)
            {
            case TEXFile.PixelFormat.DXT1:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt1);
                break;

            case TEXFile.PixelFormat.DXT3:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt3);
                break;

            case TEXFile.PixelFormat.DXT5:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt5);
                break;

            case TEXFile.PixelFormat.ARGB:
                argbData = mipmap.Data;
                break;

            default:
                throw new Exception("Unknown pixel format?");
            }

            string   atlasExt           = "xml";
            FileInfo fileInfo           = new FileInfo(filename);
            string   fileDir            = fileInfo.DirectoryName;
            string   fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");
            string   atlasDataPath      = fileDir + @"\" + fileNameWithoutExt + "." + atlasExt;
            List <KleiTextureAtlasElement> atlasElements = new List <KleiTextureAtlasElement>();

            if (File.Exists(atlasDataPath))
            {
                atlasElements = ReadAtlasData(atlasDataPath, mipmap.Width, mipmap.Height);
            }

            var imgReader = new BinaryReader(new MemoryStream(argbData));

            Bitmap pt = new Bitmap((int)mipmap.Width, (int)mipmap.Height);

            for (int y = 0; y < mipmap.Height; y++)
            {
                for (int x = 0; x < mipmap.Width; x++)
                {
                    byte r = imgReader.ReadByte();
                    byte g = imgReader.ReadByte();
                    byte b = imgReader.ReadByte();
                    byte a = imgReader.ReadByte();
                    pt.SetPixel(x, y, Color.FromArgb(a, r, g, b));
                }
                if (OnProgressUpdate != null)
                {
                    OnProgressUpdate(y * 100 / mipmap.Height);
                }
            }

            pt.RotateFlip(RotateFlipType.RotateNoneFlipY);

            CurrentFileRaw = pt;

            OnRawImage(new FileRawImageEventArgs(pt, atlasElements));
        }