{"record":{"id":"c429fe28b3f3bca1","repo":"Yalantis/uCrop","slug":"save-tiff-specified-compression-type-u-is-in","errorCode":null,"errorMessage":"save_tiff(): Specified compression type (%u) is invalid (should be in range [0,%u]).","messagePattern":"save_tiff\\(\\): Specified compression type \\(%u\\) is invalid \\(should be in range \\[0,%u\\]\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62767,"sourceCode":"                              const char *const description, double val_min, double val_max) const {\n      if (is_empty() || !tif || pixel_t) return *this;\n      const unsigned int compression_codes[] = {\n        COMPRESSION_NONE, COMPRESSION_ADOBE_DEFLATE, COMPRESSION_CCITT_T4, COMPRESSION_CCITT_T6,\n        COMPRESSION_CCITTFAX3, COMPRESSION_CCITTFAX4, COMPRESSION_CCITTRLE, COMPRESSION_CCITTRLEW,\n        COMPRESSION_DCS, COMPRESSION_DEFLATE, COMPRESSION_IT8BL, COMPRESSION_IT8CTPAD, COMPRESSION_IT8LW,\n        COMPRESSION_IT8MP, COMPRESSION_JBIG, COMPRESSION_JP2000, COMPRESSION_JPEG,\n#ifdef COMPRESSION_JXL\n        COMPRESSION_JXL,\n#else\n        COMPRESSION_NONE,\n#endif\n        COMPRESSION_LERC, COMPRESSION_LZMA, COMPRESSION_LZW, COMPRESSION_NEXT, COMPRESSION_OJPEG,\n        COMPRESSION_PACKBITS, COMPRESSION_PIXARFILM, COMPRESSION_PIXARLOG, COMPRESSION_SGILOG,\n        COMPRESSION_SGILOG24, COMPRESSION_T43, COMPRESSION_T85, COMPRESSION_THUNDERSCAN, COMPRESSION_WEBP,\n        COMPRESSION_ZSTD },\n        nb_compression_codes = sizeof(compression_codes)/sizeof(unsigned int);\n      if (compression_type>=nb_compression_codes)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_tiff(): Specified compression type (%u) is invalid \"\n                                    \"(should be in range [0,%u]).\",\n                                    cimg_instance,nb_compression_codes - 1);\n      const unsigned int compression_code = compression_codes[compression_type];\n      const char *const filename = TIFFFileName(tif);\n      cimg_uint32 rowsperstrip = (cimg_uint32)-1;\n      cimg_uint16 spp = _spectrum, bpp = sizeof(t)*8, photometric;\n      if (spp==3 || spp==4) photometric = PHOTOMETRIC_RGB;\n      else photometric = PHOTOMETRIC_MINISBLACK;\n      TIFFSetDirectory(tif,directory);\n      TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,_width);\n      TIFFSetField(tif,TIFFTAG_IMAGELENGTH,_height);\n      if (voxel_size) {\n        const float vx = voxel_size[0], vy = voxel_size[1], vz = voxel_size[2];\n        TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_NONE);\n        TIFFSetField(tif,TIFFTAG_XRESOLUTION,1.f/vx);\n        TIFFSetField(tif,TIFFTAG_YRESOLUTION,1.f/vy);\n        CImg<charT> s_description(256);","sourceCodeStart":62749,"sourceCodeEnd":62785,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62749-L62785","documentation":"In _save_tiff, the compression_type argument is used as an index into a fixed table of libtiff COMPRESSION_* codes; if the index is >= nb_compression_codes the value does not map to a known compression scheme and CImgArgumentException is thrown, reporting the valid range [0, nb-1].","triggerScenarios":"Calling save_tiff(filename, compression_type) or _save_tiff with a numeric compression id beyond the size of CImg's table (table size varies with libtiff build/version); passing raw libtiff COMPRESSION_* constants (e.g. COMPRESSION_LZW=5) directly instead of the table index.","commonSituations":"Users copying libtiff constant values into CImg's index parameter; libtiff upgrade/downgrade changing the number of supported codes; hard-coded index from documentation of a different CImg/libtiff version.","solutions":["Pass a table index within [0, nb_compression_codes-1], not a raw COMPRESSION_* constant","Determine the index of the desired scheme in CImg's compression_codes array for your build","Use compression_type=0 (no/none) to test, then add compression incrementally","Pin libtiff version so the table size matches your chosen index"],"exampleFix":"// before\nimg.save_tiff(\"out.tif\", COMPRESSION_DEFLATE); // raw constant = wrong\n// after\n// index of COMPRESSION_ADOBE_DEFLATE within CImg's compression_codes table\nimg.save_tiff(\"out.tif\", 8);","handlingStrategy":"validation","validationCode":"// index into CImg's compression_codes table, not a raw COMPRESSION_* constant\nif (compression_type < 0 || compression_type > maxCompressionIndex()) throw std::out_of_range(\"bad tiff compression index\");","typeGuard":"bool isValidCompressionIndex(unsigned t) { return t <= 30; } // keep in sync with libtiff build table size","tryCatchPattern":"try { img.save_tiff(path, compIdx); } catch (CImgArgumentException &e) { /* invalid compression */ compIdx = 0; img.save_tiff(path, compIdx); }","preventionTips":["Pass table indices, never raw libtiff COMPRESSION_* values","Check CImg docs/source for the compression_codes table size in your version","Pin libtiff/CImg versions in the build","Default to compression_type=0 when unsure"],"tags":["tiff","invalid-argument","compression","libtiff"],"backgroundTag":"invalid-enum-value","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"}