C# IFF图形结构解析代码
程序员文章站
2023-11-27 21:15:10
图形数据区的数据存放也和别的图形文件有天地之区别。 例如 256色图形 第1个字节放的是 前8个相素的最第位 后面依次类推。知道相素的最后一个最底位完毕并补码后 才是相素的...
图形数据区的数据存放也和别的图形文件有天地之区别。
例如 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
}
例如 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
}