示例#1
0
        private void UpdateImages()
        {
            // only perform if image was already loaded
            if (!imageLoaded)
            {
                return;
            }

            // prepares quantizer
            errorCache.Clear();

            // tries to retrieve an image based on HSB quantization
            Int32         parallelTaskCount = activeQuantizer.AllowParallel ? Convert.ToInt32(listParallel.Text) : 1;
            TaskScheduler uiScheduler       = TaskScheduler.FromCurrentSynchronizationContext();
            Int32         colorCount        = GetColorCount();

            // disables all the controls and starts running
            sourceImage = Image.FromFile(dialogOpenFile.FileName);
            Text        = Resources.Running;
            SwitchControls(false);
            DateTime before = DateTime.Now;

            // quantization process
            Task quantization = Task.Factory.StartNew(() =>
                                                      targetImage = ImageBuffer.QuantizeImage(sourceImage, activeQuantizer, activeDitherer, colorCount, parallelTaskCount),
                                                      TaskCreationOptions.LongRunning);

            // finishes after running
            quantization.ContinueWith(task =>
            {
                // detects operation duration
                TimeSpan duration = DateTime.Now - before;
                TimeSpan perPixel = new TimeSpan(duration.Ticks / (sourceImage.Width * sourceImage.Height));

                // detects error and color count
                Int32 originalColorCount = activeQuantizer.GetColorCount();
                String nrmsdString       = string.Empty;

                // calculates NRMSD error, if requested
                if (checkShowError.Checked)
                {
                    Double nrmsd = ImageBuffer.CalculateImageNormalizedMeanError(sourceImage, targetImage, parallelTaskCount);
                    nrmsdString  = string.Format(" (NRMSD = {0:0.#####})", nrmsd);
                }

                // spits some duration statistics (those actually slow the processing quite a bit, turn them off to make it quicker)
                editSourceInfo.Text = string.Format("Original: {0} colors ({1} x {2})", originalColorCount, sourceImage.Width, sourceImage.Height);
                editTargetInfo.Text = string.Format("Quantized: {0} colors{1}", colorCount, nrmsdString);

                // new GIF and PNG sizes
                Int32 newGifSize, newPngSize;

                // retrieves a GIF image based on our HSB-quantized one
                GetConvertedImage(targetImage, ImageFormat.Gif, out newGifSize);

                // retrieves a PNG image based on our HSB-quantized one
                GetConvertedImage(targetImage, ImageFormat.Png, out newPngSize);

                // spits out the statistics
                Text = string.Format("Simple palette quantizer (duration 0:{0:00}.{1:0000000}, per pixel 0.{2:0000000})", duration.Seconds, duration.Ticks, perPixel.Ticks);
                editProjectedGifSize.Text = projectedGifSize.ToString();
                editProjectedPngSize.Text = sourceFileInfo.Length.ToString();
                editNewGifSize.Text       = newGifSize.ToString();
                editNewPngSize.Text       = newPngSize.ToString();
                pictureTarget.Image       = targetImage;

                // enables controls again
                SwitchControls(true);
            }, uiScheduler);
        }