📄 jfifmarkersegment.java
字号:
thumb.write(ios, writer); } void print() { printTag("JFXX"); thumb.print(); } } /** * A superclass for the varieties of thumbnails that can * be stored in a JFIF extension marker segment. */ abstract class JFIFThumb implements Cloneable { long streamPos = -1L; // Save the thumbnail pos when reading abstract int getLength(); // When writing abstract int getWidth(); abstract int getHeight(); abstract BufferedImage getThumbnail(ImageInputStream iis, JPEGImageReader reader) throws IOException; protected JFIFThumb() {} protected JFIFThumb(JPEGBuffer buffer) throws IOException{ // Save the stream position for reading the thumbnail later streamPos = buffer.getStreamPosition(); } abstract void print(); abstract IIOMetadataNode getNativeNode(); abstract void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException; protected Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) {} // won't happen return null; } } abstract class JFIFThumbUncompressed extends JFIFThumb { BufferedImage thumbnail = null; int thumbWidth; int thumbHeight; String name; JFIFThumbUncompressed(JPEGBuffer buffer, int width, int height, int skip, String name) throws IOException { super(buffer); thumbWidth = width; thumbHeight = height; // Now skip the thumbnail data buffer.skipData(skip); this.name = name; } JFIFThumbUncompressed(Node node, String name) throws IIOInvalidTreeException { thumbWidth = 0; thumbHeight = 0; this.name = name; NamedNodeMap attrs = node.getAttributes(); int count = attrs.getLength(); if (count > 2) { throw new IIOInvalidTreeException (name +" node cannot have > 2 attributes", node); } if (count != 0) { int value = getAttributeValue(node, attrs, "thumbWidth", 0, 255, false); thumbWidth = (value != -1) ? value : thumbWidth; value = getAttributeValue(node, attrs, "thumbHeight", 0, 255, false); thumbHeight = (value != -1) ? value : thumbHeight; } } JFIFThumbUncompressed(BufferedImage thumb) { thumbnail = thumb; thumbWidth = thumb.getWidth(); thumbHeight = thumb.getHeight(); name = null; // not used when writing } void readByteBuffer(ImageInputStream iis, byte [] data, JPEGImageReader reader, float workPortion, float workOffset) throws IOException { int progInterval = Math.max((int)(data.length/20/workPortion), 1); for (int offset = 0; offset < data.length;) { int len = Math.min(progInterval, data.length-offset); iis.read(data, offset, len); offset += progInterval; float percentDone = ((float) offset* 100) / data.length * workPortion + workOffset; if (percentDone > 100.0F) { percentDone = 100.0F; } reader.thumbnailProgress (percentDone); } } int getWidth() { return thumbWidth; } int getHeight() { return thumbHeight; } IIOMetadataNode getNativeNode() { IIOMetadataNode node = new IIOMetadataNode(name); node.setAttribute("thumbWidth", Integer.toString(thumbWidth)); node.setAttribute("thumbHeight", Integer.toString(thumbHeight)); return node; } 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); } 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); } void print() { System.out.print(name + " width: "); System.out.println(thumbWidth); System.out.print(name + " height: "); System.out.println(thumbHeight); } } /** * A JFIF thumbnail stored as RGB, one byte per channel, * interleaved. */ class JFIFThumbRGB extends JFIFThumbUncompressed { JFIFThumbRGB(JPEGBuffer buffer, int width, int height) throws IOException { super(buffer, width, height, width*height*3, "JFIFthumbRGB"); } JFIFThumbRGB(Node node) throws IIOInvalidTreeException { super(node, "JFIFthumbRGB"); } JFIFThumbRGB(BufferedImage thumb) throws IllegalThumbException { super(thumb); } int getLength() { return (thumbWidth*thumbHeight*3); } BufferedImage getThumbnail(ImageInputStream iis, JPEGImageReader reader) throws IOException { iis.mark(); iis.seek(streamPos); DataBufferByte buffer = new DataBufferByte(getLength()); readByteBuffer(iis, buffer.getData(), reader, 1.0F, 0.0F); iis.reset(); WritableRaster raster = Raster.createInterleavedRaster(buffer, thumbWidth, thumbHeight, thumbWidth*3, 3, new int [] {0, 1, 2}, null); ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); } void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException { super.write(ios, writer); // width and height writePixels(ios, writer); } } /** * A JFIF thumbnail stored as an indexed palette image * using an RGB palette. */ class JFIFThumbPalette extends JFIFThumbUncompressed { private static final int PALETTE_SIZE = 768; JFIFThumbPalette(JPEGBuffer buffer, int width, int height) throws IOException { super(buffer, width, height, PALETTE_SIZE + width * height, "JFIFThumbPalette"); } JFIFThumbPalette(Node node) throws IIOInvalidTreeException { super(node, "JFIFThumbPalette"); } JFIFThumbPalette(BufferedImage thumb) throws IllegalThumbException { super(thumb); IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel(); if (icm.getMapSize() > 256) { throw new IllegalThumbException(); } } int getLength() { return (thumbWidth*thumbHeight + PALETTE_SIZE); } BufferedImage getThumbnail(ImageInputStream iis, JPEGImageReader reader) throws IOException { iis.mark(); iis.seek(streamPos); // read the palette byte [] palette = new byte [PALETTE_SIZE]; float palettePart = ((float) PALETTE_SIZE) / getLength(); readByteBuffer(iis, palette, reader, palettePart, 0.0F); DataBufferByte buffer = new DataBufferByte(thumbWidth*thumbHeight); readByteBuffer(iis, buffer.getData(), reader, 1.0F-palettePart, palettePart); iis.read(); iis.reset(); IndexColorModel cm = new IndexColorModel(8, 256, palette, 0, false); SampleModel sm = cm.createCompatibleSampleModel(thumbWidth, thumbHeight); WritableRaster raster = Raster.createWritableRaster(sm, buffer, null); return new BufferedImage(cm, raster, false, null); } void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException { super.write(ios, writer); // width and height // Write the palette (must be 768 bytes) byte [] palette = new byte[768]; IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel(); byte [] reds = new byte [256]; byte [] greens = new byte [256]; byte [] blues = new byte [256]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); for (int i = 0; i < 256; i++) { palette[i*3] = reds[i]; palette[i*3+1] = greens[i]; palette[i*3+2] = blues[i]; } ios.write(palette); writePixels(ios, writer); } } /** * A JFIF thumbnail stored as a JPEG stream. No JFIF or * JFIF extension markers are permitted. There is no need * to clip these, but the entire image must fit into a * single JFXX marker segment. */ class JFIFThumbJPEG extends JFIFThumb { JPEGMetadata thumbMetadata = null; byte [] data = null; // Compressed image data, for writing private static final int PREAMBLE_SIZE = 6; JFIFThumbJPEG(JPEGBuffer buffer, int length, JPEGImageReader reader) throws IOException { super(buffer); // Compute the final stream position long finalPos = streamPos + (length - PREAMBLE_SIZE); // Set the stream back to the start of the thumbnail // and read its metadata (but don't decode the image) buffer.iis.seek(streamPos); thumbMetadata = new JPEGMetadata(false, true, buffer.iis, reader); // Set the stream to the computed final position buffer.iis.seek(finalPos); // Clear the now invalid buffer buffer.bufAvail = 0; buffer.bufPtr = 0; } 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); } } 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -