Typo fix.
[emacs.git] / src / image.c
blob706745fb479c1ea93fd872aa243871f82026605f
1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include "sysstdio.h"
22 #include <unistd.h>
24 #ifdef HAVE_PNG
25 #if defined HAVE_LIBPNG_PNG_H
26 # include <libpng/png.h>
27 #else
28 # include <png.h>
29 #endif
30 #endif
32 #include <setjmp.h>
33 #include <c-ctype.h>
35 #include "lisp.h"
36 #include "frame.h"
37 #include "window.h"
38 #include "dispextern.h"
39 #include "blockinput.h"
40 #include "systime.h"
41 #include <epaths.h>
42 #include "character.h"
43 #include "coding.h"
44 #include "termhooks.h"
45 #include "font.h"
47 #ifdef HAVE_SYS_STAT_H
48 #include <sys/stat.h>
49 #endif /* HAVE_SYS_STAT_H */
51 #ifdef HAVE_SYS_TYPES_H
52 #include <sys/types.h>
53 #endif /* HAVE_SYS_TYPES_H */
55 #ifdef HAVE_WINDOW_SYSTEM
56 #include TERM_HEADER
57 #endif /* HAVE_WINDOW_SYSTEM */
59 #ifdef HAVE_X_WINDOWS
60 #define COLOR_TABLE_SUPPORT 1
62 typedef struct x_bitmap_record Bitmap_Record;
63 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
64 #define NO_PIXMAP None
66 #define PIX_MASK_RETAIN 0
67 #define PIX_MASK_DRAW 1
68 #endif /* HAVE_X_WINDOWS */
70 #ifdef HAVE_NTGUI
72 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
73 #ifdef WINDOWSNT
74 # include "w32.h"
75 #endif
77 /* W32_TODO : Color tables on W32. */
78 #undef COLOR_TABLE_SUPPORT
80 typedef struct w32_bitmap_record Bitmap_Record;
81 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
82 #define NO_PIXMAP 0
84 #define PIX_MASK_RETAIN 0
85 #define PIX_MASK_DRAW 1
87 #define x_defined_color w32_defined_color
88 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
90 /* Versions of libpng, libgif, and libjpeg that we were compiled with,
91 or -1 if no PNG/GIF support was compiled in. This is tested by
92 w32-win.el to correctly set up the alist used to search for the
93 respective image libraries. */
94 Lisp_Object Qlibpng_version, Qlibgif_version, Qlibjpeg_version;
96 #endif /* HAVE_NTGUI */
98 #ifdef HAVE_NS
99 #undef COLOR_TABLE_SUPPORT
101 typedef struct ns_bitmap_record Bitmap_Record;
103 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
104 #define NO_PIXMAP 0
106 #define PIX_MASK_RETAIN 0
107 #define PIX_MASK_DRAW 1
109 #define x_defined_color(f, name, color_def, alloc) \
110 ns_defined_color (f, name, color_def, alloc, 0)
111 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
112 #endif /* HAVE_NS */
115 /* The symbol `postscript' identifying images of this type. */
117 static Lisp_Object Qpostscript;
119 static void x_disable_image (struct frame *, struct image *);
120 static void x_edge_detection (struct frame *, struct image *, Lisp_Object,
121 Lisp_Object);
123 static void init_color_table (void);
124 static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b);
125 #ifdef COLOR_TABLE_SUPPORT
126 static void free_color_table (void);
127 static unsigned long *colors_in_color_table (int *n);
128 #endif
130 static Lisp_Object QCmax_width, QCmax_height;
132 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
133 id, which is just an int that this section returns. Bitmaps are
134 reference counted so they can be shared among frames.
136 Bitmap indices are guaranteed to be > 0, so a negative number can
137 be used to indicate no bitmap.
139 If you use x_create_bitmap_from_data, then you must keep track of
140 the bitmaps yourself. That is, creating a bitmap from the same
141 data more than once will not be caught. */
143 #ifdef HAVE_NS
144 /* Use with images created by ns_image_for_XPM. */
145 static unsigned long
146 XGetPixel (XImagePtr ximage, int x, int y)
148 return ns_get_pixel (ximage, x, y);
151 /* Use with images created by ns_image_for_XPM; alpha set to 1;
152 pixel is assumed to be in RGB form. */
153 static void
154 XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel)
156 ns_put_pixel (ximage, x, y, pixel);
158 #endif /* HAVE_NS */
161 /* Functions to access the contents of a bitmap, given an id. */
163 static int
164 x_bitmap_height (struct frame *f, ptrdiff_t id)
166 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].height;
169 static int
170 x_bitmap_width (struct frame *f, ptrdiff_t id)
172 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].width;
175 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
176 ptrdiff_t
177 x_bitmap_pixmap (struct frame *f, ptrdiff_t id)
179 /* HAVE_NTGUI needs the explicit cast here. */
180 return (ptrdiff_t) FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
182 #endif
184 #ifdef HAVE_X_WINDOWS
186 x_bitmap_mask (struct frame *f, ptrdiff_t id)
188 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
190 #endif
192 /* Allocate a new bitmap record. Returns index of new record. */
194 static ptrdiff_t
195 x_allocate_bitmap_record (struct frame *f)
197 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
198 ptrdiff_t i;
200 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
201 return ++dpyinfo->bitmaps_last;
203 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
204 if (dpyinfo->bitmaps[i].refcount == 0)
205 return i + 1;
207 dpyinfo->bitmaps =
208 xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size,
209 10, -1, sizeof *dpyinfo->bitmaps);
210 return ++dpyinfo->bitmaps_last;
213 /* Add one reference to the reference count of the bitmap with id ID. */
215 void
216 x_reference_bitmap (struct frame *f, ptrdiff_t id)
218 ++FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
221 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
223 ptrdiff_t
224 x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height)
226 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
227 ptrdiff_t id;
229 #ifdef HAVE_X_WINDOWS
230 Pixmap bitmap;
231 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
232 bits, width, height);
233 if (! bitmap)
234 return -1;
235 #endif /* HAVE_X_WINDOWS */
237 #ifdef HAVE_NTGUI
238 Pixmap bitmap;
239 bitmap = CreateBitmap (width, height,
240 FRAME_DISPLAY_INFO (XFRAME (frame))->n_planes,
241 FRAME_DISPLAY_INFO (XFRAME (frame))->n_cbits,
242 bits);
243 if (! bitmap)
244 return -1;
245 #endif /* HAVE_NTGUI */
247 #ifdef HAVE_NS
248 void *bitmap = ns_image_from_XBM (bits, width, height);
249 if (!bitmap)
250 return -1;
251 #endif
253 id = x_allocate_bitmap_record (f);
255 #ifdef HAVE_NS
256 dpyinfo->bitmaps[id - 1].img = bitmap;
257 dpyinfo->bitmaps[id - 1].depth = 1;
258 #endif
260 dpyinfo->bitmaps[id - 1].file = NULL;
261 dpyinfo->bitmaps[id - 1].height = height;
262 dpyinfo->bitmaps[id - 1].width = width;
263 dpyinfo->bitmaps[id - 1].refcount = 1;
265 #ifdef HAVE_X_WINDOWS
266 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
267 dpyinfo->bitmaps[id - 1].have_mask = false;
268 dpyinfo->bitmaps[id - 1].depth = 1;
269 #endif /* HAVE_X_WINDOWS */
271 #ifdef HAVE_NTGUI
272 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
273 dpyinfo->bitmaps[id - 1].hinst = NULL;
274 dpyinfo->bitmaps[id - 1].depth = 1;
275 #endif /* HAVE_NTGUI */
277 return id;
280 /* Create bitmap from file FILE for frame F. */
282 ptrdiff_t
283 x_create_bitmap_from_file (struct frame *f, Lisp_Object file)
285 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
287 #ifdef HAVE_NTGUI
288 return -1; /* W32_TODO : bitmap support */
289 #endif /* HAVE_NTGUI */
291 #ifdef HAVE_NS
292 ptrdiff_t id;
293 void *bitmap = ns_image_from_file (file);
295 if (!bitmap)
296 return -1;
299 id = x_allocate_bitmap_record (f);
300 dpyinfo->bitmaps[id - 1].img = bitmap;
301 dpyinfo->bitmaps[id - 1].refcount = 1;
302 dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
303 dpyinfo->bitmaps[id - 1].depth = 1;
304 dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap);
305 dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap);
306 return id;
307 #endif
309 #ifdef HAVE_X_WINDOWS
310 unsigned int width, height;
311 Pixmap bitmap;
312 int xhot, yhot, result;
313 ptrdiff_t id;
314 Lisp_Object found;
315 char *filename;
317 /* Look for an existing bitmap with the same name. */
318 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
320 if (dpyinfo->bitmaps[id].refcount
321 && dpyinfo->bitmaps[id].file
322 && !strcmp (dpyinfo->bitmaps[id].file, SSDATA (file)))
324 ++dpyinfo->bitmaps[id].refcount;
325 return id + 1;
329 /* Search bitmap-file-path for the file, if appropriate. */
330 if (openp (Vx_bitmap_file_path, file, Qnil, &found,
331 make_number (R_OK), false)
332 < 0)
333 return -1;
335 filename = SSDATA (found);
337 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
338 filename, &width, &height, &bitmap, &xhot, &yhot);
339 if (result != BitmapSuccess)
340 return -1;
342 id = x_allocate_bitmap_record (f);
343 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
344 dpyinfo->bitmaps[id - 1].have_mask = false;
345 dpyinfo->bitmaps[id - 1].refcount = 1;
346 dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
347 dpyinfo->bitmaps[id - 1].depth = 1;
348 dpyinfo->bitmaps[id - 1].height = height;
349 dpyinfo->bitmaps[id - 1].width = width;
351 return id;
352 #endif /* HAVE_X_WINDOWS */
355 /* Free bitmap B. */
357 static void
358 free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm)
360 #ifdef HAVE_X_WINDOWS
361 XFreePixmap (dpyinfo->display, bm->pixmap);
362 if (bm->have_mask)
363 XFreePixmap (dpyinfo->display, bm->mask);
364 #endif /* HAVE_X_WINDOWS */
366 #ifdef HAVE_NTGUI
367 DeleteObject (bm->pixmap);
368 #endif /* HAVE_NTGUI */
370 #ifdef HAVE_NS
371 ns_release_object (bm->img);
372 #endif
374 if (bm->file)
376 xfree (bm->file);
377 bm->file = NULL;
381 /* Remove reference to bitmap with id number ID. */
383 void
384 x_destroy_bitmap (struct frame *f, ptrdiff_t id)
386 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
388 if (id > 0)
390 Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1];
392 if (--bm->refcount == 0)
394 block_input ();
395 free_bitmap_record (dpyinfo, bm);
396 unblock_input ();
401 /* Free all the bitmaps for the display specified by DPYINFO. */
403 void
404 x_destroy_all_bitmaps (Display_Info *dpyinfo)
406 ptrdiff_t i;
407 Bitmap_Record *bm = dpyinfo->bitmaps;
409 for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++)
410 if (bm->refcount > 0)
411 free_bitmap_record (dpyinfo, bm);
413 dpyinfo->bitmaps_last = 0;
416 static bool x_create_x_image_and_pixmap (struct frame *, int, int, int,
417 XImagePtr *, Pixmap *);
418 static void x_destroy_x_image (XImagePtr ximg);
420 #ifdef HAVE_NTGUI
421 static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *,
422 bool, HGDIOBJ *);
423 static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC,
424 HGDIOBJ);
425 #else
426 static XImagePtr image_get_x_image (struct frame *, struct image *, bool);
427 static void image_unget_x_image (struct image *, bool, XImagePtr);
428 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
429 image_get_x_image (f, img, mask_p)
430 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
431 image_unget_x_image (img, mask_p, ximg)
432 #endif
434 #ifdef HAVE_X_WINDOWS
436 static void image_sync_to_pixmaps (struct frame *, struct image *);
438 /* Useful functions defined in the section
439 `Image type independent image structures' below. */
441 static unsigned long four_corners_best (XImagePtr ximg,
442 int *corners,
443 unsigned long width,
444 unsigned long height);
447 /* Create a mask of a bitmap. Note is this not a perfect mask.
448 It's nicer with some borders in this context */
450 void
451 x_create_bitmap_mask (struct frame *f, ptrdiff_t id)
453 Pixmap pixmap, mask;
454 XImagePtr ximg, mask_img;
455 unsigned long width, height;
456 bool result;
457 unsigned long bg;
458 unsigned long x, y, xp, xm, yp, ym;
459 GC gc;
461 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
463 if (!(id > 0))
464 return;
466 pixmap = x_bitmap_pixmap (f, id);
467 width = x_bitmap_width (f, id);
468 height = x_bitmap_height (f, id);
470 block_input ();
471 ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
472 ~0, ZPixmap);
474 if (!ximg)
476 unblock_input ();
477 return;
480 result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);
482 unblock_input ();
483 if (!result)
485 XDestroyImage (ximg);
486 return;
489 bg = four_corners_best (ximg, NULL, width, height);
491 for (y = 0; y < ximg->height; ++y)
493 for (x = 0; x < ximg->width; ++x)
495 xp = x != ximg->width - 1 ? x + 1 : 0;
496 xm = x != 0 ? x - 1 : ximg->width - 1;
497 yp = y != ximg->height - 1 ? y + 1 : 0;
498 ym = y != 0 ? y - 1 : ximg->height - 1;
499 if (XGetPixel (ximg, x, y) == bg
500 && XGetPixel (ximg, x, yp) == bg
501 && XGetPixel (ximg, x, ym) == bg
502 && XGetPixel (ximg, xp, y) == bg
503 && XGetPixel (ximg, xp, yp) == bg
504 && XGetPixel (ximg, xp, ym) == bg
505 && XGetPixel (ximg, xm, y) == bg
506 && XGetPixel (ximg, xm, yp) == bg
507 && XGetPixel (ximg, xm, ym) == bg)
508 XPutPixel (mask_img, x, y, 0);
509 else
510 XPutPixel (mask_img, x, y, 1);
514 eassert (input_blocked_p ());
515 gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL);
516 XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0,
517 width, height);
518 XFreeGC (FRAME_X_DISPLAY (f), gc);
520 dpyinfo->bitmaps[id - 1].have_mask = true;
521 dpyinfo->bitmaps[id - 1].mask = mask;
523 XDestroyImage (ximg);
524 x_destroy_x_image (mask_img);
527 #endif /* HAVE_X_WINDOWS */
530 /***********************************************************************
531 Image types
532 ***********************************************************************/
534 /* List of supported image types. Use define_image_type to add new
535 types. Use lookup_image_type to find a type for a given symbol. */
537 static struct image_type *image_types;
539 /* The symbol `xbm' which is used as the type symbol for XBM images. */
541 static Lisp_Object Qxbm;
543 /* Keywords. */
545 Lisp_Object QCascent, QCmargin, QCrelief;
546 Lisp_Object QCconversion;
547 static Lisp_Object QCheuristic_mask;
548 static Lisp_Object QCcolor_symbols;
549 static Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry;
550 static Lisp_Object QCcrop, QCrotation;
552 /* Other symbols. */
554 static Lisp_Object Qcount, Qextension_data, Qdelay;
555 static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
557 /* Forward function prototypes. */
559 static struct image_type *lookup_image_type (Lisp_Object);
560 static void x_laplace (struct frame *, struct image *);
561 static void x_emboss (struct frame *, struct image *);
562 static void x_build_heuristic_mask (struct frame *, struct image *,
563 Lisp_Object);
564 #ifdef WINDOWSNT
565 #define CACHE_IMAGE_TYPE(type, status) \
566 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
567 #else
568 #define CACHE_IMAGE_TYPE(type, status)
569 #endif
571 #define ADD_IMAGE_TYPE(type) \
572 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
574 /* Define a new image type from TYPE. This adds a copy of TYPE to
575 image_types and caches the loading status of TYPE. */
577 static struct image_type *
578 define_image_type (struct image_type *type)
580 struct image_type *p = NULL;
581 Lisp_Object target_type = *type->type;
582 bool type_valid = 1;
584 block_input ();
586 for (p = image_types; p; p = p->next)
587 if (EQ (*p->type, target_type))
588 goto done;
590 if (type->init)
592 #if defined HAVE_NTGUI && defined WINDOWSNT
593 /* If we failed to load the library before, don't try again. */
594 Lisp_Object tested = Fassq (target_type, Vlibrary_cache);
595 if (CONSP (tested) && NILP (XCDR (tested)))
596 type_valid = 0;
597 else
598 #endif
600 type_valid = type->init ();
601 CACHE_IMAGE_TYPE (target_type, type_valid ? Qt : Qnil);
605 if (type_valid)
607 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
608 The initialized data segment is read-only. */
609 p = xmalloc (sizeof *p);
610 *p = *type;
611 p->next = image_types;
612 image_types = p;
615 done:
616 unblock_input ();
617 return p;
621 /* Value is true if OBJECT is a valid Lisp image specification. A
622 valid image specification is a list whose car is the symbol
623 `image', and whose rest is a property list. The property list must
624 contain a value for key `:type'. That value must be the name of a
625 supported image type. The rest of the property list depends on the
626 image type. */
628 bool
629 valid_image_p (Lisp_Object object)
631 bool valid_p = 0;
633 if (IMAGEP (object))
635 Lisp_Object tem;
637 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
638 if (EQ (XCAR (tem), QCtype))
640 tem = XCDR (tem);
641 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
643 struct image_type *type;
644 type = lookup_image_type (XCAR (tem));
645 if (type)
646 valid_p = type->valid_p (object);
649 break;
653 return valid_p;
657 /* Log error message with format string FORMAT and argument ARG.
658 Signaling an error, e.g. when an image cannot be loaded, is not a
659 good idea because this would interrupt redisplay, and the error
660 message display would lead to another redisplay. This function
661 therefore simply displays a message. */
663 static void
664 image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2)
666 add_to_log (format, arg1, arg2);
671 /***********************************************************************
672 Image specifications
673 ***********************************************************************/
675 enum image_value_type
677 IMAGE_DONT_CHECK_VALUE_TYPE,
678 IMAGE_STRING_VALUE,
679 IMAGE_STRING_OR_NIL_VALUE,
680 IMAGE_SYMBOL_VALUE,
681 IMAGE_POSITIVE_INTEGER_VALUE,
682 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR,
683 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
684 IMAGE_ASCENT_VALUE,
685 IMAGE_INTEGER_VALUE,
686 IMAGE_FUNCTION_VALUE,
687 IMAGE_NUMBER_VALUE,
688 IMAGE_BOOL_VALUE
691 /* Structure used when parsing image specifications. */
693 struct image_keyword
695 /* Name of keyword. */
696 const char *name;
698 /* The type of value allowed. */
699 enum image_value_type type;
701 /* True means key must be present. */
702 bool mandatory_p;
704 /* Used to recognize duplicate keywords in a property list. */
705 int count;
707 /* The value that was found. */
708 Lisp_Object value;
712 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
713 has the format (image KEYWORD VALUE ...). One of the keyword/
714 value pairs must be `:type TYPE'. KEYWORDS is a vector of
715 image_keywords structures of size NKEYWORDS describing other
716 allowed keyword/value pairs. Value is true if SPEC is valid. */
718 static bool
719 parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
720 int nkeywords, Lisp_Object type)
722 int i;
723 Lisp_Object plist;
725 if (!IMAGEP (spec))
726 return 0;
728 plist = XCDR (spec);
729 while (CONSP (plist))
731 Lisp_Object key, value;
733 /* First element of a pair must be a symbol. */
734 key = XCAR (plist);
735 plist = XCDR (plist);
736 if (!SYMBOLP (key))
737 return 0;
739 /* There must follow a value. */
740 if (!CONSP (plist))
741 return 0;
742 value = XCAR (plist);
743 plist = XCDR (plist);
745 /* Find key in KEYWORDS. Error if not found. */
746 for (i = 0; i < nkeywords; ++i)
747 if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0)
748 break;
750 if (i == nkeywords)
751 continue;
753 /* Record that we recognized the keyword. If a keywords
754 was found more than once, it's an error. */
755 keywords[i].value = value;
756 if (keywords[i].count > 1)
757 return 0;
758 ++keywords[i].count;
760 /* Check type of value against allowed type. */
761 switch (keywords[i].type)
763 case IMAGE_STRING_VALUE:
764 if (!STRINGP (value))
765 return 0;
766 break;
768 case IMAGE_STRING_OR_NIL_VALUE:
769 if (!STRINGP (value) && !NILP (value))
770 return 0;
771 break;
773 case IMAGE_SYMBOL_VALUE:
774 if (!SYMBOLP (value))
775 return 0;
776 break;
778 case IMAGE_POSITIVE_INTEGER_VALUE:
779 if (! RANGED_INTEGERP (1, value, INT_MAX))
780 return 0;
781 break;
783 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR:
784 if (RANGED_INTEGERP (0, value, INT_MAX))
785 break;
786 if (CONSP (value)
787 && RANGED_INTEGERP (0, XCAR (value), INT_MAX)
788 && RANGED_INTEGERP (0, XCDR (value), INT_MAX))
789 break;
790 return 0;
792 case IMAGE_ASCENT_VALUE:
793 if (SYMBOLP (value) && EQ (value, Qcenter))
794 break;
795 else if (RANGED_INTEGERP (0, value, 100))
796 break;
797 return 0;
799 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
800 /* Unlike the other integer-related cases, this one does not
801 verify that VALUE fits in 'int'. This is because callers
802 want EMACS_INT. */
803 if (!INTEGERP (value) || XINT (value) < 0)
804 return 0;
805 break;
807 case IMAGE_DONT_CHECK_VALUE_TYPE:
808 break;
810 case IMAGE_FUNCTION_VALUE:
811 value = indirect_function (value);
812 if (!NILP (Ffunctionp (value)))
813 break;
814 return 0;
816 case IMAGE_NUMBER_VALUE:
817 if (!INTEGERP (value) && !FLOATP (value))
818 return 0;
819 break;
821 case IMAGE_INTEGER_VALUE:
822 if (! TYPE_RANGED_INTEGERP (int, value))
823 return 0;
824 break;
826 case IMAGE_BOOL_VALUE:
827 if (!NILP (value) && !EQ (value, Qt))
828 return 0;
829 break;
831 default:
832 emacs_abort ();
833 break;
836 if (EQ (key, QCtype) && !EQ (type, value))
837 return 0;
840 /* Check that all mandatory fields are present. */
841 for (i = 0; i < nkeywords; ++i)
842 if (keywords[i].mandatory_p && keywords[i].count == 0)
843 return 0;
845 return NILP (plist);
849 /* Return the value of KEY in image specification SPEC. Value is nil
850 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
851 was found in SPEC. */
853 static Lisp_Object
854 image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found)
856 Lisp_Object tail;
858 eassert (valid_image_p (spec));
860 for (tail = XCDR (spec);
861 CONSP (tail) && CONSP (XCDR (tail));
862 tail = XCDR (XCDR (tail)))
864 if (EQ (XCAR (tail), key))
866 if (found)
867 *found = 1;
868 return XCAR (XCDR (tail));
872 if (found)
873 *found = 0;
874 return Qnil;
878 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
879 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
880 PIXELS non-nil means return the size in pixels, otherwise return the
881 size in canonical character units.
882 FRAME is the frame on which the image will be displayed. FRAME nil
883 or omitted means use the selected frame. */)
884 (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame)
886 Lisp_Object size;
888 size = Qnil;
889 if (valid_image_p (spec))
891 struct frame *f = decode_window_system_frame (frame);
892 ptrdiff_t id = lookup_image (f, spec);
893 struct image *img = IMAGE_FROM_ID (f, id);
894 int width = img->width + 2 * img->hmargin;
895 int height = img->height + 2 * img->vmargin;
897 if (NILP (pixels))
898 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
899 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
900 else
901 size = Fcons (make_number (width), make_number (height));
903 else
904 error ("Invalid image specification");
906 return size;
910 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
911 doc: /* Return t if image SPEC has a mask bitmap.
912 FRAME is the frame on which the image will be displayed. FRAME nil
913 or omitted means use the selected frame. */)
914 (Lisp_Object spec, Lisp_Object frame)
916 Lisp_Object mask;
918 mask = Qnil;
919 if (valid_image_p (spec))
921 struct frame *f = decode_window_system_frame (frame);
922 ptrdiff_t id = lookup_image (f, spec);
923 struct image *img = IMAGE_FROM_ID (f, id);
924 if (img->mask)
925 mask = Qt;
927 else
928 error ("Invalid image specification");
930 return mask;
933 DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0,
934 doc: /* Return metadata for image SPEC.
935 FRAME is the frame on which the image will be displayed. FRAME nil
936 or omitted means use the selected frame. */)
937 (Lisp_Object spec, Lisp_Object frame)
939 Lisp_Object ext;
941 ext = Qnil;
942 if (valid_image_p (spec))
944 struct frame *f = decode_window_system_frame (frame);
945 ptrdiff_t id = lookup_image (f, spec);
946 struct image *img = IMAGE_FROM_ID (f, id);
947 ext = img->lisp_data;
950 return ext;
954 /***********************************************************************
955 Image type independent image structures
956 ***********************************************************************/
958 #define MAX_IMAGE_SIZE 10.0
959 /* Allocate and return a new image structure for image specification
960 SPEC. SPEC has a hash value of HASH. */
962 static struct image *
963 make_image (Lisp_Object spec, EMACS_UINT hash)
965 struct image *img = xzalloc (sizeof *img);
966 Lisp_Object file = image_spec_value (spec, QCfile, NULL);
968 eassert (valid_image_p (spec));
969 img->dependencies = NILP (file) ? Qnil : list1 (file);
970 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
971 eassert (img->type != NULL);
972 img->spec = spec;
973 img->lisp_data = Qnil;
974 img->ascent = DEFAULT_IMAGE_ASCENT;
975 img->hash = hash;
976 img->corners[BOT_CORNER] = -1; /* Full image */
977 return img;
981 /* Free image IMG which was used on frame F, including its resources. */
983 static void
984 free_image (struct frame *f, struct image *img)
986 if (img)
988 struct image_cache *c = FRAME_IMAGE_CACHE (f);
990 /* Remove IMG from the hash table of its cache. */
991 if (img->prev)
992 img->prev->next = img->next;
993 else
994 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
996 if (img->next)
997 img->next->prev = img->prev;
999 c->images[img->id] = NULL;
1001 /* Free resources, then free IMG. */
1002 img->type->free (f, img);
1003 xfree (img);
1007 /* Return true if the given widths and heights are valid for display. */
1009 static bool
1010 check_image_size (struct frame *f, int width, int height)
1012 int w, h;
1014 if (width <= 0 || height <= 0)
1015 return 0;
1017 if (INTEGERP (Vmax_image_size))
1018 return (width <= XINT (Vmax_image_size)
1019 && height <= XINT (Vmax_image_size));
1020 else if (FLOATP (Vmax_image_size))
1022 if (f != NULL)
1024 w = FRAME_PIXEL_WIDTH (f);
1025 h = FRAME_PIXEL_HEIGHT (f);
1027 else
1028 w = h = 1024; /* Arbitrary size for unknown frame. */
1029 return (width <= XFLOAT_DATA (Vmax_image_size) * w
1030 && height <= XFLOAT_DATA (Vmax_image_size) * h);
1032 else
1033 return 1;
1036 /* Prepare image IMG for display on frame F. Must be called before
1037 drawing an image. */
1039 void
1040 prepare_image_for_display (struct frame *f, struct image *img)
1042 /* We're about to display IMG, so set its timestamp to `now'. */
1043 img->timestamp = current_timespec ();
1045 /* If IMG doesn't have a pixmap yet, load it now, using the image
1046 type dependent loader function. */
1047 if (img->pixmap == NO_PIXMAP && !img->load_failed_p)
1048 img->load_failed_p = ! img->type->load (f, img);
1050 #ifdef HAVE_X_WINDOWS
1051 if (!img->load_failed_p)
1053 block_input ();
1054 image_sync_to_pixmaps (f, img);
1055 unblock_input ();
1057 #endif
1061 /* Value is the number of pixels for the ascent of image IMG when
1062 drawn in face FACE. */
1065 image_ascent (struct image *img, struct face *face, struct glyph_slice *slice)
1067 int height;
1068 int ascent;
1070 if (slice->height == img->height)
1071 height = img->height + img->vmargin;
1072 else if (slice->y == 0)
1073 height = slice->height + img->vmargin;
1074 else
1075 height = slice->height;
1077 if (img->ascent == CENTERED_IMAGE_ASCENT)
1079 if (face->font)
1081 #ifdef HAVE_NTGUI
1082 /* W32 specific version. Why?. ++kfs */
1083 ascent = height / 2 - (FONT_DESCENT (face->font)
1084 - FONT_BASE (face->font)) / 2;
1085 #else
1086 /* This expression is arranged so that if the image can't be
1087 exactly centered, it will be moved slightly up. This is
1088 because a typical font is `top-heavy' (due to the presence
1089 uppercase letters), so the image placement should err towards
1090 being top-heavy too. It also just generally looks better. */
1091 ascent = (height + FONT_BASE (face->font)
1092 - FONT_DESCENT (face->font) + 1) / 2;
1093 #endif /* HAVE_NTGUI */
1095 else
1096 ascent = height / 2;
1098 else
1099 ascent = height * (img->ascent / 100.0);
1101 return ascent;
1105 /* Image background colors. */
1107 /* Find the "best" corner color of a bitmap.
1108 On W32, XIMG is assumed to a device context with the bitmap selected. */
1110 static RGB_PIXEL_COLOR
1111 four_corners_best (XImagePtr_or_DC ximg, int *corners,
1112 unsigned long width, unsigned long height)
1114 RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0);
1115 int i, best_count;
1117 if (corners && corners[BOT_CORNER] >= 0)
1119 /* Get the colors at the corner_pixels of ximg. */
1120 corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]);
1121 corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]);
1122 corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1);
1123 corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1);
1125 else
1127 /* Get the colors at the corner_pixels of ximg. */
1128 corner_pixels[0] = GET_PIXEL (ximg, 0, 0);
1129 corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0);
1130 corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1);
1131 corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1);
1133 /* Choose the most frequently found color as background. */
1134 for (i = best_count = 0; i < 4; ++i)
1136 int j, n;
1138 for (j = n = 0; j < 4; ++j)
1139 if (corner_pixels[i] == corner_pixels[j])
1140 ++n;
1142 if (n > best_count)
1143 best = corner_pixels[i], best_count = n;
1146 return best;
1149 /* Portability macros */
1151 #ifdef HAVE_NTGUI
1153 #define Free_Pixmap(display, pixmap) \
1154 DeleteObject (pixmap)
1156 #elif defined (HAVE_NS)
1158 #define Free_Pixmap(display, pixmap) \
1159 ns_release_object (pixmap)
1161 #else
1163 #define Free_Pixmap(display, pixmap) \
1164 XFreePixmap (display, pixmap)
1166 #endif /* !HAVE_NTGUI && !HAVE_NS */
1169 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1170 it is guessed heuristically. If non-zero, XIMG is an existing
1171 XImage object (or device context with the image selected on W32) to
1172 use for the heuristic. */
1174 RGB_PIXEL_COLOR
1175 image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg)
1177 if (! img->background_valid)
1178 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1180 bool free_ximg = !ximg;
1181 #ifdef HAVE_NTGUI
1182 HGDIOBJ prev;
1183 #endif /* HAVE_NTGUI */
1185 if (free_ximg)
1186 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
1188 img->background = four_corners_best (ximg, img->corners, img->width, img->height);
1190 if (free_ximg)
1191 image_unget_x_image_or_dc (img, 0, ximg, prev);
1193 img->background_valid = 1;
1196 return img->background;
1199 /* Return the `background_transparent' field of IMG. If IMG doesn't
1200 have one yet, it is guessed heuristically. If non-zero, MASK is an
1201 existing XImage object to use for the heuristic. */
1204 image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask)
1206 if (! img->background_transparent_valid)
1207 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1209 if (img->mask)
1211 bool free_mask = !mask;
1212 #ifdef HAVE_NTGUI
1213 HGDIOBJ prev;
1214 #endif /* HAVE_NTGUI */
1216 if (free_mask)
1217 mask = image_get_x_image_or_dc (f, img, 1, &prev);
1219 img->background_transparent
1220 = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN);
1222 if (free_mask)
1223 image_unget_x_image_or_dc (img, 1, mask, prev);
1225 else
1226 img->background_transparent = 0;
1228 img->background_transparent_valid = 1;
1231 return img->background_transparent;
1235 /***********************************************************************
1236 Helper functions for X image types
1237 ***********************************************************************/
1239 /* Clear X resources of image IMG on frame F according to FLAGS.
1240 FLAGS is bitwise-or of the following masks:
1241 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1242 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1243 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1244 any. */
1246 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1247 #define CLEAR_IMAGE_MASK (1 << 1)
1248 #define CLEAR_IMAGE_COLORS (1 << 2)
1250 static void
1251 x_clear_image_1 (struct frame *f, struct image *img, int flags)
1253 if (flags & CLEAR_IMAGE_PIXMAP)
1255 if (img->pixmap)
1257 Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
1258 img->pixmap = NO_PIXMAP;
1259 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1260 img->background_valid = 0;
1262 #ifdef HAVE_X_WINDOWS
1263 if (img->ximg)
1265 x_destroy_x_image (img->ximg);
1266 img->ximg = NULL;
1267 img->background_valid = 0;
1269 #endif
1272 if (flags & CLEAR_IMAGE_MASK)
1274 if (img->mask)
1276 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
1277 img->mask = NO_PIXMAP;
1278 img->background_transparent_valid = 0;
1280 #ifdef HAVE_X_WINDOWS
1281 if (img->mask_img)
1283 x_destroy_x_image (img->mask_img);
1284 img->mask_img = NULL;
1285 img->background_transparent_valid = 0;
1287 #endif
1290 if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors)
1292 /* W32_TODO: color table support. */
1293 #ifdef HAVE_X_WINDOWS
1294 x_free_colors (f, img->colors, img->ncolors);
1295 #endif /* HAVE_X_WINDOWS */
1296 xfree (img->colors);
1297 img->colors = NULL;
1298 img->ncolors = 0;
1303 /* Free X resources of image IMG which is used on frame F. */
1305 static void
1306 x_clear_image (struct frame *f, struct image *img)
1308 block_input ();
1309 x_clear_image_1 (f, img,
1310 CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS);
1311 unblock_input ();
1315 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1316 cannot be allocated, use DFLT. Add a newly allocated color to
1317 IMG->colors, so that it can be freed again. Value is the pixel
1318 color. */
1320 static unsigned long
1321 x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name,
1322 unsigned long dflt)
1324 XColor color;
1325 unsigned long result;
1327 eassert (STRINGP (color_name));
1329 if (x_defined_color (f, SSDATA (color_name), &color, 1)
1330 && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors,
1331 INT_MAX))
1333 /* This isn't called frequently so we get away with simply
1334 reallocating the color vector to the needed size, here. */
1335 ptrdiff_t ncolors = img->ncolors + 1;
1336 img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors);
1337 img->colors[ncolors - 1] = color.pixel;
1338 img->ncolors = ncolors;
1339 result = color.pixel;
1341 else
1342 result = dflt;
1344 return result;
1349 /***********************************************************************
1350 Image Cache
1351 ***********************************************************************/
1353 static void cache_image (struct frame *f, struct image *img);
1355 /* Return a new, initialized image cache that is allocated from the
1356 heap. Call free_image_cache to free an image cache. */
1358 struct image_cache *
1359 make_image_cache (void)
1361 struct image_cache *c = xmalloc (sizeof *c);
1363 c->size = 50;
1364 c->used = c->refcount = 0;
1365 c->images = xmalloc (c->size * sizeof *c->images);
1366 c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
1367 return c;
1371 /* Find an image matching SPEC in the cache, and return it. If no
1372 image is found, return NULL. */
1373 static struct image *
1374 search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash)
1376 struct image *img;
1377 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1378 int i = hash % IMAGE_CACHE_BUCKETS_SIZE;
1380 if (!c) return NULL;
1382 /* If the image spec does not specify a background color, the cached
1383 image must have the same background color as the current frame.
1384 The foreground color must also match, for the sake of monochrome
1385 images.
1387 In fact, we could ignore the foreground color matching condition
1388 for color images, or if the image spec specifies :foreground;
1389 similarly we could ignore the background color matching condition
1390 for formats that don't use transparency (such as jpeg), or if the
1391 image spec specifies :background. However, the extra memory
1392 usage is probably negligible in practice, so we don't bother. */
1394 for (img = c->buckets[i]; img; img = img->next)
1395 if (img->hash == hash
1396 && !NILP (Fequal (img->spec, spec))
1397 && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f)
1398 && img->frame_background == FRAME_BACKGROUND_PIXEL (f))
1399 break;
1400 return img;
1404 /* Search frame F for an image with spec SPEC, and free it. */
1406 static void
1407 uncache_image (struct frame *f, Lisp_Object spec)
1409 struct image *img = search_image_cache (f, spec, sxhash (spec, 0));
1410 if (img)
1412 free_image (f, img);
1413 /* As display glyphs may still be referring to the image ID, we
1414 must garbage the frame (Bug#6426). */
1415 SET_FRAME_GARBAGED (f);
1420 /* Free image cache of frame F. Be aware that X frames share images
1421 caches. */
1423 void
1424 free_image_cache (struct frame *f)
1426 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1427 if (c)
1429 ptrdiff_t i;
1431 /* Cache should not be referenced by any frame when freed. */
1432 eassert (c->refcount == 0);
1434 for (i = 0; i < c->used; ++i)
1435 free_image (f, c->images[i]);
1436 xfree (c->images);
1437 xfree (c->buckets);
1438 xfree (c);
1439 FRAME_IMAGE_CACHE (f) = NULL;
1444 /* Clear image cache of frame F. FILTER=t means free all images.
1445 FILTER=nil means clear only images that haven't been
1446 displayed for some time.
1447 Else, only free the images which have FILTER in their `dependencies'.
1448 Should be called from time to time to reduce the number of loaded images.
1449 If image-cache-eviction-delay is non-nil, this frees images in the cache
1450 which weren't displayed for at least that many seconds. */
1452 static void
1453 clear_image_cache (struct frame *f, Lisp_Object filter)
1455 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1457 if (c)
1459 ptrdiff_t i, nfreed = 0;
1461 /* Block input so that we won't be interrupted by a SIGIO
1462 while being in an inconsistent state. */
1463 block_input ();
1465 if (!NILP (filter))
1467 /* Filter image cache. */
1468 for (i = 0; i < c->used; ++i)
1470 struct image *img = c->images[i];
1471 if (img && (EQ (Qt, filter)
1472 || !NILP (Fmember (filter, img->dependencies))))
1474 free_image (f, img);
1475 ++nfreed;
1479 else if (INTEGERP (Vimage_cache_eviction_delay))
1481 /* Free cache based on timestamp. */
1482 struct timespec old, t;
1483 double delay;
1484 ptrdiff_t nimages = 0;
1486 for (i = 0; i < c->used; ++i)
1487 if (c->images[i])
1488 nimages++;
1490 /* If the number of cached images has grown unusually large,
1491 decrease the cache eviction delay (Bug#6230). */
1492 delay = XINT (Vimage_cache_eviction_delay);
1493 if (nimages > 40)
1494 delay = 1600 * delay / nimages / nimages;
1495 delay = max (delay, 1);
1497 t = current_timespec ();
1498 old = timespec_sub (t, dtotimespec (delay));
1500 for (i = 0; i < c->used; ++i)
1502 struct image *img = c->images[i];
1503 if (img && timespec_cmp (img->timestamp, old) < 0)
1505 free_image (f, img);
1506 ++nfreed;
1511 /* We may be clearing the image cache because, for example,
1512 Emacs was iconified for a longer period of time. In that
1513 case, current matrices may still contain references to
1514 images freed above. So, clear these matrices. */
1515 if (nfreed)
1517 Lisp_Object tail, frame;
1519 FOR_EACH_FRAME (tail, frame)
1521 struct frame *fr = XFRAME (frame);
1522 if (FRAME_IMAGE_CACHE (fr) == c)
1523 clear_current_matrices (fr);
1526 windows_or_buffers_changed = 19;
1529 unblock_input ();
1533 void
1534 clear_image_caches (Lisp_Object filter)
1536 /* FIXME: We want to do
1537 * struct terminal *t;
1538 * for (t = terminal_list; t; t = t->next_terminal)
1539 * clear_image_cache (t, filter); */
1540 Lisp_Object tail, frame;
1541 FOR_EACH_FRAME (tail, frame)
1542 if (FRAME_WINDOW_P (XFRAME (frame)))
1543 clear_image_cache (XFRAME (frame), filter);
1546 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
1547 0, 1, 0,
1548 doc: /* Clear the image cache.
1549 FILTER nil or a frame means clear all images in the selected frame.
1550 FILTER t means clear the image caches of all frames.
1551 Anything else, means only clear those images which refer to FILTER,
1552 which is then usually a filename. */)
1553 (Lisp_Object filter)
1555 if (!(EQ (filter, Qnil) || FRAMEP (filter)))
1556 clear_image_caches (filter);
1557 else
1558 clear_image_cache (decode_window_system_frame (filter), Qt);
1560 return Qnil;
1564 DEFUN ("image-flush", Fimage_flush, Simage_flush,
1565 1, 2, 0,
1566 doc: /* Flush the image with specification SPEC on frame FRAME.
1567 This removes the image from the Emacs image cache. If SPEC specifies
1568 an image file, the next redisplay of this image will read from the
1569 current contents of that file.
1571 FRAME nil or omitted means use the selected frame.
1572 FRAME t means refresh the image on all frames. */)
1573 (Lisp_Object spec, Lisp_Object frame)
1575 if (!valid_image_p (spec))
1576 error ("Invalid image specification");
1578 if (EQ (frame, Qt))
1580 Lisp_Object tail;
1581 FOR_EACH_FRAME (tail, frame)
1583 struct frame *f = XFRAME (frame);
1584 if (FRAME_WINDOW_P (f))
1585 uncache_image (f, spec);
1588 else
1589 uncache_image (decode_window_system_frame (frame), spec);
1591 return Qnil;
1595 /* Compute masks and transform image IMG on frame F, as specified
1596 by the image's specification, */
1598 static void
1599 postprocess_image (struct frame *f, struct image *img)
1601 /* Manipulation of the image's mask. */
1602 if (img->pixmap)
1604 Lisp_Object conversion, spec;
1605 Lisp_Object mask;
1607 spec = img->spec;
1609 /* `:heuristic-mask t'
1610 `:mask heuristic'
1611 means build a mask heuristically.
1612 `:heuristic-mask (R G B)'
1613 `:mask (heuristic (R G B))'
1614 means build a mask from color (R G B) in the
1615 image.
1616 `:mask nil'
1617 means remove a mask, if any. */
1619 mask = image_spec_value (spec, QCheuristic_mask, NULL);
1620 if (!NILP (mask))
1621 x_build_heuristic_mask (f, img, mask);
1622 else
1624 bool found_p;
1626 mask = image_spec_value (spec, QCmask, &found_p);
1628 if (EQ (mask, Qheuristic))
1629 x_build_heuristic_mask (f, img, Qt);
1630 else if (CONSP (mask)
1631 && EQ (XCAR (mask), Qheuristic))
1633 if (CONSP (XCDR (mask)))
1634 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
1635 else
1636 x_build_heuristic_mask (f, img, XCDR (mask));
1638 else if (NILP (mask) && found_p && img->mask)
1639 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
1643 /* Should we apply an image transformation algorithm? */
1644 conversion = image_spec_value (spec, QCconversion, NULL);
1645 if (EQ (conversion, Qdisabled))
1646 x_disable_image (f, img);
1647 else if (EQ (conversion, Qlaplace))
1648 x_laplace (f, img);
1649 else if (EQ (conversion, Qemboss))
1650 x_emboss (f, img);
1651 else if (CONSP (conversion)
1652 && EQ (XCAR (conversion), Qedge_detection))
1654 Lisp_Object tem;
1655 tem = XCDR (conversion);
1656 if (CONSP (tem))
1657 x_edge_detection (f, img,
1658 Fplist_get (tem, QCmatrix),
1659 Fplist_get (tem, QCcolor_adjustment));
1665 /* Return the id of image with Lisp specification SPEC on frame F.
1666 SPEC must be a valid Lisp image specification (see valid_image_p). */
1668 ptrdiff_t
1669 lookup_image (struct frame *f, Lisp_Object spec)
1671 struct image *img;
1672 EMACS_UINT hash;
1674 /* F must be a window-system frame, and SPEC must be a valid image
1675 specification. */
1676 eassert (FRAME_WINDOW_P (f));
1677 eassert (valid_image_p (spec));
1679 /* Look up SPEC in the hash table of the image cache. */
1680 hash = sxhash (spec, 0);
1681 img = search_image_cache (f, spec, hash);
1682 if (img && img->load_failed_p)
1684 free_image (f, img);
1685 img = NULL;
1688 /* If not found, create a new image and cache it. */
1689 if (img == NULL)
1691 block_input ();
1692 img = make_image (spec, hash);
1693 cache_image (f, img);
1694 img->load_failed_p = ! img->type->load (f, img);
1695 img->frame_foreground = FRAME_FOREGROUND_PIXEL (f);
1696 img->frame_background = FRAME_BACKGROUND_PIXEL (f);
1698 /* If we can't load the image, and we don't have a width and
1699 height, use some arbitrary width and height so that we can
1700 draw a rectangle for it. */
1701 if (img->load_failed_p)
1703 Lisp_Object value;
1705 value = image_spec_value (spec, QCwidth, NULL);
1706 img->width = (INTEGERP (value)
1707 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
1708 value = image_spec_value (spec, QCheight, NULL);
1709 img->height = (INTEGERP (value)
1710 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
1712 else
1714 /* Handle image type independent image attributes
1715 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1716 `:background COLOR'. */
1717 Lisp_Object ascent, margin, relief, bg;
1718 int relief_bound;
1720 ascent = image_spec_value (spec, QCascent, NULL);
1721 if (INTEGERP (ascent))
1722 img->ascent = XFASTINT (ascent);
1723 else if (EQ (ascent, Qcenter))
1724 img->ascent = CENTERED_IMAGE_ASCENT;
1726 margin = image_spec_value (spec, QCmargin, NULL);
1727 if (INTEGERP (margin))
1728 img->vmargin = img->hmargin = XFASTINT (margin);
1729 else if (CONSP (margin))
1731 img->hmargin = XFASTINT (XCAR (margin));
1732 img->vmargin = XFASTINT (XCDR (margin));
1735 relief = image_spec_value (spec, QCrelief, NULL);
1736 relief_bound = INT_MAX - max (img->hmargin, img->vmargin);
1737 if (RANGED_INTEGERP (- relief_bound, relief, relief_bound))
1739 img->relief = XINT (relief);
1740 img->hmargin += eabs (img->relief);
1741 img->vmargin += eabs (img->relief);
1744 if (! img->background_valid)
1746 bg = image_spec_value (img->spec, QCbackground, NULL);
1747 if (!NILP (bg))
1749 img->background
1750 = x_alloc_image_color (f, img, bg,
1751 FRAME_BACKGROUND_PIXEL (f));
1752 img->background_valid = 1;
1756 /* Do image transformations and compute masks, unless we
1757 don't have the image yet. */
1758 if (!EQ (*img->type->type, Qpostscript))
1759 postprocess_image (f, img);
1762 unblock_input ();
1765 /* We're using IMG, so set its timestamp to `now'. */
1766 img->timestamp = current_timespec ();
1768 /* Value is the image id. */
1769 return img->id;
1773 /* Cache image IMG in the image cache of frame F. */
1775 static void
1776 cache_image (struct frame *f, struct image *img)
1778 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1779 ptrdiff_t i;
1781 /* Find a free slot in c->images. */
1782 for (i = 0; i < c->used; ++i)
1783 if (c->images[i] == NULL)
1784 break;
1786 /* If no free slot found, maybe enlarge c->images. */
1787 if (i == c->used && c->used == c->size)
1788 c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images);
1790 /* Add IMG to c->images, and assign IMG an id. */
1791 c->images[i] = img;
1792 img->id = i;
1793 if (i == c->used)
1794 ++c->used;
1796 /* Add IMG to the cache's hash table. */
1797 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
1798 img->next = c->buckets[i];
1799 if (img->next)
1800 img->next->prev = img;
1801 img->prev = NULL;
1802 c->buckets[i] = img;
1806 /* Call FN on every image in the image cache of frame F. Used to mark
1807 Lisp Objects in the image cache. */
1809 /* Mark Lisp objects in image IMG. */
1811 static void
1812 mark_image (struct image *img)
1814 mark_object (img->spec);
1815 mark_object (img->dependencies);
1817 if (!NILP (img->lisp_data))
1818 mark_object (img->lisp_data);
1822 void
1823 mark_image_cache (struct image_cache *c)
1825 if (c)
1827 ptrdiff_t i;
1828 for (i = 0; i < c->used; ++i)
1829 if (c->images[i])
1830 mark_image (c->images[i]);
1836 /***********************************************************************
1837 X / NS / W32 support code
1838 ***********************************************************************/
1840 #ifdef WINDOWSNT
1842 /* Macro for defining functions that will be loaded from image DLLs. */
1843 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1845 /* Macro for loading those image functions from the library. */
1846 #define LOAD_IMGLIB_FN(lib,func) { \
1847 fn_##func = (void *) GetProcAddress (lib, #func); \
1848 if (!fn_##func) return 0; \
1851 #endif /* WINDOWSNT */
1853 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1854 windowing system.
1855 WIDTH and HEIGHT must both be positive.
1856 If XIMG is null, assume it is a bitmap. */
1857 static bool
1858 x_check_image_size (XImagePtr ximg, int width, int height)
1860 #ifdef HAVE_X_WINDOWS
1861 /* Respect Xlib's limits: it cannot deal with images that have more
1862 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1863 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1864 enum
1866 XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX),
1867 X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX))
1870 int bitmap_pad, depth, bytes_per_line;
1871 if (ximg)
1873 bitmap_pad = ximg->bitmap_pad;
1874 depth = ximg->depth;
1875 bytes_per_line = ximg->bytes_per_line;
1877 else
1879 bitmap_pad = 8;
1880 depth = 1;
1881 bytes_per_line = (width >> 3) + ((width & 7) != 0);
1883 return (width <= (INT_MAX - (bitmap_pad - 1)) / depth
1884 && height <= X_IMAGE_BYTES_MAX / bytes_per_line);
1885 #else
1886 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1887 For now, assume that every image size is allowed on these systems. */
1888 return 1;
1889 #endif
1892 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1893 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1894 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1895 via xmalloc. Print error messages via image_error if an error
1896 occurs. Value is true if successful.
1898 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1899 should indicate the bit depth of the image. */
1901 static bool
1902 x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth,
1903 XImagePtr *ximg, Pixmap *pixmap)
1905 #ifdef HAVE_X_WINDOWS
1906 Display *display = FRAME_X_DISPLAY (f);
1907 Window window = FRAME_X_WINDOW (f);
1908 Screen *screen = FRAME_X_SCREEN (f);
1910 eassert (input_blocked_p ());
1912 if (depth <= 0)
1913 depth = DefaultDepthOfScreen (screen);
1914 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
1915 depth, ZPixmap, 0, NULL, width, height,
1916 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
1917 if (*ximg == NULL)
1919 image_error ("Unable to allocate X image", Qnil, Qnil);
1920 return 0;
1923 if (! x_check_image_size (*ximg, width, height))
1925 x_destroy_x_image (*ximg);
1926 *ximg = NULL;
1927 image_error ("Image too large (%dx%d)",
1928 make_number (width), make_number (height));
1929 return 0;
1932 /* Allocate image raster. */
1933 (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height);
1935 /* Allocate a pixmap of the same size. */
1936 *pixmap = XCreatePixmap (display, window, width, height, depth);
1937 if (*pixmap == NO_PIXMAP)
1939 x_destroy_x_image (*ximg);
1940 *ximg = NULL;
1941 image_error ("Unable to create X pixmap", Qnil, Qnil);
1942 return 0;
1945 return 1;
1946 #endif /* HAVE_X_WINDOWS */
1948 #ifdef HAVE_NTGUI
1950 BITMAPINFOHEADER *header;
1951 HDC hdc;
1952 int scanline_width_bits;
1953 int remainder;
1954 int palette_colors = 0;
1956 if (depth == 0)
1957 depth = 24;
1959 if (depth != 1 && depth != 4 && depth != 8
1960 && depth != 16 && depth != 24 && depth != 32)
1962 image_error ("Invalid image bit depth specified", Qnil, Qnil);
1963 return 0;
1966 scanline_width_bits = width * depth;
1967 remainder = scanline_width_bits % 32;
1969 if (remainder)
1970 scanline_width_bits += 32 - remainder;
1972 /* Bitmaps with a depth less than 16 need a palette. */
1973 /* BITMAPINFO structure already contains the first RGBQUAD. */
1974 if (depth < 16)
1975 palette_colors = 1 << (depth - 1);
1977 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
1979 header = &(*ximg)->info.bmiHeader;
1980 memset (&(*ximg)->info, 0, sizeof (BITMAPINFO));
1981 header->biSize = sizeof (*header);
1982 header->biWidth = width;
1983 header->biHeight = -height; /* negative indicates a top-down bitmap. */
1984 header->biPlanes = 1;
1985 header->biBitCount = depth;
1986 header->biCompression = BI_RGB;
1987 header->biClrUsed = palette_colors;
1989 /* TODO: fill in palette. */
1990 if (depth == 1)
1992 (*ximg)->info.bmiColors[0].rgbBlue = 0;
1993 (*ximg)->info.bmiColors[0].rgbGreen = 0;
1994 (*ximg)->info.bmiColors[0].rgbRed = 0;
1995 (*ximg)->info.bmiColors[0].rgbReserved = 0;
1996 (*ximg)->info.bmiColors[1].rgbBlue = 255;
1997 (*ximg)->info.bmiColors[1].rgbGreen = 255;
1998 (*ximg)->info.bmiColors[1].rgbRed = 255;
1999 (*ximg)->info.bmiColors[1].rgbReserved = 0;
2002 hdc = get_frame_dc (f);
2004 /* Create a DIBSection and raster array for the bitmap,
2005 and store its handle in *pixmap. */
2006 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
2007 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
2008 /* casting avoids a GCC warning */
2009 (void **)&((*ximg)->data), NULL, 0);
2011 /* Realize display palette and garbage all frames. */
2012 release_frame_dc (f, hdc);
2014 if (*pixmap == NULL)
2016 DWORD err = GetLastError ();
2017 Lisp_Object errcode;
2018 /* All system errors are < 10000, so the following is safe. */
2019 XSETINT (errcode, err);
2020 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
2021 x_destroy_x_image (*ximg);
2022 return 0;
2025 return 1;
2027 #endif /* HAVE_NTGUI */
2029 #ifdef HAVE_NS
2030 *pixmap = ns_image_for_XPM (width, height, depth);
2031 if (*pixmap == 0)
2033 *ximg = NULL;
2034 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil);
2035 return 0;
2037 *ximg = *pixmap;
2038 return 1;
2039 #endif
2043 /* Destroy XImage XIMG. Free XIMG->data. */
2045 static void
2046 x_destroy_x_image (XImagePtr ximg)
2048 eassert (input_blocked_p ());
2049 if (ximg)
2051 #ifdef HAVE_X_WINDOWS
2052 xfree (ximg->data);
2053 ximg->data = NULL;
2054 XDestroyImage (ximg);
2055 #endif /* HAVE_X_WINDOWS */
2056 #ifdef HAVE_NTGUI
2057 /* Data will be freed by DestroyObject. */
2058 ximg->data = NULL;
2059 xfree (ximg);
2060 #endif /* HAVE_NTGUI */
2061 #ifdef HAVE_NS
2062 ns_release_object (ximg);
2063 #endif /* HAVE_NS */
2068 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2069 are width and height of both the image and pixmap. */
2071 static void
2072 x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height)
2074 #ifdef HAVE_X_WINDOWS
2075 GC gc;
2077 eassert (input_blocked_p ());
2078 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
2079 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
2080 XFreeGC (FRAME_X_DISPLAY (f), gc);
2081 #endif /* HAVE_X_WINDOWS */
2083 #ifdef HAVE_NTGUI
2084 #if 0 /* I don't think this is necessary looking at where it is used. */
2085 HDC hdc = get_frame_dc (f);
2086 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
2087 release_frame_dc (f, hdc);
2088 #endif
2089 #endif /* HAVE_NTGUI */
2091 #ifdef HAVE_NS
2092 eassert (ximg == pixmap);
2093 ns_retain_object (ximg);
2094 #endif
2097 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2098 with image_put_x_image. */
2100 static bool
2101 image_create_x_image_and_pixmap (struct frame *f, struct image *img,
2102 int width, int height, int depth,
2103 XImagePtr *ximg, bool mask_p)
2105 eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP);
2107 return x_create_x_image_and_pixmap (f, width, height, depth, ximg,
2108 !mask_p ? &img->pixmap : &img->mask);
2111 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2112 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2113 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2114 On the other platforms, it puts XIMG into the pixmap, then frees
2115 the X image and its buffer. */
2117 static void
2118 image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg,
2119 bool mask_p)
2121 #ifdef HAVE_X_WINDOWS
2122 if (!mask_p)
2124 eassert (img->ximg == NULL);
2125 img->ximg = ximg;
2127 else
2129 eassert (img->mask_img == NULL);
2130 img->mask_img = ximg;
2132 #else
2133 x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask,
2134 img->width, img->height);
2135 x_destroy_x_image (ximg);
2136 #endif
2139 #ifdef HAVE_X_WINDOWS
2140 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2141 the X images and their buffers. */
2143 static void
2144 image_sync_to_pixmaps (struct frame *f, struct image *img)
2146 if (img->ximg)
2148 x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height);
2149 x_destroy_x_image (img->ximg);
2150 img->ximg = NULL;
2152 if (img->mask_img)
2154 x_put_x_image (f, img->mask_img, img->mask, img->width, img->height);
2155 x_destroy_x_image (img->mask_img);
2156 img->mask_img = NULL;
2159 #endif
2161 #ifdef HAVE_NTGUI
2162 /* Create a memory device context for IMG on frame F. It stores the
2163 currently selected GDI object into *PREV for future restoration by
2164 image_unget_x_image_or_dc. */
2166 static XImagePtr_or_DC
2167 image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p,
2168 HGDIOBJ *prev)
2170 HDC frame_dc = get_frame_dc (f);
2171 XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc);
2173 release_frame_dc (f, frame_dc);
2174 *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask);
2176 return ximg;
2179 static void
2180 image_unget_x_image_or_dc (struct image *img, bool mask_p,
2181 XImagePtr_or_DC ximg, HGDIOBJ prev)
2183 SelectObject (ximg, prev);
2184 DeleteDC (ximg);
2186 #else /* !HAVE_NTGUI */
2187 /* Get the X image for IMG on frame F. The resulting X image data
2188 should be treated as read-only at least on X. */
2190 static XImagePtr
2191 image_get_x_image (struct frame *f, struct image *img, bool mask_p)
2193 #ifdef HAVE_X_WINDOWS
2194 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2196 if (ximg_in_img)
2197 return ximg_in_img;
2198 else
2199 return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask,
2200 0, 0, img->width, img->height, ~0, ZPixmap);
2201 #elif defined (HAVE_NS)
2202 XImagePtr pixmap = !mask_p ? img->pixmap : img->mask;
2204 ns_retain_object (pixmap);
2205 return pixmap;
2206 #endif
2209 static void
2210 image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg)
2212 #ifdef HAVE_X_WINDOWS
2213 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2215 if (ximg_in_img)
2216 eassert (ximg == ximg_in_img);
2217 else
2218 XDestroyImage (ximg);
2219 #elif defined (HAVE_NS)
2220 ns_release_object (ximg);
2221 #endif
2223 #endif /* !HAVE_NTGUI */
2226 /***********************************************************************
2227 File Handling
2228 ***********************************************************************/
2230 /* Find image file FILE. Look in data-directory/images, then
2231 x-bitmap-file-path. Value is the encoded full name of the file
2232 found, or nil if not found. */
2234 Lisp_Object
2235 x_find_image_file (Lisp_Object file)
2237 Lisp_Object file_found, search_path;
2238 int fd;
2240 /* TODO I think this should use something like image-load-path
2241 instead. Unfortunately, that can contain non-string elements. */
2242 search_path = Fcons (Fexpand_file_name (build_string ("images"),
2243 Vdata_directory),
2244 Vx_bitmap_file_path);
2246 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2247 fd = openp (search_path, file, Qnil, &file_found, Qnil, false);
2249 if (fd == -1)
2250 file_found = Qnil;
2251 else
2253 file_found = ENCODE_FILE (file_found);
2254 if (fd != -2)
2255 emacs_close (fd);
2258 return file_found;
2262 /* Read FILE into memory. Value is a pointer to a buffer allocated
2263 with xmalloc holding FILE's contents. Value is null if an error
2264 occurred. *SIZE is set to the size of the file. */
2266 static unsigned char *
2267 slurp_file (char *file, ptrdiff_t *size)
2269 FILE *fp = emacs_fopen (file, "rb");
2270 unsigned char *buf = NULL;
2271 struct stat st;
2273 if (fp)
2275 ptrdiff_t count = SPECPDL_INDEX ();
2276 record_unwind_protect_ptr (fclose_unwind, fp);
2278 if (fstat (fileno (fp), &st) == 0
2279 && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX))
2281 /* Report an error if we read past the purported EOF.
2282 This can happen if the file grows as we read it. */
2283 ptrdiff_t buflen = st.st_size;
2284 buf = xmalloc (buflen + 1);
2285 if (fread (buf, 1, buflen + 1, fp) == buflen)
2286 *size = buflen;
2287 else
2289 xfree (buf);
2290 buf = NULL;
2294 unbind_to (count, Qnil);
2297 return buf;
2302 /***********************************************************************
2303 XBM images
2304 ***********************************************************************/
2306 static bool xbm_load (struct frame *f, struct image *img);
2307 static bool xbm_image_p (Lisp_Object object);
2308 static bool xbm_file_p (Lisp_Object);
2311 /* Indices of image specification fields in xbm_format, below. */
2313 enum xbm_keyword_index
2315 XBM_TYPE,
2316 XBM_FILE,
2317 XBM_WIDTH,
2318 XBM_HEIGHT,
2319 XBM_DATA,
2320 XBM_FOREGROUND,
2321 XBM_BACKGROUND,
2322 XBM_ASCENT,
2323 XBM_MARGIN,
2324 XBM_RELIEF,
2325 XBM_ALGORITHM,
2326 XBM_HEURISTIC_MASK,
2327 XBM_MASK,
2328 XBM_LAST
2331 /* Vector of image_keyword structures describing the format
2332 of valid XBM image specifications. */
2334 static const struct image_keyword xbm_format[XBM_LAST] =
2336 {":type", IMAGE_SYMBOL_VALUE, 1},
2337 {":file", IMAGE_STRING_VALUE, 0},
2338 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2339 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2340 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2341 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
2342 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
2343 {":ascent", IMAGE_ASCENT_VALUE, 0},
2344 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
2345 {":relief", IMAGE_INTEGER_VALUE, 0},
2346 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2347 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2348 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
2351 /* Structure describing the image type XBM. */
2353 static struct image_type xbm_type =
2355 &Qxbm,
2356 xbm_image_p,
2357 xbm_load,
2358 x_clear_image,
2359 NULL,
2360 NULL
2363 /* Tokens returned from xbm_scan. */
2365 enum xbm_token
2367 XBM_TK_IDENT = 256,
2368 XBM_TK_NUMBER
2372 /* Return true if OBJECT is a valid XBM-type image specification.
2373 A valid specification is a list starting with the symbol `image'
2374 The rest of the list is a property list which must contain an
2375 entry `:type xbm'.
2377 If the specification specifies a file to load, it must contain
2378 an entry `:file FILENAME' where FILENAME is a string.
2380 If the specification is for a bitmap loaded from memory it must
2381 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2382 WIDTH and HEIGHT are integers > 0. DATA may be:
2384 1. a string large enough to hold the bitmap data, i.e. it must
2385 have a size >= (WIDTH + 7) / 8 * HEIGHT
2387 2. a bool-vector of size >= WIDTH * HEIGHT
2389 3. a vector of strings or bool-vectors, one for each line of the
2390 bitmap.
2392 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2393 may not be specified in this case because they are defined in the
2394 XBM file.
2396 Both the file and data forms may contain the additional entries
2397 `:background COLOR' and `:foreground COLOR'. If not present,
2398 foreground and background of the frame on which the image is
2399 displayed is used. */
2401 static bool
2402 xbm_image_p (Lisp_Object object)
2404 struct image_keyword kw[XBM_LAST];
2406 memcpy (kw, xbm_format, sizeof kw);
2407 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
2408 return 0;
2410 eassert (EQ (kw[XBM_TYPE].value, Qxbm));
2412 if (kw[XBM_FILE].count)
2414 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
2415 return 0;
2417 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
2419 /* In-memory XBM file. */
2420 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
2421 return 0;
2423 else
2425 Lisp_Object data;
2426 int width, height;
2428 /* Entries for `:width', `:height' and `:data' must be present. */
2429 if (!kw[XBM_WIDTH].count
2430 || !kw[XBM_HEIGHT].count
2431 || !kw[XBM_DATA].count)
2432 return 0;
2434 data = kw[XBM_DATA].value;
2435 width = XFASTINT (kw[XBM_WIDTH].value);
2436 height = XFASTINT (kw[XBM_HEIGHT].value);
2438 /* Check type of data, and width and height against contents of
2439 data. */
2440 if (VECTORP (data))
2442 EMACS_INT i;
2444 /* Number of elements of the vector must be >= height. */
2445 if (ASIZE (data) < height)
2446 return 0;
2448 /* Each string or bool-vector in data must be large enough
2449 for one line of the image. */
2450 for (i = 0; i < height; ++i)
2452 Lisp_Object elt = AREF (data, i);
2454 if (STRINGP (elt))
2456 if (SCHARS (elt)
2457 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
2458 return 0;
2460 else if (BOOL_VECTOR_P (elt))
2462 if (bool_vector_size (elt) < width)
2463 return 0;
2465 else
2466 return 0;
2469 else if (STRINGP (data))
2471 if (SCHARS (data)
2472 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
2473 return 0;
2475 else if (BOOL_VECTOR_P (data))
2477 if (bool_vector_size (data) / height < width)
2478 return 0;
2480 else
2481 return 0;
2484 return 1;
2488 /* Scan a bitmap file. FP is the stream to read from. Value is
2489 either an enumerator from enum xbm_token, or a character for a
2490 single-character token, or 0 at end of file. If scanning an
2491 identifier, store the lexeme of the identifier in SVAL. If
2492 scanning a number, store its value in *IVAL. */
2494 static int
2495 xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival)
2497 unsigned int c;
2499 loop:
2501 /* Skip white space. */
2502 while (*s < end && (c = *(*s)++, c_isspace (c)))
2505 if (*s >= end)
2506 c = 0;
2507 else if (c_isdigit (c))
2509 int value = 0, digit;
2511 if (c == '0' && *s < end)
2513 c = *(*s)++;
2514 if (c == 'x' || c == 'X')
2516 while (*s < end)
2518 c = *(*s)++;
2519 if (c_isdigit (c))
2520 digit = c - '0';
2521 else if (c >= 'a' && c <= 'f')
2522 digit = c - 'a' + 10;
2523 else if (c >= 'A' && c <= 'F')
2524 digit = c - 'A' + 10;
2525 else
2526 break;
2527 value = 16 * value + digit;
2530 else if (c_isdigit (c))
2532 value = c - '0';
2533 while (*s < end
2534 && (c = *(*s)++, c_isdigit (c)))
2535 value = 8 * value + c - '0';
2538 else
2540 value = c - '0';
2541 while (*s < end
2542 && (c = *(*s)++, c_isdigit (c)))
2543 value = 10 * value + c - '0';
2546 if (*s < end)
2547 *s = *s - 1;
2548 *ival = value;
2549 c = XBM_TK_NUMBER;
2551 else if (c_isalpha (c) || c == '_')
2553 *sval++ = c;
2554 while (*s < end
2555 && (c = *(*s)++, (c_isalnum (c) || c == '_')))
2556 *sval++ = c;
2557 *sval = 0;
2558 if (*s < end)
2559 *s = *s - 1;
2560 c = XBM_TK_IDENT;
2562 else if (c == '/' && **s == '*')
2564 /* C-style comment. */
2565 ++*s;
2566 while (**s && (**s != '*' || *(*s + 1) != '/'))
2567 ++*s;
2568 if (**s)
2570 *s += 2;
2571 goto loop;
2575 return c;
2578 #ifdef HAVE_NTGUI
2580 /* Create a Windows bitmap from X bitmap data. */
2581 static HBITMAP
2582 w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
2584 static unsigned char swap_nibble[16]
2585 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2586 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2587 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2588 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2589 int i, j, w1, w2;
2590 unsigned char *bits, *p;
2591 HBITMAP bmp;
2593 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
2594 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2595 bits = alloca (height * w2);
2596 memset (bits, 0, height * w2);
2597 for (i = 0; i < height; i++)
2599 p = bits + i*w2;
2600 for (j = 0; j < w1; j++)
2602 /* Bitswap XBM bytes to match how Windows does things. */
2603 unsigned char c = *data++;
2604 *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
2605 | (swap_nibble[(c>>4) & 0xf]));
2608 bmp = CreateBitmap (width, height, 1, 1, (char *) bits);
2610 return bmp;
2613 static void
2614 convert_mono_to_color_image (struct frame *f, struct image *img,
2615 COLORREF foreground, COLORREF background)
2617 HDC hdc, old_img_dc, new_img_dc;
2618 HGDIOBJ old_prev, new_prev;
2619 HBITMAP new_pixmap;
2621 hdc = get_frame_dc (f);
2622 old_img_dc = CreateCompatibleDC (hdc);
2623 new_img_dc = CreateCompatibleDC (hdc);
2624 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
2625 release_frame_dc (f, hdc);
2626 old_prev = SelectObject (old_img_dc, img->pixmap);
2627 new_prev = SelectObject (new_img_dc, new_pixmap);
2628 /* Windows convention for mono bitmaps is black = background,
2629 white = foreground. */
2630 SetTextColor (new_img_dc, background);
2631 SetBkColor (new_img_dc, foreground);
2633 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
2634 0, 0, SRCCOPY);
2636 SelectObject (old_img_dc, old_prev);
2637 SelectObject (new_img_dc, new_prev);
2638 DeleteDC (old_img_dc);
2639 DeleteDC (new_img_dc);
2640 DeleteObject (img->pixmap);
2641 if (new_pixmap == 0)
2642 fprintf (stderr, "Failed to convert image to color.\n");
2643 else
2644 img->pixmap = new_pixmap;
2647 #define XBM_BIT_SHUFFLE(b) (~(b))
2649 #else
2651 #define XBM_BIT_SHUFFLE(b) (b)
2653 #endif /* HAVE_NTGUI */
2656 static void
2657 Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data,
2658 RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg,
2659 bool non_default_colors)
2661 #ifdef HAVE_NTGUI
2662 img->pixmap
2663 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
2665 /* If colors were specified, transfer the bitmap to a color one. */
2666 if (non_default_colors)
2667 convert_mono_to_color_image (f, img, fg, bg);
2669 #elif defined (HAVE_NS)
2670 img->pixmap = ns_image_from_XBM (data, img->width, img->height);
2672 #else
2673 img->pixmap =
2674 (x_check_image_size (0, img->width, img->height)
2675 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
2676 FRAME_X_WINDOW (f),
2677 data,
2678 img->width, img->height,
2679 fg, bg,
2680 DefaultDepthOfScreen (FRAME_X_SCREEN (f)))
2681 : NO_PIXMAP);
2682 #endif /* !HAVE_NTGUI && !HAVE_NS */
2687 /* Replacement for XReadBitmapFileData which isn't available under old
2688 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2689 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2690 the image. Return in *DATA the bitmap data allocated with xmalloc.
2691 Value is true if successful. DATA null means just test if
2692 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2693 inhibit the call to image_error when the image size is invalid (the
2694 bitmap remains unread). */
2696 static bool
2697 xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end,
2698 int *width, int *height, char **data,
2699 bool inhibit_image_error)
2701 unsigned char *s = contents;
2702 char buffer[BUFSIZ];
2703 bool padding_p = 0;
2704 bool v10 = 0;
2705 int bytes_per_line, i, nbytes;
2706 char *p;
2707 int value;
2708 int LA1;
2710 #define match() \
2711 LA1 = xbm_scan (&s, end, buffer, &value)
2713 #define expect(TOKEN) \
2714 do \
2716 if (LA1 != (TOKEN)) \
2717 goto failure; \
2718 match (); \
2720 while (0)
2722 #define expect_ident(IDENT) \
2723 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2724 match (); \
2725 else \
2726 goto failure
2728 *width = *height = -1;
2729 if (data)
2730 *data = NULL;
2731 LA1 = xbm_scan (&s, end, buffer, &value);
2733 /* Parse defines for width, height and hot-spots. */
2734 while (LA1 == '#')
2736 match ();
2737 expect_ident ("define");
2738 expect (XBM_TK_IDENT);
2740 if (LA1 == XBM_TK_NUMBER)
2742 char *q = strrchr (buffer, '_');
2743 q = q ? q + 1 : buffer;
2744 if (strcmp (q, "width") == 0)
2745 *width = value;
2746 else if (strcmp (q, "height") == 0)
2747 *height = value;
2749 expect (XBM_TK_NUMBER);
2752 if (!check_image_size (f, *width, *height))
2754 if (!inhibit_image_error)
2755 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
2756 goto failure;
2758 else if (data == NULL)
2759 goto success;
2761 /* Parse bits. Must start with `static'. */
2762 expect_ident ("static");
2763 if (LA1 == XBM_TK_IDENT)
2765 if (strcmp (buffer, "unsigned") == 0)
2767 match ();
2768 expect_ident ("char");
2770 else if (strcmp (buffer, "short") == 0)
2772 match ();
2773 v10 = 1;
2774 if (*width % 16 && *width % 16 < 9)
2775 padding_p = 1;
2777 else if (strcmp (buffer, "char") == 0)
2778 match ();
2779 else
2780 goto failure;
2782 else
2783 goto failure;
2785 expect (XBM_TK_IDENT);
2786 expect ('[');
2787 expect (']');
2788 expect ('=');
2789 expect ('{');
2791 if (! x_check_image_size (0, *width, *height))
2793 if (!inhibit_image_error)
2794 image_error ("Image too large (%dx%d)",
2795 make_number (*width), make_number (*height));
2796 goto failure;
2798 bytes_per_line = (*width + 7) / 8 + padding_p;
2799 nbytes = bytes_per_line * *height;
2800 p = *data = xmalloc (nbytes);
2802 if (v10)
2804 for (i = 0; i < nbytes; i += 2)
2806 int val = value;
2807 expect (XBM_TK_NUMBER);
2809 *p++ = XBM_BIT_SHUFFLE (val);
2810 if (!padding_p || ((i + 2) % bytes_per_line))
2811 *p++ = XBM_BIT_SHUFFLE (value >> 8);
2813 if (LA1 == ',' || LA1 == '}')
2814 match ();
2815 else
2816 goto failure;
2819 else
2821 for (i = 0; i < nbytes; ++i)
2823 int val = value;
2824 expect (XBM_TK_NUMBER);
2826 *p++ = XBM_BIT_SHUFFLE (val);
2828 if (LA1 == ',' || LA1 == '}')
2829 match ();
2830 else
2831 goto failure;
2835 success:
2836 return 1;
2838 failure:
2840 if (data && *data)
2842 xfree (*data);
2843 *data = NULL;
2845 return 0;
2847 #undef match
2848 #undef expect
2849 #undef expect_ident
2853 /* Load XBM image IMG which will be displayed on frame F from buffer
2854 CONTENTS. END is the end of the buffer. Value is true if
2855 successful. */
2857 static bool
2858 xbm_load_image (struct frame *f, struct image *img, unsigned char *contents,
2859 unsigned char *end)
2861 bool rc;
2862 char *data;
2863 bool success_p = 0;
2865 rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height,
2866 &data, 0);
2867 if (rc)
2869 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
2870 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
2871 bool non_default_colors = 0;
2872 Lisp_Object value;
2874 eassert (img->width > 0 && img->height > 0);
2876 /* Get foreground and background colors, maybe allocate colors. */
2877 value = image_spec_value (img->spec, QCforeground, NULL);
2878 if (!NILP (value))
2880 foreground = x_alloc_image_color (f, img, value, foreground);
2881 non_default_colors = 1;
2883 value = image_spec_value (img->spec, QCbackground, NULL);
2884 if (!NILP (value))
2886 background = x_alloc_image_color (f, img, value, background);
2887 img->background = background;
2888 img->background_valid = 1;
2889 non_default_colors = 1;
2892 Create_Pixmap_From_Bitmap_Data (f, img, data,
2893 foreground, background,
2894 non_default_colors);
2895 xfree (data);
2897 if (img->pixmap == NO_PIXMAP)
2899 x_clear_image (f, img);
2900 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
2902 else
2903 success_p = 1;
2905 else
2906 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
2908 return success_p;
2912 /* Value is true if DATA looks like an in-memory XBM file. */
2914 static bool
2915 xbm_file_p (Lisp_Object data)
2917 int w, h;
2918 return (STRINGP (data)
2919 && xbm_read_bitmap_data (NULL, SDATA (data),
2920 (SDATA (data) + SBYTES (data)),
2921 &w, &h, NULL, 1));
2925 /* Fill image IMG which is used on frame F with pixmap data. Value is
2926 true if successful. */
2928 static bool
2929 xbm_load (struct frame *f, struct image *img)
2931 bool success_p = 0;
2932 Lisp_Object file_name;
2934 eassert (xbm_image_p (img->spec));
2936 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2937 file_name = image_spec_value (img->spec, QCfile, NULL);
2938 if (STRINGP (file_name))
2940 Lisp_Object file;
2941 unsigned char *contents;
2942 ptrdiff_t size;
2944 file = x_find_image_file (file_name);
2945 if (!STRINGP (file))
2947 image_error ("Cannot find image file `%s'", file_name, Qnil);
2948 return 0;
2951 contents = slurp_file (SSDATA (file), &size);
2952 if (contents == NULL)
2954 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
2955 return 0;
2958 success_p = xbm_load_image (f, img, contents, contents + size);
2959 xfree (contents);
2961 else
2963 struct image_keyword fmt[XBM_LAST];
2964 Lisp_Object data;
2965 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
2966 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
2967 bool non_default_colors = 0;
2968 char *bits;
2969 bool parsed_p;
2970 bool in_memory_file_p = 0;
2972 /* See if data looks like an in-memory XBM file. */
2973 data = image_spec_value (img->spec, QCdata, NULL);
2974 in_memory_file_p = xbm_file_p (data);
2976 /* Parse the image specification. */
2977 memcpy (fmt, xbm_format, sizeof fmt);
2978 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
2979 eassert (parsed_p);
2981 /* Get specified width, and height. */
2982 if (!in_memory_file_p)
2984 img->width = XFASTINT (fmt[XBM_WIDTH].value);
2985 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
2986 eassert (img->width > 0 && img->height > 0);
2987 if (!check_image_size (f, img->width, img->height))
2989 image_error ("Invalid image size (see `max-image-size')",
2990 Qnil, Qnil);
2991 return 0;
2995 /* Get foreground and background colors, maybe allocate colors. */
2996 if (fmt[XBM_FOREGROUND].count
2997 && STRINGP (fmt[XBM_FOREGROUND].value))
2999 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
3000 foreground);
3001 non_default_colors = 1;
3004 if (fmt[XBM_BACKGROUND].count
3005 && STRINGP (fmt[XBM_BACKGROUND].value))
3007 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
3008 background);
3009 non_default_colors = 1;
3012 if (in_memory_file_p)
3013 success_p = xbm_load_image (f, img, SDATA (data),
3014 (SDATA (data)
3015 + SBYTES (data)));
3016 else
3018 if (VECTORP (data))
3020 int i;
3021 char *p;
3022 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3024 p = bits = alloca (nbytes * img->height);
3025 for (i = 0; i < img->height; ++i, p += nbytes)
3027 Lisp_Object line = AREF (data, i);
3028 if (STRINGP (line))
3029 memcpy (p, SDATA (line), nbytes);
3030 else
3031 memcpy (p, bool_vector_data (line), nbytes);
3034 else if (STRINGP (data))
3035 bits = SSDATA (data);
3036 else
3037 bits = (char *) bool_vector_data (data);
3039 #ifdef HAVE_NTGUI
3041 char *invertedBits;
3042 int nbytes, i;
3043 /* Windows mono bitmaps are reversed compared with X. */
3044 invertedBits = bits;
3045 nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR
3046 * img->height;
3047 bits = alloca (nbytes);
3048 for (i = 0; i < nbytes; i++)
3049 bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]);
3051 #endif
3052 /* Create the pixmap. */
3054 if (x_check_image_size (0, img->width, img->height))
3055 Create_Pixmap_From_Bitmap_Data (f, img, bits,
3056 foreground, background,
3057 non_default_colors);
3058 else
3059 img->pixmap = NO_PIXMAP;
3061 if (img->pixmap)
3062 success_p = 1;
3063 else
3065 image_error ("Unable to create pixmap for XBM image `%s'",
3066 img->spec, Qnil);
3067 x_clear_image (f, img);
3072 return success_p;
3077 /***********************************************************************
3078 XPM images
3079 ***********************************************************************/
3081 #if defined (HAVE_XPM) || defined (HAVE_NS)
3083 static bool xpm_image_p (Lisp_Object object);
3084 static bool xpm_load (struct frame *f, struct image *img);
3086 #endif /* HAVE_XPM || HAVE_NS */
3088 #ifdef HAVE_XPM
3089 #ifdef HAVE_NTGUI
3090 /* Indicate to xpm.h that we don't have Xlib. */
3091 #define FOR_MSW
3092 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3093 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3094 #define XColor xpm_XColor
3095 #define XImage xpm_XImage
3096 #define Display xpm_Display
3097 #define PIXEL_ALREADY_TYPEDEFED
3098 #include "X11/xpm.h"
3099 #undef FOR_MSW
3100 #undef XColor
3101 #undef XImage
3102 #undef Display
3103 #undef PIXEL_ALREADY_TYPEDEFED
3104 #else
3105 #include "X11/xpm.h"
3106 #endif /* HAVE_NTGUI */
3107 #endif /* HAVE_XPM */
3109 #if defined (HAVE_XPM) || defined (HAVE_NS)
3110 /* The symbol `xpm' identifying XPM-format images. */
3112 static Lisp_Object Qxpm;
3114 /* Indices of image specification fields in xpm_format, below. */
3116 enum xpm_keyword_index
3118 XPM_TYPE,
3119 XPM_FILE,
3120 XPM_DATA,
3121 XPM_ASCENT,
3122 XPM_MARGIN,
3123 XPM_RELIEF,
3124 XPM_ALGORITHM,
3125 XPM_HEURISTIC_MASK,
3126 XPM_MASK,
3127 XPM_COLOR_SYMBOLS,
3128 XPM_BACKGROUND,
3129 XPM_LAST
3132 /* Vector of image_keyword structures describing the format
3133 of valid XPM image specifications. */
3135 static const struct image_keyword xpm_format[XPM_LAST] =
3137 {":type", IMAGE_SYMBOL_VALUE, 1},
3138 {":file", IMAGE_STRING_VALUE, 0},
3139 {":data", IMAGE_STRING_VALUE, 0},
3140 {":ascent", IMAGE_ASCENT_VALUE, 0},
3141 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
3142 {":relief", IMAGE_INTEGER_VALUE, 0},
3143 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3144 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3145 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3146 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3147 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
3150 #if defined HAVE_NTGUI && defined WINDOWSNT
3151 static bool init_xpm_functions (void);
3152 #else
3153 #define init_xpm_functions NULL
3154 #endif
3156 /* Structure describing the image type XPM. */
3158 static struct image_type xpm_type =
3160 &Qxpm,
3161 xpm_image_p,
3162 xpm_load,
3163 x_clear_image,
3164 init_xpm_functions,
3165 NULL
3168 #ifdef HAVE_X_WINDOWS
3170 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3171 functions for allocating image colors. Our own functions handle
3172 color allocation failures more gracefully than the ones on the XPM
3173 lib. */
3175 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3176 #define ALLOC_XPM_COLORS
3177 #endif
3178 #endif /* HAVE_X_WINDOWS */
3180 #ifdef ALLOC_XPM_COLORS
3182 static struct xpm_cached_color *xpm_cache_color (struct frame *, char *,
3183 XColor *, int);
3185 /* An entry in a hash table used to cache color definitions of named
3186 colors. This cache is necessary to speed up XPM image loading in
3187 case we do color allocations ourselves. Without it, we would need
3188 a call to XParseColor per pixel in the image. */
3190 struct xpm_cached_color
3192 /* Next in collision chain. */
3193 struct xpm_cached_color *next;
3195 /* Color definition (RGB and pixel color). */
3196 XColor color;
3198 /* Color name. */
3199 char name[FLEXIBLE_ARRAY_MEMBER];
3202 /* The hash table used for the color cache, and its bucket vector
3203 size. */
3205 #define XPM_COLOR_CACHE_BUCKETS 1001
3206 static struct xpm_cached_color **xpm_color_cache;
3208 /* Initialize the color cache. */
3210 static void
3211 xpm_init_color_cache (struct frame *f, XpmAttributes *attrs)
3213 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
3214 xpm_color_cache = xzalloc (nbytes);
3215 init_color_table ();
3217 if (attrs->valuemask & XpmColorSymbols)
3219 int i;
3220 XColor color;
3222 for (i = 0; i < attrs->numsymbols; ++i)
3223 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3224 attrs->colorsymbols[i].value, &color))
3226 color.pixel = lookup_rgb_color (f, color.red, color.green,
3227 color.blue);
3228 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
3233 /* Free the color cache. */
3235 static void
3236 xpm_free_color_cache (void)
3238 struct xpm_cached_color *p, *next;
3239 int i;
3241 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
3242 for (p = xpm_color_cache[i]; p; p = next)
3244 next = p->next;
3245 xfree (p);
3248 xfree (xpm_color_cache);
3249 xpm_color_cache = NULL;
3250 free_color_table ();
3253 /* Return the bucket index for color named COLOR_NAME in the color
3254 cache. */
3256 static int
3257 xpm_color_bucket (char *color_name)
3259 EMACS_UINT hash = hash_string (color_name, strlen (color_name));
3260 return hash % XPM_COLOR_CACHE_BUCKETS;
3264 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3265 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3266 entry added. */
3268 static struct xpm_cached_color *
3269 xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket)
3271 size_t nbytes;
3272 struct xpm_cached_color *p;
3274 if (bucket < 0)
3275 bucket = xpm_color_bucket (color_name);
3277 nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1;
3278 p = xmalloc (nbytes);
3279 strcpy (p->name, color_name);
3280 p->color = *color;
3281 p->next = xpm_color_cache[bucket];
3282 xpm_color_cache[bucket] = p;
3283 return p;
3286 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3287 return the cached definition in *COLOR. Otherwise, make a new
3288 entry in the cache and allocate the color. Value is false if color
3289 allocation failed. */
3291 static bool
3292 xpm_lookup_color (struct frame *f, char *color_name, XColor *color)
3294 struct xpm_cached_color *p;
3295 int h = xpm_color_bucket (color_name);
3297 for (p = xpm_color_cache[h]; p; p = p->next)
3298 if (strcmp (p->name, color_name) == 0)
3299 break;
3301 if (p != NULL)
3302 *color = p->color;
3303 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3304 color_name, color))
3306 color->pixel = lookup_rgb_color (f, color->red, color->green,
3307 color->blue);
3308 p = xpm_cache_color (f, color_name, color, h);
3310 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3311 with transparency, and it's useful. */
3312 else if (strcmp ("opaque", color_name) == 0)
3314 memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */
3315 color->pixel = FRAME_FOREGROUND_PIXEL (f);
3316 p = xpm_cache_color (f, color_name, color, h);
3319 return p != NULL;
3323 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3324 CLOSURE is a pointer to the frame on which we allocate the
3325 color. Return in *COLOR the allocated color. Value is non-zero
3326 if successful. */
3328 static int
3329 xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color,
3330 void *closure)
3332 return xpm_lookup_color (closure, color_name, color);
3336 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3337 is a pointer to the frame on which we allocate the color. Value is
3338 non-zero if successful. */
3340 static int
3341 xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure)
3343 return 1;
3346 #endif /* ALLOC_XPM_COLORS */
3349 #ifdef WINDOWSNT
3351 /* XPM library details. */
3353 DEF_IMGLIB_FN (void, XpmFreeAttributes, (XpmAttributes *));
3354 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer, (Display *, char *, xpm_XImage **,
3355 xpm_XImage **, XpmAttributes *));
3356 DEF_IMGLIB_FN (int, XpmReadFileToImage, (Display *, char *, xpm_XImage **,
3357 xpm_XImage **, XpmAttributes *));
3358 DEF_IMGLIB_FN (void, XImageFree, (xpm_XImage *));
3360 static bool
3361 init_xpm_functions (void)
3363 HMODULE library;
3365 if (!(library = w32_delayed_load (Qxpm)))
3366 return 0;
3368 LOAD_IMGLIB_FN (library, XpmFreeAttributes);
3369 LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer);
3370 LOAD_IMGLIB_FN (library, XpmReadFileToImage);
3371 LOAD_IMGLIB_FN (library, XImageFree);
3372 return 1;
3375 #endif /* WINDOWSNT */
3377 #if defined HAVE_NTGUI && !defined WINDOWSNT
3378 /* Glue for code below */
3379 #define fn_XpmReadFileToImage XpmReadFileToImage
3380 #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
3381 #define fn_XImageFree XImageFree
3382 #define fn_XpmFreeAttributes XpmFreeAttributes
3383 #endif /* HAVE_NTGUI && !WINDOWSNT */
3385 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3386 for XPM images. Such a list must consist of conses whose car and
3387 cdr are strings. */
3389 static bool
3390 xpm_valid_color_symbols_p (Lisp_Object color_symbols)
3392 while (CONSP (color_symbols))
3394 Lisp_Object sym = XCAR (color_symbols);
3395 if (!CONSP (sym)
3396 || !STRINGP (XCAR (sym))
3397 || !STRINGP (XCDR (sym)))
3398 break;
3399 color_symbols = XCDR (color_symbols);
3402 return NILP (color_symbols);
3406 /* Value is true if OBJECT is a valid XPM image specification. */
3408 static bool
3409 xpm_image_p (Lisp_Object object)
3411 struct image_keyword fmt[XPM_LAST];
3412 memcpy (fmt, xpm_format, sizeof fmt);
3413 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
3414 /* Either `:file' or `:data' must be present. */
3415 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
3416 /* Either no `:color-symbols' or it's a list of conses
3417 whose car and cdr are strings. */
3418 && (fmt[XPM_COLOR_SYMBOLS].count == 0
3419 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
3422 #endif /* HAVE_XPM || HAVE_NS */
3424 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3425 ptrdiff_t
3426 x_create_bitmap_from_xpm_data (struct frame *f, const char **bits)
3428 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
3429 ptrdiff_t id;
3430 int rc;
3431 XpmAttributes attrs;
3432 Pixmap bitmap, mask;
3434 memset (&attrs, 0, sizeof attrs);
3436 attrs.visual = FRAME_X_VISUAL (f);
3437 attrs.colormap = FRAME_X_COLORMAP (f);
3438 attrs.valuemask |= XpmVisual;
3439 attrs.valuemask |= XpmColormap;
3441 rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3442 (char **) bits, &bitmap, &mask, &attrs);
3443 if (rc != XpmSuccess)
3445 XpmFreeAttributes (&attrs);
3446 return -1;
3449 id = x_allocate_bitmap_record (f);
3450 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
3451 dpyinfo->bitmaps[id - 1].have_mask = true;
3452 dpyinfo->bitmaps[id - 1].mask = mask;
3453 dpyinfo->bitmaps[id - 1].file = NULL;
3454 dpyinfo->bitmaps[id - 1].height = attrs.height;
3455 dpyinfo->bitmaps[id - 1].width = attrs.width;
3456 dpyinfo->bitmaps[id - 1].depth = attrs.depth;
3457 dpyinfo->bitmaps[id - 1].refcount = 1;
3459 XpmFreeAttributes (&attrs);
3460 return id;
3462 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3464 /* Load image IMG which will be displayed on frame F. Value is
3465 true if successful. */
3467 #ifdef HAVE_XPM
3469 static bool
3470 xpm_load (struct frame *f, struct image *img)
3472 int rc;
3473 XpmAttributes attrs;
3474 Lisp_Object specified_file, color_symbols;
3475 #ifdef HAVE_NTGUI
3476 HDC hdc;
3477 xpm_XImage * xpm_image = NULL, * xpm_mask = NULL;
3478 #endif /* HAVE_NTGUI */
3480 /* Configure the XPM lib. Use the visual of frame F. Allocate
3481 close colors. Return colors allocated. */
3482 memset (&attrs, 0, sizeof attrs);
3484 #ifndef HAVE_NTGUI
3485 attrs.visual = FRAME_X_VISUAL (f);
3486 attrs.colormap = FRAME_X_COLORMAP (f);
3487 attrs.valuemask |= XpmVisual;
3488 attrs.valuemask |= XpmColormap;
3489 #endif /* HAVE_NTGUI */
3491 #ifdef ALLOC_XPM_COLORS
3492 /* Allocate colors with our own functions which handle
3493 failing color allocation more gracefully. */
3494 attrs.color_closure = f;
3495 attrs.alloc_color = xpm_alloc_color;
3496 attrs.free_colors = xpm_free_colors;
3497 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
3498 #else /* not ALLOC_XPM_COLORS */
3499 /* Let the XPM lib allocate colors. */
3500 attrs.valuemask |= XpmReturnAllocPixels;
3501 #ifdef XpmAllocCloseColors
3502 attrs.alloc_close_colors = 1;
3503 attrs.valuemask |= XpmAllocCloseColors;
3504 #else /* not XpmAllocCloseColors */
3505 attrs.closeness = 600;
3506 attrs.valuemask |= XpmCloseness;
3507 #endif /* not XpmAllocCloseColors */
3508 #endif /* ALLOC_XPM_COLORS */
3510 /* If image specification contains symbolic color definitions, add
3511 these to `attrs'. */
3512 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
3513 if (CONSP (color_symbols))
3515 Lisp_Object tail;
3516 XpmColorSymbol *xpm_syms;
3517 int i, size;
3519 attrs.valuemask |= XpmColorSymbols;
3521 /* Count number of symbols. */
3522 attrs.numsymbols = 0;
3523 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
3524 ++attrs.numsymbols;
3526 /* Allocate an XpmColorSymbol array. */
3527 size = attrs.numsymbols * sizeof *xpm_syms;
3528 xpm_syms = alloca (size);
3529 memset (xpm_syms, 0, size);
3530 attrs.colorsymbols = xpm_syms;
3532 /* Fill the color symbol array. */
3533 for (tail = color_symbols, i = 0;
3534 CONSP (tail);
3535 ++i, tail = XCDR (tail))
3537 Lisp_Object name;
3538 Lisp_Object color;
3539 char *empty_string = (char *) "";
3541 if (!CONSP (XCAR (tail)))
3543 xpm_syms[i].name = empty_string;
3544 xpm_syms[i].value = empty_string;
3545 continue;
3547 name = XCAR (XCAR (tail));
3548 color = XCDR (XCAR (tail));
3549 if (STRINGP (name))
3551 xpm_syms[i].name = alloca (SCHARS (name) + 1);
3552 strcpy (xpm_syms[i].name, SSDATA (name));
3554 else
3555 xpm_syms[i].name = empty_string;
3556 if (STRINGP (color))
3558 xpm_syms[i].value = alloca (SCHARS (color) + 1);
3559 strcpy (xpm_syms[i].value, SSDATA (color));
3561 else
3562 xpm_syms[i].value = empty_string;
3566 /* Create a pixmap for the image, either from a file, or from a
3567 string buffer containing data in the same format as an XPM file. */
3568 #ifdef ALLOC_XPM_COLORS
3569 xpm_init_color_cache (f, &attrs);
3570 #endif
3572 specified_file = image_spec_value (img->spec, QCfile, NULL);
3574 #ifdef HAVE_NTGUI
3576 HDC frame_dc = get_frame_dc (f);
3577 hdc = CreateCompatibleDC (frame_dc);
3578 release_frame_dc (f, frame_dc);
3580 #endif /* HAVE_NTGUI */
3582 if (STRINGP (specified_file))
3584 Lisp_Object file = x_find_image_file (specified_file);
3585 if (!STRINGP (file))
3587 image_error ("Cannot find image file `%s'", specified_file, Qnil);
3588 #ifdef ALLOC_XPM_COLORS
3589 xpm_free_color_cache ();
3590 #endif
3591 return 0;
3594 #ifdef HAVE_NTGUI
3595 #ifdef WINDOWSNT
3596 /* FILE is encoded in UTF-8, but image libraries on Windows
3597 support neither UTF-8 nor UTF-16 encoded file names. So we
3598 need to re-encode it in ANSI. */
3599 file = ansi_encode_filename (file);
3600 #endif
3601 /* XpmReadFileToPixmap is not available in the Windows port of
3602 libxpm. But XpmReadFileToImage almost does what we want. */
3603 rc = fn_XpmReadFileToImage (&hdc, SDATA (file),
3604 &xpm_image, &xpm_mask,
3605 &attrs);
3606 #else
3607 rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file),
3608 &img->ximg, &img->mask_img,
3609 &attrs);
3610 #endif /* HAVE_NTGUI */
3612 else
3614 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
3615 if (!STRINGP (buffer))
3617 image_error ("Invalid image data `%s'", buffer, Qnil);
3618 #ifdef ALLOC_XPM_COLORS
3619 xpm_free_color_cache ();
3620 #endif
3621 return 0;
3623 #ifdef HAVE_NTGUI
3624 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3625 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3626 rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer),
3627 &xpm_image, &xpm_mask,
3628 &attrs);
3629 #else
3630 rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer),
3631 &img->ximg, &img->mask_img,
3632 &attrs);
3633 #endif /* HAVE_NTGUI */
3636 #ifdef HAVE_X_WINDOWS
3637 if (rc == XpmSuccess)
3639 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3640 img->ximg->width, img->ximg->height,
3641 img->ximg->depth);
3642 if (img->pixmap == NO_PIXMAP)
3644 x_clear_image (f, img);
3645 rc = XpmNoMemory;
3647 else if (img->mask_img)
3649 img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3650 img->mask_img->width,
3651 img->mask_img->height,
3652 img->mask_img->depth);
3653 if (img->mask == NO_PIXMAP)
3655 x_clear_image (f, img);
3656 rc = XpmNoMemory;
3660 #endif
3662 if (rc == XpmSuccess)
3664 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3665 img->colors = colors_in_color_table (&img->ncolors);
3666 #else /* not ALLOC_XPM_COLORS */
3667 int i;
3669 #ifdef HAVE_NTGUI
3670 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3671 plus some duplicate attributes. */
3672 if (xpm_image && xpm_image->bitmap)
3674 img->pixmap = xpm_image->bitmap;
3675 /* XImageFree in libXpm frees XImage struct without destroying
3676 the bitmap, which is what we want. */
3677 fn_XImageFree (xpm_image);
3679 if (xpm_mask && xpm_mask->bitmap)
3681 /* The mask appears to be inverted compared with what we expect.
3682 TODO: invert our expectations. See other places where we
3683 have to invert bits because our idea of masks is backwards. */
3684 HGDIOBJ old_obj;
3685 old_obj = SelectObject (hdc, xpm_mask->bitmap);
3687 PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT);
3688 SelectObject (hdc, old_obj);
3690 img->mask = xpm_mask->bitmap;
3691 fn_XImageFree (xpm_mask);
3692 DeleteDC (hdc);
3695 DeleteDC (hdc);
3696 #endif /* HAVE_NTGUI */
3698 /* Remember allocated colors. */
3699 img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors);
3700 img->ncolors = attrs.nalloc_pixels;
3701 for (i = 0; i < attrs.nalloc_pixels; ++i)
3703 img->colors[i] = attrs.alloc_pixels[i];
3704 #ifdef DEBUG_X_COLORS
3705 register_color (img->colors[i]);
3706 #endif
3708 #endif /* not ALLOC_XPM_COLORS */
3710 img->width = attrs.width;
3711 img->height = attrs.height;
3712 eassert (img->width > 0 && img->height > 0);
3714 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3715 #ifdef HAVE_NTGUI
3716 fn_XpmFreeAttributes (&attrs);
3717 #else
3718 XpmFreeAttributes (&attrs);
3719 #endif /* HAVE_NTGUI */
3721 #ifdef HAVE_X_WINDOWS
3722 /* Maybe fill in the background field while we have ximg handy. */
3723 IMAGE_BACKGROUND (img, f, img->ximg);
3724 if (img->mask_img)
3725 /* Fill in the background_transparent field while we have the
3726 mask handy. */
3727 image_background_transparent (img, f, img->mask_img);
3728 #endif
3730 else
3732 #ifdef HAVE_NTGUI
3733 DeleteDC (hdc);
3734 #endif /* HAVE_NTGUI */
3736 switch (rc)
3738 case XpmOpenFailed:
3739 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
3740 break;
3742 case XpmFileInvalid:
3743 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
3744 break;
3746 case XpmNoMemory:
3747 image_error ("Out of memory (%s)", img->spec, Qnil);
3748 break;
3750 case XpmColorFailed:
3751 image_error ("Color allocation error (%s)", img->spec, Qnil);
3752 break;
3754 default:
3755 image_error ("Unknown error (%s)", img->spec, Qnil);
3756 break;
3760 #ifdef ALLOC_XPM_COLORS
3761 xpm_free_color_cache ();
3762 #endif
3763 return rc == XpmSuccess;
3766 #endif /* HAVE_XPM */
3768 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3770 /* XPM support functions for NS where libxpm is not available.
3771 Only XPM version 3 (without any extensions) is supported. */
3773 static void xpm_put_color_table_v (Lisp_Object, const unsigned char *,
3774 int, Lisp_Object);
3775 static Lisp_Object xpm_get_color_table_v (Lisp_Object,
3776 const unsigned char *, int);
3777 static void xpm_put_color_table_h (Lisp_Object, const unsigned char *,
3778 int, Lisp_Object);
3779 static Lisp_Object xpm_get_color_table_h (Lisp_Object,
3780 const unsigned char *, int);
3782 /* Tokens returned from xpm_scan. */
3784 enum xpm_token
3786 XPM_TK_IDENT = 256,
3787 XPM_TK_STRING,
3788 XPM_TK_EOF
3791 /* Scan an XPM data and return a character (< 256) or a token defined
3792 by enum xpm_token above. *S and END are the start (inclusive) and
3793 the end (exclusive) addresses of the data, respectively. Advance
3794 *S while scanning. If token is either XPM_TK_IDENT or
3795 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3796 length of the corresponding token, respectively. */
3798 static int
3799 xpm_scan (const unsigned char **s,
3800 const unsigned char *end,
3801 const unsigned char **beg,
3802 ptrdiff_t *len)
3804 int c;
3806 while (*s < end)
3808 /* Skip white-space. */
3809 while (*s < end && (c = *(*s)++, c_isspace (c)))
3812 /* gnus-pointer.xpm uses '-' in its identifier.
3813 sb-dir-plus.xpm uses '+' in its identifier. */
3814 if (c_isalpha (c) || c == '_' || c == '-' || c == '+')
3816 *beg = *s - 1;
3817 while (*s < end
3818 && (c = **s, c_isalnum (c)
3819 || c == '_' || c == '-' || c == '+'))
3820 ++*s;
3821 *len = *s - *beg;
3822 return XPM_TK_IDENT;
3824 else if (c == '"')
3826 *beg = *s;
3827 while (*s < end && **s != '"')
3828 ++*s;
3829 *len = *s - *beg;
3830 if (*s < end)
3831 ++*s;
3832 return XPM_TK_STRING;
3834 else if (c == '/')
3836 if (*s < end && **s == '*')
3838 /* C-style comment. */
3839 ++*s;
3842 while (*s < end && *(*s)++ != '*')
3845 while (*s < end && **s != '/');
3846 if (*s < end)
3847 ++*s;
3849 else
3850 return c;
3852 else
3853 return c;
3856 return XPM_TK_EOF;
3859 /* Functions for color table lookup in XPM data. A key is a string
3860 specifying the color of each pixel in XPM data. A value is either
3861 an integer that specifies a pixel color, Qt that specifies
3862 transparency, or Qnil for the unspecified color. If the length of
3863 the key string is one, a vector is used as a table. Otherwise, a
3864 hash table is used. */
3866 static Lisp_Object
3867 xpm_make_color_table_v (void (**put_func) (Lisp_Object,
3868 const unsigned char *,
3869 int,
3870 Lisp_Object),
3871 Lisp_Object (**get_func) (Lisp_Object,
3872 const unsigned char *,
3873 int))
3875 *put_func = xpm_put_color_table_v;
3876 *get_func = xpm_get_color_table_v;
3877 return Fmake_vector (make_number (256), Qnil);
3880 static void
3881 xpm_put_color_table_v (Lisp_Object color_table,
3882 const unsigned char *chars_start,
3883 int chars_len,
3884 Lisp_Object color)
3886 ASET (color_table, *chars_start, color);
3889 static Lisp_Object
3890 xpm_get_color_table_v (Lisp_Object color_table,
3891 const unsigned char *chars_start,
3892 int chars_len)
3894 return AREF (color_table, *chars_start);
3897 static Lisp_Object
3898 xpm_make_color_table_h (void (**put_func) (Lisp_Object,
3899 const unsigned char *,
3900 int,
3901 Lisp_Object),
3902 Lisp_Object (**get_func) (Lisp_Object,
3903 const unsigned char *,
3904 int))
3906 *put_func = xpm_put_color_table_h;
3907 *get_func = xpm_get_color_table_h;
3908 return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE),
3909 make_float (DEFAULT_REHASH_SIZE),
3910 make_float (DEFAULT_REHASH_THRESHOLD),
3911 Qnil);
3914 static void
3915 xpm_put_color_table_h (Lisp_Object color_table,
3916 const unsigned char *chars_start,
3917 int chars_len,
3918 Lisp_Object color)
3920 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
3921 EMACS_UINT hash_code;
3922 Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
3924 hash_lookup (table, chars, &hash_code);
3925 hash_put (table, chars, color, hash_code);
3928 static Lisp_Object
3929 xpm_get_color_table_h (Lisp_Object color_table,
3930 const unsigned char *chars_start,
3931 int chars_len)
3933 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
3934 ptrdiff_t i =
3935 hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL);
3937 return i >= 0 ? HASH_VALUE (table, i) : Qnil;
3940 enum xpm_color_key {
3941 XPM_COLOR_KEY_S,
3942 XPM_COLOR_KEY_M,
3943 XPM_COLOR_KEY_G4,
3944 XPM_COLOR_KEY_G,
3945 XPM_COLOR_KEY_C
3948 static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"};
3950 static int
3951 xpm_str_to_color_key (const char *s)
3953 int i;
3955 for (i = 0;
3956 i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0];
3957 i++)
3958 if (strcmp (xpm_color_key_strings[i], s) == 0)
3959 return i;
3960 return -1;
3963 static bool
3964 xpm_load_image (struct frame *f,
3965 struct image *img,
3966 const unsigned char *contents,
3967 const unsigned char *end)
3969 const unsigned char *s = contents, *beg, *str;
3970 unsigned char buffer[BUFSIZ];
3971 int width, height, x, y;
3972 int num_colors, chars_per_pixel;
3973 ptrdiff_t len;
3974 int LA1;
3975 void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object);
3976 Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int);
3977 Lisp_Object frame, color_symbols, color_table;
3978 int best_key;
3979 bool have_mask = false;
3980 XImagePtr ximg = NULL, mask_img = NULL;
3982 #define match() \
3983 LA1 = xpm_scan (&s, end, &beg, &len)
3985 #define expect(TOKEN) \
3986 do \
3988 if (LA1 != (TOKEN)) \
3989 goto failure; \
3990 match (); \
3992 while (0)
3994 #define expect_ident(IDENT) \
3995 if (LA1 == XPM_TK_IDENT \
3996 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3997 match (); \
3998 else \
3999 goto failure
4001 if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0))
4002 goto failure;
4003 s += 9;
4004 match ();
4005 expect_ident ("static");
4006 expect_ident ("char");
4007 expect ('*');
4008 expect (XPM_TK_IDENT);
4009 expect ('[');
4010 expect (']');
4011 expect ('=');
4012 expect ('{');
4013 expect (XPM_TK_STRING);
4014 if (len >= BUFSIZ)
4015 goto failure;
4016 memcpy (buffer, beg, len);
4017 buffer[len] = '\0';
4018 if (sscanf (buffer, "%d %d %d %d", &width, &height,
4019 &num_colors, &chars_per_pixel) != 4
4020 || width <= 0 || height <= 0
4021 || num_colors <= 0 || chars_per_pixel <= 0)
4022 goto failure;
4024 if (!check_image_size (f, width, height))
4026 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
4027 goto failure;
4030 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)
4031 #ifndef HAVE_NS
4032 || !image_create_x_image_and_pixmap (f, img, width, height, 1,
4033 &mask_img, 1)
4034 #endif
4037 image_error ("Image too large", Qnil, Qnil);
4038 goto failure;
4041 expect (',');
4043 XSETFRAME (frame, f);
4044 if (!NILP (Fxw_display_color_p (frame)))
4045 best_key = XPM_COLOR_KEY_C;
4046 else if (!NILP (Fx_display_grayscale_p (frame)))
4047 best_key = (XFASTINT (Fx_display_planes (frame)) > 2
4048 ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4);
4049 else
4050 best_key = XPM_COLOR_KEY_M;
4052 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
4053 if (chars_per_pixel == 1)
4054 color_table = xpm_make_color_table_v (&put_color_table,
4055 &get_color_table);
4056 else
4057 color_table = xpm_make_color_table_h (&put_color_table,
4058 &get_color_table);
4060 while (num_colors-- > 0)
4062 char *color, *max_color;
4063 int key, next_key, max_key = 0;
4064 Lisp_Object symbol_color = Qnil, color_val;
4065 XColor cdef;
4067 expect (XPM_TK_STRING);
4068 if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel)
4069 goto failure;
4070 memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel);
4071 buffer[len - chars_per_pixel] = '\0';
4073 str = strtok (buffer, " \t");
4074 if (str == NULL)
4075 goto failure;
4076 key = xpm_str_to_color_key (str);
4077 if (key < 0)
4078 goto failure;
4081 color = strtok (NULL, " \t");
4082 if (color == NULL)
4083 goto failure;
4085 while ((str = strtok (NULL, " \t")) != NULL)
4087 next_key = xpm_str_to_color_key (str);
4088 if (next_key >= 0)
4089 break;
4090 color[strlen (color)] = ' ';
4093 if (key == XPM_COLOR_KEY_S)
4095 if (NILP (symbol_color))
4096 symbol_color = build_string (color);
4098 else if (max_key < key && key <= best_key)
4100 max_key = key;
4101 max_color = color;
4103 key = next_key;
4105 while (str);
4107 color_val = Qnil;
4108 if (!NILP (color_symbols) && !NILP (symbol_color))
4110 Lisp_Object specified_color = Fassoc (symbol_color, color_symbols);
4112 if (CONSP (specified_color) && STRINGP (XCDR (specified_color)))
4114 if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0)
4115 color_val = Qt;
4116 else if (x_defined_color (f, SSDATA (XCDR (specified_color)),
4117 &cdef, 0))
4118 color_val = make_number (cdef.pixel);
4121 if (NILP (color_val) && max_key > 0)
4123 if (xstrcasecmp (max_color, "None") == 0)
4124 color_val = Qt;
4125 else if (x_defined_color (f, max_color, &cdef, 0))
4126 color_val = make_number (cdef.pixel);
4128 if (!NILP (color_val))
4129 (*put_color_table) (color_table, beg, chars_per_pixel, color_val);
4131 expect (',');
4134 for (y = 0; y < height; y++)
4136 expect (XPM_TK_STRING);
4137 str = beg;
4138 if (len < width * chars_per_pixel)
4139 goto failure;
4140 for (x = 0; x < width; x++, str += chars_per_pixel)
4142 Lisp_Object color_val =
4143 (*get_color_table) (color_table, str, chars_per_pixel);
4145 XPutPixel (ximg, x, y,
4146 (INTEGERP (color_val) ? XINT (color_val)
4147 : FRAME_FOREGROUND_PIXEL (f)));
4148 #ifndef HAVE_NS
4149 XPutPixel (mask_img, x, y,
4150 (!EQ (color_val, Qt) ? PIX_MASK_DRAW
4151 : (have_mask = true, PIX_MASK_RETAIN)));
4152 #else
4153 if (EQ (color_val, Qt))
4154 ns_set_alpha (ximg, x, y, 0);
4155 #endif
4157 if (y + 1 < height)
4158 expect (',');
4161 img->width = width;
4162 img->height = height;
4164 /* Maybe fill in the background field while we have ximg handy. */
4165 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
4166 IMAGE_BACKGROUND (img, f, ximg);
4168 image_put_x_image (f, img, ximg, 0);
4169 #ifndef HAVE_NS
4170 if (have_mask)
4172 /* Fill in the background_transparent field while we have the
4173 mask handy. */
4174 image_background_transparent (img, f, mask_img);
4176 image_put_x_image (f, img, mask_img, 1);
4178 else
4180 x_destroy_x_image (mask_img);
4181 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
4183 #endif
4184 return 1;
4186 failure:
4187 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
4188 x_destroy_x_image (ximg);
4189 x_destroy_x_image (mask_img);
4190 x_clear_image (f, img);
4191 return 0;
4193 #undef match
4194 #undef expect
4195 #undef expect_ident
4198 static bool
4199 xpm_load (struct frame *f,
4200 struct image *img)
4202 bool success_p = 0;
4203 Lisp_Object file_name;
4205 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4206 file_name = image_spec_value (img->spec, QCfile, NULL);
4207 if (STRINGP (file_name))
4209 Lisp_Object file;
4210 unsigned char *contents;
4211 ptrdiff_t size;
4213 file = x_find_image_file (file_name);
4214 if (!STRINGP (file))
4216 image_error ("Cannot find image file `%s'", file_name, Qnil);
4217 return 0;
4220 contents = slurp_file (SSDATA (file), &size);
4221 if (contents == NULL)
4223 image_error ("Error loading XPM image `%s'", img->spec, Qnil);
4224 return 0;
4227 success_p = xpm_load_image (f, img, contents, contents + size);
4228 xfree (contents);
4230 else
4232 Lisp_Object data;
4234 data = image_spec_value (img->spec, QCdata, NULL);
4235 if (!STRINGP (data))
4237 image_error ("Invalid image data `%s'", data, Qnil);
4238 return 0;
4240 success_p = xpm_load_image (f, img, SDATA (data),
4241 SDATA (data) + SBYTES (data));
4244 return success_p;
4247 #endif /* HAVE_NS && !HAVE_XPM */
4251 /***********************************************************************
4252 Color table
4253 ***********************************************************************/
4255 #ifdef COLOR_TABLE_SUPPORT
4257 /* An entry in the color table mapping an RGB color to a pixel color. */
4259 struct ct_color
4261 int r, g, b;
4262 unsigned long pixel;
4264 /* Next in color table collision list. */
4265 struct ct_color *next;
4268 /* The bucket vector size to use. Must be prime. */
4270 #define CT_SIZE 101
4272 /* Value is a hash of the RGB color given by R, G, and B. */
4274 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4276 /* The color hash table. */
4278 static struct ct_color **ct_table;
4280 /* Number of entries in the color table. */
4282 static int ct_colors_allocated;
4283 enum
4285 ct_colors_allocated_max =
4286 min (INT_MAX,
4287 min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long))
4290 /* Initialize the color table. */
4292 static void
4293 init_color_table (void)
4295 int size = CT_SIZE * sizeof (*ct_table);
4296 ct_table = xzalloc (size);
4297 ct_colors_allocated = 0;
4301 /* Free memory associated with the color table. */
4303 static void
4304 free_color_table (void)
4306 int i;
4307 struct ct_color *p, *next;
4309 for (i = 0; i < CT_SIZE; ++i)
4310 for (p = ct_table[i]; p; p = next)
4312 next = p->next;
4313 xfree (p);
4316 xfree (ct_table);
4317 ct_table = NULL;
4321 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4322 entry for that color already is in the color table, return the
4323 pixel color of that entry. Otherwise, allocate a new color for R,
4324 G, B, and make an entry in the color table. */
4326 static unsigned long
4327 lookup_rgb_color (struct frame *f, int r, int g, int b)
4329 unsigned hash = CT_HASH_RGB (r, g, b);
4330 int i = hash % CT_SIZE;
4331 struct ct_color *p;
4332 Display_Info *dpyinfo;
4334 /* Handle TrueColor visuals specially, which improves performance by
4335 two orders of magnitude. Freeing colors on TrueColor visuals is
4336 a nop, and pixel colors specify RGB values directly. See also
4337 the Xlib spec, chapter 3.1. */
4338 dpyinfo = FRAME_DISPLAY_INFO (f);
4339 if (dpyinfo->red_bits > 0)
4341 unsigned long pr, pg, pb;
4343 /* Apply gamma-correction like normal color allocation does. */
4344 if (f->gamma)
4346 XColor color;
4347 color.red = r, color.green = g, color.blue = b;
4348 gamma_correct (f, &color);
4349 r = color.red, g = color.green, b = color.blue;
4352 /* Scale down RGB values to the visual's bits per RGB, and shift
4353 them to the right position in the pixel color. Note that the
4354 original RGB values are 16-bit values, as usual in X. */
4355 pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset;
4356 pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
4357 pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset;
4359 /* Assemble the pixel color. */
4360 return pr | pg | pb;
4363 for (p = ct_table[i]; p; p = p->next)
4364 if (p->r == r && p->g == g && p->b == b)
4365 break;
4367 if (p == NULL)
4370 #ifdef HAVE_X_WINDOWS
4371 XColor color;
4372 Colormap cmap;
4373 bool rc;
4374 #else
4375 COLORREF color;
4376 #endif
4378 if (ct_colors_allocated_max <= ct_colors_allocated)
4379 return FRAME_FOREGROUND_PIXEL (f);
4381 #ifdef HAVE_X_WINDOWS
4382 color.red = r;
4383 color.green = g;
4384 color.blue = b;
4386 cmap = FRAME_X_COLORMAP (f);
4387 rc = x_alloc_nearest_color (f, cmap, &color);
4388 if (rc)
4390 ++ct_colors_allocated;
4391 p = xmalloc (sizeof *p);
4392 p->r = r;
4393 p->g = g;
4394 p->b = b;
4395 p->pixel = color.pixel;
4396 p->next = ct_table[i];
4397 ct_table[i] = p;
4399 else
4400 return FRAME_FOREGROUND_PIXEL (f);
4402 #else
4403 #ifdef HAVE_NTGUI
4404 color = PALETTERGB (r, g, b);
4405 #else
4406 color = RGB_TO_ULONG (r, g, b);
4407 #endif /* HAVE_NTGUI */
4408 ++ct_colors_allocated;
4409 p = xmalloc (sizeof *p);
4410 p->r = r;
4411 p->g = g;
4412 p->b = b;
4413 p->pixel = color;
4414 p->next = ct_table[i];
4415 ct_table[i] = p;
4416 #endif /* HAVE_X_WINDOWS */
4420 return p->pixel;
4424 /* Look up pixel color PIXEL which is used on frame F in the color
4425 table. If not already present, allocate it. Value is PIXEL. */
4427 static unsigned long
4428 lookup_pixel_color (struct frame *f, unsigned long pixel)
4430 int i = pixel % CT_SIZE;
4431 struct ct_color *p;
4433 for (p = ct_table[i]; p; p = p->next)
4434 if (p->pixel == pixel)
4435 break;
4437 if (p == NULL)
4439 XColor color;
4440 Colormap cmap;
4441 bool rc;
4443 if (ct_colors_allocated_max <= ct_colors_allocated)
4444 return FRAME_FOREGROUND_PIXEL (f);
4446 #ifdef HAVE_X_WINDOWS
4447 cmap = FRAME_X_COLORMAP (f);
4448 color.pixel = pixel;
4449 x_query_color (f, &color);
4450 rc = x_alloc_nearest_color (f, cmap, &color);
4451 #else
4452 block_input ();
4453 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
4454 color.pixel = pixel;
4455 XQueryColor (NULL, cmap, &color);
4456 rc = x_alloc_nearest_color (f, cmap, &color);
4457 unblock_input ();
4458 #endif /* HAVE_X_WINDOWS */
4460 if (rc)
4462 ++ct_colors_allocated;
4464 p = xmalloc (sizeof *p);
4465 p->r = color.red;
4466 p->g = color.green;
4467 p->b = color.blue;
4468 p->pixel = pixel;
4469 p->next = ct_table[i];
4470 ct_table[i] = p;
4472 else
4473 return FRAME_FOREGROUND_PIXEL (f);
4475 return p->pixel;
4479 /* Value is a vector of all pixel colors contained in the color table,
4480 allocated via xmalloc. Set *N to the number of colors. */
4482 static unsigned long *
4483 colors_in_color_table (int *n)
4485 int i, j;
4486 struct ct_color *p;
4487 unsigned long *colors;
4489 if (ct_colors_allocated == 0)
4491 *n = 0;
4492 colors = NULL;
4494 else
4496 colors = xmalloc (ct_colors_allocated * sizeof *colors);
4497 *n = ct_colors_allocated;
4499 for (i = j = 0; i < CT_SIZE; ++i)
4500 for (p = ct_table[i]; p; p = p->next)
4501 colors[j++] = p->pixel;
4504 return colors;
4507 #else /* COLOR_TABLE_SUPPORT */
4509 static unsigned long
4510 lookup_rgb_color (struct frame *f, int r, int g, int b)
4512 unsigned long pixel;
4514 #ifdef HAVE_NTGUI
4515 pixel = PALETTERGB (r >> 8, g >> 8, b >> 8);
4516 #endif /* HAVE_NTGUI */
4518 #ifdef HAVE_NS
4519 pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8);
4520 #endif /* HAVE_NS */
4521 return pixel;
4524 static void
4525 init_color_table (void)
4528 #endif /* COLOR_TABLE_SUPPORT */
4531 /***********************************************************************
4532 Algorithms
4533 ***********************************************************************/
4535 /* Edge detection matrices for different edge-detection
4536 strategies. */
4538 static int emboss_matrix[9] = {
4539 /* x - 1 x x + 1 */
4540 2, -1, 0, /* y - 1 */
4541 -1, 0, 1, /* y */
4542 0, 1, -2 /* y + 1 */
4545 static int laplace_matrix[9] = {
4546 /* x - 1 x x + 1 */
4547 1, 0, 0, /* y - 1 */
4548 0, 0, 0, /* y */
4549 0, 0, -1 /* y + 1 */
4552 /* Value is the intensity of the color whose red/green/blue values
4553 are R, G, and B. */
4555 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4558 /* On frame F, return an array of XColor structures describing image
4559 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4560 means also fill the red/green/blue members of the XColor
4561 structures. Value is a pointer to the array of XColors structures,
4562 allocated with xmalloc; it must be freed by the caller. */
4564 static XColor *
4565 x_to_xcolors (struct frame *f, struct image *img, bool rgb_p)
4567 int x, y;
4568 XColor *colors, *p;
4569 XImagePtr_or_DC ximg;
4570 #ifdef HAVE_NTGUI
4571 HGDIOBJ prev;
4572 #endif /* HAVE_NTGUI */
4574 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height)
4575 memory_full (SIZE_MAX);
4576 colors = xmalloc (sizeof *colors * img->width * img->height);
4578 /* Get the X image or create a memory device context for IMG. */
4579 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
4581 /* Fill the `pixel' members of the XColor array. I wished there
4582 were an easy and portable way to circumvent XGetPixel. */
4583 p = colors;
4584 for (y = 0; y < img->height; ++y)
4586 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4587 XColor *row = p;
4588 for (x = 0; x < img->width; ++x, ++p)
4589 p->pixel = GET_PIXEL (ximg, x, y);
4590 if (rgb_p)
4591 x_query_colors (f, row, img->width);
4593 #else
4595 for (x = 0; x < img->width; ++x, ++p)
4597 /* W32_TODO: palette support needed here? */
4598 p->pixel = GET_PIXEL (ximg, x, y);
4599 if (rgb_p)
4601 p->red = RED16_FROM_ULONG (p->pixel);
4602 p->green = GREEN16_FROM_ULONG (p->pixel);
4603 p->blue = BLUE16_FROM_ULONG (p->pixel);
4606 #endif /* HAVE_X_WINDOWS */
4609 image_unget_x_image_or_dc (img, 0, ximg, prev);
4611 return colors;
4614 #ifdef HAVE_NTGUI
4616 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4617 created with CreateDIBSection, with the pointer to the bit values
4618 stored in ximg->data. */
4620 static void
4621 XPutPixel (XImagePtr ximg, int x, int y, COLORREF color)
4623 int width = ximg->info.bmiHeader.biWidth;
4624 unsigned char * pixel;
4626 /* True color images. */
4627 if (ximg->info.bmiHeader.biBitCount == 24)
4629 int rowbytes = width * 3;
4630 /* Ensure scanlines are aligned on 4 byte boundaries. */
4631 if (rowbytes % 4)
4632 rowbytes += 4 - (rowbytes % 4);
4634 pixel = ximg->data + y * rowbytes + x * 3;
4635 /* Windows bitmaps are in BGR order. */
4636 *pixel = GetBValue (color);
4637 *(pixel + 1) = GetGValue (color);
4638 *(pixel + 2) = GetRValue (color);
4640 /* Monochrome images. */
4641 else if (ximg->info.bmiHeader.biBitCount == 1)
4643 int rowbytes = width / 8;
4644 /* Ensure scanlines are aligned on 4 byte boundaries. */
4645 if (rowbytes % 4)
4646 rowbytes += 4 - (rowbytes % 4);
4647 pixel = ximg->data + y * rowbytes + x / 8;
4648 /* Filter out palette info. */
4649 if (color & 0x00ffffff)
4650 *pixel = *pixel | (1 << x % 8);
4651 else
4652 *pixel = *pixel & ~(1 << x % 8);
4654 else
4655 image_error ("XPutPixel: palette image not supported", Qnil, Qnil);
4658 #endif /* HAVE_NTGUI */
4660 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4661 RGB members are set. F is the frame on which this all happens.
4662 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4664 static void
4665 x_from_xcolors (struct frame *f, struct image *img, XColor *colors)
4667 int x, y;
4668 XImagePtr oimg = NULL;
4669 XColor *p;
4671 init_color_table ();
4673 x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_COLORS);
4674 image_create_x_image_and_pixmap (f, img, img->width, img->height, 0,
4675 &oimg, 0);
4676 p = colors;
4677 for (y = 0; y < img->height; ++y)
4678 for (x = 0; x < img->width; ++x, ++p)
4680 unsigned long pixel;
4681 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
4682 XPutPixel (oimg, x, y, pixel);
4685 xfree (colors);
4687 image_put_x_image (f, img, oimg, 0);
4688 #ifdef COLOR_TABLE_SUPPORT
4689 img->colors = colors_in_color_table (&img->ncolors);
4690 free_color_table ();
4691 #endif /* COLOR_TABLE_SUPPORT */
4695 /* On frame F, perform edge-detection on image IMG.
4697 MATRIX is a nine-element array specifying the transformation
4698 matrix. See emboss_matrix for an example.
4700 COLOR_ADJUST is a color adjustment added to each pixel of the
4701 outgoing image. */
4703 static void
4704 x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjust)
4706 XColor *colors = x_to_xcolors (f, img, 1);
4707 XColor *new, *p;
4708 int x, y, i, sum;
4710 for (i = sum = 0; i < 9; ++i)
4711 sum += eabs (matrix[i]);
4713 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4715 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height)
4716 memory_full (SIZE_MAX);
4717 new = xmalloc (sizeof *new * img->width * img->height);
4719 for (y = 0; y < img->height; ++y)
4721 p = COLOR (new, 0, y);
4722 p->red = p->green = p->blue = 0xffff/2;
4723 p = COLOR (new, img->width - 1, y);
4724 p->red = p->green = p->blue = 0xffff/2;
4727 for (x = 1; x < img->width - 1; ++x)
4729 p = COLOR (new, x, 0);
4730 p->red = p->green = p->blue = 0xffff/2;
4731 p = COLOR (new, x, img->height - 1);
4732 p->red = p->green = p->blue = 0xffff/2;
4735 for (y = 1; y < img->height - 1; ++y)
4737 p = COLOR (new, 1, y);
4739 for (x = 1; x < img->width - 1; ++x, ++p)
4741 int r, g, b, yy, xx;
4743 r = g = b = i = 0;
4744 for (yy = y - 1; yy < y + 2; ++yy)
4745 for (xx = x - 1; xx < x + 2; ++xx, ++i)
4746 if (matrix[i])
4748 XColor *t = COLOR (colors, xx, yy);
4749 r += matrix[i] * t->red;
4750 g += matrix[i] * t->green;
4751 b += matrix[i] * t->blue;
4754 r = (r / sum + color_adjust) & 0xffff;
4755 g = (g / sum + color_adjust) & 0xffff;
4756 b = (b / sum + color_adjust) & 0xffff;
4757 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
4761 xfree (colors);
4762 x_from_xcolors (f, img, new);
4764 #undef COLOR
4768 /* Perform the pre-defined `emboss' edge-detection on image IMG
4769 on frame F. */
4771 static void
4772 x_emboss (struct frame *f, struct image *img)
4774 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
4778 /* Transform image IMG which is used on frame F with a Laplace
4779 edge-detection algorithm. The result is an image that can be used
4780 to draw disabled buttons, for example. */
4782 static void
4783 x_laplace (struct frame *f, struct image *img)
4785 x_detect_edges (f, img, laplace_matrix, 45000);
4789 /* Perform edge-detection on image IMG on frame F, with specified
4790 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4792 MATRIX must be either
4794 - a list of at least 9 numbers in row-major form
4795 - a vector of at least 9 numbers
4797 COLOR_ADJUST nil means use a default; otherwise it must be a
4798 number. */
4800 static void
4801 x_edge_detection (struct frame *f, struct image *img, Lisp_Object matrix,
4802 Lisp_Object color_adjust)
4804 int i = 0;
4805 int trans[9];
4807 if (CONSP (matrix))
4809 for (i = 0;
4810 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
4811 ++i, matrix = XCDR (matrix))
4812 trans[i] = XFLOATINT (XCAR (matrix));
4814 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
4816 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
4817 trans[i] = XFLOATINT (AREF (matrix, i));
4820 if (NILP (color_adjust))
4821 color_adjust = make_number (0xffff / 2);
4823 if (i == 9 && NUMBERP (color_adjust))
4824 x_detect_edges (f, img, trans, XFLOATINT (color_adjust));
4828 /* Transform image IMG on frame F so that it looks disabled. */
4830 static void
4831 x_disable_image (struct frame *f, struct image *img)
4833 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
4834 #ifdef HAVE_NTGUI
4835 int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits;
4836 #else
4837 int n_planes = dpyinfo->n_planes;
4838 #endif /* HAVE_NTGUI */
4840 if (n_planes >= 2)
4842 /* Color (or grayscale). Convert to gray, and equalize. Just
4843 drawing such images with a stipple can look very odd, so
4844 we're using this method instead. */
4845 XColor *colors = x_to_xcolors (f, img, 1);
4846 XColor *p, *end;
4847 const int h = 15000;
4848 const int l = 30000;
4850 for (p = colors, end = colors + img->width * img->height;
4851 p < end;
4852 ++p)
4854 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
4855 int i2 = (0xffff - h - l) * i / 0xffff + l;
4856 p->red = p->green = p->blue = i2;
4859 x_from_xcolors (f, img, colors);
4862 /* Draw a cross over the disabled image, if we must or if we
4863 should. */
4864 if (n_planes < 2 || cross_disabled_images)
4866 #ifndef HAVE_NTGUI
4867 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4869 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4871 Display *dpy = FRAME_X_DISPLAY (f);
4872 GC gc;
4874 image_sync_to_pixmaps (f, img);
4875 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
4876 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
4877 XDrawLine (dpy, img->pixmap, gc, 0, 0,
4878 img->width - 1, img->height - 1);
4879 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
4880 img->width - 1, 0);
4881 XFreeGC (dpy, gc);
4883 if (img->mask)
4885 gc = XCreateGC (dpy, img->mask, 0, NULL);
4886 XSetForeground (dpy, gc, MaskForeground (f));
4887 XDrawLine (dpy, img->mask, gc, 0, 0,
4888 img->width - 1, img->height - 1);
4889 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
4890 img->width - 1, 0);
4891 XFreeGC (dpy, gc);
4893 #endif /* !HAVE_NS */
4894 #else
4895 HDC hdc, bmpdc;
4896 HGDIOBJ prev;
4898 hdc = get_frame_dc (f);
4899 bmpdc = CreateCompatibleDC (hdc);
4900 release_frame_dc (f, hdc);
4902 prev = SelectObject (bmpdc, img->pixmap);
4904 SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f));
4905 MoveToEx (bmpdc, 0, 0, NULL);
4906 LineTo (bmpdc, img->width - 1, img->height - 1);
4907 MoveToEx (bmpdc, 0, img->height - 1, NULL);
4908 LineTo (bmpdc, img->width - 1, 0);
4910 if (img->mask)
4912 SelectObject (bmpdc, img->mask);
4913 SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f));
4914 MoveToEx (bmpdc, 0, 0, NULL);
4915 LineTo (bmpdc, img->width - 1, img->height - 1);
4916 MoveToEx (bmpdc, 0, img->height - 1, NULL);
4917 LineTo (bmpdc, img->width - 1, 0);
4919 SelectObject (bmpdc, prev);
4920 DeleteDC (bmpdc);
4921 #endif /* HAVE_NTGUI */
4926 /* Build a mask for image IMG which is used on frame F. FILE is the
4927 name of an image file, for error messages. HOW determines how to
4928 determine the background color of IMG. If it is a list '(R G B)',
4929 with R, G, and B being integers >= 0, take that as the color of the
4930 background. Otherwise, determine the background color of IMG
4931 heuristically. */
4933 static void
4934 x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how)
4936 XImagePtr_or_DC ximg;
4937 #ifndef HAVE_NTGUI
4938 XImagePtr mask_img;
4939 #else
4940 HGDIOBJ prev;
4941 char *mask_img;
4942 int row_width;
4943 #endif /* HAVE_NTGUI */
4944 int x, y;
4945 bool use_img_background;
4946 unsigned long bg = 0;
4948 if (img->mask)
4949 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
4951 #ifndef HAVE_NTGUI
4952 #ifndef HAVE_NS
4953 /* Create an image and pixmap serving as mask. */
4954 if (! image_create_x_image_and_pixmap (f, img, img->width, img->height, 1,
4955 &mask_img, 1))
4956 return;
4957 #endif /* !HAVE_NS */
4958 #else
4959 /* Create the bit array serving as mask. */
4960 row_width = (img->width + 7) / 8;
4961 mask_img = xzalloc (row_width * img->height);
4962 #endif /* HAVE_NTGUI */
4964 /* Get the X image or create a memory device context for IMG. */
4965 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
4967 /* Determine the background color of ximg. If HOW is `(R G B)'
4968 take that as color. Otherwise, use the image's background color. */
4969 use_img_background = 1;
4971 if (CONSP (how))
4973 int rgb[3], i;
4975 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
4977 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
4978 how = XCDR (how);
4981 if (i == 3 && NILP (how))
4983 char color_name[30];
4984 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
4985 bg = (
4986 #ifdef HAVE_NTGUI
4987 0x00ffffff & /* Filter out palette info. */
4988 #endif /* HAVE_NTGUI */
4989 x_alloc_image_color (f, img, build_string (color_name), 0));
4990 use_img_background = 0;
4994 if (use_img_background)
4995 bg = four_corners_best (ximg, img->corners, img->width, img->height);
4997 /* Set all bits in mask_img to 1 whose color in ximg is different
4998 from the background color bg. */
4999 #ifndef HAVE_NTGUI
5000 for (y = 0; y < img->height; ++y)
5001 for (x = 0; x < img->width; ++x)
5002 #ifndef HAVE_NS
5003 XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg
5004 ? PIX_MASK_DRAW : PIX_MASK_RETAIN));
5005 #else
5006 if (XGetPixel (ximg, x, y) == bg)
5007 ns_set_alpha (ximg, x, y, 0);
5008 #endif /* HAVE_NS */
5009 #ifndef HAVE_NS
5010 /* Fill in the background_transparent field while we have the mask handy. */
5011 image_background_transparent (img, f, mask_img);
5013 /* Put mask_img into the image. */
5014 image_put_x_image (f, img, mask_img, 1);
5015 #endif /* !HAVE_NS */
5016 #else
5017 for (y = 0; y < img->height; ++y)
5018 for (x = 0; x < img->width; ++x)
5020 COLORREF p = GetPixel (ximg, x, y);
5021 if (p != bg)
5022 mask_img[y * row_width + x / 8] |= 1 << (x % 8);
5025 /* Create the mask image. */
5026 img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height,
5027 mask_img);
5028 /* Fill in the background_transparent field while we have the mask handy. */
5029 SelectObject (ximg, img->mask);
5030 image_background_transparent (img, f, ximg);
5032 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5033 xfree (mask_img);
5034 #endif /* HAVE_NTGUI */
5036 image_unget_x_image_or_dc (img, 0, ximg, prev);
5040 /***********************************************************************
5041 PBM (mono, gray, color)
5042 ***********************************************************************/
5044 static bool pbm_image_p (Lisp_Object object);
5045 static bool pbm_load (struct frame *f, struct image *img);
5047 /* The symbol `pbm' identifying images of this type. */
5049 static Lisp_Object Qpbm;
5051 /* Indices of image specification fields in gs_format, below. */
5053 enum pbm_keyword_index
5055 PBM_TYPE,
5056 PBM_FILE,
5057 PBM_DATA,
5058 PBM_ASCENT,
5059 PBM_MARGIN,
5060 PBM_RELIEF,
5061 PBM_ALGORITHM,
5062 PBM_HEURISTIC_MASK,
5063 PBM_MASK,
5064 PBM_FOREGROUND,
5065 PBM_BACKGROUND,
5066 PBM_LAST
5069 /* Vector of image_keyword structures describing the format
5070 of valid user-defined image specifications. */
5072 static const struct image_keyword pbm_format[PBM_LAST] =
5074 {":type", IMAGE_SYMBOL_VALUE, 1},
5075 {":file", IMAGE_STRING_VALUE, 0},
5076 {":data", IMAGE_STRING_VALUE, 0},
5077 {":ascent", IMAGE_ASCENT_VALUE, 0},
5078 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
5079 {":relief", IMAGE_INTEGER_VALUE, 0},
5080 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5081 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5082 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5083 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
5084 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5087 /* Structure describing the image type `pbm'. */
5089 static struct image_type pbm_type =
5091 &Qpbm,
5092 pbm_image_p,
5093 pbm_load,
5094 x_clear_image,
5095 NULL,
5096 NULL
5100 /* Return true if OBJECT is a valid PBM image specification. */
5102 static bool
5103 pbm_image_p (Lisp_Object object)
5105 struct image_keyword fmt[PBM_LAST];
5107 memcpy (fmt, pbm_format, sizeof fmt);
5109 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
5110 return 0;
5112 /* Must specify either :data or :file. */
5113 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
5117 /* Get next char skipping comments in Netpbm header. Returns -1 at
5118 end of input. */
5120 static int
5121 pbm_next_char (unsigned char **s, unsigned char *end)
5123 int c = -1;
5125 while (*s < end && (c = *(*s)++, c == '#'))
5127 /* Skip to the next line break. */
5128 while (*s < end && (c = *(*s)++, c != '\n' && c != '\r'))
5131 c = -1;
5134 return c;
5138 /* Scan a decimal number from *S and return it. Advance *S while
5139 reading the number. END is the end of the string. Value is -1 at
5140 end of input. */
5142 static int
5143 pbm_scan_number (unsigned char **s, unsigned char *end)
5145 int c = 0, val = -1;
5147 /* Skip white-space. */
5148 while ((c = pbm_next_char (s, end)) != -1 && c_isspace (c))
5151 if (c_isdigit (c))
5153 /* Read decimal number. */
5154 val = c - '0';
5155 while ((c = pbm_next_char (s, end)) != -1 && c_isdigit (c))
5156 val = 10 * val + c - '0';
5159 return val;
5163 /* Load PBM image IMG for use on frame F. */
5165 static bool
5166 pbm_load (struct frame *f, struct image *img)
5168 bool raw_p;
5169 int x, y;
5170 int width, height, max_color_idx = 0;
5171 XImagePtr ximg;
5172 Lisp_Object file, specified_file;
5173 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
5174 unsigned char *contents = NULL;
5175 unsigned char *end, *p;
5176 ptrdiff_t size;
5178 specified_file = image_spec_value (img->spec, QCfile, NULL);
5180 if (STRINGP (specified_file))
5182 file = x_find_image_file (specified_file);
5183 if (!STRINGP (file))
5185 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5186 return 0;
5189 contents = slurp_file (SSDATA (file), &size);
5190 if (contents == NULL)
5192 image_error ("Error reading `%s'", file, Qnil);
5193 return 0;
5196 p = contents;
5197 end = contents + size;
5199 else
5201 Lisp_Object data;
5202 data = image_spec_value (img->spec, QCdata, NULL);
5203 if (!STRINGP (data))
5205 image_error ("Invalid image data `%s'", data, Qnil);
5206 return 0;
5208 p = SDATA (data);
5209 end = p + SBYTES (data);
5212 /* Check magic number. */
5213 if (end - p < 2 || *p++ != 'P')
5215 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5216 error:
5217 xfree (contents);
5218 return 0;
5221 switch (*p++)
5223 case '1':
5224 raw_p = 0, type = PBM_MONO;
5225 break;
5227 case '2':
5228 raw_p = 0, type = PBM_GRAY;
5229 break;
5231 case '3':
5232 raw_p = 0, type = PBM_COLOR;
5233 break;
5235 case '4':
5236 raw_p = 1, type = PBM_MONO;
5237 break;
5239 case '5':
5240 raw_p = 1, type = PBM_GRAY;
5241 break;
5243 case '6':
5244 raw_p = 1, type = PBM_COLOR;
5245 break;
5247 default:
5248 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5249 goto error;
5252 /* Read width, height, maximum color-component. Characters
5253 starting with `#' up to the end of a line are ignored. */
5254 width = pbm_scan_number (&p, end);
5255 height = pbm_scan_number (&p, end);
5257 if (type != PBM_MONO)
5259 max_color_idx = pbm_scan_number (&p, end);
5260 if (max_color_idx > 65535 || max_color_idx < 0)
5262 image_error ("Unsupported maximum PBM color value", Qnil, Qnil);
5263 goto error;
5267 if (!check_image_size (f, width, height))
5269 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
5270 goto error;
5273 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
5274 goto error;
5276 /* Initialize the color hash table. */
5277 init_color_table ();
5279 if (type == PBM_MONO)
5281 int c = 0, g;
5282 struct image_keyword fmt[PBM_LAST];
5283 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
5284 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
5286 /* Parse the image specification. */
5287 memcpy (fmt, pbm_format, sizeof fmt);
5288 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
5290 /* Get foreground and background colors, maybe allocate colors. */
5291 if (fmt[PBM_FOREGROUND].count
5292 && STRINGP (fmt[PBM_FOREGROUND].value))
5293 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
5294 if (fmt[PBM_BACKGROUND].count
5295 && STRINGP (fmt[PBM_BACKGROUND].value))
5297 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
5298 img->background = bg;
5299 img->background_valid = 1;
5302 for (y = 0; y < height; ++y)
5303 for (x = 0; x < width; ++x)
5305 if (raw_p)
5307 if ((x & 7) == 0)
5309 if (p >= end)
5311 x_destroy_x_image (ximg);
5312 x_clear_image (f, img);
5313 image_error ("Invalid image size in image `%s'",
5314 img->spec, Qnil);
5315 goto error;
5317 c = *p++;
5319 g = c & 0x80;
5320 c <<= 1;
5322 else
5323 g = pbm_scan_number (&p, end);
5325 XPutPixel (ximg, x, y, g ? fg : bg);
5328 else
5330 int expected_size = height * width;
5331 if (max_color_idx > 255)
5332 expected_size *= 2;
5333 if (type == PBM_COLOR)
5334 expected_size *= 3;
5336 if (raw_p && p + expected_size > end)
5338 x_destroy_x_image (ximg);
5339 x_clear_image (f, img);
5340 image_error ("Invalid image size in image `%s'",
5341 img->spec, Qnil);
5342 goto error;
5345 for (y = 0; y < height; ++y)
5346 for (x = 0; x < width; ++x)
5348 int r, g, b;
5350 if (type == PBM_GRAY && raw_p)
5352 r = g = b = *p++;
5353 if (max_color_idx > 255)
5354 r = g = b = r * 256 + *p++;
5356 else if (type == PBM_GRAY)
5357 r = g = b = pbm_scan_number (&p, end);
5358 else if (raw_p)
5360 r = *p++;
5361 if (max_color_idx > 255)
5362 r = r * 256 + *p++;
5363 g = *p++;
5364 if (max_color_idx > 255)
5365 g = g * 256 + *p++;
5366 b = *p++;
5367 if (max_color_idx > 255)
5368 b = b * 256 + *p++;
5370 else
5372 r = pbm_scan_number (&p, end);
5373 g = pbm_scan_number (&p, end);
5374 b = pbm_scan_number (&p, end);
5377 if (r < 0 || g < 0 || b < 0)
5379 x_destroy_x_image (ximg);
5380 image_error ("Invalid pixel value in image `%s'",
5381 img->spec, Qnil);
5382 goto error;
5385 /* RGB values are now in the range 0..max_color_idx.
5386 Scale this to the range 0..0xffff supported by X. */
5387 r = (double) r * 65535 / max_color_idx;
5388 g = (double) g * 65535 / max_color_idx;
5389 b = (double) b * 65535 / max_color_idx;
5390 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
5394 #ifdef COLOR_TABLE_SUPPORT
5395 /* Store in IMG->colors the colors allocated for the image, and
5396 free the color table. */
5397 img->colors = colors_in_color_table (&img->ncolors);
5398 free_color_table ();
5399 #endif /* COLOR_TABLE_SUPPORT */
5401 img->width = width;
5402 img->height = height;
5404 /* Maybe fill in the background field while we have ximg handy. */
5406 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
5407 /* Casting avoids a GCC warning. */
5408 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
5410 /* Put ximg into the image. */
5411 image_put_x_image (f, img, ximg, 0);
5413 /* X and W32 versions did it here, MAC version above. ++kfs
5414 img->width = width;
5415 img->height = height; */
5417 xfree (contents);
5418 return 1;
5422 /***********************************************************************
5424 ***********************************************************************/
5426 #if defined (HAVE_PNG) || defined (HAVE_NS)
5428 /* Function prototypes. */
5430 static bool png_image_p (Lisp_Object object);
5431 static bool png_load (struct frame *f, struct image *img);
5433 /* The symbol `png' identifying images of this type. */
5435 static Lisp_Object Qpng;
5437 /* Indices of image specification fields in png_format, below. */
5439 enum png_keyword_index
5441 PNG_TYPE,
5442 PNG_DATA,
5443 PNG_FILE,
5444 PNG_ASCENT,
5445 PNG_MARGIN,
5446 PNG_RELIEF,
5447 PNG_ALGORITHM,
5448 PNG_HEURISTIC_MASK,
5449 PNG_MASK,
5450 PNG_BACKGROUND,
5451 PNG_LAST
5454 /* Vector of image_keyword structures describing the format
5455 of valid user-defined image specifications. */
5457 static const struct image_keyword png_format[PNG_LAST] =
5459 {":type", IMAGE_SYMBOL_VALUE, 1},
5460 {":data", IMAGE_STRING_VALUE, 0},
5461 {":file", IMAGE_STRING_VALUE, 0},
5462 {":ascent", IMAGE_ASCENT_VALUE, 0},
5463 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
5464 {":relief", IMAGE_INTEGER_VALUE, 0},
5465 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5466 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5467 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5468 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5471 #if defined HAVE_NTGUI && defined WINDOWSNT
5472 static bool init_png_functions (void);
5473 #else
5474 #define init_png_functions NULL
5475 #endif
5477 /* Structure describing the image type `png'. */
5479 static struct image_type png_type =
5481 &Qpng,
5482 png_image_p,
5483 png_load,
5484 x_clear_image,
5485 init_png_functions,
5486 NULL
5489 /* Return true if OBJECT is a valid PNG image specification. */
5491 static bool
5492 png_image_p (Lisp_Object object)
5494 struct image_keyword fmt[PNG_LAST];
5495 memcpy (fmt, png_format, sizeof fmt);
5497 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
5498 return 0;
5500 /* Must specify either the :data or :file keyword. */
5501 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
5504 #endif /* HAVE_PNG || HAVE_NS */
5507 #ifdef HAVE_PNG
5509 #ifdef WINDOWSNT
5510 /* PNG library details. */
5512 DEF_IMGLIB_FN (png_voidp, png_get_io_ptr, (png_structp));
5513 DEF_IMGLIB_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t));
5514 DEF_IMGLIB_FN (png_structp, png_create_read_struct, (png_const_charp, png_voidp,
5515 png_error_ptr, png_error_ptr));
5516 DEF_IMGLIB_FN (png_infop, png_create_info_struct, (png_structp));
5517 DEF_IMGLIB_FN (void, png_destroy_read_struct, (png_structpp, png_infopp, png_infopp));
5518 DEF_IMGLIB_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr));
5519 DEF_IMGLIB_FN (void, png_set_sig_bytes, (png_structp, int));
5520 DEF_IMGLIB_FN (void, png_read_info, (png_structp, png_infop));
5521 DEF_IMGLIB_FN (png_uint_32, png_get_IHDR, (png_structp, png_infop,
5522 png_uint_32 *, png_uint_32 *,
5523 int *, int *, int *, int *, int *));
5524 DEF_IMGLIB_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32));
5525 DEF_IMGLIB_FN (void, png_set_strip_16, (png_structp));
5526 DEF_IMGLIB_FN (void, png_set_expand, (png_structp));
5527 DEF_IMGLIB_FN (void, png_set_gray_to_rgb, (png_structp));
5528 DEF_IMGLIB_FN (void, png_set_background, (png_structp, png_color_16p,
5529 int, int, double));
5530 DEF_IMGLIB_FN (png_uint_32, png_get_bKGD, (png_structp, png_infop, png_color_16p *));
5531 DEF_IMGLIB_FN (void, png_read_update_info, (png_structp, png_infop));
5532 DEF_IMGLIB_FN (png_byte, png_get_channels, (png_structp, png_infop));
5533 DEF_IMGLIB_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop));
5534 DEF_IMGLIB_FN (void, png_read_image, (png_structp, png_bytepp));
5535 DEF_IMGLIB_FN (void, png_read_end, (png_structp, png_infop));
5536 DEF_IMGLIB_FN (void, png_error, (png_structp, png_const_charp));
5538 #if (PNG_LIBPNG_VER >= 10500)
5539 DEF_IMGLIB_FN (void, png_longjmp, (png_structp, int)) PNG_NORETURN;
5540 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn, (png_structp, png_longjmp_ptr, size_t));
5541 #endif /* libpng version >= 1.5 */
5543 static bool
5544 init_png_functions (void)
5546 HMODULE library;
5548 if (!(library = w32_delayed_load (Qpng)))
5549 return 0;
5551 LOAD_IMGLIB_FN (library, png_get_io_ptr);
5552 LOAD_IMGLIB_FN (library, png_sig_cmp);
5553 LOAD_IMGLIB_FN (library, png_create_read_struct);
5554 LOAD_IMGLIB_FN (library, png_create_info_struct);
5555 LOAD_IMGLIB_FN (library, png_destroy_read_struct);
5556 LOAD_IMGLIB_FN (library, png_set_read_fn);
5557 LOAD_IMGLIB_FN (library, png_set_sig_bytes);
5558 LOAD_IMGLIB_FN (library, png_read_info);
5559 LOAD_IMGLIB_FN (library, png_get_IHDR);
5560 LOAD_IMGLIB_FN (library, png_get_valid);
5561 LOAD_IMGLIB_FN (library, png_set_strip_16);
5562 LOAD_IMGLIB_FN (library, png_set_expand);
5563 LOAD_IMGLIB_FN (library, png_set_gray_to_rgb);
5564 LOAD_IMGLIB_FN (library, png_set_background);
5565 LOAD_IMGLIB_FN (library, png_get_bKGD);
5566 LOAD_IMGLIB_FN (library, png_read_update_info);
5567 LOAD_IMGLIB_FN (library, png_get_channels);
5568 LOAD_IMGLIB_FN (library, png_get_rowbytes);
5569 LOAD_IMGLIB_FN (library, png_read_image);
5570 LOAD_IMGLIB_FN (library, png_read_end);
5571 LOAD_IMGLIB_FN (library, png_error);
5573 #if (PNG_LIBPNG_VER >= 10500)
5574 LOAD_IMGLIB_FN (library, png_longjmp);
5575 LOAD_IMGLIB_FN (library, png_set_longjmp_fn);
5576 #endif /* libpng version >= 1.5 */
5578 return 1;
5580 #else
5582 #define fn_png_get_io_ptr png_get_io_ptr
5583 #define fn_png_sig_cmp png_sig_cmp
5584 #define fn_png_create_read_struct png_create_read_struct
5585 #define fn_png_create_info_struct png_create_info_struct
5586 #define fn_png_destroy_read_struct png_destroy_read_struct
5587 #define fn_png_set_read_fn png_set_read_fn
5588 #define fn_png_set_sig_bytes png_set_sig_bytes
5589 #define fn_png_read_info png_read_info
5590 #define fn_png_get_IHDR png_get_IHDR
5591 #define fn_png_get_valid png_get_valid
5592 #define fn_png_set_strip_16 png_set_strip_16
5593 #define fn_png_set_expand png_set_expand
5594 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5595 #define fn_png_set_background png_set_background
5596 #define fn_png_get_bKGD png_get_bKGD
5597 #define fn_png_read_update_info png_read_update_info
5598 #define fn_png_get_channels png_get_channels
5599 #define fn_png_get_rowbytes png_get_rowbytes
5600 #define fn_png_read_image png_read_image
5601 #define fn_png_read_end png_read_end
5602 #define fn_png_error png_error
5604 #if (PNG_LIBPNG_VER >= 10500)
5605 #define fn_png_longjmp png_longjmp
5606 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5607 #endif /* libpng version >= 1.5 */
5609 #endif /* WINDOWSNT */
5611 /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp
5612 will do, POSIX _setjmp and _longjmp (if available) are often faster.
5613 Do not use sys_setjmp, as PNG supports only jmp_buf.
5614 It's OK if the longjmp substitute restores the signal mask. */
5615 #ifdef HAVE__SETJMP
5616 # define FAST_SETJMP(j) _setjmp (j)
5617 # define FAST_LONGJMP _longjmp
5618 #else
5619 # define FAST_SETJMP(j) setjmp (j)
5620 # define FAST_LONGJMP longjmp
5621 #endif
5623 #if PNG_LIBPNG_VER < 10500
5624 #define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1)
5625 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5626 #else
5627 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5628 #define PNG_LONGJMP(ptr) fn_png_longjmp (ptr, 1)
5629 #define PNG_JMPBUF(ptr) \
5630 (*fn_png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf)))
5631 #endif
5633 /* Error and warning handlers installed when the PNG library
5634 is initialized. */
5636 static _Noreturn void
5637 my_png_error (png_struct *png_ptr, const char *msg)
5639 eassert (png_ptr != NULL);
5640 /* Avoid compiler warning about deprecated direct access to
5641 png_ptr's fields in libpng versions 1.4.x. */
5642 image_error ("PNG error: %s", build_string (msg), Qnil);
5643 PNG_LONGJMP (png_ptr);
5647 static void
5648 my_png_warning (png_struct *png_ptr, const char *msg)
5650 eassert (png_ptr != NULL);
5651 image_error ("PNG warning: %s", build_string (msg), Qnil);
5654 /* Memory source for PNG decoding. */
5656 struct png_memory_storage
5658 unsigned char *bytes; /* The data */
5659 ptrdiff_t len; /* How big is it? */
5660 ptrdiff_t index; /* Where are we? */
5664 /* Function set as reader function when reading PNG image from memory.
5665 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5666 bytes from the input to DATA. */
5668 static void
5669 png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length)
5671 struct png_memory_storage *tbr = fn_png_get_io_ptr (png_ptr);
5673 if (length > tbr->len - tbr->index)
5674 fn_png_error (png_ptr, "Read error");
5676 memcpy (data, tbr->bytes + tbr->index, length);
5677 tbr->index = tbr->index + length;
5681 /* Function set as reader function when reading PNG image from a file.
5682 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5683 bytes from the input to DATA. */
5685 static void
5686 png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length)
5688 FILE *fp = fn_png_get_io_ptr (png_ptr);
5690 if (fread (data, 1, length, fp) < length)
5691 fn_png_error (png_ptr, "Read error");
5695 /* Load PNG image IMG for use on frame F. Value is true if
5696 successful. */
5698 struct png_load_context
5700 /* These are members so that longjmp doesn't munge local variables. */
5701 png_struct *png_ptr;
5702 png_info *info_ptr;
5703 png_info *end_info;
5704 FILE *fp;
5705 png_byte *pixels;
5706 png_byte **rows;
5709 static bool
5710 png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
5712 Lisp_Object file, specified_file;
5713 Lisp_Object specified_data;
5714 int x, y;
5715 ptrdiff_t i;
5716 XImagePtr ximg, mask_img = NULL;
5717 png_struct *png_ptr;
5718 png_info *info_ptr = NULL, *end_info = NULL;
5719 FILE *fp = NULL;
5720 png_byte sig[8];
5721 png_byte *pixels = NULL;
5722 png_byte **rows = NULL;
5723 png_uint_32 width, height;
5724 int bit_depth, color_type, interlace_type;
5725 png_byte channels;
5726 png_uint_32 row_bytes;
5727 bool transparent_p;
5728 struct png_memory_storage tbr; /* Data to be read */
5730 /* Find out what file to load. */
5731 specified_file = image_spec_value (img->spec, QCfile, NULL);
5732 specified_data = image_spec_value (img->spec, QCdata, NULL);
5734 if (NILP (specified_data))
5736 file = x_find_image_file (specified_file);
5737 if (!STRINGP (file))
5739 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5740 return 0;
5743 /* Open the image file. */
5744 fp = emacs_fopen (SSDATA (file), "rb");
5745 if (!fp)
5747 image_error ("Cannot open image file `%s'", file, Qnil);
5748 return 0;
5751 /* Check PNG signature. */
5752 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
5753 || fn_png_sig_cmp (sig, 0, sizeof sig))
5755 fclose (fp);
5756 image_error ("Not a PNG file: `%s'", file, Qnil);
5757 return 0;
5760 else
5762 if (!STRINGP (specified_data))
5764 image_error ("Invalid image data `%s'", specified_data, Qnil);
5765 return 0;
5768 /* Read from memory. */
5769 tbr.bytes = SDATA (specified_data);
5770 tbr.len = SBYTES (specified_data);
5771 tbr.index = 0;
5773 /* Check PNG signature. */
5774 if (tbr.len < sizeof sig
5775 || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig))
5777 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
5778 return 0;
5781 /* Need to skip past the signature. */
5782 tbr.bytes += sizeof (sig);
5785 /* Initialize read and info structs for PNG lib. */
5786 png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING,
5787 NULL, my_png_error,
5788 my_png_warning);
5789 if (png_ptr)
5791 info_ptr = fn_png_create_info_struct (png_ptr);
5792 end_info = fn_png_create_info_struct (png_ptr);
5795 c->png_ptr = png_ptr;
5796 c->info_ptr = info_ptr;
5797 c->end_info = end_info;
5798 c->fp = fp;
5799 c->pixels = pixels;
5800 c->rows = rows;
5802 if (! (info_ptr && end_info))
5804 fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
5805 png_ptr = 0;
5807 if (! png_ptr)
5809 if (fp) fclose (fp);
5810 return 0;
5813 /* Set error jump-back. We come back here when the PNG library
5814 detects an error. */
5815 if (FAST_SETJMP (PNG_JMPBUF (png_ptr)))
5817 error:
5818 if (c->png_ptr)
5819 fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
5820 xfree (c->pixels);
5821 xfree (c->rows);
5822 if (c->fp)
5823 fclose (c->fp);
5824 return 0;
5827 /* Silence a bogus diagnostic; see GCC bug 54561. */
5828 IF_LINT (fp = c->fp);
5830 /* Read image info. */
5831 if (!NILP (specified_data))
5832 fn_png_set_read_fn (png_ptr, &tbr, png_read_from_memory);
5833 else
5834 fn_png_set_read_fn (png_ptr, fp, png_read_from_file);
5836 fn_png_set_sig_bytes (png_ptr, sizeof sig);
5837 fn_png_read_info (png_ptr, info_ptr);
5838 fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
5839 &interlace_type, NULL, NULL);
5841 if (! (width <= INT_MAX && height <= INT_MAX
5842 && check_image_size (f, width, height)))
5844 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
5845 goto error;
5848 /* Create the X image and pixmap now, so that the work below can be
5849 omitted if the image is too large for X. */
5850 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
5851 goto error;
5853 /* If image contains simply transparency data, we prefer to
5854 construct a clipping mask. */
5855 if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
5856 transparent_p = 1;
5857 else
5858 transparent_p = 0;
5860 /* This function is easier to write if we only have to handle
5861 one data format: RGB or RGBA with 8 bits per channel. Let's
5862 transform other formats into that format. */
5864 /* Strip more than 8 bits per channel. */
5865 if (bit_depth == 16)
5866 fn_png_set_strip_16 (png_ptr);
5868 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5869 if available. */
5870 fn_png_set_expand (png_ptr);
5872 /* Convert grayscale images to RGB. */
5873 if (color_type == PNG_COLOR_TYPE_GRAY
5874 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
5875 fn_png_set_gray_to_rgb (png_ptr);
5877 /* Handle alpha channel by combining the image with a background
5878 color. Do this only if a real alpha channel is supplied. For
5879 simple transparency, we prefer a clipping mask. */
5880 if (!transparent_p)
5882 /* png_color_16 *image_bg; */
5883 Lisp_Object specified_bg
5884 = image_spec_value (img->spec, QCbackground, NULL);
5885 int shift = (bit_depth == 16) ? 0 : 8;
5887 if (STRINGP (specified_bg))
5888 /* The user specified `:background', use that. */
5890 XColor color;
5891 if (x_defined_color (f, SSDATA (specified_bg), &color, 0))
5893 png_color_16 user_bg;
5895 memset (&user_bg, 0, sizeof user_bg);
5896 user_bg.red = color.red >> shift;
5897 user_bg.green = color.green >> shift;
5898 user_bg.blue = color.blue >> shift;
5900 fn_png_set_background (png_ptr, &user_bg,
5901 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
5904 else
5906 /* We use the current frame background, ignoring any default
5907 background color set by the image. */
5908 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5909 XColor color;
5910 png_color_16 frame_background;
5912 color.pixel = FRAME_BACKGROUND_PIXEL (f);
5913 x_query_color (f, &color);
5915 memset (&frame_background, 0, sizeof frame_background);
5916 frame_background.red = color.red >> shift;
5917 frame_background.green = color.green >> shift;
5918 frame_background.blue = color.blue >> shift;
5919 #endif /* HAVE_X_WINDOWS */
5921 fn_png_set_background (png_ptr, &frame_background,
5922 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
5926 /* Update info structure. */
5927 fn_png_read_update_info (png_ptr, info_ptr);
5929 /* Get number of channels. Valid values are 1 for grayscale images
5930 and images with a palette, 2 for grayscale images with transparency
5931 information (alpha channel), 3 for RGB images, and 4 for RGB
5932 images with alpha channel, i.e. RGBA. If conversions above were
5933 sufficient we should only have 3 or 4 channels here. */
5934 channels = fn_png_get_channels (png_ptr, info_ptr);
5935 eassert (channels == 3 || channels == 4);
5937 /* Number of bytes needed for one row of the image. */
5938 row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr);
5940 /* Allocate memory for the image. */
5941 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows < height
5942 || min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height < row_bytes)
5943 memory_full (SIZE_MAX);
5944 c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height);
5945 c->rows = rows = xmalloc (height * sizeof *rows);
5946 for (i = 0; i < height; ++i)
5947 rows[i] = pixels + i * row_bytes;
5949 /* Read the entire image. */
5950 fn_png_read_image (png_ptr, rows);
5951 fn_png_read_end (png_ptr, info_ptr);
5952 if (fp)
5954 fclose (fp);
5955 c->fp = NULL;
5958 /* Create an image and pixmap serving as mask if the PNG image
5959 contains an alpha channel. */
5960 if (channels == 4
5961 && !transparent_p
5962 && !image_create_x_image_and_pixmap (f, img, width, height, 1,
5963 &mask_img, 1))
5965 x_destroy_x_image (ximg);
5966 x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP);
5967 goto error;
5970 /* Fill the X image and mask from PNG data. */
5971 init_color_table ();
5973 for (y = 0; y < height; ++y)
5975 png_byte *p = rows[y];
5977 for (x = 0; x < width; ++x)
5979 int r, g, b;
5981 r = *p++ << 8;
5982 g = *p++ << 8;
5983 b = *p++ << 8;
5984 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
5985 /* An alpha channel, aka mask channel, associates variable
5986 transparency with an image. Where other image formats
5987 support binary transparency---fully transparent or fully
5988 opaque---PNG allows up to 254 levels of partial transparency.
5989 The PNG library implements partial transparency by combining
5990 the image with a specified background color.
5992 I'm not sure how to handle this here nicely: because the
5993 background on which the image is displayed may change, for
5994 real alpha channel support, it would be necessary to create
5995 a new image for each possible background.
5997 What I'm doing now is that a mask is created if we have
5998 boolean transparency information. Otherwise I'm using
5999 the frame's background color to combine the image with. */
6001 if (channels == 4)
6003 if (mask_img)
6004 XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN);
6005 ++p;
6010 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6011 /* Set IMG's background color from the PNG image, unless the user
6012 overrode it. */
6014 png_color_16 *bg;
6015 if (fn_png_get_bKGD (png_ptr, info_ptr, &bg))
6017 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
6018 img->background_valid = 1;
6022 #ifdef COLOR_TABLE_SUPPORT
6023 /* Remember colors allocated for this image. */
6024 img->colors = colors_in_color_table (&img->ncolors);
6025 free_color_table ();
6026 #endif /* COLOR_TABLE_SUPPORT */
6028 /* Clean up. */
6029 fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
6030 xfree (rows);
6031 xfree (pixels);
6033 img->width = width;
6034 img->height = height;
6036 /* Maybe fill in the background field while we have ximg handy.
6037 Casting avoids a GCC warning. */
6038 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6040 /* Put ximg into the image. */
6041 image_put_x_image (f, img, ximg, 0);
6043 /* Same for the mask. */
6044 if (mask_img)
6046 /* Fill in the background_transparent field while we have the
6047 mask handy. Casting avoids a GCC warning. */
6048 image_background_transparent (img, f, (XImagePtr_or_DC)mask_img);
6050 image_put_x_image (f, img, mask_img, 1);
6053 return 1;
6056 static bool
6057 png_load (struct frame *f, struct image *img)
6059 struct png_load_context c;
6060 return png_load_body (f, img, &c);
6063 #else /* HAVE_PNG */
6065 #ifdef HAVE_NS
6066 static bool
6067 png_load (struct frame *f, struct image *img)
6069 return ns_load_image (f, img,
6070 image_spec_value (img->spec, QCfile, NULL),
6071 image_spec_value (img->spec, QCdata, NULL));
6073 #endif /* HAVE_NS */
6076 #endif /* !HAVE_PNG */
6080 /***********************************************************************
6081 JPEG
6082 ***********************************************************************/
6084 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6086 static bool jpeg_image_p (Lisp_Object object);
6087 static bool jpeg_load (struct frame *f, struct image *img);
6089 /* The symbol `jpeg' identifying images of this type. */
6091 static Lisp_Object Qjpeg;
6093 /* Indices of image specification fields in gs_format, below. */
6095 enum jpeg_keyword_index
6097 JPEG_TYPE,
6098 JPEG_DATA,
6099 JPEG_FILE,
6100 JPEG_ASCENT,
6101 JPEG_MARGIN,
6102 JPEG_RELIEF,
6103 JPEG_ALGORITHM,
6104 JPEG_HEURISTIC_MASK,
6105 JPEG_MASK,
6106 JPEG_BACKGROUND,
6107 JPEG_LAST
6110 /* Vector of image_keyword structures describing the format
6111 of valid user-defined image specifications. */
6113 static const struct image_keyword jpeg_format[JPEG_LAST] =
6115 {":type", IMAGE_SYMBOL_VALUE, 1},
6116 {":data", IMAGE_STRING_VALUE, 0},
6117 {":file", IMAGE_STRING_VALUE, 0},
6118 {":ascent", IMAGE_ASCENT_VALUE, 0},
6119 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
6120 {":relief", IMAGE_INTEGER_VALUE, 0},
6121 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6122 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6123 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6124 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6127 #if defined HAVE_NTGUI && defined WINDOWSNT
6128 static bool init_jpeg_functions (void);
6129 #else
6130 #define init_jpeg_functions NULL
6131 #endif
6133 /* Structure describing the image type `jpeg'. */
6135 static struct image_type jpeg_type =
6137 &Qjpeg,
6138 jpeg_image_p,
6139 jpeg_load,
6140 x_clear_image,
6141 init_jpeg_functions,
6142 NULL
6145 /* Return true if OBJECT is a valid JPEG image specification. */
6147 static bool
6148 jpeg_image_p (Lisp_Object object)
6150 struct image_keyword fmt[JPEG_LAST];
6152 memcpy (fmt, jpeg_format, sizeof fmt);
6154 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
6155 return 0;
6157 /* Must specify either the :data or :file keyword. */
6158 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
6161 #endif /* HAVE_JPEG || HAVE_NS */
6163 #ifdef HAVE_JPEG
6165 /* Work around a warning about HAVE_STDLIB_H being redefined in
6166 jconfig.h. */
6167 #ifdef HAVE_STDLIB_H
6168 #undef HAVE_STDLIB_H
6169 #endif /* HAVE_STLIB_H */
6171 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6172 /* In older releases of the jpeg library, jpeglib.h will define boolean
6173 differently depending on __WIN32__, so make sure it is defined. */
6174 #define __WIN32__ 1
6175 #endif
6177 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6178 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6179 using the Windows boolean type instead of the jpeglib boolean type
6180 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6181 headers are included along with windows.h, so under Cygwin, jpeglib
6182 attempts to define a conflicting boolean type. Worse, forcing
6183 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6184 because it created an ABI incompatibility between the
6185 already-compiled jpeg library and the header interface definition.
6187 The best we can do is to define jpeglib's boolean type to a
6188 different name. This name, jpeg_boolean, remains in effect through
6189 the rest of image.c.
6191 #if defined CYGWIN && defined HAVE_NTGUI
6192 #define boolean jpeg_boolean
6193 #endif
6194 #include <jpeglib.h>
6195 #include <jerror.h>
6197 #ifdef WINDOWSNT
6199 /* JPEG library details. */
6200 DEF_IMGLIB_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t));
6201 DEF_IMGLIB_FN (boolean, jpeg_start_decompress, (j_decompress_ptr));
6202 DEF_IMGLIB_FN (boolean, jpeg_finish_decompress, (j_decompress_ptr));
6203 DEF_IMGLIB_FN (void, jpeg_destroy_decompress, (j_decompress_ptr));
6204 DEF_IMGLIB_FN (int, jpeg_read_header, (j_decompress_ptr, boolean));
6205 DEF_IMGLIB_FN (JDIMENSION, jpeg_read_scanlines, (j_decompress_ptr, JSAMPARRAY, JDIMENSION));
6206 DEF_IMGLIB_FN (struct jpeg_error_mgr *, jpeg_std_error, (struct jpeg_error_mgr *));
6207 DEF_IMGLIB_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int));
6209 static bool
6210 init_jpeg_functions (void)
6212 HMODULE library;
6214 if (!(library = w32_delayed_load (Qjpeg)))
6215 return 0;
6217 LOAD_IMGLIB_FN (library, jpeg_finish_decompress);
6218 LOAD_IMGLIB_FN (library, jpeg_read_scanlines);
6219 LOAD_IMGLIB_FN (library, jpeg_start_decompress);
6220 LOAD_IMGLIB_FN (library, jpeg_read_header);
6221 LOAD_IMGLIB_FN (library, jpeg_CreateDecompress);
6222 LOAD_IMGLIB_FN (library, jpeg_destroy_decompress);
6223 LOAD_IMGLIB_FN (library, jpeg_std_error);
6224 LOAD_IMGLIB_FN (library, jpeg_resync_to_restart);
6225 return 1;
6228 /* Wrapper since we can't directly assign the function pointer
6229 to another function pointer that was declared more completely easily. */
6230 static boolean
6231 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired)
6233 return fn_jpeg_resync_to_restart (cinfo, desired);
6236 #else
6238 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6239 #define fn_jpeg_start_decompress jpeg_start_decompress
6240 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6241 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6242 #define fn_jpeg_read_header jpeg_read_header
6243 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6244 #define fn_jpeg_std_error jpeg_std_error
6245 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6247 #endif /* WINDOWSNT */
6249 struct my_jpeg_error_mgr
6251 struct jpeg_error_mgr pub;
6252 sys_jmp_buf setjmp_buffer;
6254 /* The remaining members are so that longjmp doesn't munge local
6255 variables. */
6256 struct jpeg_decompress_struct cinfo;
6257 enum
6259 MY_JPEG_ERROR_EXIT,
6260 MY_JPEG_INVALID_IMAGE_SIZE,
6261 MY_JPEG_CANNOT_CREATE_X
6262 } failure_code;
6263 #ifdef lint
6264 FILE *fp;
6265 #endif
6269 static _Noreturn void
6270 my_error_exit (j_common_ptr cinfo)
6272 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
6273 mgr->failure_code = MY_JPEG_ERROR_EXIT;
6274 sys_longjmp (mgr->setjmp_buffer, 1);
6278 /* Init source method for JPEG data source manager. Called by
6279 jpeg_read_header() before any data is actually read. See
6280 libjpeg.doc from the JPEG lib distribution. */
6282 static void
6283 our_common_init_source (j_decompress_ptr cinfo)
6288 /* Method to terminate data source. Called by
6289 jpeg_finish_decompress() after all data has been processed. */
6291 static void
6292 our_common_term_source (j_decompress_ptr cinfo)
6297 /* Fill input buffer method for JPEG data source manager. Called
6298 whenever more data is needed. We read the whole image in one step,
6299 so this only adds a fake end of input marker at the end. */
6301 static JOCTET our_memory_buffer[2];
6303 static boolean
6304 our_memory_fill_input_buffer (j_decompress_ptr cinfo)
6306 /* Insert a fake EOI marker. */
6307 struct jpeg_source_mgr *src = cinfo->src;
6309 our_memory_buffer[0] = (JOCTET) 0xFF;
6310 our_memory_buffer[1] = (JOCTET) JPEG_EOI;
6312 src->next_input_byte = our_memory_buffer;
6313 src->bytes_in_buffer = 2;
6314 return 1;
6318 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6319 is the JPEG data source manager. */
6321 static void
6322 our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes)
6324 struct jpeg_source_mgr *src = cinfo->src;
6326 if (src)
6328 if (num_bytes > src->bytes_in_buffer)
6329 ERREXIT (cinfo, JERR_INPUT_EOF);
6331 src->bytes_in_buffer -= num_bytes;
6332 src->next_input_byte += num_bytes;
6337 /* Set up the JPEG lib for reading an image from DATA which contains
6338 LEN bytes. CINFO is the decompression info structure created for
6339 reading the image. */
6341 static void
6342 jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len)
6344 struct jpeg_source_mgr *src = cinfo->src;
6346 if (! src)
6348 /* First time for this JPEG object? */
6349 src = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6350 JPOOL_PERMANENT, sizeof *src);
6351 cinfo->src = src;
6352 src->next_input_byte = data;
6355 src->init_source = our_common_init_source;
6356 src->fill_input_buffer = our_memory_fill_input_buffer;
6357 src->skip_input_data = our_memory_skip_input_data;
6358 src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */
6359 src->term_source = our_common_term_source;
6360 src->bytes_in_buffer = len;
6361 src->next_input_byte = data;
6365 struct jpeg_stdio_mgr
6367 struct jpeg_source_mgr mgr;
6368 boolean finished;
6369 FILE *file;
6370 JOCTET *buffer;
6374 /* Size of buffer to read JPEG from file.
6375 Not too big, as we want to use alloc_small. */
6376 #define JPEG_STDIO_BUFFER_SIZE 8192
6379 /* Fill input buffer method for JPEG data source manager. Called
6380 whenever more data is needed. The data is read from a FILE *. */
6382 static boolean
6383 our_stdio_fill_input_buffer (j_decompress_ptr cinfo)
6385 struct jpeg_stdio_mgr *src;
6387 src = (struct jpeg_stdio_mgr *) cinfo->src;
6388 if (!src->finished)
6390 ptrdiff_t bytes;
6392 bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file);
6393 if (bytes > 0)
6394 src->mgr.bytes_in_buffer = bytes;
6395 else
6397 WARNMS (cinfo, JWRN_JPEG_EOF);
6398 src->finished = 1;
6399 src->buffer[0] = (JOCTET) 0xFF;
6400 src->buffer[1] = (JOCTET) JPEG_EOI;
6401 src->mgr.bytes_in_buffer = 2;
6403 src->mgr.next_input_byte = src->buffer;
6406 return 1;
6410 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6411 is the JPEG data source manager. */
6413 static void
6414 our_stdio_skip_input_data (j_decompress_ptr cinfo, long int num_bytes)
6416 struct jpeg_stdio_mgr *src;
6417 src = (struct jpeg_stdio_mgr *) cinfo->src;
6419 while (num_bytes > 0 && !src->finished)
6421 if (num_bytes <= src->mgr.bytes_in_buffer)
6423 src->mgr.bytes_in_buffer -= num_bytes;
6424 src->mgr.next_input_byte += num_bytes;
6425 break;
6427 else
6429 num_bytes -= src->mgr.bytes_in_buffer;
6430 src->mgr.bytes_in_buffer = 0;
6431 src->mgr.next_input_byte = NULL;
6433 our_stdio_fill_input_buffer (cinfo);
6439 /* Set up the JPEG lib for reading an image from a FILE *.
6440 CINFO is the decompression info structure created for
6441 reading the image. */
6443 static void
6444 jpeg_file_src (j_decompress_ptr cinfo, FILE *fp)
6446 struct jpeg_stdio_mgr *src = (struct jpeg_stdio_mgr *) cinfo->src;
6448 if (! src)
6450 /* First time for this JPEG object? */
6451 src = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6452 JPOOL_PERMANENT, sizeof *src);
6453 cinfo->src = (struct jpeg_source_mgr *) src;
6454 src->buffer = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6455 JPOOL_PERMANENT,
6456 JPEG_STDIO_BUFFER_SIZE);
6459 src->file = fp;
6460 src->finished = 0;
6461 src->mgr.init_source = our_common_init_source;
6462 src->mgr.fill_input_buffer = our_stdio_fill_input_buffer;
6463 src->mgr.skip_input_data = our_stdio_skip_input_data;
6464 src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */
6465 src->mgr.term_source = our_common_term_source;
6466 src->mgr.bytes_in_buffer = 0;
6467 src->mgr.next_input_byte = NULL;
6471 /* Load image IMG for use on frame F. Patterned after example.c
6472 from the JPEG lib. */
6474 static bool
6475 jpeg_load_body (struct frame *f, struct image *img,
6476 struct my_jpeg_error_mgr *mgr)
6478 Lisp_Object file, specified_file;
6479 Lisp_Object specified_data;
6480 FILE *fp = NULL;
6481 JSAMPARRAY buffer;
6482 int row_stride, x, y;
6483 XImagePtr ximg = NULL;
6484 unsigned long *colors;
6485 int width, height;
6487 /* Open the JPEG file. */
6488 specified_file = image_spec_value (img->spec, QCfile, NULL);
6489 specified_data = image_spec_value (img->spec, QCdata, NULL);
6491 if (NILP (specified_data))
6493 file = x_find_image_file (specified_file);
6494 if (!STRINGP (file))
6496 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6497 return 0;
6500 fp = emacs_fopen (SSDATA (file), "rb");
6501 if (fp == NULL)
6503 image_error ("Cannot open `%s'", file, Qnil);
6504 return 0;
6507 else if (!STRINGP (specified_data))
6509 image_error ("Invalid image data `%s'", specified_data, Qnil);
6510 return 0;
6513 IF_LINT (mgr->fp = fp);
6515 /* Customize libjpeg's error handling to call my_error_exit when an
6516 error is detected. This function will perform a longjmp. */
6517 mgr->cinfo.err = fn_jpeg_std_error (&mgr->pub);
6518 mgr->pub.error_exit = my_error_exit;
6519 if (sys_setjmp (mgr->setjmp_buffer))
6521 switch (mgr->failure_code)
6523 case MY_JPEG_ERROR_EXIT:
6525 char buf[JMSG_LENGTH_MAX];
6526 mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf);
6527 image_error ("Error reading JPEG image `%s': %s", img->spec,
6528 build_string (buf));
6529 break;
6532 case MY_JPEG_INVALID_IMAGE_SIZE:
6533 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
6534 break;
6536 case MY_JPEG_CANNOT_CREATE_X:
6537 break;
6540 /* Close the input file and destroy the JPEG object. */
6541 if (fp)
6542 fclose (fp);
6543 fn_jpeg_destroy_decompress (&mgr->cinfo);
6545 /* If we already have an XImage, free that. */
6546 x_destroy_x_image (ximg);
6548 /* Free pixmap and colors. */
6549 x_clear_image (f, img);
6550 return 0;
6553 /* Silence a bogus diagnostic; see GCC bug 54561. */
6554 IF_LINT (fp = mgr->fp);
6556 /* Create the JPEG decompression object. Let it read from fp.
6557 Read the JPEG image header. */
6558 fn_jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo);
6560 if (NILP (specified_data))
6561 jpeg_file_src (&mgr->cinfo, fp);
6562 else
6563 jpeg_memory_src (&mgr->cinfo, SDATA (specified_data),
6564 SBYTES (specified_data));
6566 fn_jpeg_read_header (&mgr->cinfo, 1);
6568 /* Customize decompression so that color quantization will be used.
6569 Start decompression. */
6570 mgr->cinfo.quantize_colors = 1;
6571 fn_jpeg_start_decompress (&mgr->cinfo);
6572 width = img->width = mgr->cinfo.output_width;
6573 height = img->height = mgr->cinfo.output_height;
6575 if (!check_image_size (f, width, height))
6577 mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE;
6578 sys_longjmp (mgr->setjmp_buffer, 1);
6581 /* Create X image and pixmap. */
6582 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
6584 mgr->failure_code = MY_JPEG_CANNOT_CREATE_X;
6585 sys_longjmp (mgr->setjmp_buffer, 1);
6588 /* Allocate colors. When color quantization is used,
6589 mgr->cinfo.actual_number_of_colors has been set with the number of
6590 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6591 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6592 No more than 255 colors will be generated. */
6594 int i, ir, ig, ib;
6596 if (mgr->cinfo.out_color_components > 2)
6597 ir = 0, ig = 1, ib = 2;
6598 else if (mgr->cinfo.out_color_components > 1)
6599 ir = 0, ig = 1, ib = 0;
6600 else
6601 ir = 0, ig = 0, ib = 0;
6603 /* Use the color table mechanism because it handles colors that
6604 cannot be allocated nicely. Such colors will be replaced with
6605 a default color, and we don't have to care about which colors
6606 can be freed safely, and which can't. */
6607 init_color_table ();
6608 colors = alloca (mgr->cinfo.actual_number_of_colors * sizeof *colors);
6610 for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i)
6612 /* Multiply RGB values with 255 because X expects RGB values
6613 in the range 0..0xffff. */
6614 int r = mgr->cinfo.colormap[ir][i] << 8;
6615 int g = mgr->cinfo.colormap[ig][i] << 8;
6616 int b = mgr->cinfo.colormap[ib][i] << 8;
6617 colors[i] = lookup_rgb_color (f, r, g, b);
6620 #ifdef COLOR_TABLE_SUPPORT
6621 /* Remember those colors actually allocated. */
6622 img->colors = colors_in_color_table (&img->ncolors);
6623 free_color_table ();
6624 #endif /* COLOR_TABLE_SUPPORT */
6627 /* Read pixels. */
6628 row_stride = width * mgr->cinfo.output_components;
6629 buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo,
6630 JPOOL_IMAGE, row_stride, 1);
6631 for (y = 0; y < height; ++y)
6633 fn_jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
6634 for (x = 0; x < mgr->cinfo.output_width; ++x)
6635 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
6638 /* Clean up. */
6639 fn_jpeg_finish_decompress (&mgr->cinfo);
6640 fn_jpeg_destroy_decompress (&mgr->cinfo);
6641 if (fp)
6642 fclose (fp);
6644 /* Maybe fill in the background field while we have ximg handy. */
6645 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6646 /* Casting avoids a GCC warning. */
6647 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6649 /* Put ximg into the image. */
6650 image_put_x_image (f, img, ximg, 0);
6651 return 1;
6654 static bool
6655 jpeg_load (struct frame *f, struct image *img)
6657 struct my_jpeg_error_mgr mgr;
6658 return jpeg_load_body (f, img, &mgr);
6661 #else /* HAVE_JPEG */
6663 #ifdef HAVE_NS
6664 static bool
6665 jpeg_load (struct frame *f, struct image *img)
6667 return ns_load_image (f, img,
6668 image_spec_value (img->spec, QCfile, NULL),
6669 image_spec_value (img->spec, QCdata, NULL));
6671 #endif /* HAVE_NS */
6673 #endif /* !HAVE_JPEG */
6677 /***********************************************************************
6678 TIFF
6679 ***********************************************************************/
6681 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6683 static bool tiff_image_p (Lisp_Object object);
6684 static bool tiff_load (struct frame *f, struct image *img);
6686 /* The symbol `tiff' identifying images of this type. */
6688 static Lisp_Object Qtiff;
6690 /* Indices of image specification fields in tiff_format, below. */
6692 enum tiff_keyword_index
6694 TIFF_TYPE,
6695 TIFF_DATA,
6696 TIFF_FILE,
6697 TIFF_ASCENT,
6698 TIFF_MARGIN,
6699 TIFF_RELIEF,
6700 TIFF_ALGORITHM,
6701 TIFF_HEURISTIC_MASK,
6702 TIFF_MASK,
6703 TIFF_BACKGROUND,
6704 TIFF_INDEX,
6705 TIFF_LAST
6708 /* Vector of image_keyword structures describing the format
6709 of valid user-defined image specifications. */
6711 static const struct image_keyword tiff_format[TIFF_LAST] =
6713 {":type", IMAGE_SYMBOL_VALUE, 1},
6714 {":data", IMAGE_STRING_VALUE, 0},
6715 {":file", IMAGE_STRING_VALUE, 0},
6716 {":ascent", IMAGE_ASCENT_VALUE, 0},
6717 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
6718 {":relief", IMAGE_INTEGER_VALUE, 0},
6719 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6720 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6721 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6722 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
6723 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
6726 #if defined HAVE_NTGUI && defined WINDOWSNT
6727 static bool init_tiff_functions (void);
6728 #else
6729 #define init_tiff_functions NULL
6730 #endif
6732 /* Structure describing the image type `tiff'. */
6734 static struct image_type tiff_type =
6736 &Qtiff,
6737 tiff_image_p,
6738 tiff_load,
6739 x_clear_image,
6740 init_tiff_functions,
6741 NULL
6744 /* Return true if OBJECT is a valid TIFF image specification. */
6746 static bool
6747 tiff_image_p (Lisp_Object object)
6749 struct image_keyword fmt[TIFF_LAST];
6750 memcpy (fmt, tiff_format, sizeof fmt);
6752 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
6753 return 0;
6755 /* Must specify either the :data or :file keyword. */
6756 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
6759 #endif /* HAVE_TIFF || HAVE_NS */
6761 #ifdef HAVE_TIFF
6763 #include <tiffio.h>
6765 #ifdef WINDOWSNT
6767 /* TIFF library details. */
6768 DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler));
6769 DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetWarningHandler, (TIFFErrorHandler));
6770 DEF_IMGLIB_FN (TIFF *, TIFFOpen, (const char *, const char *));
6771 DEF_IMGLIB_FN (TIFF *, TIFFClientOpen, (const char *, const char *, thandle_t,
6772 TIFFReadWriteProc, TIFFReadWriteProc,
6773 TIFFSeekProc, TIFFCloseProc, TIFFSizeProc,
6774 TIFFMapFileProc, TIFFUnmapFileProc));
6775 DEF_IMGLIB_FN (int, TIFFGetField, (TIFF *, ttag_t, ...));
6776 DEF_IMGLIB_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int));
6777 DEF_IMGLIB_FN (void, TIFFClose, (TIFF *));
6778 DEF_IMGLIB_FN (int, TIFFSetDirectory, (TIFF *, tdir_t));
6780 static bool
6781 init_tiff_functions (void)
6783 HMODULE library;
6785 if (!(library = w32_delayed_load (Qtiff)))
6786 return 0;
6788 LOAD_IMGLIB_FN (library, TIFFSetErrorHandler);
6789 LOAD_IMGLIB_FN (library, TIFFSetWarningHandler);
6790 LOAD_IMGLIB_FN (library, TIFFOpen);
6791 LOAD_IMGLIB_FN (library, TIFFClientOpen);
6792 LOAD_IMGLIB_FN (library, TIFFGetField);
6793 LOAD_IMGLIB_FN (library, TIFFReadRGBAImage);
6794 LOAD_IMGLIB_FN (library, TIFFClose);
6795 LOAD_IMGLIB_FN (library, TIFFSetDirectory);
6796 return 1;
6799 #else
6801 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6802 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6803 #define fn_TIFFOpen TIFFOpen
6804 #define fn_TIFFClientOpen TIFFClientOpen
6805 #define fn_TIFFGetField TIFFGetField
6806 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6807 #define fn_TIFFClose TIFFClose
6808 #define fn_TIFFSetDirectory TIFFSetDirectory
6809 #endif /* WINDOWSNT */
6812 /* Reading from a memory buffer for TIFF images Based on the PNG
6813 memory source, but we have to provide a lot of extra functions.
6814 Blah.
6816 We really only need to implement read and seek, but I am not
6817 convinced that the TIFF library is smart enough not to destroy
6818 itself if we only hand it the function pointers we need to
6819 override. */
6821 typedef struct
6823 unsigned char *bytes;
6824 ptrdiff_t len;
6825 ptrdiff_t index;
6827 tiff_memory_source;
6829 static tsize_t
6830 tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size)
6832 tiff_memory_source *src = (tiff_memory_source *) data;
6834 size = min (size, src->len - src->index);
6835 memcpy (buf, src->bytes + src->index, size);
6836 src->index += size;
6837 return size;
6840 static tsize_t
6841 tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size)
6843 return -1;
6846 static toff_t
6847 tiff_seek_in_memory (thandle_t data, toff_t off, int whence)
6849 tiff_memory_source *src = (tiff_memory_source *) data;
6850 ptrdiff_t idx;
6852 switch (whence)
6854 case SEEK_SET: /* Go from beginning of source. */
6855 idx = off;
6856 break;
6858 case SEEK_END: /* Go from end of source. */
6859 idx = src->len + off;
6860 break;
6862 case SEEK_CUR: /* Go from current position. */
6863 idx = src->index + off;
6864 break;
6866 default: /* Invalid `whence'. */
6867 return -1;
6870 if (idx > src->len || idx < 0)
6871 return -1;
6873 src->index = idx;
6874 return src->index;
6877 static int
6878 tiff_close_memory (thandle_t data)
6880 /* NOOP */
6881 return 0;
6884 static int
6885 tiff_mmap_memory (thandle_t data, tdata_t *pbase, toff_t *psize)
6887 /* It is already _IN_ memory. */
6888 return 0;
6891 static void
6892 tiff_unmap_memory (thandle_t data, tdata_t base, toff_t size)
6894 /* We don't need to do this. */
6897 static toff_t
6898 tiff_size_of_memory (thandle_t data)
6900 return ((tiff_memory_source *) data)->len;
6903 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6904 compiler error compiling tiff_handler, see Bugzilla bug #17406
6905 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6906 this function as external works around that problem. */
6907 #if defined (__MINGW32__) && __GNUC__ == 3
6908 # define MINGW_STATIC
6909 #else
6910 # define MINGW_STATIC static
6911 #endif
6913 MINGW_STATIC void
6914 tiff_handler (const char *, const char *, const char *, va_list)
6915 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6916 MINGW_STATIC void
6917 tiff_handler (const char *log_format, const char *title,
6918 const char *format, va_list ap)
6920 /* doprnt is not suitable here, as TIFF handlers are called from
6921 libtiff and are passed arbitrary printf directives. Instead, use
6922 vsnprintf, taking care to be portable to nonstandard environments
6923 where vsnprintf returns -1 on buffer overflow. Since it's just a
6924 log entry, it's OK to truncate it. */
6925 char buf[4000];
6926 int len = vsnprintf (buf, sizeof buf, format, ap);
6927 add_to_log (log_format, build_string (title),
6928 make_string (buf, max (0, min (len, sizeof buf - 1))));
6930 #undef MINGW_STATIC
6932 static void tiff_error_handler (const char *, const char *, va_list)
6933 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6934 static void
6935 tiff_error_handler (const char *title, const char *format, va_list ap)
6937 tiff_handler ("TIFF error: %s %s", title, format, ap);
6941 static void tiff_warning_handler (const char *, const char *, va_list)
6942 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6943 static void
6944 tiff_warning_handler (const char *title, const char *format, va_list ap)
6946 tiff_handler ("TIFF warning: %s %s", title, format, ap);
6950 /* Load TIFF image IMG for use on frame F. Value is true if
6951 successful. */
6953 static bool
6954 tiff_load (struct frame *f, struct image *img)
6956 Lisp_Object file, specified_file;
6957 Lisp_Object specified_data;
6958 TIFF *tiff;
6959 int width, height, x, y, count;
6960 uint32 *buf;
6961 int rc;
6962 XImagePtr ximg;
6963 tiff_memory_source memsrc;
6964 Lisp_Object image;
6966 specified_file = image_spec_value (img->spec, QCfile, NULL);
6967 specified_data = image_spec_value (img->spec, QCdata, NULL);
6969 fn_TIFFSetErrorHandler ((TIFFErrorHandler) tiff_error_handler);
6970 fn_TIFFSetWarningHandler ((TIFFErrorHandler) tiff_warning_handler);
6972 if (NILP (specified_data))
6974 /* Read from a file */
6975 file = x_find_image_file (specified_file);
6976 if (!STRINGP (file))
6978 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6979 return 0;
6981 #ifdef WINDOWSNT
6982 file = ansi_encode_filename (file);
6983 #endif
6985 /* Try to open the image file. */
6986 tiff = fn_TIFFOpen (SSDATA (file), "r");
6987 if (tiff == NULL)
6989 image_error ("Cannot open `%s'", file, Qnil);
6990 return 0;
6993 else
6995 if (!STRINGP (specified_data))
6997 image_error ("Invalid image data `%s'", specified_data, Qnil);
6998 return 0;
7001 /* Memory source! */
7002 memsrc.bytes = SDATA (specified_data);
7003 memsrc.len = SBYTES (specified_data);
7004 memsrc.index = 0;
7006 tiff = fn_TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc,
7007 tiff_read_from_memory,
7008 tiff_write_from_memory,
7009 tiff_seek_in_memory,
7010 tiff_close_memory,
7011 tiff_size_of_memory,
7012 tiff_mmap_memory,
7013 tiff_unmap_memory);
7015 if (!tiff)
7017 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
7018 return 0;
7022 image = image_spec_value (img->spec, QCindex, NULL);
7023 if (INTEGERP (image))
7025 EMACS_INT ino = XFASTINT (image);
7026 if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
7027 && fn_TIFFSetDirectory (tiff, ino)))
7029 image_error ("Invalid image number `%s' in image `%s'",
7030 image, img->spec);
7031 fn_TIFFClose (tiff);
7032 return 0;
7036 /* Get width and height of the image, and allocate a raster buffer
7037 of width x height 32-bit values. */
7038 fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
7039 fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
7041 if (!check_image_size (f, width, height))
7043 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7044 fn_TIFFClose (tiff);
7045 return 0;
7048 /* Create the X image and pixmap. */
7049 if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width
7050 && image_create_x_image_and_pixmap (f, img, width, height, 0,
7051 &ximg, 0)))
7053 fn_TIFFClose (tiff);
7054 return 0;
7057 buf = xmalloc (sizeof *buf * width * height);
7059 rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0);
7061 /* Count the number of images in the file. */
7062 for (count = 1; fn_TIFFSetDirectory (tiff, count); count++)
7063 continue;
7065 if (count > 1)
7066 img->lisp_data = Fcons (Qcount,
7067 Fcons (make_number (count),
7068 img->lisp_data));
7070 fn_TIFFClose (tiff);
7071 if (!rc)
7073 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
7074 xfree (buf);
7075 return 0;
7078 /* Initialize the color table. */
7079 init_color_table ();
7081 /* Process the pixel raster. Origin is in the lower-left corner. */
7082 for (y = 0; y < height; ++y)
7084 uint32 *row = buf + y * width;
7086 for (x = 0; x < width; ++x)
7088 uint32 abgr = row[x];
7089 int r = TIFFGetR (abgr) << 8;
7090 int g = TIFFGetG (abgr) << 8;
7091 int b = TIFFGetB (abgr) << 8;
7092 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
7096 #ifdef COLOR_TABLE_SUPPORT
7097 /* Remember the colors allocated for the image. Free the color table. */
7098 img->colors = colors_in_color_table (&img->ncolors);
7099 free_color_table ();
7100 #endif /* COLOR_TABLE_SUPPORT */
7102 img->width = width;
7103 img->height = height;
7105 /* Maybe fill in the background field while we have ximg handy. */
7106 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7107 /* Casting avoids a GCC warning on W32. */
7108 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7110 /* Put ximg into the image. */
7111 image_put_x_image (f, img, ximg, 0);
7112 xfree (buf);
7114 return 1;
7117 #else /* HAVE_TIFF */
7119 #ifdef HAVE_NS
7120 static bool
7121 tiff_load (struct frame *f, struct image *img)
7123 return ns_load_image (f, img,
7124 image_spec_value (img->spec, QCfile, NULL),
7125 image_spec_value (img->spec, QCdata, NULL));
7127 #endif /* HAVE_NS */
7129 #endif /* !HAVE_TIFF */
7133 /***********************************************************************
7135 ***********************************************************************/
7137 #if defined (HAVE_GIF) || defined (HAVE_NS)
7139 static bool gif_image_p (Lisp_Object object);
7140 static bool gif_load (struct frame *f, struct image *img);
7141 static void gif_clear_image (struct frame *f, struct image *img);
7143 /* The symbol `gif' identifying images of this type. */
7145 static Lisp_Object Qgif;
7147 /* Indices of image specification fields in gif_format, below. */
7149 enum gif_keyword_index
7151 GIF_TYPE,
7152 GIF_DATA,
7153 GIF_FILE,
7154 GIF_ASCENT,
7155 GIF_MARGIN,
7156 GIF_RELIEF,
7157 GIF_ALGORITHM,
7158 GIF_HEURISTIC_MASK,
7159 GIF_MASK,
7160 GIF_IMAGE,
7161 GIF_BACKGROUND,
7162 GIF_LAST
7165 /* Vector of image_keyword structures describing the format
7166 of valid user-defined image specifications. */
7168 static const struct image_keyword gif_format[GIF_LAST] =
7170 {":type", IMAGE_SYMBOL_VALUE, 1},
7171 {":data", IMAGE_STRING_VALUE, 0},
7172 {":file", IMAGE_STRING_VALUE, 0},
7173 {":ascent", IMAGE_ASCENT_VALUE, 0},
7174 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
7175 {":relief", IMAGE_INTEGER_VALUE, 0},
7176 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7177 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7178 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7179 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
7180 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7183 #if defined HAVE_NTGUI && defined WINDOWSNT
7184 static bool init_gif_functions (void);
7185 #else
7186 #define init_gif_functions NULL
7187 #endif
7189 /* Structure describing the image type `gif'. */
7191 static struct image_type gif_type =
7193 &Qgif,
7194 gif_image_p,
7195 gif_load,
7196 gif_clear_image,
7197 init_gif_functions,
7198 NULL
7201 /* Free X resources of GIF image IMG which is used on frame F. */
7203 static void
7204 gif_clear_image (struct frame *f, struct image *img)
7206 img->lisp_data = Qnil;
7207 x_clear_image (f, img);
7210 /* Return true if OBJECT is a valid GIF image specification. */
7212 static bool
7213 gif_image_p (Lisp_Object object)
7215 struct image_keyword fmt[GIF_LAST];
7216 memcpy (fmt, gif_format, sizeof fmt);
7218 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
7219 return 0;
7221 /* Must specify either the :data or :file keyword. */
7222 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
7225 #endif /* HAVE_GIF */
7227 #ifdef HAVE_GIF
7229 #if defined (HAVE_NTGUI)
7231 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7232 Undefine before redefining to avoid a preprocessor warning. */
7233 #ifdef DrawText
7234 #undef DrawText
7235 #endif
7236 /* avoid conflict with QuickdrawText.h */
7237 #define DrawText gif_DrawText
7238 #include <gif_lib.h>
7239 #undef DrawText
7241 /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */
7242 #ifndef GIFLIB_MINOR
7243 #define GIFLIB_MINOR 0
7244 #endif
7245 #ifndef GIFLIB_RELEASE
7246 #define GIFLIB_RELEASE 0
7247 #endif
7249 #else /* HAVE_NTGUI */
7251 #include <gif_lib.h>
7253 #endif /* HAVE_NTGUI */
7255 /* Giflib before 5.0 didn't define these macros. */
7256 #ifndef GIFLIB_MAJOR
7257 #define GIFLIB_MAJOR 4
7258 #endif
7260 #ifdef WINDOWSNT
7262 /* GIF library details. */
7263 DEF_IMGLIB_FN (int, DGifCloseFile, (GifFileType *));
7264 DEF_IMGLIB_FN (int, DGifSlurp, (GifFileType *));
7265 #if GIFLIB_MAJOR < 5
7266 DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc));
7267 DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *));
7268 #else
7269 DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *));
7270 DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *, int *));
7271 DEF_IMGLIB_FN (char *, GifErrorString, (int));
7272 #endif
7274 static bool
7275 init_gif_functions (void)
7277 HMODULE library;
7279 if (!(library = w32_delayed_load (Qgif)))
7280 return 0;
7282 LOAD_IMGLIB_FN (library, DGifCloseFile);
7283 LOAD_IMGLIB_FN (library, DGifSlurp);
7284 LOAD_IMGLIB_FN (library, DGifOpen);
7285 LOAD_IMGLIB_FN (library, DGifOpenFileName);
7286 #if GIFLIB_MAJOR >= 5
7287 LOAD_IMGLIB_FN (library, GifErrorString);
7288 #endif
7289 return 1;
7292 #else
7294 #define fn_DGifCloseFile DGifCloseFile
7295 #define fn_DGifSlurp DGifSlurp
7296 #define fn_DGifOpen DGifOpen
7297 #define fn_DGifOpenFileName DGifOpenFileName
7298 #if 5 <= GIFLIB_MAJOR
7299 # define fn_GifErrorString GifErrorString
7300 #endif
7302 #endif /* WINDOWSNT */
7304 /* Reading a GIF image from memory
7305 Based on the PNG memory stuff to a certain extent. */
7307 typedef struct
7309 unsigned char *bytes;
7310 ptrdiff_t len;
7311 ptrdiff_t index;
7313 gif_memory_source;
7315 /* Make the current memory source available to gif_read_from_memory.
7316 It's done this way because not all versions of libungif support
7317 a UserData field in the GifFileType structure. */
7318 static gif_memory_source *current_gif_memory_src;
7320 static int
7321 gif_read_from_memory (GifFileType *file, GifByteType *buf, int len)
7323 gif_memory_source *src = current_gif_memory_src;
7325 if (len > src->len - src->index)
7326 return -1;
7328 memcpy (buf, src->bytes + src->index, len);
7329 src->index += len;
7330 return len;
7334 /* Load GIF image IMG for use on frame F. Value is true if
7335 successful. */
7337 static const int interlace_start[] = {0, 4, 2, 1};
7338 static const int interlace_increment[] = {8, 8, 4, 2};
7340 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7342 static bool
7343 gif_load (struct frame *f, struct image *img)
7345 Lisp_Object file;
7346 int rc, width, height, x, y, i, j;
7347 XImagePtr ximg;
7348 ColorMapObject *gif_color_map;
7349 unsigned long pixel_colors[256];
7350 GifFileType *gif;
7351 gif_memory_source memsrc;
7352 Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
7353 Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
7354 Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
7355 unsigned long bgcolor = 0;
7356 EMACS_INT idx;
7357 #if GIFLIB_MAJOR >= 5
7358 int gif_err;
7359 #endif
7361 if (NILP (specified_data))
7363 file = x_find_image_file (specified_file);
7364 if (!STRINGP (file))
7366 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7367 return 0;
7369 #ifdef WINDOWSNT
7370 file = ansi_encode_filename (file);
7371 #endif
7373 /* Open the GIF file. */
7374 #if GIFLIB_MAJOR < 5
7375 gif = fn_DGifOpenFileName (SSDATA (file));
7376 if (gif == NULL)
7378 image_error ("Cannot open `%s'", file, Qnil);
7379 return 0;
7381 #else
7382 gif = fn_DGifOpenFileName (SSDATA (file), &gif_err);
7383 if (gif == NULL)
7385 image_error ("Cannot open `%s': %s",
7386 file, build_string (fn_GifErrorString (gif_err)));
7387 return 0;
7389 #endif
7391 else
7393 if (!STRINGP (specified_data))
7395 image_error ("Invalid image data `%s'", specified_data, Qnil);
7396 return 0;
7399 /* Read from memory! */
7400 current_gif_memory_src = &memsrc;
7401 memsrc.bytes = SDATA (specified_data);
7402 memsrc.len = SBYTES (specified_data);
7403 memsrc.index = 0;
7405 #if GIFLIB_MAJOR < 5
7406 gif = fn_DGifOpen (&memsrc, gif_read_from_memory);
7407 if (!gif)
7409 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
7410 return 0;
7412 #else
7413 gif = fn_DGifOpen (&memsrc, gif_read_from_memory, &gif_err);
7414 if (!gif)
7416 image_error ("Cannot open memory source `%s': %s",
7417 img->spec, build_string (fn_GifErrorString (gif_err)));
7418 return 0;
7420 #endif
7423 /* Before reading entire contents, check the declared image size. */
7424 if (!check_image_size (f, gif->SWidth, gif->SHeight))
7426 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7427 fn_DGifCloseFile (gif);
7428 return 0;
7431 /* Read entire contents. */
7432 rc = fn_DGifSlurp (gif);
7433 if (rc == GIF_ERROR || gif->ImageCount <= 0)
7435 image_error ("Error reading `%s'", img->spec, Qnil);
7436 fn_DGifCloseFile (gif);
7437 return 0;
7440 /* Which sub-image are we to display? */
7442 Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL);
7443 idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0;
7444 if (idx < 0 || idx >= gif->ImageCount)
7446 image_error ("Invalid image number `%s' in image `%s'",
7447 image_number, img->spec);
7448 fn_DGifCloseFile (gif);
7449 return 0;
7453 width = img->width = gif->SWidth;
7454 height = img->height = gif->SHeight;
7456 img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top;
7457 img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left;
7458 img->corners[BOT_CORNER]
7459 = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height;
7460 img->corners[RIGHT_CORNER]
7461 = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width;
7463 if (!check_image_size (f, width, height))
7465 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7466 fn_DGifCloseFile (gif);
7467 return 0;
7470 /* Check that the selected subimages fit. It's not clear whether
7471 the GIF spec requires this, but Emacs can crash if they don't fit. */
7472 for (j = 0; j <= idx; ++j)
7474 struct SavedImage *subimage = gif->SavedImages + j;
7475 int subimg_width = subimage->ImageDesc.Width;
7476 int subimg_height = subimage->ImageDesc.Height;
7477 int subimg_top = subimage->ImageDesc.Top;
7478 int subimg_left = subimage->ImageDesc.Left;
7479 if (! (0 <= subimg_width && 0 <= subimg_height
7480 && 0 <= subimg_top && subimg_top <= height - subimg_height
7481 && 0 <= subimg_left && subimg_left <= width - subimg_width))
7483 image_error ("Subimage does not fit in image", Qnil, Qnil);
7484 fn_DGifCloseFile (gif);
7485 return 0;
7489 /* Create the X image and pixmap. */
7490 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
7492 fn_DGifCloseFile (gif);
7493 return 0;
7496 /* Clear the part of the screen image not covered by the image.
7497 Full animated GIF support requires more here (see the gif89 spec,
7498 disposal methods). Let's simply assume that the part not covered
7499 by a sub-image is in the frame's background color. */
7500 for (y = 0; y < img->corners[TOP_CORNER]; ++y)
7501 for (x = 0; x < width; ++x)
7502 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7504 for (y = img->corners[BOT_CORNER]; y < height; ++y)
7505 for (x = 0; x < width; ++x)
7506 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7508 for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y)
7510 for (x = 0; x < img->corners[LEFT_CORNER]; ++x)
7511 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7512 for (x = img->corners[RIGHT_CORNER]; x < width; ++x)
7513 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7516 /* Read the GIF image into the X image. */
7518 /* FIXME: With the current implementation, loading an animated gif
7519 is quadratic in the number of animation frames, since each frame
7520 is a separate struct image. We must provide a way for a single
7521 gif_load call to construct and save all animation frames. */
7523 init_color_table ();
7524 if (STRINGP (specified_bg))
7525 bgcolor = x_alloc_image_color (f, img, specified_bg,
7526 FRAME_BACKGROUND_PIXEL (f));
7527 for (j = 0; j <= idx; ++j)
7529 /* We use a local variable `raster' here because RasterBits is a
7530 char *, which invites problems with bytes >= 0x80. */
7531 struct SavedImage *subimage = gif->SavedImages + j;
7532 unsigned char *raster = (unsigned char *) subimage->RasterBits;
7533 int transparency_color_index = -1;
7534 int disposal = 0;
7535 int subimg_width = subimage->ImageDesc.Width;
7536 int subimg_height = subimage->ImageDesc.Height;
7537 int subimg_top = subimage->ImageDesc.Top;
7538 int subimg_left = subimage->ImageDesc.Left;
7540 /* Find the Graphic Control Extension block for this sub-image.
7541 Extract the disposal method and transparency color. */
7542 for (i = 0; i < subimage->ExtensionBlockCount; i++)
7544 ExtensionBlock *extblock = subimage->ExtensionBlocks + i;
7546 if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION)
7547 && extblock->ByteCount == 4
7548 && extblock->Bytes[0] & 1)
7550 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7551 to background". Treat any other value like 2. */
7552 disposal = (extblock->Bytes[0] >> 2) & 7;
7553 transparency_color_index = (unsigned char) extblock->Bytes[3];
7554 break;
7558 /* We can't "keep in place" the first subimage. */
7559 if (j == 0)
7560 disposal = 2;
7562 /* For disposal == 0, the spec says "No disposal specified. The
7563 decoder is not required to take any action." In practice, it
7564 seems we need to treat this like "keep in place", see e.g.
7565 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7566 if (disposal == 0)
7567 disposal = 1;
7569 /* Allocate subimage colors. */
7570 memset (pixel_colors, 0, sizeof pixel_colors);
7571 gif_color_map = subimage->ImageDesc.ColorMap;
7572 if (!gif_color_map)
7573 gif_color_map = gif->SColorMap;
7575 if (gif_color_map)
7576 for (i = 0; i < gif_color_map->ColorCount; ++i)
7578 if (transparency_color_index == i)
7579 pixel_colors[i] = STRINGP (specified_bg)
7580 ? bgcolor : FRAME_BACKGROUND_PIXEL (f);
7581 else
7583 int r = gif_color_map->Colors[i].Red << 8;
7584 int g = gif_color_map->Colors[i].Green << 8;
7585 int b = gif_color_map->Colors[i].Blue << 8;
7586 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
7590 /* Apply the pixel values. */
7591 if (GIFLIB_MAJOR < 5 && gif->SavedImages[j].ImageDesc.Interlace)
7593 int row, pass;
7595 for (y = 0, row = interlace_start[0], pass = 0;
7596 y < subimg_height;
7597 y++, row += interlace_increment[pass])
7599 while (subimg_height <= row)
7600 row = interlace_start[++pass];
7602 for (x = 0; x < subimg_width; x++)
7604 int c = raster[y * subimg_width + x];
7605 if (transparency_color_index != c || disposal != 1)
7606 XPutPixel (ximg, x + subimg_left, row + subimg_top,
7607 pixel_colors[c]);
7611 else
7613 for (y = 0; y < subimg_height; ++y)
7614 for (x = 0; x < subimg_width; ++x)
7616 int c = raster[y * subimg_width + x];
7617 if (transparency_color_index != c || disposal != 1)
7618 XPutPixel (ximg, x + subimg_left, y + subimg_top,
7619 pixel_colors[c]);
7624 #ifdef COLOR_TABLE_SUPPORT
7625 img->colors = colors_in_color_table (&img->ncolors);
7626 free_color_table ();
7627 #endif /* COLOR_TABLE_SUPPORT */
7629 /* Save GIF image extension data for `image-metadata'.
7630 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7631 img->lisp_data = Qnil;
7632 if (gif->SavedImages[idx].ExtensionBlockCount > 0)
7634 int delay = 0;
7635 ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks;
7636 for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++)
7637 /* Append (... FUNCTION "BYTES") */
7639 img->lisp_data
7640 = Fcons (make_number (ext->Function),
7641 Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
7642 img->lisp_data));
7643 if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION
7644 && ext->ByteCount == 4)
7646 delay = ext->Bytes[2] << CHAR_BIT;
7647 delay |= ext->Bytes[1];
7650 img->lisp_data = list2 (Qextension_data, img->lisp_data);
7651 if (delay)
7652 img->lisp_data
7653 = Fcons (Qdelay,
7654 Fcons (make_float (delay / 100.0),
7655 img->lisp_data));
7658 if (gif->ImageCount > 1)
7659 img->lisp_data = Fcons (Qcount,
7660 Fcons (make_number (gif->ImageCount),
7661 img->lisp_data));
7663 fn_DGifCloseFile (gif);
7665 /* Maybe fill in the background field while we have ximg handy. */
7666 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7667 /* Casting avoids a GCC warning. */
7668 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7670 /* Put ximg into the image. */
7671 image_put_x_image (f, img, ximg, 0);
7673 return 1;
7676 #else /* !HAVE_GIF */
7678 #ifdef HAVE_NS
7679 static bool
7680 gif_load (struct frame *f, struct image *img)
7682 return ns_load_image (f, img,
7683 image_spec_value (img->spec, QCfile, NULL),
7684 image_spec_value (img->spec, QCdata, NULL));
7686 #endif /* HAVE_NS */
7688 #endif /* HAVE_GIF */
7691 #ifdef HAVE_IMAGEMAGICK
7693 /***********************************************************************
7694 ImageMagick
7695 ***********************************************************************/
7697 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
7698 safely rounded and clipped to int range. */
7700 static int
7701 scale_image_size (int size, size_t divisor, size_t multiplier)
7703 if (divisor != 0)
7705 double s = size;
7706 double scaled = s * multiplier / divisor + 0.5;
7707 if (scaled < INT_MAX)
7708 return scaled;
7710 return INT_MAX;
7713 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
7714 Use SPEC to deduce the size. Store the desired size into
7715 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
7716 static void
7717 compute_image_size (size_t width, size_t height,
7718 Lisp_Object spec,
7719 int *d_width, int *d_height)
7721 Lisp_Object value;
7722 int desired_width, desired_height;
7724 /* If width and/or height is set in the display spec assume we want
7725 to scale to those values. If either h or w is unspecified, the
7726 unspecified should be calculated from the specified to preserve
7727 aspect ratio. */
7728 value = image_spec_value (spec, QCwidth, NULL);
7729 desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
7730 value = image_spec_value (spec, QCheight, NULL);
7731 desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
7733 if (desired_width == -1)
7735 value = image_spec_value (spec, QCmax_width, NULL);
7736 if (NATNUMP (value))
7738 int max_width = min (XFASTINT (value), INT_MAX);
7739 if (max_width < width)
7741 /* The image is wider than :max-width. */
7742 desired_width = max_width;
7743 if (desired_height == -1)
7745 desired_height = scale_image_size (desired_width,
7746 width, height);
7747 value = image_spec_value (spec, QCmax_height, NULL);
7748 if (NATNUMP (value))
7750 int max_height = min (XFASTINT (value), INT_MAX);
7751 if (max_height < desired_height)
7753 desired_height = max_height;
7754 desired_width = scale_image_size (desired_height,
7755 height, width);
7763 if (desired_height == -1)
7765 value = image_spec_value (spec, QCmax_height, NULL);
7766 if (NATNUMP (value))
7768 int max_height = min (XFASTINT (value), INT_MAX);
7769 if (max_height < height)
7770 desired_height = max_height;
7774 if (desired_width != -1 && desired_height == -1)
7775 /* w known, calculate h. */
7776 desired_height = scale_image_size (desired_width, width, height);
7778 if (desired_width == -1 && desired_height != -1)
7779 /* h known, calculate w. */
7780 desired_width = scale_image_size (desired_height, height, width);
7782 *d_width = desired_width;
7783 *d_height = desired_height;
7786 static Lisp_Object Qimagemagick;
7788 static bool imagemagick_image_p (Lisp_Object);
7789 static bool imagemagick_load (struct frame *, struct image *);
7790 static void imagemagick_clear_image (struct frame *, struct image *);
7792 /* Indices of image specification fields in imagemagick_format. */
7794 enum imagemagick_keyword_index
7796 IMAGEMAGICK_TYPE,
7797 IMAGEMAGICK_DATA,
7798 IMAGEMAGICK_FILE,
7799 IMAGEMAGICK_ASCENT,
7800 IMAGEMAGICK_MARGIN,
7801 IMAGEMAGICK_RELIEF,
7802 IMAGEMAGICK_ALGORITHM,
7803 IMAGEMAGICK_HEURISTIC_MASK,
7804 IMAGEMAGICK_MASK,
7805 IMAGEMAGICK_BACKGROUND,
7806 IMAGEMAGICK_HEIGHT,
7807 IMAGEMAGICK_WIDTH,
7808 IMAGEMAGICK_MAX_HEIGHT,
7809 IMAGEMAGICK_MAX_WIDTH,
7810 IMAGEMAGICK_FORMAT,
7811 IMAGEMAGICK_ROTATION,
7812 IMAGEMAGICK_CROP,
7813 IMAGEMAGICK_LAST
7816 /* Vector of image_keyword structures describing the format
7817 of valid user-defined image specifications. */
7819 static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] =
7821 {":type", IMAGE_SYMBOL_VALUE, 1},
7822 {":data", IMAGE_STRING_VALUE, 0},
7823 {":file", IMAGE_STRING_VALUE, 0},
7824 {":ascent", IMAGE_ASCENT_VALUE, 0},
7825 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
7826 {":relief", IMAGE_INTEGER_VALUE, 0},
7827 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7828 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7829 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7830 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
7831 {":height", IMAGE_INTEGER_VALUE, 0},
7832 {":width", IMAGE_INTEGER_VALUE, 0},
7833 {":max-height", IMAGE_INTEGER_VALUE, 0},
7834 {":max-width", IMAGE_INTEGER_VALUE, 0},
7835 {":format", IMAGE_SYMBOL_VALUE, 0},
7836 {":rotation", IMAGE_NUMBER_VALUE, 0},
7837 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
7840 #if defined HAVE_NTGUI && defined WINDOWSNT
7841 static bool init_imagemagick_functions (void);
7842 #else
7843 #define init_imagemagick_functions NULL
7844 #endif
7846 /* Structure describing the image type for any image handled via
7847 ImageMagick. */
7849 static struct image_type imagemagick_type =
7851 &Qimagemagick,
7852 imagemagick_image_p,
7853 imagemagick_load,
7854 imagemagick_clear_image,
7855 init_imagemagick_functions,
7856 NULL
7859 /* Free X resources of imagemagick image IMG which is used on frame F. */
7861 static void
7862 imagemagick_clear_image (struct frame *f,
7863 struct image *img)
7865 x_clear_image (f, img);
7868 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7869 this by calling parse_image_spec and supplying the keywords that
7870 identify the IMAGEMAGICK format. */
7872 static bool
7873 imagemagick_image_p (Lisp_Object object)
7875 struct image_keyword fmt[IMAGEMAGICK_LAST];
7876 memcpy (fmt, imagemagick_format, sizeof fmt);
7878 if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick))
7879 return 0;
7881 /* Must specify either the :data or :file keyword. */
7882 return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1;
7885 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7886 Therefore rename the function so it doesn't collide with ImageMagick. */
7887 #define DrawRectangle DrawRectangleGif
7888 #include <wand/MagickWand.h>
7890 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7891 Emacs seems to work fine with the hidden version, so unhide it. */
7892 #include <magick/version.h>
7893 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7894 extern WandExport void PixelGetMagickColor (const PixelWand *,
7895 MagickPixelPacket *);
7896 #endif
7898 /* Log ImageMagick error message.
7899 Useful when a ImageMagick function returns the status `MagickFalse'. */
7901 static void
7902 imagemagick_error (MagickWand *wand)
7904 char *description;
7905 ExceptionType severity;
7907 description = MagickGetException (wand, &severity);
7908 image_error ("ImageMagick error: %s",
7909 build_string (description),
7910 Qnil);
7911 MagickRelinquishMemory (description);
7914 /* Possibly give ImageMagick some extra help to determine the image
7915 type by supplying a "dummy" filename based on the Content-Type. */
7917 static char *
7918 imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent])
7920 Lisp_Object symbol = intern ("image-format-suffixes");
7921 Lisp_Object val = find_symbol_value (symbol);
7922 Lisp_Object format;
7924 if (! CONSP (val))
7925 return NULL;
7927 format = image_spec_value (spec, intern (":format"), NULL);
7928 val = Fcar_safe (Fcdr_safe (Fassq (format, val)));
7929 if (! STRINGP (val))
7930 return NULL;
7932 /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
7933 as ImageMagick would ignore the extra bytes anyway. */
7934 snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val));
7935 return hint_buffer;
7938 /* Animated images (e.g., GIF89a) are composed from one "master image"
7939 (which is the first one, and then there's a number of images that
7940 follow. If following images have non-transparent colors, these are
7941 composed "on top" of the master image. So, in general, one has to
7942 compute ann the preceding images to be able to display a particular
7943 sub-image.
7945 Computing all the preceding images is too slow, so we maintain a
7946 cache of previously computed images. We have to maintain a cache
7947 separate from the image cache, because the images may be scaled
7948 before display. */
7950 struct animation_cache
7952 MagickWand *wand;
7953 int index;
7954 struct timespec update_time;
7955 struct animation_cache *next;
7956 char signature[FLEXIBLE_ARRAY_MEMBER];
7959 static struct animation_cache *animation_cache = NULL;
7961 static struct animation_cache *
7962 imagemagick_create_cache (char *signature)
7964 struct animation_cache *cache
7965 = xmalloc (offsetof (struct animation_cache, signature)
7966 + strlen (signature) + 1);
7967 cache->wand = 0;
7968 cache->index = 0;
7969 cache->next = 0;
7970 strcpy (cache->signature, signature);
7971 return cache;
7974 /* Discard cached images that haven't been used for a minute. */
7975 static void
7976 imagemagick_prune_animation_cache (void)
7978 struct animation_cache **pcache = &animation_cache;
7979 struct timespec old = timespec_sub (current_timespec (),
7980 make_timespec (60, 0));
7982 while (*pcache)
7984 struct animation_cache *cache = *pcache;
7985 if (timespec_cmp (old, cache->update_time) <= 0)
7986 pcache = &cache->next;
7987 else
7989 if (cache->wand)
7990 DestroyMagickWand (cache->wand);
7991 *pcache = cache->next;
7992 xfree (cache);
7997 static struct animation_cache *
7998 imagemagick_get_animation_cache (MagickWand *wand)
8000 char *signature = MagickGetImageSignature (wand);
8001 struct animation_cache *cache;
8002 struct animation_cache **pcache = &animation_cache;
8004 imagemagick_prune_animation_cache ();
8006 while (1)
8008 cache = *pcache;
8009 if (! cache)
8011 *pcache = cache = imagemagick_create_cache (signature);
8012 break;
8014 if (strcmp (signature, cache->signature) == 0)
8015 break;
8016 pcache = &cache->next;
8019 DestroyString (signature);
8020 cache->update_time = current_timespec ();
8021 return cache;
8024 static MagickWand *
8025 imagemagick_compute_animated_image (MagickWand *super_wand, int ino)
8027 int i;
8028 MagickWand *composite_wand;
8029 size_t dest_width, dest_height;
8030 struct animation_cache *cache = imagemagick_get_animation_cache (super_wand);
8032 MagickSetIteratorIndex (super_wand, 0);
8034 if (ino == 0 || cache->wand == NULL || cache->index > ino)
8036 composite_wand = MagickGetImage (super_wand);
8037 if (cache->wand)
8038 DestroyMagickWand (cache->wand);
8040 else
8041 composite_wand = cache->wand;
8043 dest_width = MagickGetImageWidth (composite_wand);
8044 dest_height = MagickGetImageHeight (composite_wand);
8046 for (i = max (1, cache->index + 1); i <= ino; i++)
8048 MagickWand *sub_wand;
8049 PixelIterator *source_iterator, *dest_iterator;
8050 PixelWand **source, **dest;
8051 size_t source_width, source_height;
8052 ssize_t source_left, source_top;
8053 MagickPixelPacket pixel;
8054 DisposeType dispose;
8055 ptrdiff_t lines = 0;
8057 MagickSetIteratorIndex (super_wand, i);
8058 sub_wand = MagickGetImage (super_wand);
8060 MagickGetImagePage (sub_wand, &source_width, &source_height,
8061 &source_left, &source_top);
8063 /* This flag says how to handle transparent pixels. */
8064 dispose = MagickGetImageDispose (sub_wand);
8066 source_iterator = NewPixelIterator (sub_wand);
8067 if (! source_iterator)
8069 DestroyMagickWand (composite_wand);
8070 DestroyMagickWand (sub_wand);
8071 cache->wand = NULL;
8072 image_error ("Imagemagick pixel iterator creation failed",
8073 Qnil, Qnil);
8074 return NULL;
8077 dest_iterator = NewPixelIterator (composite_wand);
8078 if (! dest_iterator)
8080 DestroyMagickWand (composite_wand);
8081 DestroyMagickWand (sub_wand);
8082 DestroyPixelIterator (source_iterator);
8083 cache->wand = NULL;
8084 image_error ("Imagemagick pixel iterator creation failed",
8085 Qnil, Qnil);
8086 return NULL;
8089 /* The sub-image may not start at origin, so move the destination
8090 iterator to where the sub-image should start. */
8091 if (source_top > 0)
8093 PixelSetIteratorRow (dest_iterator, source_top);
8094 lines = source_top;
8097 while ((source = PixelGetNextIteratorRow (source_iterator, &source_width))
8098 != NULL)
8100 ptrdiff_t x;
8102 /* Sanity check. This shouldn't happen, but apparently
8103 does in some pictures. */
8104 if (++lines >= dest_height)
8105 break;
8107 dest = PixelGetNextIteratorRow (dest_iterator, &dest_width);
8108 for (x = 0; x < source_width; x++)
8110 /* Sanity check. This shouldn't happen, but apparently
8111 also does in some pictures. */
8112 if (x + source_left > dest_width - 1)
8113 break;
8114 /* Normally we only copy over non-transparent pixels,
8115 but if the disposal method is "Background", then we
8116 copy over all pixels. */
8117 if (dispose == BackgroundDispose ||
8118 PixelGetAlpha (source[x]))
8120 PixelGetMagickColor (source[x], &pixel);
8121 PixelSetMagickColor (dest[x + source_left], &pixel);
8124 PixelSyncIterator (dest_iterator);
8127 DestroyPixelIterator (source_iterator);
8128 DestroyPixelIterator (dest_iterator);
8129 DestroyMagickWand (sub_wand);
8132 /* Cache a copy for the next iteration. The current wand will be
8133 destroyed by the caller. */
8134 cache->wand = CloneMagickWand (composite_wand);
8135 cache->index = ino;
8137 return composite_wand;
8141 /* Helper function for imagemagick_load, which does the actual loading
8142 given contents and size, apart from frame and image structures,
8143 passed from imagemagick_load. Uses librimagemagick to do most of
8144 the image processing.
8146 F is a pointer to the Emacs frame; IMG to the image structure to
8147 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
8148 be parsed; SIZE is the number of bytes of data; and FILENAME is
8149 either the file name or the image data.
8151 Return true if successful. */
8153 static bool
8154 imagemagick_load_image (struct frame *f, struct image *img,
8155 unsigned char *contents, unsigned int size,
8156 char *filename)
8158 size_t width, height;
8159 MagickBooleanType status;
8160 XImagePtr ximg;
8161 int x, y;
8162 MagickWand *image_wand;
8163 PixelIterator *iterator;
8164 PixelWand **pixels, *bg_wand = NULL;
8165 MagickPixelPacket pixel;
8166 Lisp_Object image;
8167 Lisp_Object value;
8168 Lisp_Object crop;
8169 EMACS_INT ino;
8170 int desired_width, desired_height;
8171 double rotation;
8172 int pixelwidth;
8173 char hint_buffer[MaxTextExtent];
8174 char *filename_hint = NULL;
8176 /* Handle image index for image types who can contain more than one image.
8177 Interface :index is same as for GIF. First we "ping" the image to see how
8178 many sub-images it contains. Pinging is faster than loading the image to
8179 find out things about it. */
8181 /* Initialize the imagemagick environment. */
8182 MagickWandGenesis ();
8183 image = image_spec_value (img->spec, QCindex, NULL);
8184 ino = INTEGERP (image) ? XFASTINT (image) : 0;
8185 image_wand = NewMagickWand ();
8187 if (filename)
8188 status = MagickReadImage (image_wand, filename);
8189 else
8191 filename_hint = imagemagick_filename_hint (img->spec, hint_buffer);
8192 MagickSetFilename (image_wand, filename_hint);
8193 status = MagickReadImageBlob (image_wand, contents, size);
8196 if (status == MagickFalse)
8198 imagemagick_error (image_wand);
8199 DestroyMagickWand (image_wand);
8200 return 0;
8203 if (ino < 0 || ino >= MagickGetNumberImages (image_wand))
8205 image_error ("Invalid image number `%s' in image `%s'",
8206 image, img->spec);
8207 DestroyMagickWand (image_wand);
8208 return 0;
8211 if (MagickGetNumberImages (image_wand) > 1)
8212 img->lisp_data =
8213 Fcons (Qcount,
8214 Fcons (make_number (MagickGetNumberImages (image_wand)),
8215 img->lisp_data));
8217 /* If we have an animated image, get the new wand based on the
8218 "super-wand". */
8219 if (MagickGetNumberImages (image_wand) > 1)
8221 MagickWand *super_wand = image_wand;
8222 image_wand = imagemagick_compute_animated_image (super_wand, ino);
8223 if (! image_wand)
8224 image_wand = super_wand;
8225 else
8226 DestroyMagickWand (super_wand);
8229 /* Retrieve the frame's background color, for use later. */
8231 XColor bgcolor;
8232 Lisp_Object specified_bg;
8234 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
8235 if (!STRINGP (specified_bg)
8236 || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0))
8238 #ifndef HAVE_NS
8239 bgcolor.pixel = FRAME_BACKGROUND_PIXEL (f);
8240 x_query_color (f, &bgcolor);
8241 #else
8242 ns_query_color (FRAME_BACKGROUND_COLOR (f), &bgcolor, 1);
8243 #endif
8246 bg_wand = NewPixelWand ();
8247 PixelSetRed (bg_wand, (double) bgcolor.red / 65535);
8248 PixelSetGreen (bg_wand, (double) bgcolor.green / 65535);
8249 PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535);
8252 compute_image_size (MagickGetImageWidth (image_wand),
8253 MagickGetImageHeight (image_wand),
8254 img->spec, &desired_width, &desired_height);
8256 if (desired_width != -1 && desired_height != -1)
8258 status = MagickScaleImage (image_wand, desired_width, desired_height);
8259 if (status == MagickFalse)
8261 image_error ("Imagemagick scale failed", Qnil, Qnil);
8262 imagemagick_error (image_wand);
8263 goto imagemagick_error;
8267 /* crop behaves similar to image slicing in Emacs but is more memory
8268 efficient. */
8269 crop = image_spec_value (img->spec, QCcrop, NULL);
8271 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8273 /* After some testing, it seems MagickCropImage is the fastest crop
8274 function in ImageMagick. This crop function seems to do less copying
8275 than the alternatives, but it still reads the entire image into memory
8276 before cropping, which is apparently difficult to avoid when using
8277 imagemagick. */
8278 size_t crop_width = XINT (XCAR (crop));
8279 crop = XCDR (crop);
8280 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8282 size_t crop_height = XINT (XCAR (crop));
8283 crop = XCDR (crop);
8284 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8286 ssize_t crop_x = XINT (XCAR (crop));
8287 crop = XCDR (crop);
8288 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8290 ssize_t crop_y = XINT (XCAR (crop));
8291 MagickCropImage (image_wand, crop_width, crop_height,
8292 crop_x, crop_y);
8298 /* Furthermore :rotation. we need background color and angle for
8299 rotation. */
8301 TODO background handling for rotation specified_bg =
8302 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8303 (specified_bg). */
8304 value = image_spec_value (img->spec, QCrotation, NULL);
8305 if (FLOATP (value))
8307 rotation = extract_float (value);
8308 status = MagickRotateImage (image_wand, bg_wand, rotation);
8309 if (status == MagickFalse)
8311 image_error ("Imagemagick image rotate failed", Qnil, Qnil);
8312 imagemagick_error (image_wand);
8313 goto imagemagick_error;
8317 /* Set the canvas background color to the frame or specified
8318 background, and flatten the image. Note: as of ImageMagick
8319 6.6.0, SVG image transparency is not handled properly
8320 (e.g. etc/images/splash.svg shows a white background always). */
8322 MagickWand *new_wand;
8323 MagickSetImageBackgroundColor (image_wand, bg_wand);
8324 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8325 new_wand = MagickMergeImageLayers (image_wand, MergeLayer);
8326 #else
8327 new_wand = MagickFlattenImages (image_wand);
8328 #endif
8329 DestroyMagickWand (image_wand);
8330 image_wand = new_wand;
8333 /* Finally we are done manipulating the image. Figure out the
8334 resulting width/height and transfer ownership to Emacs. */
8335 height = MagickGetImageHeight (image_wand);
8336 width = MagickGetImageWidth (image_wand);
8338 if (! (width <= INT_MAX && height <= INT_MAX
8339 && check_image_size (f, width, height)))
8341 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
8342 goto imagemagick_error;
8345 /* We can now get a valid pixel buffer from the imagemagick file, if all
8346 went ok. */
8348 init_color_table ();
8350 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8351 if (imagemagick_render_type != 0)
8353 /* Magicexportimage is normally faster than pixelpushing. This
8354 method is also well tested. Some aspects of this method are
8355 ad-hoc and needs to be more researched. */
8356 int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/
8357 const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/
8358 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8359 if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth,
8360 &ximg, 0))
8362 #ifdef COLOR_TABLE_SUPPORT
8363 free_color_table ();
8364 #endif
8365 image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
8366 goto imagemagick_error;
8369 /* Oddly, the below code doesn't seem to work:*/
8370 /* switch(ximg->bitmap_unit){ */
8371 /* case 8: */
8372 /* pixelwidth=CharPixel; */
8373 /* break; */
8374 /* case 16: */
8375 /* pixelwidth=ShortPixel; */
8376 /* break; */
8377 /* case 32: */
8378 /* pixelwidth=LongPixel; */
8379 /* break; */
8380 /* } */
8382 Here im just guessing the format of the bitmap.
8383 happens to work fine for:
8384 - bw djvu images
8385 on rgb display.
8386 seems about 3 times as fast as pixel pushing(not carefully measured)
8388 pixelwidth = CharPixel; /*??? TODO figure out*/
8389 MagickExportImagePixels (image_wand, 0, 0, width, height,
8390 exportdepth, pixelwidth, ximg->data);
8392 else
8393 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8395 size_t image_height;
8397 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8398 if (!image_create_x_image_and_pixmap (f, img, width, height, 0,
8399 &ximg, 0))
8401 #ifdef COLOR_TABLE_SUPPORT
8402 free_color_table ();
8403 #endif
8404 image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
8405 goto imagemagick_error;
8408 /* Copy imagemagick image to x with primitive yet robust pixel
8409 pusher loop. This has been tested a lot with many different
8410 images. */
8412 /* Copy pixels from the imagemagick image structure to the x image map. */
8413 iterator = NewPixelIterator (image_wand);
8414 if (! iterator)
8416 #ifdef COLOR_TABLE_SUPPORT
8417 free_color_table ();
8418 #endif
8419 x_destroy_x_image (ximg);
8420 image_error ("Imagemagick pixel iterator creation failed",
8421 Qnil, Qnil);
8422 goto imagemagick_error;
8425 image_height = MagickGetImageHeight (image_wand);
8426 for (y = 0; y < image_height; y++)
8428 pixels = PixelGetNextIteratorRow (iterator, &width);
8429 if (! pixels)
8430 break;
8431 for (x = 0; x < (long) width; x++)
8433 PixelGetMagickColor (pixels[x], &pixel);
8434 XPutPixel (ximg, x, y,
8435 lookup_rgb_color (f,
8436 pixel.red,
8437 pixel.green,
8438 pixel.blue));
8441 DestroyPixelIterator (iterator);
8444 #ifdef COLOR_TABLE_SUPPORT
8445 /* Remember colors allocated for this image. */
8446 img->colors = colors_in_color_table (&img->ncolors);
8447 free_color_table ();
8448 #endif /* COLOR_TABLE_SUPPORT */
8450 img->width = width;
8451 img->height = height;
8453 /* Put ximg into the image. */
8454 image_put_x_image (f, img, ximg, 0);
8456 /* Final cleanup. image_wand should be the only resource left. */
8457 DestroyMagickWand (image_wand);
8458 if (bg_wand) DestroyPixelWand (bg_wand);
8460 /* `MagickWandTerminus' terminates the imagemagick environment. */
8461 MagickWandTerminus ();
8463 return 1;
8465 imagemagick_error:
8466 DestroyMagickWand (image_wand);
8467 if (bg_wand) DestroyPixelWand (bg_wand);
8469 MagickWandTerminus ();
8470 /* TODO more cleanup. */
8471 image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil);
8472 return 0;
8476 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8477 successful. this function will go into the imagemagick_type structure, and
8478 the prototype thus needs to be compatible with that structure. */
8480 static bool
8481 imagemagick_load (struct frame *f, struct image *img)
8483 bool success_p = 0;
8484 Lisp_Object file_name;
8486 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8487 file_name = image_spec_value (img->spec, QCfile, NULL);
8488 if (STRINGP (file_name))
8490 Lisp_Object file;
8492 file = x_find_image_file (file_name);
8493 if (!STRINGP (file))
8495 image_error ("Cannot find image file `%s'", file_name, Qnil);
8496 return 0;
8498 #ifdef WINDOWSNT
8499 file = ansi_encode_filename (file);
8500 #endif
8501 success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file));
8503 /* Else its not a file, its a lisp object. Load the image from a
8504 lisp object rather than a file. */
8505 else
8507 Lisp_Object data;
8509 data = image_spec_value (img->spec, QCdata, NULL);
8510 if (!STRINGP (data))
8512 image_error ("Invalid image data `%s'", data, Qnil);
8513 return 0;
8515 success_p = imagemagick_load_image (f, img, SDATA (data),
8516 SBYTES (data), NULL);
8519 return success_p;
8522 DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0,
8523 doc: /* Return a list of image types supported by ImageMagick.
8524 Each entry in this list is a symbol named after an ImageMagick format
8525 tag. See the ImageMagick manual for a list of ImageMagick formats and
8526 their descriptions (http://www.imagemagick.org/script/formats.php).
8527 You can also try the shell command: `identify -list format'.
8529 Note that ImageMagick recognizes many file-types that Emacs does not
8530 recognize as images, such as C. See `imagemagick-types-enable'
8531 and `imagemagick-types-inhibit'. */)
8532 (void)
8534 Lisp_Object typelist = Qnil;
8535 size_t numf = 0;
8536 ExceptionInfo ex;
8537 char **imtypes;
8538 size_t i;
8539 Lisp_Object Qimagemagicktype;
8541 GetExceptionInfo(&ex);
8542 imtypes = GetMagickList ("*", &numf, &ex);
8543 DestroyExceptionInfo(&ex);
8545 for (i = 0; i < numf; i++)
8547 Qimagemagicktype = intern (imtypes[i]);
8548 typelist = Fcons (Qimagemagicktype, typelist);
8549 imtypes[i] = MagickRelinquishMemory (imtypes[i]);
8552 MagickRelinquishMemory (imtypes);
8553 return Fnreverse (typelist);
8556 #endif /* defined (HAVE_IMAGEMAGICK) */
8560 /***********************************************************************
8562 ***********************************************************************/
8564 #if defined (HAVE_RSVG)
8566 /* Function prototypes. */
8568 static bool svg_image_p (Lisp_Object object);
8569 static bool svg_load (struct frame *f, struct image *img);
8571 static bool svg_load_image (struct frame *, struct image *,
8572 unsigned char *, ptrdiff_t);
8574 /* The symbol `svg' identifying images of this type. */
8576 static Lisp_Object Qsvg;
8578 /* Indices of image specification fields in svg_format, below. */
8580 enum svg_keyword_index
8582 SVG_TYPE,
8583 SVG_DATA,
8584 SVG_FILE,
8585 SVG_ASCENT,
8586 SVG_MARGIN,
8587 SVG_RELIEF,
8588 SVG_ALGORITHM,
8589 SVG_HEURISTIC_MASK,
8590 SVG_MASK,
8591 SVG_BACKGROUND,
8592 SVG_LAST
8595 /* Vector of image_keyword structures describing the format
8596 of valid user-defined image specifications. */
8598 static const struct image_keyword svg_format[SVG_LAST] =
8600 {":type", IMAGE_SYMBOL_VALUE, 1},
8601 {":data", IMAGE_STRING_VALUE, 0},
8602 {":file", IMAGE_STRING_VALUE, 0},
8603 {":ascent", IMAGE_ASCENT_VALUE, 0},
8604 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
8605 {":relief", IMAGE_INTEGER_VALUE, 0},
8606 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8607 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8608 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8609 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8612 #if defined HAVE_NTGUI && defined WINDOWSNT
8613 static bool init_svg_functions (void);
8614 #else
8615 #define init_svg_functions NULL
8616 #endif
8618 /* Structure describing the image type `svg'. Its the same type of
8619 structure defined for all image formats, handled by emacs image
8620 functions. See struct image_type in dispextern.h. */
8622 static struct image_type svg_type =
8624 &Qsvg,
8625 svg_image_p,
8626 svg_load,
8627 x_clear_image,
8628 init_svg_functions,
8629 NULL
8633 /* Return true if OBJECT is a valid SVG image specification. Do
8634 this by calling parse_image_spec and supplying the keywords that
8635 identify the SVG format. */
8637 static bool
8638 svg_image_p (Lisp_Object object)
8640 struct image_keyword fmt[SVG_LAST];
8641 memcpy (fmt, svg_format, sizeof fmt);
8643 if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg))
8644 return 0;
8646 /* Must specify either the :data or :file keyword. */
8647 return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1;
8650 #include <librsvg/rsvg.h>
8652 #ifdef WINDOWSNT
8654 /* SVG library functions. */
8655 DEF_IMGLIB_FN (RsvgHandle *, rsvg_handle_new, (void));
8656 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions, (RsvgHandle *, RsvgDimensionData *));
8657 DEF_IMGLIB_FN (gboolean, rsvg_handle_write, (RsvgHandle *, const guchar *, gsize, GError **));
8658 DEF_IMGLIB_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **));
8659 DEF_IMGLIB_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *));
8660 DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback, (RsvgHandle *, RsvgSizeFunc, gpointer, GDestroyNotify));
8662 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *));
8663 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *));
8664 DEF_IMGLIB_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *));
8665 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *));
8666 DEF_IMGLIB_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *));
8667 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *));
8668 DEF_IMGLIB_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *));
8669 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *));
8671 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8672 DEF_IMGLIB_FN (void, g_type_init, (void));
8673 #endif
8674 DEF_IMGLIB_FN (void, g_object_unref, (gpointer));
8675 DEF_IMGLIB_FN (void, g_error_free, (GError *));
8677 Lisp_Object Qgdk_pixbuf, Qglib, Qgobject;
8679 static bool
8680 init_svg_functions (void)
8682 HMODULE library, gdklib, glib, gobject;
8684 if (!(glib = w32_delayed_load (Qglib))
8685 || !(gobject = w32_delayed_load (Qgobject))
8686 || !(gdklib = w32_delayed_load (Qgdk_pixbuf))
8687 || !(library = w32_delayed_load (Qsvg)))
8688 return 0;
8690 LOAD_IMGLIB_FN (library, rsvg_handle_new);
8691 LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions);
8692 LOAD_IMGLIB_FN (library, rsvg_handle_write);
8693 LOAD_IMGLIB_FN (library, rsvg_handle_close);
8694 LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf);
8696 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width);
8697 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height);
8698 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels);
8699 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride);
8700 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace);
8701 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels);
8702 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha);
8703 LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample);
8705 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8706 LOAD_IMGLIB_FN (gobject, g_type_init);
8707 #endif
8708 LOAD_IMGLIB_FN (gobject, g_object_unref);
8709 LOAD_IMGLIB_FN (glib, g_error_free);
8711 return 1;
8714 #else
8715 /* The following aliases for library functions allow dynamic loading
8716 to be used on some platforms. */
8717 #define fn_rsvg_handle_new rsvg_handle_new
8718 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8719 #define fn_rsvg_handle_write rsvg_handle_write
8720 #define fn_rsvg_handle_close rsvg_handle_close
8721 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8723 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8724 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8725 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8726 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8727 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8728 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8729 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8730 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8732 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8733 #define fn_g_type_init g_type_init
8734 #endif
8735 #define fn_g_object_unref g_object_unref
8736 #define fn_g_error_free g_error_free
8737 #endif /* !WINDOWSNT */
8739 /* Load SVG image IMG for use on frame F. Value is true if
8740 successful. */
8742 static bool
8743 svg_load (struct frame *f, struct image *img)
8745 bool success_p = 0;
8746 Lisp_Object file_name;
8748 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8749 file_name = image_spec_value (img->spec, QCfile, NULL);
8750 if (STRINGP (file_name))
8752 Lisp_Object file;
8753 unsigned char *contents;
8754 ptrdiff_t size;
8756 file = x_find_image_file (file_name);
8757 if (!STRINGP (file))
8759 image_error ("Cannot find image file `%s'", file_name, Qnil);
8760 return 0;
8763 /* Read the entire file into memory. */
8764 contents = slurp_file (SSDATA (file), &size);
8765 if (contents == NULL)
8767 image_error ("Error loading SVG image `%s'", img->spec, Qnil);
8768 return 0;
8770 /* If the file was slurped into memory properly, parse it. */
8771 success_p = svg_load_image (f, img, contents, size);
8772 xfree (contents);
8774 /* Else its not a file, its a lisp object. Load the image from a
8775 lisp object rather than a file. */
8776 else
8778 Lisp_Object data;
8780 data = image_spec_value (img->spec, QCdata, NULL);
8781 if (!STRINGP (data))
8783 image_error ("Invalid image data `%s'", data, Qnil);
8784 return 0;
8786 success_p = svg_load_image (f, img, SDATA (data), SBYTES (data));
8789 return success_p;
8792 /* svg_load_image is a helper function for svg_load, which does the
8793 actual loading given contents and size, apart from frame and image
8794 structures, passed from svg_load.
8796 Uses librsvg to do most of the image processing.
8798 Returns true when successful. */
8799 static bool
8800 svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */
8801 struct image *img, /* Pointer to emacs image structure. */
8802 unsigned char *contents, /* String containing the SVG XML data to be parsed. */
8803 ptrdiff_t size) /* Size of data in bytes. */
8805 RsvgHandle *rsvg_handle;
8806 RsvgDimensionData dimension_data;
8807 GError *err = NULL;
8808 GdkPixbuf *pixbuf;
8809 int width;
8810 int height;
8811 const guint8 *pixels;
8812 int rowstride;
8813 XImagePtr ximg;
8814 Lisp_Object specified_bg;
8815 XColor background;
8816 int x;
8817 int y;
8819 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8820 /* g_type_init is a glib function that must be called prior to
8821 using gnome type library functions (obsolete since 2.36.0). */
8822 fn_g_type_init ();
8823 #endif
8825 /* Make a handle to a new rsvg object. */
8826 rsvg_handle = fn_rsvg_handle_new ();
8828 /* Parse the contents argument and fill in the rsvg_handle. */
8829 fn_rsvg_handle_write (rsvg_handle, contents, size, &err);
8830 if (err) goto rsvg_error;
8832 /* The parsing is complete, rsvg_handle is ready to used, close it
8833 for further writes. */
8834 fn_rsvg_handle_close (rsvg_handle, &err);
8835 if (err) goto rsvg_error;
8837 fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data);
8838 if (! check_image_size (f, dimension_data.width, dimension_data.height))
8840 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
8841 goto rsvg_error;
8844 /* We can now get a valid pixel buffer from the svg file, if all
8845 went ok. */
8846 pixbuf = fn_rsvg_handle_get_pixbuf (rsvg_handle);
8847 if (!pixbuf) goto rsvg_error;
8848 fn_g_object_unref (rsvg_handle);
8850 /* Extract some meta data from the svg handle. */
8851 width = fn_gdk_pixbuf_get_width (pixbuf);
8852 height = fn_gdk_pixbuf_get_height (pixbuf);
8853 pixels = fn_gdk_pixbuf_get_pixels (pixbuf);
8854 rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf);
8856 /* Validate the svg meta data. */
8857 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
8858 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4);
8859 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf));
8860 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
8862 /* Try to create a x pixmap to hold the svg pixmap. */
8863 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
8865 fn_g_object_unref (pixbuf);
8866 return 0;
8869 init_color_table ();
8871 /* Handle alpha channel by combining the image with a background
8872 color. */
8873 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
8874 if (!STRINGP (specified_bg)
8875 || !x_defined_color (f, SSDATA (specified_bg), &background, 0))
8877 #ifndef HAVE_NS
8878 background.pixel = FRAME_BACKGROUND_PIXEL (f);
8879 x_query_color (f, &background);
8880 #else
8881 ns_query_color (FRAME_BACKGROUND_COLOR (f), &background, 1);
8882 #endif
8885 /* SVG pixmaps specify transparency in the last byte, so right
8886 shift 8 bits to get rid of it, since emacs doesn't support
8887 transparency. */
8888 background.red >>= 8;
8889 background.green >>= 8;
8890 background.blue >>= 8;
8892 /* This loop handles opacity values, since Emacs assumes
8893 non-transparent images. Each pixel must be "flattened" by
8894 calculating the resulting color, given the transparency of the
8895 pixel, and the image background color. */
8896 for (y = 0; y < height; ++y)
8898 for (x = 0; x < width; ++x)
8900 int red;
8901 int green;
8902 int blue;
8903 int opacity;
8905 red = *pixels++;
8906 green = *pixels++;
8907 blue = *pixels++;
8908 opacity = *pixels++;
8910 red = ((red * opacity)
8911 + (background.red * ((1 << 8) - opacity)));
8912 green = ((green * opacity)
8913 + (background.green * ((1 << 8) - opacity)));
8914 blue = ((blue * opacity)
8915 + (background.blue * ((1 << 8) - opacity)));
8917 XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue));
8920 pixels += rowstride - 4 * width;
8923 #ifdef COLOR_TABLE_SUPPORT
8924 /* Remember colors allocated for this image. */
8925 img->colors = colors_in_color_table (&img->ncolors);
8926 free_color_table ();
8927 #endif /* COLOR_TABLE_SUPPORT */
8929 fn_g_object_unref (pixbuf);
8931 img->width = width;
8932 img->height = height;
8934 /* Maybe fill in the background field while we have ximg handy.
8935 Casting avoids a GCC warning. */
8936 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
8938 /* Put ximg into the image. */
8939 image_put_x_image (f, img, ximg, 0);
8941 return 1;
8943 rsvg_error:
8944 fn_g_object_unref (rsvg_handle);
8945 /* FIXME: Use error->message so the user knows what is the actual
8946 problem with the image. */
8947 image_error ("Error parsing SVG image `%s'", img->spec, Qnil);
8948 fn_g_error_free (err);
8949 return 0;
8952 #endif /* defined (HAVE_RSVG) */
8957 /***********************************************************************
8958 Ghostscript
8959 ***********************************************************************/
8961 #ifdef HAVE_X_WINDOWS
8962 #define HAVE_GHOSTSCRIPT 1
8963 #endif /* HAVE_X_WINDOWS */
8965 #ifdef HAVE_GHOSTSCRIPT
8967 static bool gs_image_p (Lisp_Object object);
8968 static bool gs_load (struct frame *f, struct image *img);
8969 static void gs_clear_image (struct frame *f, struct image *img);
8971 /* Keyword symbols. */
8973 static Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
8975 /* Indices of image specification fields in gs_format, below. */
8977 enum gs_keyword_index
8979 GS_TYPE,
8980 GS_PT_WIDTH,
8981 GS_PT_HEIGHT,
8982 GS_FILE,
8983 GS_LOADER,
8984 GS_BOUNDING_BOX,
8985 GS_ASCENT,
8986 GS_MARGIN,
8987 GS_RELIEF,
8988 GS_ALGORITHM,
8989 GS_HEURISTIC_MASK,
8990 GS_MASK,
8991 GS_BACKGROUND,
8992 GS_LAST
8995 /* Vector of image_keyword structures describing the format
8996 of valid user-defined image specifications. */
8998 static const struct image_keyword gs_format[GS_LAST] =
9000 {":type", IMAGE_SYMBOL_VALUE, 1},
9001 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9002 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9003 {":file", IMAGE_STRING_VALUE, 1},
9004 {":loader", IMAGE_FUNCTION_VALUE, 0},
9005 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9006 {":ascent", IMAGE_ASCENT_VALUE, 0},
9007 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
9008 {":relief", IMAGE_INTEGER_VALUE, 0},
9009 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9010 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9011 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9012 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9015 /* Structure describing the image type `ghostscript'. */
9017 static struct image_type gs_type =
9019 &Qpostscript,
9020 gs_image_p,
9021 gs_load,
9022 gs_clear_image,
9023 NULL,
9024 NULL
9028 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9030 static void
9031 gs_clear_image (struct frame *f, struct image *img)
9033 x_clear_image (f, img);
9037 /* Return true if OBJECT is a valid Ghostscript image
9038 specification. */
9040 static bool
9041 gs_image_p (Lisp_Object object)
9043 struct image_keyword fmt[GS_LAST];
9044 Lisp_Object tem;
9045 int i;
9047 memcpy (fmt, gs_format, sizeof fmt);
9049 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9050 return 0;
9052 /* Bounding box must be a list or vector containing 4 integers. */
9053 tem = fmt[GS_BOUNDING_BOX].value;
9054 if (CONSP (tem))
9056 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9057 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9058 return 0;
9059 if (!NILP (tem))
9060 return 0;
9062 else if (VECTORP (tem))
9064 if (ASIZE (tem) != 4)
9065 return 0;
9066 for (i = 0; i < 4; ++i)
9067 if (!INTEGERP (AREF (tem, i)))
9068 return 0;
9070 else
9071 return 0;
9073 return 1;
9077 /* Load Ghostscript image IMG for use on frame F. Value is true
9078 if successful. */
9080 static bool
9081 gs_load (struct frame *f, struct image *img)
9083 uprintmax_t printnum1, printnum2;
9084 char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)];
9085 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9086 Lisp_Object frame;
9087 double in_width, in_height;
9088 Lisp_Object pixel_colors = Qnil;
9090 /* Compute pixel size of pixmap needed from the given size in the
9091 image specification. Sizes in the specification are in pt. 1 pt
9092 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9093 info. */
9094 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9095 in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0;
9096 in_width *= FRAME_RES_X (f);
9097 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9098 in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0;
9099 in_height *= FRAME_RES_Y (f);
9101 if (! (in_width <= INT_MAX && in_height <= INT_MAX
9102 && check_image_size (f, in_width, in_height)))
9104 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
9105 return 0;
9107 img->width = in_width;
9108 img->height = in_height;
9110 /* Create the pixmap. */
9111 eassert (img->pixmap == NO_PIXMAP);
9113 if (x_check_image_size (0, img->width, img->height))
9115 /* Only W32 version did BLOCK_INPUT here. ++kfs */
9116 block_input ();
9117 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9118 img->width, img->height,
9119 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9120 unblock_input ();
9123 if (!img->pixmap)
9125 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
9126 return 0;
9129 /* Call the loader to fill the pixmap. It returns a process object
9130 if successful. We do not record_unwind_protect here because
9131 other places in redisplay like calling window scroll functions
9132 don't either. Let the Lisp loader use `unwind-protect' instead. */
9133 printnum1 = FRAME_X_WINDOW (f);
9134 printnum2 = img->pixmap;
9135 window_and_pixmap_id
9136 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9138 printnum1 = FRAME_FOREGROUND_PIXEL (f);
9139 printnum2 = FRAME_BACKGROUND_PIXEL (f);
9140 pixel_colors
9141 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9143 XSETFRAME (frame, f);
9144 loader = image_spec_value (img->spec, QCloader, NULL);
9145 if (NILP (loader))
9146 loader = intern ("gs-load-image");
9148 img->lisp_data = call6 (loader, frame, img->spec,
9149 make_number (img->width),
9150 make_number (img->height),
9151 window_and_pixmap_id,
9152 pixel_colors);
9153 return PROCESSP (img->lisp_data);
9157 /* Kill the Ghostscript process that was started to fill PIXMAP on
9158 frame F. Called from XTread_socket when receiving an event
9159 telling Emacs that Ghostscript has finished drawing. */
9161 void
9162 x_kill_gs_process (Pixmap pixmap, struct frame *f)
9164 struct image_cache *c = FRAME_IMAGE_CACHE (f);
9165 int class;
9166 ptrdiff_t i;
9167 struct image *img;
9169 /* Find the image containing PIXMAP. */
9170 for (i = 0; i < c->used; ++i)
9171 if (c->images[i]->pixmap == pixmap)
9172 break;
9174 /* Should someone in between have cleared the image cache, for
9175 instance, give up. */
9176 if (i == c->used)
9177 return;
9179 /* Kill the GS process. We should have found PIXMAP in the image
9180 cache and its image should contain a process object. */
9181 img = c->images[i];
9182 eassert (PROCESSP (img->lisp_data));
9183 Fkill_process (img->lisp_data, Qnil);
9184 img->lisp_data = Qnil;
9186 #if defined (HAVE_X_WINDOWS)
9188 /* On displays with a mutable colormap, figure out the colors
9189 allocated for the image by looking at the pixels of an XImage for
9190 img->pixmap. */
9191 class = FRAME_X_VISUAL (f)->class;
9192 if (class != StaticColor && class != StaticGray && class != TrueColor)
9194 XImagePtr ximg;
9196 block_input ();
9198 /* Try to get an XImage for img->pixmep. */
9199 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
9200 0, 0, img->width, img->height, ~0, ZPixmap);
9201 if (ximg)
9203 int x, y;
9205 /* Initialize the color table. */
9206 init_color_table ();
9208 /* For each pixel of the image, look its color up in the
9209 color table. After having done so, the color table will
9210 contain an entry for each color used by the image. */
9211 for (y = 0; y < img->height; ++y)
9212 for (x = 0; x < img->width; ++x)
9214 unsigned long pixel = XGetPixel (ximg, x, y);
9215 lookup_pixel_color (f, pixel);
9218 /* Record colors in the image. Free color table and XImage. */
9219 #ifdef COLOR_TABLE_SUPPORT
9220 img->colors = colors_in_color_table (&img->ncolors);
9221 free_color_table ();
9222 #endif
9223 XDestroyImage (ximg);
9225 #if 0 /* This doesn't seem to be the case. If we free the colors
9226 here, we get a BadAccess later in x_clear_image when
9227 freeing the colors. */
9228 /* We have allocated colors once, but Ghostscript has also
9229 allocated colors on behalf of us. So, to get the
9230 reference counts right, free them once. */
9231 if (img->ncolors)
9232 x_free_colors (f, img->colors, img->ncolors);
9233 #endif
9235 else
9236 image_error ("Cannot get X image of `%s'; colors will not be freed",
9237 img->spec, Qnil);
9239 unblock_input ();
9241 #endif /* HAVE_X_WINDOWS */
9243 /* Now that we have the pixmap, compute mask and transform the
9244 image if requested. */
9245 block_input ();
9246 postprocess_image (f, img);
9247 unblock_input ();
9250 #endif /* HAVE_GHOSTSCRIPT */
9253 /***********************************************************************
9254 Tests
9255 ***********************************************************************/
9257 #ifdef GLYPH_DEBUG
9259 DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
9260 doc: /* Value is non-nil if SPEC is a valid image specification. */)
9261 (Lisp_Object spec)
9263 return valid_image_p (spec) ? Qt : Qnil;
9267 DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
9268 (Lisp_Object spec)
9270 ptrdiff_t id = -1;
9272 if (valid_image_p (spec))
9273 id = lookup_image (SELECTED_FRAME (), spec);
9275 debug_print (spec);
9276 return make_number (id);
9279 #endif /* GLYPH_DEBUG */
9282 /***********************************************************************
9283 Initialization
9284 ***********************************************************************/
9286 DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0,
9287 doc: /* Initialize image library implementing image type TYPE.
9288 Return non-nil if TYPE is a supported image type.
9290 If image libraries are loaded dynamically (currently only the case on
9291 MS-Windows), load the library for TYPE if it is not yet loaded, using
9292 the library file(s) specified by `dynamic-library-alist'. */)
9293 (Lisp_Object type)
9295 return lookup_image_type (type) ? Qt : Qnil;
9298 /* Look up image type TYPE, and return a pointer to its image_type
9299 structure. Return 0 if TYPE is not a known image type. */
9301 static struct image_type *
9302 lookup_image_type (Lisp_Object type)
9304 /* Types pbm and xbm are built-in and always available. */
9305 if (EQ (type, Qpbm))
9306 return define_image_type (&pbm_type);
9308 if (EQ (type, Qxbm))
9309 return define_image_type (&xbm_type);
9311 #if defined (HAVE_XPM) || defined (HAVE_NS)
9312 if (EQ (type, Qxpm))
9313 return define_image_type (&xpm_type);
9314 #endif
9316 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9317 if (EQ (type, Qjpeg))
9318 return define_image_type (&jpeg_type);
9319 #endif
9321 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9322 if (EQ (type, Qtiff))
9323 return define_image_type (&tiff_type);
9324 #endif
9326 #if defined (HAVE_GIF) || defined (HAVE_NS)
9327 if (EQ (type, Qgif))
9328 return define_image_type (&gif_type);
9329 #endif
9331 #if defined (HAVE_PNG) || defined (HAVE_NS)
9332 if (EQ (type, Qpng))
9333 return define_image_type (&png_type);
9334 #endif
9336 #if defined (HAVE_RSVG)
9337 if (EQ (type, Qsvg))
9338 return define_image_type (&svg_type);
9339 #endif
9341 #if defined (HAVE_IMAGEMAGICK)
9342 if (EQ (type, Qimagemagick))
9343 return define_image_type (&imagemagick_type);
9344 #endif
9346 #ifdef HAVE_GHOSTSCRIPT
9347 if (EQ (type, Qpostscript))
9348 return define_image_type (&gs_type);
9349 #endif
9351 return NULL;
9354 /* Reset image_types before dumping.
9355 Called from Fdump_emacs. */
9357 void
9358 reset_image_types (void)
9360 while (image_types)
9362 struct image_type *next = image_types->next;
9363 xfree (image_types);
9364 image_types = next;
9368 void
9369 syms_of_image (void)
9371 /* Initialize this only once; it will be reset before dumping. */
9372 image_types = NULL;
9374 /* Must be defined now because we're going to update it below, while
9375 defining the supported image types. */
9376 DEFVAR_LISP ("image-types", Vimage_types,
9377 doc: /* List of potentially supported image types.
9378 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9379 To check whether it is really supported, use `image-type-available-p'. */);
9380 Vimage_types = Qnil;
9382 DEFVAR_LISP ("max-image-size", Vmax_image_size,
9383 doc: /* Maximum size of images.
9384 Emacs will not load an image into memory if its pixel width or
9385 pixel height exceeds this limit.
9387 If the value is an integer, it directly specifies the maximum
9388 image height and width, measured in pixels. If it is a floating
9389 point number, it specifies the maximum image height and width
9390 as a ratio to the frame height and width. If the value is
9391 non-numeric, there is no explicit limit on the size of images. */);
9392 Vmax_image_size = make_float (MAX_IMAGE_SIZE);
9394 DEFSYM (Qcount, "count");
9395 DEFSYM (Qextension_data, "extension-data");
9396 DEFSYM (Qdelay, "delay");
9398 DEFSYM (QCascent, ":ascent");
9399 DEFSYM (QCmargin, ":margin");
9400 DEFSYM (QCrelief, ":relief");
9401 DEFSYM (QCconversion, ":conversion");
9402 DEFSYM (QCcolor_symbols, ":color-symbols");
9403 DEFSYM (QCheuristic_mask, ":heuristic-mask");
9404 DEFSYM (QCindex, ":index");
9405 DEFSYM (QCgeometry, ":geometry");
9406 DEFSYM (QCcrop, ":crop");
9407 DEFSYM (QCrotation, ":rotation");
9408 DEFSYM (QCmatrix, ":matrix");
9409 DEFSYM (QCcolor_adjustment, ":color-adjustment");
9410 DEFSYM (QCmask, ":mask");
9412 DEFSYM (Qlaplace, "laplace");
9413 DEFSYM (Qemboss, "emboss");
9414 DEFSYM (Qedge_detection, "edge-detection");
9415 DEFSYM (Qheuristic, "heuristic");
9417 DEFSYM (Qpostscript, "postscript");
9418 DEFSYM (QCmax_width, ":max-width");
9419 DEFSYM (QCmax_height, ":max-height");
9420 #ifdef HAVE_GHOSTSCRIPT
9421 ADD_IMAGE_TYPE (Qpostscript);
9422 DEFSYM (QCloader, ":loader");
9423 DEFSYM (QCbounding_box, ":bounding-box");
9424 DEFSYM (QCpt_width, ":pt-width");
9425 DEFSYM (QCpt_height, ":pt-height");
9426 #endif /* HAVE_GHOSTSCRIPT */
9428 #ifdef HAVE_NTGUI
9429 DEFSYM (Qlibpng_version, "libpng-version");
9430 Fset (Qlibpng_version,
9431 #if HAVE_PNG
9432 make_number (PNG_LIBPNG_VER)
9433 #else
9434 make_number (-1)
9435 #endif
9437 DEFSYM (Qlibgif_version, "libgif-version");
9438 Fset (Qlibgif_version,
9439 #ifdef HAVE_GIF
9440 make_number (GIFLIB_MAJOR * 10000
9441 + GIFLIB_MINOR * 100
9442 + GIFLIB_RELEASE)
9443 #else
9444 make_number (-1)
9445 #endif
9447 DEFSYM (Qlibjpeg_version, "libjpeg-version");
9448 Fset (Qlibjpeg_version,
9449 #if HAVE_JPEG
9450 make_number (JPEG_LIB_VERSION)
9451 #else
9452 make_number (-1)
9453 #endif
9455 #endif
9457 DEFSYM (Qpbm, "pbm");
9458 ADD_IMAGE_TYPE (Qpbm);
9460 DEFSYM (Qxbm, "xbm");
9461 ADD_IMAGE_TYPE (Qxbm);
9463 #if defined (HAVE_XPM) || defined (HAVE_NS)
9464 DEFSYM (Qxpm, "xpm");
9465 ADD_IMAGE_TYPE (Qxpm);
9466 #endif
9468 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9469 DEFSYM (Qjpeg, "jpeg");
9470 ADD_IMAGE_TYPE (Qjpeg);
9471 #endif
9473 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9474 DEFSYM (Qtiff, "tiff");
9475 ADD_IMAGE_TYPE (Qtiff);
9476 #endif
9478 #if defined (HAVE_GIF) || defined (HAVE_NS)
9479 DEFSYM (Qgif, "gif");
9480 ADD_IMAGE_TYPE (Qgif);
9481 #endif
9483 #if defined (HAVE_PNG) || defined (HAVE_NS)
9484 DEFSYM (Qpng, "png");
9485 ADD_IMAGE_TYPE (Qpng);
9486 #endif
9488 #if defined (HAVE_IMAGEMAGICK)
9489 DEFSYM (Qimagemagick, "imagemagick");
9490 ADD_IMAGE_TYPE (Qimagemagick);
9491 #endif
9493 #if defined (HAVE_RSVG)
9494 DEFSYM (Qsvg, "svg");
9495 ADD_IMAGE_TYPE (Qsvg);
9496 #ifdef HAVE_NTGUI
9497 /* Other libraries used directly by svg code. */
9498 DEFSYM (Qgdk_pixbuf, "gdk-pixbuf");
9499 DEFSYM (Qglib, "glib");
9500 DEFSYM (Qgobject, "gobject");
9501 #endif /* HAVE_NTGUI */
9502 #endif /* HAVE_RSVG */
9504 defsubr (&Sinit_image_library);
9505 #ifdef HAVE_IMAGEMAGICK
9506 defsubr (&Simagemagick_types);
9507 #endif
9508 defsubr (&Sclear_image_cache);
9509 defsubr (&Simage_flush);
9510 defsubr (&Simage_size);
9511 defsubr (&Simage_mask_p);
9512 defsubr (&Simage_metadata);
9514 #ifdef GLYPH_DEBUG
9515 defsubr (&Simagep);
9516 defsubr (&Slookup_image);
9517 #endif
9519 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images,
9520 doc: /* Non-nil means always draw a cross over disabled images.
9521 Disabled images are those having a `:conversion disabled' property.
9522 A cross is always drawn on black & white displays. */);
9523 cross_disabled_images = 0;
9525 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path,
9526 doc: /* List of directories to search for window system bitmap files. */);
9527 Vx_bitmap_file_path = decode_env_path (0, PATH_BITMAPS, 0);
9529 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay,
9530 doc: /* Maximum time after which images are removed from the cache.
9531 When an image has not been displayed this many seconds, Emacs
9532 automatically removes it from the image cache. If the cache contains
9533 a large number of images, the actual eviction time may be shorter.
9534 The value can also be nil, meaning the cache is never cleared.
9536 The function `clear-image-cache' disregards this variable. */);
9537 Vimage_cache_eviction_delay = make_number (300);
9538 #ifdef HAVE_IMAGEMAGICK
9539 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type,
9540 doc: /* Integer indicating which ImageMagick rendering method to use.
9541 The options are:
9542 0 -- the default method (pixel pushing)
9543 1 -- a newer method ("MagickExportImagePixels") that may perform
9544 better (speed etc) in some cases, but has not been as thoroughly
9545 tested with Emacs as the default method. This method requires
9546 ImageMagick version 6.4.6 (approximately) or later.
9547 */);
9548 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9549 imagemagick_render_type = 0;
9550 #endif