예제 #1
0
 private bool SetBrightnessAndContrast(Contrast contrast, Brightness brightness)
 {
     lock (brightLock)
     {
         lock (contrastLock)
         {
             if (!this.Contrast.Equals(contrast) || !this.Brightness.Equals(brightness))
             {
                 this.Contrast.SetTo(contrast);
                 this.Brightness.SetTo(brightness);
                 return(true);
             }
             return(false);
         }
     }
 }
예제 #2
0
        /// <summary>
        /// Pushes the work of recalculating the image for
        /// a new contrast and brightness to a background
        /// thread with Task.Run(() => {});
        /// Only fires the event changed handler if the
        /// brightness and contrast calculated are still
        /// valid upon completion.
        /// </summary>
        /// <param name="contrast"></param>
        /// <param name="brightness"></param>
        public void AdjustImage(Contrast contrast, Brightness brightness)
        {
            if (!this.SetBrightnessAndContrast(contrast, brightness))
            {
                return;
            }

            var adjust = Task.Run(() =>
            {
                if (contrast.IsDefault && brightness.IsDefault)
                {
                    this.CurrentBytes = this.OriginalBytes;
                    this.ImageUpdated?.Invoke(this, EventArgs.Empty);
                    return;
                }

                var temp = new byte[this.OriginalBytes.Length];

                var pixel = new byte[4];
                using (Stream source = this.OriginalBytes.AsBuffer().AsStream())
                {
                    using (Stream dest = temp.AsBuffer().AsStream())
                    {
                        while (source.Read(pixel, 0, 4) > 0)
                        {
                            pixel = CalculatePixel(pixel, brightness.Value, contrast.Value);
                            dest.Write(pixel, 0, 4);
                        }
                    }
                }

                lock (brightLock)
                {
                    lock (contrastLock)
                    {
                        if (this.Brightness.Equals(brightness) && this.Contrast.Equals(contrast))
                        {
                            this.CurrentBytes = temp;
                            this.ImageUpdated?.Invoke(this, EventArgs.Empty);
                        }
                    }
                }
            });
        }