public IEnumerable <int> Process(IEnumerable <Color> colors)
        {
            if (_quantizer == null)
            {
                throw new ArgumentNullException(nameof(_quantizer));
            }
            if (ErrorFactorMatrix == null)
            {
                throw new ArgumentNullException(nameof(ErrorFactorMatrix));
            }

            var colorList = colors.ToArray();

            _quantizer.CreatePalette(colorList);

            var indices         = new int[colorList.Length];
            var errorComponents = new ColorComponentError[colorList.Length];

            for (int i = 0; i < errorComponents.Length; i++)
            {
                errorComponents[i] = new ColorComponentError(0, 0, 0);
            }
            var errors =
                new ErrorDiffusionList <Color, ColorComponentError>(colorList, errorComponents)
                .ToArray();

            ParallelProcessing.ProcessList(
                errors, indices, _width,
                MatrixSideWidth + 1, _quantizer.TaskCount, ProcessingAction);

            return(indices);
        }
Ejemplo n.º 2
0
        public IEnumerable <int> Process(IEnumerable <Color> colors)
        {
            var colorList = colors.ToArray();

            _quantizer.CreatePalette(colorList);

            var processingAction = new Action <LineTask <Color[], int[]> >(taskModel =>
            {
                var matrixWidth  = Matrix.GetLength(0);
                var matrixHeight = Matrix.GetLength(1);

                for (int i = taskModel.Start; i < taskModel.Start + taskModel.Length; i++)
                {
                    int x = i % _width % matrixWidth;
                    int y = i / _width % matrixHeight;

                    int threshold = Convert.ToInt32(Matrix[x, y]);
                    var color     = taskModel.Input[i];

                    int red   = GetClampedValue(color.R + threshold, 0, 255);
                    int green = GetClampedValue(color.G + threshold, 0, 255);
                    int blue  = GetClampedValue(color.B + threshold, 0, 255);

                    taskModel.Output[i] = _quantizer.GetPaletteIndex(Color.FromArgb(color.A, red, green, blue));
                }
            });

            var indices = new int[colorList.Length];

            ParallelProcessing.ProcessList(colorList, indices, processingAction, _quantizer.TaskCount);

            return(indices);
        }