예제 #1
0
        protected Bitmap CalculateDifference(Bitmap bmp1, Bitmap bmp2)
        {
            if (bmp1 == null || bmp2 == null || Equals(bmp1, bmp2) || !bmp1.Size.Equals(bmp2.Size) || !bmp1.PixelFormat.Equals(bmp2.PixelFormat))
            {
                throw new InvalidOperationException("Can't calculate.");
            }

            return(BitmapOperations.Create((sources, i, j) =>
            {
                return sources[0].GetPixel(i, j) - sources[1].GetPixel(i, j);
            }, bmp1.TransformToEffectiveBitmap(), bmp2.TransformToEffectiveBitmap()).ToBitmap());
        }
예제 #2
0
        private EffectiveBitmap Watermark(EffectiveBitmap original, EffectiveBitmap watermark)
        {
            return(BitmapOperations.Create((sources, i, j) =>
            {
                var originalPixel = sources[0].GetPixel(i, j);
                var watermarkPixel = sources[1].GetPixel(i, j);

                var r = (int)(originalPixel.R + (watermarkPixel.R * parameters.Ratio)) / 2;
                var g = (int)(originalPixel.G + (watermarkPixel.G * parameters.Ratio)) / 2;
                var b = (int)(originalPixel.B + (watermarkPixel.B * parameters.Ratio)) / 2;

                return new PixelInfo(r, g, b);
            }, original, watermark));
        }
예제 #3
0
        private EffectiveBitmap CleanWatermark(EffectiveBitmap watermarked)
        {
            return(BitmapOperations.Create((sources, i, j) =>
            {
                var watermarkedImgPixel = sources[0].GetPixel(i, j);

                var divider = (int)(Math.Pow(2, parameters.BitsForWatermark) + 0.5);
                // TODO better method instead of random bit on last position
                var r = (byte)(watermarkedImgPixel.R - watermarkedImgPixel.R % divider + CreateRandomNumber(divider));
                var g = (byte)(watermarkedImgPixel.G - watermarkedImgPixel.G % divider + CreateRandomNumber(divider));
                var b = (byte)(watermarkedImgPixel.B - watermarkedImgPixel.B % divider + CreateRandomNumber(divider));

                return new PixelInfo(r, g, b);
            }, watermarked));
        }
예제 #4
0
        private EffectiveBitmap PreprocessToSimplifiedWatermark(EffectiveBitmap watermark)
        {
            var divider = (byte)(Math.Ceiling(255 / Math.Pow(2, parameters.BitsForWatermark)) + 0.5);

            return(BitmapOperations.Create((sources, i, j) =>
            {
                var watermarkPixel = sources[0].GetPixel(i, j);

                byte r = (byte)(watermarkPixel.R / divider);
                byte g = (byte)(watermarkPixel.G / divider);
                byte b = (byte)(watermarkPixel.B / divider);

                return new PixelInfo(r, g, b);
            }, watermark));
        }
예제 #5
0
        private EffectiveBitmap ExtractWatermark(EffectiveBitmap watermarked)
        {
            return(BitmapOperations.Create((sources, i, j) =>
            {
                var watermarkedImgPixel = sources[0].GetPixel(i, j);

                var divider = (int)Math.Pow(2, parameters.BitsForWatermark);
                var multiplier = 255 / (divider - 1);
                byte r = (byte)(watermarkedImgPixel.R % divider * multiplier);
                byte g = (byte)(watermarkedImgPixel.G % divider * multiplier);
                byte b = (byte)(watermarkedImgPixel.B % divider * multiplier);

                return new PixelInfo(r, g, b);
            }, watermarked));
        }
예제 #6
0
        private EffectiveBitmap CleanWatermark(EffectiveBitmap imgWatermarked, EffectiveBitmap watermark)
        {
            return(BitmapOperations.Create((sources, i, j) =>
            {
                var watermarkedImgPixel = sources[0].GetPixel(i, j);
                var watermarkPixel = sources[1].GetPixel(i, j);

                var r = (int)(watermarkedImgPixel.R * 2 - watermarkPixel.R * parameters.Ratio);
                var g = (int)(watermarkedImgPixel.G * 2 - watermarkPixel.G * parameters.Ratio);
                var b = (int)(watermarkedImgPixel.B * 2 - watermarkPixel.B * parameters.Ratio);

                r = r < 0 ? r + 1 : r;
                g = g < 0 ? g + 1 : g;
                b = b < 0 ? b + 1 : b;

                return new PixelInfo(r, g, b);
            }, imgWatermarked, watermark));
        }
예제 #7
0
        private EffectiveBitmap ExtractWatermark(EffectiveBitmap imgWatermarked, EffectiveBitmap original)
        {
            return(BitmapOperations.Create((sources, i, j) =>
            {
                var watermarkedImgPixel = sources[0].GetPixel(i, j);
                var originalPixel = sources[1].GetPixel(i, j);

                var r = (int)((2 * watermarkedImgPixel.R - originalPixel.R) * (1 / parameters.Ratio));
                var g = (int)((2 * watermarkedImgPixel.G - originalPixel.G) * (1 / parameters.Ratio));
                var b = (int)((2 * watermarkedImgPixel.B - originalPixel.B) * (1 / parameters.Ratio));

                r = r < 0 ? 0 : r;
                g = g < 0 ? 0 : g;
                b = b < 0 ? 0 : b;

                return new PixelInfo(r, g, b);
            }, imgWatermarked, original));
        }
