_imagingmath.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * The Python Imaging Library
  3. *
  4. * a simple math add-on for the Python Imaging Library
  5. *
  6. * history:
  7. * 1999-02-15 fl Created
  8. * 2005-05-05 fl Simplified and cleaned up for PIL 1.1.6
  9. *
  10. * Copyright (c) 1999-2005 by Secret Labs AB
  11. * Copyright (c) 2005 by Fredrik Lundh
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Python.h"
  16. #include "Imaging.h"
  17. #include "py3.h"
  18. #include "math.h"
  19. #include "float.h"
  20. #define MAX_INT32 2147483647.0
  21. #define MIN_INT32 -2147483648.0
  22. #define UNOP(name, op, type)\
  23. void name(Imaging out, Imaging im1)\
  24. {\
  25. int x, y;\
  26. for (y = 0; y < out->ysize; y++) {\
  27. type* p0 = (type*) out->image[y];\
  28. type* p1 = (type*) im1->image[y];\
  29. for (x = 0; x < out->xsize; x++) {\
  30. *p0 = op(type, *p1);\
  31. p0++; p1++;\
  32. }\
  33. }\
  34. }
  35. #define BINOP(name, op, type)\
  36. void name(Imaging out, Imaging im1, Imaging im2)\
  37. {\
  38. int x, y;\
  39. for (y = 0; y < out->ysize; y++) {\
  40. type* p0 = (type*) out->image[y];\
  41. type* p1 = (type*) im1->image[y];\
  42. type* p2 = (type*) im2->image[y];\
  43. for (x = 0; x < out->xsize; x++) {\
  44. *p0 = op(type, *p1, *p2);\
  45. p0++; p1++; p2++;\
  46. }\
  47. }\
  48. }
  49. #define NEG(type, v1) -(v1)
  50. #define INVERT(type, v1) ~(v1)
  51. #define ADD(type, v1, v2) (v1)+(v2)
  52. #define SUB(type, v1, v2) (v1)-(v2)
  53. #define MUL(type, v1, v2) (v1)*(v2)
  54. #define MIN(type, v1, v2) ((v1)<(v2))?(v1):(v2)
  55. #define MAX(type, v1, v2) ((v1)>(v2))?(v1):(v2)
  56. #define AND(type, v1, v2) (v1)&(v2)
  57. #define OR(type, v1, v2) (v1)|(v2)
  58. #define XOR(type, v1, v2) (v1)^(v2)
  59. #define LSHIFT(type, v1, v2) (v1)<<(v2)
  60. #define RSHIFT(type, v1, v2) (v1)>>(v2)
  61. #define ABS_I(type, v1) abs((v1))
  62. #define ABS_F(type, v1) fabs((v1))
  63. /* --------------------------------------------------------------------
  64. * some day, we should add FPE protection mechanisms. see pyfpe.h for
  65. * details.
  66. *
  67. * PyFPE_START_PROTECT("Error in foobar", return 0)
  68. * PyFPE_END_PROTECT(result)
  69. */
  70. #define DIV_I(type, v1, v2) ((v2)!=0)?(v1)/(v2):0
  71. #define DIV_F(type, v1, v2) ((v2)!=0.0F)?(v1)/(v2):0.0F
  72. #define MOD_I(type, v1, v2) ((v2)!=0)?(v1)%(v2):0
  73. #define MOD_F(type, v1, v2) ((v2)!=0.0F)?fmod((v1),(v2)):0.0F
  74. static int powi(int x, int y)
  75. {
  76. double v = pow(x, y) + 0.5;
  77. if (errno == EDOM)
  78. return 0;
  79. if (v < MIN_INT32)
  80. v = MIN_INT32;
  81. else if (v > MAX_INT32)
  82. v = MAX_INT32;
  83. return (int) v;
  84. }
  85. #define POW_I(type, v1, v2) powi(v1, v2)
  86. #define POW_F(type, v1, v2) powf(v1, v2) /* FIXME: EDOM handling */
  87. #define DIFF_I(type, v1, v2) abs((v1)-(v2))
  88. #define DIFF_F(type, v1, v2) fabs((v1)-(v2))
  89. #define EQ(type, v1, v2) (v1)==(v2)
  90. #define NE(type, v1, v2) (v1)!=(v2)
  91. #define LT(type, v1, v2) (v1)<(v2)
  92. #define LE(type, v1, v2) (v1)<=(v2)
  93. #define GT(type, v1, v2) (v1)>(v2)
  94. #define GE(type, v1, v2) (v1)>=(v2)
  95. UNOP(abs_I, ABS_I, INT32)
  96. UNOP(neg_I, NEG, INT32)
  97. BINOP(add_I, ADD, INT32)
  98. BINOP(sub_I, SUB, INT32)
  99. BINOP(mul_I, MUL, INT32)
  100. BINOP(div_I, DIV_I, INT32)
  101. BINOP(mod_I, MOD_I, INT32)
  102. BINOP(pow_I, POW_I, INT32)
  103. BINOP(diff_I, DIFF_I, INT32)
  104. UNOP(invert_I, INVERT, INT32)
  105. BINOP(and_I, AND, INT32)
  106. BINOP(or_I, OR, INT32)
  107. BINOP(xor_I, XOR, INT32)
  108. BINOP(lshift_I, LSHIFT, INT32)
  109. BINOP(rshift_I, RSHIFT, INT32)
  110. BINOP(min_I, MIN, INT32)
  111. BINOP(max_I, MAX, INT32)
  112. BINOP(eq_I, EQ, INT32)
  113. BINOP(ne_I, NE, INT32)
  114. BINOP(lt_I, LT, INT32)
  115. BINOP(le_I, LE, INT32)
  116. BINOP(gt_I, GT, INT32)
  117. BINOP(ge_I, GE, INT32)
  118. UNOP(abs_F, ABS_F, FLOAT32)
  119. UNOP(neg_F, NEG, FLOAT32)
  120. BINOP(add_F, ADD, FLOAT32)
  121. BINOP(sub_F, SUB, FLOAT32)
  122. BINOP(mul_F, MUL, FLOAT32)
  123. BINOP(div_F, DIV_F, FLOAT32)
  124. BINOP(mod_F, MOD_F, FLOAT32)
  125. BINOP(pow_F, POW_F, FLOAT32)
  126. BINOP(diff_F, DIFF_F, FLOAT32)
  127. BINOP(min_F, MIN, FLOAT32)
  128. BINOP(max_F, MAX, FLOAT32)
  129. BINOP(eq_F, EQ, FLOAT32)
  130. BINOP(ne_F, NE, FLOAT32)
  131. BINOP(lt_F, LT, FLOAT32)
  132. BINOP(le_F, LE, FLOAT32)
  133. BINOP(gt_F, GT, FLOAT32)
  134. BINOP(ge_F, GE, FLOAT32)
  135. static PyObject *
  136. _unop(PyObject* self, PyObject* args)
  137. {
  138. Imaging out;
  139. Imaging im1;
  140. void (*unop)(Imaging, Imaging);
  141. Py_ssize_t op, i0, i1;
  142. if (!PyArg_ParseTuple(args, "nnn", &op, &i0, &i1))
  143. return NULL;
  144. out = (Imaging) i0;
  145. im1 = (Imaging) i1;
  146. unop = (void*) op;
  147. unop(out, im1);
  148. Py_INCREF(Py_None);
  149. return Py_None;
  150. }
  151. static PyObject *
  152. _binop(PyObject* self, PyObject* args)
  153. {
  154. Imaging out;
  155. Imaging im1;
  156. Imaging im2;
  157. void (*binop)(Imaging, Imaging, Imaging);
  158. Py_ssize_t op, i0, i1, i2;
  159. if (!PyArg_ParseTuple(args, "nnnn", &op, &i0, &i1, &i2))
  160. return NULL;
  161. out = (Imaging) i0;
  162. im1 = (Imaging) i1;
  163. im2 = (Imaging) i2;
  164. binop = (void*) op;
  165. binop(out, im1, im2);
  166. Py_INCREF(Py_None);
  167. return Py_None;
  168. }
  169. static PyMethodDef _functions[] = {
  170. {"unop", _unop, 1},
  171. {"binop", _binop, 1},
  172. {NULL, NULL}
  173. };
  174. static void
  175. install(PyObject *d, char* name, void* value)
  176. {
  177. PyObject *v = PyInt_FromSsize_t((Py_ssize_t) value);
  178. if (!v || PyDict_SetItemString(d, name, v))
  179. PyErr_Clear();
  180. Py_XDECREF(v);
  181. }
  182. static int
  183. setup_module(PyObject* m) {
  184. PyObject* d = PyModule_GetDict(m);
  185. install(d, "abs_I", abs_I);
  186. install(d, "neg_I", neg_I);
  187. install(d, "add_I", add_I);
  188. install(d, "sub_I", sub_I);
  189. install(d, "diff_I", diff_I);
  190. install(d, "mul_I", mul_I);
  191. install(d, "div_I", div_I);
  192. install(d, "mod_I", mod_I);
  193. install(d, "min_I", min_I);
  194. install(d, "max_I", max_I);
  195. install(d, "pow_I", pow_I);
  196. install(d, "invert_I", invert_I);
  197. install(d, "and_I", and_I);
  198. install(d, "or_I", or_I);
  199. install(d, "xor_I", xor_I);
  200. install(d, "lshift_I", lshift_I);
  201. install(d, "rshift_I", rshift_I);
  202. install(d, "eq_I", eq_I);
  203. install(d, "ne_I", ne_I);
  204. install(d, "lt_I", lt_I);
  205. install(d, "le_I", le_I);
  206. install(d, "gt_I", gt_I);
  207. install(d, "ge_I", ge_I);
  208. install(d, "abs_F", abs_F);
  209. install(d, "neg_F", neg_F);
  210. install(d, "add_F", add_F);
  211. install(d, "sub_F", sub_F);
  212. install(d, "diff_F", diff_F);
  213. install(d, "mul_F", mul_F);
  214. install(d, "div_F", div_F);
  215. install(d, "mod_F", mod_F);
  216. install(d, "min_F", min_F);
  217. install(d, "max_F", max_F);
  218. install(d, "pow_F", pow_F);
  219. install(d, "eq_F", eq_F);
  220. install(d, "ne_F", ne_F);
  221. install(d, "lt_F", lt_F);
  222. install(d, "le_F", le_F);
  223. install(d, "gt_F", gt_F);
  224. install(d, "ge_F", ge_F);
  225. return 0;
  226. }
  227. #if PY_VERSION_HEX >= 0x03000000
  228. PyMODINIT_FUNC
  229. PyInit__imagingmath(void) {
  230. PyObject* m;
  231. static PyModuleDef module_def = {
  232. PyModuleDef_HEAD_INIT,
  233. "_imagingmath", /* m_name */
  234. NULL, /* m_doc */
  235. -1, /* m_size */
  236. _functions, /* m_methods */
  237. };
  238. m = PyModule_Create(&module_def);
  239. if (setup_module(m) < 0)
  240. return NULL;
  241. return m;
  242. }
  243. #else
  244. PyMODINIT_FUNC
  245. init_imagingmath(void)
  246. {
  247. PyObject* m = Py_InitModule("_imagingmath", _functions);
  248. setup_module(m);
  249. }
  250. #endif