Пример #1
0
 public TileBitmap CreateBitmapByTile(LevelDef level, int beginRow, int beginCol, int width, int height, TileIdentify tile, out bool memoryIsNotEnough)
 {
     memoryIsNotEnough = false;
     try
     {
         T[][] buffers = null;
         bool  isOK    = ReadRasterFile(out buffers, beginRow, beginCol, width, height, tile);
         if (!isOK)
         {
             return(null);
         }
         TileBitmap tb = new TileBitmap();
         tb.Level  = level;
         tb.Tile   = tile;
         tb.Bitmap = BuildBitmap(buffers, level, tile);
         return(tb);
     }
     catch (OutOfMemoryException)
     {
         memoryIsNotEnough = true;
     }
     catch (ArgumentException)
     {
         memoryIsNotEnough = true;
     }
     return(null);
 }
Пример #2
0
        private void CreateLevelBufferWholeFile(FileStream fs, int bandNo, LevelDef levelDef)
        {
            Size  lvSize      = levelDef.Size;
            float scale       = levelDef.Scale;
            int   rowsOfBlock = lvSize.Height;
            int   blockCount  = 0;

            byte[]   buffer        = CreateBuffer(lvSize.Width, ref rowsOfBlock, out blockCount);
            GCHandle handle        = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   bufferPointer = handle.AddrOfPinnedObject();

            try
            {
                int         bRow = 0, eRow = 0, actualRows = 0;
                int         oBeginRow = 0, oEndRow = 0;
                IRasterBand raster = _dataProvider.GetRasterBand(bandNo);
                for (int i = 0; i < blockCount; i++, bRow += rowsOfBlock)
                {
                    eRow       = Math.Min(bRow + rowsOfBlock, lvSize.Height);
                    actualRows = eRow - bRow;
                    oBeginRow  = (int)(bRow / scale);
                    oEndRow    = (int)(eRow / scale);
                    raster.Read(0, oBeginRow, raster.Width, oEndRow - oBeginRow,
                                bufferPointer, raster.DataType,
                                lvSize.Width, actualRows);
                    fs.Write(buffer, 0, actualRows * lvSize.Width * _dataTypeSize);
                }
            }
            finally
            {
                handle.Free();
            }
        }
Пример #3
0
        public LevelDef[] GetLevelDefs(int width, int height)
        {
            int             rowCount = 0, colCount = 0;//not use
            int             levelCount = GetLevelCount(width, height);
            List <LevelDef> levels     = new List <LevelDef>(levelCount);

            for (int i = 0; i < levelCount; i++)
            {
                Size           size  = GetSizeByLevel(i, width, height);
                TileIdentify[] tiles = GetTileIdentifies(size.Width, size.Height, out rowCount, out colCount);
                //
                LevelDef level = new LevelDef();
                level.Rows           = rowCount;
                level.Cols           = colCount;
                level.LevelNo        = i;
                level.Scale          = Math.Max(size.Width / (float)width, size.Height / (float)height);
                level.Size           = size;
                level.TileIdentities = tiles;
                if (tiles != null)
                {
                    foreach (TileIdentify t in tiles)
                    {
                        t.Level = level;
                    }
                }
                levels.Add(level);
            }
            return(levels.Count > 0 ? levels.ToArray() : null);
        }
Пример #4
0
        public int LoadTileBitmaps(LevelDef[] levels, bool isSync)
        {
            _levels = levels;
            TileIdentify[] visibleTiles;
            LevelDef       nearestLevel;

            _tilesLocator.Compute(_canvas, out nearestLevel, out visibleTiles);
            if (nearestLevel.IsEmpty())
            {
                return(0);
            }
            try
            {
                _currentLevel = nearestLevel;
                if (visibleTiles != null)
                {
                    foreach (TileIdentify tile in visibleTiles)
                    {
                        if (isSync)//同步
                        {
                            LoadTileBitmap(tile);
                        }
                        else       //异步
                        {
                            _loadStack.Push(tile);
                        }
                    }
                }
                return(visibleTiles != null ? visibleTiles.Length : 0);
            }
            finally
            {
                ReStartLoadWorker();
            }
        }
