private void readRLE(BufferedImage im, LittleEndianInputStream in)
			throws IOException {
		in.readInt(); // length
		int palSize = in.readInt();
		if (palSize < 1 || palSize > 256)
			throw new FileFormatException("Invalid palette size");
		int[] pal = new int[palSize];
		for (int i = 0; i < palSize; i++)
			pal[i] = readPix(in);

		int x = 0;
		int y = 0;
		while (y < height) {
			int c = in.readByte();
			int num = (c >> 1) + 1;
			if ((c & 1) == 0) {
				for (int i = 0; i < num; i++) {
					im.setRGB(x, y, pal[in.readByte()]);
					if (++x >= width) { x = 0; y++; }
				}
			}
			else {
				int pix = pal[in.readByte()];
				for (int i = 0; i < num; i++) {
					im.setRGB(x, y, pix);
					if (++x >= width) { x = 0; y++; }
				}
			}
		}
	}