Tk Source Code

View Ticket
Login
Ticket UUID: aa802cdecff0578780672841b374bf988f46e31b
Title: Copying/drawing on images with many transparent spaces is very slow
Type: Bug Version: 8.6
Submitter: dusthillresident Created on: 2024-06-20 17:05:43
Subsystem: 41. Photo Images Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2024-06-20 21:33:20
Resolution: Duplicate Closed By: dusthillresident
    Closed on: 2024-06-20 21:33:20
Description:

Copying 'photo' images with many transparent spaces in them is very slow. Drawing (with 'imageName put') into 'photo' images that contain lots of transparent spaces is also slow.

In some cases, on a Ryzen 5 3600 computer, it can take over 8 seconds to copy a 640x480 image that contains a lot of transparent spaces.

This unfortunately severely damages the usefulness of the 'photo' images for certain applications (like for example, a Paint/drawing app)

Here is a little demonstration Tcl script. It puts randomly positioned blue pixels in a 640x480 image, in batches of 1600 at at time, and displays the time taken to draw one batch of pixels. There is a button labelled "benchmark copy" that you can use to see how long it takes to copy the current state of the displayed image into a blank image.

package require Tk

set w 640
set h 480
image create photo p -width $w -height $h
pack [label .l -image p]
pack [frame .f] -fill x
pack [label .f.l2] -fill x -side left
pack [button .f.b1 -text "Clear to transparent" -command {p blank}] -side right
pack [button .f.b2 -text "Clear to black" -command {p put black -to 0 0 $w $h}] -side right
image create photo copytester
pack [button .f.b3 -text "Benchmark copy" -command {
 .f.l2 configure -text "Please wait, performing image copy..."
 update
 copytester blank
 set t [time {copytester copy p -compositingrule set}]
 tk_messageBox -title "Copy benchmark" -message "Image copy time benchmark" -detail \
  [string cat \
   "It took this long to copy the current state of image 'p' into a blank image:\n\n" \
   [lrange $t 0 1] "\n\n" \
   "That's " [expr {[lindex $t 0]/1000000.0}] " seconds"]
}] -side right

while 1 {
 set t [time {
  for {set i 0} {$i<1600} {incr i} {
   p put blue -to [expr {int(rand()*$w)}] [expr {int(rand()*$h)}]
  }
 }]
 .f.l2 configure -text $t
 update
}

User Comments: dusthillresident added on 2024-06-20 21:33:20:
Hi fvogel, thank you for the reply. Re:seeing the problem on other platforms, I've tested this on Linux and MacOS and both are affected.
- dhr

fvogel added on 2024-06-20 20:51:06:

Thanks for the report. I can reproduce on Windows (do you see the problem on other platforms as well?)

Profiling shows that 99.9% of the time is spent on this line in the generic code. The comment a bit upwards in the code is interesting and shows that performance here has already been analyzed, with an implementation choice consciously made.

For Windows, one level deeper, 99.9% of the time is spent on that line calling a GDI function conbining regions in TkpBuildRegionFromAlphaData().

All in all, this is (yet another) duplicate of [919066]. Christopher Chavez has proposed an approach in that ticket to fix it.