/** {@inheritDoc} */
 public RenderedImage getPreviewImage(ImageInfo imageInfo, int maxWidth, int maxHeight)
     throws BadImageFileException, IOException, UnknownImageTypeException {
   final ImageMetadata metadata = imageInfo.getMetadata();
   final ImageMetadataDirectory dir = metadata.getDirectoryFor(CIFFDirectory.class);
   if (dir != null) {
     final ImageMetaValue colorSpace = dir.getValue(CIFF_COLOR_SPACE);
     ColorSpace cs = JAIContext.sRGBColorSpace;
     if (colorSpace != null)
       switch (colorSpace.getIntValue()) {
         case CIFF_COLOR_SPACE_ADOBE_RGB:
           cs = JAIContext.adobeRGBColorSpace;
           break;
       }
     final RenderedImage image =
         JPEGImageType.getImageFromBuffer(
             imageInfo.getByteBuffer(),
             dir.getValue(CIFF_PREVIEW_IMAGE_OFFSET),
             0,
             dir.getValue(CIFF_PREVIEW_IMAGE_LENGTH),
             cs,
             maxWidth,
             maxHeight);
     if (image != null) return image;
   }
   return super.getPreviewImage(imageInfo, maxWidth, maxHeight);
 }
  protected void paintComponent(Graphics graphics) {
    if (justShown) {
      if (!paintTimer.isRunning()) paintTimer.start();
      return;
    }

    if (!isWidthInitialized) {
      // Only paint if the component size has been initialized.  Layout
      // jumps are typical the first time this component is displayed,
      // because the preferred height depends on the component width.
      return;
    }
    Graphics2D g = (Graphics2D) graphics;
    Rectangle clip = g.getClipBounds();

    // Figure out which ImageDatums fall within the clip bounds.
    List<ImageDatum> datums = getAllImageData();
    int[] indices = getIndices(datums.size(), clip);

    // Iterate backwards through indices, so repaints get enqueued
    // in a visually pleasing order.
    for (int i = indices.length - 1; i >= 0; i--) {
      int index = indices[i];
      if (index < 0) {
        continue;
      }
      ImageDatum datum = datums.get(index);
      if (datum == null) {
        // A race; the image disappeared during painting.
        continue;
      }
      RenderedImage image = datum.getImage(this);

      // This queue prevents GC of recently painted images:
      recentImages.add(image);

      Rectangle rect = getBounds(index);
      g.setClip(clip.intersection(rect));

      File file = datum.getFile();
      String label = file.getName();
      ImageDatumType type = datum.getType();
      String tag = type.toString();
      ImageMetadata meta = datum.getMetadata(true);
      int rating = meta.getRating();
      boolean selected = selection.isSelected(datum);
      renderer.paint(g, image, label, tag, rating, rect, selected);

      ImageGroup group = datum.getGroup();
      if (group.isNonTrivial()) {
        ImageGroupCountRenderer.paint(g, rect, datum);
      }
    }
    g.setClip(clip);

    // The control is drawn as an overlay.
    if (controller.isEnabled()) {
      Rectangle ctrlRect = controller.getRect();
      if (ctrlRect != null) {
        if (ctrlRect.intersects(clip)) {
          controller.paint(g);
        }
      }
    }
  }