public StructureImage(StciSubImage aStciImage, List <Color> aPalette, bool aIsAnimated) : this(aStciImage, aPalette) { this.FIsAnimated = aIsAnimated; this.OffsetX += TileWidth / 2; this.OffsetY += TileHeight / 2; }
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; } } } } } }
public StructureImage(StciSubImage aStciImage, List <Color> aPalette) { this.Height = aStciImage.Header.Height; this.Width = aStciImage.Header.Width; this.OffsetX = aStciImage.Header.OffsetX; this.OffsetY = aStciImage.Header.OffsetY; BitmapPalette _pb = new BitmapPalette(aPalette); PixelFormat _pf = PixelFormats.Indexed8; this.Bitmap = BitmapSource.Create( aStciImage.Header.Width, aStciImage.Header.Height, 96, 96, _pf, _pb, aStciImage.ImageData, aStciImage.Header.Width * _pf.BitsPerPixel / 8); }
public StructureImage(StciSubImage aStciImage, List <Color> aPalette) { PixelFormat _pf = PixelFormats.Indexed8; this.Height = aStciImage.Header.Height; this.Width = aStciImage.Header.Width; this.OffsetX = aStciImage.Header.OffsetX; this.OffsetY = aStciImage.Header.OffsetY; this.Stride = aStciImage.Header.Width * _pf.BitsPerPixel / 8; aPalette[0] = Color.FromArgb(0x00, 0x00, 0x00, 0x00); BitmapPalette _pb = new BitmapPalette(aPalette); this.Bitmap = BitmapSource.Create( this.Width, this.Height, 96, 96, _pf, _pb, aStciImage.ImageData, this.Stride); }
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 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 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); }