Sun Raster File Format Summary

Also Known As: RAS


Type Bitmap
Colors Variable
Compression RLE
Maximum Image Size Variable
Multiple Images Per File No
Numerical Format Big-endian
Originator Sun Microsystems
Platform SunOS
Supporting Applications Many UNIX-based
See Also Sun Icon

Usage
Sun Raster is the native bitmap format of the Sun UNIX platforms.

Comments
A simple bitmap format with wide distribution, particularly in the UNIX world.

Vendor specifications are available for this format.

Code fragments are available for this format.

Sample images are available for this format.


The Sun Raster image file format is the native bitmap format of the Sun Microsystems UNIX platforms using the SunOS operating system. This format is capable of storing black-and-white, gray-scale, and color bitmapped data of any pixel depth. The use of color maps and a simple Run-Length data compression are also supported. Typically, most images found on a SunOS system are Sun Raster images, and this format is supported by most UNIX imaging applications.

Contents:
File Organization
File Details
For Further Information

File Organization

The basic layout of a Sun Raster file is a header, followed by an optional color map, and then by the bitmapped image data.

File Details

The Sun Raster file header is 32 bytes in length and has the following format:

typedef struct _SunRaster
{
  DWORD MagicNumber;      /* Magic (identification) number */
  DWORD Width;            /* Width of image in pixels */
  DWORD Height;           /* Height of image in pixels */
  DWORD Depth;            /* Number of bits per pixel */
  DWORD Length;           /* Size of image data in bytes */
  DWORD Type;             /* Type of raster file */
  DWORD ColorMapType;     /* Type of color map */
  DWORD ColorMapLength;   /* Size of the color map in bytes */
} SUNRASTER;

MagicNumber is used to identify a file as a Sun Raster image and always contains the value 59a66a95h. This value is stored in big-endian byte order, as are the entire contents of every Sun Raster file. Reading this magic number using the little-endian byte order (as is possible on the Intel-based Sun 386i system) produces the value 956aa659h, a clue that you are not reading the raster file using the proper byte order.

Width and Height specify the size of the image in pixels. The width of a scan line is always a multiple of 16 bits, padded when necessary.

Depth is the number of bits per pixel of the image data. The typical values for this field are 1, 8, 24, and 32; a value of 32 indicates 24-bit values with a pad byte preceding the pixel values. Note that 24- and 32-bit pixel data (assuming no color map) is in BGR format, rather than RGB, unless the image type is RGB.

Length is the actual size of the bitmapped data in the bitmap file (that is, the file size minus the header and the color map length). Do not expect this value to always be accurate, however. In the original release of the Sun Raster format, this field indicated the type of encoding used on the bitmapped data and was always set to 00h (no encoding). In the second release, this field was renamed and was used to indicate the length of the bitmapped data. Therefore, older raster files will appear to have a length of 00h. In this case, the Length must be calculated by multiplying together the values of the Height, Width, and Depth fields.

Type is the version (or flavor) of the bitmap file. The following values are typically found in the Type field:

0000h Old
0001h Standard
0002h Byte-encoded
0003h RGB format
0004h TIFF format
0005h IFF format
FFFFh Experimental

Both Old and Standard formats are the same. They indicate that the image data within the file is not compressed, and most Sun Raster files you will encounter are stored in this manner.

The Byte-encoded type indicates that the image data is compressed using a Run-length encoding scheme (described later in this section).

The TIFF and IFF format types indicate that the raster file was originally converted from either of these file formats.

The Experimental type is implementation-specific and is generally an indication that the image file does not conform to the Sun Raster file format specification.

ColorMapType indicates the type of color map included in the raster file, or whether a color map is included at all. The following values are typically found in the ColorMapType field:

0000h No color map
0001h RGB color map
0002h Raw color map

ColorMapLength contains the number of bytes stored in the color map.