예제 #8
0
        public override async IAsyncEnumerable <AlgorithmResultElement> AddWatermark([EnumeratorCancellation] CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            var haared = ProcessHaar(parameters.Original, false, parameters.Layers);

            yield return(new AlgorithmResultElement("DWT", haared, new ResultDescription(ToString())));

            ct.ThrowIfCancellationRequested();

            var watermark = parameters.Watermark;

            watermark = parameters.Watermark.Resize(watermark.Width / (2 * parameters.Layers), watermark.Height / (2 * parameters.Layers));

            var haaredWatermarked = BitmapOperations.Create((sources, i, j) =>
            {
                var layers         = parameters.Layers;
                var originalPixel  = sources[0].GetPixel(i, j);
                var watermarkPixel = sources[1].GetPixel(i % watermark.Width, j % watermark.Height);

                if (i >= sources[0].Height * (2 * layers - 1) / (2 * layers) && j <= sources[0].Width / (2 * layers))
                {
                    var alpha = parameters.Alpha;

                    var r = (int)(originalPixel.R + (watermarkPixel.R * alpha)) % 255;
                    var g = (int)(originalPixel.G + (watermarkPixel.G * alpha)) % 255;
                    var b = (int)(originalPixel.B + (watermarkPixel.B * alpha)) % 255;

                    return(new PixelInfo(r, g, b));
                }

                return(originalPixel);
            }, haared, watermark);

            yield return(new AlgorithmResultElement("DWT + watermark", haaredWatermarked, new ResultDescription(ToString())));

            ct.ThrowIfCancellationRequested();

            var watermarked = ProcessHaar(haaredWatermarked, true, parameters.Layers);

            yield return(new AlgorithmResultElement("Watermarked", watermarked, new ResultDescription(ToString())));
        }
예제 #9
0
        public override async IAsyncEnumerable <AlgorithmResultElement> RemoveWatermark([EnumeratorCancellation] CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            var haaredWatermarked = ProcessHaar(parameters.Watermarked, false, parameters.Layers);

            ct.ThrowIfCancellationRequested();
            var haaredOriginal = ProcessHaar(parameters.Original, false, parameters.Layers);

            ct.ThrowIfCancellationRequested();
            var extractedWatermark = BitmapOperations.Create((sources, i, j) =>
            {
                var layers = parameters.Layers;
                var haaredWatermarkedPixel = sources[0].GetPixel(i, j);
                var haaredPixel            = sources[1].GetPixel(i, j);

                var alpha = parameters.Alpha;

                var r = (int)((haaredWatermarkedPixel.R - haaredPixel.R) / alpha) % 255;
                var g = (int)((haaredWatermarkedPixel.G - haaredPixel.G) / alpha) % 255;
                var b = (int)((haaredWatermarkedPixel.B - haaredPixel.B) / alpha) % 255;

                return(new PixelInfo(r, g, b));
            }, haaredWatermarked, haaredOriginal);

            EffectiveBitmap croppedRemovedWatermark = null;

            if (parameters.Layers == 1)
            {
                croppedRemovedWatermark = extractedWatermark.Crop(extractedWatermark.Width / 2, 0, extractedWatermark.Width, extractedWatermark.Height / 2);
            }
            else if (parameters.Layers == 2)
            {
                croppedRemovedWatermark = extractedWatermark.Crop(extractedWatermark.Width * 3 / 4, 0, extractedWatermark.Width, extractedWatermark.Height / 4);
            }

            ct.ThrowIfCancellationRequested();

            var haaredRemovedWatermark = BitmapOperations.Create((sources, i, j) =>
            {
                var layers         = parameters.Layers;
                var originalPixel  = sources[0].GetPixel(i, j);
                var watermarkPixel = sources[1].GetPixel(i, j);

                if (i > sources[0].Height * (2 * layers - 1) / (2 * layers) && j < sources[0].Width / (2 * layers))
                {
                    var alpha = parameters.Alpha;

                    var r = (int)(originalPixel.R - (watermarkPixel.R * alpha)) % 255;
                    var g = (int)(originalPixel.G - (watermarkPixel.G * alpha)) % 255;
                    var b = (int)(originalPixel.B - (watermarkPixel.B * alpha)) % 255;

                    return(new PixelInfo(r, g, b));
                }

                return(originalPixel);
            }, haaredWatermarked, extractedWatermark);

            var removedWatermark = ProcessHaar(haaredRemovedWatermark, true, parameters.Layers);

            ct.ThrowIfCancellationRequested();
            yield return(new AlgorithmResultElement("Cleaned", removedWatermark, new ResultDescription(ToString())));

            ct.ThrowIfCancellationRequested();
            yield return(new AlgorithmResultElement("Extracted watermark", croppedRemovedWatermark, new ResultDescription(ToString())));
        }