private static ConvolutionColor GetColor(DataSource dataSource, int[] pixels, IntPoint coordinate, Point point)
        {
            int x = coordinate.X;
            int y = coordinate.Y;

            var x0 = dataSource.XCoordinates[x];
            var x1 = dataSource.XCoordinates[x + 1];

            var y0 = dataSource.YCoordinates[y];
            var y1 = dataSource.YCoordinates[y + 1];

            double xRatio = GetRatio(x0, x1, point.X);
            double yRatio = GetRatio(y0, y1, point.Y);

            int width = dataSource.Width;

            var v00 = pixels[x + y * width];
            var v01 = pixels[x + 1 + y * width];
            var v10 = pixels[x + (y + 1) * width];
            var v11 = pixels[x + 1 + (y + 1) * width];

            //var result = (int)(((1 - xRatio) * v00 + xRatio * v10 +
            //                (1 - xRatio) * v01 + xRatio * v11 +
            //                (1 - yRatio) * v00 + yRatio * v01 +
            //                (1 - yRatio) * v10 + yRatio * v11) * 0.25);
            var result = v00;

            return(ConvolutionColor.FromArgb(result));
        }
        private int[] CreateConvolutionArray(int width, int height, int[] pixels)
        {
            var dataSource = DataSource;

            const int L = 20;

            int[] effectivePixels = new int[width * height];
            Parallel.For(0, width * height, i =>
            {
                int ix = i % width;
                int iy = i / width;

                double sum = 1;
                double positiveDistance = 0;
                Point position          = dataSource.Grid[ix, iy];
                ConvolutionColor color  = ConvolutionColor.FromArgb(pixels[ix + width * iy]);
                int counter             = 0;
                do
                {
                    counter++;

                    var vector = dataSource.Data[ix, iy];
                    vector.Normalize();
                    position         += vector;
                    positiveDistance += vector.Length;

                    IntPoint coordinate;
                    bool found = GetCoordinate(dataSource, position, out coordinate);
                    if (found)
                    {
                        ix = coordinate.X;
                        iy = coordinate.Y;
                        var currentColor = ConvolutionColor.FromArgb(pixels[ix + iy * width]);                        // *1 / Math.Sqrt(counter);
                        color           += currentColor;

                        sum += 1;
                    }
                    else
                    {
                        break;
                    }
                }while (positiveDistance < L || counter < 50);

                var negativeDistance = 0.0;
                counter = 0;
                do
                {
                    counter++;

                    var vector = dataSource.Data[ix, iy];
                    vector.Normalize();
                    position         -= vector;
                    negativeDistance += vector.Length;

                    IntPoint coordinate;
                    bool found = GetCoordinate(dataSource, position, out coordinate);
                    if (found)
                    {
                        ix = coordinate.X;
                        iy = coordinate.Y;
                        var currentColor = ConvolutionColor.FromArgb(pixels[ix + iy * width]);                        // * 1 / Math.Sqrt(counter);
                        color           += currentColor;

                        sum += 1;
                    }
                    else
                    {
                        break;
                    }
                }while (negativeDistance < L || counter < 50);

                color /= sum;
                effectivePixels[i] = color.ToArgb();
            });
            return(effectivePixels);
        }
