194 lines
5.3 KiB
C#
194 lines
5.3 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Tga.Core
|
|
{
|
|
|
|
public partial class TGALoader
|
|
{
|
|
|
|
#region Public Methods
|
|
|
|
public static TGAFile Load(Stream stream)
|
|
{
|
|
TGAFile tgaFile = null;
|
|
|
|
using (BinaryReader reader = new BinaryReader(stream))
|
|
{
|
|
tgaFile = Load(reader);
|
|
reader.Close();
|
|
stream.Close();
|
|
}
|
|
return tgaFile;
|
|
}
|
|
|
|
public static TGAFile Load(string filename)
|
|
{
|
|
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
|
|
{
|
|
return Load(stream);
|
|
}
|
|
}
|
|
|
|
public static void Write(TGAFile tgaFile, Stream stream)
|
|
{
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
{
|
|
Write(tgaFile, writer);
|
|
writer.Close();
|
|
stream.Close();
|
|
}
|
|
|
|
}
|
|
|
|
public static void Write(TGAFile tgaFile, string filename)
|
|
{
|
|
string newFileName = Path.ChangeExtension(filename, ".tmp.tga");
|
|
|
|
try
|
|
{
|
|
using (FileStream stream = new FileStream(newFileName, FileMode.CreateNew, FileAccess.Write))
|
|
{
|
|
Write(tgaFile, stream);
|
|
}
|
|
if (File.Exists(newFileName))
|
|
{
|
|
File.Delete(filename);
|
|
File.Move(newFileName, filename);
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
if (File.Exists(newFileName))
|
|
{
|
|
File.Delete(newFileName);
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public static bool SetRemains(byte[] remains, TGAFile toCast)
|
|
{
|
|
TGAFileEx tgaFile = toCast as TGAFileEx;
|
|
|
|
if (null == tgaFile)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
tgaFile.Remains = remains;
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Protected Methods
|
|
|
|
protected static TGAFile Load(BinaryReader reader)
|
|
{
|
|
TGAFileEx tgaFile = new TGAFileEx();
|
|
TGAHeader infos = tgaFile.Infos;
|
|
|
|
// Read header
|
|
infos.id_length = reader.ReadByte();
|
|
infos.color_map_type = reader.ReadByte();
|
|
infos.image_type = reader.ReadByte();
|
|
infos.cm_first_entry = reader.ReadUInt16();
|
|
infos.cm_length = reader.ReadUInt16();
|
|
infos.cm_size = reader.ReadByte();
|
|
infos.is_xorigin = reader.ReadUInt16();
|
|
infos.is_yorigin = reader.ReadUInt16();
|
|
infos.is_width = reader.ReadUInt16();
|
|
infos.is_height = reader.ReadUInt16();
|
|
infos.is_pixel_depth = reader.ReadByte();
|
|
infos.is_image_descriptor = reader.ReadByte();
|
|
|
|
// Read ID length and ID if there are some
|
|
if(0 != infos.id_length)
|
|
{
|
|
tgaFile.ID = reader.ReadBytes(infos.id_length);
|
|
}
|
|
|
|
// Read Indice if there are some
|
|
if (1 == infos.color_map_type)
|
|
{
|
|
int b = (infos.cm_length*(infos.cm_size/8));
|
|
|
|
tgaFile.Indice = reader.ReadBytes(b);
|
|
}
|
|
|
|
Action<TGAFileEx, BinaryReader> readProcedure;
|
|
|
|
if (TGALoaderRead.ReadProc.TryGetValue(infos.image_type, out readProcedure))
|
|
{
|
|
readProcedure(tgaFile, reader);
|
|
}
|
|
|
|
using (MemoryStream remains = new MemoryStream())
|
|
{
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
remains.WriteByte(reader.ReadByte());
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
}
|
|
tgaFile.Remains = remains.ToArray();
|
|
}
|
|
|
|
return tgaFile;
|
|
}
|
|
|
|
protected static void Write(TGAFile tgaFile, BinaryWriter writer)
|
|
{
|
|
TGAHeader infos = tgaFile.Infos;
|
|
|
|
// Write header
|
|
writer.Write(infos.id_length);
|
|
writer.Write(infos.color_map_type);
|
|
writer.Write(infos.image_type);
|
|
writer.Write(infos.cm_first_entry);
|
|
writer.Write(infos.cm_length);
|
|
writer.Write(infos.cm_size);
|
|
writer.Write(infos.is_xorigin);
|
|
writer.Write(infos.is_yorigin);
|
|
writer.Write(infos.is_width);
|
|
writer.Write(infos.is_height);
|
|
writer.Write(infos.is_pixel_depth);
|
|
writer.Write(infos.is_image_descriptor);
|
|
|
|
// Write IDs
|
|
if (0 != infos.id_length)
|
|
{
|
|
writer.Write(infos.id_length);
|
|
}
|
|
|
|
// Write Color types
|
|
if (1 == infos.color_map_type)
|
|
{
|
|
writer.Write(tgaFile.Indice);
|
|
}
|
|
|
|
// Pixel buffer is backward
|
|
Action<TGAFile, BinaryWriter> writeProcedure;
|
|
|
|
if (TGALoaderWrite.WriteProc.TryGetValue(infos.image_type, out writeProcedure))
|
|
{
|
|
writeProcedure(tgaFile, writer);
|
|
}
|
|
|
|
byte[] remains = tgaFile.Remains;
|
|
|
|
if (null != remains && remains.Length > 0)
|
|
{
|
|
writer.Write(remains);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|