public OrderedDither(uint length) { Guard.MustBeGreaterThan(length, 0, nameof(length)); DenseMatrix <uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length); // Create a new matrix to run against, that pre-thresholds the values. // We don't want to adjust the original matrix generation code as that // creates known, easy to test values. // https://en.wikipedia.org/wiki/Ordered_dithering#Algorithm var thresholdMatrix = new DenseMatrix <float>((int)length); float m2 = length * length; for (int y = 0; y < length; y++) { for (int x = 0; x < length; x++) { thresholdMatrix[y, x] = ((ditherMatrix[y, x] + 1) / m2) - .5F; } } this.modulusX = ditherMatrix.Columns; this.modulusY = ditherMatrix.Rows; this.thresholdMatrix = thresholdMatrix; }
/// <summary> /// Initializes a new instance of the <see cref="OrderedDither"/> class. /// </summary> /// <param name="length">The length of the matrix sides</param> public OrderedDither(uint length) { DenseMatrix <uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length); this.modulusX = ditherMatrix.Columns; this.modulusY = ditherMatrix.Rows; // Adjust the matrix range for 0-255 // TODO: It looks like it's actually possible to dither an image using it's own colors. We should investigate for V2 // https://stackoverflow.com/questions/12422407/monochrome-dithering-in-javascript-bayer-atkinson-floyd-steinberg int multiplier = 256 / ditherMatrix.Count; for (int y = 0; y < ditherMatrix.Rows; y++) { for (int x = 0; x < ditherMatrix.Columns; x++) { ditherMatrix[y, x] = (uint)((ditherMatrix[y, x] + 1) * multiplier) - 1; } } this.thresholdMatrix = ditherMatrix; }