0

Merge branch 'master' into rendermode-options

This commit is contained in:
Aaron Griffith
2011-07-12 12:17:25 -04:00
48 changed files with 368 additions and 73 deletions

902
overviewer_core/src/Draw.c Normal file
View File

@@ -0,0 +1,902 @@
/*
* The Python Imaging Library.
* $Id$
*
* a simple drawing package for the Imaging library
*
* history:
* 1996-04-13 fl Created.
* 1996-04-30 fl Added transforms and polygon support.
* 1996-08-12 fl Added filled polygons.
* 1996-11-05 fl Fixed float/int confusion in polygon filler
* 1997-07-04 fl Support 32-bit images (C++ would have been nice)
* 1998-09-09 fl Eliminated qsort casts; improved rectangle clipping
* 1998-09-10 fl Fixed fill rectangle to include lower edge (!)
* 1998-12-29 fl Added arc, chord, and pieslice primitives
* 1999-01-10 fl Added some level 2 ("arrow") stuff (experimental)
* 1999-02-06 fl Added bitmap primitive
* 1999-07-26 fl Eliminated a compiler warning
* 1999-07-31 fl Pass ink as void* instead of int
* 2002-12-10 fl Added experimental RGBA-on-RGB drawing
* 2004-09-04 fl Support simple wide lines (no joins)
* 2005-05-25 fl Fixed line width calculation
* 2011-04-01 Modified for use in Minecraft-Overviewer
*
* Copyright (c) 1996-2006 by Fredrik Lundh
* Copyright (c) 1997-2006 by Secret Labs AB.
*
* This file is part of the Python Imaging Library
*
* By obtaining, using, and/or copying this software and/or its associated
* documentation, you agree that you have read, understood, and will comply
* with the following terms and conditions:
*
* Permission to use, copy, modify, and distribute this software and its
* associated documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies, and that
* both that copyright notice and this permission notice appear in supporting
* documentation, and that the name of Secret Labs AB or the author not be used
* in advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
/* FIXME: support fill/outline attribute for all filled shapes */
/* FIXME: support zero-winding fill */
/* FIXME: add drawing context, support affine transforms */
/* FIXME: support clip window (and mask?) */
#include "Imaging.h"
#include <math.h>
#define CEIL(v) (int) ceil(v)
#define FLOOR(v) ((v) >= 0.0 ? (int) (v) : (int) floor(v))
#define INK8(ink) (*(UINT8*)ink)
#define INK32(ink) (*(INT32*)ink)
/* like (a * b + 127) / 255), but much faster on most platforms */
#define MULDIV255(a, b, tmp)\
(tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8))
#define BLEND(mask, in1, in2, tmp1, tmp2)\
(MULDIV255(in1, 255 - mask, tmp1) + MULDIV255(in2, mask, tmp2))
/* -------------------------------------------------------------------- */
/* Primitives */
/* -------------------------------------------------------------------- */
typedef struct {
/* edge descriptor for polygon engine */
int d;
int x0, y0;
int xmin, ymin, xmax, ymax;
float dx;
} Edge;
static inline void
point8(Imaging im, int x, int y, int ink)
{
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize)
im->image8[y][x] = (UINT8) ink;
}
static inline void
point32(Imaging im, int x, int y, int ink)
{
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize)
im->image32[y][x] = ink;
}
static inline void
point32rgba(Imaging im, int x, int y, int ink)
{
unsigned int tmp1, tmp2;
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) {
UINT8* out = (UINT8*) im->image[y]+x*4;
UINT8* in = (UINT8*) &ink;
out[0] = BLEND(in[3], out[0], in[0], tmp1, tmp2);
out[1] = BLEND(in[3], out[1], in[1], tmp1, tmp2);
out[2] = BLEND(in[3], out[2], in[2], tmp1, tmp2);
}
}
static inline void
hline8(Imaging im, int x0, int y0, int x1, int ink)
{
int tmp;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1)
tmp = x0, x0 = x1, x1 = tmp;
if (x0 < 0)
x0 = 0;
else if (x0 >= im->xsize)
return;
if (x1 < 0)
return;
else if (x1 >= im->xsize)
x1 = im->xsize-1;
if (x0 <= x1)
memset(im->image8[y0] + x0, (UINT8) ink, x1 - x0 + 1);
}
}
static inline void
hline32(Imaging im, int x0, int y0, int x1, int ink)
{
int tmp;
INT32* p;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1)
tmp = x0, x0 = x1, x1 = tmp;
if (x0 < 0)
x0 = 0;
else if (x0 >= im->xsize)
return;
if (x1 < 0)
return;
else if (x1 >= im->xsize)
x1 = im->xsize-1;
p = im->image32[y0];
while (x0 <= x1)
p[x0++] = ink;
}
}
static inline void
hline32rgba(Imaging im, int x0, int y0, int x1, int ink)
{
int tmp;
unsigned int tmp1, tmp2;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1)
tmp = x0, x0 = x1, x1 = tmp;
if (x0 < 0)
x0 = 0;
else if (x0 >= im->xsize)
return;
if (x1 < 0)
return;
else if (x1 >= im->xsize)
x1 = im->xsize-1;
if (x0 <= x1) {
UINT8* out = (UINT8*) im->image[y0]+x0*4;
UINT8* in = (UINT8*) &ink;
while (x0 <= x1) {
out[0] = BLEND(in[3], out[0], in[0], tmp1, tmp2);
out[1] = BLEND(in[3], out[1], in[1], tmp1, tmp2);
out[2] = BLEND(in[3], out[2], in[2], tmp1, tmp2);
x0++; out += 4;
}
}
}
}
static inline void
line8(Imaging im, int x0, int y0, int x1, int y1, int ink)
{
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1-x0;
if (dx < 0)
dx = -dx, xs = -1;
else
xs = 1;
dy = y1-y0;
if (dy < 0)
dy = -dy, ys = -1;
else
ys = 1;
n = (dx > dy) ? dx : dy;
if (dx == 0)
/* vertical */
for (i = 0; i < dy; i++) {
point8(im, x0, y0, ink);
y0 += ys;
}
else if (dy == 0)
/* horizontal */
for (i = 0; i < dx; i++) {
point8(im, x0, y0, ink);
x0 += xs;
}
else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point8(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point8(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static inline void
line32(Imaging im, int x0, int y0, int x1, int y1, int ink)
{
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1-x0;
if (dx < 0)
dx = -dx, xs = -1;
else
xs = 1;
dy = y1-y0;
if (dy < 0)
dy = -dy, ys = -1;
else
ys = 1;
n = (dx > dy) ? dx : dy;
if (dx == 0)
/* vertical */
for (i = 0; i < dy; i++) {
point32(im, x0, y0, ink);
y0 += ys;
}
else if (dy == 0)
/* horizontal */
for (i = 0; i < dx; i++) {
point32(im, x0, y0, ink);
x0 += xs;
}
else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point32(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point32(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static inline void
line32rgba(Imaging im, int x0, int y0, int x1, int y1, int ink)
{
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1-x0;
if (dx < 0)
dx = -dx, xs = -1;
else
xs = 1;
dy = y1-y0;
if (dy < 0)
dy = -dy, ys = -1;
else
ys = 1;
n = (dx > dy) ? dx : dy;
if (dx == 0)
/* vertical */
for (i = 0; i < dy; i++) {
point32rgba(im, x0, y0, ink);
y0 += ys;
}
else if (dy == 0)
/* horizontal */
for (i = 0; i < dx; i++) {
point32rgba(im, x0, y0, ink);
x0 += xs;
}
else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point32rgba(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point32rgba(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static int
x_cmp(const void *x0, const void *x1)
{
float diff = *((float*)x0) - *((float*)x1);
if (diff < 0)
return -1;
else if (diff > 0)
return 1;
else
return 0;
}
static inline int
polygon8(Imaging im, int n, Edge *e, int ink, int eofill)
{
int i, j;
float *xx;
int ymin, ymax;
float y;
if (n <= 0)
return 0;
/* Find upper and lower polygon boundary (within image) */
ymin = e[0].ymin;
ymax = e[0].ymax;
for (i = 1; i < n; i++) {
if (e[i].ymin < ymin) ymin = e[i].ymin;
if (e[i].ymax > ymax) ymax = e[i].ymax;
}
if (ymin < 0)
ymin = 0;
if (ymax >= im->ysize)
ymax = im->ysize-1;
/* Process polygon edges */
xx = malloc(n * sizeof(float));
if (!xx)
return -1;
for (;ymin <= ymax; ymin++) {
y = ymin+0.5F;
for (i = j = 0; i < n; i++)
if (y >= e[i].ymin && y <= e[i].ymax) {
if (e[i].d == 0)
hline8(im, e[i].xmin, ymin, e[i].xmax, ink);
else
xx[j++] = (y-e[i].y0) * e[i].dx + e[i].x0;
}
if (j == 2) {
if (xx[0] < xx[1])
hline8(im, CEIL(xx[0]-0.5), ymin, FLOOR(xx[1]+0.5), ink);
else
hline8(im, CEIL(xx[1]-0.5), ymin, FLOOR(xx[0]+0.5), ink);
} else {
qsort(xx, j, sizeof(float), x_cmp);
for (i = 0; i < j-1 ; i += 2)
hline8(im, CEIL(xx[i]-0.5), ymin, FLOOR(xx[i+1]+0.5), ink);
}
}
free(xx);
return 0;
}
static inline int
polygon32(Imaging im, int n, Edge *e, int ink, int eofill)
{
int i, j;
float *xx;
int ymin, ymax;
float y;
if (n <= 0)
return 0;
/* Find upper and lower polygon boundary (within image) */
ymin = e[0].ymin;
ymax = e[0].ymax;
for (i = 1; i < n; i++) {
if (e[i].ymin < ymin) ymin = e[i].ymin;
if (e[i].ymax > ymax) ymax = e[i].ymax;
}
if (ymin < 0)
ymin = 0;
if (ymax >= im->ysize)
ymax = im->ysize-1;
/* Process polygon edges */
xx = malloc(n * sizeof(float));
if (!xx)
return -1;
for (;ymin <= ymax; ymin++) {
y = ymin+0.5F;
for (i = j = 0; i < n; i++) {
if (y >= e[i].ymin && y <= e[i].ymax) {
if (e[i].d == 0)
hline32(im, e[i].xmin, ymin, e[i].xmax, ink);
else
xx[j++] = (y-e[i].y0) * e[i].dx + e[i].x0;
}
}
if (j == 2) {
if (xx[0] < xx[1])
hline32(im, CEIL(xx[0]-0.5), ymin, FLOOR(xx[1]+0.5), ink);
else
hline32(im, CEIL(xx[1]-0.5), ymin, FLOOR(xx[0]+0.5), ink);
} else {
qsort(xx, j, sizeof(float), x_cmp);
for (i = 0; i < j-1 ; i += 2)
hline32(im, CEIL(xx[i]-0.5), ymin, FLOOR(xx[i+1]+0.5), ink);
}
}
free(xx);
return 0;
}
static inline int
polygon32rgba(Imaging im, int n, Edge *e, int ink, int eofill)
{
int i, j;
float *xx;
int ymin, ymax;
float y;
if (n <= 0)
return 0;
/* Find upper and lower polygon boundary (within image) */
ymin = e[0].ymin;
ymax = e[0].ymax;
for (i = 1; i < n; i++) {
if (e[i].ymin < ymin) ymin = e[i].ymin;
if (e[i].ymax > ymax) ymax = e[i].ymax;
}
if (ymin < 0)
ymin = 0;
if (ymax >= im->ysize)
ymax = im->ysize-1;
/* Process polygon edges */
xx = malloc(n * sizeof(float));
if (!xx)
return -1;
for (;ymin <= ymax; ymin++) {
y = ymin+0.5F;
for (i = j = 0; i < n; i++) {
if (y >= e[i].ymin && y <= e[i].ymax) {
if (e[i].d == 0)
hline32rgba(im, e[i].xmin, ymin, e[i].xmax, ink);
else
xx[j++] = (y-e[i].y0) * e[i].dx + e[i].x0;
}
}
if (j == 2) {
if (xx[0] < xx[1])
hline32rgba(im, CEIL(xx[0]-0.5), ymin, FLOOR(xx[1]+0.5), ink);
else
hline32rgba(im, CEIL(xx[1]-0.5), ymin, FLOOR(xx[0]+0.5), ink);
} else {
qsort(xx, j, sizeof(float), x_cmp);
for (i = 0; i < j-1 ; i += 2)
hline32rgba(im, CEIL(xx[i]-0.5), ymin, FLOOR(xx[i+1]+0.5), ink);
}
}
free(xx);
return 0;
}
static inline void
add_edge(Edge *e, int x0, int y0, int x1, int y1)
{
/* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */
if (x0 <= x1)
e->xmin = x0, e->xmax = x1;
else
e->xmin = x1, e->xmax = x0;
if (y0 <= y1)
e->ymin = y0, e->ymax = y1;
else
e->ymin = y1, e->ymax = y0;
if (y0 == y1) {
e->d = 0;
e->dx = 0.0;
} else {
e->dx = ((float)(x1-x0)) / (y1-y0);
if (y0 == e->ymin)
e->d = 1;
else
e->d = -1;
}
e->x0 = x0;
e->y0 = y0;
}
typedef struct {
void (*point)(Imaging im, int x, int y, int ink);
void (*hline)(Imaging im, int x0, int y0, int x1, int ink);
void (*line)(Imaging im, int x0, int y0, int x1, int y1, int ink);
int (*polygon)(Imaging im, int n, Edge *e, int ink, int eofill);
} DRAW;
DRAW draw8 = { point8, hline8, line8, polygon8 };
DRAW draw32 = { point32, hline32, line32, polygon32 };
DRAW draw32rgba = { point32rgba, hline32rgba, line32rgba, polygon32rgba };
/* -------------------------------------------------------------------- */
/* Interface */
/* -------------------------------------------------------------------- */
#define DRAWINIT()\
if (im->image8) {\
draw = &draw8;\
ink = INK8(ink_);\
} else {\
draw = (op) ? &draw32rgba : &draw32; \
ink = INK32(ink_);\
}
int
ImagingDrawPoint(Imaging im, int x0, int y0, const void* ink_, int op)
{
DRAW* draw;
INT32 ink;
DRAWINIT();
draw->point(im, x0, y0, ink);
return 0;
}
int
ImagingDrawLine(Imaging im, int x0, int y0, int x1, int y1, const void* ink_,
int op)
{
DRAW* draw;
INT32 ink;
DRAWINIT();
draw->line(im, x0, y0, x1, y1, ink);
return 0;
}
int
ImagingDrawWideLine(Imaging im, int x0, int y0, int x1, int y1,
const void* ink_, int width, int op)
{
DRAW* draw;
INT32 ink;
Edge e[4];
int dx, dy;
double d;
DRAWINIT();
if (width <= 1) {
draw->line(im, x0, y0, x1, y1, ink);
return 0;
}
dx = x1-x0;
dy = y1-y0;
if (dx == 0 && dy == 0) {
draw->point(im, x0, y0, ink);
return 0;
}
d = width / sqrt((float) (dx*dx + dy*dy)) / 2.0;
dx = (int) floor(d * (y1-y0) + 0.5);
dy = (int) floor(d * (x1-x0) + 0.5);
add_edge(e+0, x0 - dx, y0 + dy, x1 - dx, y1 + dy);
add_edge(e+1, x1 - dx, y1 + dy, x1 + dx, y1 - dy);
add_edge(e+2, x1 + dx, y1 - dy, x0 + dx, y0 - dy);
add_edge(e+3, x0 + dx, y0 - dy, x0 - dx, y0 + dy);
draw->polygon(im, 4, e, ink, 0);
return 0;
}
/* -------------------------------------------------------------------- */
/* standard shapes */
#define ARC 0
#define CHORD 1
#define PIESLICE 2
/* -------------------------------------------------------------------- */
/* experimental level 2 ("arrow") graphics stuff. this implements
portions of the arrow api on top of the Edge structure. the
semantics are ok, except that "curve" flattens the bezier curves by
itself */
#if 1 /* ARROW_GRAPHICS */
struct ImagingOutlineInstance {
float x0, y0;
float x, y;
int count;
Edge *edges;
int size;
};
void
ImagingOutlineDelete(ImagingOutline outline)
{
if (!outline)
return;
if (outline->edges)
free(outline->edges);
free(outline);
}
static Edge*
allocate(ImagingOutline outline, int extra)
{
Edge* e;
if (outline->count + extra > outline->size) {
/* expand outline buffer */
outline->size += extra + 25;
if (!outline->edges)
e = malloc(outline->size * sizeof(Edge));
else
e = realloc(outline->edges, outline->size * sizeof(Edge));
if (!e)
return NULL;
outline->edges = e;
}
e = outline->edges + outline->count;
outline->count += extra;
return e;
}
int
ImagingOutlineMove(ImagingOutline outline, float x0, float y0)
{
outline->x = outline->x0 = x0;
outline->y = outline->y0 = y0;
return 0;
}
int
ImagingOutlineLine(ImagingOutline outline, float x1, float y1)
{
Edge* e;
e = allocate(outline, 1);
if (!e)
return -1; /* out of memory */
add_edge(e, (int) outline->x, (int) outline->y, (int) x1, (int) y1);
outline->x = x1;
outline->y = y1;
return 0;
}
int
ImagingOutlineCurve(ImagingOutline outline, float x1, float y1,
float x2, float y2, float x3, float y3)
{
Edge* e;
int i;
float xo, yo;
#define STEPS 32
e = allocate(outline, STEPS);
if (!e)
return -1; /* out of memory */
xo = outline->x;
yo = outline->y;
/* flatten the bezier segment */
for (i = 1; i <= STEPS; i++) {
float t = ((float) i) / STEPS;
float t2 = t*t;
float t3 = t2*t;
float u = 1.0F - t;
float u2 = u*u;
float u3 = u2*u;
float x = outline->x*u3 + 3*(x1*t*u2 + x2*t2*u) + x3*t3 + 0.5;
float y = outline->y*u3 + 3*(y1*t*u2 + y2*t2*u) + y3*t3 + 0.5;
add_edge(e++, xo, yo, (int) x, (int) y);
xo = x, yo = y;
}
outline->x = xo;
outline->y = yo;
return 0;
}
int
ImagingOutlineCurve2(ImagingOutline outline, float cx, float cy,
float x3, float y3)
{
/* add bezier curve based on three control points (as
in the Flash file format) */
return ImagingOutlineCurve(
outline,
(outline->x + cx + cx)/3, (outline->y + cy + cy)/3,
(cx + cx + x3)/3, (cy + cy + y3)/3,
x3, y3);
}
int
ImagingOutlineClose(ImagingOutline outline)
{
if (outline->x == outline->x0 && outline->y == outline->y0)
return 0;
return ImagingOutlineLine(outline, outline->x0, outline->y0);
}
int
ImagingDrawOutline(Imaging im, ImagingOutline outline, const void* ink_,
int fill, int op)
{
DRAW* draw;
INT32 ink;
DRAWINIT();
draw->polygon(im, outline->count, outline->edges, ink, 0);
return 0;
}
#endif

View File

@@ -0,0 +1,360 @@
/*
* 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 file implements a custom alpha_over function for (some) PIL
* images. It's designed to be used through composite.py, which
* includes a proxy alpha_over function that falls back to the default
* PIL paste if this extension is not found.
*/
#include "overviewer.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;
inline 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;
}
/* helper function to setup s{x,y}, d{x,y}, and {x,y}size variables
in these composite functions -- even handles auto-sizing to src! */
static inline void
setup_source_destination(Imaging src, Imaging dest,
int *sx, int *sy, int *dx, int *dy, int *xsize, int *ysize)
{
/* handle negative/zero sizes appropriately */
if (*xsize <= 0 || *ysize <= 0) {
*xsize = src->xsize;
*ysize = src->ysize;
}
/* 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;
/* clip dimensions, if needed */
if (*dx + *xsize > dest->xsize)
*xsize = dest->xsize - *dx;
if (*dy + *ysize > dest->ysize)
*ysize = dest->ysize - *dy;
}
/* convenience alpha_over with 1.0 as overall_alpha */
inline PyObject* alpha_over(PyObject *dest, PyObject *src, PyObject *mask,
int dx, int dy, int xsize, int ysize) {
return alpha_over_full(dest, src, mask, 1.0f, dx, dy, xsize, ysize);
}
/* the full alpha_over function, in a form that can be called from C
* overall_alpha is multiplied with the whole mask, useful for lighting...
* 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!
*/
inline PyObject *
alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alpha,
int dx, int dy, int xsize, int ysize) {
/* libImaging handles */
Imaging imDest, imSrc, imMask;
/* cached blend properties */
int src_has_alpha, mask_offset, mask_stride;
/* source position */
int sx, sy;
/* iteration variables */
unsigned int x, y, i;
/* temporary calculation variables */
int tmp1, tmp2, tmp3;
/* integer [0, 255] version of overall_alpha */
UINT8 overall_alpha_int = 255 * overall_alpha;
/* short-circuit this whole thing if overall_alpha is zero */
if (overall_alpha_int == 0)
return dest;
imDest = imaging_python_to_c(dest);
imSrc = imaging_python_to_c(src);
imMask = imaging_python_to_c(mask);
if (!imDest || !imSrc || !imMask)
return NULL;
/* 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;
}
/* 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;
/* setup source & destination vars */
setup_source_destination(imSrc, imDest, &sx, &sy, &dx, &dy, &xsize, &ysize);
/* check that there remains any blending to be done */
if (xsize <= 0 || ysize <= 0) {
/* nothing to do, return */
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++) {
UINT8 in_alpha;
/* apply overall_alpha */
if (overall_alpha_int != 255 && *inmask != 0) {
in_alpha = MULDIV255(*inmask, overall_alpha_int, tmp1);
} else {
in_alpha = *inmask;
}
/* special cases */
if (in_alpha == 255 || *outmask == 0) {
*outmask = in_alpha;
*out = *in;
out++, in++;
*out = *in;
out++, in++;
*out = *in;
out++, in++;
} else if (in_alpha == 0) {
/* do nothing -- source is fully transparent */
out += 3;
in += 3;
} else {
/* general case */
int alpha = in_alpha + MULDIV255(*outmask, 255 - in_alpha, tmp1);
for (i = 0; i < 3; i++) {
/* general case */
*out = MULDIV255(*in, in_alpha, tmp1) +
MULDIV255(MULDIV255(*out, *outmask, tmp2), 255 - in_alpha, tmp3);
*out = (*out * 255) / alpha;
out++, in++;
}
*outmask = alpha;
}
out++;
if (src_has_alpha)
in++;
outmask += 4;
inmask += mask_stride;
}
}
return dest;
}
/* 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)
{
/* raw input python variables */
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;
/* destination position read */
if (!PyArg_ParseTuple(pos, "iiii", &dx, &dy, &xsize, &ysize)) {
/* try again, but this time try to read a point */
PyErr_Clear();
xsize = 0;
ysize = 0;
if (!PyArg_ParseTuple(pos, "ii", &dx, &dy)) {
PyErr_SetString(PyExc_TypeError,
"given blend destination rect is not valid");
return NULL;
}
}
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;
}
/* like alpha_over, but instead of src image it takes a source color
* also, it multiplies instead of doing an over operation
*/
PyObject *
tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
unsigned char sb, unsigned char sa,
PyObject *mask, int dx, int dy, int xsize, int ysize) {
/* libImaging handles */
Imaging imDest, imMask;
/* cached blend properties */
int mask_offset, mask_stride;
/* source position */
int sx, sy;
/* iteration variables */
unsigned int x, y;
/* temporary calculation variables */
int tmp1, tmp2;
imDest = imaging_python_to_c(dest);
imMask = imaging_python_to_c(mask);
if (!imDest || !imMask)
return NULL;
/* 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(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;
}
/* 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;
/* setup source & destination vars */
setup_source_destination(imMask, imDest, &sx, &sy, &dx, &dy, &xsize, &ysize);
/* check that there remains any blending to be done */
if (xsize <= 0 || ysize <= 0) {
/* nothing to do, return */
return dest;
}
for (y = 0; y < ysize; y++) {
UINT8 *out = (UINT8 *)imDest->image[dy + y] + dx * 4;
UINT8 *inmask = (UINT8 *)imMask->image[sy + y] + sx * mask_stride + mask_offset;
for (x = 0; x < xsize; x++) {
/* special cases */
if (*inmask == 255) {
*out = MULDIV255(*out, sr, tmp1);
out++;
*out = MULDIV255(*out, sg, tmp1);
out++;
*out = MULDIV255(*out, sb, tmp1);
out++;
*out = MULDIV255(*out, sa, tmp1);
out++;
} else if (*inmask == 0) {
/* do nothing -- source is fully transparent */
out += 4;
} else {
/* general case */
/* TODO work out general case */
*out = MULDIV255(*out, (255 - *inmask) + MULDIV255(sr, *inmask, tmp1), tmp2);
out++;
*out = MULDIV255(*out, (255 - *inmask) + MULDIV255(sg, *inmask, tmp1), tmp2);
out++;
*out = MULDIV255(*out, (255 - *inmask) + MULDIV255(sb, *inmask, tmp1), tmp2);
out++;
*out = MULDIV255(*out, (255 - *inmask) + MULDIV255(sa, *inmask, tmp1), tmp2);
out++;
}
inmask += mask_stride;
}
}
return dest;
}

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/>.
*/
/* simple routines for dealing with endian conversion */
#define UNKNOWN_ENDIAN 0
#define BIG_ENDIAN 1
#define LITTLE_ENDIAN 2
static int endianness = UNKNOWN_ENDIAN;
void init_endian(void) {
/* figure out what our endianness is! */
short word = 0x0001;
char* byte = (char*)(&word);
endianness = byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN;
}
unsigned short big_endian_ushort(unsigned short in) {
return (endianness == LITTLE_ENDIAN) ? ((in >> 8) | (in << 8)) : in;
}
unsigned int big_endian_uint(unsigned int in) {
return (endianness == LITTLE_ENDIAN) ? (((in & 0x000000FF) << 24) + ((in & 0x0000FF00) << 8) + ((in & 0x00FF0000) >> 8) + ((in & 0xFF000000) >> 24)) : in;
}

View File

@@ -0,0 +1,450 @@
/*
* 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/>.
*/
#include "overviewer.h"
static PyObject *textures = NULL;
static PyObject *chunk_mod = NULL;
static PyObject *blockmap = NULL;
static PyObject *special_blocks = NULL;
static PyObject *specialblockmap = NULL;
static PyObject *transparent_blocks = NULL;
PyObject *init_chunk_render(PyObject *self, PyObject *args) {
/* this function only needs to be called once, anything more should be
* ignored */
if (blockmap) {
Py_RETURN_NONE;
}
textures = PyImport_ImportModule("overviewer_core.textures");
/* ensure none of these pointers are NULL */
if ((!textures)) {
return NULL;
}
chunk_mod = PyImport_ImportModule("overviewer_core.chunk");
/* ensure none of these pointers are NULL */
if ((!chunk_mod)) {
return NULL;
}
blockmap = PyObject_GetAttrString(textures, "blockmap");
if (!blockmap)
return NULL;
special_blocks = PyObject_GetAttrString(textures, "special_blocks");
if (!special_blocks)
return NULL;
specialblockmap = PyObject_GetAttrString(textures, "specialblockmap");
if (!specialblockmap)
return NULL;
transparent_blocks = PyObject_GetAttrString(chunk_mod, "transparent_blocks");
if (!transparent_blocks)
return NULL;
Py_RETURN_NONE;
}
int
is_transparent(unsigned char b) {
PyObject *block = PyInt_FromLong(b);
int ret = PySequence_Contains(transparent_blocks, block);
Py_DECREF(block);
return ret;
}
unsigned char
check_adjacent_blocks(RenderState *state, int x,int y,int z, unsigned char blockid) {
/*
* Generates a pseudo ancillary data for blocks that depend of
* what are surrounded and don't have ancillary data. This
* function is through generate_pseudo_data.
*
* This uses a binary number of 4 digits to encode the info.
* The encode is:
*
* 0b1234:
* Bit: 1 2 3 4
* Side: +x +y -x -y
* Values: bit = 0 -> The corresponding side block has different blockid
* bit = 1 -> The corresponding side block has same blockid
* Example: if the bit1 is 1 that means that there is a block with
* blockid in the side of the +x direction.
*/
unsigned char pdata=0;
if (state->x == 15) { /* +x direction */
if (state->up_right_blocks != Py_None) { /* just in case we are in the end of the world */
if (getArrayByte3D(state->up_right_blocks, 0, y, z) == blockid) {
pdata = pdata|(1 << 3);
}
}
} else {
if (getArrayByte3D(state->blocks, x + 1, y, z) == blockid) {
pdata = pdata|(1 << 3);
}
}
if (state->y == 15) { /* +y direction*/
if (state->right_blocks != Py_None) {
if (getArrayByte3D(state->right_blocks, x, 0, z) == blockid) {
pdata = pdata|(1 << 2);
}
}
} else {
if (getArrayByte3D(state->blocks, x, y + 1, z) == blockid) {
pdata = pdata|(1 << 2);
}
}
if (state->x == 0) { /* -x direction*/
if (state->left_blocks != Py_None) {
if (getArrayByte3D(state->left_blocks, 15, y, z) == blockid) {
pdata = pdata|(1 << 1);
}
}
} else {
if (getArrayByte3D(state->blocks, x - 1, y, z) == blockid) {
pdata = pdata|(1 << 1);
}
}
if (state->y == 0) { /* -y direction */
if (state->up_left_blocks != Py_None) {
if (getArrayByte3D(state->up_left_blocks, x, 15, z) == blockid) {
pdata = pdata|(1 << 0);
}
}
} else {
if (getArrayByte3D(state->blocks, x, y - 1, z) == blockid) {
pdata = pdata|(1 << 0);
}
}
return pdata;
}
unsigned char
generate_pseudo_data(RenderState *state, unsigned char ancilData) {
/*
* Generates a fake ancillary data for blocks that are drawn
* depending on what are surrounded.
*/
int x = state->x, y = state->y, z = state->z;
unsigned char data = 0;
if (state->block == 2) { /* grass */
/* return 0x10 if grass is covered in snow */
if (z < 127 && getArrayByte3D(state->blocks, x, y, z+1) == 78)
return 0x10;
return ancilData;
} else if (state->block == 9) { /* water */
/* an aditional bit for top is added to the 4 bits of check_adjacent_blocks */
if (ancilData == 0) { /* static water */
if ((z != 127) && (getArrayByte3D(state->blocks, x, y, z+1) == 9)) {
data = 0;
} else {
data = 16;
}
return data; /* = 0b10000 */
} else if ((ancilData > 0) && (ancilData < 8)) { /* flowing water */
data = (check_adjacent_blocks(state, x, y, z, state->block) ^ 0x0f) | 0x10;
return data;
} else if (ancilData >= 8) { /* falling water */
data = (check_adjacent_blocks(state, x, y, z, state->block) ^ 0x0f);
return data;
}
} else if (state->block == 85) { /* fences */
return check_adjacent_blocks(state, x, y, z, state->block);
} else if (state->block == 55) { /* redstone */
/* three addiotional bit are added, one for on/off state, and
* another two for going-up redstone wire in the same block
* (connection with the level z+1) */
unsigned char above_level_data = 0, same_level_data = 0, below_level_data = 0, possibly_connected = 0, final_data = 0;
/* check for air in z+1, no air = no connection with upper level */
if ((z != 127) && (getArrayByte3D(state->left_blocks, x, y, z) == 0)) {
above_level_data = check_adjacent_blocks(state, x, y, z + 1, state->block);
} /* else above_level_data = 0 */
/* check connection with same level */
same_level_data = check_adjacent_blocks(state, x, y, z, 55);
/* check the posibility of connection with z-1 level, check for air */
possibly_connected = check_adjacent_blocks(state, x, y, z, 0);
/* check connection with z-1 level */
if (z != 0) {
below_level_data = check_adjacent_blocks(state, x, y, z - 1, state->block);
} /* else below_level_data = 0 */
final_data = above_level_data | same_level_data | (below_level_data & possibly_connected);
/* add the three bits */
if (ancilData > 0) { /* powered redstone wire */
final_data = final_data | 0x40;
}
if ((above_level_data & 0x01)) { /* draw top left going up redstonewire */
final_data = final_data | 0x20;
}
if ((above_level_data & 0x08)) { /* draw top right going up redstonewire */
final_data = final_data | 0x10;
}
return final_data;
} else if (state-> block == 54) { /* chests */
/* the top 2 bits are used to store the type of chest
* (single or double), the 2 bottom bits are used for
* orientation, look textures.py for more information. */
/* if placed alone chests always face west, return 0 to make a
* chest facing west */
unsigned char chest_data = 0, air_data = 0, final_data = 0;
/* search for chests */
chest_data = check_adjacent_blocks(state, x, y, z, 54);
/* search for air */
air_data = check_adjacent_blocks(state, x, y, z, 0);
if (chest_data == 1) { /* another chest in the east */
final_data = final_data | 0x8; /* only can face to north or south */
if ( (air_data & 0x2) == 2 ) {
final_data = final_data | 0x1; /* facing north */
} else {
final_data = final_data | 0x3; /* facing south */
}
} else if (chest_data == 2) { /* in the north */
final_data = final_data | 0x4; /* only can face to east or west */
if ( !((air_data & 0x4) == 4) ) { /* 0 = west */
final_data = final_data | 0x2; /* facing east */
}
} else if (chest_data == 4) { /*in the west */
final_data = final_data | 0x4;
if ( (air_data & 0x2) == 2 ) {
final_data = final_data | 0x1; /* facing north */
} else {
final_data = final_data | 0x3; /* facing south */
}
} else if (chest_data == 8) { /*in the south */
final_data = final_data | 0x8;
if ( !((air_data & 0x4) == 4) ) {
final_data = final_data | 0x2; /* facing east */
}
} else if (chest_data == 0) {
/* Single chest, determine the orientation */
if ( ((air_data & 0x8) == 0) && ((air_data & 0x2) == 2) ) { /* block in +x and no block in -x */
final_data = final_data | 0x1; /* facing north */
} else if ( ((air_data & 0x2) == 0) && ((air_data & 0x8) == 8)) {
final_data = final_data | 0x3;
} else if ( ((air_data & 0x4) == 0) && ((air_data & 0x1) == 1)) {
final_data = final_data | 0x2;
} /* else, facing west, value = 0 */
} else {
/* more than one adjacent chests! render as normal chest */
return 0;
}
return final_data;
} else if (state->block == 90) {
return check_adjacent_blocks(state, x, y, z, state->block);
}
return 0;
}
/* TODO triple check this to make sure reference counting is correct */
PyObject*
chunk_render(PyObject *self, PyObject *args) {
RenderState state;
PyObject *rendermode_py;
int xoff, yoff;
PyObject *imgsize, *imgsize0_py, *imgsize1_py;
int imgsize0, imgsize1;
PyObject *blocks_py;
PyObject *left_blocks_py;
PyObject *right_blocks_py;
PyObject *up_left_blocks_py;
PyObject *up_right_blocks_py;
RenderMode *rendermode;
PyObject *t = NULL;
if (!PyArg_ParseTuple(args, "OOiiO", &state.self, &state.img, &xoff, &yoff, &state.blockdata_expanded))
return NULL;
/* fill in important modules */
state.textures = textures;
state.chunk = chunk_mod;
/* set up the render mode */
rendermode_py = PyObject_GetAttrString(state.self, "rendermode");
state.rendermode = rendermode = render_mode_create(PyString_AsString(rendermode_py), &state);
Py_DECREF(rendermode_py);
if (rendermode == NULL) {
return NULL;
}
/* get the image size */
imgsize = PyObject_GetAttrString(state.img, "size");
imgsize0_py = PySequence_GetItem(imgsize, 0);
imgsize1_py = PySequence_GetItem(imgsize, 1);
Py_DECREF(imgsize);
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: */
blocks_py = PyObject_GetAttrString(state.self, "blocks");
state.blocks = blocks_py;
left_blocks_py = PyObject_GetAttrString(state.self, "left_blocks");
state.left_blocks = left_blocks_py;
right_blocks_py = PyObject_GetAttrString(state.self, "right_blocks");
state.right_blocks = right_blocks_py;
up_left_blocks_py = PyObject_GetAttrString(state.self, "up_left_blocks");
state.up_left_blocks = up_left_blocks_py;
up_right_blocks_py = PyObject_GetAttrString(state.self, "up_right_blocks");
state.up_right_blocks = up_right_blocks_py;
for (state.x = 15; state.x > -1; state.x--) {
for (state.y = 0; state.y < 16; state.y++) {
PyObject *blockid = NULL;
/* set up the render coordinates */
state.imgx = xoff + state.x*12 + state.y*12;
/* 128*12 -- offset for z direction, 15*6 -- offset for x */
state.imgy = yoff - state.x*6 + state.y*6 + 128*12 + 15*6;
for (state.z = 0; state.z < 128; state.z++) {
state.imgy -= 12;
/* get blockid */
state.block = getArrayByte3D(blocks_py, state.x, state.y, state.z);
if (state.block == 0) {
continue;
}
/* make sure we're rendering inside the image boundaries */
if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) {
continue;
}
if ((state.imgy >= imgsize1 + 24) || (state.imgy <= -24)) {
continue;
}
/* decref'd on replacement *and* at the end of the z for block */
if (blockid) {
Py_DECREF(blockid);
}
blockid = PyInt_FromLong(state.block);
// check for occlusion
if (render_mode_occluded(rendermode, state.x, state.y, state.z)) {
continue;
}
// everything stored here will be a borrowed ref
/* get the texture and mask from block type / ancil. data */
if (!PySequence_Contains(special_blocks, blockid)) {
/* t = textures.blockmap[blockid] */
t = PyList_GetItem(blockmap, state.block);
} else {
PyObject *tmp;
unsigned char ancilData = getArrayByte3D(state.blockdata_expanded, state.x, state.y, state.z);
if ((state.block == 85) || (state.block == 9) || (state.block == 55) || (state.block == 54) || (state.block == 2) || (state.block == 90)) {
ancilData = generate_pseudo_data(&state, ancilData);
}
tmp = PyTuple_New(2);
Py_INCREF(blockid); /* because SetItem steals */
PyTuple_SetItem(tmp, 0, blockid);
PyTuple_SetItem(tmp, 1, PyInt_FromLong(ancilData));
/* this is a borrowed reference. no need to decref */
t = PyDict_GetItem(specialblockmap, tmp);
Py_DECREF(tmp);
}
/* if we found a proper texture, render it! */
if (t != NULL && t != Py_None)
{
PyObject *src, *mask, *mask_light;
src = PyTuple_GetItem(t, 0);
mask = PyTuple_GetItem(t, 1);
mask_light = PyTuple_GetItem(t, 2);
if (mask == Py_None)
mask = src;
render_mode_draw(rendermode, src, mask, mask_light);
}
}
if (blockid) {
Py_DECREF(blockid);
blockid = NULL;
}
}
}
/* free up the rendermode info */
render_mode_destroy(rendermode);
Py_DECREF(blocks_py);
Py_XDECREF(left_blocks_py);
Py_XDECREF(right_blocks_py);
Py_XDECREF(up_left_blocks_py);
Py_XDECREF(up_right_blocks_py);
return Py_BuildValue("i",2);
}

View File

@@ -0,0 +1,63 @@
/*
* 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/>.
*/
#include "overviewer.h"
PyObject *get_extension_version(PyObject *self, PyObject *args) {
return Py_BuildValue("i", OVERVIEWER_EXTENSION_VERSION);
}
static PyMethodDef COverviewerMethods[] = {
{"alpha_over", alpha_over_wrap, METH_VARARGS,
"alpha over composite function"},
{"init_chunk_render", init_chunk_render, METH_VARARGS,
"Initializes the stuffs renderer."},
{"render_loop", chunk_render, METH_VARARGS,
"Renders stuffs"},
{"get_render_modes", get_render_modes, METH_VARARGS,
"returns available render modes"},
{"get_render_mode_info", get_render_mode_info, METH_VARARGS,
"returns info for a particular render mode"},
{"get_render_mode_inheritance", get_render_mode_inheritance, METH_VARARGS,
"returns inheritance chain for a particular render mode"},
{"get_render_mode_children", get_render_mode_children, METH_VARARGS,
"returns (direct) children for a particular render mode"},
{"set_render_mode_options", set_render_mode_options, METH_VARARGS,
"sets the default options for a given render mode"},
{"add_custom_render_mode", add_custom_render_mode, METH_VARARGS,
"add a new rendermode derived from an existing mode"},
{"extension_version", get_extension_version, METH_VARARGS,
"Returns the extension version"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initc_overviewer(void)
{
(void)Py_InitModule("c_overviewer", COverviewerMethods);
/* for numpy */
import_array();
init_endian();
}

View File

@@ -0,0 +1,98 @@
/*
* 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__
// increment this value if you've made a change to the c extesion
// and want to force users to rebuild
#define OVERVIEWER_EXTENSION_VERSION 8
/* Python PIL, and numpy headers */
#include <Python.h>
#include <Imaging.h>
#include <numpy/arrayobject.h>
/* macro for getting a value out of various numpy arrays */
#define getArrayByte3D(array, x,y,z) (*(unsigned char *)(PyArray_GETPTR3((array), (x), (y), (z))))
#define getArrayShort1D(array, x) (*(unsigned short *)(PyArray_GETPTR1((array), (x))))
/* generally useful MAX / MIN macros */
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/* 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_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alpha,
int dx, int dy, int xsize, int ysize);
PyObject *alpha_over_wrap(PyObject *self, PyObject *args);
PyObject *tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
unsigned char sb, unsigned char sa,
PyObject *mask, int dx, int dy, int xsize, int ysize);
/* forward declaration of RenderMode object */
typedef struct _RenderMode RenderMode;
/* in iterate.c */
typedef struct {
/* the ChunkRenderer object */
PyObject *self;
/* important modules, for convenience */
PyObject *textures;
PyObject *chunk;
/* the current render mode in use */
RenderMode *rendermode;
/* the rest only make sense for occluded() and draw() !! */
/* the tile image and destination */
PyObject *img;
int imgx, imgy;
/* the block position and type, and the block array */
int x, y, z;
unsigned char block;
PyObject *blockdata_expanded;
PyObject *blocks;
PyObject *up_left_blocks;
PyObject *up_right_blocks;
PyObject *left_blocks;
PyObject *right_blocks;
} RenderState;
PyObject *init_chunk_render(PyObject *self, PyObject *args);
int is_transparent(unsigned char b);
PyObject *chunk_render(PyObject *self, PyObject *args);
/* pull in the rendermode info */
#include "rendermodes.h"
/* in endian.c */
void init_endian(void);
unsigned short big_endian_ushort(unsigned short in);
unsigned int big_endian_uint(unsigned int in);
#endif /* __OVERVIEWER_H_INCLUDED__ */

View File

@@ -0,0 +1,288 @@
/*
* 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/>.
*/
#include "overviewer.h"
#include <math.h>
static inline int
touches_light(unsigned int x, unsigned int y, unsigned int z,
PyObject *light, PyObject *left_light, PyObject *right_light,
PyObject *up_left_light, PyObject *up_right_light) {
if (getArrayByte3D(light, x, y, z+1) != 0) {
return 1;
}
if ((x == 15)) {
if (up_right_light != Py_None) {
if (getArrayByte3D(up_right_light, 0, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x+1, y, z) != 0) {
return 1;
}
}
if (x == 0) {
if (left_light != Py_None) {
if (getArrayByte3D(left_light, 15, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x-1, y, z) != 0) {
return 1;
}
}
if (y == 15) {
if (right_light != Py_None) {
if (getArrayByte3D(right_light, 0, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x, y+1, z) != 0) {
return 1;
}
}
if (y == 0) {
if (up_left_light != Py_None) {
if (getArrayByte3D(up_left_light, 15, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x, y-1, z) != 0) {
return 1;
}
}
return 0;
}
static int
rendermode_cave_occluded(void *data, RenderState *state, int x, int y, int z) {
RenderModeCave* self;
int dz = 0;
self = (RenderModeCave *)data;
/* first, check to see if it's "normally" occluded */
if (rendermode_lighting.occluded(data, state, x, y, z))
return 1;
/* check if the block is touching skylight */
if (z != 127) {
if (touches_light(x, y, z, self->skylight, self->left_skylight, self->right_skylight, self->up_left_skylight, self->up_right_skylight)) {
return 1;
}
if (self->only_lit && !touches_light(x, y, z, self->blocklight, self->left_blocklight, self->right_blocklight, self->up_left_blocklight, self->up_right_blocklight)) {
return 1;
}
/* check for normal occlusion */
/* use ajacent chunks, if not you get blocks spreaded in chunk edges */
if ( (x == 0) && (y != 15) ) {
if (state->left_blocks != Py_None) {
if (!is_transparent(getArrayByte3D(state->left_blocks, 15, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
} else {
return 1;
}
}
if ( (x != 0) && (y == 15) ) {
if (state->right_blocks != Py_None) {
if (!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->right_blocks, x, 0, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1))) {
return 1;
}
} else {
return 1;
}
}
if ( (x == 0) && (y == 15) ) {
if ((state->left_blocks != Py_None) &&
(state->right_blocks != Py_None)) {
if (!is_transparent(getArrayByte3D(state->left_blocks, 15, y, z)) &&
!is_transparent(getArrayByte3D(state->right_blocks, x, 0, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1))) {
return 1;
}
} else {
return 1;
}
}
if ( (x != 0) && (y != 15) &&
!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
} else { /* if z == 127 skip */
return 1;
}
/* check for lakes and seas and don't render them */
/* at this point of the code the block has no skylight
* and is not occluded, but a deep sea can fool these
* 2 tests */
if ((getArrayByte3D(state->blocks, x, y, z) == 9) ||
(getArrayByte3D(state->blocks, x, y, z+1) == 9)) {
for (dz = z+1; dz < 127; dz++) { /* go up and check for skylight */
if (getArrayByte3D(self->skylight, x, y, dz) != 0) {
return 1;
}
if (getArrayByte3D(state->blocks, x, y, dz) != 9) {
/* we are out of the water! and there's no skylight
* , i.e. is a cave lake or something similar */
return 0;
}
}
}
return 0;
}
static int
rendermode_cave_start(void *data, RenderState *state, PyObject *options) {
RenderModeCave* self;
int ret;
self = (RenderModeCave *)data;
/* first, chain up */
ret = rendermode_lighting.start(data, state, options);
if (ret != 0)
return ret;
self->depth_tinting = 1;
if (!render_mode_parse_option(options, "depth_tinting", "i", &(self->depth_tinting)))
return 1;
self->only_lit = 0;
if (!render_mode_parse_option(options, "only_lit", "i", &(self->only_lit)))
return 1;
self->lighting = 0;
if (!render_mode_parse_option(options, "lighting", "i", &(self->lighting)))
return 1;
/* if there's skylight we are in the surface! */
self->skylight = PyObject_GetAttrString(state->self, "skylight");
self->left_skylight = PyObject_GetAttrString(state->self, "left_skylight");
self->right_skylight = PyObject_GetAttrString(state->self, "right_skylight");
self->up_left_skylight = PyObject_GetAttrString(state->self, "up_left_skylight");
self->up_right_skylight = PyObject_GetAttrString(state->self, "up_right_skylight");
if (self->only_lit) {
self->blocklight = PyObject_GetAttrString(state->self, "blocklight");
self->left_blocklight = PyObject_GetAttrString(state->self, "left_blocklight");
self->right_blocklight = PyObject_GetAttrString(state->self, "right_blocklight");
self->up_left_blocklight = PyObject_GetAttrString(state->self, "up_left_blocklight");
self->up_right_blocklight = PyObject_GetAttrString(state->self, "up_right_blocklight");
}
/* colors for tinting */
self->depth_colors = PyObject_GetAttrString(state->chunk, "depth_colors");
return 0;
}
static void
rendermode_cave_finish(void *data, RenderState *state) {
RenderModeCave* self;
self = (RenderModeCave *)data;
Py_DECREF(self->skylight);
Py_DECREF(self->left_skylight);
Py_DECREF(self->right_skylight);
Py_DECREF(self->up_left_skylight);
Py_DECREF(self->up_right_skylight);
if (self->only_lit) {
Py_DECREF(self->blocklight);
Py_DECREF(self->left_blocklight);
Py_DECREF(self->right_blocklight);
Py_DECREF(self->up_left_blocklight);
Py_DECREF(self->up_right_blocklight);
}
Py_DECREF(self->depth_colors);
rendermode_lighting.finish(data, state);
}
static void
rendermode_cave_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
RenderModeCave* self;
int z, r, g, b;
self = (RenderModeCave *)data;
z = state->z;
r = 0, g = 0, b = 0;
/* draw the normal block */
if (self->lighting) {
rendermode_lighting.draw(data, state, src, mask, mask_light);
} else {
rendermode_normal.draw(data, state, src, mask, mask_light);
}
if (self->depth_tinting) {
/* get the colors and tint and tint */
r = PyInt_AsLong(PyList_GetItem(self->depth_colors, 0 + z*3));
g = PyInt_AsLong(PyList_GetItem(self->depth_colors, 1 + z*3));
b = PyInt_AsLong(PyList_GetItem(self->depth_colors, 2 + z*3));
tint_with_mask(state->img, r, g, b, 255, mask, state->imgx, state->imgy, 0, 0);
}
}
const RenderModeOption rendermode_cave_options[] = {
{"depth_tinting", "tint caves based on how deep they are (default: True)"},
{"only_lit", "only render lit caves (default: False)"},
{"lighting", "render caves with lighting enabled (default: False)"},
{NULL, NULL}
};
RenderModeInterface rendermode_cave = {
"cave", "render only caves",
rendermode_cave_options,
&rendermode_lighting,
sizeof(RenderModeCave),
rendermode_cave_start,
rendermode_cave_finish,
rendermode_cave_occluded,
rendermode_cave_draw,
};

View File

@@ -0,0 +1,322 @@
/*
* 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/>.
*/
#include "overviewer.h"
#include <math.h>
/* figures out the black_coeff from a given skylight and blocklight,
used in lighting calculations */
static float calculate_darkness(unsigned char skylight, unsigned char blocklight) {
return 1.0f - powf(0.8f, 15.0 - MAX(blocklight, skylight));
}
/* loads the appropriate light data for the given (possibly non-local)
* coordinates, and returns a black_coeff this is exposed, so other (derived)
* rendermodes can use it
*
* authoratative is a return slot for whether or not this lighting calculation
* is true, or a guess. If we guessed, *authoratative will be false, but if it
* was calculated correctly from available light data, it will be true. You
* may (and probably should) pass NULL.
*/
inline unsigned char
estimate_blocklevel(RenderModeLighting *self, RenderState *state,
int x, int y, int z, int *authoratative) {
/* placeholders for later data arrays, coordinates */
PyObject *blocks = NULL;
PyObject *blocklight = NULL;
int local_x = x, local_y = y, local_z = z;
unsigned char block, blocklevel;
unsigned int average_count = 0, average_gather = 0, coeff = 0;
/* defaults to "guess" until told otherwise */
if (authoratative)
*authoratative = 0;
/* find out what chunk we're in, and translate accordingly */
if (x >= 0 && y < 16) {
blocks = state->blocks;
blocklight = self->blocklight;
} else if (x < 0) {
local_x += 16;
blocks = state->left_blocks;
blocklight = self->left_blocklight;
} else if (y >= 16) {
local_y -= 16;
blocks = state->right_blocks;
blocklight = self->right_blocklight;
}
/* make sure we have correctly-ranged coordinates */
if (!(local_x >= 0 && local_x < 16 &&
local_y >= 0 && local_y < 16 &&
local_z >= 0 && local_z < 128)) {
return 0;
}
/* also, make sure we have enough info to correctly calculate lighting */
if (blocks == Py_None || blocks == NULL ||
blocklight == Py_None || blocklight == NULL) {
return 0;
}
block = getArrayByte3D(blocks, local_x, local_y, local_z);
if (authoratative == NULL) {
int auth;
/* iterate through all surrounding blocks to take an average */
int dx, dy, dz, local_block;
for (dx = -1; dx <= 1; dx += 2) {
for (dy = -1; dy <= 1; dy += 2) {
for (dz = -1; dz <= 1; dz += 2) {
/* skip if block is out of range */
if (x+dx < 0 || x+dx >= 16 ||
y+dy < 0 || y+dy >= 16 ||
z+dz < 0 || z+dz >= 128) {
continue;
}
coeff = estimate_blocklevel(self, state, x+dx, y+dy, z+dz, &auth);
local_block = getArrayByte3D(blocks, x+dx, y+dy, z+dz);
/* only add if the block is transparent, this seems to look better than
using every block */
if (auth && is_transparent(local_block)) {
average_gather += coeff;
average_count++;
}
}
}
}
}
/* only return the average if at least one was authoratative */
if (average_count > 0) {
return average_gather / average_count;
}
blocklevel = getArrayByte3D(blocklight, local_x, local_y, local_z);
/* no longer a guess */
if (!(block == 44 || block == 53 || block == 67) && authoratative) {
*authoratative = 1;
}
return blocklevel;
}
inline float
get_lighting_coefficient(RenderModeLighting *self, RenderState *state,
int x, int y, int z) {
/* placeholders for later data arrays, coordinates */
PyObject *blocks = NULL;
PyObject *skylight = NULL;
PyObject *blocklight = NULL;
int local_x = x, local_y = y, local_z = z;
unsigned char block, skylevel, blocklevel;
/* find out what chunk we're in, and translate accordingly */
if (x >= 0 && y < 16) {
blocks = state->blocks;
skylight = self->skylight;
blocklight = self->blocklight;
} else if (x < 0) {
local_x += 16;
blocks = state->left_blocks;
skylight = self->left_skylight;
blocklight = self->left_blocklight;
} else if (y >= 16) {
local_y -= 16;
blocks = state->right_blocks;
skylight = self->right_skylight;
blocklight = self->right_blocklight;
}
/* make sure we have correctly-ranged coordinates */
if (!(local_x >= 0 && local_x < 16 &&
local_y >= 0 && local_y < 16 &&
local_z >= 0 && local_z < 128)) {
return self->calculate_darkness(15, 0);
}
/* also, make sure we have enough info to correctly calculate lighting */
if (blocks == Py_None || blocks == NULL ||
skylight == Py_None || skylight == NULL ||
blocklight == Py_None || blocklight == NULL) {
return self->calculate_darkness(15, 0);
}
block = getArrayByte3D(blocks, local_x, local_y, local_z);
skylevel = getArrayByte3D(skylight, local_x, local_y, local_z);
blocklevel = getArrayByte3D(blocklight, local_x, local_y, local_z);
/* special half-step handling */
if (block == 44 || block == 53 || block == 67) {
unsigned int upper_block;
/* stairs and half-blocks take the skylevel from the upper block if it's transparent */
if (local_z != 127) {
upper_block = getArrayByte3D(blocks, local_x, local_y, local_z + 1);
if (is_transparent(upper_block)) {
skylevel = getArrayByte3D(skylight, local_x, local_y, local_z + 1);
}
} else {
upper_block = 0;
skylevel = 15;
}
/* the block has a bad blocklevel, estimate it from neigborhood
/* use given coordinates, no local ones! */
blocklevel = estimate_blocklevel(self, state, x, y, z, NULL);
}
if (block == 10 || block == 11) {
/* lava blocks should always be lit! */
return 0.0f;
}
return self->calculate_darkness(skylevel, blocklevel);
}
/* shades the drawn block with the given facemask/black_color, based on the
lighting results from (x, y, z) */
static inline void
do_shading_with_mask(RenderModeLighting *self, RenderState *state,
int x, int y, int z, PyObject *mask) {
float black_coeff;
PyObject *blocks;
/* first, check for occlusion if the block is in the local chunk */
if (x >= 0 && x < 16 && y >= 0 && y < 16 && z >= 0 && z < 128) {
unsigned char block = getArrayByte3D(state->blocks, x, y, z);
int occluded = render_mode_occluded(state->rendermode, x, y, z);
if (!occluded && !is_transparent(block)) {
/* this face isn't visible, so don't draw anything */
return;
}
}
black_coeff = get_lighting_coefficient(self, state, x, y, z);
black_coeff *= self->shade_strength;
alpha_over_full(state->img, self->black_color, mask, black_coeff, state->imgx, state->imgy, 0, 0);
}
static int
rendermode_lighting_start(void *data, RenderState *state, PyObject *options) {
RenderModeLighting* self;
/* first, chain up */
int ret = rendermode_normal.start(data, state, options);
if (ret != 0)
return ret;
self = (RenderModeLighting *)data;
self->shade_strength = 1.0;
if (!render_mode_parse_option(options, "shade_strength", "f", &(self->shade_strength)))
return 1;
self->black_color = PyObject_GetAttrString(state->chunk, "black_color");
self->facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks");
// borrowed references, don't need to be decref'd
self->facemasks[0] = PyTuple_GetItem(self->facemasks_py, 0);
self->facemasks[1] = PyTuple_GetItem(self->facemasks_py, 1);
self->facemasks[2] = PyTuple_GetItem(self->facemasks_py, 2);
self->skylight = PyObject_GetAttrString(state->self, "skylight");
self->blocklight = PyObject_GetAttrString(state->self, "blocklight");
self->left_skylight = PyObject_GetAttrString(state->self, "left_skylight");
self->left_blocklight = PyObject_GetAttrString(state->self, "left_blocklight");
self->right_skylight = PyObject_GetAttrString(state->self, "right_skylight");
self->right_blocklight = PyObject_GetAttrString(state->self, "right_blocklight");
self->calculate_darkness = calculate_darkness;
return 0;
}
static void
rendermode_lighting_finish(void *data, RenderState *state) {
RenderModeLighting *self = (RenderModeLighting *)data;
Py_DECREF(self->black_color);
Py_DECREF(self->facemasks_py);
Py_DECREF(self->skylight);
Py_DECREF(self->blocklight);
Py_DECREF(self->left_skylight);
Py_DECREF(self->left_blocklight);
Py_DECREF(self->right_skylight);
Py_DECREF(self->right_blocklight);
/* now chain up */
rendermode_normal.finish(data, state);
}
static int
rendermode_lighting_occluded(void *data, RenderState *state, int x, int y, int z) {
/* no special occlusion here */
return rendermode_normal.occluded(data, state, x, y, z);
}
static void
rendermode_lighting_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
RenderModeLighting* self;
int x, y, z;
/* first, chain up */
rendermode_normal.draw(data, state, src, mask, mask_light);
self = (RenderModeLighting *)data;
x = state->x, y = state->y, z = state->z;
if (is_transparent(state->block)) {
/* transparent: do shading on whole block */
do_shading_with_mask(self, state, x, y, z, mask_light);
} else {
/* opaque: do per-face shading */
do_shading_with_mask(self, state, x, y, z+1, self->facemasks[0]);
do_shading_with_mask(self, state, x-1, y, z, self->facemasks[1]);
do_shading_with_mask(self, state, x, y+1, z, self->facemasks[2]);
}
}
const RenderModeOption rendermode_lighting_options[] = {
{"shade_strength", "how dark to make the shadows, from 0.0 to 1.0 (default: 1.0)"},
{NULL, NULL}
};
RenderModeInterface rendermode_lighting = {
"lighting", "draw shadows from the lighting data",
rendermode_lighting_options,
&rendermode_normal,
sizeof(RenderModeLighting),
rendermode_lighting_start,
rendermode_lighting_finish,
rendermode_lighting_occluded,
rendermode_lighting_draw,
};

View File

@@ -0,0 +1,159 @@
/*
* 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/>.
*/
#include "overviewer.h"
struct MineralColor {
unsigned char blockid;
unsigned char r, g, b;
};
/* put more valuable ores first -- they take precedence */
static struct MineralColor default_minerals[] = {
{48 /* Mossy Stone */, 31, 153, 9},
{56 /* Diamond Ore */, 32, 230, 220},
{21 /* Lapis Lazuli */, 0, 23, 176},
{14 /* Gold Ore */, 255, 234, 0},
{15 /* Iron Ore */, 204, 204, 204},
{73 /* Redstone */, 186, 0, 0},
{74 /* Lit Redstone */, 186, 0, 0},
{16 /* Coal Ore */, 54, 54, 54},
/* end of list marker */
{0, 0, 0, 0}
};
static void get_color(void *data, RenderState *state,
unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a) {
int x = state->x, y = state->y, z_max = state->z + 1, z;
int max_i = -1;
RenderModeMineral* self = (RenderModeMineral *)data;
struct MineralColor *minerals = (struct MineralColor *)(self->minerals);
*a = 0;
for (z = 0; z <= z_max; z++) {
int i, tmp;
unsigned char blockid = getArrayByte3D(state->blocks, x, y, z);
for (i = 0; (max_i == -1 || i < max_i) && minerals[i].blockid != 0; i++) {
if (minerals[i].blockid == blockid) {
*r = minerals[i].r;
*g = minerals[i].g;
*b = minerals[i].b;
tmp = (128 - z_max + z) * 2 - 40;
*a = MIN(MAX(0, tmp), 255);
max_i = i;
break;
}
}
}
}
static int
rendermode_mineral_start(void *data, RenderState *state, PyObject *options) {
PyObject *opt;
RenderModeMineral* self;
/* first, chain up */
int ret = rendermode_overlay.start(data, state, options);
if (ret != 0)
return ret;
/* now do custom initializations */
self = (RenderModeMineral *)data;
opt = PyDict_GetItemString(options, "minerals");
if (opt) {
struct MineralColor *minerals = NULL;
Py_ssize_t minerals_size = 0, i;
/* create custom minerals */
if (!PyList_Check(opt)) {
PyErr_SetString(PyExc_TypeError, "'minerals' must be a list");
return 1;
}
minerals_size = PyList_GET_SIZE(opt);
minerals = self->minerals = calloc(minerals_size + 1, sizeof(struct MineralColor));
if (minerals == NULL) {
return 1;
}
for (i = 0; i < minerals_size; i++) {
PyObject *mineral = PyList_GET_ITEM(opt, i);
if (!PyArg_ParseTuple(mineral, "b(bbb)", &(minerals[i].blockid), &(minerals[i].r), &(minerals[i].g), &(minerals[i].b))) {
free(minerals);
self->minerals = NULL;
return 1;
}
}
} else {
self->minerals = default_minerals;
}
/* setup custom color */
self->parent.get_color = get_color;
return 0;
}
static void
rendermode_mineral_finish(void *data, RenderState *state) {
/* first free all *our* stuff */
RenderModeMineral* self = (RenderModeMineral *)data;
if (self->minerals && self->minerals != default_minerals) {
free(self->minerals);
}
/* now, chain up */
rendermode_overlay.finish(data, state);
}
static int
rendermode_mineral_occluded(void *data, RenderState *state, int x, int y, int z) {
/* no special occlusion here */
return rendermode_overlay.occluded(data, state, x, y, z);
}
static void
rendermode_mineral_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
/* draw normally */
rendermode_overlay.draw(data, state, src, mask, mask_light);
}
const RenderModeOption rendermode_mineral_options[] = {
{"minerals", "a list of (blockid, (r, g, b)) tuples for coloring minerals"},
{NULL, NULL}
};
RenderModeInterface rendermode_mineral = {
"mineral", "draws a colored overlay showing where ores are located",
rendermode_mineral_options,
&rendermode_overlay,
sizeof(RenderModeMineral),
rendermode_mineral_start,
rendermode_mineral_finish,
rendermode_mineral_occluded,
rendermode_mineral_draw,
};

View File

@@ -0,0 +1,71 @@
/*
* 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/>.
*/
#include "overviewer.h"
#include <math.h>
/* figures out the black_coeff from a given skylight and blocklight, used in
lighting calculations -- note this is *different* from the one in
rendermode-lighting.c (the "skylight - 11" part) */
static float calculate_darkness(unsigned char skylight, unsigned char blocklight) {
return 1.0f - powf(0.8f, 15.0 - MAX(blocklight, skylight - 11));
}
static int
rendermode_night_start(void *data, RenderState *state, PyObject *options) {
RenderModeNight* self;
/* first, chain up */
int ret = rendermode_lighting.start(data, state, options);
if (ret != 0)
return ret;
/* override the darkness function with our night version! */
self = (RenderModeNight *)data;
self->parent.calculate_darkness = calculate_darkness;
return 0;
}
static void
rendermode_night_finish(void *data, RenderState *state) {
/* nothing special to do */
rendermode_lighting.finish(data, state);
}
static int
rendermode_night_occluded(void *data, RenderState *state, int x, int y, int z) {
/* no special occlusion here */
return rendermode_lighting.occluded(data, state, x, y, z);
}
static void
rendermode_night_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
/* nothing special to do */
rendermode_lighting.draw(data, state, src, mask, mask_light);
}
RenderModeInterface rendermode_night = {
"night", "like \"lighting\", except at night",
NULL,
&rendermode_lighting,
sizeof(RenderModeNight),
rendermode_night_start,
rendermode_night_finish,
rendermode_night_occluded,
rendermode_night_draw,
};

View File

@@ -0,0 +1,308 @@
/*
* 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/>.
*/
#include "overviewer.h"
static int
rendermode_normal_start(void *data, RenderState *state, PyObject *options) {
PyObject *chunk_x_py, *chunk_y_py, *world, *use_biomes, *worlddir;
RenderModeNormal *self = (RenderModeNormal *)data;
/* load up the given options, first */
self->edge_opacity = 0.15;
if (!render_mode_parse_option(options, "edge_opacity", "f", &(self->edge_opacity)))
return 1;
self->min_depth = 0;
if (!render_mode_parse_option(options, "min_depth", "I", &(self->min_depth)))
return 1;
self->max_depth = 127;
if (!render_mode_parse_option(options, "max_depth", "I", &(self->max_depth)))
return 1;
self->height_fading = 0;
if (!render_mode_parse_option(options, "height_fading", "i", &(self->height_fading)))
return 1;
if (self->height_fading) {
self->black_color = PyObject_GetAttrString(state->chunk, "black_color");
self->white_color = PyObject_GetAttrString(state->chunk, "white_color");
}
chunk_x_py = PyObject_GetAttrString(state->self, "chunkX");
chunk_y_py = PyObject_GetAttrString(state->self, "chunkY");
/* careful now -- C's % operator works differently from python's
we can't just do x % 32 like we did before */
self->chunk_x = PyInt_AsLong(chunk_x_py);
self->chunk_y = PyInt_AsLong(chunk_y_py);
while (self->chunk_x < 0)
self->chunk_x += 32;
while (self->chunk_y < 0)
self->chunk_y += 32;
self->chunk_x %= 32;
self->chunk_y %= 32;
/* fetch the biome data from textures.py, if needed */
world = PyObject_GetAttrString(state->self, "world");
worlddir = PyObject_GetAttrString(world, "worlddir");
use_biomes = PyObject_GetAttrString(world, "useBiomeData");
Py_DECREF(world);
if (PyObject_IsTrue(use_biomes)) {
PyObject *facemasks_py;
self->biome_data = PyObject_CallMethod(state->textures, "getBiomeData", "OOO",
worlddir, chunk_x_py, chunk_y_py);
if (self->biome_data == Py_None) {
self->biome_data = NULL;
self->foliagecolor = NULL;
self->grasscolor = NULL;
self->leaf_texture = NULL;
self->grass_texture = NULL;
self->tall_grass_texture = NULL;
self->facemask_top = NULL;
} else {
self->foliagecolor = PyObject_GetAttrString(state->textures, "foliagecolor");
self->grasscolor = PyObject_GetAttrString(state->textures, "grasscolor");
self->leaf_texture = PyObject_GetAttrString(state->textures, "biome_leaf_texture");
self->grass_texture = PyObject_GetAttrString(state->textures, "biome_grass_texture");
self->tall_grass_texture = PyObject_GetAttrString(state->textures, "biome_tall_grass_texture");
self->tall_fern_texture = PyObject_GetAttrString(state->textures, "biome_tall_fern_texture");
facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks");
/* borrowed reference, needs to be incref'd if we keep it */
self->facemask_top = PyTuple_GetItem(facemasks_py, 0);
Py_INCREF(self->facemask_top);
Py_DECREF(facemasks_py);
}
} else {
self->biome_data = NULL;
self->foliagecolor = NULL;
self->grasscolor = NULL;
self->leaf_texture = NULL;
self->grass_texture = NULL;
self->tall_grass_texture = NULL;
self->tall_fern_texture = NULL;
self->facemask_top = NULL;
}
Py_DECREF(use_biomes);
Py_DECREF(worlddir);
Py_DECREF(chunk_x_py);
Py_DECREF(chunk_y_py);
return 0;
}
static void
rendermode_normal_finish(void *data, RenderState *state) {
RenderModeNormal *self = (RenderModeNormal *)data;
Py_XDECREF(self->biome_data);
Py_XDECREF(self->foliagecolor);
Py_XDECREF(self->grasscolor);
Py_XDECREF(self->leaf_texture);
Py_XDECREF(self->grass_texture);
Py_XDECREF(self->tall_grass_texture);
Py_XDECREF(self->tall_fern_texture);
Py_XDECREF(self->facemask_top);
Py_XDECREF(self->black_color);
Py_XDECREF(self->white_color);
}
static int
rendermode_normal_occluded(void *data, RenderState *state, int x, int y, int z) {
RenderModeNormal *self = (RenderModeNormal *)data;
if (z > self->max_depth || z < self->min_depth) {
return 1;
}
if ( (x != 0) && (y != 15) && (z != self->max_depth) &&
!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
return 0;
}
static void
rendermode_normal_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
RenderModeNormal *self = (RenderModeNormal *)data;
int randx = 0,randy = 0;
unsigned char data_byte;
/* first, check to see if we should use biome-compatible src, mask */
if (self->biome_data) {
if (state->block == 18) {
src = mask = self->leaf_texture;
} else if (state->block == 31) {
/* add a random offset to the postion of the tall grass to make it more wild */
randx = rand() % 6 + 1 - 3;
randy = rand() % 6 + 1 - 3;
state->imgx = state->imgx + randx;
state->imgy = state->imgy + randy;
data_byte = getArrayByte3D(state->blockdata_expanded, state->x, state->y, state->z);
if (data_byte == 1) {
src = mask = self->tall_grass_texture;
} else if (data_byte == 2) {
src = mask = self->tall_fern_texture;
}
}
}
/* draw the block! */
alpha_over(state->img, src, mask, state->imgx, state->imgy, 0, 0);
if (self->biome_data) {
/* do the biome stuff! */
unsigned int index;
PyObject *color = NULL, *facemask = NULL;
unsigned char r, g, b;
index = ((self->chunk_y * 16) + state->y) * 16 * 32 + (self->chunk_x * 16) + state->x;
index = big_endian_ushort(getArrayShort1D(self->biome_data, index));
switch (state->block) {
case 2:
/* grass -- skip for snowgrass */
if (state->z < 127 && getArrayByte3D(state->blocks, state->x, state->y, state->z+1) == 78)
break;
color = PySequence_GetItem(self->grasscolor, index);
facemask = self->grass_texture;
alpha_over(state->img, self->grass_texture, self->grass_texture, state->imgx, state->imgy, 0, 0);
break;
case 18:
/* leaves */
color = PySequence_GetItem(self->foliagecolor, index);
facemask = mask;
break;
case 31:
/* tall grass */
if ( getArrayByte3D(state->blockdata_expanded, state->x, state->y, state->z) != 0 )
{ /* do not tint dead shrubs */
color = PySequence_GetItem(self->grasscolor, index);
facemask = mask;
break;
}
break;
default:
break;
};
if (color)
{
/* we've got work to do */
r = PyInt_AsLong(PyTuple_GET_ITEM(color, 0));
g = PyInt_AsLong(PyTuple_GET_ITEM(color, 1));
b = PyInt_AsLong(PyTuple_GET_ITEM(color, 2));
Py_DECREF(color);
tint_with_mask(state->img, r, g, b, 255, facemask, state->imgx, state->imgy, 0, 0);
}
}
if (self->height_fading) {
/* do some height fading */
PyObject *height_color = self->white_color;
/* negative alpha => darkness, positive => light */
float alpha = (1.0 / (1 + expf((70 - state->z) / 11.0))) * 0.6 - 0.55;
if (alpha < 0.0) {
alpha *= -1;
height_color = self->black_color;
}
alpha_over_full(state->img, height_color, mask_light, alpha, state->imgx, state->imgy, 0, 0);
}
/* Draw some edge lines! */
// draw.line(((imgx+12,imgy+increment), (imgx+22,imgy+5+increment)), fill=(0,0,0), width=1)
if (state->block == 44 || state->block == 78 || !is_transparent(state->block)) {
Imaging img_i = imaging_python_to_c(state->img);
unsigned char ink[] = {0, 0, 0, 255 * self->edge_opacity};
int increment=0;
if (state->block == 44) // half-step
increment=6;
else if ((state->block == 78) || (state->block == 93) || (state->block == 94)) // snow, redstone repeaters (on and off)
increment=9;
if ((state->x == 15) && (state->up_right_blocks != Py_None)) {
unsigned char side_block = getArrayByte3D(state->up_right_blocks, 0, state->y, state->z);
if (side_block != state->block && is_transparent(side_block)) {
ImagingDrawLine(img_i, state->imgx+12, state->imgy+1+increment, state->imgx+22+1, state->imgy+5+1+increment, &ink, 1);
ImagingDrawLine(img_i, state->imgx+12, state->imgy+increment, state->imgx+22+1, state->imgy+5+increment, &ink, 1);
}
} else if (state->x != 15) {
unsigned char side_block = getArrayByte3D(state->blocks, state->x+1, state->y, state->z);
if (side_block != state->block && is_transparent(side_block)) {
ImagingDrawLine(img_i, state->imgx+12, state->imgy+1+increment, state->imgx+22+1, state->imgy+5+1+increment, &ink, 1);
ImagingDrawLine(img_i, state->imgx+12, state->imgy+increment, state->imgx+22+1, state->imgy+5+increment, &ink, 1);
}
}
// if y != 0 and blocks[x,y-1,z] == 0
// chunk boundries are annoying
if ((state->y == 0) && (state->up_left_blocks != Py_None)) {
unsigned char side_block = getArrayByte3D(state->up_left_blocks, state->x, 15, state->z);
if (side_block != state->block && is_transparent(side_block)) {
ImagingDrawLine(img_i, state->imgx, state->imgy+6+1+increment, state->imgx+12+1, state->imgy+1+increment, &ink, 1);
ImagingDrawLine(img_i, state->imgx, state->imgy+6+increment, state->imgx+12+1, state->imgy+increment, &ink, 1);
}
} else if (state->y != 0) {
unsigned char side_block = getArrayByte3D(state->blocks, state->x, state->y-1, state->z);
if (side_block != state->block && is_transparent(side_block)) {
// draw.line(((imgx,imgy+6+increment), (imgx+12,imgy+increment)), fill=(0,0,0), width=1)
ImagingDrawLine(img_i, state->imgx, state->imgy+6+1+increment, state->imgx+12+1, state->imgy+1+increment, &ink, 1);
ImagingDrawLine(img_i, state->imgx, state->imgy+6+increment, state->imgx+12+1, state->imgy+increment, &ink, 1);
}
}
}
}
const RenderModeOption rendermode_normal_options[] = {
{"edge_opacity", "darkness of the edge lines, from 0.0 to 1.0 (default: 0.15)"},
{"min_depth", "lowest level of blocks to render (default: 0)"},
{"max_depth", "highest level of blocks to render (default: 127)"},
{"height_fading", "darken or lighten blocks based on height (default: False)"},
{NULL, NULL}
};
RenderModeInterface rendermode_normal = {
"normal", "nothing special, just render the blocks",
rendermode_normal_options,
NULL,
sizeof(RenderModeNormal),
rendermode_normal_start,
rendermode_normal_finish,
rendermode_normal_occluded,
rendermode_normal_draw,
};

View File

@@ -0,0 +1,136 @@
/*
* 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/>.
*/
#include "overviewer.h"
static void get_color(void *data, RenderState *state,
unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a) {
*r = 200;
*g = 200;
*b = 255;
*a = 155;
}
static int
rendermode_overlay_start(void *data, RenderState *state, PyObject *options) {
PyObject *facemasks_py;
RenderModeOverlay *self = (RenderModeOverlay *)data;
facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks");
/* borrowed reference, needs to be incref'd if we keep it */
self->facemask_top = PyTuple_GetItem(facemasks_py, 0);
Py_INCREF(self->facemask_top);
Py_DECREF(facemasks_py);
self->white_color = PyObject_GetAttrString(state->chunk, "white_color");
self->solid_blocks = PyObject_GetAttrString(state->chunk, "solid_blocks");
self->fluid_blocks = PyObject_GetAttrString(state->chunk, "fluid_blocks");
self->get_color = get_color;
return 0;
}
static void
rendermode_overlay_finish(void *data, RenderState *state) {
RenderModeOverlay *self = (RenderModeOverlay *)data;
Py_DECREF(self->facemask_top);
Py_DECREF(self->white_color);
Py_DECREF(self->solid_blocks);
Py_DECREF(self->fluid_blocks);
}
static int
rendermode_overlay_occluded(void *data, RenderState *state, int x, int y, int z) {
if ( (x != 0) && (y != 15) && (z != 127) &&
!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
return 0;
}
static void
rendermode_overlay_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
RenderModeOverlay *self = (RenderModeOverlay *)data;
unsigned char r, g, b, a;
PyObject *top_block_py, *block_py;
// exactly analogous to edge-line code for these special blocks
int increment=0;
if (state->block == 44) // half-step
increment=6;
else if (state->block == 78) // snow
increment=9;
/* clear the draw space -- set alpha to 0 within mask */
tint_with_mask(state->img, 255, 255, 255, 0, mask, state->imgx, state->imgy, 0, 0);
/* skip rendering the overlay if we can't see it */
if (state->z != 127) {
unsigned char top_block = getArrayByte3D(state->blocks, state->x, state->y, state->z+1);
if (!is_transparent(top_block)) {
return;
}
/* check to be sure this block is solid/fluid */
top_block_py = PyInt_FromLong(top_block);
if (PySequence_Contains(self->solid_blocks, top_block_py) ||
PySequence_Contains(self->fluid_blocks, top_block_py)) {
/* top block is fluid or solid, skip drawing */
Py_DECREF(top_block_py);
return;
}
Py_DECREF(top_block_py);
}
/* check to be sure this block is solid/fluid */
block_py = PyInt_FromLong(state->block);
if (!PySequence_Contains(self->solid_blocks, block_py) &&
!PySequence_Contains(self->fluid_blocks, block_py)) {
/* not fluid or solid, skip drawing the overlay */
Py_DECREF(block_py);
return;
}
Py_DECREF(block_py);
/* get our color info */
self->get_color(data, state, &r, &g, &b, &a);
/* do the overlay */
if (a > 0) {
alpha_over(state->img, self->white_color, self->facemask_top, state->imgx, state->imgy + increment, 0, 0);
tint_with_mask(state->img, r, g, b, a, self->facemask_top, state->imgx, state->imgy + increment, 0, 0);
}
}
RenderModeInterface rendermode_overlay = {
"overlay", "base rendermode for informational overlays",
NULL,
NULL,
sizeof(RenderModeOverlay),
rendermode_overlay_start,
rendermode_overlay_finish,
rendermode_overlay_occluded,
rendermode_overlay_draw,
};

View File

@@ -0,0 +1,119 @@
/*
* 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/>.
*/
#include "overviewer.h"
#include <math.h>
static void get_color(void *data, RenderState *state,
unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a) {
RenderModeSpawn* self = (RenderModeSpawn *)data;
int x = state->x, y = state->y, z = state->z;
int z_light = z + 1;
unsigned char blocklight, skylight;
PyObject *block_py;
/* set a nice, pretty red color */
*r = 229;
*g = 36;
*b = 38;
/* default to no overlay, until told otherwise */
*a = 0;
block_py = PyInt_FromLong(state->block);
if (PySequence_Contains(self->nospawn_blocks, block_py)) {
/* nothing can spawn on this */
Py_DECREF(block_py);
return;
}
Py_DECREF(block_py);
blocklight = getArrayByte3D(self->blocklight, x, y, MIN(127, z_light));
/* if we're at the top, force 15 (brightest!) skylight */
if (z_light == 128) {
skylight = 15;
} else {
skylight = getArrayByte3D(self->skylight, x, y, z_light);
}
if (MAX(blocklight, skylight) <= 7) {
/* hostile mobs spawn in daylight */
*a = 240;
} else if (MAX(blocklight, skylight - 11) <= 7) {
/* hostile mobs spawn at night */
*a = 150;
}
}
static int
rendermode_spawn_start(void *data, RenderState *state, PyObject *options) {
RenderModeSpawn* self;
/* first, chain up */
int ret = rendermode_overlay.start(data, state, options);
if (ret != 0)
return ret;
/* now do custom initializations */
self = (RenderModeSpawn *)data;
self->nospawn_blocks = PyObject_GetAttrString(state->chunk, "nospawn_blocks");
self->blocklight = PyObject_GetAttrString(state->self, "blocklight");
self->skylight = PyObject_GetAttrString(state->self, "skylight");
/* setup custom color */
self->parent.get_color = get_color;
return 0;
}
static void
rendermode_spawn_finish(void *data, RenderState *state) {
/* first free all *our* stuff */
RenderModeSpawn* self = (RenderModeSpawn *)data;
Py_DECREF(self->nospawn_blocks);
Py_DECREF(self->blocklight);
Py_DECREF(self->skylight);
/* now, chain up */
rendermode_overlay.finish(data, state);
}
static int
rendermode_spawn_occluded(void *data, RenderState *state, int x, int y, int z) {
/* no special occlusion here */
return rendermode_overlay.occluded(data, state, x, y, z);
}
static void
rendermode_spawn_draw(void *data, RenderState *state, PyObject *src, PyObject *mask, PyObject *mask_light) {
/* draw normally */
rendermode_overlay.draw(data, state, src, mask, mask_light);
}
RenderModeInterface rendermode_spawn = {
"spawn", "draws a red overlay where monsters can spawn at night",
NULL,
&rendermode_overlay,
sizeof(RenderModeSpawn),
rendermode_spawn_start,
rendermode_spawn_finish,
rendermode_spawn_occluded,
rendermode_spawn_draw,
};

View File

@@ -0,0 +1,551 @@
/*
* 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/>.
*/
#include "overviewer.h"
#include <string.h>
#include <stdarg.h>
/* list of all render modes, ending in NULL
all of these will be available to the user, so DON'T include modes
that are only useful as a base for other modes. */
static RenderModeInterface *render_modes[] = {
&rendermode_normal,
&rendermode_lighting,
&rendermode_night,
&rendermode_spawn,
&rendermode_cave,
&rendermode_mineral,
NULL
};
static PyObject *render_mode_options = NULL;
static PyObject *custom_render_modes = NULL;
/* rendermode encapsulation */
/* helper to recursively find options for a given mode */
static inline PyObject *
render_mode_create_options(const char *mode) {
const char *parent = NULL;
PyObject *base_options, *ret, *parent_options;
unsigned int i, found_concrete;
if (render_mode_options == NULL)
return PyDict_New();
base_options = PyDict_GetItemString(render_mode_options, mode);
if (base_options) {
ret = PyDict_Copy(base_options);
} else {
ret = PyDict_New();
}
/* figure out the parent mode name */
found_concrete = 0;
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, mode) == 0) {
found_concrete = 1;
if (render_modes[i]->parent) {
parent = render_modes[i]->parent->name;
}
break;
}
}
/* check custom mode info if needed */
if (found_concrete == 0 && custom_render_modes != NULL) {
PyObject *custom = PyDict_GetItemString(custom_render_modes, mode);
if (custom) {
custom = PyDict_GetItemString(custom, "parent");
if (custom) {
parent = PyString_AsString(custom);
}
}
}
/* merge parent options, if the parent was found */
if (parent) {
parent_options = render_mode_create_options(parent);
if (parent_options) {
if (PyDict_Merge(ret, parent_options, 0) == -1) {
Py_DECREF(ret);
Py_DECREF(parent_options);
return NULL;
}
Py_DECREF(parent_options);
}
}
return ret;
}
/* helper to find the first concrete, C interface for a given mode */
inline static RenderModeInterface *
render_mode_find_interface(const char *mode) {
PyObject *custom;
const char *custom_parent;
unsigned int i;
/* if it is *itself* concrete, we're done */
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, mode) == 0)
return render_modes[i];
}
/* check for custom modes */
if (custom_render_modes == NULL)
return NULL;
custom = PyDict_GetItemString(custom_render_modes, mode);
if (custom == NULL)
return NULL;
custom = PyDict_GetItemString(custom, "parent");
if (custom == NULL)
return NULL;
custom_parent = PyString_AsString(custom);
if (custom_parent == NULL)
return NULL;
return render_mode_find_interface(custom_parent);
}
RenderMode *render_mode_create(const char *mode, RenderState *state) {
PyObject *options;
RenderMode *ret = NULL;
RenderModeInterface *iface = NULL;
iface = render_mode_find_interface(mode);
if (iface == NULL)
return NULL;
options = render_mode_create_options(mode);
if (options == NULL)
return NULL;
ret = calloc(1, sizeof(RenderMode));
if (ret == NULL) {
Py_DECREF(options);
return NULL;
}
ret->mode = calloc(1, iface->data_size);
if (ret->mode == NULL) {
Py_DECREF(options);
free(ret);
return NULL;
}
ret->iface = iface;
ret->state = state;
if (iface->start(ret->mode, state, options)) {
Py_DECREF(options);
free(ret->mode);
free(ret);
return NULL;
}
Py_DECREF(options);
return ret;
}
void render_mode_destroy(RenderMode *self) {
self->iface->finish(self->mode, self->state);
free(self->mode);
free(self);
}
int render_mode_occluded(RenderMode *self, int x, int y, int z) {
return self->iface->occluded(self->mode, self->state, x, y, z);
}
void render_mode_draw(RenderMode *self, PyObject *img, PyObject *mask, PyObject *mask_light) {
self->iface->draw(self->mode, self->state, img, mask, mask_light);
}
/* options parse helper */
int render_mode_parse_option(PyObject *dict, const char *name, const char *format, ...) {
va_list ap;
PyObject *item;
int ret;
if (dict == NULL || name == NULL)
return 1;
item = PyDict_GetItemString(dict, name);
if (item == NULL)
return 1;
/* make sure the item we're parsing is a tuple
for VaParse to work correctly */
if (!PyTuple_Check(item)) {
item = PyTuple_Pack(1, item);
} else {
Py_INCREF(item);
}
va_start(ap, format);
ret = PyArg_VaParse(item, format, ap);
va_end(ap);
Py_DECREF(item);
if (!ret) {
PyObject *errtype, *errvalue, *errtraceback;
const char *errstring;
PyErr_Fetch(&errtype, &errvalue, &errtraceback);
errstring = PyString_AsString(errvalue);
PyErr_Format(PyExc_TypeError, "rendermode option \"%s\" has incorrect type (%s)", name, errstring);
Py_DECREF(errtype);
Py_DECREF(errvalue);
Py_XDECREF(errtraceback);
}
return ret;
}
/* bindings for python -- get all the rendermode names */
PyObject *get_render_modes(PyObject *self, PyObject *args) {
PyObject *modes;
unsigned int i;
if (!PyArg_ParseTuple(args, ""))
return NULL;
modes = PyList_New(0);
if (modes == NULL)
return NULL;
for (i = 0; render_modes[i] != NULL; i++) {
PyObject *name = PyString_FromString(render_modes[i]->name);
PyList_Append(modes, name);
Py_DECREF(name);
}
if (custom_render_modes != NULL) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(custom_render_modes, &pos, &key, &value)) {
PyList_Append(modes, key);
}
}
return modes;
}
/* helper, get the list of options for a render mode */
static inline PyObject *
get_render_mode_options(const char *rendermode)
{
PyObject *options;
unsigned int i, j;
options = PyList_New(0);
if (!options)
return NULL;
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, rendermode) == 0) {
if (render_modes[i]->options == NULL)
break;
for (j = 0; render_modes[i]->options[j].name != NULL; j++) {
RenderModeOption opt = render_modes[i]->options[j];
PyObject *name = PyString_FromString(opt.name);
PyObject *description = PyString_FromString(opt.description);
PyObject *option = PyDict_New();
if (!name || !description || !option) {
Py_XDECREF(name);
Py_XDECREF(description);
Py_XDECREF(option);
Py_DECREF(options);
return NULL;
}
PyDict_SetItemString(option, "name", name);
PyDict_SetItemString(option, "description", description);
PyList_Append(options, option);
Py_DECREF(name);
Py_DECREF(description);
Py_DECREF(option);
}
break;
}
}
return options;
}
/* more bindings -- return info for a given rendermode name */
PyObject *get_render_mode_info(PyObject *self, PyObject *args) {
const char* rendermode;
PyObject *info;
unsigned int i;
if (!PyArg_ParseTuple(args, "s", &rendermode))
return NULL;
info = PyDict_New();
if (info == NULL)
return NULL;
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, rendermode) == 0) {
PyObject *tmp;
tmp = PyString_FromString(render_modes[i]->name);
PyDict_SetItemString(info, "name", tmp);
Py_DECREF(tmp);
tmp = PyString_FromString(render_modes[i]->description);
PyDict_SetItemString(info, "description", tmp);
Py_DECREF(tmp);
tmp = get_render_mode_options(rendermode);
PyDict_SetItemString(info, "options", tmp);
Py_DECREF(tmp);
if (render_modes[i]->parent != NULL) {
tmp = PyString_FromString(render_modes[i]->parent->name);
PyDict_SetItemString(info, "parent", tmp);
Py_DECREF(tmp);
}
return info;
}
}
if (custom_render_modes != NULL) {
PyObject *custom = PyDict_GetItemString(custom_render_modes, rendermode);
if (custom) {
PyObject *tmp, *copy = PyDict_Copy(custom);
Py_DECREF(info);
tmp = PyString_FromString(rendermode);
PyDict_SetItemString(copy, "name", tmp);
Py_DECREF(tmp);
tmp = PyList_New(0);
PyDict_SetItemString(copy, "options", tmp);
Py_DECREF(tmp);
return copy;
}
}
Py_DECREF(info);
return PyErr_Format(PyExc_ValueError, "invalid rendermode: \"%s\"", rendermode);
}
/* bindings -- get list of inherited parents */
PyObject *get_render_mode_inheritance(PyObject *self, PyObject *args) {
const char *rendermode;
PyObject *parents;
unsigned int i;
RenderModeInterface *iface = NULL;
if (!PyArg_ParseTuple(args, "s", &rendermode))
return NULL;
parents = PyList_New(0);
if (!parents)
return NULL;
/* take care of the chain of custom modes, if there are any */
if (custom_render_modes != NULL) {
PyObject *custom = PyDict_GetItemString(custom_render_modes, rendermode);
while (custom != NULL) {
PyObject *name = PyString_FromString(rendermode);
PyList_Append(parents, name);
Py_DECREF(name);
custom = PyDict_GetItemString(custom, "parent");
rendermode = PyString_AsString(custom);
custom = PyDict_GetItem(custom_render_modes, custom);
}
}
/* now handle concrete modes */
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, rendermode) == 0) {
iface = render_modes[i];
break;
}
}
if (!iface) {
Py_DECREF(parents);
return PyErr_Format(PyExc_ValueError, "invalid rendermode: \"%s\"", rendermode);
}
while (iface) {
PyObject *name = PyString_FromString(iface->name);
PyList_Append(parents, name);
Py_DECREF(name);
iface = iface->parent;
}
PyList_Reverse(parents);
return parents;
}
/* bindings -- get list of (direct) children */
PyObject *get_render_mode_children(PyObject *self, PyObject *args) {
const char *rendermode;
PyObject *children;
unsigned int i;
if (!PyArg_ParseTuple(args, "s", &rendermode))
return NULL;
children = PyList_New(0);
if (!children)
return NULL;
for (i = 0; render_modes[i] != NULL; i++) {
if (render_modes[i]->parent && strcmp(render_modes[i]->parent->name, rendermode) == 0) {
PyObject *child_name = PyString_FromString(render_modes[i]->name);
PyList_Append(children, child_name);
Py_DECREF(child_name);
}
}
if (custom_render_modes != NULL) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(custom_render_modes, &pos, &key, &value)) {
PyObject *pyparent = PyDict_GetItemString(value, "parent");
const char *parent = PyString_AsString(pyparent);
if (strcmp(parent, rendermode) == 0) {
PyList_Append(children, key);
}
}
}
return children;
}
/* helper to decide if a rendermode supports a given option */
static inline int
render_mode_supports_option(RenderModeInterface *iface, const char *name) {
unsigned int i;
if (iface->options != NULL) {
for (i = 0; iface->options[i].name != NULL; i++) {
if (strcmp(iface->options[i].name, name) == 0) {
return 1;
}
}
}
if (iface->parent != NULL)
return render_mode_supports_option(iface->parent, name);
return 0;
}
/* python rendermode options bindings */
PyObject *set_render_mode_options(PyObject *self, PyObject *args) {
const char *rendermode;
PyObject *opts, *key, *value;
Py_ssize_t pos = 0;
RenderModeInterface *iface = NULL;
if (!PyArg_ParseTuple(args, "sO!", &rendermode, &PyDict_Type, &opts))
return NULL;
iface = render_mode_find_interface(rendermode);
if (iface == NULL) {
return PyErr_Format(PyExc_ValueError, "'%s' is not a valid rendermode name", rendermode);
}
/* check options to make sure they're available */
while (PyDict_Next(opts, &pos, &key, &value)) {
const char *name = PyString_AsString(key);
if (name == NULL)
return NULL;
if (!render_mode_supports_option(iface, name)) {
return PyErr_Format(PyExc_ValueError, "'%s' is not a valid option for rendermode '%s'", name, rendermode);
}
}
if (render_mode_options == NULL)
render_mode_options = PyDict_New();
PyDict_SetItemString(render_mode_options, rendermode, opts);
Py_RETURN_NONE;
}
PyObject *add_custom_render_mode(PyObject *self, PyObject *args) {
const char *rendermode, *parentmode;
PyObject *opts, *options, *pyparent;
if (!PyArg_ParseTuple(args, "sO!", &rendermode, &PyDict_Type, &opts))
return NULL;
if (custom_render_modes == NULL)
custom_render_modes = PyDict_New();
/* first, make sure the parent is set correctly */
pyparent = PyDict_GetItemString(opts, "parent");
if (pyparent == NULL)
return PyErr_Format(PyExc_ValueError, "'%s' does not have a parent mode", rendermode);
parentmode = PyString_AsString(pyparent);
if (parentmode == NULL)
return PyErr_Format(PyExc_ValueError, "'%s' does not have a valid parent", rendermode);
/* check that parentmode exists */
if (PyDict_GetItemString(custom_render_modes, parentmode) == NULL) {
unsigned int parent_valid = 0, i;
for (i = 0; render_modes[i] != NULL; i++) {
if (strcmp(render_modes[i]->name, parentmode) == 0) {
parent_valid = 1;
}
}
if (parent_valid == 0)
return PyErr_Format(PyExc_ValueError, "'%s' parent '%s' is not valid", rendermode, parentmode);
}
/* remove and handle options seperately, if needed */
options = PyDict_GetItemString(opts, "options");
if (options != NULL) {
PyObject *opts_copy, *set_opts_args;
opts_copy = PyDict_Copy(opts);
if (opts_copy == NULL)
return NULL;
PyDict_DelItemString(opts_copy, "options");
PyDict_SetItemString(custom_render_modes, rendermode, opts_copy);
Py_DECREF(opts_copy);
/* call set_render_mode_options */
set_opts_args = Py_BuildValue("sO", rendermode, options);
if (set_opts_args == NULL)
return NULL;
if (set_render_mode_options(NULL, set_opts_args) == NULL) {
Py_DECREF(set_opts_args);
return NULL;
}
Py_DECREF(set_opts_args);
} else {
PyDict_SetItemString(custom_render_modes, rendermode, opts);
}
Py_RETURN_NONE;
}

