/
binary.php
511 lines (416 loc) · 14.2 KB
/
binary.php
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
require_once('code/common.php');
require_once('code/tiles.php');
require_once('init.php');
# take the high 4 bits of the byte passed. Spread them out and double them. eg:
# pass in: 1234xxxx
# get back: 11223344
function high_4_to_8($hi4) {
$out = ($hi4 & 0x80) | (($hi4 & 0x40) >> 1) | (($hi4 & 0x20) >> 2) | (($hi4 & 0x10) >> 3);
return $out | ($out >> 1);
}
# take the low 4 bits of the byte passed. Spread them out and double them. eg:
# pass in: xxxx1234
# get back: 11223344
function low_4_to_8($low4) {
$out = (($low4 & 0x8) << 3) | (($low4 & 0x4) << 2) | (($low4 & 0x2) << 1) | ($low4 & 0x1);
return $out | ($out << 1);
}
# take the low 2 bits of the byte passed and fann them out
# pass in: xxxxxx12
# get back: 11112222
function low_2_to_8($low2) {
$out = ($low2 & 0x1) | (($low2 & 0x2) << 3);
return $out | ($out << 1) | ($out << 2) | ($out << 3);
}
function high_2_to_8($hi2) {
return low_2_to_8($hi2 >> 6);
}
function himid_2_to_8($hi2) {
return low_2_to_8($hi2 >> 4);
}
function lomid_2_to_8($hi2) {
return low_2_to_8($hi2 >> 2);
}
# for a hard square, we are passed a 128x128 bit shadow
# we pull data from the 32x32 data set in the tile table
function hard_square($url, $shadow) {
$si = 0; # shadow index
$ti = 0; # tile index
$oi = 0; # output index
$tiles = get_hard_tiles($url);
for($tile_number = 0; $tile_number < 64; ++$tile_number) { # TODO make sure this loops l->r t->b
$tile = $tiles[$tile_number];
$ti = 0;
$oi = (($tile_number % 8) * 4) + (floor($tile_number / 8) * 1024);
$si = (($tile_number % 8) * 2) + (floor($tile_number / 8) * 256);
for($rows = 0; $rows < 16; ++$rows) { # we do two rows at once to match shadow
for($cols = 0; $cols < 2; ++$cols) { # we do two cols at once to match shadow
# top left
$p8 = ord($tile[$ti]);
$s = ord($shadow[$si]);
$s8 = high_4_to_8($s);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
# 2nd down, left (uses s from above)
$p8 = ord($tile[$ti + T32_RB]);
# s8 already set above
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
# we output one byte (and another in the next row down),
# but only used up one nibble from shadow.
++$ti;
++$oi;
# top, 1 to right
$p8 = ord($tile[$ti]);
# s already set
$s8 = low_4_to_8($s);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
# 2nd down, 1 to right (uses s from above)
$p8 = ord($tile[$ti + T32_RB]);
# s8 already set above
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
# we output one byte (and another in the next row down),
# and the rest of the shadow byte.
++$ti;
++$oi;
++$si;
}
# end of the row, reset pointers for next row
# $ti wraps automatically
$ti += T32_RB; # we did the next row already
$oi += 28; # we went 4, and the row is 32
$oi += PIXELS_RB; # we did this next row already
$si += 14; # we went 2 and the row is 16
}
}
}
# pass a big tile, a tile 8x smaller, and the width in pixels of the small one (1,2,4,8 and 16 supported)
#
# tiles have one bit per pixel. 4x4 and 2x2 tiles are padded so each row starts on the next byte boundary.
#
# $big is changed, small is not (it's just passed by reference for speed)
#
# The result (overwriting $big) is the bitwise eXclusive OR of $big and (small zoomed 8x)
function blit_xor_8x(&$big, &$small, $width) {
$big_cur = 0;
for($row = 0; $row < $width; ++$row, $big_cur += $width * 7) {
if($width == 16) {
$small_row = (ord($small[$row * 2]) << 8) | ord($small[($row * 2) + 1]);
} else {
$small_row = ord($small[$row]) << 8;
}
for($bit = 0; $bit < $width; ++$bit, ++$big_cur) {
$mask = $small_row & 0x8000; $small_row = $small_row << 1; # pop high bit
$mask |= ($mask >> 1) | ($mask >> 2) | ($mask >> 3);
$mask = ($mask >> 12) | ($mask >> 8);
for($sub_row = 0; $sub_row < 8; ++$sub_row) {
$big_i = $big_cur + ($sub_row * $width);
$big[$big_i] = chr(ord($big[$big_i]) ^ $mask);
}
}
}
}
# fetch the parent 128x128 tile
# FIXME get other ancestors
function hard_shadow($url) {
$url_len = strlen($url);
$shadow = tile_get_128($url);
if($url_len == 0) {
return $shadow;
}
$shadow_16_tile = tile_get_128(substr($url, 0, -1));
list($x, $y) = url_char_to_xy(substr($url, -1), 128);
$shadow_16 = t128_subsection($shadow_16_tile, $x, $y, 16);
if($url_len > 1) {
$shadow_2_tile = tile_get_128(substr($url, 0, -2));
list($x, $y) = url_char_to_xy(substr($url, -2, 1), 8, 128);
list($xq, $yq) = url_char_to_xy(substr($url, -1, 1), 8, 16);
$shadow_2 = t128_subsection($shadow_2_tile, $x + $xq, $y + $yq, 2);
if($url_len > 2) {
$initial_toggle = get_initial_toggle($url, 0);
if($initial_toggle) {
$shadow_2[0] = chr(ord($shadow_2[0]) ^ 0xff);
$shadow_2[1] = chr(ord($shadow_2[1]) ^ 0xff);
}
}
blit_xor_8x($shadow_16, $shadow_2, 2);
}
blit_xor_8x($shadow, $shadow_16, 16);
return $shadow;
}
# fetch the parent 128x128 tile and return the correct quadrant (no scaling)
# FIXME get other ancestors
function medium_shadow($url) {
$url_len = strlen($url);
$parent = tile_get_128(substr($url, 0, -1));
list($x, $y) = url_char_to_xy(substr($url, -1), 2, 128);
$shadow = t128_subsection($parent, $x, $y, 64);
if($url_len == 1) {
return $shadow;
}
if($url_len > 1) {
$shadow_8_tile = tile_get_128(substr($url, 0, -2));
list($x, $y) = url_char_to_xy(substr($url, -2, 1), 8, 128);
list($xq, $yq) = url_char_to_xy(substr($url, -1, 1), 2, 16);
$shadow_8 = t128_subsection($shadow_8_tile, $x + $xq, $y + $yq, 8);
if($url_len > 2) {
$initial_toggle = get_initial_toggle($url, 2);
if($initial_toggle) {
$shadow_8[0] = chr(ord($shadow_8[0]) ^ 0xff);
$shadow_8[1] = chr(ord($shadow_8[1]) ^ 0xff);
$shadow_8[2] = chr(ord($shadow_8[2]) ^ 0xff);
$shadow_8[3] = chr(ord($shadow_8[3]) ^ 0xff);
$shadow_8[4] = chr(ord($shadow_8[4]) ^ 0xff);
$shadow_8[5] = chr(ord($shadow_8[5]) ^ 0xff);
$shadow_8[6] = chr(ord($shadow_8[6]) ^ 0xff);
$shadow_8[7] = chr(ord($shadow_8[7]) ^ 0xff);
}
}
blit_xor_8x($shadow, $shadow_8, 8);
}
return $shadow;
}
# fetch the parent 128x128 tile and return the correct 32x32 piece (no scaling)
# FIXME get other ancestors
function easy_shadow($url) {
$url_len = strlen($url);
$parent = tile_get_128(substr($url, 0, -1));
$shadow = '';
$pos = strpos(URL_CHARS, substr($url, -1));
$qx = floor(($pos % 8) / 2) * T32_RB;
$qy = floor($pos / 16) * T128_RB * 32;
for($y = 0; $y < 32; ++$y) {
$shadow .= substr($parent, $qx + $qy + ($y * T128_RB), T32_RB);
}
if($url_len == 1) {
return $shadow;
}
if($url_len > 1) {
$shadow_4_tile = tile_get_128(substr($url, 0, -2));
list($x, $y) = url_char_to_xy(substr($url, -2, 1), 8, 128);
list($xq, $yq) = url_char_to_xy(substr($url, -1, 1), 4, 16);
$shadow_4 = t128_subsection($shadow_4_tile, $x + $xq, $y + $yq, 4);
if($url_len > 2) {
$initial_toggle = get_initial_toggle($url, 2);
if($initial_toggle) {
$shadow_4[0] = chr(ord($shadow_4[0]) ^ 0xff);
$shadow_4[1] = chr(ord($shadow_4[1]) ^ 0xff);
$shadow_4[2] = chr(ord($shadow_4[2]) ^ 0xff);
$shadow_4[3] = chr(ord($shadow_4[3]) ^ 0xff);
}
}
blit_xor_8x($shadow, $shadow_4, 4);
}
return $shadow;
}
# for a medium square: shadow is a 64x64 grid
# we pull data from the 64x64 data set in the tile table
function medium_square($url, $shadow) {
$si = 0; # shadow index
$ti = 0; # tile index
$oi = 0; # output index
$tiles = get_medium_tiles($url);
for($tile_number = 0; $tile_number < 16; ++$tile_number) {
$tile = $tiles[$tile_number];
$ti = 0;
$oi = (($tile_number % 4) * 8) + (floor($tile_number / 4) * 2048);
$si = (($tile_number % 4) * 2) + (floor($tile_number / 4) * 128);
# there is a 16x16 part of the shadow covering our tile. 2 is the byte width:
for($rows = 0; $rows < 16; ++$rows) { # we do four rows at once to match shadow
for($cols = 0; $cols < 2; ++$cols) { # we do four cols at once to match shadow
$s = ord($shadow[$si]); # used for this whole context
$s8 = high_2_to_8($s); # used for the next 4 pixels
$p8 = ord($tile[$ti]);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB]);
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB2]);
$GLOBALS['pixels'][$oi + PIXELS_RB2] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB3]);
$GLOBALS['pixels'][$oi + PIXELS_RB3] = $p8 ^ $s8;
++$ti;
++$oi;
$s8 = himid_2_to_8($s); # used for the next 4 pixels
$p8 = ord($tile[$ti]);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB]);
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB2]);
$GLOBALS['pixels'][$oi + PIXELS_RB2] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB3]);
$GLOBALS['pixels'][$oi + PIXELS_RB3] = $p8 ^ $s8;
++$ti;
++$oi;
$s8 = lomid_2_to_8($s); # used for the next 4 pixels
$p8 = ord($tile[$ti]);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB]);
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB2]);
$GLOBALS['pixels'][$oi + PIXELS_RB2] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB3]);
$GLOBALS['pixels'][$oi + PIXELS_RB3] = $p8 ^ $s8;
++$ti;
++$oi;
$s8 = low_2_to_8($s); # used for the next 4 pixels
$p8 = ord($tile[$ti]);
$GLOBALS['pixels'][$oi] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB]);
$GLOBALS['pixels'][$oi + PIXELS_RB] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB2]);
$GLOBALS['pixels'][$oi + PIXELS_RB2] = $p8 ^ $s8;
$p8 = ord($tile[$ti + T64_RB3]);
$GLOBALS['pixels'][$oi + PIXELS_RB3] = $p8 ^ $s8;
++$ti;
++$oi;
# we finished the shadow byte
++$si;
}
# end of the row, reset pointers for next row
$ti += T64_RB3; # we advanced 1 row already
$oi += 24; # we advanced 8, and the row is 32
$oi += PIXELS_RB3; # we did these next rows already
$si += 6; # we advanced 2 and the row is 8
}
}
}
# for an easy square, we are passed a 32x32 grid
# we pull data from the 128x128 data set in the tile table
function easy_square($url, $shadow) {
# FIXME
$si = 0; # shadow index
$ti = 0; # tile index
$oi = 0; # output index
$tiles = get_easy_tiles($url);
for($tile_number = 0; $tile_number < 4; ++$tile_number) {
$tile = $tiles[$tile_number];
$ti = 0;
$oi = (($tile_number % 2) * 16) + (floor($tile_number / 2) * 4096);
$si = (($tile_number % 2) * 2) + (floor($tile_number / 2) * 64);
# there is a 16x16 part of the shadow covering our tile. 2 is the byte width:
for($rows = 0; $rows < 16; ++$rows) { # we do eight rows at once to match shadow
for($cols = 0; $cols < 2; ++$cols) { # we do eight cols at once to match shadow
$s = ord($shadow[$si]); # used for this whole context
for($i = 0; $i < 8; ++$i) { # horizontal
$mask = $s & 0x80; $s = $s << 1; # pop high bit
# fan out bit
$mask |= ($mask >> 1) | ($mask >> 2) | ($mask >> 3);
$mask |= ($mask >> 4);
$tmp_ti = $ti;
$tmp_oi = $oi;
for($j = 0; $j < 8; ++$j) {
$p8 = ord($tile[$tmp_ti]);
$GLOBALS['pixels'][$tmp_oi] = $p8 ^ $mask;
$tmp_ti += T128_RB;
$tmp_oi += PIXELS_RB;
}
++$ti;
++$oi;
}
# we finished the shadow byte
++$si;
}
# end of the row, reset pointers for next row
$ti += T128_RB7; # we advanced 1 row already
$oi += 16; # we advanced 16, and the row is 32
$oi += PIXELS_RB7; # we did these next rows already
$si += 2; # we advanced 2 and the row is 4
}
}
}
# find the initial "background color" for this square. that is:
# start at zoom 0, and zoom in while the square to be rendered is still contained within a single pixel. Return the cumulative toggle of that pixel.
# return 1 (toggled) or 0 (not toggled)
function get_initial_toggle($url, $dots) {
$zooms = (strlen($url) * 3) - strlen($dots);
# after zooming 7 times, a single pixel from tile '' covers the whole picture.
if($zooms < 7) {
header("X-Initial-Toggle-Note: Only $zooms zooms.");
return 0;
}
$url_chars = 0;
$toggle = 0;
while($zooms > 6) { # 6 because the lower zooms come to a single pixel below plane 0 where we have no data
$zooms -= 3;
$t128 = tile_get_128(substr($url, 0, $url_chars));
# the next two bytes of the url tell us which pixel to take
$ch1 = strpos(URL_CHARS, substr($url, $url_chars, 1));
$ch2 = strpos(URL_CHARS, substr($url, $url_chars + 1, 1));
$ch3 = strpos(URL_CHARS, substr($url, $url_chars + 2, 1));
# there are 8x8 child tiles under this tile. Each sits under 16x16 pixels?
$x = ($ch1 % 8) * 16;
$x += ($ch2 % 8) * 2;
$x += floor(($ch3 % 8) / 4);
$y = floor($ch1 / 8) * 16;
$y += floor($ch2 / 8) * 2;
$y += floor(($ch3 / 8) / 4);
$toggle ^= (ord(substr($t128, ($y * T128_RB) + floor($x / 8), 1)) >> (7 - ($x % 8))) & 1;
$url_chars += 1;
}
header("X-Initial-Toggle-Note: initial toggle: $toggle.");
return $toggle;
}
function binary_main() {
if(isset($_REQUEST['z'])) {
$url = ereg_replace('[^a-zA-Z0-9._-]', '', $_REQUEST['z']);
$pos = strpos($url, '.');
if($pos !== false) {
$dots = substr($url, $pos);
$dots = ereg_replace('[^.]', '', $dots);
$url = substr($url, 0, $pos);
if(strlen($dots) > 2 || ($url == '' && $dots != '')) {
print("Invalid URL");
return;
}
} else {
$dots = '';
}
} else {
$url = '';
$dots = '';
}
if(!am_debugging()) {
header('Content-Type: text/javascript; charset=us-ascii');
}
$SQUARE_WIDTH = 256;
# $GLOBALS['pixels'] is an array of ints, because php won't let me use ^= on chars.
$GLOBALS['pixels'] = array_fill(0, 256 * 256 / 8, 255);
$initial_toggle = get_initial_toggle($url, $dots);
$shadow = str_repeat("\000", 128 * 128 / 8); #FIXME
switch($dots) {
case '':
$shadow = hard_shadow($url);
hard_square($url, $shadow);
break;
case '..':
$shadow = medium_shadow($url);
medium_square($url, $shadow);
break;
case '.':
$shadow = easy_shadow($url);
easy_square($url, $shadow);
break;
}
$color = 0;
$out = '[';
$cur = 0;
for($i = 0; $i < PIXELS_RB * 256; $i++) {
$byte = $GLOBALS['pixels'][$i];
if($byte == $color) {
$cur += 8;
} else {
for($bit = 0x80; $bit; $bit >>= 1) {
if((($byte & $bit) ^ ($bit & $color)) == 0) {
++$cur;
} else {
$out .= $cur . ',';
$cur = 1;
$color ^= 0xff;
}
}
}
}
$out .= $cur . ']';
print($out);
exit();
}
?>