/// <summary>
        /// Creates zero or more <see cref="IDisplaySet"/>s from the given <see cref="Series"/>.
        /// </summary>
        /// <remarks>
        /// When the input <see cref="Series"/> does not have multiple echoes, no <see cref="IDisplaySet"/>s will be returned.
        /// Otherwise, at least 2 <see cref="IDisplaySet"/>s will be returned.
        /// </remarks>
        public override List <IDisplaySet> CreateDisplaySets(Series series)
        {
            List <IDisplaySet> displaySets = new List <IDisplaySet>();

            if (series.Modality == "MR")
            {
                SortedDictionary <int, List <Sop> > imagesByEchoNumber = SplitMREchos(series.Sops);
                if (imagesByEchoNumber.Count > 1)
                {
                    foreach (KeyValuePair <int, List <Sop> > echoImages in imagesByEchoNumber)
                    {
                        List <IPresentationImage> images = new List <IPresentationImage>();
                        foreach (ImageSop sop in echoImages.Value)
                        {
                            images.AddRange(PresentationImageFactory.CreateImages(sop));
                        }

                        if (images.Count > 0)
                        {
                            IDisplaySet displaySet = new DisplaySet(new MREchoDisplaySetDescriptor(series.GetIdentifier(), echoImages.Key, PresentationImageFactory));
                            foreach (IPresentationImage image in images)
                            {
                                displaySet.PresentationImages.Add(image);
                            }

                            displaySet.PresentationImages.Sort();
                            displaySets.Add(displaySet);
                        }
                    }
                }
            }

            return(displaySets);
        }
        private IDisplaySet CreateDisplaySet(Study study, IEnumerable <Series> modalitySeries)
        {
            var first = modalitySeries.FirstOrDefault();

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

            var modality = first.Modality;

            if (String.IsNullOrEmpty(modality))
            {
                return(null);
            }

            var displaySet  = new DisplaySet(new ModalityDisplaySetDescriptor(study.GetIdentifier(), modality, PresentationImageFactory));
            int seriesCount = 0;

            foreach (var series in modalitySeries)
            {
                bool added = false;
                foreach (var imageSop in series.Sops) //We don't want key images, references etc.
                {
                    foreach (var image in PresentationImageFactory.CreateImages(imageSop))
                    {
                        displaySet.PresentationImages.Add(image);
                        added = true;
                    }
                }

                if (added)
                {
                    ++seriesCount;
                }
            }

            // Degenerate case is one series, in which case we don't create this display set.
            if (seriesCount > 1)
            {
                return(displaySet);
            }

            displaySet.Dispose();
            return(null);
        }
        private IDisplaySet CreateSeriesDisplaySet(Series series)
        {
            IDisplaySet displaySet           = null;
            List <IPresentationImage> images = new List <IPresentationImage>();

            foreach (Sop sop in series.Sops)
            {
                images.AddRange(PresentationImageFactory.CreateImages(sop));
            }

            if (images.Count > 0)
            {
                DisplaySetDescriptor descriptor = new SeriesDisplaySetDescriptor(series.GetIdentifier(), PresentationImageFactory);
                displaySet = new DisplaySet(descriptor);
                foreach (IPresentationImage image in images)
                {
                    displaySet.PresentationImages.Add(image);
                }
            }

            return(displaySet);
        }
        /// <summary>
        /// Creates zero or more <see cref="IDisplaySet"/>s from the given <see cref="Series"/>.
        /// </summary>
        /// <remarks>When the input series does not contain a mixture of single and multiframe
        /// images, no <see cref="IDisplaySet"/>s will be returned.</remarks>
        public override List <IDisplaySet> CreateDisplaySets(Series series)
        {
            List <IDisplaySet> displaySets = new List <IDisplaySet>();

            List <ImageSop> singleFrames = new List <ImageSop>();
            List <ImageSop> multiFrames  = new List <ImageSop>();

            foreach (Sop sop in series.Sops)
            {
                if (sop.IsImage)
                {
                    ImageSop imageSop = (ImageSop)sop;
                    if (imageSop.NumberOfFrames > 1)
                    {
                        multiFrames.Add(imageSop);
                    }
                    else
                    {
                        singleFrames.Add(imageSop);
                    }
                }
            }

            if (multiFrames.Count > 1 || (singleFrames.Count > 0 && multiFrames.Count > 0))
            {
                if (singleFrames.Count > 0)
                {
                    List <IPresentationImage> singleFrameImages = new List <IPresentationImage>();
                    foreach (ImageSop singleFrame in singleFrames)
                    {
                        singleFrameImages.AddRange(PresentationImageFactory.CreateImages(singleFrame));
                    }

                    if (singleFrameImages.Count > 0)
                    {
                        var descriptor             = new SingleImagesDisplaySetDescriptor(series.GetIdentifier(), PresentationImageFactory);
                        var singleImagesDisplaySet = new DisplaySet(descriptor);

                        foreach (IPresentationImage singleFrameImage in singleFrameImages)
                        {
                            singleImagesDisplaySet.PresentationImages.Add(singleFrameImage);
                        }

                        singleImagesDisplaySet.PresentationImages.Sort();
                        displaySets.Add(singleImagesDisplaySet);
                    }
                }

                foreach (ImageSop multiFrame in multiFrames)
                {
                    List <IPresentationImage> multiFrameImages = PresentationImageFactory.CreateImages(multiFrame);
                    if (multiFrameImages.Count > 0)
                    {
                        MultiframeDisplaySetDescriptor descriptor =
                            new MultiframeDisplaySetDescriptor(multiFrame.ParentSeries.GetIdentifier(), multiFrame.SopInstanceUid, multiFrame.InstanceNumber);
                        DisplaySet displaySet = new DisplaySet(descriptor);

                        foreach (IPresentationImage multiFrameImage in multiFrameImages)
                        {
                            displaySet.PresentationImages.Add(multiFrameImage);
                        }

                        displaySet.PresentationImages.Sort();
                        displaySets.Add(displaySet);
                    }
                }
            }

            return(displaySets);
        }
        private List <IDisplaySet> DoCreateSingleImageDisplaySets(Series series)
        {
            List <IDisplaySet> displaySets = new List <IDisplaySet>();
            int position = 0;

            foreach (Sop sop in series.Sops)
            {
                List <IPresentationImage> images = PresentationImageFactory.CreateImages(sop);
                if (images.Count == 0)
                {
                    continue;
                }

                if (sop.IsImage)
                {
                    ImageSop imageSop = (ImageSop)sop;
                    DicomDisplaySetDescriptor descriptor;

                    if (imageSop.NumberOfFrames == 1)
                    {
                        descriptor = new SingleImageDisplaySetDescriptor(series.GetIdentifier(), imageSop, position++);
                    }
                    else
                    {
                        descriptor = new MultiframeDisplaySetDescriptor(series.GetIdentifier(), sop.SopInstanceUid, sop.InstanceNumber);
                    }

                    DisplaySet displaySet = new DisplaySet(descriptor);
                    foreach (IPresentationImage image in images)
                    {
                        displaySet.PresentationImages.Add(image);
                    }

                    displaySets.Add(displaySet);
                }
                else
                {
                    //The sop is actually a container for other referenced sops, like key images.
                    foreach (IPresentationImage image in images)
                    {
                        DisplaySetDescriptor descriptor = null;
                        if (image is IImageSopProvider)
                        {
                            IImageSopProvider provider = (IImageSopProvider)image;
                            if (provider.ImageSop.NumberOfFrames == 1)
                            {
                                descriptor = new SingleImageDisplaySetDescriptor(series.GetIdentifier(), provider.ImageSop, position++);
                            }
                            else
                            {
                                descriptor = new SingleFrameDisplaySetDescriptor(series.GetIdentifier(), provider.Frame, position++);
                            }
                        }
                        else
                        {
                            //TODO (CR Jan 2010): this because the design here is funny... the factory here should actually know something about the key object series it is building for
                            ISeriesIdentifier sourceSeries = series.GetIdentifier();
                            descriptor             = new BasicDisplaySetDescriptor();
                            descriptor.Description = sourceSeries.SeriesDescription;
                            descriptor.Name        = string.Format("{0}: {1}", sourceSeries.SeriesNumber, sourceSeries.SeriesDescription);
                            descriptor.Number      = sourceSeries.SeriesNumber.GetValueOrDefault(0);
                            descriptor.Uid         = sourceSeries.SeriesInstanceUid;
                        }

                        DisplaySet displaySet = new DisplaySet(descriptor);
                        displaySet.PresentationImages.Add(image);
                        displaySets.Add(displaySet);
                    }
                }
            }

            if (displaySets.Count == 1)
            {
                //Degenerate case; single image series, which we're not supposed to create.
                displaySets[0].Dispose();
                displaySets.Clear();
            }

            return(displaySets);
        }