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(); } }
public static JPEGMetadata getMetadata(byte[] src) { JPEGMetadata metadata = new JPEGMetadata(); Map<String, Integer> foundByteSets = findByteSets(src, BYTE_SETS); metadata.startOfFrame1 = fetchByteSetContent( src, BYTE_SET_MAPPINGS.get(START_OF_FRAME0), foundByteSets.get(START_OF_FRAME0), false); metadata.endOfImage = fetchByteSetContent( src, BYTE_SET_MAPPINGS.get(END_OF_IMAGE), foundByteSets.get(END_OF_IMAGE), false); return metadata; }
IIOMetadataNode getNativeNode() { IIOMetadataNode node = new IIOMetadataNode("JFIFthumbJPEG"); if (thumbMetadata != null) { node.appendChild(thumbMetadata.getNativeTree()); } return node; }
protected Object clone() { JFIFThumbJPEG newGuy = (JFIFThumbJPEG) super.clone(); if (thumbMetadata != null) { newGuy.thumbMetadata = (JPEGMetadata) thumbMetadata.clone(); } return newGuy; }
int getHeight() { int retval = 0; SOFMarkerSegment sof = (SOFMarkerSegment) thumbMetadata.findMarkerSegment(SOFMarkerSegment.class, true); if (sof != null) { retval = sof.numLines; } return retval; }
int getWidth() { int retval = 0; SOFMarkerSegment sof = (SOFMarkerSegment) thumbMetadata.findMarkerSegment(SOFMarkerSegment.class, true); if (sof != null) { retval = sof.samplesPerLine; } return retval; }
JFIFThumbJPEG(Node node) throws IIOInvalidTreeException { if (node.getChildNodes().getLength() > 1) { throw new IIOInvalidTreeException("JFIFThumbJPEG node must have 0 or 1 child", node); } Node child = node.getFirstChild(); if (child != null) { String name = child.getNodeName(); if (!name.equals("markerSequence")) { throw new IIOInvalidTreeException( "JFIFThumbJPEG child must be a markerSequence node", node); } thumbMetadata = new JPEGMetadata(false, true); thumbMetadata.setFromMarkerSequenceNode(child); } }