[6991] | 1 | // Python - C extension for fast computation of the Mandelbrot plot routine |
---|
| 2 | // |
---|
| 3 | // To compile (Python2.3): |
---|
| 4 | // gcc -c mandelplot_ext.c -I/usr/include/python2.3 -o mandelplot_ext.o -Wall -O3 |
---|
| 5 | // gcc -shared mandelplot_ext.o -o mandelplot_ext.so |
---|
| 6 | // |
---|
| 7 | // kmax = 256 |
---|
| 8 | // c = complex(0.5, 0.5) |
---|
| 9 | // k = calculate_point(c, kmax) |
---|
| 10 | // |
---|
| 11 | // Ole Nielsen, ANU 2004 |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | #include "Python.h" |
---|
| 15 | #include "numpy/arrayobject.h" |
---|
| 16 | |
---|
| 17 | // Computational function |
---|
| 18 | int _normalise_and_convert(double* A, |
---|
| 19 | int M, |
---|
| 20 | int N, |
---|
| 21 | PyObject* L, |
---|
| 22 | int kmax, |
---|
| 23 | int rgbmax, |
---|
| 24 | double exponent) { |
---|
| 25 | |
---|
| 26 | /* |
---|
| 27 | for i in range(A.shape[0]): |
---|
| 28 | for j in range(A.shape[1]): |
---|
| 29 | |
---|
| 30 | c = A[i,j]/kmax |
---|
| 31 | if c == 1: c = 0 #Map convergent point (kmax) to black (0) |
---|
| 32 | c = c**exponent #Morph slightly |
---|
| 33 | c = int(c * rgbmax) #Normalise to 256 levels per channel |
---|
| 34 | |
---|
| 35 | red = c / 256 / 256 |
---|
| 36 | green = (c / 256) % 256 |
---|
| 37 | blue = c % 256 |
---|
| 38 | |
---|
| 39 | L.append( (red, green, blue) ) */ |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | PyObject *tuple; |
---|
| 44 | int i, j, iN, red, green, blue, ci; |
---|
| 45 | double c; |
---|
| 46 | |
---|
| 47 | for (i=0; i<M; i++) { |
---|
| 48 | iN = i*N; |
---|
| 49 | |
---|
| 50 | for (j=0; j<N; j++) { |
---|
| 51 | |
---|
| 52 | c = A[iN + j]/kmax; //Normalise to unit interval |
---|
| 53 | c = pow(c, exponent); //Morph slightly |
---|
| 54 | ci = (int) (c*rgbmax); //Normalise to rgbmax levels per channel |
---|
| 55 | if (ci == rgbmax) ci = 0.0; //Map convergent point (kmax) to black (0) |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | //Convert to RGB |
---|
| 59 | red = ci / 256 / 256; |
---|
| 60 | green = (ci / 256) % 256; |
---|
| 61 | blue = ci % 256; |
---|
| 62 | |
---|
| 63 | tuple = Py_BuildValue("iii", red, green, blue); |
---|
| 64 | PyList_Append(L, tuple); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | return 0; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | // Interface to Python |
---|
| 73 | PyObject *normalise_and_convert(PyObject *self, PyObject *args) { |
---|
| 74 | |
---|
| 75 | //Called |
---|
| 76 | //normalise_and_convert(A, L, kmax, rgbmax, exponent) |
---|
| 77 | |
---|
| 78 | int kmax, rgbmax, M, N; |
---|
| 79 | double exponent; |
---|
| 80 | PyObject *L; |
---|
| 81 | PyArrayObject *A; |
---|
| 82 | |
---|
| 83 | // Convert Python arguments to C |
---|
| 84 | if (!PyArg_ParseTuple(args, "OOiid", &A, &L, &kmax, &rgbmax, &exponent)) |
---|
| 85 | return NULL; |
---|
| 86 | |
---|
| 87 | M = A -> dimensions[0]; |
---|
| 88 | N = A -> dimensions[1]; |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | // Call underlying routine |
---|
| 92 | _normalise_and_convert((double *) A -> data, M, N, L, |
---|
| 93 | kmax, rgbmax, exponent); |
---|
| 94 | |
---|
| 95 | // Return None |
---|
| 96 | return Py_BuildValue(""); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | // Method table for python module |
---|
| 101 | static struct PyMethodDef MethodTable[] = { |
---|
| 102 | {"normalise_and_convert", normalise_and_convert, METH_VARARGS}, |
---|
| 103 | {NULL, NULL} |
---|
| 104 | }; |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | // Module initialisation |
---|
| 108 | void initmandelplot_ext(void){ |
---|
| 109 | Py_InitModule("mandelplot_ext", MethodTable); |
---|
| 110 | import_array(); //Necessary for handling of numpy structures |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|