<commit_before>/*
 * Copyright 2009, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.mortennobel.imagescaling;

import java.awt.*;

/**
 * This class let you create dimension constrains based on a actual image.
 *
 * Class may be subclassed to create user defined behavior. To do this you need to overwrite
 * the method getDimension(Dimension).
 */
public class DimensionConstrain {
	protected DimensionConstrain ()
	{
	}

	/**
	 * Will always return a dimension with positive width and height;
	 * @param dimension of the unscaled image
	 * @return the dimension of the scaled image
	 */
	public Dimension getDimension(Dimension dimension){
		return dimension;
	}

	/**
	 * Used when the destination size is fixed. This may not keep the image aspect radio
	 * @param width destination dimension width
	 * @param height destination dimension height
	 * @return  destination dimension (width x height)
	 */
	public static DimensionConstrain createAbsolutionDimension(final int width, final int height){
		assert width>0 && height>0:"Dimension must be a positive integer";
		return new DimensionConstrain(){
			public Dimension getDimension(Dimension dimension) {
				return new Dimension(width, height);
			}
		};
	}

	/**
	 * Used when the destination size is relative to the source. This keeps the image aspect radio
	 * @param fraction resize fraction (must be a positive number)
	 * @return the new dimension (the input dimension times the fraction)
	 */
	public static DimensionConstrain createRelativeDimension(final float fraction){
		return createRelativeDimension(fraction,fraction);
	}

	/**
	 * Used when the destination size is relative to the source. This keeps the image aspect radio
	 * @param
	 * @return
	 */
	public static DimensionConstrain createRelativeDimension(final float fractionWidth, final float fractionHeight){
		assert fractionHeight>0 && fractionWidth>0:"Fractions must be larger than 0.0";
		return new DimensionConstrain(){
			public Dimension getDimension(Dimension dimension) {
				int width = Math.max(1,Math.round(fractionWidth*dimension.width));
				int height = Math.max(1,Math.round(fractionHeight*dimension.height));
				return new Dimension(width, height);
			}
		};
	}

	/**
	 * Forces the image to keep radio and be keeped within the width and height
	 * @param width
	 * @param height
	 * @return
	 */
	public static DimensionConstrain createMaxDimension(int width, int height){<commit_msg>Improve javadoc on method createRelativeDimension<commit_after>/*
 * Copyright 2009, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.mortennobel.imagescaling;

import java.awt.*;

/**
 * This class let you create dimension constrains based on a actual image.
 *
 * Class may be subclassed to create user defined behavior. To do this you need to overwrite
 * the method getDimension(Dimension).
 */
public class DimensionConstrain {
	protected DimensionConstrain ()
	{
	}

	/**
	 * Will always return a dimension with positive width and height;
	 * @param dimension of the unscaled image
	 * @return the dimension of the scaled image
	 */
	public Dimension getDimension(Dimension dimension){
		return dimension;
	}

	/**
	 * Used when the destination size is fixed. This may not keep the image aspect radio
	 * @param width destination dimension width
	 * @param height destination dimension height
	 * @return  destination dimension (width x height)
	 */
	public static DimensionConstrain createAbsolutionDimension(final int width, final int height){
		assert width>0 && height>0:"Dimension must be a positive integer";
		return new DimensionConstrain(){
			public Dimension getDimension(Dimension dimension) {
				return new Dimension(width, height);
			}
		};
	}

	/**
	 * Used when the destination size is relative to the source. This keeps the image aspect radio
	 * @param fraction resize fraction (must be a positive number)
	 * @return the new dimension (the input dimension times the fraction)
	 */
	public static DimensionConstrain createRelativeDimension(final float fraction){
		return createRelativeDimension(fraction,fraction);
	}

	/**
	 * Used when the destination size is relative to the source. This keeps the image aspect radio if fractionWidth
     * equals fractionHeight
	 * @param
	 * @return
	 */
	public static DimensionConstrain createRelativeDimension(final float fractionWidth, final float fractionHeight){
		assert fractionHeight>0 && fractionWidth>0:"Fractions must be larger than 0.0";
		return new DimensionConstrain(){
			public Dimension getDimension(Dimension dimension) {
				int width = Math.max(1,Math.round(fractionWidth*dimension.width));
				int height = Math.max(1,Math.round(fractionHeight*dimension.height));
				return new Dimension(width, height);
			}
		};
	}

	/**
	 * Forces the image to keep radio and be keeped within the width and height
	 * @param width
	 * @param height
	 * @return
	 */
	public static DimensionConstrain createMaxDimension(int width, int height){
{'fraction_non_alphanumeric': 0.07370967741935484, 'fraction_numerical': 0.004193548387096774, 'mean_word_length': 4.137531068765535, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4154108', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1548, 'n_words': 709}