private GammaRamp CreateGammaRamp(ColorBalance colorBalance) { // Create native ramp object var ramp = new GammaRamp { Red = new ushort[256], Green = new ushort[256], Blue = new ushort[256] }; // Create linear ramps for each color for (var i = 0; i < 256; i++) { ramp.Red[i] = (ushort)(i * 255 * colorBalance.Red); ramp.Green[i] = (ushort)(i * 255 * colorBalance.Green); ramp.Blue[i] = (ushort)(i * 255 * colorBalance.Blue); } // Some drivers will ignore request to change gamma if the ramp is the same as last time // so we randomize it a bit. Even though our ramp may not have changed, other applications // could have affected the gamma and we need to "force-refresh" it to ensure it's valid. _gammaChannelOffset = ++_gammaChannelOffset % 5; ramp.Red[255] = (ushort)(ramp.Red[255] + _gammaChannelOffset); ramp.Green[255] = (ushort)(ramp.Green[255] + _gammaChannelOffset); ramp.Blue[255] = (ushort)(ramp.Blue[255] + _gammaChannelOffset); return(ramp); }
public void Different_values_yield_different_images() { foreach (string file in this.images) { // arrange using (ImageFactory factory = new ImageFactory()) { factory.Load(file); ColorBalance processor = new ColorBalance(); processor.DynamicParameter = new ColorBalanceParameters(); ColorBalance processor2 = new ColorBalance(); processor2.DynamicParameter = new ColorBalanceParameters() { Blue = 155, Green = 155, Red = 155 }; // act Bitmap result = new Bitmap(processor.ProcessImage(factory)); Bitmap result2 = new Bitmap(processor2.ProcessImage(factory)); result2.Save(string.Format("{0}/{1}_colorbalance2.jpg", Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)), ImageFormat.Jpeg); // assert result.Equals(result2).Should().BeFalse("because different parameters should yield different images"); } } }
public IActionResult UpdateColorImage(int blueRed, int purpleGreen, int yellowDarkBlue) //изменение цветового баланса { UInt32 p; image = new Bitmap(ActionPictureController.pathImage); //получение матрицы с пикселями pixel = new UInt32[image.Height, image.Width]; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { pixel[y, x] = (UInt32)(image.GetPixel(x, y).ToArgb()); } } //цветовой баланс R for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { p = ColorBalance.ColorBalance_R(pixel[i, j], blueRed, 10); FromOnePixelToBitmap(i, j, p); pixel[i, j] = (UInt32)(image.GetPixel(j, i).ToArgb()); } } //цветовой баланс G for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { p = ColorBalance.ColorBalance_G(pixel[i, j], purpleGreen, 10); FromOnePixelToBitmap(i, j, p); pixel[i, j] = (UInt32)(image.GetPixel(j, i).ToArgb()); } } //цветовой баланс B for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { p = ColorBalance.ColorBalance_B(pixel[i, j], yellowDarkBlue, 10); FromOnePixelToBitmap(i, j, p); pixel[i, j] = (UInt32)(image.GetPixel(j, i).ToArgb()); } } bitmapBytes = BitmapToBytes(image); image.Dispose(); string imreBase64Data = Convert.ToBase64String(bitmapBytes); string imgDataURL = string.Format("data:image/jpeg;base64,{0}", imreBase64Data); ViewBag.ImageData = imgDataURL; return(View("~/Views/Home/UpdateImage.cshtml")); }
public void SetGamma(ColorBalance colorBalance) { lock (_lock) { // Create ramp var ramp = CreateGammaRamp(colorBalance); // Set gamma NativeMethods.SetDeviceGammaRamp(_deviceContext, ref ramp); } }
public override void Image_is_processed() { foreach (string file in this.images) { // arrange using (ImageFactory factory = new ImageFactory()) { factory.Load(file); ColorBalance processor = new ColorBalance(); processor.DynamicParameter = new ColorBalanceParameters(); // act Action act = () => { Image img = processor.ProcessImage(factory); img.Save(string.Format("{0}/{1}_colorbalance.jpg", Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)), ImageFormat.Jpeg); }; // assert act.ShouldNotThrow("because the image should have been processed without error"); } } }