implemented user photos! (imagestore table, ImageRetrieve servlet, a lot of
the underlying support) - incidentally, this is a lot of support for the SIG logo as well, just need some front end work for that in the future (of course, we now require JAI 1.1.1)
This commit is contained in:
61
src/com/silverwrist/util/image/ImageLengthPair.java
Normal file
61
src/com/silverwrist/util/image/ImageLengthPair.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Communities System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util.image;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public final class ImageLengthPair
|
||||
{
|
||||
/*--------------------------------------------------------------------------------
|
||||
* Attributes
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
private int length;
|
||||
private InputStream data;
|
||||
|
||||
/*--------------------------------------------------------------------------------
|
||||
* Constructor
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public ImageLengthPair(int length, InputStream data)
|
||||
{
|
||||
this.length = length;
|
||||
this.data = data;
|
||||
|
||||
} // end constructor
|
||||
|
||||
/*--------------------------------------------------------------------------------
|
||||
* External operations
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public final int getLength()
|
||||
{
|
||||
return length;
|
||||
|
||||
} // end getLength
|
||||
|
||||
public final InputStream getData()
|
||||
{
|
||||
return data;
|
||||
|
||||
} // end getData
|
||||
|
||||
} // end class ImageLengthPair
|
||||
153
src/com/silverwrist/util/image/ImageNormalizer.java
Normal file
153
src/com/silverwrist/util/image/ImageNormalizer.java
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Communities System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util.image;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.image.*;
|
||||
import java.awt.image.renderable.ParameterBlock;
|
||||
import java.io.*;
|
||||
import javax.media.jai.*;
|
||||
import com.sun.media.jai.codec.*;
|
||||
|
||||
public class ImageNormalizer
|
||||
{
|
||||
/*--------------------------------------------------------------------------------
|
||||
* External static operations
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public static ImageLengthPair normalizeImage(InputStream raw_data, int width, int height, String out_format)
|
||||
throws ImageNormalizerException
|
||||
{
|
||||
PlanarImage img1;
|
||||
try
|
||||
{ // Start by loading the image we're working with.
|
||||
SeekableStream istm = new ForwardSeekableStream(raw_data);
|
||||
RenderedOp rop1 = JAI.create("stream",istm);
|
||||
img1 = rop1.getRendering();
|
||||
|
||||
} // end try
|
||||
catch (RuntimeException re)
|
||||
{ // unable to get the rendering here!
|
||||
throw new ImageNormalizerException("Image data not a valid image format",re);
|
||||
|
||||
} // end catch
|
||||
|
||||
try
|
||||
{ // Compute the scaling factors required to get the image down to the appropriate size, then choose
|
||||
// the smaller of the two to use as the final scaling factor.
|
||||
Float scale_width = null, scale_height = null;
|
||||
if (img1.getWidth()>width)
|
||||
scale_width = new Float((float)width / (float)(img1.getWidth()));
|
||||
if (img1.getHeight()>height)
|
||||
scale_height = new Float((float)height / (float)(img1.getHeight()));
|
||||
Float scale = null;
|
||||
if (scale_width!=null)
|
||||
{ // we can scale by width, how about height?
|
||||
if (scale_height!=null)
|
||||
{ // yes, height too...pick the smaller of the two
|
||||
if (scale_width.floatValue()<scale_height.floatValue())
|
||||
scale = scale_width;
|
||||
else
|
||||
scale = scale_height;
|
||||
|
||||
} // end if
|
||||
else // no, just width
|
||||
scale = scale_width;
|
||||
|
||||
} // end if
|
||||
else if (scale_height!=null) // can only scale by height
|
||||
scale = scale_height;
|
||||
|
||||
// If we need to scale the image now, do so.
|
||||
PlanarImage img2;
|
||||
if (scale!=null)
|
||||
{ // scale the image down!
|
||||
ParameterBlock pb1 = new ParameterBlock();
|
||||
pb1.addSource(img1);
|
||||
pb1.add(scale.floatValue());
|
||||
pb1.add(scale.floatValue());
|
||||
pb1.add(0.0F);
|
||||
pb1.add(0.0F);
|
||||
pb1.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
|
||||
img2 = JAI.create("scale",pb1);
|
||||
|
||||
} // end if
|
||||
else // just use this as the next image
|
||||
img2 = img1;
|
||||
|
||||
// Figure out the offsets required to center the new image within the "frame."
|
||||
int offset_x = (width - img2.getWidth()) / 2;
|
||||
int offset_y = (height - img2.getHeight()) / 2;
|
||||
|
||||
// If we need to translate the image now, do so.
|
||||
PlanarImage img3;
|
||||
if ((offset_x!=0) || (offset_y!=0))
|
||||
{ // set up a translation to move the image to the right location
|
||||
ParameterBlock pb2 = new ParameterBlock();
|
||||
pb2.addSource(img2);
|
||||
pb2.add((float)offset_x);
|
||||
pb2.add((float)offset_y);
|
||||
pb2.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
|
||||
img3 = JAI.create("translate",pb2);
|
||||
|
||||
} // end if
|
||||
else // just take the image as it is
|
||||
img3 = img2;
|
||||
|
||||
// To set up the backdrop, first we need to create an image of the right size but with the same
|
||||
// sample model and color model as our transformed image.
|
||||
TiledImage back1 = new TiledImage(0,0,width,height,0,0,img3.getSampleModel(),img3.getColorModel());
|
||||
|
||||
// Now we need to make that image black. The easiest way to do that is multiply all pixel
|
||||
// values in the image by 0.
|
||||
ParameterBlock pb = new ParameterBlock();
|
||||
pb.addSource(back1);
|
||||
double[] parms = new double[1];
|
||||
parms[0] = 0.0;
|
||||
pb.add(parms);
|
||||
PlanarImage back2 = JAI.create("multiplyconst",pb);
|
||||
|
||||
// Now overlay the scaled/translated image on top of the background to get the final image.
|
||||
PlanarImage final_img = JAI.create("overlay",back2,img3);
|
||||
|
||||
// With our final image in hand, we can now encode it in the desired output format and turn
|
||||
// it into a streamful of data.
|
||||
ByteArrayOutputStream ostm = new ByteArrayOutputStream(4096);
|
||||
JAI.create("encode",final_img,ostm,out_format,null);
|
||||
byte[] odata = ostm.toByteArray();
|
||||
ostm.close();
|
||||
return new ImageLengthPair(odata.length,new ByteArrayInputStream(odata));
|
||||
|
||||
} // end try
|
||||
catch (Exception e)
|
||||
{ // catchall exception
|
||||
throw new ImageNormalizerException("unspecified error in scaling or translation",e);
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end normalizeImage
|
||||
|
||||
public static ImageLengthPair normalizeImage(InputStream raw_data, Dimension dim, String out_format)
|
||||
throws ImageNormalizerException
|
||||
{
|
||||
return normalizeImage(raw_data,dim.width,dim.height,out_format);
|
||||
|
||||
} // end normalizeImage
|
||||
|
||||
} // end class ImageNormalizer
|
||||
109
src/com/silverwrist/util/image/ImageNormalizerException.java
Normal file
109
src/com/silverwrist/util/image/ImageNormalizerException.java
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Communities System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util.image;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class ImageNormalizerException extends Exception
|
||||
{
|
||||
/*--------------------------------------------------------------------------------
|
||||
* Attributes
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
private Throwable inner = null; // internal "root cause" exception
|
||||
|
||||
/*--------------------------------------------------------------------------------
|
||||
* Constructors
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public ImageNormalizerException()
|
||||
{
|
||||
super();
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ImageNormalizerException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ImageNormalizerException(Throwable t)
|
||||
{
|
||||
super(t.getMessage());
|
||||
inner = t;
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ImageNormalizerException(String msg, Throwable t)
|
||||
{
|
||||
super(msg);
|
||||
inner = t;
|
||||
|
||||
} // end constructor
|
||||
|
||||
/*--------------------------------------------------------------------------------
|
||||
* Overrides from class Throwable
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public void printStackTrace()
|
||||
{
|
||||
this.printStackTrace(System.err);
|
||||
|
||||
} // end printStackTrace
|
||||
|
||||
public void printStackTrace(PrintStream s)
|
||||
{
|
||||
super.printStackTrace(s);
|
||||
if (inner!=null)
|
||||
{ // print the inner stack trace
|
||||
s.print("Root cause: ");
|
||||
inner.printStackTrace(s);
|
||||
|
||||
} // end if
|
||||
|
||||
} // end printStackTrace
|
||||
|
||||
public void printStackTrace(PrintWriter s)
|
||||
{
|
||||
super.printStackTrace(s);
|
||||
if (inner!=null)
|
||||
{ // print the inner stack trace
|
||||
s.print("Root cause: ");
|
||||
inner.printStackTrace(s);
|
||||
|
||||
} // end if
|
||||
|
||||
} // end printStackTrace
|
||||
|
||||
/*--------------------------------------------------------------------------------
|
||||
* External operations
|
||||
*--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public Throwable getException()
|
||||
{
|
||||
return inner;
|
||||
|
||||
} // end getException
|
||||
|
||||
} // end class ImageNormalizerException
|
||||
Reference in New Issue
Block a user