0

refactored c extensions into one

This commit is contained in:
Aaron Griffith
2011-03-08 17:43:50 -05:00
parent 7555bcf1af
commit 07dd219d20
8 changed files with 123 additions and 268 deletions

10
.gitignore vendored
View File

@@ -7,11 +7,11 @@ cachedir*
ImPlatform.h
Imaging.h
# various forms of compiled _composite extensions
_composite.so
_composite.pyd
_composite_d.pyd
_composite.dylib
# various forms of compiled c_overviewer extensions
c_overviewer.so
c_overviewer.pyd
c_overviewer_d.pyd
c_overviewer.dylib
# Mac OS X noise
.DS_Store

View File

@@ -25,7 +25,7 @@ import nbt
import textures
import world
import composite
import _iterate
import c_overviewer
"""
This module has routines related to rendering one particular chunk into an
@@ -545,7 +545,7 @@ class ChunkRenderer(object):
if not img:
img = Image.new("RGBA", (384, 1728), (38,92,255,0))
_iterate.render_loop(self, img, xoff, yoff, blockData_expanded)
c_overviewer.render_loop(self, img, xoff, yoff, blockData_expanded)
for entity in tileEntities:
if entity['id'] == 'Sign':

View File

@@ -25,7 +25,7 @@ alpha-over extension cannot be found.
extension_alpha_over = None
try:
from _composite import alpha_over as _extension_alpha_over
from c_overviewer import alpha_over as _extension_alpha_over
extension_alpha_over = _extension_alpha_over
except ImportError:
pass

View File

@@ -32,11 +32,11 @@ if py2exe != None:
setup_kwargs['options']['py2exe'] = {'bundle_files' : 1, 'excludes': 'Tkinter'}
#
# _composite.c extension
# c_overviewer extension
#
setup_kwargs['ext_modules'].append(Extension('_composite', ['src/composite.c'], include_dirs=['.'], extra_link_args=["/MANIFEST"] if platform.system() == "Windows" else []))
setup_kwargs['ext_modules'].append(Extension('_iterate', ['src/iterate.c'], include_dirs=['.'], extra_link_args=["/MANIFEST"] if platform.system() == "Windows" else []))
c_overviewer_files = ['src/main.c', 'src/composite.c', 'src/iterate.c']
setup_kwargs['ext_modules'].append(Extension('c_overviewer', c_overviewer_files, 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}
@@ -52,7 +52,7 @@ class CustomClean(clean):
# try to remove '_composite.{so,pyd,...}' extension,
# regardless of the current system's extension name convention
build_ext = self.get_finalized_command('build_ext')
pretty_fname = build_ext.get_ext_filename('_composite')
pretty_fname = build_ext.get_ext_filename('c_overviewer')
fname = pretty_fname
if os.path.exists(fname):
try:

View File

@@ -22,8 +22,7 @@
* PIL paste if this extension is not found.
*/
#include <Python.h>
#include <Imaging.h>
#include "overviewer.h"
/* like (a * b + 127) / 255), but much faster on most platforms
from PIL's _imaging.c */
@@ -36,7 +35,7 @@ typedef struct
Imaging image;
} ImagingObject;
static Imaging imaging_python_to_c(PyObject* obj)
Imaging imaging_python_to_c(PyObject* obj)
{
PyObject* im;
Imaging image;
@@ -59,26 +58,22 @@ static Imaging imaging_python_to_c(PyObject* obj)
return image;
}
static PyObject* _composite_alpha_over(PyObject* self, PyObject* args)
/* the alpha_over function, in a form that can be called from C */
/* if xsize, ysize are negative, they are instead set to the size of the image in src */
/* returns NULL on error, dest on success. You do NOT need to decref the return! */
PyObject* alpha_over(PyObject* dest, PyObject* src, PyObject* mask, int dx, int dy, int xsize, int ysize)
{
/* raw input python variables */
PyObject* dest, * src, * pos, * 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;
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);
@@ -119,11 +114,11 @@ static PyObject* _composite_alpha_over(PyObject* self, PyObject* args)
/* how many bytes to skip to get to the next alpha byte */
mask_stride = imMask->pixelsize;
/* destination position read */
if (!PyArg_ParseTuple(pos, "iiii", &dx, &dy, &xsize, &ysize))
/* handle negative/zero sizes appropriately */
if (xsize <= 0 || ysize <= 0)
{
PyErr_SetString(PyExc_TypeError, "given blend destination rect is not valid");
return NULL;
xsize = imSrc->xsize;
ysize = imSrc->ysize;
}
/* set up the source position, size and destination position */
@@ -158,7 +153,6 @@ static PyObject* _composite_alpha_over(PyObject* self, PyObject* args)
if (xsize <= 0 || ysize <= 0)
{
/* nothing to do, return */
Py_INCREF(dest);
return dest;
}
@@ -208,17 +202,39 @@ static PyObject* _composite_alpha_over(PyObject* self, PyObject* args)
}
}
Py_INCREF(dest);
return dest;
}
static PyMethodDef _CompositeMethods[] =
/* wraps alpha_over so it can be called directly from python */
/* properly refs the return value when needed: you DO need to decref the return */
PyObject* alpha_over_wrap(PyObject* self, PyObject* args)
{
{"alpha_over", _composite_alpha_over, METH_VARARGS, "alpha over composite function"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC init_composite(void)
{
(void) Py_InitModule("_composite", _CompositeMethods);
/* raw input python variables */
PyObject* dest, * src, * pos, * mask;
/* destination position and size */
int dx, dy, xsize, ysize;
if (!PyArg_ParseTuple(args, "OOOO", &dest, &src, &pos, &mask))
return NULL;
/* destination position read */
if (!PyArg_ParseTuple(pos, "iiii", &dx, &dy, &xsize, &ysize))
{
/* try again, but this time try to read a point */
xsize = 0;
ysize = 0;
if (!PyArg_ParseTuple(pos, "ii", &dx, &dy))
{
PyErr_SetString(PyExc_TypeError, "given blend destination rect is not valid");
return NULL;
}
}
PyObject* ret = alpha_over(dest, src, mask, dx, dy, xsize, ysize);
if (ret == dest)
{
/* Python needs us to own our return value */
Py_INCREF(dest);
}
return ret;
}

View File

@@ -1,19 +1,6 @@
#include <Python.h>
#include "overviewer.h"
#include <numpy/arrayobject.h>
#include <Imaging.h>
/* 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) ) ]
@@ -24,213 +11,22 @@ static inline int isTransparent(unsigned char b) {
}
static Imaging imaging_python_to_c(PyObject* obj)
// helper to handle alpha_over calls involving a texture tuple
static inline PyObject* texture_alpha_over(PyObject* dest, PyObject* t, int imgx, int imgy)
{
PyObject* im;
Imaging image;
/* first, get the 'im' attribute */
im = PyObject_GetAttrString(obj, "im");
if (!im)
return NULL;
PyObject* src, * mask;
/* 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;
src = PyTuple_GET_ITEM(t, 0);
mask = PyTuple_GET_ITEM(t, 1);
if (mask == Py_None) {
mask = src;
}
image = ((ImagingObject*)im)->image;
Py_DECREF(im);
return image;
return alpha_over(dest, src, mask, imgx, imgy, 0, 0);
}
// 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*
PyObject*
chunk_render(PyObject *self, PyObject *args) {
PyObject *chunk;
@@ -324,7 +120,7 @@ chunk_render(PyObject *self, PyObject *args) {
// note that this version of alpha_over has a different signature than the
// version in _composite.c
alpha_over(img, t, imgx, imgy );
texture_alpha_over(img, t, imgx, imgy );
} else {
// this should be a pointer to a unsigned char
@@ -342,7 +138,7 @@ chunk_render(PyObject *self, PyObject *args) {
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 );
texture_alpha_over(img, t, imgx, imgy );
imgy -= 12;
continue;
}
@@ -357,17 +153,3 @@ chunk_render(PyObject *self, PyObject *args) {
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
}

18
src/main.c Normal file
View File

@@ -0,0 +1,18 @@
#include "overviewer.h"
#include <numpy/arrayobject.h>
static PyMethodDef COverviewerMethods[] = {
{"alpha_over", alpha_over_wrap, METH_VARARGS,
"alpha over composite function"},
{"render_loop", chunk_render, METH_VARARGS,
"Renders stuffs"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initc_overviewer(void)
{
(void) Py_InitModule("c_overviewer", COverviewerMethods);
import_array(); // for numpy
}

39
src/overviewer.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* 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/>.
*/
/*
* This is a general include file for the Overviewer C extension. It
* lists useful, defined functions as well as those that are exported
* to python, so all files can use them.
*/
#ifndef __OVERVIEWER_H_INCLUDED__
#define __OVERVIEWER_H_INCLUDED__
/* Python and PIL headers */
#include <Python.h>
#include <Imaging.h>
/* in composite.c */
Imaging imaging_python_to_c(PyObject* obj);
PyObject* alpha_over(PyObject* dest, PyObject* src, PyObject* mask, int dx, int dy, int xsize, int ysize);
PyObject* alpha_over_wrap(PyObject* self, PyObject* args);
/* in iterate.c */
PyObject* chunk_render(PyObject *self, PyObject *args);
#endif /* __OVERVIEWER_H_INCLUDED__ */