/*
* 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 .
*
* 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 ,
* 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.*;
/**
* A class that performs "normalization" on an image to fit within a bounding box of a specified pixel
* size. "Normalized" images are scaled down proportionally to fit the bounding box, then centered over
* a black backdrop (so the image will be "letterboxed" if it has a larger H:V aspect ratio than the bounding
* box, or "pillarboxed" if it has a smaller H:V aspect ratio than the bounding box).
*
* @author Eric J. Bowersox <erbo@users.sf.net>
* @version X
*/
public class ImageNormalizer
{
/*--------------------------------------------------------------------------------
* External static operations
*--------------------------------------------------------------------------------
*/
/**
* "Normalizes" an image to fit within a bounding box of a specified pixel size. "Normalized" images are
* scaled down proportionally to fit the bounding box, then centered over a black backdrop (so the image
* will be "letterboxed" if it has a larger H:V aspect ratio than the bounding box, or "pillarboxed" if it
* has a smaller H:V aspect ratio than the bounding box).
*
* @param raw_data Raw image data from the input, in any JAI-recognized format.
* @param width The width of the bounding box to use to normalize the image.
* @param height The height of the bounding box to use to normalize the image.
* @param out_format The desired format for output (such as "JPEG").
* @return The normalized image data and its length, as an ImageLengthPair
.
* @exception com.silverwrist.util.image.ImageNormalizerException An error occurred in the image data or
* the image transformation process.
* @see ImageLengthPair
* @see ImageNormalizerException
*/
public static ImageLengthPair normalizeImage(InputStream raw_data, int width, int height, String out_format)
throws ImageNormalizerException
{
PlanarImage img1; // the "initial" image we're loading
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()ImageLengthPair.
* @exception com.silverwrist.util.image.ImageNormalizerException An error occurred in the image data or
* the image transformation process.
* @see ImageLengthPair
* @see ImageNormalizerException
*/
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