[jboss-cvs] jboss-seam/src/main/org/jboss/seam/core ...
Peter Muir
peter at bleepbleep.org.uk
Mon Mar 5 15:59:07 EST 2007
User: pmuir
Date: 07/03/05 15:59:07
Modified: src/main/org/jboss/seam/core Image.java
Log:
Support s:imageTransform
Revision Changes Path
1.3 +125 -81 jboss-seam/src/main/org/jboss/seam/core/Image.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Image.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/Image.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- Image.java 4 Mar 2007 22:22:53 -0000 1.2
+++ Image.java 5 Mar 2007 20:59:07 -0000 1.3
@@ -11,9 +11,13 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
+import javax.imageio.ImageReadParam;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
import org.jboss.seam.Component;
import org.jboss.seam.InterceptionType;
@@ -27,6 +31,7 @@
/**
* Image manipulation and interrogation
+ *
* @author pmuir
*
*/
@@ -39,15 +44,19 @@
public enum Type
{
- IMAGE_PNG("image/png", ".png", "PNG"),
- IMAGE_JPEG("image/jpeg", ".jpg", "JPG", "image/jpg");
+ IMAGE_PNG("image/png", ".png", "PNG"), IMAGE_JPEG("image/jpeg", ".jpg", "JPEG", "image/jpg"), IMAGE_GIF(
+ "image/gif", ".gif", "GIF");
private String mimeType;
+
private String extension;
+
private String imageFormatName;
+
private List<String> alternativeMimeTypes;
- Type(String mimeType, String extension, String imageFormatName, String...alternativeMimeTypes)
+ Type(String mimeType, String extension, String imageFormatName,
+ String... alternativeMimeTypes)
{
this.mimeType = mimeType;
this.extension = extension;
@@ -75,7 +84,7 @@
return imageFormatName;
}
- public static Type getType(String mimeType)
+ public static Type getTypeByMimeType(String mimeType)
{
for (Type type : values())
{
@@ -86,6 +95,18 @@
}
return null;
}
+
+ public static Type getTypeByFormatName(String formatName)
+ {
+ for (Type type : values())
+ {
+ if (type.getImageFormatName().equalsIgnoreCase(formatName))
+ {
+ return type;
+ }
+ }
+ return null;
+ }
}
public static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
@@ -107,26 +128,29 @@
}
/**
- * Set the image. This can be one of String (loaded from the classpath), a URL, a File, an InputStream or a byte[]
+ * Set the image. This can be one of String (loaded from the classpath), a
+ * URL, a File, an InputStream or a byte[]
*
* @param value
+ * @throws IOException
*/
- public Image set(Object value)
+ public Image set(Object value) throws IOException
{
this.input = value;
- bufferedImage = null;
+ readImage();
return this;
}
/**
- * Get the image, any conversions having been applied. Returns null if the image could not be read
+ * Get the image, any conversions having been applied. Returns null if the
+ * image could not be read
*/
public byte[] get() throws IOException
{
- if (isImage() && dirty)
+ if (dirty)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- ImageIO.write(getBufferedImage(), getContentType().getImageFormatName(), outputStream);
+ ImageIO.write(bufferedImage, getContentType().getImageFormatName(), outputStream);
output = outputStream.toByteArray();
}
return output;
@@ -151,8 +175,7 @@
public double getRatio() throws IOException
{
// Do the operation with double precision
- BufferedImage image = getBufferedImage();
- Double ratio = (double) image.getWidth() / (double) image.getHeight();
+ Double ratio = (double) bufferedImage.getWidth() / (double) bufferedImage.getHeight();
return ratio;
}
@@ -170,7 +193,7 @@
*/
public int getWidth() throws IOException
{
- return getBufferedImage().getWidth();
+ return bufferedImage.getWidth();
}
/**
@@ -178,21 +201,13 @@
*/
public int getHeight() throws IOException
{
- return getBufferedImage().getHeight();
+ return bufferedImage.getHeight();
}
/**
- * Check that the value passed is an image
- */
- public boolean isImage() throws IOException
- {
- return getBufferedImage() != null;
- }
-
- /**
- * Alter the ratio of the output image <b>without</b> altering the ratio of the input
- * by adding transparent strips. If the image is already of the correct ratio (to within
- * the given precision) nothing happens
+ * Alter the ratio of the output image <b>without</b> altering the ratio of
+ * the input by adding transparent strips. If the image is already of the
+ * correct ratio (to within the given precision) nothing happens
*/
public Image adjustRatio(double desiredRatio, double precision) throws InterruptedException,
IOException
@@ -205,10 +220,10 @@
// top/bottom to make the image squarer
double desiredHeight = getRatio() * getHeight() / desiredRatio;
double stripHeight = (desiredHeight - getHeight()) / 2;
- BufferedImage newImage = new BufferedImage(getWidth(), (int) (getHeight() + stripHeight * 2),
- DEFAULT_IMAGE_TYPE);
+ BufferedImage newImage = new BufferedImage(getWidth(),
+ (int) (getHeight() + stripHeight * 2), DEFAULT_IMAGE_TYPE);
Graphics2D graphics2D = createGraphics(newImage);
- graphics2D.drawImage(getBufferedImage(), 0, (int) stripHeight, null);
+ graphics2D.drawImage(bufferedImage, 0, (int) stripHeight, null);
bufferedImage = newImage;
}
else if (getRatio() < desiredRatio)
@@ -217,11 +232,10 @@
// top/bottom to make the image squarer
double desiredWidth = getRatio() * getWidth() / desiredRatio;
double stripWidth = (desiredWidth - getWidth()) / 2;
- BufferedImage image = getBufferedImage();
- BufferedImage newImage = new BufferedImage((int) (getWidth() + stripWidth * 2), getHeight(),
- DEFAULT_IMAGE_TYPE);
+ BufferedImage newImage = new BufferedImage((int) (getWidth() + stripWidth * 2),
+ getHeight(), DEFAULT_IMAGE_TYPE);
Graphics2D graphics2D = createGraphics(newImage);
- graphics2D.drawImage(image, (int) stripWidth, 0, null);
+ graphics2D.drawImage(bufferedImage, (int) stripWidth, 0, null);
bufferedImage = newImage;
}
dirty = true;
@@ -239,7 +253,7 @@
int height = width * getHeight() / getWidth();
BufferedImage newImage = new BufferedImage(width, height, DEFAULT_IMAGE_TYPE);
Graphics2D graphics2D = createGraphics(newImage);
- graphics2D.drawImage(getBufferedImage(), 0, 0, width, height, null);
+ graphics2D.drawImage(bufferedImage, 0, 0, width, height, null);
bufferedImage = newImage;
dirty = true;
return this;
@@ -255,43 +269,38 @@
int width = height * getWidth() / getHeight();
BufferedImage newImage = new BufferedImage(width, height, DEFAULT_IMAGE_TYPE);
Graphics2D graphics2D = createGraphics(newImage);
- graphics2D.drawImage(getBufferedImage(), 0, 0, width, height, null);
+ graphics2D.drawImage(bufferedImage, 0, 0, width, height, null);
bufferedImage = newImage;
dirty = true;
return this;
}
- private BufferedImage getBufferedImage() throws IOException
- {
- if (bufferedImage == null)
+ private void readImage() throws IOException
{
if (input instanceof URL)
{
- bufferedImage = ImageIO.read((URL) input);
+ readImage(((URL) input).openStream());
}
else if (input instanceof File)
{
- bufferedImage = ImageIO.read((File) input);
+ readImage(((File) input).toURL().openStream());
}
else if (input instanceof String)
{
- bufferedImage = ImageIO.read(Resources.getResource((String) input));
+ readImage(Resources.getResourceAsStream((String) input));
}
else if (input instanceof InputStream)
{
- bufferedImage = ImageIO.read((InputStream) input);
+ readImage((InputStream) input);
}
else if (input != null && input.getClass().isArray())
{
if (input.getClass().getComponentType().isAssignableFrom(Byte.TYPE))
{
byte[] b = (byte[]) input;
- bufferedImage = ImageIO.read(new ByteArrayInputStream(b));
- }
+ readImage(new ByteArrayInputStream(b));
}
- dirty = true;
}
- return bufferedImage;
}
/**
@@ -311,10 +320,45 @@
public static Image instance()
{
- if ( !Contexts.isConversationContextActive() )
+ if (!Contexts.isConversationContextActive())
{
throw new IllegalStateException("No active conversation scope");
}
return (Image) Component.getInstance(Image.class, true);
}
+
+ private void readImage(InputStream inputStream) throws IOException
+ {
+ ImageInputStream stream = ImageIO.createImageInputStream(inputStream);
+ if (stream == null)
+ {
+ throw new IllegalArgumentException("stream == null!");
+ }
+
+ Iterator iter = ImageIO.getImageReaders(stream);
+ if (!iter.hasNext())
+ {
+ return;
+ }
+
+ ImageReader reader = (ImageReader) iter.next();
+ ImageReadParam param = reader.getDefaultReadParam();
+ reader.setInput(stream, true, true);
+ String type = reader.getFormatName();
+ setContentType(Type.getTypeByFormatName(type));
+ bufferedImage = reader.read(0, param);
+ stream.close();
+ reader.dispose();
+ inputStream.reset();
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ byte[] buffer = new byte[256];
+ while (true)
+ {
+ int bytesRead = inputStream.read(buffer);
+ if (bytesRead == -1) break;
+ os.write(buffer, 0, bytesRead);
+ }
+ output = os.toByteArray();
+ inputStream.close();
+ }
}
More information about the jboss-cvs-commits
mailing list