Esempio n. 1
0
        public void CorrectGamma(Single gamma, IColorQuantizer quantizer, Int32 parallelTaskCount = 4)
        {
            // checks parameters
            Guard.CheckNull(quantizer, "quantizer");

            // determines which method of color retrieval to use
            IList <Point> path = quantizer.GetPointPath(Width, Height);

            // calculates gamma ramp
            Int32[] gammaRamp = new Int32[256];

            for (Int32 index = 0; index < 256; ++index)
            {
                gammaRamp[index] = Clamp((Int32)((255.0f * Math.Pow(index / 255.0f, 1.0f / gamma)) + 0.5f));
            }

            // use different scanning method depending whether the image format is indexed
            ProcessPixelFunction correctGamma = pixel =>
            {
                Color oldColor = GetColorFromPixel(pixel);
                Int32 red      = gammaRamp[oldColor.R];
                Int32 green    = gammaRamp[oldColor.G];
                Int32 blue     = gammaRamp[oldColor.B];
                Color newColor = Color.FromArgb(red, green, blue);
                SetColorToPixel(pixel, newColor, quantizer);
                return(true);
            };

            // performs the image scan, using a chosen method
            ProcessPerPixel(path, correctGamma, parallelTaskCount);
        }
Esempio n. 2
0
        private void ProcessPerPixelBase(IList <Point> path, Delegate processingAction, Int32 parallelTaskCount = 4)
        {
            // checks parameters
            Guard.CheckNull(path, "path");
            Guard.CheckNull(processingAction, "processPixelFunction");

            // determines mode
            Boolean isAdvanced = processingAction is ProcessPixelAdvancedFunction;

            // prepares the per pixel task
            Action <LineTask> processPerPixel = lineTask =>
            {
                // initializes variables per task
                Pixel pixel = new Pixel(this);

                for (Int32 pathOffset = lineTask.StartOffset; pathOffset < lineTask.EndOffset; pathOffset++)
                {
                    Point   point = path[pathOffset];
                    Boolean allowWrite;

                    // enumerates the pixel, and returns the control to the outside
                    pixel.Update(point.X, point.Y);

                    // when read is allowed, retrieves current value (in bytes)
                    if (CanRead)
                    {
                        ReadPixel(pixel);
                    }

                    // process the pixel by custom user operation
                    if (isAdvanced)
                    {
                        ProcessPixelAdvancedFunction processAdvancedFunction = (ProcessPixelAdvancedFunction)processingAction;
                        allowWrite = processAdvancedFunction(pixel, this);
                    }
                    else // use simplified version with pixel parameter only
                    {
                        ProcessPixelFunction processFunction = (ProcessPixelFunction)processingAction;
                        allowWrite = processFunction(pixel);
                    }

                    // when write is allowed, copies the value back to the row buffer
                    if (CanWrite && allowWrite)
                    {
                        WritePixel(pixel);
                    }
                }
            };

            // processes image per pixel
            ProcessInParallel(path, processPerPixel, parallelTaskCount);
        }
Esempio n. 3
0
        public void ScanColors(IColorQuantizer quantizer, Int32 parallelTaskCount = 4)
        {
            // checks parameters
            Guard.CheckNull(quantizer, "quantizer");

            // determines which method of color retrieval to use
            IList <Point> path = quantizer.GetPointPath(Width, Height);

            // use different scanning method depending whether the image format is indexed
            ProcessPixelFunction scanColors = pixel =>
            {
                quantizer.AddColor(GetColorFromPixel(pixel), pixel.X, pixel.Y);
                return(false);
            };

            // performs the image scan, using a chosen method
            ProcessPerPixel(path, scanColors, parallelTaskCount);
        }
Esempio n. 4
0
 public void ProcessPerPixel(IList<Point> path, ProcessPixelFunction processPixelFunction, Int32 parallelTaskCount = 4)
 {
     ProcessPerPixelBase(path, processPixelFunction, parallelTaskCount);
 }
Esempio n. 5
0
 public void ProcessPerPixel(IList <Point> path, ProcessPixelFunction processPixelFunction, Int32 parallelTaskCount = 4)
 {
     ProcessPerPixelBase(path, processPixelFunction, parallelTaskCount);
 }
Esempio n. 6
0
        private void ProcessPerPixelBase(IList <Point> path = null, Int32 parallelTaskCount = 4, params Delegate[] passes)
        {
            // checks parameters
            Guard.CheckNull(passes, "passes");

            // determines mode
            Boolean isAdvanced = passes.Length > 0 && passes[0] is ProcessPixelAdvancedFunction;

            // creates default path, if none is available
            if (path == null)
            {
                path = StandardPathProvider.CreatePath(Width, Height);
            }

            // prepares the per pixel task
            Action <LineTask> processPerPixel = lineTask =>
            {
                // initializes variables per task
                Pixel    pixel = CreatePixel();
                Delegate pass  = passes[lineTask.PassIndex];

                for (Int32 pathOffset = lineTask.StartOffset; pathOffset < lineTask.EndOffset; pathOffset++)
                {
                    Point   point = path[pathOffset];
                    Boolean allowWrite;

                    // enumerates the pixel, and returns the control to the outside
                    pixel.Update(point.X, point.Y);

                    // when read is allowed, retrieves current value (in bytes)
                    if (CanRead)
                    {
                        ReadPixel(pixel);
                    }

                    // process the pixel by custom user operation
                    if (isAdvanced)
                    {
                        ProcessPixelAdvancedFunction processAdvancedFunction = (ProcessPixelAdvancedFunction)pass;
                        allowWrite = processAdvancedFunction(lineTask.PassIndex, pixel, this);
                    }
                    else // use simplified version with pixel parameter only
                    {
                        ProcessPixelFunction processFunction = (ProcessPixelFunction)pass;
                        allowWrite = processFunction(lineTask.PassIndex, pixel);
                    }

                    // when write is allowed, copies the value back to the row buffer
                    if (CanWrite && allowWrite)
                    {
                        WritePixel(pixel);
                    }
                }
            };

            // processes image per pixel
            for (Int32 passIndex = 0; passIndex < passes.Length; passIndex++)
            {
                ProcessInParallel(passIndex, processPerPixel, path, parallelTaskCount);
            }
        }