001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.image;
018:
019: import java.awt.Dimension;
020: import java.awt.image.DataBuffer;
021: import java.awt.image.Raster;
022: import java.awt.image.RenderedImage;
023: import java.awt.image.SampleModel;
024: import org.geotools.resources.Utilities;
025:
026: /**
027: * An image dimension, including the number of bands.
028: *
029: * @since 2.4
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/image/ImageDimension.java $
031: * @version $Id: ImageDimension.java 26761 2007-08-29 21:25:45Z desruisseaux $
032: * @author Martin Desruisseaux
033: */
034: public class ImageDimension extends Dimension {
035: /**
036: * For compatibility between different version of this class.
037: */
038: private static final long serialVersionUID = -4349573462196081362L;
039:
040: /**
041: * The number of bands in the image or raster.
042: */
043: public int numBands;
044:
045: /**
046: * The image data type.
047: */
048: private final int dataType;
049:
050: /**
051: * Creates a new dimension initialized to the dimension of the given image.
052: */
053: public ImageDimension(final RenderedImage image) {
054: super (image.getWidth(), image.getHeight());
055: final SampleModel model = image.getSampleModel();
056: numBands = model.getNumBands();
057: dataType = model.getDataType();
058: }
059:
060: /**
061: * Creates a new dimension initialized to the dimension of the given raster.
062: */
063: public ImageDimension(final Raster raster) {
064: super (raster.getWidth(), raster.getHeight());
065: numBands = raster.getNumBands();
066: dataType = raster.getDataBuffer().getDataType();
067: }
068:
069: /**
070: * Returns the number of sample values. This is the product of
071: * {@link #width width}, {@link #height height} and {@link #numBands}.
072: */
073: public long getNumSampleValues() {
074: return (long) width * (long) height * (long) numBands;
075: }
076:
077: /**
078: * Returns the number of bytes required in order to memorize {@linkplain #getNumSampleValues
079: * all sample values}. The sample values size is determined by the image or raster given at
080: * construction time.
081: */
082: public long getMemoryUsage() {
083: return getNumSampleValues()
084: * (DataBuffer.getDataTypeSize(dataType) / 8);
085: // TODO: replace 8 by Byte.SIZE when we will be allowed to compile for J2SE 1.5.
086: }
087:
088: /**
089: * Checks whether two dimension objects have equal values.
090: */
091: //@Override
092: public boolean equals(final Object object) {
093: if (super .equals(object)
094: && object.getClass().equals(getClass())) {
095: final ImageDimension that = (ImageDimension) object;
096: return this .numBands == that.numBands
097: && this .dataType == that.dataType;
098: }
099: return false;
100: }
101:
102: /**
103: * Returns the hash code for this dimension.
104: */
105: //@Override
106: public int hashCode() {
107: return super .hashCode() + 37 * numBands;
108: }
109:
110: /**
111: * Returns a string representation of this dimension.
112: */
113: //@Override
114: public String toString() {
115: return Utilities.getShortClassName(this ) + "[width=" + width
116: + ",height=" + height + ",numBands=" + numBands + ']';
117: }
118: }
|