private static Color GetColorFromImageComparison(
     ImageComparisonResult imageComparison,
     bool finished)
 {
     if (!finished)
     {
         if (imageComparison.ComparisonResult
             == ComparisonResult.Different)
         {
             return(DifferenceColor);
         }
         else
         {
             return(DuplicateColor);
         }
     }
     return(DefaultBackColor);
 }
        private string GetTextFromImageComparison(
            int index,
            ImageComparisonResult imageComparison,
            bool finished,
            bool loaded)
        {
            var text = $"{index + 1}. Comparison:{Environment.NewLine}";

            if (!finished)
            {
                text += $"Images loaded and compared{Environment.NewLine}"
                        + $"Difference {imageComparison.Difference * 100} is ";
                if (imageComparison.ComparisonResult
                    == ComparisonResult.Different)
                {
                    text += "higher";
                }
                else
                {
                    text += "lower";
                }
                text += $" than {NumMaxDifferentPercentage.Value}";
            }
            else
            {
                text += $"Result already determined.{Environment.NewLine}";
                if (loaded)
                {
                    text += "Images loaded but comparison was skipped.";
                }
                else
                {
                    text += "Images have not been loaded.";
                }
            }
            return(text);
        }
Exemple #3
0
        private byte[] CreateImage()
        {
            if (_tile.PresentationImage == null)
            {
                DisposeSurface();
            }

            if (Surface == null)
            {
                return(null);
            }

            if (_refreshingClient)
            {
                return(GetCurrentTileBitmap());
            }

            IImageSopProvider sop = _tile.PresentationImage as IImageSopProvider;

            if (sop != null)
            {
                //TODO (CR May 2010): sops are shared between users and threads.  This will be an issue
                //for dynamic quality changes.
                DicomAttribute attrib      = sop.ImageSop[DicomTags.LossyImageCompression];
                DicomAttribute ratioAttrib = sop.ImageSop[DicomTags.LossyImageCompressionRatio];
                bool           lossy       = false;
                if (_mimeType.Equals("image/jpeg"))
                {
                    lossy = true;
                }
                if (lossy)
                {
                    attrib.SetStringValue("01");
                }
                else
                {
                    if (ratioAttrib.IsEmpty)
                    {
                        attrib.SetEmptyValue();
                    }
                }
            }

            WebViewStudyStatistics stats = new WebViewStudyStatistics(_mimeType);

            //long t0 = Environment.TickCount;
            stats.DrawToBitmapTime.Start();

            Bitmap bitmap = Bitmap;

            using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
            {
                Surface.ContextID     = graphics.GetHdc();
                Surface.ClipRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                DrawArgs drawArgs = new DrawArgs(Surface, null, Rendering.DrawMode.Render);
                CurrentImage.Draw(drawArgs);
                drawArgs = new DrawArgs(Surface, null, Rendering.DrawMode.Refresh);
                CurrentImage.Draw(drawArgs);
                graphics.ReleaseHdc(Surface.ContextID);
            }

            stats.DrawToBitmapTime.End();

            Bitmap bmp1 = null;

            if (DiagnosticsSettings.Default.CompareImageQuality)
            {
                // make a copy in case Bitmap.Save() has any side effects.
                bmp1 = (Bitmap)Bitmap.Clone();
            }

            //TODO (CR May 2010): should be in using and/or closed.  Separate function?
            MemoryStream ms = new MemoryStream();

            _quality = _defaultJpegQFactor;
            if (_isMouseDown && IsDynamicImageQualityEnabled)
            {
                _quality = GetOptimalQFactor(Bitmap.Width, Bitmap.Height, sop);

                InitOrUpdateRefreshClientTimer();
            }

            stats.SaveTime.Start();
            if (_mimeType.Equals("image/jpeg"))
            {
                EncoderParameters eps = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(Encoder.Quality, _quality);
                ImageCodecInfo ici = GetEncoderInfo(_mimeType);

                bitmap.Save(ms, ici, eps);
            }
            else if (_mimeType.Equals("image/png"))
            {
                bitmap.Save(ms, ImageFormat.Png);
            }
            stats.SaveTime.End();

            byte[] imageBuffer = ms.ToArray();

            if (Platform.IsLogLevelEnabled(LogLevel.Debug))
            {
                Platform.Log(LogLevel.Debug, "Render Frame #{0}. Size= {1}bytes. Q={2} {3}. Highbit={4}",
                             sop.ImageSop.InstanceNumber, imageBuffer.Length, _quality, _isMouseDown && IsDynamicImageQualityEnabled ? "[Dynamic]" : "",
                             sop.Frame.HighBit);
            }

            ms.Position     = 0;
            stats.ImageSize = (ulong)imageBuffer.LongLength;
            _stats.AddSubStats(stats);

            //StatisticsLogger.Log(LogLevel.Info, false, stats);
            if (_stats.SubStatistics.Count > 20)
            {
                _stats.CalculateAverage();
                //StatisticsLogger.Log(LogLevel.Info, false, _stats);
                _stats = new StatisticsSet("AverageRender");
            }

            //Console.WriteLine("Tile {0} : DrawToBitmap (size: {3}, mime: {2}):{1}ms", tile.Identifier,Environment.TickCount - t0,mimeType, ms.Length);

            //TODO (CR May 2010): #if DEBUG?
            if (DiagnosticsSettings.Default.CompareImageQuality)
            {
                Bitmap bmp2 = new Bitmap(ms);
                ImageComparisonResult result = BitmapComparison.Compare(ref bmp1, ref bmp2);
                //TODO (CR May 2010): ConsoleHelper
                Console.WriteLine("BMP vs {0} w/ client size: {1}x{2}", _mimeType, bmp2.Height, bmp2.Width);
                Console.WriteLine("\tR: MinError={2:0.00} MaxError={3:0.00}  Mean={0:0.00}  STD={1:0.00}", result.Channels[0].MeanError, result.Channels[0].StdDeviation, Math.Abs(result.Channels[0].MinError), Math.Abs(result.Channels[0].MaxError));
                Console.WriteLine("\tG: MinError={2:0.00} MaxError={3:0.00}  Mean={0:0.00}  STD={1:0.00}", result.Channels[1].MeanError, result.Channels[1].StdDeviation, Math.Abs(result.Channels[1].MinError), Math.Abs(result.Channels[1].MaxError));
                Console.WriteLine("\tB: MinError={2:0.00} MaxError={3:0.00}  Mean={0:0.00}  STD={1:0.00}", result.Channels[2].MeanError, result.Channels[2].StdDeviation, Math.Abs(result.Channels[2].MinError), Math.Abs(result.Channels[2].MaxError));
            }

            return(imageBuffer);
        }