Example #1
0
 /**
  * Writes the data for this segment to the stream in valid JPEG format. The length written takes
  * the thumbnail width and height into account. If necessary, the thumbnail is clipped to 255 x
  * 255 and a warning is sent to the writer argument. Progress updates are sent to the writer
  * argument.
  */
 void write(ImageOutputStream ios, BufferedImage thumb, JPEGImageWriter writer)
     throws IOException {
   int thumbWidth = 0;
   int thumbHeight = 0;
   int thumbLength = 0;
   int[] thumbData = null;
   if (thumb != null) {
     // Clip if necessary and get the data in thumbData
     thumbWidth = thumb.getWidth();
     thumbHeight = thumb.getHeight();
     if ((thumbWidth > MAX_THUMB_WIDTH) || (thumbHeight > MAX_THUMB_HEIGHT)) {
       writer.warningOccurred(JPEGImageWriter.WARNING_THUMB_CLIPPED);
     }
     thumbWidth = Math.min(thumbWidth, MAX_THUMB_WIDTH);
     thumbHeight = Math.min(thumbHeight, MAX_THUMB_HEIGHT);
     thumbData = thumb.getRaster().getPixels(0, 0, thumbWidth, thumbHeight, (int[]) null);
     thumbLength = thumbData.length;
   }
   length = DATA_SIZE + LENGTH_SIZE + thumbLength;
   writeTag(ios);
   byte[] id = {0x4A, 0x46, 0x49, 0x46, 0x00};
   ios.write(id);
   ios.write(majorVersion);
   ios.write(minorVersion);
   ios.write(resUnits);
   write2bytes(ios, Xdensity);
   write2bytes(ios, Ydensity);
   ios.write(thumbWidth);
   ios.write(thumbHeight);
   if (thumbData != null) {
     writer.thumbnailStarted(0);
     writeThumbnailData(ios, thumbData, writer);
     writer.thumbnailComplete();
   }
 }
Example #2
0
 /** Writes out a new JFXX extension segment, without saving it. */
 private void writeJFXXSegment(
     int index, BufferedImage thumbnail, ImageOutputStream ios, JPEGImageWriter writer)
     throws IOException {
   JFIFExtensionMarkerSegment jfxx = null;
   try {
     jfxx = new JFIFExtensionMarkerSegment(thumbnail);
   } catch (IllegalThumbException e) {
     writer.warningOccurred(JPEGImageWriter.WARNING_ILLEGAL_THUMBNAIL);
     return;
   }
   writer.thumbnailStarted(index);
   jfxx.write(ios, writer);
   writer.thumbnailComplete();
 }
Example #3
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);
    }
  }