using System;
using System.IO;
namespace Raw2Picture
{
class RawFileGenerator
{
public enum ByteFormat
{
ARGB8888 = 1,
RGBA8888 = 2
}
public string writeRawFile(string fileName, uint[] data, ByteFormat bf)
{
if (data == null)
{
return "writeRawFile error: no data";
}
byte[] byteData = new byte[data.Length * 4];
if (bf == ByteFormat.ARGB8888)
{
for (int i = 0; i < data.Length; i++)
{
byteData[i * 4 + 0] = (byte)((data[i] & 0xFF000000) >> 24);
byteData[i * 4 + 1] = (byte)((data[i] & 0x00FF0000) >> 16);
byteData[i * 4 + 2] = (byte)((data[i] & 0x0000FF00) >> 8);
byteData[i * 4 + 3] = (byte)((data[i] & 0x000000FF) >> 0);
}
}
else if (bf == ByteFormat.RGBA8888)
{
for (int i = 0; i < data.Length; i++)
{
byteData[i * 4 + 0] = (byte)((data[i] & 0x00FF0000) >> 16);
byteData[i * 4 + 1] = (byte)((data[i] & 0x0000FF00) >> 8);
byteData[i * 4 + 2] = (byte)((data[i] & 0x000000FF) >> 0);
byteData[i * 4 + 3] = (byte)((data[i] & 0xFF000000) >> 24);
}
}
else
{
return "writeRawFile error: ByteFormat not supported";
}
return writeRawFile(fileName, byteData);
}
public string writeRawFile(string fileName, byte[] data)
{
string error = "ok";
FileStream fs = null;
BinaryWriter bw = null;
if (data == null)
{
return "writeRawFile error: no data";
}
if (fileName == null)
{
return "writeRawFile error: no input file";
}
if (data.Length == 0)
{
return "writeRawFile error: data.Length is zero";
}
try
{
fs = new FileStream(fileName, FileMode.Create);
bw = new BinaryWriter(fs);
bw.Write(data, 0, data.Length);
}
catch (Exception ex)
{
error = ex.Message;
}
finally
{
if (bw != null)
{
bw.Close();
}
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
return error;
}
}
}