0

no longer validates images, runs MUCH faster to scan existing chunks.

If a chunk image can't be loaded, it re-generates it on the fly.
This commit is contained in:
Andrew Brown
2010-09-12 01:04:31 -04:00
parent 55d596fc26
commit a3a4877e6a
3 changed files with 38 additions and 24 deletions

View File

@@ -60,17 +60,6 @@ def render_and_save(chunkfile, cave=False):
# propagate the exception back to the parent process.
raise Exception()
def valid_image(filename):
"""Returns true if the file is valid, false if it can't be loaded (is
corrupt or something)
"""
try:
img = Image.open(filename)
img.load()
except Exception, e:
return False
return True
class ChunkRenderer(object):
def __init__(self, chunkfile):
if not os.path.exists(chunkfile):
@@ -140,11 +129,7 @@ class ChunkRenderer(object):
if os.path.getmtime(self.chunkfile) < os.path.getmtime(oldimg_path):
# chunkfile is older than the image, don't even bother checking
# the hash
if valid_image(oldimg_path):
return oldimg_path
else:
os.unlink(oldimg_path)
oldimg = None
return oldimg_path
# Reasons for the code to get to this point:
# 1) An old image doesn't exist
@@ -163,7 +148,7 @@ class ChunkRenderer(object):
dest_path = os.path.join(destdir, dest_filename)
if oldimg:
if dest_filename == oldimg and valid_image(dest_path):
if dest_filename == oldimg:
# There is an existing file, the chunk has a newer mtime, but the
# hashes match.
return dest_path
@@ -175,7 +160,11 @@ class ChunkRenderer(object):
# Render the chunk
img = self.chunk_render(cave=cave)
# Save it
img.save(dest_path)
try:
img.save(dest_path)
except:
os.unlink(dest_path)
raise
# Return its location
return dest_path

View File

@@ -43,7 +43,9 @@ def main():
if i > 0:
if 1000 % i == 0 or i % 1000 == 0:
print "{0}/{1} chunks rendered".format(i, len(chunks))
results['pool'].join()
print "Writing out html file"
if not os.path.exists(destdir):
os.mkdir(destdir)

View File

@@ -299,7 +299,22 @@ def render_worldtile(chunkmap, colstart, colend, rowstart, rowend, oldhash):
# 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 tilelist:
chunkimg = Image.open(chunkfile)
try:
chunkimg = Image.open(chunkfile)
except IOError, e:
print "Error opening file", chunkfile
print "Attempting to re-generate it"
os.unlink(chunkfile)
# Do some string manipulation to determine what the chunk file is
# that goes with this image. Then call chunk.render_and_save
dirname, imagename = os.path.split(chunkfile)
parts = imagename.split(".")
datafile = "c.{0}.{1}.dat".format(parts[1],parts[2])
print "Chunk came from data file", datafile
# XXX Don't forget to set cave mode here when it gets implemented!
chunk.render_and_save(os.path.join(dirname, datafile), False)
chunkimg = Image.open(chunkfile)
print "Success"
xpos = -192 + (col-colstart)*192
ypos = -96 + (row-rowstart)*96
@@ -635,13 +650,21 @@ class ReturnableProcess(multiprocessing.Process):
multiprocessing.Process.__init__(self, *args, **kwargs)
def run(self):
results = self._target(*self._args, **self._kwargs)
self._respipe_in.send(results)
self.__sem.release()
try:
results = self._target(*self._args, **self._kwargs)
except BaseException, e:
self._respipe_in.send(e)
else:
self._respipe_in.send(results)
finally:
self.__sem.release()
def get(self):
self.join()
return self._respipe_out.recv()
ret = self._respipe_out.recv()
if isinstance(ret, BaseException):
raise ret
return ret
def start(self):
self._respipe_out, self._respipe_in = multiprocessing.Pipe()