If it can help I use the following procedure wich read only the header of the
image filename and return a char* representing the type of image
char * typeImage (char * filename)
{
char * type ;
type = new char[6];
strcpy (type,"ANY");
char header[64];
FILE * fichier;
fichier = fopen (filename,"rb");
if (! fichier) return type;
fread (header,sizeof (char),64,fichier);
fclose (fichier);
if (memcmp(header, "GIF87a", 6) == 0 ||
memcmp(header, "GIF89a", 6) == 0)
{
strcpy (type,"GIF");
}
if (memcmp(header, "BM", 2) == 0) // BMP file
{
strcpy (type,"BMP");
}
if (header[0] == 'P' && (header[1] == '3' || header[1] == '6'))
{
strcpy (type,"PPM");
}
if (header[0] == 'P' && (header[1] == '2' || header[1] == '5'))
{
strcpy (type,"PGM");
}
if (header[0] == 'P' && header[1] == '1' )
{
strcpy (type,"PBM");
}
if (memcmp(header, "\211PNG", 4) == 0)
{
strcpy (type,"PNG");
}
/*
if (memcmp(header, "\377\330\377", 3) == 0 &&
header[3] >= 0xc0 && header[3] <= 0xef)
*/
if (memcmp(header, "\377\330\377", 3) == 0)
{
strcpy (type,"JPEG");
}
if (memcmp(header, "II*", 3) == 0||memcmp(header, "MM*", 3) == 0 )
{
strcpy (type,"TIFF");
}
return type;
}
> Hello,
>
> I wonder how i could detect RGB image format, without loading image in a
> buffer, and then parsing its header.
>
> For the moment, i'm doing this in my classe, which is far from being
> efficient:
>
>
> Fl_Image * Images::load (std::string filename)
> {
> Fl_Image *image = is_image_already_loaded(filename);
>
> if (!image) {
> image = new Fl_JPEG_Image (filename.c_str());
> if (image->w()) {
> //the image is cached when loading once.
> liste.insert(std::pair<std::string, Fl_Image *>(filename,image) );
> } else {
> delete image;
> image = 0;
> }
> }
>
> if (!image) {
> image = new Fl_PNG_Image (filename.c_str());
> if (image->w()) {
> liste.insert(std::pair<std::string, Fl_Image *>(filename,image) );
> } else {
> delete image;
> image = 0;
> }
> }
>
> if (!image) {
> image = new Fl_BMP_Image (filename.c_str());
> if (image->w()) {
> liste.insert(std::pair<std::string, Fl_Image *>(filename,image) );
> } else {
> CRITICAL ("unknwown format for : %s\n",filename.c_str());
> delete image;
> image = 0;
> }
> }
>
> return image;
> }
>
> Of course, i could preload the image in a buffer, and then trying
> Fl_JPEG/PNG/BMP_Image from this buffer.
> I could also use a template for the three almost identical load.
> As i cache the picture once loaded, i think i don't get a big speed
> penalty, even if i load BMP pictures.
>
> Thank you.
> (�premature optimizing is the root of all evil�, i know too :) )
>
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk