public static MemBitmap LoadImage(string imgName) { //TODO: review here again //1. do not access the system file directly //2. ask for a file from 'host' //if (!System.IO.File.Exists(imgName)) //{ // return null; //} return(MemBitmap.LoadBitmap(imgName)); //using (System.Drawing.Bitmap gdiBmp = new System.Drawing.Bitmap(imgName)) //{ // int w = gdiBmp.Width; // int h = gdiBmp.Height; // var bmpData = gdiBmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), // System.Drawing.Imaging.ImageLockMode.ReadOnly, // System.Drawing.Imaging.PixelFormat.Format32bppArgb); // PixelFarm.CpuBlit.MemBitmap newBmp = PixelFarm.CpuBlit.MemBitmap.CreateFromCopy(w, h, // w * h * 4, // bmpData.Scan0); // gdiBmp.UnlockBits(bmpData); // return newBmp; //} }
double _rotationDeg;//rotation angle in degree public ImgFilter1Demo() { _orgImg = MemBitmap.LoadBitmap("../Data/spheres.png"); _imgW = _orgImg.Width; _imgH = _orgImg.Height; _lut = new ImageFilterLookUpTable(); Normalization = true;//default _imgSpanGenCustom = new FragmentProcessing.ImgSpanGenRGBA_CustomFilter(); }
public FontTextureDemo3() { string glyphBmp = @"Data\tahoma -488129008.info.png"; if (System.IO.File.Exists(glyphBmp)) { _glyphAtlasBmp = MemBitmap.LoadBitmap(glyphBmp); } this.Width = 800; this.Height = 600; }
public override void Init() { UseBitmapExt = false; string imgFileName = "Samples\\lion1.png"; if (System.IO.File.Exists(imgFileName)) { _lionImg = MemBitmap.LoadBitmap(imgFileName); } }
/// <summary> /// get from cache or create a new one /// </summary> /// <param name="reqFont"></param> /// <returns></returns> public SimpleBitmapAtlas GetBitmapAtlas(string atlasName, out B outputBitmap) { #if DEBUG _dbugStopWatch.Reset(); _dbugStopWatch.Start(); #endif if (!_createdAtlases.TryGetValue(atlasName, out SimpleBitmapAtlas foundAtlas)) { //check from pre-built cache (if availiable) string textureInfoFile = atlasName + ".info"; string textureImgFilename = atlasName + ".png"; //check if the file exist if (StorageService.Provider.DataExists(textureInfoFile) && StorageService.Provider.DataExists(textureImgFilename)) { SimpleBitmapAtlasBuilder atlasBuilder = new SimpleBitmapAtlasBuilder(); using (System.IO.Stream fontAtlasTextureInfo = StorageService.Provider.ReadDataStream(textureInfoFile)) using (System.IO.Stream fontImgStream = StorageService.Provider.ReadDataStream(textureImgFilename)) { try { List <SimpleBitmapAtlas> atlasList = atlasBuilder.LoadAtlasInfo(fontAtlasTextureInfo); foundAtlas = atlasList[0]; foundAtlas.SetMainBitmap(MemBitmap.LoadBitmap(fontImgStream), true); _createdAtlases.Add(atlasName, foundAtlas); } catch (Exception ex) { throw ex; } } } } if (foundAtlas != null) { outputBitmap = _loadAtlases.GetOrCreateNewOne(foundAtlas); return(foundAtlas); } else { #if DEBUG //show warning about this System.Diagnostics.Debug.WriteLine("not found atlas:" + atlasName); #endif outputBitmap = default(B); return(null); } }
public FloodFillDemo() { // _magicWandConfigs = new MagicWandConfigGroup(); BackgroundColor = Color.White; _defaultImg = new MemBitmap(400, 300); AggPainter p = AggPainter.Create(_defaultImg); p.Clear(Color.White); p.FillColor = Color.Black; p.FillEllipse(20, 20, 30, 30); for (int i = 0; i < 20; i++) { p.StrokeColor = Color.Black; p.DrawEllipse(i * 10, i * 10, 20, 20); } // this.PixelSize = 32; this.Gamma = 1; _tolerance = 30; _floodFill = new ColorBucket(Color.Red, _tolerance); _magicWand = new MagicWand(_tolerance); // //_lionPng = MemBitmap.LoadBitmap("../Data/lion1_v2_2.png"); //_lionPng = MemBitmap.LoadBitmap("../Data/lion1_v2_4_1.png"); _lionPng = MemBitmap.LoadBitmap("../Data/lion1.png"); //_lionPng = MemBitmap.LoadBitmap("../Data/lion_1_v3_2.png"); //_lionPng = MemBitmap.LoadBitmap("../Data/glyph_a.png"); _starsPng = MemBitmap.LoadBitmap("../Data/stars.png"); _test_glyphs = MemBitmap.LoadBitmap("../Data/test_glyphs.png"); _rect01 = MemBitmap.LoadBitmap("../Data/rect01.png"); //_v_shape = MemBitmap.LoadBitmap("../Data/shape_v.png"); //_v_shape = MemBitmap.LoadBitmap("../Data/shape_v3.png"); _bmpToFillOn = _defaultImg; OutlineReconstruction = true; WithOutlineSimplifier = true; }
public LionAlphaMask3() { string imgFileName = "Data/lion1.png"; if (System.IO.File.Exists(imgFileName)) { lionImg = MemBitmap.LoadBitmap(imgFileName); } this.Width = 800; this.Height = 600; #if USE_CLIPPING_ALPHA_MASK //alphaMask = new AlphaMaskByteClipped(alphaMaskImageBuffer, 1, 0); #else //alphaMask = new AlphaMaskByteUnclipped(alphaMaskImageBuffer, 1, 0); #endif }
public void SetCurrentTypeface(Typeface typeface) { _currentTypeface = typeface; if (_cacheGlyphPathBuilders.TryGetValue(typeface, out _bitmapList)) { return; } //TODO: you can scale down to proper img size //or create a single texture atlas. //if not create a new one _bitmapList = new GlyphBitmapList(); _cacheGlyphPathBuilders.Add(typeface, _bitmapList); int glyphCount = typeface.GlyphCount; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { for (ushort i = 0; i < glyphCount; ++i) { ms.SetLength(0); Glyph glyph = typeface.GetGlyphByIndex(i); typeface.ReadBitmapContent(glyph, ms); GlyphBitmap glyphBitmap = new GlyphBitmap(); glyphBitmap.Width = glyph.MaxX - glyph.MinX; glyphBitmap.Height = glyph.MaxY - glyph.MinY; //glyphBitmap.Bitmap = ... glyphBitmap.Bitmap = MemBitmap.LoadBitmap(ms); //MemBitmapExtensions.SaveImage(glyphBitmap.Bitmap, "d:\\WImageTest\\testGlyphBmp_" + i + ".png"); _bitmapList.RegisterBitmap(glyph.GlyphIndex, glyphBitmap); } } }
public void RegisterBitmapAtlas(string atlasName, byte[] atlasInfoBuffer, byte[] totalImgBuffer) { //direct register atlas //instead of loading it from file if (!_createdAtlases.ContainsKey(atlasName)) { SimpleBitmapAtlasBuilder atlasBuilder = new SimpleBitmapAtlasBuilder(); using (System.IO.Stream fontAtlasTextureInfo = new MemoryStream(atlasInfoBuffer)) using (System.IO.Stream fontImgStream = new MemoryStream(totalImgBuffer)) { try { List <SimpleBitmapAtlas> atlasList = atlasBuilder.LoadAtlasInfo(fontAtlasTextureInfo); SimpleBitmapAtlas foundAtlas = atlasList[0]; foundAtlas.SetMainBitmap(MemBitmap.LoadBitmap(fontImgStream), true); _createdAtlases.Add(atlasName, foundAtlas); } catch (Exception ex) { throw ex; } } } }
public CutCopyPasteDemo() { _lionPng = MemBitmap.LoadBitmap("../Data/lion1.png"); }
public void BuildMultiFontSize(string multiFontSizeAtlasFilename, string imgOutputFilename) { //merge to the new one //1. ensure same atlas width int atlasW = 0; int j = _atlasList.Count; int totalHeight = 0; const int interAtlasSpace = 2; for (int i = 0; i < j; ++i) { TempMergingAtlasInfo atlasInfo = _atlasList[i]; SimpleBitmapAtlas fontAtlas = atlasInfo.fontAtlasFile.AtlasList[0]; totalHeight += fontAtlas.Height + interAtlasSpace; if (i == 0) { atlasW = fontAtlas.Width; } else { if (atlasW != fontAtlas.Width) { throw new NotSupportedException(); } } } //-------------------------------------------- //in this version, the glyph offsetY is measure from bottom*** int[] offsetFromBottoms = new int[j]; int offsetFromBottom = interAtlasSpace;//start offset for (int i = j - 1; i >= 0; --i) { TempMergingAtlasInfo atlasInfo = _atlasList[i]; SimpleBitmapAtlas fontAtlas = atlasInfo.fontAtlasFile.AtlasList[0]; offsetFromBottoms[i] = offsetFromBottom; offsetFromBottom += fontAtlas.Height + interAtlasSpace; } //-------------------------------------------- //merge all img to one int top = 0; using (MemBitmap memBitmap = new MemBitmap(atlasW, totalHeight)) { AggPainter painter = AggPainter.Create(memBitmap); for (int i = 0; i < j; ++i) { TempMergingAtlasInfo atlasInfo = _atlasList[i]; BitmapAtlasFile atlasFile = atlasInfo.fontAtlasFile; SimpleBitmapAtlas fontAtlas = atlasInfo.fontAtlasFile.AtlasList[0]; atlasInfo.NewCloneLocations = SimpleBitmapAtlas.CloneLocationWithOffset(fontAtlas, 0, offsetFromBottoms[i]); atlasInfo.ImgUrlDict = fontAtlas.ImgUrlDict; using (Stream fontImgStream = PixelFarm.Platforms.StorageService.Provider.ReadDataStream(atlasInfo.imgFile)) using (MemBitmap atlasBmp = MemBitmap.LoadBitmap(fontImgStream)) { painter.DrawImage(atlasBmp, 0, top); top += atlasBmp.Height + interAtlasSpace; } } memBitmap.SaveImage(imgOutputFilename); } //-------------------------------------------- //save merged font atlas //TODO: use 'File' provider to access system file using (FileStream fs = new FileStream(multiFontSizeAtlasFilename, FileMode.Create)) using (BinaryWriter w = new BinaryWriter(fs)) { //----------- //overview //total img info BitmapAtlasFile fontAtlasFile = new BitmapAtlasFile(); fontAtlasFile.StartWrite(fs); //1. simple atlas count fontAtlasFile.WriteOverviewMultiSizeFontInfo((ushort)j); //2. for (int i = 0; i < j; ++i) { TempMergingAtlasInfo atlasInfo = _atlasList[i]; RequestFont reqFont = atlasInfo.reqFont; fontAtlasFile.WriteOverviewFontInfo(reqFont.Name, reqFont.FontKey, reqFont.SizeInPoints);//size in points fontAtlasFile.WriteTotalImageInfo( (ushort)atlasW, (ushort)top, 4, atlasInfo.textureKind); // fontAtlasFile.WriteAtlasItems(atlasInfo.NewCloneLocations); if (atlasInfo.ImgUrlDict != null) { fontAtlasFile.WriteImgUrlDict(atlasInfo.ImgUrlDict); } } fontAtlasFile.EndWrite(); } }
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)); } }