/// <summary> /// Read values of type <see cref="TiffFieldType.SLong"/> from the specified tag. /// </summary> /// <param name="tag">The tag to read.</param> /// <param name="sizeLimit">The maximum number of values to read.</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 values are read and return the read values.</returns> public ValueTask <TiffValueCollection <int> > ReadSLongFieldAsync(TiffTag tag, int sizeLimit, CancellationToken cancellationToken = default) { if (Reader is null) { throw new InvalidOperationException(); } if (ImageFileDirectory is null) { throw new InvalidOperationException(); } TiffImageFileDirectoryEntry entry = ImageFileDirectory.FindEntry(tag); if (entry.Tag == TiffTag.None) { return(new ValueTask <TiffValueCollection <int> >(TiffValueCollection.Empty <int>())); } return(Reader.ReadSLongFieldAsync(entry, sizeLimit, cancellationToken: cancellationToken)); }
/// <summary> /// Read values of type <see cref="TiffFieldType.Long"/> from the specified tag. /// </summary> /// <param name="tag">The tag to read.</param> /// <param name="sizeLimit">The maximum number of values to read.</param> /// <returns>The values read.</returns> public TiffValueCollection <uint> ReadLongField(TiffTag tag, int sizeLimit) { if (Reader is null) { throw new InvalidOperationException(); } if (ImageFileDirectory is null) { throw new InvalidOperationException(); } TiffImageFileDirectoryEntry entry = ImageFileDirectory.FindEntry(tag); if (entry.Tag == TiffTag.None) { return(TiffValueCollection.Empty <uint>()); } return(Reader.ReadLongField(entry, sizeLimit)); }
/// <summary> /// Read values of type <see cref="TiffFieldType.ASCII"/> from the specified tag. /// </summary> /// <param name="tag">The tag to read.</param> /// <returns>The values read.</returns> public TiffValueCollection <string> ReadASCIIField(TiffTag tag) { if (Reader is null) { throw new InvalidOperationException(); } if (ImageFileDirectory is null) { throw new InvalidOperationException(); } TiffImageFileDirectoryEntry entry = ImageFileDirectory.FindEntry(tag); if (entry.Tag == TiffTag.None) { return(TiffValueCollection.Empty <string>()); } return(Reader.ReadASCIIField(entry)); }
internal async Task <TiffStreamRegion> WriteAlignedValues(TiffValueCollection <string> values, CancellationToken cancellationToken) { EnsureNotDisposed(); cancellationToken.ThrowIfCancellationRequested(); Debug.Assert(_writer != null); long position = await AlignToWordBoundaryAsync(cancellationToken).ConfigureAwait(false); int maxByteCount = 0; foreach (string item in values) { maxByteCount = Math.Max(maxByteCount, Encoding.ASCII.GetMaxByteCount(item.Length)); } long offset = position; int bytesWritten = 0; byte[] buffer = ArrayPool <byte> .Shared.Rent(maxByteCount + 1); try { foreach (string item in values) { int length = Encoding.ASCII.GetBytes(item, 0, item.Length, buffer, 0); buffer[length] = 0; await _writer !.WriteAsync(offset, new ArraySegment <byte>(buffer, 0, length + 1), cancellationToken).ConfigureAwait(false); offset += length + 1; AdvancePosition(length + 1); bytesWritten += length + 1; } } finally { ArrayPool <byte> .Shared.Return(buffer); } return(new TiffStreamRegion(position, bytesWritten)); }
static async Task <uint?> TransformValueTaskAsync(ValueTask <TiffValueCollection <uint> > valueTask) { TiffValueCollection <uint> result = await valueTask.ConfigureAwait(false); return(result.IsEmpty ? default(uint?) : result.GetFirstOrDefault()); }
static async Task <TiffStreamOffset> TransformValueTaskAsync(ValueTask <TiffValueCollection <TiffStreamOffset> > valueTask) { TiffValueCollection <TiffStreamOffset> result = await valueTask.ConfigureAwait(false); return(result.GetFirstOrDefault()); }
private static TiffValueCollection <string> ParseASCIIArray(ReadOnlySpan <byte> buffer) { int startIndex = 0; int count = buffer.Length; if (count == 0) { return(TiffValueCollection.Empty <string>()); } // Find the null terminator of the first string. int endIndex = startIndex + count - 1; int nullIndex = endIndex + 1; for (int i = startIndex; i <= endIndex; i++) { if (buffer[i] == 0) { nullIndex = i; break; } } string firstValue = EncodingASCIIGetString(buffer.Slice(startIndex, nullIndex - startIndex)); if (nullIndex >= endIndex) { return(TiffValueCollection.Single(firstValue)); } // Find the count of all strings. startIndex = nullIndex + 1; int strCount = 1; for (int i = startIndex; i <= endIndex; i++) { if (buffer[i] == 0) { strCount++; } } if (buffer[endIndex] != 0) { strCount++; } // Read all strings. string[] values = new string[strCount]; values[0] = firstValue; int index = 1; for (int i = startIndex; i <= endIndex; i++) { if (buffer[i] == 0) { values[index] = EncodingASCIIGetString(buffer.Slice(startIndex, i - startIndex)); index++; startIndex = i + 1; } } if (buffer[endIndex] != 0) { values[index] = EncodingASCIIGetString(buffer.Slice(startIndex, endIndex - startIndex + 1)); } return(TiffValueCollection.UnsafeWrap(values)); }
static async Task <TiffRational[]> TransformValueTaskAsync(ValueTask <TiffValueCollection <TiffRational> > valueTask) { TiffValueCollection <TiffRational> result = await valueTask.ConfigureAwait(false); return(result.GetOrCreateArray()); }
static async Task <TiffInkSet> TransformValueTaskAsync(ValueTask <TiffValueCollection <ushort> > valueTask) { TiffValueCollection <ushort> result = await valueTask.ConfigureAwait(false); return(result.IsEmpty ? TiffInkSet.CMYK : (TiffInkSet)result.GetFirstOrDefault()); }
static async Task <TiffT4Options> TransformValueTaskAsync(ValueTask <TiffValueCollection <uint> > valueTask) { TiffValueCollection <uint> result = await valueTask.ConfigureAwait(false); return(result.IsEmpty ? TiffT4Options.None : (TiffT4Options)result.GetFirstOrDefault()); }
static async Task <TiffPredictor> TransformValueTaskAsync(ValueTask <TiffValueCollection <ushort> > valueTask) { TiffValueCollection <ushort> result = await valueTask.ConfigureAwait(false); return(result.IsEmpty ? TiffPredictor.None : (TiffPredictor)result.GetFirstOrDefault()); }