

Image::ErrorHandler ehandler = 0;

void Image::DefaultErrorHandler(Image::Error err, char *func)
{
  cerr << "Image: Error " << Image::errorName(err) << " in \"" << func << "\"\n";
  exit 0;
}

void Image::handleError(Image::Error err, char *func)
{
  if (ehandler)   (*ehandler)(err, func);
  else            Image::DefaultErrorHandler(err, func);
}

Image::ErrorHandler Image::setEH(Image::ErrorHandler eh)
{
  Image::ErrorHandler r = ehandler;
  ehandler = eh;
  return r;
}

Image::ErrorHandler Image::getEH()
{
  return ehandler;
}

const char *Image::errorName(Image::Error err)
{
  switch (err) {
  case Image::NoError:    return "No error";
  case Image::Bounds:     return "Bounds error";
  case Image::Allocation: return "Out of memory";
  case Image::FileSave:   return "Unable to open output file";
  case Image::FileLoad:   return "Unable to open input file";
  case Image::LoadTag:    return "Input is not an image";
  case Image::NoData:     return "Image has no data pointer";
  case Image::NoHeader:   return "Image has no header";
  case Image::BadHeader:  return "Image has bad header";

  default:         return "Unknown error";
  }
}





void ImageHeader::handleError(Image::Error err, char *func)
{
  if (ehandler)  (*ehandler)(err, func);
  else           Image::handleError(err, func);
}

Image::ErrorHandler ImageHeader::setEH(Image::ErrorHandler eh)
{
  Image::ErrorHandler r = ehandler;
  ehandler = eh;
  return r;
}

Image::ErrorHandler ImageHeader::getEH()
{
  return ehandler;
}


void ImageOf<T> handleError(Error err, char *func)
{
  if (header  &&  header->goodtag()  &&  header->ehandler)
    (*(header->ehandler))(err, func);
  else if (ehandler)
    (*ehandler)(err, func);
  else
    Image::handleError(err, func);
}
