LBM Format

From ModdingWiki
Jump to: navigation, search

The ILBM (IFF Interleaved Bitmap) format also known as IFF or LBM, is an image format used by some older games to store graphics and is also an image format used by (Note that IFF files are in fact the file class ILBM files belong to, though ILBMs are the most common.)

In essence ILBM files consist of a number of consecutive chunks, whose order can, to some extent, be varied. Each chunk has a different function and has the same basic format. This means that a program does not have to read or decode every chunk in a file, only the ones it wants to deal with. For example a program might use a TIME chunk to store the date and time that a file was last edited. Another program does not need to know about this and can just skip the chunk and leave it unchanged. This can of course cause problems, (In this case no matter how many times the second program edits the file, the TIME chunk is not updated and will tell the first program this.) This can be a problem when using more than one program to edit files.

0      4        Chunk ID       'FourCC' identifier, tells a program what type the chunk is
4      4        Chunk size     dword giving the Size of the chunk in big endian (Excluding ID and size bytes)
8      x        Data           Chunk data


Chunks can themselves contain chunks, and even entire files, as long as the program reading them understands this. For example, the entirety of an ILBM image is stored in a FORM chunk. All chunks are an even number of bytes long (Padded with a nul byte if necessary.) which is NOT included in the chunk size value.


Contents

ILBM structure

ILBM files usually contain enough information to allow them to be displayed by an image editing program. (That is image dimensions, palette and pixel data.) Some files were designed to act as palettes for paint programs (Pixel data left blank) or to be merged into another image. This makes them much more flexible, but also much more complex than formats such as BMP or PNG.

For ILBMs the BMHD chunk and any other 'vital' chunks must appear before the BODY chunk. Any chunks appearing after the BODY chunk are considered 'extra' and many programs will leave them unread and unchanged.

FORM CHUNK (Header)
0      4        Chunk ID       'FORM'
4      4        Chunk size     Chunk size (Filesize - 8)
8      4        Filetype       Defines image type. For standalone images, 'ILBM'
12     x        Data           Image data chunks


Chunks

There are, as noted, an almost infinite number of possible chunk types. However there are only a limited number of common chunk types used and understood by most programs.


BMHD

The BMHD chunk specifies how the image is to be displayed and is usually the first chunk in the file after FORM. It not only defines the image's height/width, but where it is drawn on the screen, how to display it in various screen resolutions and if the image is compressed. In short, the information a graphics program would need.

BMHD CHUNK
0      4        Chunk ID       'BMHD'
4      4        Chunk size     Chunk size ($14)
8      2        Height         Image height (Big endian)
10     2        Width          Image width (Big endian)
12     2        x-orig         Where on screen, in pixels, the image's top-left corner is. Value is
14     2        y-orig         usually 0,0 unless image is part of a larger image or not fullscreen.
16     1        Planes         Number of planes in bitmap, 1 for monochrome, 4 for 16 color, 8 for 256 color
                               If 0 we have a colormap
17     1        Mask           1 = masked, 2 = transparent color, 3 = lasso (For MacPaint) Mask data is not
                               considered a bit plane
18     2        Compression    If 1 then image data is RLE compressed
20     2        Trans color    Transparent color, useful only for Mask > 1
22     1        xAspect        Pixel aspect, a ratio width:height; used for displaying the image
23     1        yAspect        on a variety of different screen resolutions for 320x200 5:6 or 10:11
24     2        PageWidth      The size of the screen the image is to be displayed on, in pixels,
26     2        PageHeight     Usually 320x200


BODY

The BODY chunk stores the actual image data as interleaved bitplanes (And optional mask) by row. The bitplanes appear first from 1 to n, followed by the mask plane. If the image is uncompressed then each line will be (w/16) words wide rounded up to the nearest word. If it is compressed then each line is compressed individually and is a whole number of words long when compressed.

The BODY chunk is usually the last chunk in a file, and the largest.

BODY CHUNK
0      4        Chunk ID       'BODY'
4      4        Chunk size     Chunk size
8      x        Data           Image data


Compression

If an image is compressed, each row of data (But not each bitplane) is compressed individually (Including the mask data if present.) The compression is a variety of RLE Compression using flags. It can be decoded as follows:

1.) Loop until we have Final length] byte's worth of data (Final length calculated from image size.)
2.) If [Decompressed data length] < [Final length] then:
    3. Read a byte [Value]
    4. If the byte value > 128, then:
       -> Read the next byte and output it (257 - value) times.
          Move forward 2 bytes and return to 3.)
       If the byte value < 128, then:
       -> Read and output the next [value + 1] bytes
          Move forward [Value + 2] bytes and return to 3.)
       If the byte value = 128, then:
       -> Exit loop (Stop decompressing)

In the inverse routine Packer, it's best to encode a 2 byte repeat run as a replicate run except when preceded and followed by a literal run, in which case it's best to merge the three into one literal run. Always encode >3 byte repeats as replicate runs.


CAMG

A CAMG chunk is specifically for the Commodore Amiga computer. It stores a LONG "viewport mode". This lets you specify Amiga display modes like "dual playfield" and "hold and modify". It is exceedingly optional.


CMAP

