{"record":{"id":"1a86c75369726e23","repo":"Yalantis/uCrop","slug":"load-tiff-file-s-contains-u-image-s-while","errorCode":null,"errorMessage":"load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).","messagePattern":"load_tiff\\(\\): File '(.+?)' contains %u image\\(s\\) while specified frame range is \\[%u,%u\\] \\(step %u\\)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58005,"sourceCode":"#ifndef cimg_use_tiff\n      cimg::unused(bits_per_value,voxel_size,description);\n      if (nfirst_frame || nlast_frame!=~0U || nstep_frame>1)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_tiff(): Unable to read sub-images from file '%s' unless libtiff is enabled.\",\n                                    cimg_instance,\n                                    filename);\n      return load_other(filename);\n#else\n#if cimg_verbosity<3\n      TIFFSetWarningHandler(0);\n      TIFFSetErrorHandler(0);\n#endif\n      TIFF *tif = TIFFOpen(filename,\"r\");\n      if (tif) {\n        unsigned int nb_images = 0;\n        do ++nb_images; while (TIFFReadDirectory(tif));\n        if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))\n          cimg::warn(_cimg_instance\n                     \"load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).\",\n                     cimg_instance,\n                     filename,nb_images,nfirst_frame,nlast_frame,nstep_frame);\n\n        if (nfirst_frame>=nb_images) return assign();\n        if (nlast_frame>=nb_images) nlast_frame = nb_images - 1;\n        TIFFSetDirectory(tif,0);\n        CImg<T> frame;\n        for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) {\n          frame._load_tiff(tif,l,bits_per_value,voxel_size,description);\n          if (l==nfirst_frame)\n            assign(frame._width,frame._height,1 + (nlast_frame - nfirst_frame)/nstep_frame,frame._spectrum);\n          if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum)\n            resize(std::max(frame._width,_width),\n                   std::max(frame._height,_height),-100,\n                   std::max(frame._spectrum,_spectrum),0);\n          draw_image(0,0,(l - nfirst_frame)/nstep_frame,frame);\n        }","sourceCodeStart":57987,"sourceCodeEnd":58023,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57987-L58023","documentation":"load_tiff() emits this cimg::warn (non-fatal) when the requested frame range does not fit the actual number of images/directories stored in the TIFF file. The library counts the TIFF directories and, if nfirst_frame is beyond the last frame (or nlast_frame is), warns and then clamps nlast_frame or returns an empty image. It indicates the load will not return what was asked for.","triggerScenarios":"Calling CImg::load_tiff(filename, nfirst_frame, nlast_frame, nstep_frame) where nb_images counted via TIFFReadDirectory is smaller than the requested range: nfirst_frame>=nb_images, or nlast_frame!=~0U and nlast_frame>=nb_images.","commonSituations":"Hardcoded frame indices after the source TIFF was regenerated with fewer pages; multi-page scans truncated; off-by-one assuming frames are 1-based when load_tiff is 0-based; iterating a directory of TIFFs with varying page counts.","solutions":["Check the frame count first (e.g. via CImg::load_tiff with a probe or a TIFF library call) and clamp nfirst_frame/nlast_frame to nb_images-1.","Use ~0U (default) for nlast_frame instead of a hardcoded count so the library clamps automatically.","Remember frame indices are 0-based; subtract 1 from any 1-based index.","Re-export the source TIFF with the expected number of pages if the data itself is wrong."],"exampleFix":"// before\nimg.load_tiff(\"scan.tif\", 0, 9); // assumes 10 pages\n// after\nunsigned int nb = 0; { TIFF* t = TIFFOpen(\"scan.tif\",\"r\"); do ++nb; while (TIFFReadDirectory(t)); TIFFClose(t); }\nimg.load_tiff(\"scan.tif\", 0, nb ? nb-1 : ~0U);","handlingStrategy":"validation","validationCode":"unsigned int nb = 0;\n{ /* count TIFF directories before loading */ }\nif (nfirst >= nb || (nlast != ~0U && nlast >= nb)) clamp_or_throw();","typeGuard":"bool frameRangeValid(unsigned first, unsigned last, unsigned nb) { return first < nb && (last == ~0U || last < nb); }","tryCatchPattern":null,"preventionTips":["Treat cimg warnings as log output and monitor them in CI for image pipelines.","Use ~0U for nlast_frame unless you know the exact page count.","Remember frame indices are 0-based.","Verify TIFF page counts when source files are regenerated."],"tags":["tiff","image-loading","frames","out-of-range"],"backgroundTag":"value-out-of-range","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"}