replaced pydict_next with a for loop, due to py_ssize_t warning and issues on 64bit system

This commit is contained in:
wiredfool 2013-03-13 13:31:34 -07:00
parent 03dcb5c557
commit 455304a78c

145
encode.c
View File

@ -672,29 +672,43 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
char* mode; char* mode;
char* rawmode; char* rawmode;
char* compname; char* compname;
char* filename; char* filename;
int compression; int compression;
int fp; int fp;
PyObject *dir;
PyObject *key, *value;
Py_ssize_t pos = 0;
int status;
Py_ssize_t d_size;
PyObject *keys, *values;
PyObject *dir;
PyObject *key, *value;
int pos = 0;
int status;
if (! PyArg_ParseTuple(args, "sssisO", &mode, &rawmode, &compname, &fp, &filename, &dir)) { if (! PyArg_ParseTuple(args, "sssisO", &mode, &rawmode, &compname, &fp, &filename, &dir)) {
return NULL; return NULL;
} }
if (!PyDict_Check(dir)) {
PyErr_SetString(PyExc_ValueError, "Invalid Dictionary");
return NULL;
} else {
d_size = PyDict_Size(dir);
TRACE(("dict size: %d\n", (int)d_size));
keys = PyDict_Keys(dir);
values = PyDict_Values(dir);
for (pos=0;pos<d_size;pos++){
TRACE((" key: %d\n", (int)PyInt_AsLong(PyList_GetItem(keys,pos))));
}
pos = 0;
}
if (!PyDict_Check(dir)) {
PyErr_SetString(PyExc_ValueError, "Invalid Dictionary");
return NULL;
}
TRACE(("new tiff encoder %s fp: %d, filename: %s \n", compname, fp, filename)); TRACE(("new tiff encoder %s fp: %d, filename: %s \n", compname, fp, filename));
/* UNDONE -- we can probably do almost any arbitrary compression here, /* UNDONE -- we can probably do almost any arbitrary compression here,
* so long as we're doing row/stripe based actions and not tiles. * so long as we're doing row/stripe based actions and not tiles.
*/ */
if (strcasecmp(compname, "tiff_ccitt") == 0) { if (strcasecmp(compname, "tiff_ccitt") == 0) {
compression = COMPRESSION_CCITTRLE; compression = COMPRESSION_CCITTRLE;
@ -713,7 +727,7 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
return NULL; return NULL;
} }
TRACE(("Found compression: %d\n", compression)); TRACE(("Found compression: %d\n", compression));
encoder = PyImaging_EncoderNew(sizeof(TIFFSTATE)); encoder = PyImaging_EncoderNew(sizeof(TIFFSTATE));
if (encoder == NULL) if (encoder == NULL)
@ -728,53 +742,58 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
return NULL; return NULL;
} }
while (PyDict_Next(dir, &pos, &key, &value)) { // While failes on 64 bit machines, complains that pos is an int instead of a Py_ssize_t
status = 0; // while (PyDict_Next(dir, &pos, &key, &value)) {
if (PyInt_Check(value)) { for (pos=0;pos<d_size;pos++){
TRACE(("Setting from Int: %d %ld \n", (int)PyInt_AsLong(key),PyInt_AsLong(value))); key = PyList_GetItem(keys,pos);
status = ImagingLibTiffSetField(&encoder->state, value = PyList_GetItem(values,pos);
(ttag_t) PyInt_AsLong(key), status = 0;
PyInt_AsLong(value)); TRACE(("Attempting to set key: %d", (int)PyInt_AsLong(key)));
} else if(PyBytes_Check(value)) { if (PyInt_Check(value)) {
TRACE(("Setting from String: %d, %s \n", (int)PyInt_AsLong(key),PyBytes_AsString(value))); TRACE(("Setting from Int: %d %ld \n", (int)PyInt_AsLong(key),PyInt_AsLong(value)));
status = ImagingLibTiffSetField(&encoder->state, status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) PyInt_AsLong(key), (ttag_t) PyInt_AsLong(key),
PyBytes_AsString(value)); PyInt_AsLong(value));
} else if(PyBytes_Check(value)) {
TRACE(("Setting from String: %d, %s \n", (int)PyInt_AsLong(key),PyBytes_AsString(value)));
status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) PyInt_AsLong(key),
PyBytes_AsString(value));
} else if(PyList_Check(value)) { } else if(PyList_Check(value)) {
int len,i; int len,i;
float *floatav; float *floatav;
TRACE(("Setting from List: %d \n", (int)PyInt_AsLong(key))); TRACE(("Setting from List: %d \n", (int)PyInt_AsLong(key)));
len = (int)PyList_Size(value); len = (int)PyList_Size(value);
TRACE((" %d elements, setting as floats \n", len)); TRACE((" %d elements, setting as floats \n", len));
floatav = malloc(sizeof(float)*len); floatav = malloc(sizeof(float)*len);
if (floatav) { if (floatav) {
for (i=0;i<len;i++) { for (i=0;i<len;i++) {
floatav[i] = (float)PyFloat_AsDouble(PyList_GetItem(value,i)); floatav[i] = (float)PyFloat_AsDouble(PyList_GetItem(value,i));
} }
status = ImagingLibTiffSetField(&encoder->state, status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) PyInt_AsLong(key), (ttag_t) PyInt_AsLong(key),
floatav); floatav);
free(floatav); free(floatav);
} }
} else if (PyFloat_Check(value)) { } else if (PyFloat_Check(value)) {
TRACE(("Setting from String: %d, %f \n", (int)PyInt_AsLong(key),PyFloat_AsDouble(value))); TRACE(("Setting from String: %d, %f \n", (int)PyInt_AsLong(key),PyFloat_AsDouble(value)));
status = ImagingLibTiffSetField(&encoder->state, status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) PyInt_AsLong(key), (ttag_t) PyInt_AsLong(key),
(float)PyFloat_AsDouble(value)); (float)PyFloat_AsDouble(value));
} else { } else {
TRACE(("Unhandled type for key %d : %s ", TRACE(("Unhandled type for key %d : %s ",
(int)PyInt_AsLong(key), (int)PyInt_AsLong(key),
PyBytes_AsString(PyObject_Str(value)))); PyBytes_AsString(PyObject_Str(value))));
} }
if (!status) { if (!status) {
TRACE(("Error setting Field\n")); TRACE(("Error setting Field\n"));
Py_DECREF(encoder); Py_DECREF(encoder);
PyErr_SetString(PyExc_RuntimeError, "Error setting from dictionary"); PyErr_SetString(PyExc_RuntimeError, "Error setting from dictionary");
return NULL; return NULL;
} }
} }
encoder->encode = ImagingLibTiffEncode; encoder->encode = ImagingLibTiffEncode;
return (PyObject*) encoder; return (PyObject*) encoder;