The CMAP chunk contains the image's palette and consists of 3-byte RGB values for each color used. It is the 3 * color bytes long. The number of colors in the palette will be 2^bitplanes. This chunk is optional, if not present a default palette will be used. It is possible to have an unexpected number of colors (7 colors for a 4 plane '16 color' bitmap for example.) Remember that if this has an odd number of colors it will be padded by one byte to make the chunk an even number of bytes long.

CMAP CHUNK
0      4        Chunk ID       'CMAP'
4      4        Chunk size     Chunk size
8      3x       Color entry    3 bytes, RGB values from 0-255


CRNG

The color range chunk is 'nonstandard' It's used by Electronic Arts' Deluxe Paint program to identify a contiguous range of color registers or a "shade range" and color cycling. There can be zero or more CRNG chunks in an ILBM, but all should appear before the BODY chunk. Deluxe Paint normally writes 4 CRNG chunks in an ILBM when the user asks it to "Save Picture".

CRNG CHUNK
0      4        Chunk ID       'CRNG'
4      4        Chunk size     Chunk size (8)
8      2        Paddding       0
10     2        Cycle rate     Color cycle rate
12     2        Cycle?         If no 0 then cycle colors
14     1        Low/high       Upper and lower color registers selected


The fields low and high indicate the range of color registers (color numbers) selected by this CRange; the field rate determines the speed at which the colors will step when color cycling is on. The units are such that a rate of 60 steps per second is represented as 214 = 16384. lower rates can be obtained by linear scaling: for 30 steps/second, rate = 8192.


CCRT

Commodore's Graphicraft program uses a similar chunk "CCRT" (for Color Cyling Range and Timing). This chunk contains a CycleInfo structure. Like CRNG it is a nonstandard chunk.

CCRT CHUNK
0      4        Chunk ID       'CCRT'
4      4        Chunk size     Chunk size
8      2        Cycle dir      Cycle direction 0= no cycling, 1 = forwards, -1 = backwards
10     1        Low/high       Upper and lower color registers selected
11     4        Delay S        Seconds between changing colors
15     4        Delay mS       Microseconds between changing colors
15     4        Cycle?         If no 0 then cycle colors
19     2        Padding        0


It pretty similar to a CRNG chunk. A program would probably only use one of these two methods of expressing color cycle data. You could write out both if you want to communicate this information to both Deluxe Paint and Graphicraft. A CCRT chunk expresses the color cycling rate as a number of seconds plus a number of microseconds.


DEST

The optional property "DEST" is a way to say how to scatter zero or more source bitplanes into a deeper destination image. Some readers may ignore DEST.

DEST CHUNK
0      4        Chunk ID       'DEST'
4      4        Chunk size     Chunk size
8      2        Source planes  Number of bitplanes in source image
10     2        Plane Pick     How to pick planes to scatter them into the destination image
12     2        Plane On/Off   Default data for Plane Pick
14     2        Plane Mask     Selects which bitplanes to store into

The low order depth number of bits in planePick, planeOnOff, and planeMask correspond one-to-one with destination bitplanes. Bit 0 with bitplane 0, etc. (Any higher order bits should be ignored.) "1" bits in planePick mean "put the next source bitplane into this bitplane", so the number of "1" bits should equal nPlanes. "0" bits mean "put the corresponding bit from planeOnOff into this bitplane". Bits in planeMask gate writing to the destination bitplane: "1" bits mean "write to this bitplane" while "0" bits mean "leave this bitplane alone". The normal case (with no DEST property) is equivalent to planePick = planeMask = 2nPlanesJ-1.

Remember that color numbers are formed by pixels in the destination bitmap (depth planes deep) not in the source bitmap (nPlanes planes deep).


GRAB

The optional "GRAB" chunk locates a "handle" or "hotspot" of the image relative to its upper left corner, e.g. when used as a mouse cursor or a "paint brush". It is optional.

GRAB CHUNK
0      4        Chunk ID       'GRAB'
4      4        Chunk size     Chunk size (2)
8      2        Hotspot        Location of 'hotspot'; x,y in pixels from top left corner. (1 byte x, 1 byte y)


SPRT

The SPRT chunk indicates that an image is intended to be a sprite. It should thus have a mask plane or transparent color and shouldn't be fullscreen. How this is handled depends on the program using the image. The only data stored here is the sprite order, used by many programs to place the sprite in the foreground (A sprite of order 1 appears behind one of order 0, etc.) It is optional.


SPRT CHUNK
0      4        Chunk ID       'SPRT'
4      4        Chunk size     Chunk size (4)
8      4        Order          Sprite order of image (0 is highest)


TINY

The TINY chunk contains a small preview image for various graphics programs, including DeluxePaint. It is compressed and is similar in format to the BODY chunk.


TINY CHUNK
0      4        Chunk ID       'TINY'
4      4        Chunk size     Chunk size
8      2        Width          Image width (Big endian)
10     2        Height         Image height (Big endian)
12     ?        Data           Image data

Utilities

Most utilities that work with ILBM files are rather dated, such as MacPain or DeluxePaint.

  • Image Converter Plus is a program that will convert ILBM files into any number of formats. While the full version is not free, the demo version adds a watermark that can be removed.


External links

http://en.wikipedia.org/wiki/Interchange_File_Format


Notes

Thanks to Lemm for format details.

In the Commander Keen Dreams series of games, compressed standalone ILBM images are used for title screens, but the game does not read most of the ILBM chunks. This is becuase the images were edited in DeluxePaint, then imported directly into the game's files.

Personal tools
programming