FreeWRL/FreeX3D  3.0.0
EventOutSFImage.java
1 package vrml.external.field;
2 import java.math.BigInteger;
3 import java.util.*;
6 
7 public class EventOutSFImage extends EventOut {
8  public EventOutSFImage() {EventType = FieldTypes.SFIMAGE;}
9 
10  int width;
11  int height;
12  int numComponents;
13  StringTokenizer tokens;
14 
15  private void takeapart() {
16  String rep;
17 
18  if (RLreturn == null) {
19  rep = Browser.SendEventOut (nodeptr, offset, datasize, datatype, command);
20  if (rep.length() > 2) {
21  // remove quotes at the beginning and end
22  rep = rep.substring (1,rep.length()-1);
23  }
24 
25  // System.out.println ("EventOutSFImage - command " + command + " rep " + rep);
26  tokens = new StringTokenizer (rep);
27  } else {
28  // System.out.println ("EventOutSFImage - command " + command + " RLreturn " + RLreturn);
29  tokens = new StringTokenizer (RLreturn);
30  }
31 
32  width = Integer.valueOf(tokens.nextToken()).intValue();
33  height = Integer.valueOf(tokens.nextToken()).intValue();
34  numComponents = Integer.valueOf(tokens.nextToken()).intValue();
35  }
36 
37  public int getWidth() {
38  takeapart();
39  return width;
40  }
41 
42  public int getHeight () {
43  takeapart();
44  return height;
45  }
46 
47  public int getNumComponents() {
48  takeapart();
49  return numComponents;
50  }
51 
52  public byte[] getPixels () {
53  takeapart();
54  byte retval[];
55  int count;
56  BigInteger nextVal;
57  BigInteger bigTmp;
58 
59  String nextStr;
60  int byteptr;
61  int tmp;
62 
63  retval = new byte [width*height*numComponents];
64  byteptr = 0;
65 
66  // loop through the string return vals, and make up byte array
67  for (count = 0; count < width*height; count++) {
68  nextStr = tokens.nextToken();
69 
70  // is this a hex string?
71  try {
72  nextVal = new BigInteger(nextStr);
73  } catch (Exception e) {
74  nextStr = nextStr.substring (2,nextStr.length());
75  nextVal = new BigInteger(nextStr,16);
76  }
77 
78  if (numComponents == 1) {
79  tmp = nextVal.intValue();
80  tmp = tmp & 0xff;
81  retval[byteptr] =(byte) (tmp); byteptr++;
82  }
83  if (numComponents == 2) {
84  bigTmp = nextVal.shiftRight(8);
85  tmp = bigTmp.intValue();
86  tmp = tmp & 0xff;
87  retval[byteptr] =(byte) (tmp); byteptr++;
88 
89  tmp = nextVal.intValue();
90  tmp = tmp & 0xff;
91  retval[byteptr] =(byte) (tmp); byteptr++;
92  }
93  if (numComponents == 3) {
94  bigTmp = nextVal.shiftRight(16);
95  tmp = bigTmp.intValue();
96  tmp = tmp & 0xff;
97  retval[byteptr] =(byte) (tmp); byteptr++;
98 
99  bigTmp = nextVal.shiftRight(8);
100  tmp = bigTmp.intValue();
101  tmp = tmp & 0xff;
102  retval[byteptr] =(byte) (tmp); byteptr++;
103 
104  tmp = nextVal.intValue();
105  tmp = tmp & 0xff;
106  retval[byteptr] =(byte) (tmp); byteptr++;
107  }
108  if (numComponents == 4) {
109  bigTmp = nextVal.shiftRight(24);
110  tmp = bigTmp.intValue();
111  tmp = tmp & 0xff;
112  retval[byteptr] =(byte) (tmp); byteptr++;
113 
114  bigTmp = nextVal.shiftRight(16);
115  tmp = bigTmp.intValue();
116  tmp = tmp & 0xff;
117  retval[byteptr] =(byte) (tmp); byteptr++;
118 
119  bigTmp = nextVal.shiftRight(8);
120  tmp = bigTmp.intValue();
121  tmp = tmp & 0xff;
122  retval[byteptr] =(byte) (tmp); byteptr++;
123 
124  tmp = nextVal.intValue();
125  tmp = tmp & 0xff;
126  retval[byteptr] =(byte) (tmp); byteptr++;
127  }
128  }
129 
130  return retval;
131  }
132 }