Tk Source Code

View Ticket
Login
Ticket UUID: 3603436
Title: png save has wrong component indexes in Tk8.6
Type: Bug Version: current: 8.6.0
Submitter: gauthierjp Created on: 2013-02-05 16:58:14
Subsystem: 41. Photo Images Assigned To: jan.nijtmans
Priority: 8 Severity: Minor
Status: Closed Last Modified: 2013-10-28 10:16:26
Resolution: Fixed Closed By: dkf
    Closed on: 2013-10-28 10:16:26
Description:
I create a new PNG photo image from Tk and then fill it from C with Tk_PutPhotoBlock. Writing it to file then results in wrong color component indexes. It seems to put the RGBA indexes (blockPtr->offset) in function EncodePNG as 1 2 3 3 when after some tracing into the code should really be 1 2 3 0. (not sure why it is not (0 1 2 3) though
User Comments: dkf added on 2013-10-28 10:16:26:

Thanks for tracking this one down. I've been very focused on Tcl…


jan.nijtmans added on 2013-10-28 09:54:36:

I see now what's going on. If there are 3 color components without alpha, blockPtr->offset should be filled as 0,1,2,-1 everywhere (in EncodePNG as well!). The increments of the blockPtr->offset's were done to keep compatibility with Tk 8.2 (!!), which only had 3 offsets so offset[3] was not allowed to be touched. The offsets were wrong in Tk 8.5 as well, but no-one noticed because it didn't have a PNG write function.

Should be fixed now in core-8-5-branch [e4ef1ff738] and trunk [a0a8d6bc3d].


dkf added on 2013-09-25 12:08:48:

OK, extensive shenanigans in place.

It's not immediately obvious, but ImgGetPhoto — which prepares a Tk_PhotoImageBlock for writing out as a string or saving to file — is mostly not doing very much (unless certain options are specified). One of the things it does is try to work out whether there is an alpha channel present in the data (all photo images are full RGBA internally); if it can't find one because all the A channel pixel data values are 255, it modifies the offsets so that the R offset is 1, the G offset is 2 and the B offset is 3. It doesn't touch the A offset (which is therefore 3) but perhaps should.

Next, I need to check what the downstream encoders are expecting at this point. In particular, the GIF and PNG encoders need to be checked (our PPM encoder has no alpha support, and I don't know if there are any PNM format variants that can support it).


dkf added on 2013-02-14 21:25:36:
Looking at ImgGetPhoto (in tkImgPhoto.c) I see some very odd calculations. I've  not yet worked out quite what they mean (is it really the case that there's no alpha offset off all the other offsets are non-zero?).

gauthierjp added on 2013-02-13 22:13:03:
The blockPtr->offset is filled as 0,1,2,-1 since I have no transparency in this case. I totally agree that it should be { 0 1 2 3 } within the EncodePNG but the indices get mixed up somewhere because I had to force 1 2 3 0 to get the right image written. weird !

nijtmans added on 2013-02-13 22:05:40:
How is blockPtr->offset[0..3] filled in your original
Tk_PhotoPutBlock calls? If the original image
doesn't contain transparency data, blockPtr->offset[3]
should be set to -1 or equal to blockPtr->offset[0]

dkf added on 2013-02-13 21:26:54:
The indices probably ought to be {0,1,2,3} (remember, the array is zero-indexed) or there is something really odd going on; that's the offsets that Tk always uses internally for *its* data, and they're hard-coded in tkImgPhoto.c.

gauthierjp added on 2013-02-06 00:08:57:
Adding this at the start of EncodePNG seems to fix it but I'm not sure about the implications for the various other types of PNG

    blockPtr->offset[3]=0;