public static void CalculatePseudoColor(Index2 index, ArrayView2D <uint> output, ArrayView3D <short> input, int redBand, int greenBand, int blueBand, short max) { uint r = (uint)(XMath.Clamp(input[index.X, index.Y, redBand], (short)0, max) / (float)max * 255); uint g = (uint)(XMath.Clamp(input[index.X, index.Y, greenBand], (short)0, max) / (float)max * 255); uint b = (uint)(XMath.Clamp(input[index.X, index.Y, blueBand], (short)0, max) / (float)max * 255); output[index] = r + (g << 8) + (b << 16) + ((uint)255 << 24); }
public static void ConvertToByte(Index3 index, ArrayView3D <byte> output, ArrayView3D <short> input, short maxValue) { var val = input[index]; val = XMath.Clamp(val, (short)0, maxValue); byte result = (byte)((val / (float)maxValue) * 255); output[index] = result; }
public static Vec3 aces_approx(Vec3 v) { v *= 0.6f; float a = 2.51f; float b = 0.03f; float c = 2.43f; float d = 0.59f; float e = 0.14f; Vec3 working = (v * (a * v + b)) / (v * (c * v + d) + e); return(new Vec3(XMath.Clamp(working.x, 0, 1), XMath.Clamp(working.y, 0, 1), XMath.Clamp(working.z, 0, 1))); }
public void Aces_approx_IP() { float a = 2.51f; float b = 0.03f; float c = 2.43f; float d = 0.59f; float e = 0.14f; this.x = XMath.Clamp(((this.x * 0.6f * (a * this.x * 0.6f + b)) / (this.x * 0.6f * (c * this.x * 0.6f + d) + e)), 0f, 1f); this.y = XMath.Clamp(((this.y * 0.6f * (a * this.y * 0.6f + b)) / (this.y * 0.6f * (c * this.y * 0.6f + d) + e)), 0f, 1f); this.z = XMath.Clamp(((this.z * 0.6f * (a * this.z * 0.6f + b)) / (this.z * 0.6f * (c * this.z * 0.6f + d) + e)), 0f, 1f); return; }
public static void CalculateSignatureLengthDerivative(Index2 index, ArrayView3D <float> output, ArrayView3D <short> input, short maxValue) { var x = index.X; var y = index.Y; if (x == 0) { x = 1; } if (x == input.Width - 1) { x = input.Width - 2; } if (y == 0) { y = 1; } if (y == input.Height - 1) { y = input.Height - 2; } var depth = input.Depth; float xDiffComponentSum = 0; float yDiffComponentSum = 0; for (int i = 0; i < depth; i++) { var val1 = XMath.Clamp(input[x - 1, y, i], (short)0, maxValue); var val2 = XMath.Clamp(input[x + 1, y, i], (short)0, maxValue); var xDiff = val2 - val1; xDiffComponentSum += xDiff * xDiff; val1 = XMath.Clamp(input[x, y - 1, i], (short)0, maxValue); val2 = XMath.Clamp(input[x, y + 1, i], (short)0, maxValue); var yDiff = val2 - val1; yDiffComponentSum += yDiff * yDiff; } var xDiffLength = XMath.Sqrt(xDiffComponentSum); var yDiffLength = XMath.Sqrt(yDiffComponentSum); output[index.X, index.Y, 0] = xDiffLength; output[index.X, index.Y, 1] = yDiffLength; output[index.X, index.Y, 2] = XMath.Max(xDiffLength, yDiffLength); }
public static void PixelKernel(Index2 index, ArrayView2D <Color3> pixelMap, ArrayView <byte> imageBytes, ArrayView2D <Neuron> nrn) { int tx = index.X; int ty = index.Y; int gridSizeX = (int)pixelMap.Extent.X; int gridSizeY = (int)pixelMap.Extent.Y; // ArrayView2D<float> mem = SharedMemory.Allocate2D<float>(new Index2(64, 64)); // test if (tx > 0 && tx < gridSizeX - 1) { if (ty > 0 && ty < gridSizeY - 1) { int sum0 = 0; int sum1 = 0; int sum2 = 0; int sum3 = 0; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int otherState = nrn[tx + dx, ty + dy].value; if (otherState == 0) { sum0++; } if (otherState == 1) { sum1++; } if (otherState == 2) { sum2++; } if (otherState == 3) { sum3++; } } } if (sum0 > 6) { nrn[tx, ty].newValue = 1; } if (sum1 > 4) { nrn[tx, ty].newValue = 2; } if (sum2 > 3) { nrn[tx, ty].newValue = 3; } if (sum3 > 2) { nrn[tx, ty].newValue = 0; } } } Group.Barrier(); nrn[tx, ty].value = nrn[tx, ty].newValue; if (tx == gridSizeX / 2 && ty == gridSizeY / 2) { // nrn[tx, ty].value = 0; } if (nrn[tx, ty].value == 0) { pixelMap[tx, ty].red = 1.0f; pixelMap[tx, ty].blue = 0.0f; pixelMap[tx, ty].green = 0.0f; } else if (nrn[tx, ty].value == 1) { pixelMap[tx, ty].red = 0.0f; pixelMap[tx, ty].blue = 1.0f; pixelMap[tx, ty].green = 0.0f; } else if (nrn[tx, ty].value == 2) { pixelMap[tx, ty].red = 0.0f; pixelMap[tx, ty].blue = 0.0f; pixelMap[tx, ty].green = 1.0f; } else if (nrn[tx, ty].value == 3) { pixelMap[tx, ty].red = 1.0f; pixelMap[tx, ty].blue = 1.0f; pixelMap[tx, ty].green = 0.0f; } pixelMap[tx, ty].red = XMath.Clamp(pixelMap[tx, ty].red, 0f, 1f); pixelMap[tx, ty].blue = XMath.Clamp(pixelMap[tx, ty].blue, 0f, 1f); pixelMap[tx, ty].green = XMath.Clamp(pixelMap[tx, ty].green, 0f, 1f); int i = (tx + (ty * gridSizeX)) * 4; imageBytes[i + 0] = (byte)(pixelMap[tx, ty].blue * 255f); // blue imageBytes[i + 1] = (byte)(pixelMap[tx, ty].green * 255f); // green imageBytes[i + 2] = (byte)(pixelMap[tx, ty].red * 255f); // red imageBytes[i + 3] = 255; // alpha channel }