public ColorTransformation(ColorChannelTransformation redChannelTransformation, ColorChannelTransformation greenChannelTransformation, ColorChannelTransformation blueChannelTransformation) { RedChannelTransformation = redChannelTransformation; GreenChannelTransformation = greenChannelTransformation; BlueChannelTransformation = blueChannelTransformation; }
private static void MapToColorChannel(ColorChannelTransformation colorChannelTransformation, float[] values, int threadsCount, float[] channelValuesBuffer, byte[] channelBuffer) { using (ProgressReporter.CreateScope()) { ProgressReporter.CreateScope(0.16); TransformChannelValues(values, colorChannelTransformation.TransformationFunction, channelValuesBuffer); ProgressReporter.Complete(); ProgressReporter.CreateScope(0.06); const double factor = 1e175; const double lowBound = double.MinValue*factor; const double highBound = double.MaxValue/factor; LimitValue(channelValuesBuffer, lowBound, highBound); ProgressReporter.Complete(); ProgressReporter.CreateScope(0.21); double mathExpectation = MathUtilities.MathExpectation(channelValuesBuffer, threadsCount); ProgressReporter.Complete(); ProgressReporter.CreateScope(0.48); double standardDeviation = MathUtilities.StandardDeviation(channelValuesBuffer, threadsCount); ProgressReporter.Complete(); ProgressReporter.CreateScope(0.09); double limit = standardDeviation*(1 + 2*colorChannelTransformation.DispersionCoefficient); if (double.IsNegativeInfinity(limit)) limit = lowBound; if (double.IsPositiveInfinity(limit)) limit = highBound; double rangeStart = mathExpectation - limit; double rangeEnd = mathExpectation + limit; if (rangeStart > rangeEnd) { double tmp = rangeStart; rangeStart = rangeEnd; rangeEnd = tmp; } double range = rangeEnd - rangeStart; double scale = 255/range; for (int i = 0; i < channelBuffer.Length; i++) { if (channelValuesBuffer[i] < rangeStart) channelValuesBuffer[i] = (float)rangeStart; else if (channelValuesBuffer[i] > rangeEnd) channelValuesBuffer[i] = (float)rangeEnd; channelBuffer[i] = (byte)((channelValuesBuffer[i] - rangeStart) * scale); } ProgressReporter.Complete(); } }