/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.indexing.plugins;
import com.sun.media.jai.codec.SeekableStream;
import java.awt.RenderingHints;
import java.awt.image.*;
import java.awt.image.renderable.ParameterBlock;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.media.jai.*;
/** A plugin base for indexers that wish to make use of the thumbnail system.
* @author ug22cmg */
public abstract class ThumbnailCapableIndexer implements Indexer
{
private static long seed;
/** Gets the current seed to use.
* @return The current seed. */
private static long getNextSeed()
{
seed += 10;
return seed;
}
/** Gets a new random filename that does not exist in the current
* directory.
* @return The random filename.
* @throws java.io.IOException If an error occurs */
private String getNewName() throws IOException
{
File f = null;
Random rng = new Random(getNextSeed());
int newInt = rng.nextInt(500000);
f = new File("thumbnail" + newInt + ".jpg");
if (f.exists())
return getNewName();
else
return f.getName();
}
/** Gets the thumbnail using JAI and blurring.
* @param stream An image stream to create the thumbnail from.
* @param scaleX The ammount of scaling in the X direction.
* @param scaleY The ammount of scaling in the Y direction.
* @return The thumbnail image. */
private BufferedImage getThumbnail(InputStream stream, float scaleX,
float scaleY)
{
final float fract = 1.0F / 4.0F;
final float[] a =
{
fract, fract,
fract, fract,
fract, fract
};
SeekableStream s = SeekableStream.wrapInputStream(stream, false);
RenderedOp op = JAI.create("Stream", s);
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
BufferedImageOp bio = new ConvolveOp(new Kernel(2, 2, a),
ConvolveOp.EDGE_NO_OP, hints);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_DISABLE);
hints.put(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
ParameterBlock pb = new ParameterBlock();
pb.addSource(op);
pb.add(scaleX);
pb.add(scaleY);
pb.add(0.0F);
pb.add(0.0F);
pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2));
op = JAI.create("scale", pb, hints);
return bio.filter(op.getAsBufferedImage(), null);
}
/** Creates the thumbnail image.
* @param img The image to create a thumbnail for.
* @return The thumbnail image.
* @throws java.io.IOException If an error occurs. */
private BufferedImage createThumbnail(BufferedImage img)
throws IOException
{
ByteArrayOutputStream os = null;
ByteArrayInputStream is = null;
try
{
os = new ByteArrayOutputStream();
ImageIO.write(img, "png", os);
is = new ByteArrayInputStream(os.toByteArray());
float x = 260.0F / (float) img.getWidth();
float y = 345.0F / (float) img.getHeight();
return getThumbnail(is, x, y);
}
catch (Exception e)
{
return null;
}
finally
{
if (is != null) is.close();
if (os != null) os.close();
}
}
/** Gets and saves the thumbnail of the specified image.
* @param image full size image.
* @return The file name for the thumbnail file that needs to be
* saved to the abstract property system.
* @throws java.io.IOException If an error occurs. */
protected String saveThumbnail(BufferedImage image) throws
IOException
{
File f = new File(getNewName());
ImageIO.write(createThumbnail(image), "png", f);
return f.getAbsolutePath();
}
/** Gets the thumbnail instance from the specified thumbnail string.
* @param str Thumbnail string.
* @return A buffered image representation of the thumbnail.
* @throws java.io.IOException If an error occurs. */
public static BufferedImage getThumbnail(String str) throws IOException
{
return ImageIO.read(new File(str));
}
}