public void imageRect(int x, int y, int w, int h, Object pix) {
    if (pix instanceof Image) {
      Image img = (Image)pix;
      clip = new Rectangle(x, y, w, h);
      synchronized(clip) {
        tk.prepareImage(img, -1, -1, this);
        try {
          clip.wait(1000);
        } catch(InterruptedException e) {
          throw new ErrorException("Error decoding JPEG data");
        }
      }
      clip = null;
      img.flush();
    } else {
      if (image.getSampleModel().getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = new byte[((int[])pix).length];
        for (int i = 0; i < bytes.length; i++)
          bytes[i] = (byte)((int[])pix)[i];
        pix = bytes;
      } else if (image.getSampleModel().getTransferType() ==
                 DataBuffer.TYPE_USHORT) {
        short[] shorts = new short[((int[])pix).length];
        for (int i = 0; i < shorts.length; i++)
          shorts[i] = (short)((int[])pix)[i];
        pix = shorts;
      }
      image.getSampleModel().setDataElements(x, y, w, h, pix, db);
    }
  }