mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-04-25 00:00:03 -04:00
fixed merge conflicts
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:erlend@escenic.com">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public interface AreaOfInterest {
|
||||
|
||||
Rectangle getAOI(Rectangle pCrop);
|
||||
|
||||
Dimension getOriginalDimension();
|
||||
|
||||
int calculateX(Dimension pOriginalDimension, Rectangle pCrop);
|
||||
|
||||
int calculateY(Dimension pOriginalDimension, Rectangle pCrop);
|
||||
|
||||
Dimension getCrop(Dimension pOriginalDimension, Rectangle pCrop);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:erlend@escenic.com">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public class AreaOfInterestFactory {
|
||||
private final static AtomicReference<AreaOfInterestFactory> DEFAULT =
|
||||
new AtomicReference<AreaOfInterestFactory>(new AreaOfInterestFactory());
|
||||
|
||||
public static void setDefault(AreaOfInterestFactory factory) {
|
||||
DEFAULT.set(factory);
|
||||
}
|
||||
|
||||
public static AreaOfInterestFactory getDefault() {
|
||||
return DEFAULT.get();
|
||||
}
|
||||
|
||||
public AreaOfInterest createAreaOfInterest(int pDefaultWidth, int pDefaultHeight, boolean aoiPercent, boolean aoiUniform) {
|
||||
if (aoiPercent && aoiUniform) {
|
||||
throw new IllegalArgumentException("Cannot be both uniform and percent Area of Interest");
|
||||
}
|
||||
if (aoiPercent) {
|
||||
return new PercentAreaOfInterest(pDefaultWidth, pDefaultHeight);
|
||||
}
|
||||
else if (aoiUniform) {
|
||||
return new UniformAreaOfInterest(pDefaultWidth, pDefaultHeight);
|
||||
}
|
||||
return new DefaultAreaOfInterest(pDefaultWidth, pDefaultHeight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import com.twelvemonkeys.lang.Validate;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:erlend@escenic.com">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public class AreaOfInterestWrapper implements AreaOfInterest {
|
||||
private AreaOfInterest mDelegate;
|
||||
|
||||
public AreaOfInterestWrapper(AreaOfInterest mDelegate) {
|
||||
this.mDelegate = Validate.notNull(mDelegate);
|
||||
}
|
||||
|
||||
public Rectangle getAOI(Rectangle pCrop) {
|
||||
return mDelegate.getAOI(pCrop);
|
||||
}
|
||||
|
||||
public Dimension getOriginalDimension() {
|
||||
return mDelegate.getOriginalDimension();
|
||||
}
|
||||
|
||||
public int calculateX(Dimension pOriginalDimension, Rectangle pCrop) {
|
||||
return mDelegate.calculateX(pOriginalDimension, pCrop);
|
||||
}
|
||||
|
||||
public int calculateY(Dimension pOriginalDimension, Rectangle pCrop) {
|
||||
return mDelegate.calculateY(pOriginalDimension, pCrop);
|
||||
}
|
||||
|
||||
public Dimension getCrop(Dimension pOriginalDimension, Rectangle pCrop) {
|
||||
return mDelegate.getCrop(pOriginalDimension, pCrop);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author <a href="mailto:erlend@hamnaberg.net">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public class DefaultAreaOfInterest implements AreaOfInterest {
|
||||
private final int mOriginalWidth;
|
||||
private final int mOriginalHeight;
|
||||
|
||||
public DefaultAreaOfInterest(int pOriginalWidth, int pOriginalHeight) {
|
||||
this.mOriginalWidth = pOriginalWidth;
|
||||
this.mOriginalHeight = pOriginalHeight;
|
||||
}
|
||||
|
||||
public DefaultAreaOfInterest(Dimension pOriginalDimension) {
|
||||
this(pOriginalDimension.width, pOriginalDimension.height);
|
||||
}
|
||||
|
||||
Rectangle getAOI(final int pX, final int pY, final int pWidth, final int pHeight) {
|
||||
return getAOI(new Rectangle(pX, pY, pWidth, pHeight));
|
||||
}
|
||||
|
||||
public Rectangle getAOI(final Rectangle pCrop) {
|
||||
int y = pCrop.y;
|
||||
int x = pCrop.x;
|
||||
Dimension dimension = getOriginalDimension();
|
||||
|
||||
Dimension crop = getCrop(dimension, pCrop);
|
||||
|
||||
// Center
|
||||
if (y < 0) {
|
||||
y = calculateY(dimension, new Rectangle(x, y, crop.width, crop.height));
|
||||
}
|
||||
|
||||
if (x < 0) {
|
||||
x = calculateX(dimension, new Rectangle(x, y, crop.width, crop.height));
|
||||
}
|
||||
return new Rectangle(x, y, crop.width, crop.height);
|
||||
}
|
||||
|
||||
public Dimension getOriginalDimension() {
|
||||
return new Dimension(mOriginalWidth, mOriginalHeight);
|
||||
}
|
||||
|
||||
public int calculateX(Dimension pOriginalDimension, Rectangle pCrop) {
|
||||
return (pOriginalDimension.width - pCrop.width) / 2;
|
||||
}
|
||||
|
||||
public int calculateY(Dimension pOriginalDimension, Rectangle pCrop) {
|
||||
return (pOriginalDimension.height - pCrop.height) / 2;
|
||||
}
|
||||
|
||||
public Dimension getCrop(Dimension pOriginalDimension, final Rectangle pCrop) {
|
||||
int mOriginalWidth1 = pOriginalDimension.width;
|
||||
int mOriginalHeight1 = pOriginalDimension.height;
|
||||
int x = pCrop.x;
|
||||
int y = pCrop.y;
|
||||
int cropWidth = pCrop.width;
|
||||
int cropHeight = pCrop.height;
|
||||
|
||||
if (cropWidth < 0 || (x < 0 && cropWidth > mOriginalWidth1)
|
||||
|| (x >= 0 && (x + cropWidth) > mOriginalWidth1)) {
|
||||
cropWidth = (x >= 0 ? mOriginalWidth1 - x : mOriginalWidth1);
|
||||
}
|
||||
if (cropHeight < 0 || (y < 0 && cropHeight > mOriginalHeight1)
|
||||
|| (y >= 0 && (y + cropHeight) > mOriginalHeight1)) {
|
||||
cropHeight = (y >= 0 ? mOriginalHeight1 - y : mOriginalHeight1);
|
||||
}
|
||||
return new Dimension(cropWidth, cropHeight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:erlend@escenic.com">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public class PercentAreaOfInterest extends DefaultAreaOfInterest {
|
||||
|
||||
public PercentAreaOfInterest(Dimension pOriginalDimension) {
|
||||
super(pOriginalDimension);
|
||||
}
|
||||
|
||||
public PercentAreaOfInterest(int pOriginalWidth, int pOriginalHeight) {
|
||||
super(pOriginalWidth, pOriginalHeight);
|
||||
}
|
||||
|
||||
public Dimension getCrop(Dimension pOriginalDimension, final Rectangle pCrop) {
|
||||
int cropWidth = pCrop.width;
|
||||
int cropHeight = pCrop.height;
|
||||
float ratio;
|
||||
|
||||
if (cropWidth >= 0 && cropHeight >= 0) {
|
||||
// Non-uniform
|
||||
cropWidth = Math.round((float) pOriginalDimension.width * (float) pCrop.width / 100f);
|
||||
cropHeight = Math.round((float) pOriginalDimension.height * (float) pCrop.height / 100f);
|
||||
}
|
||||
else if (cropWidth >= 0) {
|
||||
// Find ratio from pWidth
|
||||
ratio = (float) cropWidth / 100f;
|
||||
cropWidth = Math.round((float) pOriginalDimension.width * ratio);
|
||||
cropHeight = Math.round((float) pOriginalDimension.height * ratio);
|
||||
|
||||
}
|
||||
else if (cropHeight >= 0) {
|
||||
// Find ratio from pHeight
|
||||
ratio = (float) cropHeight / 100f;
|
||||
cropWidth = Math.round((float) pOriginalDimension.width * ratio);
|
||||
cropHeight = Math.round((float) pOriginalDimension.height * ratio);
|
||||
}
|
||||
// Else: No crop
|
||||
|
||||
return new Dimension(cropWidth, cropHeight);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.twelvemonkeys.servlet.image.aoi;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:erlend@escenic.com">Erlend Hamnaberg</a>
|
||||
* @version $Revision: $
|
||||
*/
|
||||
public class UniformAreaOfInterest extends DefaultAreaOfInterest {
|
||||
|
||||
public UniformAreaOfInterest(Dimension pOriginalDimension) {
|
||||
super(pOriginalDimension);
|
||||
}
|
||||
|
||||
public UniformAreaOfInterest(int pOriginalWidth, int pOriginalHeight) {
|
||||
super(pOriginalWidth, pOriginalHeight);
|
||||
}
|
||||
|
||||
public Dimension getCrop(Dimension pOriginalDimension, final Rectangle pCrop) {
|
||||
float ratio;
|
||||
if (pCrop.width >= 0 && pCrop.height >= 0) {
|
||||
// Compute both ratios
|
||||
ratio = (float) pCrop.width / (float) pCrop.height;
|
||||
float originalRatio = (float) pOriginalDimension.width / (float) pOriginalDimension.height;
|
||||
if (ratio > originalRatio) {
|
||||
pCrop.width = pOriginalDimension.width;
|
||||
pCrop.height = Math.round((float) pOriginalDimension.width / ratio);
|
||||
}
|
||||
else {
|
||||
pCrop.height = pOriginalDimension.height;
|
||||
pCrop.width = Math.round((float) pOriginalDimension.height * ratio);
|
||||
}
|
||||
}
|
||||
else if (pCrop.width >= 0) {
|
||||
// Find ratio from pWidth
|
||||
ratio = (float) pCrop.width / (float) pOriginalDimension.width;
|
||||
pCrop.height = Math.round((float) pOriginalDimension.height * ratio);
|
||||
}
|
||||
else if (pCrop.height >= 0) {
|
||||
// Find ratio from pHeight
|
||||
ratio = (float) pCrop.height / (float) pOriginalDimension.height;
|
||||
pCrop.width = Math.round((float) pOriginalDimension.width * ratio);
|
||||
}
|
||||
// Else: No crop
|
||||
return new Dimension(pCrop.width, pCrop.height);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user