Yalantis/uCrop · error · CImgIOException

save_tiff(): Invalid strip writing when saving file '%s'.

Error message

save_tiff(): Invalid strip writing when saving file '%s'.

What it means

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.

Source

Thrown at ucrop/src/main/jni/CImg.h:62817

      TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric);
      TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_code);
      rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip);
      TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip);
      TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB);
      TIFFSetField(tif,TIFFTAG_SOFTWARE,cimg_appname);

      t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
      if (buf) {
        for (unsigned int row = 0; row<_height; row+=rowsperstrip) {
          cimg_uint32 nrow = (row + rowsperstrip>_height?_height - row:rowsperstrip);
          tstrip_t strip = TIFFComputeStrip(tif,row,0);
          tsize_t i = 0;
          for (unsigned int rr = 0; rr<nrow; ++rr)
            for (unsigned int cc = 0; cc<_width; ++cc)
              for (unsigned int vv = 0; vv<spp; ++vv)
                buf[i++] = (t)(*this)(cc,row + rr,z,vv);
          if (TIFFWriteEncodedStrip(tif,strip,buf,i*sizeof(t))<0)
            throw CImgIOException(_cimg_instance
                                  "save_tiff(): Invalid strip writing when saving file '%s'.",
                                  cimg_instance,
                                  filename?filename:"(FILE*)");
        }
        _TIFFfree(buf);
      }
      TIFFWriteDirectory(tif);
      return *this;
    }

    const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z,
                              const unsigned int compression_type, const float *const voxel_size,
                              const char *const description, double val_min, double val_max) const {
      _cimg_save_tiff("uint8",cimg_uint8);
      _cimg_save_tiff("int8",cimg_int8);
      _cimg_save_tiff("uint16",cimg_uint16);
      _cimg_save_tiff("int16",cimg_int16);
      _cimg_save_tiff("uint32",cimg_uint32);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check free disk space for the destination volume
  2. Switch compression to 0 (none) or a guaranteed codec (e.g. PACKBITS) and retry
  3. Ensure the chosen compression codec exists in your libtiff build (TIFFGetVersion/codec support)
  4. Retry the save after validating stable storage and sufficient space
  5. Catch CImgIOException and save to a temporary file then rename on success

Example fix

// before
img.save_tiff("/mnt/usb/big.tif", 500); // zstd codec may be unavailable
// after
try {
  img.save_tiff("/mnt/usb/big.tif", 0); // uncompressed fallback
} catch (CImgIOException &e) {
  cimg::warn("TIFF write failed: %s", e.what());
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure space and codec support before writing large TIFFs
if (freeDiskBytes(dir) < (long long)img.size()*sizeof(T)) throw std::runtime_error("insufficient disk space");

Try / catch

try { img.save_tiff(path, comp); } catch (CImgIOException &e) { fprintf(stderr, "strip write failed: %s\n", e.what()); remove(path); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/cb0a271ab943e841. Report an issue: GitHub.