int main(int argc, char* argv[]) { TIFF *in, *out; if (argc < 2) { fprintf(stderr, "%s\n\n", TIFFGetVersion()); fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n"); return (-3); } if (argc > 2) strcpy(fname, argv[2]); in = TIFFOpen(argv[1], "r"); if (in != NULL) { do { char path[1024+1]; newfilename(); strcpy(path, fname); strcat(path, ".tif"); out = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl"); if (out == NULL) return (-2); if (!tiffcp(in, out)) return (-1); TIFFClose(out); } while (TIFFReadDirectory(in)); (void) TIFFClose(in); } return (0); }
/* * Deal the stacks into separated files and append them. */ void dealStack(const fs::path &outdir, const std::string &prefix, const fs::path &imgPath, const uint16_t nLayer) { TIFF *in, *out; static uint16_t iLayer = 0; // Suppress the warnings. TIFFErrorHandler oldhandler = TIFFSetWarningHandler(NULL); // Open the file. in = TIFFOpen(imgPath.string().c_str(), "r"); if (in == NULL) { std::cerr << "Unable to read " << imgPath.filename() << std::endl; return; } // Identify the read mode. static char mode[3] = { 'x', 'b', 0 }; // Overwrite on the first run, and append for rest of the page. mode[0] = (mode[0] == 'x') ? 'w' : 'a'; mode[1] = (TIFFIsBigEndian(in)) ? 'b' : 'l'; // Iterate through the directories. int iFile = 0; do { std::string s = genPath(outdir, prefix, iFile); out = TIFFOpen(s.c_str(), mode); try { if (out == NULL) { throw -1; } else if (!cpTiff(in, out, iLayer, nLayer)) { throw -2; } } catch (int e) { if (e == -1) { std::cerr << "Unable to create output file" << std::endl; } else if (e == -2) { std::cerr << "Unable to copy the layer" << std::endl; } else { std::cerr << "Unknown error" << std::endl; } TIFFClose(in); TIFFClose(out); return; } TIFFClose(out); iFile++; } while (TIFFReadDirectory(in)); // Increment the layer variable for next write. iLayer++; TIFFClose(in); // Restore the warning. TIFFSetWarningHandler(oldhandler); }
int main(int argc, char* argv[]) { TIFF *in, *out; if (argc < 2) { fprintf(stderr, "%s\n\n", TIFFGetVersion()); fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n"); return (-3); } if (argc > 2) { strncpy(fname, argv[2], sizeof(fname)); fname[sizeof(fname) - 1] = '\0'; } in = TIFFOpen(argv[1], "r"); if (in != NULL) { do { size_t path_len; char *path; newfilename(); path_len = strlen(fname) + sizeof(TIFF_SUFFIX); path = (char *) _TIFFmalloc(path_len); strncpy(path, fname, path_len); path[path_len - 1] = '\0'; strncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1); out = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl"); _TIFFfree(path); if (out == NULL) return (-2); if (!tiffcp(in, out)) return (-1); TIFFClose(out); } while (TIFFReadDirectory(in)); (void) TIFFClose(in); } return (0); }