Пример #5
0
        public TileIdentify[] GetVisibleTiles(out LevelDef level, out Rectangle rasterRect, out int bRow, out int bCol, out int rows, out int cols)
        {
            bRow       = bCol = rows = cols = 0;
            rasterRect = new Rectangle();
            level      = new LevelDef();
            if (_levels == null)
            {
                return(null);
            }
            TileIdentify[] visibleTiles;
            _tilesLocator.Compute(_canvas, out level, out visibleTiles);
            goto firtTimes;
toUpLevel:
            _tilesLocator.ComputeByLevel(_canvas, level, out visibleTiles);
firtTimes:
            if (visibleTiles != null)
            {
                if (AllTilesIsExist(visibleTiles))
                {
                    ComputeRasterRect(level, visibleTiles, out rasterRect);
                    GetRowColRange(visibleTiles, out bRow, out bCol, out rows, out cols);
                }
                else
                {
                    if (level.LevelNo < _levels.Length - 1)
                    {
                        level = _levels[level.LevelNo + 1];
                        goto toUpLevel;
                    }
                }
            }
            return(visibleTiles);
        }
Пример #6
0
        void CanvasEnvelopeChanged(object sender, EventArgs e)
        {
            TileIdentify[] visibleTiles;
            LevelDef       suitableLevel;

            _tilesLocator.Compute(_canvas, out suitableLevel, out visibleTiles);
            if (visibleTiles == null || visibleTiles.Length == 0)
            {
                return;
            }
            //visibleTiles的顺序需要调整
            _tilesLocator.ComputeExtand(_canvas, suitableLevel, out visibleTiles);
            _tileCacheManager.WaitLoading(visibleTiles);
            _loadStack.Clear();
            _tileCacheManager.ClearPastTiles(visibleTiles);
            _currentLevel = suitableLevel;
            _tilesLocator.GetRasterRowColOfViewWnd(_canvas, out _bRowAtViewWnd, out _bColAtViewWnd, out _widthAtViewWnd, out _heightAtViewWnd);
            foreach (TileIdentify tile in visibleTiles)
            {
                if (!_tileCacheManager.IsExist(tile))
                {
                    _loadStack.Push(tile);
                    ReStartLoadWorker();
                }
            }
        }
Пример #7
0
 private Bitmap BuildBitmap(T[][] buffers, LevelDef level, TileIdentify tile)
 {
     using (IBitmapBuilder <T> builder = GetBitmapBuilder())
     {
         Bitmap bitmap = null;
         if (buffers.Length == 1)
         {
             bitmap         = new Bitmap(_tileSize, _tileSize, PixelFormat.Format8bppIndexed);
             bitmap.Palette = BitmapBuilderFactory.GetDefaultGrayColorPalette();
             if (st1 == null && _stretcher == null)
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], ref bitmap);
             }
             else
             {
                 if (_stretcher != null)
                 {
                     builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], _stretcher, ref bitmap);
                 }
                 else
                 {
                     builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], st1.Stretcher, ref bitmap);
                 }
             }
             //
             if (_colorMapTable != null)
             {
                 ColorPalette plt = BitmapBuilderFactory.GetDefaultGrayColorPalette();
                 for (int i = 0; i < 256; i++)
                 {
                     plt.Entries[i] = Color.Black;
                 }
                 int idx = 1;
                 foreach (ColorMapItem <int> item in _colorMapTable.Items)
                 {
                     for (int v = item.MinValue; v < item.MaxValue; v++)
                     {
                         plt.Entries[idx] = item.Color;
                     }
                     idx++;
                 }
                 bitmap.Palette = plt;
             }
         }
         else if (buffers.Length == 3)
         {
             bitmap = new Bitmap(_tileSize, _tileSize, PixelFormat.Format24bppRgb);
             if (st1 == null || st2 == null || st3 == null)
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], buffers[1], buffers[2], ref bitmap);
             }
             else
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], buffers[1], buffers[2], st1.Stretcher, st2.Stretcher, st3.Stretcher, ref bitmap);
             }
         }
         return(bitmap);
     }
 }
