mirror of https://github.com/SixLabors/ImageSharp
6 changed files with 194 additions and 26 deletions
@ -0,0 +1,53 @@ |
|||||
|
// <copyright file="WhiteIsZeroTiffColor.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats.Tiff |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
using ImageSharp; |
||||
|
using ImageSharp.PixelFormats; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation (for all bit depths).
|
||||
|
/// </summary>
|
||||
|
internal static class WhiteIsZeroTiffColor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Decodes pixel data using the current photometric interpretation.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
|
/// <param name="data">The buffer to read image data from.</param>
|
||||
|
/// <param name="bitsPerSample">The number of bits per sample for each pixel.</param>
|
||||
|
/// <param name="pixels">The image buffer to write pixels to.</param>
|
||||
|
/// <param name="left">The x-coordinate of the left-hand side of the image block.</param>
|
||||
|
/// <param name="top">The y-coordinate of the top of the image block.</param>
|
||||
|
/// <param name="width">The width of the image block.</param>
|
||||
|
/// <param name="height">The height of the image block.</param>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static void Decode<TPixel>(byte[] data, uint[] bitsPerSample, PixelAccessor<TPixel> pixels, int left, int top, int width, int height) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
TPixel color = default(TPixel); |
||||
|
|
||||
|
BitReader bitReader = new BitReader(data); |
||||
|
float factor = (float)Math.Pow(2, bitsPerSample[0]) - 1.0f; |
||||
|
|
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
for (int x = left; x < left + width; x++) |
||||
|
{ |
||||
|
int value = bitReader.ReadBits(bitsPerSample[0]); |
||||
|
float intensity = 1.0f - (((float)value) / factor); |
||||
|
color.PackFromVector4(new Vector4(intensity, intensity, intensity, 1.0f)); |
||||
|
pixels[x, y] = color; |
||||
|
} |
||||
|
|
||||
|
bitReader.NextRow(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// <copyright file="BitReader.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
namespace ImageSharp.Formats.Tiff |
||||
|
{ |
||||
|
using System.IO; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Utility class to read a sequence of bits from an array
|
||||
|
/// </summary>
|
||||
|
internal class BitReader |
||||
|
{ |
||||
|
private readonly byte[] array; |
||||
|
private int offset; |
||||
|
private int bitOffset; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BitReader" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="array">The array to read data from.</param>
|
||||
|
public BitReader(byte[] array) |
||||
|
{ |
||||
|
this.array = array; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Reads the specified number of bits from the array.
|
||||
|
/// </summary>
|
||||
|
/// <param name="bits">The number of bits to read.</param>
|
||||
|
/// <returns>The value read from the array.</returns>
|
||||
|
public int ReadBits(uint bits) |
||||
|
{ |
||||
|
int value = 0; |
||||
|
|
||||
|
for (uint i = 0; i < bits; i++) |
||||
|
{ |
||||
|
int bit = (this.array[this.offset] >> (7 - this.bitOffset)) & 0x01; |
||||
|
value = (value << 1) | bit; |
||||
|
|
||||
|
this.bitOffset++; |
||||
|
|
||||
|
if (this.bitOffset == 8) |
||||
|
{ |
||||
|
this.bitOffset = 0; |
||||
|
this.offset++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Moves the reader to the next row of byte-aligned data.
|
||||
|
/// </summary>
|
||||
|
public void NextRow() |
||||
|
{ |
||||
|
if (this.bitOffset > 0) |
||||
|
{ |
||||
|
this.bitOffset = 0; |
||||
|
this.offset++; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue