diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2004-03-27 00:40:39 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2004-03-27 00:40:39 (GMT) |
commit | b9ce39098a3d287637df8f555df02d4822725bde (patch) | |
tree | 39f8d75aa70ce0377e314890f04ac028c732409d /generic/tkImgPPM.c | |
parent | 17b6a8a3a36a89a06d9efd6a6f261cbde661e95b (diff) | |
download | tk-b9ce39098a3d287637df8f555df02d4822725bde.zip tk-b9ce39098a3d287637df8f555df02d4822725bde.tar.gz tk-b9ce39098a3d287637df8f555df02d4822725bde.tar.bz2 |
Backport of FRQ 540375 changes.
Diffstat (limited to 'generic/tkImgPPM.c')
-rw-r--r-- | generic/tkImgPPM.c | 331 |
1 files changed, 321 insertions, 10 deletions
diff --git a/generic/tkImgPPM.c b/generic/tkImgPPM.c index d55cdfc..3469fcc 100644 --- a/generic/tkImgPPM.c +++ b/generic/tkImgPPM.c @@ -13,7 +13,7 @@ * Department of Computer Science, * Australian National University. * - * RCS: @(#) $Id: tkImgPPM.c,v 1.10.2.1 2004/03/26 22:01:26 dkf Exp $ + * RCS: @(#) $Id: tkImgPPM.c,v 1.10.2.2 2004/03/27 00:40:39 dkf Exp $ */ #include "tkInt.h" @@ -39,7 +39,8 @@ static int FileMatchPPM _ANSI_ARGS_((Tcl_Channel chan, CONST char *fileName, Tcl_Obj *format, - int *widthPtr, int *heightPtr)); + int *widthPtr, int *heightPtr, + Tcl_Interp *interp)); static int FileReadPPM _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Channel chan, CONST char *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle, @@ -50,13 +51,21 @@ static int FileWritePPM _ANSI_ARGS_((Tcl_Interp *interp, Tk_PhotoImageBlock *blockPtr)); static int StringWritePPM _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr)); +static int StringMatchPPM _ANSI_ARGS_((Tcl_Obj *dataObj, + Tcl_Obj *format, int *widthPtr, int *heightPtr, + Tcl_Interp *interp)); +static int StringReadPPM _ANSI_ARGS_((Tcl_Interp *interp, + Tcl_Obj *dataObj, Tcl_Obj *format, + Tk_PhotoHandle imageHandle, int destX, int destY, + int width, int height, int srcX, int srcY)); + Tk_PhotoImageFormat tkImgFmtPPM = { "ppm", /* name */ FileMatchPPM, /* fileMatchProc */ - NULL, /* stringMatchProc */ + StringMatchPPM, /* stringMatchProc */ FileReadPPM, /* fileReadProc */ - NULL, /* stringReadProc */ + StringReadPPM, /* stringReadProc */ FileWritePPM, /* fileWriteProc */ StringWritePPM, /* stringWriteProc */ }; @@ -68,6 +77,10 @@ Tk_PhotoImageFormat tkImgFmtPPM = { static int ReadPPMFileHeader _ANSI_ARGS_((Tcl_Channel chan, int *widthPtr, int *heightPtr, int *maxIntensityPtr)); +static int ReadPPMStringHeader _ANSI_ARGS_((Tcl_Obj *dataObj, + int *widthPtr, int *heightPtr, + int *maxIntensityPtr, + unsigned char **dataBufferPtr, int *dataSizePtr)); /* *---------------------------------------------------------------------- @@ -88,13 +101,14 @@ static int ReadPPMFileHeader _ANSI_ARGS_((Tcl_Channel chan, */ static int -FileMatchPPM(chan, fileName, format, widthPtr, heightPtr) +FileMatchPPM(chan, fileName, format, widthPtr, heightPtr, interp) Tcl_Channel chan; /* The image file, open for reading. */ CONST char *fileName; /* The name of the image file. */ Tcl_Obj *format; /* User-specified format string, or NULL. */ int *widthPtr, *heightPtr; /* The dimensions of the image are * returned here if the file is a valid * raw PPM file. */ + Tcl_Interp *interp; { int dummy; @@ -149,7 +163,7 @@ FileReadPPM(interp, chan, fileName, format, imageHandle, destX, destY, } if ((fileWidth <= 0) || (fileHeight <= 0)) { Tcl_AppendResult(interp, "PPM image file \"", fileName, - "\" has dimension(s) <= 0", (char *) NULL); + "\" has dimension(s) <= 0", NULL); return TCL_ERROR; } if ((maxIntensity <= 0) || (maxIntensity >= 256)) { @@ -157,8 +171,7 @@ FileReadPPM(interp, chan, fileName, format, imageHandle, destX, destY, sprintf(buffer, "%d", maxIntensity); Tcl_AppendResult(interp, "PPM image file \"", fileName, - "\" has bad maximum intensity value ", buffer, - (char *) NULL); + "\" has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; } @@ -216,7 +229,7 @@ FileReadPPM(interp, chan, fileName, format, imageHandle, destX, destY, Tcl_AppendResult(interp, "error reading PPM image file \"", fileName, "\": ", Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp), - (char *) NULL); + NULL); ckfree((char *) pixelPtr); return TCL_ERROR; } @@ -319,7 +332,7 @@ FileWritePPM(interp, fileName, format, blockPtr) writeerror: Tcl_AppendResult(interp, "error writing \"", fileName, "\": ", - Tcl_PosixError(interp), (char *) NULL); + Tcl_PosixError(interp), NULL); if (chan != NULL) { Tcl_Close(NULL, chan); } @@ -401,6 +414,185 @@ StringWritePPM(interp, format, blockPtr) /* *---------------------------------------------------------------------- * + * StringMatchPPM -- + * + * This procedure is invoked by the photo image type to see if + * a string contains image data in PPM format. + * + * Results: + * The return value is >0 if the first characters in file "f" look + * like PPM data, and 0 otherwise. + * + * Side effects: + * The access position in f may change. + * + *---------------------------------------------------------------------- + */ + +static int +StringMatchPPM(dataObj, format, widthPtr, heightPtr, interp) + Tcl_Obj *dataObj; /* The image data. */ + Tcl_Obj *format; /* User-specified format string, or NULL. */ + int *widthPtr, *heightPtr; /* The dimensions of the image are + * returned here if the file is a valid + * raw PPM file. */ + Tcl_Interp *interp; /* unused */ +{ + int dummy; + + return ReadPPMStringHeader(dataObj, widthPtr, heightPtr, + &dummy, NULL, NULL); +} + +/* + *---------------------------------------------------------------------- + * + * StringReadPPM -- + * + * This procedure is called by the photo image type to read + * PPM format data from a string and write it into a given + * photo image. + * + * Results: + * A standard TCL completion code. If TCL_ERROR is returned + * then an error message is left in the interp's result. + * + * Side effects: + * New data is added to the image given by imageHandle. + * + *---------------------------------------------------------------------- + */ + +static int +StringReadPPM(interp, dataObj, format, imageHandle, destX, destY, + width, height, srcX, srcY) + Tcl_Interp *interp; /* Interpreter to use for reporting errors. */ + Tcl_Obj *dataObj; /* The image data. */ + Tcl_Obj *format; /* User-specified format string, or NULL. */ + Tk_PhotoHandle imageHandle; /* The photo image to write into. */ + int destX, destY; /* Coordinates of top-left pixel in + * photo image to be written to. */ + int width, height; /* Dimensions of block of photo image to + * be written to. */ + int srcX, srcY; /* Coordinates of top-left pixel to be used + * in image being read. */ +{ + int fileWidth, fileHeight, maxIntensity; + int nLines, nBytes, h, type, count, dataSize; + unsigned char *pixelPtr, *dataBuffer; + Tk_PhotoImageBlock block; + + type = ReadPPMStringHeader(dataObj, &fileWidth, &fileHeight, + &maxIntensity, &dataBuffer, &dataSize); + if (type == 0) { + Tcl_AppendResult(interp, "couldn't read raw PPM header from string", + NULL); + return TCL_ERROR; + } + if ((fileWidth <= 0) || (fileHeight <= 0)) { + Tcl_AppendResult(interp, "PPM image data has dimension(s) <= 0", + NULL); + return TCL_ERROR; + } + if ((maxIntensity <= 0) || (maxIntensity >= 256)) { + char buffer[TCL_INTEGER_SPACE]; + + sprintf(buffer, "%d", maxIntensity); + Tcl_AppendResult(interp, + "PPM image data has bad maximum intensity value ", buffer, + NULL); + return TCL_ERROR; + } + + if ((srcX + width) > fileWidth) { + width = fileWidth - srcX; + } + if ((srcY + height) > fileHeight) { + height = fileHeight - srcY; + } + if ((width <= 0) || (height <= 0) + || (srcX >= fileWidth) || (srcY >= fileHeight)) { + return TCL_OK; + } + + if (type == PGM) { + block.pixelSize = 1; + block.offset[0] = 0; + block.offset[1] = 0; + block.offset[2] = 0; + } else { + block.pixelSize = 3; + block.offset[0] = 0; + block.offset[1] = 1; + block.offset[2] = 2; + } + block.offset[3] = 0; + block.width = width; + block.pitch = block.pixelSize * fileWidth; + + if (srcY > 0) { + dataBuffer += srcY * block.pitch; + dataSize -= srcY * block.pitch; + } + + if (maxIntensity == 255) { + /* + * We have all the data in memory, so write everything in one + * go. + */ + if (block.pitch*height < dataSize) { + Tcl_AppendResult(interp, "truncated PPM data", NULL); + return TCL_ERROR; + } + block.pixelPtr = dataBuffer + srcX * block.pixelSize; + block.height = height; + Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, height, + TK_PHOTO_COMPOSITE_SET); + return TCL_OK; + } + + Tk_PhotoExpand(imageHandle, destX + width, destY + height); + + nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch; + if (nLines > height) { + nLines = height; + } + if (nLines <= 0) { + nLines = 1; + } + nBytes = nLines * block.pitch; + pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes); + block.pixelPtr = pixelPtr + srcX * block.pixelSize; + + for (h = height; h > 0; h -= nLines) { + unsigned char *p; + + if (nLines > h) { + nLines = h; + nBytes = nLines * block.pitch; + } + if (dataSize < nBytes) { + ckfree((char *) pixelPtr); + 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; + } + dataSize -= nBytes; + block.height = nLines; + Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, nLines, + TK_PHOTO_COMPOSITE_SET); + destY += nLines; + } + + ckfree((char *) pixelPtr); + return TCL_OK; +} + +/* + *---------------------------------------------------------------------- + * * ReadPPMFileHeader -- * * This procedure reads the PPM header from the beginning of a @@ -502,3 +694,122 @@ ReadPPMFileHeader(chan, widthPtr, heightPtr, maxIntensityPtr) } return type; } + +/* + *---------------------------------------------------------------------- + * + * ReadPPMStringHeader -- + * + * This procedure reads the PPM header from the beginning of a + * PPM-format string and returns information from the header. + * + * Results: + * The return value is PGM if the string appears to start with + * a valid PGM header, PPM if the string appears to start with + * a valid PPM header, and 0 otherwise. If the header is valid, + * then *widthPtr and *heightPtr are modified to hold the + * dimensions of the image and *maxIntensityPtr is modified to + * hold the value of a "fully on" intensity value. + * + * Side effects: + * None + * + *---------------------------------------------------------------------- + */ + +static int +ReadPPMStringHeader(dataPtr, widthPtr, heightPtr, maxIntensityPtr, + dataBufferPtr, dataSizePtr) + Tcl_Obj *dataPtr; /* Object to read the header from. */ + int *widthPtr, *heightPtr; /* The dimensions of the image are + * returned here. */ + int *maxIntensityPtr; /* The maximum intensity value for + * the image is stored here. */ + unsigned char **dataBufferPtr; + int *dataSizePtr; +{ +#define BUFFER_SIZE 1000 + char buffer[BUFFER_SIZE]; + int i, numFields, dataSize; + int type = 0; + char c; + unsigned char *dataBuffer; + + dataBuffer = Tcl_GetByteArrayFromObj(dataPtr, &dataSize); + + /* + * Read 4 space-separated fields from the string, ignoring + * comments (any line that starts with "#"). + */ + + if (dataSize-- < 1) { + return 0; + } + c = (char) (*dataBuffer++); + i = 0; + for (numFields = 0; numFields < 4; numFields++) { + /* + * Skip comments and white space. + */ + + while (1) { + while (isspace(UCHAR(c))) { + if (dataSize-- < 1) { + return 0; + } + c = (char) (*dataBuffer++); + } + if (c != '#') { + break; + } + do { + if (dataSize-- < 1) { + return 0; + } + c = (char) (*dataBuffer++); + } while (c != '\n'); + } + + /* + * Read a field (everything up to the next white space). + */ + + while (!isspace(UCHAR(c))) { + if (i < (BUFFER_SIZE-2)) { + buffer[i] = c; + i++; + } + if (dataSize-- < 1) { + goto done; + } + c = (char) (*dataBuffer++); + } + if (i < (BUFFER_SIZE-1)) { + buffer[i] = ' '; + i++; + } + } + done: + buffer[i] = 0; + + /* + * Parse the fields, which are: id, width, height, maxIntensity. + */ + + if (strncmp(buffer, "P6 ", 3) == 0) { + type = PPM; + } else if (strncmp(buffer, "P5 ", 3) == 0) { + type = PGM; + } else { + return 0; + } + if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr) + != 3) { + return 0; + } + if (dataBufferPtr != NULL) { + *dataBufferPtr = dataBuffer; + *dataSizePtr = dataSize; + } + return type; +} |