C# IFF图形结构解析代码
图形数据区的数据存放也和别的图形文件有天地之区别。
例如 256色图形 第1个字节放的是 前8个相素的最第位 后面依次类推。知道相素的最后一个最底位完毕并补码后 才是相素的底2位。
也就是说 宽为16的图形 第1-2字节 是放最低位 3-4 放的低2位 5-6为3位 。。。一直到 15-16 为最高位
24位色 的 也是这样 就是先是 R数据 然后是G流 最后是B
使用方法
代码如下:
//显示RSB文件
ImageIFF _Iff = new ImageIFF(@"D:\temp\4.IFF");
pictureBox1.Image = _Iff.Image;
//保存
ImageIFF _Iff = new ImageIFF();
_Iff.Image = (Bitmap)Image.FromFile(@"d:\TEMP\1.bmp");
_Iff.SaveImage(@"d:\temp\ok.IFF");
/// <summary>
/// IFF文件结构
/// zgke@sina.com
/// QQ:116149
/// </summary>
public class ImageIFF
{
private uint m_Heard = 0x4d524f46; //FROM
private uint m_FileSize = 0;
private uint m_FileType = 0x4d424c49; //ILBM
private IList<ImageIFFOfANNO> m_ANNO = new List<ImageIFFOfANNO>();
private ImageIffOfBMHD m_BMHD = new ImageIffOfBMHD();
private ImageIffOfCMAP m_CMAP = new ImageIffOfCMAP();
/// <summary>
/// 图形区域
/// </summary>
private Bitmap m_Bitmap;
/// <summary>
/// 描述字段
/// </summary>
public IList<ImageIFFOfANNO> ANNO { get { return m_ANNO; } set { m_ANNO = value; } }
/// <summary>
/// 图形区域
/// </summary>
public Bitmap Image { get { return m_Bitmap; } set { m_Bitmap = value; } }
public ImageIFF()
{
}
public ImageIFF(string p_FileName)
{
if (!File.Exists(p_FileName)) throw new Exception("文件不存在!");
byte[] _Bytes = File.ReadAllBytes(p_FileName);
if (BitConverter.ToInt32(_Bytes, 0) != m_Heard) throw new Exception("文件不是IFF文件!");
m_FileSize = BytesToUint(_Bytes, 4);
if (m_FileSize != _Bytes.Length - 8) throw new Exception("文件大小不正确!");
if (BitConverter.ToInt32(_Bytes, 8) != m_FileType) throw new Exception("文件格式不正确!");
LoadData(_Bytes, 12);
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="p_FileBytes"></param>
/// <param name="p_Index">开始位置</param>
private void LoadData(byte[] p_FileBytes, uint p_Index)
{
if (p_Index >= p_FileBytes.Length) return;
uint _ReadIndex = p_Index + 4;
string _TypeText = Encoding.ASCII.GetString(p_FileBytes, (int)p_Index, 4);
switch (_TypeText)
{
case "ANNO": //描述表
m_ANNO.Add(new ImageIFFOfANNO(p_FileBytes, ref _ReadIndex));
break;
case "BMHD": //图形属性表
m_BMHD = new ImageIffOfBMHD(p_FileBytes, ref _ReadIndex);
break;
case "CMAP": //颜色表
m_CMAP = new ImageIffOfCMAP(p_FileBytes, ref _ReadIndex);
break;
case "BODY": //图形区域
LoadImage(p_FileBytes, ref _ReadIndex);
break;
}
LoadData(p_FileBytes, _ReadIndex);
}
/// <summary>
/// 获取图形数据
/// </summary>
/// <param name="p_FileBytes"></param>
/// <param name="p_ReadIndex"></param>
private void LoadImage(byte[] p_FileBytes, ref uint p_ReadIndex)
{
uint _Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Bitmap = new Bitmap(m_BMHD.Width, m_BMHD.Height, m_BMHD.Pixel);
BitmapData _BitmapData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, m_Bitmap.PixelFormat);
byte[] _WriteBytes = new byte[_BitmapData.Stride * _BitmapData.Height];
uint _ReadStride = _Size / (uint)_BitmapData.Height / 8;
int _WriteIndex = 0;
byte[] _ReadBytes = new byte[_Size / (uint)_BitmapData.Height];
int _ReadBitArray = (int)_ReadStride * 8;
switch (m_Bitmap.PixelFormat)
{
default:
throw new Exception("未实现");
case PixelFormat.Format8bppIndexed:
for (int i = 0; i != _BitmapData.Height; i++)
{
_WriteIndex = i * _BitmapData.Stride;
Array.Copy(p_FileBytes, p_ReadIndex, _ReadBytes, 0, _ReadBytes.Length); //复制一行数据到缓冲区
p_ReadIndex += (uint)_ReadBytes.Length; //定义到下一行获取的位置
System.Collections.BitArray _ReadArray = new System.Collections.BitArray(_ReadBytes); //1是true 0是false
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(new byte[] { 0 }); //1是true 0是false
for (int z = 0; z != _BitmapData.Width; z++)
{
int _ReadIndex = ((z / 8) * 8) + 7 - (z % 8); //获取顺序为 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[0] = _ReadArray[_ReadIndex];
_WriteArray[1] = _ReadArray[_ReadBitArray + _ReadIndex];
_WriteArray[2] = _ReadArray[(_ReadBitArray * 2) + _ReadIndex];
_WriteArray[3] = _ReadArray[(_ReadBitArray * 3) + _ReadIndex];
_WriteArray[4] = _ReadArray[(_ReadBitArray * 4) + _ReadIndex];
_WriteArray[5] = _ReadArray[(_ReadBitArray * 5) + _ReadIndex];
_WriteArray[6] = _ReadArray[(_ReadBitArray * 6) + _ReadIndex];
_WriteArray[7] = _ReadArray[(_ReadBitArray * 7) + _ReadIndex];
_WriteArray.CopyTo(_WriteBytes, _WriteIndex + z);
}
}
#region 设置颜色表
ColorPalette _Palette = m_Bitmap.Palette;
for (int i = 0; i != m_CMAP.ColorList.Length; i++)
{
_Palette.Entries[i] = m_CMAP.ColorList[i];
}
m_Bitmap.Palette = _Palette;
#endregion
break;
case PixelFormat.Format24bppRgb:
int _Stride = _ReadBitArray / 3; //行内便宜 也就是行的点数的 (X/16)*16+(X%16==0?0:16);
_ReadBitArray = (int)_Size / _BitmapData.Height / 3 * 8; //所有颜色分量后的色彩位置 也就是所有行的位置
for (int i = 0; i != _BitmapData.Height; i++)
{
_WriteIndex = i * _BitmapData.Stride;
Array.Copy(p_FileBytes, p_ReadIndex, _ReadBytes, 0, _ReadBytes.Length); //复制一行数据到缓冲区
p_ReadIndex += (uint)_ReadBytes.Length; //定义到下一行获取的位置
System.Collections.BitArray _ReadArray = new System.Collections.BitArray(_ReadBytes); //1是true 0是false
System.Collections.BitArray _WriteRedArray = new System.Collections.BitArray(new byte[] { 0 }); //1是true 0是false
System.Collections.BitArray _WriteGreenArray = new System.Collections.BitArray(new byte[] { 0 }); //1是true 0是false
System.Collections.BitArray _WriteBlueArray = new System.Collections.BitArray(new byte[] { 0 }); //1是true 0是false
for (int z = 0; z != _BitmapData.Width; z++)
{
int _ReadIndex = ((z / 8) * 8) + 7 - (z % 8); //获取顺序为 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteRedArray[0] = _ReadArray[_ReadIndex];
_WriteRedArray[1] = _ReadArray[_Stride + _ReadIndex];
_WriteRedArray[2] = _ReadArray[(_Stride * 2) + _ReadIndex];
_WriteRedArray[3] = _ReadArray[(_Stride * 3) + _ReadIndex];
_WriteRedArray[4] = _ReadArray[(_Stride * 4) + _ReadIndex];
_WriteRedArray[5] = _ReadArray[(_Stride * 5) + _ReadIndex];
_WriteRedArray[6] = _ReadArray[(_Stride * 6) + _ReadIndex];
_WriteRedArray[7] = _ReadArray[(_Stride * 7) + _ReadIndex]; //红色
_WriteGreenArray[0] = _ReadArray[_ReadBitArray + _ReadIndex];
_WriteGreenArray[1] = _ReadArray[_ReadBitArray + _Stride + _ReadIndex];
_WriteGreenArray[2] = _ReadArray[_ReadBitArray + (_Stride * 2) + _ReadIndex];
_WriteGreenArray[3] = _ReadArray[_ReadBitArray + (_Stride * 3) + _ReadIndex];
_WriteGreenArray[4] = _ReadArray[_ReadBitArray + (_Stride * 4) + _ReadIndex];
_WriteGreenArray[5] = _ReadArray[_ReadBitArray + (_Stride * 5) + _ReadIndex];
_WriteGreenArray[6] = _ReadArray[_ReadBitArray + (_Stride * 6) + _ReadIndex];
_WriteGreenArray[7] = _ReadArray[_ReadBitArray + (_Stride * 7) + _ReadIndex]; //绿
_WriteBlueArray[0] = _ReadArray[_ReadBitArray * 2 + _ReadIndex];
_WriteBlueArray[1] = _ReadArray[_ReadBitArray * 2 + _Stride + _ReadIndex];
_WriteBlueArray[2] = _ReadArray[_ReadBitArray * 2 + (_Stride * 2) + _ReadIndex];
_WriteBlueArray[3] = _ReadArray[_ReadBitArray * 2 + (_Stride * 3) + _ReadIndex];
_WriteBlueArray[4] = _ReadArray[_ReadBitArray * 2 + (_Stride * 4) + _ReadIndex];
_WriteBlueArray[5] = _ReadArray[_ReadBitArray * 2 + (_Stride * 5) + _ReadIndex];
_WriteBlueArray[6] = _ReadArray[_ReadBitArray * 2 + (_Stride * 6) + _ReadIndex];
_WriteBlueArray[7] = _ReadArray[_ReadBitArray * 2 + (_Stride * 7) + _ReadIndex]; //蓝
_WriteRedArray.CopyTo(_WriteBytes, _WriteIndex + z * 3 + 2);
_WriteGreenArray.CopyTo(_WriteBytes, _WriteIndex + z * 3 + 1);
_WriteBlueArray.CopyTo(_WriteBytes, _WriteIndex + z * 3);
}
}
break;
}
Marshal.Copy(_WriteBytes, 0, _BitmapData.Scan0, _WriteBytes.Length);
m_Bitmap.UnlockBits(_BitmapData);
}
/// <summary>
/// 保存数据到文件
/// </summary>
/// <param name="p_File"></param>
public void SaveImage(string p_File)
{
if (m_Bitmap == null) return;
m_ANNO.Add(new ImageIFFOfANNO("IFF-File C#ImageIFF zgke@sina.com"));
System.IO.File.WriteAllBytes(p_File, GetFileBytes());
}
/// <summary>
/// 保存图形
/// </summary>
/// <returns></returns>
private byte[] GetFileBytes()
{
MemoryStream _Stream = new MemoryStream();
_Stream.Write(BitConverter.GetBytes(m_Heard), 0, 4);
_Stream.Write(new byte[] { 0, 0, 0, 0 }, 0, 4);
_Stream.Write(BitConverter.GetBytes(m_FileType), 0, 4);
byte[] _WriteBytes = new byte[0];
for (int i = 0; i != m_ANNO.Count; i++)
{
_WriteBytes = m_ANNO[i].GetBytes();
_Stream.Write(_WriteBytes, 0, _WriteBytes.Length);
}
_WriteBytes = m_BMHD.GetBytes(m_Bitmap);
_Stream.Write(_WriteBytes, 0, _WriteBytes.Length);
switch (m_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Stream.Write(new byte[] { 0x43, 0x4D, 0x41, 0x50 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((uint)768), 0, 4);
for (int i = 0; i != 256; i++)
{
_Stream.Write(new byte[] { m_Bitmap.Palette.Entries[i].R, m_Bitmap.Palette.Entries[i].G, m_Bitmap.Palette.Entries[i].B }, 0, 3);
}
break;
default:
break;
}
byte[] _ImageBytes = SaveImage();
_Stream.Write(new byte[] { 0x42, 0x4F, 0x44, 0x59 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((uint)_ImageBytes.Length), 0, 4);
_Stream.Write(_ImageBytes, 0, _ImageBytes.Length);
m_FileSize = (uint)_Stream.Length - 8;
_Stream.Position = 4;
_Stream.Write(ImageIFF.UintToBytes(m_FileSize), 0, 4);
return _Stream.ToArray();
}
private byte[] SaveImage()
{
Bitmap _NewBitmap;
BitmapData _Data;
byte[] _ReadBytes;
int _ReadStride = 0;
int _WriteBitArray = 0;
int _Stride = 0;
int _WidthOne = 0;
int _WidthAll = 0;
byte[] _WriteBytes;
byte[] _WriteRowBytes;
int _ReadIndex = 0;
int _WriteIndex = 0;
switch (m_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Data = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
_ReadBytes = new byte[_Data.Stride * _Data.Height];
_ReadStride = _Data.Stride;
Marshal.Copy(_Data.Scan0, _ReadBytes, 0, _ReadBytes.Length);
m_Bitmap.UnlockBits(_Data);
_WidthOne = (m_Bitmap.Width / 16) * 16 + (m_Bitmap.Width % 16 == 0 ? 0 : 16);
_WidthAll = _WidthOne * m_Bitmap.Height;
_WriteBytes = new byte[_WidthAll];
_WriteRowBytes = new byte[_WidthOne];
_WriteBitArray = _WidthOne;
for (int i = 0; i != m_Bitmap.Height; i++)
{
_ReadIndex = i * _ReadStride;
Array.Copy(_WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes, 0, _WriteRowBytes.Length);
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(_WriteRowBytes);
for (int z = 0; z != m_Bitmap.Width; z++)
{
System.Collections.BitArray _ColorArray = new System.Collections.BitArray(new byte[] { _ReadBytes[_ReadIndex + z] });
_WriteIndex = ((z / 8) * 8) + 7 - (z % 8); //获取顺序为 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[_WriteIndex] = _ColorArray[0];
_WriteArray[_WriteBitArray + _WriteIndex] = _ColorArray[1];
_WriteArray[(_WriteBitArray * 2) + _WriteIndex] = _ColorArray[2];
_WriteArray[(_WriteBitArray * 3) + _WriteIndex] = _ColorArray[3];
_WriteArray[(_WriteBitArray * 4) + _WriteIndex] = _ColorArray[4];
_WriteArray[(_WriteBitArray * 5) + _WriteIndex] = _ColorArray[5];
_WriteArray[(_WriteBitArray * 6) + _WriteIndex] = _ColorArray[6];
_WriteArray[(_WriteBitArray * 7) + _WriteIndex] = _ColorArray[7];
}
_WriteArray.CopyTo(_WriteRowBytes, 0);
Array.Copy(_WriteRowBytes, 0, _WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes.Length);
}
return _WriteBytes;
break;
default:
_NewBitmap = new Bitmap(m_Bitmap.Width, m_Bitmap.Height, PixelFormat.Format24bppRgb);
Graphics _Graphics = Graphics.FromImage(_NewBitmap);
_Graphics.DrawImage(m_Bitmap, new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height));
_Graphics.Dispose();
_Data = _NewBitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
_ReadBytes = new byte[_Data.Stride * _Data.Height];
_ReadStride = _Data.Stride;
Marshal.Copy(_Data.Scan0, _ReadBytes, 0, _ReadBytes.Length);
_NewBitmap.UnlockBits(_Data);
_WidthOne = (_NewBitmap.Width / 16) * 16 + (_NewBitmap.Width % 16 == 0 ? 0 : 16);
_WidthAll = _WidthOne * 3 * _NewBitmap.Height;
_WriteBytes = new byte[_WidthAll];
_WriteRowBytes = new byte[_WidthOne * 3];
_ReadIndex = 0;
_WriteIndex = 0;
_WriteBitArray = _WidthOne * 8;
_Stride = _WidthOne;
for (int i = 0; i != _NewBitmap.Height; i++)
{
_ReadIndex = i * _ReadStride;
Array.Copy(_WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes, 0, _WriteRowBytes.Length);
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(_WriteRowBytes);
for (int z = 0; z != _NewBitmap.Width; z++)
{
byte[] _Color = new byte[] { _ReadBytes[_ReadIndex + (z * 3) + 2], _ReadBytes[_ReadIndex + (z * 3) + 1], _ReadBytes[_ReadIndex + (z * 3)] };
System.Collections.BitArray _ColorArray = new System.Collections.BitArray(_Color);
_WriteIndex = ((z / 8) * 8) + 7 - (z % 8); //获取顺序为 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[_WriteIndex] = _ColorArray[0];
_WriteArray[_Stride + _WriteIndex] = _ColorArray[1];
_WriteArray[(_Stride * 2) + _WriteIndex] = _ColorArray[2];
_WriteArray[(_Stride * 3) + _WriteIndex] = _ColorArray[3];
_WriteArray[(_Stride * 4) + _WriteIndex] = _ColorArray[4];
_WriteArray[(_Stride * 5) + _WriteIndex] = _ColorArray[5];
_WriteArray[(_Stride * 6) + _WriteIndex] = _ColorArray[6];
_WriteArray[(_Stride * 7) + _WriteIndex] = _ColorArray[7]; //红色
_WriteArray[_WriteBitArray + _WriteIndex] = _ColorArray[8];
_WriteArray[_WriteBitArray + _Stride + _WriteIndex] = _ColorArray[9];
_WriteArray[_WriteBitArray + (_Stride * 2) + _WriteIndex] = _ColorArray[10];
_WriteArray[_WriteBitArray + (_Stride * 3) + _WriteIndex] = _ColorArray[11];
_WriteArray[_WriteBitArray + (_Stride * 4) + _WriteIndex] = _ColorArray[12];
_WriteArray[_WriteBitArray + (_Stride * 5) + _WriteIndex] = _ColorArray[13];
_WriteArray[_WriteBitArray + (_Stride * 6) + _WriteIndex] = _ColorArray[14];
_WriteArray[_WriteBitArray + (_Stride * 7) + _WriteIndex] = _ColorArray[15]; //绿
_WriteArray[_WriteBitArray * 2 + _WriteIndex] = _ColorArray[16];
_WriteArray[_WriteBitArray * 2 + _Stride + _WriteIndex] = _ColorArray[17];
_WriteArray[_WriteBitArray * 2 + (_Stride * 2) + _WriteIndex] = _ColorArray[18];
_WriteArray[_WriteBitArray * 2 + (_Stride * 3) + _WriteIndex] = _ColorArray[19];
_WriteArray[_WriteBitArray * 2 + (_Stride * 4) + _WriteIndex] = _ColorArray[20];
_WriteArray[_WriteBitArray * 2 + (_Stride * 5) + _WriteIndex] = _ColorArray[21];
_WriteArray[_WriteBitArray * 2 + (_Stride * 6) + _WriteIndex] = _ColorArray[22];
_WriteArray[_WriteBitArray * 2 + (_Stride * 7) + _WriteIndex] = _ColorArray[23]; //蓝
}
_WriteArray.CopyTo(_WriteRowBytes, 0);
Array.Copy(_WriteRowBytes, 0, _WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes.Length);
}
_NewBitmap.Dispose();
return _WriteBytes;
break;
}
return new byte[0];
}
#region 结构类
/// <summary>
/// ANNO描述字段
/// </summary>
public class ImageIFFOfANNO
{
public ImageIFFOfANNO(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Text = Encoding.Default.GetString(p_FileBytes, (int)p_ReadIndex, (int)m_Size);
p_ReadIndex += m_Size;
}
public ImageIFFOfANNO(string p_Txt)
{
Text = p_Txt;
}
public ImageIFFOfANNO()
{
}
private uint m_Size = 0;
private string m_Text = "";
public string Text
{
get
{
return m_Text;
}
set
{
m_Text = value;
}
}
/// <summary>
/// 获取对应的字节数
/// </summary>
/// <returns></returns>
public byte[] GetBytes()
{
if (m_Text.Length == 0) return new byte[0];
MemoryStream _Stream = new MemoryStream();
_Stream.Write(new byte[] { 0x41, 0x4E, 0x4E, 0x4F }, 0, 4);
byte[] _TextBytes = Encoding.Default.GetBytes(m_Text);
int _Count = _TextBytes.Length;
if (_TextBytes.Length % 2 != 0) _Count++;
byte[] _ValueBytes = ImageIFF.UintToBytes((uint)_Count);
_Stream.Write(_ValueBytes, 0, _ValueBytes.Length);
_Stream.Write(_TextBytes, 0, _TextBytes.Length);
if (_TextBytes.Length % 2 != 0) _Stream.Write(new byte[] { 0x20 }, 0, 1);
return _Stream.ToArray();
}
}
/// <summary>
/// 图形设置
/// </summary>
private class ImageIffOfBMHD
{
/// <summary>
/// 区域大小
/// </summary>
private uint m_Size = 20;
private ushort m_Width = 0;
private ushort m_Height = 0;
private ushort m_X = 0;
private ushort m_Y = 0;
private PixelFormat m_Pixel = PixelFormat.Format24bppRgb;
private byte m_Masking = 0;
private byte m_Compression = 0;
private byte m_Padl = 0;
private ushort m_TColor = 0;
private byte m_XAspect = 0;
private byte m_YAspect = 0;
private ushort m_PWidth = 0;
private ushort m_PHeight = 0;
/// <summary>
/// 0不压缩 1压缩
/// </summary>
public byte Compression { get { return m_Compression; } set { m_Compression = value; } }
/// <summary>
/// 宽
/// </summary>
public ushort Width { get { return m_Width; } set { m_Width = value; } }
/// <summary>
/// 高
/// </summary>
public ushort Height { get { return m_Height; } set { m_Height = value; } }
/// <summary>
/// 颜色数
/// </summary>
public PixelFormat Pixel { get { return m_Pixel; } set { m_Pixel = value; } }
public ImageIffOfBMHD()
{
}
public ImageIffOfBMHD(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Width = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_Height = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_X = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_Y = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
switch (p_FileBytes[p_ReadIndex])
{
case 8:
m_Pixel = PixelFormat.Format8bppIndexed;
break;
case 24:
m_Pixel = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("未实现!");
}
p_ReadIndex++;
m_Masking = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_Compression = p_FileBytes[p_ReadIndex];
if (m_Compression != 0) throw new Exception("未实现RLE压缩的IFF图形");
p_ReadIndex++;
m_Padl = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_TColor = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_XAspect = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_YAspect = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_PWidth = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex); ;
p_ReadIndex += 2;
m_PHeight = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex); ;
p_ReadIndex += 2;
}
/// <summary>
/// 根据图形获取数据集
/// </summary>
/// <param name="p_Bitmap"></param>
/// <returns></returns>
public byte[] GetBytes(Bitmap p_Bitmap)
{
if (p_Bitmap == null) return new byte[0];
MemoryStream _Stream = new MemoryStream();
_Stream.Write(new byte[] { 0x42, 0x4D, 0x48, 0x44 }, 0, 4);
_Stream.Write(new byte[] { 0, 0, 0, 0x14 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Width), 0, 2);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Height), 0, 2);
_Stream.Write(ImageIFF.UintToBytes(m_X), 0, 2);
_Stream.Write(ImageIFF.UintToBytes(m_Y), 0, 2);
switch (p_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Stream.WriteByte(8);
break;
default:
_Stream.WriteByte(24);
break;
}
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.Write(ImageIFF.UintToBytes(m_TColor), 0, 2);
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Width), 0, 2);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Height), 0, 2);
return _Stream.ToArray();
}
}
/// <summary>
/// 颜色表
/// </summary>
private class ImageIffOfCMAP
{
/// <summary>
/// 区域大小
/// </summary>
private uint m_Size = 20;
/// <summary>
/// 颜色表
/// </summary>
private Color[] m_ColorList = new Color[256];
/// <summary>
/// 颜色表
/// </summary>
public Color[] ColorList { get { return m_ColorList; } set { m_ColorList = value; m_Size = (uint)m_ColorList.Length * 3; } }
public ImageIffOfCMAP()
{
}
public ImageIffOfCMAP(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
int _Count = (int)m_Size / 3;
m_ColorList = new Color[_Count];
for (int i = 0; i != _Count; i++)
{
m_ColorList[i] = Color.FromArgb(p_FileBytes[p_ReadIndex], p_FileBytes[p_ReadIndex + 1], p_FileBytes[p_ReadIndex + 2]);
p_ReadIndex += 3;
}
}
}
#endregion
#region 数据转换
/// <summary>
/// 字节转换为UINT
/// </summary>
/// <param name="p_Value">字节数组</param>
/// <param name="p_Index">开始位置</param>
/// <returns></returns>
public static uint BytesToUint(byte[] p_Value, int p_Index)
{
byte[] _ValueBytes = new byte[4];
_ValueBytes[0] = p_Value[p_Index + 3];
_ValueBytes[1] = p_Value[p_Index + 2];
_ValueBytes[2] = p_Value[p_Index + 1];
_ValueBytes[3] = p_Value[p_Index];
return BitConverter.ToUInt32(_ValueBytes, 0);
}
/// <summary>
/// 获取反转的BYTES
/// </summary>
/// <param name="p_Value"></param>
/// <returns></returns>
public static byte[] UintToBytes(uint p_Value)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Value);
Array.Reverse(_ValueBytes);
return _ValueBytes;
}
/// <summary>
/// 字节转换为UINT
/// </summary>
/// <param name="p_Value">字节数组</param>
/// <param name="p_Index">开始位置</param>
/// <returns></returns>
public static ushort BytesToShort(byte[] p_Value, int p_Index)
{
byte[] _ValueBytes = new byte[2];
_ValueBytes[0] = p_Value[p_Index + 1];
_ValueBytes[1] = p_Value[p_Index];
return BitConverter.ToUInt16(_ValueBytes, 0);
}
/// <summary>
/// 获取反转的BYTES
/// </summary>
/// <param name="p_Value"></param>
/// <returns></returns>
public static byte[] UintToBytes(ushort p_Value)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Value);
Array.Reverse(_ValueBytes);
return _ValueBytes;
}
#endregion
}