Example #1
0
  private void writeThumb(
      ImageOutputStream ios,
      BufferedImage thumb,
      JFIFExtensionMarkerSegment jfxx,
      int index,
      boolean onlyOne,
      JPEGImageWriter writer)
      throws IOException {
    ColorModel cm = thumb.getColorModel();
    ColorSpace cs = cm.getColorSpace();

    if (cm instanceof IndexColorModel) {
      // We never write a palette image into the header
      // So if it's the only one, we need to write the header first
      if (onlyOne) {
        write(ios, writer);
      }
      if ((jfxx == null) || (jfxx.code == THUMB_PALETTE)) {
        writeJFXXSegment(index, thumb, ios, writer); // default
      } else {
        // Expand to RGB
        BufferedImage thumbRGB =
            ((IndexColorModel) cm).convertToIntDiscrete(thumb.getRaster(), false);
        jfxx.setThumbnail(thumbRGB);
        writer.thumbnailStarted(index);
        jfxx.write(ios, writer); // Handles clipping if needed
        writer.thumbnailComplete();
      }
    } else if (cs.getType() == ColorSpace.TYPE_RGB) {
      if (jfxx == null) {
        if (onlyOne) {
          write(ios, thumb, writer); // As part of the header
        } else {
          writeJFXXSegment(index, thumb, ios, writer); // default
        }
      } else {
        // If this is the only one, write the header first
        if (onlyOne) {
          write(ios, writer);
        }
        if (jfxx.code == THUMB_PALETTE) {
          writeJFXXSegment(index, thumb, ios, writer); // default
          writer.warningOccurred(JPEGImageWriter.WARNING_NO_RGB_THUMB_AS_INDEXED);
        } else {
          jfxx.setThumbnail(thumb);
          writer.thumbnailStarted(index);
          jfxx.write(ios, writer); // Handles clipping if needed
          writer.thumbnailComplete();
        }
      }
    } else if (cs.getType() == ColorSpace.TYPE_GRAY) {
      if (jfxx == null) {
        if (onlyOne) {
          BufferedImage thumbRGB = expandGrayThumb(thumb);
          write(ios, thumbRGB, writer); // As part of the header
        } else {
          writeJFXXSegment(index, thumb, ios, writer); // default
        }
      } else {
        // If this is the only one, write the header first
        if (onlyOne) {
          write(ios, writer);
        }
        if (jfxx.code == THUMB_RGB) {
          BufferedImage thumbRGB = expandGrayThumb(thumb);
          writeJFXXSegment(index, thumbRGB, ios, writer);
        } else if (jfxx.code == THUMB_JPEG) {
          jfxx.setThumbnail(thumb);
          writer.thumbnailStarted(index);
          jfxx.write(ios, writer); // Handles clipping if needed
          writer.thumbnailComplete();
        } else if (jfxx.code == THUMB_PALETTE) {
          writeJFXXSegment(index, thumb, ios, writer); // default
          writer.warningOccurred(JPEGImageWriter.WARNING_NO_GRAY_THUMB_AS_INDEXED);
        }
      }
    } else {
      writer.warningOccurred(JPEGImageWriter.WARNING_ILLEGAL_THUMBNAIL);
    }
  }