{"record":{"id":"cb0a271ab943e841","repo":"Yalantis/uCrop","slug":"save-tiff-invalid-strip-writing-when-saving-fil","errorCode":null,"errorMessage":"save_tiff(): Invalid strip writing when saving file '%s'.","messagePattern":"save_tiff\\(\\): Invalid strip writing when saving file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62817,"sourceCode":"      TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric);\n      TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_code);\n      rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip);\n      TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip);\n      TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB);\n      TIFFSetField(tif,TIFFTAG_SOFTWARE,cimg_appname);\n\n      t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));\n      if (buf) {\n        for (unsigned int row = 0; row<_height; row+=rowsperstrip) {\n          cimg_uint32 nrow = (row + rowsperstrip>_height?_height - row:rowsperstrip);\n          tstrip_t strip = TIFFComputeStrip(tif,row,0);\n          tsize_t i = 0;\n          for (unsigned int rr = 0; rr<nrow; ++rr)\n            for (unsigned int cc = 0; cc<_width; ++cc)\n              for (unsigned int vv = 0; vv<spp; ++vv)\n                buf[i++] = (t)(*this)(cc,row + rr,z,vv);\n          if (TIFFWriteEncodedStrip(tif,strip,buf,i*sizeof(t))<0)\n            throw CImgIOException(_cimg_instance\n                                  \"save_tiff(): Invalid strip writing when saving file '%s'.\",\n                                  cimg_instance,\n                                  filename?filename:\"(FILE*)\");\n        }\n        _TIFFfree(buf);\n      }\n      TIFFWriteDirectory(tif);\n      return *this;\n    }\n\n    const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z,\n                              const unsigned int compression_type, const float *const voxel_size,\n                              const char *const description, double val_min, double val_max) const {\n      _cimg_save_tiff(\"uint8\",cimg_uint8);\n      _cimg_save_tiff(\"int8\",cimg_int8);\n      _cimg_save_tiff(\"uint16\",cimg_uint16);\n      _cimg_save_tiff(\"int16\",cimg_int16);\n      _cimg_save_tiff(\"uint32\",cimg_uint32);","sourceCodeStart":62799,"sourceCodeEnd":62835,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62799-L62835","documentation":"During strip-based TIFF writing, _save_tiff fills a buffer with pixel rows and calls TIFFWriteEncodedStrip; a return value <0 means libtiff failed to encode/write the strip (compression or I/O error), and CImg throws CImgIOException naming the output file.","triggerScenarios":"TIFFWriteEncodedStrip failing mid-save — typically disk full while writing, compression codec error/failure (bad codec build, e.g. LZW/ZSTD/WEBP unavailable), or underlying I/O error on the already-opened TIFF handle.","commonSituations":"Large multi-page images exhausting disk space during write; selecting a compression scheme whose codec is missing in the bundled libtiff; network/removable storage disappearing mid-write; memory-pressure failures in encoder.","solutions":["Check free disk space for the destination volume","Switch compression to 0 (none) or a guaranteed codec (e.g. PACKBITS) and retry","Ensure the chosen compression codec exists in your libtiff build (TIFFGetVersion/codec support)","Retry the save after validating stable storage and sufficient space","Catch CImgIOException and save to a temporary file then rename on success"],"exampleFix":"// before\nimg.save_tiff(\"/mnt/usb/big.tif\", 500); // zstd codec may be unavailable\n// after\ntry {\n  img.save_tiff(\"/mnt/usb/big.tif\", 0); // uncompressed fallback\n} catch (CImgIOException &e) {\n  cimg::warn(\"TIFF write failed: %s\", e.what());\n}","handlingStrategy":"try-catch","validationCode":"// ensure space and codec support before writing large TIFFs\nif (freeDiskBytes(dir) < (long long)img.size()*sizeof(T)) throw std::runtime_error(\"insufficient disk space\");","typeGuard":null,"tryCatchPattern":"try { img.save_tiff(path, comp); } catch (CImgIOException &e) { fprintf(stderr, \"strip write failed: %s\\n\", e.what()); remove(path); }","preventionTips":["Reserve sufficient disk space before large saves","Prefer widely-available codecs (none, PACKBITS, DEFLATE)","Write to a temp file and rename atomically on success","Keep libtiff codecs enabled in the NDK build"],"tags":["tiff","file-write","compression","libtiff"],"backgroundTag":"file-write-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}