public void FasAbsResultMatchesMath() { const int X = -33; int expected = Math.Abs(X); Assert.Equal(expected, ImageMaths.FastAbs(X)); }
/// <summary> /// Get the a pixel row at a given position with a length of the tile width. Mirrors pixels which exceeds the edges. /// </summary> /// <param name="source">The source image.</param> /// <param name="rowPixels">Pre-allocated pixel row span of the size of a the tile width.</param> /// <param name="x">The x position.</param> /// <param name="y">The y position.</param> /// <param name="tileWidth">The width in pixels of a tile.</param> /// <param name="configuration">The configuration.</param> private void CopyPixelRow( ImageFrame <TPixel> source, Span <Vector4> rowPixels, int x, int y, int tileWidth, Configuration configuration) { if (y < 0) { y = ImageMaths.FastAbs(y); } else if (y >= source.Height) { int diff = y - source.Height; y = source.Height - diff - 1; } // Special cases for the left and the right border where GetPixelRowSpan can not be used. if (x < 0) { rowPixels.Clear(); int idx = 0; for (int dx = x; dx < x + tileWidth; dx++) { rowPixels[idx] = source[ImageMaths.FastAbs(dx), y].ToVector4(); idx++; } return; } else if (x + tileWidth > source.Width) { rowPixels.Clear(); int idx = 0; for (int dx = x; dx < x + tileWidth; dx++) { if (dx >= source.Width) { int diff = dx - source.Width; rowPixels[idx] = source[dx - diff - 1, y].ToVector4(); } else { rowPixels[idx] = source[dx, y].ToVector4(); } idx++; } return; } this.CopyPixelRowFast(source, rowPixels, x, y, tileWidth, configuration); }
/// <summary> /// Computes a simple linear function of the three neighboring pixels (left, above, upper left), then chooses /// as predictor the neighboring pixel closest to the computed value. /// </summary> /// <param name="left">The left neighbor pixel.</param> /// <param name="above">The above neighbor pixel.</param> /// <param name="upperLeft">The upper left neighbor pixel.</param> /// <returns> /// The <see cref="byte"/>. /// </returns> private static byte PaethPredicator(byte left, byte above, byte upperLeft) { int p = left + above - upperLeft; int pa = ImageMaths.FastAbs(p - left); int pb = ImageMaths.FastAbs(p - above); int pc = ImageMaths.FastAbs(p - upperLeft); if (pa <= pb && pa <= pc) { return(left); } if (pb <= pc) { return(above); } return(upperLeft); }