~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta.Core/Extensions/CairoExtensions.cs

  • Committer: Iain Lane
  • Date: 2010-03-13 18:20:18 UTC
  • mfrom: (1.1.2)
  • Revision ID: git-v1:1781694a68ecf232d0110c0b2f3d4a1b96c74ec2
Merge commit 'upstream/0.2'

Conflicts:
        debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
115
115
                        return dirty;
116
116
                }
117
117
                
 
118
                public static Rectangle DrawPolygonal (this Context g, PointD[] points, Color color)
 
119
                {
 
120
                        Random rand=new Random();
 
121
                        
 
122
                        g.Save ();
 
123
                        g.MoveTo (points [0]);
 
124
                        foreach (var point in points) {
 
125
                                g.LineTo (point.X - rand.NextDouble()*0, point.Y);
 
126
                                //g.Stroke();
 
127
                        }
 
128
                        
 
129
                        g.Color = color;
 
130
                        
 
131
                        Rectangle dirty = g.StrokeExtents ();
 
132
                        g.Stroke ();
 
133
 
 
134
                        g.Restore ();
 
135
 
 
136
                        return dirty;
 
137
                }
 
138
                
 
139
                
 
140
                public static Rectangle FillPolygonal (this Context g, PointD[] points, Color color)
 
141
                {
 
142
                        g.Save ();
 
143
                        
 
144
                        g.MoveTo (points [0]);
 
145
                        foreach (var point in points)
 
146
                                g.LineTo (point);
 
147
                        
 
148
                        g.Color = color;
 
149
                        
 
150
                        Rectangle dirty = g.StrokeExtents ();
 
151
                        g.Fill ();
 
152
 
 
153
                        g.Restore ();
 
154
 
 
155
                        return dirty;
 
156
                }
 
157
                
118
158
                public static Rectangle FillStrokedRectangle (this Context g, Rectangle r, Color fill, Color stroke, int lineWidth)
119
159
                {
120
160
                        double x = r.X;
372
412
                        
373
413
                        return p;
374
414
                }
375
 
                
 
415
 
 
416
                public static Rectangle DrawLine (this Context g, PointD p1, PointD p2, Color color, int lineWidth)
 
417
                {
 
418
                        // Put it on a pixel line
 
419
                        if (lineWidth == 1)
 
420
                                p1 = new PointD (p1.X - 0.5, p1.Y - 0.5);
 
421
 
 
422
                        g.Save ();
 
423
 
 
424
                        g.MoveTo (p1.X, p1.Y);
 
425
                        g.LineTo (p2.X, p2.Y);
 
426
 
 
427
                        g.Color = color;
 
428
                        g.LineWidth = lineWidth;
 
429
                        g.LineCap = LineCap.Square;
 
430
 
 
431
                        Rectangle dirty = g.StrokeExtents ();
 
432
                        g.Stroke ();
 
433
 
 
434
                        g.Restore ();
 
435
 
 
436
                        return dirty;
 
437
                }
 
438
 
376
439
                public static void DrawPixbuf (this Context g, Gdk.Pixbuf pixbuf, Point dest)
377
440
                {
378
441
                        g.Save ();
452
515
                        return new Color (dstPtr->R / 255f, dstPtr->G / 255f, dstPtr->B / 255f, dstPtr->A / 255f);
453
516
                }
454
517
                
 
518
                public unsafe static void SetPixel (this Cairo.ImageSurface surf, int x, int y, Color color)
 
519
                {
 
520
                        ColorBgra* dstPtr = (ColorBgra*)surf.DataPtr;
 
521
 
 
522
                        dstPtr += (x) + (y * surf.Width);
 
523
 
 
524
                        dstPtr->R = (byte)(color.R * 255);
 
525
                        dstPtr->G = (byte)(color.G * 255);
 
526
                        dstPtr->B = (byte)(color.B * 255);
 
527
                        dstPtr->A = (byte)(color.A * 255);
 
528
                }
 
529
 
 
530
                public unsafe static ColorBgra GetColorBgra (this Cairo.ImageSurface surf, int x, int y)
 
531
                {
 
532
                        ColorBgra* dstPtr = (ColorBgra*)surf.DataPtr;
 
533
 
 
534
                        dstPtr += (x) + (y * surf.Width);
 
535
 
 
536
                        return *dstPtr;
 
537
                }
 
538
 
 
539
                public static ColorBgra ToColorBgra (this Cairo.Color color)
 
540
                {
 
541
                        ColorBgra c = new ColorBgra ();
 
542
 
 
543
                        c.R = (byte)(color.R * 255);
 
544
                        c.G = (byte)(color.G * 255);
 
545
                        c.B = (byte)(color.B * 255);
 
546
                        c.A = (byte)(color.A * 255);
 
547
 
 
548
                        return c;
 
549
                }
 
550
 
 
551
                public static Cairo.Color ToCairoColor (this ColorBgra color)
 
552
                {
 
553
                        Cairo.Color c = new Cairo.Color ();
 
554
 
 
555
                        c.R = color.R / 255d;
 
556
                        c.G = color.G / 255d;
 
557
                        c.B = color.B / 255d;
 
558
                        c.A = color.A / 255d;
 
559
 
 
560
                        return c;
 
561
                }
 
562
                
 
563
                public static Gdk.Color ToGdkColor (this ColorBgra color)
 
564
                {
 
565
                        Gdk.Color c = new Gdk.Color (color.R, color.G, color.B);
 
566
                        
 
567
                        return c;
 
568
                }
 
