/// <summary>Function to retrieve a single file name.</summary>
        /// <returns>The selected file path, or <b>null</b> if cancelled.</returns>
        public override string GetFilename()
        {
            ConfigureDialog();
            string filePath = base.GetFilename();

            if (string.IsNullOrWhiteSpace(filePath))
            {
                return(null);
            }

            var extension = new GorgonFileExtension(Path.GetExtension(filePath));

            SelectedCodec = null;

            // Check for an extension match on the codec list.
            foreach (IGorgonImageCodec codec in _codecs.Codecs.Where(item => (item.CodecCommonExtensions.Count > 0) && (item.CanDecode)))
            {
                if (codec.CodecCommonExtensions.Any(item => extension == new GorgonFileExtension(item)))
                {
                    SelectedCodec = codec;
                    break;
                }
            }

            return(filePath);
        }
Example #2
0
        /// <summary>
        /// Function to retrieve the codec used by the image.
        /// </summary>
        /// <param name="file">The file containing the image content.</param>
        /// <returns>The codec used to read the file.</returns>
        private IGorgonImageCodec GetCodec(FileInfo file)
        {
            IGorgonImageCodec result = null;
            Stream            stream = null;

            try
            {
                // Locate the file extension.
                if (!string.IsNullOrWhiteSpace(file.Extension))
                {
                    var extension = new GorgonFileExtension(file.Extension);

                    result = _codecs.CodecFileTypes.FirstOrDefault(item => item.extension == extension).codec;

                    if (result != null)
                    {
                        return(result);
                    }
                }
            }
            finally
            {
                stream?.Dispose();
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Function to retrieve the codec used by the image.
        /// </summary>
        /// <param name="file">The file containing the image content.</param>
        /// <returns>The codec used to read the file.</returns>
        private IGorgonSpriteCodec GetCodec(FileInfo file)
        {
            Stream stream = null;

            try
            {
                // Locate the file extension.
                if (!string.IsNullOrWhiteSpace(file.Extension))
                {
                    var extension = new GorgonFileExtension(file.Extension);

                    // Since all Gorgon's sprite files use the same extension, we'll have to be a little more aggressive when determining type.
                    (GorgonFileExtension, IGorgonSpriteCodec codec)[] results = _codecs.CodecFileTypes.Where(item => item.extension == extension).ToArray();
Example #4
0
        /// <summary>
        /// Function to find a writer plug-in for a given file name extension.
        /// </summary>
        /// <param name="fileExtension">Full file name or extension of the file to write.</param>
        /// <param name="skipMetaData">TRUE to skip the metadata check, FALSE to use it.</param>
        /// <returns>The plug-in used to write the file.</returns>
        public static FileWriterPlugIn GetWriterPlugIn(string fileExtension = null, bool skipMetaData = false)
        {
            FileWriterPlugIn result;

            // If we have meta-data, then use that to determine which file writer is used.
            if ((!skipMetaData) &&
                (!string.IsNullOrWhiteSpace(EditorMetaDataFile.WriterPlugInType)))
            {
                result = (from plugIn in PlugIns.WriterPlugIns
                          where string.Equals(plugIn.Value.Name,
                                              EditorMetaDataFile.WriterPlugInType,
                                              StringComparison.OrdinalIgnoreCase)
                          select plugIn.Value).FirstOrDefault();

                if (result != null)
                {
                    return(result);
                }
            }

            // We did not find a file writer in the meta data, try to derive which plug-in to use from the extension.
            if (string.IsNullOrWhiteSpace(fileExtension))
            {
                // We didn't give an extension, try and take it from the file name.
                fileExtension = Path.GetExtension(Path.GetExtension(Filename));
            }

            // If we passed in a full file name, then get its extension.
            if ((!string.IsNullOrWhiteSpace(fileExtension)) && (fileExtension.IndexOf('.') > 0))
            {
                fileExtension = Path.GetExtension(fileExtension);
            }

            // No extension?  Then likely there's no plug-in.
            if (string.IsNullOrWhiteSpace(fileExtension))
            {
                return(null);
            }

            var extension = new GorgonFileExtension(fileExtension);

            // Try to find the plug-in.
            _writerFiles.TryGetValue(extension, out result);

            return(result);
        }
        /// <summary>
        /// Function to import an image file from the physical file system into the current image.
        /// </summary>
        /// <param name="codec">The codec used to open the file.</param>
        /// <param name="filePath">The path to the file to import.</param>
        /// <returns>The source file information, image data, the virtual file entry for the working file and the original pixel format of the file.</returns>
        public (FileInfo file, IGorgonImage image, IGorgonVirtualFile workingFile, BufferFormat originalFormat) ImportImage(IGorgonImageCodec codec, string filePath)
        {
            var file = new FileInfo(filePath);
            IGorgonImageCodec  importCodec  = codec;
            IGorgonImageInfo   metaData     = null;
            IGorgonVirtualFile workFile     = null;
            IGorgonImage       importImage  = null;
            string             workFilePath = $"{Path.GetFileNameWithoutExtension(filePath)}_import_{Guid.NewGuid().ToString("N")}";

            // Try to determine if we can actually read the file using an installed codec, if we can't, then try to find a suitable codec.
            using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                if ((importCodec == null) || (!importCodec.IsReadable(stream)))
                {
                    importCodec = null;

                    foreach (IGorgonImageCodec newCodec in InstalledCodecs.Codecs.Where(item => (item.CodecCommonExtensions.Count > 0) && (item.CanDecode)))
                    {
                        if (newCodec.IsReadable(stream))
                        {
                            importCodec = newCodec;
                            break;
                        }
                    }
                }

                if (importCodec == null)
                {
                    throw new GorgonException(GorgonResult.CannotRead, string.Format(Resources.GORIMG_ERR_NO_CODEC, filePath));
                }

                metaData = importCodec.GetMetaData(stream);


                // We absolutely need to have an extension, or else the texconv tool will not work.
                var codecExtension = new GorgonFileExtension(importCodec.CodecCommonExtensions[0]);
                _log.Print($"Adding {codecExtension.Extension} extension to working file or else external tools may not be able to read it.", LoggingLevel.Verbose);
                workFilePath = $"{workFilePath}.{codecExtension.Extension}";

                using (Stream outStream = ScratchArea.OpenStream(workFilePath, FileMode.Create))
                {
                    stream.CopyTo(outStream);
                }
            }

            workFile = ScratchArea.FileSystem.GetFile(workFilePath);
            var formatInfo = new GorgonFormatInfo(metaData.Format);

            // This is always in DDS format.
            if (formatInfo.IsCompressed)
            {
                _log.Print($"Image is compressed using [{formatInfo.Format}] as its pixel format.", LoggingLevel.Intermediate);

                if (_compressor == null)
                {
                    throw new GorgonException(GorgonResult.CannotRead, string.Format(Resources.GORIMG_ERR_COMPRESSED_FILE, formatInfo.Format));
                }

                _log.Print($"Loading image '{workFile.FullPath}'...", LoggingLevel.Simple);
                importImage = _compressor.Decompress(ref workFile, metaData);

                if (importImage == null)
                {
                    throw new GorgonException(GorgonResult.CannotRead, string.Format(Resources.GORIMG_ERR_COMPRESSED_FILE, formatInfo.Format));
                }

                _log.Print($"Loaded compressed ([{formatInfo.Format}]) image data as [{importImage.Format}]", LoggingLevel.Intermediate);
            }
            else
            {
                using (Stream workStream = workFile.OpenStream())
                {
                    importImage = importCodec.LoadFromStream(workStream);
                }
            }

            return(file, importImage, workFile, metaData.Format);
        }