331 streamsize request_size = size;
332 if (
static_cast<tmsize_t>(request_size) != size)
335 data->
stream->read((
char *)buf, request_size);
343 ostream *os = data->
stream;
344 ios::pos_type pos = os->tellp();
347 streamsize request_size = size;
348 if (
static_cast<tmsize_t>(request_size) != size)
351 os->write(
reinterpret_cast<const char *
>(buf), request_size);
353 return static_cast<tmsize_t>(os->tellp() - pos);
361 ostream *os = data->
stream;
365 return static_cast<uint64_t
>(-1);
372 uint64_t new_offset =
373 static_cast<uint64_t
>(data->
start_pos) + off;
376 ios::off_type offset =
static_cast<ios::off_type
>(new_offset);
377 if (
static_cast<uint64_t
>(offset) != new_offset)
378 return static_cast<uint64_t
>(-1);
380 os->seekp(offset, ios::beg);
386 ios::off_type offset =
static_cast<ios::off_type
>(off);
387 if (
static_cast<uint64_t
>(offset) != off)
388 return static_cast<uint64_t
>(-1);
390 os->seekp(offset, ios::cur);
396 ios::off_type offset =
static_cast<ios::off_type
>(off);
397 if (
static_cast<uint64_t
>(offset) != off)
398 return static_cast<uint64_t
>(-1);
400 os->seekp(offset, ios::end);
411 ios::iostate old_state;
412 ios::pos_type origin;
414 old_state = os->rdstate();
416 os->clear(os->rdstate() & ~ios::failbit);
424 origin = os->tellp();
427 os->seekp(0, ios::end);
428 origin = os->tellp();
432 os->clear(old_state);
435 if ((
static_cast<uint64_t
>(origin) + off) >
441 os->clear(os->rdstate() & ~ios::failbit);
444 os->seekp(0, ios::end);
445 num_fill = (
static_cast<uint64_t
>(origin)) + off - os->tellp();
446 for (uint64_t i = 0; i < num_fill; i++)
450 os->seekp(
static_cast<ios::off_type
>(
451 static_cast<uint64_t
>(origin) + off),
456 return static_cast<uint64_t
>(os->tellp());
463 if (data->
start_pos == ios::pos_type(-1))
464 return static_cast<uint64_t
>(-1);
471 uint64_t new_offset =
472 static_cast<uint64_t
>(data->
start_pos) + off;
475 ios::off_type offset =
static_cast<ios::off_type
>(new_offset);
476 if (
static_cast<uint64_t
>(offset) != new_offset)
477 return static_cast<uint64_t
>(-1);
479 data->
stream->seekg(offset, ios::beg);
485 ios::off_type offset =
static_cast<ios::off_type
>(off);
486 if (
static_cast<uint64_t
>(offset) != off)
487 return static_cast<uint64_t
>(-1);
489 data->
stream->seekg(offset, ios::cur);
495 ios::off_type offset =
static_cast<ios::off_type
>(off);
496 if (
static_cast<uint64_t
>(offset) != off)
497 return static_cast<uint64_t
>(-1);
499 data->
stream->seekg(offset, ios::end);
504 ios::pos_type pos = data->
stream->tellg();
505 if (pos == ios::pos_type(-1))
506 return static_cast<uint64_t
>(-1);
508 ios::off_type relative_pos = pos - data->
start_pos;
509 if (relative_pos < 0)
510 return static_cast<uint64_t
>(-1);
512 return static_cast<uint64_t
>(relative_pos);
518 ostream *os = data->
stream;
519 ios::pos_type pos = os->tellp();
523 if (pos == ios::pos_type(-1))
526 os->seekp(0, ios::end);
529 ios::iostate old_state = os->rdstate();
531 os->clear(os->rdstate() & ~ios::failbit);
533 os->clear(old_state);
537 if (len == ios::pos_type(-1))
539 ios::iostate old_state = os->rdstate();
541 os->clear(os->rdstate() & ~ios::failbit);
543 os->clear(old_state);
548 return static_cast<uint64_t
>(len);
554 ios::pos_type pos = data->
stream->tellg();
558 if (pos == ios::pos_type(-1))
561 data->
stream->seekg(0, ios::end);
564 ios::iostate old_state = data->
stream->rdstate();
566 data->
stream->clear(data->
stream->rdstate() & ~ios::failbit);
568 data->
stream->clear(old_state);
571 len = data->
stream->tellg();
572 if (len == ios::pos_type(-1))
574 ios::iostate old_state = data->
stream->rdstate();
576 data->
stream->clear(data->
stream->rdstate() & ~ios::failbit);
578 data->
stream->clear(old_state);
583 return static_cast<uint64_t
>(len);
620 if (strchr(mode,
'w'))
623 data->
stream =
reinterpret_cast<ostream *
>(fd);
639 data->
stream =
reinterpret_cast<istream *
>(fd);
663 if (!os->fail() &&
static_cast<int>(os->tellp()) < 0)
static TIFF * _tiffStreamOpen(const char *name, const char *mode, void *fd)
Allocate a stream context and attach it through TIFFClientOpen().
static tmsize_t _tiffisWriteProc(thandle_t fd, void *buf, tmsize_t size)
Refuse writes on an input-stream adapter.
static int _tiffDummyMapProcCxx(thandle_t fd, void **base, toff_t *size)
Decline mapping for the C++ stream adapter.
static uint64_t _tiffisSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the borrowed input stream relative to its recorded TIFF origin.
static void _tiffDummyUnmapProcCxx(thandle_t fd, void *base, toff_t size)
Provide a no-op unmapper for the C++ stream adapter.
static int _tiffisCloseProc(thandle_t fd)
Delete a stream adapter without closing the borrowed stream.
static uint64_t _tiffosSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the borrowed output stream, with a zero-fill extension fallback.
static uint64_t _tiffisSizeProc(thandle_t fd)
Query the absolute end position of the borrowed stream.
static tmsize_t _tiffosWriteProc(thandle_t fd, void *buf, tmsize_t size)
Write bytes to the borrowed output stream.
static tmsize_t _tiffisReadProc(thandle_t fd, void *buf, tmsize_t size)
Read bytes from the borrowed input stream.
static tmsize_t _tiffosReadProc(thandle_t fd, void *buf, tmsize_t size)
Refuse reads on an output-stream adapter.
static int _tiffosCloseProc(thandle_t fd)
Delete a stream adapter without closing the borrowed stream.
static uint64_t _tiffosSizeProc(thandle_t fd)
Query the absolute end position of the borrowed stream.
TIFF * TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc)
Initialise a TIFF handle using client-supplied I/O callbacks.
TIFF * TIFFStreamOpen(const char *name, std::ostream *os)
Open a TIFF writer over a caller-owned C++ stream.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
void * thandle_t
Carry an opaque client I/O context through library callbacks.
Hold private state for one open TIFF library instance.
Track a borrowed input stream and its initial position.
ios::pos_type start_pos
Initial tell position, possibly the failure value -1.
istream * stream
Borrowed stream that outlives this adapter.
Track a borrowed output stream and its initial position.
ostream * stream
Borrowed stream that outlives this adapter.
ios::pos_type start_pos
Initial tell position, possibly the failure value -1.
Declare private library state, callbacks, and implementation helpers.