Hello,

> You can check if a file is has a certain format by reading the first few
> bytes. FLTK does that for jpeg and all other image files. Take a look at
> the source of Fl.jpeg.Image.

Thank you for your answer.

After thinking about it (but not so long), i've made it like this:

Fl_Image * Images::load (std::string filename)
{
  Fl_Image *image = is_image_already_loaded(filename);

  if (!image) {
    image = loadfromdisk<Fl_JPEG_Image> (filename.c_str());
    if (!image) {
      image = loadfromdisk<Fl_PNG_Image>(filename);
      if (!image) {
        image = loadfromdisk<Fl_BMP_Image> (filename.c_str());
      } else {
        CRITICAL ("unknwown format for : %s\n",filename.c_str());
      }
    }
  }

  return image;
}


As it won't overhead so much, comparing this than doing it myself, 
allocating buffers, reading headers and so on… and in the worst case, 
that's three tries before giving up (and if success, the resulting 
Fl_Image is cached for reusing it later).

loadfromdisk is a template:


template <class T>
Fl_Image * Images::loadfromdisk (const std::string &filename) {

  Fl_Image *img = new T (filename.c_str() );

  if (img->w()) {
    liste.insert ( std::pair<std::string, Fl_Image *>(filename,img) );
  } else {
    delete img;
    img = 0;
  }

  return img;
}


So you see, nothing fancy here, but i think it's readable. And i don't 
need lightspeed here (that's not as if i was loading textures for a full 
realtime wonderful cyberworld).

(at least, i've learned using templates with class instanciation).

_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to