display-name: "Images/ILBM" 

file-format image-ILBM
{
	interpret-as-internally "iff"

	source-dir: "ilbm"
	if (!exists("ilbm\\bmhd"))
	{
		source-dir = "pbm"
	}

	from source-dir + "\\bmhd"
	{
		unsigned16be width
		unsigned16be height

		signed16be origin-x
		signed16be origin-y

		unsigned8 num-planes
		unsigned8 mask        //not implemented

		unsigned8 compressed
		unsigned8 pad1

		unsigned16be transparent-color

		unsigned8 aspect-x
		unsigned8 aspect-y

		signed16 page-width
		signed16 page-height
	}

	if (exists(source-dir + "\\cmap"))
	{
		from source-dir + "\\cmap"
		{
			palette
			{
				format: "R8G8B8"
				size: file.size / 3
			}
		}
	}

	from source-dir + "\\body"
	{
		bit-stride: width
		planar-I8: false

		if (source-dir == "ilbm")
		{
			planar-I8 = true
		}

		if (compressed == 1)
		{
			compressed[file.size] "ilbm-rle"
			{
				image
				{
					format: "I" + num-planes
				}
			}
		}
		else
		{
			image
			{
				format: "I" + num-planes
			}
		}
	}
}

function ilbm-rle
{
	running: true
	loop while ((file.remaining-bytes > 0) and (running == true))
	{
		read unsigned8 value

		if (value > 128)
		{
			read unsigned8 pixel
			loop (257 - value)
			{
				write unsigned8 pixel
			}
		}
		else if (value < 128)
		{
			loop (value + 1)
			{
				read unsigned8 pixel
				write unsigned8 pixel
			}
		}
		else
		{
			running = false
		}
	}
}