示例#1
0
 public void Cut()
 {
     //1.copy
     using (MemBitmap memBitmap = _lionPng.CopyImgBuffer(20, 20, 100, 100))
     {
         Clipboard.SetImage(memBitmap);
     }
     //2. fill cut area
     using (Tools.BorrowAggPainter(_lionPng, out var painter))
     {
         var prevColor = painter.FillColor;
         painter.FillColor = Color.White;
         painter.FillRect(20.5, 20.5, 100, 100);
         painter.FillColor = prevColor;
     }
 }
示例#2
0
 public void Cut()
 {
     //1.copy
     using (MemBitmap memBitmap = _lionPng.CopyImgBuffer(20, 20, 100, 100))
     {
         using (var platformBmp = CreatePlatformBitmap(memBitmap))
         {
             System.Windows.Forms.Clipboard.SetImage(platformBmp);
         }
     }
     //2. fill cut area
     using (AggPainterPool.Borrow(_lionPng, out var painter))
     {
         var prevColor = painter.FillColor;
         painter.FillColor = Color.White;
         painter.FillRect(20.5, 20.5, 100, 100);
         painter.FillColor = prevColor;
     }
 }
示例#3
0
        protected override void OnStart(AppHost host)
        {
            var sampleButton = new LayoutFarm.CustomWidgets.Box(30, 30);

            host.AddChild(sampleButton);

            sampleButton.MouseDown += ((s, e2) =>
            {
                //click to create custom cursor
                if (!_showCustomCursor)
                {
                    if (_myCursor == null)
                    {
                        using (MemBitmap temp = new MemBitmap(16, 16))
                            using (Tools.BorrowAggPainter(temp, out AggPainter p))
                            {
                                //1. create a simple bitmap for our cursor
                                p.Clear(Color.FromArgb(0, Color.White));
                                p.FillRect(1, 1, 10, 10, Color.FromArgb(150, Color.Green));
                                p.FillRect(3, 3, 4, 4, Color.Yellow);
                                //-------
                                //create cursor file

                                CursorFile curFile = new CursorFile();

                                var iconBmp = new WindowBitmap(temp.Width, temp.Height);
                                iconBmp.RawBitmapData = MemBitmap.CopyImgBuffer(temp);

                                curFile.AddBitmap(iconBmp, 1, 1); //add bitmap with hotspot
                                curFile.Save("myicon.cur");       //save to temp file
                                _myCursor = UIPlatform.CreateCursor(new CursorRequest("myicon.cur", 16));
                            }
                    }
                    e2.CustomMouseCursor = _myCursor;
                    _showCustomCursor = true;
                }
                else
                {
                    _showCustomCursor = false;
                    e2.MouseCursorStyle = MouseCursorStyle.Default;
                }
            });

            sampleButton.MouseMove += ((s, e2) =>
            {
                if (_showCustomCursor && _myCursor != null)
                {
                    e2.CustomMouseCursor = _myCursor;
                }
            });
        }
        private void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_bitmapAtlas == null)
            {
                return;
            }

            string imgUri = (string)listBox2.SelectedItem;

            if (_pic2Gfx == null)
            {
                _pic2Gfx = pictureBox2.CreateGraphics();
            }

            _pic2Gfx.Clear(Color.White);
            if (pictureBox2.Image != null)
            {
                _pic2Gfx.DrawImage(pictureBox2.Image, 0, 0);
            }

            if (_bitmapAtlas.TryGetItem(imgUri, out AtlasItem bmpMapData))
            {
                _pic2Gfx.DrawRectangle(Pens.Red,
                                       new Rectangle(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height));

                //example
                int[] buffer = null;
                using (MemBitmap itemImg = _totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height))
                {
                    buffer = MemBitmap.CopyImgBuffer(itemImg);
                }
                //convert from membitmap to bmp

                System.Drawing.Bitmap test = new Bitmap(bmpMapData.Width, bmpMapData.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                var bmp_data = test.LockBits(new Rectangle(0, 0, bmpMapData.Width, bmpMapData.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, test.PixelFormat);
                System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmp_data.Scan0, buffer.Length);
                test.UnlockBits(bmp_data);


                DisposeExistingPictureBoxImage(pictureBox1);
                pictureBox1.Image = test;
            }
        }
        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");
                }
            }
        }
