Skip to content

Commit

Permalink
TgaFileFormat decoupled from TgaImage
Browse files Browse the repository at this point in the history
which allows saving another image sources and also provides simplified interface for loading TgaImage somewhere else using the same IImage interface.
  • Loading branch information
VasilijP committed Oct 18, 2024
1 parent 40671cf commit 0255b8b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
28 changes: 28 additions & 0 deletions tgalib-core/IImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace tgalib_core;

/// <summary>
/// Abstraction of the image usable for simplified loading and saving routines.
/// </summary>
public interface IImage
{
/// <summary>
/// Width of the image in pixels.
/// </summary>
int Width { get; }

/// <summary>
/// Height of the image in pixels.
/// </summary>
int Height { get; }

/// <summary>
/// Pixel access.
/// </summary>
/// <param name="x">X coordinate of a pixel within the image.</param>
/// <param name="y">Y coordinate of a pixel within the image. Note: TGA coordinate [0, 0] is a bottom left corner.</param>
/// <param name="r">Red component (8 bit).</param>
/// <param name="g">Green component (8 bit).</param>
/// <param name="b">Blue component (8 bit).</param>
/// <param name="a">Alpha component (8 bit).</param>
void GetPixelRgba(int x, int y, out int r, out int g, out int b, out int a);
}
30 changes: 15 additions & 15 deletions tgalib-core/TgaFileFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public static class TgaFileFormat
{
// Common save routine.
public static void CommonSave(TgaMode curTgaMode, Stream stream, TgaImage g)
public static void CommonSave(TgaMode curTgaMode, Stream stream, IImage g)
{
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
switch (curTgaMode)
Expand All @@ -58,10 +58,10 @@ public static void CommonSave(TgaMode curTgaMode, Stream stream, TgaImage g)
}

// TGA RGB 24 RLE save code.
private static void TgaRgb24RleSave(Stream tgaFile, TgaImage image)
private static void TgaRgb24RleSave(Stream tgaFile, IImage image)
{
int width = image.TgaHeader.Width;
int height = image.TgaHeader.Height;
int width = image.Width;
int height = image.Height;

// 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12| 13| 14| 15| 16|17|18
byte[] header = [ 0, 0,10, 0, 0, 0, 0, 0, 0, 0, 0, 0,(byte)(width&255),(byte)((width>>8)&255),(byte)(height&255),(byte)((height>>8)&255),24, 0];
Expand All @@ -84,10 +84,10 @@ private static void TgaRgb24RleSave(Stream tgaFile, TgaImage image)
// Stops when entire image is searched or palette is full (max 256 colors)
// Colors which are not in palette are written as color 0 (there should be no more than 256 colors in image)
// Performs RLE compression
private static void TgaPal8RleSave(Stream tgaFile, TgaImage image)
private static void TgaPal8RleSave(Stream tgaFile, IImage image)
{
int width = image.TgaHeader.Width;
int height = image.TgaHeader.Height;
int width = image.Width;
int height = image.Height;
const int maxColors = 256;

//palette (BGR)
Expand Down Expand Up @@ -130,11 +130,11 @@ private static void TgaPal8RleSave(Stream tgaFile, TgaImage image)
// Seraches image for unique colors.
// Stops when entire image is searched or palette is full (max 256 colors)
// Colors which are not in palette are written as color 0 (there should be no more than 256 colors in image)
private static void TgaPal8UncSave(Stream tgaFile, TgaImage image)
private static void TgaPal8UncSave(Stream tgaFile, IImage image)
{
//header
int width = image.TgaHeader.Width;
int height = image.TgaHeader.Height;
int width = image.Width;
int height = image.Height;
const int maxColors = 256;

//palette ( colors are stored as BGR in file )
Expand Down Expand Up @@ -172,10 +172,10 @@ private static void TgaPal8UncSave(Stream tgaFile, TgaImage image)
}

// TGA RGB 24 UNC save code.
private static void TgaRgb24UncSave(Stream tgaFile, TgaImage image)
private static void TgaRgb24UncSave(Stream tgaFile, IImage image)
{
int width = image.TgaHeader.Width;
int height = image.TgaHeader.Height;
int width = image.Width;
int height = image.Height;

// 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12| 13| 14| 15| 16|17|18
byte[] header = [0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,(byte)(width&255),(byte)((width>>8)&255),(byte)(height&255),(byte)((height>>8)&255),24, 0];
Expand All @@ -185,9 +185,9 @@ private static void TgaRgb24UncSave(Stream tgaFile, TgaImage image)
for (int x = 0; x < width; ++x)
{
image.GetPixelRgba(x, y, out int r, out int g, out int b, out int a);
tgaFile.Write([(byte)(r & 255), (byte)(g & 255), (byte)(b & 255)]); // TODO: check if byte order is correct
tgaFile.Write([(byte)(r & 255), (byte)(g & 255), (byte)(b & 255)]);
}

tgaFile.Close();
}
}
}
6 changes: 3 additions & 3 deletions tgalib-core/TgaHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TgaHeader
/// <summary>
/// Gets or sets a length of Image ID field.
/// </summary>
public byte IDLength { get; set; }
public byte IdLength { get; set; }

/// <summary>
/// Gets or sets a Color Map type(<see cref="ColorMapTypes"/>).
Expand Down Expand Up @@ -83,7 +83,7 @@ public class TgaHeader
/// <param name="reader">A binary reader that contains TGA file. Caller must dipose the binary reader.</param>
public TgaHeader(BinaryReader reader)
{
IDLength = reader.ReadByte();
IdLength = reader.ReadByte();
ColorMapType = reader.ReadByte();
ImageType = (TgaMode)reader.ReadByte();
ColorMapStart = reader.ReadUInt16();
Expand All @@ -104,7 +104,7 @@ public TgaHeader(BinaryReader reader)
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append($"IDLength : {IDLength}\r\n");
sb.Append($"IDLength : {IdLength}\r\n");
sb.Append($"ColorMapType : {ColorMapType}({ColorMapTypes.ToFormattedText(ColorMapType)})\r\n");
sb.Append($"ImageType : {ImageType}({ImageTypes.ToFormattedText(ImageType)})\r\n");
sb.Append($"ColorMapStart : {ColorMapStart}\r\n");
Expand Down
4 changes: 2 additions & 2 deletions tgalib-core/TgaImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace tgalib_core
/// <summary>
/// Represents TGA image.
/// </summary>
public class TgaImage
public class TgaImage : IImage
{
/// <summary>
/// Use the alpha channel forcefully, if true.
Expand Down Expand Up @@ -72,7 +72,7 @@ private void Init(BinaryReader reader)
{
TgaHeader = new TgaHeader(reader);

ImageId = new byte[TgaHeader.IDLength];
ImageId = new byte[TgaHeader.IdLength];
reader.Read(ImageId, 0, ImageId.Length);
PixelFormat = DecodePixelFormat();
imageWidth = TgaHeader.Width;
Expand Down

0 comments on commit 0255b8b

Please sign in to comment.