public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest) { var monochrome = new FormatConvertedBitmap(); monochrome.BeginInit(); monochrome.Source = bmp; monochrome.DestinationFormat = PixelFormat; monochrome.EndInit(); var b = Tint.A > 0 ? ColorShade(monochrome, Tint) : monochrome; _whenProcessed.OnNext(b); return(b); }
public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest) { if (bmp.PixelWidth == Width && bmp.PixelHeight == Height && !FlipHorizontally && !FlipVertically) { return(bmp); } var sw = new Stopwatch(); sw.Start(); var resized = new TransformedBitmap(bmp, new ScaleTransform( Width / bmp.Width * (FlipHorizontally ? -1 : 1), Height / bmp.Height * (FlipVertically ? -1 : 1), (double)bmp.PixelWidth / 2, (double)bmp.PixelHeight / 2)); var rBmp = BitmapFrame.Create(resized); rBmp.Freeze(); _whenProcessed.OnNext(rBmp); return(rBmp); }
public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest) { var sw = new Stopwatch(); sw.Start(); var sliceWidth = bmp.Width / (Width + Width * Spacing - Spacing); var sliceHeight = bmp.Height / (Height + Height * Spacing - Spacing); var paddingWidth = (bmp.Width * Spacing) / (Width + Width * Spacing - Spacing); var paddingHeight = (bmp.Height * Spacing) / (Height + Height * Spacing - Spacing); var destRect = new Int32Rect(); var srcRect = new Int32Rect(); var bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8; var wBmp = new WriteableBitmap(bmp); srcRect.Y = 0; srcRect.Width = (int)Math.Ceiling(sliceWidth); srcRect.Height = bmp.PixelHeight; destRect.Y = 0; destRect.Width = srcRect.Width; destRect.Height = bmp.PixelHeight; var blockSize = bytesPerPixel * srcRect.Width * bmp.PixelHeight; var stride = srcRect.Width * bytesPerPixel; var buffer = new byte[blockSize]; for (var x = 0; x < Width; x++) { srcRect.X = (int)(x * (sliceWidth + paddingWidth)); destRect.X = (int)(x * sliceWidth); bmp.CopyPixels(srcRect, buffer, stride, 0); wBmp.WritePixels(destRect, buffer, stride, 0); } srcRect.X = 0; srcRect.Width = bmp.PixelWidth; srcRect.Height = (int)Math.Ceiling(sliceHeight); destRect.X = 0; destRect.Width = bmp.PixelWidth; destRect.Height = srcRect.Height; blockSize = bytesPerPixel * bmp.PixelWidth * srcRect.Height; stride = bmp.PixelWidth * bytesPerPixel; buffer = new byte[blockSize]; for (var y = 0; y < Height; y++) { srcRect.Y = (int)(y * (sliceHeight + paddingHeight)); destRect.Y = (int)(y * sliceHeight); wBmp.CopyPixels(srcRect, buffer, stride, 0); wBmp.WritePixels(destRect, buffer, stride, 0); } var img = new CroppedBitmap(wBmp, new Int32Rect( CropLeft, CropTop, (int)(sliceWidth * Width) - CropLeft - CropRight, (int)(sliceHeight * Height) - CropTop - CropBottom)); sw.Stop(); //Console.WriteLine("Grid-sized from {0}x{1} to {2}x{3} in {4}ms.", bmp.Width, bmp.Height, img.PixelWidth, img.PixelHeight, sw.ElapsedMilliseconds); img.Freeze(); _whenProcessed.OnNext(img); return(img); }
public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest) { var sw = new Stopwatch(); sw.Start(); var sliceWidth = bmp.Width / Width; var sliceHeight = bmp.Height / Height; var destRect = new Int32Rect(); var srcRect = new Int32Rect(); var lineRect = new Int32Rect(); var bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8; var wBmp = new WriteableBitmap(bmp); srcRect.Height = 1; destRect.X = 0; destRect.Width = bmp.PixelWidth; destRect.Height = srcRect.Height; lineRect.Height = 1; lineRect.X = 0; lineRect.Width = bmp.PixelWidth; var blockSize = bytesPerPixel * bmp.PixelWidth * srcRect.Height; var stride = bmp.PixelWidth * bytesPerPixel; var buffer = new byte[blockSize]; var line = new WriteableBitmap(bmp.PixelWidth, 1, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette); for (var y = 0; y < bmp.PixelHeight; y++) { // copy line lineRect.Y = y; bmp.CopyPixels(lineRect, buffer, stride, 0); lineRect.Y = 0; line.WritePixels(lineRect, buffer, stride, 0); // calculate scaling var deltaAbs = (double)bmp.PixelWidth / 2 * Distortion; var deltaRel = deltaAbs * (1 - (double)y / bmp.PixelHeight); srcRect.Width = bmp.PixelWidth - (int)(2 * deltaRel); srcRect.X = (int)deltaRel; srcRect.Y = y; destRect.Y = y; var scaleX = (double)bmp.PixelWidth / srcRect.Width; // scale line var scaledLine = new TransformedBitmap(line, new ScaleTransform(scaleX, 1, (double)line.PixelWidth / 2, 0)); // copy scaled line to dest lineRect.X = (int)((bmp.PixelWidth * scaleX) - bmp.PixelWidth) / 2; scaledLine.CopyPixels(lineRect, buffer, stride, 0); lineRect.X = 0; lineRect.Y = y; wBmp.WritePixels(lineRect, buffer, stride, 0); } sw.Stop(); Console.WriteLine("Distored image in {0}ms.", sw.ElapsedMilliseconds); wBmp.Freeze(); _whenProcessed.OnNext(wBmp); return(wBmp); }
/// <summary> /// Processes a frame /// </summary> /// <param name="bmp">Unprocessed frame</param> /// <param name="dest">The destination for which the frame is processed.</param> /// <returns>Processed frame</returns> public abstract BitmapSource Process(BitmapSource bmp, IFrameDestination dest);
public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest) { // var luminosities = new HashSet<double>(); var bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8; var stride = bmp.PixelWidth * bytesPerPixel; var pixelBuffer = new byte[stride * bmp.PixelHeight]; var fullRect = new Int32Rect { X = 0, Y = 0, Width = bmp.PixelWidth, Height = bmp.PixelHeight }; bmp.CopyPixels(fullRect, pixelBuffer, stride, 0); for (var k = 0; k + 4 < pixelBuffer.Length; k += 4) { // convert to HSL var b = pixelBuffer[k]; var g = pixelBuffer[k + 1]; var r = pixelBuffer[k + 2]; double hue; double saturation; double luminosity; ColorUtil.RgbToHsl(r, g, b, out hue, out saturation, out luminosity); // manipulate luminosity if (NumShades > 0) { var num = NumShades; if (dest.IsRgb && Shades != null && Shades.Length == NumShades) { var index = (int)Math.Min(num - 1, Math.Max(0, (Math.Round(luminosity * Intensity * num) + Brightness))); luminosity = Shades[index]; } else { luminosity = Math.Max(0, (Math.Round(luminosity * Intensity * num) + Brightness) / num); } } else { luminosity = Math.Max(0, luminosity * Intensity + Brightness); } // luminosities.Add(luminosity); // convert back to RGB byte red; byte green; byte blue; saturation = 1; ColorUtil.HslToRgb(hue, saturation, luminosity, out red, out green, out blue); pixelBuffer[k] = blue; pixelBuffer[k + 1] = green; pixelBuffer[k + 2] = red; } // Console.WriteLine(string.Join(",", luminosities)); var wBmp = new WriteableBitmap(bmp); wBmp.WritePixels(fullRect, pixelBuffer, stride, 0); wBmp.Freeze(); _whenProcessed.OnNext(wBmp); return(wBmp); }