Click here to Skip to main content
13,352,175 members (76,363 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 10 Jul 2004

SUN Raster File Decoder

, 10 Jul 2004
Rate this:
Please Sign up or sign in to vote.
This article provides information on the decoder for Sun Raster files.

Sample Image - RAS_Decoder.jpg


This article provides information on the decoder for Sun Raster files. I had been using a free image library for quiet some time. A few days back, I wanted to support the Sun Raster file format (.ras). Since the library I am currently using doesn't support this format, I started looking for a decoder. I found that the FreeImage Imaging Library supports this format. So I used this library as a reference to write a standalone decoder.

The RAS File Format

The RAS format is described here.

The RAS Header

The RAS header is as follows:

struct RASHEADER {
DWORD magic;            // Magic number
DWORD width;            // Image width in pixels
DWORD height;           // Image height in pixels
DWORD depth;           // Depth (1, 8, 24 or 32 bits) of each pixel
DWORD length;          // Image length (in bytes)
DWORD type;            // Format of file
DWORD maptype;      // Type of colormap
DWORD maplength;   // Length of colormap (in bytes) 

Note the following:

#define RAS_MAGIC 0x59A66A95 // Magic number for Sun rasterfiles

A check for the above number should be done after reading the header information to verify if it's a SUN Raster file.

The RAS decoder consists of a class CLibRAS having the following functions:

  • BMP * LoadRAS(FILE* handle);
  • unsigned GetWidth(BMP *dib);
  • BYTE *GetBits(BMP *dib);
  • unsigned GetHeight(BMP *dib);
  • BITMAPINFO *GetInfo(BMP *dib);
  • unsigned GetLine(BMP *dib);
  • unsigned GetBPP(BMP *dib);
  • unsigned GetColorsUsed(BMP *dib);
  • RGBQUAD * GetPalette(BMP *dib);
  • unsigned GetPitch(BMP *dib);

The LoadRAS function returns the image in a DIB format which can be used with the StretchDIBits function to draw the image on the device context.

A simple usage of the class would be as follows:

CLibRAS Decoder;
BMP *dib = Decoder.LoadRAS(fopen("C:\\lady256.ras","rb"));
                  10,                       // Destination x
                  10,                       // Destination y
                  Decoder.GetWidth(dib),    // Destination width
                  Decoder.GetHeight(dib),   // Destination height
                  0,                        // Source x
                  0,                        // Source y
                  Decoder.GetWidth(dib),    // Source width
                  Decoder.GetHeight(dib),   // Source height
                  Decoder.GetBits(dib),     // Pointer to bits
                  Decoder.GetInfo(dib),     // BITMAPINFO
                  DIB_RGB_COLORS,           // Options
                  SRCCOPY);                 // Raster operator code

Would appreciate if you post the results after usage.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amol Kakhandki
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

Questionlicense? Pin
Member 418516812-Sep-11 17:32
memberMember 418516812-Sep-11 17:32 
does it can be used in MIT license?
GeneralMy vote of 1 Pin
Member 201045812-Apr-11 6:41
memberMember 201045812-Apr-11 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 11 Jul 2004
Article Copyright 2004 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid