001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.cmd.povray;
020:
021: import java.io.*;
022: import java.text.*;
023: import java.awt.*;
024: import java.awt.image.*;
025: import javax.swing.*;
026: import javax.imageio.*;
027:
028: import org.apache.log4j.*;
029: import org.apache.commons.cli.*;
030:
031: import org.homedns.dade.jcgrid.*;
032: import org.homedns.dade.jcgrid.cmd.*;
033: import org.homedns.dade.jcgrid.client.*;
034: import org.homedns.dade.jcgrid.message.*;
035: import org.homedns.dade.jcgrid.worker.impl.povray.*;
036:
037: public class JCGridClient {
038: private final static String className = JCGridClient.class
039: .getName();
040: private static Logger log = Logger.getLogger(className);
041: private static Logger logDetail = Logger.getLogger("DETAIL."
042: + className);
043:
044: //--------------------------------------------------------------------------
045:
046: public String iniFile = "jcgrid.ini";
047: public String outputFile = "jcgrid-output.png";
048: public int width = 320;
049: public int height = 240;
050: public int step = 64;
051: public boolean hasAnimation = false;
052: public int startFrame = -1;
053: public int endFrame = -1;
054: public String outputFormat = "png";
055: public boolean hasRenderingWindow = true;
056:
057: //--------------------------------------------------------------------------
058:
059: public class RenderingFeedback implements
060: POVRenderingClientFeedback {
061: private RenderingWindow rWin;
062: private BufferedImage renderingFrameBuffer;
063: private Graphics2D renderingFrameBufferG2D;
064:
065: private DecimalFormat numberFormat;
066:
067: public RenderingFeedback() {
068: rWin = null;
069: renderingFrameBuffer = null;
070: renderingFrameBufferG2D = null;
071:
072: numberFormat = new DecimalFormat("00000");
073: }
074:
075: public void error(String msg, Exception ex) {
076: log.warn(msg, ex);
077: }
078:
079: public void sendingFrgamentRequest(POVWorkRequest req) {
080: log.warn("Sending fragment[" + req.getFrame() + ","
081: + req.getWidth() + "x" + req.getHeight() + ","
082: + "(" + req.getStartCol() + "," + req.getEndCol()
083: + ")," + "(" + req.getStartRow() + ","
084: + req.getEndRow() + ")]");
085: }
086:
087: public void receivedFragmentResult(POVWorkRequest req,
088: POVWorkResult res, int idx, POVRenderingFrame frame) {
089: log.warn("Receiving fragment[" + req.getFrame() + ","
090: + req.getWidth() + "x" + req.getHeight() + ","
091: + "(" + req.getStartCol() + "," + req.getEndCol()
092: + ")," + "(" + req.getStartRow() + ","
093: + req.getEndRow() + ")]");
094:
095: if (hasRenderingWindow) {
096: renderingFrameBufferG2D.drawImage(frame
097: .getBufferedImage(), null, 0, 0);
098: rWin.update();
099: }
100: }
101:
102: public void renderingBegin() {
103: // Open rendering window if required
104:
105: if (hasRenderingWindow) {
106: // Creating rendering framebuffer
107:
108: renderingFrameBuffer = new BufferedImage(width, height,
109: BufferedImage.TYPE_3BYTE_BGR);
110: renderingFrameBufferG2D = renderingFrameBuffer
111: .createGraphics();
112:
113: rWin = new RenderingWindow(renderingFrameBuffer);
114: rWin.setVisible(true);
115: rWin.update();
116: } else
117: rWin = null;
118: }
119:
120: public void renderingEnd() {
121: if (hasRenderingWindow)
122: rWin.dispose();
123: }
124:
125: public void setProgressMaximum(int max) {
126: }
127:
128: public void setProgressMinimum(int min) {
129: }
130:
131: public void setProgressValue(int val) {
132: }
133:
134: public void setRenderingTime(POVWorkRequest req, long dt) {
135: double totTime = dt / (60.0 * 1000.0);
136: if (dt > 0.0001) {
137: long pixSec;
138: if (hasAnimation)
139: pixSec = (1000 * (endFrame - startFrame + 1)
140: * req.getWidth() * req.getHeight())
141: / dt;
142: else
143: pixSec = (1000 * req.getWidth() * req.getHeight())
144: / dt;
145:
146: log.warn("Total rendering time: " + totTime + "min ("
147: + pixSec + "pixel/sec)");
148: } else
149: log.warn("Total rendering time: N/A");
150: }
151:
152: public void completeFrame(int idx, POVRenderingFrame frame) {
153: try {
154: String fileName;
155: if (hasAnimation)
156: fileName = numberFormat.format(idx) + "-"
157: + outputFile;
158: else
159: fileName = outputFile;
160:
161: log.warn("Complete frame: " + fileName + " [" + idx
162: + "]");
163:
164: File f = new File(fileName);
165: ImageIO
166: .write(frame.getBufferedImage(), outputFormat,
167: f);
168: } catch (Exception ex) {
169: log.warn("Error while saving file: " + outputFile, ex);
170: System.exit(0);
171: }
172: }
173: }
174:
175: public void doRendering(GridNodeClientConfig config,
176: POVWorkRequest povwr) throws Exception {
177: RenderingFeedback rfback = new RenderingFeedback();
178: POVRenderingClient rc;
179: if (hasAnimation)
180: rc = new POVRenderingClient(config, rfback, povwr, step,
181: startFrame, endFrame);
182: else
183: rc = new POVRenderingClient(config, rfback, povwr, step);
184:
185: rc.start();
186: rc.join();
187: }
188:
189: //--------------------------------------------------------------------------
190:
191: public static void main(String[] args) {
192: try {
193: // Setup log4j
194:
195: MainCmd.setUpLog4J("client", true);
196:
197: // Setup GridServer
198:
199: log.warn("-----------------------------------------------");
200: log.warn("-- JCGridClient POVRay v" + Version.RELEASE);
201: log.warn("-----------------------------------------------");
202:
203: JCGridClient client = new JCGridClient();
204: GridNodeClientConfig config = new GridNodeClientConfig();
205:
206: // Other command line options
207:
208: Options options = new Options();
209: options.addOption("f", true, "set the povray ini file");
210: options.addOption("o", true, "set the povray output file");
211: options
212: .addOption("t", true,
213: "set the povray output file format [i.e. png, ppm, jpg, etc.]");
214: options.addOption("x", true,
215: "set the povray output file width");
216: options.addOption("y", true,
217: "set the povray output file height");
218: options.addOption("g", true,
219: "set the povray image fragment size");
220: options.addOption("r", false, "disable rendering window");
221: options.addOption("b", true,
222: "set the povray animation start frame");
223: options.addOption("h", true,
224: "set the povray animation end frame");
225:
226: try {
227: CommandLine cmd = MainCmd.parseCommonOptions(options,
228: config, args);
229:
230: // POVRay options
231:
232: if (cmd.hasOption("f"))
233: client.iniFile = cmd.getOptionValue("f");
234: if (cmd.hasOption("o"))
235: client.outputFile = cmd.getOptionValue("o");
236: if (cmd.hasOption("t"))
237: client.outputFormat = cmd.getOptionValue("t");
238: if (cmd.hasOption("x"))
239: client.width = Integer.parseInt(cmd
240: .getOptionValue("x"));
241: if (cmd.hasOption("y"))
242: client.height = Integer.parseInt(cmd
243: .getOptionValue("y"));
244: if (cmd.hasOption("g"))
245: client.step = Integer.parseInt(cmd
246: .getOptionValue("g"));
247: if (cmd.hasOption("r"))
248: client.hasRenderingWindow = false;
249: if (cmd.hasOption("b"))
250: client.startFrame = Integer.parseInt(cmd
251: .getOptionValue("b"));
252: if (cmd.hasOption("h"))
253: client.endFrame = Integer.parseInt(cmd
254: .getOptionValue("h"));
255:
256: if (cmd.getArgs().length > 0)
257: throw new Exception("Unknown command line option");
258: if (((client.startFrame == -1) && (client.endFrame != -1))
259: || ((client.endFrame == -1) && (client.startFrame != -1)))
260: throw new Exception(
261: "Both start and end animation frame must be set");
262:
263: if ((client.startFrame != -1)
264: && (client.endFrame != -1)) {
265: client.hasAnimation = true;
266:
267: if (client.endFrame < client.startFrame)
268: throw new Exception(
269: "End animation frame must be greater than end animation frame");
270: }
271: } catch (Exception ex) {
272: log.warn("Error while parsing command line", ex);
273:
274: HelpFormatter formatter = new HelpFormatter();
275: formatter.printHelp("JCGridClient", options);
276:
277: System.exit(0);
278: }
279:
280: // Creating work requests
281:
282: POVWorkRequest povwr = new POVWorkRequest(config
283: .getSessionName(), 0, client.iniFile, client.width,
284: client.height, 0, client.width - 1, 0,
285: client.height - 1);
286: povwr.setAnimationEnabled(client.hasAnimation);
287:
288: client.doRendering(config, povwr);
289:
290: // Rendering done
291:
292: log.warn("Done.");
293: } catch (Exception ex) {
294: log.warn("Error", ex);
295: System.exit(0);
296: }
297: }
298: }
|