diff options
| author | Matthias Melcher <fltk@matthiasm.com> | 2008-10-19 20:35:53 +0000 |
|---|---|---|
| committer | Matthias Melcher <fltk@matthiasm.com> | 2008-10-19 20:35:53 +0000 |
| commit | b989051bd61e9556ebf82c59788b4f3931cdb573 (patch) | |
| tree | 0601216d8701b5320b707c431e9a79877ce20683 /src/fl_read_image.cxx | |
| parent | 0490b303831869c12c84d61025aab1bf2d1f1414 (diff) | |
Improved stability of fl_read_image (STR #2021)
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@6476 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/fl_read_image.cxx')
| -rw-r--r-- | src/fl_read_image.cxx | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/fl_read_image.cxx b/src/fl_read_image.cxx index e1b588875..ca3e31138 100644 --- a/src/fl_read_image.cxx +++ b/src/fl_read_image.cxx @@ -77,6 +77,11 @@ fl_subimage_offsets(int a, int aw, int b, int bw, int &obw) return off; } +// this handler will catch and ignore exceptions during XGetImage +// to avoid an application crash +static int xgetimageerrhandler(Display *display, XErrorEvent *error) { + return 0; +} // // 'fl_read_image()' - Read an image from the current window. @@ -135,7 +140,11 @@ fl_read_image(uchar *p, // I - Pixel buffer or NULL to allocate } if (!win || (dx >= sx && dy >= sy && dx + w <= sw && dy + h <= sh)) { // the image is fully contained, we can use the traditional method + // however, if the window is obscured etc. the function will still fail. Make sure we + // catch the error and continue, otherwise an exception will be thrown. + XErrorHandler old_handler = XSetErrorHandler(xgetimageerrhandler); image = XGetImage(fl_display, fl_window, X, Y, w, h, AllPlanes, ZPixmap); + XSetErrorHandler(old_handler); } else { // image is crossing borders, determine visible region int nw, nh, noffx, noffy; @@ -151,12 +160,15 @@ fl_read_image(uchar *p, // I - Pixel buffer or NULL to allocate if (!image) { if (buf) free(buf); return 0; - } + } - if (!XGetSubImage(fl_display, fl_window, X + noffx, Y + noffy, - nw, nh, AllPlanes, ZPixmap, image, noffx, noffy)) { - XDestroyImage(image); - return 0; + XErrorHandler old_handler = XSetErrorHandler(xgetimageerrhandler); + XImage *subimg = XGetSubImage(fl_display, fl_window, X + noffx, Y + noffy, + nw, nh, AllPlanes, ZPixmap, image, noffx, noffy); + XSetErrorHandler(old_handler); + if (!subimg) { + XDestroyImage(image); + return 0; } } } |
