/* 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.
#define COLOR_TABLE_SUPPORT 1
typedef struct x_bitmap_record Bitmap_Record;
-#define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
+#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define NO_PIXMAP None
#define RGB_PIXEL_COLOR unsigned long
#ifdef HAVE_NTGUI
+#include "w32.h"
#include "w32term.h"
/* W32_TODO : Color tables on W32. */
#undef COLOR_TABLE_SUPPORT
typedef struct w32_bitmap_record Bitmap_Record;
-#define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
+#define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
#define NO_PIXMAP 0
#define RGB_PIXEL_COLOR COLORREF
typedef struct ns_bitmap_record Bitmap_Record;
-#define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
+#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define NO_PIXMAP 0
#define RGB_PIXEL_COLOR unsigned long
#define PIX_MASK_RETAIN 0
#define PIX_MASK_DRAW 1
-#define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual
+#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
#ifdef COLOR_TABLE_SUPPORT
static void free_color_table (void);
static unsigned long *colors_in_color_table (int *n);
-static unsigned long lookup_pixel_color (struct frame *f, unsigned long p);
#endif
static Lisp_Object Finit_image_library (Lisp_Object, Lisp_Object);
/* Functions to access the contents of a bitmap, given an id. */
int
-x_bitmap_height (FRAME_PTR f, int id)
+x_bitmap_height (FRAME_PTR f, ptrdiff_t id)
{
return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
}
int
-x_bitmap_width (FRAME_PTR f, int id)
+x_bitmap_width (FRAME_PTR f, ptrdiff_t id)
{
return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
}
#if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
int
-x_bitmap_pixmap (FRAME_PTR f, int id)
+x_bitmap_pixmap (FRAME_PTR f, ptrdiff_t id)
{
+ /* HAVE_NTGUI needs the explicit cast here. */
return (int) FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
}
#endif
#ifdef HAVE_X_WINDOWS
int
-x_bitmap_mask (FRAME_PTR f, int id)
+x_bitmap_mask (FRAME_PTR f, ptrdiff_t id)
{
return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
}
/* Allocate a new bitmap record. Returns index of new record. */
-static int
+static ptrdiff_t
x_allocate_bitmap_record (FRAME_PTR f)
{
Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
- int i;
-
- if (dpyinfo->bitmaps == NULL)
- {
- dpyinfo->bitmaps_size = 10;
- dpyinfo->bitmaps
- = (Bitmap_Record *) xmalloc (dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
- dpyinfo->bitmaps_last = 1;
- return 1;
- }
+ ptrdiff_t i;
if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
return ++dpyinfo->bitmaps_last;
if (dpyinfo->bitmaps[i].refcount == 0)
return i + 1;
- dpyinfo->bitmaps_size *= 2;
- dpyinfo->bitmaps
- = (Bitmap_Record *) xrealloc (dpyinfo->bitmaps,
- dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
+ dpyinfo->bitmaps =
+ xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size,
+ 10, -1, sizeof *dpyinfo->bitmaps);
return ++dpyinfo->bitmaps_last;
}
/* Add one reference to the reference count of the bitmap with id ID. */
void
-x_reference_bitmap (FRAME_PTR f, int id)
+x_reference_bitmap (FRAME_PTR f, ptrdiff_t id)
{
++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
}
/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
-int
+ptrdiff_t
x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height)
{
Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
- int id;
+ ptrdiff_t id;
#ifdef HAVE_X_WINDOWS
Pixmap bitmap;
/* Create bitmap from file FILE for frame F. */
-int
+ptrdiff_t
x_create_bitmap_from_file (struct frame *f, Lisp_Object file)
{
Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
#endif /* HAVE_NTGUI */
#ifdef HAVE_NS
- int id;
+ ptrdiff_t id;
void *bitmap = ns_image_from_file (file);
if (!bitmap)
#ifdef HAVE_X_WINDOWS
unsigned int width, height;
Pixmap bitmap;
- int xhot, yhot, result, id;
+ int xhot, yhot, result;
+ ptrdiff_t id;
Lisp_Object found;
int fd;
char *filename;
/* Remove reference to bitmap with id number ID. */
void
-x_destroy_bitmap (FRAME_PTR f, int id)
+x_destroy_bitmap (FRAME_PTR f, ptrdiff_t id)
{
Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
void
x_destroy_all_bitmaps (Display_Info *dpyinfo)
{
- int i;
+ ptrdiff_t i;
Bitmap_Record *bm = dpyinfo->bitmaps;
for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++)
It's nicer with some borders in this context */
int
-x_create_bitmap_mask (struct frame *f, int id)
+x_create_bitmap_mask (struct frame *f, ptrdiff_t id)
{
Pixmap pixmap, mask;
XImagePtr ximg, mask_img;
static struct image_type *image_types;
-/* Cache for delayed-loading image types. */
-
-static Lisp_Object Vimage_type_cache;
-
/* The symbol `xbm' which is used as the type symbol for XBM images. */
static Lisp_Object Qxbm;
/* Keywords. */
Lisp_Object QCascent, QCmargin, QCrelief;
-static Lisp_Object Qcount, Qextension_data;
Lisp_Object QCconversion;
static Lisp_Object QCheuristic_mask;
static Lisp_Object QCcolor_symbols;
/* Other symbols. */
+static Lisp_Object Qcount, Qextension_data, Qdelay;
static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
/* Function prototypes. */
static void x_emboss (struct frame *, struct image *);
static int x_build_heuristic_mask (struct frame *, struct image *,
Lisp_Object);
-
+#ifdef HAVE_NTGUI
#define CACHE_IMAGE_TYPE(type, status) \
- do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
+ do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
+#else
+#define CACHE_IMAGE_TYPE(type, status)
+#endif
#define ADD_IMAGE_TYPE(type) \
do { Vimage_types = Fcons (type, Vimage_types); } while (0)
/* 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 *
+static inline struct image_type *
lookup_image_type (Lisp_Object symbol)
{
struct image_type *type;
IMAGE_STRING_OR_NIL_VALUE,
IMAGE_SYMBOL_VALUE,
IMAGE_POSITIVE_INTEGER_VALUE,
- IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
+ IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR,
IMAGE_NON_NEGATIVE_INTEGER_VALUE,
IMAGE_ASCENT_VALUE,
IMAGE_INTEGER_VALUE,
break;
case IMAGE_POSITIVE_INTEGER_VALUE:
- if (!INTEGERP (value) || XINT (value) <= 0)
+ if (! RANGED_INTEGERP (1, value, INT_MAX))
return 0;
break;
- case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
- if (INTEGERP (value) && XINT (value) >= 0)
+ case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR:
+ if (RANGED_INTEGERP (0, value, INT_MAX))
break;
if (CONSP (value)
- && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
- && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
+ && RANGED_INTEGERP (0, XCAR (value), INT_MAX)
+ && RANGED_INTEGERP (0, XCDR (value), INT_MAX))
break;
return 0;
case IMAGE_ASCENT_VALUE:
if (SYMBOLP (value) && EQ (value, Qcenter))
break;
- else if (INTEGERP (value)
- && XINT (value) >= 0
- && XINT (value) <= 100)
+ else if (RANGED_INTEGERP (0, value, 100))
break;
return 0;
case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
+ /* Unlike the other integer-related cases, this one does not
+ verify that VALUE fits in 'int'. This is because callers
+ want EMACS_INT. */
if (!INTEGERP (value) || XINT (value) < 0)
return 0;
break;
break;
case IMAGE_INTEGER_VALUE:
- if (!INTEGERP (value))
+ if (! TYPE_RANGED_INTEGERP (int, value))
return 0;
break;
if (valid_image_p (spec))
{
struct frame *f = check_x_frame (frame);
- int id = lookup_image (f, spec);
+ ptrdiff_t id = lookup_image (f, spec);
struct image *img = IMAGE_FROM_ID (f, id);
int width = img->width + 2 * img->hmargin;
int height = img->height + 2 * img->vmargin;
if (valid_image_p (spec))
{
struct frame *f = check_x_frame (frame);
- int id = lookup_image (f, spec);
+ ptrdiff_t id = lookup_image (f, spec);
struct image *img = IMAGE_FROM_ID (f, id);
if (img->mask)
mask = Qt;
if (valid_image_p (spec))
{
struct frame *f = check_x_frame (frame);
- int id = lookup_image (f, spec);
+ ptrdiff_t id = lookup_image (f, spec);
struct image *img = IMAGE_FROM_ID (f, id);
- ext = img->data.lisp_val;
+ ext = img->lisp_data;
}
return ext;
Image type independent image structures
***********************************************************************/
-static struct image *make_image (Lisp_Object spec, unsigned hash);
static void free_image (struct frame *f, struct image *img);
-static int check_image_size (struct frame *f, int width, int height);
#define MAX_IMAGE_SIZE 6.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, unsigned int hash)
+make_image (Lisp_Object spec, EMACS_UINT hash)
{
struct image *img = (struct image *) xmalloc (sizeof *img);
Lisp_Object file = image_spec_value (spec, QCfile, NULL);
img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
xassert (img->type != NULL);
img->spec = spec;
- img->data.lisp_val = Qnil;
+ img->lisp_data = Qnil;
img->ascent = DEFAULT_IMAGE_ASCENT;
img->hash = hash;
img->corners[BOT_CORNER] = -1; /* Full image */
/* Return 1 if the given widths and heights are valid for display;
otherwise, return 0. */
-int
+static int
check_image_size (struct frame *f, int width, int height)
{
int w, h;
return 0;
if (INTEGERP (Vmax_image_size))
- w = h = XINT (Vmax_image_size);
+ return (width <= XINT (Vmax_image_size)
+ && height <= XINT (Vmax_image_size));
else if (FLOATP (Vmax_image_size))
{
if (f != NULL)
}
else
w = h = 1024; /* Arbitrary size for unknown frame. */
- w = (int) (XFLOAT_DATA (Vmax_image_size) * w);
- h = (int) (XFLOAT_DATA (Vmax_image_size) * h);
+ return (width <= XFLOAT_DATA (Vmax_image_size) * w
+ && height <= XFLOAT_DATA (Vmax_image_size) * h);
}
else
return 1;
-
- return (width <= w && height <= h);
}
/* Prepare image IMG for display on frame F. Must be called before
because a typical font is `top-heavy' (due to the presence
uppercase letters), so the image placement should err towards
being top-heavy too. It also just generally looks better. */
- ascent = (height + FONT_BASE(face->font)
- - FONT_DESCENT(face->font) + 1) / 2;
+ ascent = (height + FONT_BASE (face->font)
+ - FONT_DESCENT (face->font) + 1) / 2;
#endif /* HAVE_NTGUI */
}
else
ascent = height / 2;
}
else
- ascent = (int) (height * img->ascent / 100.0);
+ ascent = height * (img->ascent / 100.0);
return ascent;
}
xassert (STRINGP (color_name));
- if (x_defined_color (f, SSDATA (color_name), &color, 1))
+ if (x_defined_color (f, SSDATA (color_name), &color, 1)
+ && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors,
+ INT_MAX))
{
/* This isn't called frequently so we get away with simply
reallocating the color vector to the needed size, here. */
- ++img->ncolors;
+ ptrdiff_t ncolors = img->ncolors + 1;
img->colors =
(unsigned long *) xrealloc (img->colors,
- img->ncolors * sizeof *img->colors);
- img->colors[img->ncolors - 1] = color.pixel;
+ ncolors * sizeof *img->colors);
+ img->colors[ncolors - 1] = color.pixel;
+ img->ncolors = ncolors;
result = color.pixel;
}
else
Image Cache
***********************************************************************/
-static struct image *search_image_cache (struct frame *, Lisp_Object, unsigned);
static void cache_image (struct frame *f, struct image *img);
static void postprocess_image (struct frame *, struct image *);
int size;
memset (c, 0, sizeof *c);
- c->size = 50;
- c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
+ size = 50;
+ c->images = (struct image **) 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);
/* Find an image matching SPEC in the cache, and return it. If no
image is found, return NULL. */
static struct image *
-search_image_cache (struct frame *f, Lisp_Object spec, unsigned int hash)
+search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash)
{
struct image *img;
struct image_cache *c = FRAME_IMAGE_CACHE (f);
struct image_cache *c = FRAME_IMAGE_CACHE (f);
if (c)
{
- int i;
+ ptrdiff_t i;
/* Cache should not be referenced by any frame when freed. */
xassert (c->refcount == 0);
if (c)
{
- int i, nfreed = 0;
+ ptrdiff_t i, nfreed = 0;
/* Block input so that we won't be interrupted by a SIGIO
while being in an inconsistent state. */
{
/* Free cache based on timestamp. */
EMACS_TIME t;
- unsigned long old;
- int delay, nimages = 0;
+ double old, delay;
+ ptrdiff_t nimages = 0;
for (i = 0; i < c->used; ++i)
if (c->images[i])
/* If the number of cached images has grown unusually large,
decrease the cache eviction delay (Bug#6230). */
- delay = XFASTINT (Vimage_cache_eviction_delay);
+ delay = XINT (Vimage_cache_eviction_delay);
if (nimages > 40)
- delay = max (1, 1600 * delay / (nimages*nimages));
+ delay = 1600 * delay / nimages / nimages;
+ delay = max (delay, 1);
EMACS_GET_TIME (t);
old = EMACS_SECS (t) - delay;
/* Return the id of image with Lisp specification SPEC on frame F.
SPEC must be a valid Lisp image specification (see valid_image_p). */
-int
+ptrdiff_t
lookup_image (struct frame *f, Lisp_Object spec)
{
struct image *img;
- unsigned hash;
+ EMACS_UINT hash;
EMACS_TIME now;
/* F must be a window-system frame, and SPEC must be a valid image
img->ascent = CENTERED_IMAGE_ASCENT;
margin = image_spec_value (spec, QCmargin, NULL);
- if (INTEGERP (margin) && XINT (margin) >= 0)
+ if (INTEGERP (margin))
img->vmargin = img->hmargin = XFASTINT (margin);
- else if (CONSP (margin) && INTEGERP (XCAR (margin))
- && INTEGERP (XCDR (margin)))
+ else if (CONSP (margin))
{
- if (XINT (XCAR (margin)) > 0)
- img->hmargin = XFASTINT (XCAR (margin));
- if (XINT (XCDR (margin)) > 0)
- img->vmargin = XFASTINT (XCDR (margin));
+ img->hmargin = XFASTINT (XCAR (margin));
+ img->vmargin = XFASTINT (XCDR (margin));
}
relief = image_spec_value (spec, QCrelief, NULL);
cache_image (struct frame *f, struct image *img)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
- int i;
+ ptrdiff_t i;
/* Find a free slot in c->images. */
for (i = 0; i < c->used; ++i)
/* If no free slot found, maybe enlarge c->images. */
if (i == c->used && c->used == c->size)
- {
- c->size *= 2;
- c->images = (struct image **) xrealloc (c->images,
- c->size * sizeof *c->images);
- }
+ c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images);
/* Add IMG to c->images, and assign IMG an id. */
c->images[i] = img;
mark_object (img->spec);
mark_object (img->dependencies);
- if (!NILP (img->data.lisp_val))
- mark_object (img->data.lisp_val);
+ if (!NILP (img->lisp_data))
+ mark_object (img->lisp_data);
}
{
if (c)
{
- int i;
+ ptrdiff_t i;
for (i = 0; i < c->used; ++i)
if (c->images[i])
mark_image (c->images[i]);
#ifdef HAVE_NTGUI
/* Macro for defining functions that will be loaded from image DLLs. */
-#define DEF_IMGLIB_FN(rettype,func,args) rettype (FAR CDECL *fn_##func)args
+#define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
/* Macro for loading those image functions from the library. */
#define LOAD_IMGLIB_FN(lib,func) { \
if (!fn_##func) return 0; \
}
-/* Load a DLL implementing an image type.
- The argument LIBRARIES is usually the variable
- `dynamic-library-alist', which associates a symbol, identifying
- an external DLL library, to a list of possible filenames.
- The function returns NULL if no library could be loaded for
- the given symbol, or if the library was previously loaded;
- else the handle of the DLL. */
-static HMODULE
-w32_delayed_load (Lisp_Object libraries, Lisp_Object type)
-{
- HMODULE library = NULL;
-
- if (CONSP (libraries) && NILP (Fassq (type, Vimage_type_cache)))
- {
- Lisp_Object dlls = Fassq (type, libraries);
-
- if (CONSP (dlls))
- for (dlls = XCDR (dlls); CONSP (dlls); dlls = XCDR (dlls))
- {
- CHECK_STRING_CAR (dlls);
- if (library = LoadLibrary (SDATA (XCAR (dlls))))
- break;
- }
- }
-
- return library;
-}
-
#endif /* HAVE_NTGUI */
static int x_create_x_image_and_pixmap (struct frame *, int, int, int,
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
+ windowing system.
+ WIDTH and HEIGHT must both be positive.
+ If XIMG is null, assume it is a bitmap. */
+static int
+x_check_image_size (XImagePtr ximg, int width, int height)
+{
+#ifdef HAVE_X_WINDOWS
+ /* Respect Xlib's limits: it cannot deal with images that have more
+ than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
+ of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
+ enum
+ {
+ XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX),
+ X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX))
+ };
+
+ int bitmap_pad, depth, bytes_per_line;
+ if (ximg)
+ {
+ bitmap_pad = ximg->bitmap_pad;
+ depth = ximg->depth;
+ bytes_per_line = ximg->bytes_per_line;
+ }
+ else
+ {
+ bitmap_pad = 8;
+ depth = 1;
+ bytes_per_line = (width >> 3) + ((width & 7) != 0);
+ }
+ return (width <= (INT_MAX - (bitmap_pad - 1)) / depth
+ && height <= X_IMAGE_BYTES_MAX / bytes_per_line);
+#else
+ /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
+ For now, assume that every image size is allowed on these systems. */
+ return 1;
+#endif
+}
/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
return 0;
}
+ if (! x_check_image_size (*ximg, width, height))
+ {
+ x_destroy_x_image (*ximg);
+ *ximg = NULL;
+ image_error ("Image too large (%dx%d)",
+ make_number (width), make_number (height));
+ return 0;
+ }
+
/* Allocate image raster. */
(*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
/* Bitmaps with a depth less than 16 need a palette. */
/* BITMAPINFO structure already contains the first RGBQUAD. */
if (depth < 16)
- palette_colors = 1 << depth - 1;
+ palette_colors = 1 << (depth - 1);
*ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
- if (*ximg == NULL)
- {
- image_error ("Unable to allocate memory for XImage", Qnil, Qnil);
- return 0;
- }
header = &(*ximg)->info.bmiHeader;
memset (&(*ximg)->info, 0, sizeof (BITMAPINFO));
DWORD err = GetLastError ();
Lisp_Object errcode;
/* All system errors are < 10000, so the following is safe. */
- XSETINT (errcode, (int) err);
+ XSETINT (errcode, err);
image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
x_destroy_x_image (*ximg);
return 0;
File Handling
***********************************************************************/
-static unsigned char *slurp_file (char *, int *);
-
-
/* Find image file FILE. Look in data-directory/images, then
x-bitmap-file-path. Value is the encoded full name of the file
found, or nil if not found. */
occurred. *SIZE is set to the size of the file. */
static unsigned char *
-slurp_file (char *file, int *size)
+slurp_file (char *file, ptrdiff_t *size)
{
FILE *fp = NULL;
unsigned char *buf = NULL;
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),
fread (buf, 1, st.st_size, fp) == st.st_size))
{
{":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
{":background", IMAGE_STRING_OR_NIL_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
data. */
if (VECTORP (data))
{
- int i;
+ EMACS_INT i;
/* Number of elements of the vector must be >= height. */
- if (XVECTOR (data)->size < height)
+ if (ASIZE (data) < height)
return 0;
/* Each string or bool-vector in data must be large enough
}
else if (BOOL_VECTOR_P (data))
{
- if (XBOOL_VECTOR (data)->size < width * height)
+ if (XBOOL_VECTOR (data)->size / height < width)
return 0;
}
else
img->pixmap = ns_image_from_XBM (data, img->width, img->height);
#else
- img->pixmap
- = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
+ img->pixmap =
+ (x_check_image_size (0, img->width, img->height)
+ ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
FRAME_X_WINDOW (f),
data,
img->width, img->height,
fg, bg,
- DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
+ DefaultDepthOfScreen (FRAME_X_SCREEN (f)))
+ : NO_PIXMAP);
#endif /* !HAVE_NTGUI && !HAVE_NS */
}
expect ('=');
expect ('{');
+ if (! x_check_image_size (0, *width, *height))
+ {
+ if (!inhibit_image_error)
+ image_error ("Image too large (%dx%d)",
+ make_number (*width), make_number (*height));
+ goto failure;
+ }
bytes_per_line = (*width + 7) / 8 + padding_p;
nbytes = bytes_per_line * *height;
p = *data = (char *) xmalloc (nbytes);
{
Lisp_Object file;
unsigned char *contents;
- int size;
+ ptrdiff_t size;
file = x_find_image_file (file_name);
if (!STRINGP (file))
}
success_p = xbm_load_image (f, img, contents, contents + size);
+ xfree (contents);
}
else
{
img->width = XFASTINT (fmt[XBM_WIDTH].value);
img->height = XFASTINT (fmt[XBM_HEIGHT].value);
xassert (img->width > 0 && img->height > 0);
+ if (!check_image_size (f, img->width, img->height))
+ {
+ image_error ("Invalid image size (see `max-image-size')",
+ Qnil, Qnil);
+ return 0;
+ }
}
/* Get foreground and background colors, maybe allocate colors. */
#endif
/* Create the pixmap. */
- Create_Pixmap_From_Bitmap_Data (f, img, bits,
- foreground, background,
- non_default_colors);
+ if (x_check_image_size (0, img->width, img->height))
+ Create_Pixmap_From_Bitmap_Data (f, img, bits,
+ foreground, background,
+ non_default_colors);
+ else
+ img->pixmap = NO_PIXMAP;
+
if (img->pixmap)
success_p = 1;
else
{":file", IMAGE_STRING_VALUE, 0},
{":data", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
static int
xpm_color_bucket (char *color_name)
{
- unsigned h = 0;
- char *s;
-
- for (s = color_name; *s; ++s)
- h = (h << 2) ^ *s;
- return h %= XPM_COLOR_CACHE_BUCKETS;
+ EMACS_UINT hash = hash_string (color_name, strlen (color_name));
+ return hash % XPM_COLOR_CACHE_BUCKETS;
}
if (bucket < 0)
bucket = xpm_color_bucket (color_name);
- nbytes = sizeof *p + strlen (color_name);
+ nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1;
p = (struct xpm_cached_color *) xmalloc (nbytes);
strcpy (p->name, color_name);
p->color = *color;
#endif /* HAVE_XPM || HAVE_NS */
#if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
-int
+ptrdiff_t
x_create_bitmap_from_xpm_data (struct frame *f, const char **bits)
{
Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
- int id, rc;
+ ptrdiff_t id;
+ int rc;
XpmAttributes attrs;
Pixmap bitmap, mask;
#endif /* HAVE_NTGUI */
/* Remember allocated colors. */
+ img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors);
img->ncolors = attrs.nalloc_pixels;
- img->colors = (unsigned long *) xmalloc (img->ncolors
- * sizeof *img->colors);
for (i = 0; i < attrs.nalloc_pixels; ++i)
{
img->colors[i] = attrs.alloc_pixels[i];
/* XPM support functions for NS where libxpm is not available.
Only XPM version 3 (without any extensions) is supported. */
-static int xpm_scan (const unsigned char **, const unsigned char *,
- const unsigned char **, int *);
-static Lisp_Object xpm_make_color_table_v
- (void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object),
- Lisp_Object (**) (Lisp_Object, const unsigned char *, int));
static void xpm_put_color_table_v (Lisp_Object, const unsigned char *,
int, Lisp_Object);
static Lisp_Object xpm_get_color_table_v (Lisp_Object,
const unsigned char *, int);
-static Lisp_Object xpm_make_color_table_h
- (void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object),
- Lisp_Object (**) (Lisp_Object, const unsigned char *, int));
static void xpm_put_color_table_h (Lisp_Object, const unsigned char *,
int, Lisp_Object);
static Lisp_Object xpm_get_color_table_h (Lisp_Object,
const unsigned char *, int);
-static int xpm_str_to_color_key (const char *);
-static int xpm_load_image (struct frame *, struct image *,
- const unsigned char *, const unsigned char *);
/* Tokens returned from xpm_scan. */
xpm_scan (const unsigned char **s,
const unsigned char *end,
const unsigned char **beg,
- int *len)
+ ptrdiff_t *len)
{
int c;
Lisp_Object color)
{
struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
- unsigned hash_code;
+ EMACS_UINT hash_code;
Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
hash_lookup (table, chars, &hash_code);
int chars_len)
{
struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
- int i = hash_lookup (table, make_unibyte_string (chars_start, chars_len),
- NULL);
+ ptrdiff_t i =
+ hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL);
return i >= 0 ? HASH_VALUE (table, i) : Qnil;
}
unsigned char buffer[BUFSIZ];
int width, height, x, y;
int num_colors, chars_per_pixel;
- int len, LA1;
+ ptrdiff_t len;
+ int LA1;
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;
goto failure;
}
+ if (!x_create_x_image_and_pixmap (f, width, height, 0,
+ &ximg, &img->pixmap)
+#ifndef HAVE_NS
+ || !x_create_x_image_and_pixmap (f, width, height, 1,
+ &mask_img, &img->mask)
+#endif
+ )
+ {
+ image_error ("Image too large", Qnil, Qnil);
+ goto failure;
+ }
+
expect (',');
XSETFRAME (frame, f);
expect (',');
}
- if (!x_create_x_image_and_pixmap (f, width, height, 0,
- &ximg, &img->pixmap)
-#ifndef HAVE_NS
- || !x_create_x_image_and_pixmap (f, width, height, 1,
- &mask_img, &img->mask)
-#endif
- )
- {
- image_error ("Out of memory (%s)", img->spec, Qnil);
- goto error;
- }
-
for (y = 0; y < height; y++)
{
expect (XPM_TK_STRING);
{
Lisp_Object file;
unsigned char *contents;
- int size;
+ ptrdiff_t size;
file = x_find_image_file (file_name);
if (!STRINGP (file))
/* Number of entries in the color table. */
static int ct_colors_allocated;
+enum
+{
+ ct_colors_allocated_max =
+ min (INT_MAX,
+ min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long))
+};
/* Initialize the color table. */
XColor color;
Colormap cmap;
int rc;
+#else
+ COLORREF color;
+#endif
+ if (ct_colors_allocated_max <= ct_colors_allocated)
+ return FRAME_FOREGROUND_PIXEL (f);
+
+#ifdef HAVE_X_WINDOWS
color.red = r;
color.green = g;
color.blue = b;
return FRAME_FOREGROUND_PIXEL (f);
#else
- COLORREF color;
#ifdef HAVE_NTGUI
color = PALETTERGB (r, g, b);
#else
Colormap cmap;
int rc;
+ if (ct_colors_allocated_max <= ct_colors_allocated)
+ return FRAME_FOREGROUND_PIXEL (f);
+
#ifdef HAVE_X_WINDOWS
cmap = FRAME_X_COLORMAP (f);
color.pixel = pixel;
HGDIOBJ prev;
#endif /* HAVE_NTGUI */
- colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
+ 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);
#ifndef HAVE_NTGUI
/* Get the X image IMG->pixmap. */
#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
- new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
+ 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);
for (y = 0; y < img->height; ++y)
{
{":file", IMAGE_STRING_VALUE, 0},
{":data", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
occurred. *SIZE is set to the size of the file. */
static char *
-pbm_read_file (file, size)
- Lisp_Object file;
- int *size;
+pbm_read_file (Lisp_Object file, int *size)
{
FILE *fp = NULL;
char *buf = NULL;
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),
fread (buf, 1, st.st_size, fp) == st.st_size))
{
enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
unsigned char *contents = NULL;
unsigned char *end, *p;
- int size;
+ ptrdiff_t size;
specified_file = image_spec_value (img->spec, QCfile, NULL);
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{
HMODULE library;
- /* Try loading libpng under probable names. */
if (!(library = w32_delayed_load (libraries, Qpng)))
return 0;
/* 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
struct png_memory_storage
{
unsigned char *bytes; /* The data */
- size_t len; /* How big is it? */
- int index; /* Where are we? */
+ ptrdiff_t len; /* How big is it? */
+ ptrdiff_t index; /* Where are we? */
};
{
Lisp_Object file, specified_file;
Lisp_Object specified_data;
- int x, y, i;
+ int x, y;
+ ptrdiff_t i;
XImagePtr ximg, mask_img = NULL;
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL, *end_info = NULL;
fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
- if (!check_image_size (f, width, height))
+ if (! (width <= INT_MAX && height <= INT_MAX
+ && check_image_size (f, width, height)))
{
image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
goto error;
}
+
+ /* Create the X image and pixmap now, so that the work below can be
+ omitted if the image is too large for X. */
+ if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
+ &img->pixmap))
+ goto error;
+
/* If image contains simply transparency data, we prefer to
construct a clipping mask. */
if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr);
/* Allocate memory for the image. */
- pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
+ 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);
for (i = 0; i < height; ++i)
rows[i] = pixels + i * row_bytes;
fp = NULL;
}
- /* Create the X image and pixmap. */
- if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
- &img->pixmap))
- goto error;
-
/* Create an image and pixmap serving as mask if the PNG image
contains an alpha channel. */
if (channels == 4
for (x = 0; x < width; ++x)
{
- unsigned r, g, b;
+ int r, g, b;
r = *p++ << 8;
g = *p++ << 8;
static int
png_load (struct frame *f, struct image *img)
{
- return ns_load_image(f, img,
- image_spec_value (img->spec, QCfile, NULL),
- image_spec_value (img->spec, QCdata, NULL));
+ return ns_load_image (f, img,
+ image_spec_value (img->spec, QCfile, NULL),
+ image_spec_value (img->spec, QCdata, NULL));
}
#endif /* HAVE_NS */
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
#else
-#define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
+#define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
#define fn_jpeg_start_decompress jpeg_start_decompress
#define fn_jpeg_finish_decompress jpeg_finish_decompress
#define fn_jpeg_destroy_decompress jpeg_destroy_decompress
src = (struct jpeg_stdio_mgr *) cinfo->src;
if (!src->finished)
{
- size_t bytes;
+ ptrdiff_t bytes;
bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file);
if (bytes > 0)
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
typedef struct
{
unsigned char *bytes;
- size_t len;
- int index;
+ ptrdiff_t len;
+ ptrdiff_t index;
}
tiff_memory_source;
-static size_t
+static tsize_t
tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size)
{
tiff_memory_source *src = (tiff_memory_source *) data;
- if (size > src->len - src->index)
- return (size_t) -1;
+ size = min (size, src->len - src->index);
memcpy (buf, src->bytes + src->index, size);
src->index += size;
return size;
}
-static size_t
+static tsize_t
tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size)
{
- return (size_t) -1;
+ return -1;
}
static toff_t
tiff_seek_in_memory (thandle_t data, toff_t off, int whence)
{
tiff_memory_source *src = (tiff_memory_source *) data;
- int idx;
+ ptrdiff_t idx;
switch (whence)
{
return ((tiff_memory_source *) data)->len;
}
+/* GCC 3.x on x86 Windows targets has a bug that triggers an internal
+ compiler error compiling tiff_handler, see Bugzilla bug #17406
+ (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
+ this function as external works around that problem. */
+#if defined (__MINGW32__) && __GNUC__ == 3
+# define MINGW_STATIC
+#else
+# define MINGW_STATIC static
+#endif
+
+MINGW_STATIC void
+tiff_handler (const char *, const char *, const char *, va_list)
+ ATTRIBUTE_FORMAT_PRINTF (3, 0);
+MINGW_STATIC void
+tiff_handler (const char *log_format, const char *title,
+ const char *format, va_list ap)
+{
+ /* doprnt is not suitable here, as TIFF handlers are called from
+ libtiff and are passed arbitrary printf directives. Instead, use
+ vsnprintf, taking care to be portable to nonstandard environments
+ where vsnprintf returns -1 on buffer overflow. Since it's just a
+ log entry, it's OK to truncate it. */
+ char buf[4000];
+ int len = vsnprintf (buf, sizeof buf, format, ap);
+ add_to_log (log_format, build_string (title),
+ make_string (buf, max (0, min (len, sizeof buf - 1))));
+}
+#undef MINGW_STATIC
static void tiff_error_handler (const char *, const char *, va_list)
ATTRIBUTE_FORMAT_PRINTF (2, 0);
static void
tiff_error_handler (const char *title, const char *format, va_list ap)
{
- char buf[512];
- int len;
-
- len = sprintf (buf, "TIFF error: %s ", title);
- vsprintf (buf + len, format, ap);
- add_to_log (buf, Qnil, Qnil);
+ tiff_handler ("TIFF error: %s %s", title, format, ap);
}
static void
tiff_warning_handler (const char *title, const char *format, va_list ap)
{
- char buf[512];
- int len;
-
- len = sprintf (buf, "TIFF warning: %s ", title);
- vsprintf (buf + len, format, ap);
- add_to_log (buf, Qnil, Qnil);
+ tiff_handler ("TIFF warning: %s %s", title, format, ap);
}
memsrc.index = 0;
tiff = fn_TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc,
- (TIFFReadWriteProc) tiff_read_from_memory,
- (TIFFReadWriteProc) tiff_write_from_memory,
+ tiff_read_from_memory,
+ tiff_write_from_memory,
tiff_seek_in_memory,
tiff_close_memory,
tiff_size_of_memory,
image = image_spec_value (img->spec, QCindex, NULL);
if (INTEGERP (image))
{
- int ino = XFASTINT (image);
- if (!fn_TIFFSetDirectory (tiff, ino))
+ EMACS_INT ino = XFASTINT (image);
+ if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
+ && fn_TIFFSetDirectory (tiff, ino)))
{
image_error ("Invalid image number `%s' in image `%s'",
image, img->spec);
return 0;
}
- buf = (uint32 *) xmalloc (width * height * sizeof *buf);
+ /* Create the X image and pixmap. */
+ if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width
+ && x_create_x_image_and_pixmap (f, width, height, 0,
+ &ximg, &img->pixmap)))
+ {
+ fn_TIFFClose (tiff);
+ return 0;
+ }
+
+ buf = (uint32 *) xmalloc (sizeof *buf * width * height);
rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0);
continue;
if (count > 1)
- img->data.lisp_val = Fcons (Qcount,
- Fcons (make_number (count),
- img->data.lisp_val));
+ img->lisp_data = Fcons (Qcount,
+ Fcons (make_number (count),
+ img->lisp_data));
fn_TIFFClose (tiff);
if (!rc)
return 0;
}
- /* Create the X image and pixmap. */
- if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
- {
- xfree (buf);
- return 0;
- }
-
/* Initialize the color table. */
init_color_table ();
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
static void
gif_clear_image (struct frame *f, struct image *img)
{
- /* IMG->data.ptr_val may contain metadata with extension data. */
- img->data.lisp_val = Qnil;
+ img->lisp_data = Qnil;
x_clear_image (f, img);
}
typedef struct
{
unsigned char *bytes;
- size_t len;
- int index;
+ ptrdiff_t len;
+ ptrdiff_t index;
}
gif_memory_source;
static int
gif_load (struct frame *f, struct image *img)
{
- Lisp_Object file, specified_file;
- Lisp_Object specified_data;
- int rc, width, height, x, y, i;
- boolean transparent_p = 0;
+ Lisp_Object file;
+ int rc, width, height, x, y, i, j;
XImagePtr ximg;
ColorMapObject *gif_color_map;
unsigned long pixel_colors[256];
GifFileType *gif;
- Lisp_Object image;
- int ino, image_height, image_width;
gif_memory_source memsrc;
- unsigned char *raster;
- unsigned int transparency_color_index IF_LINT (= 0);
-
- specified_file = image_spec_value (img->spec, QCfile, NULL);
- specified_data = image_spec_value (img->spec, QCdata, NULL);
+ Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
+ Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
+ Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
+ unsigned long bgcolor = 0;
+ EMACS_INT idx;
if (NILP (specified_data))
{
/* Read entire contents. */
rc = fn_DGifSlurp (gif);
- if (rc == GIF_ERROR)
+ if (rc == GIF_ERROR || gif->ImageCount <= 0)
{
image_error ("Error reading `%s'", img->spec, Qnil);
fn_DGifCloseFile (gif);
return 0;
}
- image = image_spec_value (img->spec, QCindex, NULL);
- ino = INTEGERP (image) ? XFASTINT (image) : 0;
- if (ino >= gif->ImageCount)
- {
- image_error ("Invalid image number `%s' in image `%s'",
- image, img->spec);
- fn_DGifCloseFile (gif);
- return 0;
- }
-
- for (i = 0; i < gif->SavedImages[ino].ExtensionBlockCount; i++)
- if ((gif->SavedImages[ino].ExtensionBlocks[i].Function
- == GIF_LOCAL_DESCRIPTOR_EXTENSION)
- && gif->SavedImages[ino].ExtensionBlocks[i].ByteCount == 4
- /* Transparency enabled? */
- && gif->SavedImages[ino].ExtensionBlocks[i].Bytes[0] & 1)
+ /* Which sub-image are we to display? */
+ {
+ Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL);
+ idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0;
+ if (idx < 0 || idx >= gif->ImageCount)
{
- transparent_p = 1;
- transparency_color_index
- = (unsigned char) gif->SavedImages[ino].ExtensionBlocks[i].Bytes[3];
+ image_error ("Invalid image number `%s' in image `%s'",
+ image_number, img->spec);
+ fn_DGifCloseFile (gif);
+ return 0;
}
+ }
- img->corners[TOP_CORNER] = gif->SavedImages[ino].ImageDesc.Top;
- img->corners[LEFT_CORNER] = gif->SavedImages[ino].ImageDesc.Left;
- image_height = gif->SavedImages[ino].ImageDesc.Height;
- img->corners[BOT_CORNER] = img->corners[TOP_CORNER] + image_height;
- image_width = gif->SavedImages[ino].ImageDesc.Width;
- img->corners[RIGHT_CORNER] = img->corners[LEFT_CORNER] + image_width;
+ width = img->width = gif->SWidth;
+ height = img->height = gif->SHeight;
- width = img->width = max (gif->SWidth,
- max (gif->Image.Left + gif->Image.Width,
- img->corners[RIGHT_CORNER]));
- height = img->height = max (gif->SHeight,
- max (gif->Image.Top + gif->Image.Height,
- img->corners[BOT_CORNER]));
+ img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top;
+ img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left;
+ img->corners[BOT_CORNER]
+ = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height;
+ img->corners[RIGHT_CORNER]
+ = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width;
if (!check_image_size (f, width, height))
{
return 0;
}
- /* Allocate colors. */
- gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
- if (!gif_color_map)
- gif_color_map = gif->SColorMap;
- init_color_table ();
- memset (pixel_colors, 0, sizeof pixel_colors);
-
- if (gif_color_map)
- for (i = 0; i < gif_color_map->ColorCount; ++i)
- {
- if (transparent_p && transparency_color_index == i)
- {
- Lisp_Object specified_bg
- = image_spec_value (img->spec, QCbackground, NULL);
- pixel_colors[i] = STRINGP (specified_bg)
- ? x_alloc_image_color (f, img, specified_bg,
- FRAME_BACKGROUND_PIXEL (f))
- : FRAME_BACKGROUND_PIXEL (f);
- }
- else
- {
- int r = gif_color_map->Colors[i].Red << 8;
- int g = gif_color_map->Colors[i].Green << 8;
- int b = gif_color_map->Colors[i].Blue << 8;
- pixel_colors[i] = lookup_rgb_color (f, r, g, b);
- }
- }
-
-#ifdef COLOR_TABLE_SUPPORT
- img->colors = colors_in_color_table (&img->ncolors);
- free_color_table ();
-#endif /* COLOR_TABLE_SUPPORT */
-
- /* Clear the part of the screen image that are not covered by
- the image from the GIF file. Full animated GIF support
- requires more than can be done here (see the gif89 spec,
- disposal methods). Let's simply assume that the part
- not covered by a sub-image is in the frame's background color. */
+ /* Clear the part of the screen image not covered by the image.
+ Full animated GIF support requires more here (see the gif89 spec,
+ disposal methods). Let's simply assume that the part not covered
+ by a sub-image is in the frame's background color. */
for (y = 0; y < img->corners[TOP_CORNER]; ++y)
for (x = 0; x < width; ++x)
XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
}
- /* Read the GIF image into the X image. We use a local variable
- `raster' here because RasterBits below is a char *, and invites
- problems with bytes >= 0x80. */
- raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
-
- if (gif->SavedImages[ino].ImageDesc.Interlace)
- {
- int pass;
- int row = interlace_start[0];
+ /* Read the GIF image into the X image. */
- pass = 0;
+ /* FIXME: With the current implementation, loading an animated gif
+ is quadratic in the number of animation frames, since each frame
+ is a separate struct image. We must provide a way for a single
+ gif_load call to construct and save all animation frames. */
- for (y = 0; y < image_height; y++)
+ init_color_table ();
+ if (STRINGP (specified_bg))
+ bgcolor = x_alloc_image_color (f, img, specified_bg,
+ FRAME_BACKGROUND_PIXEL (f));
+ for (j = 0; j <= idx; ++j)
+ {
+ /* We use a local variable `raster' here because RasterBits is a
+ char *, which invites problems with bytes >= 0x80. */
+ struct SavedImage *subimage = gif->SavedImages + j;
+ unsigned char *raster = (unsigned char *) subimage->RasterBits;
+ int transparency_color_index = -1;
+ int disposal = 0;
+ int subimg_width = subimage->ImageDesc.Width;
+ int subimg_height = subimage->ImageDesc.Height;
+ int subimg_top = subimage->ImageDesc.Top;
+ int subimg_left = subimage->ImageDesc.Left;
+
+ /* Find the Graphic Control Extension block for this sub-image.
+ Extract the disposal method and transparency color. */
+ for (i = 0; i < subimage->ExtensionBlockCount; i++)
{
- if (row >= image_height)
- {
- row = interlace_start[++pass];
- while (row >= image_height)
- row = interlace_start[++pass];
- }
+ ExtensionBlock *extblock = subimage->ExtensionBlocks + i;
- for (x = 0; x < image_width; x++)
+ if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION)
+ && extblock->ByteCount == 4
+ && extblock->Bytes[0] & 1)
{
- int c = raster[(y * image_width) + x];
- XPutPixel (ximg, x + img->corners[LEFT_CORNER],
- row + img->corners[TOP_CORNER], pixel_colors[c]);
+ /* From gif89a spec: 1 = "keep in place", 2 = "restore
+ to background". Treat any other value like 2. */
+ disposal = (extblock->Bytes[0] >> 2) & 7;
+ transparency_color_index = (unsigned char) extblock->Bytes[3];
+ break;
}
-
- row += interlace_increment[pass];
}
- }
- else
- {
- for (y = 0; y < image_height; ++y)
- for (x = 0; x < image_width; ++x)
+
+ /* We can't "keep in place" the first subimage. */
+ if (j == 0)
+ disposal = 2;
+
+ /* For disposal == 0, the spec says "No disposal specified. The
+ decoder is not required to take any action." In practice, it
+ seems we need to treat this like "keep in place", see e.g.
+ http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
+ if (disposal == 0)
+ disposal = 1;
+
+ /* Allocate subimage colors. */
+ memset (pixel_colors, 0, sizeof pixel_colors);
+ gif_color_map = subimage->ImageDesc.ColorMap;
+ if (!gif_color_map)
+ gif_color_map = gif->SColorMap;
+
+ if (gif_color_map)
+ for (i = 0; i < gif_color_map->ColorCount; ++i)
{
- int c = raster[y * image_width + x];
- XPutPixel (ximg, x + img->corners[LEFT_CORNER],
- y + img->corners[TOP_CORNER], pixel_colors[c]);
+ if (transparency_color_index == i)
+ pixel_colors[i] = STRINGP (specified_bg)
+ ? bgcolor : FRAME_BACKGROUND_PIXEL (f);
+ else
+ {
+ int r = gif_color_map->Colors[i].Red << 8;
+ int g = gif_color_map->Colors[i].Green << 8;
+ int b = gif_color_map->Colors[i].Blue << 8;
+ pixel_colors[i] = lookup_rgb_color (f, r, g, b);
+ }
}
+
+ /* Apply the pixel values. */
+ if (gif->SavedImages[j].ImageDesc.Interlace)
+ {
+ int row, pass;
+
+ for (y = 0, row = interlace_start[0], pass = 0;
+ y < subimg_height;
+ y++, row += interlace_increment[pass])
+ {
+ if (row >= subimg_height)
+ {
+ row = interlace_start[++pass];
+ while (row >= subimg_height)
+ row = interlace_start[++pass];
+ }
+
+ for (x = 0; x < subimg_width; x++)
+ {
+ int c = raster[y * subimg_width + x];
+ if (transparency_color_index != c || disposal != 1)
+ XPutPixel (ximg, x + subimg_left, row + subimg_top,
+ pixel_colors[c]);
+ }
+ }
+ }
+ else
+ {
+ for (y = 0; y < subimg_height; ++y)
+ for (x = 0; x < subimg_width; ++x)
+ {
+ int c = raster[y * subimg_width + x];
+ if (transparency_color_index != c || disposal != 1)
+ XPutPixel (ximg, x + subimg_left, y + subimg_top,
+ pixel_colors[c]);
+ }
+ }
}
+#ifdef COLOR_TABLE_SUPPORT
+ img->colors = colors_in_color_table (&img->ncolors);
+ free_color_table ();
+#endif /* COLOR_TABLE_SUPPORT */
+
/* Save GIF image extension data for `image-metadata'.
Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
- img->data.lisp_val = Qnil;
- if (gif->SavedImages[ino].ExtensionBlockCount > 0)
+ img->lisp_data = Qnil;
+ if (gif->SavedImages[idx].ExtensionBlockCount > 0)
{
- ExtensionBlock *ext = gif->SavedImages[ino].ExtensionBlocks;
- for (i = 0; i < gif->SavedImages[ino].ExtensionBlockCount; i++, ext++)
+ int delay = 0;
+ ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks;
+ for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++)
/* Append (... FUNCTION "BYTES") */
- img->data.lisp_val = Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
- Fcons (make_number (ext->Function),
- img->data.lisp_val));
- img->data.lisp_val = Fcons (Qextension_data,
- Fcons (Fnreverse (img->data.lisp_val),
- Qnil));
+ {
+ img->lisp_data
+ = Fcons (make_number (ext->Function),
+ Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
+ img->lisp_data));
+ if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION
+ && ext->ByteCount == 4)
+ {
+ delay = ext->Bytes[2] << CHAR_BIT;
+ delay |= ext->Bytes[1];
+ }
+ }
+ img->lisp_data = Fcons (Qextension_data,
+ Fcons (img->lisp_data, Qnil));
+ if (delay)
+ img->lisp_data
+ = Fcons (Qdelay,
+ Fcons (make_float (delay / 100.0),
+ img->lisp_data));
}
+
if (gif->ImageCount > 1)
- img->data.lisp_val = Fcons (Qcount,
- Fcons (make_number (gif->ImageCount),
- img->data.lisp_val));
+ img->lisp_data = Fcons (Qcount,
+ Fcons (make_number (gif->ImageCount),
+ img->lisp_data));
fn_DGifCloseFile (gif);
***********************************************************************/
#if defined (HAVE_IMAGEMAGICK)
-Lisp_Object Qimagemagick;
+static Lisp_Object Qimagemagick;
+
+static int imagemagick_image_p (Lisp_Object);
+static int imagemagick_load (struct frame *, struct image *);
+static void imagemagick_clear_image (struct frame *, struct image *);
/* Indices of image specification fields in imagemagick_format. */
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
};
+/* Structure describing the image type for any image handled via
+ ImageMagick. */
+
+static struct image_type imagemagick_type =
+ {
+ &Qimagemagick,
+ imagemagick_image_p,
+ imagemagick_load,
+ imagemagick_clear_image,
+ NULL
+ };
+
/* Free X resources of imagemagick image IMG which is used on frame F. */
static void
}
/* 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 <wand/MagickWand.h>
-/* imagemagick_load_image is a helper function for imagemagick_load,
- which does the actual loading given contents and size, apart from
- frame and image structures, passed from imagemagick_load.
+/* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
+ Emacs seems to work fine with the hidden version, so unhide it. */
+#include <magick/version.h>
+#if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
+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",
+ make_string (description, strlen (description)),
+ Qnil);
+ description = (char *) MagickRelinquishMemory (description);
+}
- Uses librimagemagick to do most of the image processing.
+/* 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
+ the image processing.
- Return non-zero if successful.
-*/
+ F is a pointer to the Emacs frame; IMG to the image structure to
+ prepare; CONTENTS is the string containing the IMAGEMAGICK data to
+ 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. */
static int
-imagemagick_load_image (/* Pointer to emacs frame structure. */
- struct frame *f,
- /* Pointer to emacs image structure. */
- struct image *img,
- /* String containing the IMAGEMAGICK data to
- be parsed. */
- unsigned char *contents,
- /* Size of data in bytes. */
- unsigned int size,
- /* Filename, either pass filename or
- contents/size. */
- unsigned char *filename)
-{
- unsigned long width;
- unsigned long height;
-
- MagickBooleanType
- status;
+imagemagick_load_image (struct frame *f, struct image *img,
+ unsigned char *contents, unsigned int size,
+ char *filename)
+{
+ size_t width;
+ size_t height;
+
+ MagickBooleanType status;
XImagePtr ximg;
- Lisp_Object specified_bg;
- XColor background;
int x;
int y;
MagickPixelPacket pixel;
Lisp_Object image;
Lisp_Object value;
- Lisp_Object crop, geometry;
- long ino;
+ Lisp_Object crop;
+ EMACS_INT ino;
int desired_width, desired_height;
double rotation;
- int imagemagick_rendermethod;
+ EMACS_INT imagemagick_rendermethod;
int pixelwidth;
ImageInfo *image_info;
ExceptionInfo *exception;
image = image_spec_value (img->spec, QCindex, NULL);
ino = INTEGERP (image) ? XFASTINT (image) : 0;
ping_wand = NewMagickWand ();
- MagickSetResolution (ping_wand, 2, 2);
+ /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
+
if (filename != NULL)
{
status = MagickPingImage (ping_wand, filename);
status = MagickPingImageBlob (ping_wand, contents, size);
}
- if (ino >= MagickGetNumberImages (ping_wand))
+ if (status == MagickFalse)
+ {
+ imagemagick_error (ping_wand);
+ DestroyMagickWand (ping_wand);
+ return 0;
+ }
+
+ if (! (0 <= ino && ino < MagickGetNumberImages (ping_wand)))
{
image_error ("Invalid image number `%s' in image `%s'",
image, img->spec);
return 0;
}
- if (MagickGetNumberImages(ping_wand) > 1)
- img->data.lisp_val =
+ if (MagickGetNumberImages (ping_wand) > 1)
+ img->lisp_data =
Fcons (Qcount,
Fcons (make_number (MagickGetNumberImages (ping_wand)),
- img->data.lisp_val));
+ img->lisp_data));
DestroyMagickWand (ping_wand);
- /* Now, after pinging, we know how many images are inside the
- file. If it's not a bundle, the number is one. */
+ /* Now we know how many images are inside the file. If it's not a
+ bundle, the number is one. */
if (filename != NULL)
{
im_image = ReadImage (image_info, exception);
DestroyExceptionInfo (exception);
- if (im_image != NULL)
- {
- image_wand = NewMagickWandFromImage (im_image);
- DestroyImage(im_image);
- status = MagickTrue;
- }
- else
- status = MagickFalse;
+ if (im_image == NULL)
+ goto imagemagick_no_wand;
+ image_wand = NewMagickWandFromImage (im_image);
+ DestroyImage (im_image);
}
else
{
image_wand = NewMagickWand ();
- status = MagickReadImageBlob (image_wand, contents, size);
+ if (MagickReadImageBlob (image_wand, contents, size) == MagickFalse)
+ {
+ imagemagick_error (image_wand);
+ goto imagemagick_error;
+ }
}
- if (status == MagickFalse) goto imagemagick_error;
-
/* 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
if (status == MagickFalse)
{
image_error ("Imagemagick scale failed", Qnil, Qnil);
+ imagemagick_error (image_wand);
goto imagemagick_error;
}
}
efficient. */
crop = image_spec_value (img->spec, QCcrop, NULL);
- if (CONSP (crop) && INTEGERP (XCAR (crop)))
+ if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
{
/* After some testing, it seems MagickCropImage is the fastest crop
function in ImageMagick. This crop function seems to do less copying
than the alternatives, but it still reads the entire image into memory
- before croping, which is aparently difficult to avoid when using
+ before cropping, which is apparently difficult to avoid when using
imagemagick. */
- int w, h, x, y;
- w = XFASTINT (XCAR (crop));
+ size_t crop_width = XINT (XCAR (crop));
crop = XCDR (crop);
- if (CONSP (crop) && INTEGERP (XCAR (crop)))
+ if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
{
- h = XFASTINT (XCAR (crop));
+ size_t crop_height = XINT (XCAR (crop));
crop = XCDR (crop);
- if (CONSP (crop) && INTEGERP (XCAR (crop)))
+ if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
{
- x = XFASTINT (XCAR (crop));
+ ssize_t crop_x = XINT (XCAR (crop));
crop = XCDR (crop);
- if (CONSP (crop) && INTEGERP (XCAR (crop)))
+ if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
{
- y = XFASTINT (XCAR (crop));
- MagickCropImage (image_wand, w, h, x, y);
+ ssize_t crop_y = XINT (XCAR (crop));
+ MagickCropImage (image_wand, crop_width, crop_height,
+ crop_x, crop_y);
}
}
}
if (status == MagickFalse)
{
image_error ("Imagemagick image rotate failed", Qnil, Qnil);
+ imagemagick_error (image_wand);
goto imagemagick_error;
}
}
- /* Finaly we are done manipulating the image, figure out resulting
- width, height, and then transfer ownerwship to Emacs. */
+ /* Finally we are done manipulating the image. Figure out the
+ resulting width/height and transfer ownership to Emacs. */
height = MagickGetImageHeight (image_wand);
width = MagickGetImageWidth (image_wand);
- if (! check_image_size (f, width, height))
+ if (! (width <= INT_MAX && height <= INT_MAX
+ && check_image_size (f, width, height)))
{
image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
goto imagemagick_error;
init_color_table ();
imagemagick_rendermethod = (INTEGERP (Vimagemagick_render_type)
- ? XFASTINT (Vimagemagick_render_type) : 0);
+ ? XINT (Vimagemagick_render_type) : 0);
if (imagemagick_rendermethod == 0)
{
+ size_t image_height;
+
/* Try to create a x pixmap to hold the imagemagick pixmap. */
if (!x_create_x_image_and_pixmap (f, width, height, 0,
&ximg, &img->pixmap))
#ifdef COLOR_TABLE_SUPPORT
free_color_table ();
#endif
- image_error("Imagemagick X bitmap allocation failure", Qnil, Qnil);
+ image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
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. */
goto imagemagick_error;
}
- for (y = 0; y < (long) MagickGetImageHeight (image_wand); y++)
+ image_height = MagickGetImageHeight (image_wand);
+ for (y = 0; y < image_height; y++)
{
pixels = PixelGetNextIteratorRow (iterator, &width);
if (pixels == (PixelWand **) NULL)
if (imagemagick_rendermethod == 1)
{
- /* Magicexportimage is normaly faster than pixelpushing. This
+ /* 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);*/
- char* exportdepth = imagedepth <= 8 ? "I" : "BGRP";/*"RGBP";*/
+ 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);
+ image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
goto imagemagick_error;
}
- /* Oddly, the below code doesnt seem to work:*/
+ /* Oddly, the below code doesn't seem to work:*/
/* switch(ximg->bitmap_unit){ */
/* case 8: */
/* pixelwidth=CharPixel; */
/*&(img->pixmap));*/
ximg->data);
#else
- image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
+ image_error ("You don't have MagickExportImagePixels, upgrade ImageMagick!",
Qnil, Qnil);
#endif
}
imagemagick_error:
DestroyMagickWand (image_wand);
+ imagemagick_no_wand:
MagickWandTerminus ();
/* TODO more cleanup. */
image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil);
the prototype thus needs to be compatible with that structure. */
static int
-imagemagick_load (struct frame *f,
- struct image *img)
+imagemagick_load (struct frame *f, struct image *img)
{
int success_p = 0;
Lisp_Object file_name;
image_error ("Cannot find image file `%s'", file_name, Qnil);
return 0;
}
- success_p = imagemagick_load_image (f, img, 0, 0, SDATA (file));
+ success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file));
}
/* Else its not a file, its a lisp object. Load the image from a
lisp object rather than a file. */
return success_p;
}
-/* Structure describing the image type `imagemagick'. 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 imagemagick_type =
- {
- /* An identifier showing that this is an image structure for the
- IMAGEMAGICK format. */
- &Qimagemagick,
- /* Handle to a function that can be used to identify a IMAGEMAGICK
- file. */
- imagemagick_image_p,
- /* Handle to function used to load a IMAGEMAGICK file. */
- imagemagick_load,
- /* Handle to function to free resources for IMAGEMAGICK. */
- imagemagick_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. */
- NULL
- };
-
-
DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0,
- doc: /* Return the image types supported by ImageMagick.
-Note that ImageMagick recognizes many file-types that Emacs does not recognize
-as images, such as .c. */)
+ doc: /* Return a list of image types supported by ImageMagick.
+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).
+
+Note that ImageMagick recognizes many file-types that Emacs does not
+recognize as images, such as C. See `imagemagick-types-inhibit'. */)
(void)
{
Lisp_Object typelist = Qnil;
- unsigned long numf;
+ size_t numf = 0;
ExceptionInfo ex;
char **imtypes = GetMagickList ("*", &numf, &ex);
- int i;
+ size_t i;
Lisp_Object Qimagemagicktype;
for (i = 0; i < numf; i++)
{
Qimagemagicktype = intern (imtypes[i]);
typelist = Fcons (Qimagemagicktype, typelist);
}
- return typelist;
+ return Fnreverse (typelist);
}
#endif /* defined (HAVE_IMAGEMAGICK) */
static int svg_load (struct frame *f, struct image *img);
static int svg_load_image (struct frame *, struct image *,
- unsigned char *, unsigned int);
+ unsigned char *, ptrdiff_t);
/* The symbol `svg' identifying images of this type. */
-Lisp_Object Qsvg;
+static Lisp_Object Qsvg;
/* Indices of image specification fields in svg_format, below. */
{":data", IMAGE_STRING_VALUE, 0},
{":file", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
DEF_IMGLIB_FN (gboolean, rsvg_handle_write);
DEF_IMGLIB_FN (gboolean, rsvg_handle_close);
DEF_IMGLIB_FN (GdkPixbuf *, rsvg_handle_get_pixbuf);
-DEF_IMGLIB_FN (void, rsvg_handle_free);
DEF_IMGLIB_FN (int, gdk_pixbuf_get_width);
DEF_IMGLIB_FN (int, gdk_pixbuf_get_height);
LOAD_IMGLIB_FN (library, rsvg_handle_write);
LOAD_IMGLIB_FN (library, rsvg_handle_close);
LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf);
- LOAD_IMGLIB_FN (library, rsvg_handle_free);
LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width);
LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height);
#define fn_rsvg_handle_write rsvg_handle_write
#define fn_rsvg_handle_close rsvg_handle_close
#define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
-#define fn_rsvg_handle_free rsvg_handle_free
#define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
#define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
{
Lisp_Object file;
unsigned char *contents;
- int size;
+ ptrdiff_t size;
file = x_find_image_file (file_name);
if (!STRINGP (file))
}
/* Read the entire file into memory. */
- contents = slurp_file (SDATA (file), &size);
+ contents = slurp_file (SSDATA (file), &size);
if (contents == NULL)
{
image_error ("Error loading SVG image `%s'", img->spec, Qnil);
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. */
- unsigned int size) /* Size of data in bytes. */
+ ptrdiff_t size) /* Size of data in bytes. */
{
RsvgHandle *rsvg_handle;
RsvgDimensionData dimension_data;
- GError *error = NULL;
+ GError *err = NULL;
GdkPixbuf *pixbuf;
int width;
int height;
rsvg_handle = fn_rsvg_handle_new ();
/* Parse the contents argument and fill in the rsvg_handle. */
- fn_rsvg_handle_write (rsvg_handle, contents, size, &error);
- if (error) goto rsvg_error;
+ fn_rsvg_handle_write (rsvg_handle, contents, size, &err);
+ if (err) goto rsvg_error;
/* The parsing is complete, rsvg_handle is ready to used, close it
for further writes. */
- fn_rsvg_handle_close (rsvg_handle, &error);
- if (error) goto rsvg_error;
+ fn_rsvg_handle_close (rsvg_handle, &err);
+ if (err) goto rsvg_error;
fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data);
if (! check_image_size (f, dimension_data.width, dimension_data.height))
color. */
specified_bg = image_spec_value (img->spec, QCbackground, NULL);
if (!STRINGP (specified_bg)
- || !x_defined_color (f, SDATA (specified_bg), &background, 0))
+ || !x_defined_color (f, SSDATA (specified_bg), &background, 0))
{
#ifndef HAVE_NS
background.pixel = FRAME_BACKGROUND_PIXEL (f);
{
for (x = 0; x < width; ++x)
{
- unsigned red;
- unsigned green;
- unsigned blue;
- unsigned opacity;
+ int red;
+ int green;
+ int blue;
+ int opacity;
red = *pixels++;
green = *pixels++;
/* FIXME: Use error->message so the user knows what is the actual
problem with the image. */
image_error ("Error parsing SVG image `%s'", img->spec, Qnil);
- fn_g_error_free (error);
+ fn_g_error_free (err);
return 0;
}
{":loader", IMAGE_FUNCTION_VALUE, 0},
{":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
{":ascent", IMAGE_ASCENT_VALUE, 0},
- {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
+ {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
{":relief", IMAGE_INTEGER_VALUE, 0},
{":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
static void
gs_clear_image (struct frame *f, struct image *img)
{
- /* IMG->data.ptr_val may contain a recorded colormap. */
- xfree (img->data.ptr_val);
x_clear_image (f, img);
}
}
else if (VECTORP (tem))
{
- if (XVECTOR (tem)->size != 4)
+ if (ASIZE (tem) != 4)
return 0;
for (i = 0; i < 4; ++i)
if (!INTEGERP (XVECTOR (tem)->contents[i]))
static int
gs_load (struct frame *f, struct image *img)
{
- char buffer[100];
+ uprintmax_t printnum1, printnum2;
+ char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)];
Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
Lisp_Object frame;
double in_width, in_height;
info. */
pt_width = image_spec_value (img->spec, QCpt_width, NULL);
in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0;
- img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
+ in_width *= FRAME_X_DISPLAY_INFO (f)->resx;
pt_height = image_spec_value (img->spec, QCpt_height, NULL);
in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0;
- img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
+ in_height *= FRAME_X_DISPLAY_INFO (f)->resy;
- if (!check_image_size (f, img->width, img->height))
+ if (! (in_width <= INT_MAX && in_height <= INT_MAX
+ && check_image_size (f, in_width, in_height)))
{
image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
return 0;
}
+ img->width = in_width;
+ img->height = in_height;
/* Create the pixmap. */
xassert (img->pixmap == NO_PIXMAP);
- /* Only W32 version did BLOCK_INPUT here. ++kfs */
- BLOCK_INPUT;
- img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
- img->width, img->height,
- DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
- UNBLOCK_INPUT;
+ if (x_check_image_size (0, img->width, img->height))
+ {
+ /* Only W32 version did BLOCK_INPUT here. ++kfs */
+ BLOCK_INPUT;
+ img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
+ img->width, img->height,
+ DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
+ UNBLOCK_INPUT;
+ }
if (!img->pixmap)
{
if successful. We do not record_unwind_protect here because
other places in redisplay like calling window scroll functions
don't either. Let the Lisp loader use `unwind-protect' instead. */
- sprintf (buffer, "%lu %lu",
- (unsigned long) FRAME_X_WINDOW (f),
- (unsigned long) img->pixmap);
+ printnum1 = FRAME_X_WINDOW (f);
+ printnum2 = img->pixmap;
+ sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2);
window_and_pixmap_id = build_string (buffer);
- sprintf (buffer, "%lu %lu",
- FRAME_FOREGROUND_PIXEL (f),
- FRAME_BACKGROUND_PIXEL (f));
+ printnum1 = FRAME_FOREGROUND_PIXEL (f);
+ printnum2 = FRAME_BACKGROUND_PIXEL (f);
+ sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2);
pixel_colors = build_string (buffer);
XSETFRAME (frame, f);
if (NILP (loader))
loader = intern ("gs-load-image");
- img->data.lisp_val = call6 (loader, frame, img->spec,
- make_number (img->width),
- make_number (img->height),
- window_and_pixmap_id,
- pixel_colors);
- return PROCESSP (img->data.lisp_val);
+ img->lisp_data = call6 (loader, frame, img->spec,
+ make_number (img->width),
+ make_number (img->height),
+ window_and_pixmap_id,
+ pixel_colors);
+ return PROCESSP (img->lisp_data);
}
x_kill_gs_process (Pixmap pixmap, struct frame *f)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
- int class, i;
+ int class;
+ ptrdiff_t i;
struct image *img;
/* Find the image containing PIXMAP. */
/* 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->data.lisp_val));
- Fkill_process (img->data.lisp_val, Qnil);
- img->data.lisp_val = Qnil;
+ xassert (PROCESSP (img->lisp_data));
+ Fkill_process (img->lisp_data, Qnil);
+ img->lisp_data = Qnil;
#if defined (HAVE_X_WINDOWS)
DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
(Lisp_Object spec)
{
- int id = -1;
+ ptrdiff_t id = -1;
if (valid_image_p (spec))
id = lookup_image (SELECTED_FRAME (), spec);
of `dynamic-library-alist', which see). */)
(Lisp_Object type, Lisp_Object libraries)
{
- Lisp_Object tested;
-
+#ifdef HAVE_NTGUI
/* Don't try to reload the library. */
- tested = Fassq (type, Vimage_type_cache);
+ Lisp_Object tested = Fassq (type, Vlibrary_cache);
if (CONSP (tested))
return XCDR (tested);
+#endif
+
+ /* Types pbm and xbm are built-in and always available. */
+ if (EQ (type, Qpbm) || EQ (type, Qxbm))
+ return Qt;
#if defined (HAVE_XPM) || defined (HAVE_NS)
if (EQ (type, Qxpm))
#if defined (HAVE_IMAGEMAGICK)
if (EQ (type, Qimagemagick))
- {
- return CHECK_LIB_AVAILABLE (&imagemagick_type, init_imagemagick_functions,
- libraries);
- }
+ return CHECK_LIB_AVAILABLE (&imagemagick_type, init_imagemagick_functions,
+ libraries);
#endif
#ifdef HAVE_GHOSTSCRIPT
operation on GNU/Linux of calling dump-emacs after loading some images. */
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.
non-numeric, there is no explicit limit on the size of images. */);
Vmax_image_size = make_float (MAX_IMAGE_SIZE);
- Vimage_type_cache = Qnil;
- staticpro (&Vimage_type_cache);
-
- Qpbm = intern_c_string ("pbm");
- staticpro (&Qpbm);
+ DEFSYM (Qpbm, "pbm");
ADD_IMAGE_TYPE (Qpbm);
- Qxbm = intern_c_string ("xbm");
- staticpro (&Qxbm);
+ DEFSYM (Qxbm, "xbm");
ADD_IMAGE_TYPE (Qxbm);
define_image_type (&xbm_type, 1);
define_image_type (&pbm_type, 1);
- Qcount = intern_c_string ("count");
- staticpro (&Qcount);
- Qextension_data = intern_c_string ("extension-data");
- staticpro (&Qextension_data);
-
- QCascent = intern_c_string (":ascent");
- staticpro (&QCascent);
- QCmargin = intern_c_string (":margin");
- staticpro (&QCmargin);
- QCrelief = intern_c_string (":relief");
- staticpro (&QCrelief);
- QCconversion = intern_c_string (":conversion");
- staticpro (&QCconversion);
- QCcolor_symbols = intern_c_string (":color-symbols");
- staticpro (&QCcolor_symbols);
- QCheuristic_mask = intern_c_string (":heuristic-mask");
- staticpro (&QCheuristic_mask);
- QCindex = intern_c_string (":index");
- staticpro (&QCindex);
- QCgeometry = intern_c_string (":geometry");
- staticpro (&QCgeometry);
- QCcrop = intern_c_string (":crop");
- staticpro (&QCcrop);
- QCrotation = intern_c_string (":rotation");
- staticpro (&QCrotation);
- QCmatrix = intern_c_string (":matrix");
- staticpro (&QCmatrix);
- QCcolor_adjustment = intern_c_string (":color-adjustment");
- staticpro (&QCcolor_adjustment);
- QCmask = intern_c_string (":mask");
- staticpro (&QCmask);
-
- Qlaplace = intern_c_string ("laplace");
- staticpro (&Qlaplace);
- Qemboss = intern_c_string ("emboss");
- staticpro (&Qemboss);
- Qedge_detection = intern_c_string ("edge-detection");
- staticpro (&Qedge_detection);
- Qheuristic = intern_c_string ("heuristic");
- staticpro (&Qheuristic);
-
- Qpostscript = intern_c_string ("postscript");
- staticpro (&Qpostscript);
+ DEFSYM (Qcount, "count");
+ DEFSYM (Qextension_data, "extension-data");
+ DEFSYM (Qdelay, "delay");
+
+ DEFSYM (QCascent, ":ascent");
+ DEFSYM (QCmargin, ":margin");
+ DEFSYM (QCrelief, ":relief");
+ DEFSYM (QCconversion, ":conversion");
+ DEFSYM (QCcolor_symbols, ":color-symbols");
+ DEFSYM (QCheuristic_mask, ":heuristic-mask");
+ DEFSYM (QCindex, ":index");
+ DEFSYM (QCgeometry, ":geometry");
+ DEFSYM (QCcrop, ":crop");
+ DEFSYM (QCrotation, ":rotation");
+ DEFSYM (QCmatrix, ":matrix");
+ DEFSYM (QCcolor_adjustment, ":color-adjustment");
+ DEFSYM (QCmask, ":mask");
+
+ DEFSYM (Qlaplace, "laplace");
+ DEFSYM (Qemboss, "emboss");
+ DEFSYM (Qedge_detection, "edge-detection");
+ DEFSYM (Qheuristic, "heuristic");
+
+ DEFSYM (Qpostscript, "postscript");
#ifdef HAVE_GHOSTSCRIPT
ADD_IMAGE_TYPE (Qpostscript);
- QCloader = intern_c_string (":loader");
- staticpro (&QCloader);
- QCbounding_box = intern_c_string (":bounding-box");
- staticpro (&QCbounding_box);
- QCpt_width = intern_c_string (":pt-width");
- staticpro (&QCpt_width);
- QCpt_height = intern_c_string (":pt-height");
- staticpro (&QCpt_height);
+ DEFSYM (QCloader, ":loader");
+ DEFSYM (QCbounding_box, ":bounding-box");
+ DEFSYM (QCpt_width, ":pt-width");
+ DEFSYM (QCpt_height, ":pt-height");
#endif /* HAVE_GHOSTSCRIPT */
#ifdef HAVE_NTGUI
- Qlibpng_version = intern_c_string ("libpng-version");
- staticpro (&Qlibpng_version);
+ DEFSYM (Qlibpng_version, "libpng-version");
Fset (Qlibpng_version,
#if HAVE_PNG
make_number (PNG_LIBPNG_VER)
#endif
#if defined (HAVE_XPM) || defined (HAVE_NS)
- Qxpm = intern_c_string ("xpm");
- staticpro (&Qxpm);
+ DEFSYM (Qxpm, "xpm");
ADD_IMAGE_TYPE (Qxpm);
#endif
#if defined (HAVE_JPEG) || defined (HAVE_NS)
- Qjpeg = intern_c_string ("jpeg");
- staticpro (&Qjpeg);
+ DEFSYM (Qjpeg, "jpeg");
ADD_IMAGE_TYPE (Qjpeg);
#endif
#if defined (HAVE_TIFF) || defined (HAVE_NS)
- Qtiff = intern_c_string ("tiff");
- staticpro (&Qtiff);
+ DEFSYM (Qtiff, "tiff");
ADD_IMAGE_TYPE (Qtiff);
#endif
#if defined (HAVE_GIF) || defined (HAVE_NS)
- Qgif = intern_c_string ("gif");
- staticpro (&Qgif);
+ DEFSYM (Qgif, "gif");
ADD_IMAGE_TYPE (Qgif);
#endif
#if defined (HAVE_PNG) || defined (HAVE_NS)
- Qpng = intern_c_string ("png");
- staticpro (&Qpng);
+ DEFSYM (Qpng, "png");
ADD_IMAGE_TYPE (Qpng);
#endif
#if defined (HAVE_IMAGEMAGICK)
- Qimagemagick = intern_c_string ("imagemagick");
- staticpro (&Qimagemagick);
+ DEFSYM (Qimagemagick, "imagemagick");
ADD_IMAGE_TYPE (Qimagemagick);
#endif
#if defined (HAVE_RSVG)
- Qsvg = intern_c_string ("svg");
- staticpro (&Qsvg);
+ DEFSYM (Qsvg, "svg");
ADD_IMAGE_TYPE (Qsvg);
#ifdef HAVE_NTGUI
/* Other libraries used directly by svg code. */
- Qgdk_pixbuf = intern_c_string ("gdk-pixbuf");
- staticpro (&Qgdk_pixbuf);
- Qglib = intern_c_string ("glib");
- staticpro (&Qglib);
- Qgobject = intern_c_string ("gobject");
- staticpro (&Qgobject);
+ DEFSYM (Qgdk_pixbuf, "gdk-pixbuf");
+ DEFSYM (Qglib, "glib");
+ DEFSYM (Qgobject, "gobject");
#endif /* HAVE_NTGUI */
#endif /* HAVE_RSVG */