001: package org.openedit.store.images;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import com.openedit.OpenEditException;
011: import com.openedit.modules.image.ConvertInstructions;
012: import com.openedit.util.Exec;
013: import com.openedit.util.PathUtilities;
014:
015: public class GhostScriptConverter extends ImageMagickResizer {
016: private static final Log log = LogFactory
017: .getLog(GhostScriptConverter.class);
018: protected String fieldGhostScriptCommand;
019:
020: public boolean canConvert(String inPath) {
021: if (!System.getProperty("os.name").toLowerCase().contains(
022: "windows")) {
023: return false; //this works best on windows
024: }
025: String mime = getMimeTypeMap().getMimeType(
026: PathUtilities.extractPageType(inPath));
027: if (mime.equals("application/postscript")
028: || mime.equals("application/pdf")
029: || mime.equals("application/illustrator")
030: || mime.equals("image/x-eps")) { //application/Illustrator
031: return true;
032: }
033: return false;
034: }
035:
036: public boolean convert(File inIn, File inOut,
037: ConvertInstructions inStructions) throws OpenEditException {
038: //if this is an eps output from an ai input then use ghostscript
039: List com = createCommand(inOut, inStructions);
040: com.add("-sOutputFile=\"" + inOut.getAbsolutePath() + "\"");
041: com.add("\"" + inIn.getAbsolutePath() + "\"");
042: //com.add(" -");
043: //-q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0
044:
045: inOut.getParentFile().mkdirs();
046: long start = System.currentTimeMillis();
047: log.info("Running " + com + " on " + Thread.currentThread());
048: if (runExec(com)) {
049: log.info("Resize complete in:"
050: + (System.currentTimeMillis() - start) + " output "
051: + inOut.getAbsolutePath());
052: return true;
053: } else {
054: log.error("Could not convert " + inIn.getPath());
055: return false;
056: }
057: }
058:
059: protected List createCommand(File inOut,
060: ConvertInstructions inStructions) {
061: List com = new ArrayList();
062: com.add(getGhostScriptCommand());
063: com.add("-q");
064: com.add("-dBATCH");
065: com.add("-dSAFER");
066: com.add("-dNOPAUSE");
067:
068: String mime = getMimeTypeMap().getMimeType(
069: PathUtilities.extractPageType(inOut.getPath()));
070: if (mime.equals("application/pdf")) {
071: com.add("-sDEVICE=pdfwrite");
072: } else if (mime.equals("image/jpeg")) {
073: com.add("-sDEVICE=jpeg");
074: } else if (mime.equals("image/png")) {
075: com.add("-sDEVICE=jpeg");
076: } else if (mime.equals("image/tiff")) {
077: com.add("-sDEVICE=tiff32nc");
078: } else {
079: com.add("-sDEVICE=epswrite");
080: }
081:
082: //New version of Image Magik are 0 based. Windows is 1 based
083: int page = inStructions.getPageNumber();
084: page--;
085: page = Math.min(0, page);
086: com.add("-dFirstPage=" + page);
087: com.add("-dLastPage=" + page);
088: return com;
089: }
090:
091: public boolean resizeImage(File inFile, File inOutFile,
092: ConvertInstructions inStructions) throws Exception {
093: //if this is an eps output from an ai input then use ghostscript
094: List com = createCommand(inOutFile, inStructions);
095: com.add("-dDEVICEWIDTHPOINTS="
096: + inStructions.getMaxScaledSize().width);
097: com.add("-sOutputFile=\"" + inOutFile.getAbsolutePath() + "\"");
098: com.add("\"" + inFile.getAbsolutePath() + "\"");
099: //-q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0
100: inOutFile.getParentFile().mkdirs();
101: long start = System.currentTimeMillis();
102: //com.add(" -");
103: log.info("Running " + com);
104: if (runExec(com)) {
105: log.info("Resize complete in:"
106: + (System.currentTimeMillis() - start) + " output "
107: + inOutFile.getAbsolutePath());
108: return true;
109: } else {
110: log.error("Could not convert " + inOutFile.getPath());
111: return false;
112: }
113: }
114:
115: public void setGhostScriptCommand(String inName) {
116: fieldGhostScriptCommand = inName;
117: }
118:
119: public String getGhostScriptCommand() {
120: if (fieldGhostScriptCommand == null) {
121: String env = System.getProperty("GS_COMMAND");
122: if (env == null) {
123: env = System.getenv("GS_COMMAND");
124: }
125: if (env == null) {
126: if (System.getProperty("os.name").toUpperCase()
127: .contains("WINDOWS")) {
128: env = "gswin32c.exe";
129: } else {
130: env = "gs";
131: }
132: }
133: fieldGhostScriptCommand = env;
134: }
135:
136: return fieldGhostScriptCommand;
137:
138: }
139:
140: protected boolean runExec(List inCom) throws OpenEditException {
141: Exec exec = new Exec();
142: exec.setTrackOutput(true);
143: boolean works = exec.runExec(inCom);
144: // if ( !works )
145: // {
146: // log.info("Resize failed running again with output tracking");
147: // exec.setTrackOutput(true);
148: // works = exec.runExec(inCom);
149: // log.info(exec.getStandardOutput() + " error output:" + exec.getErrorOutput() );
150: // }
151: return works;
152: }
153:
154: }
|