Пример #8
0
        public void ComputeByLevel(ICanvas canvas, LevelDef level, out TileIdentify[] tiles)
        {
            UpdateTileComputer(canvas);
            int beginRow = 0, beginCol = 0, width = 0, height = 0;

            GetRasterRowColOfViewWnd(canvas, out beginRow, out beginCol, out width, out height);
            tiles = level.GetTileIdentifiesByOriginal(beginRow, beginCol, width, height);
        }
Пример #9
0
        public void ComputeExtand(ICanvas canvas, LevelDef level, out TileIdentify[] tiles)
        {
            double expandX = canvas.CurrentEnvelope.Width * canvas.CanvasSetting.XExpand;
            double expandY = canvas.CurrentEnvelope.Height * canvas.CanvasSetting.YExpand;
            int    beginRow = 0, beginCol = 0, width = 0, height = 0;

            GetRasterRowColOfViewWnd(canvas, expandX, expandY, out beginRow, out beginCol, out width, out height);
            tiles = level.GetTileIdentifiesByOriginal(beginRow, beginCol, width, height);
        }
Пример #10
0
 public TileIdentify(int row, int col, int offsetX, int offsetY, int beginRow, int beginCol, int width, int height)
 {
     _row      = row;
     _col      = col;
     _offsetX  = offsetX;
     _offsetY  = offsetY;
     _beginRow = beginRow;
     _beginCol = beginCol;
     _width    = width;
     _height   = height;
     _level    = new LevelDef();
 }
Пример #11
0
 public void ReLoadTileBitmapsBySync(LevelDef[] levels)
 {
     _levels = levels;
     for (int i = levels.Length - 1; i >= 0; i--)
     {
         LevelDef lv = levels[i];
         foreach (TileIdentify tile in lv.TileIdentities)
         {
             _loadStack.Push(tile);
         }
     }
     ReStartLoadWorker();
 }
Пример #12
0
        private TileBitmap LoadTileBitmap(TileIdentify tile)
        {
            int      beginRow = 0, beginCol = 0, width = 0, height = 0;
            LevelDef level = tile.Level;

            level.GetOriginalRowColByDataBlock(tile, ref beginRow, ref beginCol, ref width, ref height);
            TileBitmap tb = _dataProviderReader.CreateBitmapByTile(level, beginRow, beginCol, width, height, tile);

            if (!_disposed && tb != null)
            {
                _tileCacheManager.Put(tb);
            }
            return(tb);
        }
Пример #13
0
 public void ReLoadTileBitmapsBySync(LevelDef[] levels)
 {
     _levels = levels;
     ComputeTotalTileCount();
     for (int i = levels.Length - 1; i >= 0; i--)
     {
         LevelDef lv = levels[i];
         foreach (TileIdentify tile in lv.TileIdentities)
         {
             _loadStack.Push(tile);
         }
     }
     _loadWorker.RunWorkerAsync();
     _loadWorkerIsStarted = true;
 }
Пример #14
0
        public void Compute(ICanvas canvas, out LevelDef level, out TileIdentify[] tiles)
        {
            level = new LevelDef();
            tiles = null;
            UpdateTileComputer(canvas);
            level = GetNearestLevel(canvas);
            if (level.IsEmpty())
            {
                return;
            }
            int beginRow = 0, beginCol = 0, width = 0, height = 0;

            GetRasterRowColOfViewWnd(canvas, out beginRow, out beginCol, out width, out height);
            tiles = level.GetTileIdentifiesByOriginal(beginRow, beginCol, width, height);
        }
