001: package it.geosolutions.imageio.plugins.slices2D;
002:
003: import java.awt.Transparency;
004: import java.awt.color.ColorSpace;
005: import java.awt.image.BufferedImage;
006: import java.awt.image.ColorModel;
007: import java.awt.image.ComponentColorModel;
008: import java.awt.image.DataBuffer;
009: import java.awt.image.SampleModel;
010: import java.io.File;
011: import java.io.IOException;
012: import java.util.Iterator;
013: import java.util.logging.Logger;
014:
015: import javax.imageio.ImageReadParam;
016: import javax.imageio.ImageReader;
017: import javax.imageio.metadata.IIOMetadata;
018: import javax.imageio.spi.ImageReaderSpi;
019:
020: import com.sun.media.imageioimpl.common.ImageUtil;
021: import com.sun.media.jai.codecimpl.util.RasterFactory;
022:
023: /**
024: * An abstract super class which need to be extended by any specific format's
025: * <code>ImageReader</code> to work with 2D datasets.
026: *
027: * @author Romagnoli Daniele
028: */
029: public abstract class SliceImageReader extends ImageReader implements
030: IndexManager {
031: private static final Logger LOGGER = org.geotools.util.logging.Logging
032: .getLogger("it.geosolutions.imageio.plugins.slices2D");
033:
034: /** set it to <code>true</code> when initialization has been performed */
035: protected boolean isInitialized = false;
036:
037: /** The originating <code>File</code> */
038: protected File originatingFile = null;
039:
040: protected SliceImageReader(ImageReaderSpi originatingProvider) {
041: super (originatingProvider);
042: }
043:
044: /** Implements this method to allow structure initialization. */
045: protected abstract void initialize() throws IOException;
046:
047: public abstract int getHeight(int imageIndex) throws IOException;
048:
049: public abstract int getWidth(int imageIndex) throws IOException;
050:
051: /**
052: * Specific implementation of the read Operation. Any format's reader need
053: * to implement its own version of the read operation.
054: */
055: public abstract BufferedImage read(int imageIndex,
056: ImageReadParam param) throws IOException;
057:
058: /** return imageMetadata related to a specific product or subdataset. */
059: public abstract IIOMetadata getImageMetadata(int imageIndex)
060: throws IOException;
061:
062: /** return streamMetadata related to the whole source. */
063: public abstract IIOMetadata getStreamMetadata() throws IOException;
064:
065: public abstract Iterator getImageTypes(int imageIndex)
066: throws IOException;
067:
068: /**
069: * Utilty method returning a proper <code>ColorModel</code> given an input
070: * <code>SampleModel</code>
071: *
072: * @param sm
073: * The <code>SampleModel</code> for which we need to create a
074: * compatible <code>ColorModel</code>.
075: *
076: * @return the created <code>ColorModel</code>
077: */
078: protected ColorModel retrieveColorModel(final SampleModel sm) {
079: final int nBands = sm.getNumBands();
080: final int bufferType = sm.getDataType();
081: ColorModel cm = null;
082: ColorSpace cs = null;
083: if (nBands > 1) {
084: // Number of Bands > 1.
085: // ImageUtil.createColorModel provides to Creates a
086: // ColorModel that may be used with the specified
087: // SampleModel
088: cm = ImageUtil.createColorModel(sm);
089: if (cm == null)
090: LOGGER.info("There are no ColorModels found");
091:
092: } else if ((bufferType == DataBuffer.TYPE_BYTE)
093: || (bufferType == DataBuffer.TYPE_USHORT)
094: || (bufferType == DataBuffer.TYPE_INT)
095: || (bufferType == DataBuffer.TYPE_FLOAT)
096: || (bufferType == DataBuffer.TYPE_DOUBLE)) {
097:
098: // Just one band. Using the built-in Gray Scale Color Space
099: cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
100: cm = RasterFactory.createComponentColorModel(bufferType, // dataType
101: cs, // color space
102: false, // has alpha
103: false, // is alphaPremultiplied
104: Transparency.OPAQUE); // transparency
105: } else {
106: if (bufferType == DataBuffer.TYPE_SHORT) {
107: // Just one band. Using the built-in Gray Scale Color
108: // Space
109: cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
110: cm = new ComponentColorModel(cs, false, false,
111: Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
112: }
113: }
114: return cm;
115: }
116:
117: }
|