Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
the newest _to_raw_string returned too many bytes (about 3 times as much as
expected) and fill_image_from_string was for some reason off-color with
the newest commits
  • Loading branch information
rmast committed Oct 30, 2021
1 parent 79d00b0 commit 2d9877a
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions include/plugins/string_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,52 @@ using namespace Gamera;

template<class T>
PyObject* _to_raw_string(const T &image) {
typedef typename T::value_type value_type;
typename T::const_vec_iterator j = image.vec_begin();
std::stringstream stream;
for (; j != image.vec_end(); ++j) {
stream << *j;
}
std::string str = stream.str();
PyObject* pystring = PyBytes_FromStringAndSize(str.c_str(),
str.length());

return pystring;
}
typedef typename T::value_type value_type;
typename T::const_vec_iterator j = image.vec_begin();
size_t image_size = image.ncols() * image.nrows() * sizeof(value_type);
PyObject* pystring = PyBytes_FromStringAndSize((char *)nullptr,
(int)image_size);
if (pystring == nullptr)
return nullptr;
value_type* i = (value_type*)PyBytes_AsString(pystring);
if(i == nullptr){
return nullptr;
}
for (; j != image.vec_end(); ++i, ++j) {
*i = *j;
}
return pystring;
};

template <class T>
bool fill_image_from_string(T &image, PyObject* data_string) {
Py_XINCREF(data_string);
PyObject *s = PyObject_Bytes(data_string);
if (s == nullptr) {
PyErr_SetString(PyExc_TypeError, "could not get string from id_name tuple.");
return -1;
}
size_t length = PyBytes_GET_SIZE(data_string);
//char* s = PyString_AS_STRING(data_string);
//size_t length = PyString_GET_SIZE(data_string);
typedef typename T::value_type value_type;
size_t image_size = image.ncols() * image.nrows() * sizeof(value_type);
if (length != image_size) {
if (length > image_size) {
Py_XDECREF(data_string);
PyErr_SetString(PyExc_ValueError,
"data_string too long for image");
} else {
Py_XDECREF(data_string);
PyErr_SetString(PyExc_ValueError,
"data_string too short for image");
}
Py_XDECREF(data_string);
return false;
}
typename T::vec_iterator i = image.vec_begin();
value_type *j = (value_type *) s;
for (; i != image.vec_end(); ++i, ++j) {
*i = *j;
}
Py_XDECREF(data_string);
return true;
Py_XINCREF(data_string);
if (!PyBytes_CheckExact(data_string)) {
PyErr_SetString(PyExc_TypeError,
"data_string must be a Python string");
return false;
}
char* s = PyBytes_AS_STRING(data_string);
size_t length = PyBytes_GET_SIZE(data_string);
typedef typename T::value_type value_type;
size_t image_size = image.ncols() * image.nrows() * sizeof(value_type);
if (length != image_size) {
if (length > image_size) {
PyErr_SetString(PyExc_ValueError,
"data_string too long for image");
} else {
PyErr_SetString(PyExc_ValueError,
"data_string too short for image");
}
return false;
}
typename T::vec_iterator i = image.vec_begin();
value_type* j = (value_type*)s;
for (; i != image.vec_end(); ++i, ++j) {
*i = *j;
}
Py_XDECREF(data_string);
return true;
}

Image* _from_raw_string(Point offset, Dim size,
Expand Down Expand Up @@ -127,4 +127,3 @@ Image* _from_raw_string(Point offset, Dim size,
}

#endif

0 comments on commit 2d9877a

Please sign in to comment.