~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta/ConfigurableEffects/CurvesEffect.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:
 
1
// 
 
2
// CurvesEffect.cs
 
3
//  
 
4
// Author:
 
5
//       Krzysztof Marecki <marecki.krzysztof@gmail.com>
 
6
// 
 
7
// Copyright (c) 2010 Jonathan Pobst
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using Cairo;
 
31
 
 
32
namespace Pinta.Core
 
33
{
 
34
 
 
35
        public class CurvesEffect : BaseEffect
 
36
        {
 
37
                private SortedList<int, int>[] control_points;
 
38
                private ColorTransferMode mode;
 
39
                        
 
40
                public override string Icon {
 
41
                        get { return "Menu.Adjustments.Curves.png"; }
 
42
                }
 
43
 
 
44
                public override string Text {
 
45
                        get { return Mono.Unix.Catalog.GetString ("Curves"); }
 
46
                }
 
47
 
 
48
                public override bool IsConfigurable {
 
49
                        get { return true; }
 
50
                }
 
51
                
 
52
                public override bool LaunchConfiguration ()
 
53
                {
 
54
                        CurvesDialog dialog = new CurvesDialog ();
 
55
                        dialog.Icon = PintaCore.Resources.GetIcon (Icon);
 
56
                        int response = dialog.Run ();
 
57
                        
 
58
                        if (response == (int)Gtk.ResponseType.Ok) {
 
59
                                
 
60
                                control_points = dialog.ControlPoints;
 
61
                                mode = dialog.Mode;
 
62
                                
 
63
                                dialog.Destroy ();
 
64
                                return true;
 
65
                        }
 
66
 
 
67
                        dialog.Destroy ();
 
68
                        return false;
 
69
                }
 
70
                
 
71
                public override void RenderEffect (ImageSurface src, ImageSurface dest, Gdk.Rectangle[] rois)
 
72
                {
 
73
                        UnaryPixelOp op = MakeUop ();
 
74
                        
 
75
                        op.Apply (dest, src, rois);
 
76
                }
 
77
                
 
78
                private UnaryPixelOp MakeUop()
 
79
        {
 
80
            UnaryPixelOp op;
 
81
            byte[][] transferCurves;
 
82
            int entries;
 
83
 
 
84
            switch (mode) {
 
85
                case ColorTransferMode.Rgb:
 
86
                    UnaryPixelOps.ChannelCurve cc = new UnaryPixelOps.ChannelCurve();
 
87
                    transferCurves = new byte[][] { cc.CurveR, cc.CurveG, cc.CurveB };
 
88
                    entries = 256;
 
89
                    op = cc;
 
90
                    break;
 
91
 
 
92
                case ColorTransferMode.Luminosity:
 
93
                    UnaryPixelOps.LuminosityCurve lc = new UnaryPixelOps.LuminosityCurve();
 
94
                    transferCurves = new byte[][] { lc.Curve };
 
95
                    entries = 256;
 
96
                    op = lc;
 
97
                    break;
 
98
 
 
99
                default:
 
100
                    throw new InvalidEnumArgumentException();
 
101
            }
 
102
 
 
103
            
 
104
            int channels = transferCurves.Length;
 
105
 
 
106
            for (int channel = 0; channel < channels; channel++) {
 
107
                SortedList<int, int> channelControlPoints = control_points[channel];
 
108
                IList<int> xa = channelControlPoints.Keys;
 
109
                IList<int> ya = channelControlPoints.Values;
 
110
                SplineInterpolator interpolator = new SplineInterpolator();
 
111
                int length = channelControlPoints.Count;
 
112
 
 
113
                for (int i = 0; i < length; i++) {
 
114
                    interpolator.Add(xa[i], ya[i]);
 
115
                }
 
116
 
 
117
                for (int i = 0; i < entries; i++) {
 
118
                    transferCurves[channel][i] = Utility.ClampToByte(interpolator.Interpolate(i));
 
119
                }
 
120
            }
 
121
 
 
122
            return op;
 
123
        }
 
124
 
 
125
        }
 
126
}