private static unsafe void CopyToSurface(byte[] image_data, Cairo.ImageSurface surf) { if (image_data.Length != surf.Data.Length) { throw new ArgumentException("Mismatched image sizes"); } surf.Flush(); ColorBgra *dst = (ColorBgra *)surf.DataPtr; int len = image_data.Length / ColorBgra.SizeOf; fixed(byte *src_bytes = image_data) { ColorBgra *src = (ColorBgra *)src_bytes; for (int i = 0; i < len; ++i) { ColorBgra srcCurrent = *src; // PDN transparet is 255,255,255,0 Pinta is 0,0,0,0 if (srcCurrent.A == 0) { srcCurrent.B = 0; srcCurrent.G = 0; srcCurrent.R = 0; } *dst = srcCurrent; dst++; src++; } } surf.MarkDirty(); }
private void Image_Loaded(object sender, RoutedEventArgs e) { Image image = (Image)sender; using (ImageSurface surface = new ImageSurface(Format.Argb32, (int)image.Width, (int)image.Height)) { using (Context context = new Context(surface)) { PointD p = new PointD(10.0, 10.0); PointD p2 = new PointD(100.0, 10.0); PointD p3 = new PointD(100.0, 100.0); PointD p4 = new PointD(10.0, 100.0); context.MoveTo(p); context.LineTo(p2); context.LineTo(p3); context.LineTo(p4); context.LineTo(p); context.ClosePath(); context.Fill(); context.MoveTo(140.0, 110.0); context.SetFontSize(32.0); context.SetSourceColor(new Color(0.0, 0.0, 0.8, 1.0)); context.ShowText("Hello Cairo!"); surface.Flush(); RgbaBitmapSource source = new RgbaBitmapSource(surface.Data, surface.Width); image.Source = source; } } }
protected override Gdk.Rectangle OnMouseMove (Context g, Color strokeColor, ImageSurface surface, int x, int y, int lastX, int lastY) { // Cairo does not support a single-pixel-long single-pixel-wide line if (x == lastX && y == lastY && g.LineWidth == 1 && PintaCore.Workspace.ActiveWorkspace.PointInCanvas (new PointD(x,y))) { surface.Flush (); ColorBgra source = surface.GetColorBgraUnchecked (x, y); source = UserBlendOps.NormalBlendOp.ApplyStatic (source, strokeColor.ToColorBgra ()); surface.SetColorBgra (source, x, y); surface.MarkDirty (); return new Gdk.Rectangle (x - 1, y - 1, 3, 3); } g.MoveTo (lastX + 0.5, lastY + 0.5); g.LineTo (x + 0.5, y + 0.5); g.StrokePreserve (); Gdk.Rectangle dirty = g.FixedStrokeExtents ().ToGdkRectangle (); // For some reason (?!) we need to inflate the dirty // rectangle for small brush widths in zoomed images dirty.Inflate (1, 1); return dirty; }
private ImageSurface copySurfacePart(ImageSurface surf, Gdk.Rectangle dest_rect) { ImageSurface tmp_surface = new ImageSurface (Format.Argb32, dest_rect.Width, dest_rect.Height); using (Context g = new Context (tmp_surface)) { g.Operator = Operator.Source; g.SetSourceSurface (surf, -dest_rect.Left, -dest_rect.Top); g.Rectangle (new Rectangle (0, 0, dest_rect.Width, dest_rect.Height)); g.Fill (); } //Flush to make sure all drawing operations are finished tmp_surface.Flush (); return tmp_surface; }
public static void DisplayMessage(Cube c, String msg, SiftColor color) { ImageSurface sr = new ImageSurface (Format.ARGB32, 128, 128); Cairo.Context context = new Cairo.Context (sr); context.Color = new Cairo.Color (0, 0, 0, 0); context.Paint (); Pango.Layout pango = Pango.CairoHelper.CreateLayout (context); pango.FontDescription = Pango.FontDescription.FromString ("Arial 16"); pango.Alignment = Alignment.Center; pango.Wrap = WrapMode.WordChar; pango.Width = 128 * 1016; pango.SetText (msg); context.Color = color.ToCairo (); int pWidth = 0, pHeight = 0; pango.GetPixelSize (out pWidth, out pHeight); Log.Debug ("pango Pixel size: " + pWidth + "x" + pHeight); context.MoveTo (0, 64 - (pHeight / 2)); CairoHelper.ShowLayout (context, pango); sr.Flush (); byte[] data = sr.Data; for (int i = 0, x = 0, y = 0; i < data.Length; i += 4, x++) { if (x >= 128) { x = 0; y++; } byte b = data [i], g = data [i + 1], r = data [i + 2], a = data [i + 3]; if (a != 0 || r != 0 || g != 0 || b != 0) { SiftColor sc = new SiftColor (r, g, b); c.FillRect (sc.ToSifteo (), x, y, 1, 1); } else { // we ignore it } } ((IDisposable)context).Dispose (); ((IDisposable)pango).Dispose (); ((IDisposable)sr).Dispose (); }
public unsafe static Gdk.Pixbuf ToPixbuf(this Cairo.ImageSurface surfSource) { Cairo.ImageSurface surf = surfSource.Clone(); surf.Flush(); ColorBgra *dstPtr = (ColorBgra *)surf.DataPtr; int len = surf.Data.Length / 4; for (int i = 0; i < len; i++) { if (dstPtr->A != 0) { *dstPtr = (ColorBgra.FromBgra(dstPtr->R, dstPtr->G, dstPtr->B, dstPtr->A)); } dstPtr++; } Gdk.Pixbuf pb = new Gdk.Pixbuf(surf.Data, true, 8, surf.Width, surf.Height, surf.Stride); (surf as IDisposable).Dispose(); return(pb); }
private Pixbuf ProcessImpl(Pixbuf input, Cms.Profile input_profile, bool fast) { Pixbuf result; using (ImageInfo info = new ImageInfo (input)) { using (ImageSurface surface = new ImageSurface (Format.Argb32, input.Width, input.Height)) { using (Context ctx = new Context (surface)) { ctx.Matrix = info.Fill (info.Bounds, angle); using (SurfacePattern p = new SurfacePattern (info.Surface)) { if (fast) p.Filter = Filter.Fast; ctx.Source = p; ctx.Paint (); } result = surface.ToPixbuf(); surface.Flush (); } } } return result; }
private unsafe void ApplyAndSwap (ImageSurface dst, bool swap) { dst.Flush (); var dest_width = dst.Width; var dst_ptr = (ColorBgra*)dst.DataPtr; var mask_index = 0; ColorBgra swap_pixel; fixed (ColorBgra* fixed_ptr = pixels) { var pixel_ptr = fixed_ptr; dst_ptr += bounds.X + bounds.Y * dest_width; for (int y = bounds.Y; y <= bounds.GetBottom (); y++) { for (int x = bounds.X; x <= bounds.GetRight (); x++) { if (bitmask[mask_index++]) if (swap) { swap_pixel = *dst_ptr; *dst_ptr = *pixel_ptr; *pixel_ptr++ = swap_pixel; } else { *dst_ptr = *pixel_ptr++; } dst_ptr++; } dst_ptr += dest_width - bounds.Width; } } dst.MarkDirty (); }
protected void OnBtnConvertClicked(object sender, EventArgs e) { var pdfRectangles = new Dictionary<string, Dictionary<int, List<Tuple<Exameer.Rectangle, string>>> > (); // Merge rectangles foreach (PdfNode pdf in StorePDF) { foreach (var png in pdf.Images) { foreach (var rectangle in png.Rectangles) { if (pdfRectangles.ContainsKey (pdf.Name)) { if (pdfRectangles [pdf.Name].ContainsKey (rectangle.ID)) { pdfRectangles [pdf.Name] [rectangle.ID].Add (new Tuple<Exameer.Rectangle, string> (rectangle, png.FileName)); } else { pdfRectangles [pdf.Name].Add (rectangle.ID, new List<Tuple<Exameer.Rectangle, string>> () { new Tuple<Exameer.Rectangle, string>(rectangle, png.FileName) } ); } } else { pdfRectangles.Add (pdf.Name, new Dictionary<int, List<Tuple<Exameer.Rectangle, string>>> () {{ rectangle.ID, new List<Tuple<Exameer.Rectangle, string>>() { new Tuple<Exameer.Rectangle, string>(rectangle, png.FileName) }}} ); } } } } int problemNbr = 0; foreach (var pdf in pdfRectangles) { var createdDestinationDir = Directory.CreateDirectory ("./" + pdf.Key.Replace (".pdf", "") + "/"); foreach (var pngrects in pdf.Value) { var surfaces = new List<Tuple<ImageSurface, Exameer.Rectangle>> (); foreach (var rectangle in pngrects.Value) { surfaces.Add (new Tuple<ImageSurface, Exameer.Rectangle> (new ImageSurface (rectangle.Item2), rectangle.Item1)); } var png = new ImageSurface (pngrects.Value [0].Item2); var rectsMaxWidth = pngrects.Value.Max (x => x.Item1.Width); var rectsMaxHeight = pngrects.Value.Select (x => x.Item1.Height).Sum (); var newRectWidth = (int)((rectsMaxWidth / (float)pngrects.Value [0].Item1.Parent.Width) * png.Width); var newRectHeight = (int)((rectsMaxHeight / (float)pngrects.Value [0].Item1.Parent.Height) * png.Height); //pngrects.Value.Aggregate(0, (sum, next) => sum + next.Item1.Height); ImageSurface newImg = new ImageSurface (Format.Argb32, newRectWidth, newRectHeight); Context cr = new Context (newImg); //cr.SetSourceRGBA (1, 1, 1, 1); //cr.Paint (); // Assume they are sorted by their appearance in PNGs. int lastY = 0; foreach (var surface in surfaces) { var dstx = 0; var dsty = lastY; var r = surface.Item2; var srcx = (int)((r.x1 / (float)r.Parent.Width) * png.Width); var srcy = ((int)((r.y1 / (float)r.Parent.Height) * png.Height)); var w = (int)((r.Width / (float)r.Parent.Width) * png.Width); var h = (int)((r.Height / (float)r.Parent.Height) * png.Height); cr.SetSourceSurface (surface.Item1, dstx - srcx, dsty - srcy); cr.Rectangle (dstx, dsty, w, h); cr.Fill (); //lastY = (int)((r.Height / (float)r.Parent.Height) * png.Height); lastY = dsty + h; } newImg.Flush (); newImg.WriteToPng ("./" + pdf.Key.Replace (".pdf", "") + "/" + (problemNbr++) + ".png"); newImg.Dispose (); //newImg.Dispose (); //surfaces.ForEach (x => x.Item1.Destroy ()); surfaces.ForEach (x => x.Item1.Dispose ()); png.Dispose (); //png.Destroy (); } problemNbr = 0; } }
private void PrerareReport(bool Client) { if (!Save()) return; string TempImagePath = System.IO.Path.Combine (System.IO.Path.GetTempPath (), String.Format("Cupboard{0}.png", ItemId)); int widght = 2244; int height = 1181; ImageSurface surf = new ImageSurface(Format.ARGB32, widght, height); Cairo.Context cr = new Context(surf); int MinCubeSizeForH = Convert.ToInt32(widght / (OrderCupboard.CubesH + 1.2)); int MinCubeSizeForV = Convert.ToInt32(height / (OrderCupboard.CubesV + 1.2)); int NeedCubePxSize = Math.Min(MinCubeSizeForH, MinCubeSizeForV); OrderCupboard.Draw(cr, widght, height, NeedCubePxSize, true); surf.Flush(); surf.WriteToPng(TempImagePath); logger.Debug("Writed {0}", TempImagePath); string param = "id=" + ItemId.ToString() + "&image=" + TempImagePath + "&basel=" + OrderCupboard.CubesH.ToString() + "&baseh=" + OrderCupboard.CubesV.ToString(); string ReportPath; if (Client) { ReportPath = System.IO.Path.Combine (Directory.GetCurrentDirectory (), "Reports", "order" + ".rdl"); reportviewer1.LoadReport (new Uri (ReportPath), param, QSMain.ConnectionString); } else { ReportPath = System.IO.Path.Combine (Directory.GetCurrentDirectory (), "Reports", "order_factory" + ".rdl"); reportviewer2.LoadReport(new Uri(ReportPath), param, QSMain.ConnectionString); } }
public unsafe void Render(ImageSurface surface, Gdk.Rectangle[] rois) { byte startAlpha; byte endAlpha; if (this.alphaOnly) { ComputeAlphaOnlyValuesFromColors (this.startColor, this.endColor, out startAlpha, out endAlpha); } else { startAlpha = this.startColor.A; endAlpha = this.endColor.A; } surface.Flush (); ColorBgra* src_data_ptr = (ColorBgra*)surface.DataPtr; int src_width = surface.Width; for (int ri = 0; ri < rois.Length; ++ri) { Gdk.Rectangle rect = rois[ri]; if (this.startPoint.X == this.endPoint.X && this.startPoint.Y == this.endPoint.Y) { // Start and End point are the same ... fill with solid color. for (int y = rect.Top; y <= rect.GetBottom (); ++y) { ColorBgra* pixelPtr = surface.GetPointAddress(rect.Left, y); for (int x = rect.Left; x <= rect.GetRight (); ++x) { ColorBgra result; if (this.alphaOnly && this.alphaBlending) { byte resultAlpha = (byte)Utility.FastDivideShortByByte ((ushort)(pixelPtr->A * endAlpha), 255); result = *pixelPtr; result.A = resultAlpha; } else if (this.alphaOnly && !this.alphaBlending) { result = *pixelPtr; result.A = endAlpha; } else if (!this.alphaOnly && this.alphaBlending) { result = this.normalBlendOp.Apply (*pixelPtr, this.endColor); //if (!this.alphaOnly && !this.alphaBlending) } else { result = this.endColor; } *pixelPtr = result; ++pixelPtr; } } } else { var mainrect = rect; var start = Environment.TickCount; Parallel.ForEach(Enumerable.Range (rect.Top, rect.GetBottom () + 1), (y) => ProcessGradientLine(startAlpha, endAlpha, y, mainrect, surface, src_data_ptr, src_width)); var end = Environment.TickCount; Console.WriteLine ("Time: " + (end - start)+ "ms"); } } surface.MarkDirty (); AfterRender (); }
/// <summary> /// Internal drawing context creation on a cached surface limited to slot size /// this trigger the effective drawing routine </summary> protected virtual void RecreateCache() { int stride = 4 * Slot.Width; int bmpSize = Math.Abs (stride) * Slot.Height; bmp = new byte[bmpSize]; IsDirty = false; using (ImageSurface draw = new ImageSurface(bmp, Format.Argb32, Slot.Width, Slot.Height, stride)) { using (Context gr = new Context (draw)) { gr.Antialias = Interface.Antialias; onDraw (gr); } draw.Flush (); } }
/// <summary> /// The average color of the pixels in the current represenation of the item, /// weighted for saturation and opacity. /// </summary> /// <returns> /// A <see cref="Cairo.Color"/> /// </returns> public Cairo.Color AverageColor () { if (icon_buffers [0] == null) return new Cairo.Color (1, 1, 1, 1); if (average_color.HasValue) return average_color.Value; ImageSurface sr = new ImageSurface (Format.ARGB32, icon_buffers [0].Width, icon_buffers [0].Height); using (Context cr = new Context (sr)) { cr.Operator = Operator.Source; icon_buffers [0].Internal.Show (cr, 0, 0); } sr.Flush (); byte [] data; try { data = sr.Data; } catch { return new Cairo.Color (1, 1, 1, 1); } byte r, g, b; double rTotal = 0; double gTotal = 0; double bTotal = 0; unsafe { fixed (byte* dataSrc = data) { byte* dataPtr = dataSrc; for (int i = 0; i < data.Length - 3; i += 4) { b = dataPtr [0]; g = dataPtr [1]; r = dataPtr [2]; byte max = Math.Max (r, Math.Max (g, b)); byte min = Math.Min (r, Math.Min (g, b)); double delta = max - min; double sat; if (delta == 0) { sat = 0; } else { sat = delta / max; } double score = .2 + .8 * sat; rTotal += r * score; gTotal += g * score; bTotal += b * score; dataPtr += 4; } } } double pixelCount = icon_buffers [0].Width * icon_buffers [0].Height * byte.MaxValue; // FIXME: once we use mono 2.6.3+ we can do sr.Dispose () (sr as IDisposable).Dispose (); sr.Destroy (); average_color = new Cairo.Color (rTotal / pixelCount, gTotal / pixelCount, bTotal / pixelCount) .SetValue (.8) .MultiplySaturation (1.15); return average_color.Value; }
private Pixbuf ProcessImpl(Pixbuf input, Cms.Profile input_profile, bool fast) { Pixbuf result; using (ImageInfo info = new ImageInfo (input)) { using (Widgets.SoftFocus soft = new Widgets.SoftFocus (info)) { soft.Radius = radius; using (ImageSurface surface = new ImageSurface (Format.Argb32, input.Width, input.Height)) { using (Context ctx = new Context (surface)) { soft.Apply (ctx, info.Bounds); } result = surface.ToPixbuf(); surface.Flush (); } } } return result; }