Allow saving bytes as comments

This commit is contained in:
Andrew Murray 2023-03-24 09:45:51 +11:00
parent d3923f7142
commit 7c3fd25433
3 changed files with 21 additions and 18 deletions

View File

@ -373,15 +373,15 @@ def test_comment():
def test_save_comment(): def test_save_comment():
comment = "Created by Pillow" for comment in ("Created by Pillow", b"Created by Pillow"):
out = BytesIO() out = BytesIO()
test_card.save(out, "JPEG2000", comment=comment) test_card.save(out, "JPEG2000", comment=comment)
out.seek(0) out.seek(0)
with Image.open(out) as im: with Image.open(out) as im:
assert im.info["comment"] == b"Created by Pillow" assert im.info["comment"] == b"Created by Pillow"
too_long_comment = " " * 65532 too_long_comment = " " * 65531
with pytest.raises(ValueError): with pytest.raises(ValueError):
test_card.save(out, "JPEG2000", comment=too_long_comment) test_card.save(out, "JPEG2000", comment=too_long_comment)

View File

@ -351,10 +351,12 @@ def _save(im, fp, filename):
cinema_mode = info.get("cinema_mode", "no") cinema_mode = info.get("cinema_mode", "no")
mct = info.get("mct", 0) mct = info.get("mct", 0)
signed = info.get("signed", False) signed = info.get("signed", False)
fd = -1
comment = info.get("comment") comment = info.get("comment")
if isinstance(comment, str):
comment = comment.encode()
add_plt = info.get("add_plt", False) add_plt = info.get("add_plt", False)
fd = -1
if hasattr(fp, "fileno"): if hasattr(fp, "fileno"):
try: try:
fd = fp.fileno() fd = fp.fileno()

View File

@ -1215,11 +1215,12 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
int sgnd = 0; int sgnd = 0;
Py_ssize_t fd = -1; Py_ssize_t fd = -1;
char *comment = NULL; char *comment = NULL;
Py_ssize_t comment_size;
int add_plt = 0; int add_plt = 0;
if (!PyArg_ParseTuple( if (!PyArg_ParseTuple(
args, args,
"ss|OOOsOnOOOssbbnzp", "ss|OOOsOnOOOssbbnz#p",
&mode, &mode,
&format, &format,
&offset, &offset,
@ -1237,6 +1238,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
&sgnd, &sgnd,
&fd, &fd,
&comment, &comment,
&comment_size,
&add_plt)) { &add_plt)) {
return NULL; return NULL;
} }
@ -1319,9 +1321,9 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
} }
} }
if (comment != NULL && strlen(comment) > 0) { if (comment && comment_size > 0) {
/* Size is stored as as an uint16, subtract 4 bytes for the header */ /* Size is stored as as an uint16, subtract 4 bytes for the header */
if (strlen(comment) >= 65531) { if (comment_size >= 65531) {
PyErr_SetString( PyErr_SetString(
PyExc_ValueError, PyExc_ValueError,
"JPEG 2000 comment is too long"); "JPEG 2000 comment is too long");
@ -1329,15 +1331,14 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
return NULL; return NULL;
} }
context->comment = strdup(comment); char *p = malloc(comment_size + 1);
if (!p) {
if (context->comment == NULL) {
PyErr_SetString(
PyExc_MemoryError,
"Couldn't allocate memory for JPEG 2000 comment");
Py_DECREF(encoder); Py_DECREF(encoder);
return NULL; return ImagingError_MemoryError();
} }
memcpy(p, comment, comment_size);
p[comment_size] = '\0';
context->comment = p;
} }
if (quality_layers && PySequence_Check(quality_layers)) { if (quality_layers && PySequence_Check(quality_layers)) {