Exemple #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();
   }
 }
Exemple #2
0
    JFIFThumbJPEG(BufferedImage thumb) throws IllegalThumbException {
      int INITIAL_BUFSIZE = 4096;
      int MAZ_BUFSIZE = 65535 - 2 - PREAMBLE_SIZE;
      try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(INITIAL_BUFSIZE);
        MemoryCacheImageOutputStream mos = new MemoryCacheImageOutputStream(baos);

        JPEGImageWriter thumbWriter = new JPEGImageWriter(null);

        thumbWriter.setOutput(mos);

        // get default metadata for the thumb
        JPEGMetadata metadata =
            (JPEGMetadata) thumbWriter.getDefaultImageMetadata(new ImageTypeSpecifier(thumb), null);

        // Remove the jfif segment, which should be there.
        MarkerSegment jfif = metadata.findMarkerSegment(JFIFMarkerSegment.class, true);
        if (jfif == null) {
          throw new IllegalThumbException();
        }

        metadata.markerSequence.remove(jfif);

        /*  Use this if removing leaves a hole and causes trouble

        // Get the tree
        String format = metadata.getNativeMetadataFormatName();
        IIOMetadataNode tree =
        (IIOMetadataNode) metadata.getAsTree(format);

        // If there is no app0jfif node, the image is bad
        NodeList jfifs = tree.getElementsByTagName("app0JFIF");
        if (jfifs.getLength() == 0) {
        throw new IllegalThumbException();
        }

        // remove the app0jfif node
        Node jfif = jfifs.item(0);
        Node parent = jfif.getParentNode();
        parent.removeChild(jfif);

        metadata.setFromTree(format, tree);
        */

        thumbWriter.write(new IIOImage(thumb, null, metadata));

        thumbWriter.dispose();
        // Now check that the size is OK
        if (baos.size() > MAZ_BUFSIZE) {
          throw new IllegalThumbException();
        }
        data = baos.toByteArray();
      } catch (IOException e) {
        throw new IllegalThumbException();
      }
    }
Exemple #3
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();
 }
Exemple #4
0
 void writePixels(ImageOutputStream ios, JPEGImageWriter writer) throws IOException {
   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);
   int[] data = thumbnail.getRaster().getPixels(0, 0, thumbWidth, thumbHeight, (int[]) null);
   writeThumbnailData(ios, data, writer);
 }
Exemple #5
0
 void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException {
   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);
   ios.write(thumbWidth);
   ios.write(thumbHeight);
 }
Exemple #6
0
 /*
  * Write out the values in the integer array as a sequence of bytes,
  * reporting progress to the writer argument.
  */
 void writeThumbnailData(ImageOutputStream ios, int[] thumbData, JPEGImageWriter writer)
     throws IOException {
   int progInterval = thumbData.length / 20; // approx. every 5%
   if (progInterval == 0) {
     progInterval = 1;
   }
   for (int i = 0; i < thumbData.length; i++) {
     ios.write(thumbData[i]);
     if ((i > progInterval) && (i % progInterval == 0)) {
       writer.thumbnailProgress(((float) i * 100) / ((float) thumbData.length));
     }
   }
 }
Exemple #7
0
 void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException {
   int progInterval = data.length / 20; // approx. every 5%
   if (progInterval == 0) {
     progInterval = 1;
   }
   for (int offset = 0; offset < data.length; ) {
     int len = Math.min(progInterval, data.length - offset);
     ios.write(data, offset, len);
     offset += progInterval;
     float percentDone = ((float) offset * 100) / data.length;
     if (percentDone > 100.0F) {
       percentDone = 100.0F;
     }
     writer.thumbnailProgress(percentDone);
   }
 }
Exemple #8
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);
    }
  }