public bool Open(Stream _stream) { DTXFile.Header = new DTXHeader(); DTXFile.ImgLists = new List <ImgInfo>(); DTXFile.ImageDefineLists = new List <Object>(); BinaryReader _br = new BinaryReader(_stream); //UInt32 _ZipSize = _br.ReadUInt32(); //!--Test Int16 Hzip = _br.ReadInt16(); MemoryStream _decompress; DecompressData(_stream, out _decompress); _stream = _decompress; Read <DTXHeader>(_stream, ref DTXFile.Header); try { DTXFile.FuchsiaIdx = 0; if (DTXFile.Header.ImgType == 2) { Byte[] PaletteData = new Byte[512]; ReadBytes(_stream, 512, ref PaletteData); List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>(); Bitmap _bitmapPal = BitmapExtensions.BitmapSourceFromArray(PaletteData, 16, 16, 16); Bitmap bm2 = new Bitmap(_bitmapPal); bool ff = false; for (int i = 0; i < 16; ++i) { for (int j = 0; j < 16; ++j) { Color c = bm2.GetPixel(j, i); if (ff == false) { if (c.ToArgb() == Color.Fuchsia.ToArgb()) { ff = true; } else { DTXFile.FuchsiaIdx++; } } colors.Add(System.Windows.Media.Color.FromRgb(c.R, c.G, c.B)); } } DTXFile.Palette = new BitmapPalette(colors); //bm2.Save("E:\\p" + string.Format("\\{0}.bmp", DTXFile.Header.ImgType), ImageFormat.Bmp); } if (DTXFile.Header.ImgType == 0) { for (int imgidx = 0; imgidx < DTXFile.Header.ImgCount; ++imgidx) { Object _ani = null; if (DTXFile.Header.Version == 5 || DTXFile.Header.Version == 6) { _ani = ReadImageDefine <ImageDefineT50>(_stream); } else if (DTXFile.Header.Version == 7) { _ani = ReadImageDefine <ImageDefineT70>(_stream); } DTXFile.ImageDefineLists.Add(_ani); } //!--Save big image Int32 _Width = ReadInt32(_stream); Int32 _Height = ReadInt32(_stream); ImgInfo _Info = new ImgInfo(); _Info.Info = new ImgHeader(); _Info.Info.Width = _Width; _Info.Info.Height = _Height; DTXFile.ImgLists.Add(_Info); DTXFile.DataStorageSize = _Width * _Height * 2; DTXFile.DataStorage = new Byte[DTXFile.DataStorageSize]; ReadBytes(_stream, (UInt32)DTXFile.DataStorageSize, ref DTXFile.DataStorage); //!--This type all image as big image. DTXFile.Header.ImgCount = 1; } else if (DTXFile.Header.ImgType == 3) { for (int imgidx = 0; imgidx < DTXFile.Header.ImgCount; ++imgidx) { Object _ani = null; if (DTXFile.Header.Version == 7) { _ani = ReadImageDefine <ImageDefineT73>(_stream); } else //!--8 { _ani = ReadImageDefine <ImageDefineT83>(_stream); } DTXFile.ImageDefineLists.Add(_ani); ImgInfo _Info = new ImgInfo(); _Info.Info = new ImgHeader(); if (DTXFile.Header.Version == 7) { _Info.Info.Width = ((ImageDefineT73)_ani).Width; _Info.Info.Height = ((ImageDefineT73)_ani).Height; _Info.Info.GlobalY = ((ImageDefineT73)_ani).GlobalY; _Info.Info.GlobalX = ((ImageDefineT73)_ani).GlobalX; _Info.Name = ((ImageDefineT73)_ani).Name; } else { _Info.Info.Width = ((ImageDefineT83)_ani).Width; _Info.Info.Height = ((ImageDefineT83)_ani).Height; _Info.Info.GlobalY = ((ImageDefineT83)_ani).GlobalY; _Info.Info.GlobalX = ((ImageDefineT83)_ani).GlobalX; _Info.Name = ((ImageDefineT83)_ani).Name; } DTXFile.ImgLists.Add(_Info); } Int32 _Splitter = ReadInt32(_stream); Int32 _UnknowByte = ReadByte(_stream); Int32 _UnknowInt = ReadInt32(_stream); Byte _UnknowByte2 = ReadByte(_stream); Int16 _UnknowShort = ReadInt16(_stream); for (int imgidx = 0; imgidx < DTXFile.Header.ImgCount; ++imgidx) { ImgInfo _Info = DTXFile.ImgLists[imgidx]; DTXFile.ImgLists[imgidx] = ReadImageT3(_stream, ref _Info); } } else { if (DTXFile.Header.ImgCount - 1 >= 0) { for (int imgidx = 0; imgidx < DTXFile.Header.ImgCount; ++imgidx) { DTXFile.ImgLists.Add(ReadImage(_stream)); } } DTXFile.DataStorageSize = ReadInt32(_stream); DTXFile.DataStorage = new Byte[DTXFile.DataStorageSize]; ReadBytes(_stream, (UInt32)DTXFile.DataStorageSize, ref DTXFile.DataStorage); } BuildImage(); } catch (Exception ex) { Trace.WriteLine(ex.ToString()); return(false); } return(true); }
private bool BuildImage() { MemoryStream ms = null; if (DTXFile.Header.ImgType != 3) { ms = new MemoryStream(DTXFile.DataStorage); } for (int imgidx = 0; imgidx < DTXFile.Header.ImgCount; ++imgidx) { ImgInfo _i = DTXFile.ImgLists[imgidx]; if (_i.Info.Width == 0 || _i.Info.Height == 0) { //!--Error _i.Img = new System.Drawing.Bitmap(1, 1, PixelFormat.Format24bppRgb); DTXFile.ImgLists[imgidx] = _i; continue; } if (DTXFile.Header.ImgType == 0) { _i.Img = BitmapExtensions.BitmapSourceFromArray(DTXFile.DataStorage, _i.Info.Width, _i.Info.Height, 16); } else { Byte[] ImgD = Enumerable.Repeat((byte)DTXFile.FuchsiaIdx, _i.Info.Width * _i.Info.Height * DTXFile.Header.ImgType).ToArray(); System.Drawing.Bitmap ImgP = new System.Drawing.Bitmap(_i.Info.Width, _i.Info.Height, PixelFormat.Format24bppRgb); for (int ChkIdx = 0; ChkIdx < _i.Info.ChunkCount; ++ChkIdx) { if (DTXFile.Header.ImgType != 3) { ChunkInfo nChk = (ChunkInfo)_i.ChunkList[ChkIdx]; int _Row = nChk.Row; int _Pos = nChk.Pos; Byte[] Raw = new Byte[nChk.ChunkSize]; ReadBytes(ms, (uint)nChk.ChunkSize, ref Raw); Raw.CopyTo(ImgD, _Row * _i.Info.Width + _Pos); } else { ChunkInfoT3 nChk = (ChunkInfoT3)_i.ChunkList[ChkIdx]; int _Row = nChk.Row; int _Pos = nChk.Pos; int Bidx = 0; for (int x = nChk.Pos; x < nChk.Pos + nChk.Pixel; ++x) { int _Y = _Row - _i.Info.GlobalY; int _X = x - _i.Info.GlobalX; Byte R = nChk.Raw.Array[Bidx++]; Byte G = nChk.Raw.Array[Bidx++]; Byte B = nChk.Raw.Array[Bidx++]; ImgP.SetPixel(_X, _Y, Color.FromArgb(R, G, B)); } //Trace.WriteLine(String.Format("{0}\t\t{1}\t\t{2}\t\t{3} ", nChk.Row, nChk.Pos, nChk.Pixel, nChk.Pos + nChk.Pixel)); } } if (DTXFile.Header.ImgType != 3) { Bitmap _bp = null; if (DTXFile.Palette != null) { _bp = BitmapExtensions.BitmapSourceFromArrayIndex(ImgD, _i.Info.Width, _i.Info.Height, DTXFile.Header.ImgType * 8, DTXFile.Palette); } else { _bp = BitmapExtensions.BitmapSourceFromArray(ImgD, _i.Info.Width, _i.Info.Height, DTXFile.Header.ImgType * 8); } Bitmap bm = new Bitmap(_bp); _i.Img = bm; } else { _i.Img = ImgP; } } DTXFile.ImgLists[imgidx] = _i; } return(true); }