public static void Test(string imgdir, Func <string, MemBitmap> imgLoader)
        {
            SimpleBitmapAtlasBuilder bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            //test!
            int imgdirNameLen = imgdir.Length;

            string[] filenames = System.IO.Directory.GetFiles(imgdir, "*.png");
            ushort   index     = 0;

            Dictionary <string, ushort> imgDic = new Dictionary <string, ushort>();

            foreach (string f in filenames)
            {
                MemBitmap      itemBmp   = imgLoader(f);
                AtlasItemImage atlasItem = new AtlasItemImage(itemBmp.Width, itemBmp.Height);
                atlasItem.OriginalBounds = new PixelFarm.Drawing.RectangleF(0, 0, itemBmp.Width, itemBmp.Height);
                atlasItem.SetBitmap(itemBmp, false);
                //
                bmpAtlasBuilder.AddAtlasItemImage(index, atlasItem);
                string imgPath = f.Substring(imgdirNameLen);
                imgDic.Add(imgPath, index);
                index++;

                //------------
#if DEBUG
                if (index >= ushort.MaxValue)
                {
                    throw new NotSupportedException();
                }
#endif
                //------------
            }

            //string atlasInfoFile = "d:\\WImageTest\\test1_atlas.info";
            //string totalImgFile = "d:\\WImageTest\\test1_atlas.png";
            string atlasInfoFile = "test1_atlas.info";
            string totalImgFile  = "test1_atlas.png";
            //test, write data to disk
            AtlasItemImage totalImg = bmpAtlasBuilder.BuildSingleImage();
            bmpAtlasBuilder.ImgUrlDict = imgDic;
            bmpAtlasBuilder.SetAtlasInfo(TextureKind.Bitmap);
            bmpAtlasBuilder.SaveAtlasInfo(atlasInfoFile); //save to filename
            totalImg.Bitmap.SaveImage(totalImgFile);

            //-----
            //test, read data back
            bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
            SimpleBitmaptAtlas bitmapAtlas = bmpAtlasBuilder.LoadAtlasInfo(atlasInfoFile);
            //
            MemBitmap      totalAtlasImg = imgLoader(totalImgFile);
            AtlasItemImage atlasImg      = new AtlasItemImage(totalAtlasImg.Width, totalAtlasImg.Height);
            bitmapAtlas.TotalImg = atlasImg;

            for (int i = 0; i < index; ++i)
            {
                if (bitmapAtlas.TryGetBitmapMapData((ushort)i, out BitmapMapData bmpMapData))
                {
                    //test copy data from bitmap
                    MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                    itemImg.SaveImage("test1_atlas_item" + i + ".png");
                }
            }

            //test,
            {
                if (bitmapAtlas.TryGetBitmapMapData(@"\chk_checked.png", out BitmapMapData bmpMapData))
                {
                    MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                    itemImg.SaveImage("test1_atlas_item_a.png");
                }
            }
        }
Ejemplo n.º 2
0
        public void DrawImage(GLPainter glPainter, AtlasImageBinder atlasImgBinder, float left, float top)
        {
            switch (atlasImgBinder.State)
            {
            case LayoutFarm.BinderState.Loaded:
            {
                GLBitmap glbmp = LayoutFarm.ImageBinder.GetCacheInnerImage(atlasImgBinder) as GLBitmap;
                if (glbmp != null)
                {
                    BitmapMapData mapData = atlasImgBinder.MapData;
                    Rectangle     srcRect =
                        new Rectangle(mapData.Left,
                                      mapData.Top, //diff from font atlas***
                                      mapData.Width,
                                      mapData.Height);

                    TextureKind textureKind = _bmpAtlas.TextureKind;
                    switch (textureKind)
                    {
                    default:
                    case TextureKind.Msdf:
                        throw new NotSupportedException();

                    case TextureKind.Bitmap:
                    {
                        atlasImgBinder.State = LayoutFarm.BinderState.Loaded;
                        LayoutFarm.ImageBinder.SetCacheInnerImage(atlasImgBinder, _glBmp, false);

                        atlasImgBinder.MapData = mapData;
                        glPainter.PainterContext.DrawSubImage(_glBmp,
                                                              ref srcRect,
                                                              left,
                                                              top);
                    }
                    break;
                    }
                }
            }
            break;

            case LayoutFarm.BinderState.Unload:
            {
                //load img first
                if (_lastestImgFile != atlasImgBinder.AtlasName)
                {
                    _bmpAtlas = _atlasManager.GetBitmapAtlas(atlasImgBinder.AtlasName, out _glBmp);
                    if (_bmpAtlas == null)
                    {
                        //error
                        atlasImgBinder.State = LayoutFarm.BinderState.Error;        //not found
                        return;
                    }
                    _lastestImgFile = atlasImgBinder.AtlasName;
                }
                //--------
                if (_bmpAtlas.TryGetBitmapMapData(atlasImgBinder.ImageName, out BitmapMapData mapData))
                {
                    //found map data
                    Rectangle srcRect =
                        new Rectangle(mapData.Left,
                                      mapData.Top, //diff from font atlas***
                                      mapData.Width,
                                      mapData.Height);

                    TextureKind textureKind = _bmpAtlas.TextureKind;
                    switch (textureKind)
                    {
                    default:
                    case TextureKind.Msdf:
                        throw new NotSupportedException();

                    case TextureKind.Bitmap:
                    {
                        atlasImgBinder.State = LayoutFarm.BinderState.Loaded;
                        LayoutFarm.ImageBinder.SetCacheInnerImage(atlasImgBinder, _glBmp, false);
                        atlasImgBinder.MapData = mapData;
                        atlasImgBinder.SetPreviewImageSize(mapData.Width, mapData.Height);
                        atlasImgBinder.RaiseImageChanged();

                        glPainter.PainterContext.DrawSubImage(_glBmp,
                                                              ref srcRect,
                                                              left,
                                                              top);
                    }
                    break;
                    }
                }
                else
                {
                    atlasImgBinder.State = LayoutFarm.BinderState.Error;        //not found
                }
            }
            break;
            }
            if (atlasImgBinder.State == LayoutFarm.BinderState.Unload)
            {
            }
        }