Skip to content

Commit

Permalink
Improved method names for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin113D committed Oct 23, 2023
1 parent 6fe3e9b commit 99213f9
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 219 deletions.
30 changes: 16 additions & 14 deletions src/SA3D.Texturing/ColorTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public override bool CheckIsTransparent()
/// </summary>
/// <param name="stream">Stream to read the file data from.</param>
/// <param name="filename">Filename that should be used.</param>
public static ColorTexture ReadColoredFromFile(Stream stream, string filename)
public static ColorTexture ReadColored(Stream stream, string filename)
{
long dataStart = stream.Position;

Expand Down Expand Up @@ -119,6 +119,19 @@ public static ColorTexture ReadColoredFromFile(Stream stream, string filename)
return new ColorTexture(image.Width, image.Height, data, filename, 0);
}

/// <summary>
/// Read a color texture from file data.
/// </summary>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
public static ColorTexture ReadColored(byte[] data, string filename)
{
using(MemoryStream stream = new(data))
{
return ReadColored(stream, filename);
}
}

/// <summary>
/// Read a color texture from a file.
/// </summary>
Expand All @@ -127,22 +140,11 @@ public static ColorTexture ReadColoredFromFile(string filepath)
{
using(FileStream stream = File.OpenRead(filepath))
{
return ReadColoredFromFile(stream, Path.GetFileNameWithoutExtension(filepath));
return ReadColored(stream, Path.GetFileNameWithoutExtension(filepath));
}
}

