| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static int |
| _tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize) |
| { |
| (void) fd; (void) pbase; (void) psize; |
| return (0); |
| } |
| |
| static void |
| _tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size) |
| { |
| (void) fd; (void) base; (void) size; |
| } |
| |
| int |
| _TIFFgetMode(const char* mode, const char* module) |
| { |
| int m = -1; |
| |
| switch (mode[0]) { |
| case 'r': |
| m = O_RDONLY; |
| if (mode[1] == '+') |
| m = O_RDWR; |
| break; |
| case 'w': |
| case 'a': |
| m = O_RDWR|O_CREAT; |
| if (mode[0] == 'w') |
| m |= O_TRUNC; |
| break; |
| default: |
| TIFFErrorExt(0, module, "\"%s\": Bad mode", mode); |
| break; |
| } |
| return (m); |
| } |
| |
| 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 |
| ) |
| { |
| static const char module[] = "TIFFClientOpen"; |
| TIFF *tif; |
| int m; |
| const char* cp; |
| |
| |
| |
| |
| assert(sizeof(uint8)==1); |
| assert(sizeof(int8)==1); |
| assert(sizeof(uint16)==2); |
| assert(sizeof(int16)==2); |
| assert(sizeof(uint32)==4); |
| assert(sizeof(int32)==4); |
| assert(sizeof(uint64)==8); |
| assert(sizeof(int64)==8); |
| assert(sizeof(tmsize_t)==sizeof(void*)); |
| { |
| union{ |
| uint8 a8[2]; |
| uint16 a16; |
| } n; |
| n.a8[0]=1; |
| n.a8[1]=0; |
| |
| assert(n.a16==256); |
| |
| assert(n.a16==1); |
| |
| } |
| |
| m = _TIFFgetMode(mode, module); |
| if (m == -1) |
| goto bad2; |
| tif = (TIFF *)_TIFFmalloc((tmsize_t)(sizeof (TIFF) + strlen(name) + 1)); |
| if (tif == NULL) { |
| TIFFErrorExt(clientdata, module, "%s: Out of memory (TIFF structure)", name); |
| goto bad2; |
| } |
| _TIFFmemset(tif, 0, sizeof (*tif)); |
| tif->tif_name = (char *)tif + sizeof (TIFF); |
| strcpy(tif->tif_name, name); |
| tif->tif_mode = m &~ (O_CREAT|O_TRUNC); |
| tif->tif_curdir = (uint16) -1; |
| tif->tif_curoff = 0; |
| tif->tif_curstrip = (uint32) -1; |
| tif->tif_row = (uint32) -1; |
| tif->tif_clientdata = clientdata; |
| if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) { |
| TIFFErrorExt(clientdata, module, |
| "One of the client procedures is NULL pointer."); |
| goto bad2; |
| } |
| tif->tif_readproc = readproc; |
| tif->tif_writeproc = writeproc; |
| tif->tif_seekproc = seekproc; |
| tif->tif_closeproc = closeproc; |
| tif->tif_sizeproc = sizeproc; |
| if (mapproc) |
| tif->tif_mapproc = mapproc; |
| else |
| tif->tif_mapproc = _tiffDummyMapProc; |
| if (unmapproc) |
| tif->tif_unmapproc = unmapproc; |
| else |
| tif->tif_unmapproc = _tiffDummyUnmapProc; |
| _TIFFSetDefaultCompressionState(tif); |
| |
| |
| |
| |
| |
| tif->tif_flags = FILLORDER_MSB2LSB; |
| if (m == O_RDONLY ) |
| tif->tif_flags |= TIFF_MAPPED; |
| |
| |
| if (m == O_RDONLY || m == O_RDWR) |
| tif->tif_flags |= STRIPCHOP_DEFAULT; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for (cp = mode; *cp; cp++) |
| switch (*cp) { |
| case 'b': |
| |
| if (m&O_CREAT) |
| tif->tif_flags |= TIFF_SWAB; |
| |
| break; |
| case 'l': |
| |
| if ((m&O_CREAT)) |
| tif->tif_flags |= TIFF_SWAB; |
| |
| break; |
| case 'B': |
| tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) | |
| FILLORDER_MSB2LSB; |
| break; |
| case 'L': |
| tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) | |
| FILLORDER_LSB2MSB; |
| break; |
| case 'H': |
| tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) | |
| HOST_FILLORDER; |
| break; |
| case 'M': |
| if (m == O_RDONLY) |
| tif->tif_flags |= TIFF_MAPPED; |
| break; |
| case 'm': |
| if (m == O_RDONLY) |
| tif->tif_flags &= ~TIFF_MAPPED; |
| break; |
| case 'C': |
| if (m == O_RDONLY) |
| tif->tif_flags |= TIFF_STRIPCHOP; |
| break; |
| case 'c': |
| if (m == O_RDONLY) |
| tif->tif_flags &= ~TIFF_STRIPCHOP; |
| break; |
| case 'h': |
| tif->tif_flags |= TIFF_HEADERONLY; |
| break; |
| case '8': |
| if (m&O_CREAT) |
| tif->tif_flags |= TIFF_BIGTIFF; |
| break; |
| } |
| |
| |
| |
| if ((m & O_TRUNC) || |
| !ReadOK(tif, &tif->tif_header, sizeof (TIFFHeaderClassic))) { |
| if (tif->tif_mode == O_RDONLY) { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Cannot read TIFF header"); |
| goto bad; |
| } |
| |
| |
| |
| |
| tif->tif_header.common.tiff_magic = tif->tif_flags & TIFF_SWAB |
| ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN; |
| |
| tif->tif_header.common.tiff_magic = tif->tif_flags & TIFF_SWAB |
| ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN; |
| |
| if (!(tif->tif_flags&TIFF_BIGTIFF)) |
| { |
| tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC; |
| tif->tif_header.classic.tiff_diroff = 0; |
| if (tif->tif_flags & TIFF_SWAB) |
| TIFFSwabShort(&tif->tif_header.common.tiff_version); |
| tif->tif_header_size = sizeof(TIFFHeaderClassic); |
| } |
| else |
| { |
| tif->tif_header.common.tiff_version = TIFF_VERSION_BIG; |
| tif->tif_header.big.tiff_offsetsize = 8; |
| tif->tif_header.big.tiff_unused = 0; |
| tif->tif_header.big.tiff_diroff = 0; |
| if (tif->tif_flags & TIFF_SWAB) |
| { |
| TIFFSwabShort(&tif->tif_header.common.tiff_version); |
| TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize); |
| } |
| tif->tif_header_size = sizeof (TIFFHeaderBig); |
| } |
| |
| |
| |
| |
| |
| |
| |
| TIFFSeekFile( tif, 0, SEEK_SET ); |
| if (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size))) { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Error writing TIFF header"); |
| goto bad; |
| } |
| |
| |
| |
| if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) { |
| |
| tif->tif_flags |= TIFF_SWAB; |
| |
| } else { |
| |
| tif->tif_flags |= TIFF_SWAB; |
| |
| } |
| |
| |
| |
| if (!TIFFDefaultDirectory(tif)) |
| goto bad; |
| tif->tif_diroff = 0; |
| tif->tif_dirlist = NULL; |
| tif->tif_dirlistsize = 0; |
| tif->tif_dirnumber = 0; |
| return (tif); |
| } |
| |
| |
| |
| if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN && |
| tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN |
| |
| && |
| |
| tif->tif_header.common.tiff_magic != MDI_BIGENDIAN |
| |
| tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN |
| |
| ) { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Not a TIFF or MDI file, bad magic number %d (0x%x)", |
| |
| ) { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Not a TIFF file, bad magic number %d (0x%x)", |
| |
| tif->tif_header.common.tiff_magic, |
| tif->tif_header.common.tiff_magic); |
| goto bad; |
| } |
| if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) { |
| |
| tif->tif_flags |= TIFF_SWAB; |
| |
| } else { |
| |
| tif->tif_flags |= TIFF_SWAB; |
| |
| } |
| if (tif->tif_flags & TIFF_SWAB) |
| TIFFSwabShort(&tif->tif_header.common.tiff_version); |
| if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC)&& |
| (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Not a TIFF file, bad version number %d (0x%x)", |
| tif->tif_header.common.tiff_version, |
| tif->tif_header.common.tiff_version); |
| goto bad; |
| } |
| if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC) |
| { |
| if (tif->tif_flags & TIFF_SWAB) |
| TIFFSwabLong(&tif->tif_header.classic.tiff_diroff); |
| tif->tif_header_size = sizeof(TIFFHeaderClassic); |
| } |
| else |
| { |
| if (!ReadOK(tif, ((uint8*)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), (sizeof(TIFFHeaderBig)-sizeof(TIFFHeaderClassic)))) |
| { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Cannot read TIFF header"); |
| goto bad; |
| } |
| if (tif->tif_flags & TIFF_SWAB) |
| { |
| TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize); |
| TIFFSwabLong8(&tif->tif_header.big.tiff_diroff); |
| } |
| if (tif->tif_header.big.tiff_offsetsize != 8) |
| { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)", |
| tif->tif_header.big.tiff_offsetsize, |
| tif->tif_header.big.tiff_offsetsize); |
| goto bad; |
| } |
| if (tif->tif_header.big.tiff_unused != 0) |
| { |
| TIFFErrorExt(tif->tif_clientdata, name, |
| "Not a TIFF file, bad BigTIFF unused %d (0x%x)", |
| tif->tif_header.big.tiff_unused, |
| tif->tif_header.big.tiff_unused); |
| goto bad; |
| } |
| tif->tif_header_size = sizeof(TIFFHeaderBig); |
| tif->tif_flags |= TIFF_BIGTIFF; |
| } |
| tif->tif_flags |= TIFF_MYBUFFER; |
| tif->tif_rawcp = tif->tif_rawdata = 0; |
| tif->tif_rawdatasize = 0; |
| tif->tif_rawdataoff = 0; |
| tif->tif_rawdataloaded = 0; |
| |
| switch (mode[0]) { |
| case 'r': |
| if (!(tif->tif_flags&TIFF_BIGTIFF)) |
| tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff; |
| else |
| tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff; |
| |
| |
| |
| |
| |
| if (tif->tif_flags & TIFF_MAPPED) |
| { |
| toff_t n; |
| if (TIFFMapFileContents(tif,(void**)(&tif->tif_base),&n)) |
| { |
| tif->tif_size=(tmsize_t)n; |
| assert((toff_t)tif->tif_size==n); |
| } |
| else |
| tif->tif_flags &= ~TIFF_MAPPED; |
| } |
| |
| |
| |
| |
| |
| |
| if (tif->tif_flags & TIFF_HEADERONLY) |
| return (tif); |
| |
| |
| |
| |
| if (TIFFReadDirectory(tif)) { |
| tif->tif_rawcc = (tmsize_t)-1; |
| tif->tif_flags |= TIFF_BUFFERSETUP; |
| return (tif); |
| } |
| break; |
| case 'a': |
| |
| |
| |
| |
| |
| if (!TIFFDefaultDirectory(tif)) |
| goto bad; |
| return (tif); |
| } |
| bad: |
| tif->tif_mode = O_RDONLY; |
| TIFFCleanup(tif); |
| bad2: |
| return ((TIFF*)0); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| const char * |
| TIFFFileName(TIFF* tif) |
| { |
| return (tif->tif_name); |
| } |
| |
| |
| |
| |
| const char * |
| TIFFSetFileName(TIFF* tif, const char *name) |
| { |
| const char* old_name = tif->tif_name; |
| tif->tif_name = (char *)name; |
| return (old_name); |
| } |
| |
| |
| |
| |
| int |
| TIFFFileno(TIFF* tif) |
| { |
| return (tif->tif_fd); |
| } |
| |
| |
| |
| |
| int |
| TIFFSetFileno(TIFF* tif, int fd) |
| { |
| int old_fd = tif->tif_fd; |
| tif->tif_fd = fd; |
| return old_fd; |
| } |
| |
| |
| |
| |
| thandle_t |
| TIFFClientdata(TIFF* tif) |
| { |
| return (tif->tif_clientdata); |
| } |
| |
| |
| |
| |
| thandle_t |
| TIFFSetClientdata(TIFF* tif, thandle_t newvalue) |
| { |
| thandle_t m = tif->tif_clientdata; |
| tif->tif_clientdata = newvalue; |
| return m; |
| } |
| |
| |
| |
| |
| int |
| TIFFGetMode(TIFF* tif) |
| { |
| return (tif->tif_mode); |
| } |
| |
| |
| |
| |
| int |
| TIFFSetMode(TIFF* tif, int mode) |
| { |
| int old_mode = tif->tif_mode; |
| tif->tif_mode = mode; |
| return (old_mode); |
| } |
| |
| |
| |
| |
| |
| int |
| TIFFIsTiled(TIFF* tif) |
| { |
| return (isTiled(tif)); |
| } |
| |
| |
| |
| |
| uint32 |
| TIFFCurrentRow(TIFF* tif) |
| { |
| return (tif->tif_row); |
| } |
| |
| |
| |
| |
| uint16 |
| TIFFCurrentDirectory(TIFF* tif) |
| { |
| return (tif->tif_curdir); |
| } |
| |
| |
| |
| |
| uint32 |
| TIFFCurrentStrip(TIFF* tif) |
| { |
| return (tif->tif_curstrip); |
| } |
| |
| |
| |
| |
| uint32 |
| TIFFCurrentTile(TIFF* tif) |
| { |
| return (tif->tif_curtile); |
| } |
| |
| |
| |
| |
| int |
| TIFFIsByteSwapped(TIFF* tif) |
| { |
| return ((tif->tif_flags & TIFF_SWAB) != 0); |
| } |
| |
| |
| |
| |
| int |
| TIFFIsUpSampled(TIFF* tif) |
| { |
| return (isUpSampled(tif)); |
| } |
| |
| |
| |
| |
| int |
| TIFFIsMSB2LSB(TIFF* tif) |
| { |
| return (isFillOrder(tif, FILLORDER_MSB2LSB)); |
| } |
| |
| |
| |
| |
| int |
| TIFFIsBigEndian(TIFF* tif) |
| { |
| return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN); |
| } |
| |
| |
| |
| |
| TIFFReadWriteProc |
| TIFFGetReadProc(TIFF* tif) |
| { |
| return (tif->tif_readproc); |
| } |
| |
| |
| |
| |
| TIFFReadWriteProc |
| TIFFGetWriteProc(TIFF* tif) |
| { |
| return (tif->tif_writeproc); |
| } |
| |
| |
| |
| |
| TIFFSeekProc |
| TIFFGetSeekProc(TIFF* tif) |
| { |
| return (tif->tif_seekproc); |
| } |
| |
| |
| |
| |
| TIFFCloseProc |
| TIFFGetCloseProc(TIFF* tif) |
| { |
| return (tif->tif_closeproc); |
| } |
| |
| |
| |
| |
| TIFFSizeProc |
| TIFFGetSizeProc(TIFF* tif) |
| { |
| return (tif->tif_sizeproc); |
| } |
| |
| |
| |
| |
| TIFFMapFileProc |
| TIFFGetMapFileProc(TIFF* tif) |
| { |
| return (tif->tif_mapproc); |
| } |
| |
| |
| |
| |
| TIFFUnmapFileProc |
| TIFFGetUnmapFileProc(TIFF* tif) |
| { |
| return (tif->tif_unmapproc); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |