public async Task InitializeAsync(Uri manifestUri, CancellationToken cancellationToken)
        {
            if (manifestUri == null)
            {
                throw new ArgumentNullException(nameof(manifestUri));
            }

            // Parse DZI file.
            _manifest = await InitializeManifestAsync(manifestUri).ConfigureAwait(false);

            // Calculate Deep Zoom layers
            _layers = DeepZoomHelper.CalculateDeepZoomLayers(_manifest.Width, _manifest.Height);

            // Get output file ready
            _stream     = new FileStream(_outputFile, FileMode.Create, FileAccess.ReadWrite);
            _fileWriter = await TiffFileWriter.OpenAsync(_stream, leaveOpen : true, useBigTiff : UseBigTiff).ConfigureAwait(false);
        }
Exemple #2
0
        protected override async Task <DeepZoomManifest> InitializeManifestAsync(Uri manifestUri)
        {
            string filename = GetFileNameFromUri(manifestUri);

            _baseUri  = new Uri(manifestUri, "./");
            _baseName = Path.ChangeExtension(filename, null);

            if (!".dzi".Equals(Path.GetExtension(filename), StringComparison.OrdinalIgnoreCase) &&
                !".xml".Equals(Path.GetExtension(filename), StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(nameof(manifestUri), $"{filename} is not a dzi file.");
            }

            using (var ms = new MemoryStream())
            {
                await GetUriContentAsync(manifestUri, ms).ConfigureAwait(false);

                ms.Seek(0, SeekOrigin.Begin);
                return(await DeepZoomManifest.ParseAsync(ms).ConfigureAwait(false));
            }
        }
        public static async Task <DeepZoomManifest> ParseAsync(Stream stream)
        {
            DeepZoomManifest manifest = new DeepZoomManifest();
            string           tmp;

            using (var reader = XmlReader.Create(stream, new XmlReaderSettings {
                Async = true
            }))
            {
                while (await reader.ReadAsync().ConfigureAwait(false))
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "Image")
                        {
                            manifest.Format = reader.GetAttribute("Format");
                            tmp             = reader.GetAttribute("TileSize");
                            int.TryParse(tmp, out int tileSize);
                            manifest.TileSize = tileSize;
                            tmp = reader.GetAttribute("Overlap");
                            int.TryParse(tmp, out int overlap);
                            manifest.Overlap = overlap;
                        }
                        else if (reader.Name == "Size")
                        {
                            tmp = reader.GetAttribute("Width");
                            int.TryParse(tmp, out int width);
                            manifest.Width = width;
                            tmp            = reader.GetAttribute("Height");
                            int.TryParse(tmp, out int height);
                            manifest.Height = height;
                        }
                    }
                }
            }
            return(manifest);
        }
Exemple #4
0
        protected override async Task <DeepZoomManifest> InitializeManifestAsync(Uri manifestUri)
        {
            string filename = GetFileNameFromUri(manifestUri);

            _baseUri  = new Uri(manifestUri, "./");
            _baseName = Path.ChangeExtension(filename, null);

            // Deep Zoom format
            if (".dzi".Equals(Path.GetExtension(filename), StringComparison.OrdinalIgnoreCase) ||
                ".xml".Equals(Path.GetExtension(filename), StringComparison.OrdinalIgnoreCase))
            {
                using (var ms = new MemoryStream())
                {
                    await GetUriContentAsync(manifestUri, ms).ConfigureAwait(false);

                    ms.Seek(0, SeekOrigin.Begin);
                    return(await DeepZoomManifest.ParseAsync(ms).ConfigureAwait(false));
                }
            }

            // local TIFF file
            if ("file".Equals(manifestUri.Scheme, StringComparison.OrdinalIgnoreCase))
            {
                TiffFileReader?tiff = null;
                try
                {
                    tiff = await TiffFileReader.OpenAsync(manifestUri.LocalPath).ConfigureAwait(false);

                    TiffImageDecoder decoder = await tiff.CreateImageDecoderAsync().ConfigureAwait(false);

                    _tiff    = tiff;
                    _decoder = decoder;
                    tiff     = null;

                    return(new DeepZoomManifest {
                        Format = "jpeg", Width = decoder.Width, Height = decoder.Height, TileSize = 256, Overlap = 0
                    });
                }
                catch (InvalidDataException)
                {
                    // Do nothing
                }
                finally
                {
                    if (!(tiff is null))
                    {
                        await tiff.DisposeAsync().ConfigureAwait(false);
                    }
                }
            }

            // Other images
            if ("file".Equals(manifestUri.Scheme, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    _image = Image.Load <Rgb24>(manifestUri.LocalPath);

                    return(new DeepZoomManifest {
                        Format = "jpeg", Width = _image.Width, Height = _image.Height, TileSize = 256, Overlap = 0
                    });
                }
                catch (Exception)
                {
                    // Do nothing
                }
            }


            throw new ArgumentException(nameof(manifestUri), $"{filename} is not a dzi file.");
        }