0

fixed up new C code to be (mostly) C89-compliant

*mostly*, because the "inline" keyword isn't C standard until C99, but
I'm leaving it in because most compilers support it and it's really
handy. If it becomes an issue, we can deal with it later.
This commit is contained in:
Aaron Griffith
2011-03-17 23:04:41 -04:00
parent b20e881c5e
commit 6941bc3378
3 changed files with 65 additions and 42 deletions

View File

@@ -30,7 +30,7 @@
(tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8))
typedef struct {
PyObject_HEAD;
PyObject_HEAD
Imaging image;
} ImagingObject;
@@ -214,6 +214,8 @@ alpha_over_wrap(PyObject *self, PyObject *args)
PyObject *dest, *src, *pos, *mask;
/* destination position and size */
int dx, dy, xsize, ysize;
/* return value: dest image on success */
PyObject *ret;
if (!PyArg_ParseTuple(args, "OOOO", &dest, &src, &pos, &mask))
return NULL;
@@ -230,7 +232,7 @@ alpha_over_wrap(PyObject *self, PyObject *args)
}
}
PyObject *ret = alpha_over(dest, src, mask, dx, dy, xsize, ysize);
ret = alpha_over(dest, src, mask, dx, dy, xsize, ysize);
if (ret == dest) {
/* Python needs us to own our return value */
Py_INCREF(dest);

View File

@@ -19,16 +19,16 @@
#include <numpy/arrayobject.h>
// macro for getting blockID from a chunk of memory
/* 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
/* TODO expand this to include all transparent blocks */
return b == 0;
}
// helper to handle alpha_over calls involving a texture tuple
/* helper to handle alpha_over calls involving a texture tuple */
static inline PyObject *
texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy)
{
@@ -43,7 +43,7 @@ texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy)
return alpha_over(dest, src, mask, imgx, imgy, 0, 0);
}
// TODO triple check this to make sure reference counting is correct
/* TODO triple check this to make sure reference counting is correct */
PyObject*
chunk_render(PyObject *self, PyObject *args) {
@@ -52,50 +52,62 @@ chunk_render(PyObject *self, PyObject *args) {
int xoff, yoff;
PyObject *img;
PyObject *imgsize, *imgsize0_py, *imgsize1_py;
int imgsize0, imgsize1;
PyObject *blocks_py;
char *blocks;
PyObject *textures, *blockmap, *special_blocks, *specialblockmap;
int imgx, imgy;
int x, y, z;
if (!PyArg_ParseTuple(args, "OOiiO", &chunk, &img, &xoff, &yoff, &blockdata_expanded))
return Py_BuildValue("i", "-1");
// tuple
PyObject *imgsize = PyObject_GetAttrString(img, "size");
/* tuple */
imgsize = PyObject_GetAttrString(img, "size");
PyObject *imgsize0_py = PySequence_GetItem(imgsize, 0);
PyObject *imgsize1_py = PySequence_GetItem(imgsize, 1);
imgsize0_py = PySequence_GetItem(imgsize, 0);
imgsize1_py = PySequence_GetItem(imgsize, 1);
Py_DECREF(imgsize);
int imgsize0 = PyInt_AsLong(imgsize0_py);
int imgsize1 = PyInt_AsLong(imgsize1_py);
imgsize0 = PyInt_AsLong(imgsize0_py);
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);
/* get the block data directly from numpy: */
blocks_py = PyObject_GetAttrString(chunk, "blocks");
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 *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");
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");
/* TODO can these be global static? these don't change during program execution */
blockmap = PyObject_GetAttrString(textures, "blockmap");
special_blocks = PyObject_GetAttrString(textures, "special_blocks");
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;
/* 128*12 -- offset for z direction, 15*6 -- offset for x */
imgy = yoff - x*6 + y*6 + 128*12 + 15*6;
for (z = 0; z < 128; z++) {
unsigned char block;
PyObject *blockid;
imgy -= 12;
if ((imgx >= imgsize0 + 24) || (imgx <= -24)) {
@@ -105,13 +117,16 @@ chunk_render(PyObject *self, PyObject *args) {
continue;
}
// get blockid
unsigned char block = getBlock(blocks, x, z, y); // Note the order: x,z,y
/* get blockid
note the order: x, z, y */
block = getBlock(blocks, x, z, y);
if (block == 0) {
continue;
}
//printf("checking blockid %hhu\n", block);
PyObject *blockid = PyInt_FromLong(block); // TODO figure out how to DECREF this easily, instead at every 'continue'.
/* TODO figure out how to DECREF this easily, instead of at
every continue */
blockid = PyInt_FromLong(block);
if ( (x != 0) && (y != 15) && (z != 127) &&
@@ -123,32 +138,37 @@ chunk_render(PyObject *self, PyObject *args) {
if (!PySequence_Contains(special_blocks, blockid)) {
//t = textures.blockmap[blockid]
/* t = textures.blockmap[blockid] */
PyObject *t = PyList_GetItem(blockmap, block);
// PyList_GetItem returns borrowed ref
/* PyList_GetItem returns borrowed ref */
if (t == Py_None) {
printf("t == Py_None. blockid=%d\n", block);
continue;
}
// note that this version of alpha_over has a different signature than the
// version in _composite.c
/* note that this version of alpha_over has a different signature than the
version in _composite.c */
texture_alpha_over(img, t, imgx, imgy );
} else {
// this should be a pointer to a unsigned char
PyObject *tmp, *t;
/* 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
if (block == 85) {
/* fence. skip the generate_pseudo_ancildata for now */
continue;
}
PyObject *tmp = PyTuple_New(2);
Py_INCREF(blockid); // because SetItem steals
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
/* this is a borrowed reference. no need to decref */
t = PyDict_GetItem(specialblockmap, tmp);
Py_DECREF(tmp);
if (t != NULL)
texture_alpha_over(img, t, imgx, imgy );

View File

@@ -31,5 +31,6 @@ PyMODINIT_FUNC
initc_overviewer(void)
{
(void)Py_InitModule("c_overviewer", COverviewerMethods);
import_array(); // for numpy
/* for numpy */
import_array();
}