Skip to content

Commit c8fd4a3

Browse files
Match other format configuration handling
1 parent 7be5bf1 commit c8fd4a3

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
2222
/// </summary>
2323
private readonly MemoryAllocator memoryAllocator;
2424

25-
/// <summary>
26-
/// The global configuration.
27-
/// </summary>
28-
private Configuration? configuration;
29-
3025
/// <summary>
3126
/// Reusable buffer for writing data.
3227
/// </summary>
@@ -67,7 +62,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
6762
Guard.NotNull(image, nameof(image));
6863
Guard.NotNull(stream, nameof(stream));
6964

70-
this.configuration = image.GetConfiguration();
7165
ImageMetadata metadata = image.Metadata;
7266
TgaMetadata tgaMetadata = metadata.GetTgaMetadata();
7367
this.bitsPerPixel ??= tgaMetadata.BitsPerPixel;
@@ -123,7 +117,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
123117
}
124118
else
125119
{
126-
this.WriteImage(stream, image.Frames.RootFrame);
120+
this.WriteImage(image.GetConfiguration(), stream, image.Frames.RootFrame);
127121
}
128122

129123
stream.Flush();
@@ -133,30 +127,31 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
133127
/// Writes the pixel data to the binary stream.
134128
/// </summary>
135129
/// <typeparam name="TPixel">The pixel format.</typeparam>
130+
/// <param name="configuration">The global configuration.</param>
136131
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
137132
/// <param name="image">
138133
/// The <see cref="ImageFrame{TPixel}"/> containing pixel data.
139134
/// </param>
140-
private void WriteImage<TPixel>(Stream stream, ImageFrame<TPixel> image)
135+
private void WriteImage<TPixel>(Configuration configuration, Stream stream, ImageFrame<TPixel> image)
141136
where TPixel : unmanaged, IPixel<TPixel>
142137
{
143138
Buffer2D<TPixel> pixels = image.PixelBuffer;
144139
switch (this.bitsPerPixel)
145140
{
146141
case TgaBitsPerPixel.Pixel8:
147-
this.Write8Bit(stream, pixels);
142+
this.Write8Bit(configuration, stream, pixels);
148143
break;
149144

150145
case TgaBitsPerPixel.Pixel16:
151-
this.Write16Bit(stream, pixels);
146+
this.Write16Bit(configuration, stream, pixels);
152147
break;
153148

154149
case TgaBitsPerPixel.Pixel24:
155-
this.Write24Bit(stream, pixels);
150+
this.Write24Bit(configuration, stream, pixels);
156151
break;
157152

158153
case TgaBitsPerPixel.Pixel32:
159-
this.Write32Bit(stream, pixels);
154+
this.Write32Bit(configuration, stream, pixels);
160155
break;
161156
}
162157
}
@@ -226,7 +221,7 @@ private void WritePixel<TPixel>(Stream stream, TPixel currentPixel, Rgba32 color
226221

227222
case TgaBitsPerPixel.Pixel16:
228223
Bgra5551 bgra5551 = new(color.ToVector4());
229-
BinaryPrimitives.TryWriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue);
224+
BinaryPrimitives.WriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue);
230225
stream.WriteByte(this.buffer[0]);
231226
stream.WriteByte(this.buffer[1]);
232227

@@ -320,9 +315,10 @@ private IMemoryOwner<byte> AllocateRow(int width, int bytesPerPixel)
320315
/// Writes the 8bit pixels uncompressed to the stream.
321316
/// </summary>
322317
/// <typeparam name="TPixel">The pixel format.</typeparam>
318+
/// <param name="configuration">The global configuration.</param>
323319
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
324320
/// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> containing pixel data.</param>
325-
private void Write8Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
321+
private void Write8Bit<TPixel>(Configuration configuration, Stream stream, Buffer2D<TPixel> pixels)
326322
where TPixel : unmanaged, IPixel<TPixel>
327323
{
328324
using IMemoryOwner<byte> row = this.AllocateRow(pixels.Width, 1);
@@ -332,7 +328,7 @@ private void Write8Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
332328
{
333329
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(y);
334330
PixelOperations<TPixel>.Instance.ToL8Bytes(
335-
this.configuration,
331+
configuration,
336332
pixelSpan,
337333
rowSpan,
338334
pixelSpan.Length);
@@ -344,9 +340,10 @@ private void Write8Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
344340
/// Writes the 16bit pixels uncompressed to the stream.
345341
/// </summary>
346342
/// <typeparam name="TPixel">The pixel format.</typeparam>
343+
/// <param name="configuration">The global configuration.</param>
347344
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
348345
/// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> containing pixel data.</param>
349-
private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
346+
private void Write16Bit<TPixel>(Configuration configuration, Stream stream, Buffer2D<TPixel> pixels)
350347
where TPixel : unmanaged, IPixel<TPixel>
351348
{
352349
using IMemoryOwner<byte> row = this.AllocateRow(pixels.Width, 2);
@@ -356,7 +353,7 @@ private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
356353
{
357354
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(y);
358355
PixelOperations<TPixel>.Instance.ToBgra5551Bytes(
359-
this.configuration,
356+
configuration,
360357
pixelSpan,
361358
rowSpan,
362359
pixelSpan.Length);
@@ -368,9 +365,10 @@ private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
368365
/// Writes the 24bit pixels uncompressed to the stream.
369366
/// </summary>
370367
/// <typeparam name="TPixel">The pixel format.</typeparam>
368+
/// <param name="configuration">The global configuration.</param>
371369
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
372370
/// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> containing pixel data.</param>
373-
private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
371+
private void Write24Bit<TPixel>(Configuration configuration, Stream stream, Buffer2D<TPixel> pixels)
374372
where TPixel : unmanaged, IPixel<TPixel>
375373
{
376374
using IMemoryOwner<byte> row = this.AllocateRow(pixels.Width, 3);
@@ -380,7 +378,7 @@ private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
380378
{
381379
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(y);
382380
PixelOperations<TPixel>.Instance.ToBgr24Bytes(
383-
this.configuration,
381+
configuration,
384382
pixelSpan,
385383
rowSpan,
386384
pixelSpan.Length);
@@ -392,9 +390,10 @@ private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
392390
/// Writes the 32bit pixels uncompressed to the stream.
393391
/// </summary>
394392
/// <typeparam name="TPixel">The pixel format.</typeparam>
393+
/// <param name="configuration">The global configuration.</param>
395394
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
396395
/// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> containing pixel data.</param>
397-
private void Write32Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
396+
private void Write32Bit<TPixel>(Configuration configuration, Stream stream, Buffer2D<TPixel> pixels)
398397
where TPixel : unmanaged, IPixel<TPixel>
399398
{
400399
using IMemoryOwner<byte> row = this.AllocateRow(pixels.Width, 4);
@@ -404,7 +403,7 @@ private void Write32Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
404403
{
405404
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(y);
406405
PixelOperations<TPixel>.Instance.ToBgra32Bytes(
407-
this.configuration,
406+
configuration,
408407
pixelSpan,
409408
rowSpan,
410409
pixelSpan.Length);

0 commit comments

Comments
 (0)