Skip to content

Commit 829be7a

Browse files
Style cleanup
1 parent 9f3f908 commit 829be7a

File tree

1 file changed

+75
-80
lines changed

1 file changed

+75
-80
lines changed

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 75 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -307,39 +307,37 @@ private void ReadPaletted<TPixel>(BufferedReadStream stream, int width, int heig
307307
private void ReadPalettedRle<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, Span<byte> palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin)
308308
where TPixel : unmanaged, IPixel<TPixel>
309309
{
310-
using (IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height, AllocationOptions.Clean))
311-
{
312-
TPixel color = default;
313-
Span<byte> bufferSpan = buffer.GetSpan();
314-
this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel: 1);
310+
using IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height, AllocationOptions.Clean);
311+
TPixel color = default;
312+
Span<byte> bufferSpan = buffer.GetSpan();
313+
this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel: 1);
315314

316-
for (int y = 0; y < height; y++)
315+
for (int y = 0; y < height; y++)
316+
{
317+
int newY = InvertY(y, height, origin);
318+
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
319+
int rowStartIdx = y * width;
320+
for (int x = 0; x < width; x++)
317321
{
318-
int newY = InvertY(y, height, origin);
319-
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
320-
int rowStartIdx = y * width;
321-
for (int x = 0; x < width; x++)
322+
int idx = rowStartIdx + x;
323+
switch (colorMapPixelSizeInBytes)
322324
{
323-
int idx = rowStartIdx + x;
324-
switch (colorMapPixelSizeInBytes)
325-
{
326-
case 1:
327-
color.FromL8(Unsafe.As<byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
328-
break;
329-
case 2:
330-
this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color);
331-
break;
332-
case 3:
333-
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
334-
break;
335-
case 4:
336-
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
337-
break;
338-
}
339-
340-
int newX = InvertX(x, width, origin);
341-
pixelRow[newX] = color;
325+
case 1:
326+
color.FromL8(Unsafe.As<byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
327+
break;
328+
case 2:
329+
this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color);
330+
break;
331+
case 3:
332+
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
333+
break;
334+
case 4:
335+
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
336+
break;
342337
}
338+
339+
int newX = InvertX(x, width, origin);
340+
pixelRow[newX] = color;
343341
}
344342
}
345343
}
@@ -598,60 +596,58 @@ private void ReadRle<TPixel>(BufferedReadStream stream, int width, int height, B
598596
Guard.NotNull(this.tgaMetadata);
599597

600598
byte alphaBits = this.tgaMetadata.AlphaChannelBits;
601-
using (IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height * bytesPerPixel, AllocationOptions.Clean))
599+
using IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height * bytesPerPixel, AllocationOptions.Clean);
600+
Span<byte> bufferSpan = buffer.GetSpan();
601+
this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel);
602+
for (int y = 0; y < height; y++)
602603
{
603-
Span<byte> bufferSpan = buffer.GetSpan();
604-
this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel);
605-
for (int y = 0; y < height; y++)
604+
int newY = InvertY(y, height, origin);
605+
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
606+
int rowStartIdx = y * width * bytesPerPixel;
607+
for (int x = 0; x < width; x++)
606608
{
607-
int newY = InvertY(y, height, origin);
608-
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
609-
int rowStartIdx = y * width * bytesPerPixel;
610-
for (int x = 0; x < width; x++)
609+
int idx = rowStartIdx + (x * bytesPerPixel);
610+
switch (bytesPerPixel)
611611
{
612-
int idx = rowStartIdx + (x * bytesPerPixel);
613-
switch (bytesPerPixel)
614-
{
615-
case 1:
616-
color.FromL8(Unsafe.As<byte, L8>(ref bufferSpan[idx]));
617-
break;
618-
case 2:
619-
if (!this.hasAlpha)
620-
{
621-
// Set alpha value to 1, to treat it as opaque for Bgra5551.
622-
bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128);
623-
}
624-
625-
if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite)
626-
{
627-
color.FromLa16(Unsafe.As<byte, La16>(ref bufferSpan[idx]));
628-
}
629-
else
630-
{
631-
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref bufferSpan[idx]));
632-
}
633-
634-
break;
635-
case 3:
636-
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
637-
break;
638-
case 4:
639-
if (this.hasAlpha)
640-
{
641-
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref bufferSpan[idx]));
642-
}
643-
else
644-
{
645-
byte alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3];
646-
color.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha));
647-
}
648-
649-
break;
650-
}
612+
case 1:
613+
color.FromL8(Unsafe.As<byte, L8>(ref bufferSpan[idx]));
614+
break;
615+
case 2:
616+
if (!this.hasAlpha)
617+
{
618+
// Set alpha value to 1, to treat it as opaque for Bgra5551.
619+
bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128);
620+
}
651621

652-
int newX = InvertX(x, width, origin);
653-
pixelRow[newX] = color;
622+
if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite)
623+
{
624+
color.FromLa16(Unsafe.As<byte, La16>(ref bufferSpan[idx]));
625+
}
626+
else
627+
{
628+
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref bufferSpan[idx]));
629+
}
630+
631+
break;
632+
case 3:
633+
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
634+
break;
635+
case 4:
636+
if (this.hasAlpha)
637+
{
638+
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref bufferSpan[idx]));
639+
}
640+
else
641+
{
642+
byte alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3];
643+
color.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha));
644+
}
645+
646+
break;
654647
}
648+
649+
int newX = InvertX(x, width, origin);
650+
pixelRow[newX] = color;
655651
}
656652
}
657653
}
@@ -952,7 +948,6 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
952948
this.hasAlpha = alphaBits > 0;
953949

954950
// Bits 4 and 5 describe the image origin.
955-
TgaImageOrigin origin = (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
956-
return origin;
951+
return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
957952
}
958953
}

0 commit comments

Comments
 (0)