-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrameReader.php
More file actions
340 lines (285 loc) · 8.29 KB
/
Copy pathFrameReader.php
File metadata and controls
340 lines (285 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* ILDA (International Laser Display Association) Frame Handling in PHP.
*
* @package php-ilda
* @author Damien Walsh <me@damow.net>
*/
namespace ILDA;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Frame Reader.
* Reads frame files (.ild) and provides information about them.
*
* @since 1.0
*/
class FrameReader
{
/**
* File handle
* @var resource
*/
private $handle = null;
/**
* A logger to report the read process to.
* @private LoggerInterface
*/
private $logger = null;
/**
* The current global byte offset.
* @var integer
*/
public $byteIndex = 0;
/**
* The first "ILDA" byte of the current frame.
* @var integer
*/
public $currentFrameIndex = 0;
/**
* Construct: FrameReader
* @param string $filename The file to parse.
* @param LoggerInterface $logger A logger
*/
public function __construct($filename, LoggerInterface $logger = null)
{
// Create a null logger if no logger is provided
if ($logger == null) {
$logger = new NullLogger;
}
$this->logger = $logger;
$this->handle = fopen($filename, 'r');
if (!$this->handle) {
$this->logger->error('unable to open ILDA framefile ' . $filename);
return false;
}
// Initial verification that this is a framefile
// First bytes in file should be ILDA magic sequence
if (!$this->readValidILDA()) {
$this->logger->warning('no ILDA magic sequence at offset 0');
return false;
}
// Rewind to the start of the file after initial verification
$this->byteIndex = 0;
return $this;
}
/**
* Read the next frame from the file.
*
* @return Frame2D|Frame3D
*/
public function nextFrame()
{
// Check that "ILDA" is under the cursor
if (!$this->readValidILDA()) {
$this->logger->warning('no ILDA magic sequence found');
}
$frameType = $this->readFrameType();
$this->logger->info('discovered ' . $frameType . ' type frame at offset ' . $this->byteIndex);
$frame = null;
switch($frameType)
{
case 0x00:
$frame = new Frame3D;
break;
case 0x01:
$frame = new Frame2D;
break;
case 0x02:
case 0x03:
break;
default:
// Not a valid frame to read here
return null;
}
// Build the initial frame parameters
$frame->ildaFrameType = $frameType;
$frame->ildaFrameName = $this->readFrameName();
$frame->ildaAuthorName = $this->readAuthorName();
$frame->ildaPointCount = $this->readPointCount();
$frame->ildaFrameNumber = $this->readFrameNumber();
$frame->ildaTotalFrames = $this->readTotalFrames();
$frame->ildaScannerHeadID = $this->readScannerHeadID();
// Read points
$this->logger->info('expecting ' . $frame->ildaPointCount . ' points');
for ($point = 0; $point < $frame->ildaPointCount; $point ++) {
$frame->points[] = $this->readPoint($frame);
}
$this->logger->info('read ' . count($frame->points) . ' points');
// Skip the trailing NUL byte after the frame
$this->byteIndex += 1;
return $frame;
}
/**
* Read a point from the current file cursor.
*
* @param AbstractFrame $frame
* @return Point2D|Point3D
*/
private function readPoint(AbstractFrame $frame)
{
// Create new point based on frame type
$point = $frame->ildaFrameType === 0x00 ? new Point3D : new Point2D;
// Store X-axis
$point->x = $this->twosComplimentToInt(fread($this->handle, 2));
$this->byteIndex += 2;
// Y-axis needs postprocessing for bottom-up coordinates
$point->y = -$this->twosComplimentToInt(fread($this->handle, 2));
$this->byteIndex += 2;
if ($frame->ildaFrameType === 0x00) {
$point->z = $this->twosComplimentToInt(fread($this->handle, 2));
$this->byteIndex += 2;
}
// Read Status Code
// TODO: add colour parsing
fread($this->handle, 1);
$codeByteB = fread($this->handle, 1);
$this->byteIndex += 2;
// Blanking bit
$point->isBlanked = 0b01000000 & ord($codeByteB);
return $point;
}
/**
* Check if the cursor is currently at the ILDA magic byte string.
* Advances byteIndex automatically.
*
* @return bool
*/
private function readValidILDA()
{
fseek($this->handle, $this->byteIndex);
$magicILDA = fread($this->handle, 4);
$this->byteIndex += 4;
return $magicILDA === 'ILDA';
}
/**
* Get the frame type
* Advances byteIndex automatically.
*
* @return int
*/
private function readFrameType()
{
fseek($this->handle, $this->byteIndex);
$frameTypeBytes = fread($this->handle, 4);
$this->byteIndex += 4;
switch($frameTypeBytes)
{
case "\x00\x00\x00\x00":
return 0;
default:
return -1;
}
}
/**
* Get the frame name label
* Advances byteIndex automatically.
*
* @return string
*/
private function readFrameName()
{
fseek($this->handle, $this->byteIndex);
$ildaFrameName = fread($this->handle, 8);
$this->byteIndex += 8;
return $ildaFrameName;
}
/**
* Get the author name
* Advances byteIndex automatically.
*
* @return string
*/
private function readAuthorName()
{
fseek($this->handle, $this->byteIndex);
$ildaAuthorName = fread($this->handle, 8);
$this->byteIndex += 8;
return $ildaAuthorName;
}
/**
* Get the point count for the frame
* Advances byteIndex automatically.
*
* @return int
*/
private function readPointCount()
{
fseek($this->handle, $this->byteIndex);
$pointCountBytes = fread($this->handle, 2);
$ildaPointCount = $this->bigEndianToInt($pointCountBytes);
$this->byteIndex += 2;
return $ildaPointCount;
}
/**
* Get the frame index
* Advances byteIndex automatically.
*
* @return int
*/
private function readFrameNumber()
{
fseek($this->handle, $this->byteIndex);
$frameNumberBytes = fread($this->handle, 2);
$ildaFrameNumber = $this->bigEndianToInt($frameNumberBytes);
$this->byteIndex += 2;
return $ildaFrameNumber;
}
/**
* Get the number of frames in the sequence
* Advances byteIndex automatically.
*
* @return int
*/
private function readTotalFrames()
{
fseek($this->handle, $this->byteIndex);
$totalFramesBytes = fread($this->handle, 2);
$ildaTotalFrames = $this->bigEndianToInt($totalFramesBytes);
$this->byteIndex += 2;
return $ildaTotalFrames;
}
/**
* Get the scanner head that will draw this frame
* Advances byteIndex automatically.
*
* @return int
*/
private function readScannerHeadID()
{
fseek($this->handle, $this->byteIndex);
$scannerHeadIDBytes = fread($this->handle, 1);
$ildaScannerHeadID = $this->bigEndianToInt($scannerHeadIDBytes);
$this->byteIndex += 1;
return $ildaScannerHeadID;
}
/**
* Helper
*
* Two's Complement integer to PHP integer
*
* @param string $tcString The string of bytes to be manipulated
* @return integer
*/
private function twosComplimentToInt($tcString)
{
$unpacked = unpack('s', $tcString);
return $unpacked[1];
}
/**
* Helper
*
* Big-endian Motorola integer to PHP
*
* @param string $motorola The string of bytes to be manipulated
* @return integer
*/
private function bigEndianToInt($motorola)
{
if (strlen($motorola) < 2) {
$motorola = str_pad($motorola, 2, "\x00", STR_PAD_LEFT);
}
$unpacked = unpack('n', $motorola);
return $unpacked[1];
}
}