forked from overviewer/Minecraft-Overviewer
/
quadtree.py
executable file
·865 lines (727 loc) · 32.8 KB
/
quadtree.py
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# This file is part of the Minecraft Overviewer.
#
# Minecraft Overviewer is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# Minecraft Overviewer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
import multiprocessing
import itertools
import os
import os.path
import hashlib
import functools
import re
import shutil
import collections
import json
import sys
import logging
import util
import cPickle
import nbt
from PIL import Image
from time import gmtime, strftime
from optimizeimages import optimize_image
import composite
"""
This module has routines related to generating a quadtree of tiles
"""
def iterate_base4(d):
"""Iterates over a base 4 number with d digits"""
return itertools.product(xrange(4), repeat=d)
def catch_keyboardinterrupt(func):
"""Decorator that catches a keyboardinterrupt and raises a real exception
so that multiprocessing will propagate it properly"""
@functools.wraps(func)
def newfunc(*args, **kwargs):
try:
return func(*args, **kwargs)
except KeyboardInterrupt:
logging.error("Ctrl-C caught!")
raise Exception("Exiting")
except:
import traceback
traceback.print_exc()
raise
return newfunc
class QuadtreeGen(object):
def __init__(self, worldobj, destdir, depth=None, imgformat=None, chunkset=None, optimizeimg=None):
"""Generates a quadtree from the world given into the
given dest directory
worldobj is a world.WorldRenderer object that has already been processed
If depth is given, it overrides the calculated value. Otherwise, the
minimum depth that contains all chunks is calculated and used.
"""
self.chunkset = chunkset
assert(imgformat)
self.imgformat = imgformat
self.optimizeimg = optimizeimg
# Make the destination dir
if not os.path.exists(destdir):
os.mkdir(destdir)
if depth is None:
# Determine quadtree depth (midpoint is always 0,0)
for p in xrange(15):
# Will 2^p tiles wide and high suffice?
# X has twice as many chunks as tiles, then halved since this is a
# radius
xradius = 2**p
# Y has 4 times as many chunks as tiles, then halved since this is
# a radius
yradius = 2*2**p
if xradius >= worldobj.maxcol and -xradius <= worldobj.mincol and \
yradius >= worldobj.maxrow and -yradius <= worldobj.minrow:
break
else:
raise ValueError("Your map is waaaay too big!")
self.p = p
else:
self.p = depth
xradius = 2**depth
yradius = 2*2**depth
# Make new row and column ranges
self.mincol = -xradius
self.maxcol = xradius
self.minrow = -yradius
self.maxrow = yradius
self.world = worldobj
self.destdir = destdir
def print_statusline(self, complete, total, level, unconditional=False):
if unconditional:
pass
elif complete < 10:
if not complete % 2 == 0:
return
elif complete < 100:
if not complete % 25 == 0:
return
elif complete < 1000:
if not complete % 100 == 0:
return
else:
if not complete % 1000 == 0:
return
logging.info("{0}/{1} tiles complete on level {2}/{3}".format(
complete, total, level, self.p))
def write_html(self, worlddir=None, onlyindex=False):
"""Writes out index.html, and optionally maprefresh.js and regions.js"""
zoomlevel = self.p
imgformat = self.imgformat
indexpath = os.path.join(util.get_program_path(), "template.html")
## read spawn info from level.dat
data = nbt.load(os.path.join(worlddir, "level.dat"))[1]
spawnX = data['Data']['SpawnX']
spawnY = data['Data']['SpawnY']
spawnZ = data['Data']['SpawnZ']
html = open(indexpath, 'r').read()
html = html.replace("{defaultzoom}", str(zoomlevel / 2 + 1))
html = html.replace("{markerzoom}", str(zoomlevel / 2 + 3))
html = html.replace("{maxzoom}", str(zoomlevel))
html = html.replace("{imgformat}", str(imgformat))
html = html.replace("{mapcenter}", str(str(spawnX) + "," + str(spawnY) + "," + str(spawnZ)))
html = html.replace("{statustext}", str("Last Updated: "+strftime("%a, %d %b %Y %H:%M:%S %Z")))
with open(os.path.join(self.destdir, "index.html"), 'w') as output:
output.write(html)
if onlyindex:
return
# Write a blank image
blank = Image.new("RGBA", (1,1))
tileDir = os.path.join(self.destdir, "tiles")
if not os.path.exists(tileDir): os.mkdir(tileDir)
tileDir = os.path.join(tileDir, "unlit") #!TODO!add logic for allbranches
if not os.path.exists(tileDir): os.mkdir(tileDir)
blank.save(os.path.join(tileDir, "blank."+self.imgformat))
# copy web assets into destdir:
for root, dirs, files in os.walk(os.path.join(util.get_program_path(), "web_assets")):
for f in files:
shutil.copy(os.path.join(root, f), self.destdir)
# since we will only discover PointsOfInterest in chunks that need to be
# [re]rendered, POIs like signs in unchanged chunks will not be listed
# in self.world.POI. To make sure we don't remove these from markers.js
# we need to merge self.world.POI with the persistant data in world.PersistentData
self.world.POI += filter(lambda x: x['type'] != 'spawn', self.world.persistentData['POI'])
# write out the default marker table
with open(os.path.join(self.destdir, "markers.js"), 'w') as output:
output.write("var markerData=%s" % json.dumps(self.world.POI))
# save persistent data
self.world.persistentData['POI'] = self.world.POI
with open(self.world.pickleFile,"wb") as f:
cPickle.dump(self.world.persistentData,f)
# write out the default (empty, but documented) region table
with open(os.path.join(self.destdir, "regions.js"), 'w') as output:
output.write('var regionData=[\n')
output.write(' // {"color": "#FFAA00", "opacity": 0.5, "closed": true, "path": [\n')
output.write(' // {"x": 0, "y": 0, "z": 0},\n')
output.write(' // {"x": 0, "y": 10, "z": 0},\n')
output.write(' // {"x": 0, "y": 0, "z": 10}\n')
output.write(' // ]},\n')
output.write('];')
def _get_cur_depth(self):
"""How deep is the quadtree currently in the destdir? This glances in
index.html to see what maxZoom is set to.
returns -1 if it couldn't be detected, file not found, or nothing in
index.html matched
"""
indexfile = os.path.join(self.destdir, "index.html")
if not os.path.exists(indexfile):
return -1
matcher = re.compile(r"maxZoom:\s*(\d+)")
p = -1
for line in open(indexfile, "r"):
res = matcher.search(line)
if res:
p = int(res.group(1))
break
return p
def _increase_depth(self):
"""Moves existing tiles into place for a larger tree"""
getpath = functools.partial(os.path.join, self.destdir, "tiles", "unlit")
# At top level of the tree:
# quadrant 0 is now 0/3
# 1 is now 1/2
# 2 is now 2/1
# 3 is now 3/0
# then all that needs to be done is to regenerate the new top level
for dirnum in range(4):
newnum = (3,2,1,0)[dirnum]
newdir = "new" + str(dirnum)
newdirpath = getpath(newdir)
files = [str(dirnum)+"."+self.imgformat, str(dirnum)+".hash", str(dirnum)]
newfiles = [str(newnum)+"."+self.imgformat, str(newnum)+".hash", str(newnum)]
os.mkdir(newdirpath)
for f, newf in zip(files, newfiles):
p = getpath(f)
if os.path.exists(p):
os.rename(p, getpath(newdir, newf))
os.rename(newdirpath, getpath(str(dirnum)))
def _decrease_depth(self):
"""If the map size decreases, or perhaps the user has a depth override
in effect, re-arrange existing tiles for a smaller tree"""
getpath = functools.partial(os.path.join, self.destdir, "tiles", "unlit")
# quadrant 0/3 goes to 0
# 1/2 goes to 1
# 2/1 goes to 2
# 3/0 goes to 3
# Just worry about the directories here, the files at the top two
# levels are cheap enough to replace
if os.path.exists(getpath("0", "3")):
os.rename(getpath("0", "3"), getpath("new0"))
shutil.rmtree(getpath("0"))
os.rename(getpath("new0"), getpath("0"))
if os.path.exists(getpath("1", "2")):
os.rename(getpath("1", "2"), getpath("new1"))
shutil.rmtree(getpath("1"))
os.rename(getpath("new1"), getpath("1"))
if os.path.exists(getpath("2", "1")):
os.rename(getpath("2", "1"), getpath("new2"))
shutil.rmtree(getpath("2"))
os.rename(getpath("new2"), getpath("2"))
if os.path.exists(getpath("3", "0")):
os.rename(getpath("3", "0"), getpath("new3"))
shutil.rmtree(getpath("3"))
os.rename(getpath("new3"), getpath("3"))
def _apply_render_worldtiles(self, pool):
"""Returns an iterator over result objects. Each time a new result is
requested, a new task is added to the pool and a result returned.
"""
for path in iterate_base4(self.p):
# Get the range for this tile
colstart, rowstart = self._get_range_by_path(path)
colend = colstart + 2
rowend = rowstart + 4
# This image is rendered at:
dest = os.path.join(self.destdir, "tiles","unlit", *(str(x) for x in path))
# And uses these chunks
tilechunks = self._get_chunks_in_range(colstart, colend, rowstart,
rowend)
# Put this in the pool
# (even if tilechunks is empty, render_worldtile will delete
# existing images if appropriate)
yield pool.apply_async(func=render_worldtile, args= (tilechunks,
colstart, colend, rowstart, rowend, dest, self.imgformat,
self.optimizeimg))
def _apply_render_inntertile(self, pool, zoom):
"""Same as _apply_render_worltiles but for the inntertile routine.
Returns an iterator that yields result objects from tasks that have
been applied to the pool.
"""
for path in iterate_base4(zoom):
# This image is rendered at:
dest = os.path.join(self.destdir, "tiles","unlit", *(str(x) for x in path[:-1]))
name = str(path[-1])
yield pool.apply_async(func=render_innertile, args= (dest, name, self.imgformat, self.optimizeimg))
def _apply_render_worldinclusiontiles(self, pool):
"""Returns an iterator over result objects. Each time a new result is
requested, a new task is added to the pool and a result returned.
"""
"""Get chunks that are relevant to the tile rendering function that's
rendering that range"""
# !TODO! Add a buffer around inclusion chunks, primarily above (say 6 chunks?)
tilechunks = []
checkedpaths = []
for (col,row) in self.chunkset:
for i in range(-4,4): # Add buffer around updated chunks
for j in range(-4,4): # increases render time, but makes sure map looks correct
#!TODO! check 4 rows/cols all directions is enough (may need more)
#!TODO! totally changes total chunk numbers, update with new math
#!TODO! check whether multiple instances of same chunk only get rendered once (because after first time, hashes will match)
#!TODO! add configurable level of update (sometimes small, ie 0-just chunk itself, sometimes large ie 5, huge area)
# Get a correct path for this chunk
# Not sure why we need +6, but it works
path = self._get_path_by_coordinates(self.p,col+j,row+6+i)
if path in checkedpaths:
continue;
else:
checkedpaths.append(path)
# Get the range for this tile
colstart, rowstart = self._get_range_by_path(path)
colend = colstart + 2
rowend = rowstart + 4
# This image is rendered at:
dest = os.path.join(self.destdir, "tiles","unlit", *(str(x) for x in path))
# And uses these chunks
tilechunks = self._get_chunks_in_range(colstart, colend, rowstart, rowend)
# Put this in the pool
# (even if tilechunks is empty, render_worldtile will delete
# existing images if appropriate)
yield pool.apply_async(func=render_worldtile, args= (tilechunks,
colstart, colend, rowstart, rowend, dest, self.imgformat,
self.optimizeimg))
def _apply_render_innerinclusiontile(self, pool, zoom):
"""Same as _apply_render_worltiles but for the inntertile routine.
Returns an iterator that yields result objects from tasks that have
been applied to the pool.
"""
checkedpaths = []
for (col,row) in self.chunkset:
for i in range(-4,4): # Add buffer around updated chunks
for j in range(-4,4): # increases render time, but makes sure map looks correct
#!TODO! check 4 rows/cols all directions is enough (may need more)
#!TODO! totally changes total chunk numbers, update with new math
#!TODO! check whether multiple instances of same chunk only get rendered once (because after first time, hashes will match)
# Get a correct path for this chunk
# Not sure why we need +6, but it works
path = self._get_path_by_coordinates(zoom,col+j,row+6+i) # Wow, totally missed the zoom param
if path in checkedpaths:
continue;
else:
checkedpaths.append(path)
#print ("path : {0}").format(path)
# This image is rendered at:
dest = os.path.join(self.destdir, "tiles","unlit", *(str(x) for x in path[:-1]))
name = str(path[-1])
yield pool.apply_async(func=render_innertile, args= (dest, name, self.imgformat, self.optimizeimg))
def renderChunkset(self, procs, chunkset=None):
"""Renders all tiles"""
self.chunkset = chunkset
curdepth = self._get_cur_depth()
if curdepth != -1:
if self.p > curdepth:
logging.warning("Your map seemes to have expanded beyond its previous bounds.")
logging.warning( "Doing some tile re-arrangements... just a sec...")
for _ in xrange(self.p-curdepth):
self._increase_depth()
elif self.p < curdepth:
logging.warning("Your map seems to have shrunk. Re-arranging tiles, just a sec...")
for _ in xrange(curdepth - self.p):
self._decrease_depth()
# Create a pool
if procs == 1:
pool = FakePool()
else:
pool = multiprocessing.Pool(processes=procs)
# Render the highest level of tiles from the chunks
results = collections.deque()
complete = 0
# Check for chunklist
if self.chunkset:
total = len(self.chunkset)
logging.info("Have a chunklist, so only rendering subset of tiles")
for result in self._apply_render_worldinclusiontiles(pool):
results.append(result)
if len(results) > 10000:
# Empty the queue before adding any more, so that memory
# required has an upper bound
while len(results) > 500:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, 1)
# Wait for the rest of the results
while len(results) > 0:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, 1)
self.print_statusline(complete, total, 1, True)
# Now do the other layers
for zoom in xrange(self.p-1, 0, -1):
level = self.p - zoom + 1
assert len(results) == 0
complete = 0
#total = len(self.chunkset)
logging.info( "Starting level {0}".format(level))
for result in self._apply_render_innerinclusiontile(pool, zoom):
results.append(result)
if len(results) > 10000:
while len(results) > 500:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, level)
# Empty the queue
while len(results) > 0:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, level)
self.print_statusline(complete, total, level, True)
logging.info("Done")
pool.close()
pool.join()
else:
total = 4**self.p
logging.info("Rendering highest zoom level of tiles now.")
logging.info("There are {0} tiles to render".format(total))
logging.info("There are {0} total levels to render".format(self.p))
logging.info("Don't worry, each level has only 25% as many tiles as the last.")
logging.info("The others will go faster")
for result in self._apply_render_worldtiles(pool):
results.append(result)
if len(results) > 1000:
# Empty the queue before adding any more, so that memory
# required has an upper bound
while len(results) > 100:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, 1)
# Wait for the rest of the results
while len(results) > 0:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, 1)
self.print_statusline(complete, total, 1, True)
# Now do the other layers
for zoom in xrange(self.p-1, 0, -1):
level = self.p - zoom + 1
assert len(results) == 0
complete = 0
total = 4**zoom
logging.info("Starting level {0}".format(level))
for result in self._apply_render_inntertile(pool, zoom):
results.append(result)
if len(results) > 1000:
while len(results) > 100:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, level)
# Empty the queue
while len(results) > 0:
results.popleft().get()
complete += 1
self.print_statusline(complete, total, level)
self.print_statusline(complete, total, level, True)
logging.info("Done")
pool.close()
pool.join()
# Do the final one right here:
render_innertile(os.path.join(self.destdir, "tiles","unlit"), "base", self.imgformat, self.optimizeimg)
def _get_range_by_path(self, path):
"""Returns the x, y chunk coordinates of this tile"""
# tile/3/2/1/0/2/3/1
x, y = self.mincol, self.minrow
xsize = self.maxcol
ysize = self.maxrow
for p in path:
if p in (1, 3):
x += xsize
if p in (2, 3):
y += ysize
xsize //= 2
ysize //= 2
# print path
#print x,y
# sys.exit()
return x, y
def _get_path_by_coordinates(self, zoom, x,y):
"""Returns the path of this chunk"""
depth = self.p
xradius = 2**(depth)
yradius = 2*2**(depth)
# Make new row and column ranges
xpivot = 0
ypivot = 0
path = []
for p in range(0,zoom):
yradius /= 2
xradius /= 2
#!TODO!Improve this tree structure, should be able to use XOR
if x >= xpivot:
if y >= ypivot:
path.append(3)
xpivot = (xpivot + xradius)
ypivot = (ypivot + yradius)
continue
else:
path.append(1)
xpivot = (xpivot + xradius)
ypivot = (ypivot - yradius)
continue
if x <= xpivot:
if y >= ypivot:
path.append(2)
xpivot = (xpivot - xradius)
ypivot = (ypivot + yradius)
continue
else:
path.append(0)
xpivot = (xpivot - xradius)
ypivot = (ypivot - yradius)
continue
return path
def _get_chunks_in_range(self, colstart, colend, rowstart, rowend):
"""Get chunks that are relevant to the tile rendering function that's
rendering that range"""
chunklist = []
for row in xrange(rowstart-16, rowend+1):
for col in xrange(colstart, colend+1):
c = self.world.chunkmap.get((col, row), None)
if c:
chunklist.append((col, row, c))
return chunklist
@catch_keyboardinterrupt
def render_innertile(dest, name, imgformat, optimizeimg):
"""
Renders a tile at os.path.join(dest, name)+".ext" by taking tiles from
os.path.join(dest, name, "{0,1,2,3}.png")
"""
imgpath = os.path.join(dest, name) + "." + imgformat
hashpath = os.path.join(dest, name) + ".hash"
if name == "base":
q0path = os.path.join(dest, "0." + imgformat)
q1path = os.path.join(dest, "1." + imgformat)
q2path = os.path.join(dest, "2." + imgformat)
q3path = os.path.join(dest, "3." + imgformat)
q0hash = os.path.join(dest, "0.hash")
q1hash = os.path.join(dest, "1.hash")
q2hash = os.path.join(dest, "2.hash")
q3hash = os.path.join(dest, "3.hash")
else:
q0path = os.path.join(dest, name, "0." + imgformat)
q1path = os.path.join(dest, name, "1." + imgformat)
q2path = os.path.join(dest, name, "2." + imgformat)
q3path = os.path.join(dest, name, "3." + imgformat)
q0hash = os.path.join(dest, name, "0.hash")
q1hash = os.path.join(dest, name, "1.hash")
q2hash = os.path.join(dest, name, "2.hash")
q3hash = os.path.join(dest, name, "3.hash")
# Check which ones exist
if not os.path.exists(q0hash):
q0path = None
q0hash = None
if not os.path.exists(q1hash):
q1path = None
q1hash = None
if not os.path.exists(q2hash):
q2path = None
q2hash = None
if not os.path.exists(q3hash):
q3path = None
q3hash = None
# do they all not exist?
if not (q0path or q1path or q2path or q3path):
if os.path.exists(imgpath):
os.unlink(imgpath)
if os.path.exists(hashpath):
os.unlink(hashpath)
return
# Now check the hashes
hasher = hashlib.md5()
if q0hash:
hasher.update(open(q0hash, "rb").read())
if q1hash:
hasher.update(open(q1hash, "rb").read())
if q2hash:
hasher.update(open(q2hash, "rb").read())
if q3hash:
hasher.update(open(q3hash, "rb").read())
if os.path.exists(hashpath):
oldhash = open(hashpath, "rb").read()
else:
oldhash = None
newhash = hasher.digest()
if newhash == oldhash:
# Nothing to do
return
# Create the actual image now
img = Image.new("RGBA", (384, 384), (38,92,255,0))
# we'll use paste (NOT alpha_over) for quadtree generation because
# this is just straight image stitching, not alpha blending
if q0path:
try:
quad0 = Image.open(q0path).resize((192,192), Image.ANTIALIAS)
img.paste(quad0, (0,0))
except Exception, e:
logging.warning("Couldn't open %s. It may be corrupt, you may need to delete it. %s", q0path, e)
if q1path:
try:
quad1 = Image.open(q1path).resize((192,192), Image.ANTIALIAS)
img.paste(quad1, (192,0))
except Exception, e:
logging.warning("Couldn't open %s. It may be corrupt, you may need to delete it. %s", q1path, e)
if q2path:
try:
quad2 = Image.open(q2path).resize((192,192), Image.ANTIALIAS)
img.paste(quad2, (0, 192))
except Exception, e:
logging.warning("Couldn't open %s. It may be corrupt, you may need to delete it. %s", q2path, e)
if q3path:
try:
quad3 = Image.open(q3path).resize((192,192), Image.ANTIALIAS)
img.paste(quad3, (192, 192))
except Exception, e:
logging.warning("Couldn't open %s. It may be corrupt, you may need to delete it. %s", q3path, e)
# Save it
if imgformat == 'jpg':
img.save(imgpath, quality=95, subsampling=0)
else: # png
img.save(imgpath)
if optimizeimg:
optimize_image(imgpath, imgformat, optimizeimg)
with open(hashpath, "wb") as hashout:
hashout.write(newhash)
@catch_keyboardinterrupt
def render_worldtile(chunks, colstart, colend, rowstart, rowend, path, imgformat, optimizeimg):
"""Renders just the specified chunks into a tile and save it. Unlike usual
python conventions, rowend and colend are inclusive. Additionally, the
chunks around the edges are half-way cut off (so that neighboring tiles
will render the other half)
chunks is a list of (col, row, filename) of chunk images that are relevant
to this call
The image is saved to path+".ext" and a hash is saved to path+".hash"
If there are no chunks, this tile is not saved (if it already exists, it is
deleted)
If the hash file already exists, it is checked against the hash of each chunk.
Standard tile size has colend-colstart=2 and rowend-rowstart=4
There is no return value
"""
# width of one chunk is 384. Each column is half a chunk wide. The total
# width is (384 + 192*(numcols-1)) since the first column contributes full
# width, and each additional one contributes half since they're staggered.
# However, since we want to cut off half a chunk at each end (384 less
# pixels) and since (colend - colstart + 1) is the number of columns
# inclusive, the equation simplifies to:
width = 192 * (colend - colstart)
# Same deal with height
height = 96 * (rowend - rowstart)
# The standard tile size is 3 columns by 5 rows, which works out to 384x384
# pixels for 8 total chunks. (Since the chunks are staggered but the grid
# is not, some grid coordinates do not address chunks) The two chunks on
# the middle column are shown in full, the two chunks in the middle row are
# half cut off, and the four remaining chunks are one quarter shown.
# The above example with cols 0-3 and rows 0-4 has the chunks arranged like this:
# 0,0 2,0
# 1,1
# 0,2 2,2
# 1,3
# 0,4 2,4
# Due to how the tiles fit together, we may need to render chunks way above
# this (since very few chunks actually touch the top of the sky, some tiles
# way above this one are possibly visible in this tile). Render them
# anyways just in case). "chunks" should include up to rowstart-16
# Before we render any tiles, check the hash of each image in this tile to
# see if it's changed.
hashpath = path + ".hash"
imgpath = path + "." + imgformat
if not chunks:
# No chunks were found in this tile
if os.path.exists(imgpath):
os.unlink(imgpath)
if os.path.exists(hashpath):
os.unlink(hashpath)
return None
# Create the directory if not exists
dirdest = os.path.dirname(path)
if not os.path.exists(dirdest):
try:
os.makedirs(dirdest)
except OSError, e:
# Ignore errno EEXIST: file exists. Since this is multithreaded,
# two processes could conceivably try and create the same directory
# at the same time.
import errno
if e.errno != errno.EEXIST:
raise
imghash = hashlib.md5()
for col, row, chunkfile in chunks:
# Get the hash of this image and add it to our hash for this tile
imghash.update(
os.path.basename(chunkfile).split(".")[4]
)
digest = imghash.digest()
if os.path.exists(hashpath):
oldhash = open(hashpath, 'rb').read()
else:
oldhash = None
if digest == oldhash:
# All the chunks for this tile have not changed according to the hash
return
# Compile this image
tileimg = Image.new("RGBA", (width, height), (38,92,255,0))
# col colstart will get drawn on the image starting at x coordinates -(384/2)
# row rowstart will get drawn on the image starting at y coordinates -(192/2)
for col, row, chunkfile in chunks:
try:
chunkimg = Image.open(chunkfile)
chunkimg.load()
except Exception, e:
# If for some reason the chunk failed to load (perhaps a previous
# run was canceled and the file was only written half way,
# corrupting it), then this could error.
# Since we have no easy way of determining how this chunk was
# generated, we need to just ignore it.
#logging.warning("Could not open chunk '{0}' ({1})".format(chunkfile,e))
try:
# Remove the file so that the next run will re-generate it.
os.unlink(chunkfile)
except OSError, e:
import errno
# Ignore if file doesn't exist, another task could have already
# removed it.
if e.errno != errno.ENOENT:
logging.warning("Could not remove chunk '{0}'!".format(chunkfile))
raise
else:
logging.warning("Removed the corrupt file")
#logging.warning("You will need to re-run the Overviewer to fix this chunk")
continue
xpos = -192 + (col-colstart)*192
ypos = -96 + (row-rowstart)*96
composite.alpha_over(tileimg, chunkimg.convert("RGB"), (xpos, ypos), chunkimg)
# Save them
tileimg.save(imgpath)
if optimizeimg:
optimize_image(imgpath, imgformat, optimizeimg)
with open(hashpath, "wb") as hashout:
hashout.write(digest)
class FakeResult(object):
def __init__(self, res):
self.res = res
def get(self):
return self.res
class FakePool(object):
"""A fake pool used to render things in sync. Implements a subset of
multiprocessing.Pool"""
def apply_async(self, func, args=(), kwargs=None):
if not kwargs:
kwargs = {}
result = func(*args, **kwargs)
return FakeResult(result)
def close(self):
pass
def join(self):
pass