Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

10966 lines (9191 sloc) 299.456 kB
/* Functions for image support on window system.
Copyright (C) 1989, 1992-2015 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "sysstdio.h"
#include <unistd.h>
#ifdef HAVE_PNG
#if defined HAVE_LIBPNG_PNG_H
# include <libpng/png.h>
#else
# include <png.h>
#endif
#endif
#include <setjmp.h>
#include <c-ctype.h>
#include "lisp.h"
#include "frame.h"
#include "window.h"
#include "dispextern.h"
#include "blockinput.h"
#include "systime.h"
#include <epaths.h>
#include "character.h"
#include "coding.h"
#include "termhooks.h"
#include "font.h"
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#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;
#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define NO_PIXMAP None
#define PIX_MASK_RETAIN 0
#define PIX_MASK_DRAW 1
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
/* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
#ifdef WINDOWSNT
# include "w32.h"
#endif
/* 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 NO_PIXMAP 0
#define PIX_MASK_RETAIN 0
#define PIX_MASK_DRAW 1
#define x_defined_color w32_defined_color
#define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
/* Versions of libpng, libgif, and libjpeg that we were compiled with,
or -1 if no PNG/GIF support was compiled in. This is tested by
w32-win.el to correctly set up the alist used to search for the
respective image libraries. */
Lisp_Object Qlibpng_version, Qlibgif_version, Qlibjpeg_version;
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
#undef COLOR_TABLE_SUPPORT
typedef struct mac_bitmap_record Bitmap_Record;
#define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
#define NO_PIXMAP 0
#define PIX_MASK_DRAW 255
#define PIX_MASK_RETAIN 0
#define x_defined_color mac_defined_color
#define DefaultDepthOfScreen(screen) (one_mac_display_info.n_planes)
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
#undef COLOR_TABLE_SUPPORT
typedef struct ns_bitmap_record Bitmap_Record;
#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define NO_PIXMAP 0
#define PIX_MASK_RETAIN 0
#define PIX_MASK_DRAW 1
#define x_defined_color(f, name, color_def, alloc) \
ns_defined_color (f, name, color_def, alloc, 0)
#define DefaultDepthOfScreen(screen) x_display_list->n_planes
#endif /* HAVE_NS */
/* The symbol `postscript' identifying images of this type. */
static Lisp_Object Qpostscript;
static void x_disable_image (struct frame *, struct image *);
static void x_edge_detection (struct frame *, struct image *, Lisp_Object,
Lisp_Object);
static void init_color_table (void);
static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b);
#ifdef COLOR_TABLE_SUPPORT
static void free_color_table (void);
static unsigned long *colors_in_color_table (int *n);
#endif
static Lisp_Object QCmax_width, QCmax_height;
/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
id, which is just an int that this section returns. Bitmaps are
reference counted so they can be shared among frames.
Bitmap indices are guaranteed to be > 0, so a negative number can
be used to indicate no bitmap.
If you use x_create_bitmap_from_data, then you must keep track of
the bitmaps yourself. That is, creating a bitmap from the same
data more than once will not be caught. */
/* For Mac OS X high resolution versions of images, the actual bitmap
width/height (in pixels) is not necessarily the same as the logical
image width/height (in points). We should use the former for
bitmap or pixel-level operations especially on postprocessing.
The width and height arguments to x_create_x_image_and_pixmap
should be specified in pixels, but we don't care about the
arguments to XGetImage and x_put_x_image, because the Mac port
doesn't use these arguments. For four_corners_best, we set
img->corners using pixel coordinates rather than passing pixel
width and height as its arguments. */
#ifdef HAVE_MACGUI
#define IMAGE_BITMAP_WIDTH(img) ((img)->pixmap->width)
#define IMAGE_BITMAP_HEIGHT(img) ((img)->pixmap->height)
#else
#define IMAGE_BITMAP_WIDTH(img) ((img)->width)
#define IMAGE_BITMAP_HEIGHT(img) ((img)->height)
#endif
#ifdef HAVE_MACGUI
static void
XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel)
{
if (ximage->bits_per_pixel == 32)
((unsigned int *)(ximage->data + y * ximage->bytes_per_line))[x] = pixel;
else
((unsigned char *)(ximage->data + y * ximage->bytes_per_line))[x] = pixel;
}
static unsigned long
XGetPixel (XImagePtr ximage, int x, int y)
{
if (ximage->bits_per_pixel == 32)
return ((unsigned int *)(ximage->data + y * ximage->bytes_per_line))[x];
else
return ((unsigned char *)(ximage->data + y * ximage->bytes_per_line))[x];
}
static void
XDestroyImage (XImagePtr ximg)
{
}
static void
mac_data_provider_release_data (void *info, const void *data, size_t size)
{
xfree ((void *)data);
}
static CGImageRef
mac_create_cg_image_from_image (struct frame *f, struct image *img)
{
XImagePtr ximg = img->pixmap;
CGDataProviderRef provider;
CGImageRef result;
if (img->mask)
{
int x, y;
for (y = 0; y < ximg->height; y++)
for (x = 0; x < ximg->width; x++)
{
unsigned long color, alpha;
int dest_alpha, r, g, b;
color = XGetPixel (ximg, x, y);
alpha = XGetPixel (img->mask, x, y);
dest_alpha = 0xff - alpha;
r = RED_FROM_ULONG (color);
r = (r < dest_alpha) ? 0 : r - dest_alpha;
g = GREEN_FROM_ULONG (color);
g = (g < dest_alpha) ? 0 : g - dest_alpha;
b = BLUE_FROM_ULONG (color);
b = (b < dest_alpha) ? 0 : b - dest_alpha;
XPutPixel (ximg, x, y, ARGB_TO_ULONG (alpha, r, g, b));
}
xfree (img->mask->data);
img->mask->data = NULL;
}
block_input ();
provider = CGDataProviderCreateWithData (NULL, ximg->data,
ximg->bytes_per_line * ximg->height,
mac_data_provider_release_data);
ximg->data = NULL;
result = CGImageCreate (ximg->width, ximg->height, 8, 32,
ximg->bytes_per_line, mac_cg_color_space_rgb,
((img->mask ? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst)
| kCGBitmapByteOrder32Host),
provider, NULL, 0, kCGRenderingIntentDefault);
CGDataProviderRelease (provider);
unblock_input ();
return result;
}
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
/* Use with images created by ns_image_for_XPM. */
static unsigned long
XGetPixel (XImagePtr ximage, int x, int y)
{
return ns_get_pixel (ximage, x, y);
}
/* Use with images created by ns_image_for_XPM; alpha set to 1;
pixel is assumed to be in RGB form. */
static void
XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel)
{
ns_put_pixel (ximage, x, y, pixel);
}
#endif /* HAVE_NS */
/* Functions to access the contents of a bitmap, given an id. */
static int
x_bitmap_height (struct frame *f, ptrdiff_t id)
{
return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].height;
}
static int
x_bitmap_width (struct frame *f, ptrdiff_t id)
{
return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].width;
}
#if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
ptrdiff_t
x_bitmap_pixmap (struct frame *f, ptrdiff_t id)
{
/* HAVE_NTGUI needs the explicit cast here. */
return (ptrdiff_t) FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
}
#endif
#ifdef HAVE_X_WINDOWS
int
x_bitmap_mask (struct frame *f, ptrdiff_t id)
{
return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
}
#endif
/* Allocate a new bitmap record. Returns index of new record. */
static ptrdiff_t
x_allocate_bitmap_record (struct frame *f)
{
Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
ptrdiff_t i;
if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
return ++dpyinfo->bitmaps_last;
for (i = 0; i < dpyinfo->bitmaps_size; ++i)
if (dpyinfo->bitmaps[i].refcount == 0)
return i + 1;
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 (struct frame *f, ptrdiff_t id)
{
++FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
}
/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
ptrdiff_t
x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height)
{
Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
ptrdiff_t id;
#ifdef HAVE_X_WINDOWS
Pixmap bitmap;
bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
bits, width, height);
if (! bitmap)
return -1;
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
Pixmap bitmap;
bitmap = CreateBitmap (width, height,
FRAME_DISPLAY_INFO (XFRAME (frame))->n_planes,
FRAME_DISPLAY_INFO (XFRAME (frame))->n_cbits,
bits);
if (! bitmap)
return -1;
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
/* MAC_TODO: for now fail if width is not mod 16 (toolbox requires it) */
if (width % 16 != 0)
return -1;
#endif
#ifdef HAVE_NS
void *bitmap = ns_image_from_XBM (bits, width, height);
if (!bitmap)
return -1;
#endif
id = x_allocate_bitmap_record (f);
#ifdef HAVE_MACGUI
dpyinfo->bitmaps[id - 1].bitmap_data = xmalloc (height * width);
memcpy (dpyinfo->bitmaps[id - 1].bitmap_data, bits, height * width);
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
dpyinfo->bitmaps[id - 1].img = bitmap;
dpyinfo->bitmaps[id - 1].depth = 1;
#endif
#ifndef HAVE_MACGUI
dpyinfo->bitmaps[id - 1].file = NULL;
#endif
dpyinfo->bitmaps[id - 1].height = height;
dpyinfo->bitmaps[id - 1].width = width;
dpyinfo->bitmaps[id - 1].refcount = 1;
#ifdef HAVE_X_WINDOWS
dpyinfo->bitmaps[id - 1].pixmap = bitmap;
dpyinfo->bitmaps[id - 1].have_mask = false;
dpyinfo->bitmaps[id - 1].depth = 1;
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
dpyinfo->bitmaps[id - 1].pixmap = bitmap;
dpyinfo->bitmaps[id - 1].hinst = NULL;
dpyinfo->bitmaps[id - 1].depth = 1;
#endif /* HAVE_NTGUI */
return id;
}
/* Create bitmap from file FILE for frame F. */
ptrdiff_t
x_create_bitmap_from_file (struct frame *f, Lisp_Object file)
{
Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
#ifdef HAVE_MACGUI
return -1; /* MAC_TODO : bitmap support */
#endif /* HAVE_MACGUI */
#ifdef HAVE_NTGUI
return -1; /* W32_TODO : bitmap support */
#endif /* HAVE_NTGUI */
#ifdef HAVE_NS
ptrdiff_t id;
void *bitmap = ns_image_from_file (file);
if (!bitmap)
return -1;
id = x_allocate_bitmap_record (f);
dpyinfo->bitmaps[id - 1].img = bitmap;
dpyinfo->bitmaps[id - 1].refcount = 1;
dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
dpyinfo->bitmaps[id - 1].depth = 1;
dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap);
dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap);
return id;
#endif
#ifdef HAVE_X_WINDOWS
unsigned int width, height;
Pixmap bitmap;
int xhot, yhot, result;
ptrdiff_t id;
Lisp_Object found;
char *filename;
/* Look for an existing bitmap with the same name. */
for (id = 0; id < dpyinfo->bitmaps_last; ++id)
{
if (dpyinfo->bitmaps[id].refcount
&& dpyinfo->bitmaps[id].file
&& !strcmp (dpyinfo->bitmaps[id].file, SSDATA (file)))
{
++dpyinfo->bitmaps[id].refcount;
return id + 1;
}
}
/* Search bitmap-file-path for the file, if appropriate. */
if (openp (Vx_bitmap_file_path, file, Qnil, &found,
make_number (R_OK), false)
< 0)
return -1;
filename = SSDATA (found);
result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
filename, &width, &height, &bitmap, &xhot, &yhot);
if (result != BitmapSuccess)
return -1;
id = x_allocate_bitmap_record (f);
dpyinfo->bitmaps[id - 1].pixmap = bitmap;
dpyinfo->bitmaps[id - 1].have_mask = false;
dpyinfo->bitmaps[id - 1].refcount = 1;
dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
dpyinfo->bitmaps[id - 1].depth = 1;
dpyinfo->bitmaps[id - 1].height = height;
dpyinfo->bitmaps[id - 1].width = width;
return id;
#endif /* HAVE_X_WINDOWS */
}
/* Free bitmap B. */
static void
free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm)
{
#ifdef HAVE_X_WINDOWS
XFreePixmap (dpyinfo->display, bm->pixmap);
if (bm->have_mask)
XFreePixmap (dpyinfo->display, bm->mask);
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
DeleteObject (bm->pixmap);
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
xfree (bm->bitmap_data); /* Added ++kfs */
bm->bitmap_data = NULL;
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
ns_release_object (bm->img);
#endif
#ifndef HAVE_MACGUI
if (bm->file)
{
xfree (bm->file);
bm->file = NULL;
}
#endif
}
/* Remove reference to bitmap with id number ID. */
void
x_destroy_bitmap (struct frame *f, ptrdiff_t id)
{
Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
if (id > 0)
{
Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1];
if (--bm->refcount == 0)
{
block_input ();
free_bitmap_record (dpyinfo, bm);
unblock_input ();
}
}
}
/* Free all the bitmaps for the display specified by DPYINFO. */
void
x_destroy_all_bitmaps (Display_Info *dpyinfo)
{
ptrdiff_t i;
Bitmap_Record *bm = dpyinfo->bitmaps;
for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++)
if (bm->refcount > 0)
free_bitmap_record (dpyinfo, bm);
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_NTGUI
static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *,
bool, HGDIOBJ *);
static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC,
HGDIOBJ);
#else
static XImagePtr image_get_x_image (struct frame *, struct image *, bool);
static void image_unget_x_image (struct image *, bool, XImagePtr);
#define image_get_x_image_or_dc(f, img, mask_p, dummy) \
image_get_x_image (f, img, mask_p)
#define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
image_unget_x_image (img, mask_p, ximg)
#endif
#ifdef HAVE_X_WINDOWS
static void image_sync_to_pixmaps (struct frame *, struct image *);
/* Useful functions defined in the section
`Image type independent image structures' below. */
static unsigned long four_corners_best (XImagePtr ximg,
int *corners,
unsigned long width,
unsigned long height);
/* Create a mask of a bitmap. Note is this not a perfect mask.
It's nicer with some borders in this context */
void
x_create_bitmap_mask (struct frame *f, ptrdiff_t id)
{
Pixmap pixmap, mask;
XImagePtr ximg, mask_img;
unsigned long width, height;
bool result;
unsigned long bg;
unsigned long x, y, xp, xm, yp, ym;
GC gc;
Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
if (!(id > 0))
return;
pixmap = x_bitmap_pixmap (f, id);
width = x_bitmap_width (f, id);
height = x_bitmap_height (f, id);
block_input ();
ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
~0, ZPixmap);
if (!ximg)
{
unblock_input ();
return;
}
result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);
unblock_input ();
if (!result)
{
XDestroyImage (ximg);
return;
}
bg = four_corners_best (ximg, NULL, width, height);
for (y = 0; y < ximg->height; ++y)
{
for (x = 0; x < ximg->width; ++x)
{
xp = x != ximg->width - 1 ? x + 1 : 0;
xm = x != 0 ? x - 1 : ximg->width - 1;
yp = y != ximg->height - 1 ? y + 1 : 0;
ym = y != 0 ? y - 1 : ximg->height - 1;
if (XGetPixel (ximg, x, y) == bg
&& XGetPixel (ximg, x, yp) == bg
&& XGetPixel (ximg, x, ym) == bg
&& XGetPixel (ximg, xp, y) == bg
&& XGetPixel (ximg, xp, yp) == bg
&& XGetPixel (ximg, xp, ym) == bg
&& XGetPixel (ximg, xm, y) == bg
&& XGetPixel (ximg, xm, yp) == bg
&& XGetPixel (ximg, xm, ym) == bg)
XPutPixel (mask_img, x, y, 0);
else
XPutPixel (mask_img, x, y, 1);
}
}
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);
XFreeGC (FRAME_X_DISPLAY (f), gc);
dpyinfo->bitmaps[id - 1].have_mask = true;
dpyinfo->bitmaps[id - 1].mask = mask;
XDestroyImage (ximg);
x_destroy_x_image (mask_img);
}
#endif /* HAVE_X_WINDOWS */
/***********************************************************************
Image types
***********************************************************************/
/* List of supported image types. Use define_image_type to add new
types. Use lookup_image_type to find a type for a given symbol. */
static struct image_type *image_types;
/* The symbol `xbm' which is used as the type symbol for XBM images. */
static Lisp_Object Qxbm;
/* Keywords. */
Lisp_Object QCascent, QCmargin, QCrelief;
Lisp_Object QCconversion;
static Lisp_Object QCheuristic_mask;
static Lisp_Object QCcolor_symbols;
static Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry;
static Lisp_Object QCcrop, QCrotation;
/* Other symbols. */
static Lisp_Object Qcount, Qextension_data, Qdelay;
#ifdef HAVE_MACGUI
static Lisp_Object Qdocument_attributes;
#endif
static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
/* Forward function prototypes. */
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 void x_build_heuristic_mask (struct frame *, struct image *,
Lisp_Object);
#ifdef WINDOWSNT
#define CACHE_IMAGE_TYPE(type, status) \
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)
/* Define a new image type from TYPE. This adds a copy of TYPE to
image_types and caches the loading status of TYPE. */
static struct image_type *
define_image_type (struct image_type *type)
{
struct image_type *p = NULL;
Lisp_Object target_type = *type->type;
bool type_valid = 1;
block_input ();
for (p = image_types; p; p = p->next)
if (EQ (*p->type, target_type))
goto done;
if (type->init)
{
#if defined HAVE_NTGUI && defined WINDOWSNT
/* 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. */
p = xmalloc (sizeof *p);
*p = *type;
p->next = image_types;
image_types = p;
}
done:
unblock_input ();
return p;
}
/* 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. */
bool
valid_image_p (Lisp_Object object)
{
bool valid_p = 0;
if (IMAGEP (object))
{
Lisp_Object tem;
for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
if (EQ (XCAR (tem), QCtype))
{
tem = XCDR (tem);
if (CONSP (tem) && SYMBOLP (XCAR (tem)))
{
struct image_type *type;
type = lookup_image_type (XCAR (tem));
if (type)
valid_p = type->valid_p (object);
}
break;
}
}
return valid_p;
}
/* Log error message with format string FORMAT and argument ARG.
Signaling an error, e.g. when an image cannot be loaded, is not a
good idea because this would interrupt redisplay, and the error
message display would lead to another redisplay. This function
therefore simply displays a message. */
static void
image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2)
{
add_to_log (format, arg1, arg2);
}
/***********************************************************************
Image specifications
***********************************************************************/
enum image_value_type
{
IMAGE_DONT_CHECK_VALUE_TYPE,
IMAGE_STRING_VALUE,
IMAGE_STRING_OR_NIL_VALUE,
IMAGE_SYMBOL_VALUE,
IMAGE_POSITIVE_INTEGER_VALUE,
IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR,
IMAGE_NON_NEGATIVE_INTEGER_VALUE,
IMAGE_ASCENT_VALUE,
IMAGE_INTEGER_VALUE,
IMAGE_FUNCTION_VALUE,
IMAGE_NUMBER_VALUE,
IMAGE_BOOL_VALUE
};
/* Structure used when parsing image specifications. */
struct image_keyword
{
/* Name of keyword. */
const char *name;
/* The type of value allowed. */
enum image_value_type type;
/* True means key must be present. */
bool mandatory_p;
/* Used to recognize duplicate keywords in a property list. */
int count;
/* The value that was found. */
Lisp_Object value;
};
/* 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 true if SPEC is valid. */
static bool
parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
int nkeywords, Lisp_Object type)
{
int i;
Lisp_Object plist;
if (!IMAGEP (spec))
return 0;
plist = XCDR (spec);
while (CONSP (plist))
{
Lisp_Object key, value;
/* First element of a pair must be a symbol. */
key = XCAR (plist);
plist = XCDR (plist);
if (!SYMBOLP (key))
return 0;
/* There must follow a value. */
if (!CONSP (plist))
return 0;
value = XCAR (plist);
plist = XCDR (plist);
/* Find key in KEYWORDS. Error if not found. */
for (i = 0; i < nkeywords; ++i)
if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0)
break;
if (i == nkeywords)
continue;
/* Record that we recognized the keyword. If a keywords
was found more than once, it's an error. */
keywords[i].value = value;
if (keywords[i].count > 1)
return 0;
++keywords[i].count;
/* Check type of value against allowed type. */
switch (keywords[i].type)
{
case IMAGE_STRING_VALUE:
if (!STRINGP (value))
return 0;
break;
case IMAGE_STRING_OR_NIL_VALUE:
if (!STRINGP (value) && !NILP (value))
return 0;
break;
case IMAGE_SYMBOL_VALUE:
if (!SYMBOLP (value))
return 0;
break;
case IMAGE_POSITIVE_INTEGER_VALUE:
if (! RANGED_INTEGERP (1, value, INT_MAX))
return 0;
break;
case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR:
if (RANGED_INTEGERP (0, value, INT_MAX))
break;
if (CONSP (value)
&& 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 (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;
case IMAGE_DONT_CHECK_VALUE_TYPE:
break;
case IMAGE_FUNCTION_VALUE:
value = indirect_function (value);
if (!NILP (Ffunctionp (value)))
break;
return 0;
case IMAGE_NUMBER_VALUE:
if (!INTEGERP (value) && !FLOATP (value))
return 0;
break;
case IMAGE_INTEGER_VALUE:
if (! TYPE_RANGED_INTEGERP (int, value))
return 0;
break;
case IMAGE_BOOL_VALUE:
if (!NILP (value) && !EQ (value, Qt))
return 0;
break;
default:
emacs_abort ();
break;
}
if (EQ (key, QCtype) && !EQ (type, value))
return 0;
}
/* Check that all mandatory fields are present. */
for (i = 0; i < nkeywords; ++i)
if (keywords[i].mandatory_p && keywords[i].count == 0)
return 0;
return NILP (plist);
}
/* Return the value of KEY in image specification SPEC. Value is nil
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, bool *found)
{
Lisp_Object tail;
eassert (valid_image_p (spec));
for (tail = XCDR (spec);
CONSP (tail) && CONSP (XCDR (tail));
tail = XCDR (XCDR (tail)))
{
if (EQ (XCAR (tail), key))
{
if (found)
*found = 1;
return XCAR (XCDR (tail));
}
}
if (found)
*found = 0;
return Qnil;
}
DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
PIXELS non-nil means return the size in pixels, otherwise return the
size in canonical character units.
FRAME is the frame on which the image will be displayed. FRAME nil
or omitted means use the selected frame. */)
(Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame)
{
Lisp_Object size;
size = Qnil;
if (valid_image_p (spec))
{
struct frame *f = decode_window_system_frame (frame);
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 (NILP (pixels))
size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
make_float ((double) height / FRAME_LINE_HEIGHT (f)));
else
size = Fcons (make_number (width), make_number (height));
}
else
error ("Invalid image specification");
return size;
}
DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
doc: /* Return t if image SPEC has a mask bitmap.
FRAME is the frame on which the image will be displayed. FRAME nil
or omitted means use the selected frame. */)
(Lisp_Object spec, Lisp_Object frame)
{
Lisp_Object mask;
mask = Qnil;
if (valid_image_p (spec))
{
struct frame *f = decode_window_system_frame (frame);
ptrdiff_t id = lookup_image (f, spec);
struct image *img = IMAGE_FROM_ID (f, id);
if (img->mask)
mask = Qt;
}
else
error ("Invalid image specification");
return mask;
}
DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0,
doc: /* Return metadata for image SPEC.
FRAME is the frame on which the image will be displayed. FRAME nil
or omitted means use the selected frame. */)
(Lisp_Object spec, Lisp_Object frame)
{
Lisp_Object ext;
ext = Qnil;
if (valid_image_p (spec))
{
struct frame *f = decode_window_system_frame (frame);
ptrdiff_t id = lookup_image (f, spec);
struct image *img = IMAGE_FROM_ID (f, id);
ext = img->lisp_data;
}
return ext;
}
/***********************************************************************
Image type independent image structures
***********************************************************************/
#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 = xzalloc (sizeof *img);
Lisp_Object file = image_spec_value (spec, QCfile, NULL);
eassert (valid_image_p (spec));
img->dependencies = NILP (file) ? Qnil : list1 (file);
img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
eassert (img->type != NULL);
img->spec = spec;
img->lisp_data = Qnil;
img->ascent = DEFAULT_IMAGE_ASCENT;
img->hash = hash;
img->corners[BOT_CORNER] = -1; /* Full image */
return img;
}
/* Free image IMG which was used on frame F, including its resources. */
static void
free_image (struct frame *f, struct image *img)
{
if (img)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
/* Remove IMG from the hash table of its cache. */
if (img->prev)
img->prev->next = img->next;
else
c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
if (img->next)
img->next->prev = img->prev;
c->images[img->id] = NULL;
/* Free resources, then free IMG. */
img->type->free (f, img);
xfree (img);
}
}
/* Return true if the given widths and heights are valid for display. */
static bool
check_image_size (struct frame *f, int width, int height)
{
int w, h;
if (width <= 0 || height <= 0)
return 0;
if (INTEGERP (Vmax_image_size))
return (width <= XINT (Vmax_image_size)
&& height <= XINT (Vmax_image_size));
else if (FLOATP (Vmax_image_size))
{
if (f != NULL)
{
w = FRAME_PIXEL_WIDTH (f);
h = FRAME_PIXEL_HEIGHT (f);
}
else
w = h = 1024; /* Arbitrary size for unknown frame. */
return (width <= XFLOAT_DATA (Vmax_image_size) * w
&& height <= XFLOAT_DATA (Vmax_image_size) * h);
}
else
return 1;
}
/* Prepare image IMG for display on frame F. Must be called before
drawing an image. */
void
prepare_image_for_display (struct frame *f, struct image *img)
{
/* We're about to display IMG, so set its timestamp to `now'. */
img->timestamp = current_timespec ();
/* 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);
#ifdef HAVE_X_WINDOWS
if (!img->load_failed_p)
{
block_input ();
image_sync_to_pixmaps (f, img);
unblock_input ();
}
#elif defined (HAVE_MACGUI)
if (!img->load_failed_p && img->cg_image == NULL)
{
img->cg_image = mac_create_cg_image_from_image (f, img);
if (img->cg_image == NULL)
{
img->load_failed_p = 1;
img->type->free (f, img);
}
}
#endif
}
/* Value is the number of pixels for the ascent of image IMG when
drawn in face FACE. */
int
image_ascent (struct image *img, struct face *face, struct glyph_slice *slice)
{
int height;
int ascent;
if (slice->height == img->height)
height = img->height + img->vmargin;
else if (slice->y == 0)
height = slice->height + img->vmargin;
else
height = slice->height;
if (img->ascent == CENTERED_IMAGE_ASCENT)
{
if (face->font)
{
#ifdef HAVE_NTGUI
/* W32 specific version. Why?. ++kfs */
ascent = height / 2 - (FONT_DESCENT (face->font)
- FONT_BASE (face->font)) / 2;
#else
/* This expression is arranged so that if the image can't be
exactly centered, it will be moved slightly up. This is
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;
#endif /* HAVE_NTGUI */
}
else
ascent = height / 2;
}
else
ascent = height * (img->ascent / 100.0);
return ascent;
}
/* Image background colors. */
/* Find the "best" corner color of a bitmap.
On W32, XIMG is assumed to a device context with the bitmap selected. */
static RGB_PIXEL_COLOR
four_corners_best (XImagePtr_or_DC ximg, int *corners,
unsigned long width, unsigned long height)
{
RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0);
int i, best_count;
if (corners && corners[BOT_CORNER] >= 0)
{
/* Get the colors at the corner_pixels of ximg. */
corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]);
corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]);
corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1);
corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1);
}
else
{
/* Get the colors at the corner_pixels of ximg. */
corner_pixels[0] = GET_PIXEL (ximg, 0, 0);
corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0);
corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1);
corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1);
}
/* Choose the most frequently found color as background. */
for (i = best_count = 0; i < 4; ++i)
{
int j, n;
for (j = n = 0; j < 4; ++j)
if (corner_pixels[i] == corner_pixels[j])
++n;
if (n > best_count)
best = corner_pixels[i], best_count = n;
}
return best;
}
/* Portability macros */
#ifdef HAVE_NTGUI
#define Free_Pixmap(display, pixmap) \
DeleteObject (pixmap)
#elif defined (HAVE_NS)
#define Free_Pixmap(display, pixmap) \
ns_release_object (pixmap)
#else
#define Free_Pixmap(display, pixmap) \
XFreePixmap (display, pixmap)
#endif /* !HAVE_NTGUI && !HAVE_NS */
/* Return the `background' field of IMG. If IMG doesn't have one yet,
it is guessed heuristically. If non-zero, XIMG is an existing
XImage object (or device context with the image selected on W32) to
use for the heuristic. */
RGB_PIXEL_COLOR
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. */
{
bool free_ximg = !ximg;
#ifdef HAVE_NTGUI
HGDIOBJ prev;
#endif /* HAVE_NTGUI */
if (free_ximg)
ximg = image_get_x_image_or_dc (f, img, 0, &prev);
img->background = four_corners_best (ximg, img->corners, img->width, img->height);
if (free_ximg)
image_unget_x_image_or_dc (img, 0, ximg, prev);
img->background_valid = 1;
}
return img->background;
}
/* Return the `background_transparent' field of IMG. If IMG doesn't
have one yet, it is guessed heuristically. If non-zero, MASK is an
existing XImage object to use for the heuristic. */
int
image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask)
{
if (! img->background_transparent_valid)
/* IMG doesn't have a background yet, try to guess a reasonable value. */
{
if (img->mask)
{
bool free_mask = !mask;
#ifdef HAVE_NTGUI
HGDIOBJ prev;
#endif /* HAVE_NTGUI */
if (free_mask)
mask = image_get_x_image_or_dc (f, img, 1, &prev);
img->background_transparent
= (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN);
if (free_mask)
image_unget_x_image_or_dc (img, 1, mask, prev);
}
else
img->background_transparent = 0;
img->background_transparent_valid = 1;
}
return img->background_transparent;
}
/***********************************************************************
Helper functions for X image types
***********************************************************************/
/* Clear X resources of image IMG on frame F according to FLAGS.
FLAGS is bitwise-or of the following masks:
CLEAR_IMAGE_PIXMAP free the pixmap if any.
CLEAR_IMAGE_MASK means clear the mask pixmap if any.
CLEAR_IMAGE_COLORS means free colors allocated for the image, if
any. */
#define CLEAR_IMAGE_PIXMAP (1 << 0)
#define CLEAR_IMAGE_MASK (1 << 1)
#define CLEAR_IMAGE_COLORS (1 << 2)
static void
x_clear_image_1 (struct frame *f, struct image *img, int flags)
{
if (flags & CLEAR_IMAGE_PIXMAP)
{
if (img->pixmap)
{
Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
img->pixmap = NO_PIXMAP;
/* NOTE (HAVE_NS): background color is NOT an indexed color! */
img->background_valid = 0;
}
#ifdef HAVE_X_WINDOWS
if (img->ximg)
{
x_destroy_x_image (img->ximg);
img->ximg = NULL;
img->background_valid = 0;
}
#endif
}
if (flags & CLEAR_IMAGE_MASK)
{
if (img->mask)
{
Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
img->mask = NO_PIXMAP;
img->background_transparent_valid = 0;
}
#ifdef HAVE_X_WINDOWS
if (img->mask_img)
{
x_destroy_x_image (img->mask_img);
img->mask_img = NULL;
img->background_transparent_valid = 0;
}
#endif
}
if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors)
{
/* MAC_TODO: color table support. */
/* W32_TODO: color table support. */
#ifdef HAVE_X_WINDOWS
x_free_colors (f, img->colors, img->ncolors);
#endif /* HAVE_X_WINDOWS */
xfree (img->colors);
img->colors = NULL;
img->ncolors = 0;
}
#if defined (HAVE_MACGUI)
if (img->cg_image)
{
CGImageRelease (img->cg_image);
img->cg_image = NULL;
}
#endif
}
/* Free X resources of image IMG which is used on frame F. */
static void
x_clear_image (struct frame *f, struct image *img)
{
block_input ();
x_clear_image_1 (f, img,
CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS);
unblock_input ();
}
/* Allocate color COLOR_NAME for image IMG on frame F. If color
cannot be allocated, use DFLT. Add a newly allocated color to
IMG->colors, so that it can be freed again. Value is the pixel
color. */
static unsigned long
x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name,
unsigned long dflt)
{
XColor color;
unsigned long result;
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,
INT_MAX))
{
/* 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 = xrealloc (img->colors, ncolors * sizeof *img->colors);
img->colors[ncolors - 1] = color.pixel;
img->ncolors = ncolors;
result = color.pixel;
}
else
result = dflt;
return result;
}
/***********************************************************************
Image Cache
***********************************************************************/
static void cache_image (struct frame *f, struct image *img);
/* Return a new, initialized image cache that is allocated from the
heap. Call free_image_cache to free an image cache. */
struct image_cache *
make_image_cache (void)
{
struct image_cache *c = xmalloc (sizeof *c);
c->size = 50;
c->used = c->refcount = 0;
c->images = xmalloc (c->size * sizeof *c->images);
c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
return c;
}
/* 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, EMACS_UINT hash)
{
struct image *img;
struct image_cache *c = FRAME_IMAGE_CACHE (f);
int i = hash % IMAGE_CACHE_BUCKETS_SIZE;
if (!c) return NULL;
/* If the image spec does not specify a background color, the cached
image must have the same background color as the current frame.
The foreground color must also match, for the sake of monochrome
images.
In fact, we could ignore the foreground color matching condition
for color images, or if the image spec specifies :foreground;
similarly we could ignore the background color matching condition
for formats that don't use transparency (such as jpeg), or if the
image spec specifies :background. However, the extra memory
usage is probably negligible in practice, so we don't bother. */
for (img = c->buckets[i]; img; img = img->next)
if (img->hash == hash
&& !NILP (Fequal (img->spec, spec))
&& img->frame_foreground == FRAME_FOREGROUND_PIXEL (f)
&& img->frame_background == FRAME_BACKGROUND_PIXEL (f)
#ifdef HAVE_MACGUI
&& (img->target_backing_scale == 0
|| img->target_backing_scale == FRAME_BACKING_SCALE_FACTOR (f))
#endif
)
break;
return img;
}
/* Search frame F for an image with spec SPEC, and free it. */
static void
uncache_image (struct frame *f, Lisp_Object spec)
{
struct image *img = search_image_cache (f, spec, sxhash (spec, 0));
if (img)
{
free_image (f, img);
/* As display glyphs may still be referring to the image ID, we
must garbage the frame (Bug#6426). */
SET_FRAME_GARBAGED (f);
}
}
/* Free image cache of frame F. Be aware that X frames share images
caches. */
void
free_image_cache (struct frame *f)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
if (c)
{
ptrdiff_t i;
/* Cache should not be referenced by any frame when freed. */
eassert (c->refcount == 0);
for (i = 0; i < c->used; ++i)
free_image (f, c->images[i]);
xfree (c->images);
xfree (c->buckets);
xfree (c);
FRAME_IMAGE_CACHE (f) = NULL;
}
}
/* Clear image cache of frame F. FILTER=t means free all images.
FILTER=nil means clear only images that haven't been
displayed for some time.
Else, only free the images which have FILTER in their `dependencies'.
Should be called from time to time to reduce the number of loaded images.
If image-cache-eviction-delay is non-nil, this frees images in the cache
which weren't displayed for at least that many seconds. */
static void
clear_image_cache (struct frame *f, Lisp_Object filter)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
if (c)
{
ptrdiff_t i, nfreed = 0;
/* Block input so that we won't be interrupted by a SIGIO
while being in an inconsistent state. */
block_input ();
if (!NILP (filter))
{
/* Filter image cache. */
for (i = 0; i < c->used; ++i)
{
struct image *img = c->images[i];
if (img && (EQ (Qt, filter)
|| !NILP (Fmember (filter, img->dependencies))))
{
free_image (f, img);
++nfreed;
}
}
}
else if (INTEGERP (Vimage_cache_eviction_delay))
{
/* Free cache based on timestamp. */
struct timespec old, t;
double delay;
ptrdiff_t nimages = 0;
for (i = 0; i < c->used; ++i)
if (c->images[i])
nimages++;
/* If the number of cached images has grown unusually large,
decrease the cache eviction delay (Bug#6230). */
delay = XINT (Vimage_cache_eviction_delay);
if (nimages > 40)
delay = 1600 * delay / nimages / nimages;
delay = max (delay, 1);
t = current_timespec ();
old = timespec_sub (t, dtotimespec (delay));
for (i = 0; i < c->used; ++i)
{
struct image *img = c->images[i];
if (img && timespec_cmp (img->timestamp, old) < 0)
{
free_image (f, img);
++nfreed;
}
}
}
/* We may be clearing the image cache because, for example,
Emacs was iconified for a longer period of time. In that
case, current matrices may still contain references to
images freed above. So, clear these matrices. */
if (nfreed)
{
Lisp_Object tail, frame;
FOR_EACH_FRAME (tail, frame)
{
struct frame *fr = XFRAME (frame);
if (FRAME_IMAGE_CACHE (fr) == c)
clear_current_matrices (fr);
}
windows_or_buffers_changed = 19;
}
unblock_input ();
}
}
void
clear_image_caches (Lisp_Object filter)
{
/* FIXME: We want to do
* struct terminal *t;
* for (t = terminal_list; t; t = t->next_terminal)
* clear_image_cache (t, filter); */
Lisp_Object tail, frame;
FOR_EACH_FRAME (tail, frame)
if (FRAME_WINDOW_P (XFRAME (frame)))
clear_image_cache (XFRAME (frame), filter);
}
DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
0, 1, 0,
doc: /* Clear the image cache.
FILTER nil or a frame means clear all images in the selected frame.
FILTER t means clear the image caches of all frames.
Anything else, means only clear those images which refer to FILTER,
which is then usually a filename. */)
(Lisp_Object filter)
{
if (!(EQ (filter, Qnil) || FRAMEP (filter)))
clear_image_caches (filter);
else
clear_image_cache (decode_window_system_frame (filter), Qt);
return Qnil;
}
DEFUN ("image-flush", Fimage_flush, Simage_flush,
1, 2, 0,
doc: /* Flush the image with specification SPEC on frame FRAME.
This removes the image from the Emacs image cache. If SPEC specifies
an image file, the next redisplay of this image will read from the
current contents of that file.
FRAME nil or omitted means use the selected frame.
FRAME t means refresh the image on all frames. */)
(Lisp_Object spec, Lisp_Object frame)
{
if (!valid_image_p (spec))
error ("Invalid image specification");
if (EQ (frame, Qt))
{
Lisp_Object tail;
FOR_EACH_FRAME (tail, frame)
{
struct frame *f = XFRAME (frame);
if (FRAME_WINDOW_P (f))
uncache_image (f, spec);
}
}
else
uncache_image (decode_window_system_frame (frame), spec);
return Qnil;
}
/* Compute masks and transform image IMG on frame F, as specified
by the image's specification, */
static void
postprocess_image (struct frame *f, struct image *img)
{
/* Manipulation of the image's mask. */
if (img->pixmap)
{
Lisp_Object conversion, spec;
Lisp_Object mask;
spec = img->spec;
/* `:heuristic-mask t'
`:mask heuristic'
means build a mask heuristically.
`:heuristic-mask (R G B)'
`:mask (heuristic (R G B))'
means build a mask from color (R G B) in the
image.
`:mask nil'
means remove a mask, if any. */
mask = image_spec_value (spec, QCheuristic_mask, NULL);
if (!NILP (mask))
x_build_heuristic_mask (f, img, mask);
else
{
bool found_p;
mask = image_spec_value (spec, QCmask, &found_p);
if (EQ (mask, Qheuristic))
x_build_heuristic_mask (f, img, Qt);
else if (CONSP (mask)
&& EQ (XCAR (mask), Qheuristic))
{
if (CONSP (XCDR (mask)))
x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
else
x_build_heuristic_mask (f, img, XCDR (mask));
}
else if (NILP (mask) && found_p && img->mask)
x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
}
/* Should we apply an image transformation algorithm? */
conversion = image_spec_value (spec, QCconversion, NULL);
if (EQ (conversion, Qdisabled))
x_disable_image (f, img);
else if (EQ (conversion, Qlaplace))
x_laplace (f, img);
else if (EQ (conversion, Qemboss))
x_emboss (f, img);
else if (CONSP (conversion)
&& EQ (XCAR (conversion), Qedge_detection))
{
Lisp_Object tem;
tem = XCDR (conversion);
if (CONSP (tem))
x_edge_detection (f, img,
Fplist_get (tem, QCmatrix),
Fplist_get (tem, QCcolor_adjustment));
}
}
}
/* Return the id of image with Lisp specification SPEC on frame F.
SPEC must be a valid Lisp image specification (see valid_image_p). */
ptrdiff_t
lookup_image (struct frame *f, Lisp_Object spec)
{
struct image *img;
EMACS_UINT hash;
/* F must be a window-system frame, and SPEC must be a valid image
specification. */
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);
img = search_image_cache (f, spec, hash);
if (img && img->load_failed_p)
{
free_image (f, img);
img = NULL;
}
/* If not found, create a new image and cache it. */
if (img == NULL)
{
block_input ();
img = make_image (spec, hash);
cache_image (f, img);
img->load_failed_p = ! img->type->load (f, img);
img->frame_foreground = FRAME_FOREGROUND_PIXEL (f);
img->frame_background = FRAME_BACKGROUND_PIXEL (f);
/* If we can't load the image, and we don't have a width and
height, use some arbitrary width and height so that we can
draw a rectangle for it. */
if (img->load_failed_p)
{
Lisp_Object value;
value = image_spec_value (spec, QCwidth, NULL);
img->width = (INTEGERP (value)
? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
value = image_spec_value (spec, QCheight, NULL);
img->height = (INTEGERP (value)
? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
}
else
{
/* Handle image type independent image attributes
`: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))
img->ascent = XFASTINT (ascent);
else if (EQ (ascent, Qcenter))
img->ascent = CENTERED_IMAGE_ASCENT;
margin = image_spec_value (spec, QCmargin, NULL);
if (INTEGERP (margin))
img->vmargin = img->hmargin = XFASTINT (margin);
else if (CONSP (margin))
{
img->hmargin = XFASTINT (XCAR (margin));
img->vmargin = XFASTINT (XCDR (margin));
}
relief = image_spec_value (spec, QCrelief, NULL);
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);
img->vmargin += eabs (img->relief);
}
if (! img->background_valid)
{
bg = image_spec_value (img->spec, QCbackground, NULL);
if (!NILP (bg))
{
img->background
= x_alloc_image_color (f, img, bg,
FRAME_BACKGROUND_PIXEL (f));
img->background_valid = 1;
}
}
/* Do image transformations and compute masks, unless we
don't have the image yet. */
if (!EQ (*img->type->type, Qpostscript))
postprocess_image (f, img);
}
unblock_input ();
}
/* We're using IMG, so set its timestamp to `now'. */
img->timestamp = current_timespec ();
/* Value is the image id. */
return img->id;
}
/* Cache image IMG in the image cache of frame F. */
static void
cache_image (struct frame *f, struct image *img)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
ptrdiff_t i;
/* Find a free slot in c->images. */
for (i = 0; i < c->used; ++i)
if (c->images[i] == NULL)
break;
/* If no free slot found, maybe enlarge c->images. */
if (i == c->used && c->used == c->size)
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;
img->id = i;
if (i == c->used)
++c->used;
/* Add IMG to the cache's hash table. */
i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
img->next = c->buckets[i];
if (img->next)
img->next->prev = img;
img->prev = NULL;
c->buckets[i] = img;
}
/* Call FN on every image in the image cache of frame F. Used to mark
Lisp Objects in the image cache. */
/* Mark Lisp objects in image IMG. */
static void
mark_image (struct image *img)
{
mark_object (img->spec);
mark_object (img->dependencies);
if (!NILP (img->lisp_data))
mark_object (img->lisp_data);
}
void
mark_image_cache (struct image_cache *c)
{
if (c)
{
ptrdiff_t i;
for (i = 0; i < c->used; ++i)
if (c->images[i])
mark_image (c->images[i]);
}
}
/***********************************************************************
X / MAC / NS / W32 support code
***********************************************************************/
#ifdef WINDOWSNT
/* Macro for defining functions that will be loaded from image DLLs. */
#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) { \
fn_##func = (void *) GetProcAddress (lib, #func); \
if (!fn_##func) return 0; \
}
#endif /* WINDOWSNT */
/* 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 bool
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.
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 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 bool
x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth,
XImagePtr *ximg, Pixmap *pixmap)
{
#ifdef HAVE_X_WINDOWS
Display *display = FRAME_X_DISPLAY (f);
Window window = FRAME_X_WINDOW (f);
Screen *screen = FRAME_X_SCREEN (f);
eassert (input_blocked_p ());
if (depth <= 0)
depth = DefaultDepthOfScreen (screen);
*ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
depth, ZPixmap, 0, NULL, width, height,
depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
if (*ximg == NULL)
{
image_error ("Unable to allocate X image", Qnil, Qnil);
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 = xmalloc ((*ximg)->bytes_per_line * height);
/* Allocate a pixmap of the same size. */
*pixmap = XCreatePixmap (display, window, width, height, depth);
if (*pixmap == NO_PIXMAP)
{
x_destroy_x_image (*ximg);
*ximg = NULL;
image_error ("Unable to create X pixmap", Qnil, Qnil);
return 0;
}
return 1;
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
BITMAPINFOHEADER *header;
HDC hdc;
int scanline_width_bits;
int remainder;
int palette_colors = 0;
if (depth == 0)
depth = 24;
if (depth != 1 && depth != 4 && depth != 8
&& depth != 16 && depth != 24 && depth != 32)
{
image_error ("Invalid image bit depth specified", Qnil, Qnil);
return 0;
}
scanline_width_bits = width * depth;
remainder = scanline_width_bits % 32;
if (remainder)
scanline_width_bits += 32 - remainder;
/* 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);
*ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
header = &(*ximg)->info.bmiHeader;
memset (&(*ximg)->info, 0, sizeof (BITMAPINFO));
header->biSize = sizeof (*header);
header->biWidth = width;
header->biHeight = -height; /* negative indicates a top-down bitmap. */
header->biPlanes = 1;
header->biBitCount = depth;
header->biCompression = BI_RGB;
header->biClrUsed = palette_colors;
/* TODO: fill in palette. */
if (depth == 1)
{
(*ximg)->info.bmiColors[0].rgbBlue = 0;
(*ximg)->info.bmiColors[0].rgbGreen = 0;
(*ximg)->info.bmiColors[0].rgbRed = 0;
(*ximg)->info.bmiColors[0].rgbReserved = 0;
(*ximg)->info.bmiColors[1].rgbBlue = 255;
(*ximg)->info.bmiColors[1].rgbGreen = 255;
(*ximg)->info.bmiColors[1].rgbRed = 255;
(*ximg)->info.bmiColors[1].rgbReserved = 0;
}
hdc = get_frame_dc (f);
/* Create a DIBSection and raster array for the bitmap,
and store its handle in *pixmap. */
*pixmap = CreateDIBSection (hdc, &((*ximg)->info),
(depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
/* casting avoids a GCC warning */
(void **)&((*ximg)->data), NULL, 0);
/* Realize display palette and garbage all frames. */
release_frame_dc (f, hdc);
if (*pixmap == NULL)
{
DWORD err = GetLastError ();
Lisp_Object errcode;
/* All system errors are < 10000, so the following is safe. */
XSETINT (errcode, err);
image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
x_destroy_x_image (*ximg);
*ximg = NULL;
return 0;
}
return 1;
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
Display *display = FRAME_X_DISPLAY (f);
Window window = FRAME_X_WINDOW (f);
eassert (input_blocked_p ());
/* Allocate a pixmap of the same size. */
*pixmap = XCreatePixmap (display, window, width, height, depth);
if (*pixmap == NO_PIXMAP)
{
*ximg = NULL;
image_error ("Unable to create X pixmap", Qnil, Qnil);
return 0;
}
*ximg = *pixmap;
return 1;
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
*pixmap = ns_image_for_XPM (width, height, depth);
if (*pixmap == 0)
{
*ximg = NULL;
image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil);
return 0;
}
*ximg = *pixmap;
return 1;
#endif
}
/* Destroy XImage XIMG. Free XIMG->data. */
static void
x_destroy_x_image (XImagePtr ximg)
{
eassert (input_blocked_p ());
if (ximg)
{
#ifdef HAVE_X_WINDOWS
xfree (ximg->data);
ximg->data = NULL;
XDestroyImage (ximg);
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
/* Data will be freed by DestroyObject. */
ximg->data = NULL;
xfree (ximg);
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
XDestroyImage (ximg);
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
ns_release_object (ximg);
#endif /* HAVE_NS */
}
}
/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
are width and height of both the image and pixmap. */
static void
x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height)
{
#ifdef HAVE_X_WINDOWS
GC gc;
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);
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_NTGUI
#if 0 /* I don't think this is necessary looking at where it is used. */
HDC hdc = get_frame_dc (f);
SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
release_frame_dc (f, hdc);
#endif
#endif /* HAVE_NTGUI */
#ifdef HAVE_MACGUI
eassert (ximg == pixmap);
#endif /* HAVE_MACGUI */
#ifdef HAVE_NS
eassert (ximg == pixmap);
ns_retain_object (ximg);
#endif
}
/* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
with image_put_x_image. */
static bool
image_create_x_image_and_pixmap (struct frame *f, struct image *img,
int width, int height, int depth,
XImagePtr *ximg, bool mask_p)
{
eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP);
return x_create_x_image_and_pixmap (f, width, height, depth, ximg,
!mask_p ? &img->pixmap : &img->mask);
}
/* Put X image XIMG into image IMG on frame F, as a mask if and only
if MASK_P. On X, this simply records XIMG on a member of IMG, so
it can be put into the pixmap afterwards via image_sync_to_pixmaps.
On the other platforms, it puts XIMG into the pixmap, then frees
the X image and its buffer. */
static void
image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg,
bool mask_p)
{
#ifdef HAVE_X_WINDOWS
if (!mask_p)
{
eassert (img->ximg == NULL);
img->ximg = ximg;
}
else
{
eassert (img->mask_img == NULL);
img->mask_img = ximg;
}
#else
x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask,
img->width, img->height);
x_destroy_x_image (ximg);
#endif
}
#ifdef HAVE_X_WINDOWS
/* Put the X images recorded in IMG on frame F into pixmaps, then free
the X images and their buffers. */
static void
image_sync_to_pixmaps (struct frame *f, struct image *img)
{
if (img->ximg)
{
x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height);
x_destroy_x_image (img->ximg);
img->ximg = NULL;
}
if (img->mask_img)
{
x_put_x_image (f, img->mask_img, img->mask, img->width, img->height);
x_destroy_x_image (img->mask_img);
img->mask_img = NULL;
}
}
#endif
#ifdef HAVE_NTGUI
/* Create a memory device context for IMG on frame F. It stores the
currently selected GDI object into *PREV for future restoration by
image_unget_x_image_or_dc. */
static XImagePtr_or_DC
image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p,
HGDIOBJ *prev)
{
HDC frame_dc = get_frame_dc (f);
XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc);
release_frame_dc (f, frame_dc);
*prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask);
return ximg;
}
static void
image_unget_x_image_or_dc (struct image *img, bool mask_p,
XImagePtr_or_DC ximg, HGDIOBJ prev)
{
SelectObject (ximg, prev);
DeleteDC (ximg);
}
#else /* !HAVE_NTGUI */
/* Get the X image for IMG on frame F. The resulting X image data
should be treated as read-only at least on X. */
static XImagePtr
image_get_x_image (struct frame *f, struct image *img, bool mask_p)
{
#ifdef HAVE_X_WINDOWS
XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
if (ximg_in_img)
return ximg_in_img;
else
return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask,
0, 0, img->width, img->height, ~0, ZPixmap);
#elif defined (HAVE_MACGUI)
return !mask_p ? img->pixmap : img->mask;
#elif defined (HAVE_NS)
XImagePtr pixmap = !mask_p ? img->pixmap : img->mask;
ns_retain_object (pixmap);
return pixmap;
#endif
}
static void
image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg)
{
#ifdef HAVE_X_WINDOWS
XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
if (ximg_in_img)
eassert (ximg == ximg_in_img);
else
XDestroyImage (ximg);
#elif defined (HAVE_MACGUI)
#elif defined (HAVE_NS)
ns_release_object (ximg);
#endif
}
#endif /* !HAVE_NTGUI */
/***********************************************************************
File Handling
***********************************************************************/
/* 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. */
Lisp_Object
x_find_image_file (Lisp_Object file)
{
Lisp_Object file_found, search_path;
int fd;
/* TODO I think this should use something like image-load-path
instead. Unfortunately, that can contain non-string elements. */
search_path = Fcons (Fexpand_file_name (build_string ("images"),
Vdata_directory),
Vx_bitmap_file_path);
/* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
fd = openp (search_path, file, Qnil, &file_found, Qnil, false);
if (fd == -1)
file_found = Qnil;
else
{
file_found = ENCODE_FILE (file_found);
if (fd != -2)
emacs_close (fd);
}
return file_found;
}
/* Read FILE into memory. Value is a pointer to a buffer allocated
with xmalloc holding FILE's contents. Value is null if an error
occurred. *SIZE is set to the size of the file. */
static unsigned char *
slurp_file (char *file, ptrdiff_t *size)
{
FILE *fp = emacs_fopen (file, "rb");
unsigned char *buf = NULL;
struct stat st;
if (fp)
{
ptrdiff_t count = SPECPDL_INDEX ();
record_unwind_protect_ptr (fclose_unwind, fp);
if (fstat (fileno (fp), &st) == 0
&& 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX))
{
/* Report an error if we read past the purported EOF.
This can happen if the file grows as we read it. */
ptrdiff_t buflen = st.st_size;
buf = xmalloc (buflen + 1);
if (fread (buf, 1, buflen + 1, fp) == buflen)
*size = buflen;
else
{
xfree (buf);
buf = NULL;
}
}
unbind_to (count, Qnil);
}
return buf;
}
#ifdef HAVE_MACGUI
/***********************************************************************
MAC Image Load Functions
***********************************************************************/
static void compute_image_size (size_t, size_t, Lisp_Object, int *, int *);
/* Return the name of 2x image file that corresponds to the given
ENCODED_FILE_NAME. Return nil if the 2x file doesn't exist or not
readable. We assume ENCODED_FILE_NAME stands for an existing file
name, so its length does not exceed PATH_MAX thus fits in int. */
static Lisp_Object
mac_find_2x_image_file (Lisp_Object encoded_file_name)
{
Lisp_Object result;
char *p, *last_component;
ptrdiff_t prefix_len;
int desc;
p = strrchr (SSDATA (encoded_file_name), '/');
last_component = p ? p + 1 : SSDATA (encoded_file_name);
p = strrchr (last_component, '.');
if (p == NULL)
p = SSDATA (encoded_file_name) + SBYTES (encoded_file_name);
prefix_len = p - SSDATA (encoded_file_name);
result = make_uninit_string (SBYTES (encoded_file_name) + sizeof ("@2x") - 1);
sprintf (SSDATA (result), "%.*s@2x%.*s",
(int) prefix_len, SSDATA (encoded_file_name),
(int) (SBYTES (encoded_file_name) - prefix_len), p);
desc = emacs_open (SSDATA (result), O_RDONLY, 0);
if (desc < 0)
result = Qnil;
else
emacs_close (desc);
return result;
}
static Lisp_Object
mac_preprocess_image_for_2x_file (struct frame *f, struct image *img,
Lisp_Object file)
{
Lisp_Object file_2x = mac_find_2x_image_file (file);
if (!NILP (file_2x))
{
img->target_backing_scale = FRAME_BACKING_SCALE_FACTOR (f);
if (img->target_backing_scale == 2)
file = file_2x;
}
return file;
}
static void
mac_postprocess_image_for_2x (struct image *img)
{
/* Let four_corners_best use the corner positions measured by the
actual bitmap width/height data rather than the logical one. */
if (img->target_backing_scale == 2)
{
img->corners[LEFT_CORNER] = img->corners[TOP_CORNER] = 0;
img->corners[RIGHT_CORNER] = img->width;
img->corners[BOT_CORNER] = img->height;
img->width /= 2;
img->height /= 2;
}
}
static void
mac_cg_image_source_get_pixel_size (CGImageSourceRef source, size_t index,
int *width, int *height)
{
CFDictionaryRef props = CGImageSourceCopyPropertiesAtIndex (source, index,
NULL);
int w = 0, h = 0;
if (props)
{
CFNumberRef num;
int val;
num = CFDictionaryGetValue (props, kCGImagePropertyPixelWidth);
if (num && CFNumberGetValue (num, kCFNumberIntType, &val))
w = val;
num = CFDictionaryGetValue (props, kCGImagePropertyPixelHeight);
if (num && CFNumberGetValue (num, kCFNumberIntType, &val))
h = val;
CFRelease (props);
}
*width = w;
*height = h;
}
/* Given a multiimage CGImage SOURCE, return the index of an image
that is twice as large as the 0th image in both width and height.
If not found, return 0. */
static size_t
mac_cg_image_source_find_2x_index (CGImageSourceRef source)
{
size_t result = 0;
int base_width, base_height;
mac_cg_image_source_get_pixel_size (source, 0, &base_width, &base_height);
if (base_width > 0 && base_height > 0)
{
size_t i, count = CGImageSourceGetCount (source);
int width, height;
for (i = 1; i < count; i++)
{
mac_cg_image_source_get_pixel_size (source, i, &width, &height);
if (width == base_width * 2 && height == base_height * 2)
{
result = i;
break;
}
}
}
return result;
}
static CFStringRef
mac_create_type_identifier_for_image_spec (Lisp_Object spec)
{
CFStringRef identifier = NULL, tag_class, tag;
Lisp_Object val, format = image_spec_value (spec, intern (":format"), NULL);
if (NILP (format))
return NULL;
val = find_symbol_value (intern ("image-format-suffixes"));
if (CONSP (val)
&& (val = Fcar_safe (Fcdr_safe (Fassq (format, val))),
STRINGP (val)))
tag_class = kUTTagClassFilenameExtension;
else
{
tag_class = kUTTagClassMIMEType;
val = SYMBOL_NAME (format);
}
tag = cfstring_create_with_string (val);
if (tag)
{
identifier = UTTypeCreatePreferredIdentifierForTag (tag_class, tag, NULL);
CFRelease (tag);
}
return identifier;
}
static bool
image_load_image_io (struct frame *f, struct image *img, CFStringRef type)
{
Lisp_Object specified_file, specified_data, metadata = Qnil;
CFURLRef url = NULL;
CFDataRef data = NULL;
CFStringRef specified_type = NULL;
CFDictionaryRef options;
CFStringRef keys[2];
CFTypeRef values[2];
CFIndex num_values;
CGImageSourceRef source = NULL;
CFTypeRef obj = NULL;
size_t page_index;
/* If non-NULL and the background is not specified by the image
spec, then the background is filled with the specified color on
top of the frame background, in case the former isn't opaque. */
CGColorRef default_bg;
int width, height, scale_factor;
XImagePtr ximg = NULL, mask_img;
CGContextRef context;
CGRect rectangle, clip_rectangle;
CGAffineTransform transform;
Boolean has_alpha_p, gif_p, tiff_p;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
dispatch_group_t group;
#endif
/* Open the file. */
specified_file = image_spec_value (img->spec, QCfile, NULL);
specified_data = image_spec_value (img->spec, QCdata, NULL);
if (NILP (specified_data))
{
Lisp_Object file;
file = x_find_image_file (specified_file);
if (!STRINGP (file))
{
image_error ("Cannot find image file `%s'", specified_file, Qnil);
return 0;
}
file = mac_preprocess_image_for_2x_file (f, img, file);
url = CFURLCreateFromFileSystemRepresentation (NULL, SDATA (file),
SBYTES (file), false);
}
else
{
if (!STRINGP (specified_data))
{
image_error ("Invalid image data `%s'", specified_data, Qnil);
return 0;
}
data = CFDataCreate (NULL, SDATA (specified_data),
SBYTES (specified_data));
}
keys[0] = kCGImageSourceShouldCache;
values[0] = (CFTypeRef) kCFBooleanFalse;
num_values = 1;
if (type == NULL)
{
gif_p = tiff_p = false;
specified_type = mac_create_type_identifier_for_image_spec (img->spec);
}
else
{
gif_p = UTTypeEqual (type, kUTTypeGIF);
tiff_p = UTTypeEqual (type, kUTTypeTIFF);
specified_type = CFRetain (type);
}
if (specified_type)
{
keys[num_values] = kCGImageSourceTypeIdentifierHint;
values[num_values] = (CFTypeRef) specified_type;
num_values++;
}
options = CFDictionaryCreate (NULL, (const void **) keys,
(const void **) values, num_values,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (options)
{
if (url)
source = CGImageSourceCreateWithURL (url, options);
else if (data)
source = CGImageSourceCreateWithData (data, options);
CFRelease (options);
}
if (source)
{
CFStringRef real_type;
CFDictionaryRef src_props = NULL, props = NULL;
size_t count;
int loop_count = -1;
double delay_time = -1.0;
if (type == NULL
|| (real_type = CGImageSourceGetType (source),
real_type && UTTypeEqual (type, real_type)))
src_props = CGImageSourceCopyProperties (source, NULL);
if (src_props)
{
EMACS_INT ino = 0;
count = CGImageSourceGetCount (source);
if (type == NULL || gif_p || tiff_p)
{
Lisp_Object image = image_spec_value (img->spec, QCindex, NULL);
if (INTEGERP (image))
ino = XFASTINT (image);
else if (tiff_p && count > 1 && img->target_backing_scale == 0)
{
size_t index_2x = mac_cg_image_source_find_2x_index (source);
if (index_2x > 0)
{
img->target_backing_scale =
FRAME_BACKING_SCALE_FACTOR (f);
if (img->target_backing_scale == 2)
ino = index_2x;
}
}
}
if (ino < count)
{
props = CGImageSourceCopyPropertiesAtIndex (source, ino, NULL);
if (props)
{
CGImageRef cg_image =
CGImageSourceCreateImageAtIndex (source, ino, NULL);
if (cg_image)
{
scale_factor = (img->target_backing_scale == 2 ? 2 : 1);
width = CGImageGetWidth (cg_image) / scale_factor;
height = CGImageGetHeight (cg_image) / scale_factor;
obj = cg_image;
default_bg = NULL;
}
}
}
}
if (obj)
{
CFBooleanRef boolean =
CFDictionaryGetValue (props, kCGImagePropertyHasAlpha);
has_alpha_p = (boolean && CFBooleanGetValue (boolean));
/* Get animation-related properties for animated GIF (all
versions) or PNG (OS X 10.10 and later). Note that
kCGImagePropertyGIFLoopCount and
kCGImagePropertyAPNGLoopCount have the same value (CFSTR
"LoopCount"). Likewise for (Unclamped)DelayTime. */
if (type == NULL || gif_p)
{
CFDictionaryRef dict;
CFNumberRef num;
dict = CFDictionaryGetValue (src_props,
kCGImagePropertyGIFDictionary);
if (dict == NULL)
dict = CFDictionaryGetValue (src_props,
kCGImagePropertyPNGDictionary);
if (dict)
{
num = CFDictionaryGetValue (dict,
kCGImagePropertyGIFLoopCount);
if (num)
CFNumberGetValue (num, kCFNumberIntType, &loop_count);
}
dict = CFDictionaryGetValue (props,
kCGImagePropertyGIFDictionary);
if (dict == NULL)
dict = CFDictionaryGetValue (props,
kCGImagePropertyPNGDictionary);
if (dict)
{
/* Use the unclamped delay time if available. */
num = CFDictionaryGetValue (dict,
CFSTR ("UnclampedDelayTime"));
if (num == NULL)
num = CFDictionaryGetValue (dict,
kCGImagePropertyGIFDelayTime);
if (num)
CFNumberGetValue (num, kCFNumberDoubleType, &delay_time);
}
}
}
if (src_props)
CFRelease (src_props);
if (props)
CFRelease (props);
if (type == NULL || gif_p)
{
Lisp_Object extension_data = Qnil;
/* Save GIF image extension data.
Format is (0xff "NETSCAPE2.0" 0x00 DATA_SUB_BLOCK_FOR_LOOP_COUNT
0xf9 GRAPHIC_CONTROL_EXTENSION_BLOCK). */
if (delay_time >= 0)
{
Lisp_Object gce = make_uninit_string (4);
int centisec = delay_time * 100.0 + 0.5;
/* Fill the delay time field. */
SSET (gce, 1, centisec & 0xff);
SSET (gce, 2, (centisec >> 8) & 0xff);
/* We don't know about other fields. */
SSET (gce, 0, 0);
SSET (gce, 3, 0);
extension_data = Fcons (make_number (0xf9),
Fcons (gce,
extension_data));
}
if (loop_count >= 0)
{
Lisp_Object data_sub_block = make_uninit_string (3);
SSET (data_sub_block, 0, 0x01);
SSET (data_sub_block, 1, loop_count & 0xff);
SSET (data_sub_block, 2, (loop_count >> 8) & 0xff);
extension_data = Fcons (make_number (0),
Fcons (data_sub_block,
extension_data));
extension_data = Fcons (make_number (0xff),
Fcons (build_string ("NETSCAPE2.0"),
extension_data));
}
if (!NILP (extension_data))
metadata = Fcons (Qextension_data,
Fcons (extension_data,
metadata));
if (delay_time >= 0)
metadata = Fcons (Qdelay,
Fcons (make_float (delay_time),
metadata));
}
if ((type == NULL || gif_p || tiff_p) && count > 1)
metadata = Fcons (Qcount,
Fcons (make_number (count),
metadata));
CFRelease (source);
}
if (obj == NULL && type == NULL)
{
EmacsDocumentRef document;
options = NULL;
if (specified_type)
{
keys[0] = CFSTR ("UTI"); /* NSFileTypeDocumentOption */
options = CFDictionaryCreate (NULL, (const void **) keys,
(const void **) &specified_type, 1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
if (url)
document = mac_document_create_with_url (url, options);
else if (data)
document = mac_document_create_with_data (data, options);
else
document = NULL;
if (options)
CFRelease (options);
if (document)
{
Lisp_Object image = image_spec_value (img->spec, QCindex, NULL);
EMACS_INT ino = INTEGERP (image) ? XFASTINT (image) : 0;
size_t count = mac_document_get_page_count (document);
if (ino < count)
{
CGSize size;
CFDictionaryRef attributes;
mac_document_copy_page_info (document, ino, &size, &default_bg,
&attributes);
width = size.width;
height = size.height;
obj = document;
page_index = ino;
if (img->target_backing_scale == 0)
img->target_backing_scale = FRAME_BACKING_SCALE_FACTOR (f);
scale_factor = (img->target_backing_scale == 2 ? 2 : 1);
has_alpha_p = true;
if (default_bg == NULL)
{
/* Specify the clear color as the default
background, which actually results in the frame
background. We prefer not to use image masks for
rasterized documents because proper text
smoothing requires opaque background. */
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
default_bg =
CGColorRetain (CGColorGetConstantColor (kCGColorClear));
#else
CGFloat rgba[] = {0.0f, 0.0f, 0.0f, 0.0f};
default_bg = CGColorCreate (mac_cg_color_space_rgb, rgba);
#endif
}
if (attributes)
{
metadata = Fcons (Qdocument_attributes,
Fcons (cfobject_to_lisp (attributes, 0, -1),
Qnil));
CFRelease (attributes);
}
if (count > 1)
metadata = Fcons (Qcount,
Fcons (make_number (count),
metadata));
}
else
CFRelease (document);
}
}
if (url)
CFRelease (url);
else if (data)
CFRelease (data);
if (specified_type)
CFRelease (specified_type);
if (obj == NULL)
{
image_error ("Error reading image `%s'", img->spec, Qnil);
return 0;
}
if (type == NULL)
{
int desired_width, desired_height;
compute_image_size (width, height, img->spec,
&desired_width, &desired_height);
if (desired_width != -1 && desired_height != -1)
{
width = desired_width;
height = desired_height;
}
}
rectangle = CGRectMake (0, 0, width, height);
if (type == NULL)
{
Lisp_Object crop = image_spec_value (img->spec, QCcrop, NULL);
CGRect crop_rect;
if (CONSP (crop) && INTEGERP (XCAR (crop)))
{
crop_rect.size.width = XINT (XCAR (crop));
crop = XCDR (crop);
if (CONSP (crop) && INTEGERP (XCAR (crop)))
{
crop_rect.size.height = XINT (XCAR (crop));
crop = XCDR (crop);
if (CONSP (crop) && INTEGERP (XCAR (crop)))
{
crop_rect.origin.x = XINT (XCAR (crop));
crop = XCDR (crop);
if (CONSP (crop) && INTEGERP (XCAR (crop)))
{
crop_rect.origin.y = XINT (XCAR (crop));
/* Simulate MagickCropImage's behavior for zero
size and negative origin. */
if (crop_rect.size.width == 0)
crop_rect.size.width = width;
if (crop_rect.size.height == 0)
crop_rect.size.height = height;
if (crop_rect.origin.x < 0)
{
crop_rect.size.width += crop_rect.origin.x;
crop_rect.origin.x = 0;
}
if (crop_rect.origin.y < 0)
{
crop_rect.size.height += crop_rect.origin.y;
crop_rect.origin.y = 0;
}
crop_rect.origin.y = (CGRectGetMaxY (rectangle)
- CGRectGetMaxY (crop_rect));
crop_rect = CGRectIntersection (crop_rect, rectangle);
rectangle.origin.x = - CGRectGetMinX (crop_rect);
rectangle.origin.y = - CGRectGetMinY (crop_rect);
width = CGRectGetWidth (crop_rect);
height = CGRectGetHeight (crop_rect);
}
}
}
}
}
clip_rectangle = CGRectMake (0, 0, width, height);
transform = CGAffineTransformIdentity;
if (type == NULL)
{
Lisp_Object value = image_spec_value (img->spec, QCrotation, NULL);
if (FLOATP (value))
{
double rotation = XFLOAT_DATA (value);
CGRect rotated_clip;
CGFloat ceil_width, ceil_height;
transform = CGAffineTransformMakeRotation (- rotation * M_PI / 180);
rotated_clip = CGRectApplyAffineTransform (clip_rectangle, transform);
ceil_width = ceil (CGRectGetWidth (rotated_clip));
ceil_height = ceil (CGRectGetHeight (rotated_clip));
transform.tx = - (CGRectGetMinX (rotated_clip)
- (ceil_width - CGRectGetWidth (rotated_clip))/2);
transform.ty = - (CGRectGetMinY (rotated_clip)
- (ceil_height - CGRectGetHeight (rotated_clip))/2);
width = ceil_width;
height = ceil_height;
}
}
width *= scale_factor;
height *= scale_factor;
if (!check_image_size (f, width, height))
{
CFRelease (obj);
CGColorRelease (default_bg);
image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
return 0;
}
if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
{
CFRelease (obj);
CGColorRelease (default_bg);
image_error ("Out of memory (%s)", img->spec, Qnil);
return 0;
}
context = CGBitmapContextCreate (ximg->data, ximg->width, ximg->height, 8,
ximg->bytes_per_line,
mac_cg_color_space_rgb,
kCGImageAlphaNoneSkipFirst
| kCGBitmapByteOrder32Host);
mask_img = NULL;
if (has_alpha_p || !CGAffineTransformIsIdentity (transform))
{
Lisp_Object specified_bg;
XColor color;
CGFloat rgba[4];
CGColorRef cg_color;
specified_bg = image_spec_value (img->spec, QCbackground, NULL);
if (!STRINGP (specified_bg)
|| !mac_defined_color (f, SSDATA (specified_bg), &color, 0))
{
if (default_bg == NULL)
{
if (!x_create_x_image_and_pixmap (f, width, height, 1,
&mask_img, &img->mask))
{
CGContextRelease (context);
CFRelease (obj);
image_error ("Out of memory (%s)", img->spec, Qnil);
return 0;
}
CFRetain (obj);
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
group = dispatch_group_create ();
dispatch_group_async
(group,
dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT,
0), ^
#endif
{
CGContextRef mask_context =
CGBitmapContextCreate (mask_img->data,
mask_img->width, mask_img->height,
8, mask_img->bytes_per_line,
NULL, kCGImageAlphaOnly);
CGContextClearRect (mask_context,
CGRectMake (0, 0, width, height));
CGContextScaleCTM (mask_context, scale_factor, scale_factor);
CGContextConcatCTM (mask_context, transform);
CGContextClipToRect (mask_context, clip_rectangle);
if (CFGetTypeID (obj) == CGImageGetTypeID ())
CGContextDrawImage (mask_context, rectangle,
(CGImageRef) obj);
else
mac_document_draw_page (mask_context, rectangle,
(EmacsDocumentRef) obj,
page_index);
CGContextRelease (mask_context);
CFRelease (obj);
}
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
);
#endif
rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
}
else
{
color.pixel = FRAME_BACKGROUND_PIXEL (f);
color.red = RED16_FROM_ULONG (color.pixel);
color.green = GREEN16_FROM_ULONG (color.pixel);
color.blue = BLUE16_FROM_ULONG (color.pixel);
rgba[0] = (CGFloat) color.red / 65535.0f;
rgba[1] = (CGFloat) color.green / 65535.0f;
rgba[2] = (CGFloat) color.blue / 65535.0f;
rgba[3] = 1.0f;
}
}
else
{
rgba[0] = (CGFloat) color.red / 65535.0f;
rgba[1] = (CGFloat) color.green / 65535.0f;
rgba[2] = (CGFloat) color.blue / 65535.0f;
rgba[3] = 1.0f;
/* Background color is specified in the image spec.
Override default_bg. */
CGColorRelease (default_bg);
default_bg = NULL;
}
cg_color = CGColorCreate (mac_cg_color_space_rgb, rgba);
if (cg_color && (default_bg == NULL || CGColorGetAlpha (default_bg) != 1))
{
CGContextSetFillColorWithColor (context, cg_color);
CGContextFillRect (context, CGRectMake (0, 0, width, height));
}
CGColorRelease (cg_color);
if (default_bg && CGColorGetAlpha (default_bg) != 0)
{
CGContextSetFillColorWithColor (context, default_bg);
CGContextFillRect (context, CGRectMake (0, 0, width, height));
}
CGColorRelease (default_bg);
}
CGContextScaleCTM (context, scale_factor, scale_factor);
CGContextConcatCTM (context, transform);
CGContextClipToRect (context, clip_rectangle);
if (CFGetTypeID (obj) == CGImageGetTypeID ())
CGContextDrawImage (context, rectangle, (CGImageRef) obj);
else
mac_document_draw_page (context, rectangle, (EmacsDocumentRef) obj,
page_index);
CGContextRelease (context);
CFRelease (obj);
img->width = width;
img->height = height;
mac_postprocess_image_for_2x (img);
/* Maybe fill in the background field while we have ximg handy. */
if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
IMAGE_BACKGROUND (img, f, ximg);
img->lisp_data = metadata;
/* Put the image into the pixmap. */
x_put_x_image (f, ximg, img->pixmap, width, height);
x_destroy_x_image (ximg);
if (mask_img)
{
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
dispatch_group_wait (group, DISPATCH_TIME_FOREVER);
dispatch_release (group);
#endif
/* Fill in the background_transparent field while we have the
mask handy. */
image_background_transparent (img, f, mask_img);
x_put_x_image (f, mask_img, img->mask, width, height);
x_destroy_x_image (mask_img);
}
return 1;
}
#endif /* HAVE_MACGUI */
/***********************************************************************
XBM images
***********************************************************************/
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. */
enum xbm_keyword_index
{
XBM_TYPE,
XBM_FILE,
XBM_WIDTH,
XBM_HEIGHT,
XBM_DATA,
XBM_FOREGROUND,
XBM_BACKGROUND,
XBM_ASCENT,
XBM_MARGIN,
XBM_RELIEF,
XBM_ALGORITHM,
XBM_HEURISTIC_MASK,
XBM_MASK,
XBM_LAST
};
/* Vector of image_keyword structures describing the format
of valid XBM image specifications. */
static const struct image_keyword xbm_format[XBM_LAST] =
{
{":type", IMAGE_SYMBOL_VALUE, 1},
{":file", IMAGE_STRING_VALUE, 0},
{":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
{":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
{":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
{":background", IMAGE_STRING_OR_NIL_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 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},
{":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
};
/* Structure describing the image type XBM. */
static struct image_type xbm_type =
{
&Qxbm,
xbm_image_p,
xbm_load,
x_clear_image,
NULL,
NULL
};
/* Tokens returned from xbm_scan. */
enum xbm_token
{
XBM_TK_IDENT = 256,
XBM_TK_NUMBER
};
/* 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'.
If the specification specifies a file to load, it must contain
an entry `:file FILENAME' where FILENAME is a string.
If the specification is for a bitmap loaded from memory it must
contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
WIDTH and HEIGHT are integers > 0. DATA may be:
1. a string large enough to hold the bitmap data, i.e. it must
have a size >= (WIDTH + 7) / 8 * HEIGHT
2. a bool-vector of size >= WIDTH * HEIGHT
3. a vector of strings or bool-vectors, one for each line of the
bitmap.
4. a string containing an in-memory XBM file. WIDTH and HEIGHT
may not be specified in this case because they are defined in the
XBM file.
Both the file and data forms may contain the additional entries
`:background COLOR' and `:foreground COLOR'. If not present,
foreground and background of the frame on which the image is
displayed is used. */
static bool
xbm_image_p (Lisp_Object object)
{
struct image_keyword kw[XBM_LAST];
memcpy (kw, xbm_format, sizeof kw);
if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
return 0;
eassert (EQ (kw[XBM_TYPE].value, Qxbm));
if (kw[XBM_FILE].count)
{
if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
return 0;
}
else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
{
/* In-memory XBM file. */
if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
return 0;
}
else
{
Lisp_Object data;
int width, height;
/* Entries for `:width', `:height' and `:data' must be present. */
if (!kw[XBM_WIDTH].count
|| !kw[XBM_HEIGHT].count
|| !kw[XBM_DATA].count)
return 0;
data = kw[XBM_DATA].value;
width = XFASTINT (kw[XBM_WIDTH].value);
height = XFASTINT (kw[XBM_HEIGHT].value);
/* Check type of data, and width and height against contents of
data. */
if (VECTORP (data))
{
EMACS_INT i;
/* Number of elements of the vector must be >= height. */
if (ASIZE (data) < height)
return 0;
/* Each string or bool-vector in data must be large enough
for one line of the image. */
for (i = 0; i < height; ++i)
{
Lisp_Object elt = AREF (data, i);
if (STRINGP (elt))
{
if (SCHARS (elt)
< (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
return 0;
}
else if (BOOL_VECTOR_P (elt))
{
if (bool_vector_size (elt) < width)
return 0;
}
else
return 0;
}
}
else if (STRINGP (data))
{
if (SCHARS (data)
< (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
return 0;
}
else if (BOOL_VECTOR_P (data))
{
if (bool_vector_size (data) / height < width)
return 0;
}
else
return 0;
}
return 1;
}
/* Scan a bitmap file. FP is the stream to read from. Value is
either an enumerator from enum xbm_token, or a character for a
single-character token, or 0 at end of file. If scanning an
identifier, store the lexeme of the identifier in SVAL. If
scanning a number, store its value in *IVAL. */
static int
xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival)
{
unsigned int c;
loop:
/* Skip white space. */
while (*s < end && (c = *(*s)++, c_isspace (c)))
;
if (*s >= end)
c = 0;
else if (c_isdigit (c))
{
int value = 0, digit;
if (c == '0' && *s < end)
{
c = *(*s)++;
if (c == 'x' || c == 'X')
{
while (*s < end)
{
c = *(*s)++;
if (c_isdigit (c))
digit = c - '0';
else if (c >= 'a' && c <= 'f')
digit = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
digit = c - 'A' + 10;
else
break;
value = 16 * value + digit;
}
}
else if (c_isdigit (c))
{
value = c - '0';
while (*s < end
&& (c = *(*s)++, c_isdigit (c)))
value = 8 * value + c - '0';
}
}
else
{
value = c - '0';
while (*s < end
&& (c = *(*s)++, c_isdigit (c)))
value = 10 * value + c - '0';
}
if (*s < end)
*s = *s - 1;
*ival = value;
c = XBM_TK_NUMBER;
}
else if (c_isalpha (c) || c == '_')
{
*sval++ = c;
while (*s < end
&& (c = *(*s)++, (c_isalnum (c) || c == '_')))
*sval++ = c;
*sval = 0;
if (*s < end)
*s = *s - 1;
c = XBM_TK_IDENT;
}
else if (c == '/' && **s == '*')
{
/* C-style comment. */
++*s;
while (**s && (**s != '*' || *(*s + 1) != '/'))
++*s;
if (**s)
{
*s += 2;
goto loop;
}
}
return c;
}
#ifdef HAVE_NTGUI
/* Create a Windows bitmap from X bitmap data. */
static HBITMAP
w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
{
static unsigned char swap_nibble[16]
= { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
int i, j, w1, w2;
unsigned char *bits, *p;
HBITMAP bmp;
w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
bits = alloca (height * w2);
memset (bits, 0, height * w2);
for (i = 0; i < height; i++)
{
p = bits + i*w2;
for (j = 0; j < w1; j++)
{
/* Bitswap XBM bytes to match how Windows does things. */
unsigned char c = *data++;
*p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
| (swap_nibble[(c>>4) & 0xf]));
}
}
bmp = CreateBitmap (width, height, 1, 1, (char *) bits);
return bmp;
}
static void
convert_mono_to_color_image (struct frame *f, struct image *img,
COLORREF foreground, COLORREF background)
{
HDC hdc, old_img_dc, new_img_dc;
HGDIOBJ old_prev, new_prev;
HBITMAP new_pixmap;
hdc = get_frame_dc (f);
old_img_dc = CreateCompatibleDC (hdc);
new_img_dc = CreateCompatibleDC (hdc);
new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
release_frame_dc (f, hdc);
old_prev = SelectObject (old_img_dc, img->pixmap);
new_prev = SelectObject (new_img_dc, new_pixmap);
/* Windows convention for mono bitmaps is black = background,
white = foreground. */
SetTextColor (new_img_dc, background);
SetBkColor (new_img_dc, foreground);
BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
0, 0, SRCCOPY);
SelectObject (old_img_dc, old_prev);
SelectObject (new_img_dc, new_prev);
DeleteDC (old_img_dc);
DeleteDC (new_img_dc);
DeleteObject (img->pixmap);
if (new_pixmap == 0)
fprintf (stderr, "Failed to convert image to color.\n");
else
img->pixmap = new_pixmap;
}
#define XBM_BIT_SHUFFLE(b) (~(b))
#else
#define XBM_BIT_SHUFFLE(b) (b)
#endif /* HAVE_NTGUI */
static void
Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data,
RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg,
bool non_default_colors)
{
#ifdef HAVE_NTGUI
img->pixmap
= w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
/* If colors were specified, transfer the bitmap to a color one. */
if (non_default_colors)
convert_mono_to_color_image (f, img, fg, bg);
#elif defined (HAVE_NS)
img->pixmap = ns_image_from_XBM (data, img->width, img->height);
#else
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)))
: NO_PIXMAP);
#endif /* !HAVE_NTGUI && !HAVE_NS */
}
/* Replacement for XReadBitmapFileData which isn't available under old
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 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 bool
xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end,
int *width, int *height, char **data,
bool inhibit_image_error)
{
unsigned char *s = contents;
char buffer[BUFSIZ];
bool padding_p = 0;
bool v10 = 0;
int bytes_per_line, i, nbytes;
char *p;
int value;
int LA1;
#define match() \
LA1 = xbm_scan (&s, end, buffer, &value)
#define expect(TOKEN) \
do \
{ \
if (LA1 != (TOKEN)) \
goto failure; \
match (); \
} \
while (0)
#define expect_ident(IDENT) \
if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
match (); \
else \
goto failure
*width = *height = -1;
if (data)
*data = NULL;
LA1 = xbm_scan (&s, end, buffer, &value);
/* Parse defines for width, height and hot-spots. */
while (LA1 == '#')
{
match ();
expect_ident ("define");
expect (XBM_TK_IDENT);
if (LA1 == XBM_TK_NUMBER)
{
char *q = strrchr (buffer, '_');
q = q ? q + 1 : buffer;
if (strcmp (q, "width") == 0)
*width = value;
else if (strcmp (q, "height") == 0)
*height = value;
}
expect (XBM_TK_NUMBER);
}
if (!check_image_size (f, *width, *height))
{
if (!inhibit_image_error)
image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
goto failure;
}
else if (data == NULL)
goto success;
/* Parse bits. Must start with `static'. */
expect_ident ("static");
if (LA1 == XBM_TK_IDENT)
{
if (strcmp (buffer, "unsigned") == 0)
{
match ();
expect_ident ("char");
}
else if (strcmp (buffer, "short") == 0)
{
match ();
v10 = 1;
if (*width % 16 && *width % 16 < 9)
padding_p = 1;
}
else if (strcmp (buffer, "char") == 0)
match ();
else
goto failure;
}
else
goto failure;
expect (XBM_TK_IDENT);
expect ('[');
expect (']');
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 = xmalloc (nbytes);
if (v10)
{
for (i = 0; i < nbytes; i += 2)
{
int val = value;
expect (XBM_TK_NUMBER);
*p++ = XBM_BIT_SHUFFLE (val);
if (!padding_p || ((i + 2) % bytes_per_line))
*p++ = XBM_BIT_SHUFFLE (value >> 8);
if (LA1 == ',' || LA1 == '}')
match ();
else
goto failure;
}
}
else
{
for (i = 0; i < nbytes; ++i)
{
int val = value;
expect (XBM_TK_NUMBER);
*p++ = XBM_BIT_SHUFFLE (val);
if (LA1 == ',' || LA1 == '}')
match ();
else
goto failure;
}
}
success:
return 1;
failure:
if (data && *data)
{
xfree (*data);
*data = NULL;
}
return 0;
#undef match
#undef expect
#undef expect_ident
}
/* Load XBM image IMG which will be displayed on frame F from buffer
CONTENTS. END is the end of the buffer. Value is true if
successful. */
static bool
xbm_load_image (struct frame *f, struct image *img, unsigned char *contents,
unsigned char *end)
{
bool rc;
char *data;
bool success_p = 0;
rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height,
&data, 0);
if (rc)
{
unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
unsigned long background = FRAME_BACKGROUND_PIXEL (f);
bool non_default_colors = 0;
Lisp_Object value;
eassert (img->width > 0 && img->height > 0);
/* Get foreground and background colors, maybe allocate colors. */
value = image_spec_value (img->spec, QCforeground, NULL);
if (!NILP (value))
{
foreground = x_alloc_image_color (f, img, value, foreground);
non_default_colors = 1;
}
value = image_spec_value (img->spec, QCbackground, NULL);
if (!NILP (value))
{
background = x_alloc_image_color (f, img, value, background);
img->background = background;
img->background_valid = 1;
non_default_colors = 1;
}
Create_Pixmap_From_Bitmap_Data (f, img, data,
foreground, background,
non_default_colors);
xfree (data);
if (img->pixmap == NO_PIXMAP)
{
x_clear_image (f, img);
image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
}
else
success_p = 1;
}
else
image_error ("Error loading XBM image `%s'", img->spec, Qnil);
return success_p;
}
/* Value is true if DATA looks like an in-memory XBM file. */
static bool
xbm_file_p (Lisp_Object data)
{
int w, h;
return (STRINGP (data)
&& xbm_read_bitmap_data (NULL, SDATA (data),
(SDATA (data) + SBYTES (data)),
&w, &h, NULL, 1));
}
/* Fill image IMG which is used on frame F with pixmap data. Value is
true if successful. */
static bool
xbm_load (struct frame *f, struct image *img)
{
bool success_p = 0;
Lisp_Object file_name;
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);
if (STRINGP (file_name))
{
Lisp_Object file;
unsigned char *contents;
ptrdiff_t size;
file = x_find_image_file (file_name);
if (!STRINGP (file))
{
image_error ("Cannot find image file `%s'", file_name, Qnil);
return 0;
}
#ifdef HAVE_MACGUI
file = mac_preprocess_image_for_2x_file (f, img, file);
#endif
contents = slurp_file (SSDATA (file), &size);
if (contents == NULL)
{
image_error ("Error loading XBM image `%s'", img->spec, Qnil);
return 0;
}
success_p = xbm_load_image (f, img, contents, contents + size);
xfree (contents);
#ifdef HAVE_MACGUI
mac_postprocess_image_for_2x (img);
#endif
}
else
{
struct image_keyword fmt[XBM_LAST];
Lisp_Object data;
unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
unsigned long background = FRAME_BACKGROUND_PIXEL (f);
bool non_default_colors = 0;
char *bits;
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);
in_memory_file_p = xbm_file_p (data);
/* Parse the image specification. */
memcpy (fmt, xbm_format, sizeof fmt);
parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
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);
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')",
Qnil, Qnil);
return 0;
}
}
/* Get foreground and background colors, maybe allocate colors. */
if (fmt[XBM_FOREGROUND].count
&& STRINGP (fmt[XBM_FOREGROUND].value))
{
foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
foreground);
non_default_colors = 1;
}
if (fmt[XBM_BACKGROUND].count
&& STRINGP (fmt[XBM_BACKGROUND].value))
{
background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
background);
non_default_colors = 1;
}
if (in_memory_file_p)
success_p = xbm_load_image (f, img, SDATA (data),
(SDATA (data)
+ SBYTES (data)));
else
{
if (VECTORP (data))
{
int i;
char *p;
int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
p = bits = alloca (nbytes * img->height);
for (i = 0; i < img->height; ++i, p += nbytes)
{
Lisp_Object line = AREF (data, i);
if (STRINGP (line))
memcpy (p, SDATA (line), nbytes);
else
memcpy (p, bool_vector_data (line), nbytes);
}
}
else if (STRINGP (data))
bits = SSDATA (data);
else
bits = (char *) bool_vector_data (data);
#ifdef HAVE_NTGUI
{
char *invertedBits;
int nbytes, i;
/* Windows mono bitmaps are reversed compared with X. */
invertedBits = bits;
nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR
* img->height;
bits = alloca (nbytes);
for (i = 0; i < nbytes; i++)
bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]);
}
#endif
/* Create the pixmap. */
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
{
image_error ("Unable to create pixmap for XBM image `%s'",
img->spec, Qnil);
x_clear_image (f, img);
}
}
}
return success_p;
}
/***********************************************************************
XPM images
***********************************************************************/
#if defined (HAVE_XPM) || defined (HAVE_MACGUI) || defined (HAVE_NS)
static bool xpm_image_p (Lisp_Object object);
static bool xpm_load (struct frame *f, struct image *img);
#endif /* HAVE_XPM || HAVE_MACGUI || HAVE_NS */
#ifdef HAVE_XPM
#ifdef HAVE_NTGUI
/* Indicate to xpm.h that we don't have Xlib. */
#define FOR_MSW
/* simx.h in xpm defines XColor and XImage differently than Emacs. */
/* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
#define XColor xpm_XColor
#define XImage xpm_XImage
#define Display xpm_Display
#define PIXEL_ALREADY_TYPEDEFED
#include "X11/xpm.h"
#undef FOR_MSW
#undef XColor
#undef XImage
#undef Display
#undef PIXEL_ALREADY_TYPEDEFED
#else
#include "X11/xpm.h"
#endif /* HAVE_NTGUI */
#endif /* HAVE_XPM */
#if defined (HAVE_XPM) || defined (HAVE_MACGUI) || defined (HAVE_NS)
/* The symbol `xpm' identifying XPM-format images. */
static Lisp_Object Qxpm;
/* Indices of image specification fields in xpm_format, below. */
enum xpm_keyword_index
{
XPM_TYPE,
XPM_FILE,
XPM_DATA,
XPM_ASCENT,
XPM_MARGIN,
XPM_RELIEF,
XPM_ALGORITHM,
XPM_HEURISTIC_MASK,
XPM_MASK,
XPM_COLOR_SYMBOLS,
XPM_BACKGROUND,
XPM_LAST
};
/* Vector of image_keyword structures describing the format
of valid XPM image specifications. */
static const struct image_keyword xpm_format[XPM_LAST] =
{
{":type", IMAGE_SYMBOL_VALUE, 1},
{":file", IMAGE_STRING_VALUE, 0},
{":data", IMAGE_STRING_VALUE, 0},
{":ascent", IMAGE_ASCENT_VALUE, 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},
{":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
{":background", IMAGE_STRING_OR_NIL_VALUE, 0}
};
#if defined HAVE_NTGUI && defined WINDOWSNT
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 =
{
&Qxpm,
xpm_image_p,
xpm_load,
x_clear_image,
init_xpm_functions,
NULL
};
#ifdef HAVE_X_WINDOWS
/* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
functions for allocating image colors. Our own functions handle
color allocation failures more gracefully than the ones on the XPM
lib. */
#if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
#define ALLOC_XPM_COLORS
#endif
#endif /* HAVE_X_WINDOWS */
#ifdef ALLOC_XPM_COLORS
static struct xpm_cached_color *xpm_cache_color (struct frame *, char *,
XColor *, int);
/* An entry in a hash table used to cache color definitions of named
colors. This cache is necessary to speed up XPM image loading in
case we do color allocations ourselves. Without it, we would need
a call to XParseColor per pixel in the image. */
struct xpm_cached_color
{
/* Next in collision chain. */
struct xpm_cached_color *next;
/* Color definition (RGB and pixel color). */
XColor color;
/* Color name. */
char name[FLEXIBLE_ARRAY_MEMBER];
};
/* The hash table used for the color cache, and its bucket vector
size. */
#define XPM_COLOR_CACHE_BUCKETS 1001
static struct xpm_cached_color **xpm_color_cache;
/* Initialize the color cache. */
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 = xzalloc (nbytes);
init_color_table ();
if (attrs->valuemask & XpmColorSymbols)
{
int i;
XColor color;
for (i = 0; i < attrs->numsymbols; ++i)
if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
attrs->colorsymbols[i].value, &color))
{
color.pixel = lookup_rgb_color (f, color.red, color.green,
color.blue);
xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
}
}
}
/* Free the color cache. */
static void
xpm_free_color_cache (void)
{
struct xpm_cached_color *p, *next;
int i;
for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
for (p = xpm_color_cache[i]; p; p = next)
{
next = p->next;
xfree (p);
}
xfree (xpm_color_cache);
xpm_color_cache = NULL;
free_color_table ();
}
/* Return the bucket index for color named COLOR_NAME in the color
cache. */
static int
xpm_color_bucket (char *color_name)
{
EMACS_UINT hash = hash_string (color_name, strlen (color_name));
return hash % XPM_COLOR_CACHE_BUCKETS;
}
/* On frame F, cache values COLOR for color with name COLOR_NAME.
BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
entry added. */
static struct xpm_cached_color *
xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket)
{
size_t nbytes;
struct xpm_cached_color *p;
if (bucket < 0)
bucket = xpm_color_bucket (color_name);
nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1;
p = xmalloc (nbytes);
strcpy (p->name, color_name);
p->color = *color;
p->next = xpm_color_cache[bucket];
xpm_color_cache[bucket] = p;
return p;
}