Ejemplo n.º 1
0
        /// <summary>
        /// Overloaded Opens the document specified by the given <paramref name="fileName"/>
        /// and uses the specified  <paramref name="loader"/>.
        /// </summary>
        /// <param name="fileName">The document to be opened.</param>
        /// <param name="loader">The loader associated with the file type.</param>
        /// <param name="roiObjects">list of RegionOfInterest to add to the ImageView</param>
        /// <returns>A <see cref="bool"/> value indicating the success of the attempt to open a document.</returns>
        internal bool OpenDocument(string fileName, ISpecFileLoader loader, List <RegionOfInterest> roiObjects)
        {
            if (loader == null || string.IsNullOrEmpty(fileName))
            {
                return(false);
            }

            ISpecFileContent specFileContent = loader.Load(fileName);

            if (specFileContent == null)
            {
                return(false);
            }

            Document doc = specFileContent.Document;

            if (doc == null)
            {
                return(false);
            }

            AppContext.DocumentList.Add(doc);

            ViewCollection views = specFileContent.GetContentViews(doc);

            if (views != null)
            {
                foreach (ViewImage view in views)
                {
                    // Add in the ImageDataObject to each of the roiObjects
                    // Probably not the best place to put this.. but will do for now.
                    foreach (RegionOfInterest regionofinterest in roiObjects)
                    {
                        regionofinterest.ImageData = view.GetImagingData();
                    }

                    view.RoiObjects = roiObjects;
                    view.RoiInitialSetup();
                }

                foreach (IView view in views)
                {
                    AppContext.Application.AddView(view);
                    doc.ViewCollection.Add(view);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens the document specified by the given <paramref name="fileName"/>
        /// and uses the specified  <paramref name="loader"/>.
        /// </summary>
        /// <param name="fileName">The document to be opened.</param>
        /// <param name="loader">The loader associated with the file type.</param>
        /// <returns>A <see cref="bool"/> value indicating the success of the attempt to open a document.</returns>
        internal bool OpenDocument(string fileName, ISpecFileLoader loader)
        {
            if (loader == null || string.IsNullOrEmpty(fileName))
            {
                return(false);
            }

            ISpecFileContent specFileContent = loader.Load(fileName);

            if (specFileContent == null)
            {
                return(false);
            }

            Document doc = specFileContent.Document;

            if (doc == null)
            {
                return(false);
            }

            AppContext.DocumentList.Add(doc);

            ViewCollection views = specFileContent.GetContentViews(doc);

            if (views != null)
            {
                foreach (IView view in views)
                {
                    AppContext.Application.AddView(view);
                    doc.ViewCollection.Add(view);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load and convert the file specified by <paramref name="fileName"/> to the format specified by <paramref name="targetExt"/>
        /// and/or create a bitmap of the file. The bitmap format is defined by <paramref name="bitmapExt"/>. The result will be stored
        /// in the directory given by <paramref name="targetDir"/>.
        /// </summary>
        /// <param name="fileName">The full qualified filename of the file to process.</param>
        /// <param name="targetDir">The directory where the results should be stored.</param>
        /// <param name="targetExt">The target format to which to convert the given file. If empty, no conversion takes place.</param>
        /// <param name="bitmapExt">If not empty a bitmap of the type specified by this extension is created from the files (image) content.</param>
        /// <param name="bitmapDir">Specifies an additional directory where a copy of the bitmap (if any) should be created.</param>
        /// <returns>A <see cref="bool"/> value indicating the success of the operation.</returns>
        public bool BatchProcess(string fileName, string targetDir, string targetExt, string bitmapExt, string bitmapDir)
        {
            if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                return(false);
            }

            // load the file...
            try
            {
                // identify the loader on the basis of the extension...
                string          extension = Path.GetExtension(fileName);
                ISpecFileLoader loader    = AppContext.SpecFileLoaders.FindSupportingLoader(extension);
                if (loader == null)
                {
                    return(false);
                }

                // find the matching image writer
                IImagingWriter imageWriter = null;
                if (!string.IsNullOrEmpty(targetExt))
                {
                    foreach (var candidate in AppContext.ImagingWriters)
                    {
                        if (candidate != null)
                        {
                            bool found = false;
                            foreach (FileTypeDescriptor fileType in candidate.SupportedFileTypes)
                            {
                                if (fileType != null && fileType.IncludesExtension(targetExt))
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (found)
                            {
                                imageWriter = candidate;
                                break;
                            }
                        }
                    }
                }

                // find the matching bitmap writer
                IBitmapWriter bitmapWriter = null;
                if (!string.IsNullOrEmpty(bitmapExt))
                {
                    foreach (IBitmapWriter candidate in AppContext.BitmapWriters)
                    {
                        if (candidate != null)
                        {
                            bool found = false;
                            foreach (FileTypeDescriptor fileType in candidate.SupportedFileTypes)
                            {
                                if (fileType != null && fileType.IncludesExtension(bitmapExt))
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (found)
                            {
                                bitmapWriter = candidate as BitmapWriter;
                                break;
                            }
                        }
                    }
                }

                if (imageWriter == null && bitmapWriter == null)
                {
                    // no appropriate writers found for requested operation
                    return(false);
                }

                // let the loader load
                ISpecFileContent specFileContent = loader.Load(fileName);
                if (specFileContent == null)
                {
                    return(false);
                }

                BaseObjectList baseObjects = specFileContent.GetContent();
                if (baseObjects == null)
                {
                    return(false);
                }

                string outDir   = (!string.IsNullOrEmpty(targetDir) && Directory.Exists(targetDir)) ? targetDir : Path.GetDirectoryName(fileName);
                string filename = Path.GetFileNameWithoutExtension(fileName);

                // loop over the images an process
                foreach (BaseObject baseObject in baseObjects)
                {
                    var imaging = baseObject as Imaging;
                    if (imaging == null)
                    {
                        continue;
                    }

                    // perform image conversion if requested...
                    if (imageWriter != null)
                    {
                        // compose filename
                        string targetFile = filename + " - " + imaging.Name;
                        targetFile = Util.SubstitueInvalidPathChars(targetFile, '-');
                        targetFile = targetFile.Replace('.', '_');
                        targetFile = Path.Combine(outDir, targetFile + targetExt);
                        imageWriter.Write(imaging, targetFile, false);
                    }

                    // create a bitmap if required
                    if (bitmapWriter != null)
                    {
                        BitmapSourceList bitmaps = imaging.GetBitmaps();
                        if (bitmaps != null && bitmaps.Count > 0)
                        {
                            System.Windows.Media.Imaging.BitmapSource bitmap = bitmaps[0];
                            if (bitmap != null)
                            {
                                // compose filename
                                string bitmapFileName = filename + " - " + imaging.Name;
                                bitmapFileName = Util.SubstitueInvalidPathChars(bitmapFileName, '-');
                                bitmapFileName = bitmapFileName.Replace('.', '_');
                                string bitmapFilePath = Path.Combine(outDir, bitmapFileName + bitmapExt);
                                bitmapWriter.Write(bitmap, bitmapFilePath, false);
                                if (!string.IsNullOrEmpty(bitmapDir) && Directory.Exists(bitmapDir))
                                {
                                    bitmapFilePath = Path.Combine(bitmapDir, bitmapFileName + bitmapExt);
                                    bitmapWriter.Write(bitmap, bitmapFilePath, false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Util.ReportException(e);
                return(false);
            }

            return(true);
        }