195 lines
4.6 KiB
C#
195 lines
4.6 KiB
C#
using System;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
|
|
namespace Tga.Core
|
|
{
|
|
|
|
public class TGAFile
|
|
{
|
|
|
|
#region Constructor
|
|
|
|
protected TGAFile()
|
|
{
|
|
Infos = new TGAHeader();
|
|
Indice = null;
|
|
RGB = null;
|
|
A = null;
|
|
Content = null;
|
|
Remains = null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Properties
|
|
|
|
public TGAHeader Infos
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] Indice
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] RGB
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] A
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] ID
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] Content
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public byte[] Remains
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
public int Width
|
|
{
|
|
get
|
|
{
|
|
return Infos.is_width;
|
|
}
|
|
}
|
|
|
|
public int Height
|
|
{
|
|
get
|
|
{
|
|
return Infos.is_height;
|
|
}
|
|
}
|
|
|
|
public Size XY
|
|
{
|
|
get
|
|
{
|
|
return new Size(Infos.is_width, Infos.is_height);
|
|
}
|
|
}
|
|
|
|
public PixelFormat Format
|
|
{
|
|
get
|
|
{
|
|
switch (Infos.image_type)
|
|
{
|
|
case 10:
|
|
case 2: // 32 bits
|
|
if (Infos.is_pixel_depth > 24)
|
|
{
|
|
return PixelFormat.Format32bppArgb;
|
|
}
|
|
goto case 1;
|
|
|
|
case 1: // 24 bits
|
|
case 3:
|
|
case 9:
|
|
case 11:
|
|
return PixelFormat.Format24bppRgb;
|
|
}
|
|
|
|
return PixelFormat.Undefined;
|
|
}
|
|
}
|
|
|
|
public int BytePerPixel
|
|
{
|
|
get
|
|
{
|
|
switch (Format)
|
|
{
|
|
case PixelFormat.Format32bppArgb:
|
|
case PixelFormat.Format32bppPArgb:
|
|
return 4;
|
|
}
|
|
|
|
return 3;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
|
|
public Bitmap BuildBitmap()
|
|
{
|
|
Bitmap bmp = new Bitmap(Width, Height, Format);
|
|
int iBytePerPixel = BytePerPixel;
|
|
if (0 != iBytePerPixel)
|
|
{
|
|
int iWidth = bmp.Width;
|
|
int iHeight = bmp.Height;
|
|
int bytes = iWidth * iHeight * iBytePerPixel;
|
|
Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
|
|
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
|
|
IntPtr ptr = bmpData.Scan0;
|
|
|
|
byte[] buffer = RGB;
|
|
|
|
if (null != A)
|
|
{
|
|
int size = RGB.Length;
|
|
byte[] rgb = RGB;
|
|
byte[] a = A;
|
|
|
|
using (MemoryStream mem = new MemoryStream())
|
|
{
|
|
for (int i = 0; i < size; i += 3)
|
|
{
|
|
mem.WriteByte(rgb[i + 0]);
|
|
mem.WriteByte(rgb[i + 1]);
|
|
mem.WriteByte(rgb[i + 2]);
|
|
mem.WriteByte(a[i / 3]);
|
|
}
|
|
buffer = mem.ToArray();
|
|
}
|
|
}
|
|
System.Runtime.InteropServices.Marshal.Copy(buffer, 0, ptr, bytes);
|
|
|
|
// bmpData.Stride = -bmpData.Stride;
|
|
//
|
|
// if (3 == iBytePerPixel)
|
|
// {
|
|
// int iRow = iBytePerPixel * Width;
|
|
//
|
|
// for (int iCpt = 0; iCpt < bytes; iCpt += iRow)
|
|
// {
|
|
// IntPtr ptr = bmpData.Stride;
|
|
// System.Runtime.InteropServices.Marshal.Copy(&BGR[iCpt], bytes - 1 - iCpt, ptr, iRow);
|
|
// }
|
|
// }
|
|
|
|
bmp.UnlockBits(bmpData);
|
|
}
|
|
///bmp.Flags = 73744;
|
|
return bmp;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|