C # code for parsing the SUM raster file image of RAS files-practical tips

Source: Internet
Author: User
How to use:
Copy Code code as follows:

Imageras _ras = new Imageras (@ "D:\temp\test.ras");
pictureBox1.Image = _ras.image;
_ras.saveras (@ "D:\temp\OK.ras");

I only achieved 24-bit color and 8-bit color This structure is too simple. Only file headers and data areas. is a 8-bit color table with some special
First Red table Green table Blue table is usually RGB, RGB so put this thing incredibly rrrr ... GGG ... B....
I don't know what to think.
There is very little time to do these things in the project. The next target is the IFF file.
All code
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;
Using System.Drawing.Imaging;
Using System.Drawing;
Using System.IO;
Namespace Zgke.MyImage.ImageFile
{
<summary>
Sun Raster Graphics RAS
Zgke@sina.com
qq:116149
</summary>
public class Imageras
{
Public Imageras (String p_imagefile)
{
if (System.IO.File.Exists (p_imagefile))
{
LoadImage (System.IO.File.ReadAllBytes (p_imagefile));
}
}
Public Imageras ()
{
}
#region Private
<summary>
File Header 956aa659
</summary>
Private UINT M_MAGEIC = 0x956aa659;
<summary>
Wide
</summary>
Private UINT m_width = 0;
<summary>
High
</summary>
Private UINT m_height = 0;
<summary>
Color depth
</summary>
Private UINT m_depth = 0;
<summary>
Plot Area data size
</summary>
Private UINT m_length = 0;
<summary>
Data type
</summary>
Private UINT M_type = 0;
<summary>
Color graphic Type
</summary>
Private UINT M_maptype = 0;
<summary>
Color length
</summary>
Private UINT m_maplength = 0;
<summary>
Color table
</summary>
Private color[] m_colorlist = new color[256];
<summary>
Graphics
</summary>
Private Bitmap m_image;
#endregion
<summary>
Get graphics
</summary>
Public Bitmap Image
{
Get
{
return m_image;
}
Set
{
if (value!= null)
{
M_image = value;
M_width = (UINT) value. Width;
M_height = (UINT) value. Height;
Switch (value. PixelFormat)
{
Case pixelformat.format8bppindexed:
Break
Case PIXELFORMAT.FORMAT32BPPARGB:
Break
Default
M_depth = 24;
Break
}
}
}
}
<summary>
Get Data
</summary>
<param name= "P_imagebytes" ></param>
private void LoadImage (byte[] p_imagebytes)
{
if (Bitconverter.touint32 (p_imagebytes, 0)!= m_mageic) throw new Exception ("Incorrect file header!") ");
M_width = Bytestouint (p_imagebytes, 4);
M_height = Bytestouint (p_imagebytes, 8);
M_depth = Bytestouint (p_imagebytes, 12);
M_length = Bytestouint (p_imagebytes, 16);
M_type = Bytestouint (p_imagebytes, 20);
M_maptype = Bytestouint (p_imagebytes, 24);
M_maplength = Bytestouint (p_imagebytes, 28);
int _starindex = 32;
Switch (m_maptype)
{
Case 1:
int _colortable = (int) M_MAPLENGTH/3;
for (int i = 0; I!= _colortable; i++)
{
M_colorlist[i] = Color.FromArgb (P_imagebytes[_starindex], P_imagebytes[_starindex + _colortable], p_ImageBytes[_ Starindex + (_colortable * 2)]);
_starindex++;
}
_starindex + = _colortable * 2;
Break
Default
Break
}
LoadData (P_imagebytes, _starindex);
}
<summary>
BYTE conversion to UINT
</summary>
<param name= "P_value" > byte array </param>
<param name= "P_index" > Start position </param>
<returns></returns>
Private 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>
Get the Inverted bytes
</summary>
<param name= "P_value" ></param>
<returns></returns>
Private byte[] Uinttobytes (UINT p_value)
{
byte[] _valuebytes = bitconverter.getbytes (P_value);
Array.reverse (_valuebytes);
return _valuebytes;
}
<summary>
Get Graphics data
</summary>
<param name= "P_valuebytes" > File stay </param>
<param name= "P_starindex" >rgb left start position </param>
private void LoadData (byte[] p_valuebytes, int p_starindex)
{
PixelFormat _format = Pixelformat.format24bpprgb;
Switch (m_depth)
{
Case 8:
_format = pixelformat.format8bppindexed;
Break
Case 24:
_format = Pixelformat.format24bpprgb;
Break
Default
throw new Exception ("not implemented!") ");
}
M_image = new Bitmap (int) m_width, (int) m_height, _format);
BitmapData _data = m_image.lockbits (new Rectangle (0, 0, M_image.width, m_image.height), Imagelockmode.readwrite, m_ Image.pixelformat);
byte[] _writebytes = new byte[_data.height * _data.stride];
int _starindex = 0;
int _writeindex = 0;
for (int i = 0; I!= _data.height; i++)
{
_writeindex = i * _data.stride;
_starindex = i * (int) m_length/(int) m_height) + P_starindex;
for (int z = 0; z!= _data.width; z++)
{
Switch (m_depth)
{
Case 8:
_writebytes[_writeindex] = P_valuebytes[_starindex];
_writeindex++;
_starindex++;
Break
Case 24:
_writebytes[_writeindex] = P_valuebytes[_starindex + 2];
_writebytes[_writeindex + 1] = P_valuebytes[_starindex + 1];
_writebytes[_writeindex + 2] = P_valuebytes[_starindex];
_writeindex + 3;
_starindex + 3;
Break
}
}
}
Switch (m_depth)
{
Case 8:
ColorPalette _palette = M_image.palette;
for (int i = 0; I!= m_colorlist.length; i++)
{
_palette.entries[i] = M_colorlist[i];
}
M_image.palette = _palette;
Break
Default
Break
}
Marshal.Copy (_writebytes, 0, _data.scan0, _writebytes.length);
M_image.unlockbits (_data);
}
<summary>
Save graphic
</summary>
<returns></returns>
Private byte[] Saveimageofras ()
{
if (m_image = null) return to new byte[0];
MemoryStream _stream = new MemoryStream ();
_stream.write (Bitconverter.getbytes (m_mageic), 0, 4);
_stream.write (Uinttobytes (M_width), 0, 4);
_stream.write (Uinttobytes (M_height), 0, 4);
Switch (m_depth)
{
Case 8:
BitmapData _data256 = m_image.lockbits (new Rectangle (0, 0, M_image.width, m_image.height), Imagelockmode.readonly, m_ Image.pixelformat);
byte[] _databytes = new byte[_data256.stride * _data256.height];
int _stride = _data256.stride;
Marshal.Copy (_data256.scan0, _databytes, 0, _databytes.length);
M_image.unlockbits (_data256);
_stream.write (Uinttobytes (8), 0, 4);
UINT _widthcount = (UINT) m_image.width;
if (m_image.width% 2!= 0) _widthcount = (UINT) M_image.width + 1;
UINT _allcount = _widthcount * (UINT) m_image.height;
_stream.write (Uinttobytes (_allcount), 0, 4);
_stream.write (uinttobytes (0), 0, 4);
_stream.write (Uinttobytes (1), 0, 4);
_stream.write (Uinttobytes (768), 0, 4);
byte[] _redbytes = new byte[256];
byte[] _greenbytes = new byte[256];
byte[] _bluebytes = new byte[256];
for (int i = 0; I!= 256; i++)
{
_redbytes[i] = m_image.palette.entries[i]. R
_greenbytes[i] = m_image.palette.entries[i]. G
_bluebytes[i] = m_image.palette.entries[i]. B
}
_stream.write (_redbytes, 0, _redbytes.length);
_stream.write (_greenbytes, 0, _greenbytes.length);
_stream.write (_bluebytes, 0, _bluebytes.length);
byte[] _write = new Byte[_widthcount];
for (int i = 0; I!= m_image.height; i++)
{
Array.copy (_databytes, I * _stride, _write, 0, _widthcount);
_stream.write (_write, 0, _write.length);
}
Break
Default
Bitmap _newbitmap = new Bitmap (M_image.width, M_image.height, Pixelformat.format24bpprgb);
Graphics _graphics = Graphics.fromimage (_newbitmap);
_graphics.drawimage (M_image, New Rectangle (0, 0, m_image.width, m_image.height));
_graphics.dispose ();
BitmapData _data24 = _newbitmap.lockbits (new Rectangle (0, 0, _newbitmap.width, _newbitmap.height), Imagelockmode.readonly, _newbitmap.pixelformat);
byte[] _databytes24 = new byte[_data24.stride * _data24.height];
int _stride24 = _data24.stride;
Marshal.Copy (_data24.scan0, _databytes24, 0, _databytes24.length);
_newbitmap.unlockbits (_data24);
_stream.write (Uinttobytes (24), 0, 4);
UINT _WIDTHCOUNT24 = (UINT) _newbitmap.width;
if (_newbitmap.width% 2!= 0) _widthcount24 = (UINT) _newbitmap.width + 1;
UINT _ALLCOUNT24 = _widthcount24 * (UINT) _newbitmap.height * 3;
_widthcount24 = _widthcount24 * 3;
_stream.write (Uinttobytes (_allcount24), 0, 4);
_stream.write (uinttobytes (0), 0, 4);
_stream.write (uinttobytes (0), 0, 4);
_stream.write (uinttobytes (0), 0, 4);
byte[] _write24 = new Byte[0];
for (int i = 0; I!= m_image.height; i++)
{
_write24 = new Byte[_widthcount24];
int _writeindex = 0;
int _starindex = i * _STRIDE24;
for (int z = 0; z!= m_image.width; z++)
{
_write24[_writeindex] = _databytes24[_starindex + 2];
_write24[_writeindex + 1] = _databytes24[_starindex + 1];
_write24[_writeindex + 2] = _databytes24[_starindex];
_writeindex + 3;
_starindex + 3;
}
_stream.write (_write24, 0, _write24.length);
}
_newbitmap.dispose ();
Break
}
byte[] _return = _stream.toarray ();
return _return;
}
<summary>
Save Graphics to RAS file
</summary>
<param name= "P_file" ></param>
public void Saveras (string p_file)
{
byte[] _value = Saveimageofras ();
if (_value.length!= 0) file.writeallbytes (p_file, _value);
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.