protected override FlowReturn OnTransformIp(Gst.Buffer buf) { if (!buf.IsWritable) { return(FlowReturn.Error); } Cairo.ImageSurface img = new Cairo.ImageSurface(buf.Data, Cairo.Format.Rgb24, width, height, width * 4); using (Cairo.Context context = new Cairo.Context(img)) { double dx = (double)((buf.Timestamp / Clock.MSecond) % 2180) / 5; context.Save(); context.Scale(width / 640.0, height / 480.0); context.MoveTo(300, 10 + dx); context.LineTo(500 - dx, 400); context.LineWidth = 4.0; context.Color = new Color(0, 0, 1.0); context.Stroke(); context.Restore(); if (lastX != -1 && lastY != -1) { context.Color = new Color(1.0, 0, 0); context.Translate(lastX, lastY); context.Scale(Math.Min(width / 640.0, height / 480.0), Math.Min(width / 640.0, height / 480.0)); context.Arc(0, 0, 10.0, 0.0, 2 * Math.PI); context.Fill(); } } img.Destroy(); return(base.OnTransformIp(buf)); }
static void draw (Cairo.Context gr, int width, int height) { int w, h; ImageSurface image; gr.Scale (width, height); gr.LineWidth = 0.04; gr.Arc (0.5, 0.5, 0.3, 0, 2*M_PI); gr.Clip (); gr.NewPath (); image = new ImageSurface("data/e.png"); w = image.Width; h = image.Height; gr.Scale (1.0/w, 1.0/h); image.Show (gr, 0, 0); image.Destroy(); gr.Arc (0.5, 0.5, 0.3, 0, 2 * M_PI); gr.Clip (); gr.NewPath (); gr.Rectangle (new PointD (0, 0), 1, 1); gr.Fill (); gr.Color = new Color (0, 1, 0, 1); gr.MoveTo ( new PointD (0, 0) ); gr.LineTo ( new PointD (1, 1) ); gr.MoveTo ( new PointD (1, 0) ); gr.LineTo ( new PointD (0, 1) ); gr.Stroke (); }
protected override FlowReturn OnTransformIp (Gst.Buffer buf) { if (!buf.IsWritable) return FlowReturn.Error; Cairo.ImageSurface img = new Cairo.ImageSurface (buf.Data, Cairo.Format.Rgb24, width, height, width*4); using (Cairo.Context context = new Cairo.Context (img)) { double dx = (double) ( (buf.Timestamp / Clock.MSecond) % 2180) / 5; context.Save (); context.Scale (width / 640.0, height / 480.0); context.MoveTo (300, 10 + dx); context.LineTo (500 - dx, 400); context.LineWidth = 4.0; context.Color = new Color (0, 0, 1.0); context.Stroke(); context.Restore (); if (lastX != -1 && lastY != -1) { context.Color = new Color (1.0, 0, 0); context.Translate (lastX, lastY); context.Scale (Math.Min (width / 640.0, height / 480.0), Math.Min (width / 640.0, height / 480.0)); context.Arc (0, 0, 10.0, 0.0, 2 * Math.PI); context.Fill(); } } img.Destroy (); return base.OnTransformIp (buf); }
// Returns a Gst.Buffer presentation of one 640x480 BGRA frame using Cairo static Gst.Buffer DrawData (ulong seconds) { Gst.Buffer buffer = new Gst.Buffer (640*480*4); Cairo.ImageSurface img = new Cairo.ImageSurface (buffer.Data, Cairo.Format.Argb32, 640, 480, 640*4); using (Cairo.Context context = new Cairo.Context (img)) { double dx = (double) (seconds % 2180) / 5; context.Color = new Color (1.0, 1.0, 0); context.Paint(); context.MoveTo (300, 10 + dx); context.LineTo (500 - dx, 400); context.LineWidth = 4.0; context.Color = new Color (0, 0, 1.0); context.Stroke(); } img.Destroy(); return buffer; }
// Returns a Gst.Buffer presentation of one 640x480 BGRA frame using Cairo static Gst.Buffer DrawData(ulong seconds) { Gst.Buffer buffer = new Gst.Buffer(640 * 480 * 4); Cairo.ImageSurface img = new Cairo.ImageSurface(buffer.Data, Cairo.Format.Argb32, 640, 480, 640 * 4); using (Cairo.Context context = new Cairo.Context(img)) { double dx = (double)(seconds % 2180) / 5; context.Color = new Color(1.0, 1.0, 0); context.Paint(); context.MoveTo(300, 10 + dx); context.LineTo(500 - dx, 400); context.LineWidth = 4.0; context.Color = new Color(0, 0, 1.0); context.Stroke(); } img.Destroy(); return(buffer); }
static void draw (Cairo.Context gr, int width, int height) { int w, h; ImageSurface image; gr.Scale (width, height); gr.LineWidth = 0.04; image = new ImageSurface ("data/e.png"); w = image.Width; h = image.Height; gr.Translate (0.5, 0.5); gr.Rotate (45* M_PI/180); gr.Scale (1.0/w, 1.0/h); gr.Translate (-0.5*w, -0.5*h); image.Show (gr, 0, 0); image.Destroy (); }
static void draw (Cairo.Context gr, int width, int height) { int w, h; ImageSurface image; Matrix matrix; SurfacePattern pattern; gr.Scale (width, height); gr.LineWidth = 0.04; image = new ImageSurface ("data/e.png"); w = image.Width; h = image.Height; pattern = new SurfacePattern (image); pattern.Extend = Cairo.Extend.Repeat; gr.Translate (0.5, 0.5); gr.Rotate (M_PI / 4); gr.Scale (1 / Math.Sqrt (2), 1 / Math.Sqrt (2)); gr.Translate (- 0.5, - 0.5); matrix = new Matrix (); matrix.InitScale (w * 5.0, h * 5.0); pattern.Matrix = matrix; gr.Pattern = pattern; gr.Rectangle ( new PointD (0, 0), 1.0, 1.0); gr.Fill (); pattern.Destroy (); image.Destroy(); }
protected override bool OnExposeEvent(Gdk.EventExpose args) { if (!IsRealized) return false; if (images_transition == null) return base.OnExposeEvent(args); byte[] pixels; img = (SlideImage) images_transition.Current; pixels = img.Pixels; // TODO: To change to the right signature when the new Mono.Cairo is wide spread. Fixed in Mono.Cairo r120299 ImageSurface img_sur = new ImageSurface (ref pixels, Cairo.Format.Argb32, img.Width, img.Height, img.Stride); Cairo.Context cc = Gdk.CairoHelper.Create (args.Window); cc.Save (); cc.SetSourceSurface (img_sur, 0, 0); cc.Paint (); cc.Restore (); img_sur.Destroy (); ((IDisposable)cc).Dispose(); return base.OnExposeEvent(args); }
/** DESTRUCTIVE, ASYNC */ public static ImageSurface GetThumbnail(string path) { try { Profiler pr = new Profiler ("GetThumbnail", 500); ImageSurface thumb = null; string thumbPath; if (path.StartsWith(ThumbDir)) { thumbPath = path; } else { thumbPath = NormalThumbDir + DirSepS + ThumbnailHash (path) + ".png"; if (FileExists (thumbPath) && (LastModified(path) >= LastModified(thumbPath))) Trash(thumbPath); } pr.Time ("ThumbnailHash"); if (!FileExists(thumbPath)) { if (!FileExists(ThumbDir)) new UnixDirectoryInfo(ThumbDir).Create (); if (!FileExists(NormalThumbDir)) new UnixDirectoryInfo(NormalThumbDir).Create (); if (CreateThumbnail(path, thumbPath, thumbSize)) { pr.Time ("create thumbnail"); thumb = new ImageSurface (thumbPath); } } else { thumb = new ImageSurface (thumbPath); if (thumb.Width > thumbSize || thumb.Height > thumbSize) { ImageSurface nthumb = ScaleDownSurface (thumb, thumbSize); thumb.Destroy (); thumb.Destroy (); thumb = nthumb; } } if (thumb == null || thumb.Width < 1 || thumb.Height < 1) { if (FileExists(thumbPath)) Trash(thumbPath); throw new ArgumentException (String.Format("Failed to thumbnail {0}",path), "path"); } pr.Time ("load as ImageSurface"); return thumb; } catch (Exception e) { LogError ("Thumbnailing failed for {0}: {1}", path, e); ImageSurface thumb = new ImageSurface (Format.ARGB32, 1, 1); using (Context cr = new Context(thumb)) { cr.Color = new Color (1,0,0); cr.Rectangle (0,0,2,2); cr.Fill (); } return thumb; } }
public void imagepattern(Context cr, int width, int height) { Normalize (cr, width, height); ImageSurface image = new ImageSurface ("data/romedalen.png"); int w = image.Width; int h = image.Height; SurfacePattern pattern = new SurfacePattern (image); pattern.Extend = Extend.Repeat; cr.Translate (0.5, 0.5); cr.Rotate (Math.PI / 4); cr.Scale (1 / Math.Sqrt (2), 1 / Math.Sqrt (2)); cr.Translate (- 0.5, - 0.5); Matrix matrix = new Matrix (); matrix.InitScale (w * 5.0, h * 5.0); pattern.Matrix = matrix; cr.Source = pattern; cr.Rectangle (0, 0, 1.0, 1.0); cr.Fill (); pattern.Destroy (); image.Destroy (); }
public void image(Context cr, int width, int height) { Normalize (cr, width, height); ImageSurface image = new ImageSurface ("data/romedalen.png"); int w = image.Width; int h = image.Height; cr.Translate (0.5, 0.5); cr.Rotate (45* Math.PI/180); cr.Scale (1.0/w, 1.0/h); cr.Translate (-0.5*w, -0.5*h); cr.SetSourceSurface (image, 0, 0); cr.Paint (); image.Destroy (); }
public void clip_image(Context cr, int width, int height) { Normalize (cr, width, height); cr.Arc (0.5, 0.5, 0.3, 0, 2*Math.PI); cr.Clip (); cr.NewPath (); // path not consumed by clip() ImageSurface image = new ImageSurface ("data/romedalen.png"); int w = image.Width; int h = image.Height; cr.Scale (1.0/w, 1.0/h); cr.SetSourceSurface (image, 0, 0); cr.Paint (); image.Destroy (); }
/// <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; }
public unsafe static void GaussianBlur (this DockySurface self, int size) { // Note: This method is wickedly slow int gaussWidth = size * 2 + 1; double[] kernel = BuildGaussianKernel (gaussWidth); ImageSurface original = new ImageSurface (Format.Argb32, self.Width, self.Height); using (Cairo.Context cr = new Cairo.Context (original)) self.Internal.Show (cr, 0, 0); double gaussSum = 0; foreach (double d in kernel) gaussSum += d; for (int i = 0; i < kernel.Length; i++) kernel[i] = kernel[i] / gaussSum; int width = self.Width; int height = self.Height; byte[] src = original.Data; double[] xbuffer = new double[original.Data.Length]; double[] ybuffer = new double[original.Data.Length]; int dest, dest2, shift, source; byte* srcPtr = (byte*) original.DataPtr; fixed (double* xbufferPtr = xbuffer) fixed (double* ybufferPtr = ybuffer) fixed (double* kernelPtr = kernel) { // Horizontal Pass for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { dest = y * width + x; dest2 = dest * 4; for (int k = 0; k < gaussWidth; k++) { shift = k - size; source = dest + shift; if (x + shift <= 0 || x + shift >= width) { source = dest; } source = source * 4; xbufferPtr[dest2 + 0] = xbufferPtr[dest2 + 0] + (srcPtr[source + 0] * kernelPtr[k]); xbufferPtr[dest2 + 1] = xbufferPtr[dest2 + 1] + (srcPtr[source + 1] * kernelPtr[k]); xbufferPtr[dest2 + 2] = xbufferPtr[dest2 + 2] + (srcPtr[source + 2] * kernelPtr[k]); xbufferPtr[dest2 + 3] = xbufferPtr[dest2 + 3] + (srcPtr[source + 3] * kernelPtr[k]); } } } // Vertical Pass for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { dest = y * width + x; dest2 = dest * 4; for (int k = 0; k < gaussWidth; k++) { shift = k - size; source = dest + shift * width; if (y + shift <= 0 || y + shift >= height) { source = dest; } source = source * 4; ybufferPtr[dest2 + 0] = ybufferPtr[dest2 + 0] + (xbufferPtr[source + 0] * kernelPtr[k]); ybufferPtr[dest2 + 1] = ybufferPtr[dest2 + 1] + (xbufferPtr[source + 1] * kernelPtr[k]); ybufferPtr[dest2 + 2] = ybufferPtr[dest2 + 2] + (xbufferPtr[source + 2] * kernelPtr[k]); ybufferPtr[dest2 + 3] = ybufferPtr[dest2 + 3] + (xbufferPtr[source + 3] * kernelPtr[k]); } } } for (int i = 0; i < src.Length; i++) srcPtr[i] = (byte) ybufferPtr[i]; } self.Context.Operator = Operator.Source; self.Context.SetSource (original); self.Context.Paint (); (original as IDisposable).Dispose (); original.Destroy (); }
private ImageInfo CreateBlur(ImageInfo source) { double scale = Math.Max (256 / (double) source.Bounds.Width, 256 / (double) source.Bounds.Height); Gdk.Rectangle small = new Gdk.Rectangle (0, 0, (int) Math.Ceiling (source.Bounds.Width * scale), (int) Math.Ceiling (source.Bounds.Height * scale)); ImageSurface image = new ImageSurface (Format.Argb32, small.Width, small.Height); Context ctx = new Context (image); ctx.Matrix = source.Fit (small); ctx.Operator = Operator.Source; Pattern p = new SurfacePattern (source.Surface); ctx.Source = p; ctx.Paint (); p.Destroy (); ((IDisposable)ctx).Dispose (); Gdk.Pixbuf normal = image.ToPixbuf(); Gdk.Pixbuf blur = PixbufUtils.Blur (normal, 3, null); ImageInfo overlay = new ImageInfo (blur); blur.Dispose (); normal.Dispose (); image.Destroy (); return overlay; }
private Pattern RenderBar(int w, int h) { ImageSurface s = new ImageSurface (Format.Argb32, w, h); Context cr = new Context (s); RenderBar (cr, w, h, h / 2); // TODO Implement the new ctor - see http://bugzilla.gnome.org/show_bug.cgi?id=561394 #pragma warning disable 0618 Pattern pattern = new Pattern (s); #pragma warning restore 0618 s.Destroy (); ((IDisposable)cr).Dispose (); return pattern; }