Typo in example
[emacs.git] / src / image.c
blobc702782b64ba412aa61925f1045efe3b247385df
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 trailing arguments.
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, ...)
641 va_list ap;
642 va_start (ap, format);
643 vadd_to_log (format, ap);
644 va_end (ap);
647 static void
648 image_size_error (void)
650 image_error ("Invalid image size (see `max-image-size')");
654 /***********************************************************************
655 Image specifications
656 ***********************************************************************/
658 enum image_value_type
660 IMAGE_DONT_CHECK_VALUE_TYPE,
661 IMAGE_STRING_VALUE,
662 IMAGE_STRING_OR_NIL_VALUE,
663 IMAGE_SYMBOL_VALUE,
664 IMAGE_POSITIVE_INTEGER_VALUE,
665 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR,
666 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
667 IMAGE_ASCENT_VALUE,
668 IMAGE_INTEGER_VALUE,
669 IMAGE_FUNCTION_VALUE,
670 IMAGE_NUMBER_VALUE,
671 IMAGE_BOOL_VALUE
674 /* Structure used when parsing image specifications. */
676 struct image_keyword
678 /* Name of keyword. */
679 const char *name;
681 /* The type of value allowed. */
682 enum image_value_type type;
684 /* True means key must be present. */
685 bool mandatory_p;
687 /* Used to recognize duplicate keywords in a property list. */
688 int count;
690 /* The value that was found. */
691 Lisp_Object value;
695 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
696 has the format (image KEYWORD VALUE ...). One of the keyword/
697 value pairs must be `:type TYPE'. KEYWORDS is a vector of
698 image_keywords structures of size NKEYWORDS describing other
699 allowed keyword/value pairs. Value is true if SPEC is valid. */
701 static bool
702 parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
703 int nkeywords, Lisp_Object type)
705 int i;
706 Lisp_Object plist;
708 if (!IMAGEP (spec))
709 return 0;
711 plist = XCDR (spec);
712 while (CONSP (plist))
714 Lisp_Object key, value;
716 /* First element of a pair must be a symbol. */
717 key = XCAR (plist);
718 plist = XCDR (plist);
719 if (!SYMBOLP (key))
720 return 0;
722 /* There must follow a value. */
723 if (!CONSP (plist))
724 return 0;
725 value = XCAR (plist);
726 plist = XCDR (plist);
728 /* Find key in KEYWORDS. Error if not found. */
729 for (i = 0; i < nkeywords; ++i)
730 if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0)
731 break;
733 if (i == nkeywords)
734 continue;
736 /* Record that we recognized the keyword. If a keywords
737 was found more than once, it's an error. */
738 keywords[i].value = value;
739 if (keywords[i].count > 1)
740 return 0;
741 ++keywords[i].count;
743 /* Check type of value against allowed type. */
744 switch (keywords[i].type)
746 case IMAGE_STRING_VALUE:
747 if (!STRINGP (value))
748 return 0;
749 break;
751 case IMAGE_STRING_OR_NIL_VALUE:
752 if (!STRINGP (value) && !NILP (value))
753 return 0;
754 break;
756 case IMAGE_SYMBOL_VALUE:
757 if (!SYMBOLP (value))
758 return 0;
759 break;
761 case IMAGE_POSITIVE_INTEGER_VALUE:
762 if (! RANGED_INTEGERP (1, value, INT_MAX))
763 return 0;
764 break;
766 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR:
767 if (RANGED_INTEGERP (0, value, INT_MAX))
768 break;
769 if (CONSP (value)
770 && RANGED_INTEGERP (0, XCAR (value), INT_MAX)
771 && RANGED_INTEGERP (0, XCDR (value), INT_MAX))
772 break;
773 return 0;
775 case IMAGE_ASCENT_VALUE:
776 if (SYMBOLP (value) && EQ (value, Qcenter))
777 break;
778 else if (RANGED_INTEGERP (0, value, 100))
779 break;
780 return 0;
782 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
783 /* Unlike the other integer-related cases, this one does not
784 verify that VALUE fits in 'int'. This is because callers
785 want EMACS_INT. */
786 if (!INTEGERP (value) || XINT (value) < 0)
787 return 0;
788 break;
790 case IMAGE_DONT_CHECK_VALUE_TYPE:
791 break;
793 case IMAGE_FUNCTION_VALUE:
794 value = indirect_function (value);
795 if (!NILP (Ffunctionp (value)))
796 break;
797 return 0;
799 case IMAGE_NUMBER_VALUE:
800 if (! NUMBERP (value))
801 return 0;
802 break;
804 case IMAGE_INTEGER_VALUE:
805 if (! TYPE_RANGED_INTEGERP (int, value))
806 return 0;
807 break;
809 case IMAGE_BOOL_VALUE:
810 if (!NILP (value) && !EQ (value, Qt))
811 return 0;
812 break;
814 default:
815 emacs_abort ();
816 break;
819 if (EQ (key, QCtype) && !EQ (type, value))
820 return 0;
823 /* Check that all mandatory fields are present. */
824 for (i = 0; i < nkeywords; ++i)
825 if (keywords[i].mandatory_p && keywords[i].count == 0)
826 return 0;
828 return NILP (plist);
832 /* Return the value of KEY in image specification SPEC. Value is nil
833 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
834 was found in SPEC. */
836 static Lisp_Object
837 image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found)
839 Lisp_Object tail;
841 eassert (valid_image_p (spec));
843 for (tail = XCDR (spec);
844 CONSP (tail) && CONSP (XCDR (tail));
845 tail = XCDR (XCDR (tail)))
847 if (EQ (XCAR (tail), key))
849 if (found)
850 *found = 1;
851 return XCAR (XCDR (tail));
855 if (found)
856 *found = 0;
857 return Qnil;
861 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
862 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
863 PIXELS non-nil means return the size in pixels, otherwise return the
864 size in canonical character units.
865 FRAME is the frame on which the image will be displayed. FRAME nil
866 or omitted means use the selected frame. */)
867 (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame)
869 Lisp_Object size;
871 size = Qnil;
872 if (valid_image_p (spec))
874 struct frame *f = decode_window_system_frame (frame);
875 ptrdiff_t id = lookup_image (f, spec);
876 struct image *img = IMAGE_FROM_ID (f, id);
877 int width = img->width + 2 * img->hmargin;
878 int height = img->height + 2 * img->vmargin;
880 if (NILP (pixels))
881 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
882 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
883 else
884 size = Fcons (make_number (width), make_number (height));
886 else
887 error ("Invalid image specification");
889 return size;
893 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
894 doc: /* Return t if image SPEC has a mask bitmap.
895 FRAME is the frame on which the image will be displayed. FRAME nil
896 or omitted means use the selected frame. */)
897 (Lisp_Object spec, Lisp_Object frame)
899 Lisp_Object mask;
901 mask = Qnil;
902 if (valid_image_p (spec))
904 struct frame *f = decode_window_system_frame (frame);
905 ptrdiff_t id = lookup_image (f, spec);
906 struct image *img = IMAGE_FROM_ID (f, id);
907 if (img->mask)
908 mask = Qt;
910 else
911 error ("Invalid image specification");
913 return mask;
916 DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0,
917 doc: /* Return metadata for image SPEC.
918 FRAME is the frame on which the image will be displayed. FRAME nil
919 or omitted means use the selected frame. */)
920 (Lisp_Object spec, Lisp_Object frame)
922 Lisp_Object ext;
924 ext = Qnil;
925 if (valid_image_p (spec))
927 struct frame *f = decode_window_system_frame (frame);
928 ptrdiff_t id = lookup_image (f, spec);
929 struct image *img = IMAGE_FROM_ID (f, id);
930 ext = img->lisp_data;
933 return ext;
937 /***********************************************************************
938 Image type independent image structures
939 ***********************************************************************/
941 #define MAX_IMAGE_SIZE 10.0
942 /* Allocate and return a new image structure for image specification
943 SPEC. SPEC has a hash value of HASH. */
945 static struct image *
946 make_image (Lisp_Object spec, EMACS_UINT hash)
948 struct image *img = xzalloc (sizeof *img);
949 Lisp_Object file = image_spec_value (spec, QCfile, NULL);
951 eassert (valid_image_p (spec));
952 img->dependencies = NILP (file) ? Qnil : list1 (file);
953 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
954 eassert (img->type != NULL);
955 img->spec = spec;
956 img->lisp_data = Qnil;
957 img->ascent = DEFAULT_IMAGE_ASCENT;
958 img->hash = hash;
959 img->corners[BOT_CORNER] = -1; /* Full image */
960 return img;
964 /* Free image IMG which was used on frame F, including its resources. */
966 static void
967 free_image (struct frame *f, struct image *img)
969 if (img)
971 struct image_cache *c = FRAME_IMAGE_CACHE (f);
973 /* Remove IMG from the hash table of its cache. */
974 if (img->prev)
975 img->prev->next = img->next;
976 else
977 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
979 if (img->next)
980 img->next->prev = img->prev;
982 c->images[img->id] = NULL;
984 /* Windows NT redefines 'free', but in this file, we need to
985 avoid the redefinition. */
986 #ifdef WINDOWSNT
987 #undef free
988 #endif
989 /* Free resources, then free IMG. */
990 img->type->free (f, img);
991 xfree (img);
995 /* Return true if the given widths and heights are valid for display. */
997 static bool
998 check_image_size (struct frame *f, int width, int height)
1000 int w, h;
1002 if (width <= 0 || height <= 0)
1003 return 0;
1005 if (INTEGERP (Vmax_image_size))
1006 return (width <= XINT (Vmax_image_size)
1007 && height <= XINT (Vmax_image_size));
1008 else if (FLOATP (Vmax_image_size))
1010 if (f != NULL)
1012 w = FRAME_PIXEL_WIDTH (f);
1013 h = FRAME_PIXEL_HEIGHT (f);
1015 else
1016 w = h = 1024; /* Arbitrary size for unknown frame. */
1017 return (width <= XFLOAT_DATA (Vmax_image_size) * w
1018 && height <= XFLOAT_DATA (Vmax_image_size) * h);
1020 else
1021 return 1;
1024 /* Prepare image IMG for display on frame F. Must be called before
1025 drawing an image. */
1027 void
1028 prepare_image_for_display (struct frame *f, struct image *img)
1030 /* We're about to display IMG, so set its timestamp to `now'. */
1031 img->timestamp = current_timespec ();
1033 #ifndef USE_CAIRO
1034 /* If IMG doesn't have a pixmap yet, load it now, using the image
1035 type dependent loader function. */
1036 if (img->pixmap == NO_PIXMAP && !img->load_failed_p)
1037 img->load_failed_p = ! img->type->load (f, img);
1039 #ifdef HAVE_X_WINDOWS
1040 if (!img->load_failed_p)
1042 block_input ();
1043 image_sync_to_pixmaps (f, img);
1044 unblock_input ();
1046 #endif
1047 #endif
1051 /* Value is the number of pixels for the ascent of image IMG when
1052 drawn in face FACE. */
1055 image_ascent (struct image *img, struct face *face, struct glyph_slice *slice)
1057 int height;
1058 int ascent;
1060 if (slice->height == img->height)
1061 height = img->height + img->vmargin;
1062 else if (slice->y == 0)
1063 height = slice->height + img->vmargin;
1064 else
1065 height = slice->height;
1067 if (img->ascent == CENTERED_IMAGE_ASCENT)
1069 if (face->font)
1071 #ifdef HAVE_NTGUI
1072 /* W32 specific version. Why?. ++kfs */
1073 ascent = height / 2 - (FONT_DESCENT (face->font)
1074 - FONT_BASE (face->font)) / 2;
1075 #else
1076 /* This expression is arranged so that if the image can't be
1077 exactly centered, it will be moved slightly up. This is
1078 because a typical font is `top-heavy' (due to the presence
1079 uppercase letters), so the image placement should err towards
1080 being top-heavy too. It also just generally looks better. */
1081 ascent = (height + FONT_BASE (face->font)
1082 - FONT_DESCENT (face->font) + 1) / 2;
1083 #endif /* HAVE_NTGUI */
1085 else
1086 ascent = height / 2;
1088 else
1089 ascent = height * (img->ascent / 100.0);
1091 return ascent;
1094 #ifdef USE_CAIRO
1095 static uint32_t
1096 xcolor_to_argb32 (XColor xc)
1098 return (0xff << 24) | ((xc.red / 256) << 16)
1099 | ((xc.green / 256) << 8) | (xc.blue / 256);
1102 static uint32_t
1103 get_spec_bg_or_alpha_as_argb (struct image *img,
1104 struct frame *f)
1106 uint32_t bgcolor = 0;
1107 XColor xbgcolor;
1108 Lisp_Object bg = image_spec_value (img->spec, QCbackground, NULL);
1110 if (STRINGP (bg) && x_parse_color (f, SSDATA (bg), &xbgcolor))
1111 bgcolor = xcolor_to_argb32 (xbgcolor);
1113 return bgcolor;
1116 static void
1117 create_cairo_image_surface (struct image *img,
1118 unsigned char *data,
1119 int width,
1120 int height)
1122 cairo_surface_t *surface;
1123 cairo_format_t format = CAIRO_FORMAT_ARGB32;
1124 int stride = cairo_format_stride_for_width (format, width);
1125 surface = cairo_image_surface_create_for_data (data,
1126 format,
1127 width,
1128 height,
1129 stride);
1130 img->width = width;
1131 img->height = height;
1132 img->cr_data = surface;
1133 img->cr_data2 = data;
1134 img->pixmap = 0;
1136 #endif
1140 /* Image background colors. */
1142 /* Find the "best" corner color of a bitmap.
1143 On W32, XIMG is assumed to a device context with the bitmap selected. */
1145 static RGB_PIXEL_COLOR
1146 four_corners_best (XImagePtr_or_DC ximg, int *corners,
1147 unsigned long width, unsigned long height)
1149 RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0);
1150 int i, best_count;
1152 if (corners && corners[BOT_CORNER] >= 0)
1154 /* Get the colors at the corner_pixels of ximg. */
1155 corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]);
1156 corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]);
1157 corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1);
1158 corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1);
1160 else
1162 /* Get the colors at the corner_pixels of ximg. */
1163 corner_pixels[0] = GET_PIXEL (ximg, 0, 0);
1164 corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0);
1165 corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1);
1166 corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1);
1168 /* Choose the most frequently found color as background. */
1169 for (i = best_count = 0; i < 4; ++i)
1171 int j, n;
1173 for (j = n = 0; j < 4; ++j)
1174 if (corner_pixels[i] == corner_pixels[j])
1175 ++n;
1177 if (n > best_count)
1178 best = corner_pixels[i], best_count = n;
1181 return best;
1184 /* Portability macros */
1186 #ifdef HAVE_NTGUI
1188 #define Free_Pixmap(display, pixmap) \
1189 DeleteObject (pixmap)
1191 #elif defined (HAVE_NS)
1193 #define Free_Pixmap(display, pixmap) \
1194 ns_release_object (pixmap)
1196 #else
1198 #define Free_Pixmap(display, pixmap) \
1199 XFreePixmap (display, pixmap)
1201 #endif /* !HAVE_NTGUI && !HAVE_NS */
1204 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1205 it is guessed heuristically. If non-zero, XIMG is an existing
1206 XImage object (or device context with the image selected on W32) to
1207 use for the heuristic. */
1209 RGB_PIXEL_COLOR
1210 image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg)
1212 if (! img->background_valid)
1213 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1215 bool free_ximg = !ximg;
1216 #ifdef HAVE_NTGUI
1217 HGDIOBJ prev;
1218 #endif /* HAVE_NTGUI */
1220 if (free_ximg)
1221 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
1223 img->background = four_corners_best (ximg, img->corners, img->width, img->height);
1225 if (free_ximg)
1226 image_unget_x_image_or_dc (img, 0, ximg, prev);
1228 img->background_valid = 1;
1231 return img->background;
1234 /* Return the `background_transparent' field of IMG. If IMG doesn't
1235 have one yet, it is guessed heuristically. If non-zero, MASK is an
1236 existing XImage object to use for the heuristic. */
1239 image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask)
1241 if (! img->background_transparent_valid)
1242 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1244 if (img->mask)
1246 bool free_mask = !mask;
1247 #ifdef HAVE_NTGUI
1248 HGDIOBJ prev;
1249 #endif /* HAVE_NTGUI */
1251 if (free_mask)
1252 mask = image_get_x_image_or_dc (f, img, 1, &prev);
1254 img->background_transparent
1255 = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN);
1257 if (free_mask)
1258 image_unget_x_image_or_dc (img, 1, mask, prev);
1260 else
1261 img->background_transparent = 0;
1263 img->background_transparent_valid = 1;
1266 return img->background_transparent;
1269 #if defined (HAVE_PNG) || defined (HAVE_NS) \
1270 || defined (HAVE_IMAGEMAGICK) || defined (HAVE_RSVG)
1272 /* Store F's background color into *BGCOLOR. */
1273 static void
1274 x_query_frame_background_color (struct frame *f, XColor *bgcolor)
1276 #ifndef HAVE_NS
1277 bgcolor->pixel = FRAME_BACKGROUND_PIXEL (f);
1278 x_query_color (f, bgcolor);
1279 #else
1280 ns_query_color (FRAME_BACKGROUND_COLOR (f), bgcolor, 1);
1281 #endif
1284 #endif /* HAVE_PNG || HAVE_NS || HAVE_IMAGEMAGICK || HAVE_RSVG */
1286 /***********************************************************************
1287 Helper functions for X image types
1288 ***********************************************************************/
1290 /* Clear X resources of image IMG on frame F according to FLAGS.
1291 FLAGS is bitwise-or of the following masks:
1292 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1293 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1294 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1295 any. */
1297 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1298 #define CLEAR_IMAGE_MASK (1 << 1)
1299 #define CLEAR_IMAGE_COLORS (1 << 2)
1301 static void
1302 x_clear_image_1 (struct frame *f, struct image *img, int flags)
1304 if (flags & CLEAR_IMAGE_PIXMAP)
1306 if (img->pixmap)
1308 Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
1309 img->pixmap = NO_PIXMAP;
1310 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1311 img->background_valid = 0;
1313 #ifdef HAVE_X_WINDOWS
1314 if (img->ximg)
1316 x_destroy_x_image (img->ximg);
1317 img->ximg = NULL;
1318 img->background_valid = 0;
1320 #endif
1323 if (flags & CLEAR_IMAGE_MASK)
1325 if (img->mask)
1327 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
1328 img->mask = NO_PIXMAP;
1329 img->background_transparent_valid = 0;
1331 #ifdef HAVE_X_WINDOWS
1332 if (img->mask_img)
1334 x_destroy_x_image (img->mask_img);
1335 img->mask_img = NULL;
1336 img->background_transparent_valid = 0;
1338 #endif
1341 if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors)
1343 /* W32_TODO: color table support. */
1344 #ifdef HAVE_X_WINDOWS
1345 x_free_colors (f, img->colors, img->ncolors);
1346 #endif /* HAVE_X_WINDOWS */
1347 xfree (img->colors);
1348 img->colors = NULL;
1349 img->ncolors = 0;
1354 /* Free X resources of image IMG which is used on frame F. */
1356 static void
1357 x_clear_image (struct frame *f, struct image *img)
1359 block_input ();
1360 #ifdef USE_CAIRO
1361 if (img->cr_data)
1362 cairo_surface_destroy ((cairo_surface_t *)img->cr_data);
1363 if (img->cr_data2) xfree (img->cr_data2);
1364 #endif
1365 x_clear_image_1 (f, img,
1366 CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS);
1367 unblock_input ();
1371 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1372 cannot be allocated, use DFLT. Add a newly allocated color to
1373 IMG->colors, so that it can be freed again. Value is the pixel
1374 color. */
1376 static unsigned long
1377 x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name,
1378 unsigned long dflt)
1380 XColor color;
1381 unsigned long result;
1383 eassert (STRINGP (color_name));
1385 if (x_defined_color (f, SSDATA (color_name), &color, 1)
1386 && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors,
1387 INT_MAX))
1389 /* This isn't called frequently so we get away with simply
1390 reallocating the color vector to the needed size, here. */
1391 ptrdiff_t ncolors = img->ncolors + 1;
1392 img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors);
1393 img->colors[ncolors - 1] = color.pixel;
1394 img->ncolors = ncolors;
1395 result = color.pixel;
1397 else
1398 result = dflt;
1400 return result;
1405 /***********************************************************************
1406 Image Cache
1407 ***********************************************************************/
1409 static void cache_image (struct frame *f, struct image *img);
1411 /* Return a new, initialized image cache that is allocated from the
1412 heap. Call free_image_cache to free an image cache. */
1414 struct image_cache *
1415 make_image_cache (void)
1417 struct image_cache *c = xmalloc (sizeof *c);
1419 c->size = 50;
1420 c->used = c->refcount = 0;
1421 c->images = xmalloc (c->size * sizeof *c->images);
1422 c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
1423 return c;
1427 /* Find an image matching SPEC in the cache, and return it. If no
1428 image is found, return NULL. */
1429 static struct image *
1430 search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash)
1432 struct image *img;
1433 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1434 int i = hash % IMAGE_CACHE_BUCKETS_SIZE;
1436 if (!c) return NULL;
1438 /* If the image spec does not specify a background color, the cached
1439 image must have the same background color as the current frame.
1440 The foreground color must also match, for the sake of monochrome
1441 images.
1443 In fact, we could ignore the foreground color matching condition
1444 for color images, or if the image spec specifies :foreground;
1445 similarly we could ignore the background color matching condition
1446 for formats that don't use transparency (such as jpeg), or if the
1447 image spec specifies :background. However, the extra memory
1448 usage is probably negligible in practice, so we don't bother. */
1450 for (img = c->buckets[i]; img; img = img->next)
1451 if (img->hash == hash
1452 && !NILP (Fequal (img->spec, spec))
1453 && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f)
1454 && img->frame_background == FRAME_BACKGROUND_PIXEL (f))
1455 break;
1456 return img;
1460 /* Search frame F for an image with spec SPEC, and free it. */
1462 static void
1463 uncache_image (struct frame *f, Lisp_Object spec)
1465 struct image *img = search_image_cache (f, spec, sxhash (spec, 0));
1466 if (img)
1468 free_image (f, img);
1469 /* As display glyphs may still be referring to the image ID, we
1470 must garbage the frame (Bug#6426). */
1471 SET_FRAME_GARBAGED (f);
1476 /* Free image cache of frame F. Be aware that X frames share images
1477 caches. */
1479 void
1480 free_image_cache (struct frame *f)
1482 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1483 if (c)
1485 ptrdiff_t i;
1487 /* Cache should not be referenced by any frame when freed. */
1488 eassert (c->refcount == 0);
1490 for (i = 0; i < c->used; ++i)
1491 free_image (f, c->images[i]);
1492 xfree (c->images);
1493 xfree (c->buckets);
1494 xfree (c);
1495 FRAME_IMAGE_CACHE (f) = NULL;
1500 /* Clear image cache of frame F. FILTER=t means free all images.
1501 FILTER=nil means clear only images that haven't been
1502 displayed for some time.
1503 Else, only free the images which have FILTER in their `dependencies'.
1504 Should be called from time to time to reduce the number of loaded images.
1505 If image-cache-eviction-delay is non-nil, this frees images in the cache
1506 which weren't displayed for at least that many seconds. */
1508 static void
1509 clear_image_cache (struct frame *f, Lisp_Object filter)
1511 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1513 if (c)
1515 ptrdiff_t i, nfreed = 0;
1517 /* Block input so that we won't be interrupted by a SIGIO
1518 while being in an inconsistent state. */
1519 block_input ();
1521 if (!NILP (filter))
1523 /* Filter image cache. */
1524 for (i = 0; i < c->used; ++i)
1526 struct image *img = c->images[i];
1527 if (img && (EQ (Qt, filter)
1528 || !NILP (Fmember (filter, img->dependencies))))
1530 free_image (f, img);
1531 ++nfreed;
1535 else if (INTEGERP (Vimage_cache_eviction_delay))
1537 /* Free cache based on timestamp. */
1538 struct timespec old, t;
1539 double delay;
1540 ptrdiff_t nimages = 0;
1542 for (i = 0; i < c->used; ++i)
1543 if (c->images[i])
1544 nimages++;
1546 /* If the number of cached images has grown unusually large,
1547 decrease the cache eviction delay (Bug#6230). */
1548 delay = XINT (Vimage_cache_eviction_delay);
1549 if (nimages > 40)
1550 delay = 1600 * delay / nimages / nimages;
1551 delay = max (delay, 1);
1553 t = current_timespec ();
1554 old = timespec_sub (t, dtotimespec (delay));
1556 for (i = 0; i < c->used; ++i)
1558 struct image *img = c->images[i];
1559 if (img && timespec_cmp (img->timestamp, old) < 0)
1561 free_image (f, img);
1562 ++nfreed;
1567 /* We may be clearing the image cache because, for example,
1568 Emacs was iconified for a longer period of time. In that
1569 case, current matrices may still contain references to
1570 images freed above. So, clear these matrices. */
1571 if (nfreed)
1573 Lisp_Object tail, frame;
1575 FOR_EACH_FRAME (tail, frame)
1577 struct frame *fr = XFRAME (frame);
1578 if (FRAME_IMAGE_CACHE (fr) == c)
1579 clear_current_matrices (fr);
1582 windows_or_buffers_changed = 19;
1585 unblock_input ();
1589 void
1590 clear_image_caches (Lisp_Object filter)
1592 /* FIXME: We want to do
1593 * struct terminal *t;
1594 * for (t = terminal_list; t; t = t->next_terminal)
1595 * clear_image_cache (t, filter); */
1596 Lisp_Object tail, frame;
1597 FOR_EACH_FRAME (tail, frame)
1598 if (FRAME_WINDOW_P (XFRAME (frame)))
1599 clear_image_cache (XFRAME (frame), filter);
1602 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
1603 0, 1, 0,
1604 doc: /* Clear the image cache.
1605 FILTER nil or a frame means clear all images in the selected frame.
1606 FILTER t means clear the image caches of all frames.
1607 Anything else, means only clear those images which refer to FILTER,
1608 which is then usually a filename. */)
1609 (Lisp_Object filter)
1611 if (!(EQ (filter, Qnil) || FRAMEP (filter)))
1612 clear_image_caches (filter);
1613 else
1614 clear_image_cache (decode_window_system_frame (filter), Qt);
1616 return Qnil;
1620 DEFUN ("image-flush", Fimage_flush, Simage_flush,
1621 1, 2, 0,
1622 doc: /* Flush the image with specification SPEC on frame FRAME.
1623 This removes the image from the Emacs image cache. If SPEC specifies
1624 an image file, the next redisplay of this image will read from the
1625 current contents of that file.
1627 FRAME nil or omitted means use the selected frame.
1628 FRAME t means refresh the image on all frames. */)
1629 (Lisp_Object spec, Lisp_Object frame)
1631 if (!valid_image_p (spec))
1632 error ("Invalid image specification");
1634 if (EQ (frame, Qt))
1636 Lisp_Object tail;
1637 FOR_EACH_FRAME (tail, frame)
1639 struct frame *f = XFRAME (frame);
1640 if (FRAME_WINDOW_P (f))
1641 uncache_image (f, spec);
1644 else
1645 uncache_image (decode_window_system_frame (frame), spec);
1647 return Qnil;
1651 /* Compute masks and transform image IMG on frame F, as specified
1652 by the image's specification, */
1654 static void
1655 postprocess_image (struct frame *f, struct image *img)
1657 /* Manipulation of the image's mask. */
1658 if (img->pixmap)
1660 Lisp_Object conversion, spec;
1661 Lisp_Object mask;
1663 spec = img->spec;
1665 /* `:heuristic-mask t'
1666 `:mask heuristic'
1667 means build a mask heuristically.
1668 `:heuristic-mask (R G B)'
1669 `:mask (heuristic (R G B))'
1670 means build a mask from color (R G B) in the
1671 image.
1672 `:mask nil'
1673 means remove a mask, if any. */
1675 mask = image_spec_value (spec, QCheuristic_mask, NULL);
1676 if (!NILP (mask))
1677 x_build_heuristic_mask (f, img, mask);
1678 else
1680 bool found_p;
1682 mask = image_spec_value (spec, QCmask, &found_p);
1684 if (EQ (mask, Qheuristic))
1685 x_build_heuristic_mask (f, img, Qt);
1686 else if (CONSP (mask)
1687 && EQ (XCAR (mask), Qheuristic))
1689 if (CONSP (XCDR (mask)))
1690 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
1691 else
1692 x_build_heuristic_mask (f, img, XCDR (mask));
1694 else if (NILP (mask) && found_p && img->mask)
1695 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
1699 /* Should we apply an image transformation algorithm? */
1700 conversion = image_spec_value (spec, QCconversion, NULL);
1701 if (EQ (conversion, Qdisabled))
1702 x_disable_image (f, img);
1703 else if (EQ (conversion, Qlaplace))
1704 x_laplace (f, img);
1705 else if (EQ (conversion, Qemboss))
1706 x_emboss (f, img);
1707 else if (CONSP (conversion)
1708 && EQ (XCAR (conversion), Qedge_detection))
1710 Lisp_Object tem;
1711 tem = XCDR (conversion);
1712 if (CONSP (tem))
1713 x_edge_detection (f, img,
1714 Fplist_get (tem, QCmatrix),
1715 Fplist_get (tem, QCcolor_adjustment));
1721 /* Return the id of image with Lisp specification SPEC on frame F.
1722 SPEC must be a valid Lisp image specification (see valid_image_p). */
1724 ptrdiff_t
1725 lookup_image (struct frame *f, Lisp_Object spec)
1727 struct image *img;
1728 EMACS_UINT hash;
1730 /* F must be a window-system frame, and SPEC must be a valid image
1731 specification. */
1732 eassert (FRAME_WINDOW_P (f));
1733 eassert (valid_image_p (spec));
1735 /* Look up SPEC in the hash table of the image cache. */
1736 hash = sxhash (spec, 0);
1737 img = search_image_cache (f, spec, hash);
1738 if (img && img->load_failed_p)
1740 free_image (f, img);
1741 img = NULL;
1744 /* If not found, create a new image and cache it. */
1745 if (img == NULL)
1747 block_input ();
1748 img = make_image (spec, hash);
1749 cache_image (f, img);
1750 img->load_failed_p = ! img->type->load (f, img);
1751 img->frame_foreground = FRAME_FOREGROUND_PIXEL (f);
1752 img->frame_background = FRAME_BACKGROUND_PIXEL (f);
1754 /* If we can't load the image, and we don't have a width and
1755 height, use some arbitrary width and height so that we can
1756 draw a rectangle for it. */
1757 if (img->load_failed_p)
1759 Lisp_Object value;
1761 value = image_spec_value (spec, QCwidth, NULL);
1762 img->width = (INTEGERP (value)
1763 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
1764 value = image_spec_value (spec, QCheight, NULL);
1765 img->height = (INTEGERP (value)
1766 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
1768 else
1770 /* Handle image type independent image attributes
1771 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1772 `:background COLOR'. */
1773 Lisp_Object ascent, margin, relief, bg;
1774 int relief_bound;
1776 ascent = image_spec_value (spec, QCascent, NULL);
1777 if (INTEGERP (ascent))
1778 img->ascent = XFASTINT (ascent);
1779 else if (EQ (ascent, Qcenter))
1780 img->ascent = CENTERED_IMAGE_ASCENT;
1782 margin = image_spec_value (spec, QCmargin, NULL);
1783 if (INTEGERP (margin))
1784 img->vmargin = img->hmargin = XFASTINT (margin);
1785 else if (CONSP (margin))
1787 img->hmargin = XFASTINT (XCAR (margin));
1788 img->vmargin = XFASTINT (XCDR (margin));
1791 relief = image_spec_value (spec, QCrelief, NULL);
1792 relief_bound = INT_MAX - max (img->hmargin, img->vmargin);
1793 if (RANGED_INTEGERP (- relief_bound, relief, relief_bound))
1795 img->relief = XINT (relief);
1796 img->hmargin += eabs (img->relief);
1797 img->vmargin += eabs (img->relief);
1800 if (! img->background_valid)
1802 bg = image_spec_value (img->spec, QCbackground, NULL);
1803 if (!NILP (bg))
1805 img->background
1806 = x_alloc_image_color (f, img, bg,
1807 FRAME_BACKGROUND_PIXEL (f));
1808 img->background_valid = 1;
1812 /* Do image transformations and compute masks, unless we
1813 don't have the image yet. */
1814 if (!EQ (builtin_lisp_symbol (img->type->type), Qpostscript))
1815 postprocess_image (f, img);
1818 unblock_input ();
1821 /* We're using IMG, so set its timestamp to `now'. */
1822 img->timestamp = current_timespec ();
1824 /* Value is the image id. */
1825 return img->id;
1829 /* Cache image IMG in the image cache of frame F. */
1831 static void
1832 cache_image (struct frame *f, struct image *img)
1834 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1835 ptrdiff_t i;
1837 /* Find a free slot in c->images. */
1838 for (i = 0; i < c->used; ++i)
1839 if (c->images[i] == NULL)
1840 break;
1842 /* If no free slot found, maybe enlarge c->images. */
1843 if (i == c->used && c->used == c->size)
1844 c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images);
1846 /* Add IMG to c->images, and assign IMG an id. */
1847 c->images[i] = img;
1848 img->id = i;
1849 if (i == c->used)
1850 ++c->used;
1852 /* Add IMG to the cache's hash table. */
1853 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
1854 img->next = c->buckets[i];
1855 if (img->next)
1856 img->next->prev = img;
1857 img->prev = NULL;
1858 c->buckets[i] = img;
1862 /* Call FN on every image in the image cache of frame F. Used to mark
1863 Lisp Objects in the image cache. */
1865 /* Mark Lisp objects in image IMG. */
1867 static void
1868 mark_image (struct image *img)
1870 mark_object (img->spec);
1871 mark_object (img->dependencies);
1873 if (!NILP (img->lisp_data))
1874 mark_object (img->lisp_data);
1878 void
1879 mark_image_cache (struct image_cache *c)
1881 if (c)
1883 ptrdiff_t i;
1884 for (i = 0; i < c->used; ++i)
1885 if (c->images[i])
1886 mark_image (c->images[i]);
1892 /***********************************************************************
1893 X / NS / W32 support code
1894 ***********************************************************************/
1896 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1897 windowing system.
1898 WIDTH and HEIGHT must both be positive.
1899 If XIMG is null, assume it is a bitmap. */
1900 static bool
1901 x_check_image_size (XImagePtr ximg, int width, int height)
1903 #ifdef HAVE_X_WINDOWS
1904 /* Respect Xlib's limits: it cannot deal with images that have more
1905 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1906 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1907 enum
1909 XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX),
1910 X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX))
1913 int bitmap_pad, depth, bytes_per_line;
1914 if (ximg)
1916 bitmap_pad = ximg->bitmap_pad;
1917 depth = ximg->depth;
1918 bytes_per_line = ximg->bytes_per_line;
1920 else
1922 bitmap_pad = 8;
1923 depth = 1;
1924 bytes_per_line = (width >> 3) + ((width & 7) != 0);
1926 return (width <= (INT_MAX - (bitmap_pad - 1)) / depth
1927 && height <= X_IMAGE_BYTES_MAX / bytes_per_line);
1928 #else
1929 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1930 For now, assume that every image size is allowed on these systems. */
1931 return 1;
1932 #endif
1935 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1936 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1937 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1938 via xmalloc. Print error messages via image_error if an error
1939 occurs. Value is true if successful.
1941 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1942 should indicate the bit depth of the image. */
1944 static bool
1945 x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth,
1946 XImagePtr *ximg, Pixmap *pixmap)
1948 #ifdef HAVE_X_WINDOWS
1949 Display *display = FRAME_X_DISPLAY (f);
1950 Window window = FRAME_X_WINDOW (f);
1951 Screen *screen = FRAME_X_SCREEN (f);
1953 eassert (input_blocked_p ());
1955 if (depth <= 0)
1956 depth = DefaultDepthOfScreen (screen);
1957 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
1958 depth, ZPixmap, 0, NULL, width, height,
1959 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
1960 if (*ximg == NULL)
1962 image_error ("Unable to allocate X image");
1963 return 0;
1966 if (! x_check_image_size (*ximg, width, height))
1968 x_destroy_x_image (*ximg);
1969 *ximg = NULL;
1970 image_error ("Image too large (%dx%d)",
1971 make_number (width), make_number (height));
1972 return 0;
1975 /* Allocate image raster. */
1976 (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height);
1978 /* Allocate a pixmap of the same size. */
1979 *pixmap = XCreatePixmap (display, window, width, height, depth);
1980 if (*pixmap == NO_PIXMAP)
1982 x_destroy_x_image (*ximg);
1983 *ximg = NULL;
1984 image_error ("Unable to create X pixmap");
1985 return 0;
1988 return 1;
1989 #endif /* HAVE_X_WINDOWS */
1991 #ifdef HAVE_NTGUI
1993 BITMAPINFOHEADER *header;
1994 HDC hdc;
1995 int scanline_width_bits;
1996 int remainder;
1997 int palette_colors = 0;
1999 if (depth == 0)
2000 depth = 24;
2002 if (depth != 1 && depth != 4 && depth != 8
2003 && depth != 16 && depth != 24 && depth != 32)
2005 image_error ("Invalid image bit depth specified");
2006 return 0;
2009 scanline_width_bits = width * depth;
2010 remainder = scanline_width_bits % 32;
2012 if (remainder)
2013 scanline_width_bits += 32 - remainder;
2015 /* Bitmaps with a depth less than 16 need a palette. */
2016 /* BITMAPINFO structure already contains the first RGBQUAD. */
2017 if (depth < 16)
2018 palette_colors = 1 << (depth - 1);
2020 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
2022 header = &(*ximg)->info.bmiHeader;
2023 memset (&(*ximg)->info, 0, sizeof (BITMAPINFO));
2024 header->biSize = sizeof (*header);
2025 header->biWidth = width;
2026 header->biHeight = -height; /* negative indicates a top-down bitmap. */
2027 header->biPlanes = 1;
2028 header->biBitCount = depth;
2029 header->biCompression = BI_RGB;
2030 header->biClrUsed = palette_colors;
2032 /* TODO: fill in palette. */
2033 if (depth == 1)
2035 (*ximg)->info.bmiColors[0].rgbBlue = 0;
2036 (*ximg)->info.bmiColors[0].rgbGreen = 0;
2037 (*ximg)->info.bmiColors[0].rgbRed = 0;
2038 (*ximg)->info.bmiColors[0].rgbReserved = 0;
2039 (*ximg)->info.bmiColors[1].rgbBlue = 255;
2040 (*ximg)->info.bmiColors[1].rgbGreen = 255;
2041 (*ximg)->info.bmiColors[1].rgbRed = 255;
2042 (*ximg)->info.bmiColors[1].rgbReserved = 0;
2045 hdc = get_frame_dc (f);
2047 /* Create a DIBSection and raster array for the bitmap,
2048 and store its handle in *pixmap. */
2049 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
2050 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
2051 /* casting avoids a GCC warning */
2052 (void **)&((*ximg)->data), NULL, 0);
2054 /* Realize display palette and garbage all frames. */
2055 release_frame_dc (f, hdc);
2057 if (*pixmap == NULL)
2059 DWORD err = GetLastError ();
2060 Lisp_Object errcode;
2061 /* All system errors are < 10000, so the following is safe. */
2062 XSETINT (errcode, err);
2063 image_error ("Unable to create bitmap, error code %d", errcode);
2064 x_destroy_x_image (*ximg);
2065 *ximg = NULL;
2066 return 0;
2069 return 1;
2071 #endif /* HAVE_NTGUI */
2073 #ifdef HAVE_NS
2074 *pixmap = ns_image_for_XPM (width, height, depth);
2075 if (*pixmap == 0)
2077 *ximg = NULL;
2078 image_error ("Unable to allocate NSImage for XPM pixmap");
2079 return 0;
2081 *ximg = *pixmap;
2082 return 1;
2083 #endif
2087 /* Destroy XImage XIMG. Free XIMG->data. */
2089 static void
2090 x_destroy_x_image (XImagePtr ximg)
2092 eassert (input_blocked_p ());
2093 if (ximg)
2095 #ifdef HAVE_X_WINDOWS
2096 xfree (ximg->data);
2097 ximg->data = NULL;
2098 XDestroyImage (ximg);
2099 #endif /* HAVE_X_WINDOWS */
2100 #ifdef HAVE_NTGUI
2101 /* Data will be freed by DestroyObject. */
2102 ximg->data = NULL;
2103 xfree (ximg);
2104 #endif /* HAVE_NTGUI */
2105 #ifdef HAVE_NS
2106 ns_release_object (ximg);
2107 #endif /* HAVE_NS */
2112 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2113 are width and height of both the image and pixmap. */
2115 static void
2116 x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height)
2118 #ifdef HAVE_X_WINDOWS
2119 GC gc;
2121 eassert (input_blocked_p ());
2122 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
2123 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
2124 XFreeGC (FRAME_X_DISPLAY (f), gc);
2125 #endif /* HAVE_X_WINDOWS */
2127 #ifdef HAVE_NTGUI
2128 #if 0 /* I don't think this is necessary looking at where it is used. */
2129 HDC hdc = get_frame_dc (f);
2130 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
2131 release_frame_dc (f, hdc);
2132 #endif
2133 #endif /* HAVE_NTGUI */
2135 #ifdef HAVE_NS
2136 eassert (ximg == pixmap);
2137 ns_retain_object (ximg);
2138 #endif
2141 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2142 with image_put_x_image. */
2144 static bool
2145 image_create_x_image_and_pixmap (struct frame *f, struct image *img,
2146 int width, int height, int depth,
2147 XImagePtr *ximg, bool mask_p)
2149 eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP);
2151 return x_create_x_image_and_pixmap (f, width, height, depth, ximg,
2152 !mask_p ? &img->pixmap : &img->mask);
2155 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2156 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2157 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2158 On the other platforms, it puts XIMG into the pixmap, then frees
2159 the X image and its buffer. */
2161 static void
2162 image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg,
2163 bool mask_p)
2165 #ifdef HAVE_X_WINDOWS
2166 if (!mask_p)
2168 eassert (img->ximg == NULL);
2169 img->ximg = ximg;
2171 else
2173 eassert (img->mask_img == NULL);
2174 img->mask_img = ximg;
2176 #else
2177 x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask,
2178 img->width, img->height);
2179 x_destroy_x_image (ximg);
2180 #endif
2183 #ifdef HAVE_X_WINDOWS
2184 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2185 the X images and their buffers. */
2187 static void
2188 image_sync_to_pixmaps (struct frame *f, struct image *img)
2190 if (img->ximg)
2192 x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height);
2193 x_destroy_x_image (img->ximg);
2194 img->ximg = NULL;
2196 if (img->mask_img)
2198 x_put_x_image (f, img->mask_img, img->mask, img->width, img->height);
2199 x_destroy_x_image (img->mask_img);
2200 img->mask_img = NULL;
2203 #endif
2205 #ifdef HAVE_NTGUI
2206 /* Create a memory device context for IMG on frame F. It stores the
2207 currently selected GDI object into *PREV for future restoration by
2208 image_unget_x_image_or_dc. */
2210 static XImagePtr_or_DC
2211 image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p,
2212 HGDIOBJ *prev)
2214 HDC frame_dc = get_frame_dc (f);
2215 XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc);
2217 release_frame_dc (f, frame_dc);
2218 *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask);
2220 return ximg;
2223 static void
2224 image_unget_x_image_or_dc (struct image *img, bool mask_p,
2225 XImagePtr_or_DC ximg, HGDIOBJ prev)
2227 SelectObject (ximg, prev);
2228 DeleteDC (ximg);
2230 #else /* !HAVE_NTGUI */
2231 /* Get the X image for IMG on frame F. The resulting X image data
2232 should be treated as read-only at least on X. */
2234 static XImagePtr
2235 image_get_x_image (struct frame *f, struct image *img, bool mask_p)
2237 #ifdef HAVE_X_WINDOWS
2238 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2240 if (ximg_in_img)
2241 return ximg_in_img;
2242 else
2243 return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask,
2244 0, 0, img->width, img->height, ~0, ZPixmap);
2245 #elif defined (HAVE_NS)
2246 XImagePtr pixmap = !mask_p ? img->pixmap : img->mask;
2248 ns_retain_object (pixmap);
2249 return pixmap;
2250 #endif
2253 static void
2254 image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg)
2256 #ifdef HAVE_X_WINDOWS
2257 XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img;
2259 if (ximg_in_img)
2260 eassert (ximg == ximg_in_img);
2261 else
2262 XDestroyImage (ximg);
2263 #elif defined (HAVE_NS)
2264 ns_release_object (ximg);
2265 #endif
2267 #endif /* !HAVE_NTGUI */
2270 /***********************************************************************
2271 File Handling
2272 ***********************************************************************/
2274 /* Find image file FILE. Look in data-directory/images, then
2275 x-bitmap-file-path. Value is the full name of the file
2276 found, or nil if not found. If PFD is nonnull store into *PFD a
2277 readable file descriptor for the file, opened in binary mode. If
2278 PFD is null, do not open the file. */
2280 static Lisp_Object
2281 x_find_image_fd (Lisp_Object file, int *pfd)
2283 Lisp_Object file_found, search_path;
2284 int fd;
2286 /* TODO I think this should use something like image-load-path
2287 instead. Unfortunately, that can contain non-string elements. */
2288 search_path = Fcons (Fexpand_file_name (build_string ("images"),
2289 Vdata_directory),
2290 Vx_bitmap_file_path);
2292 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2293 fd = openp (search_path, file, Qnil, &file_found,
2294 pfd ? Qt : make_number (R_OK), false);
2295 if (fd < 0)
2296 return Qnil;
2297 if (pfd)
2298 *pfd = fd;
2299 return file_found;
2302 /* Find image file FILE. Look in data-directory/images, then
2303 x-bitmap-file-path. Value is the encoded full name of the file
2304 found, or nil if not found. */
2306 Lisp_Object
2307 x_find_image_file (Lisp_Object file)
2309 return x_find_image_fd (file, 0);
2312 /* Read FILE into memory. Value is a pointer to a buffer allocated
2313 with xmalloc holding FILE's contents. Value is null if an error
2314 occurred. FD is a file descriptor open for reading FILE. Set
2315 *SIZE to the size of the file. */
2317 static unsigned char *
2318 slurp_file (int fd, ptrdiff_t *size)
2320 FILE *fp = fdopen (fd, "rb");
2322 unsigned char *buf = NULL;
2323 struct stat st;
2325 if (fp)
2327 ptrdiff_t count = SPECPDL_INDEX ();
2328 record_unwind_protect_ptr (fclose_unwind, fp);
2330 if (fstat (fileno (fp), &st) == 0
2331 && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX))
2333 /* Report an error if we read past the purported EOF.
2334 This can happen if the file grows as we read it. */
2335 ptrdiff_t buflen = st.st_size;
2336 buf = xmalloc (buflen + 1);
2337 if (fread (buf, 1, buflen + 1, fp) == buflen)
2338 *size = buflen;
2339 else
2341 xfree (buf);
2342 buf = NULL;
2346 unbind_to (count, Qnil);
2349 return buf;
2354 /***********************************************************************
2355 XBM images
2356 ***********************************************************************/
2358 static bool xbm_load (struct frame *f, struct image *img);
2359 static bool xbm_image_p (Lisp_Object object);
2360 static bool xbm_file_p (Lisp_Object);
2363 /* Indices of image specification fields in xbm_format, below. */
2365 enum xbm_keyword_index
2367 XBM_TYPE,
2368 XBM_FILE,
2369 XBM_WIDTH,
2370 XBM_HEIGHT,
2371 XBM_DATA,
2372 XBM_FOREGROUND,
2373 XBM_BACKGROUND,
2374 XBM_ASCENT,
2375 XBM_MARGIN,
2376 XBM_RELIEF,
2377 XBM_ALGORITHM,
2378 XBM_HEURISTIC_MASK,
2379 XBM_MASK,
2380 XBM_LAST
2383 /* Vector of image_keyword structures describing the format
2384 of valid XBM image specifications. */
2386 static const struct image_keyword xbm_format[XBM_LAST] =
2388 {":type", IMAGE_SYMBOL_VALUE, 1},
2389 {":file", IMAGE_STRING_VALUE, 0},
2390 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2391 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2392 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2393 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
2394 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
2395 {":ascent", IMAGE_ASCENT_VALUE, 0},
2396 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
2397 {":relief", IMAGE_INTEGER_VALUE, 0},
2398 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2399 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2400 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
2403 /* Structure describing the image type XBM. */
2405 static struct image_type xbm_type =
2407 SYMBOL_INDEX (Qxbm),
2408 xbm_image_p,
2409 xbm_load,
2410 x_clear_image,
2411 NULL,
2412 NULL
2415 /* Tokens returned from xbm_scan. */
2417 enum xbm_token
2419 XBM_TK_IDENT = 256,
2420 XBM_TK_NUMBER
2424 /* Return true if OBJECT is a valid XBM-type image specification.
2425 A valid specification is a list starting with the symbol `image'
2426 The rest of the list is a property list which must contain an
2427 entry `:type xbm'.
2429 If the specification specifies a file to load, it must contain
2430 an entry `:file FILENAME' where FILENAME is a string.
2432 If the specification is for a bitmap loaded from memory it must
2433 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2434 WIDTH and HEIGHT are integers > 0. DATA may be:
2436 1. a string large enough to hold the bitmap data, i.e. it must
2437 have a size >= (WIDTH + 7) / 8 * HEIGHT
2439 2. a bool-vector of size >= WIDTH * HEIGHT
2441 3. a vector of strings or bool-vectors, one for each line of the
2442 bitmap.
2444 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2445 may not be specified in this case because they are defined in the
2446 XBM file.
2448 Both the file and data forms may contain the additional entries
2449 `:background COLOR' and `:foreground COLOR'. If not present,
2450 foreground and background of the frame on which the image is
2451 displayed is used. */
2453 static bool
2454 xbm_image_p (Lisp_Object object)
2456 struct image_keyword kw[XBM_LAST];
2458 memcpy (kw, xbm_format, sizeof kw);
2459 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
2460 return 0;
2462 eassert (EQ (kw[XBM_TYPE].value, Qxbm));
2464 if (kw[XBM_FILE].count)
2466 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
2467 return 0;
2469 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
2471 /* In-memory XBM file. */
2472 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
2473 return 0;
2475 else
2477 Lisp_Object data;
2478 int width, height;
2480 /* Entries for `:width', `:height' and `:data' must be present. */
2481 if (!kw[XBM_WIDTH].count
2482 || !kw[XBM_HEIGHT].count
2483 || !kw[XBM_DATA].count)
2484 return 0;
2486 data = kw[XBM_DATA].value;
2487 width = XFASTINT (kw[XBM_WIDTH].value);
2488 height = XFASTINT (kw[XBM_HEIGHT].value);
2490 /* Check type of data, and width and height against contents of
2491 data. */
2492 if (VECTORP (data))
2494 EMACS_INT i;
2496 /* Number of elements of the vector must be >= height. */
2497 if (ASIZE (data) < height)
2498 return 0;
2500 /* Each string or bool-vector in data must be large enough
2501 for one line of the image. */
2502 for (i = 0; i < height; ++i)
2504 Lisp_Object elt = AREF (data, i);
2506 if (STRINGP (elt))
2508 if (SCHARS (elt)
2509 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
2510 return 0;
2512 else if (BOOL_VECTOR_P (elt))
2514 if (bool_vector_size (elt) < width)
2515 return 0;
2517 else
2518 return 0;
2521 else if (STRINGP (data))
2523 if (SCHARS (data)
2524 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
2525 return 0;
2527 else if (BOOL_VECTOR_P (data))
2529 if (bool_vector_size (data) / height < width)
2530 return 0;
2532 else
2533 return 0;
2536 return 1;
2540 /* Scan a bitmap file. FP is the stream to read from. Value is
2541 either an enumerator from enum xbm_token, or a character for a
2542 single-character token, or 0 at end of file. If scanning an
2543 identifier, store the lexeme of the identifier in SVAL. If
2544 scanning a number, store its value in *IVAL. */
2546 static int
2547 xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival)
2549 unsigned int c;
2551 loop:
2553 /* Skip white space. */
2554 while (*s < end && (c = *(*s)++, c_isspace (c)))
2557 if (*s >= end)
2558 c = 0;
2559 else if (c_isdigit (c))
2561 int value = 0, digit;
2563 if (c == '0' && *s < end)
2565 c = *(*s)++;
2566 if (c == 'x' || c == 'X')
2568 while (*s < end)
2570 c = *(*s)++;
2571 if (c_isdigit (c))
2572 digit = c - '0';
2573 else if (c >= 'a' && c <= 'f')
2574 digit = c - 'a' + 10;
2575 else if (c >= 'A' && c <= 'F')
2576 digit = c - 'A' + 10;
2577 else
2578 break;
2579 value = 16 * value + digit;
2582 else if (c_isdigit (c))
2584 value = c - '0';
2585 while (*s < end
2586 && (c = *(*s)++, c_isdigit (c)))
2587 value = 8 * value + c - '0';
2590 else
2592 value = c - '0';
2593 while (*s < end
2594 && (c = *(*s)++, c_isdigit (c)))
2595 value = 10 * value + c - '0';
2598 if (*s < end)
2599 *s = *s - 1;
2600 *ival = value;
2601 c = XBM_TK_NUMBER;
2603 else if (c_isalpha (c) || c == '_')
2605 *sval++ = c;
2606 while (*s < end
2607 && (c = *(*s)++, (c_isalnum (c) || c == '_')))
2608 *sval++ = c;
2609 *sval = 0;
2610 if (*s < end)
2611 *s = *s - 1;
2612 c = XBM_TK_IDENT;
2614 else if (c == '/' && **s == '*')
2616 /* C-style comment. */
2617 ++*s;
2618 while (**s && (**s != '*' || *(*s + 1) != '/'))
2619 ++*s;
2620 if (**s)
2622 *s += 2;
2623 goto loop;
2627 return c;
2630 #ifdef HAVE_NTGUI
2632 /* Create a Windows bitmap from X bitmap data. */
2633 static HBITMAP
2634 w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
2636 static unsigned char swap_nibble[16]
2637 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2638 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2639 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2640 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2641 int i, j, w1, w2;
2642 unsigned char *bits, *p;
2643 HBITMAP bmp;
2645 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
2646 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2647 bits = alloca (height * w2);
2648 memset (bits, 0, height * w2);
2649 for (i = 0; i < height; i++)
2651 p = bits + i*w2;
2652 for (j = 0; j < w1; j++)
2654 /* Bitswap XBM bytes to match how Windows does things. */
2655 unsigned char c = *data++;
2656 *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
2657 | (swap_nibble[(c>>4) & 0xf]));
2660 bmp = CreateBitmap (width, height, 1, 1, (char *) bits);
2662 return bmp;
2665 static void
2666 convert_mono_to_color_image (struct frame *f, struct image *img,
2667 COLORREF foreground, COLORREF background)
2669 HDC hdc, old_img_dc, new_img_dc;
2670 HGDIOBJ old_prev, new_prev;
2671 HBITMAP new_pixmap;
2673 hdc = get_frame_dc (f);
2674 old_img_dc = CreateCompatibleDC (hdc);
2675 new_img_dc = CreateCompatibleDC (hdc);
2676 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
2677 release_frame_dc (f, hdc);
2678 old_prev = SelectObject (old_img_dc, img->pixmap);
2679 new_prev = SelectObject (new_img_dc, new_pixmap);
2680 /* Windows convention for mono bitmaps is black = background,
2681 white = foreground. */
2682 SetTextColor (new_img_dc, background);
2683 SetBkColor (new_img_dc, foreground);
2685 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
2686 0, 0, SRCCOPY);
2688 SelectObject (old_img_dc, old_prev);
2689 SelectObject (new_img_dc, new_prev);
2690 DeleteDC (old_img_dc);
2691 DeleteDC (new_img_dc);
2692 DeleteObject (img->pixmap);
2693 if (new_pixmap == 0)
2694 fprintf (stderr, "Failed to convert image to color.\n");
2695 else
2696 img->pixmap = new_pixmap;
2699 #define XBM_BIT_SHUFFLE(b) (~(b))
2701 #else
2703 #define XBM_BIT_SHUFFLE(b) (b)
2705 #endif /* HAVE_NTGUI */
2708 static void
2709 Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data,
2710 RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg,
2711 bool non_default_colors)
2713 #ifdef HAVE_NTGUI
2714 img->pixmap
2715 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
2717 /* If colors were specified, transfer the bitmap to a color one. */
2718 if (non_default_colors)
2719 convert_mono_to_color_image (f, img, fg, bg);
2721 #elif defined (HAVE_NS)
2722 img->pixmap = ns_image_from_XBM (data, img->width, img->height, fg, bg);
2724 #else
2725 img->pixmap =
2726 (x_check_image_size (0, img->width, img->height)
2727 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
2728 FRAME_X_WINDOW (f),
2729 data,
2730 img->width, img->height,
2731 fg, bg,
2732 DefaultDepthOfScreen (FRAME_X_SCREEN (f)))
2733 : NO_PIXMAP);
2734 #endif /* !HAVE_NTGUI && !HAVE_NS */
2739 /* Replacement for XReadBitmapFileData which isn't available under old
2740 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2741 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2742 the image. Return in *DATA the bitmap data allocated with xmalloc.
2743 Value is true if successful. DATA null means just test if
2744 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2745 inhibit the call to image_error when the image size is invalid (the
2746 bitmap remains unread). */
2748 static bool
2749 xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end,
2750 int *width, int *height, char **data,
2751 bool inhibit_image_error)
2753 unsigned char *s = contents;
2754 char buffer[BUFSIZ];
2755 bool padding_p = 0;
2756 bool v10 = 0;
2757 int bytes_per_line, i, nbytes;
2758 char *p;
2759 int value;
2760 int LA1;
2762 #define match() \
2763 LA1 = xbm_scan (&s, end, buffer, &value)
2765 #define expect(TOKEN) \
2766 do \
2768 if (LA1 != (TOKEN)) \
2769 goto failure; \
2770 match (); \
2772 while (0)
2774 #define expect_ident(IDENT) \
2775 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2776 match (); \
2777 else \
2778 goto failure
2780 *width = *height = -1;
2781 if (data)
2782 *data = NULL;
2783 LA1 = xbm_scan (&s, end, buffer, &value);
2785 /* Parse defines for width, height and hot-spots. */
2786 while (LA1 == '#')
2788 match ();
2789 expect_ident ("define");
2790 expect (XBM_TK_IDENT);
2792 if (LA1 == XBM_TK_NUMBER)
2794 char *q = strrchr (buffer, '_');
2795 q = q ? q + 1 : buffer;
2796 if (strcmp (q, "width") == 0)
2797 *width = value;
2798 else if (strcmp (q, "height") == 0)
2799 *height = value;
2801 expect (XBM_TK_NUMBER);
2804 if (!check_image_size (f, *width, *height))
2806 if (!inhibit_image_error)
2807 image_size_error ();
2808 goto failure;
2810 else if (data == NULL)
2811 goto success;
2813 /* Parse bits. Must start with `static'. */
2814 expect_ident ("static");
2815 if (LA1 == XBM_TK_IDENT)
2817 if (strcmp (buffer, "unsigned") == 0)
2819 match ();
2820 expect_ident ("char");
2822 else if (strcmp (buffer, "short") == 0)
2824 match ();
2825 v10 = 1;
2826 if (*width % 16 && *width % 16 < 9)
2827 padding_p = 1;
2829 else if (strcmp (buffer, "char") == 0)
2830 match ();
2831 else
2832 goto failure;
2834 else
2835 goto failure;
2837 expect (XBM_TK_IDENT);
2838 expect ('[');
2839 expect (']');
2840 expect ('=');
2841 expect ('{');
2843 if (! x_check_image_size (0, *width, *height))
2845 if (!inhibit_image_error)
2846 image_error ("Image too large (%dx%d)",
2847 make_number (*width), make_number (*height));
2848 goto failure;
2850 bytes_per_line = (*width + 7) / 8 + padding_p;
2851 nbytes = bytes_per_line * *height;
2852 p = *data = xmalloc (nbytes);
2854 if (v10)
2856 for (i = 0; i < nbytes; i += 2)
2858 int val = value;
2859 expect (XBM_TK_NUMBER);
2861 *p++ = XBM_BIT_SHUFFLE (val);
2862 if (!padding_p || ((i + 2) % bytes_per_line))
2863 *p++ = XBM_BIT_SHUFFLE (value >> 8);
2865 if (LA1 == ',' || LA1 == '}')
2866 match ();
2867 else
2868 goto failure;
2871 else
2873 for (i = 0; i < nbytes; ++i)
2875 int val = value;
2876 expect (XBM_TK_NUMBER);
2878 *p++ = XBM_BIT_SHUFFLE (val);
2880 if (LA1 == ',' || LA1 == '}')
2881 match ();
2882 else
2883 goto failure;
2887 success:
2888 return 1;
2890 failure:
2892 if (data && *data)
2894 xfree (*data);
2895 *data = NULL;
2897 return 0;
2899 #undef match
2900 #undef expect
2901 #undef expect_ident
2905 /* Load XBM image IMG which will be displayed on frame F from buffer
2906 CONTENTS. END is the end of the buffer. Value is true if
2907 successful. */
2909 static bool
2910 xbm_load_image (struct frame *f, struct image *img, unsigned char *contents,
2911 unsigned char *end)
2913 bool rc;
2914 char *data;
2915 bool success_p = 0;
2917 rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height,
2918 &data, 0);
2919 if (rc)
2921 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
2922 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
2923 bool non_default_colors = 0;
2924 Lisp_Object value;
2926 eassert (img->width > 0 && img->height > 0);
2928 /* Get foreground and background colors, maybe allocate colors. */
2929 value = image_spec_value (img->spec, QCforeground, NULL);
2930 if (!NILP (value))
2932 foreground = x_alloc_image_color (f, img, value, foreground);
2933 non_default_colors = 1;
2935 value = image_spec_value (img->spec, QCbackground, NULL);
2936 if (!NILP (value))
2938 background = x_alloc_image_color (f, img, value, background);
2939 img->background = background;
2940 img->background_valid = 1;
2941 non_default_colors = 1;
2944 Create_Pixmap_From_Bitmap_Data (f, img, data,
2945 foreground, background,
2946 non_default_colors);
2947 xfree (data);
2949 if (img->pixmap == NO_PIXMAP)
2951 x_clear_image (f, img);
2952 image_error ("Unable to create X pixmap for `%s'", img->spec);
2954 else
2955 success_p = 1;
2957 else
2958 image_error ("Error loading XBM image `%s'", img->spec);
2960 return success_p;
2964 /* Value is true if DATA looks like an in-memory XBM file. */
2966 static bool
2967 xbm_file_p (Lisp_Object data)
2969 int w, h;
2970 return (STRINGP (data)
2971 && xbm_read_bitmap_data (NULL, SDATA (data),
2972 (SDATA (data) + SBYTES (data)),
2973 &w, &h, NULL, 1));
2977 /* Fill image IMG which is used on frame F with pixmap data. Value is
2978 true if successful. */
2980 static bool
2981 xbm_load (struct frame *f, struct image *img)
2983 bool success_p = 0;
2984 Lisp_Object file_name;
2986 eassert (xbm_image_p (img->spec));
2988 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2989 file_name = image_spec_value (img->spec, QCfile, NULL);
2990 if (STRINGP (file_name))
2992 int fd;
2993 Lisp_Object file = x_find_image_fd (file_name, &fd);
2994 if (!STRINGP (file))
2996 image_error ("Cannot find image file `%s'", file_name);
2997 return 0;
3000 ptrdiff_t size;
3001 unsigned char *contents = slurp_file (fd, &size);
3002 if (contents == NULL)
3004 image_error ("Error loading XBM image `%s'", file);
3005 return 0;
3008 success_p = xbm_load_image (f, img, contents, contents + size);
3009 xfree (contents);
3011 else
3013 struct image_keyword fmt[XBM_LAST];
3014 Lisp_Object data;
3015 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
3016 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
3017 bool non_default_colors = 0;
3018 char *bits;
3019 bool parsed_p;
3020 bool in_memory_file_p = 0;
3022 /* See if data looks like an in-memory XBM file. */
3023 data = image_spec_value (img->spec, QCdata, NULL);
3024 in_memory_file_p = xbm_file_p (data);
3026 /* Parse the image specification. */
3027 memcpy (fmt, xbm_format, sizeof fmt);
3028 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
3029 eassert (parsed_p);
3031 /* Get specified width, and height. */
3032 if (!in_memory_file_p)
3034 img->width = XFASTINT (fmt[XBM_WIDTH].value);
3035 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
3036 eassert (img->width > 0 && img->height > 0);
3037 if (!check_image_size (f, img->width, img->height))
3039 image_size_error ();
3040 return 0;
3044 /* Get foreground and background colors, maybe allocate colors. */
3045 if (fmt[XBM_FOREGROUND].count
3046 && STRINGP (fmt[XBM_FOREGROUND].value))
3048 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
3049 foreground);
3050 non_default_colors = 1;
3053 if (fmt[XBM_BACKGROUND].count
3054 && STRINGP (fmt[XBM_BACKGROUND].value))
3056 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
3057 background);
3058 non_default_colors = 1;
3061 if (in_memory_file_p)
3062 success_p = xbm_load_image (f, img, SDATA (data),
3063 (SDATA (data)
3064 + SBYTES (data)));
3065 else
3067 USE_SAFE_ALLOCA;
3069 if (VECTORP (data))
3071 int i;
3072 char *p;
3073 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3075 SAFE_NALLOCA (bits, nbytes, img->height);
3076 p = bits;
3077 for (i = 0; i < img->height; ++i, p += nbytes)
3079 Lisp_Object line = AREF (data, i);
3080 if (STRINGP (line))
3081 memcpy (p, SDATA (line), nbytes);
3082 else
3083 memcpy (p, bool_vector_data (line), nbytes);
3086 else if (STRINGP (data))
3087 bits = SSDATA (data);
3088 else
3089 bits = (char *) bool_vector_data (data);
3091 #ifdef HAVE_NTGUI
3093 char *invertedBits;
3094 int nbytes, i;
3095 /* Windows mono bitmaps are reversed compared with X. */
3096 invertedBits = bits;
3097 nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3098 SAFE_NALLOCA (bits, nbytes, img->height);
3099 for (i = 0; i < nbytes; i++)
3100 bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]);
3102 #endif
3103 /* Create the pixmap. */
3105 if (x_check_image_size (0, img->width, img->height))
3106 Create_Pixmap_From_Bitmap_Data (f, img, bits,
3107 foreground, background,
3108 non_default_colors);
3109 else
3110 img->pixmap = NO_PIXMAP;
3112 if (img->pixmap)
3113 success_p = 1;
3114 else
3116 image_error ("Unable to create pixmap for XBM image `%s'",
3117 img->spec);
3118 x_clear_image (f, img);
3121 SAFE_FREE ();
3125 return success_p;
3130 /***********************************************************************
3131 XPM images
3132 ***********************************************************************/
3134 #if defined (HAVE_XPM) || defined (HAVE_NS)
3136 static bool xpm_image_p (Lisp_Object object);
3137 static bool xpm_load (struct frame *f, struct image *img);
3139 #endif /* HAVE_XPM || HAVE_NS */
3141 #ifdef HAVE_XPM
3142 #ifdef HAVE_NTGUI
3143 /* Indicate to xpm.h that we don't have Xlib. */
3144 #define FOR_MSW
3145 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3146 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3147 #define XColor xpm_XColor
3148 #define XImage xpm_XImage
3149 #define Display xpm_Display
3150 #define PIXEL_ALREADY_TYPEDEFED
3151 #include "X11/xpm.h"
3152 #undef FOR_MSW
3153 #undef XColor
3154 #undef XImage
3155 #undef Display
3156 #undef PIXEL_ALREADY_TYPEDEFED
3157 #else
3158 #include "X11/xpm.h"
3159 #endif /* HAVE_NTGUI */
3160 #endif /* HAVE_XPM */
3162 #if defined (HAVE_XPM) || defined (HAVE_NS)
3164 /* Indices of image specification fields in xpm_format, below. */
3166 enum xpm_keyword_index
3168 XPM_TYPE,
3169 XPM_FILE,
3170 XPM_DATA,
3171 XPM_ASCENT,
3172 XPM_MARGIN,
3173 XPM_RELIEF,
3174 XPM_ALGORITHM,
3175 XPM_HEURISTIC_MASK,
3176 XPM_MASK,
3177 XPM_COLOR_SYMBOLS,
3178 XPM_BACKGROUND,
3179 XPM_LAST
3182 /* Vector of image_keyword structures describing the format
3183 of valid XPM image specifications. */
3185 static const struct image_keyword xpm_format[XPM_LAST] =
3187 {":type", IMAGE_SYMBOL_VALUE, 1},
3188 {":file", IMAGE_STRING_VALUE, 0},
3189 {":data", IMAGE_STRING_VALUE, 0},
3190 {":ascent", IMAGE_ASCENT_VALUE, 0},
3191 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
3192 {":relief", IMAGE_INTEGER_VALUE, 0},
3193 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3194 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3195 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3196 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3197 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
3200 #if defined HAVE_NTGUI && defined WINDOWSNT
3201 static bool init_xpm_functions (void);
3202 #else
3203 #define init_xpm_functions NULL
3204 #endif
3206 /* Structure describing the image type XPM. */
3208 static struct image_type xpm_type =
3210 SYMBOL_INDEX (Qxpm),
3211 xpm_image_p,
3212 xpm_load,
3213 x_clear_image,
3214 init_xpm_functions,
3215 NULL
3218 #ifdef HAVE_X_WINDOWS
3220 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3221 functions for allocating image colors. Our own functions handle
3222 color allocation failures more gracefully than the ones on the XPM
3223 lib. */
3225 #ifndef USE_CAIRO
3226 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3227 #define ALLOC_XPM_COLORS
3228 #endif
3229 #endif /* USE_CAIRO */
3230 #endif /* HAVE_X_WINDOWS */
3232 #ifdef ALLOC_XPM_COLORS
3234 static struct xpm_cached_color *xpm_cache_color (struct frame *, char *,
3235 XColor *, int);
3237 /* An entry in a hash table used to cache color definitions of named
3238 colors. This cache is necessary to speed up XPM image loading in
3239 case we do color allocations ourselves. Without it, we would need
3240 a call to XParseColor per pixel in the image.
3242 FIXME Now that we're using x_parse_color and its cache, reevaluate
3243 the need for this caching layer. */
3245 struct xpm_cached_color
3247 /* Next in collision chain. */
3248 struct xpm_cached_color *next;
3250 /* Color definition (RGB and pixel color). */
3251 XColor color;
3253 /* Color name. */
3254 char name[FLEXIBLE_ARRAY_MEMBER];
3257 /* The hash table used for the color cache, and its bucket vector
3258 size. */
3260 #define XPM_COLOR_CACHE_BUCKETS 1001
3261 static struct xpm_cached_color **xpm_color_cache;
3263 /* Initialize the color cache. */
3265 static void
3266 xpm_init_color_cache (struct frame *f, XpmAttributes *attrs)
3268 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
3269 xpm_color_cache = xzalloc (nbytes);
3270 init_color_table ();
3272 if (attrs->valuemask & XpmColorSymbols)
3274 int i;
3275 XColor color;
3277 for (i = 0; i < attrs->numsymbols; ++i)
3278 if (x_parse_color (f, attrs->colorsymbols[i].value, &color))
3280 color.pixel = lookup_rgb_color (f, color.red, color.green,
3281 color.blue);
3282 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
3287 /* Free the color cache. */
3289 static void
3290 xpm_free_color_cache (void)
3292 struct xpm_cached_color *p, *next;
3293 int i;
3295 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
3296 for (p = xpm_color_cache[i]; p; p = next)
3298 next = p->next;
3299 xfree (p);
3302 xfree (xpm_color_cache);
3303 xpm_color_cache = NULL;
3304 free_color_table ();
3307 /* Return the bucket index for color named COLOR_NAME in the color
3308 cache. */
3310 static int
3311 xpm_color_bucket (char *color_name)
3313 EMACS_UINT hash = hash_string (color_name, strlen (color_name));
3314 return hash % XPM_COLOR_CACHE_BUCKETS;
3318 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3319 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3320 entry added. */
3322 static struct xpm_cached_color *
3323 xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket)
3325 size_t nbytes;
3326 struct xpm_cached_color *p;
3328 if (bucket < 0)
3329 bucket = xpm_color_bucket (color_name);
3331 nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1;
3332 p = xmalloc (nbytes);
3333 strcpy (p->name, color_name);
3334 p->color = *color;
3335 p->next = xpm_color_cache[bucket];
3336 xpm_color_cache[bucket] = p;
3337 return p;
3340 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3341 return the cached definition in *COLOR. Otherwise, make a new
3342 entry in the cache and allocate the color. Value is false if color
3343 allocation failed. */
3345 static bool
3346 xpm_lookup_color (struct frame *f, char *color_name, XColor *color)
3348 struct xpm_cached_color *p;
3349 int h = xpm_color_bucket (color_name);
3351 for (p = xpm_color_cache[h]; p; p = p->next)
3352 if (strcmp (p->name, color_name) == 0)
3353 break;
3355 if (p != NULL)
3356 *color = p->color;
3357 else if (x_parse_color (f, color_name, color))
3359 color->pixel = lookup_rgb_color (f, color->red, color->green,
3360 color->blue);
3361 p = xpm_cache_color (f, color_name, color, h);
3363 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3364 with transparency, and it's useful. */
3365 else if (strcmp ("opaque", color_name) == 0)
3367 memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */
3368 color->pixel = FRAME_FOREGROUND_PIXEL (f);
3369 p = xpm_cache_color (f, color_name, color, h);
3372 return p != NULL;
3376 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3377 CLOSURE is a pointer to the frame on which we allocate the
3378 color. Return in *COLOR the allocated color. Value is non-zero
3379 if successful. */
3381 static int
3382 xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color,
3383 void *closure)
3385 return xpm_lookup_color (closure, color_name, color);
3389 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3390 is a pointer to the frame on which we allocate the color. Value is
3391 non-zero if successful. */
3393 static int
3394 xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure)
3396 return 1;
3399 #endif /* ALLOC_XPM_COLORS */
3402 #ifdef WINDOWSNT
3404 /* XPM library details. */
3406 DEF_DLL_FN (void, XpmFreeAttributes, (XpmAttributes *));
3407 DEF_DLL_FN (int, XpmCreateImageFromBuffer,
3408 (Display *, char *, xpm_XImage **,
3409 xpm_XImage **, XpmAttributes *));
3410 DEF_DLL_FN (int, XpmReadFileToImage,
3411 (Display *, char *, xpm_XImage **,
3412 xpm_XImage **, XpmAttributes *));
3413 DEF_DLL_FN (void, XImageFree, (xpm_XImage *));
3415 static bool
3416 init_xpm_functions (void)
3418 HMODULE library;
3420 if (!(library = w32_delayed_load (Qxpm)))
3421 return 0;
3423 LOAD_DLL_FN (library, XpmFreeAttributes);
3424 LOAD_DLL_FN (library, XpmCreateImageFromBuffer);
3425 LOAD_DLL_FN (library, XpmReadFileToImage);
3426 LOAD_DLL_FN (library, XImageFree);
3427 return 1;
3430 # undef XImageFree
3431 # undef XpmCreateImageFromBuffer
3432 # undef XpmFreeAttributes
3433 # undef XpmReadFileToImage
3435 # define XImageFree fn_XImageFree
3436 # define XpmCreateImageFromBuffer fn_XpmCreateImageFromBuffer
3437 # define XpmFreeAttributes fn_XpmFreeAttributes
3438 # define XpmReadFileToImage fn_XpmReadFileToImage
3440 #endif /* WINDOWSNT */
3442 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3443 for XPM images. Such a list must consist of conses whose car and
3444 cdr are strings. */
3446 static bool
3447 xpm_valid_color_symbols_p (Lisp_Object color_symbols)
3449 while (CONSP (color_symbols))
3451 Lisp_Object sym = XCAR (color_symbols);
3452 if (!CONSP (sym)
3453 || !STRINGP (XCAR (sym))
3454 || !STRINGP (XCDR (sym)))
3455 break;
3456 color_symbols = XCDR (color_symbols);
3459 return NILP (color_symbols);
3463 /* Value is true if OBJECT is a valid XPM image specification. */
3465 static bool
3466 xpm_image_p (Lisp_Object object)
3468 struct image_keyword fmt[XPM_LAST];
3469 memcpy (fmt, xpm_format, sizeof fmt);
3470 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
3471 /* Either `:file' or `:data' must be present. */
3472 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
3473 /* Either no `:color-symbols' or it's a list of conses
3474 whose car and cdr are strings. */
3475 && (fmt[XPM_COLOR_SYMBOLS].count == 0
3476 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
3479 #endif /* HAVE_XPM || HAVE_NS */
3481 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3482 ptrdiff_t
3483 x_create_bitmap_from_xpm_data (struct frame *f, const char **bits)
3485 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
3486 ptrdiff_t id;
3487 int rc;
3488 XpmAttributes attrs;
3489 Pixmap bitmap, mask;
3491 memset (&attrs, 0, sizeof attrs);
3493 attrs.visual = FRAME_X_VISUAL (f);
3494 attrs.colormap = FRAME_X_COLORMAP (f);
3495 attrs.valuemask |= XpmVisual;
3496 attrs.valuemask |= XpmColormap;
3498 rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3499 (char **) bits, &bitmap, &mask, &attrs);
3500 if (rc != XpmSuccess)
3502 XpmFreeAttributes (&attrs);
3503 return -1;
3506 id = x_allocate_bitmap_record (f);
3507 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
3508 dpyinfo->bitmaps[id - 1].have_mask = true;
3509 dpyinfo->bitmaps[id - 1].mask = mask;
3510 dpyinfo->bitmaps[id - 1].file = NULL;
3511 dpyinfo->bitmaps[id - 1].height = attrs.height;
3512 dpyinfo->bitmaps[id - 1].width = attrs.width;
3513 dpyinfo->bitmaps[id - 1].depth = attrs.depth;
3514 dpyinfo->bitmaps[id - 1].refcount = 1;
3516 XpmFreeAttributes (&attrs);
3517 return id;
3519 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3521 /* Load image IMG which will be displayed on frame F. Value is
3522 true if successful. */
3524 #ifdef HAVE_XPM
3526 static bool
3527 xpm_load (struct frame *f, struct image *img)
3529 int rc;
3530 XpmAttributes attrs;
3531 Lisp_Object specified_file, color_symbols;
3532 USE_SAFE_ALLOCA;
3534 #ifdef HAVE_NTGUI
3535 HDC hdc;
3536 xpm_XImage * xpm_image = NULL, * xpm_mask = NULL;
3537 #endif /* HAVE_NTGUI */
3539 /* Configure the XPM lib. Use the visual of frame F. Allocate
3540 close colors. Return colors allocated. */
3541 memset (&attrs, 0, sizeof attrs);
3543 #ifndef HAVE_NTGUI
3544 attrs.visual = FRAME_X_VISUAL (f);
3545 attrs.colormap = FRAME_X_COLORMAP (f);
3546 attrs.valuemask |= XpmVisual;
3547 attrs.valuemask |= XpmColormap;
3548 #endif /* HAVE_NTGUI */
3550 #ifdef ALLOC_XPM_COLORS
3551 /* Allocate colors with our own functions which handle
3552 failing color allocation more gracefully. */
3553 attrs.color_closure = f;
3554 attrs.alloc_color = xpm_alloc_color;
3555 attrs.free_colors = xpm_free_colors;
3556 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
3557 #else /* not ALLOC_XPM_COLORS */
3558 /* Let the XPM lib allocate colors. */
3559 attrs.valuemask |= XpmReturnAllocPixels;
3560 #ifdef XpmAllocCloseColors
3561 attrs.alloc_close_colors = 1;
3562 attrs.valuemask |= XpmAllocCloseColors;
3563 #else /* not XpmAllocCloseColors */
3564 attrs.closeness = 600;
3565 attrs.valuemask |= XpmCloseness;
3566 #endif /* not XpmAllocCloseColors */
3567 #endif /* ALLOC_XPM_COLORS */
3569 /* If image specification contains symbolic color definitions, add
3570 these to `attrs'. */
3571 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
3572 if (CONSP (color_symbols))
3574 Lisp_Object tail;
3575 XpmColorSymbol *xpm_syms;
3576 ptrdiff_t i, size;
3578 attrs.valuemask |= XpmColorSymbols;
3580 /* Count number of symbols. */
3581 attrs.numsymbols = 0;
3582 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
3583 ++attrs.numsymbols;
3585 /* Allocate an XpmColorSymbol array. */
3586 SAFE_NALLOCA (xpm_syms, 1, attrs.numsymbols);
3587 size = attrs.numsymbols * sizeof *xpm_syms;
3588 memset (xpm_syms, 0, size);
3589 attrs.colorsymbols = xpm_syms;
3591 /* Fill the color symbol array. */
3592 for (tail = color_symbols, i = 0;
3593 CONSP (tail);
3594 ++i, tail = XCDR (tail))
3596 Lisp_Object name;
3597 Lisp_Object color;
3598 char *empty_string = (char *) "";
3600 if (!CONSP (XCAR (tail)))
3602 xpm_syms[i].name = empty_string;
3603 xpm_syms[i].value = empty_string;
3604 continue;
3606 name = XCAR (XCAR (tail));
3607 color = XCDR (XCAR (tail));
3608 if (STRINGP (name))
3609 SAFE_ALLOCA_STRING (xpm_syms[i].name, name);
3610 else
3611 xpm_syms[i].name = empty_string;
3612 if (STRINGP (color))
3613 SAFE_ALLOCA_STRING (xpm_syms[i].value, color);
3614 else
3615 xpm_syms[i].value = empty_string;
3619 /* Create a pixmap for the image, either from a file, or from a
3620 string buffer containing data in the same format as an XPM file. */
3621 #ifdef ALLOC_XPM_COLORS
3622 xpm_init_color_cache (f, &attrs);
3623 #endif
3625 specified_file = image_spec_value (img->spec, QCfile, NULL);
3627 #ifdef HAVE_NTGUI
3629 HDC frame_dc = get_frame_dc (f);
3630 hdc = CreateCompatibleDC (frame_dc);
3631 release_frame_dc (f, frame_dc);
3633 #endif /* HAVE_NTGUI */
3635 if (STRINGP (specified_file))
3637 Lisp_Object file = x_find_image_file (specified_file);
3638 if (!STRINGP (file))
3640 image_error ("Cannot find image file `%s'", specified_file);
3641 #ifdef ALLOC_XPM_COLORS
3642 xpm_free_color_cache ();
3643 #endif
3644 SAFE_FREE ();
3645 return 0;
3648 file = ENCODE_FILE (file);
3649 #ifdef HAVE_NTGUI
3650 #ifdef WINDOWSNT
3651 /* FILE is encoded in UTF-8, but image libraries on Windows
3652 support neither UTF-8 nor UTF-16 encoded file names. So we
3653 need to re-encode it in ANSI. */
3654 file = ansi_encode_filename (file);
3655 #endif
3656 /* XpmReadFileToPixmap is not available in the Windows port of
3657 libxpm. But XpmReadFileToImage almost does what we want. */
3658 rc = XpmReadFileToImage (&hdc, SDATA (file),
3659 &xpm_image, &xpm_mask,
3660 &attrs);
3661 #else
3662 rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file),
3663 &img->ximg, &img->mask_img,
3664 &attrs);
3665 #endif /* HAVE_NTGUI */
3667 else
3669 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
3670 if (!STRINGP (buffer))
3672 image_error ("Invalid image data `%s'", buffer);
3673 #ifdef ALLOC_XPM_COLORS
3674 xpm_free_color_cache ();
3675 #endif
3676 SAFE_FREE ();
3677 return 0;
3679 #ifdef HAVE_NTGUI
3680 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3681 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3682 rc = XpmCreateImageFromBuffer (&hdc, SDATA (buffer),
3683 &xpm_image, &xpm_mask,
3684 &attrs);
3685 #else
3686 rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer),
3687 &img->ximg, &img->mask_img,
3688 &attrs);
3689 #endif /* HAVE_NTGUI */
3692 #ifdef USE_CAIRO
3693 // Load very specific Xpm:s.
3694 if (rc == XpmSuccess
3695 && img->ximg->format == ZPixmap
3696 && img->ximg->bits_per_pixel == 32
3697 && (! img->mask_img || img->mask_img->bits_per_pixel == 1))
3699 int width = img->ximg->width;
3700 int height = img->ximg->height;
3701 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
3702 int i;
3703 uint32_t *od = (uint32_t *)data;
3704 uint32_t *id = (uint32_t *)img->ximg->data;
3705 char *mid = img->mask_img ? img->mask_img->data : 0;
3706 uint32_t bgcolor = get_spec_bg_or_alpha_as_argb (img, f);
3708 for (i = 0; i < height; ++i)
3710 int k;
3711 for (k = 0; k < width; ++k)
3713 int idx = i * img->ximg->bytes_per_line/4 + k;
3714 int maskidx = mid ? i * img->mask_img->bytes_per_line + k/8 : 0;
3715 int mask = mid ? mid[maskidx] & (1 << (k % 8)) : 1;
3717 if (mask) od[idx] = id[idx] + 0xff000000; // ff => full alpha
3718 else od[idx] = bgcolor;
3722 create_cairo_image_surface (img, data, width, height);
3724 else
3726 rc = XpmFileInvalid;
3727 x_clear_image (f, img);
3729 #else
3730 #ifdef HAVE_X_WINDOWS
3731 if (rc == XpmSuccess)
3733 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3734 img->ximg->width, img->ximg->height,
3735 img->ximg->depth);
3736 if (img->pixmap == NO_PIXMAP)
3738 x_clear_image (f, img);
3739 rc = XpmNoMemory;
3741 else if (img->mask_img)
3743 img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3744 img->mask_img->width,
3745 img->mask_img->height,
3746 img->mask_img->depth);
3747 if (img->mask == NO_PIXMAP)
3749 x_clear_image (f, img);
3750 rc = XpmNoMemory;
3754 #endif
3755 #endif /* ! USE_CAIRO */
3757 if (rc == XpmSuccess)
3759 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3760 img->colors = colors_in_color_table (&img->ncolors);
3761 #else /* not ALLOC_XPM_COLORS */
3762 int i;
3764 #ifdef HAVE_NTGUI
3765 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3766 plus some duplicate attributes. */
3767 if (xpm_image && xpm_image->bitmap)
3769 img->pixmap = xpm_image->bitmap;
3770 /* XImageFree in libXpm frees XImage struct without destroying
3771 the bitmap, which is what we want. */
3772 XImageFree (xpm_image);
3774 if (xpm_mask && xpm_mask->bitmap)
3776 /* The mask appears to be inverted compared with what we expect.
3777 TODO: invert our expectations. See other places where we
3778 have to invert bits because our idea of masks is backwards. */
3779 HGDIOBJ old_obj;
3780 old_obj = SelectObject (hdc, xpm_mask->bitmap);
3782 PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT);
3783 SelectObject (hdc, old_obj);
3785 img->mask = xpm_mask->bitmap;
3786 XImageFree (xpm_mask);
3787 DeleteDC (hdc);
3790 DeleteDC (hdc);
3791 #endif /* HAVE_NTGUI */
3793 /* Remember allocated colors. */
3794 img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors);
3795 img->ncolors = attrs.nalloc_pixels;
3796 for (i = 0; i < attrs.nalloc_pixels; ++i)
3798 img->colors[i] = attrs.alloc_pixels[i];
3799 #ifdef DEBUG_X_COLORS
3800 register_color (img->colors[i]);
3801 #endif
3803 #endif /* not ALLOC_XPM_COLORS */
3805 img->width = attrs.width;
3806 img->height = attrs.height;
3807 eassert (img->width > 0 && img->height > 0);
3809 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3810 XpmFreeAttributes (&attrs);
3812 #ifdef HAVE_X_WINDOWS
3813 /* Maybe fill in the background field while we have ximg handy. */
3814 IMAGE_BACKGROUND (img, f, img->ximg);
3815 if (img->mask_img)
3816 /* Fill in the background_transparent field while we have the
3817 mask handy. */
3818 image_background_transparent (img, f, img->mask_img);
3819 #endif
3821 else
3823 #ifdef HAVE_NTGUI
3824 DeleteDC (hdc);
3825 #endif /* HAVE_NTGUI */
3827 switch (rc)
3829 case XpmOpenFailed:
3830 image_error ("Error opening XPM file (%s)", img->spec);
3831 break;
3833 case XpmFileInvalid:
3834 image_error ("Invalid XPM file (%s)", img->spec);
3835 break;
3837 case XpmNoMemory:
3838 image_error ("Out of memory (%s)", img->spec);
3839 break;
3841 case XpmColorFailed:
3842 image_error ("Color allocation error (%s)", img->spec);
3843 break;
3845 default:
3846 image_error ("Unknown error (%s)", img->spec);
3847 break;
3851 #ifdef ALLOC_XPM_COLORS
3852 xpm_free_color_cache ();
3853 #endif
3854 SAFE_FREE ();
3855 return rc == XpmSuccess;
3858 #endif /* HAVE_XPM */
3860 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3862 /* XPM support functions for NS where libxpm is not available.
3863 Only XPM version 3 (without any extensions) is supported. */
3865 static void xpm_put_color_table_v (Lisp_Object, const unsigned char *,
3866 int, Lisp_Object);
3867 static Lisp_Object xpm_get_color_table_v (Lisp_Object,
3868 const unsigned char *, int);
3869 static void xpm_put_color_table_h (Lisp_Object, const unsigned char *,
3870 int, Lisp_Object);
3871 static Lisp_Object xpm_get_color_table_h (Lisp_Object,
3872 const unsigned char *, int);
3874 /* Tokens returned from xpm_scan. */
3876 enum xpm_token
3878 XPM_TK_IDENT = 256,
3879 XPM_TK_STRING,
3880 XPM_TK_EOF
3883 /* Scan an XPM data and return a character (< 256) or a token defined
3884 by enum xpm_token above. *S and END are the start (inclusive) and
3885 the end (exclusive) addresses of the data, respectively. Advance
3886 *S while scanning. If token is either XPM_TK_IDENT or
3887 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3888 length of the corresponding token, respectively. */
3890 static int
3891 xpm_scan (const unsigned char **s,
3892 const unsigned char *end,
3893 const unsigned char **beg,
3894 ptrdiff_t *len)
3896 int c;
3898 while (*s < end)
3900 /* Skip white-space. */
3901 while (*s < end && (c = *(*s)++, c_isspace (c)))
3904 /* gnus-pointer.xpm uses '-' in its identifier.
3905 sb-dir-plus.xpm uses '+' in its identifier. */
3906 if (c_isalpha (c) || c == '_' || c == '-' || c == '+')
3908 *beg = *s - 1;
3909 while (*s < end
3910 && (c = **s, c_isalnum (c)
3911 || c == '_' || c == '-' || c == '+'))
3912 ++*s;
3913 *len = *s - *beg;
3914 return XPM_TK_IDENT;
3916 else if (c == '"')
3918 *beg = *s;
3919 while (*s < end && **s != '"')
3920 ++*s;
3921 *len = *s - *beg;
3922 if (*s < end)
3923 ++*s;
3924 return XPM_TK_STRING;
3926 else if (c == '/')
3928 if (*s < end && **s == '*')
3930 /* C-style comment. */
3931 ++*s;
3934 while (*s < end && *(*s)++ != '*')
3937 while (*s < end && **s != '/');
3938 if (*s < end)
3939 ++*s;
3941 else
3942 return c;
3944 else
3945 return c;
3948 return XPM_TK_EOF;
3951 /* Functions for color table lookup in XPM data. A key is a string
3952 specifying the color of each pixel in XPM data. A value is either
3953 an integer that specifies a pixel color, Qt that specifies
3954 transparency, or Qnil for the unspecified color. If the length of
3955 the key string is one, a vector is used as a table. Otherwise, a
3956 hash table is used. */
3958 static Lisp_Object
3959 xpm_make_color_table_v (void (**put_func) (Lisp_Object,
3960 const unsigned char *,
3961 int,
3962 Lisp_Object),
3963 Lisp_Object (**get_func) (Lisp_Object,
3964 const unsigned char *,
3965 int))
3967 *put_func = xpm_put_color_table_v;
3968 *get_func = xpm_get_color_table_v;
3969 return Fmake_vector (make_number (256), Qnil);
3972 static void
3973 xpm_put_color_table_v (Lisp_Object color_table,
3974 const unsigned char *chars_start,
3975 int chars_len,
3976 Lisp_Object color)
3978 ASET (color_table, *chars_start, color);
3981 static Lisp_Object
3982 xpm_get_color_table_v (Lisp_Object color_table,
3983 const unsigned char *chars_start,
3984 int chars_len)
3986 return AREF (color_table, *chars_start);
3989 static Lisp_Object
3990 xpm_make_color_table_h (void (**put_func) (Lisp_Object,
3991 const unsigned char *,
3992 int,
3993 Lisp_Object),
3994 Lisp_Object (**get_func) (Lisp_Object,
3995 const unsigned char *,
3996 int))
3998 *put_func = xpm_put_color_table_h;
3999 *get_func = xpm_get_color_table_h;
4000 return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE),
4001 make_float (DEFAULT_REHASH_SIZE),
4002 make_float (DEFAULT_REHASH_THRESHOLD),
4003 Qnil);
4006 static void
4007 xpm_put_color_table_h (Lisp_Object color_table,
4008 const unsigned char *chars_start,
4009 int chars_len,
4010 Lisp_Object color)
4012 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4013 EMACS_UINT hash_code;
4014 Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
4016 hash_lookup (table, chars, &hash_code);
4017 hash_put (table, chars, color, hash_code);
4020 static Lisp_Object
4021 xpm_get_color_table_h (Lisp_Object color_table,
4022 const unsigned char *chars_start,
4023 int chars_len)
4025 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4026 ptrdiff_t i =
4027 hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL);
4029 return i >= 0 ? HASH_VALUE (table, i) : Qnil;
4032 enum xpm_color_key {
4033 XPM_COLOR_KEY_S,
4034 XPM_COLOR_KEY_M,
4035 XPM_COLOR_KEY_G4,
4036 XPM_COLOR_KEY_G,
4037 XPM_COLOR_KEY_C
4040 static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"};
4042 static int
4043 xpm_str_to_color_key (const char *s)
4045 int i;
4047 for (i = 0; i < ARRAYELTS (xpm_color_key_strings); i++)
4048 if (strcmp (xpm_color_key_strings[i], s) == 0)
4049 return i;
4050 return -1;
4053 static bool
4054 xpm_load_image (struct frame *f,
4055 struct image *img,
4056 const unsigned char *contents,
4057 const unsigned char *end)
4059 const unsigned char *s = contents, *beg, *str;
4060 unsigned char buffer[BUFSIZ];
4061 int width, height, x, y;
4062 int num_colors, chars_per_pixel;
4063 ptrdiff_t len;
4064 int LA1;
4065 void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object);
4066 Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int);
4067 Lisp_Object frame, color_symbols, color_table;
4068 int best_key;
4069 bool have_mask = false;
4070 XImagePtr ximg = NULL, mask_img = NULL;
4072 #define match() \
4073 LA1 = xpm_scan (&s, end, &beg, &len)
4075 #define expect(TOKEN) \
4076 do \
4078 if (LA1 != (TOKEN)) \
4079 goto failure; \
4080 match (); \
4082 while (0)
4084 #define expect_ident(IDENT) \
4085 if (LA1 == XPM_TK_IDENT \
4086 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
4087 match (); \
4088 else \
4089 goto failure
4091 if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0))
4092 goto failure;
4093 s += 9;
4094 match ();
4095 expect_ident ("static");
4096 expect_ident ("char");
4097 expect ('*');
4098 expect (XPM_TK_IDENT);
4099 expect ('[');
4100 expect (']');
4101 expect ('=');
4102 expect ('{');
4103 expect (XPM_TK_STRING);
4104 if (len >= BUFSIZ)
4105 goto failure;
4106 memcpy (buffer, beg, len);
4107 buffer[len] = '\0';
4108 if (sscanf (buffer, "%d %d %d %d", &width, &height,
4109 &num_colors, &chars_per_pixel) != 4
4110 || width <= 0 || height <= 0
4111 || num_colors <= 0 || chars_per_pixel <= 0)
4112 goto failure;
4114 if (!check_image_size (f, width, height))
4116 image_size_error ();
4117 goto failure;
4120 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)
4121 #ifndef HAVE_NS
4122 || !image_create_x_image_and_pixmap (f, img, width, height, 1,
4123 &mask_img, 1)
4124 #endif
4127 image_error ("Image too large");
4128 goto failure;
4131 expect (',');
4133 XSETFRAME (frame, f);
4134 if (!NILP (Fxw_display_color_p (frame)))
4135 best_key = XPM_COLOR_KEY_C;
4136 else if (!NILP (Fx_display_grayscale_p (frame)))
4137 best_key = (XFASTINT (Fx_display_planes (frame)) > 2
4138 ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4);
4139 else
4140 best_key = XPM_COLOR_KEY_M;
4142 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
4143 if (chars_per_pixel == 1)
4144 color_table = xpm_make_color_table_v (&put_color_table,
4145 &get_color_table);
4146 else
4147 color_table = xpm_make_color_table_h (&put_color_table,
4148 &get_color_table);
4150 while (num_colors-- > 0)
4152 char *color, *max_color;
4153 int key, next_key, max_key = 0;
4154 Lisp_Object symbol_color = Qnil, color_val;
4155 XColor cdef;
4157 expect (XPM_TK_STRING);
4158 if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel)
4159 goto failure;
4160 memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel);
4161 buffer[len - chars_per_pixel] = '\0';
4163 str = strtok (buffer, " \t");
4164 if (str == NULL)
4165 goto failure;
4166 key = xpm_str_to_color_key (str);
4167 if (key < 0)
4168 goto failure;
4171 color = strtok (NULL, " \t");
4172 if (color == NULL)
4173 goto failure;
4175 while ((str = strtok (NULL, " \t")) != NULL)
4177 next_key = xpm_str_to_color_key (str);
4178 if (next_key >= 0)
4179 break;
4180 color[strlen (color)] = ' ';
4183 if (key == XPM_COLOR_KEY_S)
4185 if (NILP (symbol_color))
4186 symbol_color = build_string (color);
4188 else if (max_key < key && key <= best_key)
4190 max_key = key;
4191 max_color = color;
4193 key = next_key;
4195 while (str);
4197 color_val = Qnil;
4198 if (!NILP (color_symbols) && !NILP (symbol_color))
4200 Lisp_Object specified_color = Fassoc (symbol_color, color_symbols);
4202 if (CONSP (specified_color) && STRINGP (XCDR (specified_color)))
4204 if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0)
4205 color_val = Qt;
4206 else if (x_defined_color (f, SSDATA (XCDR (specified_color)),
4207 &cdef, 0))
4208 color_val = make_number (cdef.pixel);
4211 if (NILP (color_val) && max_key > 0)
4213 if (xstrcasecmp (max_color, "None") == 0)
4214 color_val = Qt;
4215 else if (x_defined_color (f, max_color, &cdef, 0))
4216 color_val = make_number (cdef.pixel);
4218 if (!NILP (color_val))
4219 (*put_color_table) (color_table, beg, chars_per_pixel, color_val);
4221 expect (',');
4224 for (y = 0; y < height; y++)
4226 expect (XPM_TK_STRING);
4227 str = beg;
4228 if (len < width * chars_per_pixel)
4229 goto failure;
4230 for (x = 0; x < width; x++, str += chars_per_pixel)
4232 Lisp_Object color_val =
4233 (*get_color_table) (color_table, str, chars_per_pixel);
4235 XPutPixel (ximg, x, y,
4236 (INTEGERP (color_val) ? XINT (color_val)
4237 : FRAME_FOREGROUND_PIXEL (f)));
4238 #ifndef HAVE_NS
4239 XPutPixel (mask_img, x, y,
4240 (!EQ (color_val, Qt) ? PIX_MASK_DRAW
4241 : (have_mask = true, PIX_MASK_RETAIN)));
4242 #else
4243 if (EQ (color_val, Qt))
4244 ns_set_alpha (ximg, x, y, 0);
4245 #endif
4247 if (y + 1 < height)
4248 expect (',');
4251 img->width = width;
4252 img->height = height;
4254 /* Maybe fill in the background field while we have ximg handy. */
4255 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
4256 IMAGE_BACKGROUND (img, f, ximg);
4258 image_put_x_image (f, img, ximg, 0);
4259 #ifndef HAVE_NS
4260 if (have_mask)
4262 /* Fill in the background_transparent field while we have the
4263 mask handy. */
4264 image_background_transparent (img, f, mask_img);
4266 image_put_x_image (f, img, mask_img, 1);
4268 else
4270 x_destroy_x_image (mask_img);
4271 x_clear_image_1 (f, img, CLEAR_IMAGE_MASK);
4273 #endif
4274 return 1;
4276 failure:
4277 image_error ("Invalid XPM file (%s)", img->spec);
4278 x_destroy_x_image (ximg);
4279 x_destroy_x_image (mask_img);
4280 x_clear_image (f, img);
4281 return 0;
4283 #undef match
4284 #undef expect
4285 #undef expect_ident
4288 static bool
4289 xpm_load (struct frame *f,
4290 struct image *img)
4292 bool success_p = 0;
4293 Lisp_Object file_name;
4295 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4296 file_name = image_spec_value (img->spec, QCfile, NULL);
4297 if (STRINGP (file_name))
4299 int fd;
4300 Lisp_Object file = x_find_image_fd (file_name, &fd);
4301 if (!STRINGP (file))
4303 image_error ("Cannot find image file `%s'", file_name);
4304 return 0;
4307 ptrdiff_t size;
4308 unsigned char *contents = slurp_file (fd, &size);
4309 if (contents == NULL)
4311 image_error ("Error loading XPM image `%s'", file);
4312 return 0;
4315 success_p = xpm_load_image (f, img, contents, contents + size);
4316 xfree (contents);
4318 else
4320 Lisp_Object data;
4322 data = image_spec_value (img->spec, QCdata, NULL);
4323 if (!STRINGP (data))
4325 image_error ("Invalid image data `%s'", data);
4326 return 0;
4328 success_p = xpm_load_image (f, img, SDATA (data),
4329 SDATA (data) + SBYTES (data));
4332 return success_p;
4335 #endif /* HAVE_NS && !HAVE_XPM */
4339 /***********************************************************************
4340 Color table
4341 ***********************************************************************/
4343 #ifdef COLOR_TABLE_SUPPORT
4345 /* An entry in the color table mapping an RGB color to a pixel color. */
4347 struct ct_color
4349 int r, g, b;
4350 unsigned long pixel;
4352 /* Next in color table collision list. */
4353 struct ct_color *next;
4356 /* The bucket vector size to use. Must be prime. */
4358 #define CT_SIZE 101
4360 /* Value is a hash of the RGB color given by R, G, and B. */
4362 static unsigned
4363 ct_hash_rgb (unsigned r, unsigned g, unsigned b)
4365 return (r << 16) ^ (g << 8) ^ b;
4368 /* The color hash table. */
4370 static struct ct_color **ct_table;
4372 /* Number of entries in the color table. */
4374 static int ct_colors_allocated;
4375 enum
4377 ct_colors_allocated_max =
4378 min (INT_MAX,
4379 min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long))
4382 /* Initialize the color table. */
4384 static void
4385 init_color_table (void)
4387 int size = CT_SIZE * sizeof (*ct_table);
4388 ct_table = xzalloc (size);
4389 ct_colors_allocated = 0;
4393 /* Free memory associated with the color table. */
4395 static void
4396 free_color_table (void)
4398 int i;
4399 struct ct_color *p, *next;
4401 for (i = 0; i < CT_SIZE; ++i)
4402 for (p = ct_table[i]; p; p = next)
4404 next = p->next;
4405 xfree (p);
4408 xfree (ct_table);
4409 ct_table = NULL;
4413 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4414 entry for that color already is in the color table, return the
4415 pixel color of that entry. Otherwise, allocate a new color for R,
4416 G, B, and make an entry in the color table. */
4418 static unsigned long
4419 lookup_rgb_color (struct frame *f, int r, int g, int b)
4421 unsigned hash = ct_hash_rgb (r, g, b);
4422 int i = hash % CT_SIZE;
4423 struct ct_color *p;
4424 Display_Info *dpyinfo;
4426 /* Handle TrueColor visuals specially, which improves performance by
4427 two orders of magnitude. Freeing colors on TrueColor visuals is
4428 a nop, and pixel colors specify RGB values directly. See also
4429 the Xlib spec, chapter 3.1. */
4430 dpyinfo = FRAME_DISPLAY_INFO (f);
4431 if (dpyinfo->red_bits > 0)
4433 /* Apply gamma-correction like normal color allocation does. */
4434 if (f->gamma)
4436 XColor color;
4437 color.red = r, color.green = g, color.blue = b;
4438 gamma_correct (f, &color);
4439 r = color.red, g = color.green, b = color.blue;
4442 return x_make_truecolor_pixel (dpyinfo, r, g, b);
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");
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 specified_file;
5251 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
5252 unsigned char *contents = NULL;
5253 unsigned char *end, *p;
5254 #ifdef USE_CAIRO
5255 unsigned char *data = 0;
5256 uint32_t *dataptr;
5257 #else
5258 XImagePtr ximg;
5259 #endif
5261 specified_file = image_spec_value (img->spec, QCfile, NULL);
5263 if (STRINGP (specified_file))
5265 int fd;
5266 Lisp_Object file = x_find_image_fd (specified_file, &fd);
5267 if (!STRINGP (file))
5269 image_error ("Cannot find image file `%s'", specified_file);
5270 return 0;
5273 ptrdiff_t size;
5274 contents = slurp_file (fd, &size);
5275 if (contents == NULL)
5277 image_error ("Error reading `%s'", file);
5278 return 0;
5281 p = contents;
5282 end = contents + size;
5284 else
5286 Lisp_Object data;
5287 data = image_spec_value (img->spec, QCdata, NULL);
5288 if (!STRINGP (data))
5290 image_error ("Invalid image data `%s'", data);
5291 return 0;
5293 p = SDATA (data);
5294 end = p + SBYTES (data);
5297 /* Check magic number. */
5298 if (end - p < 2 || *p++ != 'P')
5300 image_error ("Not a PBM image: `%s'", img->spec);
5301 error:
5302 xfree (contents);
5303 img->pixmap = NO_PIXMAP;
5304 return 0;
5307 switch (*p++)
5309 case '1':
5310 raw_p = 0, type = PBM_MONO;
5311 break;
5313 case '2':
5314 raw_p = 0, type = PBM_GRAY;
5315 break;
5317 case '3':
5318 raw_p = 0, type = PBM_COLOR;
5319 break;
5321 case '4':
5322 raw_p = 1, type = PBM_MONO;
5323 break;
5325 case '5':
5326 raw_p = 1, type = PBM_GRAY;
5327 break;
5329 case '6':
5330 raw_p = 1, type = PBM_COLOR;
5331 break;
5333 default:
5334 image_error ("Not a PBM image: `%s'", img->spec);
5335 goto error;
5338 /* Read width, height, maximum color-component. Characters
5339 starting with `#' up to the end of a line are ignored. */
5340 width = pbm_scan_number (&p, end);
5341 height = pbm_scan_number (&p, end);
5343 #ifdef USE_CAIRO
5344 data = (unsigned char *) xmalloc (width * height * 4);
5345 dataptr = (uint32_t *) data;
5346 #endif
5348 if (type != PBM_MONO)
5350 max_color_idx = pbm_scan_number (&p, end);
5351 if (max_color_idx > 65535 || max_color_idx < 0)
5353 image_error ("Unsupported maximum PBM color value");
5354 goto error;
5358 if (!check_image_size (f, width, height))
5360 image_size_error ();
5361 goto error;
5364 #ifndef USE_CAIRO
5365 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
5366 goto error;
5367 #endif
5369 /* Initialize the color hash table. */
5370 init_color_table ();
5372 if (type == PBM_MONO)
5374 int c = 0, g;
5375 struct image_keyword fmt[PBM_LAST];
5376 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
5377 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
5378 #ifdef USE_CAIRO
5379 XColor xfg, xbg;
5380 int fga32, bga32;
5381 #endif
5382 /* Parse the image specification. */
5383 memcpy (fmt, pbm_format, sizeof fmt);
5384 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
5386 /* Get foreground and background colors, maybe allocate colors. */
5387 #ifdef USE_CAIRO
5388 if (! fmt[PBM_FOREGROUND].count
5389 || ! STRINGP (fmt[PBM_FOREGROUND].value)
5390 || ! x_defined_color (f, SSDATA (fmt[PBM_FOREGROUND].value), &xfg, 0))
5392 xfg.pixel = fg;
5393 x_query_color (f, &xfg);
5395 fga32 = xcolor_to_argb32 (xfg);
5397 if (! fmt[PBM_BACKGROUND].count
5398 || ! STRINGP (fmt[PBM_BACKGROUND].value)
5399 || ! x_defined_color (f, SSDATA (fmt[PBM_BACKGROUND].value), &xbg, 0))
5401 xbg.pixel = bg;
5402 x_query_color (f, &xbg);
5404 bga32 = xcolor_to_argb32 (xbg);
5405 #else
5406 if (fmt[PBM_FOREGROUND].count
5407 && STRINGP (fmt[PBM_FOREGROUND].value))
5408 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
5409 if (fmt[PBM_BACKGROUND].count
5410 && STRINGP (fmt[PBM_BACKGROUND].value))
5412 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
5413 img->background = bg;
5414 img->background_valid = 1;
5416 #endif
5418 for (y = 0; y < height; ++y)
5419 for (x = 0; x < width; ++x)
5421 if (raw_p)
5423 if ((x & 7) == 0)
5425 if (p >= end)
5427 #ifdef USE_CAIRO
5428 xfree (data);
5429 #else
5430 x_destroy_x_image (ximg);
5431 #endif
5432 x_clear_image (f, img);
5433 image_error ("Invalid image size in image `%s'",
5434 img->spec);
5435 goto error;
5437 c = *p++;
5439 g = c & 0x80;
5440 c <<= 1;
5442 else
5443 g = pbm_scan_number (&p, end);
5445 #ifdef USE_CAIRO
5446 *dataptr++ = g ? fga32 : bga32;
5447 #else
5448 XPutPixel (ximg, x, y, g ? fg : bg);
5449 #endif
5452 else
5454 int expected_size = height * width;
5455 if (max_color_idx > 255)
5456 expected_size *= 2;
5457 if (type == PBM_COLOR)
5458 expected_size *= 3;
5460 if (raw_p && p + expected_size > end)
5462 #ifdef USE_CAIRO
5463 xfree (data);
5464 #else
5465 x_destroy_x_image (ximg);
5466 #endif
5467 x_clear_image (f, img);
5468 image_error ("Invalid image size in image `%s'", img->spec);
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'", img->spec);
5512 goto error;
5515 #ifdef USE_CAIRO
5516 r = (double) r * 255 / max_color_idx;
5517 g = (double) g * 255 / max_color_idx;
5518 b = (double) b * 255 / max_color_idx;
5519 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
5520 #else
5521 /* RGB values are now in the range 0..max_color_idx.
5522 Scale this to the range 0..0xffff supported by X. */
5523 r = (double) r * 65535 / max_color_idx;
5524 g = (double) g * 65535 / max_color_idx;
5525 b = (double) b * 65535 / max_color_idx;
5526 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
5527 #endif
5531 #ifdef COLOR_TABLE_SUPPORT
5532 /* Store in IMG->colors the colors allocated for the image, and
5533 free the color table. */
5534 img->colors = colors_in_color_table (&img->ncolors);
5535 free_color_table ();
5536 #endif /* COLOR_TABLE_SUPPORT */
5538 img->width = width;
5539 img->height = height;
5541 /* Maybe fill in the background field while we have ximg handy. */
5543 #ifdef USE_CAIRO
5544 create_cairo_image_surface (img, data, width, height);
5545 #else
5546 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
5547 /* Casting avoids a GCC warning. */
5548 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
5550 /* Put ximg into the image. */
5551 image_put_x_image (f, img, ximg, 0);
5552 #endif
5554 /* X and W32 versions did it here, MAC version above. ++kfs
5555 img->width = width;
5556 img->height = height; */
5558 xfree (contents);
5559 return 1;
5563 /***********************************************************************
5565 ***********************************************************************/
5567 #if defined (HAVE_PNG) || defined (HAVE_NS) || defined (USE_CAIRO)
5569 /* Function prototypes. */
5571 static bool png_image_p (Lisp_Object object);
5572 static bool png_load (struct frame *f, struct image *img);
5574 /* Indices of image specification fields in png_format, below. */
5576 enum png_keyword_index
5578 PNG_TYPE,
5579 PNG_DATA,
5580 PNG_FILE,
5581 PNG_ASCENT,
5582 PNG_MARGIN,
5583 PNG_RELIEF,
5584 PNG_ALGORITHM,
5585 PNG_HEURISTIC_MASK,
5586 PNG_MASK,
5587 PNG_BACKGROUND,
5588 PNG_LAST
5591 /* Vector of image_keyword structures describing the format
5592 of valid user-defined image specifications. */
5594 static const struct image_keyword png_format[PNG_LAST] =
5596 {":type", IMAGE_SYMBOL_VALUE, 1},
5597 {":data", IMAGE_STRING_VALUE, 0},
5598 {":file", IMAGE_STRING_VALUE, 0},
5599 {":ascent", IMAGE_ASCENT_VALUE, 0},
5600 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
5601 {":relief", IMAGE_INTEGER_VALUE, 0},
5602 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5603 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5604 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5605 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5608 #if defined HAVE_NTGUI && defined WINDOWSNT
5609 static bool init_png_functions (void);
5610 #else
5611 #define init_png_functions NULL
5612 #endif
5614 /* Structure describing the image type `png'. */
5616 static struct image_type png_type =
5618 SYMBOL_INDEX (Qpng),
5619 png_image_p,
5620 png_load,
5621 x_clear_image,
5622 init_png_functions,
5623 NULL
5626 /* Return true if OBJECT is a valid PNG image specification. */
5628 static bool
5629 png_image_p (Lisp_Object object)
5631 struct image_keyword fmt[PNG_LAST];
5632 memcpy (fmt, png_format, sizeof fmt);
5634 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
5635 return 0;
5637 /* Must specify either the :data or :file keyword. */
5638 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
5641 #endif /* HAVE_PNG || HAVE_NS || USE_CAIRO */
5644 #if (defined HAVE_PNG && !defined HAVE_NS) || defined USE_CAIRO
5646 # ifdef WINDOWSNT
5647 /* PNG library details. */
5649 DEF_DLL_FN (png_voidp, png_get_io_ptr, (png_structp));
5650 DEF_DLL_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t));
5651 DEF_DLL_FN (png_structp, png_create_read_struct,
5652 (png_const_charp, png_voidp, png_error_ptr, png_error_ptr));
5653 DEF_DLL_FN (png_infop, png_create_info_struct, (png_structp));
5654 DEF_DLL_FN (void, png_destroy_read_struct,
5655 (png_structpp, png_infopp, png_infopp));
5656 DEF_DLL_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr));
5657 DEF_DLL_FN (void, png_set_sig_bytes, (png_structp, int));
5658 DEF_DLL_FN (void, png_read_info, (png_structp, png_infop));
5659 DEF_DLL_FN (png_uint_32, png_get_IHDR,
5660 (png_structp, png_infop, png_uint_32 *, png_uint_32 *,
5661 int *, int *, int *, int *, int *));
5662 DEF_DLL_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32));
5663 DEF_DLL_FN (void, png_set_strip_16, (png_structp));
5664 DEF_DLL_FN (void, png_set_expand, (png_structp));
5665 DEF_DLL_FN (void, png_set_gray_to_rgb, (png_structp));
5666 DEF_DLL_FN (void, png_set_background,
5667 (png_structp, png_color_16p, int, int, double));
5668 DEF_DLL_FN (png_uint_32, png_get_bKGD,
5669 (png_structp, png_infop, png_color_16p *));
5670 DEF_DLL_FN (void, png_read_update_info, (png_structp, png_infop));
5671 DEF_DLL_FN (png_byte, png_get_channels, (png_structp, png_infop));
5672 DEF_DLL_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop));
5673 DEF_DLL_FN (void, png_read_image, (png_structp, png_bytepp));
5674 DEF_DLL_FN (void, png_read_end, (png_structp, png_infop));
5675 DEF_DLL_FN (void, png_error, (png_structp, png_const_charp));
5677 # if (PNG_LIBPNG_VER >= 10500)
5678 DEF_DLL_FN (void, png_longjmp, (png_structp, int)) PNG_NORETURN;
5679 DEF_DLL_FN (jmp_buf *, png_set_longjmp_fn,
5680 (png_structp, png_longjmp_ptr, size_t));
5681 # endif /* libpng version >= 1.5 */
5683 static bool
5684 init_png_functions (void)
5686 HMODULE library;
5688 if (!(library = w32_delayed_load (Qpng)))
5689 return 0;
5691 LOAD_DLL_FN (library, png_get_io_ptr);
5692 LOAD_DLL_FN (library, png_sig_cmp);
5693 LOAD_DLL_FN (library, png_create_read_struct);
5694 LOAD_DLL_FN (library, png_create_info_struct);
5695 LOAD_DLL_FN (library, png_destroy_read_struct);
5696 LOAD_DLL_FN (library, png_set_read_fn);
5697 LOAD_DLL_FN (library, png_set_sig_bytes);
5698 LOAD_DLL_FN (library, png_read_info);
5699 LOAD_DLL_FN (library, png_get_IHDR);
5700 LOAD_DLL_FN (library, png_get_valid);
5701 LOAD_DLL_FN (library, png_set_strip_16);
5702 LOAD_DLL_FN (library, png_set_expand);
5703 LOAD_DLL_FN (library, png_set_gray_to_rgb);
5704 LOAD_DLL_FN (library, png_set_background);
5705 LOAD_DLL_FN (library, png_get_bKGD);
5706 LOAD_DLL_FN (library, png_read_update_info);
5707 LOAD_DLL_FN (library, png_get_channels);
5708 LOAD_DLL_FN (library, png_get_rowbytes);
5709 LOAD_DLL_FN (library, png_read_image);
5710 LOAD_DLL_FN (library, png_read_end);
5711 LOAD_DLL_FN (library, png_error);
5713 # if (PNG_LIBPNG_VER >= 10500)
5714 LOAD_DLL_FN (library, png_longjmp);
5715 LOAD_DLL_FN (library, png_set_longjmp_fn);
5716 # endif /* libpng version >= 1.5 */
5718 return 1;
5721 # undef png_create_info_struct
5722 # undef png_create_read_struct
5723 # undef png_destroy_read_struct
5724 # undef png_error
5725 # undef png_get_bKGD
5726 # undef png_get_channels
5727 # undef png_get_IHDR
5728 # undef png_get_io_ptr
5729 # undef png_get_rowbytes
5730 # undef png_get_valid
5731 # undef png_longjmp
5732 # undef png_read_end
5733 # undef png_read_image
5734 # undef png_read_info
5735 # undef png_read_update_info
5736 # undef png_set_background
5737 # undef png_set_expand
5738 # undef png_set_gray_to_rgb
5739 # undef png_set_longjmp_fn
5740 # undef png_set_read_fn
5741 # undef png_set_sig_bytes
5742 # undef png_set_strip_16
5743 # undef png_sig_cmp
5745 # define png_create_info_struct fn_png_create_info_struct
5746 # define png_create_read_struct fn_png_create_read_struct
5747 # define png_destroy_read_struct fn_png_destroy_read_struct
5748 # define png_error fn_png_error
5749 # define png_get_bKGD fn_png_get_bKGD
5750 # define png_get_channels fn_png_get_channels
5751 # define png_get_IHDR fn_png_get_IHDR
5752 # define png_get_io_ptr fn_png_get_io_ptr
5753 # define png_get_rowbytes fn_png_get_rowbytes
5754 # define png_get_valid fn_png_get_valid
5755 # define png_longjmp fn_png_longjmp
5756 # define png_read_end fn_png_read_end
5757 # define png_read_image fn_png_read_image
5758 # define png_read_info fn_png_read_info
5759 # define png_read_update_info fn_png_read_update_info
5760 # define png_set_background fn_png_set_background
5761 # define png_set_expand fn_png_set_expand
5762 # define png_set_gray_to_rgb fn_png_set_gray_to_rgb
5763 # define png_set_longjmp_fn fn_png_set_longjmp_fn
5764 # define png_set_read_fn fn_png_set_read_fn
5765 # define png_set_sig_bytes fn_png_set_sig_bytes
5766 # define png_set_strip_16 fn_png_set_strip_16
5767 # define png_sig_cmp fn_png_sig_cmp
5769 # endif /* WINDOWSNT */
5771 /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp
5772 will do, POSIX _setjmp and _longjmp (if available) are often faster.
5773 Do not use sys_setjmp, as PNG supports only jmp_buf.
5774 It's OK if the longjmp substitute restores the signal mask. */
5775 # ifdef HAVE__SETJMP
5776 # define FAST_SETJMP(j) _setjmp (j)
5777 # define FAST_LONGJMP _longjmp
5778 # else
5779 # define FAST_SETJMP(j) setjmp (j)
5780 # define FAST_LONGJMP longjmp
5781 # endif
5783 # if PNG_LIBPNG_VER < 10500
5784 # define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1)
5785 # define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5786 # else
5787 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5788 # define PNG_LONGJMP(ptr) png_longjmp (ptr, 1)
5789 # define PNG_JMPBUF(ptr) \
5790 (*png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf)))
5791 # endif
5793 /* Error and warning handlers installed when the PNG library
5794 is initialized. */
5796 static _Noreturn void
5797 my_png_error (png_struct *png_ptr, const char *msg)
5799 eassert (png_ptr != NULL);
5800 /* Avoid compiler warning about deprecated direct access to
5801 png_ptr's fields in libpng versions 1.4.x. */
5802 image_error ("PNG error: %s", build_string (msg));
5803 PNG_LONGJMP (png_ptr);
5807 static void
5808 my_png_warning (png_struct *png_ptr, const char *msg)
5810 eassert (png_ptr != NULL);
5811 image_error ("PNG warning: %s", build_string (msg));
5814 /* Memory source for PNG decoding. */
5816 struct png_memory_storage
5818 unsigned char *bytes; /* The data */
5819 ptrdiff_t len; /* How big is it? */
5820 ptrdiff_t index; /* Where are we? */
5824 /* Function set as reader function when reading PNG image from memory.
5825 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5826 bytes from the input to DATA. */
5828 static void
5829 png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length)
5831 struct png_memory_storage *tbr = png_get_io_ptr (png_ptr);
5833 if (length > tbr->len - tbr->index)
5834 png_error (png_ptr, "Read error");
5836 memcpy (data, tbr->bytes + tbr->index, length);
5837 tbr->index = tbr->index + length;
5841 /* Function set as reader function when reading PNG image from a file.
5842 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5843 bytes from the input to DATA. */
5845 static void
5846 png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length)
5848 FILE *fp = png_get_io_ptr (png_ptr);
5850 if (fread (data, 1, length, fp) < length)
5851 png_error (png_ptr, "Read error");
5855 /* Load PNG image IMG for use on frame F. Value is true if
5856 successful. */
5858 struct png_load_context
5860 /* These are members so that longjmp doesn't munge local variables. */
5861 png_struct *png_ptr;
5862 png_info *info_ptr;
5863 png_info *end_info;
5864 FILE *fp;
5865 png_byte *pixels;
5866 png_byte **rows;
5869 static bool
5870 png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
5872 Lisp_Object specified_file;
5873 Lisp_Object specified_data;
5874 int x, y;
5875 ptrdiff_t i;
5876 png_struct *png_ptr;
5877 png_info *info_ptr = NULL, *end_info = NULL;
5878 FILE *fp = NULL;
5879 png_byte sig[8];
5880 png_byte *pixels = NULL;
5881 png_byte **rows = NULL;
5882 png_uint_32 width, height;
5883 int bit_depth, color_type, interlace_type;
5884 png_byte channels;
5885 png_uint_32 row_bytes;
5886 bool transparent_p;
5887 struct png_memory_storage tbr; /* Data to be read */
5889 #ifdef USE_CAIRO
5890 unsigned char *data = 0;
5891 uint32_t *dataptr;
5892 #else
5893 XImagePtr ximg, mask_img = NULL;
5894 #endif
5896 /* Find out what file to load. */
5897 specified_file = image_spec_value (img->spec, QCfile, NULL);
5898 specified_data = image_spec_value (img->spec, QCdata, NULL);
5899 IF_LINT (Lisp_Object volatile specified_data_volatile = specified_data);
5901 if (NILP (specified_data))
5903 int fd;
5904 Lisp_Object file = x_find_image_fd (specified_file, &fd);
5905 if (!STRINGP (file))
5907 image_error ("Cannot find image file `%s'", specified_file);
5908 return 0;
5911 /* Open the image file. */
5912 fp = fdopen (fd, "rb");
5913 if (!fp)
5915 image_error ("Cannot open image file `%s'", file);
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);
5925 return 0;
5928 else
5930 if (!STRINGP (specified_data))
5932 image_error ("Invalid image data `%s'", specified_data);
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);
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_size_error ();
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 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 int fd;
6669 Lisp_Object file = x_find_image_fd (specified_file, &fd);
6670 if (!STRINGP (file))
6672 image_error ("Cannot find image file `%s'", specified_file);
6673 return 0;
6676 fp = fdopen (fd, "rb");
6677 if (fp == NULL)
6679 image_error ("Cannot open `%s'", file);
6680 return 0;
6683 else if (!STRINGP (specified_data))
6685 image_error ("Invalid image data `%s'", specified_data);
6686 return 0;
6689 /* Customize libjpeg's error handling to call my_error_exit when an
6690 error is detected. This function will perform a longjmp. */
6691 mgr->cinfo.err = jpeg_std_error (&mgr->pub);
6692 mgr->pub.error_exit = my_error_exit;
6693 if (sys_setjmp (mgr->setjmp_buffer))
6695 switch (mgr->failure_code)
6697 case MY_JPEG_ERROR_EXIT:
6699 char buf[JMSG_LENGTH_MAX];
6700 mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf);
6701 image_error ("Error reading JPEG image `%s': %s",
6702 img->spec, build_string (buf));
6703 break;
6706 case MY_JPEG_INVALID_IMAGE_SIZE:
6707 image_size_error ();
6708 break;
6710 case MY_JPEG_CANNOT_CREATE_X:
6711 break;
6714 /* Close the input file and destroy the JPEG object. */
6715 if (fp)
6716 fclose (fp);
6717 jpeg_destroy_decompress (&mgr->cinfo);
6719 /* If we already have an XImage, free that. */
6720 #ifndef USE_CAIRO
6721 x_destroy_x_image (ximg);
6722 #endif
6723 /* Free pixmap and colors. */
6724 x_clear_image (f, img);
6725 return 0;
6728 /* Silence a bogus diagnostic; see GCC bug 54561. */
6729 IF_LINT (specified_data = specified_data_volatile);
6731 /* Create the JPEG decompression object. Let it read from fp.
6732 Read the JPEG image header. */
6733 jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo);
6735 if (NILP (specified_data))
6736 jpeg_file_src (&mgr->cinfo, fp);
6737 else
6738 jpeg_memory_src (&mgr->cinfo, SDATA (specified_data),
6739 SBYTES (specified_data));
6741 jpeg_read_header (&mgr->cinfo, 1);
6743 /* Customize decompression so that color quantization will be used.
6744 Start decompression. */
6745 mgr->cinfo.quantize_colors = 1;
6746 jpeg_start_decompress (&mgr->cinfo);
6747 width = img->width = mgr->cinfo.output_width;
6748 height = img->height = mgr->cinfo.output_height;
6750 if (!check_image_size (f, width, height))
6752 mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE;
6753 sys_longjmp (mgr->setjmp_buffer, 1);
6756 #ifndef USE_CAIRO
6757 /* Create X image and pixmap. */
6758 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
6760 mgr->failure_code = MY_JPEG_CANNOT_CREATE_X;
6761 sys_longjmp (mgr->setjmp_buffer, 1);
6763 #endif
6765 /* Allocate colors. When color quantization is used,
6766 mgr->cinfo.actual_number_of_colors has been set with the number of
6767 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6768 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6769 No more than 255 colors will be generated. */
6770 USE_SAFE_ALLOCA;
6772 if (mgr->cinfo.out_color_components > 2)
6773 ir = 0, ig = 1, ib = 2;
6774 else if (mgr->cinfo.out_color_components > 1)
6775 ir = 0, ig = 1, ib = 0;
6776 else
6777 ir = 0, ig = 0, ib = 0;
6779 #ifndef CAIRO
6780 /* Use the color table mechanism because it handles colors that
6781 cannot be allocated nicely. Such colors will be replaced with
6782 a default color, and we don't have to care about which colors
6783 can be freed safely, and which can't. */
6784 init_color_table ();
6785 SAFE_NALLOCA (colors, 1, mgr->cinfo.actual_number_of_colors);
6787 for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i)
6789 /* Multiply RGB values with 255 because X expects RGB values
6790 in the range 0..0xffff. */
6791 int r = mgr->cinfo.colormap[ir][i] << 8;
6792 int g = mgr->cinfo.colormap[ig][i] << 8;
6793 int b = mgr->cinfo.colormap[ib][i] << 8;
6794 colors[i] = lookup_rgb_color (f, r, g, b);
6796 #endif
6798 #ifdef COLOR_TABLE_SUPPORT
6799 /* Remember those colors actually allocated. */
6800 img->colors = colors_in_color_table (&img->ncolors);
6801 free_color_table ();
6802 #endif /* COLOR_TABLE_SUPPORT */
6805 /* Read pixels. */
6806 row_stride = width * mgr->cinfo.output_components;
6807 buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo,
6808 JPOOL_IMAGE, row_stride, 1);
6809 #ifdef USE_CAIRO
6811 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
6812 uint32_t *dataptr = (uint32_t *) data;
6813 int r, g, b;
6815 for (y = 0; y < height; ++y)
6817 jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
6819 for (x = 0; x < width; ++x)
6821 i = buffer[0][x];
6822 r = mgr->cinfo.colormap[ir][i];
6823 g = mgr->cinfo.colormap[ig][i];
6824 b = mgr->cinfo.colormap[ib][i];
6825 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
6829 create_cairo_image_surface (img, data, width, height);
6831 #else
6832 for (y = 0; y < height; ++y)
6834 jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
6835 for (x = 0; x < mgr->cinfo.output_width; ++x)
6836 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
6838 #endif
6840 /* Clean up. */
6841 jpeg_finish_decompress (&mgr->cinfo);
6842 jpeg_destroy_decompress (&mgr->cinfo);
6843 if (fp)
6844 fclose (fp);
6846 #ifndef USE_CAIRO
6847 /* Maybe fill in the background field while we have ximg handy. */
6848 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6849 /* Casting avoids a GCC warning. */
6850 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6852 /* Put ximg into the image. */
6853 image_put_x_image (f, img, ximg, 0);
6854 #endif
6855 SAFE_FREE ();
6856 return 1;
6859 static bool
6860 jpeg_load (struct frame *f, struct image *img)
6862 struct my_jpeg_error_mgr mgr;
6863 return jpeg_load_body (f, img, &mgr);
6866 #else /* HAVE_JPEG */
6868 #ifdef HAVE_NS
6869 static bool
6870 jpeg_load (struct frame *f, struct image *img)
6872 return ns_load_image (f, img,
6873 image_spec_value (img->spec, QCfile, NULL),
6874 image_spec_value (img->spec, QCdata, NULL));
6876 #endif /* HAVE_NS */
6878 #endif /* !HAVE_JPEG */
6882 /***********************************************************************
6883 TIFF
6884 ***********************************************************************/
6886 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6888 static bool tiff_image_p (Lisp_Object object);
6889 static bool tiff_load (struct frame *f, struct image *img);
6891 /* Indices of image specification fields in tiff_format, below. */
6893 enum tiff_keyword_index
6895 TIFF_TYPE,
6896 TIFF_DATA,
6897 TIFF_FILE,
6898 TIFF_ASCENT,
6899 TIFF_MARGIN,
6900 TIFF_RELIEF,
6901 TIFF_ALGORITHM,
6902 TIFF_HEURISTIC_MASK,
6903 TIFF_MASK,
6904 TIFF_BACKGROUND,
6905 TIFF_INDEX,
6906 TIFF_LAST
6909 /* Vector of image_keyword structures describing the format
6910 of valid user-defined image specifications. */
6912 static const struct image_keyword tiff_format[TIFF_LAST] =
6914 {":type", IMAGE_SYMBOL_VALUE, 1},
6915 {":data", IMAGE_STRING_VALUE, 0},
6916 {":file", IMAGE_STRING_VALUE, 0},
6917 {":ascent", IMAGE_ASCENT_VALUE, 0},
6918 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
6919 {":relief", IMAGE_INTEGER_VALUE, 0},
6920 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6921 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6922 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6923 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
6924 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
6927 #if defined HAVE_NTGUI && defined WINDOWSNT
6928 static bool init_tiff_functions (void);
6929 #else
6930 #define init_tiff_functions NULL
6931 #endif
6933 /* Structure describing the image type `tiff'. */
6935 static struct image_type tiff_type =
6937 SYMBOL_INDEX (Qtiff),
6938 tiff_image_p,
6939 tiff_load,
6940 x_clear_image,
6941 init_tiff_functions,
6942 NULL
6945 /* Return true if OBJECT is a valid TIFF image specification. */
6947 static bool
6948 tiff_image_p (Lisp_Object object)
6950 struct image_keyword fmt[TIFF_LAST];
6951 memcpy (fmt, tiff_format, sizeof fmt);
6953 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
6954 return 0;
6956 /* Must specify either the :data or :file keyword. */
6957 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
6960 #endif /* HAVE_TIFF || HAVE_NS */
6962 #ifdef HAVE_TIFF
6964 # include <tiffio.h>
6966 # ifdef WINDOWSNT
6968 /* TIFF library details. */
6969 DEF_DLL_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler));
6970 DEF_DLL_FN (TIFFErrorHandler, TIFFSetWarningHandler, (TIFFErrorHandler));
6971 DEF_DLL_FN (TIFF *, TIFFOpen, (const char *, const char *));
6972 DEF_DLL_FN (TIFF *, TIFFClientOpen,
6973 (const char *, const char *, thandle_t, TIFFReadWriteProc,
6974 TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc,
6975 TIFFMapFileProc, TIFFUnmapFileProc));
6976 DEF_DLL_FN (int, TIFFGetField, (TIFF *, ttag_t, ...));
6977 DEF_DLL_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int));
6978 DEF_DLL_FN (void, TIFFClose, (TIFF *));
6979 DEF_DLL_FN (int, TIFFSetDirectory, (TIFF *, tdir_t));
6981 static bool
6982 init_tiff_functions (void)
6984 HMODULE library;
6986 if (!(library = w32_delayed_load (Qtiff)))
6987 return 0;
6989 LOAD_DLL_FN (library, TIFFSetErrorHandler);
6990 LOAD_DLL_FN (library, TIFFSetWarningHandler);
6991 LOAD_DLL_FN (library, TIFFOpen);
6992 LOAD_DLL_FN (library, TIFFClientOpen);
6993 LOAD_DLL_FN (library, TIFFGetField);
6994 LOAD_DLL_FN (library, TIFFReadRGBAImage);
6995 LOAD_DLL_FN (library, TIFFClose);
6996 LOAD_DLL_FN (library, TIFFSetDirectory);
6997 return 1;
7000 # undef TIFFClientOpen
7001 # undef TIFFClose
7002 # undef TIFFGetField
7003 # undef TIFFOpen
7004 # undef TIFFReadRGBAImage
7005 # undef TIFFSetDirectory
7006 # undef TIFFSetErrorHandler
7007 # undef TIFFSetWarningHandler
7009 # define TIFFClientOpen fn_TIFFClientOpen
7010 # define TIFFClose fn_TIFFClose
7011 # define TIFFGetField fn_TIFFGetField
7012 # define TIFFOpen fn_TIFFOpen
7013 # define TIFFReadRGBAImage fn_TIFFReadRGBAImage
7014 # define TIFFSetDirectory fn_TIFFSetDirectory
7015 # define TIFFSetErrorHandler fn_TIFFSetErrorHandler
7016 # define TIFFSetWarningHandler fn_TIFFSetWarningHandler
7018 # endif /* WINDOWSNT */
7021 /* Reading from a memory buffer for TIFF images Based on the PNG
7022 memory source, but we have to provide a lot of extra functions.
7023 Blah.
7025 We really only need to implement read and seek, but I am not
7026 convinced that the TIFF library is smart enough not to destroy
7027 itself if we only hand it the function pointers we need to
7028 override. */
7030 typedef struct
7032 unsigned char *bytes;
7033 ptrdiff_t len;
7034 ptrdiff_t index;
7036 tiff_memory_source;
7038 static tsize_t
7039 tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size)
7041 tiff_memory_source *src = (tiff_memory_source *) data;
7043 size = min (size, src->len - src->index);
7044 memcpy (buf, src->bytes + src->index, size);
7045 src->index += size;
7046 return size;
7049 static tsize_t
7050 tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size)
7052 return -1;
7055 static toff_t
7056 tiff_seek_in_memory (thandle_t data, toff_t off, int whence)
7058 tiff_memory_source *src = (tiff_memory_source *) data;
7059 ptrdiff_t idx;
7061 switch (whence)
7063 case SEEK_SET: /* Go from beginning of source. */
7064 idx = off;
7065 break;
7067 case SEEK_END: /* Go from end of source. */
7068 idx = src->len + off;
7069 break;
7071 case SEEK_CUR: /* Go from current position. */
7072 idx = src->index + off;
7073 break;
7075 default: /* Invalid `whence'. */
7076 return -1;
7079 if (idx > src->len || idx < 0)
7080 return -1;
7082 src->index = idx;
7083 return src->index;
7086 static int
7087 tiff_close_memory (thandle_t data)
7089 /* NOOP */
7090 return 0;
7093 static int
7094 tiff_mmap_memory (thandle_t data, tdata_t *pbase, toff_t *psize)
7096 /* It is already _IN_ memory. */
7097 return 0;
7100 static void
7101 tiff_unmap_memory (thandle_t data, tdata_t base, toff_t size)
7103 /* We don't need to do this. */
7106 static toff_t
7107 tiff_size_of_memory (thandle_t data)
7109 return ((tiff_memory_source *) data)->len;
7112 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
7113 compiler error compiling tiff_handler, see Bugzilla bug #17406
7114 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
7115 this function as external works around that problem. */
7116 # if defined (__MINGW32__) && __GNUC__ == 3
7117 # define MINGW_STATIC
7118 # else
7119 # define MINGW_STATIC static
7120 # endif
7122 MINGW_STATIC void
7123 tiff_handler (const char *, const char *, const char *, va_list)
7124 ATTRIBUTE_FORMAT_PRINTF (3, 0);
7125 MINGW_STATIC void
7126 tiff_handler (const char *log_format, const char *title,
7127 const char *format, va_list ap)
7129 /* doprnt is not suitable here, as TIFF handlers are called from
7130 libtiff and are passed arbitrary printf directives. Instead, use
7131 vsnprintf, taking care to be portable to nonstandard environments
7132 where vsnprintf returns -1 on buffer overflow. Since it's just a
7133 log entry, it's OK to truncate it. */
7134 char buf[4000];
7135 int len = vsnprintf (buf, sizeof buf, format, ap);
7136 add_to_log (log_format, build_string (title),
7137 make_string (buf, max (0, min (len, sizeof buf - 1))));
7139 # undef MINGW_STATIC
7141 static void tiff_error_handler (const char *, const char *, va_list)
7142 ATTRIBUTE_FORMAT_PRINTF (2, 0);
7143 static void
7144 tiff_error_handler (const char *title, const char *format, va_list ap)
7146 tiff_handler ("TIFF error: %s %s", title, format, ap);
7150 static void tiff_warning_handler (const char *, const char *, va_list)
7151 ATTRIBUTE_FORMAT_PRINTF (2, 0);
7152 static void
7153 tiff_warning_handler (const char *title, const char *format, va_list ap)
7155 tiff_handler ("TIFF warning: %s %s", title, format, ap);
7159 /* Load TIFF image IMG for use on frame F. Value is true if
7160 successful. */
7162 static bool
7163 tiff_load (struct frame *f, struct image *img)
7165 Lisp_Object specified_file;
7166 Lisp_Object specified_data;
7167 TIFF *tiff;
7168 int width, height, x, y, count;
7169 uint32 *buf;
7170 int rc;
7171 XImagePtr ximg;
7172 tiff_memory_source memsrc;
7173 Lisp_Object image;
7175 specified_file = image_spec_value (img->spec, QCfile, NULL);
7176 specified_data = image_spec_value (img->spec, QCdata, NULL);
7178 TIFFSetErrorHandler ((TIFFErrorHandler) tiff_error_handler);
7179 TIFFSetWarningHandler ((TIFFErrorHandler) tiff_warning_handler);
7181 if (NILP (specified_data))
7183 /* Read from a file */
7184 Lisp_Object file = x_find_image_file (specified_file);
7185 if (!STRINGP (file))
7187 image_error ("Cannot find image file `%s'", specified_file);
7188 return 0;
7191 Lisp_Object encoded_file = ENCODE_FILE (file);
7192 # ifdef WINDOWSNT
7193 encoded_file = ansi_encode_filename (encoded_file);
7194 # endif
7196 /* Try to open the image file. */
7197 tiff = TIFFOpen (SSDATA (encoded_file), "r");
7198 if (tiff == NULL)
7200 image_error ("Cannot open `%s'", file);
7201 return 0;
7204 else
7206 if (!STRINGP (specified_data))
7208 image_error ("Invalid image data `%s'", specified_data);
7209 return 0;
7212 /* Memory source! */
7213 memsrc.bytes = SDATA (specified_data);
7214 memsrc.len = SBYTES (specified_data);
7215 memsrc.index = 0;
7217 tiff = TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc,
7218 tiff_read_from_memory,
7219 tiff_write_from_memory,
7220 tiff_seek_in_memory,
7221 tiff_close_memory,
7222 tiff_size_of_memory,
7223 tiff_mmap_memory,
7224 tiff_unmap_memory);
7226 if (!tiff)
7228 image_error ("Cannot open memory source for `%s'", img->spec);
7229 return 0;
7233 image = image_spec_value (img->spec, QCindex, NULL);
7234 if (INTEGERP (image))
7236 EMACS_INT ino = XFASTINT (image);
7237 if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
7238 && TIFFSetDirectory (tiff, ino)))
7240 image_error ("Invalid image number `%s' in image `%s'",
7241 image, img->spec);
7242 TIFFClose (tiff);
7243 return 0;
7247 /* Get width and height of the image, and allocate a raster buffer
7248 of width x height 32-bit values. */
7249 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
7250 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
7252 if (!check_image_size (f, width, height))
7254 image_size_error ();
7255 TIFFClose (tiff);
7256 return 0;
7259 /* Create the X image and pixmap. */
7260 if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width
7261 && image_create_x_image_and_pixmap (f, img, width, height, 0,
7262 &ximg, 0)))
7264 TIFFClose (tiff);
7265 return 0;
7268 buf = xmalloc (sizeof *buf * width * height);
7270 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
7272 /* Count the number of images in the file. */
7273 for (count = 1; TIFFSetDirectory (tiff, count); count++)
7274 continue;
7276 if (count > 1)
7277 img->lisp_data = Fcons (Qcount,
7278 Fcons (make_number (count),
7279 img->lisp_data));
7281 TIFFClose (tiff);
7282 if (!rc)
7284 image_error ("Error reading TIFF image `%s'", img->spec);
7285 xfree (buf);
7286 return 0;
7289 #ifdef USE_CAIRO
7291 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
7292 uint32_t *dataptr = (uint32_t *) data;
7293 int r, g, b, a;
7295 for (y = 0; y < height; ++y)
7297 uint32 *row = buf + (height - 1 - y) * width;
7298 for (x = 0; x < width; ++x)
7300 uint32 abgr = row[x];
7301 int r = TIFFGetR (abgr);
7302 int g = TIFFGetG (abgr);
7303 int b = TIFFGetB (abgr);
7304 int a = TIFFGetA (abgr);
7305 *dataptr++ = (a << 24) | (r << 16) | (g << 8) | b;
7309 create_cairo_image_surface (img, data, width, height);
7311 #else
7312 /* Initialize the color table. */
7313 init_color_table ();
7315 /* Process the pixel raster. Origin is in the lower-left corner. */
7316 for (y = 0; y < height; ++y)
7318 uint32 *row = buf + y * width;
7320 for (x = 0; x < width; ++x)
7322 uint32 abgr = row[x];
7323 int r = TIFFGetR (abgr) << 8;
7324 int g = TIFFGetG (abgr) << 8;
7325 int b = TIFFGetB (abgr) << 8;
7326 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
7330 # ifdef COLOR_TABLE_SUPPORT
7331 /* Remember the colors allocated for the image. Free the color table. */
7332 img->colors = colors_in_color_table (&img->ncolors);
7333 free_color_table ();
7334 # endif /* COLOR_TABLE_SUPPORT */
7336 img->width = width;
7337 img->height = height;
7339 /* Maybe fill in the background field while we have ximg handy. */
7340 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7341 /* Casting avoids a GCC warning on W32. */
7342 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7344 /* Put ximg into the image. */
7345 image_put_x_image (f, img, ximg, 0);
7347 #endif /* ! USE_CAIRO */
7349 xfree (buf);
7350 return 1;
7353 #elif defined HAVE_NS
7355 static bool
7356 tiff_load (struct frame *f, struct image *img)
7358 return ns_load_image (f, img,
7359 image_spec_value (img->spec, QCfile, NULL),
7360 image_spec_value (img->spec, QCdata, NULL));
7363 #endif
7367 /***********************************************************************
7369 ***********************************************************************/
7371 #if defined (HAVE_GIF) || defined (HAVE_NS)
7373 static bool gif_image_p (Lisp_Object object);
7374 static bool gif_load (struct frame *f, struct image *img);
7375 static void gif_clear_image (struct frame *f, struct image *img);
7377 /* Indices of image specification fields in gif_format, below. */
7379 enum gif_keyword_index
7381 GIF_TYPE,
7382 GIF_DATA,
7383 GIF_FILE,
7384 GIF_ASCENT,
7385 GIF_MARGIN,
7386 GIF_RELIEF,
7387 GIF_ALGORITHM,
7388 GIF_HEURISTIC_MASK,
7389 GIF_MASK,
7390 GIF_IMAGE,
7391 GIF_BACKGROUND,
7392 GIF_LAST
7395 /* Vector of image_keyword structures describing the format
7396 of valid user-defined image specifications. */
7398 static const struct image_keyword gif_format[GIF_LAST] =
7400 {":type", IMAGE_SYMBOL_VALUE, 1},
7401 {":data", IMAGE_STRING_VALUE, 0},
7402 {":file", IMAGE_STRING_VALUE, 0},
7403 {":ascent", IMAGE_ASCENT_VALUE, 0},
7404 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
7405 {":relief", IMAGE_INTEGER_VALUE, 0},
7406 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7407 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7408 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7409 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
7410 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7413 #if defined HAVE_NTGUI && defined WINDOWSNT
7414 static bool init_gif_functions (void);
7415 #else
7416 #define init_gif_functions NULL
7417 #endif
7419 /* Structure describing the image type `gif'. */
7421 static struct image_type gif_type =
7423 SYMBOL_INDEX (Qgif),
7424 gif_image_p,
7425 gif_load,
7426 gif_clear_image,
7427 init_gif_functions,
7428 NULL
7431 /* Free X resources of GIF image IMG which is used on frame F. */
7433 static void
7434 gif_clear_image (struct frame *f, struct image *img)
7436 img->lisp_data = Qnil;
7437 x_clear_image (f, img);
7440 /* Return true if OBJECT is a valid GIF image specification. */
7442 static bool
7443 gif_image_p (Lisp_Object object)
7445 struct image_keyword fmt[GIF_LAST];
7446 memcpy (fmt, gif_format, sizeof fmt);
7448 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
7449 return 0;
7451 /* Must specify either the :data or :file keyword. */
7452 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
7455 #endif /* HAVE_GIF */
7457 #ifdef HAVE_GIF
7459 # ifdef HAVE_NTGUI
7461 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7462 Undefine before redefining to avoid a preprocessor warning. */
7463 # ifdef DrawText
7464 # undef DrawText
7465 # endif
7466 /* avoid conflict with QuickdrawText.h */
7467 # define DrawText gif_DrawText
7468 # include <gif_lib.h>
7469 # undef DrawText
7471 /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */
7472 # ifndef GIFLIB_MINOR
7473 # define GIFLIB_MINOR 0
7474 # endif
7475 # ifndef GIFLIB_RELEASE
7476 # define GIFLIB_RELEASE 0
7477 # endif
7479 # else /* HAVE_NTGUI */
7481 # include <gif_lib.h>
7483 # endif /* HAVE_NTGUI */
7485 /* Giflib before 5.0 didn't define these macros. */
7486 # ifndef GIFLIB_MAJOR
7487 # define GIFLIB_MAJOR 4
7488 # endif
7490 /* GifErrorString is declared to return char const * when GIFLIB_MAJOR
7491 and GIFLIB_MINOR indicate 5.1 or later. Do not bother using it in
7492 earlier releases, where either it returns char * or GIFLIB_MINOR
7493 may be incorrect. */
7494 # define HAVE_GIFERRORSTRING (5 < GIFLIB_MAJOR + (1 <= GIFLIB_MINOR))
7496 # ifdef WINDOWSNT
7498 /* GIF library details. */
7499 # if GIFLIB_MAJOR + (GIFLIB_MINOR >= 1) > 5
7500 DEF_DLL_FN (int, DGifCloseFile, (GifFileType *, int *));
7501 # else
7502 DEF_DLL_FN (int, DGifCloseFile, (GifFileType *));
7503 # endif
7504 DEF_DLL_FN (int, DGifSlurp, (GifFileType *));
7505 # if GIFLIB_MAJOR < 5
7506 DEF_DLL_FN (GifFileType *, DGifOpen, (void *, InputFunc));
7507 DEF_DLL_FN (GifFileType *, DGifOpenFileName, (const char *));
7508 # else
7509 DEF_DLL_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *));
7510 DEF_DLL_FN (GifFileType *, DGifOpenFileName, (const char *, int *));
7511 # endif
7512 # if HAVE_GIFERRORSTRING
7513 DEF_DLL_FN (char const *, GifErrorString, (int));
7514 # endif
7516 static bool
7517 init_gif_functions (void)
7519 HMODULE library;
7521 if (!(library = w32_delayed_load (Qgif)))
7522 return 0;
7524 LOAD_DLL_FN (library, DGifCloseFile);
7525 LOAD_DLL_FN (library, DGifSlurp);
7526 LOAD_DLL_FN (library, DGifOpen);
7527 LOAD_DLL_FN (library, DGifOpenFileName);
7528 # if HAVE_GIFERRORSTRING
7529 LOAD_DLL_FN (library, GifErrorString);
7530 # endif
7531 return 1;
7534 # undef DGifCloseFile
7535 # undef DGifOpen
7536 # undef DGifOpenFileName
7537 # undef DGifSlurp
7538 # undef GifErrorString
7540 # define DGifCloseFile fn_DGifCloseFile
7541 # define DGifOpen fn_DGifOpen
7542 # define DGifOpenFileName fn_DGifOpenFileName
7543 # define DGifSlurp fn_DGifSlurp
7544 # define GifErrorString fn_GifErrorString
7546 # endif /* WINDOWSNT */
7548 /* Reading a GIF image from memory
7549 Based on the PNG memory stuff to a certain extent. */
7551 typedef struct
7553 unsigned char *bytes;
7554 ptrdiff_t len;
7555 ptrdiff_t index;
7557 gif_memory_source;
7559 /* Make the current memory source available to gif_read_from_memory.
7560 It's done this way because not all versions of libungif support
7561 a UserData field in the GifFileType structure. */
7562 static gif_memory_source *current_gif_memory_src;
7564 static int
7565 gif_read_from_memory (GifFileType *file, GifByteType *buf, int len)
7567 gif_memory_source *src = current_gif_memory_src;
7569 if (len > src->len - src->index)
7570 return -1;
7572 memcpy (buf, src->bytes + src->index, len);
7573 src->index += len;
7574 return len;
7577 static int
7578 gif_close (GifFileType *gif, int *err)
7580 int retval;
7582 #if GIFLIB_MAJOR + (GIFLIB_MINOR >= 1) > 5
7583 retval = DGifCloseFile (gif, err);
7584 #else
7585 retval = DGifCloseFile (gif);
7586 #if GIFLIB_MAJOR >= 5
7587 if (err)
7588 *err = gif->Error;
7589 #endif
7590 #endif
7591 return retval;
7594 /* Load GIF image IMG for use on frame F. Value is true if
7595 successful. */
7597 static const int interlace_start[] = {0, 4, 2, 1};
7598 static const int interlace_increment[] = {8, 8, 4, 2};
7600 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7602 static bool
7603 gif_load (struct frame *f, struct image *img)
7605 int rc, width, height, x, y, i, j;
7606 ColorMapObject *gif_color_map;
7607 unsigned long pixel_colors[256];
7608 GifFileType *gif;
7609 gif_memory_source memsrc;
7610 Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
7611 Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
7612 Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
7613 unsigned long bgcolor = 0;
7614 EMACS_INT idx;
7615 int gif_err;
7617 #ifdef USE_CAIRO
7618 unsigned char *data = 0;
7619 #else
7620 XImagePtr ximg;
7621 #endif
7623 if (NILP (specified_data))
7625 Lisp_Object file = x_find_image_file (specified_file);
7626 if (!STRINGP (file))
7628 image_error ("Cannot find image file `%s'", specified_file);
7629 return 0;
7632 Lisp_Object encoded_file = ENCODE_FILE (file);
7633 #ifdef WINDOWSNT
7634 encoded_file = ansi_encode_filename (encoded_file);
7635 #endif
7637 /* Open the GIF file. */
7638 #if GIFLIB_MAJOR < 5
7639 gif = DGifOpenFileName (SSDATA (encoded_file));
7640 #else
7641 gif = DGifOpenFileName (SSDATA (encoded_file), &gif_err);
7642 #endif
7643 if (gif == NULL)
7645 #if HAVE_GIFERRORSTRING
7646 image_error ("Cannot open `%s': %s",
7647 file, build_string (GifErrorString (gif_err)));
7648 #else
7649 image_error ("Cannot open `%s'", file);
7650 #endif
7651 return 0;
7654 else
7656 if (!STRINGP (specified_data))
7658 image_error ("Invalid image data `%s'", specified_data);
7659 return 0;
7662 /* Read from memory! */
7663 current_gif_memory_src = &memsrc;
7664 memsrc.bytes = SDATA (specified_data);
7665 memsrc.len = SBYTES (specified_data);
7666 memsrc.index = 0;
7668 #if GIFLIB_MAJOR < 5
7669 gif = DGifOpen (&memsrc, gif_read_from_memory);
7670 #else
7671 gif = DGifOpen (&memsrc, gif_read_from_memory, &gif_err);
7672 #endif
7673 if (!gif)
7675 #if HAVE_GIFERRORSTRING
7676 image_error ("Cannot open memory source `%s': %s",
7677 img->spec, build_string (GifErrorString (gif_err)));
7678 #else
7679 image_error ("Cannot open memory source `%s'", img->spec);
7680 #endif
7681 return 0;
7685 /* Before reading entire contents, check the declared image size. */
7686 if (!check_image_size (f, gif->SWidth, gif->SHeight))
7688 image_size_error ();
7689 gif_close (gif, NULL);
7690 return 0;
7693 /* Read entire contents. */
7694 rc = DGifSlurp (gif);
7695 if (rc == GIF_ERROR || gif->ImageCount <= 0)
7697 image_error ("Error reading `%s'", img->spec);
7698 gif_close (gif, NULL);
7699 return 0;
7702 /* Which sub-image are we to display? */
7704 Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL);
7705 idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0;
7706 if (idx < 0 || idx >= gif->ImageCount)
7708 image_error ("Invalid image number `%s' in image `%s'",
7709 image_number, img->spec);
7710 gif_close (gif, NULL);
7711 return 0;
7715 width = img->width = gif->SWidth;
7716 height = img->height = gif->SHeight;
7718 img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top;
7719 img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left;
7720 img->corners[BOT_CORNER]
7721 = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height;
7722 img->corners[RIGHT_CORNER]
7723 = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width;
7725 if (!check_image_size (f, width, height))
7727 image_size_error ();
7728 gif_close (gif, NULL);
7729 return 0;
7732 /* Check that the selected subimages fit. It's not clear whether
7733 the GIF spec requires this, but Emacs can crash if they don't fit. */
7734 for (j = 0; j <= idx; ++j)
7736 struct SavedImage *subimage = gif->SavedImages + j;
7737 int subimg_width = subimage->ImageDesc.Width;
7738 int subimg_height = subimage->ImageDesc.Height;
7739 int subimg_top = subimage->ImageDesc.Top;
7740 int subimg_left = subimage->ImageDesc.Left;
7741 if (! (subimg_width >= 0 && subimg_height >= 0
7742 && 0 <= subimg_top && subimg_top <= height - subimg_height
7743 && 0 <= subimg_left && subimg_left <= width - subimg_width))
7745 image_error ("Subimage does not fit in image");
7746 gif_close (gif, NULL);
7747 return 0;
7751 #ifdef USE_CAIRO
7752 /* xzalloc so data is zero => transparent */
7753 data = (unsigned char *) xzalloc (width * height * 4);
7754 if (STRINGP (specified_bg))
7756 XColor color;
7757 if (x_defined_color (f, SSDATA (specified_bg), &color, 0))
7759 uint32_t *dataptr = (uint32_t *)data;
7760 int r = color.red/256;
7761 int g = color.green/256;
7762 int b = color.blue/256;
7764 for (y = 0; y < height; ++y)
7765 for (x = 0; x < width; ++x)
7766 *dataptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
7769 #else
7770 /* Create the X image and pixmap. */
7771 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
7773 gif_close (gif, NULL);
7774 return 0;
7777 /* Clear the part of the screen image not covered by the image.
7778 Full animated GIF support requires more here (see the gif89 spec,
7779 disposal methods). Let's simply assume that the part not covered
7780 by a sub-image is in the frame's background color. */
7781 for (y = 0; y < img->corners[TOP_CORNER]; ++y)
7782 for (x = 0; x < width; ++x)
7783 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7785 for (y = img->corners[BOT_CORNER]; y < height; ++y)
7786 for (x = 0; x < width; ++x)
7787 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7789 for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y)
7791 for (x = 0; x < img->corners[LEFT_CORNER]; ++x)
7792 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7793 for (x = img->corners[RIGHT_CORNER]; x < width; ++x)
7794 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7796 #endif
7798 /* Read the GIF image into the X image. */
7800 /* FIXME: With the current implementation, loading an animated gif
7801 is quadratic in the number of animation frames, since each frame
7802 is a separate struct image. We must provide a way for a single
7803 gif_load call to construct and save all animation frames. */
7805 init_color_table ();
7806 if (STRINGP (specified_bg))
7807 bgcolor = x_alloc_image_color (f, img, specified_bg,
7808 FRAME_BACKGROUND_PIXEL (f));
7809 for (j = 0; j <= idx; ++j)
7811 /* We use a local variable `raster' here because RasterBits is a
7812 char *, which invites problems with bytes >= 0x80. */
7813 struct SavedImage *subimage = gif->SavedImages + j;
7814 unsigned char *raster = (unsigned char *) subimage->RasterBits;
7815 int transparency_color_index = -1;
7816 int disposal = 0;
7817 int subimg_width = subimage->ImageDesc.Width;
7818 int subimg_height = subimage->ImageDesc.Height;
7819 int subimg_top = subimage->ImageDesc.Top;
7820 int subimg_left = subimage->ImageDesc.Left;
7822 /* Find the Graphic Control Extension block for this sub-image.
7823 Extract the disposal method and transparency color. */
7824 for (i = 0; i < subimage->ExtensionBlockCount; i++)
7826 ExtensionBlock *extblock = subimage->ExtensionBlocks + i;
7828 if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION)
7829 && extblock->ByteCount == 4
7830 && extblock->Bytes[0] & 1)
7832 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7833 to background". Treat any other value like 2. */
7834 disposal = (extblock->Bytes[0] >> 2) & 7;
7835 transparency_color_index = (unsigned char) extblock->Bytes[3];
7836 break;
7840 /* We can't "keep in place" the first subimage. */
7841 if (j == 0)
7842 disposal = 2;
7844 /* For disposal == 0, the spec says "No disposal specified. The
7845 decoder is not required to take any action." In practice, it
7846 seems we need to treat this like "keep in place", see e.g.
7847 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7848 if (disposal == 0)
7849 disposal = 1;
7851 gif_color_map = subimage->ImageDesc.ColorMap;
7852 if (!gif_color_map)
7853 gif_color_map = gif->SColorMap;
7855 #ifndef USE_CAIRO
7856 /* Allocate subimage colors. */
7857 memset (pixel_colors, 0, sizeof pixel_colors);
7859 if (gif_color_map)
7860 for (i = 0; i < gif_color_map->ColorCount; ++i)
7862 if (transparency_color_index == i)
7863 pixel_colors[i] = STRINGP (specified_bg)
7864 ? bgcolor : FRAME_BACKGROUND_PIXEL (f);
7865 else
7867 int r = gif_color_map->Colors[i].Red << 8;
7868 int g = gif_color_map->Colors[i].Green << 8;
7869 int b = gif_color_map->Colors[i].Blue << 8;
7870 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
7873 #endif
7875 /* Apply the pixel values. */
7876 if (GIFLIB_MAJOR < 5 && gif->SavedImages[j].ImageDesc.Interlace)
7878 int row, pass;
7880 for (y = 0, row = interlace_start[0], pass = 0;
7881 y < subimg_height;
7882 y++, row += interlace_increment[pass])
7884 while (subimg_height <= row)
7885 row = interlace_start[++pass];
7887 for (x = 0; x < subimg_width; x++)
7889 int c = raster[y * subimg_width + x];
7890 if (transparency_color_index != c || disposal != 1)
7892 #ifdef USE_CAIRO
7893 uint32_t *dataptr =
7894 ((uint32_t*)data + ((row + subimg_top) * subimg_width
7895 + x + subimg_left));
7896 int r = gif_color_map->Colors[c].Red;
7897 int g = gif_color_map->Colors[c].Green;
7898 int b = gif_color_map->Colors[c].Blue;
7900 if (transparency_color_index != c)
7901 *dataptr = (0xff << 24) | (r << 16) | (g << 8) | b;
7902 #else
7903 XPutPixel (ximg, x + subimg_left, row + subimg_top,
7904 pixel_colors[c]);
7905 #endif
7910 else
7912 for (y = 0; y < subimg_height; ++y)
7913 for (x = 0; x < subimg_width; ++x)
7915 int c = raster[y * subimg_width + x];
7916 if (transparency_color_index != c || disposal != 1)
7918 #ifdef USE_CAIRO
7919 uint32_t *dataptr =
7920 ((uint32_t*)data + ((y + subimg_top) * subimg_width
7921 + x + subimg_left));
7922 int r = gif_color_map->Colors[c].Red;
7923 int g = gif_color_map->Colors[c].Green;
7924 int b = gif_color_map->Colors[c].Blue;
7925 if (transparency_color_index != c)
7926 *dataptr = (0xff << 24) | (r << 16) | (g << 8) | b;
7927 #else
7928 XPutPixel (ximg, x + subimg_left, y + subimg_top,
7929 pixel_colors[c]);
7930 #endif
7936 #ifdef COLOR_TABLE_SUPPORT
7937 img->colors = colors_in_color_table (&img->ncolors);
7938 free_color_table ();
7939 #endif /* COLOR_TABLE_SUPPORT */
7941 /* Save GIF image extension data for `image-metadata'.
7942 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7943 img->lisp_data = Qnil;
7944 if (gif->SavedImages[idx].ExtensionBlockCount > 0)
7946 int delay = 0;
7947 ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks;
7948 for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++)
7949 /* Append (... FUNCTION "BYTES") */
7951 img->lisp_data
7952 = Fcons (make_number (ext->Function),
7953 Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
7954 img->lisp_data));
7955 if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION
7956 && ext->ByteCount == 4)
7958 delay = ext->Bytes[2] << CHAR_BIT;
7959 delay |= ext->Bytes[1];
7962 img->lisp_data = list2 (Qextension_data, img->lisp_data);
7963 if (delay)
7964 img->lisp_data
7965 = Fcons (Qdelay,
7966 Fcons (make_float (delay / 100.0),
7967 img->lisp_data));
7970 if (gif->ImageCount > 1)
7971 img->lisp_data = Fcons (Qcount,
7972 Fcons (make_number (gif->ImageCount),
7973 img->lisp_data));
7975 if (gif_close (gif, &gif_err) == GIF_ERROR)
7977 #if HAVE_GIFERRORSTRING
7978 char const *error_text = GifErrorString (gif_err);
7980 if (error_text)
7981 image_error ("Error closing `%s': %s",
7982 img->spec, build_string (error_text));
7983 #else
7984 image_error ("Error closing `%s'", img->spec);
7985 #endif
7988 #ifdef USE_CAIRO
7989 create_cairo_image_surface (img, data, width, height);
7990 #else
7991 /* Maybe fill in the background field while we have ximg handy. */
7992 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7993 /* Casting avoids a GCC warning. */
7994 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7996 /* Put ximg into the image. */
7997 image_put_x_image (f, img, ximg, 0);
7998 #endif
8000 return 1;
8003 #else /* !HAVE_GIF */
8005 #ifdef HAVE_NS
8006 static bool
8007 gif_load (struct frame *f, struct image *img)
8009 return ns_load_image (f, img,
8010 image_spec_value (img->spec, QCfile, NULL),
8011 image_spec_value (img->spec, QCdata, NULL));
8013 #endif /* HAVE_NS */
8015 #endif /* HAVE_GIF */
8018 #ifdef HAVE_IMAGEMAGICK
8020 /***********************************************************************
8021 ImageMagick
8022 ***********************************************************************/
8024 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
8025 safely rounded and clipped to int range. */
8027 static int
8028 scale_image_size (int size, size_t divisor, size_t multiplier)
8030 if (divisor != 0)
8032 double s = size;
8033 double scaled = s * multiplier / divisor + 0.5;
8034 if (scaled < INT_MAX)
8035 return scaled;
8037 return INT_MAX;
8040 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
8041 Use SPEC to deduce the size. Store the desired size into
8042 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
8043 static void
8044 compute_image_size (size_t width, size_t height,
8045 Lisp_Object spec,
8046 int *d_width, int *d_height)
8048 Lisp_Object value;
8049 int desired_width, desired_height;
8051 /* If width and/or height is set in the display spec assume we want
8052 to scale to those values. If either h or w is unspecified, the
8053 unspecified should be calculated from the specified to preserve
8054 aspect ratio. */
8055 value = image_spec_value (spec, QCwidth, NULL);
8056 desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
8057 value = image_spec_value (spec, QCheight, NULL);
8058 desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1;
8060 if (desired_width == -1)
8062 value = image_spec_value (spec, QCmax_width, NULL);
8063 if (NATNUMP (value))
8065 int max_width = min (XFASTINT (value), INT_MAX);
8066 if (max_width < width)
8068 /* The image is wider than :max-width. */
8069 desired_width = max_width;
8070 if (desired_height == -1)
8072 desired_height = scale_image_size (desired_width,
8073 width, height);
8074 value = image_spec_value (spec, QCmax_height, NULL);
8075 if (NATNUMP (value))
8077 int max_height = min (XFASTINT (value), INT_MAX);
8078 if (max_height < desired_height)
8080 desired_height = max_height;
8081 desired_width = scale_image_size (desired_height,
8082 height, width);
8090 if (desired_height == -1)
8092 value = image_spec_value (spec, QCmax_height, NULL);
8093 if (NATNUMP (value))
8095 int max_height = min (XFASTINT (value), INT_MAX);
8096 if (max_height < height)
8097 desired_height = max_height;
8101 if (desired_width != -1 && desired_height == -1)
8102 /* w known, calculate h. */
8103 desired_height = scale_image_size (desired_width, width, height);
8105 if (desired_width == -1 && desired_height != -1)
8106 /* h known, calculate w. */
8107 desired_width = scale_image_size (desired_height, height, width);
8109 *d_width = desired_width;
8110 *d_height = desired_height;
8113 static bool imagemagick_image_p (Lisp_Object);
8114 static bool imagemagick_load (struct frame *, struct image *);
8115 static void imagemagick_clear_image (struct frame *, struct image *);
8117 /* Indices of image specification fields in imagemagick_format. */
8119 enum imagemagick_keyword_index
8121 IMAGEMAGICK_TYPE,
8122 IMAGEMAGICK_DATA,
8123 IMAGEMAGICK_FILE,
8124 IMAGEMAGICK_ASCENT,
8125 IMAGEMAGICK_MARGIN,
8126 IMAGEMAGICK_RELIEF,
8127 IMAGEMAGICK_ALGORITHM,
8128 IMAGEMAGICK_HEURISTIC_MASK,
8129 IMAGEMAGICK_MASK,
8130 IMAGEMAGICK_BACKGROUND,
8131 IMAGEMAGICK_HEIGHT,
8132 IMAGEMAGICK_WIDTH,
8133 IMAGEMAGICK_MAX_HEIGHT,
8134 IMAGEMAGICK_MAX_WIDTH,
8135 IMAGEMAGICK_FORMAT,
8136 IMAGEMAGICK_ROTATION,
8137 IMAGEMAGICK_CROP,
8138 IMAGEMAGICK_LAST
8141 /* Vector of image_keyword structures describing the format
8142 of valid user-defined image specifications. */
8144 static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] =
8146 {":type", IMAGE_SYMBOL_VALUE, 1},
8147 {":data", IMAGE_STRING_VALUE, 0},
8148 {":file", IMAGE_STRING_VALUE, 0},
8149 {":ascent", IMAGE_ASCENT_VALUE, 0},
8150 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
8151 {":relief", IMAGE_INTEGER_VALUE, 0},
8152 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8153 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8154 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8155 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
8156 {":height", IMAGE_INTEGER_VALUE, 0},
8157 {":width", IMAGE_INTEGER_VALUE, 0},
8158 {":max-height", IMAGE_INTEGER_VALUE, 0},
8159 {":max-width", IMAGE_INTEGER_VALUE, 0},
8160 {":format", IMAGE_SYMBOL_VALUE, 0},
8161 {":rotation", IMAGE_NUMBER_VALUE, 0},
8162 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8165 #if defined HAVE_NTGUI && defined WINDOWSNT
8166 static bool init_imagemagick_functions (void);
8167 #else
8168 #define init_imagemagick_functions NULL
8169 #endif
8171 /* Structure describing the image type for any image handled via
8172 ImageMagick. */
8174 static struct image_type imagemagick_type =
8176 SYMBOL_INDEX (Qimagemagick),
8177 imagemagick_image_p,
8178 imagemagick_load,
8179 imagemagick_clear_image,
8180 init_imagemagick_functions,
8181 NULL
8184 /* Free X resources of imagemagick image IMG which is used on frame F. */
8186 static void
8187 imagemagick_clear_image (struct frame *f,
8188 struct image *img)
8190 x_clear_image (f, img);
8193 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
8194 this by calling parse_image_spec and supplying the keywords that
8195 identify the IMAGEMAGICK format. */
8197 static bool
8198 imagemagick_image_p (Lisp_Object object)
8200 struct image_keyword fmt[IMAGEMAGICK_LAST];
8201 memcpy (fmt, imagemagick_format, sizeof fmt);
8203 if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick))
8204 return 0;
8206 /* Must specify either the :data or :file keyword. */
8207 return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1;
8210 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
8211 Therefore rename the function so it doesn't collide with ImageMagick. */
8212 #define DrawRectangle DrawRectangleGif
8213 #include <wand/MagickWand.h>
8215 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
8216 Emacs seems to work fine with the hidden version, so unhide it. */
8217 #include <magick/version.h>
8218 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
8219 extern WandExport void PixelGetMagickColor (const PixelWand *,
8220 MagickPixelPacket *);
8221 #endif
8223 /* Log ImageMagick error message.
8224 Useful when a ImageMagick function returns the status `MagickFalse'. */
8226 static void
8227 imagemagick_error (MagickWand *wand)
8229 char *description;
8230 ExceptionType severity;
8232 description = MagickGetException (wand, &severity);
8233 image_error ("ImageMagick error: %s", build_string (description));
8234 MagickRelinquishMemory (description);
8237 /* Possibly give ImageMagick some extra help to determine the image
8238 type by supplying a "dummy" filename based on the Content-Type. */
8240 static char *
8241 imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent])
8243 Lisp_Object symbol = intern ("image-format-suffixes");
8244 Lisp_Object val = find_symbol_value (symbol);
8245 Lisp_Object format;
8247 if (! CONSP (val))
8248 return NULL;
8250 format = image_spec_value (spec, intern (":format"), NULL);
8251 val = Fcar_safe (Fcdr_safe (Fassq (format, val)));
8252 if (! STRINGP (val))
8253 return NULL;
8255 /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
8256 as ImageMagick would ignore the extra bytes anyway. */
8257 snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val));
8258 return hint_buffer;
8261 /* Animated images (e.g., GIF89a) are composed from one "master image"
8262 (which is the first one, and then there's a number of images that
8263 follow. If following images have non-transparent colors, these are
8264 composed "on top" of the master image. So, in general, one has to
8265 compute ann the preceding images to be able to display a particular
8266 sub-image.
8268 Computing all the preceding images is too slow, so we maintain a
8269 cache of previously computed images. We have to maintain a cache
8270 separate from the image cache, because the images may be scaled
8271 before display. */
8273 struct animation_cache
8275 MagickWand *wand;
8276 int index;
8277 struct timespec update_time;
8278 struct animation_cache *next;
8279 char signature[FLEXIBLE_ARRAY_MEMBER];
8282 static struct animation_cache *animation_cache = NULL;
8284 static struct animation_cache *
8285 imagemagick_create_cache (char *signature)
8287 struct animation_cache *cache
8288 = xmalloc (offsetof (struct animation_cache, signature)
8289 + strlen (signature) + 1);
8290 cache->wand = 0;
8291 cache->index = 0;
8292 cache->next = 0;
8293 strcpy (cache->signature, signature);
8294 return cache;
8297 /* Discard cached images that haven't been used for a minute. */
8298 static void
8299 imagemagick_prune_animation_cache (void)
8301 struct animation_cache **pcache = &animation_cache;
8302 struct timespec old = timespec_sub (current_timespec (),
8303 make_timespec (60, 0));
8305 while (*pcache)
8307 struct animation_cache *cache = *pcache;
8308 if (timespec_cmp (old, cache->update_time) <= 0)
8309 pcache = &cache->next;
8310 else
8312 if (cache->wand)
8313 DestroyMagickWand (cache->wand);
8314 *pcache = cache->next;
8315 xfree (cache);
8320 static struct animation_cache *
8321 imagemagick_get_animation_cache (MagickWand *wand)
8323 char *signature = MagickGetImageSignature (wand);
8324 struct animation_cache *cache;
8325 struct animation_cache **pcache = &animation_cache;
8327 imagemagick_prune_animation_cache ();
8329 while (1)
8331 cache = *pcache;
8332 if (! cache)
8334 *pcache = cache = imagemagick_create_cache (signature);
8335 break;
8337 if (strcmp (signature, cache->signature) == 0)
8338 break;
8339 pcache = &cache->next;
8342 DestroyString (signature);
8343 cache->update_time = current_timespec ();
8344 return cache;
8347 static MagickWand *
8348 imagemagick_compute_animated_image (MagickWand *super_wand, int ino)
8350 int i;
8351 MagickWand *composite_wand;
8352 size_t dest_width, dest_height;
8353 struct animation_cache *cache = imagemagick_get_animation_cache (super_wand);
8355 MagickSetIteratorIndex (super_wand, 0);
8357 if (ino == 0 || cache->wand == NULL || cache->index > ino)
8359 composite_wand = MagickGetImage (super_wand);
8360 if (cache->wand)
8361 DestroyMagickWand (cache->wand);
8363 else
8364 composite_wand = cache->wand;
8366 dest_height = MagickGetImageHeight (composite_wand);
8368 for (i = max (1, cache->index + 1); i <= ino; i++)
8370 MagickWand *sub_wand;
8371 PixelIterator *source_iterator, *dest_iterator;
8372 PixelWand **source, **dest;
8373 size_t source_width, source_height;
8374 ssize_t source_left, source_top;
8375 MagickPixelPacket pixel;
8376 DisposeType dispose;
8377 ptrdiff_t lines = 0;
8379 MagickSetIteratorIndex (super_wand, i);
8380 sub_wand = MagickGetImage (super_wand);
8382 MagickGetImagePage (sub_wand, &source_width, &source_height,
8383 &source_left, &source_top);
8385 /* This flag says how to handle transparent pixels. */
8386 dispose = MagickGetImageDispose (sub_wand);
8388 source_iterator = NewPixelIterator (sub_wand);
8389 if (! source_iterator)
8391 DestroyMagickWand (composite_wand);
8392 DestroyMagickWand (sub_wand);
8393 cache->wand = NULL;
8394 image_error ("Imagemagick pixel iterator creation failed");
8395 return NULL;
8398 dest_iterator = NewPixelIterator (composite_wand);
8399 if (! dest_iterator)
8401 DestroyMagickWand (composite_wand);
8402 DestroyMagickWand (sub_wand);
8403 DestroyPixelIterator (source_iterator);
8404 cache->wand = NULL;
8405 image_error ("Imagemagick pixel iterator creation failed");
8406 return NULL;
8409 /* The sub-image may not start at origin, so move the destination
8410 iterator to where the sub-image should start. */
8411 if (source_top > 0)
8413 PixelSetIteratorRow (dest_iterator, source_top);
8414 lines = source_top;
8417 while ((source = PixelGetNextIteratorRow (source_iterator, &source_width))
8418 != NULL)
8420 ptrdiff_t x;
8422 /* Sanity check. This shouldn't happen, but apparently
8423 does in some pictures. */
8424 if (++lines >= dest_height)
8425 break;
8427 dest = PixelGetNextIteratorRow (dest_iterator, &dest_width);
8428 for (x = 0; x < source_width; x++)
8430 /* Sanity check. This shouldn't happen, but apparently
8431 also does in some pictures. */
8432 if (x + source_left >= dest_width)
8433 break;
8434 /* Normally we only copy over non-transparent pixels,
8435 but if the disposal method is "Background", then we
8436 copy over all pixels. */
8437 if (dispose == BackgroundDispose || PixelGetAlpha (source[x]))
8439 PixelGetMagickColor (source[x], &pixel);
8440 PixelSetMagickColor (dest[x + source_left], &pixel);
8443 PixelSyncIterator (dest_iterator);
8446 DestroyPixelIterator (source_iterator);
8447 DestroyPixelIterator (dest_iterator);
8448 DestroyMagickWand (sub_wand);
8451 /* Cache a copy for the next iteration. The current wand will be
8452 destroyed by the caller. */
8453 cache->wand = CloneMagickWand (composite_wand);
8454 cache->index = ino;
8456 return composite_wand;
8460 /* Helper function for imagemagick_load, which does the actual loading
8461 given contents and size, apart from frame and image structures,
8462 passed from imagemagick_load. Uses librimagemagick to do most of
8463 the image processing.
8465 F is a pointer to the Emacs frame; IMG to the image structure to
8466 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
8467 be parsed; SIZE is the number of bytes of data; and FILENAME is
8468 either the file name or the image data.
8470 Return true if successful. */
8472 static bool
8473 imagemagick_load_image (struct frame *f, struct image *img,
8474 unsigned char *contents, unsigned int size,
8475 char *filename)
8477 int width, height;
8478 size_t image_width, image_height;
8479 MagickBooleanType status;
8480 XImagePtr ximg;
8481 int x, y;
8482 MagickWand *image_wand;
8483 PixelIterator *iterator;
8484 PixelWand **pixels, *bg_wand = NULL;
8485 MagickPixelPacket pixel;
8486 Lisp_Object image;
8487 Lisp_Object value;
8488 Lisp_Object crop;
8489 EMACS_INT ino;
8490 int desired_width, desired_height;
8491 double rotation;
8492 int pixelwidth;
8493 char hint_buffer[MaxTextExtent];
8494 char *filename_hint = NULL;
8496 /* Handle image index for image types who can contain more than one image.
8497 Interface :index is same as for GIF. First we "ping" the image to see how
8498 many sub-images it contains. Pinging is faster than loading the image to
8499 find out things about it. */
8501 /* Initialize the imagemagick environment. */
8502 MagickWandGenesis ();
8503 image = image_spec_value (img->spec, QCindex, NULL);
8504 ino = INTEGERP (image) ? XFASTINT (image) : 0;
8505 image_wand = NewMagickWand ();
8507 if (filename)
8508 status = MagickReadImage (image_wand, filename);
8509 else
8511 filename_hint = imagemagick_filename_hint (img->spec, hint_buffer);
8512 MagickSetFilename (image_wand, filename_hint);
8513 status = MagickReadImageBlob (image_wand, contents, size);
8516 if (status == MagickFalse)
8518 imagemagick_error (image_wand);
8519 DestroyMagickWand (image_wand);
8520 return 0;
8523 if (ino < 0 || ino >= MagickGetNumberImages (image_wand))
8525 image_error ("Invalid image number `%s' in image `%s'", image, img->spec);
8526 DestroyMagickWand (image_wand);
8527 return 0;
8530 if (MagickGetImageDelay (image_wand) > 0)
8531 img->lisp_data =
8532 Fcons (Qdelay,
8533 Fcons (make_float (MagickGetImageDelay (image_wand) / 100.0),
8534 img->lisp_data));
8536 if (MagickGetNumberImages (image_wand) > 1)
8537 img->lisp_data =
8538 Fcons (Qcount,
8539 Fcons (make_number (MagickGetNumberImages (image_wand)),
8540 img->lisp_data));
8542 /* If we have an animated image, get the new wand based on the
8543 "super-wand". */
8544 if (MagickGetNumberImages (image_wand) > 1)
8546 MagickWand *super_wand = image_wand;
8547 image_wand = imagemagick_compute_animated_image (super_wand, ino);
8548 if (! image_wand)
8549 image_wand = super_wand;
8550 else
8551 DestroyMagickWand (super_wand);
8554 /* Retrieve the frame's background color, for use later. */
8556 XColor bgcolor;
8557 Lisp_Object specified_bg;
8559 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
8560 if (!STRINGP (specified_bg)
8561 || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0))
8562 x_query_frame_background_color (f, &bgcolor);
8564 bg_wand = NewPixelWand ();
8565 PixelSetRed (bg_wand, (double) bgcolor.red / 65535);
8566 PixelSetGreen (bg_wand, (double) bgcolor.green / 65535);
8567 PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535);
8570 compute_image_size (MagickGetImageWidth (image_wand),
8571 MagickGetImageHeight (image_wand),
8572 img->spec, &desired_width, &desired_height);
8574 if (desired_width != -1 && desired_height != -1)
8576 status = MagickScaleImage (image_wand, desired_width, desired_height);
8577 if (status == MagickFalse)
8579 image_error ("Imagemagick scale failed");
8580 imagemagick_error (image_wand);
8581 goto imagemagick_error;
8585 /* crop behaves similar to image slicing in Emacs but is more memory
8586 efficient. */
8587 crop = image_spec_value (img->spec, QCcrop, NULL);
8589 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8591 /* After some testing, it seems MagickCropImage is the fastest crop
8592 function in ImageMagick. This crop function seems to do less copying
8593 than the alternatives, but it still reads the entire image into memory
8594 before cropping, which is apparently difficult to avoid when using
8595 imagemagick. */
8596 size_t crop_width = XINT (XCAR (crop));
8597 crop = XCDR (crop);
8598 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
8600 size_t crop_height = XINT (XCAR (crop));
8601 crop = XCDR (crop);
8602 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8604 ssize_t crop_x = XINT (XCAR (crop));
8605 crop = XCDR (crop);
8606 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
8608 ssize_t crop_y = XINT (XCAR (crop));
8609 MagickCropImage (image_wand, crop_width, crop_height,
8610 crop_x, crop_y);
8616 /* Furthermore :rotation. we need background color and angle for
8617 rotation. */
8619 TODO background handling for rotation specified_bg =
8620 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8621 (specified_bg). */
8622 value = image_spec_value (img->spec, QCrotation, NULL);
8623 if (FLOATP (value))
8625 rotation = extract_float (value);
8626 status = MagickRotateImage (image_wand, bg_wand, rotation);
8627 if (status == MagickFalse)
8629 image_error ("Imagemagick image rotate failed");
8630 imagemagick_error (image_wand);
8631 goto imagemagick_error;
8635 /* Set the canvas background color to the frame or specified
8636 background, and flatten the image. Note: as of ImageMagick
8637 6.6.0, SVG image transparency is not handled properly
8638 (e.g. etc/images/splash.svg shows a white background always). */
8640 MagickWand *new_wand;
8641 MagickSetImageBackgroundColor (image_wand, bg_wand);
8642 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8643 new_wand = MagickMergeImageLayers (image_wand, MergeLayer);
8644 #else
8645 new_wand = MagickFlattenImages (image_wand);
8646 #endif
8647 DestroyMagickWand (image_wand);
8648 image_wand = new_wand;
8651 /* Finally we are done manipulating the image. Figure out the
8652 resulting width/height and transfer ownership to Emacs. */
8653 image_height = MagickGetImageHeight (image_wand);
8654 image_width = MagickGetImageWidth (image_wand);
8656 if (! (image_width <= INT_MAX && image_height <= INT_MAX
8657 && check_image_size (f, image_width, image_height)))
8659 image_size_error ();
8660 goto imagemagick_error;
8663 width = image_width;
8664 height = image_height;
8666 /* We can now get a valid pixel buffer from the imagemagick file, if all
8667 went ok. */
8669 init_color_table ();
8671 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8672 if (imagemagick_render_type != 0)
8674 /* Magicexportimage is normally faster than pixelpushing. This
8675 method is also well tested. Some aspects of this method are
8676 ad-hoc and needs to be more researched. */
8677 int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/
8678 const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/
8679 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8680 if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth,
8681 &ximg, 0))
8683 #ifdef COLOR_TABLE_SUPPORT
8684 free_color_table ();
8685 #endif
8686 image_error ("Imagemagick X bitmap allocation failure");
8687 goto imagemagick_error;
8690 /* Oddly, the below code doesn't seem to work:*/
8691 /* switch(ximg->bitmap_unit){ */
8692 /* case 8: */
8693 /* pixelwidth=CharPixel; */
8694 /* break; */
8695 /* case 16: */
8696 /* pixelwidth=ShortPixel; */
8697 /* break; */
8698 /* case 32: */
8699 /* pixelwidth=LongPixel; */
8700 /* break; */
8701 /* } */
8703 Here im just guessing the format of the bitmap.
8704 happens to work fine for:
8705 - bw djvu images
8706 on rgb display.
8707 seems about 3 times as fast as pixel pushing(not carefully measured)
8709 pixelwidth = CharPixel; /*??? TODO figure out*/
8710 MagickExportImagePixels (image_wand, 0, 0, width, height,
8711 exportdepth, pixelwidth, ximg->data);
8713 else
8714 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8716 size_t image_height;
8717 MagickRealType color_scale = 65535.0 / QuantumRange;
8719 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8720 if (!image_create_x_image_and_pixmap (f, img, width, height, 0,
8721 &ximg, 0))
8723 #ifdef COLOR_TABLE_SUPPORT
8724 free_color_table ();
8725 #endif
8726 image_error ("Imagemagick X bitmap allocation failure");
8727 goto imagemagick_error;
8730 /* Copy imagemagick image to x with primitive yet robust pixel
8731 pusher loop. This has been tested a lot with many different
8732 images. */
8734 /* Copy pixels from the imagemagick image structure to the x image map. */
8735 iterator = NewPixelIterator (image_wand);
8736 if (! iterator)
8738 #ifdef COLOR_TABLE_SUPPORT
8739 free_color_table ();
8740 #endif
8741 x_destroy_x_image (ximg);
8742 image_error ("Imagemagick pixel iterator creation failed");
8743 goto imagemagick_error;
8746 image_height = MagickGetImageHeight (image_wand);
8747 for (y = 0; y < image_height; y++)
8749 size_t row_width;
8750 pixels = PixelGetNextIteratorRow (iterator, &row_width);
8751 if (! pixels)
8752 break;
8753 int xlim = min (row_width, width);
8754 for (x = 0; x < xlim; x++)
8756 PixelGetMagickColor (pixels[x], &pixel);
8757 XPutPixel (ximg, x, y,
8758 lookup_rgb_color (f,
8759 color_scale * pixel.red,
8760 color_scale * pixel.green,
8761 color_scale * pixel.blue));
8764 DestroyPixelIterator (iterator);
8767 #ifdef COLOR_TABLE_SUPPORT
8768 /* Remember colors allocated for this image. */
8769 img->colors = colors_in_color_table (&img->ncolors);
8770 free_color_table ();
8771 #endif /* COLOR_TABLE_SUPPORT */
8773 img->width = width;
8774 img->height = height;
8776 /* Put ximg into the image. */
8777 image_put_x_image (f, img, ximg, 0);
8779 /* Final cleanup. image_wand should be the only resource left. */
8780 DestroyMagickWand (image_wand);
8781 if (bg_wand) DestroyPixelWand (bg_wand);
8783 /* `MagickWandTerminus' terminates the imagemagick environment. */
8784 MagickWandTerminus ();
8786 return 1;
8788 imagemagick_error:
8789 DestroyMagickWand (image_wand);
8790 if (bg_wand) DestroyPixelWand (bg_wand);
8792 MagickWandTerminus ();
8793 /* TODO more cleanup. */
8794 image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec);
8795 return 0;
8799 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8800 successful. this function will go into the imagemagick_type structure, and
8801 the prototype thus needs to be compatible with that structure. */
8803 static bool
8804 imagemagick_load (struct frame *f, struct image *img)
8806 bool success_p = 0;
8807 Lisp_Object file_name;
8809 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8810 file_name = image_spec_value (img->spec, QCfile, NULL);
8811 if (STRINGP (file_name))
8813 Lisp_Object file = x_find_image_file (file_name);
8814 if (!STRINGP (file))
8816 image_error ("Cannot find image file `%s'", file_name);
8817 return 0;
8819 file = ENCODE_FILE (file);
8820 #ifdef WINDOWSNT
8821 file = ansi_encode_filename (file);
8822 #endif
8823 success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file));
8825 /* Else its not a file, its a lisp object. Load the image from a
8826 lisp object rather than a file. */
8827 else
8829 Lisp_Object data;
8831 data = image_spec_value (img->spec, QCdata, NULL);
8832 if (!STRINGP (data))
8834 image_error ("Invalid image data `%s'", data);
8835 return 0;
8837 success_p = imagemagick_load_image (f, img, SDATA (data),
8838 SBYTES (data), NULL);
8841 return success_p;
8844 DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0,
8845 doc: /* Return a list of image types supported by ImageMagick.
8846 Each entry in this list is a symbol named after an ImageMagick format
8847 tag. See the ImageMagick manual for a list of ImageMagick formats and
8848 their descriptions (http://www.imagemagick.org/script/formats.php).
8849 You can also try the shell command: `identify -list format'.
8851 Note that ImageMagick recognizes many file-types that Emacs does not
8852 recognize as images, such as C. See `imagemagick-types-enable'
8853 and `imagemagick-types-inhibit'. */)
8854 (void)
8856 Lisp_Object typelist = Qnil;
8857 size_t numf = 0;
8858 ExceptionInfo ex;
8859 char **imtypes;
8860 size_t i;
8862 GetExceptionInfo(&ex);
8863 imtypes = GetMagickList ("*", &numf, &ex);
8864 DestroyExceptionInfo(&ex);
8866 for (i = 0; i < numf; i++)
8868 Lisp_Object imagemagicktype = intern (imtypes[i]);
8869 typelist = Fcons (imagemagicktype, typelist);
8870 imtypes[i] = MagickRelinquishMemory (imtypes[i]);
8873 MagickRelinquishMemory (imtypes);
8874 return Fnreverse (typelist);
8877 #endif /* defined (HAVE_IMAGEMAGICK) */
8881 /***********************************************************************
8883 ***********************************************************************/
8885 #ifdef HAVE_RSVG
8887 /* Function prototypes. */
8889 static bool svg_image_p (Lisp_Object object);
8890 static bool svg_load (struct frame *f, struct image *img);
8892 static bool svg_load_image (struct frame *, struct image *,
8893 unsigned char *, ptrdiff_t, char *);
8895 /* Indices of image specification fields in svg_format, below. */
8897 enum svg_keyword_index
8899 SVG_TYPE,
8900 SVG_DATA,
8901 SVG_FILE,
8902 SVG_ASCENT,
8903 SVG_MARGIN,
8904 SVG_RELIEF,
8905 SVG_ALGORITHM,
8906 SVG_HEURISTIC_MASK,
8907 SVG_MASK,
8908 SVG_BACKGROUND,
8909 SVG_LAST
8912 /* Vector of image_keyword structures describing the format
8913 of valid user-defined image specifications. */
8915 static const struct image_keyword svg_format[SVG_LAST] =
8917 {":type", IMAGE_SYMBOL_VALUE, 1},
8918 {":data", IMAGE_STRING_VALUE, 0},
8919 {":file", IMAGE_STRING_VALUE, 0},
8920 {":ascent", IMAGE_ASCENT_VALUE, 0},
8921 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
8922 {":relief", IMAGE_INTEGER_VALUE, 0},
8923 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8924 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8925 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8926 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8929 # if defined HAVE_NTGUI && defined WINDOWSNT
8930 static bool init_svg_functions (void);
8931 # else
8932 #define init_svg_functions NULL
8933 # endif
8935 /* Structure describing the image type `svg'. Its the same type of
8936 structure defined for all image formats, handled by emacs image
8937 functions. See struct image_type in dispextern.h. */
8939 static struct image_type svg_type =
8941 SYMBOL_INDEX (Qsvg),
8942 svg_image_p,
8943 svg_load,
8944 x_clear_image,
8945 init_svg_functions,
8946 NULL
8950 /* Return true if OBJECT is a valid SVG image specification. Do
8951 this by calling parse_image_spec and supplying the keywords that
8952 identify the SVG format. */
8954 static bool
8955 svg_image_p (Lisp_Object object)
8957 struct image_keyword fmt[SVG_LAST];
8958 memcpy (fmt, svg_format, sizeof fmt);
8960 if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg))
8961 return 0;
8963 /* Must specify either the :data or :file keyword. */
8964 return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1;
8967 # include <librsvg/rsvg.h>
8969 # ifdef WINDOWSNT
8971 /* SVG library functions. */
8972 DEF_DLL_FN (RsvgHandle *, rsvg_handle_new, (void));
8973 DEF_DLL_FN (void, rsvg_handle_get_dimensions,
8974 (RsvgHandle *, RsvgDimensionData *));
8975 DEF_DLL_FN (gboolean, rsvg_handle_write,
8976 (RsvgHandle *, const guchar *, gsize, GError **));
8977 DEF_DLL_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **));
8978 DEF_DLL_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *));
8979 DEF_DLL_FN (void, rsvg_handle_set_base_uri, (RsvgHandle *, const char *));
8981 DEF_DLL_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *));
8982 DEF_DLL_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *));
8983 DEF_DLL_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *));
8984 DEF_DLL_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *));
8985 DEF_DLL_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *));
8986 DEF_DLL_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *));
8987 DEF_DLL_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *));
8988 DEF_DLL_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *));
8990 # if ! GLIB_CHECK_VERSION (2, 36, 0)
8991 DEF_DLL_FN (void, g_type_init, (void));
8992 # endif
8993 DEF_DLL_FN (void, g_object_unref, (gpointer));
8994 DEF_DLL_FN (void, g_clear_error, (GError **));
8996 static bool
8997 init_svg_functions (void)
8999 HMODULE library, gdklib = NULL, glib = NULL, gobject = NULL;
9001 if (!(glib = w32_delayed_load (Qglib))
9002 || !(gobject = w32_delayed_load (Qgobject))
9003 || !(gdklib = w32_delayed_load (Qgdk_pixbuf))
9004 || !(library = w32_delayed_load (Qsvg)))
9006 if (gdklib) FreeLibrary (gdklib);
9007 if (gobject) FreeLibrary (gobject);
9008 if (glib) FreeLibrary (glib);
9009 return 0;
9012 LOAD_DLL_FN (library, rsvg_handle_new);
9013 LOAD_DLL_FN (library, rsvg_handle_get_dimensions);
9014 LOAD_DLL_FN (library, rsvg_handle_write);
9015 LOAD_DLL_FN (library, rsvg_handle_close);
9016 LOAD_DLL_FN (library, rsvg_handle_get_pixbuf);
9017 LOAD_DLL_FN (library, rsvg_handle_set_base_uri);
9019 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_width);
9020 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_height);
9021 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_pixels);
9022 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_rowstride);
9023 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_colorspace);
9024 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_n_channels);
9025 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_has_alpha);
9026 LOAD_DLL_FN (gdklib, gdk_pixbuf_get_bits_per_sample);
9028 # if ! GLIB_CHECK_VERSION (2, 36, 0)
9029 LOAD_DLL_FN (gobject, g_type_init);
9030 # endif
9031 LOAD_DLL_FN (gobject, g_object_unref);
9032 LOAD_DLL_FN (glib, g_clear_error);
9034 return 1;
9037 /* The following aliases for library functions allow dynamic loading
9038 to be used on some platforms. */
9040 # undef gdk_pixbuf_get_bits_per_sample
9041 # undef gdk_pixbuf_get_colorspace
9042 # undef gdk_pixbuf_get_has_alpha
9043 # undef gdk_pixbuf_get_height
9044 # undef gdk_pixbuf_get_n_channels
9045 # undef gdk_pixbuf_get_pixels
9046 # undef gdk_pixbuf_get_rowstride
9047 # undef gdk_pixbuf_get_width
9048 # undef g_clear_error
9049 # undef g_object_unref
9050 # undef g_type_init
9051 # undef rsvg_handle_close
9052 # undef rsvg_handle_get_dimensions
9053 # undef rsvg_handle_get_pixbuf
9054 # undef rsvg_handle_new
9055 # undef rsvg_handle_set_base_uri
9056 # undef rsvg_handle_write
9058 # define gdk_pixbuf_get_bits_per_sample fn_gdk_pixbuf_get_bits_per_sample
9059 # define gdk_pixbuf_get_colorspace fn_gdk_pixbuf_get_colorspace
9060 # define gdk_pixbuf_get_has_alpha fn_gdk_pixbuf_get_has_alpha
9061 # define gdk_pixbuf_get_height fn_gdk_pixbuf_get_height
9062 # define gdk_pixbuf_get_n_channels fn_gdk_pixbuf_get_n_channels
9063 # define gdk_pixbuf_get_pixels fn_gdk_pixbuf_get_pixels
9064 # define gdk_pixbuf_get_rowstride fn_gdk_pixbuf_get_rowstride
9065 # define gdk_pixbuf_get_width fn_gdk_pixbuf_get_width
9066 # define g_clear_error fn_g_clear_error
9067 # define g_object_unref fn_g_object_unref
9068 # define g_type_init fn_g_type_init
9069 # define rsvg_handle_close fn_rsvg_handle_close
9070 # define rsvg_handle_get_dimensions fn_rsvg_handle_get_dimensions
9071 # define rsvg_handle_get_pixbuf fn_rsvg_handle_get_pixbuf
9072 # define rsvg_handle_new fn_rsvg_handle_new
9073 # define rsvg_handle_set_base_uri fn_rsvg_handle_set_base_uri
9074 # define rsvg_handle_write fn_rsvg_handle_write
9076 # endif /* !WINDOWSNT */
9078 /* Load SVG image IMG for use on frame F. Value is true if
9079 successful. */
9081 static bool
9082 svg_load (struct frame *f, struct image *img)
9084 bool success_p = 0;
9085 Lisp_Object file_name;
9087 /* If IMG->spec specifies a file name, create a non-file spec from it. */
9088 file_name = image_spec_value (img->spec, QCfile, NULL);
9089 if (STRINGP (file_name))
9091 int fd;
9092 Lisp_Object file = x_find_image_fd (file_name, &fd);
9093 if (!STRINGP (file))
9095 image_error ("Cannot find image file `%s'", file_name);
9096 return 0;
9099 /* Read the entire file into memory. */
9100 ptrdiff_t size;
9101 unsigned char *contents = slurp_file (fd, &size);
9102 if (contents == NULL)
9104 image_error ("Error loading SVG image `%s'", file);
9105 return 0;
9107 /* If the file was slurped into memory properly, parse it. */
9108 success_p = svg_load_image (f, img, contents, size,
9109 SSDATA (ENCODE_FILE (file)));
9110 xfree (contents);
9112 /* Else its not a file, its a lisp object. Load the image from a
9113 lisp object rather than a file. */
9114 else
9116 Lisp_Object data, original_filename;
9118 data = image_spec_value (img->spec, QCdata, NULL);
9119 if (!STRINGP (data))
9121 image_error ("Invalid image data `%s'", data);
9122 return 0;
9124 original_filename = BVAR (current_buffer, filename);
9125 success_p = svg_load_image (f, img, SDATA (data), SBYTES (data),
9126 (NILP (original_filename) ? NULL
9127 : SSDATA (original_filename)));
9130 return success_p;
9133 /* svg_load_image is a helper function for svg_load, which does the
9134 actual loading given contents and size, apart from frame and image
9135 structures, passed from svg_load.
9137 Uses librsvg to do most of the image processing.
9139 Returns true when successful. */
9140 static bool
9141 svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */
9142 struct image *img, /* Pointer to emacs image structure. */
9143 unsigned char *contents, /* String containing the SVG XML data to be parsed. */
9144 ptrdiff_t size, /* Size of data in bytes. */
9145 char *filename) /* Name of SVG file being loaded. */
9147 RsvgHandle *rsvg_handle;
9148 RsvgDimensionData dimension_data;
9149 GError *err = NULL;
9150 GdkPixbuf *pixbuf;
9151 int width;
9152 int height;
9153 const guint8 *pixels;
9154 int rowstride;
9155 XImagePtr ximg;
9156 Lisp_Object specified_bg;
9157 XColor background;
9158 int x;
9159 int y;
9161 #if ! GLIB_CHECK_VERSION (2, 36, 0)
9162 /* g_type_init is a glib function that must be called prior to
9163 using gnome type library functions (obsolete since 2.36.0). */
9164 g_type_init ();
9165 #endif
9167 /* Make a handle to a new rsvg object. */
9168 rsvg_handle = rsvg_handle_new ();
9170 /* Set base_uri for properly handling referenced images (via 'href').
9171 See rsvg bug 596114 - "image refs are relative to curdir, not .svg file"
9172 (https://bugzilla.gnome.org/show_bug.cgi?id=596114). */
9173 if (filename)
9174 rsvg_handle_set_base_uri(rsvg_handle, filename);
9176 /* Parse the contents argument and fill in the rsvg_handle. */
9177 rsvg_handle_write (rsvg_handle, contents, size, &err);
9178 if (err) goto rsvg_error;
9180 /* The parsing is complete, rsvg_handle is ready to used, close it
9181 for further writes. */
9182 rsvg_handle_close (rsvg_handle, &err);
9183 if (err) goto rsvg_error;
9185 rsvg_handle_get_dimensions (rsvg_handle, &dimension_data);
9186 if (! check_image_size (f, dimension_data.width, dimension_data.height))
9188 image_size_error ();
9189 goto rsvg_error;
9192 /* We can now get a valid pixel buffer from the svg file, if all
9193 went ok. */
9194 pixbuf = rsvg_handle_get_pixbuf (rsvg_handle);
9195 if (!pixbuf) goto rsvg_error;
9196 g_object_unref (rsvg_handle);
9198 /* Extract some meta data from the svg handle. */
9199 width = gdk_pixbuf_get_width (pixbuf);
9200 height = gdk_pixbuf_get_height (pixbuf);
9201 pixels = gdk_pixbuf_get_pixels (pixbuf);
9202 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
9204 /* Validate the svg meta data. */
9205 eassert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
9206 eassert (gdk_pixbuf_get_n_channels (pixbuf) == 4);
9207 eassert (gdk_pixbuf_get_has_alpha (pixbuf));
9208 eassert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
9210 #ifdef USE_CAIRO
9212 unsigned char *data = (unsigned char *) xmalloc (width*height*4);
9213 int y;
9214 uint32_t bgcolor = get_spec_bg_or_alpha_as_argb (img, f);
9216 for (y = 0; y < height; ++y)
9218 const guchar *iconptr = pixels + y * rowstride;
9219 uint32_t *dataptr = (uint32_t *) (data + y * rowstride);
9220 int x;
9222 for (x = 0; x < width; ++x)
9224 if (iconptr[3] == 0)
9225 *dataptr = bgcolor;
9226 else
9227 *dataptr = (iconptr[0] << 16)
9228 | (iconptr[1] << 8)
9229 | iconptr[2]
9230 | (iconptr[3] << 24);
9232 iconptr += 4;
9233 ++dataptr;
9237 create_cairo_image_surface (img, data, width, height);
9238 g_object_unref (pixbuf);
9240 #else
9241 /* Try to create a x pixmap to hold the svg pixmap. */
9242 if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
9244 g_object_unref (pixbuf);
9245 return 0;
9248 init_color_table ();
9250 /* Handle alpha channel by combining the image with a background
9251 color. */
9252 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
9253 if (!STRINGP (specified_bg)
9254 || !x_defined_color (f, SSDATA (specified_bg), &background, 0))
9255 x_query_frame_background_color (f, &background);
9257 /* SVG pixmaps specify transparency in the last byte, so right
9258 shift 8 bits to get rid of it, since emacs doesn't support
9259 transparency. */
9260 background.red >>= 8;
9261 background.green >>= 8;
9262 background.blue >>= 8;
9264 /* This loop handles opacity values, since Emacs assumes
9265 non-transparent images. Each pixel must be "flattened" by
9266 calculating the resulting color, given the transparency of the
9267 pixel, and the image background color. */
9268 for (y = 0; y < height; ++y)
9270 for (x = 0; x < width; ++x)
9272 int red;
9273 int green;
9274 int blue;
9275 int opacity;
9277 red = *pixels++;
9278 green = *pixels++;
9279 blue = *pixels++;
9280 opacity = *pixels++;
9282 red = ((red * opacity)
9283 + (background.red * ((1 << 8) - opacity)));
9284 green = ((green * opacity)
9285 + (background.green * ((1 << 8) - opacity)));
9286 blue = ((blue * opacity)
9287 + (background.blue * ((1 << 8) - opacity)));
9289 XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue));
9292 pixels += rowstride - 4 * width;
9295 #ifdef COLOR_TABLE_SUPPORT
9296 /* Remember colors allocated for this image. */
9297 img->colors = colors_in_color_table (&img->ncolors);
9298 free_color_table ();
9299 #endif /* COLOR_TABLE_SUPPORT */
9301 g_object_unref (pixbuf);
9303 img->width = width;
9304 img->height = height;
9306 /* Maybe fill in the background field while we have ximg handy.
9307 Casting avoids a GCC warning. */
9308 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
9310 /* Put ximg into the image. */
9311 image_put_x_image (f, img, ximg, 0);
9312 #endif /* ! USE_CAIRO */
9314 return 1;
9316 rsvg_error:
9317 g_object_unref (rsvg_handle);
9318 /* FIXME: Use error->message so the user knows what is the actual
9319 problem with the image. */
9320 image_error ("Error parsing SVG image `%s'", img->spec);
9321 g_clear_error (&err);
9322 return 0;
9325 #endif /* defined (HAVE_RSVG) */
9330 /***********************************************************************
9331 Ghostscript
9332 ***********************************************************************/
9334 #ifdef HAVE_X_WINDOWS
9335 #define HAVE_GHOSTSCRIPT 1
9336 #endif /* HAVE_X_WINDOWS */
9338 #ifdef HAVE_GHOSTSCRIPT
9340 static bool gs_image_p (Lisp_Object object);
9341 static bool gs_load (struct frame *f, struct image *img);
9342 static void gs_clear_image (struct frame *f, struct image *img);
9344 /* Indices of image specification fields in gs_format, below. */
9346 enum gs_keyword_index
9348 GS_TYPE,
9349 GS_PT_WIDTH,
9350 GS_PT_HEIGHT,
9351 GS_FILE,
9352 GS_LOADER,
9353 GS_BOUNDING_BOX,
9354 GS_ASCENT,
9355 GS_MARGIN,
9356 GS_RELIEF,
9357 GS_ALGORITHM,
9358 GS_HEURISTIC_MASK,
9359 GS_MASK,
9360 GS_BACKGROUND,
9361 GS_LAST
9364 /* Vector of image_keyword structures describing the format
9365 of valid user-defined image specifications. */
9367 static const struct image_keyword gs_format[GS_LAST] =
9369 {":type", IMAGE_SYMBOL_VALUE, 1},
9370 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9371 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9372 {":file", IMAGE_STRING_VALUE, 1},
9373 {":loader", IMAGE_FUNCTION_VALUE, 0},
9374 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9375 {":ascent", IMAGE_ASCENT_VALUE, 0},
9376 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0},
9377 {":relief", IMAGE_INTEGER_VALUE, 0},
9378 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9379 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9380 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9381 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9384 /* Structure describing the image type `ghostscript'. */
9386 static struct image_type gs_type =
9388 SYMBOL_INDEX (Qpostscript),
9389 gs_image_p,
9390 gs_load,
9391 gs_clear_image,
9392 NULL,
9393 NULL
9397 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9399 static void
9400 gs_clear_image (struct frame *f, struct image *img)
9402 x_clear_image (f, img);
9406 /* Return true if OBJECT is a valid Ghostscript image
9407 specification. */
9409 static bool
9410 gs_image_p (Lisp_Object object)
9412 struct image_keyword fmt[GS_LAST];
9413 Lisp_Object tem;
9414 int i;
9416 memcpy (fmt, gs_format, sizeof fmt);
9418 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9419 return 0;
9421 /* Bounding box must be a list or vector containing 4 integers. */
9422 tem = fmt[GS_BOUNDING_BOX].value;
9423 if (CONSP (tem))
9425 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9426 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9427 return 0;
9428 if (!NILP (tem))
9429 return 0;
9431 else if (VECTORP (tem))
9433 if (ASIZE (tem) != 4)
9434 return 0;
9435 for (i = 0; i < 4; ++i)
9436 if (!INTEGERP (AREF (tem, i)))
9437 return 0;
9439 else
9440 return 0;
9442 return 1;
9446 /* Load Ghostscript image IMG for use on frame F. Value is true
9447 if successful. */
9449 static bool
9450 gs_load (struct frame *f, struct image *img)
9452 uprintmax_t printnum1, printnum2;
9453 char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)];
9454 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9455 Lisp_Object frame;
9456 double in_width, in_height;
9457 Lisp_Object pixel_colors = Qnil;
9459 /* Compute pixel size of pixmap needed from the given size in the
9460 image specification. Sizes in the specification are in pt. 1 pt
9461 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9462 info. */
9463 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9464 in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0;
9465 in_width *= FRAME_RES_X (f);
9466 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9467 in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0;
9468 in_height *= FRAME_RES_Y (f);
9470 if (! (in_width <= INT_MAX && in_height <= INT_MAX
9471 && check_image_size (f, in_width, in_height)))
9473 image_size_error ();
9474 return 0;
9476 img->width = in_width;
9477 img->height = in_height;
9479 /* Create the pixmap. */
9480 eassert (img->pixmap == NO_PIXMAP);
9482 if (x_check_image_size (0, img->width, img->height))
9484 /* Only W32 version did BLOCK_INPUT here. ++kfs */
9485 block_input ();
9486 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9487 img->width, img->height,
9488 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9489 unblock_input ();
9492 if (!img->pixmap)
9494 image_error ("Unable to create pixmap for `%s'" , img->spec);
9495 return 0;
9498 /* Call the loader to fill the pixmap. It returns a process object
9499 if successful. We do not record_unwind_protect here because
9500 other places in redisplay like calling window scroll functions
9501 don't either. Let the Lisp loader use `unwind-protect' instead. */
9502 printnum1 = FRAME_X_WINDOW (f);
9503 printnum2 = img->pixmap;
9504 window_and_pixmap_id
9505 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9507 printnum1 = FRAME_FOREGROUND_PIXEL (f);
9508 printnum2 = FRAME_BACKGROUND_PIXEL (f);
9509 pixel_colors
9510 = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2);
9512 XSETFRAME (frame, f);
9513 loader = image_spec_value (img->spec, QCloader, NULL);
9514 if (NILP (loader))
9515 loader = intern ("gs-load-image");
9517 img->lisp_data = call6 (loader, frame, img->spec,
9518 make_number (img->width),
9519 make_number (img->height),
9520 window_and_pixmap_id,
9521 pixel_colors);
9522 return PROCESSP (img->lisp_data);
9526 /* Kill the Ghostscript process that was started to fill PIXMAP on
9527 frame F. Called from XTread_socket when receiving an event
9528 telling Emacs that Ghostscript has finished drawing. */
9530 void
9531 x_kill_gs_process (Pixmap pixmap, struct frame *f)
9533 struct image_cache *c = FRAME_IMAGE_CACHE (f);
9534 ptrdiff_t i;
9535 struct image *img;
9537 /* Find the image containing PIXMAP. */
9538 for (i = 0; i < c->used; ++i)
9539 if (c->images[i]->pixmap == pixmap)
9540 break;
9542 /* Should someone in between have cleared the image cache, for
9543 instance, give up. */
9544 if (i == c->used)
9545 return;
9547 /* Kill the GS process. We should have found PIXMAP in the image
9548 cache and its image should contain a process object. */
9549 img = c->images[i];
9550 eassert (PROCESSP (img->lisp_data));
9551 Fkill_process (img->lisp_data, Qnil);
9552 img->lisp_data = Qnil;
9554 #if defined (HAVE_X_WINDOWS)
9556 /* On displays with a mutable colormap, figure out the colors
9557 allocated for the image by looking at the pixels of an XImage for
9558 img->pixmap. */
9559 if (x_mutable_colormap (FRAME_X_VISUAL (f)))
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);
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