程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# IFF圖形結構解析代碼

C# IFF圖形結構解析代碼

編輯:C#基礎知識
圖形數據區的數據存放也和別的圖形文件有天地之區別。
例如 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文件結構
/// [email protected]
/// 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 [email protected]"));
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
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved