Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lic restore license paint #14425

Merged
merged 4 commits into from Jun 3, 2019

Conversation

tacaswell
Copy link
Member

This is in response to a question on the mailing list about the provenance of the png writing code (https://mail.python.org/pipermail/matplotlib-devel/2019-May/001261.html).

Details of code forensics at https://mail.python.org/pipermail/matplotlib-devel/2019-June/001270.html and https://mail.python.org/pipermail/matplotlib-devel/2019-June/001271.html

There is possibly an argument to be had that our current code

matplotlib/src/_png.cpp

Lines 142 to 397 in 4eb0037

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;
const char *names[] = { "buffer", "file", "dpi", "compression", "filter", "metadata", NULL };
// We don't need strict contiguity, just for each row to be
// contiguous, and libpng has special handling for getting RGB out
// of RGBA, ARGB or BGR. But the simplest thing to do is to
// enforce contiguity using array_view::converter_contiguous.
if (!PyArg_ParseTupleAndKeywords(args,
kwds,
"O&O|diiO:write_png",
(char **)names,
&buffer.converter_contiguous,
&buffer,
&filein,
&dpi,
&compression,
&filter,
&metadata)) {
return NULL;
}
png_uint_32 width = (png_uint_32)buffer.dim(1);
png_uint_32 height = (png_uint_32)buffer.dim(0);
npy_intp channels = buffer.dim(2);
std::vector<png_bytep> row_pointers(height);
for (png_uint_32 row = 0; row < (png_uint_32)height; ++row) {
row_pointers[row] = (png_bytep)&buffer(row, 0, 0);
}
FILE *fp = NULL;
mpl_off_t offset = 0;
bool close_file = false;
bool close_dup_file = false;
PyObject *py_file = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
struct png_color_8_struct sig_bit;
int png_color_type;
buffer_t buff;
buff.str = NULL;
switch (channels) {
case 1:
png_color_type = PNG_COLOR_TYPE_GRAY;
break;
case 3:
png_color_type = PNG_COLOR_TYPE_RGB;
break;
case 4:
png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
break;
default:
PyErr_SetString(PyExc_ValueError,
"Buffer must be an NxMxD array with D in 1, 3, 4 "
"(grayscale, RGB, RGBA)");
goto exit;
}
if (compression < 0 || compression > 9) {
PyErr_Format(PyExc_ValueError,
"compression must be in range 0-9, got %d", compression);
goto exit;
}
if (PyBytes_Check(filein) || PyUnicode_Check(filein)) {
if ((py_file = mpl_PyFile_OpenFile(filein, (char *)"wb")) == NULL) {
goto exit;
}
close_file = true;
} else {
py_file = filein;
}
if (filein == Py_None) {
buff.size = width * height * 4 + 1024;
buff.str = PyBytes_FromStringAndSize(NULL, buff.size);
if (buff.str == NULL) {
goto exit;
}
buff.cursor = 0;
} else {
if (close_file) {
fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset);
}
if (fp) {
close_dup_file = true;
} else {
PyErr_Clear();
PyObject *write_method = PyObject_GetAttrString(py_file, "write");
if (!(write_method && PyCallable_Check(write_method))) {
Py_XDECREF(write_method);
PyErr_SetString(PyExc_TypeError,
"Object does not appear to be a 8-bit string path or "
"a Python file-like object");
goto exit;
}
Py_XDECREF(write_method);
}
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Could not create write struct");
goto exit;
}
png_set_compression_level(png_ptr, compression);
if (filter >= 0) {
png_set_filter(png_ptr, 0, filter);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Could not create info struct");
goto exit;
}
if (setjmp(png_jmpbuf(png_ptr))) {
PyErr_SetString(PyExc_RuntimeError, "libpng signaled error");
goto exit;
}
if (buff.str) {
png_set_write_fn(png_ptr, (void *)&buff, &write_png_data_buffer, &flush_png_data_buffer);
} else if (fp) {
png_init_io(png_ptr, fp);
} else {
png_set_write_fn(png_ptr, (void *)py_file, &write_png_data, &flush_png_data);
}
png_set_IHDR(png_ptr,
info_ptr,
width,
height,
8,
png_color_type,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
// Save the dpi of the image in the file
if (dpi > 0.0) {
png_uint_32 dots_per_meter = (png_uint_32)(dpi / (2.54 / 100.0));
png_set_pHYs(png_ptr, info_ptr, dots_per_meter, dots_per_meter, PNG_RESOLUTION_METER);
}
#ifdef PNG_TEXT_SUPPORTED
// Save the metadata
if (metadata != NULL && metadata != Py_None) {
if (!PyDict_Check(metadata)) {
PyErr_SetString(PyExc_TypeError, "metadata must be a dict or None");
goto exit;
}
meta_size = PyDict_Size(metadata);
text = new png_text[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);
}
} else if (PyBytes_Check(meta_key)) {
text[meta_pos].key = PyBytes_AsString(meta_key);
} else {
char invalid_key[79];
sprintf(invalid_key,"INVALID KEY %d", meta_pos);
text[meta_pos].key = invalid_key;
}
if (PyUnicode_Check(meta_val)) {
PyObject *temp_val = PyUnicode_AsEncodedString(meta_val, "latin_1", "strict");
if (temp_val != NULL) {
text[meta_pos].text = PyBytes_AsString(temp_val);
}
} else if (PyBytes_Check(meta_val)) {
text[meta_pos].text = PyBytes_AsString(meta_val);
} else {
text[meta_pos].text = (char *)"Invalid value in metadata";
}
#ifdef PNG_iTXt_SUPPORTED
text[meta_pos].lang = NULL;
#endif
meta_pos++;
}
png_set_text(png_ptr, info_ptr, text, meta_size);
delete[] text;
}
#endif
sig_bit.alpha = 0;
switch (png_color_type) {
case PNG_COLOR_TYPE_GRAY:
sig_bit.gray = 8;
sig_bit.red = 0;
sig_bit.green = 0;
sig_bit.blue = 0;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
sig_bit.alpha = 8;
// fall through
case PNG_COLOR_TYPE_RGB:
sig_bit.gray = 0;
sig_bit.red = 8;
sig_bit.green = 8;
sig_bit.blue = 8;
break;
default:
PyErr_SetString(PyExc_RuntimeError, "internal error, bad png_color_type");
goto exit;
}
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_write_info(png_ptr, info_ptr);
png_write_image(png_ptr, &row_pointers[0]);
png_write_end(png_ptr, info_ptr);
exit:
if (png_ptr && info_ptr) {
png_destroy_write_struct(&png_ptr, &info_ptr);
}
if (close_dup_file) {
mpl_PyFile_DupClose(py_file, fp, offset);
}
if (close_file) {
mpl_PyFile_CloseFile(py_file);
Py_DECREF(py_file);
}
if (PyErr_Occurred()) {
Py_XDECREF(buff.str);
return NULL;
} else {
if (buff.str) {
_PyBytes_Resize(&buff.str, buff.cursor);
return buff.str;
}
Py_RETURN_NONE;
}
}
is so far from the original that is not recognizable, but it is far less effort and risk to carry the license file.