View File

@@ -0,0 +1,213 @@
/*
* 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/>.
*/
/*
* To make a new render mode (the C part, at least):
*
* * add a data struct and extern'd interface declaration below
*
* * fill in this interface struct in rendermode-(yourmode).c
* (see rendermodes-normal.c for an example: the "normal" mode)
*
* * if you want to derive from (say) the "normal" mode, put
* a RenderModeNormal entry at the top of your data struct, and
* be sure to call your parent's functions in your own!
* (see rendermode-night.c for a simple example derived from
* the "lighting" mode)
*
* * add your mode to the list in rendermodes.c
*/
#ifndef __RENDERMODES_H_INCLUDED__
#define __RENDERMODES_H_INCLUDED__
#include <Python.h>
#include "overviewer.h"
typedef struct {
const char *name;
const char *description;
} RenderModeOption;
/* rendermode interface */
typedef struct _RenderModeInterface RenderModeInterface;
struct _RenderModeInterface {
/* the name of this mode */
const char *name;
/* the short description of this render mode */
const char *description;
/* a NULL-terminated list of render mode options, or NULL */
RenderModeOption *options;
/* the rendermode this is derived from, or NULL */
RenderModeInterface *parent;
/* the size of the local storage for this rendermode */
unsigned int data_size;
/* may return non-zero on error, last arg is options */
int (*start)(void *, RenderState *, PyObject *);
void (*finish)(void *, RenderState *);
/* returns non-zero to skip rendering this block */
int (*occluded)(void *, RenderState *, int, int, int);
/* last two arguments are img and mask, from texture lookup */
void (*draw)(void *, RenderState *, PyObject *, PyObject *, PyObject *);
};
/* wrapper for passing around rendermodes */
struct _RenderMode {
void *mode;
RenderModeInterface *iface;
RenderState *state;
};
/* functions for creating / using rendermodes */
RenderMode *render_mode_create(const char *mode, RenderState *state);
void render_mode_destroy(RenderMode *self);
int render_mode_occluded(RenderMode *self, int x, int y, int z);
void render_mode_draw(RenderMode *self, PyObject *img, PyObject *mask, PyObject *mask_light);
/* helper function for reading in rendermode options
works like PyArg_ParseTuple on a dictionary item */
int render_mode_parse_option(PyObject *dict, const char *name, const char *format, ...);
/* python metadata bindings */
PyObject *get_render_modes(PyObject *self, PyObject *args);
PyObject *get_render_mode_info(PyObject *self, PyObject *args);
PyObject *get_render_mode_inheritance(PyObject *self, PyObject *args);
PyObject *get_render_mode_children(PyObject *self, PyObject *args);
/* python rendermode options bindings */
PyObject *set_render_mode_options(PyObject *self, PyObject *args);
PyObject *add_custom_render_mode(PyObject *self, PyObject *args);
/* individual rendermode interface declarations follow */
/* NORMAL */
typedef struct {
/* coordinates of the chunk, inside its region file */
int chunk_x, chunk_y;
/* biome data for the region */
PyObject *biome_data;
/* grasscolor and foliagecolor lookup tables */
PyObject *grasscolor, *foliagecolor;
/* biome-compatible grass/leaf textures */
PyObject *grass_texture, *leaf_texture, *tall_grass_texture, *tall_fern_texture;
/* top facemask for grass biome tinting */
PyObject *facemask_top;
/* black and white colors for height fading */
PyObject *black_color, *white_color;
float edge_opacity;
unsigned int min_depth;
unsigned int max_depth;
int height_fading;
} RenderModeNormal;
extern RenderModeInterface rendermode_normal;
/* OVERLAY */
typedef struct {
/* top facemask and white color image, for drawing overlays */
PyObject *facemask_top, *white_color;
/* only show overlay on top of solid or fluid blocks */
PyObject *solid_blocks, *fluid_blocks;
/* can be overridden in derived classes to control
overlay alpha and color
last four vars are r, g, b, a out */
void (*get_color)(void *, RenderState *,
unsigned char *, unsigned char *, unsigned char *, unsigned char *);
} RenderModeOverlay;
extern RenderModeInterface rendermode_overlay;
/* LIGHTING */
typedef struct {
/* inherits from normal render mode */
RenderModeNormal parent;
PyObject *black_color, *facemasks_py;
PyObject *facemasks[3];
/* extra data, loaded off the chunk class */
PyObject *skylight, *blocklight;
PyObject *left_skylight, *left_blocklight;
PyObject *right_skylight, *right_blocklight;
/* can be overridden in derived rendermodes to control lighting
arguments are skylight, blocklight */
float (*calculate_darkness)(unsigned char, unsigned char);
float shade_strength;
} RenderModeLighting;
extern RenderModeInterface rendermode_lighting;
/* NIGHT */
typedef struct {
/* inherits from lighting */
RenderModeLighting parent;
} RenderModeNight;
extern RenderModeInterface rendermode_night;
/* SPAWN */
typedef struct {
/* inherits from overlay */
RenderModeOverlay parent;
/* used to figure out which blocks are spawnable */
PyObject *nospawn_blocks;
PyObject *skylight, *blocklight;
} RenderModeSpawn;
extern RenderModeInterface rendermode_spawn;
/* CAVE */
typedef struct {
/* render blocks with lighting mode */
RenderModeLighting parent;
/* data used to know where the surface is */
PyObject *skylight;
PyObject *left_skylight;
PyObject *right_skylight;
PyObject *up_left_skylight;
PyObject *up_right_skylight;
/* data used to know where the surface is */
PyObject *blocklight;
PyObject *left_blocklight;
PyObject *right_blocklight;
PyObject *up_left_blocklight;
PyObject *up_right_blocklight;
/* colors used for tinting */
PyObject *depth_colors;
int depth_tinting;
int only_lit;
int lighting;
} RenderModeCave;
extern RenderModeInterface rendermode_cave;
/* MINERAL */
typedef struct {
/* inherits from overlay */
RenderModeOverlay parent;
void *minerals;
} RenderModeMineral;
extern RenderModeInterface rendermode_mineral;
#endif /* __RENDERMODES_H_INCLUDED__ */