{"record":{"id":"db7ea08e668478cc","repo":"Yalantis/uCrop","slug":"crop-empty-instance","errorCode":null,"errorMessage":"crop(): Empty instance.","messagePattern":"crop\\(\\): Empty instance\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":40935,"sourceCode":"       \\param c0 = C-coordinate of the upper-left crop rectangle corner.\n       \\param x1 = X-coordinate of the lower-right crop rectangle corner.\n       \\param y1 = Y-coordinate of the lower-right crop rectangle corner.\n       \\param z1 = Z-coordinate of the lower-right crop rectangle corner.\n       \\param c1 = C-coordinate of the lower-right crop rectangle corner.\n       \\param boundary_conditions = Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.\n    **/\n    CImg<T>& crop(const int x0, const int y0, const int z0, const int c0,\n                  const int x1, const int y1, const int z1, const int c1,\n                  const unsigned int boundary_conditions=0) {\n      return get_crop(x0,y0,z0,c0,x1,y1,z1,c1,boundary_conditions).move_to(*this);\n    }\n\n    //! Crop image region \\newinstance.\n    CImg<T> get_crop(const int x0, const int y0, const int z0, const int c0,\n                     const int x1, const int y1, const int z1, const int c1,\n                     const unsigned int boundary_conditions=0) const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"crop(): Empty instance.\",\n                                    cimg_instance);\n      const int\n        nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,\n        ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0,\n        nz0 = z0<z1?z0:z1, nz1 = z0^z1^nz0,\n        nc0 = c0<c1?c0:c1, nc1 = c0^c1^nc0;\n      const unsigned int\n        _boundary_conditions = nx0>=0 && nx1<width() &&\n                               ny0>=0 && ny1<height() &&\n                               nz0>=0 && nz1<depth() &&\n                               nc0>=0 && nc1<spectrum()?0:boundary_conditions;\n      CImg<T> res(1U + nx1 - nx0,1U + ny1 - ny0,1U + nz1 - nz0,1U + nc1 - nc0);\n      if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height() || nz0<0 || nz1>=depth() || nc0<0 || nc1>=spectrum())\n        switch (_boundary_conditions) {\n        case 3 : { // Mirror\n          const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth(), s2 = 2*spectrum();\n          cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&","sourceCodeStart":40917,"sourceCodeEnd":40953,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L40917-L40953","documentation":"CImg's crop()/get_crop() operates on pixel data of the instance it is called on, so it refuses to run on an empty (zero-dimension or unassigned) image. If is_empty() is true it throws CImgInstanceException with 'crop(): Empty instance.' This guards against computing coordinates on an image with no data.","triggerScenarios":"Calling img.crop(x0,y0,z0,c0,x1,y1,z1,c1) on a CImg that was default-constructed, assigned from a failed load (CImg(filename) with missing file leaves an empty image), or explicitly resized to 0. Also after assign() resets, or when a getter returned an empty image that was passed along un-checked.","commonSituations":"Image file failed to load (wrong path/format) so the CImg stayed empty; a decode step returned an empty buffer; an optional image that was never initialized flows into a crop call; a filtered pipeline step produced a 0-size result that is then cropped.","solutions":["Check img.is_empty() (or !img) before calling crop() and handle the empty case explicitly.","Verify the image was actually loaded: check the return of load()/constructor and that width()/height() are non-zero before cropping.","Fix the upstream cause — correct the file path/format or the producing step so the image contains data.","If empty input is legitimate, skip the crop or substitute a placeholder image instead of cropping."],"exampleFix":"// before\nCImg<unsigned char> img(\"missing.png\"); // load fails, img is empty\nimg.crop(0,0,99,99); // throws: Empty instance\n// after\nCImg<unsigned char> img(\"missing.png\");\nif (!img.is_empty()) img.crop(0,0,99,99);","handlingStrategy":"type-guard","validationCode":"// C++\nif (img.is_empty() || !img) {\n    // skip or load/repair the image before cropping\n    return;\n}\nimg.crop(x0, y0, z0, c0, x1, y1, z1, c1);","typeGuard":"bool isCroppers = !img.is_empty() && img.width() > 0 && img.height() > 0;","tryCatchPattern":"try {\n    img.crop(x0, y0, z0, c0, x1, y1, z1, c1);\n} catch (const cimg_library::CImgInstanceException& e) {\n    // img was empty: log and recover (reload, skip, or placeholder)\n}","preventionTips":["Always check the result of load()/the CImg(filename) constructor for an empty image before further processing","Centralize image loading in one helper that throws a clear app-level error on failed loads","Use '!img' or img.is_empty() guards at the start of any function accepting a CImg parameter","Trace 0-size results from pipeline stages; don't let empty images flow silently into downstream crops"],"tags":["cimg","empty-image","crop","invalid-state"],"backgroundTag":"empty-required-field","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"}