diff options
author | donal.k.fellows@manchester.ac.uk <dkf> | 2013-11-03 11:29:18 (GMT) |
---|---|---|
committer | donal.k.fellows@manchester.ac.uk <dkf> | 2013-11-03 11:29:18 (GMT) |
commit | 28c9cfc1d37b4bf2fceee9c3accb8a2fa653ea55 (patch) | |
tree | 9b05c47788c9412e024b5ff92b3e512763155391 /generic | |
parent | d8aa9d7fd5ffba2300ed5f295329b7a00e637c74 (diff) | |
download | tk-28c9cfc1d37b4bf2fceee9c3accb8a2fa653ea55.zip tk-28c9cfc1d37b4bf2fceee9c3accb8a2fa653ea55.tar.gz tk-28c9cfc1d37b4bf2fceee9c3accb8a2fa653ea55.tar.bz2 |
[1632447]: Allow the PPM maxval to go up to 65535, to conform with a format
definition change from around 2000 (even if that's a rare format).
Diffstat (limited to 'generic')
-rw-r--r-- | generic/tkImgPPM.c | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/generic/tkImgPPM.c b/generic/tkImgPPM.c index 8a46fde..b476ac6 100644 --- a/generic/tkImgPPM.c +++ b/generic/tkImgPPM.c @@ -140,7 +140,7 @@ FileReadPPM( * image being read. */ { int fileWidth, fileHeight, maxIntensity; - int nLines, nBytes, h, type, count; + int nLines, nBytes, h, type, count, bytesPerChannel = 1; unsigned char *pixelPtr; Tk_PhotoImageBlock block; @@ -155,13 +155,15 @@ FileReadPPM( "\" has dimension(s) <= 0", NULL); return TCL_ERROR; } - if ((maxIntensity <= 0) || (maxIntensity >= 256)) { + if ((maxIntensity <= 0) || (maxIntensity > 0xffff)) { char buffer[TCL_INTEGER_SPACE]; sprintf(buffer, "%d", maxIntensity); Tcl_AppendResult(interp, "PPM image file \"", fileName, "\" has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; + } else if (maxIntensity > 0x00ff) { + bytesPerChannel = 2; } if ((srcX + width) > fileWidth) { @@ -171,20 +173,20 @@ FileReadPPM( height = fileHeight - srcY; } if ((width <= 0) || (height <= 0) - || (srcX >= fileWidth) || (srcY >= fileHeight)) { + || (srcX >= fileWidth) || (srcY >= fileHeight)) { return TCL_OK; } if (type == PGM) { - block.pixelSize = 1; + block.pixelSize = 1 * bytesPerChannel; block.offset[0] = 0; block.offset[1] = 0; block.offset[2] = 0; } else { - block.pixelSize = 3; + block.pixelSize = 3 * bytesPerChannel; block.offset[0] = 0; - block.offset[1] = 1; - block.offset[2] = 2; + block.offset[1] = 1 * bytesPerChannel; + block.offset[2] = 2 * bytesPerChannel; } block.offset[3] = 0; block.width = width; @@ -224,12 +226,21 @@ FileReadPPM( ckfree((char *) pixelPtr); return TCL_ERROR; } - if (maxIntensity != 255) { + if (maxIntensity < 0x00ff) { unsigned char *p; for (p = pixelPtr; count > 0; count--, p++) { *p = (((int) *p) * 255)/maxIntensity; } + } else if (maxIntensity > 0x00ff) { + unsigned char *p; + unsigned int value; + + for (p = pixelPtr; count > 0; count--, p += 2) { + value = ((unsigned int) p[0]) * 256 + ((unsigned int) p[1]); + value = value * 255 / maxIntensity; + p[0] = p[1] = (unsigned char) value; + } } block.height = nLines; if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY, @@ -474,7 +485,7 @@ StringReadPPM( * image being read. */ { int fileWidth, fileHeight, maxIntensity; - int nLines, nBytes, h, type, count, dataSize; + int nLines, nBytes, h, type, count, dataSize, bytesPerChannel = 2; unsigned char *pixelPtr, *dataBuffer; Tk_PhotoImageBlock block; @@ -490,7 +501,7 @@ StringReadPPM( NULL); return TCL_ERROR; } - if ((maxIntensity <= 0) || (maxIntensity >= 256)) { + if ((maxIntensity <= 0) || (maxIntensity > 0xffff)) { char buffer[TCL_INTEGER_SPACE]; sprintf(buffer, "%d", maxIntensity); @@ -498,6 +509,8 @@ StringReadPPM( "PPM image data has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; + } else if (maxIntensity > 0x00ff) { + bytesPerChannel = 2; } if ((srcX + width) > fileWidth) { @@ -512,15 +525,15 @@ StringReadPPM( } if (type == PGM) { - block.pixelSize = 1; + block.pixelSize = 1 * bytesPerChannel; block.offset[0] = 0; block.offset[1] = 0; block.offset[2] = 0; } else { - block.pixelSize = 3; + block.pixelSize = 3 * bytesPerChannel; block.offset[0] = 0; - block.offset[1] = 1; - block.offset[2] = 2; + block.offset[1] = 1 * bytesPerChannel; + block.offset[2] = 2 * bytesPerChannel; } block.offset[3] = 0; block.width = width; @@ -531,7 +544,7 @@ StringReadPPM( dataSize -= srcY * block.pitch; } - if (maxIntensity == 255) { + if (maxIntensity == 0x00ff) { /* * We have all the data in memory, so write everything in one go. */ @@ -574,8 +587,19 @@ StringReadPPM( Tcl_AppendResult(interp, "truncated PPM data", NULL); return TCL_ERROR; } - for (p=pixelPtr,count=nBytes ; count>0 ; count--,p++,dataBuffer++) { - *p = (((int) *dataBuffer) * 255)/maxIntensity; + if (maxIntensity < 0x00ff) { + for (p=pixelPtr,count=nBytes ; count>0 ; count--,p++,dataBuffer++) { + *p = (((int) *dataBuffer) * 255)/maxIntensity; + } + } else { + unsigned char *p; + unsigned int value; + + for (p = pixelPtr; count > 0; count--, p += 2) { + value = ((unsigned int) p[0]) * 256 + ((unsigned int) p[1]); + value = value * 255 / maxIntensity; + p[0] = p[1] = (unsigned char) value; + } } dataSize -= nBytes; block.height = nLines; |