annotate Modules/imageop.c @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents fd25030223b0
children 787b969d37f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* imageopmodule - Various operations on pictures */
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #ifdef sun
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #define signed
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #endif
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
8 #include "Python.h"
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
10 #if SIZEOF_INT == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
11 typedef int Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
12 typedef unsigned int Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
13 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
14 #if SIZEOF_LONG == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
15 typedef long Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
16 typedef unsigned long Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
17 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
18 #error "No 4-byte integral type"
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
19 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
20 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
21
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
3344
3b484812acf6 Added 16-bit pixel support for some operations.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
23 #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
24 #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
26 static PyObject *ImageopError;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
27 static PyObject *ImageopDict;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
28
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
29 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
30 * Check a coordonnate, make sure that (0 < value).
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
31 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
32 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
33 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
34 check_coordonnate(int value, const char* name)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
35 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
36 if ( 0 < value)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
37 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
38 PyErr_Format(PyExc_ValueError, "%s value is negative or nul", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
39 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
40 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
41
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
42 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
43 * Check integer overflow to make sure that product == x*y*size.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
44 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
45 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
46 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
47 check_multiply_size(int product, int x, const char* xname, int y, const char* yname, int size)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
48 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
49 if ( !check_coordonnate(x, xname) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
50 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
51 if ( !check_coordonnate(y, yname) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
52 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
53 if ( size == (product / y) / x )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
54 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
55 PyErr_SetString(ImageopError, "String has incorrect length");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
56 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
57 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
58
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
59 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
60 * Check integer overflow to make sure that product == x*y.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
61 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
62 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
63 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
64 check_multiply(int product, int x, int y)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
65 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
66 return check_multiply_size(product, x, "x", y, "y", 1);
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
67 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
68
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
69 /* If this function returns true (the default if anything goes wrong), we're
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
70 behaving in a backward-compatible way with respect to how multi-byte pixels
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
71 are stored in the strings. The code in this module was originally written
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
72 for an SGI which is a big-endian system, and so the old code assumed that
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
73 4-byte integers hold the R, G, and B values in a particular order.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
74 However, on little-endian systems the order is reversed, and so not
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
75 actually compatible with what gl.lrectwrite and imgfile expect.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
76 (gl.lrectwrite and imgfile are also SGI-specific, however, it is
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
77 conceivable that the data handled here comes from or goes to an SGI or that
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
78 it is otherwise used in the expectation that the byte order in the strings
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
79 is as specified.)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
80
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
81 The function returns the value of the module variable
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
82 "backward_compatible", or 1 if the variable does not exist or is not an
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
83 int.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
84 */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
85
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
86 static int
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
87 imageop_backward_compatible(void)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
88 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
89 static PyObject *bcos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
90 PyObject *bco;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
91 long rc;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
92
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
93 if (ImageopDict == NULL) /* "cannot happen" */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
94 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
95 if (bcos == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
96 /* cache string object for future use */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
97 bcos = PyString_FromString("backward_compatible");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
98 if (bcos == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
99 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
100 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
101 bco = PyDict_GetItem(ImageopDict, bcos);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
102 if (bco == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
103 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
104 if (!PyInt_Check(bco))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
105 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
106 rc = PyInt_AsLong(bco);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
107 if (PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
108 /* not an integer, or too large, or something */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
109 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
110 rc = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
111 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
112 return rc != 0; /* convert to values 0, 1 */
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
113 }
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
114
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
115 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
116 imageop_crop(PyObject *self, PyObject *args)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
118 char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
119 short *nsp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
120 Py_Int32 *nlp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
121 int len, size, x, y, newx1, newx2, newy1, newy2, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
122 int ix, iy, xstep, ystep;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
123 PyObject *rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
125 if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
126 &newx1, &newy1, &newx2, &newy2) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
127 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
128
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
129 if ( size != 1 && size != 2 && size != 4 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
130 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
131 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
132 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
133 if ( !check_multiply_size(len, x, "x", y, "y", size) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
134 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
135
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
136 xstep = (newx1 < newx2)? 1 : -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
137 ystep = (newy1 < newy2)? 1 : -1;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
138
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
139 nlen = (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
140 if ( !check_multiply_size(nlen, abs(newx2-newx1)+1, "abs(newx2-newx1)+1", abs(newy2-newy1)+1, "abs(newy2-newy1)+1", size) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
141 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
142 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
143 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
144 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
145 ncp = (char *)PyString_AsString(rv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
146 nsp = (short *)ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
147 nlp = (Py_Int32 *)ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
148 newy2 += ystep;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
149 newx2 += xstep;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
150 for( iy = newy1; iy != newy2; iy+=ystep ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
151 for ( ix = newx1; ix != newx2; ix+=xstep ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
152 if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
153 if ( size == 1 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
154 *ncp++ = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
155 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
156 *nlp++ = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
157 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
158 if ( size == 1 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
159 *ncp++ = *CHARP(cp, x, ix, iy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
160 else if ( size == 2 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
161 *nsp++ = *SHORTP(cp, x, ix, iy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
162 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
163 *nlp++ = *LONGP(cp, x, ix, iy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
164 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
165 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
166 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
167 return rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
169
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
170 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
171 imageop_scale(PyObject *self, PyObject *args)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
173 char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
174 short *nsp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
175 Py_Int32 *nlp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
176 int len, size, x, y, newx, newy, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
177 int ix, iy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
178 int oix, oiy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
179 PyObject *rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
181 if ( !PyArg_ParseTuple(args, "s#iiiii",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
182 &cp, &len, &size, &x, &y, &newx, &newy) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
183 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
184
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
185 if ( size != 1 && size != 2 && size != 4 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
186 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
187 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
188 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
189 if ( !check_multiply_size(len, x, "x", y, "y", size) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
190 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
191 nlen = newx*newy*size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
192 if ( !check_multiply_size(nlen, newx, "newx", newy, "newy", size) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
193 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
194
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
195 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
196 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
197 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
198 ncp = (char *)PyString_AsString(rv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
199 nsp = (short *)ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
200 nlp = (Py_Int32 *)ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
201 for( iy = 0; iy < newy; iy++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
202 for ( ix = 0; ix < newx; ix++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
203 oix = ix * x / newx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
204 oiy = iy * y / newy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
205 if ( size == 1 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
206 *ncp++ = *CHARP(cp, x, oix, oiy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
207 else if ( size == 2 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
208 *nsp++ = *SHORTP(cp, x, oix, oiy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
209 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
210 *nlp++ = *LONGP(cp, x, oix, oiy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
211 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
212 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
213 return rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
216 /* Note: this routine can use a bit of optimizing */
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
217
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
218 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
219 imageop_tovideo(PyObject *self, PyObject *args)
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
220 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
221 int maxx, maxy, x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
222 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
223 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
224 int width;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
225 PyObject *rv;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
226
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
227
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
228 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
229 return 0;
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
230
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
231 if ( width != 1 && width != 4 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
232 PyErr_SetString(ImageopError, "Size should be 1 or 4");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
233 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
234 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
235 if ( !check_multiply_size(len, maxx, "max", maxy, "maxy", width) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
236 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
237
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
238 rv = PyString_FromStringAndSize(NULL, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
239 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
240 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
241 ncp = (unsigned char *)PyString_AsString(rv);
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
242
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
243 if ( width == 1 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
244 memcpy(ncp, cp, maxx); /* Copy first line */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
245 ncp += maxx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
246 for (y=1; y<maxy; y++) { /* Interpolate other lines */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
247 for(x=0; x<maxx; x++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
248 i = y*maxx + x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
249 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
250 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
251 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
252 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
253 memcpy(ncp, cp, maxx*4); /* Copy first line */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
254 ncp += maxx*4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
255 for (y=1; y<maxy; y++) { /* Interpolate other lines */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
256 for(x=0; x<maxx; x++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
257 i = (y*maxx + x)*4 + 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
258 *ncp++ = 0; /* Skip alfa comp */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
259 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
260 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
261 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
262 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
263 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
264 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
265 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
266 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
267 return rv;
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
268 }
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
269
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
270 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
271 imageop_grey2mono(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
272 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
273 int tres, x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
274 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
275 unsigned char ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
276 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
277 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
278
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
279
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
280 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
281 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
282
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
283 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
284 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
285
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
286 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
287 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
288 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
289 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
290
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
291 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
292 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
293 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
294 if ( (int)cp[i] > tres )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
295 ovalue |= bit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
296 bit >>= 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
297 if ( bit == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
298 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
299 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
300 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
301 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
302 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
303 if ( bit != 0x80 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
304 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
305 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
306 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
307
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
308 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
309 imageop_grey2grey4(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
310 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
311 int x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
312 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
313 unsigned char ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
314 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
315 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
316 int pos;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
317
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
318
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
319 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
320 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
321
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
322 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
323 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
324
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
325 rv = PyString_FromStringAndSize(NULL, (len+1)/2);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
326 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
327 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
328 ncp = (unsigned char *)PyString_AsString(rv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
329 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
330 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
331 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
332 ovalue |= ((int)cp[i] & 0xf0) >> pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
333 pos += 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
334 if ( pos == 8 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
335 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
336 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
337 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
338 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
339 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
340 if ( pos != 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
341 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
342 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
343 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
344
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
345 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
346 imageop_grey2grey2(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
347 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
348 int x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
349 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
350 unsigned char ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
351 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
352 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
353 int pos;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
354
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
355
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
356 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
357 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
358
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
359 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
360 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
361
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
362 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
363 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
364 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
365 ncp = (unsigned char *)PyString_AsString(rv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
366 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
367 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
368 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
369 ovalue |= ((int)cp[i] & 0xc0) >> pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
370 pos += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
371 if ( pos == 8 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
372 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
373 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
374 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
375 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
376 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
377 if ( pos != 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
378 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
379 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
380 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
381
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
382 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
383 imageop_dither2mono(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
384 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
385 int sum, x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
386 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
387 unsigned char ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
388 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
389 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
390
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
391
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
392 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
393 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
394
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
395 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
396 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
397
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
398 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
399 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
400 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
401 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
402
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
403 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
404 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
405 sum = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
406 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
407 sum += cp[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
408 if ( sum >= 256 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
409 sum -= 256;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
410 ovalue |= bit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
411 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
412 bit >>= 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
413 if ( bit == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
414 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
415 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
416 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
417 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
418 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
419 if ( bit != 0x80 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
420 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
421 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
422 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
423
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
424 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
425 imageop_dither2grey2(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
426 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
427 int x, y, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
428 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
429 unsigned char ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
430 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
431 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
432 int pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
433 int sum = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
434
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
435
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
436 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
437 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
438
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
439 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
440 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
441
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
442 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
443 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
444 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
445 ncp = (unsigned char *)PyString_AsString(rv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
446 pos = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
447 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
448 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
449 sum += cp[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
450 nvalue = sum & 0x180;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
451 sum -= nvalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
452 ovalue |= nvalue >> pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
453 pos += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
454 if ( pos == 9 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
455 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
456 ovalue = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
457 pos = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
458 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
459 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
460 if ( pos != 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
461 *ncp++ = ovalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
462 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
463 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
464
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
465 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
466 imageop_mono2grey(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
467 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
468 int v0, v1, x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
469 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
470 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
471 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
472
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
473 if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
474 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
475
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
476 nlen = x*y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
477 if ( !check_multiply(nlen, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
478 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
479 if ( (nlen+7)/8 != len ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
480 PyErr_SetString(ImageopError, "String has incorrect length");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
481 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
482 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
483
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
484 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
485 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
486 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
487 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
488
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
489 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
490 for ( i=0; i < nlen; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
491 if ( *cp & bit )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
492 *ncp++ = v1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
493 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
494 *ncp++ = v0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
495 bit >>= 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
496 if ( bit == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
497 bit = 0x80;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
498 cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
499 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
500 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
501 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
502 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
503
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
504 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
505 imageop_grey22grey(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
506 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
507 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
508 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
509 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
510 int i, pos, value = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
511
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
512 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
513 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
514
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
515 nlen = x*y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
516 if ( !check_multiply(nlen, x, y) ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
517 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
518 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
519 if ( (nlen+3)/4 != len ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
520 PyErr_SetString(ImageopError, "String has incorrect length");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
521 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
522 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
523
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
524 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
525 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
526 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
527 ncp = (unsigned char *)PyString_AsString(rv);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
528
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
529 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
530 for ( i=0; i < nlen; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
531 if ( pos == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
532 value = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
533 pos = 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
534 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
535 pos -= 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
536 nvalue = (value >> pos) & 0x03;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
537 *ncp++ = nvalue | (nvalue << 2) |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
538 (nvalue << 4) | (nvalue << 6);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
539 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
540 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
541 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
542
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
543 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
544 imageop_grey42grey(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
545 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
546 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
547 unsigned char *cp, *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
548 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
549 int i, pos, value = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
550
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
551 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
552 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
553
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
554 nlen = x*y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
555 if ( !check_multiply(nlen, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
556 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
557 if ( (nlen+1)/2 != len ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
558 PyErr_SetString(ImageopError, "String has incorrect length");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
559 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
560 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
561
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
562 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
563 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
564 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
565 ncp = (unsigned char *)PyString_AsString(rv);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
566
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
567 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
568 for ( i=0; i < nlen; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
569 if ( pos == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
570 value = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
571 pos = 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
572 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
573 pos -= 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
574 nvalue = (value >> pos) & 0x0f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
575 *ncp++ = nvalue | (nvalue << 4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
576 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
577 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
578 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
579
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
580 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
581 imageop_rgb2rgb8(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
582 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
583 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
584 unsigned char *cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
585 unsigned char *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
586 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
587 int i, r, g, b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
588 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
589
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
590 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
591 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
592
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
593 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
594 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
595 nlen = x*y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
596 if ( !check_multiply(nlen, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
597 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
598
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
599 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
600 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
601 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
602 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
603
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
604 for ( i=0; i < nlen; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
605 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
606 if (backward_compatible) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
607 Py_UInt32 value = * (Py_UInt32 *) cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
608 cp += 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
609 r = (int) ((value & 0xff) / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
610 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
611 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
612 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
613 cp++; /* skip alpha channel */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
614 b = (int) (*cp++ / 255. * 3. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
615 g = (int) (*cp++ / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
616 r = (int) (*cp++ / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
617 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
618 *ncp++ = (unsigned char)((r<<5) | (b<<3) | g);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
619 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
620 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
621 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
622
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
623 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
624 imageop_rgb82rgb(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
625 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
626 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
627 unsigned char *cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
628 unsigned char *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
629 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
630 int i, r, g, b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
631 unsigned char value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
632 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
633
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
634 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
635 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
636
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
637 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
638 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
639 nlen = x*y*4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
640 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
641 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
642
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
643 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
644 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
645 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
646 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
647
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
648 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
649 /* Bits in source: RRRBBGGG
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
650 ** Red and Green are multiplied by 36.5, Blue by 85
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
651 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
652 value = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
653 r = (value >> 5) & 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
654 g = (value ) & 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
655 b = (value >> 3) & 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
656 r = (r<<5) | (r<<3) | (r>>1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
657 g = (g<<5) | (g<<3) | (g>>1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
658 b = (b<<6) | (b<<4) | (b<<2) | b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
659 if (backward_compatible) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
660 Py_UInt32 nvalue = r | (g<<8) | (b<<16);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
661 * (Py_UInt32 *) ncp = nvalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
662 ncp += 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
663 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
664 *ncp++ = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
665 *ncp++ = b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
666 *ncp++ = g;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
667 *ncp++ = r;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
668 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
669 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
670 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
671 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
672
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
673 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
674 imageop_rgb2grey(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
675 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
676 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
677 unsigned char *cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
678 unsigned char *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
679 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
680 int i, r, g, b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
681 int nvalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
682 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
683
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
684 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
685 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
686
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
687 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
688 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
689 nlen = x*y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
690 if ( !check_multiply(nlen, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
691 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
692
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
693 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
694 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
695 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
696 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
697
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
698 for ( i=0; i < nlen; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
699 if (backward_compatible) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
700 Py_UInt32 value = * (Py_UInt32 *) cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
701 cp += 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
702 r = (int) ((value & 0xff) / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
703 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
704 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
705 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
706 cp++; /* skip alpha channel */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
707 b = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
708 g = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
709 r = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
710 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
711 nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
712 if ( nvalue > 255 ) nvalue = 255;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
713 *ncp++ = (unsigned char)nvalue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
714 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
715 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
716 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
717
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
718 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
719 imageop_grey2rgb(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
720 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
721 int x, y, len, nlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
722 unsigned char *cp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
723 unsigned char *ncp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
724 PyObject *rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
725 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
726 unsigned char value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
727 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
728
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
729 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
730 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
731
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
732 if ( !check_multiply(len, x, y) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
733 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
734 nlen = x*y*4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
735 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
736 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
737
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
738 rv = PyString_FromStringAndSize(NULL, nlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
739 if ( rv == 0 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
740 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
741 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
742
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
743 for ( i=0; i < len; i++ ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
744 value = *cp++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
745 if (backward_compatible) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
746 * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
747 ncp += 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
748 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
749 *ncp++ = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
750 *ncp++ = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
751 *ncp++ = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
752 *ncp++ = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
753 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
754 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
755 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
756 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
757
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
758 static PyMethodDef imageop_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
759 { "crop", imageop_crop, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
760 { "scale", imageop_scale, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
761 { "grey2mono", imageop_grey2mono, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
762 { "grey2grey2", imageop_grey2grey2, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
763 { "grey2grey4", imageop_grey2grey4, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
764 { "dither2mono", imageop_dither2mono, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
765 { "dither2grey2", imageop_dither2grey2, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
766 { "mono2grey", imageop_mono2grey, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
767 { "grey22grey", imageop_grey22grey, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
768 { "grey42grey", imageop_grey42grey, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
769 { "tovideo", imageop_tovideo, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
770 { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
771 { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
772 { "rgb2grey", imageop_rgb2grey, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
773 { "grey2rgb", imageop_grey2rgb, METH_VARARGS },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
774 { 0, 0 }
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 };
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 22772
diff changeset
778 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13495
diff changeset
779 initimageop(void)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
781 PyObject *m;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
782
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
783 if (PyErr_WarnPy3k("the imageop module has been removed in "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
784 "Python 3.0", 2) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
785 return;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
786
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
787 m = Py_InitModule("imageop", imageop_methods);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
788 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
789 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
790 ImageopDict = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
791 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
792 if (ImageopError != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49750
diff changeset
793 PyDict_SetItemString(ImageopDict, "error", ImageopError);
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 }