xtiffimage.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 662 行 · 第 1/2 页
JAVA
662 行
package org.libtiff.jai.codecimpl;/* * XTIFF: eXtensible TIFF libraries for JAI. * * The contents of this file are subject to the JAVA ADVANCED IMAGING * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE License * Version 1.0 (the "License"); You may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.sun.com/software/imaging/JAI/index.html * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS * AND WIDGET HANDLING SOURCE CODE. * The Initial Developer of the Original Code is: Sun Microsystems, Inc.. * Portions created by: Niles Ritter * are Copyright (C): Niles Ritter, GeoTIFF.org, 1999,2000. * All Rights Reserved. * Contributor(s): Niles Ritter */import java.awt.Rectangle;import java.awt.Transparency;import java.awt.color.ColorSpace;import java.awt.image.ComponentColorModel;import java.awt.image.DataBuffer;import java.awt.image.IndexColorModel;import java.awt.image.MultiPixelPackedSampleModel;import java.awt.image.PixelInterleavedSampleModel;import java.awt.image.Raster;import java.awt.image.WritableRaster;import java.io.IOException;import javax.media.jai.RasterFactory;import org.libtiff.jai.codec.XTIFF;import org.libtiff.jai.codec.XTIFFDecodeParam;import org.libtiff.jai.codec.XTIFFDirectory;import org.libtiff.jai.codec.XTIFFField;import org.libtiff.jai.codec.XTIFFTileCodec;import org.libtiff.jai.util.JaiI18N;import com.sun.media.jai.codec.ImageCodec;import com.sun.media.jai.codec.SeekableStream;import com.sun.media.jai.codec.TIFFDecodeParam;import com.sun.media.jai.codecimpl.SimpleRenderedImage;public class XTIFFImage extends SimpleRenderedImage { XTIFFTileCodec codec; XTIFFDirectory dir; TIFFDecodeParam param; int photometric_interp; SeekableStream stream; int tileSize; int tilesX, tilesY; long[] tileOffsets; long tileByteCounts[]; char colormap[]; char bitsPerSample[]; int samplesPerPixel; int extraSamples; byte palette[]; int bands; char sampleFormat[]; boolean decodePaletteAsShorts; boolean isBigEndian; // Image types int image_type; int dataType; /** * Constructs a XTIFFImage that acquires its data from a given * SeekableStream and reads from a particular IFD of the stream. The index * of the first IFD is 0. * * @param stream the SeekableStream to read from. * @param param an instance of TIFFDecodeParam, or null. * @param directory the index of the IFD to read from. */ public XTIFFImage(SeekableStream stream, TIFFDecodeParam param, int directory) throws IOException { this.stream = stream; if (param == null || !(param instanceof XTIFFDecodeParam)) { param = new XTIFFDecodeParam(param); } this.param = param; decodePaletteAsShorts = param.getDecodePaletteAsShorts(); // Read the specified directory. dir = XTIFFDirectory.create(stream, directory); properties.put("tiff.directory", dir); ((XTIFFDecodeParam) param).setDirectory(dir); // Check whether big endian or little endian format is used. isBigEndian = dir.isBigEndian(); setupImageParameters(); setupSamplesAndColor(); dir.setImageType(image_type); // Calculate number of tiles and the tileSize in bytes tilesX = (width + tileWidth - 1) / tileWidth; tilesY = (height + tileHeight - 1) / tileHeight; tileSize = tileWidth * tileHeight * bands; try { codec = dir.createTileCodec((XTIFFDecodeParam) param); } catch (Exception e) { } } /** * This method gets the image parameters from fields */ protected void setupImageParameters() { // Set basic image layout minX = minY = 0; width = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_IMAGE_WIDTH); height = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_IMAGE_LENGTH); photometric_interp = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_PHOTOMETRIC_INTERPRETATION); // Read the TIFFTAG_BITS_PER_SAMPLE field XTIFFField bitsField = dir.getField(XTIFF.TIFFTAG_BITS_PER_SAMPLE); if (bitsField == null) { // Default bitsPerSample = new char[1]; bitsPerSample[0] = 1; } else { bitsPerSample = bitsField.getAsChars(); } for (int i = 1; i < bitsPerSample.length; i++) { if (bitsPerSample[i] != bitsPerSample[1]) { throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder19")); } } // Get the number of samples per pixel XTIFFField sfield = dir.getField(XTIFF.TIFFTAG_SAMPLES_PER_PIXEL); if (sfield == null) { samplesPerPixel = 1; } else { samplesPerPixel = (int) sfield.getAsLong(0); } // Figure out if any extra samples are present. XTIFFField efield = dir.getField(XTIFF.TIFFTAG_EXTRA_SAMPLES); if (efield == null) { extraSamples = 0; } else { extraSamples = (int) efield.getAsLong(0); } // Read the TIFFTAG_SAMPLE_FORMAT tag to see whether the data might be // signed or floating point XTIFFField sampleFormatField = dir.getField(XTIFF.TIFFTAG_SAMPLE_FORMAT); if (sampleFormatField != null) { sampleFormat = sampleFormatField.getAsChars(); // Check that all the samples have the same format for (int l = 1; l < sampleFormat.length; l++) { if (sampleFormat[l] != sampleFormat[0]) { throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder20")); } } } else { sampleFormat = new char[] { 1 }; } if (sampleFormat[0] == 1 || sampleFormat[0] == 4) { // Unsigned or unknown if (bitsPerSample[0] == 8) { dataType = DataBuffer.TYPE_BYTE; } else if (bitsPerSample[0] == 16) { dataType = DataBuffer.TYPE_USHORT; } else if (bitsPerSample[0] == 32) { dataType = DataBuffer.TYPE_INT; } } else if (sampleFormat[0] == 2) { // Signed if (bitsPerSample[0] == 1 || bitsPerSample[0] == 4 || bitsPerSample[0] == 8) { throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder21")); } else if (bitsPerSample[0] == 16) { dataType = DataBuffer.TYPE_SHORT; } else if (bitsPerSample[0] == 32) { dataType = DataBuffer.TYPE_INT; } } else if (sampleFormat[0] == 3) { // Floating point // dataType = DataBuffer.TYPE_FLOAT; throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder22")); } if (dir.getField(XTIFF.TIFFTAG_TILE_WIDTH) != null) { // Image is in tiled format tileWidth = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_TILE_WIDTH); tileHeight = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_TILE_LENGTH); tileOffsets = (dir.getField(XTIFF.TIFFTAG_TILE_OFFSETS)).getAsLongs(); tileByteCounts = dir.getField(XTIFF.TIFFTAG_TILE_BYTE_COUNTS) .getAsLongs(); } else { // Image is in stripped format, looks like tiles to us tileWidth = width; XTIFFField field = dir.getField(XTIFF.TIFFTAG_ROWS_PER_STRIP); if (field == null) { // Default is infinity (2^32 -1), basically the entire image // TODO: Can do a better job of tiling here tileHeight = height; } else { long l = field.getAsLong(0); long infinity = 1; infinity = (infinity << 32) - 1; if (l == infinity) { // 2^32 - 1 (effectively infinity, entire image is 1 strip) tileHeight = height; } else { tileHeight = (int) l; } } XTIFFField tileOffsetsField = dir.getField(XTIFF.TIFFTAG_STRIP_OFFSETS); if (tileOffsetsField == null) { throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder11")); } else { tileOffsets = tileOffsetsField.getAsLongs(); } XTIFFField tileByteCountsField = dir.getField(XTIFF.TIFFTAG_STRIP_BYTE_COUNTS); if (tileByteCountsField == null) { throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder12")); } else { tileByteCounts = tileByteCountsField.getAsLongs(); } } } /** * This method constructs the sampleModel, colorModel, determines the * image_type and the bands parameter. */ protected void setupSamplesAndColor() { // Figure out which kind of image we are dealing with. switch (photometric_interp) { case XTIFF.PHOTOMETRIC_WHITE_IS_ZERO: bands = 1; // Bilevel or Grayscale - WhiteIsZero if (bitsPerSample[0] == 1) { image_type = XTIFF.TYPE_BILEVEL_WHITE_IS_ZERO; // Keep pixels packed, use IndexColorModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, tileWidth, tileHeight, 1); // Set up the palette byte r[] = new byte[] { (byte) 255, (byte) 0 }; byte g[] = new byte[] { (byte) 255, (byte) 0 }; byte b[] = new byte[] { (byte) 255, (byte) 0 }; colorModel = new IndexColorModel(1, 2, r, g, b); } else { image_type = XTIFF.TYPE_GREYSCALE_WHITE_IS_ZERO; if (bitsPerSample[0] == 4) { sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, tileWidth, tileHeight, 4); colorModel = ImageCodec.createGrayIndexColorModel(sampleModel, false); } else if (bitsPerSample[0] == 8) { sampleModel = RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, tileWidth, tileHeight, bands); colorModel = ImageCodec.createGrayIndexColorModel(sampleModel, false); } else if (bitsPerSample[0] == 16) { sampleModel = RasterFactory.createPixelInterleavedSampleModel(dataType, tileWidth, tileHeight, bands); colorModel = ImageCodec.createComponentColorModel(sampleModel); } else { throw new IllegalArgumentException(JaiI18N.getString("XTIFFImageDecoder14")); } } break; case XTIFF.PHOTOMETRIC_BLACK_IS_ZERO: bands = 1; // Bilevel or Grayscale - BlackIsZero if (bitsPerSample[0] == 1) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?