1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/* $Header: /cvsroot/VTK/VTK/Utilities/vtktiff/tif_read.c,v 1.2 2005/02/28 18:49:39 malaterre Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
* TIFF Library.
* Scanline-oriented Read Support
*/
#include "tiffiop.h"
#include <stdio.h>
#include <assert.h>
static int TIFFFillStrip(TIFF*, tstrip_t);
static int TIFFFillTile(TIFF*, ttile_t);
static int TIFFStartStrip(TIFF*, tstrip_t);
static int TIFFStartTile(TIFF*, ttile_t);
static int TIFFCheckRead(TIFF*, int);
#define NOSTRIP ((tstrip_t) -1) /* undefined state */
#define NOTILE ((ttile_t) -1) /* undefined state */
/*
* Seek to a random row+sample in a file.
*/
static int
TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
{
register TIFFDirectory *td = &tif->tif_dir;
tstrip_t strip;
if (row >= td->td_imagelength) { /* out of range */
TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
(u_long) row, (u_long) td->td_imagelength);
return (0);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
if (sample >= td->td_samplesperpixel) {
TIFFError(tif->tif_name,
"%lu: Sample out of range, max %lu",
(u_long) sample, (u_long) td->td_samplesperpixel);
return (0);
}
strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
} else
strip = row / td->td_rowsperstrip;
if (strip != tif->tif_curstrip) { /* different strip, refill */
if (!TIFFFillStrip(tif, strip))
return (0);
} else if (row < tif->tif_row) {
/*
* Moving backwards within the same strip: backup
* to the start and then decode forward (below).
*
* NB: If you're planning on lots of random access within a
* strip, it's better to just read and decode the entire
* strip, and then access the decoded data in a random fashion.
*/
if (!TIFFStartStrip(tif, strip))
return (0);
}
if (row != tif->tif_row) {
/*
* Seek forward to the desired row.
*/
if (!(*tif->tif_seek)(tif, row - tif->tif_row))
return (0);
tif->tif_row = row;
}
return (1);
}
int
TEXPORT TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
{
int e;
if (!TIFFCheckRead(tif, 0))
return (-1);
if( (e = TIFFSeek(tif, row, sample)) != 0) {
/*
* Decompress desired row into user buffer.
*/
e = (*tif->tif_decoderow)
(tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
tif->tif_row++;
if (e)
(*tif->tif_postdecode)(tif, (tidata_t) buf,
tif->tif_scanlinesize);
}
return (e > 0 ? 1 : -1);
}
/*
* Read a strip of data and decompress the specified
* amount into the user-supplied buffer.
*/
tsize_t
TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
{
TIFFDirectory *td = &tif->tif_dir;
uint32 nrows;
tsize_t stripsize;
tstrip_t sep_strip, strips_per_sep;
if (!TIFFCheckRead(tif, 0))
return (-1);
if (strip >= td->td_nstrips) {
TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld",
(long) strip, (long) td->td_nstrips);
return (-1);
}
/*
* Calculate the strip size according to the number of
* rows in the strip (check for truncated last strip on any
* of the separations).
*/
if( td->td_rowsperstrip >= td->td_imagelength )
strips_per_sep = 1;
else
strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
/ td->td_rowsperstrip;
sep_strip = strip % strips_per_sep;
if (sep_strip != strips_per_sep-1 ||
(nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
nrows = td->td_rowsperstrip;
stripsize = TIFFVStripSize(tif, nrows);
if (size == (tsize_t) -1)
size = stripsize;
else if (size > stripsize)
size = stripsize;
if (TIFFFillStrip(tif, strip) && (*tif->tif_decodestrip)(tif,
(tidata_t) buf, size, (tsample_t)(strip / td->td_stripsperimage))) {
(*tif->tif_postdecode)(tif, (tidata_t) buf, size);
return (size);
} else
return ((tsize_t) -1);
}
static tsize_t
TIFFReadRawStrip1(TIFF* tif,
tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
{
TIFFDirectory *td = &tif->tif_dir;
if (!isMapped(tif)) {
tsize_t cc;
if (!SeekOK(tif, td->td_stripoffset[strip])) {
TIFFError(module,
"%s: Seek error at scanline %lu, strip %lu",
tif->tif_name,
(u_long) tif->tif_row, (u_long) strip);
return (-1);
}
cc = TIFFReadFile(tif, buf, size);
if (cc != size) {
TIFFError(module,
"%s: Read error at scanline %lu; got %lu bytes, expected %lu",
tif->tif_name,
(u_long) tif->tif_row,
(u_long) cc,
(u_long) size);
return (-1);
}
} else {
if (td->td_stripoffset[strip] + size > tif->tif_size) {
TIFFError(module,
"%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
tif->tif_name,
(u_long) tif->tif_row,
(u_long) strip,
(u_long) tif->tif_size - td->td_stripoffset[strip],
(u_long) size);
return (-1);
}
_TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip], size);
}
return (size);
}
/*
* Read a strip of data from the file.
*/
tsize_t
TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
{
static const char module[] = "TIFFReadRawStrip";
TIFFDirectory *td = &tif->tif_dir;
tsize_t bytecount;
if (!TIFFCheckRead(tif, 0))
return ((tsize_t) -1);
if (strip >= td->td_nstrips) {
TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu",
(u_long) strip, (u_long) td->td_nstrips);
return ((tsize_t) -1);
}
bytecount = td->td_stripbytecount[strip];
if (bytecount <= 0) {
TIFFError(tif->tif_name,
"%lu: Invalid strip byte count, strip %lu",
(u_long) bytecount, (u_long) strip);
return ((tsize_t) -1);
}
if (size != (tsize_t)-1 && size < bytecount)
bytecount = size;
return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
}
/*
* Read the specified strip and setup for decoding.
* The data buffer is expanded, as necessary, to
* hold the strip's data.
*/
static int
TIFFFillStrip(TIFF* tif, tstrip_t strip)
{
static const char module[] = "TIFFFillStrip";
TIFFDirectory *td = &tif->tif_dir;
tsize_t bytecount;
bytecount = td->td_stripbytecount[strip];
if (bytecount <= 0) {
TIFFError(tif->tif_name,
"%lu: Invalid strip byte count, strip %lu",
(u_long) bytecount, (u_long) strip);
return (0);
}
if (isMapped(tif) &&
(isFillOrder(tif, td->td_fillorder) || (tif->tif_flags & TIFF_NOBITREV))) {
/*
* The image is mapped into memory and we either don't
* need to flip bits or the compression routine is going
* to handle this operation itself. In this case, avoid
* copying the raw data and instead just reference the
* data from the memory mapped file image. This assumes
* that the decompression routines do not modify the
* contents of the raw data buffer (if they try to,
* the application will get a fault since the file is
* mapped read-only).
*/
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
_TIFFfree(tif->tif_rawdata);
tif->tif_flags &= ~TIFF_MYBUFFER;
if ( td->td_stripoffset[strip] + bytecount > tif->tif_size) {
/*
* This error message might seem strange, but it's
* what would happen if a read were done instead.
*/
TIFFError(module,
"%s: Read error on strip %lu; got %lu bytes, expected %lu",
tif->tif_name,
(u_long) strip,
(u_long) tif->tif_size - td->td_stripoffset[strip],
(u_long) bytecount);
tif->tif_curstrip = NOSTRIP;
return (0);
}
tif->tif_rawdatasize = bytecount;
tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
} else {
/*
* Expand raw data buffer, if needed, to
* hold data strip coming from file
* (perhaps should set upper bound on
* the size of a buffer we'll use?).
*/
if (bytecount > tif->tif_rawdatasize) {
tif->tif_curstrip = NOSTRIP;
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
TIFFError(module,
"%s: Data buffer too small to hold strip %lu",
tif->tif_name, (u_long) strip);
return (0);
}
if (!TIFFReadBufferSetup(tif, 0,
TIFFroundup(bytecount, 1024)))
return (0);
}
if (TIFFReadRawStrip1(tif, strip, (u_char *)tif->tif_rawdata,
bytecount, module) != bytecount)
return (0);
if (!isFillOrder(tif, td->td_fillorder) &&
(tif->tif_flags & TIFF_NOBITREV) == 0)
TIFFReverseBits(tif->tif_rawdata, bytecount);
}
return (TIFFStartStrip(tif, strip));
}
/*
* Tile-oriented Read Support
* Contributed by Nancy Cam (Silicon Graphics).
*/
/*
* Read and decompress a tile of data. The
* tile is selected by the (x,y,z,s) coordinates.
*/
tsize_t
TEXPORT TIFFReadTile(TIFF* tif,
tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
{
if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
return (-1);
return (TIFFReadEncodedTile(tif,
TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
}
/*
* Read a tile of data and decompress the specified
* amount into the user-supplied buffer.
*/
tsize_t
TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
{
TIFFDirectory *td = &tif->tif_dir;
tsize_t tilesize = tif->tif_tilesize;
if (!TIFFCheckRead(tif, 1))
return (-1);
if (tile >= td->td_nstrips) {
TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld",
(long) tile, (u_long) td->td_nstrips);
return (-1);
}
if (size == (tsize_t) -1)
size = tilesize;
else if (size > tilesize)
size = tilesize;
if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
(tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
(*tif->tif_postdecode)(tif, (tidata_t) buf, size);
return (size);
} else
return (-1);
}
static tsize_t
TIFFReadRawTile1(TIFF* tif,
ttile_t tile, tdata_t buf, tsize_t size, const char* module)
{
TIFFDirectory *td = &tif->tif_dir;
if (!isMapped(tif)) {
tsize_t cc;
if (!SeekOK(tif, td->td_stripoffset[tile])) {
TIFFError(module,
"%s: Seek error at row %ld, col %ld, tile %ld",
tif->tif_name,
(long) tif->tif_row,
(long) tif->tif_col,
(long) tile);
return ((tsize_t) -1);
}
cc = TIFFReadFile(tif, buf, size);
if (cc != size) {
TIFFError(module,
"%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
tif->tif_name,
(long) tif->tif_row,
(long) tif->tif_col,
(u_long) cc,
(u_long) size);
return ((tsize_t) -1);
}
} else {
if (td->td_stripoffset[tile] + size > tif->tif_size) {
TIFFError(module,
"%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
tif->tif_name,
(long) tif->tif_row,
(long) tif->tif_col,
(long) tile,
(u_long) tif->tif_size - td->td_stripoffset[tile],
(u_long) size);
return ((tsize_t) -1);
}
_TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
}
return (size);
}
/*
* Read a tile of data from the file.
*/
tsize_t
TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
{
static const char module[] = "TIFFReadRawTile";
TIFFDirectory *td = &tif->tif_dir;
tsize_t bytecount;
if (!TIFFCheckRead(tif, 1))
return ((tsize_t) -1);
if (tile >= td->td_nstrips) {
TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu",
(u_long) tile, (u_long) td->td_nstrips);
return ((tsize_t) -1);
}
bytecount = td->td_stripbytecount[tile];
if (size != (tsize_t) -1 && size < bytecount)
bytecount = size;
return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
}
/*
* Read the specified tile and setup for decoding.
* The data buffer is expanded, as necessary, to
* hold the tile's data.
*/
static int
TIFFFillTile(TIFF* tif, ttile_t tile)
{
static const char module[] = "TIFFFillTile";
TIFFDirectory *td = &tif->tif_dir;
tsize_t bytecount;
bytecount = td->td_stripbytecount[tile];
if (bytecount <= 0) {
TIFFError(tif->tif_name,
"%lu: Invalid tile byte count, tile %lu",
(u_long) bytecount, (u_long) tile);
return (0);
}
if (isMapped(tif) &&
(isFillOrder(tif, td->td_fillorder) || (tif->tif_flags & TIFF_NOBITREV))) {
/*
* The image is mapped into memory and we either don't
* need to flip bits or the compression routine is going
* to handle this operation itself. In this case, avoid
* copying the raw data and instead just reference the
* data from the memory mapped file image. This assumes
* that the decompression routines do not modify the
* contents of the raw data buffer (if they try to,
* the application will get a fault since the file is
* mapped read-only).
*/
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
_TIFFfree(tif->tif_rawdata);
tif->tif_flags &= ~TIFF_MYBUFFER;
if ( td->td_stripoffset[tile] + bytecount > tif->tif_size) {
tif->tif_curtile = NOTILE;
return (0);
}
tif->tif_rawdatasize = bytecount;
tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
} else {
/*
* Expand raw data buffer, if needed, to
* hold data tile coming from file
* (perhaps should set upper bound on
* the size of a buffer we'll use?).
*/
if (bytecount > tif->tif_rawdatasize) {
tif->tif_curtile = NOTILE;
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
TIFFError(module,
"%s: Data buffer too small to hold tile %ld",
tif->tif_name, (long) tile);
return (0);
}
if (!TIFFReadBufferSetup(tif, 0,
TIFFroundup(bytecount, 1024)))
return (0);
}
if (TIFFReadRawTile1(tif, tile, (u_char *)tif->tif_rawdata,
bytecount, module) != bytecount)
return (0);
if (!isFillOrder(tif, td->td_fillorder) &&
(tif->tif_flags & TIFF_NOBITREV) == 0)
TIFFReverseBits(tif->tif_rawdata, bytecount);
}
return (TIFFStartTile(tif, tile));
}
/*
* Setup the raw data buffer in preparation for
* reading a strip of raw data. If the buffer
* is specified as zero, then a buffer of appropriate
* size is allocated by the library. Otherwise,
* the client must guarantee that the buffer is
* large enough to hold any individual strip of
* raw data.
*/
int
TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
{
static const char module[] = "TIFFReadBufferSetup";
if (tif->tif_rawdata) {
if (tif->tif_flags & TIFF_MYBUFFER)
_TIFFfree(tif->tif_rawdata);
tif->tif_rawdata = NULL;
}
if (bp) {
tif->tif_rawdatasize = size;
tif->tif_rawdata = (tidata_t) bp;
tif->tif_flags &= ~TIFF_MYBUFFER;
} else {
tif->tif_rawdatasize = TIFFroundup(size, 1024);
tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
tif->tif_flags |= TIFF_MYBUFFER;
}
if (tif->tif_rawdata == NULL) {
TIFFError(module,
"%s: No space for data buffer at scanline %ld",
tif->tif_name, (long) tif->tif_row);
tif->tif_rawdatasize = 0;
return (0);
}
return (1);
}
/*
* Set state to appear as if a
* strip has just been read in.
*/
static int
TIFFStartStrip(TIFF* tif, tstrip_t strip)
{
TIFFDirectory *td = &tif->tif_dir;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupdecode)(tif))
return (0);
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_curstrip = strip;
tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
tif->tif_rawcp = tif->tif_rawdata;
tif->tif_rawcc = td->td_stripbytecount[strip];
return ((*tif->tif_predecode)(tif,
(tsample_t)(strip / td->td_stripsperimage)));
}
/*
* Set state to appear as if a
* tile has just been read in.
*/
static int
TIFFStartTile(TIFF* tif, ttile_t tile)
{
TIFFDirectory *td = &tif->tif_dir;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupdecode)(tif))
return (0);
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_curtile = tile;
tif->tif_row =
(tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) *
td->td_tilelength;
tif->tif_col =
(tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
td->td_tilewidth;
tif->tif_rawcp = tif->tif_rawdata;
tif->tif_rawcc = td->td_stripbytecount[tile];
return ((*tif->tif_predecode)(tif,
(tsample_t)(tile/td->td_stripsperimage)));
}
static int
TIFFCheckRead(TIFF* tif, int tiles)
{
if (tif->tif_mode == O_WRONLY) {
TIFFError(tif->tif_name, "File not open for reading");
return (0);
}
if (tiles ^ isTiled(tif)) {
TIFFError(tif->tif_name, tiles ?
"Can not read tiles from a stripped image" :
"Can not read scanlines from a tiled image");
return (0);
}
return (1);
}
void
_TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
{
(void) tif; (void) buf; (void) cc;
}
void
_TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
{
(void) tif;
assert((cc & 1) == 0);
TIFFSwabArrayOfShort((uint16*) buf, cc/2);
}
void
_TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
{
(void) tif;
assert((cc & 3) == 0);
TIFFSwabArrayOfLong((uint32*) buf, cc/4);
}
void
_TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
{
(void) tif;
assert((cc & 7) == 0);
TIFFSwabArrayOfDouble((double*) buf, cc/8);
}
|