public void DrawMap(Map aMap) { for (int k = 0; k < 5; k++) { for (int i = 0; i < aMap.WORLD_SIZE; i++) { for (int j = 0; j < aMap.WORLD_SIZE; j++) { MapElement _element = aMap.Elementes[aMap.WORLD_SIZE * j + i]; LevelNode.TileIndex[] _indexes = _element.pLevelNodes[k].tileIndexes; foreach (LevelNode.TileIndex _tileIndex in _indexes) { int _subIndex = _tileIndex.usTypeSubIndex - 1; if (aMap.MapTileSet.Length > _tileIndex.ubType) { StciIndexed _sti = aMap.MapTileSet[_tileIndex.ubType].Sti; if (_sti.Images.Length > _subIndex) { List <Color> _palette = _sti.ColorPalette .Select(x => new Color() { A = 255, R = x.Red, G = x.Green, B = x.Blue }) .ToList(); StciSubImage _subImage = _sti.Images[_subIndex]; StructureImage _image = new StructureImage(_subImage, _palette); int _x = (i - j) * StructureImage.TileWidth + _subImage.Header.OffsetX; int _y = (i + j) * StructureImage.TileHeight + _subImage.Header.OffsetY; if (k > 3) // крыша { _y -= 50; } Point point = new Point( _x + aMap.WORLD_SIZE * StructureImage.TileWidth / 2, _y - aMap.WORLD_SIZE * StructureImage.TileHeight / 2); Rect _rect = new Rect( point, new Size(_subImage.Header.Width, _subImage.Header.Height)); DrawingVisual _visual = new DrawingVisual(); using (DrawingContext _context = _visual.RenderOpen()) { _context.DrawImage(_image.Bitmap, _rect); } this.FVisuals[k] = _visual; base.AddVisualChild(_visual); base.AddLogicalChild(_visual); } } } } } } }
private void DrawLevel(Map aMap, int aLayerNumber, DrawingContext aContext, int aLevel) { for (int i = 0; i < aMap.WORLD_SIZE; i++) { for (int j = 0; j < aMap.WORLD_SIZE; j++) { MapElement _element = aMap.Elementes[aMap.WORLD_SIZE * j + i]; LevelNode.TileIndex[] _indexes = _element.pLevelNodes[aLayerNumber].tileIndexes; if (_indexes.Length > aLevel) { LevelNode.TileIndex _tileIndex = _indexes[aLevel]; int _subIndex = _tileIndex.usTypeSubIndex - 1; if (aMap.MapTileSet.Length > _tileIndex.ubType) { StciIndexed _sti = aMap.MapTileSet[_tileIndex.ubType].Sti; if (_sti != null && _sti.Images.Length > _subIndex) { List <Color> _palette = _sti.ColorPalette .Select(x => new Color() { A = 255, R = x.Red, G = x.Green, B = x.Blue }) .ToList(); StciSubImage _subImage = _sti.Images[_subIndex]; StructureImage _image = new StructureImage(_subImage, _palette); int _x = (i - j) * StructureImage.TileWidth / 2 + _subImage.Header.OffsetX; int _y = (i + j) * StructureImage.TileHeight / 2 + _subImage.Header.OffsetY; if (aLayerNumber > 3) // крыша { _y -= 50; } Point point = new Point( _x + aMap.WORLD_SIZE * StructureImage.TileWidth / 2, _y - aMap.WORLD_SIZE * StructureImage.TileHeight / 2); Rect _rect = new Rect( point, new Size(_subImage.Header.Width, _subImage.Header.Height)); aContext.DrawImage(_image.Bitmap, _rect); this.FDrewElementsCount++; this.FProgress.Progress = FDrewElementsCount * 100 / this.FTotalElementsCount; } } } } } }
private ImageSource GetGlobalMapImagefFromSti(string aFileName) { string _path = Path.Combine(this.FGlobalMapsFolder, aFileName); IStci _sti = null; if (File.Exists(_path)) { using (FileStream input = new FileStream(_path, FileMode.Open, FileAccess.Read)) _sti = StciLoader.LoadStci(input); } else { if (this.SlfFile != null) { SlfFile.Record _record = this.SlfFile.Records .SingleOrDefault(x => x.FileName.ToUpperInvariant() == aFileName.ToUpperInvariant()); if (_record != null) { using (MemoryStream input = new MemoryStream(_record.Data)) _sti = StciLoader.LoadStci(input); } } } if (_sti == null) { ImageSourceConverter _isc = new ImageSourceConverter(); ImageSource _is = (ImageSource)_isc.ConvertFromString("MAP_STUB.bmp"); return(_is); } else if (_sti is StciIndexed) { StciIndexed _stciIndexed = (StciIndexed)_sti; List <Color> _palette = _stciIndexed.ColorPalette .Select(x => new Color() { A = 255, R = x.Red, G = x.Green, B = x.Blue }) .ToList(); StciSubImage _subImage = _stciIndexed.Images[0]; StructureImage _image = new StructureImage(_subImage, _palette); BitmapPalette _pb = new BitmapPalette(_palette); PixelFormat _pf = PixelFormats.Indexed8; BitmapSource _bitmap = BitmapSource.Create( _subImage.Header.Width, _subImage.Header.Height, 96, 96, _pf, _pb, _subImage.ImageData, _subImage.Header.Width * _pf.BitsPerPixel / 8); return(_bitmap); } else if (_sti is StciRgb) { StciRgb _stciRgb = (StciRgb)_sti; PixelFormat _pf = PixelFormats.Bgr565; BitmapSource _bitmap = BitmapSource.Create( _stciRgb.Header.ImageWidth, _stciRgb.Header.ImageHeight, 96, 96, _pf, null, _stciRgb.ImageData, _stciRgb.Header.ImageWidth * _pf.BitsPerPixel / 8); return(_bitmap); } return(null); }
private DrawingVisual DrawMapLayerByTiles(Map aMap, int aLayerNumber) { DrawingVisual _visual = new DrawingVisual(); using (DrawingContext _context = _visual.RenderOpen()) { for (int i = 0; i < aMap.WORLD_SIZE; i++) { for (int j = 0; j < aMap.WORLD_SIZE; j++) { if (i + j > 3 * aMap.WORLD_SIZE / 2 || i + j < aMap.WORLD_SIZE / 2 || i - j > aMap.WORLD_SIZE / 2 || i - j < -aMap.WORLD_SIZE / 2) { continue; } MapElement _element = aMap.Elementes[aMap.WORLD_SIZE * j + i]; LevelNode.TileIndex[] _indexes = _element.pLevelNodes[aLayerNumber].tileIndexes; for (int _index = 0; _index < _indexes.Length; _index++) { if (_indexes.Length > _index) { LevelNode.TileIndex _tileIndex = _indexes[_index]; int _subIndex = _tileIndex.usTypeSubIndex - 1; if (aMap.MapTileSet.Length > _tileIndex.ubType) { StciIndexed _sti = aMap.MapTileSet[_tileIndex.ubType].Sti; if (_sti != null && _sti.Images.Length > _subIndex) { List <Color> _palette = _sti.ColorPalette .Select(z => new Color() { A = 255, R = z.Red, G = z.Green, B = z.Blue }) .ToList(); StciSubImage _subImage = _sti.Images[_subIndex]; StructureImage _image = new StructureImage(_subImage, _palette); int _x = (i - j) * StructureImage.TileWidth + _subImage.Header.OffsetX; int _y = (i + j) * StructureImage.TileHeight + _subImage.Header.OffsetY; // Надо поднимать всё кроме клифов. Как пока не понятно. //if (aLayerNumber == 1 && _element.sHeight > 0) //{ // int _heigthLevel = _element.sHeight % 256; // int _heigth = _element.sHeight / 256; // _y -= _heigthLevel + _heigth; //} if (aLayerNumber > 3) // крыша { _y -= 50; } Point point = new Point( _x + aMap.WORLD_SIZE * StructureImage.TileWidth / 2, _y - aMap.WORLD_SIZE * StructureImage.TileHeight / 2 ); Rect _rect = new Rect( point, new Size(_subImage.Header.Width, _subImage.Header.Height)); _context.DrawImage(_image.Bitmap, _rect); this.FDrewElementsCount++; this.FProgress.Progress = FDrewElementsCount * 100 / this.FTotalElementsCount; } } } } } } } return(_visual); }
public static GifBitmapCoder ConvertStciIndexedToGif( StciIndexed aStci, UInt16 aDelay, bool aUseTransparent, int aForeshotingNumber) { var _stciPalette = aStci.Palette; var _colors = new List <Color>(StciIndexed.NUMBER_OF_COLORS); for (int i = 0; i < StciIndexed.NUMBER_OF_COLORS; i++) { _colors.Add(Color.FromRgb(_stciPalette[i * 3], _stciPalette[i * 3 + 1], _stciPalette[i * 3 + 2])); } var _palette = new BitmapPalette(_colors); var _gifCoder = new GifBitmapCoder(_palette); _gifCoder.Extensions.Add(new GifCommentExtension("Egorov A. V. for ja2.su")); Int16 _minOffsetX = Int16.MaxValue; Int16 _minOffsetY = Int16.MaxValue; int foreshotingCount = 0; foreach (var _subImage in aStci.Images) { if (_subImage.AuxData != null && _subImage.AuxData.NumberOfFrames > 0) { foreshotingCount++; } if (aForeshotingNumber > 0 && aForeshotingNumber != foreshotingCount) { continue; } var _header = _subImage.Header; _minOffsetX = Math.Min(_minOffsetX, _header.OffsetX); _minOffsetY = Math.Min(_minOffsetY, _header.OffsetY); } if (_minOffsetX < 0 || _minOffsetY < 0) { var _shiftData = new List <byte>(4); _shiftData.AddRange(BitConverter.GetBytes(_minOffsetX)); _shiftData.AddRange(BitConverter.GetBytes(_minOffsetY)); var _shiftExtension = new GifApplicationExtension(ApplicationId, _shiftData.ToArray()); _gifCoder.Extensions.Add(_shiftExtension); } foreshotingCount = 0; foreach (var _subImage in aStci.Images) { if (_subImage.AuxData != null && _subImage.AuxData.NumberOfFrames > 0) { foreshotingCount++; } if (aForeshotingNumber > 0 && aForeshotingNumber != foreshotingCount) { continue; } var _header = _subImage.Header; var _imageSource = BitmapSource.Create( _header.Width, _header.Height, 96, 96, PixelFormats.Indexed8, _palette, _subImage.ImageData, _header.Width); var _frame = BitmapFrame.Create(_imageSource); var _offsetX = _header.OffsetX; var _offsetY = _header.OffsetY; if (_minOffsetX < 0 || _minOffsetY < 0) { // GIF format suports only positive offsets _offsetX = (short)(_offsetX - _minOffsetX); _offsetY = (short)(_offsetY - _minOffsetY); } var _bf = new GifBitmapFrame(_frame, (ushort)_offsetX, (ushort)_offsetY); if (_subImage.AuxData != null) { var _auxData = new byte[AuxObjectData.SIZE]; _subImage.AuxData.Save(new MemoryStream(_auxData)); _bf.Extensions.Add(new GifApplicationExtension(ApplicationId, _auxData)); } _bf.DisposalMethod = GifFrameDisposalMethod.RestoreToBackgroundColor; _bf.UseGlobalPalette = true; _bf.UseTransparency = aUseTransparent; _bf.Delay = aDelay; if (aUseTransparent) { _bf.TransparentColorIndex = (byte)aStci.Header.TransparentColorIndex; } _gifCoder.AddFrame(_bf); } return(_gifCoder); }
public static StciIndexed ConvertGifToStciIndexed( GifBitmapCoder aCoder, bool aIsTransparent, bool aIsTrim, int aForeshotingAmount) { List <GifBitmapFrame> _bitmaps = aCoder.Frames; if (_bitmaps.Count == 0) { return(null); } var _subHeader = new StciIndexedHeader((ushort)_bitmaps.Count); var _palette = new byte[StciIndexed.NUMBER_OF_COLORS * 3]; //if (StciIndexed.NUMBER_OF_COLORS != _bitmaps[0].Frame.Palette.Colors.Count) //{ // throw new Exception(String.Format( // "GIF file palette contains {0} colors. The {1} colors required.", // _bitmaps[0].Frame.Palette.Colors.Count, StciIndexed.NUMBER_OF_COLORS)); //} var _colorsCount = Math.Min(StciIndexed.NUMBER_OF_COLORS, _bitmaps[0].Frame.Palette.Colors.Count); for (int i = 0; i < _colorsCount; i++) { var _color = _bitmaps[0].Frame.Palette.Colors[i]; _palette[i * 3] = _color.R; _palette[i * 3 + 1] = _color.G; _palette[i * 3 + 2] = _color.B; } var _appDataSize = 0; if (_bitmaps[0].Extensions.FirstOrDefault( x => x.ExtensionType == ExtensionType.ApplicationExtension && ((GifApplicationExtension)x).ApplicationId == ApplicationId) != null) { _appDataSize = _bitmaps.Count * 16; } var _header = new StciHeader(0, _bitmaps[0].TransparentColorIndex, (uint)_appDataSize, _subHeader); if (aIsTransparent) { _header.Flags |= StciFlags.STCI_TRANSPARENT; } var _subImages = new StciSubImage[_bitmaps.Count]; var _shiftEx = aCoder.Extensions.FirstOrDefault(x => x.ExtensionType == ExtensionType.ApplicationExtension && ((GifApplicationExtension)x).ApplicationId == ApplicationId); int _shiftX = 0; int _shiftY = 0; if (_shiftEx != null) { _shiftX = BitConverter.ToInt16(_shiftEx.Data, 0); _shiftY = BitConverter.ToInt16(_shiftEx.Data, 2); } int _numberOfFrames = 0; if (aForeshotingAmount > 0) { _numberOfFrames = _bitmaps.Count / aForeshotingAmount; } // process disposal method BitmapFrame _prevFrame = null; for (int i = 0; i < _bitmaps.Count; i++) { if (_bitmaps[i].DisposalMethod == GifFrameDisposalMethod.NotDispose) { var _bf = _bitmaps[i].Frame; if (_prevFrame != null) { var _wb = new WriteableBitmap(_prevFrame); byte[] _buffer = new byte[_bf.PixelWidth * _bf.PixelHeight]; _bf.CopyPixels(_buffer, _bf.PixelWidth, 0); var _rect = new Int32Rect(_bitmaps[i].OffsetX, _bitmaps[i].OffsetY, _bf.PixelWidth, _bf.PixelHeight); if (_prevFrame.PixelWidth < _rect.X + _rect.Width || _prevFrame.PixelHeight < _rect.Y + _rect.Height) { throw new Exception("Incorrect gif file."); } _wb.WritePixels(_rect, _buffer, _bf.PixelWidth, 0); _bitmaps[i].Frame = BitmapFrame.Create(_wb); _bitmaps[i].OffsetX = _bitmaps[i - 1].OffsetX; _bitmaps[i].OffsetY = _bitmaps[i - 1].OffsetY; } _prevFrame = _bitmaps[i].Frame; } } // process trim if (aIsTrim) { for (int i = 0; i < _bitmaps.Count; i++) { _bitmaps[i].Trim(aCoder.BackgroundColorIndex); } } // create subimages for (int i = 0; i < _bitmaps.Count; i++) { var _bf = _bitmaps[i].Frame; var _subImageHeader = new StciSubImageHeader(); _subImageHeader.OffsetX = (short)(_bitmaps[i].OffsetX + _shiftX); _subImageHeader.OffsetY = (short)(_bitmaps[i].OffsetY + _shiftY); _subImageHeader.Width = (ushort)_bf.PixelWidth; _subImageHeader.Height = (ushort)_bf.PixelHeight; var _subImage = new StciSubImage(_subImageHeader); _subImage.ImageData = new byte[_subImage.Header.Width * _subImage.Header.Height]; _bf.CopyPixels(_subImage.ImageData, _subImage.Header.Width, 0); if (aForeshotingAmount > 0) { _subImage.AuxData = new AuxObjectData(); if (i % _numberOfFrames == 0) { _subImage.AuxData.Flags = AuxObjectFlags.AUX_ANIMATED_TILE; _subImage.AuxData.NumberOfFrames = (byte)_numberOfFrames; } } else { var _appDataExt = _bitmaps[i].Extensions.FirstOrDefault(x => x.ExtensionType == ExtensionType.ApplicationExtension && ((GifApplicationExtension)x).ApplicationId == ApplicationId); if (_appDataExt != null) { _subImage.AuxData = new AuxObjectData(); _subImage.AuxData.Load(new MemoryStream(_appDataExt.Data)); } } _subImages[i] = _subImage; } var _stci = new StciIndexed(_header, _palette, _subImages); return(_stci); }
public static GifBitmapCoder ConvertStciIndexedToGif(StciIndexed aStci, UInt16 aDelay, bool aUseTransparent) { return(ConvertStciIndexedToGif(aStci, aDelay, aUseTransparent)); }
public void LoadMapTileSet(Map aMap) { for (int k = 0; k < TileSet.NumOfTileTypes; k++) { string _fileName = TileSet.TileSets[aMap.TilesetID].TileSurfaceFilenames[k]; int _tilesetId = aMap.TilesetID; if (_fileName == String.Empty) { _fileName = TileSet.TileSets[0].TileSurfaceFilenames[k]; _tilesetId = 0; } _fileName = Path.Combine(_tilesetId.ToString(), _fileName); string _path = Path.Combine(this.FTilesetFolder, _fileName); StciIndexed _sti = null; if (File.Exists(_path)) { //using(FileStream input = new FileStream(_path, FileMode.Open, FileAccess.Read)) _sti = (StciIndexed)StciLoader.LoadStci(_path); } else { if (File.Exists(this.FTilesetFileName)) { SlfFile.Record _record = this.TilesetSlfFile.Records .SingleOrDefault(x => x.State == 0 && x.FileName.ToUpperInvariant() == _fileName.ToUpperInvariant()); if (_record != null) { using (MemoryStream input = new MemoryStream(_record.Data)) _sti = (StciIndexed)StciLoader.LoadStci(input, _fileName); } else { throw new FileNotFoundException( String.Format("Record {0} is not found in file {1}.", _fileName, this.FTilesetFileName)); } } else { throw new FileNotFoundException( String.Format("Files {0}, {1} are not found.", _path, this.FTilesetFileName)); } } JsdFile _jsd = null; _path = Path.ChangeExtension(_path, ".jsd"); if (File.Exists(_path)) { using (FileStream input = new FileStream(_path, FileMode.Open, FileAccess.Read)) { _jsd = JsdFile.Load(input); } } else { if (File.Exists(this.FTilesetFileName)) { _fileName = Path.ChangeExtension(_fileName, "jsd"); SlfFile.Record _record = this.TilesetSlfFile.Records .SingleOrDefault(x => x.FileName.ToUpperInvariant() == _fileName.ToUpperInvariant()); if (_record != null) { using (MemoryStream input = new MemoryStream(_record.Data)) _jsd = JsdFile.Load(input); } //else //{ // throw new FileNotFoundException( // String.Format("Record {0} is not found in file {1}.", _fileName, this.FTilesetFileName)); //} } //else //{ // throw new FileNotFoundException( // String.Format("Files {0}, {1} are not found.", _path, this.FTilesetFileName)); //} } aMap.MapTileSet[k] = new Map.TileObject(_sti, _jsd); } }
public WriteableBitmap GetMapBitmap(bool[] aDrawedLayers) { PresentationSource _ps = PresentationSource.FromVisual(Application.Current.MainWindow); Matrix _m = _ps.CompositionTarget.TransformToDevice; double _dpiX = _m.M11 * 96; double _dpiY = _m.M22 * 96; int _imageHeigth = (this.FMap.WORLD_SIZE + 2) * StructureImage.TileHeight; WriteableBitmap _wbm = new WriteableBitmap( _imageHeigth * 2, _imageHeigth, _dpiX, _dpiY, PixelFormats.Bgra32, null); int _bytePerPixel = _wbm.Format.BitsPerPixel / 8; byte[] _imageData = new byte[_imageHeigth * 2 * _imageHeigth * _bytePerPixel]; for (int aLayerNumber = 0; aLayerNumber < aDrawedLayers.Length; aLayerNumber++) { if (aDrawedLayers[aLayerNumber]) { for (int i = 0; i < this.FMap.WORLD_SIZE; i++) { //for (int i = this.FMap.WORLD_SIZE; i > 0; i--) // for (int j = this.FMap.WORLD_SIZE; j > 0; j--) for (int j = 0; j < this.FMap.WORLD_SIZE; j++) { if (i + j >= 3 * this.FMap.WORLD_SIZE / 2 || i + j <= this.FMap.WORLD_SIZE / 2 || i - j >= this.FMap.WORLD_SIZE / 2 || i - j <= -this.FMap.WORLD_SIZE / 2) { continue; } MapElement _element = this.FMap.Elementes[this.FMap.WORLD_SIZE * j + i]; LevelNode.TileIndex[] _indexes = _element.pLevelNodes[aLayerNumber].tileIndexes; for (int _index = 0; _index < _indexes.Length; _index++) { if (_indexes.Length > _index) { LevelNode.TileIndex _tileIndex = _indexes[_index]; int _subIndex = _tileIndex.usTypeSubIndex - 1; if (this.FMap.MapTileSet.Length > _tileIndex.ubType) { StciIndexed _sti = this.FMap.MapTileSet[_tileIndex.ubType].Sti; if (_sti != null && _sti.Images.Length > _subIndex) { StciSubImage _subImage = _sti.Images[_subIndex]; int _x = (i - j) * StructureImage.TileWidth + _subImage.Header.OffsetX; int _y = (i + j) * StructureImage.TileHeight + _subImage.Header.OffsetY; if (aLayerNumber > 3) // крыша { _y -= 50; } _x += this.FMap.WORLD_SIZE * StructureImage.TileWidth / 2; _y -= this.FMap.WORLD_SIZE * StructureImage.TileHeight / 2; if (_x < 0 || _x + _subImage.Header.Width > _wbm.Width || _y < 0 || _y + _subImage.Header.Height > _wbm.Height) { continue; } for (int l = 0; l < _subImage.ImageData.Length; l++) { if (_subImage.ImageData[l] != 0) { int x = _x + l % _subImage.Header.Width; int y = _y + l / _subImage.Header.Width; int _offset = (int)(y * _wbm.Width + x) * _bytePerPixel; if (_offset + 3 >= _imageData.Length) { continue; } StciColor _color = _sti.ColorPalette[_subImage.ImageData[l]]; _imageData[_offset] = _color.Blue; _imageData[_offset + 1] = _color.Green; _imageData[_offset + 2] = _color.Red; _imageData[_offset + 3] = 255; } } } } } } } } } } Int32Rect _rect = new Int32Rect(0, 0, _imageHeigth * 2, _imageHeigth); int _stride = _imageHeigth * 2 * _bytePerPixel; _wbm.WritePixels(_rect, _imageData, _stride, 0); return(_wbm); }