Tk Source Code

View Ticket
Login
Ticket UUID: 4ac9d2cfd6f739303fbf414759bfd733f84e5513
Title: CGImageFromDrawableRect() not using mac_drawable offsets
Type: Bug Version: 8.6.12
Submitter: chrstphrchvz Created on: 2021-11-28 22:54:14
Subsystem: 82. X11 Emulation Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-06-05 00:52:33
Resolution: Fixed Closed By: chrstphrchvz
    Closed on: 2023-06-05 00:52:33
Description:

From report by user Jasper at https://sourceforge.net/p/tkimg/bugs/106/#bc21:

…I now find that the image created is the same size as the window specified, but shows the top left corner of the toplevel window containing it. To illustrate, here is a set of commands to draw a coloured canvas away from the top left corner, then attempt to make an image of it -- the image ends up showing the two buttons and only part of the canvas
% info patchlevel
8.6.12
% package require Img
1.4.13
% pack [button .b -text "This goes left"]
% pack [button .pl -text "No, this does"] -side left
% pack [canvas .c]
% .c create rect 75 50 225 150 -width 10 -outline red -fill green1
1
% .c config -bg blue
% image create photo -format window -data .c
image1
% image1 write test.png

The cause of this is CGImageFromDrawableRect() as of [b9d03a77f9] is missing usage of mac_drawable->xOff and mac_drawable->yOff to correctly offset the region captured from the top left corner of a toplevel.

My suggested fix is to offset image_rect in the non-pixmap case:

diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c
index f256d7ab4..11d42ae2c 100644
--- a/macosx/tkMacOSXImage.c
+++ b/macosx/tkMacOSXImage.c
@@ -656,6 +656,7 @@ CreateCGImageFromDrawableRect(
            cg_image = CGBitmapContextCreateImage((CGContextRef) cg_context);
        }
     } else {
+       image_rect = CGRectOffset(image_rect, mac_drawable->xOff, mac_drawable->yOff);
        NSView *view = TkMacOSXGetNSViewForDrawable(mac_drawable);
        if (view == nil) {
            TkMacOSXDbgMsg("Invalid source drawable");

(It may be possible to always initialize image_rect with the offsets added, but that would mean relying on the offsets to remain their initial value of 0 for pixmaps; it may be better to treat offsets as undefined and leave them unused for pixmaps.)

User Comments: chrstphrchvz added on 2023-06-05 00:52:33:

Closing: the relevant fix [4ac9d2cfd6] was released in 8.6.13.


marc_culler (claiming to be Marc Culler) added on 2021-12-03 17:46:43:
Just a comment - the following script works for me without any calls to
'after'.  The 'wait visibility' command is advertised as a way to wait
for a window to get mapped, but does not seem to work for that.  However,
binding the <Map> event does seem to work, at least in this case.
(It should probably also have a timeout, in case packing the canvas
is unexpectedly fast.)

package require Img
bind all <Map> {set mapvar "%W"}
pack [button .b -text "This goes left"]
pack [button .pl -text "No, this does"] -side left
set mapvar {}
pack [canvas .c]
vwait mapvar
.c create rect 75 50 225 150 -width 10 -outline red -fill green1
.c config -bg blue
update idletasks
image create photo -format window -data .c
image1 write /tmp/test.png

chrstphrchvz added on 2021-11-30 08:42:50:

…or before the toplevel redraws after automatically resizing from the default size of 200x200

…and which only after x {…} for sufficiently large x (e.g. 200) seems to avoid—not update and/or update idletasks.

This observation is unrelated to this ticket; just pointing it out in case others notice it when testing.


chrstphrchvz added on 2021-11-30 08:20:37:

…although I also did see issues where running Jasper’s commands as a script rather than interactively means that Img may try to capture the canvas either before it is mapped (causing a fatal error), or before the toplevel redraws after automatically resizing from the default size of 200x200 (a behavior I described in my comment dated 2021-04-25 18:42:46 on [d281848f97]).


chrstphrchvz added on 2021-11-29 19:28:07:

As I described in a comment dated 2020-08-07 16:58:18 on [fcd6717d8b], the Img 1.4.13 binary creating a completely black image when capturing a window is due to XPutPixel() being called instead of XGetPixel(). This indicates the Img 1.4.13 binary was compiled using Tk Aqua 8.6.6 through 8.6.11, which defined the int pixelpower attribute in XImage that has now been removed in 8.6.12: [9dac460bb8].


marc_culler (claiming to be Marc Culler) added on 2021-11-29 17:52:24:
Jan,

One benefit from the Apple "feature" that requires redrawing the window
in order to produce an image of it is that there is no problem when the
window is obscured by other windows.

The drawback, of course, is that it makes it impossible to use XGetImage
in any drawing procedures.

marc_culler (claiming to be Marc Culler) added on 2021-11-29 17:43:41:
Christopher,  I ended up initializing the rectangle with the offsets
added, as you said.  I couldn't see any reason to treat the pixmaps
differently.  But I could easily be missing something.  If so, please
let me know!

marc_culler (claiming to be Marc Culler) added on 2021-11-29 16:32:12:
Some good news and some bad news.

The good news is that if I build Img-1.4.13 from source I no longer get
the black windows.  That means that I can clean up some of the code
in tkMacOSXImage.c before we merge it.

The bad news is that the Img-1.4.13 binary package is broken, at least
for Big Sur and Monterey.

jan.nijtmans added on 2021-11-29 15:24:29:

Hi Marc, thanks for having a look! The "-window" option in Tkimg requires that the layer is fully drawn. Adding "update" somewhere might help. It might also be that the layer is obscured by another window on the desktop. Indeed - not ideal ....

Feel free to improve things furter! I'm not surprised that this wasn't the whole story (yet).


marc_culler (claiming to be Marc Culler) added on 2021-11-29 14:40:06:
Thanks for making that change, Jan.  That was definitely an error to omit the offsets.  Unfortunately, though, I am afraid things are not that simple.  When
I tested the commands on the ticket on one of my systems (running Big Sur)
the image that I got was not a correct image with incorrect offsets.  It
was totally black.  The -window option in the Img package sometimes works
correctly, but apparently also sometimes fails.

We have known for a long time that there are potential issues here.  The
core of CreateCGImageFromDrawableRect is the call to the NSLayer method:
[view.layer renderInContext:cg_context].  That method works by calling
[NSView drawRect] to redraw the layer into a bitmap.  Consequently it will
fail if called from within drawRect since we do not allow recursive calls
to drawRect.

I don't know if that is the issue here, though.  It seems unlikely.

I also noticed that there is redundant code in CreateCGImageFromDrawableRect
which should probably be removed.

jan.nijtmans added on 2021-11-29 11:30:40:

[9b15b746ab5a1323|fix] committed to branch now. Please verify, I'll merge this to core-8-6-branch soon.