diff --git a/chunk.py b/chunk.py index 4bd5b75..47ab960 100644 --- a/chunk.py +++ b/chunk.py @@ -25,6 +25,7 @@ import nbt import textures import world import composite +import _iterate """ This module has routines related to rendering one particular chunk into an @@ -557,201 +558,7 @@ class ChunkRenderer(object): if not img: img = Image.new("RGBA", (384, 1728), (38,92,255,0)) - for x,y,z,imgx,imgy in iterate_chunkblocks(xoff,yoff): - # make sure we're drawing within the image boundaries - # 24 == approximate width, height of one block - if imgx >= img.size[0] + 24 or imgx <= -24: - continue - if imgy >= img.size[1] + 24 or imgy <= -24: - continue - - blockid = blocks[x,y,z] - - # the following blocks don't have textures that can be pre-computed from the blockid - # alone. additional data is required. - # TODO torches, redstone torches, crops, ladders, stairs, - # levers, doors, buttons, and signs all need to be handled here (and in textures.py) - - ## minecart track, crops, ladder, doors, etc. - if blockid in textures.special_blocks: - # also handle furnaces here, since one side has a different texture than the other - ancilData = blockData_expanded[x,y,z] - try: - if blockid in pseudo_ancildata_blocks: - - pseudo_ancilData = self.generate_pseudo_ancildata(x,y,z,blockid) - ancilData = pseudo_ancilData - t = textures.specialblockmap[(blockid, ancilData)] - except KeyError: - t = None - - else: - t = textures.blockmap[blockid] - - if not t: - continue - - if self.world.useBiomeData: - if blockid == 2: #grass - index = biomeColorData[ ((startY*16)+y) * 128 + (startX*16) + x] - c = textures.grasscolor[index] - - # only tint the top texture - t = textures.prepareGrassTexture(c) - elif blockid == 18: # leaves - index = biomeColorData[ ((startY*16)+y) * 128 + (startX*16) + x] - c = textures.foliagecolor[index] - - t = textures.prepareLeafTexture(c) - - - - - # Check if this block is occluded - if cave and ( - x == 0 and y != 15 and z != 127 - ): - # If it's on the x face, only render if there's a - # transparent block in the y+1 direction OR the z-1 - # direction - if ( - blocks[x,y+1,z] not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - ): - continue - elif cave and ( - y == 15 and x != 0 and z != 127 - ): - # If it's on the facing y face, only render if there's - # a transparent block in the x-1 direction OR the z-1 - # direction - if ( - blocks[x-1,y,z] not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - ): - continue - elif cave and ( - y == 15 and x == 0 and z != 127 - ): - # If it's on the facing edge, only render if what's - # above it is transparent - if ( - blocks[x,y,z+1] not in transparent_blocks - ): - continue - elif (left_blocks == None and right_blocks == None): - # Normal block or not cave mode, check sides for - # transparentcy or render if it's a border chunk. - - if ( - x != 0 and y != 15 and z != 127 and - blocks[x-1,y,z] not in transparent_blocks and - blocks[x,y+1,z] not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - ): - continue - - elif (left_blocks != None and right_blocks == None): - - if ( - # If it has the left face covered check for - # transparent blocks in left face - y != 15 and z != 127 and - (left_blocks[15,y,z] if x == 0 else blocks[x - 1,y,z]) not in transparent_blocks and - blocks[x,y+1,z] not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - ): - continue - - elif (left_blocks == None and right_blocks != None): - - if ( - # If it has the right face covered check for - # transparent blocks in right face - x != 0 and z != 127 and - blocks[x-1,y,z] not in transparent_blocks and - (right_blocks[x,0,z] if y == 15 else blocks[x,y + 1,z]) not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - ): - continue - - elif ( - # If it's a interior chunk check for transparent blocks - # in the adjacent chunks. - z != 127 and - (left_blocks[15,y,z] if x == 0 else blocks[x - 1,y,z]) not in transparent_blocks and - (right_blocks[x,0,z] if y == 15 else blocks[x,y + 1,z]) not in transparent_blocks and - blocks[x,y,z+1] not in transparent_blocks - # Don't render if all sides aren't transparent - ): - continue - - # Draw the actual block on the image. For cave images, - # tint the block with a color proportional to its depth - if cave: - # no lighting for cave -- depth is probably more useful - composite.alpha_over(img, Image.blend(t[0],depth_colors[z],0.3), (imgx, imgy), t[1]) - else: - if not self.quadtree.lighting: - # no lighting at all - composite.alpha_over(img, t[0], (imgx, imgy), t[1]) - elif blockid in transparent_blocks: - # transparent means draw the whole - # block shaded with the current - # block's light - black_coeff, _ = self.get_lighting_coefficient(x, y, z) - if self.quadtree.spawn and black_coeff > 0.8 and blockid in solid_blocks and not ( - blockid in nospawn_blocks or ( - z != 127 and (blocks[x,y,z+1] in solid_blocks or blocks[x,y,z+1] in fluid_blocks) - ) - ): - composite.alpha_over(img, Image.blend(t[0], red_color, black_coeff), (imgx, imgy), t[1]) - else: - composite.alpha_over(img, Image.blend(t[0], black_color, black_coeff), (imgx, imgy), t[1]) - else: - # draw each face lit appropriately, - # but first just draw the block - composite.alpha_over(img, t[0], (imgx, imgy), t[1]) - - # top face - black_coeff, face_occlude = self.get_lighting_coefficient(x, y, z + 1) - # Use red instead of black for spawnable blocks - if self.quadtree.spawn and black_coeff > 0.8 and blockid in solid_blocks and not ( - blockid in nospawn_blocks or ( - z != 127 and (blocks[x,y,z+1] in solid_blocks or blocks[x,y,z+1] in fluid_blocks) - ) - ): - over_color = red_color - else: - over_color = black_color - - if not face_occlude: - composite.alpha_over(img, over_color, (imgx, imgy), ImageEnhance.Brightness(facemasks[0]).enhance(black_coeff)) - - # left face - black_coeff, face_occlude = self.get_lighting_coefficient(x - 1, y, z) - if not face_occlude: - composite.alpha_over(img, over_color, (imgx, imgy), ImageEnhance.Brightness(facemasks[1]).enhance(black_coeff)) - - # right face - black_coeff, face_occlude = self.get_lighting_coefficient(x, y + 1, z) - if not face_occlude: - composite.alpha_over(img, over_color, (imgx, imgy), ImageEnhance.Brightness(facemasks[2]).enhance(black_coeff)) - - # Draw edge lines - if blockid in (44,): # step block - increment = 6 - elif blockid in (78,): # snow - increment = 9 - else: - increment = 0 - - if blockid not in transparent_blocks or blockid in (78,): #special case snow so the outline is still drawn - draw = ImageDraw.Draw(img) - if x != 15 and blocks[x+1,y,z] == 0: - draw.line(((imgx+12,imgy+increment), (imgx+22,imgy+5+increment)), fill=(0,0,0), width=1) - if y != 0 and blocks[x,y-1,z] == 0: - draw.line(((imgx,imgy+6+increment), (imgx+12,imgy+increment)), fill=(0,0,0), width=1) + _iterate.render_loop(self, img, xoff, yoff, blockData_expanded) for entity in tileEntities: diff --git a/iterate.c b/iterate.c new file mode 100644 index 0000000..9405bf5 --- /dev/null +++ b/iterate.c @@ -0,0 +1,373 @@ +#include + +#include +#include + + +/* like (a * b + 127) / 255), but much faster on most platforms + from PIL's _imaging.c */ +#define MULDIV255(a, b, tmp) \ + (tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8)) + +typedef struct +{ + PyObject_HEAD + Imaging image; +} ImagingObject; + +// macro for getting blockID from a chunk of memory +#define getBlock(blockThing, x,y,z) blockThing[ y + ( z * 128 + ( x * 128 * 16) ) ] + +static inline int isTransparent(unsigned char b) { + // TODO expand this to include all transparent blocks + return b == 0; + +} + + +static Imaging imaging_python_to_c(PyObject* obj) +{ + PyObject* im; + Imaging image; + + /* first, get the 'im' attribute */ + im = PyObject_GetAttrString(obj, "im"); + if (!im) + return NULL; + + /* make sure 'im' is the right type */ + if (strcmp(im->ob_type->tp_name, "ImagingCore") != 0) + { + /* it's not -- raise an error and exit */ + PyErr_SetString(PyExc_TypeError, "image attribute 'im' is not a core Imaging type"); + return NULL; + } + + image = ((ImagingObject*)im)->image; + Py_DECREF(im); + return image; +} + + + +// TODO refact iterate.c and _composite.c so share implementations +static PyObject* alpha_over(PyObject* dest, PyObject* t, int imgx, int imgy) +{ + /* raw input python variables */ + PyObject * src, * mask; + /* libImaging handles */ + Imaging imDest, imSrc, imMask; + /* cached blend properties */ + int src_has_alpha, mask_offset, mask_stride; + /* destination position and size */ + int dx, dy, xsize, ysize; + /* source position */ + int sx, sy; + /* iteration variables */ + unsigned int x, y, i; + /* temporary calculation variables */ + int tmp1, tmp2, tmp3; + + src = PyTuple_GET_ITEM(t, 0); + mask = PyTuple_GET_ITEM(t, 1); + if (mask == Py_None) { + printf("mask is none\n"); + Py_INCREF(mask); + mask = src; + } + //if (!PyArg_ParseTuple(args, "OOOO", &dest, &src, &pos, &mask)) + // return NULL; + + imDest = imaging_python_to_c(dest); + imSrc = imaging_python_to_c(src); + imMask = imaging_python_to_c(mask); + + //printf("alpha1\n"); + if (!imDest || !imSrc || !imMask) { + PyErr_SetString(PyExc_ValueError, "dest, src, or mask is missing"); + return NULL; + } + //printf("alpha2\n"); + + /* check the various image modes, make sure they make sense */ + if (strcmp(imDest->mode, "RGBA") != 0) + { + PyErr_SetString(PyExc_ValueError, "given destination image does not have mode \"RGBA\""); + return NULL; + } + + if (strcmp(imSrc->mode, "RGBA") != 0 && strcmp(imSrc->mode, "RGB") != 0) + { + PyErr_SetString(PyExc_ValueError, "given source image does not have mode \"RGBA\" or \"RGB\""); + return NULL; + } + + if (strcmp(imMask->mode, "RGBA") != 0 && strcmp(imMask->mode, "L") != 0) + { + PyErr_SetString(PyExc_ValueError, "given mask image does not have mode \"RGBA\" or \"L\""); + return NULL; + } + + /* make sure mask size matches src size */ + if (imSrc->xsize != imMask->xsize || imSrc->ysize != imMask->ysize) + { + PyErr_SetString(PyExc_ValueError, "mask and source image sizes do not match"); + return NULL; + } + + //printf("alpha3\n"); + /* set up flags for the src/mask type */ + src_has_alpha = (imSrc->pixelsize == 4 ? 1 : 0); + /* how far into image the first alpha byte resides */ + mask_offset = (imMask->pixelsize == 4 ? 3 : 0); + /* how many bytes to skip to get to the next alpha byte */ + mask_stride = imMask->pixelsize; + + //printf("alpha4\n"); + /* destination position read */ + //if (!PyArg_ParseTuple(pos, "iiii", &dx, &dy, &xsize, &ysize)) + //{ + // PyErr_SetString(PyExc_TypeError, "given blend destination rect is not valid"); + // return NULL; + //} + dx = imgx; + dy = imgy; + + xsize = imSrc->xsize; + ysize = imSrc->ysize; + //printf("xsize/ysize %d/%d\n", xsize, ysize); + + //printf("alpha5\n"); + + /* set up the source position, size and destination position */ + /* handle negative dest pos */ + if (dx < 0) + { + sx = -dx; + dx = 0; + } else { + sx = 0; + } + + if (dy < 0) + { + sy = -dy; + dy = 0; + } else { + sy = 0; + } + + /* set up source dimensions */ + xsize -= sx; + ysize -= sy; + + //printf("imDest->xsize=%d imDest->yize=%d\n", imDest->xsize, imDest->ysize); + + /* clip dimensions, if needed */ + if (dx + xsize > imDest->xsize) + xsize = imDest->xsize - dx; + if (dy + ysize > imDest->ysize) + ysize = imDest->ysize - dy; + + /* check that there remains any blending to be done */ + if (xsize <= 0 || ysize <= 0) + { + /* nothing to do, return */ + Py_INCREF(dest); + return dest; + } + + for (y = 0; y < ysize; y++) + { + UINT8* out = (UINT8*) imDest->image[dy + y] + dx*4; + UINT8* outmask = (UINT8*) imDest->image[dy + y] + dx*4 + 3; + UINT8* in = (UINT8*) imSrc->image[sy + y] + sx*(imSrc->pixelsize); + UINT8* inmask = (UINT8*) imMask->image[sy + y] + sx*mask_stride + mask_offset; + + for (x = 0; x < xsize; x++) + { + /* special cases */ + if (*inmask == 255 || *outmask == 0) + { + *outmask = *inmask; + + *out = *in; + out++, in++; + *out = *in; + out++, in++; + *out = *in; + out++, in++; + } else if (*inmask == 0) { + /* do nothing -- source is fully transparent */ + out += 3; + in += 3; + } else { + /* general case */ + int alpha = *inmask + MULDIV255(*outmask, 255 - *inmask, tmp1); + for (i = 0; i < 3; i++) + { + /* general case */ + *out = MULDIV255(*in, *inmask, tmp1) + MULDIV255(MULDIV255(*out, *outmask, tmp2), 255 - *inmask, tmp3); + *out = (*out * 255) / alpha; + out++, in++; + } + + *outmask = alpha; + } + + out++; + if (src_has_alpha) + in++; + outmask += 4; + inmask += mask_stride; + } + } + + Py_INCREF(dest); + return dest; +} + + + +// TODO triple check this to make sure reference counting is correct +static PyObject* +chunk_render(PyObject *self, PyObject *args) { + + PyObject *chunk; + PyObject *blockdata_expanded; + int xoff, yoff; + PyObject *img; + + if (!PyArg_ParseTuple(args, "OOiiO", &chunk, &img, &xoff, &yoff, &blockdata_expanded)) + return Py_BuildValue("i", "-1"); + + // tuple + PyObject *imgsize = PyObject_GetAttrString(img, "size"); + + PyObject *imgsize0_py = PySequence_GetItem(imgsize, 0); + PyObject *imgsize1_py = PySequence_GetItem(imgsize, 1); + Py_DECREF(imgsize); + + int imgsize0 = PyInt_AsLong(imgsize0_py); + int imgsize1 = PyInt_AsLong(imgsize1_py); + Py_DECREF(imgsize0_py); + Py_DECREF(imgsize1_py); + + + // get the block data directly from numpy: + PyObject *blocks_py = PyObject_GetAttrString(chunk, "blocks"); + char *blocks = PyArray_BYTES(blocks_py); + Py_DECREF(blocks_py); + + //PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks"); + //PyObject *right_blocks = PyObject_GetAttrString(chunk, "right_blocks"); + //PyObject *transparent_blocks = PyObject_GetAttrString(chunk, "transparent_blocks"); + + PyObject *textures = PyImport_ImportModule("textures"); + + // TODO can these be global static? these don't change during program execution + PyObject *blockmap = PyObject_GetAttrString(textures, "blockmap"); + PyObject *special_blocks = PyObject_GetAttrString(textures, "special_blocks"); + PyObject *specialblockmap = PyObject_GetAttrString(textures, "specialblockmap"); + + Py_DECREF(textures); + + //printf("render_loop\n"); + + int imgx, imgy; + int x, y, z; + for (x = 15; x > -1; x--) { + for (y = 0; y < 16; y++) { + imgx = xoff + x*12 + y*12; + imgy = yoff - x*6 + y*6 + 1632; // 1632 == 128*12 + 16*12//2 + for (z = 0; z < 128; z++) { + //printf("c/imgx/%d\n", imgx); + //printf("c/imgy/%d\n", imgy); + if ((imgx >= imgsize0 + 24) || (imgx <= -24)) { + imgy -= 12; // NOTE: we need to do this at every continue + continue; + } + if ((imgy >= imgsize1 + 24) || (imgy <= -24)) { + imgy -= 12; + continue; + } + + // get blockid + unsigned char block = getBlock(blocks, x, z, y); // Note the order: x,z,y + if (block == 0) { + imgy -= 12; + continue; + } + //printf("checking blockid %hhu\n", block); + PyObject *blockid = PyInt_FromLong(block); // TODO figure out how to DECREF this easily, instead at every 'continue'. + + + if ( (x != 0) && (y != 15) && (z != 127) && + !isTransparent(getBlock(blocks, x-1, z, y)) && + !isTransparent(getBlock(blocks, x, z+1, y)) && + !isTransparent(getBlock(blocks, x, z, y+1)) ) { + imgy -= 12; + continue; + } + + + if (!PySequence_Contains(special_blocks, blockid)) { + //t = textures.blockmap[blockid] + PyObject *t = PyList_GetItem(blockmap, block); + // PyList_GetItem returns borrowed ref + if (t == Py_None) { + printf("t == Py_None. blockid=%d\n", block); + imgy -= 12; + continue; + + } + + // note that this version of alpha_over has a different signature than the + // version in _composite.c + alpha_over(img, t, imgx, imgy ); + + } else { + // this should be a pointer to a unsigned char + void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, x, y, z); + unsigned char ancilData = *((unsigned char*)ancilData_p); + if (block == 85) { // fence. skip the generate_pseudo_ancildata for now + imgy -= 12; + continue; + } + PyObject *tmp = PyTuple_New(2); + + Py_INCREF(blockid); // because SetItem steals + PyTuple_SetItem(tmp, 0, blockid); + PyTuple_SetItem(tmp, 1, PyInt_FromLong(ancilData)); + PyObject *t = PyDict_GetItem(specialblockmap, tmp); // this is a borrowed reference. no need to decref + Py_DECREF(tmp); + if (t != NULL) + alpha_over(img, t, imgx, imgy ); + imgy -= 12; + continue; + } + imgy -= 12; + } + } + } + + Py_DECREF(blockmap); + Py_DECREF(special_blocks); + Py_DECREF(specialblockmap); + + return Py_BuildValue("i",2); +} + +static PyMethodDef IterateMethods[] = { + {"render_loop", chunk_render, METH_VARARGS, + "Renders stuffs"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + + +PyMODINIT_FUNC +init_iterate(void) +{ + (void) Py_InitModule("_iterate", IterateMethods); + import_array(); // for numpy +} diff --git a/setup.py b/setup.py index 55ee8bb..44c19af 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ if py2exe != None: # setup_kwargs['ext_modules'].append(Extension('_composite', ['_composite.c'], include_dirs=['.'], extra_link_args=["/MANIFEST"] if platform.system() == "Windows" else [])) +setup_kwargs['ext_modules'].append(Extension('_iterate', ['iterate.c'], include_dirs=['.'], extra_link_args=["/MANIFEST"] if platform.system() == "Windows" else [])) # tell build_ext to build the extension in-place # (NOT in build/) setup_kwargs['options']['build_ext'] = {'inplace' : 1}