Skip to content

Fix save() in the quartz backend#645

Merged
jwiggins merged 3 commits into
masterfrom
fix/quartz-save-image
Mar 4, 2021
Merged

Fix save() in the quartz backend#645
jwiggins merged 3 commits into
masterfrom
fix/quartz-save-image

Conversation

@jwiggins
Copy link
Copy Markdown
Member

@jwiggins jwiggins commented Feb 28, 2021

save() doesn't work at all currently. Implement the buffer interface and use PIL.Image.frombuffer()

Implement the buffer interface and use PIL.Image.frombuffer()
@jwiggins
Copy link
Copy Markdown
Member Author

jwiggins commented Mar 1, 2021

We didn't have drawing tests for the quartz backend, so I added them. The drawing tests rely on save() to work at all.

@rahulporuri
Copy link
Copy Markdown
Contributor

Closing and opening the PR to trigger CI jobs. Not sure why but the PR has been stuck on Yellow for a while now i.e. "Waiting for status to be reported"

@rahulporuri rahulporuri closed this Mar 1, 2021
@rahulporuri rahulporuri reopened this Mar 1, 2021
@rahulporuri rahulporuri self-requested a review March 1, 2021 13:30
@jwiggins jwiggins requested review from aaronayres35 and removed request for rahulporuri March 3, 2021 14:33
Copy link
Copy Markdown
Contributor

@aaronayres35 aaronayres35 left a comment

Choose a reason for hiding this comment

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

LGTM

I can confirm the changes in this branch fixed the issues I was having with the quartz backend when running the benchmarking script.

Comment thread kiva/quartz/ABCGI.pyx Outdated

# Previously this method checked for red pixels. However this is
# not [currently] possible with the quartz backend because it writes
# out image with premultiplied alpha and none of its pixels are the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is from the if 'A' in mode: block in the above file correct?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is not. The premultiplied alpha setting happens in the __init__ method of the graphics context. I couldn't find a way to have normal alpha, so I changed the test and added this note.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So there are a few constraints here:

  • alpha_info can only be kCGImageAlphaPremultipliedLast or kCGImageAlphaPremultipliedFirst if save() is called on a graphics context.
  • From the "Supported Pixel Formats" table on this page, you can see that only 8, 16, and 32bit pixel formats are supported, so we can't even make a packed 24bit pixel buffer which would make alpha-free saving possible.

We can definitely implement saving RGB/BGR images, but it's a question of time allocation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see, thank you!

Comment thread kiva/quartz/ABCGI.pyx
Comment on lines +1549 to +1558
def __releasebuffer__(self, Py_buffer *buffer):
""" When PyBuffer_Release is called on buffers from __getbuffer__.
"""
# Just deallocate the shape and strides allocated by __getbuffer__.
# Since buffer.obj is a referenced counted reference to this object
# (thus keeping this object alive as long a connected buffer exists)
# and we don't mutate `self.data` outside of __init__ and __dealloc__,
# we have nothing further to do here.
PyMem_Free(buffer.shape)
PyMem_Free(buffer.strides)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@aaronayres35 A bit more detail for future readers 😉

@jwiggins
Copy link
Copy Markdown
Member Author

jwiggins commented Mar 4, 2021

Thanks for the feedback

@jwiggins jwiggins merged commit 0815065 into master Mar 4, 2021
@jwiggins jwiggins deleted the fix/quartz-save-image branch March 4, 2021 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants