From 0539009c671a2ffc2478a50a11f48769c5c54b27 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 23 May 2015 23:42:26 +0000 Subject: STR #2873: new function Fl_Image::fail() that returns 0, ERR_NO_IMAGE, ERR_FORMAT, or ERR_FILE_ACCESS to make life easier when loading images. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10732 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_BMP_Image.cxx | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/Fl_BMP_Image.cxx') diff --git a/src/Fl_BMP_Image.cxx b/src/Fl_BMP_Image.cxx index 8542763be..be291b616 100644 --- a/src/Fl_BMP_Image.cxx +++ b/src/Fl_BMP_Image.cxx @@ -52,13 +52,19 @@ static int read_long(FILE *fp); static unsigned short read_word(FILE *fp); static unsigned int read_dword(FILE *fp); + + /** - The constructor loads the named BMP image from the given bmp filename. -

The inherited destructor free all memory and server resources that are used by - the image. -

The destructor free all memory and server resources that are used by - the image -*/ + The constructor loads the named BMP image from the given bmp filename. + + The inherited destructor frees all memory and server resources that are + used by the image. + + Use Fl_Image::fail() to check if Fl_BMP_Image failed to load. fail() returns + ERR_FILE_ACCESS if the file could not bo opened or read, ERR_FORMAT if the + BMP format could not be decoded, and ERR_NO_IMAGE if the image could not + be loaded for another reason. + */ Fl_BMP_Image::Fl_BMP_Image(const char *bmp) // I - File to read : Fl_RGB_Image(0,0,0) { FILE *fp; // File pointer @@ -86,13 +92,17 @@ Fl_BMP_Image::Fl_BMP_Image(const char *bmp) // I - File to read // Open the file... - if ((fp = fl_fopen(bmp, "rb")) == NULL) return; + if ((fp = fl_fopen(bmp, "rb")) == NULL) { + ld(ERR_FILE_ACCESS); + return; + } // Get the header... byte = (uchar)getc(fp); // Check "BM" sync chars bit = (uchar)getc(fp); if (byte != 'B' || bit != 'M') { fclose(fp); + ld(ERR_FORMAT); return; } @@ -161,6 +171,7 @@ Fl_BMP_Image::Fl_BMP_Image(const char *bmp) // I - File to read // Check header data... if (!w() || !h() || !depth) { fclose(fp); + w(0); h(0); d(0); ld(ERR_FORMAT); return; } @@ -191,6 +202,7 @@ Fl_BMP_Image::Fl_BMP_Image(const char *bmp) // I - File to read if (((size_t)w()) * h() * d() > max_size() ) { Fl::warning("BMP file \"%s\" is too large!\n", bmp); fclose(fp); + w(0); h(0); d(0); ld(ERR_FORMAT); return; } array = new uchar[w() * h() * d()]; -- cgit v1.2.3