C # code for parsing the SUM grating file image of the RAS File

Source: Internet
Author: User

Usage:
Copy codeThe Code is as follows:
ImageRas _ Ras = new ImageRas (@ "D: \ temp \ test. ras ");
PictureBox1.Image = _ Ras. Image;
_ Ras. SaveRas (@ "d: \ temp \ OK. ras ");

I only implemented the 24-bit color and 8-bit color structure, which is too simple. Only the file header and data area are supported. The 8-bit color table has some special features.
First, the red table, the green table, and the blue table are usually RGB and RGB. In this case, the RRRR... GGG... B ....
I don't know what to think.
There is little time to do these things when there are too many projects. The next target is the IFF file.
All code
Copy codeThe Code is 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 image 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>
/// Width
/// </Summary>
Private uint m_Width = 0;
/// <Summary>
/// High
/// </Summary>
Private uint m_Height = 0;
/// <Summary>
/// Color depth
/// </Summary>
Private uint m_Depth = 0;
/// <Summary>
/// Data size in the graphic Area
/// </Summary>
Private uint m_Length = 0;
/// <Summary>
/// Data Type
/// </Summary>
Private uint m_Type = 0;
/// <Summary>
/// Color image 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>
/// Image
/// </Summary>
Private Bitmap m_Image;
# Endregion
/// <Summary>
/// Obtain the image
/// </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>
/// Obtain 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>
/// Convert byte 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>
/// Obtain the reversed 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>
/// Obtain Graphic Data
/// </Summary>
/// <Param name = "p_ValueBytes"> file retention </param>
/// <Param name = "p_StarIndex"> Leave the start position in RGB format </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 the image
/// </Summary>
/// <Returns> </returns>
Private byte [] SaveImageOfRas ()
{
If (m_Image = null) return 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 [1, 256];
Byte [] _ GreenBytes = new byte [1, 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 the image to the 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.