public override BitmapSource ExecuteFilter(BitmapSource inputImage) { locked = true; BitmapSource blurred = inputImage.Clone(); int pixelCount = inputImage.PixelWidth * inputImage.PixelHeight; int[] b = new int[pixelCount]; int[] g = new int[pixelCount]; int[] r = new int[pixelCount]; int[] b2 = new int[pixelCount]; int[] g2 = new int[pixelCount]; int[] r2 = new int[pixelCount]; int width = inputImage.PixelWidth; int height = inputImage.PixelHeight; byte[] srcPixels = new byte[width * height * 4]; byte[] destPixels = new byte[width * height * 4]; inputImage.CopyPixels(srcPixels, width * 4, 0); blurred.CopyPixels(destPixels, width * 4, 0); for (int i = 0, j = 0; i < pixelCount * 4; i += 4, j++) { b[j] = srcPixels[i]; g[j] = srcPixels[i + 1]; r[j] = srcPixels[i + 2]; } int index = 0; int bsum; int gsum; int rsum; int sum; int read; int start = 0; index = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { bsum = gsum = rsum = sum = 0; read = index - _radius; for (int z = 0; z < _kernel.Length; z++) { if (read >= start && read < start + width) { bsum += _multable[z, b[read]]; gsum += _multable[z, g[read]]; rsum += _multable[z, r[read]]; sum += _kernel[z]; } ++read; } b2[index] = (bsum / sum); g2[index] = (gsum / sum); r2[index] = (rsum / sum); ++index; } start += width; } int tempy; int pixelIndex = 0; for (int i = 0; i < height; i++) { int y = i - _radius; start = y * width; for (int j = 0; j < width; j++) { bsum = gsum = rsum = sum = 0; read = start + j; tempy = y; for (int z = 0; z < _kernel.Length; z++) { if (tempy >= 0 && tempy < height) { bsum += _multable[z, b2[read]]; gsum += _multable[z, g2[read]]; rsum += _multable[z, r2[read]]; sum += _kernel[z]; } read += width; ++tempy; } destPixels[pixelIndex] = (byte)(bsum / sum); destPixels[pixelIndex + 1] = (byte)(gsum / sum); destPixels[pixelIndex + 2] = (byte)(rsum / sum); pixelIndex += 4; } } blurred = BitmapSource.Create(blurred.PixelWidth, blurred.PixelHeight, 96, 96, blurred.Format, null, destPixels, blurred.PixelWidth * 4); locked = false; return blurred; }
private static BitmapSource MetaOrientation(BitmapMetadata meta, BitmapSource ret) { double angle = 0; if ((meta != null) && (ret != null)) //si on a des meta, tentative de récupération de l'orientation { if (meta.GetQuery("/app1/ifd/{ushort=274}") != null) { orientation = (ExifOrientations) Enum.Parse(typeof (ExifOrientations), meta.GetQuery("/app1/ifd/{ushort=274}").ToString()); } switch (orientation) { case ExifOrientations.Rotate90: angle = -90; break; case ExifOrientations.Rotate180: angle = 180; break; case ExifOrientations.Rotate270: angle = 90; break; } if (angle != 0) //on doit effectuer une rotation de l'image { ret = new TransformedBitmap(ret.Clone(), new RotateTransform(angle)); ret.Freeze(); } } return ret; }
/// <summary> /// creates a byte array from a bitmapImage /// </summary> /// <param name="source">the image for creating the byte array</param> /// <returns>a byte array</returns> public static byte[] GetByteData(BitmapSource source) { try { using (MemoryStream ms = new MemoryStream()) { PngBitmapEncoder encoder = new PngBitmapEncoder(); BitmapFrame frame = BitmapFrame.Create(source.Clone()); encoder.Frames.Add(frame); encoder.Save(ms); ms.Seek(0, SeekOrigin.Begin); return ms.GetBuffer(); } } catch { return null; } }
private static BitmapSource MetaOrientation(BitmapMetadata meta, BitmapSource ret) { double angle = 0.0; if (meta != null && ret != null) { if (meta.GetQuery("/app1/ifd/{ushort=274}") != null) Picture.orientation = (Picture.ExifOrientations)Enum.Parse(typeof(Picture.ExifOrientations), meta.GetQuery("/app1/ifd/{ushort=274}").ToString()); switch (Picture.orientation) { case Picture.ExifOrientations.Rotate180: angle = 180.0; break; case Picture.ExifOrientations.Rotate270: angle = 90.0; break; case Picture.ExifOrientations.Rotate90: angle = -90.0; break; } if (angle != 0.0) { ret = (BitmapSource)new TransformedBitmap(ret.Clone(), (Transform)new RotateTransform(angle)); ret.Freeze(); } } return ret; }
public BitmapSource Apply(BitmapSource image) { return (BitmapSource) image.Clone(); }