~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/raytrace/Harvester.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
7
    //  This library is free software; you can redistribute it and/or   //
 
8
    //  modify it under the terms of the GNU Lesser General Public      //
 
9
    //  License as published by the Free Software Foundation; either    //
 
10
    //  version 2.1 of the License, or (at your option) any later       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
13
    //  This library is distributed in the hope that it will be         //
 
14
    //  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
    //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
    //  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
19
    //  You should have received a copy of the GNU Lesser General       //
 
20
    //  Public License along with this library; if not, write to the    //
 
21
    //  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
    //  Boston, MA 02111-1307, USA.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
 
 
30
import org.jcsp.lang.*;
 
31
import org.jcsp.awt.*;
 
32
import java.awt.*;
 
33
import java.awt.image.*;
 
34
 
 
35
/**
 
36
 * @author Quickstone Technologies Limited
 
37
 */
 
38
class Harvester implements CSProcess {
 
39
 
 
40
        private final ChannelInput[] fromWorkers;
 
41
        private final ChannelInput fromFarmer;
 
42
        private final ChannelOutput toGraphics;
 
43
        private final ChannelInput fromGraphics;
 
44
 
 
45
        public Harvester (ChannelInput[] fromWorkers, ChannelInput fromFarmer,
 
46
                            ChannelOutput toGraphics, ChannelInput fromGraphics) {
 
47
                this.fromWorkers = fromWorkers;
 
48
                this.fromFarmer = fromFarmer;
 
49
                this.toGraphics = toGraphics;
 
50
                this.fromGraphics = fromGraphics;
 
51
        }
 
52
 
 
53
        public void run () {
 
54
                System.out.println ("Harvester: started");
 
55
                final Counter counter = new Counter ();
 
56
                final DisplayList displayList = new DisplayList ();
 
57
                int currentFrame = 0;
 
58
                int width = 0, height = 0;
 
59
                int[] pixels = null;
 
60
                MemoryImageSource mis = null;
 
61
                Image image = null;
 
62
                int numWorkers = 0, nextNumWorkers = 0, nextNumWorkersFrame = 0;
 
63
            toGraphics.write (new GraphicsProtocol.SetPaintable (displayList));
 
64
            fromGraphics.read ();
 
65
                while (true) {
 
66
                        // Synchronize with farmer
 
67
                        //System.out.println ("Harvester: synchronize with farmer");
 
68
                        ImagePacket ip = (ImagePacket)fromFarmer.read ();
 
69
                        if (ip != null) {
 
70
                                System.out.println ("Harvester: updating image configuration");
 
71
                                width = ip.width;
 
72
                                height = ip.height;
 
73
                                pixels = new int[width * height];
 
74
                                mis = new MemoryImageSource (width, height, pixels, 0, width);
 
75
                                mis.setAnimated (true);
 
76
                                toGraphics.write (new GraphicsProtocol.MakeMISImage (mis));
 
77
                                image = (Image)fromGraphics.read ();
 
78
                                displayList.set (new GraphicsCommand.DrawImage (image, 0, 0, width, height));
 
79
                                if (ip.numWorkers != numWorkers) {
 
80
                                        nextNumWorkers = ip.numWorkers;
 
81
                                        nextNumWorkersFrame = ip.frame;
 
82
                                }
 
83
                        }
 
84
                        // Read in the work packets for the current frame
 
85
                        if (nextNumWorkersFrame == currentFrame) {
 
86
                                numWorkers = nextNumWorkers;
 
87
                        }
 
88
                        int i = 0;
 
89
                        try {
 
90
                                //System.out.println ("Harvester: read frame " + currentFrame + " results");
 
91
                        for (; i < numWorkers; i++) {
 
92
                                ResultPacket rp = (ResultPacket)fromWorkers[currentFrame % fromWorkers.length].read ();
 
93
                                rp.expand ();
 
94
                                int j = rp.offset * width, x = 0;
 
95
                                for (int kR = 0, kG = rp.color.length / 3, kB = (kG << 1); kB < rp.color.length; ) {
 
96
                                        int a = (rp.color[kR++] << 16) & 0xFF0000;
 
97
                                        a |= (rp.color[kG++] << 8) & 0xFF00;
 
98
                                        pixels[j++] = 0xFF000000 | a | (rp.color[kB++] & 0xFF);
 
99
                                        if (++x >= width) {
 
100
                                                j += (rp.step - 1) * width;
 
101
                                                x = 0;
 
102
                                        }
 
103
                                }
 
104
                                rp.discard ();
 
105
                        }
 
106
                        mis.newPixels ();
 
107
                        } catch (ArrayIndexOutOfBoundsException e) {
 
108
                                System.out.println ("Harvester: array index problems");
 
109
                                // Image has probably changed size. I don't care for elegance so just skip any outstanding packets
 
110
                                i++;
 
111
                                for (; i < numWorkers; i++) {
 
112
                                fromWorkers[currentFrame % fromWorkers.length].read ();
 
113
                                }
 
114
                        }
 
115
                        currentFrame++;
 
116
                        counter.click ();
 
117
                }
 
118
        }
 
119
 
 
120
}