示例#6
0
        public PixelFarm.CpuBlit.BitmapAtlas.BitmapAtlasItemSource GenerateMsdfTexture(VertexStore vxs)
        {
            Shape shape = CreateShape(vxs, out EdgeBmpLut edgeBmpLut);

            if (MsdfGenParams == null)
            {
                MsdfGenParams = new MsdfGenParams();//use default
            }

            //---preview v1 bounds-----------
            PreviewSizeAndLocation(
                shape,
                MsdfGenParams,
                out int imgW, out int imgH,
                out Vector2 translateVec);

            _dx = translateVec.x;
            _dy = translateVec.y;
            //------------------------------------
            List <ContourCorner> corners = edgeBmpLut.Corners;

            TranslateCorners(corners, _dx, _dy);

            //[1] create lookup table (lut) bitmap that contains area/corner/shape information
            //each pixel inside it contains data that map to area/corner/shape

            //
            using (MemBitmap bmpLut = new MemBitmap(imgW, imgH))
                using (Tools.BorrowAggPainter(bmpLut, out var painter))
                    using (Tools.BorrowShapeBuilder(out var sh))
                    {
                        _msdfEdgePxBlender.ClearOverlapList();//reset
                        painter.RenderSurface.SetCustomPixelBlender(_msdfEdgePxBlender);

                        //1. clear all bg to black
                        painter.Clear(PixelFarm.Drawing.Color.Black);

                        sh.InitVxs(vxs) //...
                        .TranslateToNewVxs(_dx, _dy)
                        .Flatten();


                        //---------
                        //2. force fill the shape (this include hole(s) inside shape to)
                        //( we set threshold to 50 and do force fill)
                        painter.RenderSurface.SetGamma(_prebuiltThresholdGamma_50);
                        _msdfEdgePxBlender.FillMode = MsdfEdgePixelBlender.BlenderFillMode.Force;
                        painter.Fill(sh.CurrentSharedVxs, EdgeBmpLut.EncodeToColor(0, AreaKind.AreaInsideCoverage50));

                        painter.RenderSurface.SetGamma(_prebuiltThresholdGamma_50);//restore
#if DEBUG
                        //debug for output
                        //painter.Fill(v7, Color.Red);
                        //bmpLut.SaveImage("dbug_step0.png");
                        //int curr_step = 1;
#endif
                        //---------

                        int        cornerCount          = corners.Count;
                        List <int> cornerOfNextContours = edgeBmpLut.CornerOfNextContours;
                        int        startAt      = 0;
                        int        n            = 1;
                        int        corner_index = 1;

                        for (int cnt_index = 0; cnt_index < cornerOfNextContours.Count; ++cnt_index)
                        {
                            //contour scope
                            int next_corner_startAt = cornerOfNextContours[cnt_index];

                            //-----------
                            //AA-borders of the contour
                            painter.RenderSurface.SetGamma(_prebuiltThresholdGamma_OverlappedBorder); //this creates overlapped area

                            for (; n < next_corner_startAt; ++n)
                            {
                                //0-> 1
                                //1->2 ... n
                                FillBorders(painter, corners[n - 1], corners[n]);

#if DEBUG
                                //bmpLut.SaveImage("dbug_step" + curr_step + ".png");
                                //curr_step++;
#endif
                            }
                            {
                                //the last one
                                //close contour, n-> 0
                                FillBorders(painter, corners[next_corner_startAt - 1], corners[startAt]);
#if DEBUG
                                //bmpLut.SaveImage("dbug_step" + curr_step + ".png");
                                //curr_step++;
#endif
                            }

                            startAt = next_corner_startAt;
                            n++;
                            corner_index++;
                        }
#if DEBUG
                        //bmpLut.SaveImage("dbug_step2.png");
#endif


                        //painter.RenderSurface.SetGamma(_prebuiltThresholdGamma_100);
                        //_msdfEdgePxBlender.FillMode = MsdfEdgePixelBlender.BlenderFillMode.InnerAreaX;
                        //painter.Fill(sh.CurrentSharedVxs, EdgeBmpLut.EncodeToColor(0, AreaKind.AreaInsideCoverage100));



                        painter.RenderSurface.SetCustomPixelBlender(null);
                        painter.RenderSurface.SetGamma(null);

                        //
                        List <CornerList> overlappedList = MakeUniqueList(_msdfEdgePxBlender._overlapList);
                        edgeBmpLut.SetOverlappedList(overlappedList);

#if DEBUG
                        if (dbugWriteMsdfTexture)
                        {
                            //save for debug
                            //we save to msdf_shape_lut2.png
                            //and check it from external program
                            //but we generate msdf bitmap from msdf_shape_lut.png
                            bmpLut.SaveImage(dbug_msdf_shape_lutName);
                            var   bmp5       = MemBitmap.LoadBitmap(dbug_msdf_shape_lutName);
                            int[] lutBuffer5 = bmp5.CopyImgBuffer(bmpLut.Width, bmpLut.Height);
                            if (bmpLut.Width == 338 && bmpLut.Height == 477)
                            {
                                dbugBreak = true;
                            }
                            edgeBmpLut.SetBmpBuffer(bmpLut.Width, bmpLut.Height, lutBuffer5);
                            //generate actual sprite
                            PixelFarm.CpuBlit.BitmapAtlas.BitmapAtlasItemSource item = CreateMsdfImage(shape, MsdfGenParams, imgW, imgH, translateVec, edgeBmpLut);
                            //save msdf bitmap to file
                            using (MemBitmap memBmp = MemBitmap.CreateFromCopy(item.Width, item.Height, item.Source))
                            {
                                memBmp.SaveImage(dbug_msdf_output);
                            }

                            return(item);
                        }
#endif

                        //[B] after we have a lookup table
                        int[] lutBuffer = bmpLut.CopyImgBuffer(bmpLut.Width, bmpLut.Height);
                        edgeBmpLut.SetBmpBuffer(bmpLut.Width, bmpLut.Height, lutBuffer);

                        return(CreateMsdfImage(shape, MsdfGenParams, imgW, imgH, translateVec, edgeBmpLut));
                    }
        }
