source: anuga_core/source/pypar-numeric/mandelbrot_example/mandelplot_ext.c @ 5779

Last change on this file since 5779 was 5779, checked in by steve, 16 years ago

Added the old version of pypar which works with Numeric. Necessary for parallel code until we move anuga to numpy (and then we can use pypar as distribute via sourceforge).

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