Skip to content

Commit aac0783

Browse files
Merge pull request #2062 from SixLabors/bp/extrasamples2
Add support for decoding Tiff images with associated alpha data
2 parents 7bd0e03 + 3ad23e8 commit aac0783

File tree

53 files changed

+481
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+481
-126
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ private void ReadRle<TPixel>(BmpCompression compression, Buffer2D<TPixel> pixels
323323
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4]));
324324
break;
325325
case RleSkippedPixelHandling.Transparent:
326-
color.FromVector4(Vector4.Zero);
326+
color.FromScaledVector4(Vector4.Zero);
327327
break;
328328

329329
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
330330
default:
331-
color.FromVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
331+
color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
332332
break;
333333
}
334334
}
@@ -395,12 +395,12 @@ private void ReadRle24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, b
395395
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
396396
break;
397397
case RleSkippedPixelHandling.Transparent:
398-
color.FromVector4(Vector4.Zero);
398+
color.FromScaledVector4(Vector4.Zero);
399399
break;
400400

401401
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
402402
default:
403-
color.FromVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
403+
color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
404404
break;
405405
}
406406
}
@@ -1127,7 +1127,7 @@ private void ReadRgb32BitFields<TPixel>(Buffer2D<TPixel> pixels, int width, int
11271127
g * invMaxValueGreen,
11281128
b * invMaxValueBlue,
11291129
alpha);
1130-
color.FromVector4(vector4);
1130+
color.FromScaledVector4(vector4);
11311131
}
11321132
else
11331133
{

src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public enum TiffCompression : ushort
2323
/// </summary>
2424
Ccitt1D = 2,
2525

26-
/// <summary>
27-
/// PackBits compression
28-
/// </summary>
29-
PackBits = 32773,
30-
3126
/// <summary>
3227
/// T4-encoding: CCITT T.4 bi-level encoding (see Section 11 of the TIFF 6.0 specification).
3328
/// </summary>
@@ -65,27 +60,48 @@ public enum TiffCompression : ushort
6560
Deflate = 8,
6661

6762
/// <summary>
68-
/// Deflate compression - old.
63+
/// ITU-T Rec. T.82 coding, applying ITU-T Rec. T.85 (JBIG) (see RFC2301).
6964
///
70-
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
65+
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
7166
/// if this is chosen.
7267
/// </summary>
73-
OldDeflate = 32946,
68+
ItuTRecT82 = 9,
7469

7570
/// <summary>
76-
/// ITU-T Rec. T.82 coding, applying ITU-T Rec. T.85 (JBIG) (see RFC2301).
71+
/// ITU-T Rec. T.43 representation, using ITU-T Rec. T.82 (JBIG) (see RFC2301).
7772
///
7873
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
7974
/// if this is chosen.
8075
/// </summary>
81-
ItuTRecT82 = 9,
76+
ItuTRecT43 = 10,
8277

8378
/// <summary>
84-
/// ITU-T Rec. T.43 representation, using ITU-T Rec. T.82 (JBIG) (see RFC2301).
79+
/// NeXT 2-bit Grey Scale compression algorithm.
8580
///
86-
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
81+
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
82+
/// if this is chosen.
83+
/// </summary>
84+
NeXT = 32766,
85+
86+
/// <summary>
87+
/// PackBits compression.
88+
/// </summary>
89+
PackBits = 32773,
90+
91+
/// <summary>
92+
/// ThunderScan 4-bit compression.
93+
///
94+
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
8795
/// if this is chosen.
8896
/// </summary>
89-
ItuTRecT43 = 10
97+
ThunderScan = 32809,
98+
99+
/// <summary>
100+
/// Deflate compression - old.
101+
///
102+
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
103+
/// if this is chosen.
104+
/// </summary>
105+
OldDeflate = 32946,
90106
}
91107
}

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3636
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3737
L16 l16 = TiffUtils.L16Default;
3838
var color = default(TPixel);
39-
color.FromVector4(TiffUtils.Vector4Default);
39+
color.FromScaledVector4(TiffUtils.Vector4Default);
4040

4141
int offset = 0;
4242
for (int y = top; y < top + height; y++)

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2828
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
2929
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3030
var color = default(TPixel);
31-
color.FromVector4(TiffUtils.Vector4Default);
31+
color.FromScaledVector4(TiffUtils.Vector4Default);
3232
byte[] buffer = new byte[4];
3333
int bufferStartIdx = this.isBigEndian ? 1 : 0;
3434

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32FloatTiffColor{TPixel}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2929
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
3030
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3131
var color = default(TPixel);
32-
color.FromVector4(TiffUtils.Vector4Default);
32+
color.FromScaledVector4(TiffUtils.Vector4Default);
3333
byte[] buffer = new byte[4];
3434

3535
int offset = 0;
@@ -46,7 +46,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4646
offset += 4;
4747

4848
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f);
49-
color.FromVector4(colorVector);
49+
color.FromScaledVector4(colorVector);
5050
pixelRow[x] = color;
5151
}
5252
}
@@ -59,7 +59,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
5959
offset += 4;
6060

6161
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f);
62-
color.FromVector4(colorVector);
62+
color.FromScaledVector4(colorVector);
6363
pixelRow[x] = color;
6464
}
6565
}

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32TiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2828
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
2929
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3030
var color = default(TPixel);
31-
color.FromVector4(TiffUtils.Vector4Default);
31+
color.FromScaledVector4(TiffUtils.Vector4Default);
3232

3333
int offset = 0;
3434
for (int y = top; y < top + height; y++)

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4040
int value = bitReader.ReadBits(this.bitsPerSample0);
4141
float intensity = value / this.factor;
4242

43-
color.FromVector4(new Vector4(intensity, intensity, intensity, 1.0f));
43+
color.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
4444
pixelRow[x] = color;
4545
}
4646

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static TPixel[] GeneratePalette(ushort[] colorMap, int colorCount)
5959
float r = colorMap[rOffset + i] / 65535F;
6060
float g = colorMap[gOffset + i] / 65535F;
6161
float b = colorMap[bOffset + i] / 65535F;
62-
palette[i].FromVector4(new Vector4(r, g, b, 1.0f));
62+
palette[i].FromScaledVector4(new Vector4(r, g, b, 1.0f));
6363
}
6464

6565
return palette;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3636
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3737
Rgba64 rgba = TiffUtils.Rgba64Default;
3838
var color = default(TPixel);
39-
color.FromVector4(TiffUtils.Vector4Default);
39+
color.FromScaledVector4(TiffUtils.Vector4Default);
4040

4141
int offset = 0;
4242

@@ -55,7 +55,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
5555
ulong b = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2));
5656
offset += 2;
5757

58-
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
58+
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color);
5959
}
6060
}
6161
else

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
3030
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
3131
Rgba64 rgba = TiffUtils.Rgba64Default;
3232
var color = default(TPixel);
33-
color.FromVector4(TiffUtils.Vector4Default);
33+
color.FromScaledVector4(TiffUtils.Vector4Default);
3434

3535
Span<byte> redData = data[0].GetSpan();
3636
Span<byte> greenData = data[1].GetSpan();
@@ -50,7 +50,7 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
5050

5151
offset += 2;
5252

53-
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
53+
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color);
5454
}
5555
}
5656
else
@@ -63,7 +63,7 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
6363

6464
offset += 2;
6565

66-
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
66+
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color);
6767
}
6868
}
6969
}

0 commit comments

Comments
 (0)