Пример #15
0
        private TileBitmap LoadTileBitmap(TileIdentify tile, out bool memoryIsNotEnough)
        {
            memoryIsNotEnough = false;
            int      beginRow = 0, beginCol = 0, width = 0, height = 0;
            LevelDef level = tile.Level;

            level.GetOriginalRowColByDataBlock(tile, ref beginRow, ref beginCol, ref width, ref height);
            TileBitmap tb = _dataProviderReader.CreateBitmapByTile(level, beginRow, beginCol, width, height, tile, out memoryIsNotEnough);

            if (!_disposed && tb != null)
            {
                _tileCacheManager.Put(tb);
                _loadedTitleCount++;
            }
            return(tb);
        }
Пример #16
0
        private LevelDef GetNearestLevel(ICanvas canvas)
        {
            float    scale    = 1 / (canvas.ResolutionX / _originalResolutionX);
            float    dlt      = 0;
            float    minDlt   = float.MaxValue;
            LevelDef retLevel = new LevelDef();

            foreach (LevelDef level in _leves)
            {
                dlt = Math.Abs(level.Scale - scale);
                if (dlt < minDlt)
                {
                    retLevel = level;
                    minDlt   = dlt;
                }
            }
            return(retLevel);
        }
Пример #17
0
        private void CreateLevelBuffer(int bandNo, LevelDef levelDef)
        {
            Size   lvSize = levelDef.Size;
            string fName  = GetLevelBufferFileName(bandNo, levelDef);

            if (File.Exists(fName) && (new FileInfo(fName)).Length == lvSize.Width * lvSize.Height * _dataTypeSize)
            {
                return;
            }
            float scale       = levelDef.Scale;
            int   rowsOfBlock = lvSize.Height;
            int   blockCount  = 0;

            byte[]   buffer        = CreateBuffer(lvSize.Width, ref rowsOfBlock, out blockCount);
            GCHandle handle        = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   bufferPointer = handle.AddrOfPinnedObject();

            try
            {
                using (FileStream fs = new FileStream(fName, FileMode.Create))
                {
                    int         bRow = 0, eRow = 0, actualRows = 0;
                    int         oBeginRow = 0, oEndRow = 0;
                    IRasterBand raster = _dataProvider.GetRasterBand(bandNo);
                    for (int i = 0; i < blockCount; i++, bRow += rowsOfBlock)
                    {
                        eRow       = Math.Min(bRow + rowsOfBlock, lvSize.Height);
                        actualRows = eRow - bRow;
                        oBeginRow  = (int)(bRow / scale);
                        oEndRow    = (int)(eRow / scale);
                        raster.Read(0, oBeginRow, raster.Width, oEndRow - oBeginRow,
                                    bufferPointer, raster.DataType,
                                    lvSize.Width, actualRows);
                        fs.Write(buffer, 0, actualRows * lvSize.Width * _dataTypeSize);
                    }
                }
            }
            finally
            {
                handle.Free();
            }
        }
Пример #18
0
        public LevelDef GetSuitableLevel(LevelDef[] levels, int wndWidth, int wndHeight, int dataWidth, int dataHeight)
        {
            float    scaleX   = wndWidth / (float)dataWidth;
            float    scaleY   = wndHeight / (float)dataHeight;
            float    scale    = Math.Min(scaleX, scaleY);
            float    dlt      = 0;
            float    minDlt   = float.MaxValue;
            LevelDef retLevel = new LevelDef();

            foreach (LevelDef level in levels)
            {
                dlt = Math.Abs(level.Scale - scale);
                if (dlt < minDlt)
                {
                    retLevel = level;
                    minDlt   = dlt;
                }
            }
            return(retLevel);
        }
