X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/a64bfdfa5a90731b804c057f2bcc74a8ba02937c..fb39b937b0628f4592b07d0aa61a41cf696abd30:/src/image.c diff --git a/src/image.c b/src/image.c index 14c74f1060..7901b95f23 100644 --- a/src/image.c +++ b/src/image.c @@ -1,5 +1,6 @@ /* Functions for image support on window system. - Copyright (C) 1989, 1992-2011 Free Software Foundation, Inc. + +Copyright (C) 1989, 1992-2012 Free Software Foundation, Inc. This file is part of GNU Emacs. @@ -18,8 +19,6 @@ along with GNU Emacs. If not, see . */ #include #include -#include -#include #include #ifdef HAVE_PNG @@ -32,6 +31,8 @@ along with GNU Emacs. If not, see . */ #include +#include + /* This makes the fields of a Display accessible, in Xlib header files. */ #define XLIB_ILLEGAL_ACCESS @@ -48,11 +49,19 @@ along with GNU Emacs. If not, see . */ #include "termhooks.h" #include "font.h" -#ifdef HAVE_X_WINDOWS -#include "xterm.h" -#include +#ifdef HAVE_SYS_STAT_H #include +#endif /* HAVE_SYS_STAT_H */ + +#ifdef HAVE_SYS_TYPES_H +#include +#endif /* HAVE_SYS_TYPES_H */ +#ifdef HAVE_WINDOW_SYSTEM +#include TERM_HEADER +#endif /* HAVE_WINDOW_SYSTEM */ + +#ifdef HAVE_X_WINDOWS #define COLOR_TABLE_SUPPORT 1 typedef struct x_bitmap_record Bitmap_Record; @@ -65,11 +74,8 @@ typedef struct x_bitmap_record Bitmap_Record; #define PIX_MASK_DRAW 1 #endif /* HAVE_X_WINDOWS */ - #ifdef HAVE_NTGUI #include "w32.h" -#include "w32term.h" - /* W32_TODO : Color tables on W32. */ #undef COLOR_TABLE_SUPPORT @@ -82,15 +88,9 @@ typedef struct w32_bitmap_record Bitmap_Record; #define PIX_MASK_RETAIN 0 #define PIX_MASK_DRAW 1 -#define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual #define x_defined_color w32_defined_color #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits) -/* Functions from w32term.c that depend on XColor (so can't go in w32term.h - without modifying lots of files). */ -extern void x_query_colors (struct frame *f, XColor *colors, int ncolors); -extern void x_query_color (struct frame *f, XColor *color); - /* Version of libpng that we were compiled with, or -1 if no PNG support was compiled in. This is tested by w32-win.el to correctly set up the alist used to search for PNG libraries. */ @@ -98,10 +98,6 @@ Lisp_Object Qlibpng_version; #endif /* HAVE_NTGUI */ #ifdef HAVE_NS -#include "nsterm.h" -#include -#include - #undef COLOR_TABLE_SUPPORT typedef struct ns_bitmap_record Bitmap_Record; @@ -115,10 +111,8 @@ typedef struct ns_bitmap_record Bitmap_Record; #define PIX_MASK_RETAIN 0 #define PIX_MASK_DRAW 1 -#define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO (f)->visual #define x_defined_color(f, name, color_def, alloc) \ ns_defined_color (f, name, color_def, alloc, 0) -#define FRAME_X_SCREEN(f) 0 #define DefaultDepthOfScreen(screen) x_display_list->n_planes #endif /* HAVE_NS */ @@ -137,7 +131,6 @@ static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b); static void free_color_table (void); static unsigned long *colors_in_color_table (int *n); #endif -static Lisp_Object Finit_image_library (Lisp_Object, Lisp_Object); /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap id, which is just an int that this section returns. Bitmaps are @@ -319,11 +312,11 @@ x_create_bitmap_from_file (struct frame *f, Lisp_Object file) id = x_allocate_bitmap_record (f); dpyinfo->bitmaps[id - 1].img = bitmap; dpyinfo->bitmaps[id - 1].refcount = 1; - dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SBYTES (file) + 1); + dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); dpyinfo->bitmaps[id - 1].depth = 1; dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap); dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap); - strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file)); + strcpy (dpyinfo->bitmaps[id - 1].file, SSDATA (file)); return id; #endif @@ -365,7 +358,7 @@ x_create_bitmap_from_file (struct frame *f, Lisp_Object file) dpyinfo->bitmaps[id - 1].pixmap = bitmap; dpyinfo->bitmaps[id - 1].have_mask = 0; dpyinfo->bitmaps[id - 1].refcount = 1; - dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SBYTES (file) + 1); + dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); dpyinfo->bitmaps[id - 1].depth = 1; dpyinfo->bitmaps[id - 1].height = height; dpyinfo->bitmaps[id - 1].width = width; @@ -414,9 +407,9 @@ x_destroy_bitmap (FRAME_PTR f, ptrdiff_t id) if (--bm->refcount == 0) { - BLOCK_INPUT; + block_input (); free_bitmap_record (dpyinfo, bm); - UNBLOCK_INPUT; + unblock_input (); } } } @@ -436,6 +429,9 @@ x_destroy_all_bitmaps (Display_Info *dpyinfo) dpyinfo->bitmaps_last = 0; } +static bool x_create_x_image_and_pixmap (struct frame *, int, int, int, + XImagePtr *, Pixmap *); +static void x_destroy_x_image (XImagePtr ximg); #ifdef HAVE_X_WINDOWS @@ -447,23 +443,17 @@ static unsigned long four_corners_best (XImagePtr ximg, unsigned long width, unsigned long height); -static int x_create_x_image_and_pixmap (struct frame *f, int width, int height, - int depth, XImagePtr *ximg, - Pixmap *pixmap); - -static void x_destroy_x_image (XImagePtr ximg); - /* Create a mask of a bitmap. Note is this not a perfect mask. It's nicer with some borders in this context */ -int +void x_create_bitmap_mask (struct frame *f, ptrdiff_t id) { Pixmap pixmap, mask; XImagePtr ximg, mask_img; unsigned long width, height; - int result; + bool result; unsigned long bg; unsigned long x, y, xp, xm, yp, ym; GC gc; @@ -471,29 +461,29 @@ x_create_bitmap_mask (struct frame *f, ptrdiff_t id) Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); if (!(id > 0)) - return -1; + return; pixmap = x_bitmap_pixmap (f, id); width = x_bitmap_width (f, id); height = x_bitmap_height (f, id); - BLOCK_INPUT; + block_input (); ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height, ~0, ZPixmap); if (!ximg) { - UNBLOCK_INPUT; - return -1; + unblock_input (); + return; } result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask); - UNBLOCK_INPUT; + unblock_input (); if (!result) { XDestroyImage (ximg); - return -1; + return; } bg = four_corners_best (ximg, NULL, width, height); @@ -521,7 +511,7 @@ x_create_bitmap_mask (struct frame *f, ptrdiff_t id) } } - xassert (interrupt_input_blocked); + eassert (input_blocked_p ()); gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL); XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0, width, height); @@ -532,8 +522,6 @@ x_create_bitmap_mask (struct frame *f, ptrdiff_t id) XDestroyImage (ximg); x_destroy_x_image (mask_img); - - return 0; } #endif /* HAVE_X_WINDOWS */ @@ -566,15 +554,13 @@ static Lisp_Object QCcrop, QCrotation; static Lisp_Object Qcount, Qextension_data, Qdelay; static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic; -/* Function prototypes. */ +/* Forward function prototypes. */ -static Lisp_Object define_image_type (struct image_type *type, int loaded); -static struct image_type *lookup_image_type (Lisp_Object symbol); -static void image_error (const char *format, Lisp_Object, Lisp_Object); +static struct image_type *lookup_image_type (Lisp_Object); static void x_laplace (struct frame *, struct image *); static void x_emboss (struct frame *, struct image *); -static int x_build_heuristic_mask (struct frame *, struct image *, - Lisp_Object); +static void x_build_heuristic_mask (struct frame *, struct image *, + Lisp_Object); #ifdef HAVE_NTGUI #define CACHE_IMAGE_TYPE(type, status) \ do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0) @@ -588,60 +574,61 @@ static int x_build_heuristic_mask (struct frame *, struct image *, /* Define a new image type from TYPE. This adds a copy of TYPE to image_types and caches the loading status of TYPE. */ -static Lisp_Object -define_image_type (struct image_type *type, int loaded) +static struct image_type * +define_image_type (struct image_type *type) { - Lisp_Object success; + struct image_type *p = NULL; + Lisp_Object target_type = *type->type; + bool type_valid = 1; - if (!loaded) - success = Qnil; - else + block_input (); + + for (p = image_types; p; p = p->next) + if (EQ (*p->type, target_type)) + goto done; + + if (type->init) + { +#ifdef HAVE_NTGUI + /* If we failed to load the library before, don't try again. */ + Lisp_Object tested = Fassq (target_type, Vlibrary_cache); + if (CONSP (tested) && NILP (XCDR (tested))) + type_valid = 0; + else +#endif + { + type_valid = type->init (); + CACHE_IMAGE_TYPE (target_type, type_valid ? Qt : Qnil); + } + } + + if (type_valid) { /* Make a copy of TYPE to avoid a bus error in a dumped Emacs. The initialized data segment is read-only. */ - struct image_type *p = (struct image_type *) xmalloc (sizeof *p); - memcpy (p, type, sizeof *p); + p = xmalloc (sizeof *p); + *p = *type; p->next = image_types; image_types = p; - success = Qt; } - CACHE_IMAGE_TYPE (*type->type, success); - return success; -} - - -/* Look up image type SYMBOL, and return a pointer to its image_type - structure. Value is null if SYMBOL is not a known image type. */ - -static inline struct image_type * -lookup_image_type (Lisp_Object symbol) -{ - struct image_type *type; - - /* We must initialize the image-type if it hasn't been already. */ - if (NILP (Finit_image_library (symbol, Vdynamic_library_alist))) - return 0; /* unimplemented */ - - for (type = image_types; type; type = type->next) - if (EQ (symbol, *type->type)) - break; - - return type; + done: + unblock_input (); + return p; } -/* Value is non-zero if OBJECT is a valid Lisp image specification. A +/* Value is true if OBJECT is a valid Lisp image specification. A valid image specification is a list whose car is the symbol `image', and whose rest is a property list. The property list must contain a value for key `:type'. That value must be the name of a supported image type. The rest of the property list depends on the image type. */ -int +bool valid_image_p (Lisp_Object object) { - int valid_p = 0; + bool valid_p = 0; if (IMAGEP (object)) { @@ -711,8 +698,8 @@ struct image_keyword /* The type of value allowed. */ enum image_value_type type; - /* Non-zero means key must be present. */ - int mandatory_p; + /* True means key must be present. */ + bool mandatory_p; /* Used to recognize duplicate keywords in a property list. */ int count; @@ -722,18 +709,13 @@ struct image_keyword }; -static int parse_image_spec (Lisp_Object, struct image_keyword *, - int, Lisp_Object); -static Lisp_Object image_spec_value (Lisp_Object, Lisp_Object, int *); - - /* Parse image spec SPEC according to KEYWORDS. A valid image spec has the format (image KEYWORD VALUE ...). One of the keyword/ value pairs must be `:type TYPE'. KEYWORDS is a vector of image_keywords structures of size NKEYWORDS describing other - allowed keyword/value pairs. Value is non-zero if SPEC is valid. */ + allowed keyword/value pairs. Value is true if SPEC is valid. */ -static int +static bool parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, int nkeywords, Lisp_Object type) { @@ -771,10 +753,9 @@ parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, /* Record that we recognized the keyword. If a keywords was found more than once, it's an error. */ keywords[i].value = value; - ++keywords[i].count; - if (keywords[i].count > 1) return 0; + ++keywords[i].count; /* Check type of value against allowed type. */ switch (keywords[i].type) @@ -848,7 +829,7 @@ parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, break; default: - abort (); + emacs_abort (); break; } @@ -866,15 +847,15 @@ parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, /* Return the value of KEY in image specification SPEC. Value is nil - if KEY is not present in SPEC. if FOUND is not null, set *FOUND - to 1 if KEY was found in SPEC, set it to 0 otherwise. */ + if KEY is not present in SPEC. Set *FOUND depending on whether KEY + was found in SPEC. */ static Lisp_Object -image_spec_value (Lisp_Object spec, Lisp_Object key, int *found) +image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found) { Lisp_Object tail; - xassert (valid_image_p (spec)); + eassert (valid_image_p (spec)); for (tail = XCDR (spec); CONSP (tail) && CONSP (XCDR (tail)); @@ -974,23 +955,20 @@ or omitted means use the selected frame. */) Image type independent image structures ***********************************************************************/ -static void free_image (struct frame *f, struct image *img); - -#define MAX_IMAGE_SIZE 6.0 +#define MAX_IMAGE_SIZE 10.0 /* Allocate and return a new image structure for image specification SPEC. SPEC has a hash value of HASH. */ static struct image * make_image (Lisp_Object spec, EMACS_UINT hash) { - struct image *img = (struct image *) xmalloc (sizeof *img); + struct image *img = xzalloc (sizeof *img); Lisp_Object file = image_spec_value (spec, QCfile, NULL); - xassert (valid_image_p (spec)); - memset (img, 0, sizeof *img); + eassert (valid_image_p (spec)); img->dependencies = NILP (file) ? Qnil : list1 (file); img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL)); - xassert (img->type != NULL); + eassert (img->type != NULL); img->spec = spec; img->lisp_data = Qnil; img->ascent = DEFAULT_IMAGE_ASCENT; @@ -1026,10 +1004,9 @@ free_image (struct frame *f, struct image *img) } } -/* Return 1 if the given widths and heights are valid for display; - otherwise, return 0. */ +/* Return true if the given widths and heights are valid for display. */ -static int +static bool check_image_size (struct frame *f, int width, int height) { int w, h; @@ -1062,16 +1039,13 @@ check_image_size (struct frame *f, int width, int height) void prepare_image_for_display (struct frame *f, struct image *img) { - EMACS_TIME t; - /* We're about to display IMG, so set its timestamp to `now'. */ - EMACS_GET_TIME (t); - img->timestamp = EMACS_SECS (t); + img->timestamp = current_emacs_time (); /* If IMG doesn't have a pixmap yet, load it now, using the image type dependent loader function. */ if (img->pixmap == NO_PIXMAP && !img->load_failed_p) - img->load_failed_p = img->type->load (f, img) == 0; + img->load_failed_p = ! img->type->load (f, img); } @@ -1204,7 +1178,7 @@ image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg) if (! img->background_valid) /* IMG doesn't have a background yet, try to guess a reasonable value. */ { - int free_ximg = !ximg; + bool free_ximg = !ximg; #ifdef HAVE_NTGUI HGDIOBJ prev; #endif /* HAVE_NTGUI */ @@ -1245,7 +1219,7 @@ image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_D { if (img->mask) { - int free_mask = !mask; + bool free_mask = !mask; #ifdef HAVE_NTGUI HGDIOBJ prev; #endif /* HAVE_NTGUI */ @@ -1283,23 +1257,13 @@ image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_D Helper functions for X image types ***********************************************************************/ -static void x_clear_image_1 (struct frame *, struct image *, int, - int, int); -static void x_clear_image (struct frame *f, struct image *img); -static unsigned long x_alloc_image_color (struct frame *f, - struct image *img, - Lisp_Object color_name, - unsigned long dflt); - - -/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means - free the pixmap if any. MASK_P non-zero means clear the mask - pixmap if any. COLORS_P non-zero means free colors allocated for - the image, if any. */ +/* Clear X resources of image IMG on frame F. PIXMAP_P means free the + pixmap if any. MASK_P means clear the mask pixmap if any. + COLORS_P means free colors allocated for the image, if any. */ static void -x_clear_image_1 (struct frame *f, struct image *img, int pixmap_p, int mask_p, - int colors_p) +x_clear_image_1 (struct frame *f, struct image *img, bool pixmap_p, + bool mask_p, bool colors_p) { if (pixmap_p && img->pixmap) { @@ -1334,9 +1298,9 @@ x_clear_image_1 (struct frame *f, struct image *img, int pixmap_p, int mask_p, static void x_clear_image (struct frame *f, struct image *img) { - BLOCK_INPUT; + block_input (); x_clear_image_1 (f, img, 1, 1, 1); - UNBLOCK_INPUT; + unblock_input (); } @@ -1352,7 +1316,7 @@ x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, XColor color; unsigned long result; - xassert (STRINGP (color_name)); + eassert (STRINGP (color_name)); if (x_defined_color (f, SSDATA (color_name), &color, 1) && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors, @@ -1361,9 +1325,7 @@ x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, /* This isn't called frequently so we get away with simply reallocating the color vector to the needed size, here. */ ptrdiff_t ncolors = img->ncolors + 1; - img->colors = - (unsigned long *) xrealloc (img->colors, - ncolors * sizeof *img->colors); + img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors); img->colors[ncolors - 1] = color.pixel; img->ncolors = ncolors; result = color.pixel; @@ -1381,7 +1343,6 @@ x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, ***********************************************************************/ static void cache_image (struct frame *f, struct image *img); -static void postprocess_image (struct frame *, struct image *); /* Return a new, initialized image cache that is allocated from the heap. Call free_image_cache to free an image cache. */ @@ -1389,16 +1350,14 @@ static void postprocess_image (struct frame *, struct image *); struct image_cache * make_image_cache (void) { - struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c); + struct image_cache *c = xzalloc (sizeof *c); int size; - memset (c, 0, sizeof *c); size = 50; - c->images = (struct image **) xmalloc (size * sizeof *c->images); + c->images = xmalloc (size * sizeof *c->images); c->size = size; size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets; - c->buckets = (struct image **) xmalloc (size); - memset (c->buckets, 0, size); + c->buckets = xzalloc (size); return c; } @@ -1464,7 +1423,7 @@ free_image_cache (struct frame *f) ptrdiff_t i; /* Cache should not be referenced by any frame when freed. */ - xassert (c->refcount == 0); + eassert (c->refcount == 0); for (i = 0; i < c->used; ++i) free_image (f, c->images[i]); @@ -1495,7 +1454,7 @@ clear_image_cache (struct frame *f, Lisp_Object filter) /* Block input so that we won't be interrupted by a SIGIO while being in an inconsistent state. */ - BLOCK_INPUT; + block_input (); if (!NILP (filter)) { @@ -1514,8 +1473,8 @@ clear_image_cache (struct frame *f, Lisp_Object filter) else if (INTEGERP (Vimage_cache_eviction_delay)) { /* Free cache based on timestamp. */ - EMACS_TIME t; - double old, delay; + EMACS_TIME old, t; + double delay; ptrdiff_t nimages = 0; for (i = 0; i < c->used; ++i) @@ -1529,13 +1488,13 @@ clear_image_cache (struct frame *f, Lisp_Object filter) delay = 1600 * delay / nimages / nimages; delay = max (delay, 1); - EMACS_GET_TIME (t); - old = EMACS_SECS (t) - delay; + t = current_emacs_time (); + old = sub_emacs_time (t, EMACS_TIME_FROM_DOUBLE (delay)); for (i = 0; i < c->used; ++i) { struct image *img = c->images[i]; - if (img && img->timestamp < old) + if (img && EMACS_TIME_LT (img->timestamp, old)) { free_image (f, img); ++nfreed; @@ -1561,7 +1520,7 @@ clear_image_cache (struct frame *f, Lisp_Object filter) ++windows_or_buffers_changed; } - UNBLOCK_INPUT; + unblock_input (); } } @@ -1656,7 +1615,7 @@ postprocess_image (struct frame *f, struct image *img) x_build_heuristic_mask (f, img, mask); else { - int found_p; + bool found_p; mask = image_spec_value (spec, QCmask, &found_p); @@ -1708,12 +1667,11 @@ lookup_image (struct frame *f, Lisp_Object spec) { struct image *img; EMACS_UINT hash; - EMACS_TIME now; /* F must be a window-system frame, and SPEC must be a valid image specification. */ - xassert (FRAME_WINDOW_P (f)); - xassert (valid_image_p (spec)); + eassert (FRAME_WINDOW_P (f)); + eassert (valid_image_p (spec)); /* Look up SPEC in the hash table of the image cache. */ hash = sxhash (spec, 0); @@ -1727,10 +1685,10 @@ lookup_image (struct frame *f, Lisp_Object spec) /* If not found, create a new image and cache it. */ if (img == NULL) { - BLOCK_INPUT; + block_input (); img = make_image (spec, hash); cache_image (f, img); - img->load_failed_p = img->type->load (f, img) == 0; + img->load_failed_p = ! img->type->load (f, img); img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); img->frame_background = FRAME_BACKGROUND_PIXEL (f); @@ -1754,6 +1712,7 @@ lookup_image (struct frame *f, Lisp_Object spec) `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', `:background COLOR'. */ Lisp_Object ascent, margin, relief, bg; + int relief_bound; ascent = image_spec_value (spec, QCascent, NULL); if (INTEGERP (ascent)) @@ -1771,7 +1730,8 @@ lookup_image (struct frame *f, Lisp_Object spec) } relief = image_spec_value (spec, QCrelief, NULL); - if (INTEGERP (relief)) + relief_bound = INT_MAX - max (img->hmargin, img->vmargin); + if (RANGED_INTEGERP (- relief_bound, relief, relief_bound)) { img->relief = XINT (relief); img->hmargin += eabs (img->relief); @@ -1796,12 +1756,11 @@ lookup_image (struct frame *f, Lisp_Object spec) postprocess_image (f, img); } - UNBLOCK_INPUT; + unblock_input (); } /* We're using IMG, so set its timestamp to `now'. */ - EMACS_GET_TIME (now); - img->timestamp = EMACS_SECS (now); + img->timestamp = current_emacs_time (); /* Value is the image id. */ return img->id; @@ -1888,16 +1847,11 @@ mark_image_cache (struct image_cache *c) #endif /* HAVE_NTGUI */ -static int x_create_x_image_and_pixmap (struct frame *, int, int, int, - XImagePtr *, Pixmap *); -static void x_destroy_x_image (XImagePtr); -static void x_put_x_image (struct frame *, XImagePtr, Pixmap, int, int); - -/* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the +/* Return true if XIMG's size WIDTH x HEIGHT doesn't break the windowing system. WIDTH and HEIGHT must both be positive. If XIMG is null, assume it is a bitmap. */ -static int +static bool x_check_image_size (XImagePtr ximg, int width, int height) { #ifdef HAVE_X_WINDOWS @@ -1936,12 +1890,12 @@ x_check_image_size (XImagePtr ximg, int width, int height) frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated via xmalloc. Print error messages via image_error if an error - occurs. Value is non-zero if successful. + occurs. Value is true if successful. On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH should indicate the bit depth of the image. */ -static int +static bool x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, XImagePtr *ximg, Pixmap *pixmap) { @@ -1950,7 +1904,7 @@ x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, Window window = FRAME_X_WINDOW (f); Screen *screen = FRAME_X_SCREEN (f); - xassert (interrupt_input_blocked); + eassert (input_blocked_p ()); if (depth <= 0) depth = DefaultDepthOfScreen (screen); @@ -1973,7 +1927,7 @@ x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, } /* Allocate image raster. */ - (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height); + (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height); /* Allocate a pixmap of the same size. */ *pixmap = XCreatePixmap (display, window, width, height, depth); @@ -2088,7 +2042,7 @@ x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, static void x_destroy_x_image (XImagePtr ximg) { - xassert (interrupt_input_blocked); + eassert (input_blocked_p ()); if (ximg) { #ifdef HAVE_X_WINDOWS @@ -2117,7 +2071,7 @@ x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int he #ifdef HAVE_X_WINDOWS GC gc; - xassert (interrupt_input_blocked); + eassert (input_blocked_p ()); gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); XFreeGC (FRAME_X_DISPLAY (f), gc); @@ -2132,7 +2086,7 @@ x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int he #endif /* HAVE_NTGUI */ #ifdef HAVE_NS - xassert (ximg == pixmap); + eassert (ximg == pixmap); ns_retain_object (ximg); #endif } @@ -2187,7 +2141,7 @@ slurp_file (char *file, ptrdiff_t *size) if (stat (file, &st) == 0 && (fp = fopen (file, "rb")) != NULL && 0 <= st.st_size && st.st_size <= min (PTRDIFF_MAX, SIZE_MAX) - && (buf = (unsigned char *) xmalloc (st.st_size), + && (buf = xmalloc (st.st_size), fread (buf, 1, st.st_size, fp) == st.st_size)) { *size = st.st_size; @@ -2213,15 +2167,9 @@ slurp_file (char *file, ptrdiff_t *size) XBM images ***********************************************************************/ -static int xbm_scan (unsigned char **, unsigned char *, char *, int *); -static int xbm_load (struct frame *f, struct image *img); -static int xbm_load_image (struct frame *f, struct image *img, - unsigned char *, unsigned char *); -static int xbm_image_p (Lisp_Object object); -static int xbm_read_bitmap_data (struct frame *f, - unsigned char *, unsigned char *, - int *, int *, char **, int); -static int xbm_file_p (Lisp_Object); +static bool xbm_load (struct frame *f, struct image *img); +static bool xbm_image_p (Lisp_Object object); +static bool xbm_file_p (Lisp_Object); /* Indices of image specification fields in xbm_format, below. */ @@ -2272,6 +2220,7 @@ static struct image_type xbm_type = xbm_image_p, xbm_load, x_clear_image, + NULL, NULL }; @@ -2284,10 +2233,10 @@ enum xbm_token }; -/* Return non-zero if OBJECT is a valid XBM-type image specification. +/* Return true if OBJECT is a valid XBM-type image specification. A valid specification is a list starting with the symbol `image' The rest of the list is a property list which must contain an - entry `:type xbm.. + entry `:type xbm'. If the specification specifies a file to load, it must contain an entry `:file FILENAME' where FILENAME is a string. @@ -2313,7 +2262,7 @@ enum xbm_token foreground and background of the frame on which the image is displayed is used. */ -static int +static bool xbm_image_p (Lisp_Object object) { struct image_keyword kw[XBM_LAST]; @@ -2322,7 +2271,7 @@ xbm_image_p (Lisp_Object object) if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) return 0; - xassert (EQ (kw[XBM_TYPE].value, Qxbm)); + eassert (EQ (kw[XBM_TYPE].value, Qxbm)); if (kw[XBM_FILE].count) { @@ -2364,7 +2313,7 @@ xbm_image_p (Lisp_Object object) for one line of the image. */ for (i = 0; i < height; ++i) { - Lisp_Object elt = XVECTOR (data)->contents[i]; + Lisp_Object elt = AREF (data, i); if (STRINGP (elt)) { @@ -2414,12 +2363,12 @@ xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) loop: /* Skip white space. */ - while (*s < end && (c = *(*s)++, isspace (c))) + while (*s < end && (c = *(*s)++, c_isspace (c))) ; if (*s >= end) c = 0; - else if (isdigit (c)) + else if (c_isdigit (c)) { int value = 0, digit; @@ -2431,7 +2380,7 @@ xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) while (*s < end) { c = *(*s)++; - if (isdigit (c)) + if (c_isdigit (c)) digit = c - '0'; else if (c >= 'a' && c <= 'f') digit = c - 'a' + 10; @@ -2442,11 +2391,11 @@ xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) value = 16 * value + digit; } } - else if (isdigit (c)) + else if (c_isdigit (c)) { value = c - '0'; while (*s < end - && (c = *(*s)++, isdigit (c))) + && (c = *(*s)++, c_isdigit (c))) value = 8 * value + c - '0'; } } @@ -2454,7 +2403,7 @@ xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) { value = c - '0'; while (*s < end - && (c = *(*s)++, isdigit (c))) + && (c = *(*s)++, c_isdigit (c))) value = 10 * value + c - '0'; } @@ -2463,11 +2412,11 @@ xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) *ival = value; c = XBM_TK_NUMBER; } - else if (isalpha (c) || c == '_') + else if (c_isalpha (c) || c == '_') { *sval++ = c; while (*s < end - && (c = *(*s)++, (isalnum (c) || c == '_'))) + && (c = *(*s)++, (c_isalnum (c) || c == '_'))) *sval++ = c; *sval = 0; if (*s < end) @@ -2507,7 +2456,7 @@ w32_create_pixmap_from_bitmap_data (int width, int height, char *data) w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ - bits = (unsigned char *) alloca (height * w2); + bits = alloca (height * w2); memset (bits, 0, height * w2); for (i = 0; i < height; i++) { @@ -2571,7 +2520,7 @@ convert_mono_to_color_image (struct frame *f, struct image *img, static void Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data, RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg, - int non_default_colors) + bool non_default_colors) { #ifdef HAVE_NTGUI img->pixmap @@ -2603,20 +2552,20 @@ Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data, X versions. CONTENTS is a pointer to a buffer to parse; END is the buffer's end. Set *WIDTH and *HEIGHT to the width and height of the image. Return in *DATA the bitmap data allocated with xmalloc. - Value is non-zero if successful. DATA null means just test if - CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR - is non-zero, inhibit the call to image_error when the image size is - invalid (the bitmap remains unread). */ + Value is true if successful. DATA null means just test if + CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR, + inhibit the call to image_error when the image size is invalid (the + bitmap remains unread). */ -static int +static bool xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end, int *width, int *height, char **data, - int inhibit_image_error) + bool inhibit_image_error) { unsigned char *s = contents; char buffer[BUFSIZ]; - int padding_p = 0; - int v10 = 0; + bool padding_p = 0; + bool v10 = 0; int bytes_per_line, i, nbytes; char *p; int value; @@ -2709,7 +2658,7 @@ xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *e } bytes_per_line = (*width + 7) / 8 + padding_p; nbytes = bytes_per_line * *height; - p = *data = (char *) xmalloc (nbytes); + p = *data = xmalloc (nbytes); if (v10) { @@ -2763,16 +2712,16 @@ xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *e /* Load XBM image IMG which will be displayed on frame F from buffer - CONTENTS. END is the end of the buffer. Value is non-zero if + CONTENTS. END is the end of the buffer. Value is true if successful. */ -static int +static bool xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, unsigned char *end) { - int rc; + bool rc; char *data; - int success_p = 0; + bool success_p = 0; rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, &data, 0); @@ -2780,10 +2729,10 @@ xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, { unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); unsigned long background = FRAME_BACKGROUND_PIXEL (f); - int non_default_colors = 0; + bool non_default_colors = 0; Lisp_Object value; - xassert (img->width > 0 && img->height > 0); + eassert (img->width > 0 && img->height > 0); /* Get foreground and background colors, maybe allocate colors. */ value = image_spec_value (img->spec, QCforeground, NULL); @@ -2821,9 +2770,9 @@ xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, } -/* Value is non-zero if DATA looks like an in-memory XBM file. */ +/* Value is true if DATA looks like an in-memory XBM file. */ -static int +static bool xbm_file_p (Lisp_Object data) { int w, h; @@ -2835,15 +2784,15 @@ xbm_file_p (Lisp_Object data) /* Fill image IMG which is used on frame F with pixmap data. Value is - non-zero if successful. */ + true if successful. */ -static int +static bool xbm_load (struct frame *f, struct image *img) { - int success_p = 0; + bool success_p = 0; Lisp_Object file_name; - xassert (xbm_image_p (img->spec)); + eassert (xbm_image_p (img->spec)); /* If IMG->spec specifies a file name, create a non-file spec from it. */ file_name = image_spec_value (img->spec, QCfile, NULL); @@ -2876,10 +2825,10 @@ xbm_load (struct frame *f, struct image *img) Lisp_Object data; unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); unsigned long background = FRAME_BACKGROUND_PIXEL (f); - int non_default_colors = 0; + bool non_default_colors = 0; char *bits; - int parsed_p; - int in_memory_file_p = 0; + bool parsed_p; + bool in_memory_file_p = 0; /* See if data looks like an in-memory XBM file. */ data = image_spec_value (img->spec, QCdata, NULL); @@ -2888,15 +2837,14 @@ xbm_load (struct frame *f, struct image *img) /* Parse the image specification. */ memcpy (fmt, xbm_format, sizeof fmt); parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); - (void) parsed_p; - xassert (parsed_p); + eassert (parsed_p); /* Get specified width, and height. */ if (!in_memory_file_p) { img->width = XFASTINT (fmt[XBM_WIDTH].value); img->height = XFASTINT (fmt[XBM_HEIGHT].value); - xassert (img->width > 0 && img->height > 0); + eassert (img->width > 0 && img->height > 0); if (!check_image_size (f, img->width, img->height)) { image_error ("Invalid image size (see `max-image-size')", @@ -2934,10 +2882,10 @@ xbm_load (struct frame *f, struct image *img) char *p; int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; - p = bits = (char *) alloca (nbytes * img->height); + p = bits = alloca (nbytes * img->height); for (i = 0; i < img->height; ++i, p += nbytes) { - Lisp_Object line = XVECTOR (data)->contents[i]; + Lisp_Object line = AREF (data, i); if (STRINGP (line)) memcpy (p, SDATA (line), nbytes); else @@ -2957,7 +2905,7 @@ xbm_load (struct frame *f, struct image *img) invertedBits = bits; nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * img->height; - bits = (char *) alloca (nbytes); + bits = alloca (nbytes); for (i = 0; i < nbytes; i++) bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); } @@ -2993,9 +2941,8 @@ xbm_load (struct frame *f, struct image *img) #if defined (HAVE_XPM) || defined (HAVE_NS) -static int xpm_image_p (Lisp_Object object); -static int xpm_load (struct frame *f, struct image *img); -static int xpm_valid_color_symbols_p (Lisp_Object); +static bool xpm_image_p (Lisp_Object object); +static bool xpm_load (struct frame *f, struct image *img); #endif /* HAVE_XPM || HAVE_NS */ @@ -3061,6 +3008,12 @@ static const struct image_keyword xpm_format[XPM_LAST] = {":background", IMAGE_STRING_OR_NIL_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_xpm_functions (void); +#else +#define init_xpm_functions NULL +#endif + /* Structure describing the image type XPM. */ static struct image_type xpm_type = @@ -3069,6 +3022,7 @@ static struct image_type xpm_type = xpm_image_p, xpm_load, x_clear_image, + init_xpm_functions, NULL }; @@ -3086,10 +3040,6 @@ static struct image_type xpm_type = #ifdef ALLOC_XPM_COLORS -static void xpm_init_color_cache (struct frame *, XpmAttributes *); -static void xpm_free_color_cache (void); -static int xpm_lookup_color (struct frame *, char *, XColor *); -static int xpm_color_bucket (char *); static struct xpm_cached_color *xpm_cache_color (struct frame *, char *, XColor *, int); @@ -3122,8 +3072,7 @@ static void xpm_init_color_cache (struct frame *f, XpmAttributes *attrs) { size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; - xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes); - memset (xpm_color_cache, 0, nbytes); + xpm_color_cache = xzalloc (nbytes); init_color_table (); if (attrs->valuemask & XpmColorSymbols) @@ -3187,7 +3136,7 @@ xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket) bucket = xpm_color_bucket (color_name); nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1; - p = (struct xpm_cached_color *) xmalloc (nbytes); + p = xmalloc (nbytes); strcpy (p->name, color_name); p->color = *color; p->next = xpm_color_cache[bucket]; @@ -3197,10 +3146,10 @@ xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket) /* Look up color COLOR_NAME for frame F in the color cache. If found, return the cached definition in *COLOR. Otherwise, make a new - entry in the cache and allocate the color. Value is zero if color + entry in the cache and allocate the color. Value is false if color allocation failed. */ -static int +static bool xpm_lookup_color (struct frame *f, char *color_name, XColor *color) { struct xpm_cached_color *p; @@ -3269,12 +3218,12 @@ DEF_IMGLIB_FN (int, XpmReadFileToImage, (Display *, char *, xpm_XImage **, xpm_XImage **, XpmAttributes *)); DEF_IMGLIB_FN (void, XImageFree, (xpm_XImage *)); -static int -init_xpm_functions (Lisp_Object libraries) +static bool +init_xpm_functions (void) { HMODULE library; - if (!(library = w32_delayed_load (libraries, Qxpm))) + if (!(library = w32_delayed_load (Qxpm))) return 0; LOAD_IMGLIB_FN (library, XpmFreeAttributes); @@ -3287,11 +3236,11 @@ init_xpm_functions (Lisp_Object libraries) #endif /* HAVE_NTGUI */ -/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list +/* Value is true if COLOR_SYMBOLS is a valid color symbols list for XPM images. Such a list must consist of conses whose car and cdr are strings. */ -static int +static bool xpm_valid_color_symbols_p (Lisp_Object color_symbols) { while (CONSP (color_symbols)) @@ -3308,9 +3257,9 @@ xpm_valid_color_symbols_p (Lisp_Object color_symbols) } -/* Value is non-zero if OBJECT is a valid XPM image specification. */ +/* Value is true if OBJECT is a valid XPM image specification. */ -static int +static bool xpm_image_p (Lisp_Object object) { struct image_keyword fmt[XPM_LAST]; @@ -3367,11 +3316,11 @@ x_create_bitmap_from_xpm_data (struct frame *f, const char **bits) #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ /* Load image IMG which will be displayed on frame F. Value is - non-zero if successful. */ + true if successful. */ #ifdef HAVE_XPM -static int +static bool xpm_load (struct frame *f, struct image *img) { int rc; @@ -3430,7 +3379,7 @@ xpm_load (struct frame *f, struct image *img) /* Allocate an XpmColorSymbol array. */ size = attrs.numsymbols * sizeof *xpm_syms; - xpm_syms = (XpmColorSymbol *) alloca (size); + xpm_syms = alloca (size); memset (xpm_syms, 0, size); attrs.colorsymbols = xpm_syms; @@ -3453,14 +3402,14 @@ xpm_load (struct frame *f, struct image *img) color = XCDR (XCAR (tail)); if (STRINGP (name)) { - xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1); + xpm_syms[i].name = alloca (SCHARS (name) + 1); strcpy (xpm_syms[i].name, SSDATA (name)); } else xpm_syms[i].name = empty_string; if (STRINGP (color)) { - xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1); + xpm_syms[i].value = alloca (SCHARS (color) + 1); strcpy (xpm_syms[i].value, SSDATA (color)); } else @@ -3583,7 +3532,7 @@ xpm_load (struct frame *f, struct image *img) img->width = attrs.width; img->height = attrs.height; - xassert (img->width > 0 && img->height > 0); + eassert (img->width > 0 && img->height > 0); /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ #ifdef HAVE_NTGUI @@ -3671,16 +3620,17 @@ xpm_scan (const unsigned char **s, while (*s < end) { /* Skip white-space. */ - while (*s < end && (c = *(*s)++, isspace (c))) + while (*s < end && (c = *(*s)++, c_isspace (c))) ; /* gnus-pointer.xpm uses '-' in its identifier. sb-dir-plus.xpm uses '+' in its identifier. */ - if (isalpha (c) || c == '_' || c == '-' || c == '+') + if (c_isalpha (c) || c == '_' || c == '-' || c == '+') { *beg = *s - 1; while (*s < end - && (c = **s, isalnum (c) || c == '_' || c == '-' || c == '+')) + && (c = **s, c_isalnum (c) + || c == '_' || c == '-' || c == '+')) ++*s; *len = *s - *beg; return XPM_TK_IDENT; @@ -3747,7 +3697,7 @@ xpm_put_color_table_v (Lisp_Object color_table, int chars_len, Lisp_Object color) { - XVECTOR (color_table)->contents[*chars_start] = color; + ASET (color_table, *chars_start, color); } static Lisp_Object @@ -3755,7 +3705,7 @@ xpm_get_color_table_v (Lisp_Object color_table, const unsigned char *chars_start, int chars_len) { - return XVECTOR (color_table)->contents[*chars_start]; + return AREF (color_table, *chars_start); } static Lisp_Object @@ -3824,7 +3774,7 @@ xpm_str_to_color_key (const char *s) return -1; } -static int +static bool xpm_load_image (struct frame *f, struct image *img, const unsigned char *contents, @@ -3839,7 +3789,8 @@ xpm_load_image (struct frame *f, void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); Lisp_Object frame, color_symbols, color_table; - int best_key, have_mask = 0; + int best_key; + bool have_mask = 0; XImagePtr ximg = NULL, mask_img = NULL; #define match() \ @@ -3974,7 +3925,7 @@ xpm_load_image (struct frame *f, { if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0) color_val = Qt; - else if (x_defined_color (f, SDATA (XCDR (specified_color)), + else if (x_defined_color (f, SSDATA (XCDR (specified_color)), &cdef, 0)) color_val = make_number (cdef.pixel); } @@ -4049,7 +4000,6 @@ xpm_load_image (struct frame *f, failure: image_error ("Invalid XPM file (%s)", img->spec, Qnil); - error: x_destroy_x_image (ximg); x_destroy_x_image (mask_img); x_clear_image (f, img); @@ -4060,11 +4010,11 @@ xpm_load_image (struct frame *f, #undef expect_ident } -static int +static bool xpm_load (struct frame *f, struct image *img) { - int success_p = 0; + bool success_p = 0; Lisp_Object file_name; /* If IMG->spec specifies a file name, create a non-file spec from it. */ @@ -4082,7 +4032,7 @@ xpm_load (struct frame *f, return 0; } - contents = slurp_file (SDATA (file), &size); + contents = slurp_file (SSDATA (file), &size); if (contents == NULL) { image_error ("Error loading XPM image `%s'", img->spec, Qnil); @@ -4158,8 +4108,7 @@ static void init_color_table (void) { int size = CT_SIZE * sizeof (*ct_table); - ct_table = (struct ct_color **) xmalloc (size); - memset (ct_table, 0, size); + ct_table = xzalloc (size); ct_colors_allocated = 0; } @@ -4236,7 +4185,7 @@ lookup_rgb_color (struct frame *f, int r, int g, int b) #ifdef HAVE_X_WINDOWS XColor color; Colormap cmap; - int rc; + bool rc; #else COLORREF color; #endif @@ -4254,7 +4203,7 @@ lookup_rgb_color (struct frame *f, int r, int g, int b) if (rc) { ++ct_colors_allocated; - p = (struct ct_color *) xmalloc (sizeof *p); + p = xmalloc (sizeof *p); p->r = r; p->g = g; p->b = b; @@ -4272,7 +4221,7 @@ lookup_rgb_color (struct frame *f, int r, int g, int b) color = RGB_TO_ULONG (r, g, b); #endif /* HAVE_NTGUI */ ++ct_colors_allocated; - p = (struct ct_color *) xmalloc (sizeof *p); + p = xmalloc (sizeof *p); p->r = r; p->g = g; p->b = b; @@ -4304,7 +4253,7 @@ lookup_pixel_color (struct frame *f, unsigned long pixel) { XColor color; Colormap cmap; - int rc; + bool rc; if (ct_colors_allocated_max <= ct_colors_allocated) return FRAME_FOREGROUND_PIXEL (f); @@ -4315,19 +4264,19 @@ lookup_pixel_color (struct frame *f, unsigned long pixel) x_query_color (f, &color); rc = x_alloc_nearest_color (f, cmap, &color); #else - BLOCK_INPUT; + block_input (); cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); color.pixel = pixel; XQueryColor (NULL, cmap, &color); rc = x_alloc_nearest_color (f, cmap, &color); - UNBLOCK_INPUT; + unblock_input (); #endif /* HAVE_X_WINDOWS */ if (rc) { ++ct_colors_allocated; - p = (struct ct_color *) xmalloc (sizeof *p); + p = xmalloc (sizeof *p); p->r = color.red; p->g = color.green; p->b = color.blue; @@ -4359,8 +4308,7 @@ colors_in_color_table (int *n) } else { - colors = (unsigned long *) xmalloc (ct_colors_allocated - * sizeof *colors); + colors = xmalloc (ct_colors_allocated * sizeof *colors); *n = ct_colors_allocated; for (i = j = 0; i < CT_SIZE; ++i) @@ -4399,14 +4347,6 @@ init_color_table (void) Algorithms ***********************************************************************/ -static XColor *x_to_xcolors (struct frame *, struct image *, int); -static void x_from_xcolors (struct frame *, struct image *, XColor *); -static void x_detect_edges (struct frame *, struct image *, int[9], int); - -#ifdef HAVE_NTGUI -static void XPutPixel (XImagePtr , int, int, COLORREF); -#endif /* HAVE_NTGUI */ - /* Edge detection matrices for different edge-detection strategies. */ @@ -4432,12 +4372,12 @@ static int laplace_matrix[9] = { /* On frame F, return an array of XColor structures describing image IMG->pixmap. Each XColor structure has its pixel color set. RGB_P - non-zero means also fill the red/green/blue members of the XColor + means also fill the red/green/blue members of the XColor structures. Value is a pointer to the array of XColors structures, allocated with xmalloc; it must be freed by the caller. */ static XColor * -x_to_xcolors (struct frame *f, struct image *img, int rgb_p) +x_to_xcolors (struct frame *f, struct image *img, bool rgb_p) { int x, y; XColor *colors, *p; @@ -4449,7 +4389,7 @@ x_to_xcolors (struct frame *f, struct image *img, int rgb_p) if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height) memory_full (SIZE_MAX); - colors = (XColor *) xmalloc (sizeof *colors * img->width * img->height); + colors = xmalloc (sizeof *colors * img->width * img->height); #ifndef HAVE_NTGUI /* Get the X image IMG->pixmap. */ @@ -4468,9 +4408,8 @@ x_to_xcolors (struct frame *f, struct image *img, int rgb_p) p = colors; for (y = 0; y < img->height; ++y) { - XColor *row = p; - #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) + XColor *row = p; for (x = 0; x < img->width; ++x, ++p) p->pixel = GET_PIXEL (ximg, x, y); if (rgb_p) @@ -4603,7 +4542,7 @@ x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjus if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height) memory_full (SIZE_MAX); - new = (XColor *) xmalloc (sizeof *new * img->width * img->height); + new = xmalloc (sizeof *new * img->width * img->height); for (y = 0; y < img->height; ++y) { @@ -4753,14 +4692,12 @@ x_disable_image (struct frame *f, struct image *img) if (n_planes < 2 || cross_disabled_images) { #ifndef HAVE_NTGUI - Display *dpy = FRAME_X_DISPLAY (f); - GC gc; - #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ #define MaskForeground(f) WHITE_PIX_DEFAULT (f) - gc = XCreateGC (dpy, img->pixmap, 0, NULL); + Display *dpy = FRAME_X_DISPLAY (f); + GC gc = XCreateGC (dpy, img->pixmap, 0, NULL); XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); XDrawLine (dpy, img->pixmap, gc, 0, 0, img->width - 1, img->height - 1); @@ -4816,9 +4753,9 @@ x_disable_image (struct frame *f, struct image *img) determine the background color of IMG. If it is a list '(R G B)', with R, G, and B being integers >= 0, take that as the color of the background. Otherwise, determine the background color of IMG - heuristically. Value is non-zero if successful. */ + heuristically. */ -static int +static void x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) { XImagePtr_or_DC ximg; @@ -4830,7 +4767,8 @@ x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) char *mask_img; int row_width; #endif /* HAVE_NTGUI */ - int x, y, rc, use_img_background; + int x, y; + bool rc, use_img_background; unsigned long bg = 0; if (img->mask) @@ -4846,7 +4784,7 @@ x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1, &mask_img, &img->mask); if (!rc) - return 0; + return; #endif /* !HAVE_NS */ /* Get the X image of IMG->pixmap. */ @@ -4856,8 +4794,7 @@ x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) #else /* Create the bit array serving as mask. */ row_width = (img->width + 7) / 8; - mask_img = xmalloc (row_width * img->height); - memset (mask_img, 0, row_width * img->height); + mask_img = xzalloc (row_width * img->height); /* Create a memory device context for IMG->pixmap. */ frame_dc = get_frame_dc (f); @@ -4937,8 +4874,6 @@ x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) #endif /* HAVE_NTGUI */ Destroy_Image (ximg, prev); - - return 1; } @@ -4946,9 +4881,8 @@ x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) PBM (mono, gray, color) ***********************************************************************/ -static int pbm_image_p (Lisp_Object object); -static int pbm_load (struct frame *f, struct image *img); -static int pbm_scan_number (unsigned char **, unsigned char *); +static bool pbm_image_p (Lisp_Object object); +static bool pbm_load (struct frame *f, struct image *img); /* The symbol `pbm' identifying images of this type. */ @@ -4998,13 +4932,14 @@ static struct image_type pbm_type = pbm_image_p, pbm_load, x_clear_image, + NULL, NULL }; -/* Return non-zero if OBJECT is a valid PBM image specification. */ +/* Return true if OBJECT is a valid PBM image specification. */ -static int +static bool pbm_image_p (Lisp_Object object) { struct image_keyword fmt[PBM_LAST]; @@ -5031,7 +4966,7 @@ pbm_scan_number (unsigned char **s, unsigned char *end) while (*s < end) { /* Skip white-space. */ - while (*s < end && (c = *(*s)++, isspace (c))) + while (*s < end && (c = *(*s)++, c_isspace (c))) ; if (c == '#') @@ -5040,11 +4975,11 @@ pbm_scan_number (unsigned char **s, unsigned char *end) while (*s < end && (c = *(*s)++, c != '\n')) ; } - else if (isdigit (c)) + else if (c_isdigit (c)) { /* Read decimal number. */ val = c - '0'; - while (*s < end && (c = *(*s)++, isdigit (c))) + while (*s < end && (c = *(*s)++, c_isdigit (c))) val = 10 * val + c - '0'; break; } @@ -5073,7 +5008,7 @@ pbm_read_file (Lisp_Object file, int *size) if (stat (SDATA (file), &st) == 0 && (fp = fopen (SDATA (file), "rb")) != NULL && 0 <= st.st_size && st.st_size <= min (PTRDIFF_MAX, SIZE_MAX) - && (buf = (char *) xmalloc (st.st_size), + && (buf = xmalloc (st.st_size), fread (buf, 1, st.st_size, fp) == st.st_size)) { *size = st.st_size; @@ -5097,10 +5032,11 @@ pbm_read_file (Lisp_Object file, int *size) /* Load PBM image IMG for use on frame F. */ -static int +static bool pbm_load (struct frame *f, struct image *img) { - int raw_p, x, y; + bool raw_p; + int x, y; int width, height, max_color_idx = 0; XImagePtr ximg; Lisp_Object file, specified_file; @@ -5363,8 +5299,8 @@ pbm_load (struct frame *f, struct image *img) /* Function prototypes. */ -static int png_image_p (Lisp_Object object); -static int png_load (struct frame *f, struct image *img); +static bool png_image_p (Lisp_Object object); +static bool png_load (struct frame *f, struct image *img); /* The symbol `png' identifying images of this type. */ @@ -5404,6 +5340,12 @@ static const struct image_keyword png_format[PNG_LAST] = {":background", IMAGE_STRING_OR_NIL_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_png_functions (void); +#else +#define init_png_functions NULL +#endif + /* Structure describing the image type `png'. */ static struct image_type png_type = @@ -5412,12 +5354,13 @@ static struct image_type png_type = png_image_p, png_load, x_clear_image, + init_png_functions, NULL }; -/* Return non-zero if OBJECT is a valid PNG image specification. */ +/* Return true if OBJECT is a valid PNG image specification. */ -static int +static bool png_image_p (Lisp_Object object) { struct image_keyword fmt[PNG_LAST]; @@ -5469,12 +5412,12 @@ DEF_IMGLIB_FN (void, png_longjmp, (png_structp, int)); DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn, (png_structp, png_longjmp_ptr, size_t)); #endif /* libpng version >= 1.5 */ -static int -init_png_functions (Lisp_Object libraries) +static bool +init_png_functions (void) { HMODULE library; - if (!(library = w32_delayed_load (libraries, Qpng))) + if (!(library = w32_delayed_load (Qpng))) return 0; LOAD_IMGLIB_FN (library, png_get_io_ptr); @@ -5537,25 +5480,33 @@ init_png_functions (Lisp_Object libraries) #endif /* HAVE_NTGUI */ +/* Possibly inefficient/inexact substitutes for _setjmp and _longjmp. + Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp + substitute may munge the signal mask, but that should be OK here. + MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in + the system header setjmp.h; don't mess up that. */ +#ifndef HAVE__SETJMP +# define _setjmp(j) setjmp (j) +# define _longjmp longjmp +#endif #if (PNG_LIBPNG_VER < 10500) -#define PNG_LONGJMP(ptr) (longjmp ((ptr)->jmpbuf, 1)) +#define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1)) #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf) #else /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */ #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1)) #define PNG_JMPBUF(ptr) \ - (*fn_png_set_longjmp_fn ((ptr), longjmp, sizeof (jmp_buf))) + (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf))) #endif /* Error and warning handlers installed when the PNG library is initialized. */ -static void my_png_error (png_struct *, const char *) NO_RETURN; -static void +static _Noreturn void my_png_error (png_struct *png_ptr, const char *msg) { - xassert (png_ptr != NULL); + eassert (png_ptr != NULL); /* Avoid compiler warning about deprecated direct access to png_ptr's fields in libpng versions 1.4.x. */ image_error ("PNG error: %s", build_string (msg), Qnil); @@ -5566,7 +5517,7 @@ my_png_error (png_struct *png_ptr, const char *msg) static void my_png_warning (png_struct *png_ptr, const char *msg) { - xassert (png_ptr != NULL); + eassert (png_ptr != NULL); image_error ("PNG warning: %s", build_string (msg), Qnil); } @@ -5612,28 +5563,39 @@ png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length) } -/* Load PNG image IMG for use on frame F. Value is non-zero if +/* Load PNG image IMG for use on frame F. Value is true if successful. */ -static int -png_load (struct frame *f, struct image *img) +struct png_load_context +{ + /* These are members so that longjmp doesn't munge local variables. */ + png_struct *png_ptr; + png_info *info_ptr; + png_info *end_info; + FILE *fp; + png_byte *pixels; + png_byte **rows; +}; + +static bool +png_load_body (struct frame *f, struct image *img, struct png_load_context *c) { Lisp_Object file, specified_file; Lisp_Object specified_data; int x, y; ptrdiff_t i; XImagePtr ximg, mask_img = NULL; - png_struct *png_ptr = NULL; + png_struct *png_ptr; png_info *info_ptr = NULL, *end_info = NULL; - FILE *volatile fp = NULL; + FILE *fp = NULL; png_byte sig[8]; - png_byte * volatile pixels = NULL; - png_byte ** volatile rows = NULL; + png_byte *pixels = NULL; + png_byte **rows = NULL; png_uint_32 width, height; int bit_depth, color_type, interlace_type; png_byte channels; png_uint_32 row_bytes; - int transparent_p; + bool transparent_p; struct png_memory_storage tbr; /* Data to be read */ /* Find out what file to load. */ @@ -5695,41 +5657,47 @@ png_load (struct frame *f, struct image *img) png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, my_png_error, my_png_warning); - if (!png_ptr) + if (png_ptr) { - if (fp) fclose (fp); - return 0; + info_ptr = fn_png_create_info_struct (png_ptr); + end_info = fn_png_create_info_struct (png_ptr); } - info_ptr = fn_png_create_info_struct (png_ptr); - if (!info_ptr) + c->png_ptr = png_ptr; + c->info_ptr = info_ptr; + c->end_info = end_info; + c->fp = fp; + c->pixels = pixels; + c->rows = rows; + + if (! (info_ptr && end_info)) { - fn_png_destroy_read_struct (&png_ptr, NULL, NULL); - if (fp) fclose (fp); - return 0; + fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); + png_ptr = 0; } - - end_info = fn_png_create_info_struct (png_ptr); - if (!end_info) + if (! png_ptr) { - fn_png_destroy_read_struct (&png_ptr, &info_ptr, NULL); if (fp) fclose (fp); return 0; } /* Set error jump-back. We come back here when the PNG library detects an error. */ - if (setjmp (PNG_JMPBUF (png_ptr))) + if (_setjmp (PNG_JMPBUF (png_ptr))) { error: - if (png_ptr) - fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); - xfree (pixels); - xfree (rows); - if (fp) fclose (fp); + if (c->png_ptr) + fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); + xfree (c->pixels); + xfree (c->rows); + if (c->fp) + fclose (c->fp); return 0; } + /* Silence a bogus diagnostic; see GCC bug 54561. */ + IF_LINT (fp = c->fp); + /* Read image info. */ if (!NILP (specified_data)) fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory); @@ -5836,7 +5804,7 @@ png_load (struct frame *f, struct image *img) images with alpha channel, i.e. RGBA. If conversions above were sufficient we should only have 3 or 4 channels here. */ channels = fn_png_get_channels (png_ptr, info_ptr); - xassert (channels == 3 || channels == 4); + eassert (channels == 3 || channels == 4); /* Number of bytes needed for one row of the image. */ row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); @@ -5845,8 +5813,8 @@ png_load (struct frame *f, struct image *img) if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows < height || min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height < row_bytes) memory_full (SIZE_MAX); - pixels = (png_byte *) xmalloc (sizeof *pixels * row_bytes * height); - rows = (png_byte **) xmalloc (height * sizeof *rows); + c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height); + c->rows = rows = xmalloc (height * sizeof *rows); for (i = 0; i < height; ++i) rows[i] = pixels + i * row_bytes; @@ -5856,7 +5824,7 @@ png_load (struct frame *f, struct image *img) if (fp) { fclose (fp); - fp = NULL; + c->fp = NULL; } /* Create an image and pixmap serving as mask if the PNG image @@ -5931,7 +5899,7 @@ png_load (struct frame *f, struct image *img) #endif /* COLOR_TABLE_SUPPORT */ /* Clean up. */ - fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); + fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); xfree (rows); xfree (pixels); @@ -5960,10 +5928,17 @@ png_load (struct frame *f, struct image *img) return 1; } +static bool +png_load (struct frame *f, struct image *img) +{ + struct png_load_context c; + return png_load_body (f, img, &c); +} + #else /* HAVE_PNG */ #ifdef HAVE_NS -static int +static bool png_load (struct frame *f, struct image *img) { return ns_load_image (f, img, @@ -5983,8 +5958,8 @@ png_load (struct frame *f, struct image *img) #if defined (HAVE_JPEG) || defined (HAVE_NS) -static int jpeg_image_p (Lisp_Object object); -static int jpeg_load (struct frame *f, struct image *img); +static bool jpeg_image_p (Lisp_Object object); +static bool jpeg_load (struct frame *f, struct image *img); /* The symbol `jpeg' identifying images of this type. */ @@ -6024,6 +5999,12 @@ static const struct image_keyword jpeg_format[JPEG_LAST] = {":background", IMAGE_STRING_OR_NIL_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_jpeg_functions (void); +#else +#define init_jpeg_functions NULL +#endif + /* Structure describing the image type `jpeg'. */ static struct image_type jpeg_type = @@ -6032,12 +6013,13 @@ static struct image_type jpeg_type = jpeg_image_p, jpeg_load, x_clear_image, + init_jpeg_functions, NULL }; -/* Return non-zero if OBJECT is a valid JPEG image specification. */ +/* Return true if OBJECT is a valid JPEG image specification. */ -static int +static bool jpeg_image_p (Lisp_Object object) { struct image_keyword fmt[JPEG_LAST]; @@ -6086,12 +6068,12 @@ DEF_IMGLIB_FN (JDIMENSION, jpeg_read_scanlines, (j_decompress_ptr, JSAMPARRAY, J DEF_IMGLIB_FN (struct jpeg_error_mgr *, jpeg_std_error, (struct jpeg_error_mgr *)); DEF_IMGLIB_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int)); -static int -init_jpeg_functions (Lisp_Object libraries) +static bool +init_jpeg_functions (void) { HMODULE library; - if (!(library = w32_delayed_load (libraries, Qjpeg))) + if (!(library = w32_delayed_load (Qjpeg))) return 0; LOAD_IMGLIB_FN (library, jpeg_finish_decompress); @@ -6129,16 +6111,29 @@ jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired) struct my_jpeg_error_mgr { struct jpeg_error_mgr pub; - jmp_buf setjmp_buffer; + sys_jmp_buf setjmp_buffer; + + /* The remaining members are so that longjmp doesn't munge local + variables. */ + struct jpeg_decompress_struct cinfo; + enum + { + MY_JPEG_ERROR_EXIT, + MY_JPEG_INVALID_IMAGE_SIZE, + MY_JPEG_CANNOT_CREATE_X + } failure_code; +#ifdef lint + FILE *fp; +#endif }; -static void my_error_exit (j_common_ptr) NO_RETURN; -static void +static _Noreturn void my_error_exit (j_common_ptr cinfo) { struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; - longjmp (mgr->setjmp_buffer, 1); + mgr->failure_code = MY_JPEG_ERROR_EXIT; + sys_longjmp (mgr->setjmp_buffer, 1); } @@ -6206,7 +6201,7 @@ our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) reading the image. */ static void -jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, unsigned int len) +jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len) { struct jpeg_source_mgr *src; @@ -6343,18 +6338,16 @@ jpeg_file_src (j_decompress_ptr cinfo, FILE *fp) /* Load image IMG for use on frame F. Patterned after example.c from the JPEG lib. */ -static int -jpeg_load (struct frame *f, struct image *img) +static bool +jpeg_load_body (struct frame *f, struct image *img, + struct my_jpeg_error_mgr *mgr) { - struct jpeg_decompress_struct cinfo; - struct my_jpeg_error_mgr mgr; Lisp_Object file, specified_file; Lisp_Object specified_data; - FILE * volatile fp = NULL; + FILE *fp = NULL; JSAMPARRAY buffer; int row_stride, x, y; XImagePtr ximg = NULL; - int rc; unsigned long *colors; int width, height; @@ -6384,26 +6377,37 @@ jpeg_load (struct frame *f, struct image *img) return 0; } + IF_LINT (mgr->fp = fp); + /* Customize libjpeg's error handling to call my_error_exit when an error is detected. This function will perform a longjmp. */ - cinfo.err = fn_jpeg_std_error (&mgr.pub); - mgr.pub.error_exit = my_error_exit; - - if ((rc = setjmp (mgr.setjmp_buffer)) != 0) + mgr->cinfo.err = fn_jpeg_std_error (&mgr->pub); + mgr->pub.error_exit = my_error_exit; + if (sys_setjmp (mgr->setjmp_buffer)) { - if (rc == 1) + switch (mgr->failure_code) { - /* Called from my_error_exit. Display a JPEG error. */ - char buf[JMSG_LENGTH_MAX]; - cinfo.err->format_message ((j_common_ptr) &cinfo, buf); - image_error ("Error reading JPEG image `%s': %s", img->spec, - build_string (buf)); + case MY_JPEG_ERROR_EXIT: + { + char buf[JMSG_LENGTH_MAX]; + mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf); + image_error ("Error reading JPEG image `%s': %s", img->spec, + build_string (buf)); + break; + } + + case MY_JPEG_INVALID_IMAGE_SIZE: + image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); + break; + + case MY_JPEG_CANNOT_CREATE_X: + break; } /* Close the input file and destroy the JPEG object. */ if (fp) - fclose ((FILE *) fp); - fn_jpeg_destroy_decompress (&cinfo); + fclose (fp); + fn_jpeg_destroy_decompress (&mgr->cinfo); /* If we already have an XImage, free that. */ x_destroy_x_image (ximg); @@ -6413,46 +6417,52 @@ jpeg_load (struct frame *f, struct image *img) return 0; } + /* Silence a bogus diagnostic; see GCC bug 54561. */ + IF_LINT (fp = mgr->fp); + /* Create the JPEG decompression object. Let it read from fp. Read the JPEG image header. */ - fn_jpeg_CreateDecompress (&cinfo, JPEG_LIB_VERSION, sizeof (cinfo)); + fn_jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo); if (NILP (specified_data)) - jpeg_file_src (&cinfo, (FILE *) fp); + jpeg_file_src (&mgr->cinfo, fp); else - jpeg_memory_src (&cinfo, SDATA (specified_data), + jpeg_memory_src (&mgr->cinfo, SDATA (specified_data), SBYTES (specified_data)); - fn_jpeg_read_header (&cinfo, 1); + fn_jpeg_read_header (&mgr->cinfo, 1); /* Customize decompression so that color quantization will be used. Start decompression. */ - cinfo.quantize_colors = 1; - fn_jpeg_start_decompress (&cinfo); - width = img->width = cinfo.output_width; - height = img->height = cinfo.output_height; + mgr->cinfo.quantize_colors = 1; + fn_jpeg_start_decompress (&mgr->cinfo); + width = img->width = mgr->cinfo.output_width; + height = img->height = mgr->cinfo.output_height; if (!check_image_size (f, width, height)) { - image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); - longjmp (mgr.setjmp_buffer, 2); + mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE; + sys_longjmp (mgr->setjmp_buffer, 1); } /* Create X image and pixmap. */ if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) - longjmp (mgr.setjmp_buffer, 2); + { + mgr->failure_code = MY_JPEG_CANNOT_CREATE_X; + sys_longjmp (mgr->setjmp_buffer, 1); + } /* Allocate colors. When color quantization is used, - cinfo.actual_number_of_colors has been set with the number of - colors generated, and cinfo.colormap is a two-dimensional array - of color indices in the range 0..cinfo.actual_number_of_colors. + mgr->cinfo.actual_number_of_colors has been set with the number of + colors generated, and mgr->cinfo.colormap is a two-dimensional array + of color indices in the range 0..mgr->cinfo.actual_number_of_colors. No more than 255 colors will be generated. */ { int i, ir, ig, ib; - if (cinfo.out_color_components > 2) + if (mgr->cinfo.out_color_components > 2) ir = 0, ig = 1, ib = 2; - else if (cinfo.out_color_components > 1) + else if (mgr->cinfo.out_color_components > 1) ir = 0, ig = 1, ib = 0; else ir = 0, ig = 0, ib = 0; @@ -6462,16 +6472,15 @@ jpeg_load (struct frame *f, struct image *img) a default color, and we don't have to care about which colors can be freed safely, and which can't. */ init_color_table (); - colors = (unsigned long *) alloca (cinfo.actual_number_of_colors - * sizeof *colors); + colors = alloca (mgr->cinfo.actual_number_of_colors * sizeof *colors); - for (i = 0; i < cinfo.actual_number_of_colors; ++i) + for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i) { /* Multiply RGB values with 255 because X expects RGB values in the range 0..0xffff. */ - int r = cinfo.colormap[ir][i] << 8; - int g = cinfo.colormap[ig][i] << 8; - int b = cinfo.colormap[ib][i] << 8; + int r = mgr->cinfo.colormap[ir][i] << 8; + int g = mgr->cinfo.colormap[ig][i] << 8; + int b = mgr->cinfo.colormap[ib][i] << 8; colors[i] = lookup_rgb_color (f, r, g, b); } @@ -6483,21 +6492,21 @@ jpeg_load (struct frame *f, struct image *img) } /* Read pixels. */ - row_stride = width * cinfo.output_components; - buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE, - row_stride, 1); + row_stride = width * mgr->cinfo.output_components; + buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo, + JPOOL_IMAGE, row_stride, 1); for (y = 0; y < height; ++y) { - fn_jpeg_read_scanlines (&cinfo, buffer, 1); - for (x = 0; x < cinfo.output_width; ++x) + fn_jpeg_read_scanlines (&mgr->cinfo, buffer, 1); + for (x = 0; x < mgr->cinfo.output_width; ++x) XPutPixel (ximg, x, y, colors[buffer[0][x]]); } /* Clean up. */ - fn_jpeg_finish_decompress (&cinfo); - fn_jpeg_destroy_decompress (&cinfo); + fn_jpeg_finish_decompress (&mgr->cinfo); + fn_jpeg_destroy_decompress (&mgr->cinfo); if (fp) - fclose ((FILE *) fp); + fclose (fp); /* Maybe fill in the background field while we have ximg handy. */ if (NILP (image_spec_value (img->spec, QCbackground, NULL))) @@ -6510,10 +6519,17 @@ jpeg_load (struct frame *f, struct image *img) return 1; } +static bool +jpeg_load (struct frame *f, struct image *img) +{ + struct my_jpeg_error_mgr mgr; + return jpeg_load_body (f, img, &mgr); +} + #else /* HAVE_JPEG */ #ifdef HAVE_NS -static int +static bool jpeg_load (struct frame *f, struct image *img) { return ns_load_image (f, img, @@ -6532,8 +6548,8 @@ jpeg_load (struct frame *f, struct image *img) #if defined (HAVE_TIFF) || defined (HAVE_NS) -static int tiff_image_p (Lisp_Object object); -static int tiff_load (struct frame *f, struct image *img); +static bool tiff_image_p (Lisp_Object object); +static bool tiff_load (struct frame *f, struct image *img); /* The symbol `tiff' identifying images of this type. */ @@ -6575,6 +6591,12 @@ static const struct image_keyword tiff_format[TIFF_LAST] = {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_tiff_functions (void); +#else +#define init_tiff_functions NULL +#endif + /* Structure describing the image type `tiff'. */ static struct image_type tiff_type = @@ -6583,12 +6605,13 @@ static struct image_type tiff_type = tiff_image_p, tiff_load, x_clear_image, + init_tiff_functions, NULL }; -/* Return non-zero if OBJECT is a valid TIFF image specification. */ +/* Return true if OBJECT is a valid TIFF image specification. */ -static int +static bool tiff_image_p (Lisp_Object object) { struct image_keyword fmt[TIFF_LAST]; @@ -6622,12 +6645,12 @@ DEF_IMGLIB_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int)); DEF_IMGLIB_FN (void, TIFFClose, (TIFF *)); DEF_IMGLIB_FN (int, TIFFSetDirectory, (TIFF *, tdir_t)); -static int -init_tiff_functions (Lisp_Object libraries) +static bool +init_tiff_functions (void) { HMODULE library; - if (!(library = w32_delayed_load (libraries, Qtiff))) + if (!(library = w32_delayed_load (Qtiff))) return 0; LOAD_IMGLIB_FN (library, TIFFSetErrorHandler); @@ -6792,10 +6815,10 @@ tiff_warning_handler (const char *title, const char *format, va_list ap) } -/* Load TIFF image IMG for use on frame F. Value is non-zero if +/* Load TIFF image IMG for use on frame F. Value is true if successful. */ -static int +static bool tiff_load (struct frame *f, struct image *img) { Lisp_Object file, specified_file; @@ -6896,7 +6919,7 @@ tiff_load (struct frame *f, struct image *img) return 0; } - buf = (uint32 *) xmalloc (sizeof *buf * width * height); + buf = xmalloc (sizeof *buf * width * height); rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); @@ -6960,7 +6983,7 @@ tiff_load (struct frame *f, struct image *img) #else /* HAVE_TIFF */ #ifdef HAVE_NS -static int +static bool tiff_load (struct frame *f, struct image *img) { return ns_load_image (f, img, @@ -6979,8 +7002,8 @@ tiff_load (struct frame *f, struct image *img) #if defined (HAVE_GIF) || defined (HAVE_NS) -static int gif_image_p (Lisp_Object object); -static int gif_load (struct frame *f, struct image *img); +static bool gif_image_p (Lisp_Object object); +static bool gif_load (struct frame *f, struct image *img); static void gif_clear_image (struct frame *f, struct image *img); /* The symbol `gif' identifying images of this type. */ @@ -7023,6 +7046,12 @@ static const struct image_keyword gif_format[GIF_LAST] = {":background", IMAGE_STRING_OR_NIL_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_gif_functions (void); +#else +#define init_gif_functions NULL +#endif + /* Structure describing the image type `gif'. */ static struct image_type gif_type = @@ -7031,6 +7060,7 @@ static struct image_type gif_type = gif_image_p, gif_load, gif_clear_image, + init_gif_functions, NULL }; @@ -7043,9 +7073,9 @@ gif_clear_image (struct frame *f, struct image *img) x_clear_image (f, img); } -/* Return non-zero if OBJECT is a valid GIF image specification. */ +/* Return true if OBJECT is a valid GIF image specification. */ -static int +static bool gif_image_p (Lisp_Object object) { struct image_keyword fmt[GIF_LAST]; @@ -7088,12 +7118,12 @@ DEF_IMGLIB_FN (int, DGifSlurp, (GifFileType *)); DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc)); DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *)); -static int -init_gif_functions (Lisp_Object libraries) +static bool +init_gif_functions (void) { HMODULE library; - if (!(library = w32_delayed_load (libraries, Qgif))) + if (!(library = w32_delayed_load (Qgif))) return 0; LOAD_IMGLIB_FN (library, DGifCloseFile); @@ -7142,7 +7172,7 @@ gif_read_from_memory (GifFileType *file, GifByteType *buf, int len) } -/* Load GIF image IMG for use on frame F. Value is non-zero if +/* Load GIF image IMG for use on frame F. Value is true if successful. */ static const int interlace_start[] = {0, 4, 2, 1}; @@ -7150,7 +7180,7 @@ static const int interlace_increment[] = {8, 8, 4, 2}; #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249 -static int +static bool gif_load (struct frame *f, struct image *img) { Lisp_Object file; @@ -7448,7 +7478,7 @@ gif_load (struct frame *f, struct image *img) #else /* !HAVE_GIF */ #ifdef HAVE_NS -static int +static bool gif_load (struct frame *f, struct image *img) { return ns_load_image (f, img, @@ -7467,8 +7497,8 @@ gif_load (struct frame *f, struct image *img) static Lisp_Object Qimagemagick; -static int imagemagick_image_p (Lisp_Object); -static int imagemagick_load (struct frame *, struct image *); +static bool imagemagick_image_p (Lisp_Object); +static bool imagemagick_load (struct frame *, struct image *); static void imagemagick_clear_image (struct frame *, struct image *); /* Indices of image specification fields in imagemagick_format. */ @@ -7513,6 +7543,12 @@ static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] = {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0} }; +#ifdef HAVE_NTGUI +static bool init_imagemagick_functions (void); +#else +#define init_imagemagick_functions NULL +#endif + /* Structure describing the image type for any image handled via ImageMagick. */ @@ -7522,6 +7558,7 @@ static struct image_type imagemagick_type = imagemagick_image_p, imagemagick_load, imagemagick_clear_image, + init_imagemagick_functions, NULL }; @@ -7534,11 +7571,11 @@ imagemagick_clear_image (struct frame *f, x_clear_image (f, img); } -/* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do +/* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do this by calling parse_image_spec and supplying the keywords that identify the IMAGEMAGICK format. */ -static int +static bool imagemagick_image_p (Lisp_Object object) { struct image_keyword fmt[IMAGEMAGICK_LAST]; @@ -7552,7 +7589,7 @@ imagemagick_image_p (Lisp_Object object) } /* The GIF library also defines DrawRectangle, but its never used in Emacs. - Therefore rename the function so it doesnt collide with ImageMagick. */ + Therefore rename the function so it doesn't collide with ImageMagick. */ #define DrawRectangle DrawRectangleGif #include @@ -7564,6 +7601,22 @@ extern WandExport void PixelGetMagickColor (const PixelWand *, MagickPixelPacket *); #endif +/* Log ImageMagick error message. + Useful when a ImageMagick function returns the status `MagickFalse'. */ + +static void +imagemagick_error (MagickWand *wand) +{ + char *description; + ExceptionType severity; + + description = MagickGetException (wand, &severity); + image_error ("ImageMagick error: %s", + build_string (description), + Qnil); + description = (char *) MagickRelinquishMemory (description); +} + /* Helper function for imagemagick_load, which does the actual loading given contents and size, apart from frame and image structures, passed from imagemagick_load. Uses librimagemagick to do most of @@ -7574,26 +7627,21 @@ extern WandExport void PixelGetMagickColor (const PixelWand *, be parsed; SIZE is the number of bytes of data; and FILENAME is either the file name or the image data. - Return non-zero if successful. */ + Return true if successful. */ -static int +static bool imagemagick_load_image (struct frame *f, struct image *img, unsigned char *contents, unsigned int size, char *filename) { - size_t width; - size_t height; - + size_t width, height; MagickBooleanType status; - XImagePtr ximg; - int x; - int y; - - MagickWand *image_wand; - MagickWand *ping_wand; + int x, y; + MagickWand *image_wand; + MagickWand *ping_wand; PixelIterator *iterator; - PixelWand **pixels; + PixelWand **pixels, *bg_wand = NULL; MagickPixelPacket pixel; Lisp_Object image; Lisp_Object value; @@ -7601,12 +7649,7 @@ imagemagick_load_image (struct frame *f, struct image *img, EMACS_INT ino; int desired_width, desired_height; double rotation; - EMACS_INT imagemagick_rendermethod; int pixelwidth; - ImageInfo *image_info; - ExceptionInfo *exception; - Image * im_image; - /* Handle image index for image types who can contain more than one image. Interface :index is same as for GIF. First we "ping" the image to see how @@ -7618,17 +7661,20 @@ imagemagick_load_image (struct frame *f, struct image *img, image = image_spec_value (img->spec, QCindex, NULL); ino = INTEGERP (image) ? XFASTINT (image) : 0; ping_wand = NewMagickWand (); - MagickSetResolution (ping_wand, 2, 2); - if (filename != NULL) - { - status = MagickPingImage (ping_wand, filename); - } - else + /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */ + + status = filename + ? MagickPingImage (ping_wand, filename) + : MagickPingImageBlob (ping_wand, contents, size); + + if (status == MagickFalse) { - status = MagickPingImageBlob (ping_wand, contents, size); + imagemagick_error (ping_wand); + DestroyMagickWand (ping_wand); + return 0; } - if (! (0 <= ino && ino < MagickGetNumberImages (ping_wand))) + if (ino < 0 || ino >= MagickGetNumberImages (ping_wand)) { image_error ("Invalid image number `%s' in image `%s'", image, img->spec); @@ -7645,36 +7691,46 @@ imagemagick_load_image (struct frame *f, struct image *img, DestroyMagickWand (ping_wand); /* Now we know how many images are inside the file. If it's not a - bundle, the number is one. */ - - if (filename != NULL) - { - image_info = CloneImageInfo ((ImageInfo *) NULL); - (void) strcpy (image_info->filename, filename); - image_info->number_scenes = 1; - image_info->scene = ino; - exception = AcquireExceptionInfo (); + bundle, the number is one. Load the image data. */ - im_image = ReadImage (image_info, exception); - DestroyExceptionInfo (exception); + image_wand = NewMagickWand (); - if (im_image == NULL) - goto imagemagick_no_wand; - image_wand = NewMagickWandFromImage (im_image); - DestroyImage (im_image); - } - else + if ((filename + ? MagickReadImage (image_wand, filename) + : MagickReadImageBlob (image_wand, contents, size)) + == MagickFalse) { - image_wand = NewMagickWand (); - if (MagickReadImageBlob (image_wand, contents, size) == MagickFalse) - goto imagemagick_error; + imagemagick_error (image_wand); + goto imagemagick_error; } + /* Retrieve the frame's background color, for use later. */ + { + XColor bgcolor; + Lisp_Object specified_bg; + + specified_bg = image_spec_value (img->spec, QCbackground, NULL); + if (!STRINGP (specified_bg) + || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0)) + { +#ifndef HAVE_NS + bgcolor.pixel = FRAME_BACKGROUND_PIXEL (f); + x_query_color (f, &bgcolor); +#else + ns_query_color (FRAME_BACKGROUND_COLOR (f), &bgcolor, 1); +#endif + } + + bg_wand = NewPixelWand (); + PixelSetRed (bg_wand, (double) bgcolor.red / 65535); + PixelSetGreen (bg_wand, (double) bgcolor.green / 65535); + PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535); + } + /* If width and/or height is set in the display spec assume we want to scale to those values. If either h or w is unspecified, the unspecified should be calculated from the specified to preserve aspect ratio. */ - value = image_spec_value (img->spec, QCwidth, NULL); desired_width = (INTEGERP (value) ? XFASTINT (value) : -1); value = image_spec_value (img->spec, QCheight, NULL); @@ -7695,6 +7751,7 @@ imagemagick_load_image (struct frame *f, struct image *img, if (status == MagickFalse) { image_error ("Imagemagick scale failed", Qnil, Qnil); + imagemagick_error (image_wand); goto imagemagick_error; } } @@ -7739,25 +7796,37 @@ imagemagick_load_image (struct frame *f, struct image *img, value = image_spec_value (img->spec, QCrotation, NULL); if (FLOATP (value)) { - PixelWand* background = NewPixelWand (); - PixelSetColor (background, "#ffffff");/*TODO remove hardcode*/ - rotation = extract_float (value); - - status = MagickRotateImage (image_wand, background, rotation); - DestroyPixelWand (background); + status = MagickRotateImage (image_wand, bg_wand, rotation); if (status == MagickFalse) { image_error ("Imagemagick image rotate failed", Qnil, Qnil); + imagemagick_error (image_wand); goto imagemagick_error; } } /* Finally we are done manipulating the image. Figure out the - resulting width/height and transfer ownerwship to Emacs. */ + resulting width/height and transfer ownership to Emacs. */ height = MagickGetImageHeight (image_wand); width = MagickGetImageWidth (image_wand); + /* Set the canvas background color to the frame or specified + background, and flatten the image. Note: as of ImageMagick + 6.6.0, SVG image transparency is not handled properly + (e.g. etc/images/splash.svg shows a white background always). */ + { + MagickWand *new_wand; + MagickSetImageBackgroundColor (image_wand, bg_wand); +#ifdef HAVE_MAGICKMERGEIMAGELAYERS + new_wand = MagickMergeImageLayers (image_wand, MergeLayer); +#else + new_wand = MagickFlattenImages (image_wand); +#endif + DestroyMagickWand (image_wand); + image_wand = new_wand; + } + if (! (width <= INT_MAX && height <= INT_MAX && check_image_size (f, width, height))) { @@ -7769,9 +7838,51 @@ imagemagick_load_image (struct frame *f, struct image *img, went ok. */ init_color_table (); - imagemagick_rendermethod = (INTEGERP (Vimagemagick_render_type) - ? XINT (Vimagemagick_render_type) : 0); - if (imagemagick_rendermethod == 0) + +#ifdef HAVE_MAGICKEXPORTIMAGEPIXELS + if (imagemagick_render_type != 0) + { + /* Magicexportimage is normally faster than pixelpushing. This + method is also well tested. Some aspects of this method are + ad-hoc and needs to be more researched. */ + int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/ + const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/ + /* Try to create a x pixmap to hold the imagemagick pixmap. */ + if (!x_create_x_image_and_pixmap (f, width, height, imagedepth, + &ximg, &img->pixmap)) + { +#ifdef COLOR_TABLE_SUPPORT + free_color_table (); +#endif + image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); + goto imagemagick_error; + } + + /* Oddly, the below code doesn't seem to work:*/ + /* switch(ximg->bitmap_unit){ */ + /* case 8: */ + /* pixelwidth=CharPixel; */ + /* break; */ + /* case 16: */ + /* pixelwidth=ShortPixel; */ + /* break; */ + /* case 32: */ + /* pixelwidth=LongPixel; */ + /* break; */ + /* } */ + /* + Here im just guessing the format of the bitmap. + happens to work fine for: + - bw djvu images + on rgb display. + seems about 3 times as fast as pixel pushing(not carefully measured) + */ + pixelwidth = CharPixel; /*??? TODO figure out*/ + MagickExportImagePixels (image_wand, 0, 0, width, height, + exportdepth, pixelwidth, ximg->data); + } + else +#endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */ { size_t image_height; @@ -7786,7 +7897,7 @@ imagemagick_load_image (struct frame *f, struct image *img, goto imagemagick_error; } - /* Copy imagegmagick image to x with primitive yet robust pixel + /* Copy imagemagick image to x with primitive yet robust pixel pusher loop. This has been tested a lot with many different images. */ @@ -7822,67 +7933,12 @@ imagemagick_load_image (struct frame *f, struct image *img, DestroyPixelIterator (iterator); } - if (imagemagick_rendermethod == 1) - { - /* Magicexportimage is normaly faster than pixelpushing. This - method is also well tested. Some aspects of this method are - ad-hoc and needs to be more researched. */ - int imagedepth = 24;/*MagickGetImageDepth(image_wand);*/ - const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP";/*"RGBP";*/ - /* Try to create a x pixmap to hold the imagemagick pixmap. */ - if (!x_create_x_image_and_pixmap (f, width, height, imagedepth, - &ximg, &img->pixmap)) - { -#ifdef COLOR_TABLE_SUPPORT - free_color_table (); -#endif - image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); - goto imagemagick_error; - } - - - /* Oddly, the below code doesnt seem to work:*/ - /* switch(ximg->bitmap_unit){ */ - /* case 8: */ - /* pixelwidth=CharPixel; */ - /* break; */ - /* case 16: */ - /* pixelwidth=ShortPixel; */ - /* break; */ - /* case 32: */ - /* pixelwidth=LongPixel; */ - /* break; */ - /* } */ - /* - Here im just guessing the format of the bitmap. - happens to work fine for: - - bw djvu images - on rgb display. - seems about 3 times as fast as pixel pushing(not carefully measured) - */ - pixelwidth = CharPixel;/*??? TODO figure out*/ -#ifdef HAVE_MAGICKEXPORTIMAGEPIXELS - MagickExportImagePixels (image_wand, - 0, 0, - width, height, - exportdepth, - pixelwidth, - /*&(img->pixmap));*/ - ximg->data); -#else - image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!", - Qnil, Qnil); -#endif - } - - #ifdef COLOR_TABLE_SUPPORT /* Remember colors allocated for this image. */ img->colors = colors_in_color_table (&img->ncolors); free_color_table (); #endif /* COLOR_TABLE_SUPPORT */ - img->width = width; img->height = height; @@ -7891,9 +7947,10 @@ imagemagick_load_image (struct frame *f, struct image *img, x_put_x_image (f, ximg, img->pixmap, width, height); x_destroy_x_image (ximg); - /* Final cleanup. image_wand should be the only resource left. */ DestroyMagickWand (image_wand); + if (bg_wand) DestroyPixelWand (bg_wand); + /* `MagickWandTerminus' terminates the imagemagick environment. */ MagickWandTerminus (); @@ -7901,7 +7958,8 @@ imagemagick_load_image (struct frame *f, struct image *img, imagemagick_error: DestroyMagickWand (image_wand); - imagemagick_no_wand: + if (bg_wand) DestroyPixelWand (bg_wand); + MagickWandTerminus (); /* TODO more cleanup. */ image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil); @@ -7909,14 +7967,14 @@ imagemagick_load_image (struct frame *f, struct image *img, } -/* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if +/* Load IMAGEMAGICK image IMG for use on frame F. Value is true if successful. this function will go into the imagemagick_type structure, and the prototype thus needs to be compatible with that structure. */ -static int +static bool imagemagick_load (struct frame *f, struct image *img) { - int success_p = 0; + bool success_p = 0; Lisp_Object file_name; /* If IMG->spec specifies a file name, create a non-file spec from it. */ @@ -7957,23 +8015,30 @@ DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0, Each entry in this list is a symbol named after an ImageMagick format tag. See the ImageMagick manual for a list of ImageMagick formats and their descriptions (http://www.imagemagick.org/script/formats.php). +You can also try the shell command: `identify -list format'. Note that ImageMagick recognizes many file-types that Emacs does not -recognize as images, such as C. See `imagemagick-types-inhibit'. */) +recognize as images, such as C. See `imagemagick-types-enable' +and `imagemagick-types-inhibit'. */) (void) { Lisp_Object typelist = Qnil; size_t numf = 0; ExceptionInfo ex; - char **imtypes = GetMagickList ("*", &numf, &ex); + char **imtypes; size_t i; Lisp_Object Qimagemagicktype; + + GetExceptionInfo(&ex); + imtypes = GetMagickList ("*", &numf, &ex); + DestroyExceptionInfo(&ex); + for (i = 0; i < numf; i++) { Qimagemagicktype = intern (imtypes[i]); typelist = Fcons (Qimagemagicktype, typelist); } - return typelist; + return Fnreverse (typelist); } #endif /* defined (HAVE_IMAGEMAGICK) */ @@ -7988,11 +8053,11 @@ recognize as images, such as C. See `imagemagick-types-inhibit'. */) /* Function prototypes. */ -static int svg_image_p (Lisp_Object object); -static int svg_load (struct frame *f, struct image *img); +static bool svg_image_p (Lisp_Object object); +static bool svg_load (struct frame *f, struct image *img); -static int svg_load_image (struct frame *, struct image *, - unsigned char *, ptrdiff_t); +static bool svg_load_image (struct frame *, struct image *, + unsigned char *, ptrdiff_t); /* The symbol `svg' identifying images of this type. */ @@ -8032,31 +8097,32 @@ static const struct image_keyword svg_format[SVG_LAST] = {":background", IMAGE_STRING_OR_NIL_VALUE, 0} }; +#ifdef HAVE_NTGUI +static bool init_svg_functions (void); +#else +#define init_svg_functions NULL +#endif + /* Structure describing the image type `svg'. Its the same type of structure defined for all image formats, handled by emacs image functions. See struct image_type in dispextern.h. */ static struct image_type svg_type = { - /* An identifier showing that this is an image structure for the SVG format. */ &Qsvg, - /* Handle to a function that can be used to identify a SVG file. */ svg_image_p, - /* Handle to function used to load a SVG file. */ svg_load, - /* Handle to function to free sresources for SVG. */ x_clear_image, - /* An internal field to link to the next image type in a list of - image types, will be filled in when registering the format. */ + init_svg_functions, NULL }; -/* Return non-zero if OBJECT is a valid SVG image specification. Do +/* Return true if OBJECT is a valid SVG image specification. Do this by calling parse_image_spec and supplying the keywords that identify the SVG format. */ -static int +static bool svg_image_p (Lisp_Object object) { struct image_keyword fmt[SVG_LAST]; @@ -8095,15 +8161,15 @@ DEF_IMGLIB_FN (void, g_error_free); Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; -static int -init_svg_functions (Lisp_Object libraries) +static bool +init_svg_functions (void) { HMODULE library, gdklib, glib, gobject; - if (!(glib = w32_delayed_load (libraries, Qglib)) - || !(gobject = w32_delayed_load (libraries, Qgobject)) - || !(gdklib = w32_delayed_load (libraries, Qgdk_pixbuf)) - || !(library = w32_delayed_load (libraries, Qsvg))) + if (!(glib = w32_delayed_load (Qglib)) + || !(gobject = w32_delayed_load (Qgobject)) + || !(gdklib = w32_delayed_load (Qgdk_pixbuf)) + || !(library = w32_delayed_load (Qsvg))) return 0; LOAD_IMGLIB_FN (library, rsvg_handle_new); @@ -8151,14 +8217,13 @@ init_svg_functions (Lisp_Object libraries) #define fn_g_error_free g_error_free #endif /* !HAVE_NTGUI */ -/* Load SVG image IMG for use on frame F. Value is non-zero if - successful. this function will go into the svg_type structure, and - the prototype thus needs to be compatible with that structure. */ +/* Load SVG image IMG for use on frame F. Value is true if + successful. */ -static int +static bool svg_load (struct frame *f, struct image *img) { - int success_p = 0; + bool success_p = 0; Lisp_Object file_name; /* If IMG->spec specifies a file name, create a non-file spec from it. */ @@ -8211,8 +8276,8 @@ svg_load (struct frame *f, struct image *img) Uses librsvg to do most of the image processing. - Returns non-zero when successful. */ -static int + Returns true when successful. */ +static bool svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */ struct image *img, /* Pointer to emacs image structure. */ unsigned char *contents, /* String containing the SVG XML data to be parsed. */ @@ -8379,8 +8444,8 @@ svg_load_image (struct frame *f, /* Pointer to emacs frame structure. * #ifdef HAVE_GHOSTSCRIPT -static int gs_image_p (Lisp_Object object); -static int gs_load (struct frame *f, struct image *img); +static bool gs_image_p (Lisp_Object object); +static bool gs_load (struct frame *f, struct image *img); static void gs_clear_image (struct frame *f, struct image *img); /* Keyword symbols. */ @@ -8435,6 +8500,7 @@ static struct image_type gs_type = gs_image_p, gs_load, gs_clear_image, + NULL, NULL }; @@ -8448,10 +8514,10 @@ gs_clear_image (struct frame *f, struct image *img) } -/* Return non-zero if OBJECT is a valid Ghostscript image +/* Return true if OBJECT is a valid Ghostscript image specification. */ -static int +static bool gs_image_p (Lisp_Object object) { struct image_keyword fmt[GS_LAST]; @@ -8478,7 +8544,7 @@ gs_image_p (Lisp_Object object) if (ASIZE (tem) != 4) return 0; for (i = 0; i < 4; ++i) - if (!INTEGERP (XVECTOR (tem)->contents[i])) + if (!INTEGERP (AREF (tem, i))) return 0; } else @@ -8488,10 +8554,10 @@ gs_image_p (Lisp_Object object) } -/* Load Ghostscript image IMG for use on frame F. Value is non-zero +/* Load Ghostscript image IMG for use on frame F. Value is true if successful. */ -static int +static bool gs_load (struct frame *f, struct image *img) { uprintmax_t printnum1, printnum2; @@ -8522,16 +8588,16 @@ gs_load (struct frame *f, struct image *img) img->height = in_height; /* Create the pixmap. */ - xassert (img->pixmap == NO_PIXMAP); + eassert (img->pixmap == NO_PIXMAP); if (x_check_image_size (0, img->width, img->height)) { /* Only W32 version did BLOCK_INPUT here. ++kfs */ - BLOCK_INPUT; + block_input (); img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), img->width, img->height, DefaultDepthOfScreen (FRAME_X_SCREEN (f))); - UNBLOCK_INPUT; + unblock_input (); } if (!img->pixmap) @@ -8546,13 +8612,13 @@ gs_load (struct frame *f, struct image *img) don't either. Let the Lisp loader use `unwind-protect' instead. */ printnum1 = FRAME_X_WINDOW (f); printnum2 = img->pixmap; - sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2); - window_and_pixmap_id = build_string (buffer); + window_and_pixmap_id + = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); printnum1 = FRAME_FOREGROUND_PIXEL (f); printnum2 = FRAME_BACKGROUND_PIXEL (f); - sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2); - pixel_colors = build_string (buffer); + pixel_colors + = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); XSETFRAME (frame, f); loader = image_spec_value (img->spec, QCloader, NULL); @@ -8593,7 +8659,7 @@ x_kill_gs_process (Pixmap pixmap, struct frame *f) /* Kill the GS process. We should have found PIXMAP in the image cache and its image should contain a process object. */ img = c->images[i]; - xassert (PROCESSP (img->lisp_data)); + eassert (PROCESSP (img->lisp_data)); Fkill_process (img->lisp_data, Qnil); img->lisp_data = Qnil; @@ -8607,7 +8673,7 @@ x_kill_gs_process (Pixmap pixmap, struct frame *f) { XImagePtr ximg; - BLOCK_INPUT; + block_input (); /* Try to get an XImage for img->pixmep. */ ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, @@ -8650,15 +8716,15 @@ x_kill_gs_process (Pixmap pixmap, struct frame *f) image_error ("Cannot get X image of `%s'; colors will not be freed", img->spec, Qnil); - UNBLOCK_INPUT; + unblock_input (); } #endif /* HAVE_X_WINDOWS */ /* Now that we have the pixmap, compute mask and transform the image if requested. */ - BLOCK_INPUT; + block_input (); postprocess_image (f, img); - UNBLOCK_INPUT; + unblock_input (); } #endif /* HAVE_GHOSTSCRIPT */ @@ -8668,7 +8734,7 @@ x_kill_gs_process (Pixmap pixmap, struct frame *f) Tests ***********************************************************************/ -#if GLYPH_DEBUG +#ifdef GLYPH_DEBUG DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, doc: /* Value is non-nil if SPEC is a valid image specification. */) @@ -8690,98 +8756,102 @@ DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") return make_number (id); } -#endif /* GLYPH_DEBUG != 0 */ +#endif /* GLYPH_DEBUG */ /*********************************************************************** Initialization ***********************************************************************/ -#ifdef HAVE_NTGUI -/* Image types that rely on external libraries are loaded dynamically - if the library is available. */ -#define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ - define_image_type (image_type, init_lib_fn (libraries)) -#else -#define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ - define_image_type (image_type, 1) -#endif /* HAVE_NTGUI */ - -DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 2, 2, 0, +DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0, doc: /* Initialize image library implementing image type TYPE. Return non-nil if TYPE is a supported image type. -Image types pbm and xbm are prebuilt; other types are loaded here. -Libraries to load are specified in alist LIBRARIES (usually, the value -of `dynamic-library-alist', which see). */) - (Lisp_Object type, Lisp_Object libraries) +If image libraries are loaded dynamically (currently only the case on +MS-Windows), load the library for TYPE if it is not yet loaded, using +the library file(s) specified by `dynamic-library-alist'. */) + (Lisp_Object type) { -#ifdef HAVE_NTGUI - /* Don't try to reload the library. */ - Lisp_Object tested = Fassq (type, Vlibrary_cache); - if (CONSP (tested)) - return XCDR (tested); -#endif + return lookup_image_type (type) ? Qt : Qnil; +} + +/* Look up image type TYPE, and return a pointer to its image_type + structure. Return 0 if TYPE is not a known image type. */ +static struct image_type * +lookup_image_type (Lisp_Object type) +{ /* Types pbm and xbm are built-in and always available. */ - if (EQ (type, Qpbm) || EQ (type, Qxbm)) - return Qt; + if (EQ (type, Qpbm)) + return define_image_type (&pbm_type); + + if (EQ (type, Qxbm)) + return define_image_type (&xbm_type); #if defined (HAVE_XPM) || defined (HAVE_NS) if (EQ (type, Qxpm)) - return CHECK_LIB_AVAILABLE (&xpm_type, init_xpm_functions, libraries); + return define_image_type (&xpm_type); #endif #if defined (HAVE_JPEG) || defined (HAVE_NS) if (EQ (type, Qjpeg)) - return CHECK_LIB_AVAILABLE (&jpeg_type, init_jpeg_functions, libraries); + return define_image_type (&jpeg_type); #endif #if defined (HAVE_TIFF) || defined (HAVE_NS) if (EQ (type, Qtiff)) - return CHECK_LIB_AVAILABLE (&tiff_type, init_tiff_functions, libraries); + return define_image_type (&tiff_type); #endif #if defined (HAVE_GIF) || defined (HAVE_NS) if (EQ (type, Qgif)) - return CHECK_LIB_AVAILABLE (&gif_type, init_gif_functions, libraries); + return define_image_type (&gif_type); #endif #if defined (HAVE_PNG) || defined (HAVE_NS) if (EQ (type, Qpng)) - return CHECK_LIB_AVAILABLE (&png_type, init_png_functions, libraries); + return define_image_type (&png_type); #endif #if defined (HAVE_RSVG) if (EQ (type, Qsvg)) - return CHECK_LIB_AVAILABLE (&svg_type, init_svg_functions, libraries); + return define_image_type (&svg_type); #endif #if defined (HAVE_IMAGEMAGICK) if (EQ (type, Qimagemagick)) - return CHECK_LIB_AVAILABLE (&imagemagick_type, init_imagemagick_functions, - libraries); + return define_image_type (&imagemagick_type); #endif #ifdef HAVE_GHOSTSCRIPT if (EQ (type, Qpostscript)) - return CHECK_LIB_AVAILABLE (&gs_type, init_gs_functions, libraries); + return define_image_type (&gs_type); #endif - /* If the type is not recognized, avoid testing it ever again. */ - CACHE_IMAGE_TYPE (type, Qnil); - return Qnil; + return NULL; +} + +/* Reset image_types before dumping. + Called from Fdump_emacs. */ + +void +reset_image_types (void) +{ + while (image_types) + { + struct image_type *next = image_types->next; + xfree (image_types); + image_types = next; + } } void syms_of_image (void) { - /* Initialize this only once, since that's what we do with Vimage_types - and they are supposed to be in sync. Initializing here gives correct - operation on GNU/Linux of calling dump-emacs after loading some images. */ + /* Initialize this only once; it will be reset before dumping. */ image_types = NULL; - /* Must be defined now becase we're going to update it below, while + /* Must be defined now because we're going to update it below, while defining the supported image types. */ DEFVAR_LISP ("image-types", Vimage_types, doc: /* List of potentially supported image types. @@ -8801,15 +8871,6 @@ as a ratio to the frame height and width. If the value is non-numeric, there is no explicit limit on the size of images. */); Vmax_image_size = make_float (MAX_IMAGE_SIZE); - DEFSYM (Qpbm, "pbm"); - ADD_IMAGE_TYPE (Qpbm); - - DEFSYM (Qxbm, "xbm"); - ADD_IMAGE_TYPE (Qxbm); - - define_image_type (&xbm_type, 1); - define_image_type (&pbm_type, 1); - DEFSYM (Qcount, "count"); DEFSYM (Qextension_data, "extension-data"); DEFSYM (Qdelay, "delay"); @@ -8853,6 +8914,12 @@ non-numeric, there is no explicit limit on the size of images. */); ); #endif + DEFSYM (Qpbm, "pbm"); + ADD_IMAGE_TYPE (Qpbm); + + DEFSYM (Qxbm, "xbm"); + ADD_IMAGE_TYPE (Qxbm); + #if defined (HAVE_XPM) || defined (HAVE_NS) DEFSYM (Qxpm, "xpm"); ADD_IMAGE_TYPE (Qxpm); @@ -8904,7 +8971,7 @@ non-numeric, there is no explicit limit on the size of images. */); defsubr (&Simage_mask_p); defsubr (&Simage_metadata); -#if GLYPH_DEBUG +#ifdef GLYPH_DEBUG defsubr (&Simagep); defsubr (&Slookup_image); #endif @@ -8929,13 +8996,17 @@ The value can also be nil, meaning the cache is never cleared. The function `clear-image-cache' disregards this variable. */); Vimage_cache_eviction_delay = make_number (300); #ifdef HAVE_IMAGEMAGICK - DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type, - doc: /* Choose between ImageMagick render methods. */); + DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type, + doc: /* Integer indicating which ImageMagick rendering method to use. +The options are: + 0 -- the default method (pixel pushing) + 1 -- a newer method ("MagickExportImagePixels") that may perform + better (speed etc) in some cases, but has not been as thoroughly + tested with Emacs as the default method. This method requires + ImageMagick version 6.4.6 (approximately) or later. +*/); + /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */ + imagemagick_render_type = 0; #endif } - -void -init_image (void) -{ -}