Yalantis/uCrop · error · CImgInstanceException

display_graph(): Empty instance.

Error message

display_graph(): Empty instance.

What it means

Thrown by the graph-display routines (_display_graph/display_graph) when the CImg instance whose data should be plotted is empty — it has no pixels/values to draw. The method needs at least one data point to construct a graph, so an empty (default-constructed or failed-load) image cannot be visualized. Fill the instance with data before calling display_graph().

Source

Thrown at ucrop/src/main/jni/CImg.h:61123

    }

    //! Display 1D graph in an interactive window \overloading.
    const CImg<T>& display_graph(const char *const title=0,
                                 const unsigned int plot_type=1, const unsigned int vertex_type=1,
                                 const char *const labelx=0, const double xmin=0, const double xmax=0,
                                 const char *const labely=0, const double ymin=0, const double ymax=0,
                                 const bool exit_on_anykey=false) const {
      CImgDisplay disp;
      return _display_graph(disp,title,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax,exit_on_anykey);
    }

    const CImg<T>& _display_graph(CImgDisplay &disp, const char *const title=0,
                                  const unsigned int plot_type=1, const unsigned int vertex_type=1,
                                  const char *const labelx=0, const double xmin=0, const double xmax=0,
                                  const char *const labely=0, const double ymin=0, const double ymax=0,
                                  const bool exit_on_anykey=false) const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "display_graph(): Empty instance.",
                                    cimg_instance);
      if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
                   set_title(title?"%s":"CImg<%s>",title?title:pixel_type());
      const ulongT siz = (ulongT)_width*_height*_depth, siz1 = std::max((ulongT)1,siz - 1);
      const unsigned int old_normalization = disp.normalization();
      disp.show().flush()._normalization = 0;

      double y0 = ymin, y1 = ymax, nxmin = xmin, nxmax = xmax;
      if (nxmin==nxmax) { nxmin = 0; nxmax = siz1; }
      int x0 = 0, x1 = width()*height()*depth() - 1, key = 0;

      for (bool reset_view = true; !key && !disp.is_closed(); ) {
        if (reset_view) { x0 = 0; x1 = width()*height()*depth() - 1; y0 = ymin; y1 = ymax; reset_view = false; }
        CImg<T> zoom(x1 - x0 + 1,1,1,spectrum());
        cimg_forC(*this,c) zoom.get_shared_channel(c) = CImg<T>(data(x0,0,0,c),x1 - x0 + 1,1,1,1,true);
        if (y0==y1) { y0 = zoom.min_max(y1); const double dy = y1 - y0; y0-=dy/20; y1+=dy/20; }
        if (y0==y1) { --y0; ++y1; }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check img.is_empty() before calling display_graph().
  2. Verify the data-loading/cropping step actually produced pixels.
  3. Populate the instance with the values to plot before displaying.
  4. Use a fallback message/plot when data is empty instead of displaying.

Example fix

// before
img.display_graph("values");
// after
if (!img.is_empty()) img.display_graph("values");
else cimg::warn("No data to plot.");
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_empty()) { cimg::warn("Nothing to plot"); return; }

Try / catch

try { img.display_graph(); } catch (CImgInstanceException& e) { /* plot fallback / show message */ }

Prevention

When it happens

Trigger: Calling display_graph() on a CImg constructed empty (CImg<T>()), on a crop that selected zero pixels, or on an image loaded from an empty file.

Common situations: Plotting a histogram of an empty ROI, plotting data from a failed load that returned an empty instance, slice extraction with inverted coordinates producing zero-size image.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/34508044495fa5b6. Report an issue: GitHub.