示例#7
0
        public static void BuildBitmapAtlas(AtlasProject atlasProj, Func <string, MemBitmap> imgLoader, bool test_extract = false)
        {
            //demonstrate how to build a bitmap atlas
            List <AtlasItemSourceFile> fileList = atlasProj.Items;
            //1. create builder
            var bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();

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

            foreach (AtlasItemSourceFile f in fileList)
            {
                if (f.Kind != AtlasItemSourceKind.Image)
                {
                    continue;
                }

                //3. load a bitmap

                BitmapAtlasItemSource atlasItem = null;
                using (MemBitmap itemBmp = imgLoader(f.AbsoluteFilename))
                {
                    //4. get information about it
                    atlasItem = new BitmapAtlasItemSource(itemBmp.Width, itemBmp.Height);
                    atlasItem.SetImageBuffer(MemBitmap.CopyImgBuffer(itemBmp));
                }

                atlasItem.UniqueInt16Name = index;
                //5. add to builder
                bmpAtlasBuilder.AddItemSource(atlasItem);

                //get relative filename
                string imgPath = "//" + f.Link;
                imgDic.Add(imgPath, index);
                index++;

                //------------
#if DEBUG
                if (index >= ushort.MaxValue)
                {
                    throw new NotSupportedException();
                }
#endif
                //------------
            }
            if (imgDic.Count == 0)
            {
                //no file
                return;
            }

            string atlasInfoFile = atlasProj.OutputFilename + ".info";
            string totalImgFile  = atlasProj.OutputFilename + ".png";

            //5. merge all small images into a bigone
            using (MemBitmap totalImg = bmpAtlasBuilder.BuildSingleImage(false))
            {
                bmpAtlasBuilder.ImgUrlDict = imgDic;
                bmpAtlasBuilder.SetAtlasInfo(TextureKind.Bitmap, 0); //font size
                                                                     //6. save atlas info and total-img (.png file)
                bmpAtlasBuilder.SaveAtlasInfo(atlasInfoFile);
                totalImg.SaveImage(totalImgFile);
            }


            //----------------------
            //7. create an atlas file in a source file version, user can embed the source to file
            //easy, just read .info and .png then convert to binary buffer

            BuildAtlasInEmbededSourceVersion(atlasProj, atlasInfoFile, totalImgFile, imgDic);

            //----------------------
            //test, read data back
            //----------------------
            if (test_extract)
            {
                bmpAtlasBuilder = new SimpleBitmapAtlasBuilder();
                SimpleBitmapAtlas bitmapAtlas = bmpAtlasBuilder.LoadAtlasInfo(atlasInfoFile)[0];
                //
                MemBitmap totalAtlasImg = imgLoader(totalImgFile);
                bitmapAtlas.SetMainBitmap(imgLoader(totalImgFile), true);

                //-----
                for (int i = 0; i < index; ++i)
                {
                    if (bitmapAtlas.TryGetItem((ushort)i, out AtlasItem 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.TryGetItem(@"\chk_checked.png", out AtlasItem bmpMapData))
                    {
                        MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height);
                        itemImg.SaveImage("test1_atlas_item_a.png");
                    }
                }
            }
        }