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
}

(0)

相关推荐

  • C#画笔Pen保存和恢复图形对象的设置方法

    本文实例讲述了C#画笔Pen保存和恢复图形对象的设置方法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespa

  • C#实现判断图形文件格式的方法

    本文简单讲述了C#实现判断图形文件格式的方法,非常实用.分享给大家供大家参考.具体方法如下: 1. 通过文件的扩展名来判断. 这种方法比较简单,但若是有人故意改下文件扩展名,这种方法就不起作用了. 2.通过C#自身提供的方法来进行判断(判断某种确定的图片类型). 示例如下: bool isJPG = false; System.Drawing.Image img = System.Drawing.Image.FromFile(filesName); if ( img.RawFormat.Equa

  • C#实现在图像中绘制文字图形的方法

    本文实例讲述了C#实现在图像中绘制文字图形的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using S

  • c#图片处理之图片裁剪成不规则图形

    为了让大家知道下面内容是否是自己想要的,我先发效果图. 好了,那就开始贴代码了 以下为一个按钮的事件,为裁剪准备图片.裁剪路径.保存路径 复制代码 代码如下: private void button1_Click(object sender, EventArgs e)        {            GraphicsPath path = new GraphicsPath();            Point[] p = {                            new

  • C#画笔Pen使用路径绘制图形的方法

    本文实例讲述了C#画笔Pen使用路径绘制图形的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namesp

  • 深入C# winform清除由GDI绘制出来的所有线条或图形的解决方法

    在C#winform应用程序中,可以用GDI绘制出线条或图形.1.在主窗体上绘制线条或图形 复制代码 代码如下: using (Graphics g = this.CreateGraphics())      {            g.DrawLine(Pens.Blue, new Point(10, 10), new Point(100, 100));      } 2.在指定的容器上绘制线条或图形,如在panel1上绘制 复制代码 代码如下: using (Graphics g = th

  • C#实现图形路径变换的方法

    本文实例讲述了C#实现图形路径变换的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace a

  • C#实现图形位置组合转换的方法

    本文实例讲述了C#实现图形位置组合转换的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace

  • 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

  • webpack 如何解析代码模块路径的实现

    前言 webpack是如何解析代码模块路径 webpack 中有一个很关键的模块 enhanced-resolve 就是处理依赖模块路径的解析的,这个模块可以说是 Node.js 那一套模块路径解析的增强版本,有很多可以自定义的解析配置. 模块解析规则 解析相对路径 查找相对当前模块的路径下是否有对应文件或文件夹 是文件则直接加载 是文件夹则继续查找文件夹下的 package.json 文件 有 package.json 文件则按照文件中 main 字段的文件名来查找文件 无 package.j

  • PHP JSON 数据解析代码

    使用此代码可以顺利解析人人连接网站POST获取的数据. 复制代码 代码如下: $json_string='{"id":1,"name":"jb51","email":"admin@jb51.net","interest":["wordpress","php"]} '; $obj=json_decode($json_string); echo $ob

  • 如何在node环境实现“get数据解析”代码实例

    1.自己写 const http = require('http'); http.createServer(function(req,res){ var get = {}; if(req.url.indexOf('?')!=-1){ var arr = req.url.split('?'); var url = arr[0]; var arr2 = arr[1].split('&'); for(var i=0;i<arr2.length;i++){ var arr3 = arr2[i].sp

  • PHP XML数据解析代码

    复制代码 代码如下: //xml string $xml_string="<?xml version='1.0'?> <users> <user id='398'> <name>Foo</name> <email>foo@bar.com</name> </user> <user id='867'> <name>Foobar</name> <email>f

  • PHP中PCRE正则解析代码详解

    一.前言 前面的博客里,有对字符集的解析.这里就不是字符集的事儿了,在PHP中很多函数的处理默认是unicode中的UTF-8编码格式.那么废话不多说,直接开始正题. 二.PHP函数mb_split解析 <?php $preg_strings = '测.试.一.下'; $preg_str = mb_split('.', $preg_strings); print_r($preg_str); 输出效果 Array( [0] => 测 [1] => 试 [2] => 一 [3] =&g

  • Android Retrofit2数据解析代码解析

    在弄数据解析这块,浪费了很长的时间,最开始一直觉得传过来用对象接收的,类型是json,往那个方式去想了.搞了很久. 后来看了别人写的才发觉,真是很简单,感谢 https://www.jianshu.com/p/d0081e8a7edc 给我启发. 加入的jar包 // Retrofit库 implementation 'com.squareup.retrofit2:retrofit:2.0.2' implementation 'com.squareup.retrofit2:converter-g

  • C# 解析 RAS文件 SUM 光栅文件图象的代码

    使用方法: 复制代码 代码如下: ImageRas _Ras = new ImageRas(@"D:\temp\test.ras"); pictureBox1.Image = _Ras.Image; _Ras.SaveRas(@"d:\temp\OK.ras"); 我只实现了24位色和8位色 这个结构也太简单了.只有文件头和数据区 .就是8位色的色彩表有些特殊 先是红色表 绿色表 蓝色表 平时都是 RGB.RGB 这样放 这东西居然RRRR.....GGG.....

  • XML解析四种方式代码示例详解

    XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四种:1.DOM解析:2.SAX解析:3.JDOM解析:4.DOM4J解析.其中前两种属于基础方法,是官方提供的平台无关的解析方式:后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台. 针对以下XML文件,会对四种方式进行详细描述: <?xml version="1.0&q

  • C++解析特殊符号tab、换行符号实例代码

    前言: 我们经常会遇到一些Linux内核信息需要,比如一个wifi数据,中间是用tab键盘隔开的,然后每一行用换行符进行区分,如下所示的数据 第一位置是:wifi名称 第二个位置是:信号强度 第三个位置是:信号类型2.4Ghz或者5Ghz 第四个位置是:加密类型 第五个类型是:MAC地址 遇到这样的数据,我们比较常见的C函数解析是用while循环去匹配字符串里面的'\t'(tab键),'\n'(换行符号)等,然后用C标准库里面str函数,例如strncpy进行复制或者strstr去查找,类似于下

随机推荐