### Install TiffLibrary Package Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Use the .NET CLI to add the TiffLibrary package to your project. ```bash dotnet add package TiffLibrary --version ``` -------------------------------- ### Decoding an Entire Image Region Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/using-image-decoder-apis.md A basic example demonstrating how to decode the full dimensions of an image into a pixel buffer writer. ```csharp TiffImageDecoder decoder = GetDecoder(); ITiffPixelBufferWriter writer = GetWriter(decoder.Width, decoder.Height); await decoder.DecodeAsync(offset: default, readSize: new TiffSize(decoder.Width, decoder.Height), destinationOffset: default, writer: writer); ``` -------------------------------- ### Creating and Cropping TiffPixelBuffer Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Demonstrates initializing a TiffPixelBuffer and applying various cropping operations to define sub-regions. ```csharp ITiffPixelBuffer pixelBuffer = new TiffMemoryPixelBuffer(new TiffGray8[1000 * 1000], 1000, 1000, writable: true); TiffPixelBuffer structBuffer; // Create the struct using its constructor structBuffer = new TiffPixelBuffer(pixelBuffer); // Create the struct using extension methods on ITiffPixelBuffer structBuffer = pixelBuffer.AsPixelBuffer(); // Create the struct using Crop extension methods on ITiffPixelBuffer structBuffer = pixelBuffer.Crop(new TiffPoint(200, 100)); // A 800x900 region skipping the first 200 columns and 100 rows in the orinigal buffer structBuffer = pixelBuffer.Crop(new TiffPoint(200, 100), new TiffSize(400, 300)); // A 400x300 region skipping the first 200 columns and 100 rows in the orinigal buffer // You can even call Crop on the struct to further limit the area. structBuffer = structBuffer.Crop(new TiffPoint(50, 50)); structBuffer = structBuffer.Crop(new TiffPoint(50, 50), new TiffSize(100, 100)); ``` -------------------------------- ### Import TiffLibrary Namespace Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Include the necessary namespace in your C# source files. ```csharp using TiffLibrary; ``` -------------------------------- ### Filling a Pixel Buffer with Black Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Demonstrates iterating through rows of an ITiffPixelBufferWriter to fill the buffer with a default black pixel value. ```csharp void FillBlack(ITiffPixelBufferWriter writer) { int width = writer.Width; int height = writer.Height; TiffGray8 blackPixel = default; for (int i = 0; i < height; i++) { using TiffPixelSpanHandle handle = writer.GetRowSpan(i, 0, width); handle.GetSpan().Fill(blackPixel); } } ``` -------------------------------- ### Configure TiffLibrary for ImageSharp Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Register the TiffConfigurationModule during application startup to enable TIFF support. ```csharp SixLabors.ImageSharp.Configuration.Default.Configure(new TiffLibrary.ImageSharpAdapter.TiffConfigurationModule()); ``` -------------------------------- ### Run TiffJpegExtractor via CLI Source: https://github.com/yigolden-oss/tifflibrary/blob/main/tools/TiffJpegExtractor/README.md Executes the extraction tool using the .NET CLI, specifying input and output paths. ```bash dotnet run -- C:\Data\your-input.tif --output C:\Data\some-empty-directory ``` -------------------------------- ### Implement Wrapper for ImageSharp Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Create a custom wrapper to bridge ImageSharp buffers with TiffLibrary by reinterpreting pixel types. ```csharp using System; using System.Runtime.InteropServices; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using TiffLibrary.PixelFormats; namespace TiffLibrary.Tests { class ImageSharpPixelBuffer : ITiffPixelBuffer { private readonly Image _image; public ImageSharpPixelBuffer(Image image) { _image = image; } public int Width => _image.Width; public int Height => _image.Height; public Span GetSpan() { return MemoryMarshal.Cast(_image.GetPixelSpan()); } } } ``` -------------------------------- ### Reading a Sub-region with ITiffPixelBufferReader Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Demonstrates how to read a specific sub-region from a pixel buffer reader into a destination buffer using an adapter. ```csharp public async Task ReadRegionAsync(ITiffPixelBufferReader reader, ITiffPixelBuffer destination) { // Prepare the writer var writer = new TiffPixelBufferWriterAdapter(destination); // The offset of the region var offset = new TiffPoint(512, 512); // Read pixels await reader.ReadAsync(offset, writer.AsPixelBufferWriter(), CancellationToken.None); } ``` -------------------------------- ### Create TiffMemoryPixelBuffer Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Instantiate a pixel buffer using a Memory object to wrap pixel data. ```csharp Memory pixels = new TiffRgba32[256 * 256]; var pixelBuffer = new TiffMemoryPixelBuffer(pixels, 256, 256, writable: true); ``` -------------------------------- ### Open TIFF Files for Reading Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Open TIFF files using file paths, streams, or byte buffers. ```csharp using TiffFileReader tiff = await TiffFileReader.OpenAsync(@"C:\Data\test.tif"); ``` ```csharp Stream stream = GetStream(); using TiffFileReader tiff = await TiffFileReader.OpenAsync(stream, leaveOpen: false); ``` ```csharp byte[] buffer = GetBuffer(); using TiffFileReader tiff = await TiffFileReader.OpenAsync(buffer, 0, buffer.Length); ``` -------------------------------- ### Decode Image from IFD Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Shows how to decode an image from an IFD into a pixel buffer. ```csharp using TiffLibrary.PixelFormats; using TiffFileReader tiff = await TiffFileReader.OpenAsync(stream, leaveOpen: false); TiffImageFileDirectory ifd = await tiff.ReadImageFileDirectoryAsync(); // Create the decoder for the specified IFD. TiffImageDecoder decoder = await tiff.CreateImageDecoderAsync(ifd); // Create an array to store the pixels TiffRgba32[] pixels = new TiffRgba32[decoder.Width * decoder.Height]; TiffMemoryPixelBuffer pixelBuffer = new TiffMemoryPixelBuffer(pixels, decoder.Width, decoder.Height, writable: true); // Decode the image. Note that this call will create a `Stream` instance for reading pixel data if we are reading from file. It will be disposed before DecodeAsync completes. await decoder.DecodeAsync(pixelBuffer); ``` -------------------------------- ### Read Image File Directory (IFD) Entries Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Access IFD structures to retrieve image metadata and entry information. ```csharp using TiffFileReader tiff = await TiffFileReader.OpenAsync(@"C:\Data\test.tif"); // ReadImageFileDirectoryAsync method with no parameter will read the list of all the entries in the first IFD. TiffImageFileDirectory ifd = await tiff.ReadImageFileDirectoryAsync(); // Tests whether an entry with the specified tag exists bool exists = ifd.Contains(TiffTag.RowsPerStrip); // Gets information of an entry. TiffImageFileDirectoryEntry entry = ifd.FindEntry(TiffTag.RowsPerStrip); TiffFieldType fieldType = entry.Type; // The data type of the values. In this case, it can be TiffFieldType.Short. long count = entry.ValueCount; // Each entry contains an array of elements of the specific type. ValueCount is the length of the array. // To read actual values from the entry, TiffFieldReader should be used, which will be covered later in this documentation. ``` -------------------------------- ### TiffImageDecoder Extension Methods Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/using-image-decoder-apis.md A comprehensive list of extension methods provided by TiffImageDecoderExtensions for various buffer types including ITiffPixelBufferWriter, TiffPixelBufferWriter, ITiffPixelBuffer, and TiffPixelBuffer. ```csharp // APIs that works on ITiffPixelBufferWriter interface Task DecodeAsync(ITiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, ITiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, ITiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, ITiffPixelBufferWriter writer, CancellationToken cancellationToken); // This is the instance method on TiffImageDecoder // APIs that works on TiffPixelBufferWriter struct Task DecodeAsync(TiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPixelBufferWriter writer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, TiffPixelBufferWriter writer, CancellationToken cancellationToken); // APIs that works on ITiffPixelBuffer interface Task DecodeAsync(ITiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, ITiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, ITiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, ITiffPixelBuffer buffer, CancellationToken cancellationToken); // APIs that works on TiffPixelBuffer struct Task DecodeAsync(TiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPixelBuffer buffer, CancellationToken cancellationToken); Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, TiffPixelBuffer buffer, CancellationToken cancellationToken); ``` -------------------------------- ### Define Custom ITiffPixelBuffer Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Implement the ITiffPixelBuffer interface to support custom memory layouts or external image formats. ```csharp public interface ITiffPixelBuffer where TPixel : unmanaged { int Width { get; } int Height { get; } Span GetSpan(); } ``` -------------------------------- ### Encode RGBA Image to TIFF Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Configures a TiffImageEncoder with specific compression and tiling settings, then writes the pixel buffer to a new TIFF file. ```csharp TiffRgba32[] pixels = new TiffRgba32[1024 * 1024]; // Operations to fill pixels. ... // Build The image encoder. var builder = new TiffImageEncoderBuilder(); builder.PhotometricInterpretation = TiffPhotometricInterpretation.RGB; builder.EnableTransparencyForRgb = true; builder.IsTiled = true; builder.TileSize = new TiffSize(256, 256); builder.Compression = TiffCompression.Deflate; builder.ApplyPredictor = TiffPredictor.HorizontalDifferencing; TiffImageEncoder encoder = builder.Build(); // The encoder instance can be reused. // The pixel buffer to read from. var pixelBuffer = new TiffMemoryPixelBuffer(pixels, 1024, 1024, writable: false); // Opens the file for writing. using var writer = await TiffFileWriter.OpenAsync(@"C:\Data\test2.tif", useBigTiff: false); // Encode the image into an IFD. TiffStreamOffset ifdOffset; using (var ifdWriter = writer.CreateImageFileDirectory()) { await encoder.EncodeAsync(ifdWriter, pixelBuffer); // Write other properties here (eg, XResolution, YResolution) await ifdWriter.WriteTagAsync(TiffTag.XResolution, TiffValueCollection.Single(new TiffRational(96, 1))); await ifdWriter.WriteTagAsync(TiffTag.YResolution, TiffValueCollection.Single(new TiffRational(96, 1))); await ifdWriter.WriteTagAsync(TiffTag.ResolutionUnit, TiffValueCollection.Single((ushort)TiffResolutionUnit.Inch)); ifdOffset = await ifdWriter.FlushAsync(); } // Set this IFD to be the first IFD and flush TIFF file header. writer.SetFirstImageFileDirectoryOffset(ifdOffset); await writer.FlushAsync(); ``` -------------------------------- ### Enumerate All IFDs Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Iterate through all IFDs in a TIFF file using the linked-list structure. ```csharp using TiffFileReader tiff = await TiffFileReader.OpenAsync(@"C:\Data\test.tif"); TiffStreamOffset ifdOffset = tiff.FirstImageFileDirectoryOffset; while (!ifdOffset.IsZero) { // Read the list of entries in this IFD. TiffImageFileDirectory ifd = await tiff.ReadImageFileDirectoryAsync(ifdOffset); // Operations on the IFD goes here. // Move to the next IFD. ifdOffset = ifd.NextOffset; } ``` -------------------------------- ### Read IFD Entry Values Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Demonstrates reading field values from an IFD using TiffFieldReader and the TiffTagReader helper for well-known tags. ```csharp using TiffFileReader tiff = await TiffFileReader.OpenAsync(@"C:\Data\test.tif"); using TiffFieldReader fieldReader = await tiff.CreateFieldReaderAsync(); TiffImageFileDirectory ifd = await tiff.ReadImageFileDirectoryAsync(); // Read the values of BitsPerSample tag. TiffImageFileDirectoryEntry entry = ifd.FindEntry(TiffTag.BitsPerSample); TiffValueCollection bitsPerSample = await fieldReader.ReadShortFieldAsync(entry); int count = bitsPerSample.Count; ushort bitsPerSample0 = bitsPerSample.GetFirstOrDefault(); // or bitsPerSample[0] ushort bitsPerSample1 = bitsPerSample[1]; ushort bitsPerSample2 = bitsPerSample[2]; // Alternatively, you can use the TiffTagReader helper if you are trying to read well-known tags. TiffTagReader tagReader = new TiffTagReader(fieldReader, ifd); TiffValueCollection bitsPerSample = await tagReader.ReadBitsPerSampleAsync(); // TiffValueCollection is a array-like structure for optimizing memory usage. It is allocation-free if the value collection contains no or only one elements. ``` -------------------------------- ### ReadAsync Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Reads a region of pixels from the buffer into a specified destination writer. ```APIDOC ## ReadAsync ### Description Reads pixels from the current buffer starting at the specified offset and writes them to the provided destination writer. ### Method ValueTask ReadAsync(TiffPoint offset, TiffPixelBufferWriter destination, CancellationToken cancellationToken) ### Parameters - **offset** (TiffPoint) - The starting coordinate in the pixel buffer. - **destination** (TiffPixelBufferWriter) - The writer object where pixels will be copied. - **cancellationToken** (CancellationToken) - Token to monitor for cancellation requests. ### Returns - **ValueTask** - Represents the asynchronous operation of reading the pixels. ``` -------------------------------- ### Use Pixel Buffer with Decoder and Encoder Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Pass the pixel buffer to TiffImageDecoder or TiffImageEncoder to perform read or write operations. ```csharp // Decode an image into the pixel buffer. TiffImageDecoder decoder = GetDecoder(); await decoder.DecodeAsync(pixelBuffer); // Encode an image from the pixel buffer. TiffImageFileDirectoryWriter ifdWriter = GetWriter(); TiffImageEncoder encoder = GetEncoder(); await encoder.EncodeAsync(ifdWriter, pixelBuffer); ``` -------------------------------- ### Decode and Encode TIFF Files Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Use standard ImageSharp Load and Save methods to process TIFF files, optionally specifying TiffEncoder settings for custom compression and photometric interpretation. ```csharp using (Image image = Image.Load(@"C:\Data\test.tif")) { image.Save(@"C:\Data\test2.tif"); // Or image.Save(@"C:\Data\test2.tif", new TiffEncoder { PhotometricInterpretation = TiffPhotometricInterpretation.RGB, EnableTransparencyForRgb = true, Compression = TiffCompression.Deflate, DeflateCompressionLevel = TiffDeflateCompressionLevel.Default, Predictor = TiffPredictor.HorizontalDifferencing }); } ``` -------------------------------- ### ITiffPixelBufferReader Interface Definition Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md The interface definition for reading pixel buffers, requiring unmanaged pixel types and IDisposable implementation. ```csharp public interface ITiffPixelBufferReader : IDisposable where TPixel : unmanaged { ValueTask ReadAsync(TiffPoint offset, TiffPixelBufferWriter destination, CancellationToken cancellationToken); } ``` -------------------------------- ### Decode Sub-region of an Image Source: https://github.com/yigolden-oss/tifflibrary/blob/main/README.md Illustrates decoding a specific sub-region of an image to manage memory usage for large files. ```csharp TiffImageDecoder decoder = await tiff.CreateImageDecoderAsync(ifd); // Buffer for a sub-region. TiffRgba32[] pixels = new TiffRgba32[1024 * 1024]; TiffMemoryPixelBuffer pixelBuffer = new TiffMemoryPixelBuffer(pixels, 1024, 1024, writable: true); // Read a sub-region starting from (2048, 2048). await decoder.DecodeAsync(new TiffPoint(2048, 2048), pixelBuffer); ``` -------------------------------- ### ITiffPixelBufferWriter Interface Definition Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Defines the interface for a write-only 2D pixel buffer region, providing methods to access row and column spans. ```csharp public interface ITiffPixelBufferWriter : IDisposable where TPixel : unmanaged { TiffPixelSpanHandle GetRowSpan(int rowIndex, int start, int length); TiffPixelSpanHandle GetColumnSpan(int colIndex, int start, int length); } ``` -------------------------------- ### TiffImageDecoder Base Class Definition Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/using-image-decoder-apis.md The abstract base class defining the interface for image decoding, including dimensions and the asynchronous decode method. ```csharp public abstract class TiffImageDecoder { public abstract int Width { get; } public abstract int Height { get; } public abstract Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, ITiffPixelBufferWriter writer, CancellationToken cancellationToken = default) where TPixel : unmanaged; } ``` -------------------------------- ### TiffImageDecoder.DecodeAsync Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/using-image-decoder-apis.md Decodes an image or a sub-region of an image from the IFD and writes the pixels into the provided writer. ```APIDOC ## Method: DecodeAsync ### Description Decodes the image data from the IFD and writes it into the specified pixel buffer writer. Supports decoding full images or specific sub-regions by defining offsets and sizes. ### Signature `public abstract Task DecodeAsync(TiffPoint offset, TiffSize readSize, TiffPoint destinationOffset, ITiffPixelBufferWriter writer, CancellationToken cancellationToken = default) where TPixel : unmanaged;` ### Parameters - **offset** (TiffPoint) - The starting point of the region to read from the IFD. - **readSize** (TiffSize) - The dimensions of the region to read. - **destinationOffset** (TiffPoint) - The offset within the destination pixel buffer where writing begins. - **writer** (ITiffPixelBufferWriter) - The writer interface used to store the decoded pixels. - **cancellationToken** (CancellationToken) - Optional token to cancel the asynchronous operation. ``` -------------------------------- ### TiffPixelSpanHandle Class Definition Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Represents a temporary 1D pixel span buffer. Dispose must be called to flush changes to the underlying pixel buffer. ```csharp public abstract class TiffPixelSpanHandle : IDisposable where TPixel : unmanaged { public abstract Span GetSpan(); public abstract void Dispose(); } ``` -------------------------------- ### ITiffPixelBufferWriter.GetColumnSpan Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Retrieves a writable span handle for a specific column in the pixel buffer. ```APIDOC ## ITiffPixelBufferWriter.GetColumnSpan ### Description Returns a writable buffer for a specified 1-dimensional span of pixels within a column. ### Signature `TiffPixelSpanHandle GetColumnSpan(int colIndex, int start, int length)` ### Parameters - **colIndex** (int) - The index of the column to access. - **start** (int) - The starting row index within the column. - **length** (int) - The number of pixels to include in the span. ``` -------------------------------- ### TiffPixelSpanHandle.GetSpan Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Retrieves the underlying memory span for the pixel handle. ```APIDOC ## TiffPixelSpanHandle.GetSpan ### Description Returns the buffer to be filled for the current span of pixels. After filling, call Dispose() to flush the data. ``` -------------------------------- ### ITiffPixelBufferWriter.GetRowSpan Source: https://github.com/yigolden-oss/tifflibrary/blob/main/Documentation/working-with-pixel-buffer.md Retrieves a writable span handle for a specific row in the pixel buffer. ```APIDOC ## ITiffPixelBufferWriter.GetRowSpan ### Description Returns a writable buffer for a specified 1-dimensional span of pixels within a row. ### Signature `TiffPixelSpanHandle GetRowSpan(int rowIndex, int start, int length)` ### Parameters - **rowIndex** (int) - The index of the row to access. - **start** (int) - The starting column index within the row. - **length** (int) - The number of pixels to include in the span. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.