1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
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 //
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 //
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. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
30
import org.jcsp.lang.*;
31
import org.jcsp.awt.*;
33
import java.awt.image.*;
36
* @author Quickstone Technologies Limited
38
class Harvester implements CSProcess {
40
private final ChannelInput[] fromWorkers;
41
private final ChannelInput fromFarmer;
42
private final ChannelOutput toGraphics;
43
private final ChannelInput fromGraphics;
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;
54
System.out.println ("Harvester: started");
55
final Counter counter = new Counter ();
56
final DisplayList displayList = new DisplayList ();
58
int width = 0, height = 0;
60
MemoryImageSource mis = null;
62
int numWorkers = 0, nextNumWorkers = 0, nextNumWorkersFrame = 0;
63
toGraphics.write (new GraphicsProtocol.SetPaintable (displayList));
66
// Synchronize with farmer
67
//System.out.println ("Harvester: synchronize with farmer");
68
ImagePacket ip = (ImagePacket)fromFarmer.read ();
70
System.out.println ("Harvester: updating image configuration");
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;
84
// Read in the work packets for the current frame
85
if (nextNumWorkersFrame == currentFrame) {
86
numWorkers = nextNumWorkers;
90
//System.out.println ("Harvester: read frame " + currentFrame + " results");
91
for (; i < numWorkers; i++) {
92
ResultPacket rp = (ResultPacket)fromWorkers[currentFrame % fromWorkers.length].read ();
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);
100
j += (rp.step - 1) * width;
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
111
for (; i < numWorkers; i++) {
112
fromWorkers[currentFrame % fromWorkers.length].read ();