[Yanel-commits] rev 59435 - public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image
michi at wyona.com
michi at wyona.com
Sun Jul 17 23:02:46 CEST 2011
Author: michi
Date: 2011-07-17 23:02:46 +0200 (Sun, 17 Jul 2011)
New Revision: 59435
Modified:
public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java
Log:
calculation of scale factor improved, javadoc added and exists implemented
Modified: public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java 2011-07-17 16:27:58 UTC (rev 59434)
+++ public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java 2011-07-17 21:02:46 UTC (rev 59435)
@@ -17,15 +17,19 @@
import javax.imageio.ImageIO;
/**
- *
+ * Resource to scale (down) images (but only jpeg so far)
*/
public class ImageResource extends Resource implements ViewableV2 {
private static Logger log = Logger.getLogger(ImageResource.class);
+ private String DEFAULT_CACHE_DIRECTORY = "/cached-images";
+
+ /**
+ * @see org.wyona.yanel.core.api.attributes.ViewableV2#exists()
+ */
public boolean exists() throws Exception {
- // TODO Auto-generated method stub
- return false;
+ return getRealm().getRepository().existsNode(getPath());
}
public long getSize() throws Exception {
@@ -34,10 +38,12 @@
}
/**
- *
+ * @see org.wyona.yanel.core.api.attributes.ViewableV2#getView(String)
*/
public View getView(String viewId) throws Exception {
- View view = new View();
+ if (!exists()) {
+ throw new org.wyona.yanel.core.ResourceNotFoundException("No such image: " + getPath());
+ }
// TODO: Compare last modified!
@@ -45,35 +51,25 @@
int sourceWidth = sourceImg.getWidth();
int sourceHeight = sourceImg.getHeight();
if (log.isDebugEnabled()) log.debug("Source Width: " + sourceWidth + ", Source Height: " + sourceHeight);
- double sourceRatio = (double) sourceWidth / (double) sourceHeight;
- int destWidth = sourceWidth;
- //if (getEnvironment().getRequest().getParameter("width") != null) destWidth = new Integer(getEnvironment().getRequest().getParameter("width")).intValue();
- if (getResourceConfigProperty("width") != null) destWidth = new Integer(getResourceConfigProperty("width")).intValue();
+ int destWidth = getDestWidth();
+ int destHeight = getDestHeight();
- int destHeight = sourceHeight;
- //if (getEnvironment().getRequest().getParameter("height") != null) destHeight = new Integer(getEnvironment().getRequest().getParameter("height")).intValue();
- if (getResourceConfigProperty("height") != null) destHeight = new Integer(getResourceConfigProperty("height")).intValue();
-
if (log.isDebugEnabled()) log.debug(" Dest Width: " + destWidth + ", Dest Height: " + destHeight);
- double destRatio = (double) destWidth / (double) destHeight;
-
- // TODO: If either destination width or destination height is not specified, then use sourceRatio in order to compute the correct width ot height which was not specified!
- if (sourceRatio != destRatio) log.error("Source (" + sourceRatio + ") and destination (" + destRatio + ") width/height ratio are NOT the same!");
-
- double scaleFactor = (double) destHeight / (double) sourceHeight;
+ double scaleFactor = getScaleFactor(sourceWidth, sourceHeight, destWidth, destHeight); //(double) destHeight / (double) sourceHeight;
if (log.isDebugEnabled()) log.debug("Scale factor: " + scaleFactor);
String cacheRootPath = getResourceConfigProperty("cache-root-path");
if (cacheRootPath == null) {
- cacheRootPath = "/cached-images";
+ cacheRootPath = DEFAULT_CACHE_DIRECTORY;
log.warn("No cache root path configured within resource configuration. Use default '" + cacheRootPath + "'!");
}
org.wyona.yarep.core.Node cacheNode = org.wyona.yarep.util.YarepUtil.addNodes(getRealm().getRepository(), cacheRootPath + getPath(), org.wyona.yarep.core.NodeType.RESOURCE);
ImageIO.write(scale(sourceImg, scaleFactor), "JPG", cacheNode.getOutputStream());
+ View view = new View();
view.setInputStream(cacheNode.getInputStream());
view.setMimeType("image/jpeg");
return view;
@@ -98,4 +94,67 @@
resizer.filter(orig, result);
return result;
}
+
+ /**
+ * Get destination width
+ */
+ private int getDestWidth() throws Exception {
+ // TODO: Get destination width from query string: getEnvironment().getRequest().getParameter("width")
+ if (getResourceConfigProperty("width") != null) {
+ return new Integer(getResourceConfigProperty("width")).intValue();
+ } else {
+ log.debug("No destination width configured");
+ if (getResourceConfigProperty("height") != null) {
+ return -1;
+ } else {
+ throw new Exception("Neither height nor width configured!");
+ }
+ }
+ }
+
+ /**
+ * Get destination height
+ */
+ private int getDestHeight() throws Exception {
+ // TODO: Get destination height from query string: getEnvironment().getRequest().getParameter("height")
+ if (getResourceConfigProperty("height") != null) {
+ return new Integer(getResourceConfigProperty("height")).intValue();
+ } else {
+ log.debug("No destination height configured");
+ if (getResourceConfigProperty("width") != null) {
+ return -1;
+ } else {
+ throw new Exception("Neither height nor width configured!");
+ }
+ }
+ }
+
+ /**
+ * Get scale factor
+ */
+ private double getScaleFactor(double sourceWidth, double sourceHeight, double destWidth, double destHeight) {
+ if (destWidth > 0 && destHeight <= 0) {
+ log.debug("Use width to calculate scale factor");
+ return (double) destWidth / (double) sourceWidth;
+ } else if (destWidth <= 0 && destHeight > 0) {
+ log.debug("Use height to calculate scale factor");
+ return (double) destHeight / (double) sourceHeight;
+ } else if (destWidth <= 0 && destHeight <= 0) {
+ log.error("Neither destination width nor height make sense to calculate scale factor, hence do not scale!");
+ return 1;
+ } else if (destWidth > 0 && destHeight > 0) {
+ log.warn("Width or height could be used, hence use height to calculate scale factor");
+
+ double destRatio = (double) destWidth / (double) destHeight;
+ double sourceRatio = (double) sourceWidth / (double) sourceHeight;
+ if (sourceRatio != destRatio) {
+ log.warn("Source (" + sourceRatio + ") and destination (" + destRatio + ") width/height ratio are NOT the same!");
+ }
+
+ return (double) destHeight / (double) sourceHeight;
+ } else {
+ log.error("We should never get here!");
+ return 1;
+ }
+ }
}
More information about the Yanel-commits
mailing list