01: /*
02: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
03:
04: This program is free software; you can redistribute it and/or
05: modify it under the terms of the GNU General Public License
06: as published by the Free Software Foundation; either version 2
07: of the License, or (at your option) any later version.
08:
09: This program is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with this program; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: */
18:
19: package org.homedns.dade.jcgrid.worker.impl.povray;
20:
21: import java.awt.*;
22: import java.awt.image.*;
23: import javax.swing.*;
24: import javax.imageio.*;
25:
26: public class POVRenderingFrame {
27: private BufferedImage renderingFrameBuffer;
28: private Graphics2D renderingFrameBufferG2D;
29: private int fragmentTotCount;
30:
31: private int fragmentCount;
32:
33: public POVRenderingFrame(int width, int height, int count) {
34: renderingFrameBuffer = new BufferedImage(width, height,
35: BufferedImage.TYPE_3BYTE_BGR);
36: renderingFrameBufferG2D = renderingFrameBuffer.createGraphics();
37:
38: fragmentTotCount = count;
39: fragmentCount = 0;
40:
41: for (int y = 0; y < renderingFrameBuffer.getHeight(); y += 10) {
42: for (int x = 0; x < renderingFrameBuffer.getWidth(); x += 10) {
43: if (((x / 10 + y / 10) % 2) == 0)
44: renderingFrameBufferG2D.setColor(Color.BLACK);
45: else
46: renderingFrameBufferG2D.setColor(Color.WHITE);
47: renderingFrameBufferG2D.fillRect(x, y, 10, 10);
48: }
49: }
50: }
51:
52: public BufferedImage getBufferedImage() {
53: return renderingFrameBuffer;
54: }
55:
56: public Graphics2D getGraphics2D() {
57: return renderingFrameBufferG2D;
58: }
59:
60: public void addFragmentResult(POVWorkRequest req, POVWorkResult res) {
61: // Get the rendered fragment
62: // I'm using this trick because POVRay file output
63: // is not well formatted (height is wrong). POVRay requires
64: // "wrong" height in order to support "+C" option.
65:
66: byte[] ppmData = res.getData();
67: int ppmIdx = 0;
68: for (int j = 0; j < 3; ppmIdx++) {
69: if (ppmData[ppmIdx] == '\n')
70: j++;
71: }
72:
73: for (int y = req.getStartRow(); y <= req.getEndRow(); y++) {
74: for (int x = 0; x < req.getWidth(); x++) {
75: int r = ppmData[ppmIdx++] & 0xff;
76: int g = ppmData[ppmIdx++] & 0xff;
77: int b = ppmData[ppmIdx++] & 0xff;
78:
79: if ((x >= req.getStartCol()) && (x <= req.getEndCol())) {
80: renderingFrameBufferG2D
81: .setColor(new Color(r, g, b));
82: renderingFrameBufferG2D.fillRect(x, y, 1, 1);
83: }
84: }
85: }
86:
87: fragmentCount++;
88: }
89:
90: public boolean isComplete() {
91: return fragmentCount >= fragmentTotCount;
92: }
93: }
|