The Principle and Implementation of softglow.

Source: Internet
Author: User

Due to the inconsistency between the editing of the csdn blog and the blog garden, some images in the text are misplaced. To avoid affecting the browsing effect, we recommend that you click the open link.

Image softening effects are achieved in many commercial software applications, such as meitu xiuxiu and guangying magic hands. It can generate a new pair of smooth and soft light effects for the original image, giving a hazy beauty, as shown in the following figures:

Currently, with regard to the controllable parameters of the algorithm, meitu xiuxiu provides only one degree (0-100%) of control, and its algorithm Adjustment Effect and amplitude are relatively small, the optical magic hand has two parameters: softening degree and high-gloss softening. The softening degree controls the softening effect and the high-gloss softening adjusts the brightness of the image. Some open-source software such as imagestone, paint.net, and gimp also have softglow algorithms. They all provide three controls: radius, brightness, contrast (sharpness ), among them, imagestone is actually the gimp Algorithm for translation, while the gimp and paint.net algorithms are the same in basic principle and differ in details.

We take the implementation process of paint.net as an example. In the source code of paint.net, gloweffect. CS extracts some of its source code to briefly describe the process of this algorithm.

        public GlowEffect()  : base(StaticName, StaticImage, null, EffectDirectives.None, true)        {            this.blurEffect = new BlurEffect();            this.bcAdjustment = new BrightnessAndContrastAdjustment();            this.screenBlendOp = new UserBlendOps.ScreenBlendOp();        }

And code snippets:

   public override unsafe void Render(            EffectConfigToken parameters,             RenderArgs dstArgs,             RenderArgs srcArgs,             System.Drawing.Rectangle[] rois,             int startIndex,             int length)        {            // First we blur the source, and write the result to the destination surface            // Then we apply Brightness/Contrast with the input as the dst, and the output as the dst            // Third, we apply the Screen blend operation so that dst = dst OVER src            ThreeAmountsConfigToken token = (ThreeAmountsConfigToken)parameters;            AmountEffectConfigToken blurToken = new AmountEffectConfigToken(token.Amount1);            this.blurEffect.Render(blurToken, dstArgs, srcArgs, rois, startIndex, length);            BrightnessAndContrastAdjustmentConfigToken bcToken = new BrightnessAndContrastAdjustmentConfigToken(token.Amount2, token.Amount3);            this.bcAdjustment.Render(bcToken, 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* dstPtr = dstArgs.Surface.GetPointAddressUnchecked(roi.Left, y);                    ColorBgra* srcPtr = srcArgs.Surface.GetPointAddressUnchecked(roi.Left, y);                    screenBlendOp.Apply(dstPtr, srcPtr, dstPtr, roi.Width);                }            }        }

The above code preliminarily concluded that they are based on Gaussian blur and brightness contrast adjustment of the two filters and can be mixed slightly.

Step 1: Back up original images;

Step 2: Perform Gaussian blur on the original image based on the specified radius;

Step 3: Adjust the brightness and contrast of the blurred image;

Step 4: Use the backup data of the original image to mix the original image (the image after the two or three steps above) with the original image in the Photoshop Screen mode.

Here is a simple introduction to the mixed color filtering algorithm: Blend = x + y-x * Y/255. x and y represent the base color and mixed color respectively, and blend represent the result color.

The source code of the algorithm can be referred to the several open-source software I mentioned above. Of course, this may require you to have a certain programming Foundation. After all, the software frameworks are complicated.

The algorithm execution speed depends only on the second step, because the brightness and contrast adjustment is actually a look-up process (the brightness and contrast commands of PS are actually more complex than you think, this has the opportunity to talk again, you can also refer to the afber's blog http://blog.csdn.net/maozefa/article/details/4778934), and the fourth step is actually can use the way to speed up the table (but must use a way ). Gaussian blur, an old growth point, no one on the Web page I searched provided a complete, perfect, execution speed unrelated to the specified radius, and runable VB or VC or Java program source code (generally are provided for reference ). I have my own code, but I am not willing to share it. Really Interested In I recommend you go to find gimp algorithm code, in the entire gimp source code system, at least three Gaussian fuzzy optimization code is given, respectively located in the blur-gauss.c (given 2 types: rle and IIR optimization code) and contrast-retinex.c (code is particularly concise), of course, those code if extracted should also be sorted out and optimized at the code level. Paint.net also provides Gaussian fuzzy functions, but its essence is not Gaussian fuzzy. Instead, it uses a linear Weight Function to replace the constant weight. However, there is no floating point operation in the algorithm, there is also an optimization algorithm that is much faster than the code in paint.net, that is, the execution time is less than the radius, and the optimization algorithm is more than twice faster than any real Gaussian fuzzy optimization algorithm, the effect is not much different. It can be used as a backup algorithm for real-time scenarios.

Similarly, provide a compiled file to a friend who is interested in studying the algorithm to see the effect:

Http://files.cnblogs.com/Imageshop/SoftGlow.rar

 

* ****** Basically I do not provide source code, but I will try to use text to clearly describe the corresponding algorithm or provide reference documentation ******

* ****** The result of your own efforts and practices is true for your own things. People must rely on their own ************

* ***** Author: laviewpbt time: 2013.6.20 contact QQ: 33184777 reprinted please keep this information *******

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.