_imagingmath.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "libImaging/Imaging.h"
  17. #include "math.h"
  18. #include "float.h"
  19. #define MAX_INT32 2147483647.0
  20. #define MIN_INT32 -2147483648.0
  21. #define UNOP(name, op, type) \
  22. void name(Imaging out, Imaging im1) { \
  23. int x, y; \
  24. for (y = 0; y < out->ysize; y++) { \
  25. type *p0 = (type *)out->image[y]; \
  26. type *p1 = (type *)im1->image[y]; \
  27. for (x = 0; x < out->xsize; x++) { \
  28. *p0 = op(type, *p1); \
  29. p0++; \
  30. p1++; \
  31. } \
  32. } \
  33. }
  34. #define BINOP(name, op, type) \
  35. void name(Imaging out, Imaging im1, Imaging im2) { \
  36. int x, y; \
  37. for (y = 0; y < out->ysize; y++) { \
  38. type *p0 = (type *)out->image[y]; \
  39. type *p1 = (type *)im1->image[y]; \
  40. type *p2 = (type *)im2->image[y]; \
  41. for (x = 0; x < out->xsize; x++) { \
  42. *p0 = op(type, *p1, *p2); \
  43. p0++; \
  44. p1++; \
  45. 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
  75. powi(int x, int y) {
  76. double v = pow(x, y) + 0.5;
  77. if (errno == EDOM) {
  78. return 0;
  79. }
  80. if (v < MIN_INT32) {
  81. v = MIN_INT32;
  82. } else if (v > MAX_INT32) {
  83. v = MAX_INT32;
  84. }
  85. return (int)v;
  86. }
  87. #define POW_I(type, v1, v2) powi(v1, v2)
  88. #define POW_F(type, v1, v2) powf(v1, v2) /* FIXME: EDOM handling */
  89. #define DIFF_I(type, v1, v2) abs((v1) - (v2))
  90. #define DIFF_F(type, v1, v2) fabs((v1) - (v2))
  91. #define EQ(type, v1, v2) (v1) == (v2)
  92. #define NE(type, v1, v2) (v1) != (v2)
  93. #define LT(type, v1, v2) (v1) < (v2)
  94. #define LE(type, v1, v2) (v1) <= (v2)
  95. #define GT(type, v1, v2) (v1) > (v2)
  96. #define GE(type, v1, v2) (v1) >= (v2)
  97. UNOP(abs_I, ABS_I, INT32)
  98. UNOP(neg_I, NEG, INT32)
  99. BINOP(add_I, ADD, INT32)
  100. BINOP(sub_I, SUB, INT32)
  101. BINOP(mul_I, MUL, INT32)
  102. BINOP(div_I, DIV_I, INT32)
  103. BINOP(mod_I, MOD_I, INT32)
  104. BINOP(pow_I, POW_I, INT32)
  105. BINOP(diff_I, DIFF_I, INT32)
  106. UNOP(invert_I, INVERT, INT32)
  107. BINOP(and_I, AND, INT32)
  108. BINOP(or_I, OR, INT32)
  109. BINOP(xor_I, XOR, INT32)
  110. BINOP(lshift_I, LSHIFT, INT32)
  111. BINOP(rshift_I, RSHIFT, INT32)
  112. BINOP(min_I, MIN, INT32)
  113. BINOP(max_I, MAX, INT32)
  114. BINOP(eq_I, EQ, INT32)
  115. BINOP(ne_I, NE, INT32)
  116. BINOP(lt_I, LT, INT32)
  117. BINOP(le_I, LE, INT32)
  118. BINOP(gt_I, GT, INT32)
  119. BINOP(ge_I, GE, INT32)
  120. UNOP(abs_F, ABS_F, FLOAT32)
  121. UNOP(neg_F, NEG, FLOAT32)
  122. BINOP(add_F, ADD, FLOAT32)
  123. BINOP(sub_F, SUB, FLOAT32)
  124. BINOP(mul_F, MUL, FLOAT32)
  125. BINOP(div_F, DIV_F, FLOAT32)
  126. BINOP(mod_F, MOD_F, FLOAT32)
  127. BINOP(pow_F, POW_F, FLOAT32)
  128. BINOP(diff_F, DIFF_F, FLOAT32)
  129. BINOP(min_F, MIN, FLOAT32)
  130. BINOP(max_F, MAX, FLOAT32)
  131. BINOP(eq_F, EQ, FLOAT32)
  132. BINOP(ne_F, NE, FLOAT32)
  133. BINOP(lt_F, LT, FLOAT32)
  134. BINOP(le_F, LE, FLOAT32)
  135. BINOP(gt_F, GT, FLOAT32)
  136. BINOP(ge_F, GE, FLOAT32)
  137. static PyObject *
  138. _unop(PyObject *self, PyObject *args) {
  139. Imaging out;
  140. Imaging im1;
  141. void (*unop)(Imaging, Imaging);
  142. Py_ssize_t op, i0, i1;
  143. if (!PyArg_ParseTuple(args, "nnn", &op, &i0, &i1)) {
  144. return NULL;
  145. }
  146. out = (Imaging)i0;
  147. im1 = (Imaging)i1;
  148. unop = (void *)op;
  149. unop(out, im1);
  150. Py_INCREF(Py_None);
  151. return Py_None;
  152. }
  153. static PyObject *
  154. _binop(PyObject *self, PyObject *args) {
  155. Imaging out;
  156. Imaging im1;
  157. Imaging im2;
  158. void (*binop)(Imaging, Imaging, Imaging);
  159. Py_ssize_t op, i0, i1, i2;
  160. if (!PyArg_ParseTuple(args, "nnnn", &op, &i0, &i1, &i2)) {
  161. return NULL;
  162. }
  163. out = (Imaging)i0;
  164. im1 = (Imaging)i1;
  165. im2 = (Imaging)i2;
  166. binop = (void *)op;
  167. binop(out, im1, im2);
  168. Py_INCREF(Py_None);
  169. return Py_None;
  170. }
  171. static PyMethodDef _functions[] = {
  172. {"unop", _unop, 1}, {"binop", _binop, 1}, {NULL, NULL}};
  173. static void
  174. install(PyObject *d, char *name, void *value) {
  175. PyObject *v = PyLong_FromSsize_t((Py_ssize_t)value);
  176. if (!v || PyDict_SetItemString(d, name, v)) {
  177. PyErr_Clear();
  178. }
  179. Py_XDECREF(v);
  180. }
  181. static int
  182. setup_module(PyObject *m) {
  183. PyObject *d = PyModule_GetDict(m);
  184. install(d, "abs_I", abs_I);
  185. install(d, "neg_I", neg_I);
  186. install(d, "add_I", add_I);
  187. install(d, "sub_I", sub_I);
  188. install(d, "diff_I", diff_I);
  189. install(d, "mul_I", mul_I);
  190. install(d, "div_I", div_I);
  191. install(d, "mod_I", mod_I);
  192. install(d, "min_I", min_I);
  193. install(d, "max_I", max_I);
  194. install(d, "pow_I", pow_I);
  195. install(d, "invert_I", invert_I);
  196. install(d, "and_I", and_I);
  197. install(d, "or_I", or_I);
  198. install(d, "xor_I", xor_I);
  199. install(d, "lshift_I", lshift_I);
  200. install(d, "rshift_I", rshift_I);
  201. install(d, "eq_I", eq_I);
  202. install(d, "ne_I", ne_I);
  203. install(d, "lt_I", lt_I);
  204. install(d, "le_I", le_I);
  205. install(d, "gt_I", gt_I);
  206. install(d, "ge_I", ge_I);
  207. install(d, "abs_F", abs_F);
  208. install(d, "neg_F", neg_F);
  209. install(d, "add_F", add_F);
  210. install(d, "sub_F", sub_F);
  211. install(d, "diff_F", diff_F);
  212. install(d, "mul_F", mul_F);
  213. install(d, "div_F", div_F);
  214. install(d, "mod_F", mod_F);
  215. install(d, "min_F", min_F);
  216. install(d, "max_F", max_F);
  217. install(d, "pow_F", pow_F);
  218. install(d, "eq_F", eq_F);
  219. install(d, "ne_F", ne_F);
  220. install(d, "lt_F", lt_F);
  221. install(d, "le_F", le_F);
  222. install(d, "gt_F", gt_F);
  223. install(d, "ge_F", ge_F);
  224. return 0;
  225. }
  226. PyMODINIT_FUNC
  227. PyInit__imagingmath(void) {
  228. PyObject *m;
  229. static PyModuleDef module_def = {
  230. PyModuleDef_HEAD_INIT,
  231. "_imagingmath", /* m_name */
  232. NULL, /* m_doc */
  233. -1, /* m_size */
  234. _functions, /* m_methods */
  235. };
  236. m = PyModule_Create(&module_def);
  237. if (setup_module(m) < 0) {
  238. return NULL;
  239. }
  240. return m;
  241. }