/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { this.computing = true; if (bitmap != null) { Bitmap b = new Bitmap(bitmap.Width, bitmap.Height); // get new frame b.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); using (Graphics g = Graphics.FromImage(b)) { //g.Clear(Color.White); g.DrawImageUnscaled(bitmap, 0, 0); } //Bitmap b = new Bitmap(bitmap); //b = bitmap; gh_bitmap = new GrasshopperBitmapGoo(); gh_bitmap.Image = b; //gh_bitmap.Image = b; if (!gh_bitmap.IsValid) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, gh_bitmap.IsValidWhyNot); } else { DA.SetData(0, gh_bitmap); //gh_bitmap.Dispose(); } //b.Dispose(); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Invalid Bitmap"); } this.computing = false; }
protected override void SolveInstance(IGH_DataAccess DA) { string title = ""; GrasshopperBitmapGoo GH_b = new GrasshopperBitmapGoo(); DA.GetData(0, ref title); DA.GetData(1, ref GH_b); PterodactylGrasshopperBitmapGoo GH_bmp = new PterodactylGrasshopperBitmapGoo(); PterodactylEngine.Image reportObject = new PterodactylEngine.Image(title, GH_bmp.ReferenceTag); GH_bmp.Value = GH_b.Value.Clone(new Rectangle(0, 0, GH_b.Value.Width, GH_b.Value.Height), GH_b.Value.PixelFormat); GH_bmp.ReportPart = reportObject.Create(); DA.SetData(0, GH_bmp); }
protected override void SolveInstance(IGH_DataAccess DA) { // Variables Bitmap bm; GrasshopperBitmapGoo ghbm = new GrasshopperBitmapGoo(); double t = 50.0; double a = 1.0; double mts = 2; bool opt = true; double opttol = 0.2; bool inv = false; int colorCount = 2; // Get Data from Input Params if (!DA.GetData(0, ref ghbm)) { return; } if (DA.GetData(1, ref t)) { if (0.0 > t || t > 100.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Threshold must lie between 0.0 to 100.0"); return; } } if (DA.GetData(2, ref a)) { if (0.0 > a || a > 1.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Corner Threshold must lie between 0.0 to 1.0"); return; } } DA.GetData(3, ref mts); DA.GetData(4, ref opt); if (DA.GetData(5, ref opttol)) { if (0.0 > opttol || opttol > 1.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Tolerance for Optimization must lie between 0.0 to 1.0"); return; } } DA.GetData(6, ref inv); if (DA.GetData(7, ref colorCount)) { if (colorCount < 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Color Count cannot be negative"); return; } } else { if (getColors) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Color Count not set. Setting 'Get Colors' to False. Please set a value for Color Count and re-enable Get Colors by right-clicking on the component"); getColors = false; } } // set Data in Potrace fields Potrace.Treshold = t / 100; Potrace.alphamax = a * (4 / 3); Potrace.turdsize = ((int)Math.Round(mts, 0, MidpointRounding.AwayFromZero)); Potrace.curveoptimizing = opt; Potrace.opttolerance = opttol; if (ghbm.IsValid && ghbm.Image != null) { bm = ghbm.Image; } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid Bitmap"); return; } // convert png transparent background to white if (!getColors || (colorCount == 0)) { using (Bitmap b = new Bitmap(bm.Width, bm.Height)) { b.SetResolution(bm.HorizontalResolution, bm.VerticalResolution); using (Graphics g = Graphics.FromImage(b)) { g.Clear(Color.White); g.DrawImageUnscaled(bm, 0, 0); } b.RotateFlip(RotateFlipType.RotateNoneFlipY); DataTree <Curve> crvs = new DataTree <Curve>(); Potrace.Potrace_Trace(b, crvs, inv); DA.SetDataTree(0, crvs); } } else { using (Bitmap b = new Bitmap(bm.Width, bm.Height)) { b.SetResolution(bm.HorizontalResolution, bm.VerticalResolution); using (Graphics g = Graphics.FromImage(b)) { g.Clear(Color.Transparent); g.DrawImageUnscaled(bm, 0, 0); } b.RotateFlip(RotateFlipType.RotateNoneFlipY); WuQuantizer quantizer = new WuQuantizer(); Bitmap quantized = (Bitmap)quantizer.QuantizeImage(b, colorCount + 1); Color[] colors = new Color[colorCount]; Array.Copy(quantized.Palette.Entries, 0, colors, 0, colorCount); DataTree <GH_Colour> colorsOut = new DataTree <GH_Colour>(); DataTree <Curve> crvs = new DataTree <Curve>(); for (int i = 0; i < colorCount; i++) { Bitmap temp = quantized.Clone(new Rectangle(0, 0, b.Width, b.Height), PixelFormat.Format32bppArgb); var bmData = temp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe { byte *p = (byte *)bmData.Scan0; int stopAddress = (int)p + bmData.Stride * bmData.Height; while ((int)p != stopAddress) { if (p[0] == colors[i].B && p[1] == colors[i].G && p[2] == colors[i].R && p[3] == colors[i].A) { p[0] = p[1] = p[2] = p[3] = 255; } else { p[0] = p[1] = p[2] = p[3] = 0; } p += 4; } temp.UnlockBits(bmData); List <Curve> curves = new List <Curve>(); Potrace.Potrace_Trace(temp, curves, true); crvs.AddRange(curves, new GH_Path(i)); if (inv) { Color invCol = Color.FromArgb(((int)colors[i].A), (255 - ((int)colors[i].R)), (255 - ((int)colors[i].G)), (255 - ((int)colors[i].B))); colorsOut.Add(new GH_Colour(invCol), new GH_Path(i)); } else { colorsOut.Add(new GH_Colour(colors[i]), new GH_Path(i)); } Potrace.Clear(); Potrace.Treshold = t / 100; Potrace.alphamax = a * (4 / 3); Potrace.turdsize = ((int)Math.Round(mts, 0, MidpointRounding.AwayFromZero)); Potrace.curveoptimizing = opt; Potrace.opttolerance = opttol; } } DA.SetDataTree(0, crvs); DA.SetDataTree(2, colorsOut); } } Rectangle3d boundary = new Rectangle3d(Plane.WorldXY, (double)bm.Width, (double)bm.Height); DA.SetData(1, boundary); }