/// <summary> /// Process frame. /// </summary> /// <param name="bmData">Bitmap data</param> /// <param name="bmSrc">Bitmap data</param> private unsafe double ProcessFrameWithFilter(BitmapData bmData, BitmapData bmSrc) { byte *dst = (byte *)bmData.Scan0.ToPointer(); byte *src = (byte *)bmSrc.Scan0.ToPointer(); int width = bmData.Width, height = bmData.Height; double variance = 0.0; for (int x = 0; x < width; x++) { int y, k; for (y = 0; y < height; y++, dst += 4, src += 4) { for (k = 0; k < 3; k++) { var difference = Math.Abs(dst[k] - src[k]); dst[k] = Maths.Byte(difference); } var summary = RGB.Average(dst[2], dst[1], dst[0]); if (summary > Threshold) { variance++; } } } ; return(variance / (width * height)); }
private Optimizer(RGB[] pixels, int limit, int error) { _pixels = pixels; _limit = limit; _baseColor = RGB.Average(pixels).Unscale(limit); _bestSln = new Solution { Error = error }; }
Optimizer(RGB[] pixels, int limit, int error) { this.pixels = pixels; this.limit = limit; baseColor = RGB.Average(pixels).Unscale(limit); best_soln = new Solution { error = error }; }
/// <summary> /// Apply filter. /// </summary> /// <param name="bmData">Bitmap data</param> /// <param name="bmSrc">Bitmap data</param> public unsafe void Apply(BitmapData bmData, BitmapData bmSrc) { byte *p = (byte *)bmData.Scan0.ToPointer(); byte *pSrc = (byte *)bmSrc.Scan0.ToPointer(); int y, x, width = bmData.Width, height = bmData.Height; for (x = 0; x < width; x++) { for (y = 0; y < height; y++, p += 4, pSrc += 4) { p[3] = (byte)RGB.Average(pSrc[2], pSrc[1], pSrc[0]); } } return; }
/// <summary> /// Apply filter. /// </summary> /// <param name="bmData">Bitmap data</param> /// <param name="bmMax">Bitmap data</param> /// <param name="bmMin">Bitmap data</param> private unsafe void ApplyGrayscale(BitmapData bmData, BitmapData bmMax, BitmapData bmMin) { byte *p = (byte *)bmData.Scan0.ToPointer(); byte *pMax = (byte *)bmMax.Scan0.ToPointer(); byte *pMin = (byte *)bmMin.Scan0.ToPointer(); int width = bmData.Width, height = bmData.Height, stride = bmData.Stride; float required = 1.0f - this.contrast; Parallel.For(0, height, j => { int i, k, k1, k2, v, jstride = j * stride; float mag, max, min; float num1, num2, num3; for (i = 0; i < width; i++) { k = jstride + i * 4; k1 = k + 1; k2 = k + 2; // Local function: v = k; mag = RGB.Average(p[v], p[v + 1], p[v + 2]) / 255.0f; max = RGB.Average(pMax[v], pMax[v + 1], pMax[v + 2]) / 255.0f; min = RGB.Average(pMin[v], pMin[v + 1], pMin[v + 2]) / 255.0f; num1 = max - min; if (num1 < required) { num2 = min + (required - num1) * min / (num1 - 1f); min = Maths.Float(num2); max = Maths.Float(num2 + required); } num1 = max - min; num3 = mag - min; if (num1 > 0) { p[v] = p[v + 1] = p[v + 2] = Maths.Byte(255 * num3 / num1); } // end local function. } } ); }
/// <summary> /// Gets a histogram of the image. /// </summary> /// <param name="bmData">Bitmap data</param> /// <returns>Array</returns> public unsafe static int[] Histogram(this BitmapData bmData) { int[] rgb = new int[256]; byte *p = (byte *)bmData.Scan0.ToPointer(); int y, x, width = bmData.Width, height = bmData.Height; byte brightness; for (y = 0; y < height; y++) { for (x = 0; x < width; x++, p += 4) { brightness = (byte)RGB.Average(p[2], p[1], p[0]); rgb[brightness]++; } } return(rgb); }
/// <summary> /// Apply filter. /// </summary> /// <param name="bmData">Bitmap data</param> public unsafe void Apply(BitmapData bmData) { byte *p = (byte *)bmData.Scan0.ToPointer(); int width = bmData.Width, height = bmData.Height; int stride = bmData.Stride; // filter color float rf = color.R / 255.0f; float gf = color.G / 255.0f; float bf = color.B / 255.0f; // do job Parallel.For(0, height, y => { // bitmap color float lm; float rb; float gb; float bb; int x, ystride, k; ystride = y * stride; for (x = 0; x < width; x++) { k = ystride + x * 4; // luma lm = RGB.Average(p[k + 2], p[k + 1], p[k]) / 255.0f; // blending rb = this.blendf(lm, rf) * 255.0f; gb = this.blendf(lm, gf) * 255.0f; bb = this.blendf(lm, bf) * 255.0f; // recording p[k + 2] = Maths.Byte(rb * s + p[k + 2] * (1.0f - s)); p[k + 1] = Maths.Byte(gb * s + p[k + 1] * (1.0f - s)); p[k] = Maths.Byte(bb * s + p[k] * (1.0f - s)); } }); return; }
/// <summary> /// Converts Bitmap to averaged channel value matrix. /// </summary> /// <param name="bmData">Bitmap data</param> /// <returns>Matrix</returns> public unsafe static float[,] ToGrayscale(this BitmapData bmData) { int width = bmData.Width, height = bmData.Height, stride = bmData.Stride; float[,] rgb = new float[height, width]; byte *p = (byte *)bmData.Scan0.ToPointer(); Parallel.For(0, height, j => { int i, k, jstride = j * stride; for (i = 0; i < width; i++) { k = jstride + i * 4; rgb[j, i] = RGB.Average(p[k + 2], p[k + 1], p[k]) / 255.0f; } }); return(rgb); }
/// <summary> /// Converts Bitmap into unshort matrix. /// </summary> /// <param name="depth">Image</param> /// <returns>Bitmap</returns> public unsafe static ushort[,] FromBitmap(this Bitmap depth) { var bmData = depth.Lock32bpp(); var width = bmData.Width; var height = bmData.Height; var stride = bmData.Stride; var src = (byte *)bmData.Scan0.ToPointer(); var output = new ushort[height, width]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { var k = x * 3 + y * stride; output[y, x] = (ushort)RGB.Average(src[k + 0], src[k + 1], src[k + 2]); } } depth.Unlock(bmData); return(output); }
/// <summary> /// Apply filter. /// </summary> /// <param name="bmData">Bitmap data</param> /// <param name="bmSrc">Bitmap data</param> private unsafe void ApplyGrayscale(BitmapData bmData, BitmapData bmSrc) { byte *p = (byte *)bmData.Scan0.ToPointer(), pSrc = (byte *)bmSrc.Scan0.ToPointer(); int width = bmData.Width, height = bmData.Height, stride = bmData.Stride; float length = values.GetLength(0) - 1; Parallel.For(0, height, j => { int i, k, lumax, lumay, jstride = j * stride; for (i = 0; i < width; i++) { k = jstride + i * 4; lumax = RGB.Average(p[k + 2], p[k + 1], p[k]); lumay = RGB.Average(pSrc[k + 2], pSrc[k + 1], pSrc[k]); p[k + 2] = p[k + 1] = p[k] = Maths.Byte(this.values[lumax, lumay] * length); } }); return; }
/// <summary> /// Apply filter. /// </summary> /// <param name="bmData">Bitmap data</param> private unsafe void ApplyGrayscale(BitmapData bmData) { byte *p = (byte *)bmData.Scan0.ToPointer(); int width = bmData.Width, height = bmData.Height, stride = bmData.Stride; float length = values.Length - 1; Parallel.For(0, height, y => { int x, ystride, k; int luma; ystride = y * stride; for (x = 0; x < width; x++) { k = ystride + x * 4; luma = RGB.Average(p[k + 2], p[k + 1], p[k]); p[k + 2] = p[k + 1] = p[k] = Maths.Byte(values[luma] * length); } } ); return; }
/// <summary> /// Render all UI. /// </summary> private void InitializeUI() { if (PanelEntity.Attributes.ContainsKey("friendly_name")) { TextBlock textBlock = FindName("DeviceText") as TextBlock; textBlock.Text = PanelEntity.Name().ToUpper(); } // For the group panel, average the color of children entities together if (ChildrenEntities != null) { // Set the color adjustment sliders and color wheel by blending the average // color for each. Include entities which are in the On state only. IEnumerable <Entity> onEntities = ChildrenEntities.Where(x => string.Equals(x.State, "on", StringComparison.InvariantCultureIgnoreCase)); if (onEntities.Any()) { // Average the brightness IEnumerable <Entity> brightnessEntities = onEntities.Where(x => x.Attributes.ContainsKey("brightness")); if (brightnessEntities.Any()) { double averageBrightness = onEntities.Select(x => Convert.ToDouble(x.Attributes["brightness"])).Cast <double>().Average(); UpdateBrightnessControl(averageBrightness); } else { Ellipse ellipse = this.FindName("BrightnessCircle") as Ellipse; ellipse.Visibility = Visibility.Collapsed; } // Average the color temperature IEnumerable <Entity> colorTempEntities = onEntities.Where(x => x.Attributes.ContainsKey("color_temp")); if (colorTempEntities.Any()) { double averageColorTemperature = colorTempEntities.Select(x => Convert.ToDouble(x.Attributes["color_temp"])).Cast <double>().Average(); UpdateColorTemperatureControl(Convert.ToInt32(averageColorTemperature)); ShowColorTemperatureCircle(Visibility.Visible); } else { ShowColorTemperatureCircle(Visibility.Collapsed); } // Average the color IEnumerable <Entity> rgbEntities = onEntities.Where(x => x.Attributes.ContainsKey("rgb_color")); if (rgbEntities.Any()) { RGB averageColor = RGB.Average(rgbEntities.Select(x => new RGB( Convert.ToByte(x.Attributes["rgb_color"][0]), Convert.ToByte(x.Attributes["rgb_color"][1]), Convert.ToByte(x.Attributes["rgb_color"][2]))).Cast <RGB>()); SetColorWheelLocation(averageColor); ShowColorWheelCircle(Visibility.Visible); } else { ShowColorWheelCircle(Visibility.Collapsed); } } else { UpdateBrightnessControl(0.0); ShowColorTemperatureCircle(Visibility.Collapsed); ShowColorWheelCircle(Visibility.Collapsed); } } else { if (string.Equals(PanelEntity.State, "on", StringComparison.InvariantCultureIgnoreCase)) { if (PanelEntity.Attributes.ContainsKey("brightness")) { UpdateBrightnessControl(Convert.ToDouble(PanelEntity.Attributes["brightness"])); } else { Ellipse ellipse = this.FindName("BrightnessCircle") as Ellipse; ellipse.Visibility = Visibility.Collapsed; } if (PanelEntity.Attributes.ContainsKey("color_temp")) { UpdateColorTemperatureControl(Convert.ToInt32(PanelEntity.Attributes["color_temp"])); ShowColorTemperatureCircle(Visibility.Visible); } else { ShowColorTemperatureCircle(Visibility.Collapsed); } if (PanelEntity.Attributes.ContainsKey("rgb_color")) { Newtonsoft.Json.Linq.JArray rgbColors = PanelEntity.Attributes["rgb_color"]; RGB rgb = new RGB() { R = Convert.ToByte(rgbColors[0]), G = Convert.ToByte(rgbColors[1]), B = Convert.ToByte(rgbColors[2]), }; SetColorWheelLocation(rgb); ShowColorWheelCircle(Visibility.Visible); } else { ShowColorWheelCircle(Visibility.Collapsed); } } else { UpdateBrightnessControl(0.0); ShowColorTemperatureCircle(Visibility.Collapsed); ShowColorWheelCircle(Visibility.Collapsed); } } // ColorTemperature should be a constant yellowish-hue line for brightness-only lights if (!PanelEntity.HasSupportedFeatures((uint)LightPlatformSupportedFeatures.ColorTemperature, ChildrenEntities)) { Rectangle colorTemperature = this.FindName("ColorTemperature") as Rectangle; colorTemperature.Fill = new SolidColorBrush(Colors.LightYellow); } // Update the power button last so as to render with any color updates ShowHightlightColor(ButtonState.NotPressed); }