Пример #19
0
        void CanvasEnvelopeChanged(object sender, EventArgs e)
        {
            TileIdentify[] visibleTiles;
            LevelDef       suitableLevel;

            _tilesLocator.Compute(_canvas, out suitableLevel, out visibleTiles);
            if (visibleTiles == null)
            {
                return;
            }
            _currentLevel = suitableLevel;
            _tilesLocator.GetRasterRowColOfViewWnd(_canvas, out _bRowAtViewWnd, out _bColAtViewWnd, out _widthAtViewWnd, out _heightAtViewWnd);
            foreach (TileIdentify tile in visibleTiles)
            {
                if (!_tileCacheManager.IsExist(tile))
                {
                    _loadStack.Push(tile);
                }
            }
        }
Пример #20
0
        public TileBitmap CreateBitmapByTile(LevelDef level, int beginRow, int beginCol, int width, int height, TileIdentify tile)
        {
            lock (_lockObj)
            {
                T[][] buffers = null;
                //从磁盘中读取缓存的瓦片
tryAgainLine:
                if (_enableDiskCache && _fileDiskCacheManager.IsExist(tile, _selectedBandNos))
                {
                    buffers = new T[_selectedBandNos.Length][];
                    for (int i = 0; i < _selectedBandNos.Length; i++)
                    {
                        buffers[i] = _fileDiskCacheManager.GetTile <T>(tile, _selectedBandNos[i]);
                        if (buffers[i] == null)//缓存已经从磁盘清除
                        {
                            goto tryAgainLine;
                        }
                    }
                }
                else
                {
                    bool isOK = ReadRasterFile(out buffers, beginRow, beginCol, width, height, tile);
                    if (!isOK)
                    {
                        return(null);
                    }
                    Console.WriteLine("direct read , " + Environment.TickCount.ToString());
                    //将该瓦片保存在磁盘
                    if (_enableDiskCache)
                    {
                        _asyncCacheHandler.BeginInvoke(buffers, tile, _selectedBandNos, null, null);
                    }
                }
                TileBitmap tb = new TileBitmap();
                tb.Level  = level;
                tb.Tile   = tile;
                tb.Bitmap = BuildBitmap(buffers, level, tile);
                return(tb);
            }
        }
Пример #21
0
        private void CreateBufferByTile(LevelDef levelDef, int bandNo)
        {
            TileIdentify[] tiles = levelDef.TileIdentities;
            if (tiles == null || tiles.Length == 0)
            {
                return;
            }
            byte[]   buffer = new byte[_tileSetting.TileSize * _tileSetting.TileSize * _dataTypeSize];
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   ptr    = handle.AddrOfPinnedObject();
            float    scale  = levelDef.Scale;

            try
            {
                int         oBeginRow, oBeginCol, oWidth, oHeight;
                IRasterBand raster = _dataProvider.GetRasterBand(bandNo);
                foreach (TileIdentify tile in tiles)
                {
                    string fName = GetTileBufferFileName(tile, bandNo) + "_" + tile.Width.ToString() + "_" + tile.Height + ".RAW";
                    if (File.Exists(fName) && (new FileInfo(fName)).Length == tile.Width * tile.Height * _dataTypeSize)
                    {
                        continue;
                    }
                    oBeginRow = (int)(tile.BeginRow / scale);
                    oBeginCol = (int)(tile.BeginCol / scale);
                    oWidth    = (int)(tile.Width / scale);
                    oHeight   = (int)(tile.Height / scale);
                    raster.Read(oBeginCol, oBeginRow, oWidth, oHeight, ptr, raster.DataType, tile.Width, tile.Height);
                    using (FileStream fs = new FileStream(fName, FileMode.Create))
                    {
                        fs.Write(buffer, 0, tile.Width * tile.Height * _dataTypeSize);
                    }
                }
            }
            finally
            {
                handle.Free();
            }
        }
