BoltBait's Pages - Portrait Effect

Sarcasm, just one of the services I offer.
 

Paint.NET Portrait Effect

I'm still learning how to create complex effect filters for Paint.NET.  This is the first one that I wrote that combines preexisting effects.  After I wrote this one, I did the Ink Sketch effect.

 

The Idea

I thought that it would be easy to automate the steps that are sometimes used to make portraits look softer and more professional.  You can use this on other pictures, but it really is meant for portraits.  It adds a little brown to smooth out skin but not too much detail is lost.

NOTE:Rick has bundled this effect with Paint.NET version 3.10 and beyond as "Soften Portrait".  If you have upgraded your Paint.NET to 3.10 or higher, there is no need to download this effect DLL--you already have it!

  

The Effect DLL

You can download the precompiled effect DLL here: PortraitEffect.dll

Just drop this file in your \program files\Paint.NET\effects directory and you should be all set.

If you need help installing effects, read this page: Installing Effects 

 

Instructions for Use

The best way to use this is to follow these steps:

1.  Open your graphic.

2.  Click the Effects > Portrait menu.

3.  Adjust the sliders for the desired effect.

 

 

Source Code

Note: This is not a CodeLab script.

/* PortraitEffect.cs 
Copyright (c) 2007 David Issel 
Contact Info: BoltBait@hotmail.com http://www.BoltBait.com 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 
*/ 
using System; 
using System.Collections; 
using System.Drawing; 
using PaintDotNet; 
using PaintDotNet.Effects; 

namespace PortraitEffect 
{ 
    public class EffectPlugin 
        : PaintDotNet.Effects.Effect 
    { 
        public static string StaticName 
        { 
            get 
            { 
                return "Portrait"; 
            } 
        } 

        public static Bitmap StaticIcon 
        { 
            get 
            { 
                return new Bitmap(typeof(EffectPlugin), "EffectPluginIcon.png"); 
            } 
        } 
        private BlurEffect blurEffect = new BlurEffect(); 
        private UnaryPixelOps.Desaturate desaturateOp = new UnaryPixelOps.Desaturate(); 
        private SepiaEffect sepiaEffect = new SepiaEffect(); 
        private BrightnessAndContrastAdjustment bacAdjustment = new BrightnessAndContrastAdjustment(); 
        private UserBlendOps.OverlayBlendOp OverlayOp = new UserBlendOps.OverlayBlendOp(); 
        private UnaryPixelOp levels; 

        public override unsafe void Render(EffectConfigToken parameters, RenderArgs dstArgs, RenderArgs srcArgs, Rectangle[] rois, int startIndex, int length) 
        { 
            ThreeAmountsConfigToken tacd = (ThreeAmountsConfigToken)parameters; 
            float RedAdjust = 1.0f + ((float)tacd.Amount3 / 100.0f); 
            float BlueAdjust = 1.0f - ((float)tacd.Amount3 / 100.0f); 

            AmountEffectConfigToken blurToken = new AmountEffectConfigToken(tacd.Amount1 * 3); 
            BrightnessAndContrastAdjustmentConfigToken bacToken = new BrightnessAndContrastAdjustmentConfigToken(tacd.Amount2, -tacd.Amount2/2); 

            this.blurEffect.Render(blurToken, dstArgs, srcArgs, rois, startIndex, length); 
            this.bacAdjustment.Render(bacToken, dstArgs, dstArgs, rois, startIndex, length); 

            for (int i = startIndex; i < startIndex + length; ++i) 
            { 
                Rectangle roi = rois[i]; 

                for (int y = roi.Top; y < roi.Bottom; ++y) 
                { 
                    ColorBgra* srcPtr = srcArgs.Surface.GetPointAddress(roi.X, roi.Y); 
                    ColorBgra* dstPtr = dstArgs.Surface.GetPointAddress(roi.X, roi.Y); 

                    for (int x = roi.Left; x < roi.Right; ++x) 
                    { 
                        ColorBgra srcGrey = this.desaturateOp.Apply(*srcPtr); 
                        srcGrey.R = Utility.ClampToByte((int)((float)srcGrey.R * RedAdjust)); 
                        srcGrey.B = Utility.ClampToByte((int)((float)srcGrey.B * BlueAdjust)); 
                        ColorBgra mypixel = this.OverlayOp.Apply(srcGrey, *dstPtr); 
                        *dstPtr = mypixel; 

                        ++srcPtr; 
                        ++dstPtr; 
                    } 
                } 
            } 
        } 

        public override EffectConfigDialog CreateConfigDialog() 
        { 
            ThreeAmountsConfigDialog tacd = new ThreeAmountsConfigDialog(); 
            tacd.Text = StaticName; 

            tacd.Amount1Label = "Soften"; 
            tacd.Amount1Minimum = 0; 
            tacd.Amount1Maximum = 10; 
            tacd.Amount1Default = 5; 

            tacd.Amount2Label = "Lighting"; 
            tacd.Amount2Minimum = -20; 
            tacd.Amount2Maximum = 20; 
            tacd.Amount2Default = 0; 

            tacd.Amount3Label = "Warmth"; 
            tacd.Amount3Minimum = 0; 
            tacd.Amount3Maximum = 20; 
            tacd.Amount3Default = 10; 
            
            return tacd; 
        } 

        public EffectPlugin() 
            : base(StaticName, StaticIcon, true) 
        { 

        } 

    } 
}

 

Sunday, May 27, 2007