示例#1
1
Fl_RGB_Image *img_to_rgb(const char *file)
{
  FILE *fp;
  size_t len;
  unsigned char bytes[8];
  struct stat st;

  if (!file || stat(file, &st) == -1 || st.st_size > IMGFILE_MAX) {
    return NULL;
  }

  /* get filetype from extension */
  if (HASEXT(file, ".svg")) {
    if (st.st_size > SVG_MAX) {
      return NULL;
    }
    Fl_SVG_Image *svg = new Fl_SVG_Image(file, NULL);
    CHECK_IF_LOADED(svg)
    return svg;
  }

  if (HASEXT(file, ".svgz") || HASEXT(file, ".svg.gz")) {
    if (st.st_size > SVGZ_MAX) {
      return NULL;
    }
    Fl_SVG_Image *svg = new Fl_SVG_Image(file, NULL);
    CHECK_IF_LOADED(svg)
    return svg;
  }

  if (HASEXT(file, ".xpm")) {
    Fl_XPM_Image *xpm = new Fl_XPM_Image(file);
    CHECK_IF_LOADED(xpm)
    Fl_RGB_Image *rgb = new Fl_RGB_Image(xpm, Fl_Color(0));
    delete xpm;
    return rgb;
  }

  if (HASEXT(file, ".xbm")) {
    Fl_XBM_Image *in = new Fl_XBM_Image(file);
    CHECK_IF_LOADED(in)
    Fl_Image_Surface *surf = new Fl_Image_Surface(in->w(), in->h());
    if (!surf) {
      delete in;
      return NULL;
    }
    surf->set_current();
    fl_color(FL_WHITE);
    fl_rectf(0, 0, in->w(), in->h());
    fl_color(FL_BLACK);
    in->draw(0, 0);
    Fl_RGB_Image *rgb = surf->image();
    delete in;
    delete surf;
    return rgb;
  }

  /* get filetype from magic bytes */
  if (!(fp = fopen(file, "r"))) {
    return NULL;
  }

  len = fread(bytes, 1, sizeof(bytes), fp);
  if (ferror(fp) || len < sizeof(bytes)) {
    fclose(fp);
    return NULL;
  }

  fclose(fp);

  if (memcmp(bytes, "\211PNG\r\n\032\n", 8) == 0) {
    Fl_PNG_Image *png = new Fl_PNG_Image(file);
    CHECK_IF_LOADED(png)
    return png;
  }
示例#2
0
bool Fl_GroupAniSwitch::generate_img( Fl_Group* src, Fl_RGB_Image* &dst )
{
    if ( src != NULL )
    {
        Fl_Image_Surface* imgsurf = new Fl_Image_Surface( src->w(), src->h(), 0 );

        if ( imgsurf != NULL )
        {
            imgsurf->set_current();
            imgsurf->draw( src );
            dst = imgsurf->image();

            Fl_Display_Device::display_device()->set_current();

            delete imgsurf;

            if ( dst != NULL )
                return true;
        }
    }

    return false;
}