/// <summary> /// Read values of type <see cref="TiffFieldType.IFD"/> 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 <TiffStreamOffset> > ReadIFDFieldAsync(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 <TiffStreamOffset> >(TiffValueCollection.Empty <TiffStreamOffset>())); } return(Reader.ReadIFDFieldAsync(entry, sizeLimit, cancellationToken: cancellationToken)); }
/// <summary> /// Read values of type <see cref="TiffFieldType.SLong"/> from the specified tag. /// </summary> /// <param name="tag">The tag to read.</param> /// <returns>The values read.</returns> public TiffValueCollection <int> ReadSLongField(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 <int>()); } return(Reader.ReadSLongField(entry)); }
/// <summary> /// Read values of type <see cref="TiffFieldType.SRational"/> 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 <TiffSRational> ReadSRationalField(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 <TiffSRational>()); } return(Reader.ReadSRationalField(entry, sizeLimit)); }
/// <summary> /// Read values of type <see cref="TiffFieldType.ASCII"/> from the specified tag. /// </summary> /// <param name="tag">The tag 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 <string> > ReadASCIIFieldAsync(TiffTag tag, 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 <string> >(TiffValueCollection.Empty <string>())); } return(Reader.ReadASCIIFieldAsync(entry, cancellationToken: cancellationToken)); }
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)); }