forked from scottransom/psrfits_utils
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswig_addedfunc.c
122 lines (114 loc) · 2.7 KB
/
swig_addedfunc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <Python.h>
#include "swig_addedfunc.h"
void convert2_float_array(float *result, PyObject *input, int size) {
int i;
if (!PySequence_Check(input)) {
printf("Expected a sequence\n");
exit(EXIT_FAILURE);
}
int length=PySequence_Length(input);
if (length > size) {
printf("Size mismatch.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < length; i++) {
PyObject *o = PySequence_GetItem(input,i);
if (PyNumber_Check(o)) {
result[i] = (float) PyFloat_AsDouble(o);
} else {
printf("Sequence elements must be numbers\n");
free(result);
exit(EXIT_FAILURE);
}
free(o);
}
}
void convert2_double_array(double *result, PyObject *input, int size) {
int i;
if (!PySequence_Check(input)) {
printf("Expected a sequence\n");
exit(EXIT_FAILURE);
}
int length=PySequence_Length(input);
if (length > size) {
printf("Size mismatch.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < length; i++) {
PyObject *o = PySequence_GetItem(input,i);
if (PyNumber_Check(o)) {
result[i] = (double) PyFloat_AsDouble(o);
} else {
printf("Sequence elements must be numbers\n");
free(result);
exit(EXIT_FAILURE);
}
free(o);
}
}
void print_float_array(float *input,int size)
{
int i;
for(i=1;i<size;++i)
{
printf("%.07f\n",input[i]-input[i-1]);
}
}
void print_double_array(double *input,int size)
{
int i;
for(i=1;i<size;++i)
{
printf("%.14f\n",input[i]-input[i-1]);
}
}
unsigned char *convert_uchar_array(unsigned char *result,PyObject *input, int size) {
int i;
if (!PySequence_Check(input)) {
printf("Expected a sequence\n");
exit(EXIT_FAILURE);
}
int length=PySequence_Length(input);
if (length > size) {
printf("Size mismatch.\n");
exit(EXIT_FAILURE);
}
// result = (unsigned char *) malloc(size*sizeof(unsigned char));
if(result==NULL)
{
fprintf(stderr,"Unable to allocate %d bytes.\n",(size*sizeof(unsigned char)));
return result;
}
for (i = 0; i < length; i++) {
PyObject *o = PySequence_GetItem(input,i);
if (PyNumber_Check(o)) {
if(PyFloat_AsDouble(o)>255)
result[i] = (unsigned char) 255;
else
result[i] = (unsigned char) PyFloat_AsDouble(o);
} else {
PyErr_SetString(PyExc_ValueError,"uchar: Sequence elements must be numbers");
free(result);
return NULL;
}
free(o);
}
return result;
}
void print_uchar_array(unsigned char *input,int size)
{
int i;
for(i=0;i<size;++i)
{
printf("%u\n",input[i]);
}
}
long double get_ld(double in)
{
long double result=(long double) in;
return result;
}
void set_float_value(float *array,int index, float value)
{
array[index]=value;
}