///////////////////////////////////////////////////////////////////////////////// // Paint.NET // // Copyright (C) Rick Brewster, Tom Jackson, and past contributors. // // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. // // See src/Resources/Files/License.txt for full licensing and attribution // // details. // // . // ///////////////////////////////////////////////////////////////////////////////// using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; namespace PaintDotNet.Effects { public class CurvesEffectConfigToken : EffectConfigToken { private SortedList[] controlPoints; public SortedList[] ControlPoints { get { return controlPoints; } set { uop = null; controlPoints = value; } } private ColorTransferMode colorTransferMode; public ColorTransferMode ColorTransferMode { get { return colorTransferMode; } set { uop = null; colorTransferMode = value; } } protected UnaryPixelOp uop; public UnaryPixelOp Uop { get { if (uop == null) { uop = MakeUop(); } return uop; } } protected UnaryPixelOp MakeUop() { UnaryPixelOp uopRet; byte[][] transferCurves; int entries; switch (colorTransferMode) { case ColorTransferMode.Rgb: UnaryPixelOps.ChannelCurve cc = new UnaryPixelOps.ChannelCurve(); transferCurves = new byte[][] { cc.CurveR, cc.CurveG, cc.CurveB }; entries = 256; uopRet = cc; break; case ColorTransferMode.Luminosity: UnaryPixelOps.LuminosityCurve lc = new UnaryPixelOps.LuminosityCurve(); transferCurves = new byte[][] { lc.Curve }; entries = 256; uopRet = lc; break; default: throw new InvalidEnumArgumentException(); } int channels = transferCurves.Length; for (int channel = 0; channel < channels; ++channel) { SortedList channelControlPoints = controlPoints[channel]; IList xa = channelControlPoints.Keys; IList ya = channelControlPoints.Values; SplineInterpolator interpolator = new SplineInterpolator(); int length = channelControlPoints.Count; for (int i = 0; i < length; ++i) { interpolator.Add(xa[i], ya[i]); } for (int i = 0; i < entries; ++i) { transferCurves[channel][i] = Utility.ClampToByte(interpolator.Interpolate(i)); } } return uopRet; } public override object Clone() { return new CurvesEffectConfigToken(this); } public CurvesEffectConfigToken() { controlPoints = new SortedList[1]; for (int i = 0; i < this.controlPoints.Length; ++i) { SortedList newList = new SortedList(); newList.Add(0, 0); newList.Add(255, 255); controlPoints[i] = newList; } colorTransferMode = ColorTransferMode.Luminosity; } protected CurvesEffectConfigToken(CurvesEffectConfigToken copyMe) : base(copyMe) { this.uop = copyMe.Uop; this.colorTransferMode = copyMe.ColorTransferMode; this.controlPoints = (SortedList[])copyMe.controlPoints.Clone(); } } }