Marked as release critical to bring us back into license compliance.

The png writing code is derived from
https://www.object-craft.com.au/projects/paint/

The original code came in via 6cc5c32
The license was added in 29dfcd2
The license was removed in e3a5f1d

The derived code is currently in `src/_png.cpp`
This was moved to the top of the file in
ba40160 but I think it still only
applies to this function.
@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jun 2, 2019
@tacaswell tacaswell added this to the v2.2.5 milestone Jun 2, 2019
Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

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

Maybe tweak the comment. Or leave it. Or just capitalize the first letter.

src/_png.cpp Outdated Show resolved Hide resolved
@WeatherGod WeatherGod merged commit 270d16d into matplotlib:master Jun 3, 2019
@QuLogic
Copy link
Member

QuLogic commented Jun 3, 2019

@meeseeksdev backport to v2.2.x

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 3, 2019
@tacaswell tacaswell deleted the lic_restore_license_paint branch June 3, 2019 20:57
@tacaswell
Copy link
Member Author

@meeseeksdev backport to v3.1.x

@tacaswell
Copy link
Member Author

I fixed the 2.2.5 milestone to trigger the backports automatically again.

jklymak added a commit that referenced this pull request Jun 4, 2019
…425-on-v3.1.x

Backport PR #14425 on branch v3.1.x (Lic restore license paint)
tacaswell added a commit that referenced this pull request Jun 4, 2019
…425-on-v2.2.x

Backport PR #14425 on branch v2.2.x (Lic restore license paint)
@anntzer anntzer mentioned this pull request Nov 10, 2019
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants