Tk Source Code

View Ticket
Login
Ticket UUID: 440c528a996ab0e1ed79f8fdffa69689a18d50f8
Title: 16 bit Portable GrayMap broken
Type: Patch Version: 8.6.12
Submitter: anonymous Created on: 2021-11-08 19:48:09
Subsystem: 43. Photo Image|PPM Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2021-12-04 18:00:44
Resolution: Fixed Closed By: fvogel
    Closed on: 2021-12-04 18:00:44
Description:
Reading PPM/PGM with magic P5 (binary gray map) is broken if maxVal is > 255.
When reading from file, the result seems ok, but the loop exceeds the buffer size.
When reading from string, only random data is shown.

Test script:

set i2 "P5\n4 3\n65535\n\x00\x01\x20\x02\x40\x03\x60\x04\x80\x05\xa0\x06\xc0\x07\xe0\x08\xf0\x09\xf0\x0a\xf1\x0b\xff\xff"
image create photo myimage -data $i2
image create photo myimagez
myimagez copy myimage -zoom 20
label .imgLabel
pack .imgLabel
.imgLabel configure -image myimagez


Patch:

--- tk8.6.12orig/generic/tkImgPPM.c     2021-10-29 19:08:23.000000000 +0200
+++ tk8.6.12/generic/tkImgPPM.c 2021-11-08 20:27:48.379312157 +0100
@@ -240,7 +240,7 @@
            unsigned char *p;
            unsigned int value;

-           for (p = pixelPtr; count > 0; count--, p += 2) {
+           for (p = pixelPtr; count > 0; count -= 2, p += 2) {
                value = ((unsigned int) p[0]) * 256 + ((unsigned int) p[1]);
                value = value * 255 / maxIntensity;
                p[0] = p[1] = (unsigned char) value;
@@ -602,8 +602,8 @@
        } else {
            unsigned int value;

-           for (p = pixelPtr,count=nBytes; count > 1; count-=2, p += 2) {
-               value = ((unsigned int) p[0]) * 256 + ((unsigned int) p[1]);
+           for (p = pixelPtr,count=nBytes; count > 1; count-=2, p += 2, dataBuffer += 2) {
+               value = ((unsigned int) dataBuffer[0]) * 256 + ((unsigned int) dataBuffer[1]);
                value = value * 255 / maxIntensity;
                p[0] = p[1] = (unsigned char) value;
            }
User Comments: fvogel added on 2021-12-04 18:00:44:
Merged into core-8-6-branch and trunk.

fvogel added on 2021-11-08 20:36:21:

Indeed you are right, well spotted! When maxVal > 255, we have two bytes per channel.

Many thanks for the patch! I have committed it in a bugfix branch for the time being, see [2f8e5b44ab].

How can I credit you in the commit log? By what name would you like to be known?