| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#if 0 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#define USE_OLD_IMAGE |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "tkInt.h" |
| 28 |
#include "tkPort.h" |
| 29 |
#include "tkVMacro.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#define MAX_MEMORY 10000 |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define PGM 1 |
| 43 |
#define PPM 2 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static int FileMatchPPM _ANSI_ARGS_((Tcl_Channel chan, |
| 50 |
Tcl_Obj *fileName, Tcl_Obj *formatString, |
| 51 |
int *widthPtr, int *heightPtr, Tcl_Interp *interp)); |
| 52 |
static int FileReadPPM _ANSI_ARGS_((Tcl_Interp *interp, |
| 53 |
Tcl_Channel chan, Tcl_Obj *fileName, |
| 54 |
Tcl_Obj *formatString, Tk_PhotoHandle imageHandle, |
| 55 |
int destX, int destY, int width, int height, |
| 56 |
int srcX, int srcY)); |
| 57 |
static int FileWritePPM _ANSI_ARGS_((Tcl_Interp *interp, |
| 58 |
char *fileName, Tcl_Obj *formatString, |
| 59 |
Tk_PhotoImageBlock *blockPtr)); |
| 60 |
|
| 61 |
Tk_PhotoImageFormat tkImgFmtPPM = { |
| 62 |
"PPM", |
| 63 |
FileMatchPPM, |
| 64 |
NULL, |
| 65 |
FileReadPPM, |
| 66 |
NULL, |
| 67 |
FileWritePPM, |
| 68 |
NULL, |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static int ReadPPMFileHeader _ANSI_ARGS_((Tcl_Channel chan, |
| 76 |
int *widthPtr, int *heightPtr, |
| 77 |
int *maxIntensityPtr)); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static int |
| 98 |
FileMatchPPM(chan, fileName, formatString, widthPtr, heightPtr, interp) |
| 99 |
Tcl_Channel chan; |
| 100 |
Tcl_Obj *fileName; |
| 101 |
Tcl_Obj *formatString; |
| 102 |
int *widthPtr, *heightPtr; |
| 103 |
|
| 104 |
|
| 105 |
Tcl_Interp *interp; |
| 106 |
{ |
| 107 |
int dummy; |
| 108 |
|
| 109 |
return ReadPPMFileHeader(chan, widthPtr, heightPtr, &dummy); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static int |
| 133 |
FileReadPPM(interp, chan, fileNameObj, formatString, imageHandle, destX, destY, |
| 134 |
width, height, srcX, srcY) |
| 135 |
Tcl_Interp *interp; |
| 136 |
Tcl_Channel chan; |
| 137 |
Tcl_Obj *fileNameObj; |
| 138 |
Tcl_Obj *formatString; |
| 139 |
Tk_PhotoHandle imageHandle; |
| 140 |
int destX, destY; |
| 141 |
|
| 142 |
int width, height; |
| 143 |
|
| 144 |
int srcX, srcY; |
| 145 |
|
| 146 |
{ |
| 147 |
int fileWidth, fileHeight, maxIntensity; |
| 148 |
int nLines, nBytes, h, type, count; |
| 149 |
unsigned char *pixelPtr; |
| 150 |
Tk_PhotoImageBlock block; |
| 151 |
char *fileName =Tcl_GetString(fileNameObj); |
| 152 |
|
| 153 |
type = ReadPPMFileHeader(chan, &fileWidth, &fileHeight, &maxIntensity); |
| 154 |
if (type == 0) { |
| 155 |
Tcl_AppendResult(interp, "couldn't read raw PPM header from file \"", |
| 156 |
fileName, "\"", NULL); |
| 157 |
return TCL_ERROR; |
| 158 |
} |
| 159 |
if ((fileWidth <= 0) || (fileHeight <= 0)) { |
| 160 |
Tcl_AppendResult(interp, "PPM image file \"", fileName, |
| 161 |
"\" has dimension(s) <= 0", (char *) NULL); |
| 162 |
return TCL_ERROR; |
| 163 |
} |
| 164 |
if ((maxIntensity <= 0) || (maxIntensity >= 256)) { |
| 165 |
char buffer[TCL_INTEGER_SPACE]; |
| 166 |
|
| 167 |
sprintf(buffer, "%d", maxIntensity); |
| 168 |
Tcl_AppendResult(interp, "PPM image file \"", fileName, |
| 169 |
"\" has bad maximum intensity value ", buffer, |
| 170 |
(char *) NULL); |
| 171 |
return TCL_ERROR; |
| 172 |
} |
| 173 |
|
| 174 |
if ((srcX + width) > fileWidth) { |
| 175 |
width = fileWidth - srcX; |
| 176 |
} |
| 177 |
if ((srcY + height) > fileHeight) { |
| 178 |
height = fileHeight - srcY; |
| 179 |
} |
| 180 |
if ((width <= 0) || (height <= 0) |
| 181 |
|| (srcX >= fileWidth) || (srcY >= fileHeight)) { |
| 182 |
return TCL_OK; |
| 183 |
} |
| 184 |
|
| 185 |
if (type == PGM) { |
| 186 |
block.pixelSize = 1; |
| 187 |
block.offset[0] = 0; |
| 188 |
block.offset[1] = 0; |
| 189 |
block.offset[2] = 0; |
| 190 |
} |
| 191 |
else { |
| 192 |
block.pixelSize = 3; |
| 193 |
block.offset[0] = 0; |
| 194 |
block.offset[1] = 1; |
| 195 |
block.offset[2] = 2; |
| 196 |
} |
| 197 |
block.offset[3] = 0; |
| 198 |
block.width = width; |
| 199 |
block.pitch = block.pixelSize * fileWidth; |
| 200 |
|
| 201 |
Tk_PhotoExpand(imageHandle, destX + width, destY + height); |
| 202 |
|
| 203 |
if (srcY > 0) { |
| 204 |
Tcl_Seek(chan, (Tcl_WideInt)(srcY * block.pitch), SEEK_CUR); |
| 205 |
} |
| 206 |
|
| 207 |
nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch; |
| 208 |
if (nLines > height) { |
| 209 |
nLines = height; |
| 210 |
} |
| 211 |
if (nLines <= 0) { |
| 212 |
nLines = 1; |
| 213 |
} |
| 214 |
nBytes = nLines * block.pitch; |
| 215 |
pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes); |
| 216 |
block.pixelPtr = pixelPtr + srcX * block.pixelSize; |
| 217 |
|
| 218 |
for (h = height; h > 0; h -= nLines) { |
| 219 |
if (nLines > h) { |
| 220 |
nLines = h; |
| 221 |
nBytes = nLines * block.pitch; |
| 222 |
} |
| 223 |
count = Tcl_Read(chan, (char *) pixelPtr, nBytes); |
| 224 |
if (count != nBytes) { |
| 225 |
Tcl_AppendResult(interp, "error reading PPM image file \"", |
| 226 |
fileName, "\": ", |
| 227 |
Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp), |
| 228 |
(char *) NULL); |
| 229 |
ckfree((char *) pixelPtr); |
| 230 |
return TCL_ERROR; |
| 231 |
} |
| 232 |
if (maxIntensity != 255) { |
| 233 |
unsigned char *p; |
| 234 |
|
| 235 |
for (p = pixelPtr; count > 0; count--, p++) { |
| 236 |
*p = (((int) *p) * 255)/maxIntensity; |
| 237 |
} |
| 238 |
} |
| 239 |
block.height = nLines; |
| 240 |
Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, nLines, |
| 241 |
TK_PHOTO_COMPOSITE_SET); |
| 242 |
destY += nLines; |
| 243 |
} |
| 244 |
|
| 245 |
ckfree((char *) pixelPtr); |
| 246 |
return TCL_OK; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static int |
| 268 |
FileWritePPM(interp, fileName, formatString, blockPtr) |
| 269 |
Tcl_Interp *interp; |
| 270 |
char *fileName; |
| 271 |
Tcl_Obj *formatString; |
| 272 |
Tk_PhotoImageBlock *blockPtr; |
| 273 |
{ |
| 274 |
Tcl_Channel chan; |
| 275 |
int w, h; |
| 276 |
int greenOffset, blueOffset, nBytes; |
| 277 |
unsigned char *pixelPtr, *pixLinePtr; |
| 278 |
char header[16 + TCL_INTEGER_SPACE * 2]; |
| 279 |
|
| 280 |
chan = Tcl_OpenFileChannel(interp, fileName, "w", 0666); |
| 281 |
if (chan == NULL) { |
| 282 |
return TCL_ERROR; |
| 283 |
} |
| 284 |
|
| 285 |
if (Tcl_SetChannelOption(interp, chan, "-translation", "binary") |
| 286 |
!= TCL_OK) { |
| 287 |
Tcl_Close(NULL, chan); |
| 288 |
return TCL_ERROR; |
| 289 |
} |
| 290 |
if (Tcl_SetChannelOption(interp, chan, "-encoding", "binary") |
| 291 |
!= TCL_OK) { |
| 292 |
Tcl_Close(NULL, chan); |
| 293 |
return TCL_ERROR; |
| 294 |
} |
| 295 |
|
| 296 |
sprintf(header, "P6\n%d %d\n255\n", blockPtr->width, blockPtr->height); |
| 297 |
Tcl_Write(chan, header, -1); |
| 298 |
|
| 299 |
pixLinePtr = blockPtr->pixelPtr + blockPtr->offset[0]; |
| 300 |
greenOffset = blockPtr->offset[1] - blockPtr->offset[0]; |
| 301 |
blueOffset = blockPtr->offset[2] - blockPtr->offset[0]; |
| 302 |
|
| 303 |
if ((greenOffset == 1) && (blueOffset == 2) && (blockPtr->pixelSize == 3) |
| 304 |
&& (blockPtr->pitch == (blockPtr->width * 3))) { |
| 305 |
nBytes = blockPtr->height * blockPtr->pitch; |
| 306 |
if (Tcl_Write(chan, (char *) pixLinePtr, nBytes) != nBytes) { |
| 307 |
goto writeerror; |
| 308 |
} |
| 309 |
} else { |
| 310 |
for (h = blockPtr->height; h > 0; h--) { |
| 311 |
pixelPtr = pixLinePtr; |
| 312 |
for (w = blockPtr->width; w > 0; w--) { |
| 313 |
if ((Tcl_Write(chan, (char *) &pixelPtr[0], 1) == -1) |
| 314 |
|| (Tcl_Write(chan, (char *) &pixelPtr[greenOffset], 1) == -1) |
| 315 |
|| (Tcl_Write(chan, (char *) &pixelPtr[blueOffset], 1) == -1)) { |
| 316 |
goto writeerror; |
| 317 |
} |
| 318 |
pixelPtr += blockPtr->pixelSize; |
| 319 |
} |
| 320 |
pixLinePtr += blockPtr->pitch; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (Tcl_Close(NULL, chan) == 0) { |
| 325 |
return TCL_OK; |
| 326 |
} |
| 327 |
chan = NULL; |
| 328 |
|
| 329 |
writeerror: |
| 330 |
Tcl_AppendResult(interp, "error writing \"", fileName, "\": ", |
| 331 |
Tcl_PosixError(interp), (char *) NULL); |
| 332 |
if (chan != NULL) { |
| 333 |
Tcl_Close(NULL, chan); |
| 334 |
} |
| 335 |
return TCL_ERROR; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static int |
| 361 |
ReadPPMFileHeader(chan, widthPtr, heightPtr, maxIntensityPtr) |
| 362 |
Tcl_Channel chan; |
| 363 |
int *widthPtr, *heightPtr; |
| 364 |
|
| 365 |
int *maxIntensityPtr; |
| 366 |
|
| 367 |
{ |
| 368 |
#define BUFFER_SIZE 1000 |
| 369 |
char buffer[BUFFER_SIZE]; |
| 370 |
int i, numFields; |
| 371 |
int type = 0; |
| 372 |
char c; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if (Tcl_Read(chan, &c, 1) != 1) { |
| 380 |
return 0; |
| 381 |
} |
| 382 |
i = 0; |
| 383 |
for (numFields = 0; numFields < 4; numFields++) { |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
while (1) { |
| 389 |
while (isspace(UCHAR(c))) { |
| 390 |
if (Tcl_Read(chan, &c, 1) != 1) { |
| 391 |
return 0; |
| 392 |
} |
| 393 |
} |
| 394 |
if (c != '#') { |
| 395 |
break; |
| 396 |
} |
| 397 |
do { |
| 398 |
if (Tcl_Read(chan, &c, 1) != 1) { |
| 399 |
return 0; |
| 400 |
} |
| 401 |
} while (c != '\n'); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
while (!isspace(UCHAR(c))) { |
| 409 |
if (i < (BUFFER_SIZE-2)) { |
| 410 |
buffer[i] = c; |
| 411 |
i++; |
| 412 |
} |
| 413 |
if (Tcl_Read(chan, &c, 1) != 1) { |
| 414 |
goto done; |
| 415 |
} |
| 416 |
} |
| 417 |
if (i < (BUFFER_SIZE-1)) { |
| 418 |
buffer[i] = ' '; |
| 419 |
i++; |
| 420 |
} |
| 421 |
} |
| 422 |
done: |
| 423 |
buffer[i] = 0; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if (strncmp(buffer, "P6 ", 3) == 0) { |
| 430 |
type = PPM; |
| 431 |
} else if (strncmp(buffer, "P5 ", 3) == 0) { |
| 432 |
type = PGM; |
| 433 |
} else { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr) |
| 437 |
!= 3) { |
| 438 |
return 0; |
| 439 |
} |
| 440 |
return type; |
| 441 |
} |