view servlet/src/digilib/image/ImageSize.java @ 486:a92f319aec1c

Servlet version 1.16.1 - now with absolute scaling: mo=ascale&scale=X - reworked scale factor and resolution selection so that absolute scale can use prescaled images (osize can do that too) ASSIGNED - # 23: display thumbnails with absolute scaling https://itgroup.mpiwg-berlin.mpg.de:8080/tracs/cdli/ticket/23
author robcast
date Sat, 12 May 2007 14:23:34 +0200
parents eec0d8c9c3c9
children
line wrap: on
line source

/*
 * ImageSize.java -- digilib image size class. 
 * Digital Image Library servlet components 
 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) 
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version. Please read license.txt for the full details. A copy of the GPL may
 * be found at http://www.gnu.org/copyleft/lgpl.html You should have received a
 * copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA Created on 26.08.2003
 */

package digilib.image;

/** Class for image size (width, height).
 * 
 * A width or height of 0 is treated as a 'wildcard' that matches any size.
 * 
 * @author casties
 *          
 */
public class ImageSize {
	public int width;
	public int height;

	public ImageSize() {
		super();
	}

	public ImageSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public ImageSize(ImageSize i) {
		this.width = i.width;
		this.height = i.height;
	}

	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	/**
	 * Returns if the size of this image is smaller in every dimension than the
	 * other image.
	 * 
	 * 
	 * 
	 * @param is
	 * @return
	 */
	public boolean isTotallySmallerThan(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height <= is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width <= is.width);
		}
		return ((this.width <= is.width)&&(this.height <= is.height));
	}

	/**
	 * Returns if the size of this image is smaller in at least one dimension
	 * than the other image.
	 * 
	 * @param is
	 * @return
	 */
	public boolean isSmallerThan(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height <= is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width <= is.width);
		}
		return ((this.width <= is.width) || (this.height <= is.height));
	}

	/**
	 * Returns if the size of this image is bigger in every dimension than the
	 * other image.
	 * 
	 * 
	 * 
	 * @param is
	 * @return
	 */
	public boolean isTotallyBiggerThan(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height >= is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width >= is.width);
		}
		return ((this.width >= is.width) && (this.height >= is.height));
	}

	/**
	 * Returns if the size of this image is bigger in at least one dimension
	 * than the other image.
	 * 
	 * 
	 * 
	 * @param is
	 * @return
	 */
	public boolean isBiggerThan(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height >= is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width >= is.width);
		}
		return ((this.width >= is.width) || (this.height >= is.height));
	}

	/**
	 * Returns if this image has the same size or height as the other image.
	 * 
	 * 
	 * 
	 * @param is
	 * @return
	 */
	public boolean fitsIn(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height == is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width == is.width);
		}
		return (
			(this.width == is.width)
				&& (this.height <= is.height)
				|| (this.width <= is.width)
				&& (this.height == is.height));
	}

	/**
	 * Returns if the size of this image is the same as the other image.
	 * 
	 * 
	 * 
	 * @param is
	 * @return
	 */
	public boolean equals(ImageSize is) {
		if ((this.width == 0)||(is.width == 0)) {
			// width wildcard
			return (this.height == is.height);
		}
		if ((this.height == 0)||(is.height == 0)) {
			// height wildcard
			return (this.width == is.width);
		}
		return ((this.width == is.width) && (this.height == is.height));
	}

	/**
	 * @return
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * Returns the aspect ratio.
	 * 
	 * Aspect ratio is (width/height). So it's <1 for portrait and  >1 for
	 * landscape.
	 * 
	 * @return
	 */
	public float getAspect() {
		return (height > 0) ? ((float) width / (float) height) : 0;
	}
	
	/**
	 * Returns a scaled copy of this image size. 
	 * 
	 * @param scale
	 * @return
	 */
	public ImageSize getScaled(float scale) {
		return new ImageSize((int) (width * scale), (int) (height * scale));
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String s = "[" + width + "x" + height + "]";
		return s;
	}
}