{"record":{"id":"c035ea6b47c833d3","repo":"Yalantis/uCrop","slug":"save-tiff-unsupported-pixel-type-s-for-file","errorCode":null,"errorMessage":"save_tiff(): Unsupported pixel type '%s' for file '%s'.","messagePattern":"save_tiff\\(\\): Unsupported pixel type '(.+?)' for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62842,"sourceCode":"      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);\n      _cimg_save_tiff(\"int32\",cimg_int32);\n      _cimg_save_tiff(\"uint64\",cimg_uint32); // 'int64' as 'int32'\n      _cimg_save_tiff(\"int64\",cimg_int32);\n      _cimg_save_tiff(\"float32\",cimg_float32);\n      _cimg_save_tiff(\"float64\",cimg_float32); // 'float64' as 'float32'\n      const char *const filename = TIFFFileName(tif);\n      throw CImgInstanceException(_cimg_instance\n                                  \"save_tiff(): Unsupported pixel type '%s' for file '%s'.\",\n                                  cimg_instance,\n                                  pixel_type(),filename?filename:\"(FILE*)\");\n      return *this;\n    }\n#endif\n\n    //! Save image as a MINC2 file.\n    /**\n       \\param filename Filename, as a C-string.\n       \\param imitate_file If non-zero, reference filename, as a C-string, to borrow header from.\n    **/\n    const CImg<T>& save_minc2(const char *const filename,\n                              const char *const imitate_file=0) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                   \"save_minc2(): Specified filename is (null).\",\n                                   cimg_instance);","sourceCodeStart":62824,"sourceCodeEnd":62860,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62824-L62860","documentation":"CImg's save_tiff() only supports a fixed set of pixel types (int32, uint64-as-int32, int64-as-int32, float32, float64-as-float32) when writing via libtiff. When the image instance's pixel type T does not map to any registered writer case, the dispatcher falls through and throws CImgInstanceException naming the actual pixel_type() and the TIFF file name. Note that per the source, 64-bit types are silently downcast, so the error is only about truly unmatched types (e.g. unsigned char/short or float16-style data).","triggerScenarios":"Calling CImg<T>::save_tiff() (directly or via save()) with a CImg instantiated on an unsupported element type such as CImg<unsigned char>, CImg<unsigned short>, or CImg<bool>, since the TIFF writer has no _cimg_save_tiff case for those widths.","commonSituations":"Saving grayscale masks or edge maps built as unsigned char/short images to .tif/.tiff; code written for a float pipeline later reused with 8-bit images; pixel type changed during a refactor while the save call stayed generic through save(filename).","solutions":["Convert the image to a supported type before saving: img.get_float32().save_tiff(filename) or get_save via .get_resize of type, e.g. CImg<float>(img).save_tiff(filename).","If 8-bit data must stay 8-bit, use a different format that supports it (save_png, save_bmp) or the cimg_use_tiff build with a matching case.","Check the CImg version; newer versions register more pixel type cases for TIFF — updating CImg.h may remove the error.","Specialize the save call on pixel type explicitly instead of calling save() generically, so unsupported types are rejected early with a clear message."],"exampleFix":"// before\nCImg<unsigned char> mask(512,512,1,3,0);\nmask.save_tiff(\"out.tif\"); // throws: unsupported pixel type 'unsigned char'\n\n// after\nCImg<float> maskf(mask);\nmaskf.save_tiff(\"out.tif\");","handlingStrategy":"validation","validationCode":"static const char* kTiffTypes[] = {\"int32\",\"uint32\",\"int64\",\"uint64\",\"float32\",\"float64\"};\nbool ok = false;\nfor (const char* t : kTiffTypes)\n  if (!cimg::strcasecmp(img.pixel_type(), t)) { ok = true; break; }\nif (!ok) throw std::runtime_error(std::string(\"TIFF unsupported for \") + img.pixel_type());","typeGuard":"template <typename T>\nconstexpr bool tiff_supported() {\n  return std::is_same_v<T,cimg_int32> || std::is_same_v<T,cimg_uint32>\n      || std::is_same_v<T,float> || std::is_same_v<T,double>;\n}","tryCatchPattern":"try {\n  img.save_tiff(\"out.tif\");\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"TIFF save failed (%s); converting to float\\n\", e.what());\n  CImg<float>(img).save_tiff(\"out.tif\");\n}","preventionTips":["Only call save_tiff on 32/64-bit-typed CImg instances","Prefer generic save() with a pixel-type check helper","Note 64-bit types are downcast to 32-bit by TIFF writer — check docs for precision loss"],"tags":["cimg","tiff","pixel-type","image-saving"],"backgroundTag":"unsupported-dtype","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"}