~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta.Core/Effects/UnaryPixelOp.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:
2
2
// Paint.NET                                                                   //
3
3
// Copyright (C) Rick Brewster, Tom Jackson, and past contributors.            //
4
4
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
5
 
// See src/Resources/Files/License.txt for full licensing and attribution      //
6
 
// details.                                                                    //
7
 
// .                                                                           //
 
5
// See license-pdn.txt for full licensing and attribution details.             //
8
6
/////////////////////////////////////////////////////////////////////////////////
9
7
 
10
8
using System;
11
9
using System.Threading;
 
10
using Cairo;
12
11
 
13
12
namespace Pinta.Core
14
13
{
15
 
    /// <summary>
16
 
    /// Defines a way to operate on a pixel, or a region of pixels, in a unary fashion.
17
 
    /// That is, it is a simple function F that takes one parameter and returns a
18
 
    /// result of the form: d = F(c)
19
 
    /// </summary>
20
 
    [Serializable]
21
 
    public unsafe abstract class UnaryPixelOp
22
 
        : PixelOp
23
 
    {
24
 
        public abstract ColorBgra Apply(ColorBgra color);
25
 
 
26
 
        public unsafe override void Apply(ColorBgra *dst, ColorBgra *src, int length)
27
 
        {
28
 
            unsafe
29
 
            {
30
 
                while (length > 0)
31
 
                {
32
 
                    *dst = Apply(*src);
33
 
                    ++dst;
34
 
                    ++src;
35
 
                    --length;
36
 
                }
37
 
            }
38
 
        }
39
 
 
40
 
        public unsafe virtual void Apply(ColorBgra* ptr, int length)
41
 
        {
42
 
            unsafe
43
 
            {
44
 
                while (length > 0)
45
 
                {
46
 
                    *ptr = Apply(*ptr);
47
 
                    ++ptr;
48
 
                    --length;
49
 
                }
50
 
            }
51
 
        }
52
 
 
53
 
//        private unsafe void ApplyRectangle(Surface surface, Rectangle rect)
54
 
//        {
55
 
//            for (int y = rect.Top; y < rect.Bottom; ++y)
56
 
//            {
57
 
//                ColorBgra *ptr = surface.GetPointAddress(rect.Left, y);
58
 
//                Apply(ptr, rect.Width);
59
 
//            }
60
 
//        }
61
 
//
62
 
//        public void Apply(Surface surface, Rectangle[] roi, int startIndex, int length)
63
 
//        {
64
 
//            Rectangle regionBounds = Utility.GetRegionBounds(roi, startIndex, length);
65
 
//
66
 
//            if (regionBounds != Rectangle.Intersect(surface.Bounds, regionBounds))
67
 
//            {
68
 
//                throw new ArgumentOutOfRangeException("roi", "Region is out of bounds");
69
 
//            }
70
 
//
71
 
//            unsafe
72
 
//            {
73
 
//                for (int x = startIndex; x < startIndex + length; ++x)
74
 
//                {
75
 
//                    ApplyRectangle(surface, roi[x]);
76
 
//                }
77
 
//            }
78
 
//        }
79
 
//
80
 
//        public void Apply(Surface surface, Rectangle[] roi)
81
 
//        {
82
 
//            Apply(surface, roi, 0, roi.Length);
83
 
//        }
84
 
//
85
 
//        public void Apply(Surface surface, RectangleF[] roiF, int startIndex, int length)
86
 
//        {
87
 
//            Rectangle regionBounds = Rectangle.Truncate(Utility.GetRegionBounds(roiF, startIndex, length));
88
 
//
89
 
//            if (regionBounds != Rectangle.Intersect(surface.Bounds, regionBounds))
90
 
//            {
91
 
//                throw new ArgumentOutOfRangeException("roiF", "Region is out of bounds");
92
 
//            }
93
 
//
94
 
//            unsafe
95
 
//            {
96
 
//                for (int x = startIndex; x < startIndex + length; ++x)
97
 
//                {
98
 
//                    ApplyRectangle(surface, Rectangle.Truncate(roiF[x]));
99
 
//                }
100
 
//            }
101
 
//        }
102
 
//
103
 
//        public void Apply(Surface surface, RectangleF[] roiF)
104
 
//        {
105
 
//            Apply(surface, roiF, 0, roiF.Length);
106
 
//        }
107
 
//
108
 
//        public unsafe void Apply(Surface surface, Rectangle roi)
109
 
//        {
110
 
//            ApplyRectangle(surface, roi);
111
 
//        }
112
 
//
113
 
//        public void Apply(Surface surface, Scanline scan)
114
 
//        {
115
 
//            Apply(surface.GetPointAddress(scan.X, scan.Y), scan.Length);
116
 
//        }
117
 
//
118
 
//        public void Apply(Surface surface, Scanline[] scans)
119
 
//        {
120
 
//            foreach (Scanline scan in scans)
121
 
//            {
122
 
//                Apply(surface, scan);
123
 
//            }
124
 
//        }
125
 
//
126
 
//        public override void Apply(Surface dst, Point dstOffset, Surface src, Point srcOffset, int scanLength)
127
 
//        {
128
 
//            Apply(dst.GetPointAddress(dstOffset), src.GetPointAddress(srcOffset), scanLength);
129
 
//        }
130
 
//
131
 
//        public void Apply(Surface dst, Surface src, Rectangle roi)
132
 
//        {
133
 
//            for (int y = roi.Top; y < roi.Bottom; ++y)
134
 
//            {
135
 
//                ColorBgra *dstPtr = dst.GetPointAddress(roi.Left, y);
136
 
//                ColorBgra *srcPtr = src.GetPointAddress(roi.Left, y);
137
 
//                Apply(dstPtr, srcPtr, roi.Width);
138
 
//            }
139
 
//        }
140
 
//
141
 
//        public void Apply(Surface surface, PdnRegion roi)
142
 
//        {
143
 
//            Apply(surface, roi.GetRegionScansReadOnlyInt());
144
 
//        }
145
 
 
146
 
        public UnaryPixelOp()
147
 
        {
148
 
        }
149
 
    }
 
14
        /// <summary>
 
15
        /// Defines a way to operate on a pixel, or a region of pixels, in a unary fashion.
 
16
        /// That is, it is a simple function F that takes one parameter and returns a
 
17
        /// result of the form: d = F(c)
 
18
        /// </summary>
 
19
        [Serializable]
 
20
        public unsafe abstract class UnaryPixelOp : PixelOp
 
21
        {
 
22
                public UnaryPixelOp ()
 
23
                {
 
24
                }
 
25
 
 
26
                public abstract ColorBgra Apply (ColorBgra color);
 
27
 
 
28
                public unsafe override void Apply (ColorBgra* dst, ColorBgra* src, int length)
 
29
                {
 
30
                        unsafe {
 
31
                                while (length > 0) {
 
32
                                        *dst = Apply (*src);
 
33
                                        ++dst;
 
34
                                        ++src;
 
35
                                        --length;
 
36
                                }
 
37
                        }
 
38
                }
 
39
 
 
40
                public unsafe virtual void Apply (ColorBgra* ptr, int length)
 
41
                {
 
42
                        unsafe {
 
43
                                while (length > 0) {
 
44
                                        *ptr = Apply (*ptr);
 
45
                                        ++ptr;
 
46
                                        --length;
 
47
                                }
 
48
                        }
 
49
                }
 
50
 
 
51
                private unsafe void ApplyRectangle (ImageSurface surface, Gdk.Rectangle rect)
 
52
                {
 
53
                        for (int y = rect.Left; y < rect.Bottom; ++y) {
 
54
                                ColorBgra* ptr = surface.GetPointAddress (rect.Left, y);
 
55
                                Apply (ptr, rect.Width);
 
56
                        }
 
57
                }
 
58
 
 
59
                public void Apply (ImageSurface surface, Gdk.Rectangle[] roi, int startIndex, int length)
 
60
                {
 
61
                        Gdk.Rectangle regionBounds = Utility.GetRegionBounds (roi, startIndex, length);
 
62
 
 
63
                        if (regionBounds != Gdk.Rectangle.Intersect (surface.GetBounds (), regionBounds))
 
64
                                throw new ArgumentOutOfRangeException ("roi", "Region is out of bounds");
 
65
 
 
66
                        unsafe {
 
67
                                for (int x = startIndex; x < startIndex + length; ++x)
 
68
                                        ApplyRectangle (surface, roi[x]);
 
69
                        }
 
70
                }
 
71
 
 
72
                public void Apply (ImageSurface surface, Gdk.Rectangle[] roi)
 
73
                {
 
74
                        Apply (surface, roi, 0, roi.Length);
 
75
                }
 
76
 
 
77
                public unsafe void Apply (ImageSurface surface, Gdk.Rectangle roi)
 
78
                {
 
79
                        ApplyRectangle (surface, roi);
 
80
                }
 
81
 
 
82
                public override void Apply (ImageSurface dst, Gdk.Point dstOffset, ImageSurface src, Gdk.Point srcOffset, int scanLength)
 
83
                {
 
84
                        Apply (dst.GetPointAddress (dstOffset), src.GetPointAddress (srcOffset), scanLength);
 
85
                }
 
86
                
 
87
                public void Apply (ImageSurface dst, ImageSurface src, Gdk.Rectangle roi)
 
88
                {
 
89
                        for (int y = roi.Y; y < roi.Bottom; ++y) {
 
90
                                ColorBgra* dstPtr = dst.GetPointAddressUnchecked (roi.X, y);
 
91
                                ColorBgra* srcPtr = src.GetPointAddressUnchecked (roi.X, y);
 
92
                                Apply (dstPtr, srcPtr, roi.Width);
 
93
                        }
 
94
                }
 
95
 
 
96
                public void Apply (ImageSurface dst, ImageSurface src, Gdk.Rectangle[] rois)
 
97
                {
 
98
                        foreach (Gdk.Rectangle roi in rois)
 
99
                                Apply (dst, src, roi);
 
100
                }
 
101
        }
150
102
}