/// <summary>
/// Read a color texture from file data.
/// </summary>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
public static ColorTexture ReadColoredFromFile(byte[] data, string filename)
{
using(MemoryStream stream = new(data))
{
return ReadColoredFromFile(stream, filename);
}
}


}
}
117 changes: 58 additions & 59 deletions src/SA3D.Texturing/IndexTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override ReadOnlySpan<byte> GetColorPixels()
/// </summary>
/// <param name="stream">The file data stream to write to.</param>
/// <param name="storeInAlpha">Whether the index should be stored in the alpha channel, instead of outputing a grayscale image.</param>
public void WriteIndexedToPNGFileStream(Stream stream, bool storeInAlpha)
public void WriteIndexedAsPNG(Stream stream, bool storeInAlpha)
{
PngEncoder encoder = new()
{
Expand All @@ -162,28 +162,28 @@ public void WriteIndexedToPNGFileStream(Stream stream, bool storeInAlpha)
}

/// <summary>
/// Write the indexed texture to a PNG file.
/// Encode the indexed texture as a PNG file.
/// </summary>
/// <param name="filepath">The path to the file to write to.</param>
/// <param name="storeInAlpha">Whether the index should be stored in the alpha channel, instead of outputing a grayscale image.</param>
public void WriteIndexedToPNGFile(string filepath, bool storeInAlpha)
public byte[] WriteIndexedAsPNGToBytes(bool storeInAlpha)
{
using(FileStream stream = File.Create(filepath))
using(MemoryStream stream = new())
{
WriteIndexedToPNGFileStream(stream, storeInAlpha);
WriteIndexedAsPNG(stream, storeInAlpha);
return stream.ToArray();
}
}

/// <summary>
/// Encode the indexed texture as a PNG file.
/// Write the indexed texture to a PNG file.
/// </summary>
/// <param name="filepath">The path to the file to write to.</param>
/// <param name="storeInAlpha">Whether the index should be stored in the alpha channel, instead of outputing a grayscale image.</param>
public byte[] WriteIndexedToPNGFileData(bool storeInAlpha)
public void WriteIndexedAsPNGToFile(string filepath, bool storeInAlpha)
{
using(MemoryStream stream = new())
using(FileStream stream = File.Create(filepath))
{
WriteIndexedToPNGFileStream(stream, storeInAlpha);
return stream.ToArray();
WriteIndexedAsPNG(stream, storeInAlpha);
}
}

Expand All @@ -192,7 +192,7 @@ public byte[] WriteIndexedToPNGFileData(bool storeInAlpha)
/// Encode the indexed texture as a DDS file.
/// </summary>
/// <param name="stream">The file data stream to write to.</param>
public void WriteIndexedToDDSFileStream(Stream stream)
public void WriteIndexedAsDDS(Stream stream)
{
int prevRow = PaletteRow;
PaletteRow = 0;
Expand All @@ -207,26 +207,26 @@ public void WriteIndexedToDDSFileStream(Stream stream)
}

/// <summary>
/// Write the indexed texture to a DDS file.
/// Encode the indexed texture as a DDS file.
/// </summary>
/// <param name="filepath">The path to the file to write to.</param>
public void WriteIndexedToDDSFile(string filepath)
public byte[] WriteIndexedAsDDSToBytes()
{
using(FileStream stream = File.Create(filepath))
using(MemoryStream stream = new())
{
WriteIndexedToDDSFileStream(stream);
WriteIndexedAsDDS(stream);
return stream.ToArray();
}
}

/// <summary>
/// Encode the indexed texture as a DDS file.
/// Write the indexed texture to a DDS file.
/// </summary>
public byte[] WriteIndexedToDDSFileData()
/// <param name="filepath">The path to the file to write to.</param>
public void WriteIndexedAsDDSToFile(string filepath)
{
using(MemoryStream stream = new())
using(FileStream stream = File.Create(filepath))
{
WriteIndexedToDDSFileStream(stream);
return stream.ToArray();
WriteIndexedAsDDS(stream);
}
}

Expand All @@ -239,7 +239,7 @@ public byte[] WriteIndexedToDDSFileData()
/// <param name="index4">Whether the file stores 4 bit indices.</param>
/// <param name="storedInAlpha">Whether the index data is stored in alpha.</param>
/// <returns>Whether the file can be reas as an index texture.</returns>
public static bool CheckCanReadIndexedFromFile(Stream stream, out bool index4, out bool storedInAlpha)
public static bool CheckCanReadIndexed(Stream stream, out bool index4, out bool storedInAlpha)
{
index4 = false;
storedInAlpha = false;
Expand Down Expand Up @@ -308,34 +308,34 @@ public static bool CheckCanReadIndexedFromFile(Stream stream, out bool index4, o
}

/// <summary>
/// Verifies whether a file is readable as an index texture.
/// Verifies whether file data is readable as an index texture.
/// <br/> Does not alter the stream position.
/// </summary>
/// <param name="filepath">Path from which the file should be read.</param>
/// <param name="data">File data to read.</param>
/// <param name="index4">Whether the file stores 4 bit indices.</param>
/// <param name="storedInAlpha">Whether the index data is stored in alpha.</param>
/// <returns>Whether the file can be reas as an index texture.</returns>
public static bool CheckCanReadIndexedFromFile(string filepath, out bool index4, out bool storedInAlpha)
public static bool CheckCanReadIndexed(byte[] data, out bool index4, out bool storedInAlpha)
{
using(FileStream stream = File.OpenRead(filepath))
using(MemoryStream stream = new(data))
{
return CheckCanReadIndexedFromFile(stream, out index4, out storedInAlpha);
return CheckCanReadIndexed(stream, out index4, out storedInAlpha);
}
}

/// <summary>
/// Verifies whether file data is readable as an index texture.
/// Verifies whether a file is readable as an index texture.
/// <br/> Does not alter the stream position.
/// </summary>
/// <param name="data">File data to read.</param>
/// <param name="filepath">Path from which the file should be read.</param>
/// <param name="index4">Whether the file stores 4 bit indices.</param>
/// <param name="storedInAlpha">Whether the index data is stored in alpha.</param>
/// <returns>Whether the file can be reas as an index texture.</returns>
public static bool CheckCanReadIndexedFromFile(byte[] data, out bool index4, out bool storedInAlpha)
public static bool CheckCanReadIndexedFromFile(string filepath, out bool index4, out bool storedInAlpha)
{
using(MemoryStream stream = new(data))
using(FileStream stream = File.OpenRead(filepath))
{
return CheckCanReadIndexedFromFile(stream, out index4, out storedInAlpha);
return CheckCanReadIndexed(stream, out index4, out storedInAlpha);
}
}

Expand All @@ -347,13 +347,13 @@ public static bool CheckCanReadIndexedFromFile(byte[] data, out bool index4, out
/// <param name="filename">Filename that should be used.</param>
/// <param name="result">The read index texture. Null if file was not an index texture</param>
/// <returns>Whether the file was successfully read as index texture.</returns>
public static bool TryReadIndexedFromFile(Stream stream, string filename, [MaybeNullWhen(false)] out IndexTexture result)
public static bool TryReadIndexed(Stream stream, string filename, [MaybeNullWhen(false)] out IndexTexture result)
{
if(CheckCanReadIndexedFromFile(stream, out bool index4, out bool inAlpha))
if(CheckCanReadIndexed(stream, out bool index4, out bool inAlpha))
{
result = inAlpha
? ReadFromFile<A8>(stream, filename, index4)
: ReadFromFile<L8>(stream, filename, index4);
? Read<A8>(stream, filename, index4)
: Read<L8>(stream, filename, index4);

return true;
}
Expand All @@ -365,31 +365,31 @@ public static bool TryReadIndexedFromFile(Stream stream, string filename, [Maybe
}

/// <summary>
/// Attempts to read an indexed texture from a file.
/// Attempts to read an indexed texture from file data.
/// </summary>
/// <param name="filepath">Path from which the file should be read.</param>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
/// <param name="result">The read index texture. Null if file was not an index texture</param>
/// <returns>Whether the file was successfully read as index texture.</returns>
public static bool TryReadIndexedFromFile(string filepath, [MaybeNullWhen(false)] out IndexTexture result)
public static bool TryReadIndexed(byte[] data, string filename, [MaybeNullWhen(false)] out IndexTexture result)
{
using(FileStream stream = File.OpenRead(filepath))
using(MemoryStream stream = new(data))
{
return TryReadIndexedFromFile(stream, Path.GetFileNameWithoutExtension(filepath), out result);
return TryReadIndexed(stream, filename, out result);
}
}

/// <summary>
/// Attempts to read an indexed texture from file data.
/// Attempts to read an indexed texture from a file.
/// </summary>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
/// <param name="filepath">Path from which the file should be read.</param>
/// <param name="result">The read index texture. Null if file was not an index texture</param>
/// <returns>Whether the file was successfully read as index texture.</returns>
public static bool TryReadIndexedFromFile(byte[] data, string filename, [MaybeNullWhen(false)] out IndexTexture result)
public static bool TryReadIndexedFromFile(string filepath, [MaybeNullWhen(false)] out IndexTexture result)
{
using(MemoryStream stream = new(data))
using(FileStream stream = File.OpenRead(filepath))
{
return TryReadIndexedFromFile(stream, filename, out result);
return TryReadIndexed(stream, Path.GetFileNameWithoutExtension(filepath), out result);
}
}

Expand All @@ -401,9 +401,9 @@ public static bool TryReadIndexedFromFile(byte[] data, string filename, [MaybeNu
/// <param name="filename">Filename that should be used.</param>
/// <returns>The read index texture.</returns>
/// <exception cref="InvalidDataException"></exception>
public static IndexTexture ReadIndexedFromFile(Stream stream, string filename)
public static IndexTexture ReadIndexed(Stream stream, string filename)
{
if(TryReadIndexedFromFile(stream, filename, out IndexTexture? result))
if(TryReadIndexed(stream, filename, out IndexTexture? result))
{
return result;
}
Expand All @@ -414,12 +414,13 @@ public static IndexTexture ReadIndexedFromFile(Stream stream, string filename)
/// <summary>
/// Reads an index texture from a file data stream.
/// </summary>
/// <param name="filepath">Path from which the file should be read.</param>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
/// <returns>The read index texture.</returns>
/// <exception cref="InvalidDataException"></exception>
public static IndexTexture ReadIndexedFromFile(string filepath)
public static IndexTexture ReadIndexed(byte[] data, string filename)
{
if(TryReadIndexedFromFile(filepath, out IndexTexture? result))
if(TryReadIndexed(data, filename, out IndexTexture? result))
{
return result;
}
Expand All @@ -430,22 +431,20 @@ public static IndexTexture ReadIndexedFromFile(string filepath)
/// <summary>
/// Reads an index texture from a file data stream.
/// </summary>
/// <param name="data">File data to read.</param>
/// <param name="filename">Filename that should be used.</param>
/// <param name="filepath">Path from which the file should be read.</param>
/// <returns>The read index texture.</returns>
/// <exception cref="InvalidDataException"></exception>
public static IndexTexture ReadIndexedFromFile(byte[] data, string filename)
public static IndexTexture ReadIndexedFromFile(string filepath)
{
if(TryReadIndexedFromFile(data, filename, out IndexTexture? result))
if(TryReadIndexedFromFile(filepath, out IndexTexture? result))
{
return result;
}

throw new InvalidDataException("File Data was not able to be read as an index texture.");
}


private static IndexTexture ReadFromFile<TPixel>(Stream stream, string filename, bool isIndex4)
private static IndexTexture Read<TPixel>(Stream stream, string filename, bool isIndex4)
where TPixel : unmanaged, IPixel<TPixel>
{
byte[] data;
Expand Down
Loading

0 comments on commit 99213f9

Please sign in to comment.