If ColorMapType is 0000h (no color map), ColorMapLength is 0000h. If ColorMapType is 0001h (RGB color map) or 0002h (raw color map), ColorMapLength is the number of bytes in the color map. In the case of an RGB color map, the colors are separated into three planes, stored in RGB order, with each plane being one-third the size of the ColorMapLength value. For example, a 256-element color map for a 24-bit image consists of three 256-byte color planes and has a length of 768 bytes (Depth = 24, ColorMapType = 01h, ColorMapLength = 768). A raw color map is any other type of color map not defined by the Sun Raster file format and is stored as individual byte values.

Bitmap files with a Depth of 1 contain 2-color image data. Typically, 1-bit bitmap images do not have a color map. Each bit in the bitmap represents a pixel, with a value of 0 representing black and a value of 1 representing white (the bits are stored most significant bit first within each byte). If a color map is present in a 1-bit image file, it is a 2-color map, each color being 24 bits in length (Depth = 1, ColorMapType = 01h, ColorMapLength = 6). Each bit of image data is then an index pointing to one of these two colors in the map.

Raster files with a Depth of 8 may contain either color or gray-scale image data. Images with pixels eight or fewer bits in depth do not include a color map (Depth = 8, ColorMapType = 00h, ColorMapLength = 0). Each byte of image data contains the value of the color it stores. If a color map is present in an 8-bit raster file, the pixel values are index pointers into the color map. Such an image, although it may contain a 24-bit color map (Depth = 8, ColorMapType = 01h, ColorMapLength = 768), can contain only a maximum of 256 colors.

Raster files with a Depth of 24 (or 32) normally do not have color maps. Instead, the colors values are stored directly in the image data itself (truecolor bitmap). If a 24-bit image has a color map, it is either a raw color map, or an RGB color map that contains more than 256 elements.

The Run-length encoding (RLE) scheme optionally used in Sun Raster files (Type = 0002h) is used to encode bytes of image data separately. RLE encoding may be found in any Sun Raster file regardless of the type of image data it contains.

The RLE packets are typically three bytes in size:

  • The first byte is a Flag Value indicating the type of RLE packet.

  • The second byte is the Run Count.

  • The third byte is the Run Value.

A Flag Value of 80h is followed by a Run Count in the range of 01h to FFh. The Run Value follows the Run count and is in the range of 00h to FFh. The pixel run is the Run Value repeated Run Count times.

There are two exceptions to this algorithm. First, if the Run Count following the Flag Value is 00h, this is an indication that the run is a single byte in length and has a value of 80h. And second, if the Flag Value is not 80h, then it is assumed that the data is unencoded pixel data and is written directly to the output stream.

For example, a run of 100 pixels with the value of 0Ah would encode as the values 80h 64h 0Ah. A single pixel value of 80h would encode as the values 80h 00h. The four unencoded bytes 12345678h would be stored in the RLE stream as 12h 34h 56h 78h.

Note also that the Sun Raster bitmap is read as if it is a single stream of data. Therefore, the encoding of pixel runs does not stop at the end of each scan line.

For Further Information

For further information about the Sun Raster format, see the descriptions included on the CD-ROM and the SunOS manual page entitled rasterfile. The man page entry describes only the basic layout of the Sun Raster format. Information about the order of bit planes or the RLE encoding used on the image data is not included. The following file contains the Sun Raster header declaration and field values:

/usr/include/rasterfile.h

You can also contact Sun Microsystems at:

Sun Microsystems Incorporated
2550 Garcia Avenue
Mountain View, CA 94043
Voice: 415-960-1300
FTP: ftp://ftp.sun.com/
WWW: http://www.sun.com/

In addition, there are also many publicly available UNIX-based image file viewers and converters that support the Sun Raster format. See the FBM, ImageMagick, pbmplus, xli, xloadimage, and xv packages on the CD-ROM.


This page is taken from the Encyclopedia of Graphics File Formats and is licensed by O'Reilly under the Creative Common/Attribution license.

More Resources