| 1 |
/* |
| 2 |
* tiff.c |
| 3 |
* |
| 4 |
* TIFF photo image type, Tcl/Tk package. |
| 5 |
* |
| 6 |
* A photo image handler for the Tagged Image File Format. |
| 7 |
* |
| 8 |
* For a list of available format options see function ParseFormatOpts |
| 9 |
* and the documentation img-tiff. |
| 10 |
* |
| 11 |
* Copyright (c) 1997-2025 Jan Nijtmans <[email protected]> |
| 12 |
* Copyright (c) 2002-2025 Andreas Kupries <[email protected]> |
| 13 |
* Copyright (c) 2003-2025 Paul Obermeier <[email protected]> |
| 14 |
* |
| 15 |
* See the file "license.terms" for information on usage and redistribution |
| 16 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef _WIN32 |
| 21 |
# define HAVE_BOOLEAN |
| 22 |
# include <Windows.h> |
| 23 |
#endif |
| 24 |
#include "tkimg.h" |
| 25 |
#include "tifftcl.h" |
| 26 |
#include "zlibtcl.h" |
| 27 |
|
| 28 |
#ifdef HAVE_STDLIB_H |
| 29 |
#undef HAVE_STDLIB_H |
| 30 |
#endif |
| 31 |
#include "jpegtcl.h" |
| 32 |
|
| 33 |
static int SetupTiffLibrary(Tcl_Interp *interp); |
| 34 |
|
| 35 |
#define MORE_INITIALIZATION \ |
| 36 |
if (SetupTiffLibrary (interp) != TCL_OK) { return TCL_ERROR; } |
| 37 |
|
| 38 |
/* Force usage of Tk_CreatePhotoImageFormatVersion3 |
| 39 |
supporting image matadata introduced in Tk8.7. |
| 40 |
Must be specified before inclusion of init.c. |
| 41 |
*/ |
| 42 |
#define USE_FORMAT_VERSION3 1 |
| 43 |
|
| 44 |
/* |
| 45 |
* Generic initialization code, parameterized via CPACKAGE and PACKAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include "init.c" |
| 49 |
|
| 50 |
#include "tiffInt.h" |
| 51 |
|
| 52 |
extern DLLIMPORT int unlink(const char *); |
| 53 |
|
| 54 |
/* Format options structure for use with ParseFormatOpts */ |
| 55 |
typedef struct { |
| 56 |
int pageIndex; /* Read option -index */ |
| 57 |
int verbose; /* Read/Write option -verbose */ |
| 58 |
int compression; /* Write option -compression */ |
| 59 |
char byteorder[3]; /* Write option -byteorder */ |
| 60 |
double xres; /* Write option -resolution or -xresolution */ |
| 61 |
double yres; /* Write option -resolution or -yresolution */ |
| 62 |
} FMTOPT; |
| 63 |
|
| 64 |
/* |
| 65 |
* Prototypes for local procedures defined in this file: |
| 66 |
*/ |
| 67 |
static int CommonMatch( |
| 68 |
TIFF *tif, |
| 69 |
tkimg_Stream *handle, |
| 70 |
int *widthPtr, int *heightPtr, |
| 71 |
double *xdpiPtr, double *ydpiPtr, |
| 72 |
int *numPagesPtr, |
| 73 |
int pageIndex |
| 74 |
); |
| 75 |
static int CommonRead( |
| 76 |
Tcl_Interp *interp, |
| 77 |
TIFF *tif, |
| 78 |
const char *fileName, |
| 79 |
Tcl_Obj *format, |
| 80 |
Tk_PhotoHandle imageHandle, |
| 81 |
int destX, int destY, |
| 82 |
int width, int height, |
| 83 |
int srcX, int srcY, |
| 84 |
Tcl_Obj *metadataOut |
| 85 |
); |
| 86 |
static int CommonWrite( |
| 87 |
Tcl_Interp *interp, |
| 88 |
TIFF *tif, |
| 89 |
const char *fileName, |
| 90 |
FMTOPT *opts, |
| 91 |
Tk_PhotoImageBlock *blockPtr, |
| 92 |
Tcl_Obj *metadataIn |
| 93 |
); |
| 94 |
|
| 95 |
static int ParseFormatOpts(Tcl_Interp *interp, Tcl_Obj *format, FMTOPT *opts, int mode); |
| 96 |
|
| 97 |
static void _TIFFerr(const char *, const char *, va_list); |
| 98 |
static void _TIFFwarn(const char *, const char *, va_list); |
| 99 |
|
| 100 |
/* |
| 101 |
* The functions for the TIFF input handler |
| 102 |
*/ |
| 103 |
|
| 104 |
static int mapDummy(thandle_t, tdata_t *, toff_t *); |
| 105 |
static void unMapDummy(thandle_t, tdata_t, toff_t); |
| 106 |
static int closeDummy(thandle_t); |
| 107 |
static tsize_t writeDummy(thandle_t, tdata_t, tsize_t); |
| 108 |
|
| 109 |
static tsize_t readMFile(thandle_t, tdata_t, tsize_t); |
| 110 |
static toff_t seekMFile(thandle_t, toff_t, int); |
| 111 |
static toff_t sizeMFile(thandle_t); |
| 112 |
|
| 113 |
static tsize_t readString(thandle_t, tdata_t, tsize_t); |
| 114 |
static tsize_t writeString(thandle_t, tdata_t, tsize_t); |
| 115 |
static toff_t seekString(thandle_t, toff_t, int); |
| 116 |
static toff_t sizeString(thandle_t); |
| 117 |
|
| 118 |
static char *errorMessage = NULL; |
| 119 |
|
| 120 |
static void printImgInfo (int pageIndex, uint32_t width, uint32_t height, float hdpi, float vdpi, |
| 121 |
const char *fileName, const char *msg) |
| 122 |
{ |
| 123 |
Tcl_Channel outChan; |
| 124 |
char str[256]; |
| 125 |
|
| 126 |
outChan = Tcl_GetStdChannel (TCL_STDOUT); |
| 127 |
if (!outChan) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
tkimg_snprintf(str, 256, "%s %s\n", msg, fileName); IMGOUT; |
| 132 |
tkimg_snprintf(str, 256, "\tSize in pixel: %d x %d\n", width, height); IMGOUT; |
| 133 |
tkimg_snprintf(str, 256, "\tDots per inch: %.0f x %.0f\n", hdpi, vdpi); IMGOUT; |
| 134 |
tkimg_snprintf(str, 256, "\tPage index : %d\n", pageIndex); IMGOUT; |
| 135 |
Tcl_Flush(outChan); |
| 136 |
} |
| 137 |
|
| 138 |
static int |
| 139 |
SetupTiffLibrary (Tcl_Interp *interp) |
| 140 |
{ |
| 141 |
static int initialized = 0; |
| 142 |
|
| 143 |
if (!Tifftcl_InitStubs(interp, TIFFTCL_VERSION, 0)) { |
| 144 |
return TCL_ERROR; |
| 145 |
} |
| 146 |
|
| 147 |
if (errorMessage) { |
| 148 |
ckfree(errorMessage); |
| 149 |
errorMessage = NULL; |
| 150 |
} |
| 151 |
if (TIFFSetErrorHandler != NULL) { |
| 152 |
TIFFSetErrorHandler(_TIFFerr); |
| 153 |
} |
| 154 |
if (TIFFSetWarningHandler != NULL) { |
| 155 |
TIFFSetWarningHandler(_TIFFwarn); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Initialize jpeg and zlib too, for use by the CODEC's we register |
| 160 |
* with the base TIFF library in this package. |
| 161 |
*/ |
| 162 |
|
| 163 |
if (!Jpegtcl_InitStubs(interp, JPEGTCL_VERSION, 0)) { |
| 164 |
return TCL_ERROR; |
| 165 |
} |
| 166 |
|
| 167 |
if (!initialized) { |
| 168 |
initialized = 1; |
| 169 |
if (!Zlibtcl_InitStubs(interp, ZLIBTCL_VERSION, 0)) { |
| 170 |
return TCL_ERROR; |
| 171 |
} |
| 172 |
TIFFRegisterCODEC (COMPRESSION_DEFLATE, "Deflate", TkimgTIFFInitZip); |
| 173 |
TIFFRegisterCODEC (COMPRESSION_ADOBE_DEFLATE, "AdobeDeflate", TkimgTIFFInitZip); |
| 174 |
|
| 175 |
if (!Jpegtcl_InitStubs(interp, JPEGTCL_VERSION, 0)) { |
| 176 |
return TCL_ERROR; |
| 177 |
} |
| 178 |
TIFFRegisterCODEC (COMPRESSION_JPEG, "JPEG", TkimgTIFFInitJpeg); |
| 179 |
TIFFRegisterCODEC (COMPRESSION_PIXARLOG, "PixarLog", TkimgTIFFInitPixar); |
| 180 |
} |
| 181 |
return TCL_OK; |
| 182 |
} |
| 183 |
|
| 184 |
static void |
| 185 |
_TIFFerr( |
| 186 |
const char *module, |
| 187 |
const char *fmt, |
| 188 |
va_list ap |
| 189 |
) { |
| 190 |
char buf [2048]; |
| 191 |
char *cp = buf; |
| 192 |
|
| 193 |
if (module != NULL) { |
| 194 |
tkimg_snprintf(cp, 2048, "%s: ", module); |
| 195 |
cp += strlen(module) + 2; |
| 196 |
} |
| 197 |
|
| 198 |
tkimg_vsnprintf(cp, 2048 - (cp - buf), fmt, ap); |
| 199 |
if (errorMessage) { |
| 200 |
ckfree(errorMessage); |
| 201 |
errorMessage = NULL; |
| 202 |
} |
| 203 |
if (strstr(buf, "Null count for")) { |
| 204 |
/* Ignore error messages: Null count for "Tag 34391" (type 1, writecount -3, passcount 1) |
| 205 |
* which are generated since TIFF version 4.0 and are due to old Photoshop private tags. |
| 206 |
*/ |
| 207 |
return; |
| 208 |
} |
| 209 |
errorMessage = (char *) ckalloc(strlen(buf)+1); |
| 210 |
strcpy(errorMessage, buf); |
| 211 |
} |
| 212 |
|
| 213 |
/* warnings are not processed in Tcl */ |
| 214 |
static void |
| 215 |
_TIFFwarn( |
| 216 |
const char *module, |
| 217 |
const char *fmt, |
| 218 |
va_list ap |
| 219 |
) { |
| 220 |
} |
| 221 |
|
| 222 |
static int |
| 223 |
mapDummy( |
| 224 |
thandle_t fd, |
| 225 |
tdata_t *base, |
| 226 |
toff_t *size |
| 227 |
) { |
| 228 |
return (toff_t) 0; |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
unMapDummy( |
| 233 |
thandle_t fd, |
| 234 |
tdata_t base, |
| 235 |
toff_t size |
| 236 |
) { |
| 237 |
} |
| 238 |
|
| 239 |
static int |
| 240 |
closeDummy(thandle_t fd) |
| 241 |
{ |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
static tsize_t |
| 246 |
writeDummy( |
| 247 |
thandle_t fd, |
| 248 |
tdata_t data, |
| 249 |
tsize_t size |
| 250 |
) { |
| 251 |
return size; |
| 252 |
} |
| 253 |
|
| 254 |
static tsize_t |
| 255 |
readMFile( |
| 256 |
thandle_t fd, |
| 257 |
tdata_t data, |
| 258 |
tsize_t size |
| 259 |
) { |
| 260 |
return (tsize_t) tkimg_Read((tkimg_Stream *) fd, (char *) data, size) ; |
| 261 |
} |
| 262 |
|
| 263 |
static toff_t |
| 264 |
seekMFile( |
| 265 |
thandle_t fd, |
| 266 |
toff_t off, |
| 267 |
int whence |
| 268 |
) { |
| 269 |
return Tcl_Seek(((tkimg_Stream *) fd)->channel, (int) off, whence); |
| 270 |
} |
| 271 |
|
| 272 |
static toff_t |
| 273 |
sizeMFile(thandle_t fd) |
| 274 |
{ |
| 275 |
int fsize; |
| 276 |
|
| 277 |
fsize = Tcl_Seek(((tkimg_Stream *) fd)->channel, (int) 0, SEEK_END); |
| 278 |
return fsize < 0 ? 0 : (toff_t) fsize; |
| 279 |
} |
| 280 |
|
| 281 |
static tsize_t |
| 282 |
readString( |
| 283 |
thandle_t fd, |
| 284 |
tdata_t data, |
| 285 |
tsize_t size |
| 286 |
) { |
| 287 |
tkimg_Stream *handle = (tkimg_Stream *) fd; |
| 288 |
|
| 289 |
if ((size + handle->position) > handle->length) { |
| 290 |
/* Avoid unsigned underflow. */ |
| 291 |
if (handle->position > handle->length) { |
| 292 |
size = 0; |
| 293 |
} else { |
| 294 |
size = handle->length - handle->position; |
| 295 |
} |
| 296 |
} |
| 297 |
if (size) { |
| 298 |
memcpy((char *) data, handle->data + handle->position, (size_t) size); |
| 299 |
handle->position += size; |
| 300 |
} |
| 301 |
return size; |
| 302 |
} |
| 303 |
|
| 304 |
static tsize_t |
| 305 |
writeString( |
| 306 |
thandle_t fd, |
| 307 |
tdata_t data, |
| 308 |
tsize_t size |
| 309 |
) { |
| 310 |
tkimg_Stream *handle = (tkimg_Stream *) fd; |
| 311 |
unsigned char *destPtr = Tcl_GetByteArrayFromObj(handle->byteObj, (Tcl_Size *)NULL); |
| 312 |
|
| 313 |
if (handle->position + size > handle->length) { |
| 314 |
handle->length = handle->position + size; |
| 315 |
destPtr = Tcl_SetByteArrayLength(handle->byteObj, handle->length); |
| 316 |
} |
| 317 |
memcpy(destPtr + handle->position, (char *) data, (size_t) size); |
| 318 |
handle->position += size; |
| 319 |
return size; |
| 320 |
} |
| 321 |
|
| 322 |
static toff_t |
| 323 |
seekString( |
| 324 |
thandle_t fd, |
| 325 |
toff_t off, |
| 326 |
int whence |
| 327 |
) { |
| 328 |
tkimg_Stream *handle = (tkimg_Stream *) fd; |
| 329 |
|
| 330 |
switch (whence) { |
| 331 |
case SEEK_SET: |
| 332 |
handle->position = (int) off; |
| 333 |
break; |
| 334 |
case SEEK_CUR: |
| 335 |
handle->position += (int) off; |
| 336 |
break; |
| 337 |
case SEEK_END: |
| 338 |
handle->position = handle->length + (int) off; |
| 339 |
break; |
| 340 |
} |
| 341 |
if (handle->position < 0) { |
| 342 |
handle->position = 0; |
| 343 |
return -1; |
| 344 |
} |
| 345 |
return (toff_t) handle->position; |
| 346 |
} |
| 347 |
|
| 348 |
static toff_t |
| 349 |
sizeString(thandle_t fd) |
| 350 |
{ |
| 351 |
return ((tkimg_Stream *) fd)->length; |
| 352 |
} |
| 353 |
|
| 354 |
static int FileMatchVersion3( |
| 355 |
Tcl_Interp *interp, |
| 356 |
Tcl_Channel chan, |
| 357 |
const char *fileName, |
| 358 |
Tcl_Obj *format, |
| 359 |
Tcl_Obj *metadataIn, |
| 360 |
int *widthPtr, int *heightPtr, |
| 361 |
Tcl_Obj *metadataOut |
| 362 |
) { |
| 363 |
FMTOPT opts; |
| 364 |
TIFF *tif; |
| 365 |
int retVal = 1; |
| 366 |
double xdpi, ydpi; |
| 367 |
int pageIndex = 0; |
| 368 |
int numPages; |
| 369 |
tkimg_Stream handle; |
| 370 |
memset(&handle, 0, sizeof (tkimg_Stream)); |
| 371 |
|
| 372 |
if (ParseFormatOpts(interp, format, &opts, IMG_READ) == TCL_OK) { |
| 373 |
pageIndex = opts.pageIndex; |
| 374 |
} |
| 375 |
|
| 376 |
tkimg_ReadInitFile(&handle, chan); |
| 377 |
|
| 378 |
tif = TIFFClientOpen(fileName, "r", (thandle_t) &handle, |
| 379 |
readMFile, writeDummy, seekMFile, closeDummy, |
| 380 |
sizeMFile, mapDummy, unMapDummy); |
| 381 |
if (tif) { |
| 382 |
retVal = CommonMatch(tif, &handle, widthPtr, heightPtr, &xdpi, &ydpi, &numPages, pageIndex); |
| 383 |
if (retVal && xdpi >= 0.0 && ydpi >= 0.0) { |
| 384 |
if (TCL_ERROR == tkimg_SetResolution( metadataOut, xdpi, ydpi)) { |
| 385 |
retVal = 0; |
| 386 |
} |
| 387 |
} |
| 388 |
if (retVal && numPages >= 1) { |
| 389 |
if (TCL_ERROR == tkimg_SetNumPages( metadataOut, numPages)) { |
| 390 |
retVal = 0; |
| 391 |
} |
| 392 |
} |
| 393 |
TIFFClose(tif); |
| 394 |
} else { |
| 395 |
retVal = 0; |
| 396 |
} |
| 397 |
if (errorMessage) { |
| 398 |
ckfree(errorMessage); |
| 399 |
errorMessage = NULL; |
| 400 |
} |
| 401 |
return retVal; |
| 402 |
} |
| 403 |
|
| 404 |
static int StringMatchVersion3( |
| 405 |
Tcl_Interp *interp, |
| 406 |
Tcl_Obj *dataObj, |
| 407 |
Tcl_Obj *format, |
| 408 |
Tcl_Obj *metadataIn, |
| 409 |
int *widthPtr, int *heightPtr, |
| 410 |
Tcl_Obj *metadataOut |
| 411 |
) { |
| 412 |
FMTOPT opts; |
| 413 |
TIFF *tif; |
| 414 |
int retVal = 1; |
| 415 |
double xdpi, ydpi; |
| 416 |
int pageIndex = 0; |
| 417 |
int numPages; |
| 418 |
tkimg_Stream handle; |
| 419 |
memset(&handle, 0, sizeof (tkimg_Stream)); |
| 420 |
|
| 421 |
if (ParseFormatOpts(interp, format, &opts, IMG_READ) == TCL_OK) { |
| 422 |
pageIndex = opts.pageIndex; |
| 423 |
} |
| 424 |
|
| 425 |
if (!tkimg_ReadInitString(&handle, dataObj)) { |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
tif = TIFFClientOpen("inline data", "r", (thandle_t) &handle, |
| 430 |
readString, writeString, seekString, closeDummy, |
| 431 |
sizeString, mapDummy, unMapDummy); |
| 432 |
if (tif) { |
| 433 |
retVal = CommonMatch(tif, &handle, widthPtr, heightPtr, &xdpi, &ydpi, &numPages, pageIndex); |
| 434 |
if (retVal && xdpi >= 0.0 && ydpi >= 0.0) { |
| 435 |
if (TCL_ERROR == tkimg_SetResolution (metadataOut, xdpi, ydpi)) { |
| 436 |
return 0; |
| 437 |
} |
| 438 |
} |
| 439 |
if (retVal && numPages >= 1) { |
| 440 |
if (TCL_ERROR == tkimg_SetNumPages (metadataOut, numPages)) { |
| 441 |
retVal = 0; |
| 442 |
} |
| 443 |
} |
| 444 |
TIFFClose(tif); |
| 445 |
} else { |
| 446 |
retVal = 0; |
| 447 |
} |
| 448 |
if (errorMessage) { |
| 449 |
ckfree(errorMessage); |
| 450 |
errorMessage = NULL; |
| 451 |
} |
| 452 |
return retVal; |
| 453 |
} |
| 454 |
|
| 455 |
#if HAVE_FORMAT_VERSION3 == 0 |
| 456 |
static int FileMatch( |
| 457 |
Tcl_Channel chan, |
| 458 |
const char *fileName, |
| 459 |
Tcl_Obj *format, |
| 460 |
int *widthPtr, |
| 461 |
int *heightPtr, |
| 462 |
Tcl_Interp *interp |
| 463 |
) { |
| 464 |
return FileMatchVersion3( |
| 465 |
interp, chan, fileName, format, NULL, |
| 466 |
widthPtr, heightPtr, NULL); |
| 467 |
} |
| 468 |
|
| 469 |
static int StringMatch( |
| 470 |
Tcl_Obj *dataObj, |
| 471 |
Tcl_Obj *format, |
| 472 |
int *widthPtr, |
| 473 |
int *heightPtr, |
| 474 |
Tcl_Interp *interp |
| 475 |
) { |
| 476 |
return StringMatchVersion3(interp, dataObj, format, NULL, widthPtr, heightPtr, NULL); |
| 477 |
} |
| 478 |
#endif |
| 479 |
|
| 480 |
static int CommonMatch( |
| 481 |
TIFF *tif, |
| 482 |
tkimg_Stream *handle, |
| 483 |
int *widthPtr, int *heightPtr, |
| 484 |
double *xdpiPtr, double *ydpiPtr, |
| 485 |
int *numPagesPtr, |
| 486 |
int pageIndex |
| 487 |
) { |
| 488 |
uint32_t w, h; |
| 489 |
float xres = 0.0f, yres = 0.0f; |
| 490 |
uint16_t resUnit; |
| 491 |
uint16_t numPages = 0; |
| 492 |
int curIndex; |
| 493 |
|
| 494 |
curIndex = pageIndex; |
| 495 |
while (curIndex-- != 0) { |
| 496 |
if (TIFFReadDirectory(tif) != 1) { |
| 497 |
return 0; |
| 498 |
} |
| 499 |
numPages++; |
| 500 |
} |
| 501 |
|
| 502 |
if ((1 != TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &w)) || |
| 503 |
(1 != TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &h))) { |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
if (1 != TIFFGetField (tif, TIFFTAG_RESOLUTIONUNIT, &resUnit)) { |
| 508 |
resUnit = RESUNIT_INCH; |
| 509 |
} |
| 510 |
if (resUnit == RESUNIT_INCH || resUnit == RESUNIT_CENTIMETER) { |
| 511 |
if ((1 != TIFFGetField (tif, TIFFTAG_XRESOLUTION, &xres)) || |
| 512 |
(1 != TIFFGetField (tif, TIFFTAG_YRESOLUTION, &yres))) { |
| 513 |
xres = -1.0f; |
| 514 |
yres = -1.0f; |
| 515 |
} else { |
| 516 |
if (resUnit == RESUNIT_CENTIMETER) { |
| 517 |
xres *= 2.54f; |
| 518 |
yres *= 2.54f; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
if (xres == 0.0f) { |
| 523 |
xres = -1.0f; |
| 524 |
} |
| 525 |
if (yres == 0.0f) { |
| 526 |
yres = -1.0f; |
| 527 |
} |
| 528 |
|
| 529 |
do { |
| 530 |
numPages++; |
| 531 |
} while (TIFFReadDirectory(tif)); |
| 532 |
|
| 533 |
*widthPtr = w; |
| 534 |
*heightPtr = h; |
| 535 |
*xdpiPtr = xres; |
| 536 |
*ydpiPtr = yres; |
| 537 |
*numPagesPtr = numPages; |
| 538 |
return 1; |
| 539 |
} |
| 540 |
|
| 541 |
static int FileReadVersion3( |
| 542 |
Tcl_Interp *interp, |
| 543 |
Tcl_Channel chan, |
| 544 |
const char *fileName, |
| 545 |
Tcl_Obj *format, |
| 546 |
Tcl_Obj *metadataIn, |
| 547 |
Tk_PhotoHandle imageHandle, |
| 548 |
int destX, int destY, |
| 549 |
int width, int height, |
| 550 |
int srcX, int srcY, |
| 551 |
Tcl_Obj *metadataOut |
| 552 |
) { |
| 553 |
TIFF *tif; |
| 554 |
int retVal; |
| 555 |
tkimg_Stream handle; |
| 556 |
memset(&handle, 0, sizeof (tkimg_Stream)); |
| 557 |
|
| 558 |
tkimg_ReadInitFile(&handle, chan); |
| 559 |
|
| 560 |
tif = TIFFClientOpen(fileName, "r", (thandle_t) &handle, |
| 561 |
readMFile, writeDummy, seekMFile, closeDummy, |
| 562 |
sizeMFile, mapDummy, unMapDummy); |
| 563 |
if (tif) { |
| 564 |
retVal = CommonRead(interp, tif, fileName, format, imageHandle, |
| 565 |
destX, destY, width, height, srcX, srcY, metadataOut); |
| 566 |
TIFFClose(tif); |
| 567 |
} else { |
| 568 |
retVal = TCL_ERROR; |
| 569 |
} |
| 570 |
if (retVal == TCL_ERROR) { |
| 571 |
if (strlen (Tcl_GetStringResult(interp)) == 0 && errorMessage) { |
| 572 |
Tcl_AppendResult(interp, errorMessage, (char *) NULL); |
| 573 |
ckfree(errorMessage); |
| 574 |
errorMessage = NULL; |
| 575 |
} |
| 576 |
} |
| 577 |
return retVal; |
| 578 |
} |
| 579 |
|
| 580 |
static int StringReadVersion3( |
| 581 |
Tcl_Interp *interp, |
| 582 |
Tcl_Obj *dataObj, |
| 583 |
Tcl_Obj *format, |
| 584 |
Tcl_Obj *metadataIn, |
| 585 |
Tk_PhotoHandle imageHandle, |
| 586 |
int destX, int destY, |
| 587 |
int width, int height, |
| 588 |
int srcX, int srcY, |
| 589 |
Tcl_Obj *metadataOut |
| 590 |
) { |
| 591 |
TIFF *tif; |
| 592 |
int retVal; |
| 593 |
tkimg_Stream handle; |
| 594 |
memset(&handle, 0, sizeof (tkimg_Stream)); |
| 595 |
|
| 596 |
if (!tkimg_ReadInitString(&handle, dataObj)) { |
| 597 |
return 0; |
| 598 |
} |
| 599 |
|
| 600 |
tif = TIFFClientOpen("inline data", "r", (thandle_t) &handle, |
| 601 |
readString, writeDummy, seekString, closeDummy, |
| 602 |
sizeString, mapDummy, unMapDummy); |
| 603 |
if (tif) { |
| 604 |
retVal = CommonRead(interp, tif, "InlineData", format, imageHandle, |
| 605 |
destX, destY, width, height, srcX, srcY, metadataOut); |
| 606 |
TIFFClose(tif); |
| 607 |
} else { |
| 608 |
retVal = TCL_ERROR; |
| 609 |
} |
| 610 |
if (retVal == TCL_ERROR) { |
| 611 |
if (strlen (Tcl_GetStringResult(interp)) == 0 && errorMessage) { |
| 612 |
Tcl_AppendResult(interp, errorMessage, (char *) NULL); |
| 613 |
ckfree(errorMessage); |
| 614 |
errorMessage = NULL; |
| 615 |
} |
| 616 |
} |
| 617 |
return retVal; |
| 618 |
} |
| 619 |
|
| 620 |
#if HAVE_FORMAT_VERSION3 == 0 |
| 621 |
static int FileRead( |
| 622 |
Tcl_Interp *interp, |
| 623 |
Tcl_Channel chan, |
| 624 |
const char *fileName, |
| 625 |
Tcl_Obj *format, |
| 626 |
Tk_PhotoHandle imageHandle, |
| 627 |
int destX, int destY, |
| 628 |
int width, int height, |
| 629 |
int srcX, int srcY |
| 630 |
) { |
| 631 |
return FileReadVersion3( |
| 632 |
interp, chan, fileName, format, NULL, imageHandle, |
| 633 |
destX, destY, width, height, srcX, srcY, NULL); |
| 634 |
} |
| 635 |
|
| 636 |
static int StringRead( |
| 637 |
Tcl_Interp *interp, |
| 638 |
Tcl_Obj *dataObj, |
| 639 |
Tcl_Obj *format, |
| 640 |
Tk_PhotoHandle imageHandle, |
| 641 |
int destX, int destY, |
| 642 |
int width, int height, |
| 643 |
int srcX, int srcY |
| 644 |
) { |
| 645 |
return StringReadVersion3( |
| 646 |
interp, dataObj, format, NULL, imageHandle, |
| 647 |
destX, destY, width, height, srcX, srcY, NULL); |
| 648 |
} |
| 649 |
#endif |
| 650 |
|
| 651 |
static int CommonRead( |
| 652 |
Tcl_Interp *interp, |
| 653 |
TIFF *tif, |
| 654 |
const char *fileName, |
| 655 |
Tcl_Obj *format, |
| 656 |
Tk_PhotoHandle imageHandle, |
| 657 |
int destX, int destY, |
| 658 |
int width, int height, |
| 659 |
int srcX, int srcY, |
| 660 |
Tcl_Obj *metadataOut |
| 661 |
) { |
| 662 |
Tk_PhotoImageBlock block; |
| 663 |
uint32_t w, h; |
| 664 |
float xres = 0.0f, yres = 0.0f; |
| 665 |
uint16_t resUnit; |
| 666 |
size_t npixels; |
| 667 |
uint32_t *raster; |
| 668 |
int pageIndex = 0; |
| 669 |
FMTOPT opts; |
| 670 |
|
| 671 |
if (ParseFormatOpts(interp, format, &opts, IMG_READ) == TCL_ERROR) { |
| 672 |
return TCL_ERROR; |
| 673 |
} |
| 674 |
pageIndex = opts.pageIndex; |
| 675 |
while (pageIndex-- != 0) { |
| 676 |
if (TIFFReadDirectory(tif) != 1) { |
| 677 |
Tcl_AppendResult(interp,"no image data for this index", |
| 678 |
(char *) NULL); |
| 679 |
return TCL_ERROR; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
#ifdef WORDS_BIGENDIAN |
| 684 |
block.offset[0] = 3; |
| 685 |
block.offset[1] = 2; |
| 686 |
block.offset[2] = 1; |
| 687 |
block.offset[3] = 0; |
| 688 |
#else |
| 689 |
block.offset[0] = 0; |
| 690 |
block.offset[1] = 1; |
| 691 |
block.offset[2] = 2; |
| 692 |
block.offset[3] = 3; |
| 693 |
#endif |
| 694 |
block.pixelSize = sizeof (uint32_t); |
| 695 |
|
| 696 |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); |
| 697 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 698 |
npixels = (size_t)w * (size_t)h; |
| 699 |
|
| 700 |
if (1 != TIFFGetField (tif, TIFFTAG_RESOLUTIONUNIT, &resUnit)) { |
| 701 |
resUnit = RESUNIT_INCH; |
| 702 |
} |
| 703 |
if (resUnit == RESUNIT_INCH || resUnit == RESUNIT_CENTIMETER) { |
| 704 |
if ((1 != TIFFGetField (tif, TIFFTAG_XRESOLUTION, &xres)) || |
| 705 |
(1 != TIFFGetField (tif, TIFFTAG_YRESOLUTION, &yres))) { |
| 706 |
xres = -1.0f; |
| 707 |
yres = -1.0f; |
| 708 |
} else { |
| 709 |
if (resUnit == RESUNIT_CENTIMETER) { |
| 710 |
xres *= 2.54f; |
| 711 |
yres *= 2.54f; |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
if (xres == 0.0f) { |
| 716 |
xres = -1.0f; |
| 717 |
} |
| 718 |
if (yres == 0.0f) { |
| 719 |
yres = -1.0f; |
| 720 |
} |
| 721 |
if (xres >= 0.0f && yres >= 0.0f) { |
| 722 |
if (TCL_ERROR == tkimg_SetResolution( metadataOut, (double)xres, (double)yres)) { |
| 723 |
return TCL_ERROR; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
if (opts.verbose) { |
| 728 |
printImgInfo (opts.pageIndex, w, h, xres, yres, fileName, "Reading image:"); |
| 729 |
} |
| 730 |
if (npixels * sizeof (uint32_t) >= UINT_MAX) { |
| 731 |
Tcl_AppendResult(interp,"Image size too large", (char *) NULL); |
| 732 |
return TCL_ERROR; |
| 733 |
} |
| 734 |
|
| 735 |
raster = (uint32_t*) TkimgTIFFmalloc(npixels * sizeof (uint32_t)); |
| 736 |
if (raster == NULL) { |
| 737 |
Tcl_AppendResult(interp, "Cannot allocate raster memory", (char *) NULL); |
| 738 |
return TCL_ERROR; |
| 739 |
} |
| 740 |
block.width = width; |
| 741 |
block.height = height; |
| 742 |
block.pitch = - (block.pixelSize * (int) w); |
| 743 |
block.pixelPtr = ((unsigned char *) raster) + ((1-h) * block.pitch); |
| 744 |
|
| 745 |
if (!TIFFReadRGBAImage(tif, w, h, raster, 0) || errorMessage) { |
| 746 |
if (errorMessage) { |
| 747 |
Tcl_AppendResult(interp, errorMessage, (char *) NULL); |
| 748 |
ckfree(errorMessage); |
| 749 |
errorMessage = NULL; |
| 750 |
} |
| 751 |
TkimgTIFFfree (raster); |
| 752 |
return TCL_ERROR; |
| 753 |
} |
| 754 |
if (Tk_PhotoExpand(interp, imageHandle, width, height) == TCL_ERROR) { |
| 755 |
TkimgTIFFfree (raster); |
| 756 |
return TCL_ERROR; |
| 757 |
} |
| 758 |
|
| 759 |
block.pixelPtr += srcY * block.pitch + srcX * block.pixelSize; |
| 760 |
block.offset[3] = block.offset[0]; /* don't use transparency */ |
| 761 |
if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, |
| 762 |
destY, width, height, TK_PHOTO_COMPOSITE_SET) == TCL_ERROR) { |
| 763 |
TkimgTIFFfree (raster); |
| 764 |
return TCL_ERROR; |
| 765 |
} |
| 766 |
|
| 767 |
TkimgTIFFfree (raster); |
| 768 |
return TCL_OK; |
| 769 |
} |
| 770 |
|
| 771 |
static int FileWriteVersion3( |
| 772 |
Tcl_Interp *interp, |
| 773 |
const char *fileName, |
| 774 |
Tcl_Obj *format, |
| 775 |
Tcl_Obj *metadataIn, |
| 776 |
Tk_PhotoImageBlock *blockPtr |
| 777 |
) { |
| 778 |
TIFF *tif; |
| 779 |
int result; |
| 780 |
Tcl_DString nameBuffer; |
| 781 |
const char *fullname; |
| 782 |
FMTOPT opts; |
| 783 |
|
| 784 |
if (!(fullname=Tcl_TranslateFileName(interp, fileName, &nameBuffer))) { |
| 785 |
return TCL_ERROR; |
| 786 |
} |
| 787 |
|
| 788 |
if (ParseFormatOpts(interp, format, &opts, IMG_WRITE) == TCL_ERROR) { |
| 789 |
Tcl_DStringFree(&nameBuffer); |
| 790 |
return TCL_ERROR; |
| 791 |
} |
| 792 |
|
| 793 |
if (!(tif = TIFFOpen(fullname, opts.byteorder))) { |
| 794 |
Tcl_AppendResult(interp, fileName, ": ", Tcl_PosixError(interp), |
| 795 |
(char *)NULL); |
| 796 |
Tcl_DStringFree(&nameBuffer); |
| 797 |
return TCL_ERROR; |
| 798 |
} |
| 799 |
|
| 800 |
Tcl_DStringFree(&nameBuffer); |
| 801 |
|
| 802 |
result = CommonWrite(interp, tif, fileName, &opts, blockPtr, metadataIn); |
| 803 |
TIFFClose(tif); |
| 804 |
return result; |
| 805 |
} |
| 806 |
|
| 807 |
static int StringWriteVersion3( |
| 808 |
Tcl_Interp *interp, |
| 809 |
Tcl_Obj *format, |
| 810 |
Tcl_Obj *metadataIn, |
| 811 |
Tk_PhotoImageBlock *blockPtr |
| 812 |
) { |
| 813 |
int result; |
| 814 |
TIFF *tif; |
| 815 |
FMTOPT opts; |
| 816 |
tkimg_Stream handle; |
| 817 |
memset(&handle, 0, sizeof (tkimg_Stream)); |
| 818 |
|
| 819 |
if (ParseFormatOpts(interp, format, &opts, IMG_WRITE) == TCL_ERROR) { |
| 820 |
return TCL_ERROR; |
| 821 |
} |
| 822 |
|
| 823 |
tkimg_WriteInitString(&handle); |
| 824 |
|
| 825 |
tif = TIFFClientOpen("inline data", opts.byteorder, (thandle_t) &handle, |
| 826 |
readString, writeString, seekString, closeDummy, |
| 827 |
sizeString, mapDummy, unMapDummy); |
| 828 |
|
| 829 |
result = CommonWrite(interp, tif, "InlineData", &opts, blockPtr, metadataIn); |
| 830 |
TIFFClose(tif); |
| 831 |
|
| 832 |
if (result != TCL_OK) { |
| 833 |
Tcl_AppendResult(interp, errorMessage, (char *) NULL); |
| 834 |
ckfree(errorMessage); |
| 835 |
errorMessage = NULL; |
| 836 |
return TCL_ERROR; |
| 837 |
} |
| 838 |
|
| 839 |
if (result == TCL_OK) { |
| 840 |
Tcl_SetObjResult(interp, handle.byteObj); |
| 841 |
} |
| 842 |
return result; |
| 843 |
} |
| 844 |
|
| 845 |
#if HAVE_FORMAT_VERSION3 == 0 |
| 846 |
static int FileWrite( |
| 847 |
Tcl_Interp *interp, |
| 848 |
const char *fileName, |
| 849 |
Tcl_Obj *format, |
| 850 |
Tk_PhotoImageBlock *blockPtr |
| 851 |
) { |
| 852 |
return FileWriteVersion3(interp, fileName, format, NULL, blockPtr); |
| 853 |
} |
| 854 |
|
| 855 |
static int StringWrite( |
| 856 |
Tcl_Interp *interp, |
| 857 |
Tcl_Obj *format, |
| 858 |
Tk_PhotoImageBlock *blockPtr |
| 859 |
) { |
| 860 |
return StringWriteVersion3(interp, format, NULL, blockPtr); |
| 861 |
} |
| 862 |
#endif |
| 863 |
|
| 864 |
static int ParseFormatOpts( |
| 865 |
Tcl_Interp *interp, |
| 866 |
Tcl_Obj *format, |
| 867 |
FMTOPT *opts, |
| 868 |
int mode |
| 869 |
) { |
| 870 |
static const char *const readOptions[] = { |
| 871 |
"-verbose", "-index", NULL |
| 872 |
}; |
| 873 |
enum readEnums { |
| 874 |
R_VERBOSE, R_INDEX |
| 875 |
}; |
| 876 |
static const char *const writeOptions[] = { |
| 877 |
"-verbose", "-compression", "-byteorder", "-resolution", "-xresolution", "-yresolution", NULL |
| 878 |
}; |
| 879 |
enum writeEnums { |
| 880 |
W_VERBOSE, W_COMPRESSION, W_BYTEORDER, W_RESOLUTION, W_XRESOLUTION, W_YRESOLUTION |
| 881 |
}; |
| 882 |
Tcl_Size objc, i; |
| 883 |
int index; |
| 884 |
char *optionStr; |
| 885 |
Tcl_Obj **objv; |
| 886 |
int intVal; |
| 887 |
double doubleVal; |
| 888 |
int boolVal; |
| 889 |
|
| 890 |
/* Initialize options with default values. */ |
| 891 |
opts->verbose = 0; |
| 892 |
opts->pageIndex = 0; |
| 893 |
opts->compression = COMPRESSION_NONE; |
| 894 |
opts->byteorder[0] = 'w'; |
| 895 |
opts->byteorder[1] = '\0'; |
| 896 |
opts->xres = (double)IMG_DEFAULT_DPI; |
| 897 |
opts->yres = (double)IMG_DEFAULT_DPI; |
| 898 |
|
| 899 |
if (tkimg_ListObjGetElements(interp, format, &objc, &objv) == TCL_ERROR) { |
| 900 |
return TCL_ERROR; |
| 901 |
} |
| 902 |
for (i=1; i<objc; i++) { |
| 903 |
if (mode == IMG_READ) { |
| 904 |
if (Tcl_GetIndexFromObj(interp, objv[i], readOptions, |
| 905 |
"format option", 0, &index) == TCL_ERROR) { |
| 906 |
return TCL_ERROR; |
| 907 |
} |
| 908 |
} else { |
| 909 |
if (Tcl_GetIndexFromObj(interp, objv[i], writeOptions, |
| 910 |
"format option", 0, &index) == TCL_ERROR) { |
| 911 |
return TCL_ERROR; |
| 912 |
} |
| 913 |
} |
| 914 |
if (++i >= objc) { |
| 915 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 916 |
"No value specified for option \"%s\".", Tcl_GetString(objv[--i]))); |
| 917 |
return TCL_ERROR; |
| 918 |
} |
| 919 |
optionStr = Tcl_GetString(objv[i]); |
| 920 |
if (mode == IMG_READ) { |
| 921 |
switch (index) { |
| 922 |
case R_VERBOSE: { |
| 923 |
if (Tcl_GetBoolean(interp, optionStr, &boolVal) == TCL_ERROR) { |
| 924 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 925 |
"Invalid verbose mode \"%s\": must be 1 or 0, on or off, true or false.", |
| 926 |
optionStr)); |
| 927 |
return TCL_ERROR; |
| 928 |
} |
| 929 |
opts->verbose = boolVal; |
| 930 |
break; |
| 931 |
} |
| 932 |
case R_INDEX: { |
| 933 |
if (Tcl_GetInt (interp, optionStr, &intVal) == TCL_ERROR) { |
| 934 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 935 |
"Invalid index value \"%s\": must be an integer value greater or equal to zero.", |
| 936 |
optionStr)); |
| 937 |
return TCL_ERROR; |
| 938 |
} |
| 939 |
opts->pageIndex = intVal; |
| 940 |
break; |
| 941 |
} |
| 942 |
} |
| 943 |
} else { |
| 944 |
switch (index) { |
| 945 |
case W_VERBOSE: { |
| 946 |
if (Tcl_GetBoolean(interp, optionStr, &boolVal) == TCL_ERROR) { |
| 947 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 948 |
"Invalid verbose mode \"%s\": must be 1 or 0, on or off, true or false.", |
| 949 |
optionStr)); |
| 950 |
return TCL_ERROR; |
| 951 |
} |
| 952 |
opts->verbose = boolVal; |
| 953 |
break; |
| 954 |
} |
| 955 |
case W_COMPRESSION: { |
| 956 |
if (!strncmp (optionStr, "none", strlen ("none"))) { |
| 957 |
opts->compression = COMPRESSION_NONE; |
| 958 |
} else if (!strncmp (optionStr, "deflate", strlen ("deflate"))) { |
| 959 |
opts->compression = COMPRESSION_DEFLATE; |
| 960 |
} else if (!strncmp (optionStr, "jpeg",strlen ("jpeg"))) { |
| 961 |
opts->compression = COMPRESSION_JPEG; |
| 962 |
} else if (!strncmp (optionStr, "logluv", strlen ("logluv"))) { |
| 963 |
opts->compression = COMPRESSION_SGILOG; |
| 964 |
} else if (!strncmp (optionStr, "lzw", strlen ("lzw"))) { |
| 965 |
opts->compression = COMPRESSION_LZW; |
| 966 |
} else if (!strncmp (optionStr, "packbits", strlen ("packbits"))) { |
| 967 |
opts->compression = COMPRESSION_PACKBITS; |
| 968 |
} else if (!strncmp (optionStr, "pixarlog", strlen ("pixarlog"))) { |
| 969 |
opts->compression = COMPRESSION_PIXARLOG; |
| 970 |
} else { |
| 971 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 972 |
"Invalid compression mode \"%s\": " |
| 973 |
"must be deflate, jpeg, logluv, lzw, packbits, pixarlog or none.", |
| 974 |
optionStr)); |
| 975 |
return TCL_ERROR; |
| 976 |
} |
| 977 |
break; |
| 978 |
} |
| 979 |
case W_BYTEORDER: { |
| 980 |
if (!strncmp (optionStr, "none", strlen ("none"))) { |
| 981 |
strcpy (opts->byteorder, "w"); |
| 982 |
} else if (!strncmp (optionStr, "smallendian", strlen ("smallendian"))) { |
| 983 |
strcpy (opts->byteorder, "wl"); |
| 984 |
} else if (!strncmp (optionStr,"littleendian", strlen ("littleendian"))) { |
| 985 |
strcpy (opts->byteorder, "wl"); |
| 986 |
} else if (!strncmp (optionStr,"bigendian", strlen ("bigendian"))) { |
| 987 |
strcpy (opts->byteorder, "wb"); |
| 988 |
} else if (!strncmp (optionStr,"network", strlen ("network"))) { |
| 989 |
strcpy (opts->byteorder, "wb"); |
| 990 |
} else { |
| 991 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 992 |
"Invalid byteorder \"%s\": " |
| 993 |
"must be bigendian, littleendian, network, smallendian or none.", |
| 994 |
optionStr)); |
| 995 |
return TCL_ERROR; |
| 996 |
} |
| 997 |
break; |
| 998 |
} |
| 999 |
case W_RESOLUTION: { |
| 1000 |
if (tkimg_GetDistanceValue (interp, optionStr, &doubleVal) == TCL_ERROR) { |
| 1001 |
Tcl_AppendResult (interp, " specified for x resolution.", (char *) NULL); |
| 1002 |
return TCL_ERROR; |
| 1003 |
} |
| 1004 |
opts->xres = doubleVal; |
| 1005 |
opts->yres = doubleVal; |
| 1006 |
if (i+1 >= objc) { |
| 1007 |
/* No more parameters available. */ |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
optionStr = Tcl_GetString(objv[i+1]); |
| 1011 |
if (optionStr[0] == '-' ) { |
| 1012 |
/* Next parameter is an option. */ |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
if (tkimg_GetDistanceValue (interp, optionStr, &doubleVal) == TCL_ERROR) { |
| 1016 |
Tcl_AppendResult (interp, " specified for y resolution.", (char *) NULL); |
| 1017 |
return TCL_ERROR; |
| 1018 |
} |
| 1019 |
opts->yres = doubleVal; |
| 1020 |
i++; |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
case W_XRESOLUTION: { |
| 1024 |
if (tkimg_GetDistanceValue (interp, optionStr, &doubleVal) == TCL_ERROR) { |
| 1025 |
Tcl_AppendResult (interp, " specified for x resolution.", (char *) NULL); |
| 1026 |
return TCL_ERROR; |
| 1027 |
} |
| 1028 |
opts->xres = doubleVal; |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
case W_YRESOLUTION: { |
| 1032 |
if (tkimg_GetDistanceValue (interp, optionStr, &doubleVal) == TCL_ERROR) { |
| 1033 |
Tcl_AppendResult (interp, " specified for y resolution.", (char *) NULL); |
| 1034 |
return TCL_ERROR; |
| 1035 |
} |
| 1036 |
opts->yres = doubleVal; |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
return TCL_OK; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int CommonWrite( |
| 1046 |
Tcl_Interp *interp, |
| 1047 |
TIFF *tif, |
| 1048 |
const char *fileName, |
| 1049 |
FMTOPT *opts, |
| 1050 |
Tk_PhotoImageBlock *blockPtr, |
| 1051 |
Tcl_Obj *metadataIn |
| 1052 |
) { |
| 1053 |
int numsamples; |
| 1054 |
unsigned char *data = NULL; |
| 1055 |
double xdpi, ydpi; |
| 1056 |
float xres, yres; |
| 1057 |
|
| 1058 |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, blockPtr->width); |
| 1059 |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, blockPtr->height); |
| 1060 |
TIFFSetField(tif, TIFFTAG_COMPRESSION, opts->compression); |
| 1061 |
|
| 1062 |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 1063 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 1064 |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 1065 |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, blockPtr->height); |
| 1066 |
|
| 1067 |
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); |
| 1068 |
|
| 1069 |
if (TCL_ERROR == tkimg_GetResolution(interp, metadataIn, &xdpi, &ydpi)) { |
| 1070 |
return TCL_ERROR; |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (opts->xres != IMG_DEFAULT_DPI && opts->yres != IMG_DEFAULT_DPI) { |
| 1074 |
/* Resolution values specified in the format string (-xresolution, -yresolution) |
| 1075 |
overwrite the values specified with option -metadata. */ |
| 1076 |
xdpi = (float)opts->xres; |
| 1077 |
ydpi = (float)opts->yres; |
| 1078 |
} |
| 1079 |
xres = (float)xdpi; |
| 1080 |
yres = (float)ydpi; |
| 1081 |
|
| 1082 |
TIFFSetField(tif, TIFFTAG_XRESOLUTION, xres); |
| 1083 |
TIFFSetField(tif, TIFFTAG_YRESOLUTION, yres); |
| 1084 |
|
| 1085 |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 1086 |
if ((blockPtr->offset[0] == blockPtr->offset[1]) && |
| 1087 |
(blockPtr->offset[0] == blockPtr->offset[2])) { |
| 1088 |
numsamples = 1; |
| 1089 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 1090 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 1091 |
} else { |
| 1092 |
numsamples = 3; |
| 1093 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 1094 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 1095 |
} |
| 1096 |
|
| 1097 |
if ((blockPtr->pitch == numsamples * blockPtr->width) |
| 1098 |
&& (blockPtr->pixelSize == numsamples)) { |
| 1099 |
data = blockPtr->pixelPtr; |
| 1100 |
} else { |
| 1101 |
unsigned char *srcPtr, *dstPtr, *rowPtr; |
| 1102 |
int greenOffset, blueOffset, alphaOffset, x, y; |
| 1103 |
dstPtr = data = (unsigned char *) attemptckalloc(numsamples * |
| 1104 |
blockPtr->width * blockPtr->height); |
| 1105 |
if (dstPtr == NULL) { |
| 1106 |
Tcl_AppendResult (interp, "Unable to allocate memory for image data.", (char *) NULL); |
| 1107 |
return TCL_ERROR; |
| 1108 |
} |
| 1109 |
rowPtr = blockPtr->pixelPtr + blockPtr->offset[0]; |
| 1110 |
greenOffset = blockPtr->offset[1] - blockPtr->offset[0]; |
| 1111 |
blueOffset = blockPtr->offset[2] - blockPtr->offset[0]; |
| 1112 |
alphaOffset = blockPtr->offset[0]; |
| 1113 |
if (alphaOffset < blockPtr->offset[2]) { |
| 1114 |
alphaOffset = blockPtr->offset[2]; |
| 1115 |
} |
| 1116 |
if (++alphaOffset < blockPtr->pixelSize) { |
| 1117 |
alphaOffset -= blockPtr->offset[0]; |
| 1118 |
} else { |
| 1119 |
alphaOffset = 0; |
| 1120 |
} |
| 1121 |
if (blueOffset || greenOffset) { |
| 1122 |
for (y = blockPtr->height; y > 0; y--) { |
| 1123 |
srcPtr = rowPtr; |
| 1124 |
for (x = blockPtr->width; x>0; x--) { |
| 1125 |
if (alphaOffset && !srcPtr[alphaOffset]) { |
| 1126 |
*dstPtr++ = 0xd9; |
| 1127 |
*dstPtr++ = 0xd9; |
| 1128 |
*dstPtr++ = 0xd9; |
| 1129 |
} else { |
| 1130 |
*dstPtr++ = srcPtr[0]; |
| 1131 |
*dstPtr++ = srcPtr[greenOffset]; |
| 1132 |
*dstPtr++ = srcPtr[blueOffset]; |
| 1133 |
} |
| 1134 |
srcPtr += blockPtr->pixelSize; |
| 1135 |
} |
| 1136 |
rowPtr += blockPtr->pitch; |
| 1137 |
} |
| 1138 |
} else { |
| 1139 |
for (y = blockPtr->height; y > 0; y--) { |
| 1140 |
srcPtr = rowPtr; |
| 1141 |
for (x = blockPtr->width; x>0; x--) { |
| 1142 |
*dstPtr++ = srcPtr[0]; |
| 1143 |
srcPtr += blockPtr->pixelSize; |
| 1144 |
} |
| 1145 |
rowPtr += blockPtr->pitch; |
| 1146 |
} |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
TIFFWriteEncodedStrip(tif, 0, data, |
| 1151 |
numsamples * blockPtr->width * blockPtr->height); |
| 1152 |
if (data != blockPtr->pixelPtr) { |
| 1153 |
ckfree((char *) data); |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (opts->verbose) { |
| 1157 |
printImgInfo (-1, blockPtr->width, blockPtr->height, xres, yres, fileName, "Saving image:"); |
| 1158 |
} |
| 1159 |
|
| 1160 |
return TCL_OK; |
| 1161 |
} |
| 1162 |
|
| 1163 |
void |
| 1164 |
TkimgTIFFfree(tdata_t data) |
| 1165 |
{ |
| 1166 |
if (_TIFFfree) { |
| 1167 |
_TIFFfree(data); |
| 1168 |
} else { |
| 1169 |
ckfree((char *) data); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
tdata_t |
| 1174 |
TkimgTIFFmalloc(tsize_t size) |
| 1175 |
{ |
| 1176 |
if (_TIFFmalloc) { |
| 1177 |
return _TIFFmalloc(size); |
| 1178 |
} else { |
| 1179 |
return attemptckalloc(size); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
tdata_t |
| 1184 |
TkimgTIFFrealloc( |
| 1185 |
tdata_t data, |
| 1186 |
tsize_t size |
| 1187 |
) { |
| 1188 |
if (_TIFFrealloc) { |
| 1189 |
return _TIFFrealloc(data, size); |
| 1190 |
} else { |
| 1191 |
return attemptckrealloc(data, size); |
| 1192 |
} |
| 1193 |
} |