{"record":{"id":"154b43fc0193aa69","repo":"Yalantis/uCrop","slug":"draw-quiver-invalid-sampling-value-g-should-b","errorCode":null,"errorMessage":"draw_quiver(): Invalid sampling value %g (should be >0)","messagePattern":"draw_quiver\\(\\): Invalid sampling value %g \\(should be >0\\)","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":53108,"sourceCode":"       \\param sampling Length (in pixels) between each arrow.\n       \\param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length).\n       \\param is_arrow Tells if arrows must be drawn, instead of oriented segments.\n       \\param pattern Used pattern to draw lines.\n       \\note Clipping is supported.\n    **/\n    template<typename t1, typename t2>\n    CImg<T>& draw_quiver(const CImg<t1>& flow,\n                         const CImg<t2>& color, const float opacity=1,\n                         const unsigned int sampling=25, const float factor=-20,\n                         const bool is_arrow=true, const unsigned int pattern=~0U) {\n      if (is_empty()) return *this;\n      if (!flow || flow._spectrum!=2)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_quiver(): Invalid dimensions of specified flow (%u,%u,%u,%u,%p).\",\n                                    cimg_instance,\n                                    flow._width,flow._height,flow._depth,flow._spectrum,flow._data);\n      if (sampling<=0)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_quiver(): Invalid sampling value %g \"\n                                    \"(should be >0)\",\n                                    cimg_instance,\n                                    sampling);\n      const bool colorfield = (color._width==flow._width && color._height==flow._height &&\n                               color._depth==1 && color._spectrum==_spectrum);\n      if (is_overlapped(flow)) return draw_quiver(+flow,color,opacity,sampling,factor,is_arrow,pattern);\n      float vmax,fact;\n      if (factor<=0) {\n        float m, M = (float)flow.get_norm(2).max_min(m);\n        vmax = (float)std::max(cimg::abs(m),cimg::abs(M));\n        if (!vmax) vmax = 1;\n        fact = -factor;\n      } else { fact = factor; vmax = 1; }\n\n      for (unsigned int y = sampling/2; y<_height; y+=sampling)\n        for (unsigned int x = sampling/2; x<_width; x+=sampling) {\n          const unsigned int X = x*flow._width/_width, Y = y*flow._height/_height;","sourceCodeStart":53090,"sourceCodeEnd":53126,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L53090-L53126","documentation":"draw_quiver() validates that its sampling parameter (the grid spacing in pixels between drawn arrows) is strictly positive, throwing CImgArgumentException with the offending value otherwise. Sampling controls downsampling of the flow field; zero or negative spacing is mathematically meaningless for the stride loop.","triggerScenarios":"Calling img.draw_quiver(flow,color,opacity,0) or with a negative sampling value; computing sampling dynamically (e.g. width/step) where step>width yields 0; passing float 0 through a default-argument chain.","commonSituations":"Auto-deriving sampling from image size for tiny images (e.g. sampling = w/64 == 0 for small thumbnails); user-configurable density setting allowed to be 0 or negative; unit mix-ups where sampling was given as a density instead of spacing.","solutions":["Clamp before calling: if (sampling<1) sampling=1.","Ensure derived sampling uses at least std::max(1u, width/step).","Validate the user/config-facing density parameter and reject or clamp non-positive values at the input boundary.","If a very dense quiver plot is intended, use sampling=1, not 0."],"exampleFix":"// before\nunsigned sampling = img.width()/density; // density>width => 0\nimg.draw_quiver(flow,color,1.f,sampling);\n// after\nunsigned sampling = std::max(1u, img.width()/density);\nimg.draw_quiver(flow,color,1.f,sampling);","handlingStrategy":"validation","validationCode":"unsigned sampling = std::max(1u, computedSampling);\nimg.draw_quiver(flow,color,1.f,sampling);","typeGuard":null,"tryCatchPattern":"try { img.draw_quiver(flow,color,1.f,sampling); }\ncatch (CImgArgumentException& e) { log_error(\"draw_quiver sampling=%u rejected\", sampling); }","preventionTips":["Clamp computed sampling with std::max(1u, value) at derivation time.","For tiny images, guard width/step computations against truncation to 0.","Validate user-facing density settings to reject <=0 up front.","Remember sampling is a pixel stride (>=1), not a density."],"tags":["cimg","image-processing","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}