-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReader.cs
376 lines (319 loc) · 13.3 KB
/
Reader.cs
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Net.Security;
using UnityEngine;
using Random = System.Random;
namespace CircleTag
{
public static class Reader
{
private static TagImage _debugImage;
public static byte[] Read(byte[] bytes, int width, int height, uint tolerance, byte[] debugBytes = null)
{
TagImage tagImage = new TagImage()
{
Bytes = bytes,
Width = width,
Height = height,
ColorDifferenceTolerance = tolerance
};
#if DEBUG
if (debugBytes != null)
{
_debugImage = new TagImage()
{
Bytes = debugBytes,
Width = width,
Height = height
};
}
#endif
tagImage.BaseColor = tagImage.ReadColor(tagImage.CenterX, tagImage.CenterY);
int maxIterations = Mathf.Min(width, height) / 32;
if (maxIterations <= 0)
{
return null;
}
if (!TryFindTagCenterCoordinatesAndRadius(tagImage, maxIterations))
{
return null;
}
if (!TryFindStartingAngleAndSegmentSize(tagImage))
{
return null;
}
if (!TryFindLayerSizeAndCount(tagImage))
{
return null;
}
return TryReadData(tagImage, out byte[] readBytes) ? readBytes : null;
}
private static bool TryFindTagCenterCoordinatesAndRadius(TagImage tagImage, int maxIterations)
{
// Try finding the tag center iteratively
for (int iteration = 0; iteration < maxIterations; iteration++)
{
// Search center in four pixel steps
int offX = iteration * 5;
int offY = iteration * 5;
// Check if we went past the half of the width of the image
if(offX > tagImage.CenterX) return false;
if(offY > tagImage.CenterY) return false;
// Alternate offset direction between iterations
if (iteration % 2 == 0)
{
offX = -offX;
offY = -offY;
}
if (iteration % 4 == 0)
{
offY = -offY;
}
if (iteration % 3 == 0)
{
offX = -offX;
}
// Try finding the center with the given offset
if (TryFindTagCenterCoordinates(tagImage, offX, offY, out int codeCenterX, out int codeCenterY, out int radius))
{
tagImage.CodeCenterX = codeCenterX;
tagImage.CodeCenterY = codeCenterY;
tagImage.CodeRadius = radius;
return true;
}
}
return false;
}
private static bool TryFindLayerSizeAndCount(TagImage tagImage)
{
int layerSize = 0;
int layerCount = 0;
double angle = tagImage.CodeStartingAngle + tagImage.CodeSegmentSize / 2.0;
int startingIndex = tagImage.CodeRadius;
uint dataColor = 0;
int usedWidth = Math.Min(tagImage.CodeCenterX, tagImage.Width / 2);
// Find layer size
for (int i = startingIndex; i < tagImage.Width; i++)
{
CalculateCoords(tagImage, angle, i, out int x, out int y);
if(x < 0 || y < 0 || x > tagImage.Width || y > tagImage.Height) continue;
uint color = tagImage.ReadColor(x, y);
uint diff = tagImage.ColorDiff(color);
if(diff < tagImage.ColorDifferenceTolerance)
{
continue;
}
dataColor = color;
layerSize = i - startingIndex - 1;
startingIndex = i;
break;
}
if (layerSize == 0 || layerSize == usedWidth)
{
return false;
}
// Find size of the tag data block (layer size * layer count, starting from the first data layer)
int dataSize = -1;
for (int i = startingIndex; i < tagImage.Width; i++)
{
CalculateCoords(tagImage, angle, i, out int x, out int y);
if(x < 0 || y < 0 || x > tagImage.Width || y > tagImage.Height) continue;
uint color = tagImage.ReadColor(x, y);
uint diff = TagImage.ColorDiff(color, dataColor);
if(diff < tagImage.ColorDifferenceTolerance)
{
continue;
}
dataSize = i - startingIndex - 1;
break;
}
if (dataSize < 0)
{
return false;
}
layerCount = (int)Math.Round((double)dataSize / (double)layerSize);
tagImage.CodeLayerSize = layerSize;
tagImage.CodeLayerCount = layerCount;
return true;
}
private static int CalculateSegmentCount(double segmentSize)
{
int segments = (int) Math.Round(Math.PI * 2 / segmentSize);
// If the number of segments - 1 can be divided by 8 equally, it should be a correct amount of segments
return (segments -1) % 8 == 0 ? segments : -1;
}
private static bool TryReadData(TagImage tagImage, out byte[] bytes)
{
bytes = null;
int segments = CalculateSegmentCount(tagImage.CodeSegmentSize);
if (segments <= 0) return false;
double halfSegment = tagImage.CodeSegmentSize / 2.0;
int byteIndex = 0;
byte currentBit = 1;
byte currentByte = 0;
byte sizeByte = 0;
for (int layer = 0; layer < tagImage.CodeLayerCount; layer++)
{
for (int segment = 1; segment < segments; segment++)
{
// Calculate coordinates
double angle = tagImage.CodeStartingAngle + segment * tagImage.CodeSegmentSize + halfSegment;
int distance = tagImage.CodeRadius + tagImage.CodeLayerSize + layer * tagImage.CodeLayerSize + tagImage.CodeLayerSize / 2;
CalculateCoords(tagImage, angle, distance, out int x, out int y);
// Read bit
if (tagImage.CheckPixel(x, y))
{
currentByte |= currentBit;
}
// Advance bit and check if there are more bits
currentBit <<= 1;
if (currentBit > 0) continue;
// No more bits, handle byte
currentBit = 1;
if (sizeByte == 0)
{
// Size not read yet, use the current byte value as data size and continue reading
sizeByte = currentByte;
currentByte = 0;
bytes = new byte[sizeByte];
continue;
}
if (byteIndex == sizeByte)
{
// We have read all bytes, check hash byte and return
byte hash = CalculateHash(bytes);
return hash == currentByte;
}
// Push the current byte to the byte array and continue reading
bytes[byteIndex] = currentByte;
currentByte = 0;
byteIndex++;
}
}
return false;
}
public static byte CalculateHash(byte[] bytes)
{
byte hash = 0;
unchecked
{
for (int i = 0; i < bytes.Length; i++)
{
hash += bytes[i];
}
}
return hash;
}
private static void CalculateCoords(TagImage image, double angle, int distance, out int x, out int y)
{
x = (int) Math.Round(Math.Cos(-angle) * distance) + image.CodeCenterX;
y = (int) Math.Round(Math.Sin(-angle) * distance) + image.CodeCenterY;
}
private static bool TryFindStartingAngleAndSegmentSize(TagImage tagImage)
{
double startingAngle = -1;
const double angleStepSize = Math.PI / 360.0 / 2.0;
const int radiusOffset = 2;
double currentAngle = 0.0;
for (int step = 0; currentAngle < Math.PI * 2; step++)
{
currentAngle = angleStepSize * step;
CalculateCoords(tagImage, currentAngle, tagImage.CodeRadius + radiusOffset, out int x, out int y);
_debugImage?.WriteColor(x, y, 0xff0000ff);
if(x < 0 || y < 0 || x > tagImage.Width || y > tagImage.Width)
{
continue;
}
bool hasPixel = tagImage.CheckPixel(x, y);
// Find the first pixel in the notch of the first layer
if (startingAngle < 0 && !hasPixel)
{
startingAngle = currentAngle - angleStepSize;
_debugImage?.WriteColor(x, y, 0xffff00ff);
}
// Find the last pixel in the notch of the first layer
if (startingAngle > 0 && hasPixel)
{
tagImage.CodeStartingAngle = startingAngle;
tagImage.CodeSegmentSize = currentAngle - startingAngle;
_debugImage?.WriteColor(x, y, 0xff00ffff);
return true;
}
}
return false;
}
private static bool TryFindTagCenterCoordinates(TagImage tagImage, int offX, int offY, out int foundCenterX, out int foundCenterY, out int radius)
{
foundCenterX = -1;
foundCenterY = -1;
radius = -1;
if (!TryCalculateDistances(tagImage, tagImage.CenterX + offX, tagImage.CenterY + offY, out int left, out int right, out int down, out int up))
{
return false;
}
int width = right - left;
int height = up - down;
foundCenterX = (left + width) >> 1;
foundCenterY = (down + height) >> 1;
int diff = width - height;
// Optimized diff = abs(diff), works as long as diff is not int.MinValue, which it is assumed to not be in this case.
diff = (diff + (diff >> 31)) ^ (diff >> 31);
// If the difference is too large, failed to read the image
if (diff > 1) return false;
// Then calculate the radius from given width and height
radius = (width < height ? width : height) >> 1;
return true;
}
private static bool TryCalculateDistances(TagImage tagImage, int originX, int originY, out int left, out int right, out int down, out int up)
{
left = -1;
right = -1;
down = -1;
up = -1;
int size = tagImage.Width / 2;
uint diff = 0;
for (int i = 1; i < size; i++)
{
int x = originX - i;
int y = originY;
if(x >= 0 && x < tagImage.Width)
{
if (left < 0 && tagImage.CheckPixel(x, y, out diff))
{
left = x + 1;
}
_debugImage?.WriteColor(x, y, diff | 0xff000000);
}
x = originX + i;
if(x >= 0 && x < tagImage.Width)
{
if (right < 0 && tagImage.CheckPixel(x, y, out diff))
{
right = x - 1;
}
_debugImage?.WriteColor(x, y, diff | 0xff000000);
}
x = originX;
y = originY - i;
if(y >= 0 && y < tagImage.Height)
{
if (down < 0 && tagImage.CheckPixel(x, y, out diff))
{
down = y + 1;
}
_debugImage?.WriteColor(x, y, diff | 0xff000000);
}
y = originY + i;
if(y >= 0 && y < tagImage.Height)
{
if (up < 0 && tagImage.CheckPixel(x, y, out diff))
{
up = y - 1;
}
_debugImage?.WriteColor(x, y, diff | 0xff000000);
}
if(left >= 0 && right >= 0 && down >= 0 && up >= 0) return true;
}
return false;
}
}
}