001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.util;
007
008import java.io.*;
009import java.util.*;
010import fc.io.*;
011import fc.util.*;
012
013import java.awt.*;
014import java.awt.geom.*;
015import java.awt.image.*;
016import javax.imageio.*;
017
018/* Misc. image related utils */
019public final class ImageUtil
020{
021
022/**
023Resizes the specified image file to the new width and height and writes it to
024the targetFile (creating the targetFile if it does not already exist).
025<p>
026The format string is the output format of the file, like "jpg", "gif" etc. This
027can be different than the format of the source file. The understood formats
028are those understood {@link javax.io.ImageWriter} (the usual suspects like gif,
029jpg, png, etc, all seem to work).
030*/
031public static void resize(File sourceFile, File targetFile, String format, int newWidth, int newHeight)
032throws IOException  
033  {
034  BufferedImage resized = resize(sourceFile, newWidth, newHeight);
035  ImageIO.write(resized, format, targetFile);
036  }
037
038
039/**
040Resizes the specified image file to the new width and height and writes it to
041the targetFile (creating the targetFile if it does not already exist).
042<p>
043The output format is the same as the input format and is intuited from the file
044name extension of the specified source file (therefore, source files specified
045to this method, should be like <code>foo.gif</code>, <code>foo.jpg</code>, etc).
046*/
047public static void resize(File sourceFile, File targetFile, int newWidth, int newHeight)
048throws IOException  
049  {
050  String name = sourceFile.getName();  
051  int pos = name.lastIndexOf(".");
052    if (pos == -1) {
053      throw new IOException("The specified source file: [" + sourceFile.getAbsolutePath() + "] has no extension, and this method needs an extension (like .gif, .jpg etc)");
054      }
055  String extension = name.substring(pos+1, name.length()); 
056  //System.out.println(extension);
057  resize(sourceFile, targetFile, extension, newWidth, newHeight);
058  }
059
060/**
061Resizes the specified image file to the new width and height and returns the
062new image.
063*/
064public static BufferedImage resize(File sourceFile, int newWidth, int newHeight)
065throws IOException  
066  {
067  BufferedImage sourceImage = ImageIO.read(sourceFile);
068  //WritableRaster raster = image.getRaster(); //for pixel manipulation, not needed
069
070    int srcWidth = sourceImage.getWidth();
071    int srcHeight = sourceImage.getHeight();
072    
073    BufferedImage targetImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
074  Graphics2D g2 = targetImage.createGraphics();
075  AffineTransform transform = AffineTransform.getScaleInstance((double)newWidth/srcWidth, (double)newHeight/srcHeight);
076
077  g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
078            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
079            
080  g2.drawImage(sourceImage, transform, null);
081  g2.dispose(); 
082
083  return targetImage;
084  }
085
086public static void main (String args[]) throws IOException  
087  {
088  Args myargs = new Args(args);
089  myargs.setUsage(myargs.getMainClassName() + " -source path-to-source-image -target path-to-write-target-img -width new-width -height new-height");
090  
091  File source = new File(myargs.getRequired("source"));
092  File target = new File(myargs.getRequired("target"));
093  int width = myargs.getRequiredInt("width");
094  int height = myargs.getRequiredInt("height");
095  
096  if (! source.exists()) {
097    System.out.println("Cannot read source file: " + source.getAbsolutePath());
098    System.exit(1);
099    }
100
101  resize(source, target, width, height);  
102  }
103}