/* * Copyright 2009 Jeffrey Palm */ import java.awt.*; import java.awt.image.*; import java.io.*; import java.util.*; import java.util.List; import javax.imageio.*; import javax.swing.*; /** * Extracts letters from flickr images from this account of the * form: * *
* *-*-*-*-*-*-* * | |a|b|c|d| | * *-*-*-*-*-*-* * |e|f|g|h|i|j| * *-*-*-*-*-*-* * |k|l|m|n|o|p| * *-*-*-*-*-*-* * |q|r|s|t|u|v| * *-*-*-*-*-*-* * | |w|x|y|z| | * *-*-*-*-*-*-* ** * and then writes an image NAME.jpg to the directory *
letters/NAME/a.jpg through
* letters/NAME/z.jpg
*/
public final class AlphaMaker {
private final File lettersDir = new File("letters");
public static void main(String[] args) {
if (args == null || args.length == 0) {
File testFile = null;
for (File f : new File(".").listFiles()) {
if (f.getName().endsWith(".jpg")) {
testFile = f;
break;
}
}
args = new String[]{testFile.getAbsolutePath()};
}
try {
new AlphaMaker().realMain(args);
} catch (Exception e) {e.printStackTrace();}
}
void realMain(String[] args) throws Exception {
if (!lettersDir.exists()) {
lettersDir.mkdirs();
}
for (String arg : args) {
extract(arg);
}
}
/**
* Extracts the letters of image (see the header comments) to the
* subdirectory of the letters directory excluding the
* image extension. e.g. an image named foo.jpg will
* have the letters written to letters/foo/a.jpg
* through letters/foo/z.jpg.
*
* @param image source image
*/
private void extract(String image) throws Exception {
note("image: " + image);
//
// create the output directory using the input image name
// also clean it out, if it exists
//
String name = new File(image).getName();
int ilastDot = name.indexOf('.');
String outputDirName = ilastDot == -1 ? name : name.substring(0,ilastDot);
File outputDir = new File(lettersDir,outputDirName);
if (!outputDir.exists()) {
outputDir.mkdirs();
} else {
for (File img : outputDir.listFiles()) {
img.delete();
}
}
//
// create an image and get the array of pixels
//
Image img = Toolkit.getDefaultToolkit().createImage(image);
JButton obs = new JButton(new ImageIcon(img));
int w = img.getWidth(obs);
int h = img.getHeight(obs);
note("size: " + w + "x" + h);
//
// get the pixels
//
int[] pixels = new int[w*h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {pg.grabPixels();} catch (Exception e) {e.printStackTrace();}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) return;
//
// This image is a 6x5 square (probably sized 240x200, but we
// won't stick to that) with the corners being blank, so we have:
//
// *-*-*-*-*-*-*
// | |a|b|c|d| |
// *-*-*-*-*-*-*
// |e|f|g|h|i|j|
// *-*-*-*-*-*-*
// |k|l|m|n|o|p|
// *-*-*-*-*-*-*
// |q|r|s|t|u|v|
// *-*-*-*-*-*-*
// | |w|x|y|z| |
// *-*-*-*-*-*-*
//
int letterWidth = w/6;
int letterHeight = h/5;
note("letter width : " + letterWidth);
note("letter height : " + letterHeight);
char c = 'a';
// top row
for (int i=1; i<=4; i++) {
writeLetter(pixels,w,outputDir,c++,i*letterWidth,0*letterHeight,(i+1)*letterWidth,1*letterHeight);
}
// middle 3 rows
for (int j=1; j<=3; j++) {
for (int i=0; i<=5; i++) {
writeLetter(pixels,w,outputDir,c++,i*letterWidth,j*letterHeight,(i+1)*letterWidth,(j+1)*letterHeight);
}
}
// bottom row
for (int i=1; i<=4; i++) {
writeLetter(pixels,w,outputDir,c++,i*letterWidth,4*letterHeight,(i+1)*letterWidth,5*letterHeight);
}
}
/**
* Writes out letter.jpg to
* outputDir with upper left coordinates
* (x0,y0) and lower right coordinates
* (x1,y1).
*
* @param pixel pixels of the image
* @param outputDir output directory
* @param width width of passed in image
* @param letter letter we're extracting
* @param x0 upper left x coordinate
* @param y0 upper left y coordinate
* @param x1 lower right x coordinate
* @param y1 lower right y coordinate
*/
private void writeLetter(int[] pixels, int width,
File outputDir, char letter,
int x0, int y0, int x1, int y1) {
BufferedImage img = new BufferedImage(x1-x0,y1-y0,BufferedImage.TYPE_INT_ARGB);
for (int i=y0, y=0; ii
*/
private String hex(int i) {
return Integer.toHexString(i);
}
/**
* Debugging.
*
* @param msg message to print for debugging
*/
protected final void note(Object msg) {
System.err.println("[" + getClass().getName() + "] " + String.valueOf(msg));
}
}