Пример #22
0
        private void ComputeRasterRect(LevelDef level, TileIdentify[] visibleTiles, out Rectangle rasterRect)
        {
            rasterRect = new Rectangle();
            if (visibleTiles == null)
            {
                return;
            }
            int x = 0, y = 0, w = 0, h = 0;
            int tileSize = _tileSetting.TileSize;
            int top = int.MaxValue, bottom = int.MinValue;
            int left = int.MaxValue, right = int.MinValue;

            foreach (TileIdentify tile in visibleTiles)
            {
                level.GetOriginalRowCol(tileSize, tile, ref y, ref x, ref w, ref h);
                if (x < left)
                {
                    left = x;
                }
                if ((x + w) > right)
                {
                    right = x + w;
                }
                if (y < top)
                {
                    top = y;
                }
                if ((y + h) > bottom)
                {
                    bottom = y + h;
                }
            }
            rasterRect.X      = left;
            rasterRect.Y      = top;
            rasterRect.Width  = right - left;
            rasterRect.Height = bottom - top;
        }
Пример #23
0
        public int LoadTileBitmaps(LevelDef[] levels, bool isSync)
        {
            _levels = levels;
            ComputeTotalTileCount();
            //同步加载最适合的级别(修改为异步5.15)
            TileIdentify[] visibleTiles;
            LevelDef       nearestLevel;

            _tilesLocator.Compute(_canvas, out nearestLevel, out visibleTiles);
            if (nearestLevel.IsEmpty())
            {
                return(0);
            }
            _currentLevel = nearestLevel;
            if (visibleTiles != null)
            {
                foreach (TileIdentify tile in visibleTiles)
                {
                    bool memoryIsNotEnough = false;
                    if (isSync)//同步
                    {
                        LoadTileBitmap(tile, out memoryIsNotEnough);
                        if (memoryIsNotEnough)
                        {
                            Console.WriteLine("LoadTileBitmaps:内存不足,无法完成请求的瓦片!");
                            return(0);
                        }
                    }
                    else       //异步
                    {
                        _loadStack.Push(tile);
                    }
                }
                foreach (TileIdentify tile in nearestLevel.TileIdentities)
                {
                    if (Array.IndexOf(visibleTiles, tile) < 0)
                    {
                        _loadStack.Push(tile);
                    }
                }
            }
            else
            {
                foreach (TileIdentify tile in nearestLevel.TileIdentities)
                {
                    _loadStack.Push(tile);
                }
            }
            //MessageBox.Show("最小层加载完毕");
            //将剩余级别插入异步装载队列(从低分辨率开始)
            for (int i = levels.Length - 1; i >= 0; i--)
            {
                LevelDef lv = levels[i];
                if (lv.LevelNo != nearestLevel.LevelNo)
                {
                    foreach (TileIdentify tile in lv.TileIdentities)
                    {
                        _loadStack.Push(tile);
                    }
                }
            }
            //MessageBox.Show("剩余层已添加到队列");
            _loadWorker.RunWorkerAsync();
            _loadWorkerIsStarted = true;
            //MessageBox.Show("异步加载已启动");
            return(visibleTiles != null ? visibleTiles.Length : 0);
        }
Пример #24
0
        public TileBitmap GetTileBitmap(TileIdentify tile)
        {
            LevelDef level = tile.Level;

            return(_tileCacheManager.Get(level.LevelNo, tile.Row, tile.Col));
        }
Пример #25
0
 private void PrintCurrentLevel(LevelDef nearestLevel, ICanvas canvas)
 {
     Console.WriteLine(nearestLevel.ToString());
     Console.Write(" ");
     Console.WriteLine((1 / canvas.ResolutionX).ToString());
 }
Пример #26
0
 private string GetLevelBufferFileName(int bandNo, LevelDef levelDef)
 {
     return(Path.Combine(_fDir, string.Format("B{0}_L{1}", bandNo, levelDef.LevelNo)));
 }