package net.sf.jpluck.apps.cmdline;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.sf.jpluck.apps.ExitCodes;
import net.sf.jpluck.apps.OptionsUtil;
import net.sf.jpluck.palm.bitmap.CompositeBitmap;
import net.sf.jpluck.palm.bitmap.ImageConverter;
import net.sf.jpluck.plucker.CompositeImageRecord;
import net.sf.jpluck.plucker.Document;
import net.sf.jpluck.plucker.ImageRecord;
import net.sf.jpluck.plucker.TextRecord;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
public class Gallery {
private static final String VERSION = "1.0.2";
private static final String RELEASE_DATE = "2004-03-20";
public static void main(String[] args) throws Exception {
Options options = OptionsUtil.createGeneric();
options.addOption("bpp", true, "Bit depth. (1|2|4|8|16) Default 16 bpp.");
options.addOption("category", true, "Document category. Max. 15 characters.");
options.addOption("nobookmarks", false, "Do not generate bookmarks for images.");
options.addOption("maxheight", true, "Maximum height in pixels");
options.addOption("maxwidth", true, "Maximum width in pixels");
options.addOption("name", true, "Document name. Max. 31 characters.");
GnuParser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
if (cl.hasOption("help")) {
String usage = "java -jar gallery.jar [options] <PDB file> <filename pattern 1> [filename pattern N]";
String description = "Converts GIF|JPEG|PNG images to a multi-image Plucker document.";
OptionsUtil.printHelp(usage, description, options);
System.exit(ExitCodes.OK);
}
if (cl.hasOption("version")) {
System.out.println("Gallery " + VERSION + " (" + RELEASE_DATE + ")");
System.exit(ExitCodes.OK);
}
String[] arguments = cl.getArgs();
if (arguments.length < 2) {
System.err.println("ERROR: invalid number of arguments " + arguments.length);
System.exit(1);
}
String filename = arguments[0];
File pdbFile = new File(filename);
String name;
if (cl.hasOption("name")) {
name = cl.getOptionValue("name");
} else {
name = pdbFile.getName();
}
name = name.trim();
if (name.length() > 31) {
name = name.substring(0, 31).trim();
}
int bpp = 16;
if (cl.hasOption("bpp")) {
bpp = Integer.parseInt(cl.getOptionValue("bpp"));
}
int maxWidth = 0;
if (cl.hasOption("maxwidth")) {
maxWidth = Integer.parseInt(cl.getOptionValue("maxwidth"));
}
int maxHeight = 0;
if (cl.hasOption("maxheight")) {
maxHeight = Integer.parseInt(cl.getOptionValue("maxheight"));
}
boolean noBookmarks = cl.hasOption("nobookmarks");
Document document = new Document(name, Document.ZLIB_COMPRESSION);
String category = null;
if (cl.hasOption("category")) {
category = cl.getOptionValue("category");
if (category.length() > 15) {
category = category.substring(0, 15).trim();
}
document.setCategories(new String[] { category });
}
TextRecord home=new TextRecord("home");
home.addParagraph().addFontH1().addText(name).addFontRegular();
for (int i = 1; i < arguments.length; i++) {
System.out.println(arguments[i] + " (" + i + "/" + (arguments.length-1) + ")");
File file = new File(arguments[i]);
if (!file.isFile()) {
throw new IOException("Not a file: " + file.getAbsolutePath());
}
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
image = ImageConverter.rescale(image, ((maxWidth > 0) ? maxWidth : width),
((maxHeight > 0) ? maxHeight : height));
CompositeBitmap compositeBitmap = CompositeBitmap.create(image, bpp);
String uri = file.toURI().toString();
home.addParagraph(2).addText(i + " ").addLinkStart(uri).addText(file.getName()).addLinkEnd();
String[] uris = new String[compositeBitmap.getRows()];
for (int j = 0, n = compositeBitmap.getRows(); j < n; j++) {
uris[j] = uri + "-" + j;
document.addRecord(new ImageRecord(uris[j], compositeBitmap.getBitmapAt(j, 0)));
}
document.addRecord(new CompositeImageRecord(uri, uris, 1));
if (!noBookmarks) {
document.addBookmark(file.getName(), uri);
}
}
document.addRecord(home);
document.setHome(home);
System.out.println("Writing " + pdbFile.getAbsolutePath());
document.write(pdbFile);
System.out.println("Done!");
}
}