Click here to Skip to main content
15,893,668 members
Articles / Desktop Programming / WPF

Image Magic - Image Levels using Custom Controls

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
26 May 2010CPOL11 min read 42.3K   2.2K   20  
Drop in WPF custom leveling controls and logic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LevelsLogic
{
    public class GammaRemap
    {
        private int[] remap;
        private double pshopGamma;      //actually 1/gamma (pShop gamma), valid values 0.10 - 9.99
        private double gamma;           //regular gamma, >= 0.0
        private int    grayGamma;       //where gray (127) is mapped to. 0-255  This is what sets the Gamma
 
        public int[] Remap { get{return remap;} }
        public double PshopGamma { get{return pshopGamma;} }
        public double Gamma { get { return gamma;} }

        public double GrayGamma
        {
            set
            {
                ComputeRemap(value);
            }
        }

        public GammaRemap()
        {
            remap = new int[256];
            for (int i = 0; i <= 255; i++)
                remap[i] = i;
            pshopGamma = 1.0;
            gamma = 1.0;
            grayGamma = 127;
        } //ctor


        //given the value of the gray input slider, compute the remap array
        private void ComputeRemap(double gray_d)
        {
            //most common gray value, if its already been done, do nothing
            if ((int)Math.Floor(gray_d) == 127 && grayGamma==127)
                return;
            grayGamma = (int)Math.Floor(gray_d);
            if (grayGamma <= 0) grayGamma = 1;
            gamma = (Math.Log10(grayGamma/255.0D)) / (Math.Log10(127/255.0D));
            pshopGamma = 1 / gamma;

            for (int i = 0 + 1; i < 256; i++)
            {
                //compute brightness, input adjusted to normalize over black to white
                double inputBrightness = i /255.0D;
                double outputBrightness = Math.Pow(inputBrightness, gamma);
                remap[i] = (int)Math.Floor(outputBrightness * 255);
            }
        } //ComputeRemap()


        public void GammaAdjustRemapArray(int[] srcRemap)
        {
            for (int i = 0; i < 256; i++)
                srcRemap[i] = remap[srcRemap[i]];
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions