-
-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1330 from IldarKhayrutdinov/tiff-codec_rebased
#12 Tiff codec
- Loading branch information
Showing
169 changed files
with
12,673 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/ImageSharp/Formats/Tiff/Compression/DeflateTiffCompression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using SixLabors.ImageSharp.Memory; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Tiff | ||
{ | ||
/// <summary> | ||
/// Class to handle cases where TIFF image data is compressed using Deflate compression. | ||
/// </summary> | ||
/// <remarks> | ||
/// Note that the 'OldDeflate' compression type is identical to the 'Deflate' compression type. | ||
/// </remarks> | ||
internal class DeflateTiffCompression : TiffBaseCompression | ||
{ | ||
public DeflateTiffCompression(MemoryAllocator allocator) | ||
: base(allocator) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Decompress(Stream stream, int byteCount, Span<byte> buffer) | ||
{ | ||
// Read the 'zlib' header information | ||
int cmf = stream.ReadByte(); | ||
int flag = stream.ReadByte(); | ||
|
||
if ((cmf & 0x0f) != 8) | ||
{ | ||
throw new Exception($"Bad compression method for ZLIB header: cmf={cmf}"); | ||
} | ||
|
||
// If the 'fdict' flag is set then we should skip the next four bytes | ||
bool fdict = (flag & 32) != 0; | ||
|
||
if (fdict) | ||
{ | ||
stream.ReadByte(); | ||
stream.ReadByte(); | ||
stream.ReadByte(); | ||
stream.ReadByte(); | ||
} | ||
|
||
// The subsequent data is the Deflate compressed data (except for the last four bytes of checksum) | ||
int headerLength = fdict ? 10 : 6; | ||
var subStream = new SubStream(stream, byteCount - headerLength); | ||
using (var deflateStream = new DeflateStream(subStream, CompressionMode.Decompress, true)) | ||
{ | ||
deflateStream.ReadFull(buffer); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/ImageSharp/Formats/Tiff/Compression/LzwTiffCompression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.IO; | ||
using SixLabors.ImageSharp.Memory; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Tiff | ||
{ | ||
/// <summary> | ||
/// Class to handle cases where TIFF image data is compressed using LZW compression. | ||
/// </summary> | ||
internal class LzwTiffCompression : TiffBaseCompression | ||
{ | ||
public LzwTiffCompression(MemoryAllocator allocator) | ||
: base(allocator) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Decompress(Stream stream, int byteCount, Span<byte> buffer) | ||
{ | ||
var subStream = new SubStream(stream, byteCount); | ||
using (var decoder = new TiffLzwDecoder(subStream)) | ||
{ | ||
decoder.DecodePixels(buffer.Length, 8, buffer); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/ImageSharp/Formats/Tiff/Compression/NoneTiffCompression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.IO; | ||
using SixLabors.ImageSharp.Memory; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Tiff | ||
{ | ||
/// <summary> | ||
/// Class to handle cases where TIFF image data is not compressed. | ||
/// </summary> | ||
internal class NoneTiffCompression : TiffBaseCompression | ||
{ | ||
public NoneTiffCompression(MemoryAllocator allocator) | ||
: base(allocator) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Decompress(Stream stream, int byteCount, Span<byte> buffer) | ||
{ | ||
stream.ReadFull(buffer, byteCount); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/ImageSharp/Formats/Tiff/Compression/PackBitsTiffCompression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
using SixLabors.ImageSharp.Memory; | ||
|
||
namespace SixLabors.ImageSharp.Formats.Tiff | ||
{ | ||
/// <summary> | ||
/// Class to handle cases where TIFF image data is compressed using PackBits compression. | ||
/// </summary> | ||
internal class PackBitsTiffCompression : TiffBaseCompression | ||
{ | ||
public PackBitsTiffCompression(MemoryAllocator allocator) | ||
: base(allocator) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Decompress(Stream stream, int byteCount, Span<byte> buffer) | ||
{ | ||
using IMemoryOwner<byte> compressedDataMemory = this.Allocator.Allocate<byte>(byteCount); | ||
|
||
Span<byte> compressedData = compressedDataMemory.GetSpan(); | ||
|
||
stream.ReadFull(compressedData, byteCount); | ||
int compressedOffset = 0; | ||
int decompressedOffset = 0; | ||
|
||
while (compressedOffset < byteCount) | ||
{ | ||
byte headerByte = compressedData[compressedOffset]; | ||
|
||
if (headerByte <= (byte)127) | ||
{ | ||
int literalOffset = compressedOffset + 1; | ||
int literalLength = compressedData[compressedOffset] + 1; | ||
|
||
compressedData.Slice(literalOffset, literalLength).CopyTo(buffer.Slice(decompressedOffset)); | ||
|
||
compressedOffset += literalLength + 1; | ||
decompressedOffset += literalLength; | ||
} | ||
else if (headerByte == (byte)0x80) | ||
{ | ||
compressedOffset += 1; | ||
} | ||
else | ||
{ | ||
byte repeatData = compressedData[compressedOffset + 1]; | ||
int repeatLength = 257 - headerByte; | ||
|
||
ArrayCopyRepeat(repeatData, buffer, decompressedOffset, repeatLength); | ||
|
||
compressedOffset += 2; | ||
decompressedOffset += repeatLength; | ||
} | ||
} | ||
} | ||
|
||
private static void ArrayCopyRepeat(byte value, Span<byte> destinationArray, int destinationIndex, int length) | ||
{ | ||
for (int i = 0; i < length; i++) | ||
{ | ||
destinationArray[i + destinationIndex] = value; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.