C# 解析 SUM 光栅文件图象 (RAS文件)

使用方法、

 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......B....

不知道怎么想的。

项目多了很少有时间做这些东西了。下个目标是IFF文件

全部代码

  1. using  System;  
  2. using  System.Collections.Generic;  
  3. using  System.Text;  
  4. using  System.Runtime.InteropServices;  
  5. using  System.Drawing.Imaging;  
  6. using  System.Drawing;  
  7. using  System.IO;  
  8.   
  9. namespace  Zgke.MyImage.ImageFile  
  10. {  
  11.     /// <summary>   
  12.     /// SUN光栅图形 RAS   
  13.     /// zgke@sina.com     
  14.     /// qq:116149   
  15.     /// </summary>   
  16.     public   class  ImageRas  
  17.     {  
  18.         public  ImageRas( string  p_ImageFile)  
  19.         {  
  20.             if  (System.IO.File.Exists(p_ImageFile))  
  21.             {  
  22.                 LoadImage(System.IO.File.ReadAllBytes(p_ImageFile));  
  23.             }  
  24.         }  
  25.   
  26.         public  ImageRas()  
  27.         {  
  28.         }  
  29.  
  30.         #region 私有   
  31.         /// <summary>   
  32.         /// 文件头 956AA659   
  33.         /// </summary>   
  34.         private   uint  m_Mageic = 0x956AA659;  
  35.   
  36.         /// <summary>   
  37.         /// 宽   
  38.         /// </summary>   
  39.         private   uint  m_Width = 0;  
  40.   
  41.         /// <summary>   
  42.         /// 高   
  43.         /// </summary>   
  44.         private   uint  m_Height = 0;  
  45.   
  46.         /// <summary>   
  47.         /// 颜色深   
  48.         /// </summary>   
  49.         private   uint  m_Depth = 0;  
  50.   
  51.         /// <summary>   
  52.         /// 图形区域数据大小   
  53.         /// </summary>   
  54.         private   uint  m_Length = 0;  
  55.   
  56.         /// <summary>   
  57.         /// 数据类型   
  58.         /// </summary>   
  59.         private   uint  m_Type = 0;  
  60.   
  61.         /// <summary>   
  62.         /// 色彩图形类型   
  63.         /// </summary>   
  64.         private   uint  m_MapType = 0;  
  65.   
  66.         /// <summary>   
  67.         /// 色彩长度   
  68.         /// </summary>   
  69.         private   uint  m_MapLength = 0;  
  70.   
  71.         /// <summary>   
  72.         /// 颜色表   
  73.         /// </summary>   
  74.         private  Color[] m_ColorList =  new  Color[256];  
  75.   
  76.         /// <summary>   
  77.         /// 图形   
  78.         /// </summary>   
  79.         private  Bitmap m_Image;  
  80.         #endregion   
  81.   
  82.         /// <summary>   
  83.         /// 获取图形   
  84.         /// </summary>   
  85.         public  Bitmap Image  
  86.         {  
  87.             get   
  88.             {  
  89.                 return  m_Image;  
  90.             }  
  91.             set   
  92.             {  
  93.                 if  (value !=  null )  
  94.                 {  
  95.                     m_Image = value;  
  96.                     m_Width = (uint )value.Width;  
  97.                     m_Height = (uint )value.Height;  
  98.                     switch  (value.PixelFormat)  
  99.                     {  
  100.                         case  PixelFormat.Format8bppIndexed:  
  101.   
  102.                             break ;  
  103.                         case  PixelFormat.Format32bppArgb:  
  104.   
  105.                             break ;  
  106.                         default :  
  107.                             m_Depth = 24;  
  108.                             break ;  
  109.                     }  
  110.                 }  
  111.             }  
  112.         }  
  113.   
  114.         /// <summary>   
  115.         /// 获取数据   
  116.         /// </summary>   
  117.         /// <param name="p_ImageBytes"></param>   
  118.         private   void  LoadImage( byte [] p_ImageBytes)  
  119.         {  
  120.             if  (BitConverter.ToUInt32(p_ImageBytes, 0) != m_Mageic)  throw   new  Exception( "文件头不正确!" );  
  121.             m_Width = BytesToUint(p_ImageBytes, 4);  
  122.             m_Height = BytesToUint(p_ImageBytes, 8);  
  123.             m_Depth = BytesToUint(p_ImageBytes, 12);  
  124.             m_Length = BytesToUint(p_ImageBytes, 16);  
  125.             m_Type = BytesToUint(p_ImageBytes, 20);  
  126.             m_MapType = BytesToUint(p_ImageBytes, 24);  
  127.             m_MapLength = BytesToUint(p_ImageBytes, 28);  
  128.             int  _StarIndex = 32;  
  129.             switch  (m_MapType)  
  130.             {  
  131.   
  132.                 case  1:  
  133.                     int  _ColorTable = ( int )m_MapLength / 3;  
  134.                     for  ( int  i = 0; i != _ColorTable; i++)  
  135.                     {  
  136.                         m_ColorList[i] = Color.FromArgb(p_ImageBytes[_StarIndex], p_ImageBytes[_StarIndex + _ColorTable], p_ImageBytes[_StarIndex + (_ColorTable * 2)]);  
  137.                         _StarIndex++;  
  138.                     }  
  139.                     _StarIndex += _ColorTable * 2;  
  140.                     break ;  
  141.                 default :  
  142.                     break ;  
  143.             }  
  144.   
  145.             LoadData(p_ImageBytes, _StarIndex);  
  146.         }  
  147.   
  148.         /// <summary>   
  149.         /// 字节转换为UINT   
  150.         /// </summary>   
  151.         /// <param name="p_Value"& gt;字节数组</param>   
  152.         /// <param name="p_Index">开始位置</param>   
  153.         /// <returns></returns>   
  154.         private   uint  BytesToUint( byte [] p_Value,  int  p_Index)  
  155.         {  
  156.             byte [] _ValueBytes =  new   byte [4];  
  157.             _ValueBytes[0] = p_Value[p_Index + 3];  
  158.             _ValueBytes[1] = p_Value[p_Index + 2];  
  159.             _ValueBytes[2] = p_Value[p_Index + 1];  
  160.             _ValueBytes[3] = p_Value[p_Index];  
  161.             return  BitConverter.ToUInt32(_ValueBytes, 0);  
  162.         }  
  163.   
  164.         /// <summary>   
  165.         /// 获取反转的BYTES   
  166.         /// </summary>   
  167.         /// <param name="p_Value"></param>   
  168.         /// <returns></returns>   
  169.         private   byte [] UintToBytes( uint  p_Value)  
  170.         {  
  171.             byte [] _ValueBytes = BitConverter.GetBytes(p_Value);  
  172.             Array.Reverse(_ValueBytes);  
  173.             return  _ValueBytes;  
  174.         }  
  175.   
  176.         /// <summary>   
  177.         /// 获取图形数据   
  178.         /// </summary>   
  179.         /// <param name="p_ValueBytes"& gt;文件留</param>   
  180.         /// <param name="p_StarIndex">RGB留开始位置</param& gt;   
  181.         private   void  LoadData( byte [] p_ValueBytes,  int  p_StarIndex)  
  182.         {  
  183.             PixelFormat _Format = PixelFormat.Format24bppRgb;  
  184.             switch  (m_Depth)  
  185.             {  
  186.                 case  8:  
  187.                     _Format = PixelFormat.Format8bppIndexed;  
  188.                     break ;  
  189.                 case  24:  
  190.                     _Format = PixelFormat.Format24bppRgb;  
  191.                     break ;  
  192.                 default :  
  193.                     throw   new  Exception( " 未实现!" );  
  194.   
  195.             }  
  196.             m_Image = new  Bitmap(( int )m_Width, ( int )m_Height, _Format);  
  197.             BitmapData _Data = m_Image.LockBits(new  Rectangle(0, 0, m_Image.Width, m_Image.Height), ImageLockMode.ReadWrite, m_Image.PixelFormat);  
  198.             byte [] _WriteBytes =  new   byte [_Data.Height * _Data.Stride];  
  199.             int  _StarIndex = 0;  
  200.             int  _WriteIndex = 0;  
  201.   
  202.             for  ( int  i = 0; i != _Data.Height; i++)  
  203.             {  
  204.                 _WriteIndex = i * _Data.Stride;  
  205.                 _StarIndex = i * ((int )m_Length / ( int )m_Height) + p_StarIndex;  
  206.                 for  ( int  z = 0; z != _Data.Width; z++)  
  207.                 {  
  208.                     switch  (m_Depth)  
  209.                     {  
  210.                         case  8:  
  211.                             _WriteBytes[_WriteIndex] = p_ValueBytes[_StarIndex];  
  212.                             _WriteIndex++;  
  213.                             _StarIndex++;  
  214.                             break ;  
  215.                         case  24:  
  216.                             _WriteBytes[_WriteIndex] = p_ValueBytes[_StarIndex + 2];  
  217.                             _WriteBytes[_WriteIndex + 1] = p_ValueBytes[_StarIndex + 1];  
  218.                             _WriteBytes[_WriteIndex + 2] = p_ValueBytes[_StarIndex];  
  219.                             _WriteIndex += 3;  
  220.                             _StarIndex += 3;  
  221.                             break ;  
  222.                     }  
  223.                 }  
  224.             }  
  225.   
  226.             switch  (m_Depth)  
  227.             {  
  228.                 case  8:  
  229.                     ColorPalette _Palette = m_Image.Palette;  
  230.                     for  ( int  i = 0; i != m_ColorList.Length; i++)  
  231.                     {  
  232.                         _Palette.Entries[i] = m_ColorList[i];  
  233.                     }  
  234.                     m_Image.Palette = _Palette;  
  235.                     break ;  
  236.                 default :  
  237.                     break ;  
  238.             }  
  239.   
  240.   
  241.             Marshal.Copy(_WriteBytes, 0, _Data.Scan0, _WriteBytes.Length);  
  242.             m_Image.UnlockBits(_Data);  
  243.   
  244.         }  
  245.   
  246.         /// <summary>   
  247.         /// 保存图形   
  248.         /// </summary>   
  249.         /// <returns></returns>   
  250.         private   byte [] SaveImageOfRas()  
  251.         {  
  252.             if  (m_Image ==  nullreturn   new   byte [0];  
  253.             MemoryStream _Stream = new  MemoryStream();  
  254.             _Stream.Write(BitConverter.GetBytes(m_Mageic), 0, 4);  
  255.             _Stream.Write(UintToBytes(m_Width), 0, 4);  
  256.             _Stream.Write(UintToBytes(m_Height), 0, 4);  
  257.   
  258.             switch  (m_Depth)  
  259.             {  
  260.                 case  8:  
  261.                     BitmapData _Data256 = m_Image.LockBits(new  Rectangle(0, 0, m_Image.Width, m_Image.Height), ImageLockMode.ReadOnly, m_Image.PixelFormat);  
  262.                     byte [] _DataBytes =  new   byte [_Data256.Stride * _Data256.Height];  
  263.                     int  _Stride = _Data256.Stride;  
  264.                     Marshal.Copy(_Data256.Scan0, _DataBytes, 0, _DataBytes.Length);  
  265.                     m_Image.UnlockBits(_Data256);  
  266.   
  267.                     _Stream.Write(UintToBytes(8), 0, 4);  
  268.                     uint  _WidthCount = ( uint )m_Image.Width;  
  269.                     if  (m_Image.Width % 2 != 0) _WidthCount = ( uint )m_Image.Width + 1;  
  270.                     uint  _AllCount = _WidthCount * ( uint )m_Image.Height;  
  271.                     _Stream.Write(UintToBytes(_AllCount), 0, 4);  
  272.                     _Stream.Write(UintToBytes(0), 0, 4);  
  273.                     _Stream.Write(UintToBytes(1), 0, 4);  
  274.                     _Stream.Write(UintToBytes(768), 0, 4);  
  275.                     byte [] _RedBytes =  new   byte [256];  
  276.                     byte [] _GreenBytes =  new   byte [256];  
  277.                     byte [] _BlueBytes =  new   byte [256];  
  278.                     for  ( int  i = 0; i != 256; i++)  
  279.                     {  
  280.                         _RedBytes[i] = m_Image.Palette.Entries[i].R;  
  281.                         _GreenBytes[i] = m_Image.Palette.Entries[i].G;  
  282.                         _BlueBytes[i] = m_Image.Palette.Entries[i].B;  
  283.                     }  
  284.                     _Stream.Write(_RedBytes, 0, _RedBytes.Length);  
  285.                     _Stream.Write(_GreenBytes, 0, _GreenBytes.Length);  
  286.                     _Stream.Write(_BlueBytes, 0, _BlueBytes.Length);  
  287.                     byte [] _Write =  new   byte [_WidthCount];  
  288.                     for  ( int  i = 0; i != m_Image.Height; i++)  
  289.                     {  
  290.                         Array.Copy(_DataBytes, i * _Stride, _Write, 0, _WidthCount);  
  291.                         _Stream.Write(_Write, 0, _Write.Length);  
  292.                     }  
  293.                     break ;  
  294.                 default :  
  295.                     Bitmap _NewBitmap = new  Bitmap(m_Image.Width, m_Image.Height, PixelFormat.Format24bppRgb);  
  296.                     Graphics _Graphics = Graphics.FromImage(_NewBitmap);  
  297.                     _Graphics.DrawImage(m_Image, new  Rectangle(0, 0, m_Image.Width, m_Image.Height));  
  298.                     _Graphics.Dispose();  
  299.                     BitmapData _Data24 = _NewBitmap.LockBits(new  Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height), ImageLockMode.ReadOnly, _NewBitmap.PixelFormat);  
  300.                     byte [] _DataBytes24 =  new   byte [_Data24.Stride * _Data24.Height];  
  301.                     int  _Stride24 = _Data24.Stride;  
  302.                     Marshal.Copy(_Data24.Scan0, _DataBytes24, 0, _DataBytes24.Length);  
  303.                     _NewBitmap.UnlockBits(_Data24);  
  304.   
  305.                     _Stream.Write(UintToBytes(24), 0, 4);  
  306.                     uint  _WidthCount24 = ( uint )_NewBitmap.Width;  
  307.                     if  (_NewBitmap.Width % 2 != 0) _WidthCount24 = ( uint )_NewBitmap.Width + 1;  
  308.                     uint  _AllCount24 = _WidthCount24 * ( uint )_NewBitmap.Height * 3;  
  309.                     _WidthCount24 = _WidthCount24 * 3;  
  310.                     _Stream.Write(UintToBytes(_AllCount24), 0, 4);  
  311.                     _Stream.Write(UintToBytes(0), 0, 4);  
  312.                     _Stream.Write(UintToBytes(0), 0, 4);  
  313.                     _Stream.Write(UintToBytes(0), 0, 4);  
  314.                     byte [] _Write24 =  new   byte [0];  
  315.                     for  ( int  i = 0; i != m_Image.Height; i++)  
  316.                     {  
  317.                         _Write24 = new   byte [_WidthCount24];  
  318.                         int  _WriteIndex = 0;  
  319.                         int  _StarIndex = i * _Stride24;  
  320.                         for  ( int  z = 0; z != m_Image.Width; z++)  
  321.                         {  
  322.                             _Write24[_WriteIndex] = _DataBytes24[_StarIndex + 2];  
  323.                             _Write24[_WriteIndex + 1] = _DataBytes24[_StarIndex + 1];  
  324.                             _Write24[_WriteIndex + 2] = _DataBytes24[_StarIndex];  
  325.                             _WriteIndex += 3;  
  326.                             _StarIndex += 3;  
  327.                         }  
  328.   
  329.                         _Stream.Write(_Write24, 0, _Write24.Length);  
  330.                     }  
  331.                     _NewBitmap.Dispose();  
  332.                     break ;  
  333.             }  
  334.   
  335.   
  336.   
  337.   
  338.             byte [] _Return = _Stream.ToArray();  
  339.             return  _Return;  
  340.         }  
  341.   
  342.         /// <summary>   
  343.         /// 保存图形到RAS文件   
  344.         /// </summary>   
  345.         /// <param name="p_File"></param>   
  346.         public   void  SaveRas( string  p_File)  
  347.         {  
  348.             byte [] _Value = SaveImageOfRas();  
  349.             if  (_Value.Length != 0) File.WriteAllBytes(p_File, _Value);  
  350.   
  351.         }  
  352.     }  

  353. 来至http://blog.csdn.net/zgke/archive/2010/03/18/5393334.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值