/// <summary>Reads metadata from an <see cref="Stream"/>.</summary>
        /// <param name="stream">A stream from which the file data may be read.  The stream must be positioned at the beginning of the file's data.</param>
        /// <returns>A list of <see cref="Directory"/> instances containing the various types of metadata found within the file's data.</returns>
        /// <exception cref="ImageProcessingException">The file type is unknown, or processing errors occurred.</exception>
        /// <exception cref="System.IO.IOException"/>
        public static DirectoryList ReadMetadata(Stream stream)
        {
            var fileType = FileTypeDetector.DetectFileType(stream);

            var directories = new List <Directory>();

            directories.AddRange(fileType switch
            {
                FileType.Arw => TiffMetadataReader.ReadMetadata(stream),
                FileType.Avi => AviMetadataReader.ReadMetadata(stream),
                FileType.Bmp => BmpMetadataReader.ReadMetadata(stream),
                FileType.Crx => QuickTimeMetadataReader.ReadMetadata(stream),
                FileType.Cr2 => TiffMetadataReader.ReadMetadata(stream),
                FileType.Eps => EpsMetadataReader.ReadMetadata(stream),
                FileType.Gif => GifMetadataReader.ReadMetadata(stream),
                FileType.Ico => IcoMetadataReader.ReadMetadata(stream),
                FileType.Jpeg => JpegMetadataReader.ReadMetadata(stream),
                FileType.Mp3 => Mp3MetadataReader.ReadMetadata(stream),
                FileType.Nef => TiffMetadataReader.ReadMetadata(stream),
                FileType.Netpbm => new Directory[] { NetpbmMetadataReader.ReadMetadata(stream) },
                FileType.Orf => TiffMetadataReader.ReadMetadata(stream),
                FileType.Pcx => new Directory[] { PcxMetadataReader.ReadMetadata(stream) },
                FileType.Png => PngMetadataReader.ReadMetadata(stream),
                FileType.Psd => PsdMetadataReader.ReadMetadata(stream),
                FileType.QuickTime => QuickTimeMetadataReader.ReadMetadata(stream),
                FileType.Raf => RafMetadataReader.ReadMetadata(stream),
                FileType.Rw2 => TiffMetadataReader.ReadMetadata(stream),
                FileType.Tga => TgaMetadataReader.ReadMetadata(stream),
                FileType.Tiff => TiffMetadataReader.ReadMetadata(stream),
                FileType.Wav => WavMetadataReader.ReadMetadata(stream),
                FileType.WebP => WebPMetadataReader.ReadMetadata(stream),

                FileType.Unknown => throw new ImageProcessingException("File format could not be determined"),
                _ => Enumerable.Empty <Directory>()
            });
Пример #2
0
        public virtual Task <Size> GetImageSize(byte[]?bytes)
        {
            return(Task.Run(() =>
            {
                if (bytes == null)
                {
                    return Size.Empty;
                }
                try
                {
                    using (var stream = new MemoryStream(bytes))
                    {
                        var type = FileTypeDetector.DetectFileType(stream);
                        stream.Seek(0, SeekOrigin.Begin);
                        switch (type)
                        {
                        case FileType.Jpeg:
                            var jpeg = JpegMetadataReader.ReadMetadata(stream);
                            var jpegdir = jpeg.OfType <JpegDirectory>().FirstOrDefault();
                            return new Size(jpegdir.GetImageWidth(), jpegdir.GetImageHeight());

                        case FileType.Png:
                            var png = PngMetadataReader.ReadMetadata(stream);
                            var pngdir = png.OfType <PngDirectory>().FirstOrDefault();
                            return new Size(pngdir.GetInt32(PngDirectory.TagImageWidth), pngdir.GetInt32(PngDirectory.TagImageHeight));

                        case FileType.Gif:
                            var gif = GifMetadataReader.ReadMetadata(stream);
                            var gifdir = gif.OfType <GifHeaderDirectory>().FirstOrDefault();
                            return new Size(gifdir.GetInt32(GifHeaderDirectory.TagImageWidth), gifdir.GetInt32(GifHeaderDirectory.TagImageHeight));

                        case FileType.WebP:
                            var webp = WebPMetadataReader.ReadMetadata(stream);
                            var webpdir = webp.OfType <WebPDirectory>().FirstOrDefault();
                            return new Size(webpdir.GetInt32(WebPDirectory.TagImageWidth), webpdir.GetInt32(WebPDirectory.TagImageHeight));

                        case FileType.Bmp:
                            var bmp = BmpMetadataReader.ReadMetadata(stream);
                            var bmpdir = bmp.OfType <BmpHeaderDirectory>().FirstOrDefault();
                            return new Size(bmpdir.GetInt32(BmpHeaderDirectory.TagImageWidth), bmpdir.GetInt32(BmpHeaderDirectory.TagImageHeight));

                        default:
                            return Size.Empty;
                        }
                    }
                }
                catch (Exception)
                {
                    //Crashes.TrackError(e);
                    // No need to track this
                }
                return Size.Empty;
            }));
        }
Пример #3
0
        public static IEnumerable <MetadataExtractor.Directory> GetFileMetaData(string fullpath)
        {
            var type = GetType(fullpath);
            IEnumerable <MetadataExtractor.Directory> data = type switch
            {
                FileTypeEnum.JPG => JpegMetadataReader.ReadMetadata(fullpath),
                FileTypeEnum.CR2 => TiffMetadataReader.ReadMetadata(fullpath),
                FileTypeEnum.PNG => PngMetadataReader.ReadMetadata(fullpath),
                _ => throw new Exception(),// is unreachable
            };

            return(data.ToList());
        }
Пример #4
0
        ReadMetadata([NotNull] Stream stream)
        {
            var fileType = FileTypeDetector.DetectFileType(stream);

            switch (fileType)
            {
            case FileType.Jpeg:
                return(JpegMetadataReader.ReadMetadata(stream));

            case FileType.Tiff:
            case FileType.Arw:
            case FileType.Cr2:
            case FileType.Nef:
            case FileType.Orf:
            case FileType.Rw2:
                return(TiffMetadataReader.ReadMetadata(stream));

            case FileType.Psd:
                return(PsdMetadataReader.ReadMetadata(stream));

            case FileType.Png:
                return(PngMetadataReader.ReadMetadata(stream));

            case FileType.Bmp:
                return(new Directory[] { BmpMetadataReader.ReadMetadata(stream) });

            case FileType.Gif:
                return(GifMetadataReader.ReadMetadata(stream));

            case FileType.Ico:
                return(IcoMetadataReader.ReadMetadata(stream));

            case FileType.Pcx:
                return(new Directory[] { PcxMetadataReader.ReadMetadata(stream) });

            case FileType.Riff:
                return(WebPMetadataReader.ReadMetadata(stream));

            case FileType.Raf:
                return(RafMetadataReader.ReadMetadata(stream));

            case FileType.QuickTime:
                return(QuickTimeMetadataReader.ReadMetadata(stream));

            case FileType.Netpbm:
                return(new Directory[] { NetpbmMetadataReader.ReadMetadata(stream) });
            }

            throw new ImageProcessingException("File format is not supported");
        }
Пример #5
0
        /// <summary>
        /// メタデータの構造データを取得する
        /// </summary>
        /// <param name="filePath">ファイルパス</param>
        /// <param name="fileExtensionType">ファイルの拡張子</param>
        /// <returns>メタデータの構造データ</returns>
        private static IEnumerable <Directory> GetMetadataDirectories(string filePath, FileExtensionType fileExtensionType)
        {
            var directories = fileExtensionType switch
            {
                FileExtensionType.Jpeg => JpegMetadataReader.ReadMetadata(filePath),
                FileExtensionType.Bmp => BmpMetadataReader.ReadMetadata(filePath),
                FileExtensionType.Png => PngMetadataReader.ReadMetadata(filePath),
                FileExtensionType.Gif => GifMetadataReader.ReadMetadata(filePath),
                FileExtensionType.Tiff or FileExtensionType.Dng or FileExtensionType.Nef => TiffMetadataReader.ReadMetadata(filePath),
                FileExtensionType.Unknown or _ => throw new ArgumentOutOfRangeException(nameof(fileExtensionType)),
            };

            return(directories);
        }
        public static Com.Drew.Metadata.Metadata ReadMetadata([NotNull] InputStream inputStream)
        {
            BufferedInputStream bufferedInputStream = inputStream is BufferedInputStream ? (BufferedInputStream)inputStream : new BufferedInputStream(inputStream);
            FileType            fileType            = FileTypeDetector.DetectFileType(bufferedInputStream) ?? FileType.Unknown;

            if (fileType == FileType.Jpeg)
            {
                return(JpegMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Tiff || fileType == FileType.Arw || fileType == FileType.Cr2 || fileType == FileType.Nef || fileType == FileType.Orf || fileType == FileType.Rw2)
            {
                return(TiffMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Psd)
            {
                return(PsdMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Png)
            {
                return(PngMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Bmp)
            {
                return(BmpMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Gif)
            {
                return(GifMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Ico)
            {
                return(IcoMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Pcx)
            {
                return(PcxMetadataReader.ReadMetadata(bufferedInputStream));
            }
            if (fileType == FileType.Riff)
            {
                return(WebpMetadataReader.ReadMetadata(bufferedInputStream));
            }
            throw new ImageProcessingException("File format is not supported");
        }
        public static Com.Drew.Metadata.Metadata ReadMetadata(InputStream inputStream)
        {
            InputStream bufferedInputStream = inputStream is BufferedInputStream ? inputStream : new BufferedInputStream(inputStream);
            int         magicNumber         = PeekMagicNumber(bufferedInputStream);

            if (magicNumber == -1)
            {
                throw new ImageProcessingException("Could not determine file's magic number.");
            }
            // This covers all JPEG files
            if ((magicNumber & JpegFileMagicNumber) == JpegFileMagicNumber)
            {
                return(JpegMetadataReader.ReadMetadata(bufferedInputStream));
            }
            // This covers all TIFF and camera RAW files
            if (magicNumber == IntelTiffMagicNumber || magicNumber == MotorolaTiffMagicNumber)
            {
                return(TiffMetadataReader.ReadMetadata(bufferedInputStream));
            }
            // This covers PSD files
            // TODO we should really check all 4 bytes of the PSD magic number
            if (magicNumber == PsdMagicNumber)
            {
                return(PsdMetadataReader.ReadMetadata(bufferedInputStream));
            }
            // This covers BMP files
            if (magicNumber == PngMagicNumber)
            {
                return(PngMetadataReader.ReadMetadata(bufferedInputStream));
            }
            // This covers BMP files
            if (magicNumber == BmpMagicNumber)
            {
                return(BmpMetadataReader.ReadMetadata(bufferedInputStream));
            }
            // This covers GIF files
            if (magicNumber == GifMagicNumber)
            {
                return(GifMetadataReader.ReadMetadata(bufferedInputStream));
            }
            throw new ImageProcessingException("File format is not supported");
        }
Пример #8
0
        /// <summary>Reads metadata from an <see cref="Stream"/>.</summary>
        /// <param name="stream">A stream from which the file data may be read.  The stream must be positioned at the beginning of the file's data.</param>
        /// <returns>A list of <see cref="Directory"/> instances containing the various types of metadata found within the file's data.</returns>
        /// <exception cref="ImageProcessingException">The file type is unknown, or processing errors occurred.</exception>
        /// <exception cref="System.IO.IOException"/>
        public static DirectoryList ReadMetadata(Stream stream)
        {
            var fileType = FileTypeDetector.DetectFileType(stream);

            var fileTypeDirectory = new FileTypeDirectory(fileType);

            switch (fileType)
            {
            case FileType.Jpeg:
                return(Append(JpegMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Tiff:
            case FileType.Arw:
            case FileType.Cr2:
            case FileType.Nef:
            case FileType.Orf:
            case FileType.Rw2:
                return(Append(TiffMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Psd:
                return(Append(PsdMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Png:
                return(Append(PngMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Bmp:
                return(Append(BmpMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Gif:
                return(Append(GifMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Ico:
                return(Append(IcoMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Pcx:
                return(new Directory[] { PcxMetadataReader.ReadMetadata(stream), fileTypeDirectory });

            case FileType.WebP:
                return(Append(WebPMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Avi:
                return(Append(AviMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Wav:
                return(Append(WavMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Raf:
                return(Append(RafMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.QuickTime:
            case FileType.Crx:
                return(Append(QuickTimeMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Netpbm:
                return(new Directory[] { NetpbmMetadataReader.ReadMetadata(stream), fileTypeDirectory });

            case FileType.Unknown:
                throw new ImageProcessingException("File format could not be determined");

            case FileType.Riff:
            case FileType.Crw:
            default:
                throw new ImageProcessingException("File format is not supported");
            }
Пример #9
0
 /// <exception cref="PngProcessingException"/>
 /// <exception cref="System.IO.IOException"/>
 private static IEnumerable <Directory> ProcessFile(string filePath)
 {
     using var stream = TestDataUtil.OpenRead(filePath);
     return(PngMetadataReader.ReadMetadata(stream));
 }
Пример #10
0
 /// <summary>
 /// Extrait du fichier les métadonnées correspondant au type de la clé et les encapsules dans un container
 /// </summary>
 /// <typeparam name="TKey">le type de clé</typeparam>
 /// <param name="file">le fichier d'où l'on cherche à extraire les métadonnées</param>
 /// <param name="ctsToken">le token d'annulation</param>
 /// <returns>le conteneur de métadonnées</returns>
 public static async Task <IMetadataContainer <TKey> > ToMetadataContainerAsync <TKey>(this FileInfo file, CancellationToken ctsToken) where TKey : Enum
 {
     return(new MetadataContainer <TKey>(await PngMetadataReader.GetMetadataAsync(file.FullName, ctsToken)));
 }
 private static IReadOnlyList <Directory> ProcessFile([NotNull] string filePath)
 {
     using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         return(PngMetadataReader.ReadMetadata(stream));
 }
Пример #12
0
    protected override async Task <Metadata> ReadMetadata(
        Metadata metadata,
        MetadataReaderFileInfo fileInfo,
        MetadataReaderOption option)
    {
        var directories = await _fileService.ReadFileStream(fileInfo.FileHandle, readStream =>
        {
            if (fileInfo.MimeType == MimeType.image_png)
            {
                return(ValueTask.FromResult(PngMetadataReader.ReadMetadata(readStream)));
            }

            if (fileInfo.MimeType == MimeType.image_jpeg)
            {
                return(ValueTask.FromResult(JpegMetadataReader.ReadMetadata(readStream)));
            }

            if (fileInfo.MimeType == MimeType.image_bmp)
            {
                return(ValueTask.FromResult(BmpMetadataReader.ReadMetadata(readStream)));
            }

            if (fileInfo.MimeType == MimeType.image_gif)
            {
                return(ValueTask.FromResult(GifMetadataReader.ReadMetadata(readStream)));
            }

            if (fileInfo.MimeType == MimeType.image_webp)
            {
                return(ValueTask.FromResult(WebPMetadataReader.ReadMetadata(readStream)));
            }

            return(ValueTask.FromResult(MetadataExtractor.ImageMetadataReader.ReadMetadata(readStream)));
        });

        foreach (var directory in directories)
        {
            if (directory is JpegDirectory jpegDirectory)
            {
                ParseJpegDirectory(jpegDirectory, metadata);
            }
            else if (directory is WebPDirectory webPDirectory)
            {
                ParseWebPDirectory(webPDirectory, metadata);
            }
            else if (directory is ExifIfd0Directory exifIfd0Directory)
            {
                ParseExifDirectory(exifIfd0Directory, metadata);
            }
            else if (directory is ExifSubIfdDirectory exifSubIfdDirectory)
            {
                ParseExifDirectory(exifSubIfdDirectory, metadata);
            }
            else if (directory is PngDirectory pngDirectory)
            {
                ParsePngDirectory(pngDirectory, metadata);
            }
            else if (directory is XmpDirectory xmpDirectory)
            {
                ParseXmpDirectory(xmpDirectory, metadata);
            }
            else if (directory is CanonMakernoteDirectory canonMakernoteDirectory)
            {
                ParseCanonMakernoteDirectory(canonMakernoteDirectory, metadata);
            }
        }

        return(metadata);
    }