Exemplo n.º 1
0
        internal async Task <TiffStreamRegion> WriteAlignedValues(TiffValueCollection <TiffSRational> values, CancellationToken cancellationToken)
        {
            EnsureNotDisposed();
            cancellationToken.ThrowIfCancellationRequested();

            Debug.Assert(_writer != null);
            long position = await AlignToWordBoundaryAsync(cancellationToken).ConfigureAwait(false);

            const int ElementSize = 2 * sizeof(int);
            int       byteCount   = ElementSize * values.Count;

            byte[] buffer = ArrayPool <byte> .Shared.Rent(byteCount);

            try
            {
                MemoryMarshal.AsBytes(values.GetOrCreateArray().AsSpan()).CopyTo(buffer);
                await _writer !.WriteAsync(position, new ArraySegment <byte>(buffer, 0, byteCount), cancellationToken).ConfigureAwait(false);
                AdvancePosition(byteCount);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }

            return(new TiffStreamRegion(position, byteCount));
        }
        /// <summary>
        /// Read the values of <see cref="TiffTag.WhitePoint"/>.
        /// </summary>
        /// <param name="tagReader">The tag reader to use.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user want to stop the current task.</param>
        /// <returns>A <see cref="ValueTask{TiffValueCollection}"/> that completes when the value of the tag is read and return the read values.</returns>
        public static ValueTask <TiffRational[]> ReadWhitePointAsync(this TiffTagReader tagReader, CancellationToken cancellationToken = default)
        {
            ValueTask <TiffValueCollection <TiffRational> > valueTask = tagReader.ReadRationalFieldAsync(TiffTag.WhitePoint, sizeLimit: -1, cancellationToken);

            if (valueTask.IsCompletedSuccessfully)
            {
                TiffValueCollection <TiffRational> result = valueTask.GetAwaiter().GetResult();
                return(new ValueTask <TiffRational[]>(result.GetOrCreateArray()));
            }

            return(new ValueTask <TiffRational[]>(TransformValueTaskAsync(valueTask)));
Exemplo n.º 3
0
        /// <summary>
        /// Read the values of <see cref="TiffTag.JPEGTables"/>.
        /// </summary>
        /// <param name="tagReader">The tag reader to use.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user want to stop the current task.</param>
        /// <returns>A <see cref="ValueTask{TiffValueCollection}"/> that completes when the value of the tag is read and return the read values.</returns>
        public static ValueTask <byte[]> ReadJPEGTablesAsync(this TiffTagReader tagReader, CancellationToken cancellationToken = default)
        {
            ValueTask <TiffValueCollection <byte> > valueTask = tagReader.ReadByteFieldAsync(TiffTag.JPEGTables, sizeLimit: -1, cancellationToken);

            if (valueTask.IsCompletedSuccessfully)
            {
                TiffValueCollection <byte> result = valueTask.GetAwaiter().GetResult();
                return(new ValueTask <byte[]>(result.GetOrCreateArray()));
            }

            return(new ValueTask <byte[]>(TransformValueTaskAsync(valueTask)));
        /// <summary>
        /// Read the values of <see cref="TiffTag.HalftoneHints"/>.
        /// </summary>
        /// <param name="tagReader">The tag reader to use.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user want to stop the current task.</param>
        /// <returns>A <see cref="ValueTask{TiffValueCollection}"/> that completes when the value of the tag is read and return the read values.</returns>
        public static ValueTask <ushort[]> ReadHalftoneHintsAsync(this TiffTagReader tagReader, CancellationToken cancellationToken = default)
        {
            ValueTask <TiffValueCollection <ushort> > valueTask = tagReader.ReadShortFieldAsync(TiffTag.HalftoneHints, sizeLimit: -1, cancellationToken);

            if (valueTask.IsCompletedSuccessfully)
            {
                TiffValueCollection <ushort> result = valueTask.GetAwaiter().GetResult();
                return(new ValueTask <ushort[]>(result.GetOrCreateArray()));
            }

            return(new ValueTask <ushort[]>(TransformValueTaskAsync(valueTask)));
            static async Task <TiffRational[]> TransformValueTaskAsync(ValueTask <TiffValueCollection <TiffRational> > valueTask)
            {
                TiffValueCollection <TiffRational> result = await valueTask.ConfigureAwait(false);

                return(result.GetOrCreateArray());
            }