Yalantis/uCrop · error · CImgInstanceException

select_graph(): Empty instance.

Error message

select_graph(): Empty instance.

What it means

get_select_graph() opens an interactive display to let the user select a graph plot of the image data. It is a non-const-sensitive instance method requiring actual pixel data; calling it on an empty CImg instance throws CImgInstanceException before any display work happens.

Source

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

        else {
          const float
            m = (float)cimg::type<T>::min(),
            M = (float)cimg::type<T>::max();
          (img2d-=m)*=255.f/(M - m>0?M - m:1);
        } break;
      }
      if (img2d.spectrum()==2) img2d.channels(0,2);
      return img2d;
    }

    //! Select sub-graph in a graph.
    CImg<intT> get_select_graph(CImgDisplay &disp,
                                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
                                    "select_graph(): Empty instance.",
                                    cimg_instance);
      if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
                   set_title("CImg<%s>",pixel_type());
      const ulongT siz = (ulongT)_width*_height*_depth;
      const unsigned int old_normalization = disp.normalization();
      disp.show().set_button().set_wheel()._normalization = 0;

      double nymin = ymin, nymax = ymax, nxmin = xmin, nxmax = xmax;
      if (nymin==nymax) { nymin = (Tfloat)min_max(nymax); const double dy = nymax - nymin; nymin-=dy/20; nymax+=dy/20; }
      if (nymin==nymax) { --nymin; ++nymax; }
      if (nxmin==nxmax && nxmin==0) { nxmin = 0; nxmax = siz - 1.; }

      static const unsigned char black[] = { 0, 0, 0 }, white[] = { 255, 255, 255 }, gray[] = { 220, 220, 220 };
      static const unsigned char gray2[] = { 110, 110, 110 }, ngray[] = { 35, 35, 35 };

      CImg<ucharT> colormap(3,_spectrum);
      if (_spectrum==1) { colormap[0] = colormap[1] = 120; colormap[2] = 200; }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check img.is_empty() (or !img) before calling select_graph().
  2. Verify the preceding load()/assign() succeeded before invoking display-based methods.
  3. Initialize the image with real data (load a file or allocate dimensions) before interactive selection.

Example fix

// before
CImg<float> img; // empty
img.select_graph();
// after
CImg<float> img("data.png");
if (!img.is_empty()) img.select_graph();
Defensive patterns

Strategy: type-guard

Validate before calling

if (img.is_empty()) {
  std::cerr << "Cannot select graph on empty image" << std::endl;
  return;
}
img.select_graph();

Type guard

bool readyForGraph(const cimg_library::CImg<float>& img) { return !img.is_empty(); }

Try / catch

try {
  img.select_graph();
} catch (const cimg_library::CImgInstanceException& e) {
  std::cerr << "Empty image: " << e.what() << std::endl;
}

Prevention

When it happens

Trigger: Calling select_graph()/get_select_graph() on a default-constructed CImg, on an instance after assign() (which empties it), or after a failed load() left the image empty.

Common situations: Load failure path not checked (file missing) followed by interactive selection; constructing the image conditionally so one branch leaves it empty; reusing a variable that was reassigned to an empty 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/2fdbfea537131836. Report an issue: GitHub.