xtiffdirectory.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 851 行 · 第 1/2 页
JAVA
851 行
case XTIFFField.TIFF_SHORT: char[] cvalues = new char[count]; for (j = 0; j < count; j++) { cvalues[j] = (char) (readUnsignedShort(stream)); } obj = cvalues; break; case XTIFFField.TIFF_LONG: long[] lvalues = new long[count]; for (j = 0; j < count; j++) { lvalues[j] = readUnsignedInt(stream); } obj = lvalues; break; case XTIFFField.TIFF_RATIONAL: long[][] llvalues = new long[count][2]; for (j = 0; j < count; j++) { llvalues[j][0] = readUnsignedInt(stream); llvalues[j][1] = readUnsignedInt(stream); } obj = llvalues; break; case XTIFFField.TIFF_SSHORT: short[] svalues = new short[count]; for (j = 0; j < count; j++) { svalues[j] = readShort(stream); } obj = svalues; break; case XTIFFField.TIFF_SLONG: int[] ivalues = new int[count]; for (j = 0; j < count; j++) { ivalues[j] = readInt(stream); } obj = ivalues; break; case XTIFFField.TIFF_SRATIONAL: int[][] iivalues = new int[count][2]; for (j = 0; j < count; j++) { iivalues[j][0] = readInt(stream); iivalues[j][1] = readInt(stream); } obj = iivalues; break; case XTIFFField.TIFF_FLOAT: float[] fvalues = new float[count]; for (j = 0; j < count; j++) { fvalues[j] = readFloat(stream); } obj = fvalues; break; case XTIFFField.TIFF_DOUBLE: double[] dvalues = new double[count]; for (j = 0; j < count; j++) { dvalues[j] = readDouble(stream); } obj = dvalues; break; default: System.err.println(JaiI18N.getString("XTIFFDirectory0")); break; } return obj; } /** * Method for reading a field from stream. Positions stream at the next * field location. */ private XTIFFField readField() throws IOException, ArrayIndexOutOfBoundsException {// int j; int tag = readUnsignedShort(stream); int type = readUnsignedShort(stream); int count = (int) readUnsignedInt(stream); int value = 0; // The place to return to to read the next tag long nextTagOffset = stream.getFilePointer() + 4; try { // If the tag data can't fit in 4 bytes, the next 4 bytes // contain the starting offset of the data if (count * sizeOfType(type) > 4) { value = (int) (readUnsignedInt(stream)); stream.seek(value); } } catch (ArrayIndexOutOfBoundsException ae) { System.err.println(tag + " " + JaiI18N.getString("XTIFFDirectory4")); // if the data type is unknown we should skip this TIFF Field stream.seek(nextTagOffset); throw ae; } Object obj = readFieldValue(tag, type, count); // Position stream at next field and return this one stream.seek(nextTagOffset); return createField(tag, type, count, obj); } // Methods to read primitive data types from the stream protected short readShort(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readShort(); } else { return stream.readShortLE(); } } protected int readUnsignedShort(SeekableStream stream) throws IOException { if (isBigEndian) { int val = stream.readUnsignedShort(); return val; } else { int val = stream.readUnsignedShortLE(); return val; } } protected int readInt(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readInt(); } else { return stream.readIntLE(); } } protected long readUnsignedInt(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readUnsignedInt(); } else { return stream.readUnsignedIntLE(); } } protected long readLong(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readLong(); } else { return stream.readLongLE(); } } protected float readFloat(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readFloat(); } else { return stream.readFloatLE(); } } protected double readDouble(SeekableStream stream) throws IOException { if (isBigEndian) { return stream.readDouble(); } else { return stream.readDoubleLE(); } } // Static methods used by the public static method below private static int readUnsignedShort(SeekableStream stream, boolean isBigEndian) throws IOException { if (isBigEndian) { return stream.readUnsignedShort(); } else { return stream.readUnsignedShortLE(); } } private static long readUnsignedInt(SeekableStream stream, boolean isBigEndian) throws IOException { if (isBigEndian) { return stream.readUnsignedInt(); } else { return stream.readUnsignedIntLE(); } } // Utilities /** * Returns the number of image directories (subimages) stored in a given * TIFF file, represented by a <code>SeekableStream</code>. */ public static int getNumDirectories(SeekableStream stream) throws IOException { long pointer = stream.getFilePointer(); // Save stream pointer stream.seek(0L); int endian = stream.readUnsignedShort(); if (!isValidEndianTag(endian)) { throw new IllegalArgumentException(JaiI18N.getString("XTIFFDirectory1")); } boolean isBigEndian = (endian == 0x4d4d); int magic = readUnsignedShort(stream, isBigEndian); if (magic != 42) { throw new IllegalArgumentException(JaiI18N.getString("XTIFFDirectory2")); } stream.seek(4L); long offset = readUnsignedInt(stream, isBigEndian); int numDirectories = 0; while (offset != 0L) { ++numDirectories; stream.seek(offset); int entries = readUnsignedShort(stream, isBigEndian); stream.skip(12 * entries); offset = readUnsignedInt(stream, isBigEndian); } stream.seek(pointer); // Reset stream pointer return numDirectories; } /** * Returns a boolean indicating whether the byte order used in the the TIFF * file is big-endian (i.e. whether the byte order is from the most * significant to the least significant) */ public boolean isBigEndian() { return isBigEndian; } /** * Specifies the type of compression to be used. The compression type * specified will be honored only if it is compatible with the image being * written out. * * @param compression The compression type. */ public void setCompression(int compression) { // this.compression = compression; // Check to see if compression supported // Add Field addField(XTIFF.TIFFTAG_COMPRESSION, XTIFFField.TIFF_SHORT, 1, new char[] { (char) compression }); } /** * Return the type of compression indicated in the TIFF fields, or * XTIFF.COMPRESSION_NON if not specified. */ public int getCompression() { if (getField(XTIFF.TIFFTAG_COMPRESSION) == null) return XTIFF.COMPRESSION_NONE; return (int) getFieldAsLong(XTIFF.TIFFTAG_COMPRESSION); } /** * If set, the data will be written out in tiled format, instead of in * strips. * * @param isTiled Specifies whether the image data should be wriiten out in * tiled format. */ public void setIsTiled(boolean isTiled) { this._isTiled = isTiled; } /** * Constructs a tile codec for decoding data, using the compression defined * in the current directory. * * @param param the encoding param * @see XTIFFTileCodec */ public XTIFFTileCodec createTileCodec(XTIFFDecodeParam param) throws IOException { int compression = getCompression(); XTIFFTileCodec codec = getTileCodec(compression); if (codec == null) throw new IOException("Compression type (" + compression + ") not supported"); return codec.create(param); } /** * Constructs a tile codec for encoding data, using the compression defined * in the current directory. * * @param param the encoding param * @see XTIFFTileCodec */ public XTIFFTileCodec createTileCodec(XTIFFEncodeParam param) throws IOException { int compression = getCompression(); XTIFFTileCodec codec = getTileCodec(compression); if (codec == null) throw new IOException("Compression type (" + compression + ") not supported"); return codec.create(param); } /** * Set the XTIFFFactory, which is used to construct the XTIFFDirectory * object assigned as a "tiff.directory" property in the resulting jai * image. * * @param fact the factory to register. The factory is guaranteed to always * be non-null; if a null is passed in then the default XTIFFFactory * is used. a null object is passed in * @see XTIFFFactory */ public static void setFactory(XTIFFFactory fact) { if (fact == null) factory = new XTIFFFactory(); else factory = fact; } /** * Constructs a XTIFFDirectory from a SeekableStream. The directory * parameter specifies which directory to read from the linked list present * in the stream; directory 0 is normally read but it is possible to store * multiple images in a single TIFF file by maintaing multiple directories. * * @param stream a SeekableStream to read from. * @param directory the index of the directory to read. * @see XTIFFFactory */ public static XTIFFDirectory create(SeekableStream stream, int directory) throws IOException { return factory.createDirectory(stream, directory); } /** * Constructs a TIFFDirectory by reading a SeekableStream. The ifd_offset * parameter specifies the stream offset from which to begin reading; this * mechanism is sometimes used to store private IFDs within a TIFF file that * are not part of the normal sequence of IFDs. Uses the XTIFFFactory to do * this, so to extend the directory class, the factory method should be * extended and registered instead of this one. * * @param stream a SeekableStream to read from. * @param ifd_offset the long byte offset of the directory. * @see XTIFFFactory */ public static XTIFFDirectory create(SeekableStream stream, long ifd_offset) throws IOException { return factory.createDirectory(stream, ifd_offset); } /** * Constructs an XTIFFDirectory from the currently. registered * XTIFFDirectory factory. * * @see XTIFFFactory */ public static XTIFFDirectory create() { return factory.createDirectory(); } /** * Return the currently registered XTIFFTileCodec for this compression type. * Used by the XTIFFImage to decode the compression data. * * @see XTIFFTileCodec */ public static XTIFFTileCodec getTileCodec(int comp) { return (XTIFFTileCodec) tileCodecs.get(new Integer(comp)); } /** * UnRegister the XTIFFTileCodec corresponding to the TIFF compression type. * * @param comp The TIFF compression code indicated */ public static void unRegisterTileCodec(int comp) { XTIFFTileCodec cod = getTileCodec(comp); tileCodecs.remove(cod); } /** * Register a new XTIFFTileCodec for encoding and decoding compressed TIFF * image data. This overrides any existing codec previously registered. * * @param comp The TIFF compression code indicated by the * @param codec The codec to register. XTIFF.TIFFTAG_COMPRESSION field. * @see XTIFFTileCodec */ public static void registerTileCodec(int comp, XTIFFTileCodec codec) { tileCodecs.put(new Integer(comp), codec); } /** * Get the JAI Image decoded type. This method is called by the * XTIFFTileCodeImpl object during the decode() method to determine what * type of colorspace and sample model to use. */ public int getImageType() { return imageType; } /** * Set the JAI Image decoded type. This method is called by the XTIFFImage * constructor to indicate to the XTIFFTileCodec what type of colorspace and * sample model to use. The types are enumerated in the XTIFF class. * * @see XTIFF * @see XTIFFImage * @see XTIFFTileCodec */ public void setImageType(int image_type) { imageType = image_type; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?