public void decode(final CachedInputStream pool)
    throws IOException
  {
    final InputStream input = (CachedInputStream)pool.clone();

    // Make sure that everything is clear
    pmap.clear();

    // Code version
    int version = input.read();

    if((version & 0x7f) != DJVUPALETTEVERSION)
    {
      throw new IOException("bad palette version " + version);
    }

    // Code palette
    int palettesize = (input.read() << 8);
    palettesize |= input.read();

    if(palettesize < 0)
    {
      throw new IOException("Bad palette size " + palettesize);
    }

    final GPixel[] palette = new GPixel[palettesize];

    for(int c = 0; c < palettesize; c++)
    {
      final byte b = (byte)input.read();
      final byte g = (byte)input.read();
      final byte r = (byte)input.read();
      palette[c] = new GPixel(b, g, r);
    }

    setPalette(palette);

    // Code data
    if((version & 0x80) != 0)
    {
      int datasize = (input.read() << 16);
      datasize |= (input.read() << 8);
      datasize |= input.read();

      if(datasize < 0)
      {
        throw new IOException("bad palette datasize");
      }

      colordata = new int[datasize];

      final InputStream bsinput =
        BSInputStream.createBSInputStream(this).init(input);

      for(int d = 0; d < datasize; d++)
      {
        int s = (bsinput.read() << 8);
        s |= bsinput.read();

        if((s < 0) || (s >= palettesize))
        {
          throw new IOException("bad palette data");
        }

        colordata[d] = s;
      }
    }
  }