; Remove accidentally duplicated redisplay test.
[emacs.git] / src / image.c
blob066db74f786b659b6804ebb97295d80a82f10d10
1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2015 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 /* Include this before including <setjmp.h> to work around bugs with
25 older libpng; see Bug#17429. */
26 #if defined HAVE_PNG && !defined HAVE_NS
27 # include <png.h>
28 #endif
30 #include <setjmp.h>
31 #include <c-ctype.h>
33 #include "lisp.h"
34 #include "frame.h"
35 #include "window.h"
36 #include "buffer.h"
37 #include "dispextern.h"
38 #include "blockinput.h"
39 #include "systime.h"
40 #include <epaths.h>
41 #include "character.h"
42 #include "coding.h"
43 #include "termhooks.h"
44 #include "font.h"
46 #ifdef HAVE_SYS_STAT_H
47 #include <sys/stat.h>
48 #endif /* HAVE_SYS_STAT_H */
50 #ifdef HAVE_SYS_TYPES_H
51 #include <sys/types.h>
52 #endif /* HAVE_SYS_TYPES_H */
54 #ifdef HAVE_WINDOW_SYSTEM
55 #include TERM_HEADER
56 #endif /* HAVE_WINDOW_SYSTEM */
58 #ifdef HAVE_X_WINDOWS
59 #define COLOR_TABLE_SUPPORT 1
61 typedef struct x_bitmap_record Bitmap_Record;
62 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
63 #define NO_PIXMAP None
65 #define PIX_MASK_RETAIN 0
66 #define PIX_MASK_DRAW 1
67 #endif /* HAVE_X_WINDOWS */
69 #ifdef HAVE_NTGUI
71 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
72 #ifdef WINDOWSNT
73 # include "w32.h"
74 #endif
76 /* W32_TODO : Color tables on W32. */
77 #undef COLOR_TABLE_SUPPORT
79 typedef struct w32_bitmap_record Bitmap_Record;
80 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
81 #define NO_PIXMAP 0
83 #define PIX_MASK_RETAIN 0
84 #define PIX_MASK_DRAW 1
86 #define x_defined_color w32_defined_color
87 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
89 #endif /* HAVE_NTGUI */
91 #ifdef USE_CAIRO
92 #undef COLOR_TABLE_SUPPORT
93 #endif
95 #ifdef HAVE_NS
96 #undef COLOR_TABLE_SUPPORT
98 typedef struct ns_bitmap_record Bitmap_Record;
100 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
101 #define NO_PIXMAP 0
103 #define PIX_MASK_RETAIN 0
104 #define PIX_MASK_DRAW 1
106 #define x_defined_color(f, name, color_def, alloc) \
107 ns_defined_color (f, name, color_def, alloc, 0)
108 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
109 #endif /* HAVE_NS */
111 static void x_disable_image (struct frame *, struct image *);
112 static void x_edge_detection (struct frame *, struct image *, Lisp_Object,
113 Lisp_Object);
115 static void init_color_table (void);
116 static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b);
117 #ifdef COLOR_TABLE_SUPPORT
118 static void free_color_table (void);
119 static unsigned long *colors_in_color_table (int *n);
120 #endif
122 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
123 id, which is just an int that this section returns. Bitmaps are
124 reference counted so they can be shared among frames.
126 Bitmap indices are guaranteed to be > 0, so a negative number can
127 be used to indicate no bitmap.
129 If you use x_create_bitmap_from_data, then you must keep track of
130 the bitmaps yourself. That is, creating a bitmap from the same
131 data more than once will not be caught. */
133 #ifdef HAVE_NS
134 /* Use with images created by ns_image_for_XPM. */
135 static unsigned long
136 XGetPixel (XImagePtr ximage, int x, int y)
138 return ns_get_pixel (ximage, x, y);
141 /* Use with images created by ns_image_for_XPM; alpha set to 1;
142 pixel is assumed to be in RGB form. */
143 static void
144 XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel)
146 ns_put_pixel (ximage, x, y, pixel);
148 #endif /* HAVE_NS */
151 /* Functions to access the contents of a bitmap, given an id. */
153 #ifdef HAVE_X_WINDOWS
154 static int
155 x_bitmap_height (struct frame *f, ptrdiff_t id)
157 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].height;
160 static int
161 x_bitmap_width (struct frame *f, ptrdiff_t id)
163 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].width;
165 #endif
167 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
168 ptrdiff_t
169 x_bitmap_pixmap (struct frame *f, ptrdiff_t id)
171 /* HAVE_NTGUI needs the explicit cast here. */
172 return (ptrdiff_t) FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
174 #endif
176 #ifdef HAVE_X_WINDOWS
178 x_bitmap_mask (struct frame *f, ptrdiff_t id)
180 return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
182 #endif
184 /* Allocate a new bitmap record. Returns index of new record. */
186 static ptrdiff_t
187 x_allocate_bitmap_record (struct frame *f)
189 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
190 ptrdiff_t i;
192 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
193 return ++dpyinfo->bitmaps_last;
195 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
196 if (dpyinfo->bitmaps[i].refcount == 0)
197 return i + 1;
199 dpyinfo->bitmaps =
200 xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size,
201 10, -1, sizeof *dpyinfo->bitmaps);
202 return ++dpyinfo->bitmaps_last;
205 /* Add one reference to the reference count of the bitmap with id ID. */
207 void
208 x_reference_bitmap (struct frame *f, ptrdiff_t id)
210 ++FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
213 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
215 ptrdiff_t
216 x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height)
218 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
219 ptrdiff_t id;
221 #ifdef HAVE_X_WINDOWS
222 Pixmap bitmap;
223 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
224 bits, width, height);
225 if (! bitmap)
226 return -1;
227 #endif /* HAVE_X_WINDOWS */
229 #ifdef HAVE_NTGUI
230 Pixmap bitmap;
231 bitmap = CreateBitmap (width, height,
232 FRAME_DISPLAY_INFO (XFRAME (frame))->n_planes,
233 FRAME_DISPLAY_INFO (XFRAME (frame))->n_cbits,
234 bits);
235 if (! bitmap)
236 return -1;
237 #endif /* HAVE_NTGUI */
239 #ifdef HAVE_NS
240 void *bitmap = ns_image_from_XBM (bits, width, height, 0, 0);
241 if (!bitmap)
242 return -1;
243 #endif
245 id = x_allocate_bitmap_record (f);
247 #ifdef HAVE_NS
248 dpyinfo->bitmaps[id - 1].img = bitmap;
249 dpyinfo->bitmaps[id - 1].depth = 1;
250 #endif
252 dpyinfo->bitmaps[id - 1].file = NULL;
253 dpyinfo->bitmaps[id - 1].height = height;
254 dpyinfo->bitmaps[id - 1].width = width;
255 dpyinfo->bitmaps[id - 1].refcount = 1;
257 #ifdef HAVE_X_WINDOWS
258 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
259 dpyinfo->bitmaps[id - 1].have_mask = false;
260 dpyinfo->bitmaps[id - 1].depth = 1;
261 #endif /* HAVE_X_WINDOWS */
263 #ifdef HAVE_NTGUI
264 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
265 dpyinfo->bitmaps[id - 1].hinst = NULL;
266 dpyinfo->bitmaps[id - 1].depth = 1;
267 #endif /* HAVE_NTGUI */
269 return id;
272 /* Create bitmap from file FILE for frame F. */
274 ptrdiff_t
275 x_create_bitmap_from_file (struct frame *f, Lisp_Object file)
277 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
279 #ifdef HAVE_NTGUI
280 return -1; /* W32_TODO : bitmap support */
281 #endif /* HAVE_NTGUI */
283 #ifdef HAVE_NS
284 ptrdiff_t id;
285 void *bitmap = ns_image_from_file (file);
287 if (!bitmap)
288 return -1;
291 id = x_allocate_bitmap_record (f);
292 dpyinfo->bitmaps[id - 1].img = bitmap;
293 dpyinfo->bitmaps[id - 1].refcount = 1;
294 dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
295 dpyinfo->bitmaps[id - 1].depth = 1;
296 dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap);
297 dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap);
298 return id;
299 #endif
301 #ifdef HAVE_X_WINDOWS
302 unsigned int width, height;
303 Pixmap bitmap;
304 int xhot, yhot, result;
305 ptrdiff_t id;
306 Lisp_Object found;
307 char *filename;
309 /* Look for an existing bitmap with the same name. */
310 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
312 if (dpyinfo->bitmaps[id].refcount
313 && dpyinfo->bitmaps[id].file
314 && !strcmp (dpyinfo->bitmaps[id].file, SSDATA (file)))
316 ++dpyinfo->bitmaps[id].refcount;
317 return id + 1;
321 /* Search bitmap-file-path for the file, if appropriate. */
322 if (openp (Vx_bitmap_file_path, file, Qnil, &found,
323 make_number (R_OK), false)
324 < 0)
325 return -1;
327 filename = SSDATA (found);
329 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
330 filename, &width, &height, &bitmap, &xhot, &yhot);
331 if (result != BitmapSuccess)
332 return -1;
334 id = x_allocate_bitmap_record (f);
335 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
336 dpyinfo->bitmaps[id - 1].have_mask = false;
337 dpyinfo->bitmaps[id - 1].refcount = 1;
338 dpyinfo->bitmaps[id - 1].file = xlispstrdup (file);
339 dpyinfo->bitmaps[id - 1].depth = 1;
340 dpyinfo->bitmaps[id - 1].height = height;
341 dpyinfo->bitmaps[id - 1].width = width;
343 return id;
344 #endif /* HAVE_X_WINDOWS */
347 /* Free bitmap B. */
349 static void
350 free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm)
352 #ifdef HAVE_X_WINDOWS
353 XFreePixmap (dpyinfo->display, bm->pixmap);
354 if (bm->have_mask)
355 XFreePixmap (dpyinfo->display, bm->mask);
356 #endif /* HAVE_X_WINDOWS */
358 #ifdef HAVE_NTGUI
359 DeleteObject (bm->pixmap);
360 #endif /* HAVE_NTGUI */
362 #ifdef HAVE_NS
363 ns_release_object (bm->img);
364 #endif
366 if (bm->file)
368 xfree (bm->file);
369 bm->file = NULL;
373 /* Remove reference to bitmap with id number ID. */
375 void
376 x_destroy_bitmap (struct frame *f, ptrdiff_t id)
378 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
380 if (id > 0)
382 Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1];
384 if (--bm->refcount == 0)
386 block_input ();
387 free_bitmap_record (dpyinfo, bm);
388 unblock_input ();
393 /* Free all the bitmaps for the display specified by DPYINFO. */
395 void
396 x_destroy_all_bitmaps (Display_Info *dpyinfo)
398 ptrdiff_t i;
399 Bitmap_Record *bm = dpyinfo->bitmaps;
401 for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++)
402 if (bm->refcount > 0)
403 free_bitmap_record (dpyinfo, bm);
405 dpyinfo->bitmaps_last = 0;
408 static bool x_create_x_image_and_pixmap (struct frame *, int, int, int,
409 XImagePtr *, Pixmap *);
410 static void x_destroy_x_image (XImagePtr ximg);
412 #ifdef HAVE_NTGUI
413 static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *,
414 bool, HGDIOBJ *);
415 static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC,
416 HGDIOBJ);
417 #else
418 static XImagePtr image_get_x_image (struct frame *, struct image *, bool);
419 static void image_unget_x_image (struct image *, bool, XImagePtr);
420 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
421 image_get_x_image (f, img, mask_p)
422 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
423 image_unget_x_image (img, mask_p, ximg)
424 #endif
426 #ifdef HAVE_X_WINDOWS
428 static void image_sync_to_pixmaps (struct frame *, struct image *);
430 /* Useful functions defined in the section
431 `Image type independent image structures' below. */
433 static unsigned long four_corners_best (XImagePtr ximg,
434 int *corners,
435 unsigned long width,
436 unsigned long height);
439 /* Create a mask of a bitmap. Note is this not a perfect mask.
440 It's nicer with some borders in this context */
442 void
443 x_create_bitmap_mask (struct frame *f, ptrdiff_t id)
445 Pixmap pixmap, mask;
446 XImagePtr ximg, mask_img;
447 unsigned long width, height;
448 bool result;
449 unsigned long bg;
450 unsigned long x, y, xp, xm, yp, ym;
451 GC gc;
453 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
455 if (!(id > 0))
456 return;
458 pixmap = x_bitmap_pixmap (f, id);
459 width = x_bitmap_width (f, id);
460 height = x_bitmap_height (f, id);
462 block_input ();
463 ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
464 ~0, ZPixmap);
466 if (!ximg)
468 unblock_input ();
469 return;
472 result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);
474 unblock_input ();
475 if (!result)
477 XDestroyImage (ximg);
478 return;
481 bg = four_corners_best (ximg, NULL, width, height);
483 for (y = 0; y < ximg->height; ++y)
485 for (x = 0; x < ximg->width; ++x)
487 xp = x != ximg->width - 1 ? x + 1 : 0;
488 xm = x != 0 ? x - 1 : ximg->width - 1;
489 yp = y != ximg->height - 1 ? y + 1 : 0;
490 ym = y != 0 ? y - 1 : ximg->height - 1;
491 if (XGetPixel (ximg, x, y) == bg
492 && XGetPixel (ximg, x, yp) == bg
493 && XGetPixel (ximg, x, ym) == bg
494 && XGetPixel (ximg, xp, y) == bg
495 && XGetPixel (ximg, xp, yp) == bg
496 && XGetPixel (ximg, xp, ym) == bg
497 && XGetPixel (ximg, xm, y) == bg
498 && XGetPixel (ximg, xm, yp) == bg
499 && XGetPixel (ximg, xm, ym) == bg)
500 XPutPixel (mask_img, x, y, 0);
501 else
502 XPutPixel (mask_img, x, y, 1);
506 eassert (input_blocked_p ());
507 gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL);
508 XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0,
509 width, height);
510 XFreeGC (FRAME_X_DISPLAY (f), gc);
512 dpyinfo->bitmaps[id - 1].have_mask = true;
513 dpyinfo->bitmaps[id - 1].mask = mask;
515 XDestroyImage (ximg);
516 x_destroy_x_image (mask_img);
519 #endif /* HAVE_X_WINDOWS */
521 /***********************************************************************
522 Image types
523 ***********************************************************************/
525 /* List of supported image types. Use define_image_type to add new
526 types. Use lookup_image_type to find a type for a given symbol. */
528 static struct image_type *image_types;
530 /* Forward function prototypes. */
532 static struct image_type *lookup_image_type (Lisp_Object);
533 static void x_laplace (struct frame *, struct image *);
534 static void x_emboss (struct frame *, struct image *);
535 static void x_build_heuristic_mask (struct frame *, struct image *,
536 Lisp_Object);
537 #ifdef WINDOWSNT
538 #define CACHE_IMAGE_TYPE(type, status) \
539 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
540 #else
541 #define CACHE_IMAGE_TYPE(type, status)
542 #endif
544 #define ADD_IMAGE_TYPE(type) \
545 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
547 /* Define a new image type from TYPE. This adds a copy of TYPE to
548 image_types and caches the loading status of TYPE. */
550 static struct image_type *
551 define_image_type (struct image_type *type)
553 struct image_type *p = NULL;
554 int new_type = type->type;
555 bool type_valid = true;
557 block_input ();
559 for (p = image_types; p; p = p->next)
560 if (p->type == new_type)
561 goto done;
563 if (type->init)
565 #if defined HAVE_NTGUI && defined WINDOWSNT
566 /* If we failed to load the library before, don't try again. */
567 Lisp_Object tested = Fassq (builtin_lisp_symbol (new_type),
568 Vlibrary_cache);
569 if (CONSP (tested) && NILP (XCDR (tested)))
570 type_valid = false;
571 else
572 #endif
574 type_valid = type->init ();
575 CACHE_IMAGE_TYPE (builtin_lisp_symbol (new_type),
576 type_valid ? Qt : Qnil);
580 if (type_valid)
582 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
583 The initialized data segment is read-only. */
584 p = xmalloc (sizeof *p);
585 *p = *type;
586 p->next = image_types;
587 image_types = p;
590 done:
591 unblock_input ();
592 return p;
596 /* Value is true if OBJECT is a valid Lisp image specification. A
597 valid image specification is a list whose car is the symbol
598 `image', and whose rest is a property list. The property list must
599 contain a value for key `:type'. That value must be the name of a
600 supported image type. The rest of the property list depends on the
601 image type. */
603 bool
604 valid_image_p (Lisp_Object object)
606 bool valid_p = 0;
608 if (IMAGEP (object))
610 Lisp_Object tem;
612 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
613 if (EQ (XCAR (tem), QCtype))
615 tem = XCDR (tem);
616 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
618 struct image_type *type;
619 type = lookup_image_type (XCAR (tem));
620 if (type)
621 valid_p = type->valid_p (object);
624 break;
628 return valid_p;
632 /* Log error message with format string FORMAT and argument ARG.
633 Signaling an error, e.g. when an image cannot be loaded, is not a
634 good idea because this would interrupt redisplay, and the error
635 message display would lead to another redisplay. This function
636 therefore simply displays a message. */
638 static void
639 image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2)
641 add_to_log (format, arg1, arg2);
646 /***********************************************************************
647 Image specifications
648 ***********************************************************************/
650 enum image_value_type
652 IMAGE_DONT_CHECK_VALUE_TYPE,
653 IMAGE_STRING_VALUE,
654 IMAGE_STRING_OR_NIL_VALUE,
655 IMAGE_SYMBOL_VALUE,
656 IMAGE_POSITIVE_INTEGER_VALUE,
657 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR,
658 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
659 IMAGE_ASCENT_VALUE,
660 IMAGE_INTEGER_VALUE,
661 IMAGE_FUNCTION_VALUE,
662 IMAGE_NUMBER_VALUE,
663 IMAGE_BOOL_VALUE
666 /* Structure used when parsing image specifications. */
668 struct image_keyword
670 /* Name of keyword. */
671 const char *name;
673 /* The type of value allowed. */
674 enum image_value_type type;
676 /* True means key must be present. */
677 bool mandatory_p;
679 /* Used to recognize duplicate keywords in a property list. */
680 int count;
682 /* The value that was found. */
683 Lisp_Object value;
687 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
688 has the format (image KEYWORD VALUE ...). One of the keyword/
689 value pairs must be `:type TYPE'. KEYWORDS is a vector of
690 image_keywords structures of size NKEYWORDS describing other
691 allowed keyword/value pairs. Value is true if SPEC is valid. */
693 static bool
694 parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
695 int nkeywords, Lisp_Object type)
697 int i;
698 Lisp_Object plist;
700 if (!IMAGEP (spec))
701 return 0;
703 plist = XCDR (spec);
704 while (CONSP (plist))
706 Lisp_Object key, value;
708 /* First element of a pair must be a symbol. */
709 key = XCAR (plist);
710 plist = XCDR (plist);
711 if (!SYMBOLP (key))
712 return 0;
714 /* There must follow a value. */
715 if (!CONSP (plist))
716 return 0;
717 value = XCAR (plist);
718 plist = XCDR (plist);
720 /* Find key in KEYWORDS. Error if not found. */
721 for (i = 0; i < nkeywords; ++i)
722 if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0)
723 break;
725 if (i == nkeywords)
726 continue;
728 /* Record that we recognized the keyword. If a keywords
729 was found more than once, it's an error. */
730 keywords[i].value = value;
731 if (keywords[i].count > 1)
732 return 0;
733 ++keywords[i].count;
735 /* Check type of value against allowed type. */
736 switch (keywords[i].type)
738 case IMAGE_STRING_VALUE:
739 if (!STRINGP (value))
740 return 0;
741 break;
743 case IMAGE_STRING_OR_NIL_VALUE:
744 if (!STRINGP (value) && !NILP (value))
745 return 0;
746 break;
748 case IMAGE_SYMBOL_VALUE:
749 if (!SYMBOLP (value))
750 return 0;
751 break;
753 case IMAGE_POSITIVE_INTEGER_VALUE:
754 if (! RANGED_INTEGERP (1, value, INT_MAX))
755 return 0;
756 break;
758 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR:
759 if (RANGED_INTEGERP (0, value, INT_MAX))
760 break;
761 if (CONSP (value)
762 && RANGED_INTEGERP (0, XCAR (value), INT_MAX)
763 && RANGED_INTEGERP (0, XCDR (value), INT_MAX))
764 break;
765 return 0;
767 case IMAGE_ASCENT_VALUE:
768 if (SYMBOLP (value) && EQ (value, Qcenter))
769 break;
770 else if (RANGED_INTEGERP (0, value, 100))
771 break;
772 return 0;
774 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
775 /* Unlike the other integer-related cases, this one does not
776 verify that VALUE fits in 'int'. This is because callers
777 want EMACS_INT. */
778 if (!INTEGERP (value) || XINT (value) < 0)
779 return 0;
780 break;
782 case IMAGE_DONT_CHECK_VALUE_TYPE:
783 break;
785 case IMAGE_FUNCTION_VALUE:
786 value = indirect_function (value);
787 if (!NILP (Ffunctionp (value)))
788 break;
789 return 0;
791 case IMAGE_NUMBER_VALUE:
792 if (!INTEGERP (value) && !FLOATP (value))
793 return 0;
794 break;
796 case IMAGE_INTEGER_VALUE:
797 if (! TYPE_RANGED_INTEGERP (int, value))
798 return 0;
799 break;
801 case IMAGE_BOOL_VALUE:
802 if (!NILP (value) && !EQ (value, Qt))
803 return 0;
804 break;
806 default:
807 emacs_abort ();
808 break;
811 if (EQ (key, QCtype) && !EQ (type, value))
812 return 0;
815 /* Check that all mandatory fields are present. */
816 for (i = 0; i < nkeywords; ++i)
817 if (keywords[i].mandatory_p && keywords[i].count == 0)
818 return 0;
820 return NILP (plist);
824 /* Return the value of KEY in image specification SPEC. Value is nil
825 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
826 was found in SPEC. */
828 static Lisp_Object
829 image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found)
831 Lisp_Object tail;
833 eassert (valid_image_p (spec));
835 for (tail = XCDR (spec);
836 CONSP (tail) && CONSP (XCDR (tail));
837 tail = XCDR (XCDR (tail)))
839 if (EQ (XCAR (tail), key))
841 if (found)
842 *found = 1;
843 return XCAR (XCDR (tail));
847 if (found)
848 *found = 0;
849 return Qnil;
853 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
854 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
855 PIXELS non-nil means return the size in pixels, otherwise return the
856 size in canonical character units.
857 FRAME is the frame on which the image will be displayed. FRAME nil
858 or omitted means use the selected frame. */)
859 (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame)
861 Lisp_Object size;
863 size = Qnil;
864 if (valid_image_p (spec))
866 struct frame *f = decode_window_system_frame (frame);
867 ptrdiff_t id = lookup_image (f, spec);
868 struct image *img = IMAGE_FROM_ID (f, id);
869 int width = img->width + 2 * img->hmargin;
870 int height = img->height + 2 * img->vmargin;
872 if (NILP (pixels))
873 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
874 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
875 else
876 size = Fcons (make_number (width), make_number (height));
878 else
879 error ("Invalid image specification");
881 return size;
885 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
886 doc: /* Return t if image SPEC has a mask bitmap.
887 FRAME is the frame on which the image will be displayed. FRAME nil
888 or omitted means use the selected frame. */)
889 (Lisp_Object spec, Lisp_Object frame)
891 Lisp_Object mask;
893 mask = Qnil;
894 if (valid_image_p (spec))
896 struct frame *f = decode_window_system_frame (frame);
897 ptrdiff_t id = lookup_image (f, spec);
898 struct image *img = IMAGE_FROM_ID (f, id);
899 if (img->mask)
900 mask = Qt;
902 else
903 error ("Invalid image specification");
905 return mask;
908 DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0,
909 doc: /* Return metadata for image SPEC.
910 FRAME is the frame on which the image will be displayed. FRAME nil
911 or omitted means use the selected frame. */)
912 (Lisp_Object spec, Lisp_Object frame)
914 Lisp_Object ext;
916 ext = Qnil;
917 if (valid_image_p (spec))
919 struct frame *f = decode_window_system_frame (frame);
920 ptrdiff_t id = lookup_image (f, spec);
921 struct image *img = IMAGE_FROM_ID (f, id);
922 ext = img->lisp_data;
925 return ext;
929 /***********************************************************************
930 Image type independent image structures
931 ***********************************************************************/
933 #define MAX_IMAGE_SIZE 10.0
934 /* Allocate and return a new image structure for image specification
935 SPEC. SPEC has a hash value of HASH. */
937 static struct image *
938 make_image (Lisp_Object spec, EMACS_UINT hash)
940 struct image *img = xzalloc (sizeof *img);
941 Lisp_Object file = image_spec_value (spec, QCfile, NULL);
943 eassert (valid_image_p (spec));
944 img->dependencies = NILP (file) ? Qnil : list1 (file);
945 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
946 eassert (img->type != NULL);
947 img->spec = spec;
948 img->lisp_data = Qnil;
949 img->ascent = DEFAULT_IMAGE_ASCENT;
950 img->hash = hash;
951 img->corners[BOT_CORNER] = -1; /* Full image */
952 return img;
956 /* Free image IMG which was used on frame F, including its resources. */
958 static void
959 free_image (struct frame *f, struct image *img)
961 if (img)
963 struct image_cache *c = FRAME_IMAGE_CACHE (f);
965 /* Remove IMG from the hash table of its cache. */
966 if (img->prev)
967 img->prev->next = img->next;
968 else
969 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
971 if (img->next)
972 img->next->prev = img->prev;
974 c->images[img->id] = NULL;
976 /* Windows NT redefines 'free', but in this file, we need to
977 avoid the redefinition. */
978 #ifdef WINDOWSNT
979 #undef free
980 #endif
981 /* Free resources, then free IMG. */
982 img->type->free (f, img);
983 xfree (img);
987 /* Return true if the given widths and heights are valid for display. */
989 static bool
990 check_image_size (struct frame *f, int width, int height)
992 int w, h;
994 if (width <= 0 || height <= 0)
995 return 0;
997 if (INTEGERP (Vmax_image_size))
998 return (width <= XINT (Vmax_image_size)
999 && height <= XINT (Vmax_image_size));
1000 else if (FLOATP (Vmax_image_size))
1002 if (f != NULL)
1004 w = FRAME_PIXEL_WIDTH (f);
1005 h = FRAME_PIXEL_HEIGHT (f);
1007 else
1008 w = h = 1024; /* Arbitrary size for unknown frame. */
1009 return (width <= XFLOAT_DATA (Vmax_image_size) * w
1010 && height <= XFLOAT_DATA (Vmax_image_size) * h);
1012 else
1013 return 1;
1016 /* Prepare image IMG for display on frame F. Must be called before
1017 drawing an image. */
1019 void
1020 prepare_image_for_display (struct frame *f, struct image *img)
1022 /* We're about to display IMG, so set its timestamp to `now'. */
1023 img->timestamp = current_timespec ();
1025 #ifndef USE_CAIRO
1026 /* If IMG doesn't have a pixmap yet, load it now, using the image
1027 type dependent loader function. */
1028 if (img->pixmap == NO_PIXMAP && !img->load_failed_p)
1029 img->load_failed_p = ! img->type->load (f, img);
1031 #ifdef HAVE_X_WINDOWS
1032 if (!img->load_failed_p)
1034 block_input ();
1035 image_sync_to_pixmaps (f, img);
1036 unblock_input ();
1038 #endif
1039 #endif
1043 /* Value is the number of pixels for the ascent of image IMG when
1044 drawn in face FACE. */
1047 image_ascent (struct image *img, struct face *face, struct glyph_slice *slice)
1049 int height;
1050 int ascent;
1052 if (slice->height == img->height)
1053 height = img->height + img->vmargin;
1054 else if (slice->y == 0)
1055 height = slice->height + img->vmargin;
1056 else
1057 height = slice->height;
1059 if (img->ascent == CENTERED_IMAGE_ASCENT)
1061 if (face->font)
1063 #ifdef HAVE_NTGUI
1064 /* W32 specific version. Why?. ++kfs */
1065 ascent = height / 2 - (FONT_DESCENT (face->font)
1066 - FONT_BASE (face->font)) / 2;
1067 #else
1068 /* This expression is arranged so that if the image can't be
1069 exactly centered, it will be moved slightly up. This is
1070 because a typical font is `top-heavy' (due to the presence
1071 uppercase letters), so the image placement should err towards
1072 being top-heavy too. It also just generally looks better. */
1073 ascent = (height + FONT_BASE (face->font)
1074 - FONT_DESCENT (face->font) + 1) / 2;
1075 #endif /* HAVE_NTGUI */
1077 else
1078 ascent = height / 2;
1080 else
1081 ascent = height * (img->ascent / 100.0);
1083 return ascent;
1086 #ifdef USE_CAIRO
1087 static uint32_t
1088 xcolor_to_argb32 (XColor xc)
1090 return (0xff << 24) | ((xc.red / 256) << 16)
1091 | ((xc.green / 256) << 8) | (xc.blue / 256);
1094 static uint32_t
1095 get_spec_bg_or_alpha_as_argb (struct image *img,
1096 struct frame *f)
1098 uint32_t bgcolor = 0;
1099 XColor xbgcolor;
1100 Lisp_Object bg = image_spec_value (img->spec, QCbackground, NULL);
1102 if (STRINGP (bg) && XParseColor (FRAME_X_DISPLAY (f),
1103 FRAME_X_COLORMAP (f),
1104 SSDATA (bg),
1105 &xbgcolor))
1106 bgcolor = xcolor_to_argb32 (xbgcolor);
1108 return bgcolor;
1111 static void
1112 create_cairo_image_surface (struct image *img,
1113 unsigned char *data,
1114 int width,
1115 int height)
1117 cairo_surface_t *surface;
1118 cairo_format_t format = CAIRO_FORMAT_ARGB32;
1119 int stride = cairo_format_stride_for_width (format, width);
1120 surface = cairo_image_surface_create_for_data (data,
1121 format,
1122 width,
1123 height,
1124 stride);
1125 img->width = width;
1126 img->height = height;
1127 img->cr_data = surface;
1128 img->cr_data2 = data;
1129 img->pixmap = 0;
1131 #endif
1135 /* Image background colors. */
1137 /* Find the "best" corner color of a bitmap.
1138 On W32, XIMG is assumed to a device context with the bitmap selected. */
1140 static RGB_PIXEL_COLOR
1141 four_corners_best (XImagePtr_or_DC ximg, int *corners,
1142 unsigned long width, unsigned long height)
1144 RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0);
1145 int i, best_count;
1147 if (corners && corners[BOT_CORNER] >= 0)
1149 /* Get the colors at the corner_pixels of ximg. */
1150 corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]);
1151 corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]);
1152 corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1);
1153 corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1);
1155 else
1157 /* Get the colors at the corner_pixels of ximg. */
1158 corner_pixels[0] = GET_PIXEL (ximg, 0, 0);
1159 corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0);
1160 corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1);
1161 corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1);
1163 /* Choose the most frequently found color as background. */
1164 for (i = best_count = 0; i < 4; ++i)
1166 int j, n;
1168 for (j = n = 0; j < 4; ++j)
1169 if (corner_pixels[i] == corner_pixels[j])
1170 ++n;
1172 if (n > best_count)
1173 best = corner_pixels[i], best_count = n;
1176 return best;
1179 /* Portability macros */
1181 #ifdef HAVE_NTGUI
1183 #define Free_Pixmap(display, pixmap) \
1184 DeleteObject (pixmap)
1186 #elif defined (HAVE_NS)
1188 #define Free_Pixmap(display, pixmap) \
1189 ns_release_object (pixmap)
1191 #else
1193 #define Free_Pixmap(display, pixmap) \
1194 XFreePixmap (display, pixmap)
1196 #endif /* !HAVE_NTGUI && !HAVE_NS */
1199 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1200 it is guessed heuristically. If non-zero, XIMG is an existing
1201 XImage object (or device context with the image selected on W32) to
1202 use for the heuristic. */
1204 RGB_PIXEL_COLOR
1205 image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg)
1207 if (! img->background_valid)
1208 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1210 bool free_ximg = !ximg;
1211 #ifdef HAVE_NTGUI
1212 HGDIOBJ prev;
1213 #endif /* HAVE_NTGUI */
1215 if (free_ximg)
1216 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
1218 img->background = four_corners_best (ximg, img->corners, img->width, img->height);
1220 if (free_ximg)
1221 image_unget_x_image_or_dc (img, 0, ximg, prev);
1223 img->background_valid = 1;
1226 return img->background;
1229 /* Return the `background_transparent' field of IMG. If IMG doesn't
1230 have one yet, it is guessed heuristically. If non-zero, MASK is an
1231 existing XImage object to use for the heuristic. */
1234 image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask)
1236 if (! img->background_transparent_valid)
1237 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1239 if (img->mask)
1241 bool free_mask = !mask;
1242 #ifdef HAVE_NTGUI
1243 HGDIOBJ prev;
1244 #endif /* HAVE_NTGUI */
1246 if (free_mask)
1247 mask = image_get_x_image_or_dc (f, img, 1, &prev);
1249 img->background_transparent
1250 = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN);
1252 if (free_mask)
1253 image_unget_x_image_or_dc (img, 1, mask, prev);
1255 else
1256 img->background_transparent = 0;
1258 img->background_transparent_valid = 1;
1261 return img->background_transparent;
1264 #if defined (HAVE_PNG) || defined (HAVE_NS) \
1265 || defined (HAVE_IMAGEMAGICK) || defined (HAVE_RSVG)
1267 /* Store F's background color into *BGCOLOR. */
1268 static void
1269 x_query_frame_background_color (struct frame *f, XColor *bgcolor)
1271 #ifndef HAVE_NS
1272 bgcolor->pixel = FRAME_BACKGROUND_PIXEL (f);
1273 x_query_color (f, bgcolor);
1274 #else
1275 ns_query_color (FRAME_BACKGROUND_COLOR (f), bgcolor, 1);
1276 #endif
1279 #endif /* HAVE_PNG || HAVE_NS || HAVE_IMAGEMAGICK || HAVE_RSVG */
1281 /***********************************************************************
1282 Helper functions for X image types
1283 ***********************************************************************/
1285 /* Clear X resources of image IMG on frame F according to FLAGS.
1286 FLAGS is bitwise-or of the following masks:
1287 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1288 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1289 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1290 any. */
1292 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1293 #define CLEAR_IMAGE_MASK (1 << 1)
1294 #define CLEAR_IMAGE_COLORS (1 << 2)
1296 static void
1297 x_clear_image_1 (struct frame *f, struct image *img, int flags)
1299 if (flags & CLEAR_IMAGE_PIXMAP)
1301 if (img->pixmap)
1303 Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
1304 img->pixmap = NO_PIXMAP;
1305 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1306 img->background_valid = 0;
1308 #ifdef HAVE_X_WINDOWS
1309 if (img->ximg)
1311 x_destroy_x_image (img->ximg);
1312 img->ximg = NULL;
1313 img->background_valid = 0;
1315 #endif
1318 if (flags & CLEAR_IMAGE_MASK)
1320 if (img->mask)
1322 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
1323 img->mask = NO_PIXMAP;
1324 img->background_transparent_valid = 0;
1326 #ifdef HAVE_X_WINDOWS
1327 if (img->mask_img)
1329 x_destroy_x_image (img->mask_img);
1330 img->mask_img = NULL;
1331 img->background_transparent_valid = 0;
1333 #endif
1336 if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors)
1338 /* W32_TODO: color table support. */
1339 #ifdef HAVE_X_WINDOWS
1340 x_free_colors (f, img->colors, img->ncolors);
1341 #endif /* HAVE_X_WINDOWS */
1342 xfree (img->colors);
1343 img->colors = NULL;
1344 img->ncolors = 0;
1349 /* Free X resources of image IMG which is used on frame F. */
1351 static void
1352 x_clear_image (struct frame *f, struct image *img)
1354 block_input ();
1355 #ifdef USE_CAIRO
1356 if (img->cr_data)
1357 cairo_surface_destroy ((cairo_surface_t *)img->cr_data);
1358 if (img->cr_data2) xfree (img->cr_data2);
1359 #endif
1360 x_clear_image_1 (f, img,
1361 CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS);
1362 unblock_input ();
1366 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1367 cannot be allocated, use DFLT. Add a newly allocated color to
1368 IMG->colors, so that it can be freed again. Value is the pixel
1369 color. */
1371 static unsigned long
1372 x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name,
1373 unsigned long dflt)
1375 XColor color;
1376 unsigned long result;
1378 eassert (STRINGP (color_name));
1380 if (x_defined_color (f, SSDATA (color_name), &color, 1)
1381 && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors,
1382 INT_MAX))
1384 /* This isn't called frequently so we get away with simply
1385 reallocating the color vector to the needed size, here. */
1386 ptrdiff_t ncolors = img->ncolors + 1;
1387 img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors);
1388 img->colors[ncolors - 1] = color.pixel;
1389 img->ncolors = ncolors;
1390 result = color.pixel;
1392 else
1393 result = dflt;
1395 return result;
1400 /***********************************************************************
1401 Image Cache
1402 ***********************************************************************/
1404 static void cache_image (struct frame *f, struct image *img);
1406 /* Return a new, initialized image cache that is allocated from the
1407 heap. Call free_image_cache to free an image cache. */
1409 struct image_cache *
1410 make_image_cache (void)
1412 struct image_cache *c = xmalloc (sizeof *c);
1414 c->size = 50;
1415 c->used = c->refcount = 0;
1416 c->images = xmalloc (c->size * sizeof *c->images);
1417 c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
1418 return c;
1422 /* Find an image matching SPEC in the cache, and return it. If no
1423 image is found, return NULL. */
1424 static struct image *
1425 search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash)
1427 struct image *img;
1428 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1429 int i = hash % IMAGE_CACHE_BUCKETS_SIZE;
1431 if (!c) return NULL;
1433 /* If the image spec does not specify a background color, the cached
1434 image must have the same background color as the current frame.
1435 The foreground color must also match, for the sake of monochrome
1436 images.
1438 In fact, we could ignore the foreground color matching condition
1439 for color images, or if the image spec specifies :foreground;
1440 similarly we could ignore the background color matching condition
1441 for formats that don't use transparency (such as jpeg), or if the
1442 image spec specifies :background. However, the extra memory
1443 usage is probably negligible in practice, so we don't bother. */
1445 for (img = c->buckets[i]; img; img = img->next)
1446 if (img->hash == hash
1447 && !NILP (Fequal (img->spec, spec))
1448 && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f)
1449 && img->frame_background == FRAME_BACKGROUND_PIXEL (f))
1450 break;
1451 return img;
1455 /* Search frame F for an image with spec SPEC, and free it. */
1457 static void
1458 uncache_image (struct frame *f, Lisp_Object spec)
1460 struct image *img = search_image_cache (f, spec, sxhash (spec, 0));
1461 if (img)
1463 free_image (f, img);
1464 /* As display glyphs may still be referring to the image ID, we
1465 must garbage the frame (Bug#6426). */
1466 SET_FRAME_GARBAGED (f);
1471 /* Free image cache of frame F. Be aware that X frames share images
1472 caches. */
1474 void
1475 free_image_cache (struct frame *f)
1477 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1478 if (c)
1480 ptrdiff_t i;
1482 /* Cache should not be referenced by any frame when freed. */
1483 eassert (c->refcount == 0);
1485 for (i = 0; i < c->used; ++i)
1486 free_image (f, c->images[i]);
1487 xfree (c->images);
1488 xfree (c->buckets);
1489 xfree (c);
1490 FRAME_IMAGE_CACHE (f) = NULL;
1495 /* Clear image cache of frame F. FILTER=t means free all images.
1496 FILTER=nil means clear only images that haven't been
1497 displayed for some time.
1498 Else, only free the images which have FILTER in their `dependencies'.
1499 Should be called from time to time to reduce the number of loaded images.
1500 If image-cache-eviction-delay is non-nil, this frees images in the cache
1501 which weren't displayed for at least that many seconds. */
1503 static void
1504 clear_image_cache (struct frame *f, Lisp_Object filter)
1506 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1508 if (c)
1510 ptrdiff_t i, nfreed = 0;
1512 /* Block input so that we won't be interrupted by a SIGIO
1513 while being in an inconsistent state. */
1514 block_input ();
1516 if (!NILP (filter))
1518 /* Filter image cache. */
1519 for (i = 0; i < c->used; ++i)
1521 struct image *img = c->images[i];
1522 if (img && (EQ (Qt, filter)
1523 || !NILP (Fmember (filter, img->dependencies))))
1525 free_image (f, img);
1526 ++nfreed;
1530 else if (INTEGERP (Vimage_cache_eviction_delay))
1532 /* Free cache based on timestamp. */
1533 struct timespec old, t;
1534 double delay;
1535 ptrdiff_t nimages = 0;
1537 for (i = 0; i < c->used; ++i)
1538 if (c->images[i])
1539 nimages++;
1541 /* If the number of cached images has grown unusually large,
1542 decrease the cache eviction delay (Bug#6230). */
1543 delay = XINT (Vimage_cache_eviction_delay);
1544 if (nimages > 40)
1545 delay = 1600 * delay / nimages / nimages;
1546 delay = max (delay, 1);
1548 t = current_timespec ();
1549 old = timespec_sub (t, dtotimespec (delay));
1551 for (i = 0; i < c->used; ++i)
1553 struct image *img = c->images[i];
1554 if (img && timespec_cmp (img->timestamp, old) < 0)
1556 free_image (f, img);
1557 ++nfreed;
1562 /* We may be clearing the image cache because, for example,
1563 Emacs was iconified for a longer period of time. In that
1564 case, current matrices may still contain references to
1565 images freed above. So, clear these matrices. */
1566 if (nfreed)
1568 Lisp_Object tail, frame;
1570 FOR_EACH_FRAME (tail, frame)
1572 struct frame *fr = XFRAME (frame);
1573 if (FRAME_IMAGE_CACHE (fr) == c)
1574 clear_current_matrices (fr);
1577 windows_or_buffers_changed = 19;
1580 unblock_input ();
1584 void
1585 clear_image_caches (Lisp_Object filter)
1587 /* FIXME: We want to do
1588 * struct terminal *t;
1589 * for (t = terminal_list; t; t = t->next_terminal)
1590 * clear_image_cache (t, filter); */
1591 Lisp_Object tail, frame;
1592 FOR_EACH_FRAME (tail, frame)
1593 if (FRAME_WINDOW_P (XFRAME (frame)))
1594 clear_image_cache (XFRAME (frame), filter);
1597 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
1598 0, 1, 0,
1599 doc: /* Clear the image cache.
1600 FILTER nil or a frame means clear all images in the selected frame.
1601 FILTER t means clear the image caches of all frames.
1602 Anything else, means only clear those images which refer to FILTER,
1603 which is then usually a filename. */)
1604 (Lisp_Object filter)
1606 if (!(EQ (filter, Qnil) || FRAMEP (filter)))
1607 clear_image_caches (filter);
1608 else
1609 clear_image_cache (decode_window_system_frame (filter), Qt);
1611 return Qnil;
1615 DEFUN ("image-flush", Fimage_flush, Simage_flush,
1616 1, 2, 0,
1617 doc: /* Flush the image with specification SPEC on frame FRAME.
1618 This removes the image from the Emacs image cache. If SPEC specifies
1619 an image file, the next redisplay of this image will read from the
1620 current contents of that file.
1622 FRAME nil or omitted means use the selected frame.
1623 FRAME t means refresh the image on all frames. */)
1624 (Lisp_Object spec, Lisp_Object frame)
1626 if (!valid_image_p (spec))
1627 error ("Invalid image specification");
1629 if (EQ (frame, Qt))
1631 Lisp_Object tail;
1632 FOR_EACH_FRAME (tail, frame)
1634 struct frame *f = XFRAME (frame);
1635 if (FRAME_WINDOW_P (f))
1636 uncache_image (f, spec);
1639 else
1640 uncache_image (decode_window_system_frame (frame), spec);
1642 return Qnil;
1646 /* Compute masks and transform image IMG on frame F, as specified
1647 by the image's specification, */
1649 static void
1650 postprocess_image (struct frame *f, struct image *img)
1652 /* Manipulation of the image's mask. */
1653 if (img->pixmap)
1655 Lisp_Object conversion, spec;
1656 Lisp_Object mask;
1658 spec = img->spec;
1660 /* `:heuristic-mask t'
1661 `:mask heuristic'
1662 means build a mask heuristically.
1663 `:heuristic-mask (R G B)'
1664 `:mask (heuristic (R G B))'
1665 means build a mask from color (R G B) in the
1666 image.
1667 `:mask nil'
1668 means remove a mask, if any. */
1670 mask = image_spec_value (spec, QCheuristic_mask, NULL);
1671 if (!NILP (mask))
1672 x_build_heuristic_mask (f, img, mask);
1673 else
1675 bool found_p;
1677 mask = image_spec_value (spec, QCmask, &found_p);
1679 if (EQ (mask, Qheuristic))
1680 x_build_heuristic_mask (f, img, Qt);
1681 else if (CONSP (mask)
1682 && EQ (XCAR (mask), Qheuristic))
1684 if (CONSP (XCDR (mask)))
1685 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
1686 else
1687 x_build_heuristic_mask (f, img, XCDR (mask));
1689 else if (NILP (mask) && found_p && img->mask)
1690 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
1694 /* Should we apply an image transformation algorithm? */
1695 conversion = image_spec_value (spec, QCconversion, NULL);
1696 if (EQ (conversion, Qdisabled))
1697 x_disable_image (f, img);
1698 else if (EQ (conversion, Qlaplace))
1699 x_laplace (f, img);
1700 else if (EQ (conversion, Qemboss))
1701 x_emboss (f, img);
1702 else if (CONSP (conversion)
1703 && EQ (XCAR (conversion), Qedge_detection))
1705 Lisp_Object tem;
1706 tem = XCDR (conversion);
1707 if (CONSP (tem))
1708 x_edge_detection (f, img,
1709 Fplist_get (tem, QCmatrix),
1710 Fplist_get (tem, QCcolor_adjustment));
1716 /* Return the id of image with Lisp specification SPEC on frame F.
1717 SPEC must be a valid Lisp image specification (see valid_image_p). */
1719 ptrdiff_t
1720 lookup_image (struct frame *f, Lisp_Object spec)
1722 struct image *img;
1723 EMACS_UINT hash;
1725 /* F must be a window-system frame, and SPEC must be a valid image
1726 specification. */
1727 eassert (FRAME_WINDOW_P (f));
1728 eassert (valid_image_p (spec));
1730 /* Look up SPEC in the hash table of the image cache. */
1731 hash = sxhash (spec, 0);
1732 img = search_image_cache (f, spec, hash);
1733 if (img && img->load_failed_p)
1735 free_image (f, img);
1736 img = NULL;
1739 /* If not found, create a new image and cache it. */
1740 if (img == NULL)
1742 block_input ();
1743 img = make_image (spec, hash);
1744 cache_image (f, img);
1745 img->load_failed_p = ! img->type->load (f, img);
1746 img->frame_foreground = FRAME_FOREGROUND_PIXEL (f);
1747 img->frame_background = FRAME_BACKGROUND_PIXEL (f);
1749 /* If we can't load the image, and we don't have a width and
1750 height, use some arbitrary width and height so that we can
1751 draw a rectangle for it. */
1752 if (img->load_failed_p)
1754 Lisp_Object value;
1756 value = image_spec_value (spec, QCwidth, NULL);
1757 img->width = (INTEGERP (value)
1758 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
1759 value = image_spec_value (spec, QCheight, NULL);
1760 img->height = (INTEGERP (value)
1761 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
1763 else
1765 /* Handle image type independent image attributes
1766 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1767 `:background COLOR'. */
1768 Lisp_Object ascent, margin, relief, bg;
1769 int relief_bound;
1771 ascent = image_spec_value (spec, QCascent, NULL);
1772 if (INTEGERP (ascent))
1773 img->ascent = XFASTINT (ascent);
1774 else if (EQ (ascent, Qcenter))
1775 img->ascent = CENTERED_IMAGE_ASCENT;
1777 margin = image_spec_value (spec, QCmargin, NULL);
1778 if (INTEGERP (margin))
1779 img->vmargin = img->hmargin = XFASTINT (margin);
1780 else if (CONSP (margin))
1782 img->hmargin = XFASTINT (XCAR (margin));
1783 img->vmargin = XFASTINT (XCDR (margin));
1786 relief = image_spec_value (spec, QCrelief, NULL);
1787 relief_bound = INT_MAX - max (img->hmargin, img->vmargin);
1788 if (RANGED_INTEGERP (- relief_bound, relief, relief_bound))
1790 img->relief = XINT (relief);
1791 img->hmargin += eabs (img->relief);
1792 img->vmargin += eabs (img->relief);
1795 if (! img->background_valid)
1797 bg = image_spec_value (img->spec, QCbackground, NULL);
1798 if (!NILP (bg))
1800 img->background
1801 = x_alloc_image_color (f, img, bg,
1802 FRAME_BACKGROUND_PIXEL (f));
1803 img->background_valid = 1;
1807 /* Do image transformations and compute masks, unless we
1808 don't have the image yet. */
1809 if (!EQ (builtin_lisp_symbol (img->type->type), Qpostscript))
1810 postprocess_image (f, img);
1813 unblock_input ();
1816 /* We're using IMG, so set its timestamp to `now'. */
1817 img->timestamp = current_timespec ();
1819 /* Value is the image id. */
1820 return img->id;
1824 /* Cache image IMG in the image cache of frame F. */
1826 static void
1827 cache_image (struct frame *f, struct image *img)
1829 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1830 ptrdiff_t i;
1832 /* Find a free slot in c->images. */
1833 for (i = 0; i < c->used; ++i)
1834 if (c->images[i] == NULL)
1835 break;
1837 /* If no free slot found, maybe enlarge c->images. */
1838 if (i == c->used && c->used == c->size)
1839 c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images);
1841 /* Add IMG to c->images, and assign IMG an id. */
1842 c->images[i] = img;
1843 img->id = i;
1844 if (i == c->used)
1845 ++c->used;
1847 /* Add IMG to the cache's hash table. */
1848 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
1849 img->next = c->buckets[i];
1850 if (img->next)
1851 img->next->prev = img;
1852 img->prev = NULL;
1853 c->buckets[i] = img;
1857 /* Call FN on every image in the image cache of frame F. Used to mark
1858 Lisp Objects in the image cache. */
1860 /* Mark Lisp objects in image IMG. */
1862 static void
1863 mark_image (struct image *img)
1865 mark_object (img->spec);
1866 mark_object (img->dependencies);
1868 if (!NILP (img->lisp_data))
1869 mark_object (img->lisp_data);
1873 void
1874 mark_image_cache (struct image_cache *c)
1876 if (c)
1878 ptrdiff_t i;
1879 for (i = 0; i < c->used; ++i)
1880 if (c->images[i])
1881 mark_image (c->images[i]);
1887 /***********************************************************************
1888 X / NS / W32 support code
1889 ***********************************************************************/
1891 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1892 windowing system.
1893 WIDTH and HEIGHT must both be positive.
1894 If XIMG is null, assume it is a bitmap. */
1895 static bool
1896 x_check_image_size (XImagePtr ximg, int width, int height)
1898 #ifdef HAVE_X_WINDOWS
1899 /* Respect Xlib's limits: it cannot deal with images that have more
1900 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1901 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1902 enum
1904 XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX),
1905 X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX))
1908 int bitmap_pad, depth, bytes_per_line;
1909 if (ximg)
1911 bitmap_pad = ximg->bitmap_pad;
1912 depth = ximg->depth;
1913 bytes_per_line = ximg->bytes_per_line;
1915 else
1917 bitmap_pad = 8;
1918 depth = 1;
1919 bytes_per_line = (width >> 3) + ((width & 7) != 0);
1921 return (width <= (INT_MAX - (bitmap_pad - 1)) / depth
1922 && height <= X_IMAGE_BYTES_MAX / bytes_per_line);
1923 #else
1924 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1925 For now, assume that every image size is allowed on these systems. */
1926 return 1;
1927 #endif
1930 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1931 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1932 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1933 via xmalloc. Print error messages via image_error if an error
1934 occurs. Value is true if successful.
1936 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1937 should indicate the bit depth of the image. */
1939 static bool
1940 x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth,
1941 XImagePtr *ximg, Pixmap *pixmap)
1943 #ifdef HAVE_X_WINDOWS
1944 Display *display = FRAME_X_DISPLAY (f);
1945 Window window = FRAME_X_WINDOW (f);
1946 Screen *screen = FRAME_X_SCREEN (f);
1948 eassert (input_blocked_p ());
1950 if (depth <= 0)
1951 depth = DefaultDepthOfScreen (screen);
1952 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
1953 depth, ZPixmap, 0, NULL, width, height,
1954 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
1955 if (*ximg == NULL)
1957 image_error ("Unable to allocate X image", Qnil, Qnil);
1958 return 0;
1961 if (! x_check_image_size (*ximg, width, height))
1963 x_destroy_x_image (*ximg);
1964 *ximg = NULL;
1965 image_error ("Image too large (%dx%d)",
1966 make_number (width), make_number (height));
1967 return 0;
1970 /* Allocate image raster. */
1971 (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height);
1973 /* Allocate a pixmap of the same size. */
1974 *pixmap = XCreatePixmap (display, window, width, height, depth);
1975 if (*pixmap == NO_PIXMAP)
1977 x_destroy_x_image (*ximg);
1978 *ximg = NULL;
1979 image_error ("Unable to create X pixmap", Qnil, Qnil);
1980 return 0;
1983 return 1;
1984 #endif /* HAVE_X_WINDOWS */
1986 #ifdef HAVE_NTGUI
1988 BITMAPINFOHEADER *header;
1989 HDC hdc;
1990 int scanline_width_bits;
1991 int remainder;
1992 int palette_colors = 0;
1994 if (depth == 0)
1995 depth = 24;
1997 if (depth != 1 && depth != 4 && depth != 8
1998 && depth != 16 && depth != 24 && depth != 32)
2000 image_error ("Invalid image bit depth specified", Qnil, Qnil);
2001 return 0;
2004 scanline_width_bits = width * depth;
2005 remainder = scanline_width_bits % 32;
2007 if (remainder)
2008 scanline_width_bits += 32 - remainder;
2010 /* Bitmaps with a depth less than 16 need a palette. */
2011 /* BITMAPINFO structure already contains the first RGBQUAD. */
2012 if (depth < 16)
2013 palette_colors = 1 << (depth - 1);
2015 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
2017 header = &(*ximg)->info.bmiHeader;
2018 memset (&(*ximg)->info, 0, sizeof (BITMAPINFO));
2019 header->biSize = sizeof (*header);
2020 header->biWidth = width;
2021 header->biHeight = -height; /* negative indicates a top-down bitmap. */
2022 header->biPlanes = 1;
2023 header->biBitCount = depth;
2024 header->biCompression = BI_RGB;
2025 header->biClrUsed = palette_colors;
2027 /* TODO: fill in palette. */
2028 if (depth == 1)
2030 (*ximg)->info.bmiColors[0].rgbBlue = 0;
2031 (*ximg)->info.bmiColors[0].rgbGreen = 0;
2032 (*ximg)->info.bmiColors[0].rgbRed = 0;
2033 (*ximg)->info.bmiColors[0].rgbReserved = 0;
2034 (*ximg)->info.bmiColors[1].rgbBlue = 255;
2035 (*ximg)->info.bmiColors[1].rgbGreen = 255;
2036 (*ximg)->info.bmiColors[1].rgbRed = 255;
2037 (*ximg)->info.bmiColors[1].rgbReserved = 0;
2040 hdc = get_frame_dc (f);
2042 /* Create a DIBSection and raster array for the bitmap,
2043 and store its handle in *pixmap. */
2044 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
2045 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
2046 /* casting avoids a GCC warning */
2047 (void **)&((*ximg)->data), NULL, 0);
2049 /* Realize display palette and garbage all frames. */
2050 release_frame_dc (f, hdc);
2052 if (*pixmap == NULL)
2054 DWORD err = GetLastError ();
2055 Lisp_Object errcode;
2056 /* All system errors are < 10000, so the following is safe. */
2057 XSETINT (errcode, err);
2058 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
2059 x_destroy_x_image (*ximg);
2060 *ximg = NULL;
2061 return 0;
2064 return 1;
2066 #endif /* HAVE_NTGUI */
2068 #ifdef HAVE_NS
2069 *pixmap = ns_image_for_XPM (width, height, depth);
2070 if (*pixmap == 0)
2072 *ximg = NULL;
2073 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil);
2074 return 0;
2076 *ximg = *pixmap;
2077 return 1;
2078 #endif
2082 /* Destroy XImage XIMG. Free XIMG->data. */
2084 static void
2085 x_destroy_x_image (XImagePtr ximg)
2087 eassert (input_blocked_p ());
2088 if (ximg)
2090 #ifdef HAVE_X_WINDOWS
2091 xfree (ximg->data);
2092 ximg->data = NULL;
2093 XDestroyImage (ximg);
2094 #endif /* HAVE_X_WINDOWS */
2095 #ifdef HAVE_NTGUI
2096 /* Data will be freed by DestroyObject. */
2097 ximg->data = NULL;
2098 xfree (ximg);
2099 #endif /* HAVE_NTGUI */
2100 #ifdef HAVE_NS
2101 ns_release_object (ximg);
2102 #endif /* HAVE_NS */
2107 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2108 are width and height of both the image and pixmap. */
2110 static void
2111 x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height)
2113 #ifdef HAVE_X_WINDOWS
2114 GC gc;
2116 eassert (input_blocked_p ());
2117 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
2118 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
2119 XFreeGC (FRAME_X_DISPLAY (f), gc);
2120 #endif /* HAVE_X_WINDOWS */
2122 #ifdef HAVE_NTGUI
2123 #if 0 /* I don't think this is necessary looking at where it is used. */
2124 HDC hdc = get_frame_dc (f);
2125 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
2126 release_frame_dc (f, hdc);
2127 #endif
2128 #endif /* HAVE_NTGUI */
2130 #ifdef HAVE_NS
2131 eassert (ximg == pixmap);
2132 ns_retain_object (ximg);
2133 #endif
2136 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2137 with image_put_x_image. */
2139 static bool
2140 image_create_x_image_and_pixmap (struct frame *f, struct image *img,
2141 int width, int height, int depth,
2142 XImagePtr *ximg, bool mask_p)
2144 eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP);
2146 return x_create_x_image_and_pixmap (f, width, height, depth, ximg,
2147 !mask_p ? &img->pixmap : &img->mask);
2150 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2151 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2152 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2153 On the other platforms, it puts XIMG into the pixmap, then frees
2154 the X image and its buffer. */
2156 static void
2157 image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg,
2158 bool mask_p)
2160 #ifdef HAVE_X_WINDOWS
2161 if (!mask_p)
2163 eassert (img->ximg == NULL);
2164 img->ximg = ximg;
2166 else
2168 eassert (img->mask_img == NULL);
2169 img->mask_img = ximg;
2171 #else
2172 x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask,
2173 img->width, img->height);
2174 x_destroy_x_image (ximg);
2175 #endif
2178 #ifdef HAVE_X_WINDOWS
2179 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2180 the X images and their buffers. */
2182 static void
2183 image_sync_to_pixmaps (struct frame *f, struct image *img)
2185 if (img->ximg)
2187 x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height);
2188 x_destroy_x_image (img->ximg);
2189 img->ximg = NULL;
2191 if (img->mask_img)
2193 x_put_x_image (f, img->mask_img, img->mask, img->width, img->height);
2194 x_destroy_x_image (img->mask_img);
2195 img->mask_img = NULL;
2198 #endif
2200 #ifdef HAVE_NTGUI
2201 /* Create a memory device context for IMG on frame F. It stores the
2202 currently selected GDI object into *PREV for future restoration by
2203 image_unget_x_image_or_dc. */
2205 static XImagePtr_or_DC
2206 image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p,
2207 HGDIOBJ *prev)
2209 HDC frame_dc = get_frame_dc (f);
2210 XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc);
2212 release_frame_dc (f, frame_dc);
2213 *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask);
2215 return ximg;
2218 static void
2219 image_unget_x_image_or_dc (struct image *img, bool mask_p,
2220 XImagePtr_or_DC ximg, HGDIOBJ prev)
2222 SelectObject (ximg, prev);
2223 DeleteDC (ximg);
2225 #else /* !HAVE_NTGUI */
2226 /* Get the X image for IMG on frame F. The resulting X image data
2227 should be treated as read-only at least on X. */
2229 static XImagePtr
2230 image_get_x_image (struct frame *f, struct image *img, bool mask_p)
2232 #ifdef HAVE_X_WINDOWS
2233 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2235 if (ximg_in_img)
2236 return ximg_in_img;
2237 else
2238 return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask,
2239 0, 0, img->width, img->height, ~0, ZPixmap);
2240 #elif defined (HAVE_NS)
2241 XImagePtr pixmap = !mask_p ? img->pixmap : img->mask;
2243 ns_retain_object (pixmap);
2244 return pixmap;
2245 #endif
2248 static void
2249 image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg)
2251 #ifdef HAVE_X_WINDOWS
2252 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2254 if (ximg_in_img)
2255 eassert (ximg == ximg_in_img);
2256 else
2257 XDestroyImage (ximg);
2258 #elif defined (HAVE_NS)
2259 ns_release_object (ximg);
2260 #endif
2262 #endif /* !HAVE_NTGUI */
2265 /***********************************************************************
2266 File Handling
2267 ***********************************************************************/
2269 /* Find image file FILE. Look in data-directory/images, then
2270 x-bitmap-file-path. Value is the encoded full name of the file
2271 found, or nil if not found. */
2273 Lisp_Object
2274 x_find_image_file (Lisp_Object file)
2276 Lisp_Object file_found, search_path;
2277 int fd;
2279 /* TODO I think this should use something like image-load-path
2280 instead. Unfortunately, that can contain non-string elements. */
2281 search_path = Fcons (Fexpand_file_name (build_string ("images"),
2282 Vdata_directory),
2283 Vx_bitmap_file_path);
2285 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2286 fd = openp (search_path, file, Qnil, &file_found, Qnil, false);
2288 if (fd == -1)
2289 file_found = Qnil;
2290 else
2292 file_found = ENCODE_FILE (file_found);
2293 if (fd != -2)
2294 emacs_close (fd);
2297 return file_found;
2301 /* Read FILE into memory. Value is a pointer to a buffer allocated
2302 with xmalloc holding FILE's contents. Value is null if an error
2303 occurred. *SIZE is set to the size of the file. */
2305 static unsigned char *
2306 slurp_file (char *file, ptrdiff_t *size)
2308 FILE *fp = emacs_fopen (file, "rb");
2309 unsigned char *buf = NULL;
2310 struct stat st;
2312 if (fp)
2314 ptrdiff_t count = SPECPDL_INDEX ();
2315 record_unwind_protect_ptr (fclose_unwind, fp);
2317 if (fstat (fileno (fp), &st) == 0
2318 && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX))
2320 /* Report an error if we read past the purported EOF.
2321 This can happen if the file grows as we read it. */
2322 ptrdiff_t buflen = st.st_size;
2323 buf = xmalloc (buflen + 1);
2324 if (fread (buf, 1, buflen + 1, fp) == buflen)
2325 *size = buflen;
2326 else
2328 xfree (buf);
2329 buf = NULL;
2333 unbind_to (count, Qnil);
2336 return buf;
2341 /***********************************************************************
2342 XBM images
2343 ***********************************************************************/
2345 static bool xbm_load (struct frame *f, struct image *img);
2346 static bool xbm_image_p (Lisp_Object object);
2347 static bool xbm_file_p (Lisp_Object);
2350 /* Indices of image specification fields in xbm_format, below. */
2352 enum xbm_keyword_index
2354 XBM_TYPE,
2355 XBM_FILE,
2356 XBM_WIDTH,
2357 XBM_HEIGHT,
2358 XBM_DATA,
2359 XBM_FOREGROUND,
2360 XBM_BACKGROUND,
2361 XBM_ASCENT,
2362 XBM_MARGIN,
2363 XBM_RELIEF,
2364 XBM_ALGORITHM,
2365 XBM_HEURISTIC_MASK,
2366 XBM_MASK,
2367 XBM_LAST
2370 /* Vector of image_keyword structures describing the format
2371 of valid XBM image specifications. */
2373 static const struct image_keyword xbm_format[XBM_LAST] =
2375 {":type", IMAGE_SYMBOL_VALUE, 1},
2376 {":file", IMAGE_STRING_VALUE, 0},
2377 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2378 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2379 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2380 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
2381 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
2382 {":ascent", IMAGE_ASCENT_VALUE, 0},
2383 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
2384 {":relief", IMAGE_INTEGER_VALUE, 0},
2385 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2386 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2387 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
2390 /* Structure describing the image type XBM. */
2392 static struct image_type xbm_type =
2394 SYMBOL_INDEX (Qxbm),
2395 xbm_image_p,
2396 xbm_load,
2397 x_clear_image,
2398 NULL,
2399 NULL
2402 /* Tokens returned from xbm_scan. */
2404 enum xbm_token
2406 XBM_TK_IDENT = 256,
2407 XBM_TK_NUMBER
2411 /* Return true if OBJECT is a valid XBM-type image specification.
2412 A valid specification is a list starting with the symbol `image'
2413 The rest of the list is a property list which must contain an
2414 entry `:type xbm'.
2416 If the specification specifies a file to load, it must contain
2417 an entry `:file FILENAME' where FILENAME is a string.
2419 If the specification is for a bitmap loaded from memory it must
2420 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2421 WIDTH and HEIGHT are integers > 0. DATA may be:
2423 1. a string large enough to hold the bitmap data, i.e. it must
2424 have a size >= (WIDTH + 7) / 8 * HEIGHT
2426 2. a bool-vector of size >= WIDTH * HEIGHT
2428 3. a vector of strings or bool-vectors, one for each line of the
2429 bitmap.
2431 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2432 may not be specified in this case because they are defined in the
2433 XBM file.
2435 Both the file and data forms may contain the additional entries
2436 `:background COLOR' and `:foreground COLOR'. If not present,
2437 foreground and background of the frame on which the image is
2438 displayed is used. */
2440 static bool
2441 xbm_image_p (Lisp_Object object)
2443 struct image_keyword kw[XBM_LAST];
2445 memcpy (kw, xbm_format, sizeof kw);
2446 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
2447 return 0;
2449 eassert (EQ (kw[XBM_TYPE].value, Qxbm));
2451 if (kw[XBM_FILE].count)
2453 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
2454 return 0;
2456 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
2458 /* In-memory XBM file. */
2459 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
2460 return 0;
2462 else
2464 Lisp_Object data;
2465 int width, height;
2467 /* Entries for `:width', `:height' and `:data' must be present. */
2468 if (!kw[XBM_WIDTH].count
2469 || !kw[XBM_HEIGHT].count
2470 || !kw[XBM_DATA].count)
2471 return 0;
2473 data = kw[XBM_DATA].value;
2474 width = XFASTINT (kw[XBM_WIDTH].value);
2475 height = XFASTINT (kw[XBM_HEIGHT].value);
2477 /* Check type of data, and width and height against contents of
2478 data. */
2479 if (VECTORP (data))
2481 EMACS_INT i;
2483 /* Number of elements of the vector must be >= height. */
2484 if (ASIZE (data) < height)
2485 return 0;
2487 /* Each string or bool-vector in data must be large enough
2488 for one line of the image. */
2489 for (i = 0; i < height; ++i)
2491 Lisp_Object elt = AREF (data, i);
2493 if (STRINGP (elt))
2495 if (SCHARS (elt)
2496 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
2497 return 0;
2499 else if (BOOL_VECTOR_P (elt))
2501 if (bool_vector_size (elt) < width)
2502 return 0;
2504 else
2505 return 0;
2508 else if (STRINGP (data))
2510 if (SCHARS (data)
2511 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
2512 return 0;
2514 else if (BOOL_VECTOR_P (data))
2516 if (bool_vector_size (data) / height < width)
2517 return 0;
2519 else
2520 return 0;
2523 return 1;
2527 /* Scan a bitmap file. FP is the stream to read from. Value is
2528 either an enumerator from enum xbm_token, or a character for a
2529 single-character token, or 0 at end of file. If scanning an
2530 identifier, store the lexeme of the identifier in SVAL. If
2531 scanning a number, store its value in *IVAL. */
2533 static int
2534 xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival)
2536 unsigned int c;
2538 loop:
2540 /* Skip white space. */
2541 while (*s < end && (c = *(*s)++, c_isspace (c)))
2544 if (*s >= end)
2545 c = 0;
2546 else if (c_isdigit (c))
2548 int value = 0, digit;
2550 if (c == '0' && *s < end)
2552 c = *(*s)++;
2553 if (c == 'x' || c == 'X')
2555 while (*s < end)
2557 c = *(*s)++;
2558 if (c_isdigit (c))
2559 digit = c - '0';
2560 else if (c >= 'a' && c <= 'f')
2561 digit = c - 'a' + 10;
2562 else if (c >= 'A' && c <= 'F')
2563 digit = c - 'A' + 10;
2564 else
2565 break;
2566 value = 16 * value + digit;
2569 else if (c_isdigit (c))
2571 value = c - '0';
2572 while (*s < end
2573 && (c = *(*s)++, c_isdigit (c)))
2574 value = 8 * value + c - '0';
2577 else
2579 value = c - '0';
2580 while (*s < end
2581 && (c = *(*s)++, c_isdigit (c)))
2582 value = 10 * value + c - '0';
2585 if (*s < end)
2586 *s = *s - 1;
2587 *ival = value;
2588 c = XBM_TK_NUMBER;
2590 else if (c_isalpha (c) || c == '_')
2592 *sval++ = c;
2593 while (*s < end
2594 && (c = *(*s)++, (c_isalnum (c) || c == '_')))
2595 *sval++ = c;
2596 *sval = 0;
2597 if (*s < end)
2598 *s = *s - 1;
2599 c = XBM_TK_IDENT;
2601 else if (c == '/' && **s == '*')
2603 /* C-style comment. */
2604 ++*s;
2605 while (**s && (**s != '*' || *(*s + 1) != '/'))
2606 ++*s;
2607 if (**s)
2609 *s += 2;
2610 goto loop;
2614 return c;
2617 #ifdef HAVE_NTGUI
2619 /* Create a Windows bitmap from X bitmap data. */
2620 static HBITMAP
2621 w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
2623 static unsigned char swap_nibble[16]
2624 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2625 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2626 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2627 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2628 int i, j, w1, w2;
2629 unsigned char *bits, *p;
2630 HBITMAP bmp;
2632 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
2633 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2634 bits = alloca (height * w2);
2635 memset (bits, 0, height * w2);
2636 for (i = 0; i < height; i++)
2638 p = bits + i*w2;
2639 for (j = 0; j < w1; j++)
2641 /* Bitswap XBM bytes to match how Windows does things. */
2642 unsigned char c = *data++;
2643 *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
2644 | (swap_nibble[(c>>4) & 0xf]));
2647 bmp = CreateBitmap (width, height, 1, 1, (char *) bits);
2649 return bmp;
2652 static void
2653 convert_mono_to_color_image (struct frame *f, struct image *img,
2654 COLORREF foreground, COLORREF background)
2656 HDC hdc, old_img_dc, new_img_dc;
2657 HGDIOBJ old_prev, new_prev;
2658 HBITMAP new_pixmap;
2660 hdc = get_frame_dc (f);
2661 old_img_dc = CreateCompatibleDC (hdc);
2662 new_img_dc = CreateCompatibleDC (hdc);
2663 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
2664 release_frame_dc (f, hdc);
2665 old_prev = SelectObject (old_img_dc, img->pixmap);
2666 new_prev = SelectObject (new_img_dc, new_pixmap);
2667 /* Windows convention for mono bitmaps is black = background,
2668 white = foreground. */
2669 SetTextColor (new_img_dc, background);
2670 SetBkColor (new_img_dc, foreground);
2672 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
2673 0, 0, SRCCOPY);
2675 SelectObject (old_img_dc, old_prev);
2676 SelectObject (new_img_dc, new_prev);
2677 DeleteDC (old_img_dc);
2678 DeleteDC (new_img_dc);
2679 DeleteObject (img->pixmap);
2680 if (new_pixmap == 0)
2681 fprintf (stderr, "Failed to convert image to color.\n");
2682 else
2683 img->pixmap = new_pixmap;
2686 #define XBM_BIT_SHUFFLE(b) (~(b))
2688 #else
2690 #define XBM_BIT_SHUFFLE(b) (b)
2692 #endif /* HAVE_NTGUI */
2695 static void
2696 Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data,
2697 RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg,
2698 bool non_default_colors)
2700 #ifdef HAVE_NTGUI
2701 img->pixmap
2702 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
2704 /* If colors were specified, transfer the bitmap to a color one. */
2705 if (non_default_colors)
2706 convert_mono_to_color_image (f, img, fg, bg);
2708 #elif defined (HAVE_NS)
2709 img->pixmap = ns_image_from_XBM (data, img->width, img->height, fg, bg);
2711 #else
2712 img->pixmap =
2713 (x_check_image_size (0, img->width, img->height)
2714 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
2715 FRAME_X_WINDOW (f),
2716 data,
2717 img->width, img->height,
2718 fg, bg,
2719 DefaultDepthOfScreen (FRAME_X_SCREEN (f)))
2720 : NO_PIXMAP);
2721 #endif /* !HAVE_NTGUI && !HAVE_NS */
2726 /* Replacement for XReadBitmapFileData which isn't available under old
2727 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2728 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2729 the image. Return in *DATA the bitmap data allocated with xmalloc.
2730 Value is true if successful. DATA null means just test if
2731 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2732 inhibit the call to image_error when the image size is invalid (the
2733 bitmap remains unread). */
2735 static bool
2736 xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end,
2737 int *width, int *height, char **data,
2738 bool inhibit_image_error)
2740 unsigned char *s = contents;
2741 char buffer[BUFSIZ];
2742 bool padding_p = 0;
2743 bool v10 = 0;
2744 int bytes_per_line, i, nbytes;
2745 char *p;
2746 int value;
2747 int LA1;
2749 #define match() \
2750 LA1 = xbm_scan (&s, end, buffer, &value)
2752 #define expect(TOKEN) \
2753 do \
2755 if (LA1 != (TOKEN)) \
2756 goto failure; \
2757 match (); \
2759 while (0)
2761 #define expect_ident(IDENT) \
2762 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2763 match (); \
2764 else \
2765 goto failure
2767 *width = *height = -1;
2768 if (data)
2769 *data = NULL;
2770 LA1 = xbm_scan (&s, end, buffer, &value);
2772 /* Parse defines for width, height and hot-spots. */
2773 while (LA1 == '#')
2775 match ();
2776 expect_ident ("define");
2777 expect (XBM_TK_IDENT);
2779 if (LA1 == XBM_TK_NUMBER)
2781 char *q = strrchr (buffer, '_');
2782 q = q ? q + 1 : buffer;
2783 if (strcmp (q, "width") == 0)
2784 *width = value;
2785 else if (strcmp (q, "height") == 0)
2786 *height = value;
2788 expect (XBM_TK_NUMBER);
2791 if (!check_image_size (f, *width, *height))
2793 if (!inhibit_image_error)
2794 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
2795 goto failure;
2797 else if (data == NULL)
2798 goto success;
2800 /* Parse bits. Must start with `static'. */
2801 expect_ident ("static");
2802 if (LA1 == XBM_TK_IDENT)
2804 if (strcmp (buffer, "unsigned") == 0)
2806 match ();
2807 expect_ident ("char");
2809 else if (strcmp (buffer, "short") == 0)
2811 match ();
2812 v10 = 1;
2813 if (*width % 16 && *width % 16 < 9)
2814 padding_p = 1;
2816 else if (strcmp (buffer, "char") == 0)
2817 match ();
2818 else
2819 goto failure;
2821 else
2822 goto failure;
2824 expect (XBM_TK_IDENT);
2825 expect ('[');
2826 expect (']');
2827 expect ('=');
2828 expect ('{');
2830 if (! x_check_image_size (0, *width, *height))
2832 if (!inhibit_image_error)
2833 image_error ("Image too large (%dx%d)",
2834 make_number (*width), make_number (*height));
2835 goto failure;
2837 bytes_per_line = (*width + 7) / 8 + padding_p;
2838 nbytes = bytes_per_line * *height;
2839 p = *data = xmalloc (nbytes);
2841 if (v10)
2843 for (i = 0; i < nbytes; i += 2)
2845 int val = value;
2846 expect (XBM_TK_NUMBER);
2848 *p++ = XBM_BIT_SHUFFLE (val);
2849 if (!padding_p || ((i + 2) % bytes_per_line))
2850 *p++ = XBM_BIT_SHUFFLE (value >> 8);
2852 if (LA1 == ',' || LA1 == '}')
2853 match ();
2854 else
2855 goto failure;
2858 else
2860 for (i = 0; i < nbytes; ++i)
2862 int val = value;
2863 expect (XBM_TK_NUMBER);
2865 *p++ = XBM_BIT_SHUFFLE (val);
2867 if (LA1 == ',' || LA1 == '}')
2868 match ();
2869 else
2870 goto failure;
2874 success:
2875 return 1;
2877 failure:
2879 if (data && *data)
2881 xfree (*data);
2882 *data = NULL;
2884 return 0;
2886 #undef match
2887 #undef expect
2888 #undef expect_ident
2892 /* Load XBM image IMG which will be displayed on frame F from buffer
2893 CONTENTS. END is the end of the buffer. Value is true if
2894 successful. */
2896 static bool
2897 xbm_load_image (struct frame *f, struct image *img, unsigned char *contents,
2898 unsigned char *end)
2900 bool rc;
2901 char *data;
2902 bool success_p = 0;
2904 rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height,
2905 &data, 0);
2906 if (rc)
2908 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
2909 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
2910 bool non_default_colors = 0;
2911 Lisp_Object value;
2913 eassert (img->width > 0 && img->height > 0);
2915 /* Get foreground and background colors, maybe allocate colors. */
2916 value = image_spec_value (img->spec, QCforeground, NULL);
2917 if (!NILP (value))
2919 foreground = x_alloc_image_color (f, img, value, foreground);
2920 non_default_colors = 1;
2922 value = image_spec_value (img->spec, QCbackground, NULL);
2923 if (!NILP (value))
2925 background = x_alloc_image_color (f, img, value, background);
2926 img->background = background;
2927 img->background_valid = 1;
2928 non_default_colors = 1;
2931 Create_Pixmap_From_Bitmap_Data (f, img, data,
2932 foreground, background,
2933 non_default_colors);
2934 xfree (data);
2936 if (img->pixmap == NO_PIXMAP)
2938 x_clear_image (f, img);
2939 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
2941 else
2942 success_p = 1;
2944 else
2945 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
2947 return success_p;
2951 /* Value is true if DATA looks like an in-memory XBM file. */
2953 static bool
2954 xbm_file_p (Lisp_Object data)
2956 int w, h;
2957 return (STRINGP (data)
2958 && xbm_read_bitmap_data (NULL, SDATA (data),
2959 (SDATA (data) + SBYTES (data)),
2960 &w, &h, NULL, 1));
2964 /* Fill image IMG which is used on frame F with pixmap data. Value is
2965 true if successful. */
2967 static bool
2968 xbm_load (struct frame *f, struct image *img)
2970 bool success_p = 0;
2971 Lisp_Object file_name;
2973 eassert (xbm_image_p (img->spec));
2975 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2976 file_name = image_spec_value (img->spec, QCfile, NULL);
2977 if (STRINGP (file_name))
2979 Lisp_Object file;
2980 unsigned char *contents;
2981 ptrdiff_t size;
2983 file = x_find_image_file (file_name);
2984 if (!STRINGP (file))
2986 image_error ("Cannot find image file `%s'", file_name, Qnil);
2987 return 0;
2990 contents = slurp_file (SSDATA (file), &size);
2991 if (contents == NULL)
2993 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
2994 return 0;
2997 success_p = xbm_load_image (f, img, contents, contents + size);
2998 xfree (contents);
3000 else
3002 struct image_keyword fmt[XBM_LAST];
3003 Lisp_Object data;
3004 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
3005 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
3006 bool non_default_colors = 0;
3007 char *bits;
3008 bool parsed_p;
3009 bool in_memory_file_p = 0;
3011 /* See if data looks like an in-memory XBM file. */
3012 data = image_spec_value (img->spec, QCdata, NULL);
3013 in_memory_file_p = xbm_file_p (data);
3015 /* Parse the image specification. */
3016 memcpy (fmt, xbm_format, sizeof fmt);
3017 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
3018 eassert (parsed_p);
3020 /* Get specified width, and height. */
3021 if (!in_memory_file_p)
3023 img->width = XFASTINT (fmt[XBM_WIDTH].value);
3024 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
3025 eassert (img->width > 0 && img->height > 0);
3026 if (!check_image_size (f, img->width, img->height))
3028 image_error ("Invalid image size (see `max-image-size')",
3029 Qnil, Qnil);
3030 return 0;
3034 /* Get foreground and background colors, maybe allocate colors. */
3035 if (fmt[XBM_FOREGROUND].count
3036 && STRINGP (fmt[XBM_FOREGROUND].value))
3038 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
3039 foreground);
3040 non_default_colors = 1;
3043 if (fmt[XBM_BACKGROUND].count
3044 && STRINGP (fmt[XBM_BACKGROUND].value))
3046 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
3047 background);
3048 non_default_colors = 1;
3051 if (in_memory_file_p)
3052 success_p = xbm_load_image (f, img, SDATA (data),
3053 (SDATA (data)
3054 + SBYTES (data)));
3055 else
3057 USE_SAFE_ALLOCA;
3059 if (VECTORP (data))
3061 int i;
3062 char *p;
3063 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3065 SAFE_NALLOCA (bits, nbytes, img->height);
3066 p = bits;
3067 for (i = 0; i < img->height; ++i, p += nbytes)
3069 Lisp_Object line = AREF (data, i);
3070 if (STRINGP (line))
3071 memcpy (p, SDATA (line), nbytes);
3072 else
3073 memcpy (p, bool_vector_data (line), nbytes);
3076 else if (STRINGP (data))
3077 bits = SSDATA (data);
3078 else
3079 bits = (char *) bool_vector_data (data);
3081 #ifdef HAVE_NTGUI
3083 char *invertedBits;
3084 int nbytes, i;
3085 /* Windows mono bitmaps are reversed compared with X. */
3086 invertedBits = bits;
3087 nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3088 SAFE_NALLOCA (bits, nbytes, img->height);
3089 for (i = 0; i < nbytes; i++)
3090 bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]);
3092 #endif
3093 /* Create the pixmap. */
3095 if (x_check_image_size (0, img->width, img->height))
3096 Create_Pixmap_From_Bitmap_Data (f, img, bits,
3097 foreground, background,
3098 non_default_colors);
3099 else
3100 img->pixmap = NO_PIXMAP;
3102 if (img->pixmap)
3103 success_p = 1;
3104 else
3106 image_error ("Unable to create pixmap for XBM image `%s'",
3107 img->spec, Qnil);
3108 x_clear_image (f, img);
3111 SAFE_FREE ();
3115 return success_p;
3120 /***********************************************************************
3121 XPM images
3122 ***********************************************************************/
3124 #if defined (HAVE_XPM) || defined (HAVE_NS)
3126 static bool xpm_image_p (Lisp_Object object);
3127 static bool xpm_load (struct frame *f, struct image *img);
3129 #endif /* HAVE_XPM || HAVE_NS */
3131 #ifdef HAVE_XPM
3132 #ifdef HAVE_NTGUI
3133 /* Indicate to xpm.h that we don't have Xlib. */
3134 #define FOR_MSW
3135 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3136 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3137 #define XColor xpm_XColor
3138 #define XImage xpm_XImage
3139 #define Display xpm_Display
3140 #define PIXEL_ALREADY_TYPEDEFED
3141 #include "X11/xpm.h"
3142 #undef FOR_MSW
3143 #undef XColor
3144 #undef XImage
3145 #undef Display
3146 #undef PIXEL_ALREADY_TYPEDEFED
3147 #else
3148 #include "X11/xpm.h"
3149 #endif /* HAVE_NTGUI */
3150 #endif /* HAVE_XPM */
3152 #if defined (HAVE_XPM) || defined (HAVE_NS)
3154 /* Indices of image specification fields in xpm_format, below. */
3156 enum xpm_keyword_index
3158 XPM_TYPE,
3159 XPM_FILE,
3160 XPM_DATA,
3161 XPM_ASCENT,
3162 XPM_MARGIN,
3163 XPM_RELIEF,
3164 XPM_ALGORITHM,
3165 XPM_HEURISTIC_MASK,
3166 XPM_MASK,
3167 XPM_COLOR_SYMBOLS,
3168 XPM_BACKGROUND,
3169 XPM_LAST
3172 /* Vector of image_keyword structures describing the format
3173 of valid XPM image specifications. */
3175 static const struct image_keyword xpm_format[XPM_LAST] =
3177 {":type", IMAGE_SYMBOL_VALUE, 1},
3178 {":file", IMAGE_STRING_VALUE, 0},
3179 {":data", IMAGE_STRING_VALUE, 0},
3180 {":ascent", IMAGE_ASCENT_VALUE, 0},
3181 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
3182 {":relief", IMAGE_INTEGER_VALUE, 0},
3183 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3184 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3185 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3186 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3187 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
3190 #if defined HAVE_NTGUI && defined WINDOWSNT
3191 static bool init_xpm_functions (void);
3192 #else
3193 #define init_xpm_functions NULL
3194 #endif
3196 /* Structure describing the image type XPM. */
3198 static struct image_type xpm_type =
3200 SYMBOL_INDEX (Qxpm),
3201 xpm_image_p,
3202 xpm_load,
3203 x_clear_image,
3204 init_xpm_functions,
3205 NULL
3208 #ifdef HAVE_X_WINDOWS
3210 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3211 functions for allocating image colors. Our own functions handle
3212 color allocation failures more gracefully than the ones on the XPM
3213 lib. */
3215 #ifndef USE_CAIRO
3216 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3217 #define ALLOC_XPM_COLORS
3218 #endif
3219 #endif /* USE_CAIRO */
3220 #endif /* HAVE_X_WINDOWS */
3222 #ifdef ALLOC_XPM_COLORS
3224 static struct xpm_cached_color *xpm_cache_color (struct frame *, char *,
3225 XColor *, int);
3227 /* An entry in a hash table used to cache color definitions of named
3228 colors. This cache is necessary to speed up XPM image loading in
3229 case we do color allocations ourselves. Without it, we would need
3230 a call to XParseColor per pixel in the image. */
3232 struct xpm_cached_color
3234 /* Next in collision chain. */
3235 struct xpm_cached_color *next;
3237 /* Color definition (RGB and pixel color). */
3238 XColor color;
3240 /* Color name. */
3241 char name[FLEXIBLE_ARRAY_MEMBER];
3244 /* The hash table used for the color cache, and its bucket vector
3245 size. */
3247 #define XPM_COLOR_CACHE_BUCKETS 1001
3248 static struct xpm_cached_color **xpm_color_cache;
3250 /* Initialize the color cache. */
3252 static void
3253 xpm_init_color_cache (struct frame *f, XpmAttributes *attrs)
3255 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
3256 xpm_color_cache = xzalloc (nbytes);
3257 init_color_table ();
3259 if (attrs->valuemask & XpmColorSymbols)
3261 int i;
3262 XColor color;
3264 for (i = 0; i < attrs->numsymbols; ++i)
3265 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3266 attrs->colorsymbols[i].value, &color))
3268 color.pixel = lookup_rgb_color (f, color.red, color.green,
3269 color.blue);
3270 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
3275 /* Free the color cache. */
3277 static void
3278 xpm_free_color_cache (void)
3280 struct xpm_cached_color *p, *next;
3281 int i;
3283 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
3284 for (p = xpm_color_cache[i]; p; p = next)
3286 next = p->next;
3287 xfree (p);
3290 xfree (xpm_color_cache);
3291 xpm_color_cache = NULL;
3292 free_color_table ();
3295 /* Return the bucket index for color named COLOR_NAME in the color
3296 cache. */
3298 static int
3299 xpm_color_bucket (char *color_name)
3301 EMACS_UINT hash = hash_string (color_name, strlen (color_name));
3302 return hash % XPM_COLOR_CACHE_BUCKETS;
3306 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3307 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3308 entry added. */
3310 static struct xpm_cached_color *
3311 xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket)
3313 size_t nbytes;
3314 struct xpm_cached_color *p;
3316 if (bucket < 0)
3317 bucket = xpm_color_bucket (color_name);
3319 nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1;
3320 p = xmalloc (nbytes);
3321 strcpy (p->name, color_name);
3322 p->color = *color;
3323 p->next = xpm_color_cache[bucket];
3324 xpm_color_cache[bucket] = p;
3325 return p;
3328 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3329 return the cached definition in *COLOR. Otherwise, make a new
3330 entry in the cache and allocate the color. Value is false if color
3331 allocation failed. */
3333 static bool
3334 xpm_lookup_color (struct frame *f, char *color_name, XColor *color)
3336 struct xpm_cached_color *p;
3337 int h = xpm_color_bucket (color_name);
3339 for (p = xpm_color_cache[h]; p; p = p->next)
3340 if (strcmp (p->name, color_name) == 0)
3341 break;
3343 if (p != NULL)
3344 *color = p->color;
3345 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3346 color_name, color))
3348 color->pixel = lookup_rgb_color (f, color->red, color->green,
3349 color->blue);
3350 p = xpm_cache_color (f, color_name, color, h);
3352 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3353 with transparency, and it's useful. */
3354 else if (strcmp ("opaque", color_name) == 0)
3356 memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */
3357 color->pixel = FRAME_FOREGROUND_PIXEL (f);
3358 p = xpm_cache_color (f, color_name, color, h);
3361 return p != NULL;
3365 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3366 CLOSURE is a pointer to the frame on which we allocate the
3367 color. Return in *COLOR the allocated color. Value is non-zero
3368 if successful. */
3370 static int
3371 xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color,
3372 void *closure)
3374 return xpm_lookup_color (closure, color_name, color);
3378 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3379 is a pointer to the frame on which we allocate the color. Value is
3380 non-zero if successful. */
3382 static int
3383 xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure)
3385 return 1;
3388 #endif /* ALLOC_XPM_COLORS */
3391 #ifdef WINDOWSNT
3393 /* XPM library details. */
3395 DEF_DLL_FN (void, XpmFreeAttributes, (XpmAttributes *));
3396 DEF_DLL_FN (int, XpmCreateImageFromBuffer,
3397 (Display *, char *, xpm_XImage **,
3398 xpm_XImage **, XpmAttributes *));
3399 DEF_DLL_FN (int, XpmReadFileToImage,
3400 (Display *, char *, xpm_XImage **,
3401 xpm_XImage **, XpmAttributes *));
3402 DEF_DLL_FN (void, XImageFree, (xpm_XImage *));
3404 static bool
3405 init_xpm_functions (void)
3407 HMODULE library;
3409 if (!(library = w32_delayed_load (Qxpm)))
3410 return 0;
3412 LOAD_DLL_FN (library, XpmFreeAttributes);
3413 LOAD_DLL_FN (library, XpmCreateImageFromBuffer);
3414 LOAD_DLL_FN (library, XpmReadFileToImage);
3415 LOAD_DLL_FN (library, XImageFree);
3416 return 1;
3419 # undef XImageFree
3420 # undef XpmCreateImageFromBuffer
3421 # undef XpmFreeAttributes
3422 # undef XpmReadFileToImage
3424 # define XImageFree fn_XImageFree
3425 # define XpmCreateImageFromBuffer fn_XpmCreateImageFromBuffer
3426 # define XpmFreeAttributes fn_XpmFreeAttributes
3427 # define XpmReadFileToImage fn_XpmReadFileToImage
3429 #endif /* WINDOWSNT */
3431 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3432 for XPM images. Such a list must consist of conses whose car and
3433 cdr are strings. */
3435 static bool
3436 xpm_valid_color_symbols_p (Lisp_Object color_symbols)
3438 while (CONSP (color_symbols))
3440 Lisp_Object sym = XCAR (color_symbols);
3441 if (!CONSP (sym)
3442 || !STRINGP (XCAR (sym))
3443 || !STRINGP (XCDR (sym)))
3444 break;
3445 color_symbols = XCDR (color_symbols);
3448 return NILP (color_symbols);
3452 /* Value is true if OBJECT is a valid XPM image specification. */
3454 static bool
3455 xpm_image_p (Lisp_Object object)
3457 struct image_keyword fmt[XPM_LAST];
3458 memcpy (fmt, xpm_format, sizeof fmt);
3459 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
3460 /* Either `:file' or `:data' must be present. */
3461 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
3462 /* Either no `:color-symbols' or it's a list of conses
3463 whose car and cdr are strings. */
3464 && (fmt[XPM_COLOR_SYMBOLS].count == 0
3465 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
3468 #endif /* HAVE_XPM || HAVE_NS */
3470 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3471 ptrdiff_t
3472 x_create_bitmap_from_xpm_data (struct frame *f, const char **bits)
3474 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
3475 ptrdiff_t id;
3476 int rc;
3477 XpmAttributes attrs;
3478 Pixmap bitmap, mask;
3480 memset (&attrs, 0, sizeof attrs);
3482 attrs.visual = FRAME_X_VISUAL (f);
3483 attrs.colormap = FRAME_X_COLORMAP (f);
3484 attrs.valuemask |= XpmVisual;
3485 attrs.valuemask |= XpmColormap;
3487 rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3488 (char **) bits, &bitmap, &mask, &attrs);
3489 if (rc != XpmSuccess)
3491 XpmFreeAttributes (&attrs);
3492 return -1;
3495 id = x_allocate_bitmap_record (f);
3496 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
3497 dpyinfo->bitmaps[id - 1].have_mask = true;
3498 dpyinfo->bitmaps[id - 1].mask = mask;
3499 dpyinfo->bitmaps[id - 1].file = NULL;
3500 dpyinfo->bitmaps[id - 1].height = attrs.height;
3501 dpyinfo->bitmaps[id - 1].width = attrs.width;
3502 dpyinfo->bitmaps[id - 1].depth = attrs.depth;
3503 dpyinfo->bitmaps[id - 1].refcount = 1;
3505 XpmFreeAttributes (&attrs);
3506 return id;
3508 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3510 /* Load image IMG which will be displayed on frame F. Value is
3511 true if successful. */
3513 #ifdef HAVE_XPM
3515 static bool
3516 xpm_load (struct frame *f, struct image *img)
3518 int rc;
3519 XpmAttributes attrs;
3520 Lisp_Object specified_file, color_symbols;
3521 USE_SAFE_ALLOCA;
3523 #ifdef HAVE_NTGUI
3524 HDC hdc;
3525 xpm_XImage * xpm_image = NULL, * xpm_mask = NULL;
3526 #endif /* HAVE_NTGUI */
3528 /* Configure the XPM lib. Use the visual of frame F. Allocate
3529 close colors. Return colors allocated. */
3530 memset (&attrs, 0, sizeof attrs);
3532 #ifndef HAVE_NTGUI
3533 attrs.visual = FRAME_X_VISUAL (f);
3534 attrs.colormap = FRAME_X_COLORMAP (f);
3535 attrs.valuemask |= XpmVisual;
3536 attrs.valuemask |= XpmColormap;
3537 #endif /* HAVE_NTGUI */
3539 #ifdef ALLOC_XPM_COLORS
3540 /* Allocate colors with our own functions which handle
3541 failing color allocation more gracefully. */
3542 attrs.color_closure = f;
3543 attrs.alloc_color = xpm_alloc_color;
3544 attrs.free_colors = xpm_free_colors;
3545 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
3546 #else /* not ALLOC_XPM_COLORS */
3547 /* Let the XPM lib allocate colors. */
3548 attrs.valuemask |= XpmReturnAllocPixels;
3549 #ifdef XpmAllocCloseColors
3550 attrs.alloc_close_colors = 1;
3551 attrs.valuemask |= XpmAllocCloseColors;
3552 #else /* not XpmAllocCloseColors */
3553 attrs.closeness = 600;
3554 attrs.valuemask |= XpmCloseness;
3555 #endif /* not XpmAllocCloseColors */
3556 #endif /* ALLOC_XPM_COLORS */
3558 /* If image specification contains symbolic color definitions, add
3559 these to `attrs'. */
3560 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
3561 if (CONSP (color_symbols))
3563 Lisp_Object tail;
3564 XpmColorSymbol *xpm_syms;
3565 ptrdiff_t i, size;
3567 attrs.valuemask |= XpmColorSymbols;
3569 /* Count number of symbols. */
3570 attrs.numsymbols = 0;
3571 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
3572 ++attrs.numsymbols;
3574 /* Allocate an XpmColorSymbol array. */
3575 SAFE_NALLOCA (xpm_syms, 1, attrs.numsymbols);
3576 size = attrs.numsymbols * sizeof *xpm_syms;
3577 memset (xpm_syms, 0, size);
3578 attrs.colorsymbols = xpm_syms;
3580 /* Fill the color symbol array. */
3581 for (tail = color_symbols, i = 0;
3582 CONSP (tail);
3583 ++i, tail = XCDR (tail))
3585 Lisp_Object name;
3586 Lisp_Object color;
3587 char *empty_string = (char *) "";
3589 if (!CONSP (XCAR (tail)))
3591 xpm_syms[i].name = empty_string;
3592 xpm_syms[i].value = empty_string;
3593 continue;
3595 name = XCAR (XCAR (tail));
3596 color = XCDR (XCAR (tail));
3597 if (STRINGP (name))
3598 SAFE_ALLOCA_STRING (xpm_syms[i].name, name);
3599 else
3600 xpm_syms[i].name = empty_string;
3601 if (STRINGP (color))
3602 SAFE_ALLOCA_STRING (xpm_syms[i].value, color);
3603 else
3604 xpm_syms[i].value = empty_string;
3608 /* Create a pixmap for the image, either from a file, or from a
3609 string buffer containing data in the same format as an XPM file. */
3610 #ifdef ALLOC_XPM_COLORS
3611 xpm_init_color_cache (f, &attrs);
3612 #endif
3614 specified_file = image_spec_value (img->spec, QCfile, NULL);
3616 #ifdef HAVE_NTGUI
3618 HDC frame_dc = get_frame_dc (f);
3619 hdc = CreateCompatibleDC (frame_dc);
3620 release_frame_dc (f, frame_dc);
3622 #endif /* HAVE_NTGUI */
3624 if (STRINGP (specified_file))
3626 Lisp_Object file = x_find_image_file (specified_file);
3627 if (!STRINGP (file))
3629 image_error ("Cannot find image file `%s'", specified_file, Qnil);
3630 #ifdef ALLOC_XPM_COLORS
3631 xpm_free_color_cache ();
3632 #endif
3633 SAFE_FREE ();
3634 return 0;
3637 #ifdef HAVE_NTGUI
3638 #ifdef WINDOWSNT
3639 /* FILE is encoded in UTF-8, but image libraries on Windows
3640 support neither UTF-8 nor UTF-16 encoded file names. So we
3641 need to re-encode it in ANSI. */
3642 file = ansi_encode_filename (file);
3643 #endif
3644 /* XpmReadFileToPixmap is not available in the Windows port of
3645 libxpm. But XpmReadFileToImage almost does what we want. */
3646 rc = XpmReadFileToImage (&hdc, SDATA (file),
3647 &xpm_image, &xpm_mask,
3648 &attrs);
3649 #else
3650 rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file),
3651 &img->ximg, &img->mask_img,
3652 &attrs);
3653 #endif /* HAVE_NTGUI */
3655 else
3657 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
3658 if (!STRINGP (buffer))
3660 image_error ("Invalid image data `%s'", buffer, Qnil);
3661 #ifdef ALLOC_XPM_COLORS
3662 xpm_free_color_cache ();
3663 #endif
3664 SAFE_FREE ();
3665 return 0;
3667 #ifdef HAVE_NTGUI
3668 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3669 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3670 rc = XpmCreateImageFromBuffer (&hdc, SDATA (buffer),
3671 &xpm_image, &xpm_mask,
3672 &attrs);
3673 #else
3674 rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer),
3675 &img->ximg, &img->mask_img,
3676 &attrs);
3677 #endif /* HAVE_NTGUI */
3680 #ifdef USE_CAIRO
3681 // Load very specific Xpm:s.
3682 if (rc == XpmSuccess
3683 && img->ximg->format == ZPixmap
3684 && img->ximg->bits_per_pixel == 32
3685 && (! img->mask_img || img->mask_img->bits_per_pixel == 1))
3687 int width = img->ximg->width;
3688 int height = img->ximg->height;
3689 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
3690 int i;
3691 uint32_t *od = (uint32_t *)data;
3692 uint32_t *id = (uint32_t *)img->ximg->data;
3693 char *mid = img->mask_img ? img->mask_img->data : 0;
3694 uint32_t bgcolor = get_spec_bg_or_alpha_as_argb (img, f);
3696 for (i = 0; i < height; ++i)
3698 int k;
3699 for (k = 0; k < width; ++k)
3701 int idx = i * img->ximg->bytes_per_line/4 + k;
3702 int maskidx = mid ? i * img->mask_img->bytes_per_line + k/8 : 0;
3703 int mask = mid ? mid[maskidx] & (1 << (k % 8)) : 1;
3705 if (mask) od[idx] = id[idx] + 0xff000000; // ff => full alpha
3706 else od[idx] = bgcolor;
3710 create_cairo_image_surface (img, data, width, height);
3712 else
3714 rc = XpmFileInvalid;
3715 x_clear_image (f, img);
3717 #else
3718 #ifdef HAVE_X_WINDOWS
3719 if (rc == XpmSuccess)
3721 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3722 img->ximg->width, img->ximg->height,
3723 img->ximg->depth);
3724 if (img->pixmap == NO_PIXMAP)
3726 x_clear_image (f, img);
3727 rc = XpmNoMemory;
3729 else if (img->mask_img)
3731 img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3732 img->mask_img->width,
3733 img->mask_img->height,
3734 img->mask_img->depth);
3735 if (img->mask == NO_PIXMAP)
3737 x_clear_image (f, img);
3738 rc = XpmNoMemory;
3742 #endif
3743 #endif /* ! USE_CAIRO */
3745 if (rc == XpmSuccess)
3747 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3748 img->colors = colors_in_color_table (&img->ncolors);
3749 #else /* not ALLOC_XPM_COLORS */
3750 int i;
3752 #ifdef HAVE_NTGUI
3753 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3754 plus some duplicate attributes. */
3755 if (xpm_image && xpm_image->bitmap)
3757 img->pixmap = xpm_image->bitmap;
3758 /* XImageFree in libXpm frees XImage struct without destroying
3759 the bitmap, which is what we want. */
3760 XImageFree (xpm_image);
3762 if (xpm_mask && xpm_mask->bitmap)
3764 /* The mask appears to be inverted compared with what we expect.
3765 TODO: invert our expectations. See other places where we
3766 have to invert bits because our idea of masks is backwards. */
3767 HGDIOBJ old_obj;
3768 old_obj = SelectObject (hdc, xpm_mask->bitmap);
3770 PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT);
3771 SelectObject (hdc, old_obj);
3773 img->mask = xpm_mask->bitmap;
3774 XImageFree (xpm_mask);
3775 DeleteDC (hdc);
3778 DeleteDC (hdc);
3779 #endif /* HAVE_NTGUI */
3781 /* Remember allocated colors. */
3782 img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors);
3783 img->ncolors = attrs.nalloc_pixels;
3784 for (i = 0; i < attrs.nalloc_pixels; ++i)
3786 img->colors[i] = attrs.alloc_pixels[i];
3787 #ifdef DEBUG_X_COLORS
3788 register_color (img->colors[i]);
3789 #endif
3791 #endif /* not ALLOC_XPM_COLORS */
3793 img->width = attrs.width;
3794 img->height = attrs.height;
3795 eassert (img->width > 0 && img->height > 0);
3797 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3798 XpmFreeAttributes (&attrs);
3800 #ifdef HAVE_X_WINDOWS
3801 /* Maybe fill in the background field while we have ximg handy. */
3802 IMAGE_BACKGROUND (img, f, img->ximg);
3803 if (img->mask_img)
3804 /* Fill in the background_transparent field while we have the
3805 mask handy. */
3806 image_background_transparent (img, f, img->mask_img);
3807 #endif
3809 else
3811 #ifdef HAVE_NTGUI
3812 DeleteDC (hdc);
3813 #endif /* HAVE_NTGUI */
3815 switch (rc)
3817 case XpmOpenFailed:
3818 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
3819 break;
3821 case XpmFileInvalid:
3822 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
3823 break;
3825 case XpmNoMemory:
3826 image_error ("Out of memory (%s)", img->spec, Qnil);
3827 break;
3829 case XpmColorFailed:
3830 image_error ("Color allocation error (%s)", img->spec, Qnil);
3831 break;
3833 default:
3834 image_error ("Unknown error (%s)", img->spec, Qnil);
3835 break;
3839 #ifdef ALLOC_XPM_COLORS
3840 xpm_free_color_cache ();
3841 #endif
3842 SAFE_FREE ();
3843 return rc == XpmSuccess;
3846 #endif /* HAVE_XPM */
3848 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3850 /* XPM support functions for NS where libxpm is not available.
3851 Only XPM version 3 (without any extensions) is supported. */
3853 static void xpm_put_color_table_v (Lisp_Object, const unsigned char *,
3854 int, Lisp_Object);
3855 static Lisp_Object xpm_get_color_table_v (Lisp_Object,
3856 const unsigned char *, int);
3857 static void xpm_put_color_table_h (Lisp_Object, const unsigned char *,
3858 int, Lisp_Object);
3859 static Lisp_Object xpm_get_color_table_h (Lisp_Object,
3860 const unsigned char *, int);
3862 /* Tokens returned from xpm_scan. */
3864 enum xpm_token
3866 XPM_TK_IDENT = 256,
3867 XPM_TK_STRING,
3868 XPM_TK_EOF
3871 /* Scan an XPM data and return a character (< 256) or a token defined
3872 by enum xpm_token above. *S and END are the start (inclusive) and
3873 the end (exclusive) addresses of the data, respectively. Advance
3874 *S while scanning. If token is either XPM_TK_IDENT or
3875 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3876 length of the corresponding token, respectively. */
3878 static int
3879 xpm_scan (const unsigned char **s,
3880 const unsigned char *end,
3881 const unsigned char **beg,
3882 ptrdiff_t *len)
3884 int c;
3886 while (*s < end)
3888 /* Skip white-space. */
3889 while (*s < end && (c = *(*s)++, c_isspace (c)))
3892 /* gnus-pointer.xpm uses '-' in its identifier.
3893 sb-dir-plus.xpm uses '+' in its identifier. */
3894 if (c_isalpha (c) || c == '_' || c == '-' || c == '+')
3896 *beg = *s - 1;
3897 while (*s < end
3898 && (c = **s, c_isalnum (c)
3899 || c == '_' || c == '-' || c == '+'))
3900 ++*s;
3901 *len = *s - *beg;
3902 return XPM_TK_IDENT;
3904 else if (c == '"')
3906 *beg = *s;
3907 while (*s < end && **s != '"')
3908 ++*s;
3909 *len = *s - *beg;
3910 if (*s < end)
3911 ++*s;
3912 return XPM_TK_STRING;
3914 else if (c == '/')
3916 if (*s < end && **s == '*')
3918 /* C-style comment. */
3919 ++*s;
3922 while (*s < end && *(*s)++ != '*')
3925 while (*s < end && **s != '/');
3926 if (*s < end)
3927 ++*s;
3929 else
3930 return c;
3932 else
3933 return c;
3936 return XPM_TK_EOF;
3939 /* Functions for color table lookup in XPM data. A key is a string
3940 specifying the color of each pixel in XPM data. A value is either
3941 an integer that specifies a pixel color, Qt that specifies
3942 transparency, or Qnil for the unspecified color. If the length of
3943 the key string is one, a vector is used as a table. Otherwise, a
3944 hash table is used. */
3946 static Lisp_Object
3947 xpm_make_color_table_v (void (**put_func) (Lisp_Object,
3948 const unsigned char *,
3949 int,
3950 Lisp_Object),
3951 Lisp_Object (**get_func) (Lisp_Object,
3952 const unsigned char *,
3953 int))
3955 *put_func = xpm_put_color_table_v;
3956 *get_func = xpm_get_color_table_v;
3957 return Fmake_vector (make_number (256), Qnil);
3960 static void
3961 xpm_put_color_table_v (Lisp_Object color_table,
3962 const unsigned char *chars_start,
3963 int chars_len,
3964 Lisp_Object color)
3966 ASET (color_table, *chars_start, color);
3969 static Lisp_Object
3970 xpm_get_color_table_v (Lisp_Object color_table,
3971 const unsigned char *chars_start,
3972 int chars_len)
3974 return AREF (color_table, *chars_start);
3977 static Lisp_Object
3978 xpm_make_color_table_h (void (**put_func) (Lisp_Object,
3979 const unsigned char *,
3980 int,
3981 Lisp_Object),
3982 Lisp_Object (**get_func) (Lisp_Object,
3983 const unsigned char *,
3984 int))
3986 *put_func = xpm_put_color_table_h;
3987 *get_func = xpm_get_color_table_h;
3988 return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE),
3989 make_float (DEFAULT_REHASH_SIZE),
3990 make_float (DEFAULT_REHASH_THRESHOLD),
3991 Qnil);
3994 static void
3995 xpm_put_color_table_h (Lisp_Object color_table,
3996 const unsigned char *chars_start,
3997 int chars_len,
3998 Lisp_Object color)
4000 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4001 EMACS_UINT hash_code;
4002 Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
4004 hash_lookup (table, chars, &hash_code);
4005 hash_put (table, chars, color, hash_code);
4008 static Lisp_Object
4009 xpm_get_color_table_h (Lisp_Object color_table,
4010 const unsigned char *chars_start,
4011 int chars_len)
4013 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4014 ptrdiff_t i =
4015 hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL);
4017 return i >= 0 ? HASH_VALUE (table, i) : Qnil;
4020 enum xpm_color_key {
4021 XPM_COLOR_KEY_S,
4022 XPM_COLOR_KEY_M,
4023 XPM_COLOR_KEY_G4,
4024 XPM_COLOR_KEY_G,
4025 XPM_COLOR_KEY_C
4028 static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"};
4030 static int
4031 xpm_str_to_color_key (const char *s)
4033 int i;
4035 for (i = 0; i < ARRAYELTS (xpm_color_key_strings); i++)
4036 if (strcmp (xpm_color_key_strings[i], s) == 0)
4037 return i;
4038 return -1;
4041 static bool
4042 xpm_load_image (struct frame *f,
4043 struct image *img,
4044 const unsigned char *contents,
4045 const unsigned char *end)
4047 const unsigned char *s = contents, *beg, *str;
4048 unsigned char buffer[BUFSIZ];
4049 int width, height, x, y;
4050 int num_colors, chars_per_pixel;
4051 ptrdiff_t len;
4052 int LA1;
4053 void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object);
4054 Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int);
4055 Lisp_Object frame, color_symbols, color_table;
4056 int best_key;
4057 bool have_mask = false;
4058 XImagePtr ximg = NULL, mask_img = NULL;
4060 #define match() \
4061 LA1 = xpm_scan (&s, end, &beg, &len)
4063 #define expect(TOKEN) \
4064 do \
4066 if (LA1 != (TOKEN)) \
4067 goto failure; \
4068 match (); \
4070 while (0)
4072 #define expect_ident(IDENT) \
4073 if (LA1 == XPM_TK_IDENT \
4074 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
4075 match (); \
4076 else \
4077 goto failure
4079 if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0))
4080 goto failure;
4081 s += 9;
4082 match ();
4083 expect_ident ("static");
4084 expect_ident ("char");
4085 expect ('*');
4086 expect (XPM_TK_IDENT);
4087 expect ('[');
4088 expect (']');
4089 expect ('=');
4090 expect ('{');
4091 expect (XPM_TK_STRING);
4092 if (len >= BUFSIZ)
4093 goto failure;
4094 memcpy (buffer, beg, len);
4095 buffer[len] = '\0';
4096 if (sscanf (buffer, "%d %d %d %d", &width, &height,
4097 &num_colors, &chars_per_pixel) != 4
4098 || width <= 0 || height <= 0
4099 || num_colors <= 0 || chars_per_pixel <= 0)
4100 goto failure;
4102 if (!check_image_size (f, width, height))
4104 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
4105 goto failure;
4108 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)
4109 #ifndef HAVE_NS
4110 || !image_create_x_image_and_pixmap (f, img, width, height, 1,
4111 &mask_img, 1)
4112 #endif
4115 image_error ("Image too large", Qnil, Qnil);
4116 goto failure;
4119 expect (',');
4121 XSETFRAME (frame, f);
4122 if (!NILP (Fxw_display_color_p (frame)))
4123 best_key = XPM_COLOR_KEY_C;
4124 else if (!NILP (Fx_display_grayscale_p (frame)))
4125 best_key = (XFASTINT (Fx_display_planes (frame)) > 2
4126 ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4);
4127 else
4128 best_key = XPM_COLOR_KEY_M;
4130 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
4131 if (chars_per_pixel == 1)
4132 color_table = xpm_make_color_table_v (&put_color_table,
4133 &get_color_table);
4134 else
4135 color_table = xpm_make_color_table_h (&put_color_table,
4136 &get_color_table);
4138 while (num_colors-- > 0)
4140 char *color, *max_color;
4141 int key, next_key, max_key = 0;
4142 Lisp_Object symbol_color = Qnil, color_val;
4143 XColor cdef;
4145 expect (XPM_TK_STRING);
4146 if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel)
4147 goto failure;
4148 memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel);
4149 buffer[len - chars_per_pixel] = '\0';
4151 str = strtok (buffer, " \t");
4152 if (str == NULL)
4153 goto failure;
4154 key = xpm_str_to_color_key (str);
4155 if (key < 0)
4156 goto failure;
4159 color = strtok (NULL, " \t");
4160 if (color == NULL)
4161 goto failure;
4163 while ((str = strtok (NULL, " \t")) != NULL)
4165 next_key = xpm_str_to_color_key (str);
4166 if (next_key >= 0)
4167 break;
4168 color[strlen (color)] = ' ';
4171 if (key == XPM_COLOR_KEY_S)
4173 if (NILP (symbol_color))
4174 symbol_color = build_string (color);
4176 else if (max_key < key && key <= best_key)
4178 max_key = key;
4179 max_color = color;
4181 key = next_key;
4183 while (str);
4185 color_val = Qnil;
4186 if (!NILP (color_symbols) && !NILP (symbol_color))
4188 Lisp_Object specified_color = Fassoc (symbol_color, color_symbols);
4190 if (CONSP (specified_color) && STRINGP (XCDR (specified_color)))
4192 if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0)
4193 color_val = Qt;
4194 else if (x_defined_color (f, SSDATA (XCDR (specified_color)),
4195 &cdef, 0))
4196 color_val = make_number (cdef.pixel);
4199 if (NILP (color_val) && max_key > 0)
4201 if (xstrcasecmp (max_color, "None") == 0)
4202 color_val = Qt;
4203 else if (x_defined_color (f, max_color, &cdef, 0))
4204 color_val = make_number (cdef.pixel);
4206 if (!NILP (color_val))
4207 (*put_color_table) (color_table, beg, chars_per_pixel, color_val);
4209 expect (',');
4212 for (y = 0; y < height; y++)
4214 expect (XPM_TK_STRING);
4215 str = beg;
4216 if (len < width * chars_per_pixel)
4217 goto failure;
4218 for (x = 0; x < width; x++, str += chars_per_pixel)
4220 Lisp_Object color_val =
4221 (*get_color_table) (color_table, str, chars_per_pixel);
4223 XPutPixel (ximg, x, y,
4224 (INTEGERP (color_val) ? XINT (color_val)
4225 : FRAME_FOREGROUND_PIXEL (f)));
4226 #ifndef HAVE_NS
4227 XPutPixel (mask_img, x, y,
4228 (!EQ (color_val, Qt) ? PIX_MASK_DRAW
4229 : (have_mask = true, PIX_MASK_RETAIN)));
4230 #else
4231 if (EQ (color_val, Qt))
4232 ns_set_alpha (ximg, x, y, 0);
4233 #endif
4235 if (y + 1 < height)
4236 expect (',');
4239 img->width = width;
4240 img->height = height;
4242 /* Maybe fill in the background field while we have ximg handy. */
4243 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
4244 IMAGE_BACKGROUND (img, f, ximg);
4246 image_put_x_image (f, img, ximg, 0);
4247 #ifndef HAVE_NS
4248 if (have_mask)
4250 /* Fill in the background_transparent field while we have the
4251 mask handy. */
4252 image_background_transparent (img, f, mask_img);
4254 image_put_x_image (f, img, mask_img, 1);
4256 else
4258 x_destroy_x_image (mask_img);
4259 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
4261 #endif
4262 return 1;
4264 failure:
4265 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
4266 x_destroy_x_image (ximg);
4267 x_destroy_x_image (mask_img);
4268 x_clear_image (f, img);
4269 return 0;
4271 #undef match
4272 #undef expect
4273 #undef expect_ident
4276 static bool
4277 xpm_load (struct frame *f,
4278 struct image *img)
4280 bool success_p = 0;
4281 Lisp_Object file_name;
4283 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4284 file_name = image_spec_value (img->spec, QCfile, NULL);
4285 if (STRINGP (file_name))
4287 Lisp_Object file;
4288 unsigned char *contents;
4289 ptrdiff_t size;
4291 file = x_find_image_file (file_name);
4292 if (!STRINGP (file))
4294 image_error ("Cannot find image file `%s'", file_name, Qnil);
4295 return 0;
4298 contents = slurp_file (SSDATA (file), &size);
4299 if (contents == NULL)
4301 image_error ("Error loading XPM image `%s'", img->spec, Qnil);
4302 return 0;
4305 success_p = xpm_load_image (f, img, contents, contents + size);
4306 xfree (contents);
4308 else
4310 Lisp_Object data;
4312 data = image_spec_value (img->spec, QCdata, NULL);
4313 if (!STRINGP (data))
4315 image_error ("Invalid image data `%s'", data, Qnil);
4316 return 0;
4318 success_p = xpm_load_image (f, img, SDATA (data),
4319 SDATA (data) + SBYTES (data));
4322 return success_p;
4325 #endif /* HAVE_NS && !HAVE_XPM */
4329 /***********************************************************************
4330 Color table
4331 ***********************************************************************/
4333 #ifdef COLOR_TABLE_SUPPORT
4335 /* An entry in the color table mapping an RGB color to a pixel color. */
4337 struct ct_color
4339 int r, g, b;
4340 unsigned long pixel;
4342 /* Next in color table collision list. */
4343 struct ct_color *next;
4346 /* The bucket vector size to use. Must be prime. */
4348 #define CT_SIZE 101
4350 /* Value is a hash of the RGB color given by R, G, and B. */
4352 static unsigned
4353 ct_hash_rgb (unsigned r, unsigned g, unsigned b)
4355 return (r << 16) ^ (g << 8) ^ b;
4358 /* The color hash table. */
4360 static struct ct_color **ct_table;
4362 /* Number of entries in the color table. */
4364 static int ct_colors_allocated;
4365 enum
4367 ct_colors_allocated_max =
4368 min (INT_MAX,
4369 min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long))
4372 /* Initialize the color table. */
4374 static void
4375 init_color_table (void)
4377 int size = CT_SIZE * sizeof (*ct_table);
4378 ct_table = xzalloc (size);
4379 ct_colors_allocated = 0;
4383 /* Free memory associated with the color table. */
4385 static void
4386 free_color_table (void)
4388 int i;
4389 struct ct_color *p, *next;
4391 for (i = 0; i < CT_SIZE; ++i)
4392 for (p = ct_table[i]; p; p = next)
4394 next = p->next;
4395 xfree (p);
4398 xfree (ct_table);
4399 ct_table = NULL;
4403 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4404 entry for that color already is in the color table, return the
4405 pixel color of that entry. Otherwise, allocate a new color for R,
4406 G, B, and make an entry in the color table. */
4408 static unsigned long
4409 lookup_rgb_color (struct frame *f, int r, int g, int b)
4411 unsigned hash = ct_hash_rgb (r, g, b);
4412 int i = hash % CT_SIZE;
4413 struct ct_color *p;
4414 Display_Info *dpyinfo;
4416 /* Handle TrueColor visuals specially, which improves performance by
4417 two orders of magnitude. Freeing colors on TrueColor visuals is
4418 a nop, and pixel colors specify RGB values directly. See also
4419 the Xlib spec, chapter 3.1. */
4420 dpyinfo = FRAME_DISPLAY_INFO (f);
4421 if (dpyinfo->red_bits > 0)
4423 unsigned long pr, pg, pb;
4425 /* Apply gamma-correction like normal color allocation does. */
4426 if (f->gamma)
4428 XColor color;
4429 color.red = r, color.green = g, color.blue = b;
4430 gamma_correct (f, &color);
4431 r = color.red, g = color.green, b = color.blue;
4434 /* Scale down RGB values to the visual's bits per RGB, and shift
4435 them to the right position in the pixel color. Note that the
4436 original RGB values are 16-bit values, as usual in X. */
4437 pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset;
4438 pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
4439 pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset;
4441 /* Assemble the pixel color. */
4442 return pr | pg | pb;
4445 for (p = ct_table[i]; p; p = p->next)
4446 if (p->r == r && p->g == g && p->b == b)
4447 break;
4449 if (p == NULL)
4452 #ifdef HAVE_X_WINDOWS
4453 XColor color;
4454 Colormap cmap;
4455 bool rc;
4456 #else
4457 COLORREF color;
4458 #endif
4460 if (ct_colors_allocated_max <= ct_colors_allocated)
4461 return FRAME_FOREGROUND_PIXEL (f);
4463 #ifdef HAVE_X_WINDOWS
4464 color.red = r;
4465 color.green = g;
4466 color.blue = b;
4468 cmap = FRAME_X_COLORMAP (f);
4469 rc = x_alloc_nearest_color (f, cmap, &color);
4470 if (rc)
4472 ++ct_colors_allocated;
4473 p = xmalloc (sizeof *p);
4474 p->r = r;
4475 p->g = g;
4476 p->b = b;
4477 p->pixel = color.pixel;
4478 p->next = ct_table[i];
4479 ct_table[i] = p;
4481 else
4482 return FRAME_FOREGROUND_PIXEL (f);
4484 #else
4485 #ifdef HAVE_NTGUI
4486 color = PALETTERGB (r, g, b);
4487 #else
4488 color = RGB_TO_ULONG (r, g, b);
4489 #endif /* HAVE_NTGUI */
4490 ++ct_colors_allocated;
4491 p = xmalloc (sizeof *p);
4492 p->r = r;
4493 p->g = g;
4494 p->b = b;
4495 p->pixel = color;
4496 p->next = ct_table[i];
4497 ct_table[i] = p;
4498 #endif /* HAVE_X_WINDOWS */
4502 return p->pixel;
4506 /* Look up pixel color PIXEL which is used on frame F in the color
4507 table. If not already present, allocate it. Value is PIXEL. */
4509 static unsigned long
4510 lookup_pixel_color (struct frame *f, unsigned long pixel)
4512 int i = pixel % CT_SIZE;
4513 struct ct_color *p;
4515 for (p = ct_table[i]; p; p = p->next)
4516 if (p->pixel == pixel)
4517 break;
4519 if (p == NULL)
4521 XColor color;
4522 Colormap cmap;
4523 bool rc;
4525 if (ct_colors_allocated >= ct_colors_allocated_max)
4526 return FRAME_FOREGROUND_PIXEL (f);
4528 #ifdef HAVE_X_WINDOWS
4529 cmap = FRAME_X_COLORMAP (f);
4530 color.pixel = pixel;
4531 x_query_color (f, &color);
4532 rc = x_alloc_nearest_color (f, cmap, &color);
4533 #else
4534 block_input ();
4535 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
4536 color.pixel = pixel;
4537 XQueryColor (NULL, cmap, &color);
4538 rc = x_alloc_nearest_color (f, cmap, &color);
4539 unblock_input ();
4540 #endif /* HAVE_X_WINDOWS */
4542 if (rc)
4544 ++ct_colors_allocated;
4546 p = xmalloc (sizeof *p);
4547 p->r = color.red;
4548 p->g = color.green;
4549 p->b = color.blue;
4550 p->pixel = pixel;
4551 p->next = ct_table[i];
4552 ct_table[i] = p;
4554 else
4555 return FRAME_FOREGROUND_PIXEL (f);
4557 return p->pixel;
4561 /* Value is a vector of all pixel colors contained in the color table,
4562 allocated via xmalloc. Set *N to the number of colors. */
4564 static unsigned long *
4565 colors_in_color_table (int *n)
4567 int i, j;
4568 struct ct_color *p;
4569 unsigned long *colors;
4571 if (ct_colors_allocated == 0)
4573 *n = 0;
4574 colors = NULL;
4576 else
4578 colors = xmalloc (ct_colors_allocated * sizeof *colors);
4579 *n = ct_colors_allocated;
4581 for (i = j = 0; i < CT_SIZE; ++i)
4582 for (p = ct_table[i]; p; p = p->next)
4583 colors[j++] = p->pixel;
4586 return colors;
4589 #else /* COLOR_TABLE_SUPPORT */
4591 static unsigned long
4592 lookup_rgb_color (struct frame *f, int r, int g, int b)
4594 unsigned long pixel;
4596 #ifdef HAVE_NTGUI
4597 pixel = PALETTERGB (r >> 8, g >> 8, b >> 8);
4598 #endif /* HAVE_NTGUI */
4600 #ifdef HAVE_NS
4601 pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8);
4602 #endif /* HAVE_NS */
4603 return pixel;
4606 static void
4607 init_color_table (void)
4610 #endif /* COLOR_TABLE_SUPPORT */
4613 /***********************************************************************
4614 Algorithms
4615 ***********************************************************************/
4617 /* Edge detection matrices for different edge-detection
4618 strategies. */
4620 static int emboss_matrix[9] = {
4621 /* x - 1 x x + 1 */
4622 2, -1, 0, /* y - 1 */
4623 -1, 0, 1, /* y */
4624 0, 1, -2 /* y + 1 */
4627 static int laplace_matrix[9] = {
4628 /* x - 1 x x + 1 */
4629 1, 0, 0, /* y - 1 */
4630 0, 0, 0, /* y */
4631 0, 0, -1 /* y + 1 */
4634 /* Value is the intensity of the color whose red/green/blue values
4635 are R, G, and B. */
4637 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4640 /* On frame F, return an array of XColor structures describing image
4641 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4642 means also fill the red/green/blue members of the XColor
4643 structures. Value is a pointer to the array of XColors structures,
4644 allocated with xmalloc; it must be freed by the caller. */
4646 static XColor *
4647 x_to_xcolors (struct frame *f, struct image *img, bool rgb_p)
4649 int x, y;
4650 XColor *colors, *p;
4651 XImagePtr_or_DC ximg;
4652 #ifdef HAVE_NTGUI
4653 HGDIOBJ prev;
4654 #endif /* HAVE_NTGUI */
4656 if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width)
4657 memory_full (SIZE_MAX);
4658 colors = xmalloc (sizeof *colors * img->width * img->height);
4660 /* Get the X image or create a memory device context for IMG. */
4661 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
4663 /* Fill the `pixel' members of the XColor array. I wished there
4664 were an easy and portable way to circumvent XGetPixel. */
4665 p = colors;
4666 for (y = 0; y < img->height; ++y)
4668 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4669 XColor *row = p;
4670 for (x = 0; x < img->width; ++x, ++p)
4671 p->pixel = GET_PIXEL (ximg, x, y);
4672 if (rgb_p)
4673 x_query_colors (f, row, img->width);
4675 #else
4677 for (x = 0; x < img->width; ++x, ++p)
4679 /* W32_TODO: palette support needed here? */
4680 p->pixel = GET_PIXEL (ximg, x, y);
4681 if (rgb_p)
4683 p->red = RED16_FROM_ULONG (p->pixel);
4684 p->green = GREEN16_FROM_ULONG (p->pixel);
4685 p->blue = BLUE16_FROM_ULONG (p->pixel);
4688 #endif /* HAVE_X_WINDOWS */
4691 image_unget_x_image_or_dc (img, 0, ximg, prev);
4693 return colors;
4696 #ifdef HAVE_NTGUI
4698 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4699 created with CreateDIBSection, with the pointer to the bit values
4700 stored in ximg->data. */
4702 static void
4703 XPutPixel (XImagePtr ximg, int x, int y, COLORREF color)
4705 int width = ximg->info.bmiHeader.biWidth;
4706 unsigned char * pixel;
4708 /* True color images. */
4709 if (ximg->info.bmiHeader.biBitCount == 24)
4711 int rowbytes = width * 3;
4712 /* Ensure scanlines are aligned on 4 byte boundaries. */
4713 if (rowbytes % 4)
4714 rowbytes += 4 - (rowbytes % 4);
4716 pixel = ximg->data + y * rowbytes + x * 3;
4717 /* Windows bitmaps are in BGR order. */
4718 *pixel = GetBValue (color);
4719 *(pixel + 1) = GetGValue (color);
4720 *(pixel + 2) = GetRValue (color);
4722 /* Monochrome images. */
4723 else if (ximg->info.bmiHeader.biBitCount == 1)
4725 int rowbytes = width / 8;
4726 /* Ensure scanlines are aligned on 4 byte boundaries. */
4727 if (rowbytes % 4)
4728 rowbytes += 4 - (rowbytes % 4);
4729 pixel = ximg->data + y * rowbytes + x / 8;
4730 /* Filter out palette info. */
4731 if (color & 0x00ffffff)
4732 *pixel = *pixel | (1 << x % 8);
4733 else
4734 *pixel = *pixel & ~(1 << x % 8);
4736 else
4737 image_error ("XPutPixel: palette image not supported", Qnil, Qnil);
4740 #endif /* HAVE_NTGUI */
4742 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4743 RGB members are set. F is the frame on which this all happens.
4744 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4746 static void
4747 x_from_xcolors (struct frame *f, struct image *img, XColor *colors)
4749 int x, y;
4750 XImagePtr oimg = NULL;
4751 XColor *p;
4753 init_color_table ();
4755 x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_COLORS);
4756 image_create_x_image_and_pixmap (f, img, img->width, img->height, 0,
4757 &oimg, 0);
4758 p = colors;
4759 for (y = 0; y < img->height; ++y)
4760 for (x = 0; x < img->width; ++x, ++p)
4762 unsigned long pixel;
4763 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
4764 XPutPixel (oimg, x, y, pixel);
4767 xfree (colors);
4769 image_put_x_image (f, img, oimg, 0);
4770 #ifdef COLOR_TABLE_SUPPORT
4771 img->colors = colors_in_color_table (&img->ncolors);
4772 free_color_table ();
4773 #endif /* COLOR_TABLE_SUPPORT */
4777 /* On frame F, perform edge-detection on image IMG.
4779 MATRIX is a nine-element array specifying the transformation
4780 matrix. See emboss_matrix for an example.
4782 COLOR_ADJUST is a color adjustment added to each pixel of the
4783 outgoing image. */
4785 static void
4786 x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjust)
4788 XColor *colors = x_to_xcolors (f, img, 1);
4789 XColor *new, *p;
4790 int x, y, i, sum;
4792 for (i = sum = 0; i < 9; ++i)
4793 sum += eabs (matrix[i]);
4795 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4797 if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width)
4798 memory_full (SIZE_MAX);
4799 new = xmalloc (sizeof *new * img->width * img->height);
4801 for (y = 0; y < img->height; ++y)
4803 p = COLOR (new, 0, y);
4804 p->red = p->green = p->blue = 0xffff/2;
4805 p = COLOR (new, img->width - 1, y);
4806 p->red = p->green = p->blue = 0xffff/2;
4809 for (x = 1; x < img->width - 1; ++x)
4811 p = COLOR (new, x, 0);
4812 p->red = p->green = p->blue = 0xffff/2;
4813 p = COLOR (new, x, img->height - 1);
4814 p->red = p->green = p->blue = 0xffff/2;
4817 for (y = 1; y < img->height - 1; ++y)
4819 p = COLOR (new, 1, y);
4821 for (x = 1; x < img->width - 1; ++x, ++p)
4823 int r, g, b, yy, xx;
4825 r = g = b = i = 0;
4826 for (yy = y - 1; yy < y + 2; ++yy)
4827 for (xx = x - 1; xx < x + 2; ++xx, ++i)
4828 if (matrix[i])
4830 XColor *t = COLOR (colors, xx, yy);
4831 r += matrix[i] * t->red;
4832 g += matrix[i] * t->green;
4833 b += matrix[i] * t->blue;
4836 r = (r / sum + color_adjust) & 0xffff;
4837 g = (g / sum + color_adjust) & 0xffff;
4838 b = (b / sum + color_adjust) & 0xffff;
4839 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
4843 xfree (colors);
4844 x_from_xcolors (f, img, new);
4846 #undef COLOR
4850 /* Perform the pre-defined `emboss' edge-detection on image IMG
4851 on frame F. */
4853 static void
4854 x_emboss (struct frame *f, struct image *img)
4856 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
4860 /* Transform image IMG which is used on frame F with a Laplace
4861 edge-detection algorithm. The result is an image that can be used
4862 to draw disabled buttons, for example. */
4864 static void
4865 x_laplace (struct frame *f, struct image *img)
4867 x_detect_edges (f, img, laplace_matrix, 45000);
4871 /* Perform edge-detection on image IMG on frame F, with specified
4872 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4874 MATRIX must be either
4876 - a list of at least 9 numbers in row-major form
4877 - a vector of at least 9 numbers
4879 COLOR_ADJUST nil means use a default; otherwise it must be a
4880 number. */
4882 static void
4883 x_edge_detection (struct frame *f, struct image *img, Lisp_Object matrix,
4884 Lisp_Object color_adjust)
4886 int i = 0;
4887 int trans[9];
4889 if (CONSP (matrix))
4891 for (i = 0;
4892 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
4893 ++i, matrix = XCDR (matrix))
4894 trans[i] = XFLOATINT (XCAR (matrix));
4896 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
4898 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
4899 trans[i] = XFLOATINT (AREF (matrix, i));
4902 if (NILP (color_adjust))
4903 color_adjust = make_number (0xffff / 2);
4905 if (i == 9 && NUMBERP (color_adjust))
4906 x_detect_edges (f, img, trans, XFLOATINT (color_adjust));
4910 /* Transform image IMG on frame F so that it looks disabled. */
4912 static void
4913 x_disable_image (struct frame *f, struct image *img)
4915 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
4916 #ifdef HAVE_NTGUI
4917 int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits;
4918 #else
4919 int n_planes = dpyinfo->n_planes;
4920 #endif /* HAVE_NTGUI */
4922 if (n_planes >= 2)
4924 /* Color (or grayscale). Convert to gray, and equalize. Just
4925 drawing such images with a stipple can look very odd, so
4926 we're using this method instead. */
4927 XColor *colors = x_to_xcolors (f, img, 1);
4928 XColor *p, *end;
4929 const int h = 15000;
4930 const int l = 30000;
4932 for (p = colors, end = colors + img->width * img->height;
4933 p < end;
4934 ++p)
4936 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
4937 int i2 = (0xffff - h - l) * i / 0xffff + l;
4938 p->red = p->green = p->blue = i2;
4941 x_from_xcolors (f, img, colors);
4944 /* Draw a cross over the disabled image, if we must or if we
4945 should. */
4946 if (n_planes < 2 || cross_disabled_images)
4948 #ifndef HAVE_NTGUI
4949 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4951 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4953 Display *dpy = FRAME_X_DISPLAY (f);
4954 GC gc;
4956 image_sync_to_pixmaps (f, img);
4957 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
4958 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
4959 XDrawLine (dpy, img->pixmap, gc, 0, 0,
4960 img->width - 1, img->height - 1);
4961 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
4962 img->width - 1, 0);
4963 XFreeGC (dpy, gc);
4965 if (img->mask)
4967 gc = XCreateGC (dpy, img->mask, 0, NULL);
4968 XSetForeground (dpy, gc, MaskForeground (f));
4969 XDrawLine (dpy, img->mask, gc, 0, 0,
4970 img->width - 1, img->height - 1);
4971 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
4972 img->width - 1, 0);
4973 XFreeGC (dpy, gc);
4975 #endif /* !HAVE_NS */
4976 #else
4977 HDC hdc, bmpdc;
4978 HGDIOBJ prev;
4980 hdc = get_frame_dc (f);
4981 bmpdc = CreateCompatibleDC (hdc);
4982 release_frame_dc (f, hdc);
4984 prev = SelectObject (bmpdc, img->pixmap);
4986 SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f));
4987 MoveToEx (bmpdc, 0, 0, NULL);
4988 LineTo (bmpdc, img->width - 1, img->height - 1);
4989 MoveToEx (bmpdc, 0, img->height - 1, NULL);
4990 LineTo (bmpdc, img->width - 1, 0);
4992 if (img->mask)
4994 SelectObject (bmpdc, img->mask);
4995 SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f));
4996 MoveToEx (bmpdc, 0, 0, NULL);
4997 LineTo (bmpdc, img->width - 1, img->height - 1);
4998 MoveToEx (bmpdc, 0, img->height - 1, NULL);
4999 LineTo (bmpdc, img->width - 1, 0);
5001 SelectObject (bmpdc, prev);
5002 DeleteDC (bmpdc);
5003 #endif /* HAVE_NTGUI */
5008 /* Build a mask for image IMG which is used on frame F. FILE is the
5009 name of an image file, for error messages. HOW determines how to
5010 determine the background color of IMG. If it is a list '(R G B)',
5011 with R, G, and B being integers >= 0, take that as the color of the
5012 background. Otherwise, determine the background color of IMG
5013 heuristically. */
5015 static void
5016 x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how)
5018 XImagePtr_or_DC ximg;
5019 #ifndef HAVE_NTGUI
5020 XImagePtr mask_img;
5021 #else
5022 HGDIOBJ prev;
5023 char *mask_img;
5024 int row_width;
5025 #endif /* HAVE_NTGUI */
5026 int x, y;
5027 bool use_img_background;
5028 unsigned long bg = 0;
5030 if (img->mask)
5031 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
5033 #ifndef HAVE_NTGUI
5034 #ifndef HAVE_NS
5035 /* Create an image and pixmap serving as mask. */
5036 if (! image_create_x_image_and_pixmap (f, img, img->width, img->height, 1,
5037 &mask_img, 1))
5038 return;
5039 #endif /* !HAVE_NS */
5040 #else
5041 /* Create the bit array serving as mask. */
5042 row_width = (img->width + 7) / 8;
5043 mask_img = xzalloc (row_width * img->height);
5044 #endif /* HAVE_NTGUI */
5046 /* Get the X image or create a memory device context for IMG. */
5047 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
5049 /* Determine the background color of ximg. If HOW is `(R G B)'
5050 take that as color. Otherwise, use the image's background color. */
5051 use_img_background = 1;
5053 if (CONSP (how))
5055 int rgb[3], i;
5057 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
5059 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
5060 how = XCDR (how);
5063 if (i == 3 && NILP (how))
5065 char color_name[30];
5066 sprintf (color_name, "#%04x%04x%04x",
5067 rgb[0] + 0u, rgb[1] + 0u, rgb[2] + 0u);
5068 bg = (
5069 #ifdef HAVE_NTGUI
5070 0x00ffffff & /* Filter out palette info. */
5071 #endif /* HAVE_NTGUI */
5072 x_alloc_image_color (f, img, build_string (color_name), 0));
5073 use_img_background = 0;
5077 if (use_img_background)
5078 bg = four_corners_best (ximg, img->corners, img->width, img->height);
5080 /* Set all bits in mask_img to 1 whose color in ximg is different
5081 from the background color bg. */
5082 #ifndef HAVE_NTGUI
5083 for (y = 0; y < img->height; ++y)
5084 for (x = 0; x < img->width; ++x)
5085 #ifndef HAVE_NS
5086 XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg
5087 ? PIX_MASK_DRAW : PIX_MASK_RETAIN));
5088 #else
5089 if (XGetPixel (ximg, x, y) == bg)
5090 ns_set_alpha (ximg, x, y, 0);
5091 #endif /* HAVE_NS */
5092 #ifndef HAVE_NS
5093 /* Fill in the background_transparent field while we have the mask handy. */
5094 image_background_transparent (img, f, mask_img);
5096 /* Put mask_img into the image. */
5097 image_put_x_image (f, img, mask_img, 1);
5098 #endif /* !HAVE_NS */
5099 #else
5100 for (y = 0; y < img->height; ++y)
5101 for (x = 0; x < img->width; ++x)
5103 COLORREF p = GetPixel (ximg, x, y);
5104 if (p != bg)
5105 mask_img[y * row_width + x / 8] |= 1 << (x % 8);
5108 /* Create the mask image. */
5109 img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height,
5110 mask_img);
5111 /* Fill in the background_transparent field while we have the mask handy. */
5112 SelectObject (ximg, img->mask);
5113 image_background_transparent (img, f, ximg);
5115 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5116 xfree (mask_img);
5117 #endif /* HAVE_NTGUI */
5119 image_unget_x_image_or_dc (img, 0, ximg, prev);
5123 /***********************************************************************
5124 PBM (mono, gray, color)
5125 ***********************************************************************/
5127 static bool pbm_image_p (Lisp_Object object);
5128 static bool pbm_load (struct frame *f, struct image *img);
5130 /* Indices of image specification fields in gs_format, below. */
5132 enum pbm_keyword_index
5134 PBM_TYPE,
5135 PBM_FILE,
5136 PBM_DATA,
5137 PBM_ASCENT,
5138 PBM_MARGIN,
5139 PBM_RELIEF,
5140 PBM_ALGORITHM,
5141 PBM_HEURISTIC_MASK,
5142 PBM_MASK,
5143 PBM_FOREGROUND,
5144 PBM_BACKGROUND,
5145 PBM_LAST
5148 /* Vector of image_keyword structures describing the format
5149 of valid user-defined image specifications. */
5151 static const struct image_keyword pbm_format[PBM_LAST] =
5153 {":type", IMAGE_SYMBOL_VALUE, 1},
5154 {":file", IMAGE_STRING_VALUE, 0},
5155 {":data", IMAGE_STRING_VALUE, 0},
5156 {":ascent", IMAGE_ASCENT_VALUE, 0},
5157 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
5158 {":relief", IMAGE_INTEGER_VALUE, 0},
5159 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5160 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5161 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5162 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
5163 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5166 /* Structure describing the image type `pbm'. */
5168 static struct image_type pbm_type =
5170 SYMBOL_INDEX (Qpbm),
5171 pbm_image_p,
5172 pbm_load,
5173 x_clear_image,
5174 NULL,
5175 NULL
5179 /* Return true if OBJECT is a valid PBM image specification. */
5181 static bool
5182 pbm_image_p (Lisp_Object object)
5184 struct image_keyword fmt[PBM_LAST];
5186 memcpy (fmt, pbm_format, sizeof fmt);
5188 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
5189 return 0;
5191 /* Must specify either :data or :file. */
5192 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
5196 /* Get next char skipping comments in Netpbm header. Returns -1 at
5197 end of input. */
5199 static int
5200 pbm_next_char (unsigned char **s, unsigned char *end)
5202 int c = -1;
5204 while (*s < end && (c = *(*s)++, c == '#'))
5206 /* Skip to the next line break. */
5207 while (*s < end && (c = *(*s)++, c != '\n' && c != '\r'))
5210 c = -1;
5213 return c;
5217 /* Scan a decimal number from *S and return it. Advance *S while
5218 reading the number. END is the end of the string. Value is -1 at
5219 end of input. */
5221 static int
5222 pbm_scan_number (unsigned char **s, unsigned char *end)
5224 int c = 0, val = -1;
5226 /* Skip white-space. */
5227 while ((c = pbm_next_char (s, end)) != -1 && c_isspace (c))
5230 if (c_isdigit (c))
5232 /* Read decimal number. */
5233 val = c - '0';
5234 while ((c = pbm_next_char (s, end)) != -1 && c_isdigit (c))
5235 val = 10 * val + c - '0';
5238 return val;
5242 /* Load PBM image IMG for use on frame F. */
5244 static bool
5245 pbm_load (struct frame *f, struct image *img)
5247 bool raw_p;
5248 int x, y;
5249 int width, height, max_color_idx = 0;
5250 Lisp_Object file, specified_file;
5251 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
5252 unsigned char *contents = NULL;
5253 unsigned char *end, *p;
5254 ptrdiff_t size;
5255 #ifdef USE_CAIRO
5256 unsigned char *data = 0;
5257 uint32_t *dataptr;
5258 #else
5259 XImagePtr ximg;
5260 #endif
5262 specified_file = image_spec_value (img->spec, QCfile, NULL);
5264 if (STRINGP (specified_file))
5266 file = x_find_image_file (specified_file);
5267 if (!STRINGP (file))
5269 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5270 return 0;
5273 contents = slurp_file (SSDATA (file), &size);
5274 if (contents == NULL)
5276 image_error ("Error reading `%s'", file, Qnil);
5277 return 0;
5280 p = contents;
5281 end = contents + size;
5283 else
5285 Lisp_Object data;
5286 data = image_spec_value (img->spec, QCdata, NULL);
5287 if (!STRINGP (data))
5289 image_error ("Invalid image data `%s'", data, Qnil);
5290 return 0;
5292 p = SDATA (data);
5293 end = p + SBYTES (data);
5296 /* Check magic number. */
5297 if (end - p < 2 || *p++ != 'P')
5299 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5300 error:
5301 xfree (contents);
5302 img->pixmap = NO_PIXMAP;
5303 return 0;
5306 switch (*p++)
5308 case '1':
5309 raw_p = 0, type = PBM_MONO;
5310 break;
5312 case '2':
5313 raw_p = 0, type = PBM_GRAY;
5314 break;
5316 case '3':
5317 raw_p = 0, type = PBM_COLOR;
5318 break;
5320 case '4':
5321 raw_p = 1, type = PBM_MONO;
5322 break;
5324 case '5':
5325 raw_p = 1, type = PBM_GRAY;
5326 break;
5328 case '6':
5329 raw_p = 1, type = PBM_COLOR;
5330 break;
5332 default:
5333 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5334 goto error;
5337 /* Read width, height, maximum color-component. Characters
5338 starting with `#' up to the end of a line are ignored. */
5339 width = pbm_scan_number (&p, end);
5340 height = pbm_scan_number (&p, end);
5342 #ifdef USE_CAIRO
5343 data = (unsigned char *) xmalloc (width * height * 4);
5344 dataptr = (uint32_t *) data;
5345 #endif
5347 if (type != PBM_MONO)
5349 max_color_idx = pbm_scan_number (&p, end);
5350 if (max_color_idx > 65535 || max_color_idx < 0)
5352 image_error ("Unsupported maximum PBM color value", Qnil, Qnil);
5353 goto error;
5357 if (!check_image_size (f, width, height))
5359 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
5360 goto error;
5363 #ifndef USE_CAIRO
5364 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
5365 goto error;
5366 #endif
5368 /* Initialize the color hash table. */
5369 init_color_table ();
5371 if (type == PBM_MONO)
5373 int c = 0, g;
5374 struct image_keyword fmt[PBM_LAST];
5375 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
5376 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
5377 #ifdef USE_CAIRO
5378 XColor xfg, xbg;
5379 int fga32, bga32;
5380 #endif
5381 /* Parse the image specification. */
5382 memcpy (fmt, pbm_format, sizeof fmt);
5383 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
5385 /* Get foreground and background colors, maybe allocate colors. */
5386 #ifdef USE_CAIRO
5387 if (! fmt[PBM_FOREGROUND].count
5388 || ! STRINGP (fmt[PBM_FOREGROUND].value)
5389 || ! x_defined_color (f, SSDATA (fmt[PBM_FOREGROUND].value), &xfg, 0))
5391 xfg.pixel = fg;
5392 x_query_color (f, &xfg);
5394 fga32 = xcolor_to_argb32 (xfg);
5396 if (! fmt[PBM_BACKGROUND].count
5397 || ! STRINGP (fmt[PBM_BACKGROUND].value)
5398 || ! x_defined_color (f, SSDATA (fmt[PBM_BACKGROUND].value), &xbg, 0))
5400 xbg.pixel = bg;
5401 x_query_color (f, &xbg);
5403 bga32 = xcolor_to_argb32 (xbg);
5404 #else
5405 if (fmt[PBM_FOREGROUND].count
5406 && STRINGP (fmt[PBM_FOREGROUND].value))
5407 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
5408 if (fmt[PBM_BACKGROUND].count
5409 && STRINGP (fmt[PBM_BACKGROUND].value))
5411 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
5412 img->background = bg;
5413 img->background_valid = 1;
5415 #endif
5417 for (y = 0; y < height; ++y)
5418 for (x = 0; x < width; ++x)
5420 if (raw_p)
5422 if ((x & 7) == 0)
5424 if (p >= end)
5426 #ifdef USE_CAIRO
5427 xfree (data);
5428 #else
5429 x_destroy_x_image (ximg);
5430 #endif
5431 x_clear_image (f, img);
5432 image_error ("Invalid image size in image `%s'",
5433 img->spec, Qnil);
5434 goto error;
5436 c = *p++;
5438 g = c & 0x80;
5439 c <<= 1;
5441 else
5442 g = pbm_scan_number (&p, end);
5444 #ifdef USE_CAIRO
5445 *dataptr++ = g ? fga32 : bga32;
5446 #else
5447 XPutPixel (ximg, x, y, g ? fg : bg);
5448 #endif
5451 else
5453 int expected_size = height * width;
5454 if (max_color_idx > 255)
5455 expected_size *= 2;
5456 if (type == PBM_COLOR)
5457 expected_size *= 3;
5459 if (raw_p && p + expected_size > end)
5461 #ifdef USE_CAIRO
5462 xfree (data);
5463 #else
5464 x_destroy_x_image (ximg);
5465 #endif
5466 x_clear_image (f, img);
5467 image_error ("Invalid image size in image `%s'",
5468 img->spec, Qnil);
5469 goto error;
5472 for (y = 0; y < height; ++y)
5473 for (x = 0; x < width; ++x)
5475 int r, g, b;
5477 if (type == PBM_GRAY && raw_p)
5479 r = g = b = *p++;
5480 if (max_color_idx > 255)
5481 r = g = b = r * 256 + *p++;
5483 else if (type == PBM_GRAY)
5484 r = g = b = pbm_scan_number (&p, end);
5485 else if (raw_p)
5487 r = *p++;
5488 if (max_color_idx > 255)
5489 r = r * 256 + *p++;
5490 g = *p++;
5491 if (max_color_idx > 255)
5492 g = g * 256 + *p++;
5493 b = *p++;
5494 if (max_color_idx > 255)
5495 b = b * 256 + *p++;
5497 else
5499 r = pbm_scan_number (&p, end);
5500 g = pbm_scan_number (&p, end);
5501 b = pbm_scan_number (&p, end);
5504 if (r < 0 || g < 0 || b < 0)
5506 #ifdef USE_CAIRO
5507 xfree (data);
5508 #else
5509 x_destroy_x_image (ximg);
5510 #endif
5511 image_error ("Invalid pixel value in image `%s'",
5512 img->spec, Qnil);
5513 goto error;
5516 #ifdef USE_CAIRO
5517 r = (double) r * 255 / max_color_idx;
5518 g = (double) g * 255 / max_color_idx;
5519 b = (double) b * 255 / max_color_idx;
5520 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
5521 #else
5522 /* RGB values are now in the range 0..max_color_idx.
5523 Scale this to the range 0..0xffff supported by X. */
5524 r = (double) r * 65535 / max_color_idx;
5525 g = (double) g * 65535 / max_color_idx;
5526 b = (double) b * 65535 / max_color_idx;
5527 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
5528 #endif
5532 #ifdef COLOR_TABLE_SUPPORT
5533 /* Store in IMG->colors the colors allocated for the image, and
5534 free the color table. */
5535 img->colors = colors_in_color_table (&img->ncolors);
5536 free_color_table ();
5537 #endif /* COLOR_TABLE_SUPPORT */
5539 img->width = width;
5540 img->height = height;
5542 /* Maybe fill in the background field while we have ximg handy. */
5544 #ifdef USE_CAIRO
5545 create_cairo_image_surface (img, data, width, height);
5546 #else
5547 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
5548 /* Casting avoids a GCC warning. */
5549 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
5551 /* Put ximg into the image. */
5552 image_put_x_image (f, img, ximg, 0);
5553 #endif
5555 /* X and W32 versions did it here, MAC version above. ++kfs
5556 img->width = width;
5557 img->height = height; */
5559 xfree (contents);
5560 return 1;
5564 /***********************************************************************
5566 ***********************************************************************/
5568 #if defined (HAVE_PNG) || defined (HAVE_NS) || defined (USE_CAIRO)
5570 /* Function prototypes. */
5572 static bool png_image_p (Lisp_Object object);
5573 static bool png_load (struct frame *f, struct image *img);
5575 /* Indices of image specification fields in png_format, below. */
5577 enum png_keyword_index
5579 PNG_TYPE,
5580 PNG_DATA,
5581 PNG_FILE,
5582 PNG_ASCENT,
5583 PNG_MARGIN,
5584 PNG_RELIEF,
5585 PNG_ALGORITHM,
5586 PNG_HEURISTIC_MASK,
5587 PNG_MASK,
5588 PNG_BACKGROUND,
5589 PNG_LAST
5592 /* Vector of image_keyword structures describing the format
5593 of valid user-defined image specifications. */
5595 static const struct image_keyword png_format[PNG_LAST] =
5597 {":type", IMAGE_SYMBOL_VALUE, 1},
5598 {":data", IMAGE_STRING_VALUE, 0},
5599 {":file", IMAGE_STRING_VALUE, 0},
5600 {":ascent", IMAGE_ASCENT_VALUE, 0},
5601 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
5602 {":relief", IMAGE_INTEGER_VALUE, 0},
5603 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5604 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5605 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5606 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5609 #if defined HAVE_NTGUI && defined WINDOWSNT
5610 static bool init_png_functions (void);
5611 #else
5612 #define init_png_functions NULL
5613 #endif
5615 /* Structure describing the image type `png'. */
5617 static struct image_type png_type =
5619 SYMBOL_INDEX (Qpng),
5620 png_image_p,
5621 png_load,
5622 x_clear_image,
5623 init_png_functions,
5624 NULL
5627 /* Return true if OBJECT is a valid PNG image specification. */
5629 static bool
5630 png_image_p (Lisp_Object object)
5632 struct image_keyword fmt[PNG_LAST];
5633 memcpy (fmt, png_format, sizeof fmt);
5635 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
5636 return 0;
5638 /* Must specify either the :data or :file keyword. */
5639 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
5642 #endif /* HAVE_PNG || HAVE_NS || USE_CAIRO */
5645 #if (defined HAVE_PNG && !defined HAVE_NS) || defined USE_CAIRO
5647 # ifdef WINDOWSNT
5648 /* PNG library details. */
5650 DEF_DLL_FN (png_voidp, png_get_io_ptr, (png_structp));
5651 DEF_DLL_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t));
5652 DEF_DLL_FN (png_structp, png_create_read_struct,
5653 (png_const_charp, png_voidp, png_error_ptr, png_error_ptr));
5654 DEF_DLL_FN (png_infop, png_create_info_struct, (png_structp));
5655 DEF_DLL_FN (void, png_destroy_read_struct,
5656 (png_structpp, png_infopp, png_infopp));
5657 DEF_DLL_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr));
5658 DEF_DLL_FN (void, png_set_sig_bytes, (png_structp, int));
5659 DEF_DLL_FN (void, png_read_info, (png_structp, png_infop));
5660 DEF_DLL_FN (png_uint_32, png_get_IHDR,
5661 (png_structp, png_infop, png_uint_32 *, png_uint_32 *,
5662 int *, int *, int *, int *, int *));
5663 DEF_DLL_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32));
5664 DEF_DLL_FN (void, png_set_strip_16, (png_structp));
5665 DEF_DLL_FN (void, png_set_expand, (png_structp));
5666 DEF_DLL_FN (void, png_set_gray_to_rgb, (png_structp));
5667 DEF_DLL_FN (void, png_set_background,
5668 (png_structp, png_color_16p, int, int, double));
5669 DEF_DLL_FN (png_uint_32, png_get_bKGD,
5670 (png_structp, png_infop, png_color_16p *));
5671 DEF_DLL_FN (void, png_read_update_info, (png_structp, png_infop));
5672 DEF_DLL_FN (png_byte, png_get_channels, (png_structp, png_infop));
5673 DEF_DLL_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop));
5674 DEF_DLL_FN (void, png_read_image, (png_structp, png_bytepp));
5675 DEF_DLL_FN (void, png_read_end, (png_structp, png_infop));
5676 DEF_DLL_FN (void, png_error, (png_structp, png_const_charp));
5678 # if (PNG_LIBPNG_VER >= 10500)
5679 DEF_DLL_FN (void, png_longjmp, (png_structp, int)) PNG_NORETURN;
5680 DEF_DLL_FN (jmp_buf *, png_set_longjmp_fn,
5681 (png_structp, png_longjmp_ptr, size_t));
5682 # endif /* libpng version >= 1.5 */
5684 static bool
5685 init_png_functions (void)
5687 HMODULE library;
5689 if (!(library = w32_delayed_load (Qpng)))
5690 return 0;
5692 LOAD_DLL_FN (library, png_get_io_ptr);
5693 LOAD_DLL_FN (library, png_sig_cmp);
5694 LOAD_DLL_FN (library, png_create_read_struct);
5695 LOAD_DLL_FN (library, png_create_info_struct);
5696 LOAD_DLL_FN (library, png_destroy_read_struct);
5697 LOAD_DLL_FN (library, png_set_read_fn);
5698 LOAD_DLL_FN (library, png_set_sig_bytes);
5699 LOAD_DLL_FN (library, png_read_info);
5700 LOAD_DLL_FN (library, png_get_IHDR);
5701 LOAD_DLL_FN (library, png_get_valid);
5702 LOAD_DLL_FN (library, png_set_strip_16);
5703 LOAD_DLL_FN (library, png_set_expand);
5704 LOAD_DLL_FN (library, png_set_gray_to_rgb);
5705 LOAD_DLL_FN (library, png_set_background);
5706 LOAD_DLL_FN (library, png_get_bKGD);
5707 LOAD_DLL_FN (library, png_read_update_info);
5708 LOAD_DLL_FN (library, png_get_channels);
5709 LOAD_DLL_FN (library, png_get_rowbytes);
5710 LOAD_DLL_FN (library, png_read_image);
5711 LOAD_DLL_FN (library, png_read_end);
5712 LOAD_DLL_FN (library, png_error);
5714 # if (PNG_LIBPNG_VER >= 10500)
5715 LOAD_DLL_FN (library, png_longjmp);
5716 LOAD_DLL_FN (library, png_set_longjmp_fn);
5717 # endif /* libpng version >= 1.5 */
5719 return 1;
5722 # undef png_create_info_struct
5723 # undef png_create_read_struct
5724 # undef png_destroy_read_struct
5725 # undef png_error
5726 # undef png_get_bKGD
5727 # undef png_get_channels
5728 # undef png_get_IHDR
5729 # undef png_get_io_ptr
5730 # undef png_get_rowbytes
5731 # undef png_get_valid
5732 # undef png_longjmp
5733 # undef png_read_end
5734 # undef png_read_image
5735 # undef png_read_info
5736 # undef png_read_update_info
5737 # undef png_set_background
5738 # undef png_set_expand
5739 # undef png_set_gray_to_rgb
5740 # undef png_set_longjmp_fn
5741 # undef png_set_read_fn
5742 # undef png_set_sig_bytes
5743 # undef png_set_strip_16
5744 # undef png_sig_cmp
5746 # define png_create_info_struct fn_png_create_info_struct
5747 # define png_create_read_struct fn_png_create_read_struct
5748 # define png_destroy_read_struct fn_png_destroy_read_struct
5749 # define png_error fn_png_error
5750 # define png_get_bKGD fn_png_get_bKGD
5751 # define png_get_channels fn_png_get_channels
5752 # define png_get_IHDR fn_png_get_IHDR
5753 # define png_get_io_ptr fn_png_get_io_ptr
5754 # define png_get_rowbytes fn_png_get_rowbytes
5755 # define png_get_valid fn_png_get_valid
5756 # define png_longjmp fn_png_longjmp
5757 # define png_read_end fn_png_read_end
5758 # define png_read_image fn_png_read_image
5759 # define png_read_info fn_png_read_info
5760 # define png_read_update_info fn_png_read_update_info
5761 # define png_set_background fn_png_set_background
5762 # define png_set_expand fn_png_set_expand
5763 # define png_set_gray_to_rgb fn_png_set_gray_to_rgb
5764 # define png_set_longjmp_fn fn_png_set_longjmp_fn
5765 # define png_set_read_fn fn_png_set_read_fn
5766 # define png_set_sig_bytes fn_png_set_sig_bytes
5767 # define png_set_strip_16 fn_png_set_strip_16
5768 # define png_sig_cmp fn_png_sig_cmp
5770 # endif /* WINDOWSNT */
5772 /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp
5773 will do, POSIX _setjmp and _longjmp (if available) are often faster.
5774 Do not use sys_setjmp, as PNG supports only jmp_buf.
5775 It's OK if the longjmp substitute restores the signal mask. */
5776 # ifdef HAVE__SETJMP
5777 # define FAST_SETJMP(j) _setjmp (j)
5778 # define FAST_LONGJMP _longjmp
5779 # else
5780 # define FAST_SETJMP(j) setjmp (j)
5781 # define FAST_LONGJMP longjmp
5782 # endif
5784 # if PNG_LIBPNG_VER < 10500
5785 # define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1)
5786 # define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5787 # else
5788 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5789 # define PNG_LONGJMP(ptr) png_longjmp (ptr, 1)
5790 # define PNG_JMPBUF(ptr) \
5791 (*png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf)))
5792 # endif
5794 /* Error and warning handlers installed when the PNG library
5795 is initialized. */
5797 static _Noreturn void
5798 my_png_error (png_struct *png_ptr, const char *msg)
5800 eassert (png_ptr != NULL);
5801 /* Avoid compiler warning about deprecated direct access to
5802 png_ptr's fields in libpng versions 1.4.x. */
5803 image_error ("PNG error: %s", build_string (msg), Qnil);
5804 PNG_LONGJMP (png_ptr);
5808 static void
5809 my_png_warning (png_struct *png_ptr, const char *msg)
5811 eassert (png_ptr != NULL);
5812 image_error ("PNG warning: %s", build_string (msg), Qnil);
5815 /* Memory source for PNG decoding. */
5817 struct png_memory_storage
5819 unsigned char *bytes; /* The data */
5820 ptrdiff_t len; /* How big is it? */
5821 ptrdiff_t index; /* Where are we? */
5825 /* Function set as reader function when reading PNG image from memory.
5826 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5827 bytes from the input to DATA. */
5829 static void
5830 png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length)
5832 struct png_memory_storage *tbr = png_get_io_ptr (png_ptr);
5834 if (length > tbr->len - tbr->index)
5835 png_error (png_ptr, "Read error");
5837 memcpy (data, tbr->bytes + tbr->index, length);
5838 tbr->index = tbr->index + length;
5842 /* Function set as reader function when reading PNG image from a file.
5843 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5844 bytes from the input to DATA. */
5846 static void
5847 png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length)
5849 FILE *fp = png_get_io_ptr (png_ptr);
5851 if (fread (data, 1, length, fp) < length)
5852 png_error (png_ptr, "Read error");
5856 /* Load PNG image IMG for use on frame F. Value is true if
5857 successful. */
5859 struct png_load_context
5861 /* These are members so that longjmp doesn't munge local variables. */
5862 png_struct *png_ptr;
5863 png_info *info_ptr;
5864 png_info *end_info;
5865 FILE *fp;
5866 png_byte *pixels;
5867 png_byte **rows;
5870 static bool
5871 png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
5873 Lisp_Object file, specified_file;
5874 Lisp_Object specified_data;
5875 int x, y;
5876 ptrdiff_t i;
5877 png_struct *png_ptr;
5878 png_info *info_ptr = NULL, *end_info = NULL;
5879 FILE *fp = NULL;
5880 png_byte sig[8];
5881 png_byte *pixels = NULL;
5882 png_byte **rows = NULL;
5883 png_uint_32 width, height;
5884 int bit_depth, color_type, interlace_type;
5885 png_byte channels;
5886 png_uint_32 row_bytes;
5887 bool transparent_p;
5888 struct png_memory_storage tbr; /* Data to be read */
5890 #ifdef USE_CAIRO
5891 unsigned char *data = 0;
5892 uint32_t *dataptr;
5893 #else
5894 XImagePtr ximg, mask_img = NULL;
5895 #endif
5897 /* Find out what file to load. */
5898 specified_file = image_spec_value (img->spec, QCfile, NULL);
5899 specified_data = image_spec_value (img->spec, QCdata, NULL);
5900 IF_LINT (Lisp_Object volatile specified_data_volatile = specified_data);
5902 if (NILP (specified_data))
5904 file = x_find_image_file (specified_file);
5905 if (!STRINGP (file))
5907 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5908 return 0;
5911 /* Open the image file. */
5912 fp = emacs_fopen (SSDATA (file), "rb");
5913 if (!fp)
5915 image_error ("Cannot open image file `%s'", file, Qnil);
5916 return 0;
5919 /* Check PNG signature. */
5920 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
5921 || png_sig_cmp (sig, 0, sizeof sig))
5923 fclose (fp);
5924 image_error ("Not a PNG file: `%s'", file, Qnil);
5925 return 0;
5928 else
5930 if (!STRINGP (specified_data))
5932 image_error ("Invalid image data `%s'", specified_data, Qnil);
5933 return 0;
5936 /* Read from memory. */
5937 tbr.bytes = SDATA (specified_data);
5938 tbr.len = SBYTES (specified_data);
5939 tbr.index = 0;
5941 /* Check PNG signature. */
5942 if (tbr.len < sizeof sig
5943 || png_sig_cmp (tbr.bytes, 0, sizeof sig))
5945 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
5946 return 0;
5949 /* Need to skip past the signature. */
5950 tbr.bytes += sizeof (sig);
5953 /* Initialize read and info structs for PNG lib. */
5954 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
5955 NULL, my_png_error,
5956 my_png_warning);
5957 if (png_ptr)
5959 info_ptr = png_create_info_struct (png_ptr);
5960 end_info = png_create_info_struct (png_ptr);
5963 c->png_ptr = png_ptr;
5964 c->info_ptr = info_ptr;
5965 c->end_info = end_info;
5966 c->fp = fp;
5967 c->pixels = pixels;
5968 c->rows = rows;
5970 if (! (info_ptr && end_info))
5972 png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
5973 png_ptr = 0;
5975 if (! png_ptr)
5977 if (fp) fclose (fp);
5978 return 0;
5981 /* Set error jump-back. We come back here when the PNG library
5982 detects an error. */
5983 if (FAST_SETJMP (PNG_JMPBUF (png_ptr)))
5985 error:
5986 if (c->png_ptr)
5987 png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
5988 xfree (c->pixels);
5989 xfree (c->rows);
5990 if (c->fp)
5991 fclose (c->fp);
5992 return 0;
5995 /* Silence a bogus diagnostic; see GCC bug 54561. */
5996 IF_LINT (fp = c->fp);
5997 IF_LINT (specified_data = specified_data_volatile);
5999 /* Read image info. */
6000 if (!NILP (specified_data))
6001 png_set_read_fn (png_ptr, &tbr, png_read_from_memory);
6002 else
6003 png_set_read_fn (png_ptr, fp, png_read_from_file);
6005 png_set_sig_bytes (png_ptr, sizeof sig);
6006 png_read_info (png_ptr, info_ptr);
6007 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
6008 &interlace_type, NULL, NULL);
6010 if (! (width <= INT_MAX && height <= INT_MAX
6011 && check_image_size (f, width, height)))
6013 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
6014 goto error;
6017 #ifndef USE_CAIRO
6018 /* Create the X image and pixmap now, so that the work below can be
6019 omitted if the image is too large for X. */
6020 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
6021 goto error;
6022 #endif
6024 /* If image contains simply transparency data, we prefer to
6025 construct a clipping mask. */
6026 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
6027 transparent_p = 1;
6028 else
6029 transparent_p = 0;
6031 /* This function is easier to write if we only have to handle
6032 one data format: RGB or RGBA with 8 bits per channel. Let's
6033 transform other formats into that format. */
6035 /* Strip more than 8 bits per channel. */
6036 if (bit_depth == 16)
6037 png_set_strip_16 (png_ptr);
6039 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
6040 if available. */
6041 png_set_expand (png_ptr);
6043 /* Convert grayscale images to RGB. */
6044 if (color_type == PNG_COLOR_TYPE_GRAY
6045 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6046 png_set_gray_to_rgb (png_ptr);
6048 /* Handle alpha channel by combining the image with a background
6049 color. Do this only if a real alpha channel is supplied. For
6050 simple transparency, we prefer a clipping mask. */
6051 if (!transparent_p)
6053 /* png_color_16 *image_bg; */
6054 Lisp_Object specified_bg
6055 = image_spec_value (img->spec, QCbackground, NULL);
6056 XColor color;
6058 /* If the user specified a color, try to use it; if not, use the
6059 current frame background, ignoring any default background
6060 color set by the image. */
6061 if (STRINGP (specified_bg)
6062 ? x_defined_color (f, SSDATA (specified_bg), &color, false)
6063 : (x_query_frame_background_color (f, &color), true))
6064 /* The user specified `:background', use that. */
6066 int shift = bit_depth == 16 ? 0 : 8;
6067 png_color_16 bg = { 0 };
6068 bg.red = color.red >> shift;
6069 bg.green = color.green >> shift;
6070 bg.blue = color.blue >> shift;
6072 png_set_background (png_ptr, &bg,
6073 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
6077 /* Update info structure. */
6078 png_read_update_info (png_ptr, info_ptr);
6080 /* Get number of channels. Valid values are 1 for grayscale images
6081 and images with a palette, 2 for grayscale images with transparency
6082 information (alpha channel), 3 for RGB images, and 4 for RGB
6083 images with alpha channel, i.e. RGBA. If conversions above were
6084 sufficient we should only have 3 or 4 channels here. */
6085 channels = png_get_channels (png_ptr, info_ptr);
6086 eassert (channels == 3 || channels == 4);
6088 /* Number of bytes needed for one row of the image. */
6089 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
6091 /* Allocate memory for the image. */
6092 if (height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows
6093 || row_bytes > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height)
6094 memory_full (SIZE_MAX);
6095 c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height);
6096 c->rows = rows = xmalloc (height * sizeof *rows);
6097 for (i = 0; i < height; ++i)
6098 rows[i] = pixels + i * row_bytes;
6100 /* Read the entire image. */
6101 png_read_image (png_ptr, rows);
6102 png_read_end (png_ptr, info_ptr);
6103 if (fp)
6105 fclose (fp);
6106 c->fp = NULL;
6109 #ifdef USE_CAIRO
6110 data = (unsigned char *) xmalloc (width * height * 4);
6111 dataptr = (uint32_t *) data;
6112 #else
6113 /* Create an image and pixmap serving as mask if the PNG image
6114 contains an alpha channel. */
6115 if (channels == 4
6116 && !transparent_p
6117 && !image_create_x_image_and_pixmap (f, img, width, height, 1,
6118 &mask_img, 1))
6120 x_destroy_x_image (ximg);
6121 x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP);
6122 goto error;
6124 #endif
6126 /* Fill the X image and mask from PNG data. */
6127 init_color_table ();
6129 for (y = 0; y < height; ++y)
6131 png_byte *p = rows[y];
6133 for (x = 0; x < width; ++x)
6135 int r, g, b;
6137 #ifdef USE_CAIRO
6138 int a = 0xff;
6139 r = *p++;
6140 g = *p++;
6141 b = *p++;
6142 if (channels == 4) a = *p++;
6143 *dataptr++ = (a << 24) | (r << 16) | (g << 8) | b;
6144 #else
6145 r = *p++ << 8;
6146 g = *p++ << 8;
6147 b = *p++ << 8;
6148 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
6149 /* An alpha channel, aka mask channel, associates variable
6150 transparency with an image. Where other image formats
6151 support binary transparency---fully transparent or fully
6152 opaque---PNG allows up to 254 levels of partial transparency.
6153 The PNG library implements partial transparency by combining
6154 the image with a specified background color.
6156 I'm not sure how to handle this here nicely: because the
6157 background on which the image is displayed may change, for
6158 real alpha channel support, it would be necessary to create
6159 a new image for each possible background.
6161 What I'm doing now is that a mask is created if we have
6162 boolean transparency information. Otherwise I'm using
6163 the frame's background color to combine the image with. */
6165 if (channels == 4)
6167 if (mask_img)
6168 XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN);
6169 ++p;
6171 #endif
6175 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6176 /* Set IMG's background color from the PNG image, unless the user
6177 overrode it. */
6179 png_color_16 *bg;
6180 if (png_get_bKGD (png_ptr, info_ptr, &bg))
6182 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
6183 img->background_valid = 1;
6187 # ifdef COLOR_TABLE_SUPPORT
6188 /* Remember colors allocated for this image. */
6189 img->colors = colors_in_color_table (&img->ncolors);
6190 free_color_table ();
6191 # endif /* COLOR_TABLE_SUPPORT */
6193 /* Clean up. */
6194 png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info);
6195 xfree (rows);
6196 xfree (pixels);
6198 img->width = width;
6199 img->height = height;
6201 #ifdef USE_CAIRO
6202 create_cairo_image_surface (img, data, width, height);
6203 #else
6204 /* Maybe fill in the background field while we have ximg handy.
6205 Casting avoids a GCC warning. */
6206 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6208 /* Put ximg into the image. */
6209 image_put_x_image (f, img, ximg, 0);
6211 /* Same for the mask. */
6212 if (mask_img)
6214 /* Fill in the background_transparent field while we have the
6215 mask handy. Casting avoids a GCC warning. */
6216 image_background_transparent (img, f, (XImagePtr_or_DC)mask_img);
6218 image_put_x_image (f, img, mask_img, 1);
6220 #endif
6222 return 1;
6225 static bool
6226 png_load (struct frame *f, struct image *img)
6228 struct png_load_context c;
6229 return png_load_body (f, img, &c);
6232 #elif defined HAVE_NS
6234 static bool
6235 png_load (struct frame *f, struct image *img)
6237 return ns_load_image (f, img,
6238 image_spec_value (img->spec, QCfile, NULL),
6239 image_spec_value (img->spec, QCdata, NULL));
6243 #endif /* HAVE_NS */
6247 /***********************************************************************
6248 JPEG
6249 ***********************************************************************/
6251 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6253 static bool jpeg_image_p (Lisp_Object object);
6254 static bool jpeg_load (struct frame *f, struct image *img);
6256 /* Indices of image specification fields in gs_format, below. */
6258 enum jpeg_keyword_index
6260 JPEG_TYPE,
6261 JPEG_DATA,
6262 JPEG_FILE,
6263 JPEG_ASCENT,
6264 JPEG_MARGIN,
6265 JPEG_RELIEF,
6266 JPEG_ALGORITHM,
6267 JPEG_HEURISTIC_MASK,
6268 JPEG_MASK,
6269 JPEG_BACKGROUND,
6270 JPEG_LAST
6273 /* Vector of image_keyword structures describing the format
6274 of valid user-defined image specifications. */
6276 static const struct image_keyword jpeg_format[JPEG_LAST] =
6278 {":type", IMAGE_SYMBOL_VALUE, 1},
6279 {":data", IMAGE_STRING_VALUE, 0},
6280 {":file", IMAGE_STRING_VALUE, 0},
6281 {":ascent", IMAGE_ASCENT_VALUE, 0},
6282 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
6283 {":relief", IMAGE_INTEGER_VALUE, 0},
6284 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6285 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6286 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6287 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6290 #if defined HAVE_NTGUI && defined WINDOWSNT
6291 static bool init_jpeg_functions (void);
6292 #else
6293 #define init_jpeg_functions NULL
6294 #endif
6296 /* Structure describing the image type `jpeg'. */
6298 static struct image_type jpeg_type =
6300 SYMBOL_INDEX (Qjpeg),
6301 jpeg_image_p,
6302 jpeg_load,
6303 x_clear_image,
6304 init_jpeg_functions,
6305 NULL
6308 /* Return true if OBJECT is a valid JPEG image specification. */
6310 static bool
6311 jpeg_image_p (Lisp_Object object)
6313 struct image_keyword fmt[JPEG_LAST];
6315 memcpy (fmt, jpeg_format, sizeof fmt);
6317 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
6318 return 0;
6320 /* Must specify either the :data or :file keyword. */
6321 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
6324 #endif /* HAVE_JPEG || HAVE_NS */
6326 #ifdef HAVE_JPEG
6328 /* Work around a warning about HAVE_STDLIB_H being redefined in
6329 jconfig.h. */
6330 # ifdef HAVE_STDLIB_H
6331 # undef HAVE_STDLIB_H
6332 # endif
6334 # if defined (HAVE_NTGUI) && !defined (__WIN32__)
6335 /* In older releases of the jpeg library, jpeglib.h will define boolean
6336 differently depending on __WIN32__, so make sure it is defined. */
6337 # define __WIN32__ 1
6338 # endif
6340 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6341 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6342 using the Windows boolean type instead of the jpeglib boolean type
6343 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6344 headers are included along with windows.h, so under Cygwin, jpeglib
6345 attempts to define a conflicting boolean type. Worse, forcing
6346 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6347 because it created an ABI incompatibility between the
6348 already-compiled jpeg library and the header interface definition.
6350 The best we can do is to define jpeglib's boolean type to a
6351 different name. This name, jpeg_boolean, remains in effect through
6352 the rest of image.c.
6354 # if defined CYGWIN && defined HAVE_NTGUI
6355 # define boolean jpeg_boolean
6356 # endif
6357 # include <jpeglib.h>
6358 # include <jerror.h>
6360 # ifdef WINDOWSNT
6362 /* JPEG library details. */
6363 DEF_DLL_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t));
6364 DEF_DLL_FN (boolean, jpeg_start_decompress, (j_decompress_ptr));
6365 DEF_DLL_FN (boolean, jpeg_finish_decompress, (j_decompress_ptr));
6366 DEF_DLL_FN (void, jpeg_destroy_decompress, (j_decompress_ptr));
6367 DEF_DLL_FN (int, jpeg_read_header, (j_decompress_ptr, boolean));
6368 DEF_DLL_FN (JDIMENSION, jpeg_read_scanlines,
6369 (j_decompress_ptr, JSAMPARRAY, JDIMENSION));
6370 DEF_DLL_FN (struct jpeg_error_mgr *, jpeg_std_error,
6371 (struct jpeg_error_mgr *));
6372 DEF_DLL_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int));
6374 static bool
6375 init_jpeg_functions (void)
6377 HMODULE library;
6379 if (!(library = w32_delayed_load (Qjpeg)))
6380 return 0;
6382 LOAD_DLL_FN (library, jpeg_finish_decompress);
6383 LOAD_DLL_FN (library, jpeg_read_scanlines);
6384 LOAD_DLL_FN (library, jpeg_start_decompress);
6385 LOAD_DLL_FN (library, jpeg_read_header);
6386 LOAD_DLL_FN (library, jpeg_CreateDecompress);
6387 LOAD_DLL_FN (library, jpeg_destroy_decompress);
6388 LOAD_DLL_FN (library, jpeg_std_error);
6389 LOAD_DLL_FN (library, jpeg_resync_to_restart);
6390 return 1;
6393 # undef jpeg_CreateDecompress
6394 # undef jpeg_destroy_decompress
6395 # undef jpeg_finish_decompress
6396 # undef jpeg_read_header
6397 # undef jpeg_read_scanlines
6398 # undef jpeg_resync_to_restart
6399 # undef jpeg_start_decompress
6400 # undef jpeg_std_error
6402 # define jpeg_CreateDecompress fn_jpeg_CreateDecompress
6403 # define jpeg_destroy_decompress fn_jpeg_destroy_decompress
6404 # define jpeg_finish_decompress fn_jpeg_finish_decompress
6405 # define jpeg_read_header fn_jpeg_read_header
6406 # define jpeg_read_scanlines fn_jpeg_read_scanlines
6407 # define jpeg_resync_to_restart fn_jpeg_resync_to_restart
6408 # define jpeg_start_decompress fn_jpeg_start_decompress
6409 # define jpeg_std_error fn_jpeg_std_error
6411 /* Wrapper since we can't directly assign the function pointer
6412 to another function pointer that was declared more completely easily. */
6413 static boolean
6414 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired)
6416 return jpeg_resync_to_restart (cinfo, desired);
6418 # undef jpeg_resync_to_restart
6419 # define jpeg_resync_to_restart jpeg_resync_to_restart_wrapper
6421 # endif /* WINDOWSNT */
6423 struct my_jpeg_error_mgr
6425 struct jpeg_error_mgr pub;
6426 sys_jmp_buf setjmp_buffer;
6428 /* The remaining members are so that longjmp doesn't munge local
6429 variables. */
6430 struct jpeg_decompress_struct cinfo;
6431 enum
6433 MY_JPEG_ERROR_EXIT,
6434 MY_JPEG_INVALID_IMAGE_SIZE,
6435 MY_JPEG_CANNOT_CREATE_X
6436 } failure_code;
6440 static _Noreturn void
6441 my_error_exit (j_common_ptr cinfo)
6443 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
6444 mgr->failure_code = MY_JPEG_ERROR_EXIT;
6445 sys_longjmp (mgr->setjmp_buffer, 1);
6449 /* Init source method for JPEG data source manager. Called by
6450 jpeg_read_header() before any data is actually read. See
6451 libjpeg.doc from the JPEG lib distribution. */
6453 static void
6454 our_common_init_source (j_decompress_ptr cinfo)
6459 /* Method to terminate data source. Called by
6460 jpeg_finish_decompress() after all data has been processed. */
6462 static void
6463 our_common_term_source (j_decompress_ptr cinfo)
6468 /* Fill input buffer method for JPEG data source manager. Called
6469 whenever more data is needed. We read the whole image in one step,
6470 so this only adds a fake end of input marker at the end. */
6472 static JOCTET our_memory_buffer[2];
6474 static boolean
6475 our_memory_fill_input_buffer (j_decompress_ptr cinfo)
6477 /* Insert a fake EOI marker. */
6478 struct jpeg_source_mgr *src = cinfo->src;
6480 our_memory_buffer[0] = (JOCTET) 0xFF;
6481 our_memory_buffer[1] = (JOCTET) JPEG_EOI;
6483 src->next_input_byte = our_memory_buffer;
6484 src->bytes_in_buffer = 2;
6485 return 1;
6489 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6490 is the JPEG data source manager. */
6492 static void
6493 our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes)
6495 struct jpeg_source_mgr *src = cinfo->src;
6497 if (src)
6499 if (num_bytes > src->bytes_in_buffer)
6500 ERREXIT (cinfo, JERR_INPUT_EOF);
6502 src->bytes_in_buffer -= num_bytes;
6503 src->next_input_byte += num_bytes;
6508 /* Set up the JPEG lib for reading an image from DATA which contains
6509 LEN bytes. CINFO is the decompression info structure created for
6510 reading the image. */
6512 static void
6513 jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len)
6515 struct jpeg_source_mgr *src = cinfo->src;
6517 if (! src)
6519 /* First time for this JPEG object? */
6520 src = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6521 JPOOL_PERMANENT, sizeof *src);
6522 cinfo->src = src;
6523 src->next_input_byte = data;
6526 src->init_source = our_common_init_source;
6527 src->fill_input_buffer = our_memory_fill_input_buffer;
6528 src->skip_input_data = our_memory_skip_input_data;
6529 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
6530 src->term_source = our_common_term_source;
6531 src->bytes_in_buffer = len;
6532 src->next_input_byte = data;
6536 struct jpeg_stdio_mgr
6538 struct jpeg_source_mgr mgr;
6539 boolean finished;
6540 FILE *file;
6541 JOCTET *buffer;
6545 /* Size of buffer to read JPEG from file.
6546 Not too big, as we want to use alloc_small. */
6547 #define JPEG_STDIO_BUFFER_SIZE 8192
6550 /* Fill input buffer method for JPEG data source manager. Called
6551 whenever more data is needed. The data is read from a FILE *. */
6553 static boolean
6554 our_stdio_fill_input_buffer (j_decompress_ptr cinfo)
6556 struct jpeg_stdio_mgr *src;
6558 src = (struct jpeg_stdio_mgr *) cinfo->src;
6559 if (!src->finished)
6561 ptrdiff_t bytes;
6563 bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file);
6564 if (bytes > 0)
6565 src->mgr.bytes_in_buffer = bytes;
6566 else
6568 WARNMS (cinfo, JWRN_JPEG_EOF);
6569 src->finished = 1;
6570 src->buffer[0] = (JOCTET) 0xFF;
6571 src->buffer[1] = (JOCTET) JPEG_EOI;
6572 src->mgr.bytes_in_buffer = 2;
6574 src->mgr.next_input_byte = src->buffer;
6577 return 1;
6581 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6582 is the JPEG data source manager. */
6584 static void
6585 our_stdio_skip_input_data (j_decompress_ptr cinfo, long int num_bytes)
6587 struct jpeg_stdio_mgr *src;
6588 src = (struct jpeg_stdio_mgr *) cinfo->src;
6590 while (num_bytes > 0 && !src->finished)
6592 if (num_bytes <= src->mgr.bytes_in_buffer)
6594 src->mgr.bytes_in_buffer -= num_bytes;
6595 src->mgr.next_input_byte += num_bytes;
6596 break;
6598 else
6600 num_bytes -= src->mgr.bytes_in_buffer;
6601 src->mgr.bytes_in_buffer = 0;
6602 src->mgr.next_input_byte = NULL;
6604 our_stdio_fill_input_buffer (cinfo);
6610 /* Set up the JPEG lib for reading an image from a FILE *.
6611 CINFO is the decompression info structure created for
6612 reading the image. */
6614 static void
6615 jpeg_file_src (j_decompress_ptr cinfo, FILE *fp)
6617 struct jpeg_stdio_mgr *src = (struct jpeg_stdio_mgr *) cinfo->src;
6619 if (! src)
6621 /* First time for this JPEG object? */
6622 src = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6623 JPOOL_PERMANENT, sizeof *src);
6624 cinfo->src = (struct jpeg_source_mgr *) src;
6625 src->buffer = cinfo->mem->alloc_small ((j_common_ptr) cinfo,
6626 JPOOL_PERMANENT,
6627 JPEG_STDIO_BUFFER_SIZE);
6630 src->file = fp;
6631 src->finished = 0;
6632 src->mgr.init_source = our_common_init_source;
6633 src->mgr.fill_input_buffer = our_stdio_fill_input_buffer;
6634 src->mgr.skip_input_data = our_stdio_skip_input_data;
6635 src->mgr.resync_to_restart = jpeg_resync_to_restart; /* Use default. */
6636 src->mgr.term_source = our_common_term_source;
6637 src->mgr.bytes_in_buffer = 0;
6638 src->mgr.next_input_byte = NULL;
6641 /* Load image IMG for use on frame F. Patterned after example.c
6642 from the JPEG lib. */
6644 static bool
6645 jpeg_load_body (struct frame *f, struct image *img,
6646 struct my_jpeg_error_mgr *mgr)
6648 Lisp_Object file, specified_file;
6649 Lisp_Object specified_data;
6650 /* The 'volatile' silences a bogus diagnostic; see GCC bug 54561. */
6651 FILE * IF_LINT (volatile) fp = NULL;
6652 JSAMPARRAY buffer;
6653 int row_stride, x, y;
6654 unsigned long *colors;
6655 int width, height;
6656 int i, ir, ig, ib;
6657 #ifndef USE_CAIRO
6658 XImagePtr ximg = NULL;
6659 #endif
6661 /* Open the JPEG file. */
6662 specified_file = image_spec_value (img->spec, QCfile, NULL);
6663 specified_data = image_spec_value (img->spec, QCdata, NULL);
6664 IF_LINT (Lisp_Object volatile specified_data_volatile = specified_data);
6666 if (NILP (specified_data))
6668 file = x_find_image_file (specified_file);
6669 if (!STRINGP (file))
6671 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6672 return 0;
6675 fp = emacs_fopen (SSDATA (file), "rb");
6676 if (fp == NULL)
6678 image_error ("Cannot open `%s'", file, Qnil);
6679 return 0;
6682 else if (!STRINGP (specified_data))
6684 image_error ("Invalid image data `%s'", specified_data, Qnil);
6685 return 0;
6688 /* Customize libjpeg's error handling to call my_error_exit when an
6689 error is detected. This function will perform a longjmp. */
6690 mgr->cinfo.err = jpeg_std_error (&mgr->pub);
6691 mgr->pub.error_exit = my_error_exit;
6692 if (sys_setjmp (mgr->setjmp_buffer))
6694 switch (mgr->failure_code)
6696 case MY_JPEG_ERROR_EXIT:
6698 char buf[JMSG_LENGTH_MAX];
6699 mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf);
6700 image_error ("Error reading JPEG image `%s': %s", img->spec,
6701 build_string (buf));
6702 break;
6705 case MY_JPEG_INVALID_IMAGE_SIZE:
6706 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
6707 break;
6709 case MY_JPEG_CANNOT_CREATE_X:
6710 break;
6713 /* Close the input file and destroy the JPEG object. */
6714 if (fp)
6715 fclose (fp);
6716 jpeg_destroy_decompress (&mgr->cinfo);
6718 /* If we already have an XImage, free that. */
6719 #ifndef USE_CAIRO
6720 x_destroy_x_image (ximg);
6721 #endif
6722 /* Free pixmap and colors. */
6723 x_clear_image (f, img);
6724 return 0;
6727 /* Silence a bogus diagnostic; see GCC bug 54561. */
6728 IF_LINT (specified_data = specified_data_volatile);
6730 /* Create the JPEG decompression object. Let it read from fp.
6731 Read the JPEG image header. */
6732 jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo);
6734 if (NILP (specified_data))
6735 jpeg_file_src (&mgr->cinfo, fp);
6736 else
6737 jpeg_memory_src (&mgr->cinfo, SDATA (specified_data),
6738 SBYTES (specified_data));
6740 jpeg_read_header (&mgr->cinfo, 1);
6742 /* Customize decompression so that color quantization will be used.
6743 Start decompression. */
6744 mgr->cinfo.quantize_colors = 1;
6745 jpeg_start_decompress (&mgr->cinfo);
6746 width = img->width = mgr->cinfo.output_width;
6747 height = img->height = mgr->cinfo.output_height;
6749 if (!check_image_size (f, width, height))
6751 mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE;
6752 sys_longjmp (mgr->setjmp_buffer, 1);
6755 #ifndef USE_CAIRO
6756 /* Create X image and pixmap. */
6757 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
6759 mgr->failure_code = MY_JPEG_CANNOT_CREATE_X;
6760 sys_longjmp (mgr->setjmp_buffer, 1);
6762 #endif
6764 /* Allocate colors. When color quantization is used,
6765 mgr->cinfo.actual_number_of_colors has been set with the number of
6766 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6767 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6768 No more than 255 colors will be generated. */
6769 USE_SAFE_ALLOCA;
6771 if (mgr->cinfo.out_color_components > 2)
6772 ir = 0, ig = 1, ib = 2;
6773 else if (mgr->cinfo.out_color_components > 1)
6774 ir = 0, ig = 1, ib = 0;
6775 else
6776 ir = 0, ig = 0, ib = 0;
6778 #ifndef CAIRO
6779 /* Use the color table mechanism because it handles colors that
6780 cannot be allocated nicely. Such colors will be replaced with
6781 a default color, and we don't have to care about which colors
6782 can be freed safely, and which can't. */
6783 init_color_table ();
6784 SAFE_NALLOCA (colors, 1, mgr->cinfo.actual_number_of_colors);
6786 for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i)
6788 /* Multiply RGB values with 255 because X expects RGB values
6789 in the range 0..0xffff. */
6790 int r = mgr->cinfo.colormap[ir][i] << 8;
6791 int g = mgr->cinfo.colormap[ig][i] << 8;
6792 int b = mgr->cinfo.colormap[ib][i] << 8;
6793 colors[i] = lookup_rgb_color (f, r, g, b);
6795 #endif
6797 #ifdef COLOR_TABLE_SUPPORT
6798 /* Remember those colors actually allocated. */
6799 img->colors = colors_in_color_table (&img->ncolors);
6800 free_color_table ();
6801 #endif /* COLOR_TABLE_SUPPORT */
6804 /* Read pixels. */
6805 row_stride = width * mgr->cinfo.output_components;
6806 buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo,
6807 JPOOL_IMAGE, row_stride, 1);
6808 #ifdef USE_CAIRO
6810 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
6811 uint32_t *dataptr = (uint32_t *) data;
6812 int r, g, b;
6814 for (y = 0; y < height; ++y)
6816 jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
6818 for (x = 0; x < width; ++x)
6820 i = buffer[0][x];
6821 r = mgr->cinfo.colormap[ir][i];
6822 g = mgr->cinfo.colormap[ig][i];
6823 b = mgr->cinfo.colormap[ib][i];
6824 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
6828 create_cairo_image_surface (img, data, width, height);
6830 #else
6831 for (y = 0; y < height; ++y)
6833 jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
6834 for (x = 0; x < mgr->cinfo.output_width; ++x)
6835 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
6837 #endif
6839 /* Clean up. */
6840 jpeg_finish_decompress (&mgr->cinfo);
6841 jpeg_destroy_decompress (&mgr->cinfo);
6842 if (fp)
6843 fclose (fp);
6845 #ifndef USE_CAIRO
6846 /* Maybe fill in the background field while we have ximg handy. */
6847 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6848 /* Casting avoids a GCC warning. */
6849 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6851 /* Put ximg into the image. */
6852 image_put_x_image (f, img, ximg, 0);
6853 #endif
6854 SAFE_FREE ();
6855 return 1;
6858 static bool
6859 jpeg_load (struct frame *f, struct image *img)
6861 struct my_jpeg_error_mgr mgr;
6862 return jpeg_load_body (f, img, &mgr);
6865 #else /* HAVE_JPEG */
6867 #ifdef HAVE_NS
6868 static bool
6869 jpeg_load (struct frame *f, struct image *img)
6871 return ns_load_image (f, img,
6872 image_spec_value (img->spec, QCfile, NULL),
6873 image_spec_value (img->spec, QCdata, NULL));
6875 #endif /* HAVE_NS */
6877 #endif /* !HAVE_JPEG */
6881 /***********************************************************************
6882 TIFF
6883 ***********************************************************************/
6885 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6887 static bool tiff_image_p (Lisp_Object object);
6888 static bool tiff_load (struct frame *f, struct image *img);
6890 /* Indices of image specification fields in tiff_format, below. */
6892 enum tiff_keyword_index
6894 TIFF_TYPE,
6895 TIFF_DATA,
6896 TIFF_FILE,
6897 TIFF_ASCENT,
6898 TIFF_MARGIN,
6899 TIFF_RELIEF,
6900 TIFF_ALGORITHM,
6901 TIFF_HEURISTIC_MASK,
6902 TIFF_MASK,
6903 TIFF_BACKGROUND,
6904 TIFF_INDEX,
6905 TIFF_LAST
6908 /* Vector of image_keyword structures describing the format
6909 of valid user-defined image specifications. */
6911 static const struct image_keyword tiff_format[TIFF_LAST] =
6913 {":type", IMAGE_SYMBOL_VALUE, 1},
6914 {":data", IMAGE_STRING_VALUE, 0},
6915 {":file", IMAGE_STRING_VALUE, 0},
6916 {":ascent", IMAGE_ASCENT_VALUE, 0},
6917 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
6918 {":relief", IMAGE_INTEGER_VALUE, 0},
6919 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6920 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6921 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6922 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
6923 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
6926 #if defined HAVE_NTGUI && defined WINDOWSNT
6927 static bool init_tiff_functions (void);
6928 #else
6929 #define init_tiff_functions NULL
6930 #endif
6932 /* Structure describing the image type `tiff'. */
6934 static struct image_type tiff_type =
6936 SYMBOL_INDEX (Qtiff),
6937 tiff_image_p,
6938 tiff_load,
6939 x_clear_image,
6940 init_tiff_functions,
6941 NULL
6944 /* Return true if OBJECT is a valid TIFF image specification. */
6946 static bool
6947 tiff_image_p (Lisp_Object object)
6949 struct image_keyword fmt[TIFF_LAST];
6950 memcpy (fmt, tiff_format, sizeof fmt);
6952 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
6953 return 0;
6955 /* Must specify either the :data or :file keyword. */
6956 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
6959 #endif /* HAVE_TIFF || HAVE_NS */
6961 #ifdef HAVE_TIFF
6963 # include <tiffio.h>
6965 # ifdef WINDOWSNT
6967 /* TIFF library details. */
6968 DEF_DLL_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler));
6969 DEF_DLL_FN (TIFFErrorHandler, TIFFSetWarningHandler, (TIFFErrorHandler));
6970 DEF_DLL_FN (TIFF *, TIFFOpen, (const char *, const char *));
6971 DEF_DLL_FN (TIFF *, TIFFClientOpen,
6972 (const char *, const char *, thandle_t, TIFFReadWriteProc,
6973 TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc,
6974 TIFFMapFileProc, TIFFUnmapFileProc));
6975 DEF_DLL_FN (int, TIFFGetField, (TIFF *, ttag_t, ...));
6976 DEF_DLL_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int));
6977 DEF_DLL_FN (void, TIFFClose, (TIFF *));
6978 DEF_DLL_FN (int, TIFFSetDirectory, (TIFF *, tdir_t));
6980 static bool
6981 init_tiff_functions (void)
6983 HMODULE library;
6985 if (!(library = w32_delayed_load (Qtiff)))
6986 return 0;
6988 LOAD_DLL_FN (library, TIFFSetErrorHandler);
6989 LOAD_DLL_FN (library, TIFFSetWarningHandler);
6990 LOAD_DLL_FN (library, TIFFOpen);
6991 LOAD_DLL_FN (library, TIFFClientOpen);
6992 LOAD_DLL_FN (library, TIFFGetField);
6993 LOAD_DLL_FN (library, TIFFReadRGBAImage);
6994 LOAD_DLL_FN (library, TIFFClose);
6995 LOAD_DLL_FN (library, TIFFSetDirectory);
6996 return 1;
6999 # undef TIFFClientOpen
7000 # undef TIFFClose
7001 # undef TIFFGetField
7002 # undef TIFFOpen
7003 # undef TIFFReadRGBAImage
7004 # undef TIFFSetDirectory
7005 # undef TIFFSetErrorHandler
7006 # undef TIFFSetWarningHandler
7008 # define TIFFClientOpen fn_TIFFClientOpen
7009 # define TIFFClose fn_TIFFClose
7010 # define TIFFGetField fn_TIFFGetField
7011 # define TIFFOpen fn_TIFFOpen
7012 # define TIFFReadRGBAImage fn_TIFFReadRGBAImage
7013 # define TIFFSetDirectory fn_TIFFSetDirectory
7014 # define TIFFSetErrorHandler fn_TIFFSetErrorHandler
7015 # define TIFFSetWarningHandler fn_TIFFSetWarningHandler
7017 # endif /* WINDOWSNT */
7020 /* Reading from a memory buffer for TIFF images Based on the PNG
7021 memory source, but we have to provide a lot of extra functions.
7022 Blah.
7024 We really only need to implement read and seek, but I am not
7025 convinced that the TIFF library is smart enough not to destroy
7026 itself if we only hand it the function pointers we need to
7027 override. */
7029 typedef struct
7031 unsigned char *bytes;
7032 ptrdiff_t len;
7033 ptrdiff_t index;
7035 tiff_memory_source;
7037 static tsize_t
7038 tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size)
7040 tiff_memory_source *src = (tiff_memory_source *) data;
7042 size = min (size, src->len - src->index);
7043 memcpy (buf, src->bytes + src->index, size);
7044 src->index += size;
7045 return size;
7048 static tsize_t
7049 tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size)
7051 return -1;
7054 static toff_t
7055 tiff_seek_in_memory (thandle_t data, toff_t off, int whence)
7057 tiff_memory_source *src = (tiff_memory_source *) data;
7058 ptrdiff_t idx;
7060 switch (whence)
7062 case SEEK_SET: /* Go from beginning of source. */
7063 idx = off;
7064 break;
7066 case SEEK_END: /* Go from end of source. */
7067 idx = src->len + off;
7068 break;
7070 case SEEK_CUR: /* Go from current position. */
7071 idx = src->index + off;
7072 break;
7074 default: /* Invalid `whence'. */
7075 return -1;
7078 if (idx > src->len || idx < 0)
7079 return -1;
7081 src->index = idx;
7082 return src->index;
7085 static int
7086 tiff_close_memory (thandle_t data)
7088 /* NOOP */
7089 return 0;
7092 static int
7093 tiff_mmap_memory (thandle_t data, tdata_t *pbase, toff_t *psize)
7095 /* It is already _IN_ memory. */
7096 return 0;
7099 static void
7100 tiff_unmap_memory (thandle_t data, tdata_t base, toff_t size)
7102 /* We don't need to do this. */
7105 static toff_t
7106 tiff_size_of_memory (thandle_t data)
7108 return ((tiff_memory_source *) data)->len;
7111 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
7112 compiler error compiling tiff_handler, see Bugzilla bug #17406
7113 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
7114 this function as external works around that problem. */
7115 # if defined (__MINGW32__) && __GNUC__ == 3
7116 # define MINGW_STATIC
7117 # else
7118 # define MINGW_STATIC static
7119 # endif
7121 MINGW_STATIC void
7122 tiff_handler (const char *, const char *, const char *, va_list)
7123 ATTRIBUTE_FORMAT_PRINTF (3, 0);
7124 MINGW_STATIC void
7125 tiff_handler (const char *log_format, const char *title,
7126 const char *format, va_list ap)
7128 /* doprnt is not suitable here, as TIFF handlers are called from
7129 libtiff and are passed arbitrary printf directives. Instead, use
7130 vsnprintf, taking care to be portable to nonstandard environments
7131 where vsnprintf returns -1 on buffer overflow. Since it's just a
7132 log entry, it's OK to truncate it. */
7133 char buf[4000];
7134 int len = vsnprintf (buf, sizeof buf, format, ap);
7135 add_to_log (log_format, build_string (title),
7136 make_string (buf, max (0, min (len, sizeof buf - 1))));
7138 # undef MINGW_STATIC
7140 static void tiff_error_handler (const char *, const char *, va_list)
7141 ATTRIBUTE_FORMAT_PRINTF (2, 0);
7142 static void
7143 tiff_error_handler (const char *title, const char *format, va_list ap)
7145 tiff_handler ("TIFF error: %s %s", title, format, ap);
7149 static void tiff_warning_handler (const char *, const char *, va_list)
7150 ATTRIBUTE_FORMAT_PRINTF (2, 0);
7151 static void
7152 tiff_warning_handler (const char *title, const char *format, va_list ap)
7154 tiff_handler ("TIFF warning: %s %s", title, format, ap);
7158 /* Load TIFF image IMG for use on frame F. Value is true if
7159 successful. */
7161 static bool
7162 tiff_load (struct frame *f, struct image *img)
7164 Lisp_Object file, specified_file;
7165 Lisp_Object specified_data;
7166 TIFF *tiff;
7167 int width, height, x, y, count;
7168 uint32 *buf;
7169 int rc;
7170 XImagePtr ximg;
7171 tiff_memory_source memsrc;
7172 Lisp_Object image;
7174 specified_file = image_spec_value (img->spec, QCfile, NULL);
7175 specified_data = image_spec_value (img->spec, QCdata, NULL);
7177 TIFFSetErrorHandler ((TIFFErrorHandler) tiff_error_handler);
7178 TIFFSetWarningHandler ((TIFFErrorHandler) tiff_warning_handler);
7180 if (NILP (specified_data))
7182 /* Read from a file */
7183 file = x_find_image_file (specified_file);
7184 if (!STRINGP (file))
7186 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7187 return 0;
7189 # ifdef WINDOWSNT
7190 file = ansi_encode_filename (file);
7191 # endif
7193 /* Try to open the image file. */
7194 tiff = TIFFOpen (SSDATA (file), "r");
7195 if (tiff == NULL)
7197 image_error ("Cannot open `%s'", file, Qnil);
7198 return 0;
7201 else
7203 if (!STRINGP (specified_data))
7205 image_error ("Invalid image data `%s'", specified_data, Qnil);
7206 return 0;
7209 /* Memory source! */
7210 memsrc.bytes = SDATA (specified_data);
7211 memsrc.len = SBYTES (specified_data);
7212 memsrc.index = 0;
7214 tiff = TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc,
7215 tiff_read_from_memory,
7216 tiff_write_from_memory,
7217 tiff_seek_in_memory,
7218 tiff_close_memory,
7219 tiff_size_of_memory,
7220 tiff_mmap_memory,
7221 tiff_unmap_memory);
7223 if (!tiff)
7225 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
7226 return 0;
7230 image = image_spec_value (img->spec, QCindex, NULL);
7231 if (INTEGERP (image))
7233 EMACS_INT ino = XFASTINT (image);
7234 if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
7235 && TIFFSetDirectory (tiff, ino)))
7237 image_error ("Invalid image number `%s' in image `%s'",
7238 image, img->spec);
7239 TIFFClose (tiff);
7240 return 0;
7244 /* Get width and height of the image, and allocate a raster buffer
7245 of width x height 32-bit values. */
7246 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
7247 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
7249 if (!check_image_size (f, width, height))
7251 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7252 TIFFClose (tiff);
7253 return 0;
7256 /* Create the X image and pixmap. */
7257 if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width
7258 && image_create_x_image_and_pixmap (f, img, width, height, 0,
7259 &ximg, 0)))
7261 TIFFClose (tiff);
7262 return 0;
7265 buf = xmalloc (sizeof *buf * width * height);
7267 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
7269 /* Count the number of images in the file. */
7270 for (count = 1; TIFFSetDirectory (tiff, count); count++)
7271 continue;
7273 if (count > 1)
7274 img->lisp_data = Fcons (Qcount,
7275 Fcons (make_number (count),
7276 img->lisp_data));
7278 TIFFClose (tiff);
7279 if (!rc)
7281 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
7282 xfree (buf);
7283 return 0;
7286 #ifdef USE_CAIRO
7288 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
7289 uint32_t *dataptr = (uint32_t *) data;
7290 int r, g, b, a;
7292 for (y = 0; y < height; ++y)
7294 uint32 *row = buf + (height - 1 - y) * width;
7295 for (x = 0; x < width; ++x)
7297 uint32 abgr = row[x];
7298 int r = TIFFGetR (abgr);
7299 int g = TIFFGetG (abgr);
7300 int b = TIFFGetB (abgr);
7301 int a = TIFFGetA (abgr);
7302 *dataptr++ = (a << 24) | (r << 16) | (g << 8) | b;
7306 create_cairo_image_surface (img, data, width, height);
7308 #else
7309 /* Initialize the color table. */
7310 init_color_table ();
7312 /* Process the pixel raster. Origin is in the lower-left corner. */
7313 for (y = 0; y < height; ++y)
7315 uint32 *row = buf + y * width;
7317 for (x = 0; x < width; ++x)
7319 uint32 abgr = row[x];
7320 int r = TIFFGetR (abgr) << 8;
7321 int g = TIFFGetG (abgr) << 8;
7322 int b = TIFFGetB (abgr) << 8;
7323 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
7327 # ifdef COLOR_TABLE_SUPPORT
7328 /* Remember the colors allocated for the image. Free the color table. */
7329 img->colors = colors_in_color_table (&img->ncolors);
7330 free_color_table ();
7331 # endif /* COLOR_TABLE_SUPPORT */
7333 img->width = width;
7334 img->height = height;
7336 /* Maybe fill in the background field while we have ximg handy. */
7337 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7338 /* Casting avoids a GCC warning on W32. */
7339 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7341 /* Put ximg into the image. */
7342 image_put_x_image (f, img, ximg, 0);
7344 #endif /* ! USE_CAIRO */
7346 xfree (buf);
7347 return 1;
7350 #elif defined HAVE_NS
7352 static bool
7353 tiff_load (struct frame *f, struct image *img)
7355 return ns_load_image (f, img,
7356 image_spec_value (img->spec, QCfile, NULL),
7357 image_spec_value (img->spec, QCdata, NULL));
7360 #endif
7364 /***********************************************************************
7366 ***********************************************************************/
7368 #if defined (HAVE_GIF) || defined (HAVE_NS)
7370 static bool gif_image_p (Lisp_Object object);
7371 static bool gif_load (struct frame *f, struct image *img);
7372 static void gif_clear_image (struct frame *f, struct image *img);
7374 /* Indices of image specification fields in gif_format, below. */
7376 enum gif_keyword_index
7378 GIF_TYPE,
7379 GIF_DATA,
7380 GIF_FILE,
7381 GIF_ASCENT,
7382 GIF_MARGIN,
7383 GIF_RELIEF,
7384 GIF_ALGORITHM,
7385 GIF_HEURISTIC_MASK,
7386 GIF_MASK,
7387 GIF_IMAGE,
7388 GIF_BACKGROUND,
7389 GIF_LAST
7392 /* Vector of image_keyword structures describing the format
7393 of valid user-defined image specifications. */
7395 static const struct image_keyword gif_format[GIF_LAST] =
7397 {":type", IMAGE_SYMBOL_VALUE, 1},
7398 {":data", IMAGE_STRING_VALUE, 0},
7399 {":file", IMAGE_STRING_VALUE, 0},
7400 {":ascent", IMAGE_ASCENT_VALUE, 0},
7401 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
7402 {":relief", IMAGE_INTEGER_VALUE, 0},
7403 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7404 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7405 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7406 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
7407 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7410 #if defined HAVE_NTGUI && defined WINDOWSNT
7411 static bool init_gif_functions (void);
7412 #else
7413 #define init_gif_functions NULL
7414 #endif
7416 /* Structure describing the image type `gif'. */
7418 static struct image_type gif_type =
7420 SYMBOL_INDEX (Qgif),
7421 gif_image_p,
7422 gif_load,
7423 gif_clear_image,
7424 init_gif_functions,
7425 NULL
7428 /* Free X resources of GIF image IMG which is used on frame F. */
7430 static void
7431 gif_clear_image (struct frame *f, struct image *img)
7433 img->lisp_data = Qnil;
7434 x_clear_image (f, img);
7437 /* Return true if OBJECT is a valid GIF image specification. */
7439 static bool
7440 gif_image_p (Lisp_Object object)
7442 struct image_keyword fmt[GIF_LAST];
7443 memcpy (fmt, gif_format, sizeof fmt);
7445 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
7446 return 0;
7448 /* Must specify either the :data or :file keyword. */
7449 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
7452 #endif /* HAVE_GIF */
7454 #ifdef HAVE_GIF
7456 # ifdef HAVE_NTGUI
7458 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7459 Undefine before redefining to avoid a preprocessor warning. */
7460 # ifdef DrawText
7461 # undef DrawText
7462 # endif
7463 /* avoid conflict with QuickdrawText.h */
7464 # define DrawText gif_DrawText
7465 # include <gif_lib.h>
7466 # undef DrawText
7468 /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */
7469 # ifndef GIFLIB_MINOR
7470 # define GIFLIB_MINOR 0
7471 # endif
7472 # ifndef GIFLIB_RELEASE
7473 # define GIFLIB_RELEASE 0
7474 # endif
7476 # else /* HAVE_NTGUI */
7478 # include <gif_lib.h>
7480 # endif /* HAVE_NTGUI */
7482 /* Giflib before 5.0 didn't define these macros. */
7483 # ifndef GIFLIB_MAJOR
7484 # define GIFLIB_MAJOR 4
7485 # endif
7487 # ifdef WINDOWSNT
7489 /* GIF library details. */
7490 # if GIFLIB_MAJOR + (GIFLIB_MINOR >= 1) > 5
7491 DEF_DLL_FN (int, DGifCloseFile, (GifFileType *, int *));
7492 # else
7493 DEF_DLL_FN (int, DGifCloseFile, (GifFileType *));
7494 # endif
7495 DEF_DLL_FN (int, DGifSlurp, (GifFileType *));
7496 # if GIFLIB_MAJOR < 5
7497 DEF_DLL_FN (GifFileType *, DGifOpen, (void *, InputFunc));
7498 DEF_DLL_FN (GifFileType *, DGifOpenFileName, (const char *));
7499 # else
7500 DEF_DLL_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *));
7501 DEF_DLL_FN (GifFileType *, DGifOpenFileName, (const char *, int *));
7502 DEF_DLL_FN (char *, GifErrorString, (int));
7503 # endif
7505 static bool
7506 init_gif_functions (void)
7508 HMODULE library;
7510 if (!(library = w32_delayed_load (Qgif)))
7511 return 0;
7513 LOAD_DLL_FN (library, DGifCloseFile);
7514 LOAD_DLL_FN (library, DGifSlurp);
7515 LOAD_DLL_FN (library, DGifOpen);
7516 LOAD_DLL_FN (library, DGifOpenFileName);
7517 # if GIFLIB_MAJOR >= 5
7518 LOAD_DLL_FN (library, GifErrorString);
7519 # endif
7520 return 1;
7523 # undef DGifCloseFile
7524 # undef DGifOpen
7525 # undef DGifOpenFileName
7526 # undef DGifSlurp
7527 # undef GifErrorString
7529 # define DGifCloseFile fn_DGifCloseFile
7530 # define DGifOpen fn_DGifOpen
7531 # define DGifOpenFileName fn_DGifOpenFileName
7532 # define DGifSlurp fn_DGifSlurp
7533 # define GifErrorString fn_GifErrorString
7535 # endif /* WINDOWSNT */
7537 /* Reading a GIF image from memory
7538 Based on the PNG memory stuff to a certain extent. */
7540 typedef struct
7542 unsigned char *bytes;
7543 ptrdiff_t len;
7544 ptrdiff_t index;
7546 gif_memory_source;
7548 /* Make the current memory source available to gif_read_from_memory.
7549 It's done this way because not all versions of libungif support
7550 a UserData field in the GifFileType structure. */
7551 static gif_memory_source *current_gif_memory_src;
7553 static int
7554 gif_read_from_memory (GifFileType *file, GifByteType *buf, int len)
7556 gif_memory_source *src = current_gif_memory_src;
7558 if (len > src->len - src->index)
7559 return -1;
7561 memcpy (buf, src->bytes + src->index, len);
7562 src->index += len;
7563 return len;
7566 static int
7567 gif_close (GifFileType *gif, int *err)
7569 int retval;
7571 #if GIFLIB_MAJOR + (GIFLIB_MINOR >= 1) > 5
7572 retval = DGifCloseFile (gif, err);
7573 #else
7574 retval = DGifCloseFile (gif);
7575 #if GIFLIB_MAJOR >= 5
7576 if (err)
7577 *err = gif->Error;
7578 #endif
7579 #endif
7580 return retval;
7583 /* Load GIF image IMG for use on frame F. Value is true if
7584 successful. */
7586 static const int interlace_start[] = {0, 4, 2, 1};
7587 static const int interlace_increment[] = {8, 8, 4, 2};
7589 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7591 static bool
7592 gif_load (struct frame *f, struct image *img)
7594 Lisp_Object file;
7595 int rc, width, height, x, y, i, j;
7596 ColorMapObject *gif_color_map;
7597 unsigned long pixel_colors[256];
7598 GifFileType *gif;
7599 gif_memory_source memsrc;
7600 Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
7601 Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
7602 Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
7603 unsigned long bgcolor = 0;
7604 EMACS_INT idx;
7605 int gif_err;
7607 #ifdef USE_CAIRO
7608 unsigned char *data = 0;
7609 #else
7610 XImagePtr ximg;
7611 #endif
7613 if (NILP (specified_data))
7615 file = x_find_image_file (specified_file);
7616 if (!STRINGP (file))
7618 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7619 return 0;
7621 #ifdef WINDOWSNT
7622 file = ansi_encode_filename (file);
7623 #endif
7625 /* Open the GIF file. */
7626 #if GIFLIB_MAJOR < 5
7627 gif = DGifOpenFileName (SSDATA (file));
7628 if (gif == NULL)
7630 image_error ("Cannot open `%s'", file, Qnil);
7631 return 0;
7633 #else
7634 gif = DGifOpenFileName (SSDATA (file), &gif_err);
7635 if (gif == NULL)
7637 image_error ("Cannot open `%s': %s",
7638 file, build_string (GifErrorString (gif_err)));
7639 return 0;
7641 #endif
7643 else
7645 if (!STRINGP (specified_data))
7647 image_error ("Invalid image data `%s'", specified_data, Qnil);
7648 return 0;
7651 /* Read from memory! */
7652 current_gif_memory_src = &memsrc;
7653 memsrc.bytes = SDATA (specified_data);
7654 memsrc.len = SBYTES (specified_data);
7655 memsrc.index = 0;
7657 #if GIFLIB_MAJOR < 5
7658 gif = DGifOpen (&memsrc, gif_read_from_memory);
7659 if (!gif)
7661 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
7662 return 0;
7664 #else
7665 gif = DGifOpen (&memsrc, gif_read_from_memory, &gif_err);
7666 if (!gif)
7668 image_error ("Cannot open memory source `%s': %s",
7669 img->spec, build_string (GifErrorString (gif_err)));
7670 return 0;
7672 #endif
7675 /* Before reading entire contents, check the declared image size. */
7676 if (!check_image_size (f, gif->SWidth, gif->SHeight))
7678 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7679 gif_close (gif, NULL);
7680 return 0;
7683 /* Read entire contents. */
7684 rc = DGifSlurp (gif);
7685 if (rc == GIF_ERROR || gif->ImageCount <= 0)
7687 image_error ("Error reading `%s'", img->spec, Qnil);
7688 gif_close (gif, NULL);
7689 return 0;
7692 /* Which sub-image are we to display? */
7694 Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL);
7695 idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0;
7696 if (idx < 0 || idx >= gif->ImageCount)
7698 image_error ("Invalid image number `%s' in image `%s'",
7699 image_number, img->spec);
7700 gif_close (gif, NULL);
7701 return 0;
7705 width = img->width = gif->SWidth;
7706 height = img->height = gif->SHeight;
7708 img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top;
7709 img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left;
7710 img->corners[BOT_CORNER]
7711 = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height;
7712 img->corners[RIGHT_CORNER]
7713 = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width;
7715 if (!check_image_size (f, width, height))
7717 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
7718 gif_close (gif, NULL);
7719 return 0;
7722 /* Check that the selected subimages fit. It's not clear whether
7723 the GIF spec requires this, but Emacs can crash if they don't fit. */
7724 for (j = 0; j <= idx; ++j)
7726 struct SavedImage *subimage = gif->SavedImages + j;
7727 int subimg_width = subimage->ImageDesc.Width;
7728 int subimg_height = subimage->ImageDesc.Height;
7729 int subimg_top = subimage->ImageDesc.Top;
7730 int subimg_left = subimage->ImageDesc.Left;
7731 if (! (subimg_width >= 0 && subimg_height >= 0
7732 && 0 <= subimg_top && subimg_top <= height - subimg_height
7733 && 0 <= subimg_left && subimg_left <= width - subimg_width))
7735 image_error ("Subimage does not fit in image", Qnil, Qnil);
7736 gif_close (gif, NULL);
7737 return 0;
7741 #ifdef USE_CAIRO
7742 /* xzalloc so data is zero => transparent */
7743 data = (unsigned char *) xzalloc (width * height * 4);
7744 if (STRINGP (specified_bg))
7746 XColor color;
7747 if (x_defined_color (f, SSDATA (specified_bg), &color, 0))
7749 uint32_t *dataptr = (uint32_t *)data;
7750 int r = color.red/256;
7751 int g = color.green/256;
7752 int b = color.blue/256;
7754 for (y = 0; y < height; ++y)
7755 for (x = 0; x < width; ++x)
7756 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
7759 #else
7760 /* Create the X image and pixmap. */
7761 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
7763 gif_close (gif, NULL);
7764 return 0;
7767 /* Clear the part of the screen image not covered by the image.
7768 Full animated GIF support requires more here (see the gif89 spec,
7769 disposal methods). Let's simply assume that the part not covered
7770 by a sub-image is in the frame's background color. */
7771 for (y = 0; y < img->corners[TOP_CORNER]; ++y)
7772 for (x = 0; x < width; ++x)
7773 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7775 for (y = img->corners[BOT_CORNER]; y < height; ++y)
7776 for (x = 0; x < width; ++x)
7777 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7779 for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y)
7781 for (x = 0; x < img->corners[LEFT_CORNER]; ++x)
7782 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7783 for (x = img->corners[RIGHT_CORNER]; x < width; ++x)
7784 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7786 #endif
7788 /* Read the GIF image into the X image. */
7790 /* FIXME: With the current implementation, loading an animated gif
7791 is quadratic in the number of animation frames, since each frame
7792 is a separate struct image. We must provide a way for a single
7793 gif_load call to construct and save all animation frames. */
7795 init_color_table ();
7796 if (STRINGP (specified_bg))
7797 bgcolor = x_alloc_image_color (f, img, specified_bg,
7798 FRAME_BACKGROUND_PIXEL (f));
7799 for (j = 0; j <= idx; ++j)
7801 /* We use a local variable `raster' here because RasterBits is a
7802 char *, which invites problems with bytes >= 0x80. */
7803 struct SavedImage *subimage = gif->SavedImages + j;
7804 unsigned char *raster = (unsigned char *) subimage->RasterBits;
7805 int transparency_color_index = -1;
7806 int disposal = 0;
7807 int subimg_width = subimage->ImageDesc.Width;
7808 int subimg_height = subimage->ImageDesc.Height;
7809 int subimg_top = subimage->ImageDesc.Top;
7810 int subimg_left = subimage->ImageDesc.Left;
7812 /* Find the Graphic Control Extension block for this sub-image.
7813 Extract the disposal method and transparency color. */
7814 for (i = 0; i < subimage->ExtensionBlockCount; i++)
7816 ExtensionBlock *extblock = subimage->ExtensionBlocks + i;
7818 if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION)
7819 && extblock->ByteCount == 4
7820 && extblock->Bytes[0] & 1)
7822 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7823 to background". Treat any other value like 2. */
7824 disposal = (extblock->Bytes[0] >> 2) & 7;
7825 transparency_color_index = (unsigned char) extblock->Bytes[3];
7826 break;
7830 /* We can't "keep in place" the first subimage. */
7831 if (j == 0)
7832 disposal = 2;
7834 /* For disposal == 0, the spec says "No disposal specified. The
7835 decoder is not required to take any action." In practice, it
7836 seems we need to treat this like "keep in place", see e.g.
7837 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7838 if (disposal == 0)
7839 disposal = 1;
7841 gif_color_map = subimage->ImageDesc.ColorMap;
7842 if (!gif_color_map)
7843 gif_color_map = gif->SColorMap;
7845 #ifndef USE_CAIRO
7846 /* Allocate subimage colors. */
7847 memset (pixel_colors, 0, sizeof pixel_colors);
7849 if (gif_color_map)
7850 for (i = 0; i < gif_color_map->ColorCount; ++i)
7852 if (transparency_color_index == i)
7853 pixel_colors[i] = STRINGP (specified_bg)
7854 ? bgcolor : FRAME_BACKGROUND_PIXEL (f);
7855 else
7857 int r = gif_color_map->Colors[i].Red << 8;
7858 int g = gif_color_map->Colors[i].Green << 8;
7859 int b = gif_color_map->Colors[i].Blue << 8;
7860 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
7863 #endif
7865 /* Apply the pixel values. */
7866 if (GIFLIB_MAJOR < 5 && gif->SavedImages[j].ImageDesc.Interlace)
7868 int row, pass;
7870 for (y = 0, row = interlace_start[0], pass = 0;
7871 y < subimg_height;
7872 y++, row += interlace_increment[pass])
7874 while (subimg_height <= row)
7875 row = interlace_start[++pass];
7877 for (x = 0; x < subimg_width; x++)
7879 int c = raster[y * subimg_width + x];
7880 if (transparency_color_index != c || disposal != 1)
7882 #ifdef USE_CAIRO
7883 uint32_t *dataptr =
7884 ((uint32_t*)data + ((row + subimg_top) * subimg_width
7885 + x + subimg_left));
7886 int r = gif_color_map->Colors[c].Red;
7887 int g = gif_color_map->Colors[c].Green;
7888 int b = gif_color_map->Colors[c].Blue;
7890 if (transparency_color_index != c)
7891 *dataptr = (0xff << 24) | (r << 16) | (g << 8) | b;
7892 #else
7893 XPutPixel (ximg, x + subimg_left, row + subimg_top,
7894 pixel_colors[c]);
7895 #endif
7900 else
7902 for (y = 0; y < subimg_height; ++y)
7903 for (x = 0; x < subimg_width; ++x)
7905 int c = raster[y * subimg_width + x];
7906 if (transparency_color_index != c || disposal != 1)
7908 #ifdef USE_CAIRO
7909 uint32_t *dataptr =
7910 ((uint32_t*)data + ((y + subimg_top) * subimg_width
7911 + x + subimg_left));
7912 int r = gif_color_map->Colors[c].Red;
7913 int g = gif_color_map->Colors[c].Green;
7914 int b = gif_color_map->Colors[c].Blue;
7915 if (transparency_color_index != c)
7916 *dataptr = (0xff << 24) | (r << 16) | (g << 8) | b;
7917 #else
7918 XPutPixel (ximg, x + subimg_left, y + subimg_top,
7919 pixel_colors[c]);
7920 #endif
7926 #ifdef COLOR_TABLE_SUPPORT
7927 img->colors = colors_in_color_table (&img->ncolors);
7928 free_color_table ();
7929 #endif /* COLOR_TABLE_SUPPORT */
7931 /* Save GIF image extension data for `image-metadata'.
7932 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7933 img->lisp_data = Qnil;
7934 if (gif->SavedImages[idx].ExtensionBlockCount > 0)
7936 int delay = 0;
7937 ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks;
7938 for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++)
7939 /* Append (... FUNCTION "BYTES") */
7941 img->lisp_data
7942 = Fcons (make_number (ext->Function),
7943 Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
7944 img->lisp_data));
7945 if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION
7946 && ext->ByteCount == 4)
7948 delay = ext->Bytes[2] << CHAR_BIT;
7949 delay |= ext->Bytes[1];
7952 img->lisp_data = list2 (Qextension_data, img->lisp_data);
7953 if (delay)
7954 img->lisp_data
7955 = Fcons (Qdelay,
7956 Fcons (make_float (delay / 100.0),
7957 img->lisp_data));
7960 if (gif->ImageCount > 1)
7961 img->lisp_data = Fcons (Qcount,
7962 Fcons (make_number (gif->ImageCount),
7963 img->lisp_data));
7965 if (gif_close (gif, &gif_err) == GIF_ERROR)
7967 #if 5 <= GIFLIB_MAJOR
7968 char *error_text = GifErrorString (gif_err);
7970 if (error_text)
7971 image_error ("Error closing `%s': %s",
7972 img->spec, build_string (error_text));
7973 #else
7974 image_error ("Error closing `%s'", img->spec, Qnil);
7975 #endif
7978 #ifdef USE_CAIRO
7979 create_cairo_image_surface (img, data, width, height);
7980 #else
7981 /* Maybe fill in the background field while we have ximg handy. */
7982 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7983 /* Casting avoids a GCC warning. */
7984 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7986 /* Put ximg into the image. */
7987 image_put_x_image (f, img, ximg, 0);
7988 #endif
7990 return 1;
7993 #else /* !HAVE_GIF */
7995 #ifdef HAVE_NS
7996 static bool
7997 gif_load (struct frame *f, struct image *img)
7999 return ns_load_image (f, img,
8000 image_spec_value (img->spec, QCfile, NULL),
8001 image_spec_value (img->spec, QCdata, NULL));
8003 #endif /* HAVE_NS */
8005 #endif /* HAVE_GIF */
8008 #ifdef HAVE_IMAGEMAGICK
8010 /***********************************************************************
8011 ImageMagick
8012 ***********************************************************************/
8014 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
8015 safely rounded and clipped to int range. */
8017 static int
8018 scale_image_size (int size, size_t divisor, size_t multiplier)
8020 if (divisor != 0)
8022 double s = size;
8023 double scaled = s * multiplier / divisor + 0.5;
8024 if (scaled < INT_MAX)
8025 return scaled;
8027 return INT_MAX;
8030 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
8031 Use SPEC to deduce the size. Store the desired size into
8032 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
8033 static void
8034 compute_image_size (size_t width, size_t height,
8035 Lisp_Object spec,
8036 int *d_width, int *d_height)
8038 Lisp_Object value;
8039 int desired_width, desired_height;
8041 /* If width and/or height is set in the display spec assume we want
8042 to scale to those values. If either h or w is unspecified, the
8043 unspecified should be calculated from the specified to preserve
8044 aspect ratio. */
8045 value = image_spec_value (spec, QCwidth, NULL);
8046 desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
8047 value = image_spec_value (spec, QCheight, NULL);
8048 desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
8050 if (desired_width == -1)
8052 value = image_spec_value (spec, QCmax_width, NULL);
8053 if (NATNUMP (value))
8055 int max_width = min (XFASTINT (value), INT_MAX);
8056 if (max_width < width)
8058 /* The image is wider than :max-width. */
8059 desired_width = max_width;
8060 if (desired_height == -1)
8062 desired_height = scale_image_size (desired_width,
8063 width, height);
8064 value = image_spec_value (spec, QCmax_height, NULL);
8065 if (NATNUMP (value))
8067 int max_height = min (XFASTINT (value), INT_MAX);
8068 if (max_height < desired_height)
8070 desired_height = max_height;
8071 desired_width = scale_image_size (desired_height,
8072 height, width);
8080 if (desired_height == -1)
8082 value = image_spec_value (spec, QCmax_height, NULL);
8083 if (NATNUMP (value))
8085 int max_height = min (XFASTINT (value), INT_MAX);
8086 if (max_height < height)
8087 desired_height = max_height;
8091 if (desired_width != -1 && desired_height == -1)
8092 /* w known, calculate h. */
8093 desired_height = scale_image_size (desired_width, width, height);
8095 if (desired_width == -1 && desired_height != -1)
8096 /* h known, calculate w. */
8097 desired_width = scale_image_size (desired_height, height, width);
8099 *d_width = desired_width;
8100 *d_height = desired_height;
8103 static bool imagemagick_image_p (Lisp_Object);
8104 static bool imagemagick_load (struct frame *, struct image *);
8105 static void imagemagick_clear_image (struct frame *, struct image *);
8107 /* Indices of image specification fields in imagemagick_format. */
8109 enum imagemagick_keyword_index
8111 IMAGEMAGICK_TYPE,
8112 IMAGEMAGICK_DATA,
8113 IMAGEMAGICK_FILE,
8114 IMAGEMAGICK_ASCENT,
8115 IMAGEMAGICK_MARGIN,
8116 IMAGEMAGICK_RELIEF,
8117 IMAGEMAGICK_ALGORITHM,
8118 IMAGEMAGICK_HEURISTIC_MASK,
8119 IMAGEMAGICK_MASK,
8120 IMAGEMAGICK_BACKGROUND,
8121 IMAGEMAGICK_HEIGHT,
8122 IMAGEMAGICK_WIDTH,
8123 IMAGEMAGICK_MAX_HEIGHT,
8124 IMAGEMAGICK_MAX_WIDTH,
8125 IMAGEMAGICK_FORMAT,
8126 IMAGEMAGICK_ROTATION,
8127 IMAGEMAGICK_CROP,
8128 IMAGEMAGICK_LAST
8131 /* Vector of image_keyword structures describing the format
8132 of valid user-defined image specifications. */
8134 static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] =
8136 {":type", IMAGE_SYMBOL_VALUE, 1},
8137 {":data", IMAGE_STRING_VALUE, 0},
8138 {":file", IMAGE_STRING_VALUE, 0},
8139 {":ascent", IMAGE_ASCENT_VALUE, 0},
8140 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
8141 {":relief", IMAGE_INTEGER_VALUE, 0},
8142 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8143 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8144 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8145 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
8146 {":height", IMAGE_INTEGER_VALUE, 0},
8147 {":width", IMAGE_INTEGER_VALUE, 0},
8148 {":max-height", IMAGE_INTEGER_VALUE, 0},
8149 {":max-width", IMAGE_INTEGER_VALUE, 0},
8150 {":format", IMAGE_SYMBOL_VALUE, 0},
8151 {":rotation", IMAGE_NUMBER_VALUE, 0},
8152 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8155 #if defined HAVE_NTGUI && defined WINDOWSNT
8156 static bool init_imagemagick_functions (void);
8157 #else
8158 #define init_imagemagick_functions NULL
8159 #endif
8161 /* Structure describing the image type for any image handled via
8162 ImageMagick. */
8164 static struct image_type imagemagick_type =
8166 SYMBOL_INDEX (Qimagemagick),
8167 imagemagick_image_p,
8168 imagemagick_load,
8169 imagemagick_clear_image,
8170 init_imagemagick_functions,
8171 NULL
8174 /* Free X resources of imagemagick image IMG which is used on frame F. */
8176 static void
8177 imagemagick_clear_image (struct frame *f,
8178 struct image *img)
8180 x_clear_image (f, img);
8183 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
8184 this by calling parse_image_spec and supplying the keywords that
8185 identify the IMAGEMAGICK format. */
8187 static bool
8188 imagemagick_image_p (Lisp_Object object)
8190 struct image_keyword fmt[IMAGEMAGICK_LAST];
8191 memcpy (fmt, imagemagick_format, sizeof fmt);
8193 if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick))
8194 return 0;
8196 /* Must specify either the :data or :file keyword. */
8197 return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1;
8200 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
8201 Therefore rename the function so it doesn't collide with ImageMagick. */
8202 #define DrawRectangle DrawRectangleGif
8203 #include <wand/MagickWand.h>
8205 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
8206 Emacs seems to work fine with the hidden version, so unhide it. */
8207 #include <magick/version.h>
8208 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
8209 extern WandExport void PixelGetMagickColor (const PixelWand *,
8210 MagickPixelPacket *);
8211 #endif
8213 /* Log ImageMagick error message.
8214 Useful when a ImageMagick function returns the status `MagickFalse'. */
8216 static void
8217 imagemagick_error (MagickWand *wand)
8219 char *description;
8220 ExceptionType severity;
8222 description = MagickGetException (wand, &severity);
8223 image_error ("ImageMagick error: %s",
8224 build_string (description),
8225 Qnil);
8226 MagickRelinquishMemory (description);
8229 /* Possibly give ImageMagick some extra help to determine the image
8230 type by supplying a "dummy" filename based on the Content-Type. */
8232 static char *
8233 imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent])
8235 Lisp_Object symbol = intern ("image-format-suffixes");
8236 Lisp_Object val = find_symbol_value (symbol);
8237 Lisp_Object format;
8239 if (! CONSP (val))
8240 return NULL;
8242 format = image_spec_value (spec, intern (":format"), NULL);
8243 val = Fcar_safe (Fcdr_safe (Fassq (format, val)));
8244 if (! STRINGP (val))
8245 return NULL;
8247 /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
8248 as ImageMagick would ignore the extra bytes anyway. */
8249 snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val));
8250 return hint_buffer;
8253 /* Animated images (e.g., GIF89a) are composed from one "master image"
8254 (which is the first one, and then there's a number of images that
8255 follow. If following images have non-transparent colors, these are
8256 composed "on top" of the master image. So, in general, one has to
8257 compute ann the preceding images to be able to display a particular
8258 sub-image.
8260 Computing all the preceding images is too slow, so we maintain a
8261 cache of previously computed images. We have to maintain a cache
8262 separate from the image cache, because the images may be scaled
8263 before display. */
8265 struct animation_cache
8267 MagickWand *wand;
8268 int index;
8269 struct timespec update_time;
8270 struct animation_cache *next;
8271 char signature[FLEXIBLE_ARRAY_MEMBER];
8274 static struct animation_cache *animation_cache = NULL;
8276 static struct animation_cache *
8277 imagemagick_create_cache (char *signature)
8279 struct animation_cache *cache
8280 = xmalloc (offsetof (struct animation_cache, signature)
8281 + strlen (signature) + 1);
8282 cache->wand = 0;
8283 cache->index = 0;
8284 cache->next = 0;
8285 strcpy (cache->signature, signature);
8286 return cache;
8289 /* Discard cached images that haven't been used for a minute. */
8290 static void
8291 imagemagick_prune_animation_cache (void)
8293 struct animation_cache **pcache = &animation_cache;
8294 struct timespec old = timespec_sub (current_timespec (),
8295 make_timespec (60, 0));
8297 while (*pcache)
8299 struct animation_cache *cache = *pcache;
8300 if (timespec_cmp (old, cache->update_time) <= 0)
8301 pcache = &cache->next;
8302 else
8304 if (cache->wand)
8305 DestroyMagickWand (cache->wand);
8306 *pcache = cache->next;
8307 xfree (cache);
8312 static struct animation_cache *
8313 imagemagick_get_animation_cache (MagickWand *wand)
8315 char *signature = MagickGetImageSignature (wand);
8316 struct animation_cache *cache;
8317 struct animation_cache **pcache = &animation_cache;
8319 imagemagick_prune_animation_cache ();
8321 while (1)
8323 cache = *pcache;
8324 if (! cache)
8326 *pcache = cache = imagemagick_create_cache (signature);
8327 break;
8329 if (strcmp (signature, cache->signature) == 0)
8330 break;
8331 pcache = &cache->next;
8334 DestroyString (signature);
8335 cache->update_time = current_timespec ();
8336 return cache;
8339 static MagickWand *
8340 imagemagick_compute_animated_image (MagickWand *super_wand, int ino)
8342 int i;
8343 MagickWand *composite_wand;
8344 size_t dest_width, dest_height;
8345 struct animation_cache *cache = imagemagick_get_animation_cache (super_wand);
8347 MagickSetIteratorIndex (super_wand, 0);
8349 if (ino == 0 || cache->wand == NULL || cache->index > ino)
8351 composite_wand = MagickGetImage (super_wand);
8352 if (cache->wand)
8353 DestroyMagickWand (cache->wand);
8355 else
8356 composite_wand = cache->wand;
8358 dest_height = MagickGetImageHeight (composite_wand);
8360 for (i = max (1, cache->index + 1); i <= ino; i++)
8362 MagickWand *sub_wand;
8363 PixelIterator *source_iterator, *dest_iterator;
8364 PixelWand **source, **dest;
8365 size_t source_width, source_height;
8366 ssize_t source_left, source_top;
8367 MagickPixelPacket pixel;
8368 DisposeType dispose;
8369 ptrdiff_t lines = 0;
8371 MagickSetIteratorIndex (super_wand, i);
8372 sub_wand = MagickGetImage (super_wand);
8374 MagickGetImagePage (sub_wand, &source_width, &source_height,
8375 &source_left, &source_top);
8377 /* This flag says how to handle transparent pixels. */
8378 dispose = MagickGetImageDispose (sub_wand);
8380 source_iterator = NewPixelIterator (sub_wand);
8381 if (! source_iterator)
8383 DestroyMagickWand (composite_wand);
8384 DestroyMagickWand (sub_wand);
8385 cache->wand = NULL;
8386 image_error ("Imagemagick pixel iterator creation failed",
8387 Qnil, Qnil);
8388 return NULL;
8391 dest_iterator = NewPixelIterator (composite_wand);
8392 if (! dest_iterator)
8394 DestroyMagickWand (composite_wand);
8395 DestroyMagickWand (sub_wand);
8396 DestroyPixelIterator (source_iterator);
8397 cache->wand = NULL;
8398 image_error ("Imagemagick pixel iterator creation failed",
8399 Qnil, Qnil);
8400 return NULL;
8403 /* The sub-image may not start at origin, so move the destination
8404 iterator to where the sub-image should start. */
8405 if (source_top > 0)
8407 PixelSetIteratorRow (dest_iterator, source_top);
8408 lines = source_top;
8411 while ((source = PixelGetNextIteratorRow (source_iterator, &source_width))
8412 != NULL)
8414 ptrdiff_t x;
8416 /* Sanity check. This shouldn't happen, but apparently
8417 does in some pictures. */
8418 if (++lines >= dest_height)
8419 break;
8421 dest = PixelGetNextIteratorRow (dest_iterator, &dest_width);
8422 for (x = 0; x < source_width; x++)
8424 /* Sanity check. This shouldn't happen, but apparently
8425 also does in some pictures. */
8426 if (x + source_left >= dest_width)
8427 break;
8428 /* Normally we only copy over non-transparent pixels,
8429 but if the disposal method is "Background", then we
8430 copy over all pixels. */
8431 if (dispose == BackgroundDispose || PixelGetAlpha (source[x]))
8433 PixelGetMagickColor (source[x], &pixel);
8434 PixelSetMagickColor (dest[x + source_left], &pixel);
8437 PixelSyncIterator (dest_iterator);
8440 DestroyPixelIterator (source_iterator);
8441 DestroyPixelIterator (dest_iterator);
8442 DestroyMagickWand (sub_wand);
8445 /* Cache a copy for the next iteration. The current wand will be
8446 destroyed by the caller. */
8447 cache->wand = CloneMagickWand (composite_wand);
8448 cache->index = ino;
8450 return composite_wand;
8454 /* Helper function for imagemagick_load, which does the actual loading
8455 given contents and size, apart from frame and image structures,
8456 passed from imagemagick_load. Uses librimagemagick to do most of
8457 the image processing.
8459 F is a pointer to the Emacs frame; IMG to the image structure to
8460 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
8461 be parsed; SIZE is the number of bytes of data; and FILENAME is
8462 either the file name or the image data.
8464 Return true if successful. */
8466 static bool
8467 imagemagick_load_image (struct frame *f, struct image *img,
8468 unsigned char *contents, unsigned int size,
8469 char *filename)
8471 int width, height;
8472 size_t image_width, image_height;
8473 MagickBooleanType status;
8474 XImagePtr ximg;
8475 int x, y;
8476 MagickWand *image_wand;
8477 PixelIterator *iterator;
8478 PixelWand **pixels, *bg_wand = NULL;
8479 MagickPixelPacket pixel;
8480 Lisp_Object image;
8481 Lisp_Object value;
8482 Lisp_Object crop;
8483 EMACS_INT ino;
8484 int desired_width, desired_height;
8485 double rotation;
8486 int pixelwidth;
8487 char hint_buffer[MaxTextExtent];
8488 char *filename_hint = NULL;
8490 /* Handle image index for image types who can contain more than one image.
8491 Interface :index is same as for GIF. First we "ping" the image to see how
8492 many sub-images it contains. Pinging is faster than loading the image to
8493 find out things about it. */
8495 /* Initialize the imagemagick environment. */
8496 MagickWandGenesis ();
8497 image = image_spec_value (img->spec, QCindex, NULL);
8498 ino = INTEGERP (image) ? XFASTINT (image) : 0;
8499 image_wand = NewMagickWand ();
8501 if (filename)
8502 status = MagickReadImage (image_wand, filename);
8503 else
8505 filename_hint = imagemagick_filename_hint (img->spec, hint_buffer);
8506 MagickSetFilename (image_wand, filename_hint);
8507 status = MagickReadImageBlob (image_wand, contents, size);
8510 if (status == MagickFalse)
8512 imagemagick_error (image_wand);
8513 DestroyMagickWand (image_wand);
8514 return 0;
8517 if (ino < 0 || ino >= MagickGetNumberImages (image_wand))
8519 image_error ("Invalid image number `%s' in image `%s'",
8520 image, img->spec);
8521 DestroyMagickWand (image_wand);
8522 return 0;
8525 if (MagickGetImageDelay (image_wand) > 0)
8526 img->lisp_data =
8527 Fcons (Qdelay,
8528 Fcons (make_float (MagickGetImageDelay (image_wand) / 100.0),
8529 img->lisp_data));
8531 if (MagickGetNumberImages (image_wand) > 1)
8532 img->lisp_data =
8533 Fcons (Qcount,
8534 Fcons (make_number (MagickGetNumberImages (image_wand)),
8535 img->lisp_data));
8537 /* If we have an animated image, get the new wand based on the
8538 "super-wand". */
8539 if (MagickGetNumberImages (image_wand) > 1)
8541 MagickWand *super_wand = image_wand;
8542 image_wand = imagemagick_compute_animated_image (super_wand, ino);
8543 if (! image_wand)
8544 image_wand = super_wand;
8545 else
8546 DestroyMagickWand (super_wand);
8549 /* Retrieve the frame's background color, for use later. */
8551 XColor bgcolor;
8552 Lisp_Object specified_bg;
8554 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
8555 if (!STRINGP (specified_bg)
8556 || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0))
8557 x_query_frame_background_color (f, &bgcolor);
8559 bg_wand = NewPixelWand ();
8560 PixelSetRed (bg_wand, (double) bgcolor.red / 65535);
8561 PixelSetGreen (bg_wand, (double) bgcolor.green / 65535);
8562 PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535);
8565 compute_image_size (MagickGetImageWidth (image_wand),
8566 MagickGetImageHeight (image_wand),
8567 img->spec, &desired_width, &desired_height);
8569 if (desired_width != -1 && desired_height != -1)
8571 status = MagickScaleImage (image_wand, desired_width, desired_height);
8572 if (status == MagickFalse)
8574 image_error ("Imagemagick scale failed", Qnil, Qnil);
8575 imagemagick_error (image_wand);
8576 goto imagemagick_error;
8580 /* crop behaves similar to image slicing in Emacs but is more memory
8581 efficient. */
8582 crop = image_spec_value (img->spec, QCcrop, NULL);
8584 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8586 /* After some testing, it seems MagickCropImage is the fastest crop
8587 function in ImageMagick. This crop function seems to do less copying
8588 than the alternatives, but it still reads the entire image into memory
8589 before cropping, which is apparently difficult to avoid when using
8590 imagemagick. */
8591 size_t crop_width = XINT (XCAR (crop));
8592 crop = XCDR (crop);
8593 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8595 size_t crop_height = XINT (XCAR (crop));
8596 crop = XCDR (crop);
8597 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8599 ssize_t crop_x = XINT (XCAR (crop));
8600 crop = XCDR (crop);
8601 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8603 ssize_t crop_y = XINT (XCAR (crop));
8604 MagickCropImage (image_wand, crop_width, crop_height,
8605 crop_x, crop_y);
8611 /* Furthermore :rotation. we need background color and angle for
8612 rotation. */
8614 TODO background handling for rotation specified_bg =
8615 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8616 (specified_bg). */
8617 value = image_spec_value (img->spec, QCrotation, NULL);
8618 if (FLOATP (value))
8620 rotation = extract_float (value);
8621 status = MagickRotateImage (image_wand, bg_wand, rotation);
8622 if (status == MagickFalse)
8624 image_error ("Imagemagick image rotate failed", Qnil, Qnil);
8625 imagemagick_error (image_wand);
8626 goto imagemagick_error;
8630 /* Set the canvas background color to the frame or specified
8631 background, and flatten the image. Note: as of ImageMagick
8632 6.6.0, SVG image transparency is not handled properly
8633 (e.g. etc/images/splash.svg shows a white background always). */
8635 MagickWand *new_wand;
8636 MagickSetImageBackgroundColor (image_wand, bg_wand);
8637 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8638 new_wand = MagickMergeImageLayers (image_wand, MergeLayer);
8639 #else
8640 new_wand = MagickFlattenImages (image_wand);
8641 #endif
8642 DestroyMagickWand (image_wand);
8643 image_wand = new_wand;
8646 /* Finally we are done manipulating the image. Figure out the
8647 resulting width/height and transfer ownership to Emacs. */
8648 image_height = MagickGetImageHeight (image_wand);
8649 image_width = MagickGetImageWidth (image_wand);
8651 if (! (image_width <= INT_MAX && image_height <= INT_MAX
8652 && check_image_size (f, image_width, image_height)))
8654 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
8655 goto imagemagick_error;
8658 width = image_width;
8659 height = image_height;
8661 /* We can now get a valid pixel buffer from the imagemagick file, if all
8662 went ok. */
8664 init_color_table ();
8666 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8667 if (imagemagick_render_type != 0)
8669 /* Magicexportimage is normally faster than pixelpushing. This
8670 method is also well tested. Some aspects of this method are
8671 ad-hoc and needs to be more researched. */
8672 int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/
8673 const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/
8674 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8675 if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth,
8676 &ximg, 0))
8678 #ifdef COLOR_TABLE_SUPPORT
8679 free_color_table ();
8680 #endif
8681 image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
8682 goto imagemagick_error;
8685 /* Oddly, the below code doesn't seem to work:*/
8686 /* switch(ximg->bitmap_unit){ */
8687 /* case 8: */
8688 /* pixelwidth=CharPixel; */
8689 /* break; */
8690 /* case 16: */
8691 /* pixelwidth=ShortPixel; */
8692 /* break; */
8693 /* case 32: */
8694 /* pixelwidth=LongPixel; */
8695 /* break; */
8696 /* } */
8698 Here im just guessing the format of the bitmap.
8699 happens to work fine for:
8700 - bw djvu images
8701 on rgb display.
8702 seems about 3 times as fast as pixel pushing(not carefully measured)
8704 pixelwidth = CharPixel; /*??? TODO figure out*/
8705 MagickExportImagePixels (image_wand, 0, 0, width, height,
8706 exportdepth, pixelwidth, ximg->data);
8708 else
8709 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8711 size_t image_height;
8712 MagickRealType color_scale = 65535.0 / QuantumRange;
8714 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8715 if (!image_create_x_image_and_pixmap (f, img, width, height, 0,
8716 &ximg, 0))
8718 #ifdef COLOR_TABLE_SUPPORT
8719 free_color_table ();
8720 #endif
8721 image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil);
8722 goto imagemagick_error;
8725 /* Copy imagemagick image to x with primitive yet robust pixel
8726 pusher loop. This has been tested a lot with many different
8727 images. */
8729 /* Copy pixels from the imagemagick image structure to the x image map. */
8730 iterator = NewPixelIterator (image_wand);
8731 if (! iterator)
8733 #ifdef COLOR_TABLE_SUPPORT
8734 free_color_table ();
8735 #endif
8736 x_destroy_x_image (ximg);
8737 image_error ("Imagemagick pixel iterator creation failed",
8738 Qnil, Qnil);
8739 goto imagemagick_error;
8742 image_height = MagickGetImageHeight (image_wand);
8743 for (y = 0; y < image_height; y++)
8745 size_t row_width;
8746 pixels = PixelGetNextIteratorRow (iterator, &row_width);
8747 if (! pixels)
8748 break;
8749 int xlim = min (row_width, width);
8750 for (x = 0; x < xlim; x++)
8752 PixelGetMagickColor (pixels[x], &pixel);
8753 XPutPixel (ximg, x, y,
8754 lookup_rgb_color (f,
8755 color_scale * pixel.red,
8756 color_scale * pixel.green,
8757 color_scale * pixel.blue));
8760 DestroyPixelIterator (iterator);
8763 #ifdef COLOR_TABLE_SUPPORT
8764 /* Remember colors allocated for this image. */
8765 img->colors = colors_in_color_table (&img->ncolors);
8766 free_color_table ();
8767 #endif /* COLOR_TABLE_SUPPORT */
8769 img->width = width;
8770 img->height = height;
8772 /* Put ximg into the image. */
8773 image_put_x_image (f, img, ximg, 0);
8775 /* Final cleanup. image_wand should be the only resource left. */
8776 DestroyMagickWand (image_wand);
8777 if (bg_wand) DestroyPixelWand (bg_wand);
8779 /* `MagickWandTerminus' terminates the imagemagick environment. */
8780 MagickWandTerminus ();
8782 return 1;
8784 imagemagick_error:
8785 DestroyMagickWand (image_wand);
8786 if (bg_wand) DestroyPixelWand (bg_wand);
8788 MagickWandTerminus ();
8789 /* TODO more cleanup. */
8790 image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil);
8791 return 0;
8795 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8796 successful. this function will go into the imagemagick_type structure, and
8797 the prototype thus needs to be compatible with that structure. */
8799 static bool
8800 imagemagick_load (struct frame *f, struct image *img)
8802 bool success_p = 0;
8803 Lisp_Object file_name;
8805 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8806 file_name = image_spec_value (img->spec, QCfile, NULL);
8807 if (STRINGP (file_name))
8809 Lisp_Object file;
8811 file = x_find_image_file (file_name);
8812 if (!STRINGP (file))
8814 image_error ("Cannot find image file `%s'", file_name, Qnil);
8815 return 0;
8817 #ifdef WINDOWSNT
8818 file = ansi_encode_filename (file);
8819 #endif
8820 success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file));
8822 /* Else its not a file, its a lisp object. Load the image from a
8823 lisp object rather than a file. */
8824 else
8826 Lisp_Object data;
8828 data = image_spec_value (img->spec, QCdata, NULL);
8829 if (!STRINGP (data))
8831 image_error ("Invalid image data `%s'", data, Qnil);
8832 return 0;
8834 success_p = imagemagick_load_image (f, img, SDATA (data),
8835 SBYTES (data), NULL);
8838 return success_p;
8841 DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0,
8842 doc: /* Return a list of image types supported by ImageMagick.
8843 Each entry in this list is a symbol named after an ImageMagick format
8844 tag. See the ImageMagick manual for a list of ImageMagick formats and
8845 their descriptions (http://www.imagemagick.org/script/formats.php).
8846 You can also try the shell command: `identify -list format'.
8848 Note that ImageMagick recognizes many file-types that Emacs does not
8849 recognize as images, such as C. See `imagemagick-types-enable'
8850 and `imagemagick-types-inhibit'. */)
8851 (void)
8853 Lisp_Object typelist = Qnil;
8854 size_t numf = 0;
8855 ExceptionInfo ex;
8856 char **imtypes;
8857 size_t i;
8859 GetExceptionInfo(&ex);
8860 imtypes = GetMagickList ("*", &numf, &ex);
8861 DestroyExceptionInfo(&ex);
8863 for (i = 0; i < numf; i++)
8865 Lisp_Object imagemagicktype = intern (imtypes[i]);
8866 typelist = Fcons (imagemagicktype, typelist);
8867 imtypes[i] = MagickRelinquishMemory (imtypes[i]);
8870 MagickRelinquishMemory (imtypes);
8871 return Fnreverse (typelist);
8874 #endif /* defined (HAVE_IMAGEMAGICK) */
8878 /***********************************************************************
8880 ***********************************************************************/
8882 #ifdef HAVE_RSVG
8884 /* Function prototypes. */
8886 static bool svg_image_p (Lisp_Object object);
8887 static bool svg_load (struct frame *f, struct image *img);
8889 static bool svg_load_image (struct frame *, struct image *,
8890 unsigned char *, ptrdiff_t, char *);
8892 /* Indices of image specification fields in svg_format, below. */
8894 enum svg_keyword_index
8896 SVG_TYPE,
8897 SVG_DATA,
8898 SVG_FILE,
8899 SVG_ASCENT,
8900 SVG_MARGIN,
8901 SVG_RELIEF,
8902 SVG_ALGORITHM,
8903 SVG_HEURISTIC_MASK,
8904 SVG_MASK,
8905 SVG_BACKGROUND,
8906 SVG_LAST
8909 /* Vector of image_keyword structures describing the format
8910 of valid user-defined image specifications. */
8912 static const struct image_keyword svg_format[SVG_LAST] =
8914 {":type", IMAGE_SYMBOL_VALUE, 1},
8915 {":data", IMAGE_STRING_VALUE, 0},
8916 {":file", IMAGE_STRING_VALUE, 0},
8917 {":ascent", IMAGE_ASCENT_VALUE, 0},
8918 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
8919 {":relief", IMAGE_INTEGER_VALUE, 0},
8920 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8921 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8922 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8923 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8926 # if defined HAVE_NTGUI && defined WINDOWSNT
8927 static bool init_svg_functions (void);
8928 # else
8929 #define init_svg_functions NULL
8930 # endif
8932 /* Structure describing the image type `svg'. Its the same type of
8933 structure defined for all image formats, handled by emacs image
8934 functions. See struct image_type in dispextern.h. */
8936 static struct image_type svg_type =
8938 SYMBOL_INDEX (Qsvg),
8939 svg_image_p,
8940 svg_load,
8941 x_clear_image,
8942 init_svg_functions,
8943 NULL
8947 /* Return true if OBJECT is a valid SVG image specification. Do
8948 this by calling parse_image_spec and supplying the keywords that
8949 identify the SVG format. */
8951 static bool
8952 svg_image_p (Lisp_Object object)
8954 struct image_keyword fmt[SVG_LAST];
8955 memcpy (fmt, svg_format, sizeof fmt);
8957 if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg))
8958 return 0;
8960 /* Must specify either the :data or :file keyword. */
8961 return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1;
8964 # include <librsvg/rsvg.h>
8966 # ifdef WINDOWSNT
8968 /* SVG library functions. */
8969 DEF_DLL_FN (RsvgHandle *, rsvg_handle_new, (void));
8970 DEF_DLL_FN (void, rsvg_handle_get_dimensions,
8971 (RsvgHandle *, RsvgDimensionData *));
8972 DEF_DLL_FN (gboolean, rsvg_handle_write,
8973 (RsvgHandle *, const guchar *, gsize, GError **));
8974 DEF_DLL_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **));
8975 DEF_DLL_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *));
8976 DEF_DLL_FN (void, rsvg_handle_set_base_uri, (RsvgHandle *, const char *));
8978 DEF_DLL_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *));
8979 DEF_DLL_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *));
8980 DEF_DLL_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *));
8981 DEF_DLL_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *));
8982 DEF_DLL_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *));
8983 DEF_DLL_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *));
8984 DEF_DLL_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *));
8985 DEF_DLL_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *));
8987 # if ! GLIB_CHECK_VERSION (2, 36, 0)
8988 DEF_DLL_FN (void, g_type_init, (void));
8989 # endif
8990 DEF_DLL_FN (void, g_object_unref, (gpointer));
8991 DEF_DLL_FN (void, g_error_free, (GError *));
8993 static bool
8994 init_svg_functions (void)
8996 HMODULE library, gdklib = NULL, glib = NULL, gobject = NULL;
8998 if (!(glib = w32_delayed_load (Qglib))
8999 || !(gobject = w32_delayed_load (Qgobject))
9000 || !(gdklib = w32_delayed_load (Qgdk_pixbuf))
9001 || !(library = w32_delayed_load (Qsvg)))
9003 if (gdklib) FreeLibrary (gdklib);
9004 if (gobject) FreeLibrary (gobject);
9005 if (glib) FreeLibrary (glib);
9006 return 0;
9009 LOAD_DLL_FN (library, rsvg_handle_new);
9010 LOAD_DLL_FN (library, rsvg_handle_get_dimensions);
9011 LOAD_DLL_FN (library, rsvg_handle_write);
9012 LOAD_DLL_FN (library, rsvg_handle_close);
9013 LOAD_DLL_FN (library, rsvg_handle_get_pixbuf);
9014 LOAD_DLL_FN (library, rsvg_handle_set_base_uri);
9016 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_width);
9017 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_height);
9018 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_pixels);
9019 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_rowstride);
9020 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_colorspace);
9021 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_n_channels);
9022 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_has_alpha);
9023 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_bits_per_sample);
9025 # if ! GLIB_CHECK_VERSION (2, 36, 0)
9026 LOAD_DLL_FN (gobject, g_type_init);
9027 # endif
9028 LOAD_DLL_FN (gobject, g_object_unref);
9029 LOAD_DLL_FN (glib, g_error_free);
9031 return 1;
9034 /* The following aliases for library functions allow dynamic loading
9035 to be used on some platforms. */
9037 # undef gdk_pixbuf_get_bits_per_sample
9038 # undef gdk_pixbuf_get_colorspace
9039 # undef gdk_pixbuf_get_has_alpha
9040 # undef gdk_pixbuf_get_height
9041 # undef gdk_pixbuf_get_n_channels
9042 # undef gdk_pixbuf_get_pixels
9043 # undef gdk_pixbuf_get_rowstride
9044 # undef gdk_pixbuf_get_width
9045 # undef g_error_free
9046 # undef g_object_unref
9047 # undef g_type_init
9048 # undef rsvg_handle_close
9049 # undef rsvg_handle_get_dimensions
9050 # undef rsvg_handle_get_pixbuf
9051 # undef rsvg_handle_new
9052 # undef rsvg_handle_set_base_uri
9053 # undef rsvg_handle_write
9055 # define gdk_pixbuf_get_bits_per_sample fn_gdk_pixbuf_get_bits_per_sample
9056 # define gdk_pixbuf_get_colorspace fn_gdk_pixbuf_get_colorspace
9057 # define gdk_pixbuf_get_has_alpha fn_gdk_pixbuf_get_has_alpha
9058 # define gdk_pixbuf_get_height fn_gdk_pixbuf_get_height
9059 # define gdk_pixbuf_get_n_channels fn_gdk_pixbuf_get_n_channels
9060 # define gdk_pixbuf_get_pixels fn_gdk_pixbuf_get_pixels
9061 # define gdk_pixbuf_get_rowstride fn_gdk_pixbuf_get_rowstride
9062 # define gdk_pixbuf_get_width fn_gdk_pixbuf_get_width
9063 # define g_error_free fn_g_error_free
9064 # define g_object_unref fn_g_object_unref
9065 # define g_type_init fn_g_type_init
9066 # define rsvg_handle_close fn_rsvg_handle_close
9067 # define rsvg_handle_get_dimensions fn_rsvg_handle_get_dimensions
9068 # define rsvg_handle_get_pixbuf fn_rsvg_handle_get_pixbuf
9069 # define rsvg_handle_new fn_rsvg_handle_new
9070 # define rsvg_handle_set_base_uri fn_rsvg_handle_set_base_uri
9071 # define rsvg_handle_write fn_rsvg_handle_write
9073 # endif /* !WINDOWSNT */
9075 /* Load SVG image IMG for use on frame F. Value is true if
9076 successful. */
9078 static bool
9079 svg_load (struct frame *f, struct image *img)
9081 bool success_p = 0;
9082 Lisp_Object file_name;
9084 /* If IMG->spec specifies a file name, create a non-file spec from it. */
9085 file_name = image_spec_value (img->spec, QCfile, NULL);
9086 if (STRINGP (file_name))
9088 Lisp_Object file;
9089 unsigned char *contents;
9090 ptrdiff_t size;
9092 file = x_find_image_file (file_name);
9093 if (!STRINGP (file))
9095 image_error ("Cannot find image file `%s'", file_name, Qnil);
9096 return 0;
9099 /* Read the entire file into memory. */
9100 contents = slurp_file (SSDATA (file), &size);
9101 if (contents == NULL)
9103 image_error ("Error loading SVG image `%s'", img->spec, Qnil);
9104 return 0;
9106 /* If the file was slurped into memory properly, parse it. */
9107 success_p = svg_load_image (f, img, contents, size, SSDATA (file));
9108 xfree (contents);
9110 /* Else its not a file, its a lisp object. Load the image from a
9111 lisp object rather than a file. */
9112 else
9114 Lisp_Object data, original_filename;
9116 data = image_spec_value (img->spec, QCdata, NULL);
9117 if (!STRINGP (data))
9119 image_error ("Invalid image data `%s'", data, Qnil);
9120 return 0;
9122 original_filename = BVAR (current_buffer, filename);
9123 success_p = svg_load_image (f, img, SDATA (data), SBYTES (data),
9124 (NILP (original_filename) ? NULL
9125 : SSDATA (original_filename)));
9128 return success_p;
9131 /* svg_load_image is a helper function for svg_load, which does the
9132 actual loading given contents and size, apart from frame and image
9133 structures, passed from svg_load.
9135 Uses librsvg to do most of the image processing.
9137 Returns true when successful. */
9138 static bool
9139 svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */
9140 struct image *img, /* Pointer to emacs image structure. */
9141 unsigned char *contents, /* String containing the SVG XML data to be parsed. */
9142 ptrdiff_t size, /* Size of data in bytes. */
9143 char *filename) /* Name of SVG file being loaded. */
9145 RsvgHandle *rsvg_handle;
9146 RsvgDimensionData dimension_data;
9147 GError *err = NULL;
9148 GdkPixbuf *pixbuf;
9149 int width;
9150 int height;
9151 const guint8 *pixels;
9152 int rowstride;
9153 XImagePtr ximg;
9154 Lisp_Object specified_bg;
9155 XColor background;
9156 int x;
9157 int y;
9159 #if ! GLIB_CHECK_VERSION (2, 36, 0)
9160 /* g_type_init is a glib function that must be called prior to
9161 using gnome type library functions (obsolete since 2.36.0). */
9162 g_type_init ();
9163 #endif
9165 /* Make a handle to a new rsvg object. */
9166 rsvg_handle = rsvg_handle_new ();
9168 /* Set base_uri for properly handling referenced images (via 'href').
9169 See rsvg bug 596114 - "image refs are relative to curdir, not .svg file"
9170 (https://bugzilla.gnome.org/show_bug.cgi?id=596114). */
9171 if (filename)
9172 rsvg_handle_set_base_uri(rsvg_handle, filename);
9174 /* Parse the contents argument and fill in the rsvg_handle. */
9175 rsvg_handle_write (rsvg_handle, contents, size, &err);
9176 if (err) goto rsvg_error;
9178 /* The parsing is complete, rsvg_handle is ready to used, close it
9179 for further writes. */
9180 rsvg_handle_close (rsvg_handle, &err);
9181 if (err) goto rsvg_error;
9183 rsvg_handle_get_dimensions (rsvg_handle, &dimension_data);
9184 if (! check_image_size (f, dimension_data.width, dimension_data.height))
9186 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
9187 goto rsvg_error;
9190 /* We can now get a valid pixel buffer from the svg file, if all
9191 went ok. */
9192 pixbuf = rsvg_handle_get_pixbuf (rsvg_handle);
9193 if (!pixbuf) goto rsvg_error;
9194 g_object_unref (rsvg_handle);
9196 /* Extract some meta data from the svg handle. */
9197 width = gdk_pixbuf_get_width (pixbuf);
9198 height = gdk_pixbuf_get_height (pixbuf);
9199 pixels = gdk_pixbuf_get_pixels (pixbuf);
9200 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
9202 /* Validate the svg meta data. */
9203 eassert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
9204 eassert (gdk_pixbuf_get_n_channels (pixbuf) == 4);
9205 eassert (gdk_pixbuf_get_has_alpha (pixbuf));
9206 eassert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
9208 #ifdef USE_CAIRO
9210 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
9211 int y;
9212 uint32_t bgcolor = get_spec_bg_or_alpha_as_argb (img, f);
9214 for (y = 0; y < height; ++y)
9216 const guchar *iconptr = pixels + y * rowstride;
9217 uint32_t *dataptr = (uint32_t *) (data + y * rowstride);
9218 int x;
9220 for (x = 0; x < width; ++x)
9222 if (iconptr[3] == 0)
9223 *dataptr = bgcolor;
9224 else
9225 *dataptr = (iconptr[0] << 16)
9226 | (iconptr[1] << 8)
9227 | iconptr[2]
9228 | (iconptr[3] << 24);
9230 iconptr += 4;
9231 ++dataptr;
9235 create_cairo_image_surface (img, data, width, height);
9236 g_object_unref (pixbuf);
9238 #else
9239 /* Try to create a x pixmap to hold the svg pixmap. */
9240 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
9242 g_object_unref (pixbuf);
9243 return 0;
9246 init_color_table ();
9248 /* Handle alpha channel by combining the image with a background
9249 color. */
9250 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
9251 if (!STRINGP (specified_bg)
9252 || !x_defined_color (f, SSDATA (specified_bg), &background, 0))
9253 x_query_frame_background_color (f, &background);
9255 /* SVG pixmaps specify transparency in the last byte, so right
9256 shift 8 bits to get rid of it, since emacs doesn't support
9257 transparency. */
9258 background.red >>= 8;
9259 background.green >>= 8;
9260 background.blue >>= 8;
9262 /* This loop handles opacity values, since Emacs assumes
9263 non-transparent images. Each pixel must be "flattened" by
9264 calculating the resulting color, given the transparency of the
9265 pixel, and the image background color. */
9266 for (y = 0; y < height; ++y)
9268 for (x = 0; x < width; ++x)
9270 int red;
9271 int green;
9272 int blue;
9273 int opacity;
9275 red = *pixels++;
9276 green = *pixels++;
9277 blue = *pixels++;
9278 opacity = *pixels++;
9280 red = ((red * opacity)
9281 + (background.red * ((1 << 8) - opacity)));
9282 green = ((green * opacity)
9283 + (background.green * ((1 << 8) - opacity)));
9284 blue = ((blue * opacity)
9285 + (background.blue * ((1 << 8) - opacity)));
9287 XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue));
9290 pixels += rowstride - 4 * width;
9293 #ifdef COLOR_TABLE_SUPPORT
9294 /* Remember colors allocated for this image. */
9295 img->colors = colors_in_color_table (&img->ncolors);
9296 free_color_table ();
9297 #endif /* COLOR_TABLE_SUPPORT */
9299 g_object_unref (pixbuf);
9301 img->width = width;
9302 img->height = height;
9304 /* Maybe fill in the background field while we have ximg handy.
9305 Casting avoids a GCC warning. */
9306 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
9308 /* Put ximg into the image. */
9309 image_put_x_image (f, img, ximg, 0);
9310 #endif /* ! USE_CAIRO */
9312 return 1;
9314 rsvg_error:
9315 g_object_unref (rsvg_handle);
9316 /* FIXME: Use error->message so the user knows what is the actual
9317 problem with the image. */
9318 image_error ("Error parsing SVG image `%s'", img->spec, Qnil);
9319 g_error_free (err);
9320 return 0;
9323 #endif /* defined (HAVE_RSVG) */
9328 /***********************************************************************
9329 Ghostscript
9330 ***********************************************************************/
9332 #ifdef HAVE_X_WINDOWS
9333 #define HAVE_GHOSTSCRIPT 1
9334 #endif /* HAVE_X_WINDOWS */
9336 #ifdef HAVE_GHOSTSCRIPT
9338 static bool gs_image_p (Lisp_Object object);
9339 static bool gs_load (struct frame *f, struct image *img);
9340 static void gs_clear_image (struct frame *f, struct image *img);
9342 /* Indices of image specification fields in gs_format, below. */
9344 enum gs_keyword_index
9346 GS_TYPE,
9347 GS_PT_WIDTH,
9348 GS_PT_HEIGHT,
9349 GS_FILE,
9350 GS_LOADER,
9351 GS_BOUNDING_BOX,
9352 GS_ASCENT,
9353 GS_MARGIN,
9354 GS_RELIEF,
9355 GS_ALGORITHM,
9356 GS_HEURISTIC_MASK,
9357 GS_MASK,
9358 GS_BACKGROUND,
9359 GS_LAST
9362 /* Vector of image_keyword structures describing the format
9363 of valid user-defined image specifications. */
9365 static const struct image_keyword gs_format[GS_LAST] =
9367 {":type", IMAGE_SYMBOL_VALUE, 1},
9368 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9369 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9370 {":file", IMAGE_STRING_VALUE, 1},
9371 {":loader", IMAGE_FUNCTION_VALUE, 0},
9372 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9373 {":ascent", IMAGE_ASCENT_VALUE, 0},
9374 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
9375 {":relief", IMAGE_INTEGER_VALUE, 0},
9376 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9377 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9378 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9379 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9382 /* Structure describing the image type `ghostscript'. */
9384 static struct image_type gs_type =
9386 SYMBOL_INDEX (Qpostscript),
9387 gs_image_p,
9388 gs_load,
9389 gs_clear_image,
9390 NULL,
9391 NULL
9395 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9397 static void
9398 gs_clear_image (struct frame *f, struct image *img)
9400 x_clear_image (f, img);
9404 /* Return true if OBJECT is a valid Ghostscript image
9405 specification. */
9407 static bool
9408 gs_image_p (Lisp_Object object)
9410 struct image_keyword fmt[GS_LAST];
9411 Lisp_Object tem;
9412 int i;
9414 memcpy (fmt, gs_format, sizeof fmt);
9416 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9417 return 0;
9419 /* Bounding box must be a list or vector containing 4 integers. */
9420 tem = fmt[GS_BOUNDING_BOX].value;
9421 if (CONSP (tem))
9423 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9424 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9425 return 0;
9426 if (!NILP (tem))
9427 return 0;
9429 else if (VECTORP (tem))
9431 if (ASIZE (tem) != 4)
9432 return 0;
9433 for (i = 0; i < 4; ++i)
9434 if (!INTEGERP (AREF (tem, i)))
9435 return 0;
9437 else
9438 return 0;
9440 return 1;
9444 /* Load Ghostscript image IMG for use on frame F. Value is true
9445 if successful. */
9447 static bool
9448 gs_load (struct frame *f, struct image *img)
9450 uprintmax_t printnum1, printnum2;
9451 char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)];
9452 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9453 Lisp_Object frame;
9454 double in_width, in_height;
9455 Lisp_Object pixel_colors = Qnil;
9457 /* Compute pixel size of pixmap needed from the given size in the
9458 image specification. Sizes in the specification are in pt. 1 pt
9459 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9460 info. */
9461 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9462 in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0;
9463 in_width *= FRAME_RES_X (f);
9464 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9465 in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0;
9466 in_height *= FRAME_RES_Y (f);
9468 if (! (in_width <= INT_MAX && in_height <= INT_MAX
9469 && check_image_size (f, in_width, in_height)))
9471 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
9472 return 0;
9474 img->width = in_width;
9475 img->height = in_height;
9477 /* Create the pixmap. */
9478 eassert (img->pixmap == NO_PIXMAP);
9480 if (x_check_image_size (0, img->width, img->height))
9482 /* Only W32 version did BLOCK_INPUT here. ++kfs */
9483 block_input ();
9484 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9485 img->width, img->height,
9486 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9487 unblock_input ();
9490 if (!img->pixmap)
9492 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
9493 return 0;
9496 /* Call the loader to fill the pixmap. It returns a process object
9497 if successful. We do not record_unwind_protect here because
9498 other places in redisplay like calling window scroll functions
9499 don't either. Let the Lisp loader use `unwind-protect' instead. */
9500 printnum1 = FRAME_X_WINDOW (f);
9501 printnum2 = img->pixmap;
9502 window_and_pixmap_id
9503 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9505 printnum1 = FRAME_FOREGROUND_PIXEL (f);
9506 printnum2 = FRAME_BACKGROUND_PIXEL (f);
9507 pixel_colors
9508 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9510 XSETFRAME (frame, f);
9511 loader = image_spec_value (img->spec, QCloader, NULL);
9512 if (NILP (loader))
9513 loader = intern ("gs-load-image");
9515 img->lisp_data = call6 (loader, frame, img->spec,
9516 make_number (img->width),
9517 make_number (img->height),
9518 window_and_pixmap_id,
9519 pixel_colors);
9520 return PROCESSP (img->lisp_data);
9524 /* Kill the Ghostscript process that was started to fill PIXMAP on
9525 frame F. Called from XTread_socket when receiving an event
9526 telling Emacs that Ghostscript has finished drawing. */
9528 void
9529 x_kill_gs_process (Pixmap pixmap, struct frame *f)
9531 struct image_cache *c = FRAME_IMAGE_CACHE (f);
9532 int class;
9533 ptrdiff_t i;
9534 struct image *img;
9536 /* Find the image containing PIXMAP. */
9537 for (i = 0; i < c->used; ++i)
9538 if (c->images[i]->pixmap == pixmap)
9539 break;
9541 /* Should someone in between have cleared the image cache, for
9542 instance, give up. */
9543 if (i == c->used)
9544 return;
9546 /* Kill the GS process. We should have found PIXMAP in the image
9547 cache and its image should contain a process object. */
9548 img = c->images[i];
9549 eassert (PROCESSP (img->lisp_data));
9550 Fkill_process (img->lisp_data, Qnil);
9551 img->lisp_data = Qnil;
9553 #if defined (HAVE_X_WINDOWS)
9555 /* On displays with a mutable colormap, figure out the colors
9556 allocated for the image by looking at the pixels of an XImage for
9557 img->pixmap. */
9558 class = FRAME_X_VISUAL (f)->class;
9559 if (class != StaticColor && class != StaticGray && class != TrueColor)
9561 XImagePtr ximg;
9563 block_input ();
9565 /* Try to get an XImage for img->pixmep. */
9566 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
9567 0, 0, img->width, img->height, ~0, ZPixmap);
9568 if (ximg)
9570 int x, y;
9572 /* Initialize the color table. */
9573 init_color_table ();
9575 /* For each pixel of the image, look its color up in the
9576 color table. After having done so, the color table will
9577 contain an entry for each color used by the image. */
9578 #ifdef COLOR_TABLE_SUPPORT
9579 for (y = 0; y < img->height; ++y)
9580 for (x = 0; x < img->width; ++x)
9582 unsigned long pixel = XGetPixel (ximg, x, y);
9584 lookup_pixel_color (f, pixel);
9587 /* Record colors in the image. Free color table and XImage. */
9588 img->colors = colors_in_color_table (&img->ncolors);
9589 free_color_table ();
9590 #endif
9591 XDestroyImage (ximg);
9593 #if 0 /* This doesn't seem to be the case. If we free the colors
9594 here, we get a BadAccess later in x_clear_image when
9595 freeing the colors. */
9596 /* We have allocated colors once, but Ghostscript has also
9597 allocated colors on behalf of us. So, to get the
9598 reference counts right, free them once. */
9599 if (img->ncolors)
9600 x_free_colors (f, img->colors, img->ncolors);
9601 #endif
9603 else
9604 image_error ("Cannot get X image of `%s'; colors will not be freed",
9605 img->spec, Qnil);
9607 unblock_input ();
9609 #endif /* HAVE_X_WINDOWS */
9611 /* Now that we have the pixmap, compute mask and transform the
9612 image if requested. */
9613 block_input ();
9614 postprocess_image (f, img);
9615 unblock_input ();
9618 #endif /* HAVE_GHOSTSCRIPT */
9621 /***********************************************************************
9622 Tests
9623 ***********************************************************************/
9625 #ifdef GLYPH_DEBUG
9627 DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
9628 doc: /* Value is non-nil if SPEC is a valid image specification. */)
9629 (Lisp_Object spec)
9631 return valid_image_p (spec) ? Qt : Qnil;
9635 DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0,
9636 doc: /* */)
9637 (Lisp_Object spec)
9639 ptrdiff_t id = -1;
9641 if (valid_image_p (spec))
9642 id = lookup_image (SELECTED_FRAME (), spec);
9644 debug_print (spec);
9645 return make_number (id);
9648 #endif /* GLYPH_DEBUG */
9651 /***********************************************************************
9652 Initialization
9653 ***********************************************************************/
9655 DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0,
9656 doc: /* Initialize image library implementing image type TYPE.
9657 Return non-nil if TYPE is a supported image type.
9659 If image libraries are loaded dynamically (currently only the case on
9660 MS-Windows), load the library for TYPE if it is not yet loaded, using
9661 the library file(s) specified by `dynamic-library-alist'. */)
9662 (Lisp_Object type)
9664 return lookup_image_type (type) ? Qt : Qnil;
9667 /* Look up image type TYPE, and return a pointer to its image_type
9668 structure. Return 0 if TYPE is not a known image type. */
9670 static struct image_type *
9671 lookup_image_type (Lisp_Object type)
9673 /* Types pbm and xbm are built-in and always available. */
9674 if (EQ (type, Qpbm))
9675 return define_image_type (&pbm_type);
9677 if (EQ (type, Qxbm))
9678 return define_image_type (&xbm_type);
9680 #if defined (HAVE_XPM) || defined (HAVE_NS)
9681 if (EQ (type, Qxpm))
9682 return define_image_type (&xpm_type);
9683 #endif
9685 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9686 if (EQ (type, Qjpeg))
9687 return define_image_type (&jpeg_type);
9688 #endif
9690 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9691 if (EQ (type, Qtiff))
9692 return define_image_type (&tiff_type);
9693 #endif
9695 #if defined (HAVE_GIF) || defined (HAVE_NS)
9696 if (EQ (type, Qgif))
9697 return define_image_type (&gif_type);
9698 #endif
9700 #if defined (HAVE_PNG) || defined (HAVE_NS) || defined (USE_CAIRO)
9701 if (EQ (type, Qpng))
9702 return define_image_type (&png_type);
9703 #endif
9705 #if defined (HAVE_RSVG)
9706 if (EQ (type, Qsvg))
9707 return define_image_type (&svg_type);
9708 #endif
9710 #if defined (HAVE_IMAGEMAGICK)
9711 if (EQ (type, Qimagemagick))
9712 return define_image_type (&imagemagick_type);
9713 #endif
9715 #ifdef HAVE_GHOSTSCRIPT
9716 if (EQ (type, Qpostscript))
9717 return define_image_type (&gs_type);
9718 #endif
9720 return NULL;
9723 /* Reset image_types before dumping.
9724 Called from Fdump_emacs. */
9726 void
9727 reset_image_types (void)
9729 while (image_types)
9731 struct image_type *next = image_types->next;
9732 xfree (image_types);
9733 image_types = next;
9737 void
9738 syms_of_image (void)
9740 /* Initialize this only once; it will be reset before dumping. */
9741 image_types = NULL;
9743 /* Must be defined now because we're going to update it below, while
9744 defining the supported image types. */
9745 DEFVAR_LISP ("image-types", Vimage_types,
9746 doc: /* List of potentially supported image types.
9747 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9748 To check whether it is really supported, use `image-type-available-p'. */);
9749 Vimage_types = Qnil;
9751 DEFVAR_LISP ("max-image-size", Vmax_image_size,
9752 doc: /* Maximum size of images.
9753 Emacs will not load an image into memory if its pixel width or
9754 pixel height exceeds this limit.
9756 If the value is an integer, it directly specifies the maximum
9757 image height and width, measured in pixels. If it is a floating
9758 point number, it specifies the maximum image height and width
9759 as a ratio to the frame height and width. If the value is
9760 non-numeric, there is no explicit limit on the size of images. */);
9761 Vmax_image_size = make_float (MAX_IMAGE_SIZE);
9763 /* Other symbols. */
9764 DEFSYM (Qcount, "count");
9765 DEFSYM (Qextension_data, "extension-data");
9766 DEFSYM (Qdelay, "delay");
9768 /* Keywords. */
9769 DEFSYM (QCascent, ":ascent");
9770 DEFSYM (QCmargin, ":margin");
9771 DEFSYM (QCrelief, ":relief");
9772 DEFSYM (QCconversion, ":conversion");
9773 DEFSYM (QCcolor_symbols, ":color-symbols");
9774 DEFSYM (QCheuristic_mask, ":heuristic-mask");
9775 DEFSYM (QCindex, ":index");
9776 DEFSYM (QCcrop, ":crop");
9777 DEFSYM (QCrotation, ":rotation");
9778 DEFSYM (QCmatrix, ":matrix");
9779 DEFSYM (QCcolor_adjustment, ":color-adjustment");
9780 DEFSYM (QCmask, ":mask");
9782 /* Other symbols. */
9783 DEFSYM (Qlaplace, "laplace");
9784 DEFSYM (Qemboss, "emboss");
9785 DEFSYM (Qedge_detection, "edge-detection");
9786 DEFSYM (Qheuristic, "heuristic");
9788 DEFSYM (Qpostscript, "postscript");
9789 DEFSYM (QCmax_width, ":max-width");
9790 DEFSYM (QCmax_height, ":max-height");
9791 #ifdef HAVE_GHOSTSCRIPT
9792 ADD_IMAGE_TYPE (Qpostscript);
9793 DEFSYM (QCloader, ":loader");
9794 DEFSYM (QCpt_width, ":pt-width");
9795 DEFSYM (QCpt_height, ":pt-height");
9796 #endif /* HAVE_GHOSTSCRIPT */
9798 #ifdef HAVE_NTGUI
9799 /* Versions of libpng, libgif, and libjpeg that we were compiled with,
9800 or -1 if no PNG/GIF support was compiled in. This is tested by
9801 w32-win.el to correctly set up the alist used to search for the
9802 respective image libraries. */
9803 DEFSYM (Qlibpng_version, "libpng-version");
9804 Fset (Qlibpng_version,
9805 #if HAVE_PNG
9806 make_number (PNG_LIBPNG_VER)
9807 #else
9808 make_number (-1)
9809 #endif
9811 DEFSYM (Qlibgif_version, "libgif-version");
9812 Fset (Qlibgif_version,
9813 #ifdef HAVE_GIF
9814 make_number (GIFLIB_MAJOR * 10000
9815 + GIFLIB_MINOR * 100
9816 + GIFLIB_RELEASE)
9817 #else
9818 make_number (-1)
9819 #endif
9821 DEFSYM (Qlibjpeg_version, "libjpeg-version");
9822 Fset (Qlibjpeg_version,
9823 #if HAVE_JPEG
9824 make_number (JPEG_LIB_VERSION)
9825 #else
9826 make_number (-1)
9827 #endif
9829 #endif
9831 DEFSYM (Qpbm, "pbm");
9832 ADD_IMAGE_TYPE (Qpbm);
9834 DEFSYM (Qxbm, "xbm");
9835 ADD_IMAGE_TYPE (Qxbm);
9837 #if defined (HAVE_XPM) || defined (HAVE_NS)
9838 DEFSYM (Qxpm, "xpm");
9839 ADD_IMAGE_TYPE (Qxpm);
9840 #endif
9842 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9843 DEFSYM (Qjpeg, "jpeg");
9844 ADD_IMAGE_TYPE (Qjpeg);
9845 #endif
9847 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9848 DEFSYM (Qtiff, "tiff");
9849 ADD_IMAGE_TYPE (Qtiff);
9850 #endif
9852 #if defined (HAVE_GIF) || defined (HAVE_NS)
9853 DEFSYM (Qgif, "gif");
9854 ADD_IMAGE_TYPE (Qgif);
9855 #endif
9857 #if defined (HAVE_PNG) || defined (HAVE_NS)
9858 DEFSYM (Qpng, "png");
9859 ADD_IMAGE_TYPE (Qpng);
9860 #endif
9862 #if defined (HAVE_IMAGEMAGICK)
9863 DEFSYM (Qimagemagick, "imagemagick");
9864 ADD_IMAGE_TYPE (Qimagemagick);
9865 #endif
9867 #if defined (HAVE_RSVG)
9868 DEFSYM (Qsvg, "svg");
9869 ADD_IMAGE_TYPE (Qsvg);
9870 #ifdef HAVE_NTGUI
9871 /* Other libraries used directly by svg code. */
9872 DEFSYM (Qgdk_pixbuf, "gdk-pixbuf");
9873 DEFSYM (Qglib, "glib");
9874 DEFSYM (Qgobject, "gobject");
9875 #endif /* HAVE_NTGUI */
9876 #endif /* HAVE_RSVG */
9878 defsubr (&Sinit_image_library);
9879 #ifdef HAVE_IMAGEMAGICK
9880 defsubr (&Simagemagick_types);
9881 #endif
9882 defsubr (&Sclear_image_cache);
9883 defsubr (&Simage_flush);
9884 defsubr (&Simage_size);
9885 defsubr (&Simage_mask_p);
9886 defsubr (&Simage_metadata);
9888 #ifdef GLYPH_DEBUG
9889 defsubr (&Simagep);
9890 defsubr (&Slookup_image);
9891 #endif
9893 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images,
9894 doc: /* Non-nil means always draw a cross over disabled images.
9895 Disabled images are those having a `:conversion disabled' property.
9896 A cross is always drawn on black & white displays. */);
9897 cross_disabled_images = 0;
9899 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path,
9900 doc: /* List of directories to search for window system bitmap files. */);
9901 Vx_bitmap_file_path = decode_env_path (0, PATH_BITMAPS, 0);
9903 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay,
9904 doc: /* Maximum time after which images are removed from the cache.
9905 When an image has not been displayed this many seconds, Emacs
9906 automatically removes it from the image cache. If the cache contains
9907 a large number of images, the actual eviction time may be shorter.
9908 The value can also be nil, meaning the cache is never cleared.
9910 The function `clear-image-cache' disregards this variable. */);
9911 Vimage_cache_eviction_delay = make_number (300);
9912 #ifdef HAVE_IMAGEMAGICK
9913 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type,
9914 doc: /* Integer indicating which ImageMagick rendering method to use.
9915 The options are:
9916 0 -- the default method (pixel pushing)
9917 1 -- a newer method ("MagickExportImagePixels") that may perform
9918 better (speed etc) in some cases, but has not been as thoroughly
9919 tested with Emacs as the default method. This method requires
9920 ImageMagick version 6.4.6 (approximately) or later.
9921 */);
9922 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9923 imagemagick_render_type = 0;
9924 #endif