示例#3
0
        private int[] CreateConvolutionArray(int width, int height, int[] pixels)
        {
            var dataSource = this.GetValueSync <DataSource>(DataSourceProperty);;

            const int Length        = 20;
            const int maxIterations = 20;

            int pixelsCount = width * height;

            int[] effectivePixels = new int[pixelsCount];

            pixels.CopyTo(effectivePixels, 0);

            System.Threading.Tasks.Parallel.For(0, pixelsCount, i =>
            {
                if (i % 1000 == 0)
                {
                    UpdateBitmap(effectivePixels);
                }

                int ix = i % width;
                int iy = i / width;

                double sumDistance      = 1;
                double positiveDistance = 0;
                Point position          = dataSource.Grid[ix, iy];
                ConvolutionColor color  = ConvolutionColor.FromArgb(pixels[ix + width * iy]);

                int iterationsCounter = 0;
                do
                {
                    int i_x = ix;
                    int i_y = iy;
                    iterationsCounter++;

                    Vector vector =
                        //GetVector(dataSource, new IntPoint(i_x, i_y), position);
                        dataSource.Data[i_x, i_y];

                    if (vector.Length > 0)
                    {
                        vector.Normalize();
                    }

                    position         += vector;
                    positiveDistance += vector.Length;

                    IntPoint coordinate;
                    bool found = GetCoordinate(dataSource, position, out coordinate);
                    if (found)
                    {
                        i_x = coordinate.X;
                        i_y = coordinate.Y;
                        var currentColor = ConvolutionColor.FromArgb(pixels[i_x + i_y * width]);
                        color           += currentColor;

                        sumDistance += 1;
                    }
                    else
                    {
                        break;
                    }
                }while (positiveDistance < Length && iterationsCounter < maxIterations);

                var negativeDistance = 0.0;
                iterationsCounter    = 0;
                do
                {
                    int i_x = ix;
                    int i_y = iy;
                    iterationsCounter++;

                    Vector vector =
                        //GetVector(dataSource, new IntPoint(i_x, i_y), position);
                        dataSource.Data[i_x, i_y];
                    if (vector.Length > 0)
                    {
                        vector.Normalize();
                    }

                    position         -= vector;
                    negativeDistance += vector.Length;

                    IntPoint coordinate;
                    bool found = GetCoordinate(dataSource, position, out coordinate);
                    if (found)
                    {
                        i_x = coordinate.X;
                        i_y = coordinate.Y;
                        var currentColor = ConvolutionColor.FromArgb(pixels[i_x + i_y * width]);
                        color           += currentColor;

                        sumDistance += 1;
                    }
                    else
                    {
                        break;
                    }
                }while (negativeDistance < Length && iterationsCounter < maxIterations);

                color /= sumDistance;
                effectivePixels[i] = color.ToArgb();
            });

            return(effectivePixels);
        }
		private int[] CreateConvolutionArray(int width, int height, int[] pixels, ParallelOptions parallelOptions, out bool cancelled)
		{
			cancelled = false;

			var dataSource = this.GetValueSync<DataSource>(DataSourceProperty);
			if (dataSource == null)
			{
				return new int[0];
			}

			const int Length = 20;
			const int maxIterations = 20;

			int pixelsCount = width * height;
			int[] effectivePixels = new int[pixelsCount];

			if (parallelOptions.CancellationToken.IsCancellationRequested)
			{
				cancelled = true;
				return null;
			}

			pixels.CopyTo(effectivePixels, 0);

			var bounds = dataSource.GetGridBounds();

			try
			{
#if p
				Parallel.For(0, pixelsCount, parallelOptions, (i, loopState) =>
				{
					if (i % 1000 == 0 && parallelOptions.CancellationToken.IsCancellationRequested)
					{
						loopState.Break();
					}
#else
				for (int i = 0; i < pixelsCount; i++)
				{
#endif

					if (i % 2000 == 0)
						UpdateBitmap(effectivePixels);

					int ix = i % width;
					int iy = i / width;

					double sumDistance = 1;
					double positiveDistance = 0;
					Point position = dataSource.Grid[ix, iy];
					int intColor = Colors.Transparent.ToArgb();
					Vector v = dataSource.Data[ix, iy];
					if (v != missingvalue)
					{
						ConvolutionColor color = ConvolutionColor.FromArgb(pixels[ix + width * iy]);
						int iterationsCounter = 0;
						do
						{
							int i_x = ix;
							int i_y = iy;
							iterationsCounter++;

							Vector vector =
								//GetVector(dataSource, new IntPoint(i_x, i_y), position); 
								dataSource.Data[i_x, i_y];

							if (vector.Length > 0)
								vector.Normalize();

							vector = vector.ChangeLength(bounds.Width, bounds.Height, width, height);

							position += vector;
							positiveDistance += vector.Length;

							IntPoint coordinate;
							bool found = GetCoordinate(dataSource, position, out coordinate);
							if (found)
							{
								i_x = coordinate.X;
								i_y = coordinate.Y;
								var currentColor = ConvolutionColor.FromArgb(pixels[i_x + i_y * width]);
								color += currentColor;

								sumDistance += 1;
							}
							else
								break;
						}
						while (positiveDistance < Length && iterationsCounter < maxIterations);

						var negativeDistance = 0.0;
						iterationsCounter = 0;
						do
						{
							int i_x = ix;
							int i_y = iy;
							iterationsCounter++;

							Vector vector =
								//GetVector(dataSource, new IntPoint(i_x, i_y), position);
								 dataSource.Data[i_x, i_y];
							if (vector.Length > 0)
								vector.Normalize();

							position -= vector;
							negativeDistance += vector.Length;

							IntPoint coordinate;
							bool found = GetCoordinate(dataSource, position, out coordinate);
							if (found)
							{
								i_x = coordinate.X;
								i_y = coordinate.Y;
								var currentColor = ConvolutionColor.FromArgb(pixels[i_x + i_y * width]);
								color += currentColor;

								sumDistance += 1;
							}
							else
								break;
						}
						while (negativeDistance < Length && iterationsCounter < maxIterations);

						color /= sumDistance;
						intColor = color.ToArgb();
					}

					effectivePixels[(height - 1 - iy) * width + ix] = intColor;
				}
#if p
				);
#endif
			}
			catch (OperationCanceledException)
			{
				cancelled = true;
			}

			return effectivePixels;
		}