| Ticket UUID: | e7fc29ce4cab47ad673f02e11adbbdc861dc7fea | |||
| Title: | Performance problems with "photo copy -to" | |||
| Type: | Bug | Version: | 8.6, 9.0 | |
| Submitter: | obermeier | Created on: | 2024-11-10 21:03:41 | |
| Subsystem: | 41. Photo Images | Assigned To: | nobody | |
| Priority: | 5 Medium | Severity: | Important | |
| Status: | Closed | Last Modified: | 2026-09-24 12:09:46 | |
| Resolution: | Fixed | Closed By: | nemethi | |
| Closed on: | 2026-09-24 12:09:46 | |||
| Description: |
I have a simple tiling procedure and noticed a slowdown when using large images,
i.e. when doing lots of copies.
I noticed the following behaviour both on Windows and Linux:
The time for copying does not grow linearly, as expected, but gets larger the
more copies are involved.
The attached files contain 4 test images and the tiling script.
One image contains just 3 channels (RGB) and three images contain 4 channels (RGBA),
where the alpha channel is either fully opaque, fully transparent or partially transparent.
Only the partially transparent image behaves as expected, i.e. has linear time increase.
Using Tcl 9.0.0, Tk 9.0.0
Using file Balls-3chan.png
Time for 100 copies: 1.31 seconds (13.14 milliseconds / copy)
Time for 400 copies: 19.70 seconds (49.24 milliseconds / copy)
Using file Balls-4chan-transp.png
Time for 100 copies: 1.23 seconds (12.29 milliseconds / copy)
Time for 400 copies: 19.24 seconds (48.10 milliseconds / copy)
Using file Balls-4chan-opaque.png
Time for 100 copies: 1.32 seconds (13.18 milliseconds / copy)
Time for 400 copies: 19.61 seconds (49.03 milliseconds / copy)
Using file Balls-4chan-partial.png
Time for 100 copies: 0.13 seconds ( 1.33 milliseconds / copy)
Time for 400 copies: 0.54 seconds ( 1.35 milliseconds / copy)
The increasing time is due to calling function ToggleComplexAlphaIfNeeded
(generic/tkImgPhoto.c), which scans the alpha channel of the source image
and looks for alpha values not equal to zero or 255 to detect so-called
COMPLEX_ALPHA images.
In the last test image it immediately detects an alpha value of 128 at the
first pixel and returns, while in the first 3 images it loops through all
alpha values and thus takes a long time.
When rewriting ToggleComplexAlphaIfNeeded to just set the COMPLEX_ALPHA bit
(see code in #ifdef NO_TOGGLE section below), the time increase is linear
and the copy time per image is faster than before.
Using Tcl 9.0.0, Tk 9.0.0
Using file Balls-3chan.png
Time for 100 copies: 0.11 seconds ( 1.11 milliseconds / copy)
Time for 400 copies: 0.44 seconds ( 1.11 milliseconds / copy)
Using file Balls-4chan-transp.png
Time for 100 copies: 0.03 seconds ( 0.27 milliseconds / copy)
Time for 400 copies: 0.11 seconds ( 0.28 milliseconds / copy)
Using file Balls-4chan-opaque.png
Time for 100 copies: 0.11 seconds ( 1.11 milliseconds / copy)
Time for 400 copies: 0.45 seconds ( 1.13 milliseconds / copy)
Using file Balls-4chan-partial.png
Time for 100 copies: 0.11 seconds ( 1.13 milliseconds / copy)
Time for 400 copies: 0.45 seconds ( 1.12 milliseconds / copy)
static int
ToggleComplexAlphaIfNeeded(
PhotoModel *mPtr)
{
#ifdef NO_TOGGLE
mPtr->flags &= ~COMPLEX_ALPHA;
mPtr->flags |= COMPLEX_ALPHA;
#else
size_t len = (size_t)MAX(mPtr->userWidth, mPtr->width) *
(size_t)MAX(mPtr->userHeight, mPtr->height) * 4;
unsigned char *c = mPtr->pix32;
unsigned char *end;
/*
* Set the COMPLEX_ALPHA flag if we have an image with partially
* transparent bits.
*/
mPtr->flags &= ~COMPLEX_ALPHA;
if (c == NULL) {
return 0;
}
end = c + len;
c += 3; /* Start at first alpha byte. */
for (; c < end; c += 4) {
if (*c && *c != 255) {
mPtr->flags |= COMPLEX_ALPHA;
break;
}
}
#endif
return (mPtr->flags & COMPLEX_ALPHA);
}
Using the modified ToggleComplexAlphaIfNeeded function, all Tk tests
run fine as well as the Img test suite. I also did not notice any
problems with the image processing functionality of my poApps application.
| |||
| User Comments: |
nemethi (claiming to be Csaba Nemethi) added on 2026-09-24 12:09:46:
Very clever solution, Serhiy! Merged to main, core-9-0-branch, and core-8-6-branch by commits [a695524c], [3a28a2f1], and [db55ab86]. Closing the ticket. serhiy.storchaka added on 2026-09-18 15:33:00: Fixed on branch photo-copy-complex-alpha in [a01e7adfcf]: after a block is written, only its pixels are scanned for partial transparency; the whole image is rescanned only if it was already known to contain such pixels and the block has none. With the attached script all four images now take about 0.6 ms per copy for 100 and 400 copies. oehhar added on 2024-12-16 19:44:41: I also liked the explanation for the slowdown given today in the telco: The alpha channel of the pixels are checked until a value "ni {0 255}" (not fully transparent and not fully opaque) (a semi-transparent pixel) is found. In the examples with semi-transparent pixels, only few pixels must be investigated. In the other examples, all pixels are investigated. Harald obermeier added on 2024-12-16 18:17:46: I do not propose to remove the COMPLEX_ALPHA stuff, but noticed the slow-down in ToggleComplexAlphaIfNeeded in the cases of opaque or fully transparent pixels and made the above mentioned hack to demonstrate the slow-down. Additionally I noted, that without using ToggleComplexAlphaIfNeeded all of my image related code still runs fine. But of course, there may be cases, where this hack does not work, so somebody with more Tk/photo experience should take a look at the photo code. fvogel added on 2024-12-15 23:59:25: Are you proposing to simply always set the COMPLEX_ALPHA bit in ToggleComplexAlphaIfNeeded? That would mean that the optimizations currently in place in the Tk code for images without an alpha channel, or with alpha being zero or 255 everywhere in the image, are not (or no longer) needed. Finally this would mean completely removing the COMPLEX_ALPHA stuff. Do I understand correctly what you are suggesting? I note that in imgPhoto.test we can read the following lines: # Function name Tests for function #-------------------------------------------------------------------------- <SNIP> # toggleComplexAlphaIfNeeded: no tests | |||
Attachments:
- photocopy.tcl [download] added by obermeier on 2024-11-10 21:06:26. [details]
- Balls-4chan-transp.png [download] added by obermeier on 2024-11-10 21:05:58. [details]
- Balls-4chan-partial.png [download] added by obermeier on 2024-11-10 21:05:49. [details]
- Balls-4chan-opaque.png [download] added by obermeier on 2024-11-10 21:05:39. [details]
- Balls-3chan.png [download] added by obermeier on 2024-11-10 21:05:30. [details]