569
 
455
570
                public static string ToString2 (this Cairo.Color c)
456
571
                {
457
572
                        return string.Format ("R: {0} G: {1} B: {2} A: {3}", c.R, c.G, c.B, c.A);
481
596
                        return newpath;
482
597
                }
483
598
                
484
 
                public static Rectangle GetBounds (this Path path)
 
599
                public static Gdk.Rectangle GetBounds (this Path path)
485
600
                {
486
601
                        Rectangle rect;
487
602
 
488
603
                        using (Context g = new Context (PintaCore.Layers.CurrentLayer.Surface)) {
489
604
                                g.AppendPath (PintaCore.Layers.SelectionPath);
 
605
 
 
606
                                // We don't want the bounding box to include a stroke width 
 
607
                                // of 1, but setting it to 0 returns an empty rectangle.  Set
 
608
                                // it to a sufficiently small width and rounding takes care of it
 
609
                                g.LineWidth = .01;
490
610
                                rect = g.StrokeExtents ();
491
611
                        }
492
612
 
493
 
                        return new Rectangle (rect.X, rect.Y, rect.Width - rect.X, rect.Height - rect.Y);
 
613
                        return new Gdk.Rectangle ((int)rect.X, (int)rect.Y, (int)rect.Width - (int)rect.X, (int)rect.Height - (int)rect.Y);
494
614
                }
495
615
                
496
616
                public static Gdk.Color ToGdkColor (this Cairo.Color color)
502
622
                        
503
623
                        return c;
504
624
                }
 
625
                
 
626
                public static ushort GdkColorAlpha (this Cairo.Color color)
 
627
                {
 
628
                        return (ushort)(color.A * ushort.MaxValue);
 
629
                }
505
630
 
506
631
                public static double GetBottom (this Rectangle rect)
507
632
                {
518
643
                        ColorBgra* dstPtr = (ColorBgra*)surf.DataPtr;
519
644
 
520
645
                        dstPtr += (x) + (y * surf.Width);
521
 
 
522
 
                        return dstPtr;
 
646
                        
 
647
                        return dstPtr;
 
648
                }
 
649
 
 
650
                public static unsafe ColorBgra* GetPointAddressUnchecked (this ImageSurface surf, ColorBgra* srcDataPtr, int srcWidth, int x, int y)
 
651
                {
 
652
                        ColorBgra* dstPtr = srcDataPtr;
 
653
 
 
654
                        dstPtr += (x) + (y * srcWidth);
 
655
 
 
656
                        return dstPtr;
 
657
                }
 
658
 
 
659
                public static unsafe ColorBgra GetPointUnchecked (this ImageSurface surf, int x, int y)
 
660
                {
 
661
                        ColorBgra* dstPtr = (ColorBgra*)surf.DataPtr;
 
662
 
 
663
                        dstPtr += (x) + (y * surf.Width);
 
664
 
 
665
                        return *dstPtr;
 
666
                }
 
667
 
 
668
                // This isn't really an extension method, since it doesn't use
 
669
                // the passed in argument, but it's nice to have the same calling
 
670
                // convention as the uncached version.  If you can use this one
 
671
                // over the other, it is much faster in tight loops (like effects).
 
672
                public static unsafe ColorBgra GetPointUnchecked (this ImageSurface surf, ColorBgra* srcDataPtr, int srcWidth, int x, int y)
 
673
                {
 
674
                        ColorBgra* dstPtr = srcDataPtr;
 
675
 
 
676
                        dstPtr += (x) + (y * srcWidth);
 
677
 
 
678
                        return *dstPtr;
 
679
                }
 
680
 
 
681
                public static unsafe ColorBgra* GetRowAddressUnchecked (this ImageSurface surf, int y)
 
682
                {
 
683
                        ColorBgra* dstPtr = (ColorBgra*)surf.DataPtr;
 
684
 
 
685
                        dstPtr += y * surf.Width;
 
686
 
 
687
                        return dstPtr;
 
688
                }
 
689
 
 
690
                public static unsafe ColorBgra* GetRowAddressUnchecked (this ImageSurface surf, ColorBgra* srcDataPtr, int srcWidth, int y)
 
691
                {
 
692
                        ColorBgra* dstPtr = srcDataPtr;
 
693
 
 
694
                        dstPtr += y * srcWidth;
 
695
 
 
696
                        return dstPtr;
 
697
                }
 
698
 
 
699
                public static unsafe ColorBgra *GetPointAddress (this ImageSurface surf, int x, int y)
 
700
                {
 
701
                        if (x < 0 || x >= surf.Width)
 
702
                                throw new ArgumentOutOfRangeException ("x", "Out of bounds: x=" + x.ToString ());
 
703
 
 
704
                        return surf.GetPointAddressUnchecked (x, y);
 
705
                }
 
706
 
 
707
                public static unsafe ColorBgra* GetPointAddress (this ImageSurface surf, Gdk.Point point)
 
708
                {
 
709
                        return surf.GetPointAddress (point.X, point.Y);
 
710
                }
 
711
 
 
712
                public static Gdk.Rectangle GetBounds (this ImageSurface surf)
 
713
                {
 
714
                        return new Gdk.Rectangle (0, 0, surf.Width, surf.Height);
 
715
                }
 
716
 
 
717
                public static Gdk.Size GetSize (this ImageSurface surf)
 
718
                {
 
719
                        return new Gdk.Size (surf.Width, surf.Height);
523
720
                }
524
721
        }
525
722
}