Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ _get_transform_mesh(PyObject *py_affine, npy_intp *dims)
PyObject *output_mesh =
PyObject_CallMethod(
py_inverse, (char *)"transform", (char *)"O",
(char *)input_mesh.pyobj(), NULL);
(char *)input_mesh.pyobj_steal(), NULL);

Py_DECREF(py_inverse);

Expand Down
29 changes: 20 additions & 9 deletions src/_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
numpy::array_view<unsigned char, 3> buffer;
PyObject *filein;
PyObject *metadata = NULL;
PyObject *meta_key, *meta_val;
png_text *text;
Py_ssize_t pos = 0;
int meta_pos = 0;
Py_ssize_t meta_size;
double dpi = 0;
int compression = 6;
int filter = -1;
Expand Down Expand Up @@ -274,15 +269,22 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
PyErr_SetString(PyExc_TypeError, "metadata must be a dict or None");
goto exit;
}
meta_size = PyDict_Size(metadata);
text = new png_text[meta_size];

Py_ssize_t meta_size = PyDict_Size(metadata);
png_text *text = new png_text[meta_size];
PyObject *meta_key, *meta_val;
Py_ssize_t pos = 0;
int meta_pos = 0;
std::vector<PyObject*> temp_strs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know if it is worth it, but when doing push_back()s to a vector, one can save a fair amount of memory and allocation time by doing temp_strs.reserve(meta_size); before the loop for strings since we know the upper limit for this vector.

https://en.cppreference.com/w/cpp/container/vector/reserve

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect it won't be more than 10 or maybe 20, but I've added it since we do know the limit.

temp_strs.reserve(meta_size);

while (PyDict_Next(metadata, &pos, &meta_key, &meta_val)) {
text[meta_pos].compression = PNG_TEXT_COMPRESSION_NONE;
if (PyUnicode_Check(meta_key)) {
PyObject *temp_key = PyUnicode_AsEncodedString(meta_key, "latin_1", "strict");
if (temp_key != NULL) {
text[meta_pos].key = PyBytes_AsString(temp_key);
temp_strs.push_back(temp_key);
}
} else if (PyBytes_Check(meta_key)) {
text[meta_pos].key = PyBytes_AsString(meta_key);
Expand All @@ -295,6 +297,7 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *temp_val = PyUnicode_AsEncodedString(meta_val, "latin_1", "strict");
if (temp_val != NULL) {
text[meta_pos].text = PyBytes_AsString(temp_val);
temp_strs.push_back(temp_val);
}
} else if (PyBytes_Check(meta_val)) {
text[meta_pos].text = PyBytes_AsString(meta_val);
Expand All @@ -307,6 +310,10 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
meta_pos++;
}
png_set_text(png_ptr, info_ptr, text, meta_size);
for (std::vector<PyObject*>::iterator it = temp_strs.begin();
it != temp_strs.end(); ++it) {
Py_DECREF(*it);
}
delete[] text;
}
#endif
Expand Down Expand Up @@ -339,8 +346,12 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
png_write_end(png_ptr, info_ptr);

exit:
if (png_ptr && info_ptr) {
png_destroy_write_struct(&png_ptr, &info_ptr);
if (png_ptr) {
if (info_ptr) {
png_destroy_write_struct(&png_ptr, &info_ptr);
} else {
png_destroy_write_struct(&png_ptr, NULL);
}
}
if (PyErr_Occurred()) {
Py_XDECREF(buff.str);
Expand Down