
Introduction
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; DWORD width; DWORD height; DWORD depth; DWORD length; DWORD type; DWORD maptype; DWORD maplength; } RASHEADER;
Note the following:
#define RAS_MAGIC 0x59A66A95
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"));
::StretchDIBits(AfxGetMainWnd()->GetDC()->m_hDC,
10, 10, Decoder.GetWidth(dib), Decoder.GetHeight(dib), 0, 0, Decoder.GetWidth(dib), Decoder.GetHeight(dib), Decoder.GetBits(dib), Decoder.GetInfo(dib), DIB_RGB_COLORS, SRCCOPY);
Would appreciate if you post the results after usage.