1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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
36 #include "dispextern.h"
37 #include "blockinput.h"
40 #include "character.h"
42 #include "termhooks.h"
45 #ifdef HAVE_SYS_STAT_H
47 #endif /* HAVE_SYS_STAT_H */
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
51 #endif /* HAVE_SYS_TYPES_H */
53 #ifdef HAVE_WINDOW_SYSTEM
55 #endif /* HAVE_WINDOW_SYSTEM */
58 #define COLOR_TABLE_SUPPORT 1
60 typedef struct x_bitmap_record Bitmap_Record
;
61 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
62 #define NO_PIXMAP None
64 #define PIX_MASK_RETAIN 0
65 #define PIX_MASK_DRAW 1
66 #endif /* HAVE_X_WINDOWS */
70 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
75 /* W32_TODO : Color tables on W32. */
76 #undef COLOR_TABLE_SUPPORT
78 typedef struct w32_bitmap_record Bitmap_Record
;
79 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
82 #define PIX_MASK_RETAIN 0
83 #define PIX_MASK_DRAW 1
85 #define x_defined_color w32_defined_color
86 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
88 /* Versions of libpng, libgif, and libjpeg that we were compiled with,
89 or -1 if no PNG/GIF support was compiled in. This is tested by
90 w32-win.el to correctly set up the alist used to search for the
91 respective image libraries. */
92 Lisp_Object Qlibpng_version
, Qlibgif_version
, Qlibjpeg_version
;
94 #endif /* HAVE_NTGUI */
97 #undef COLOR_TABLE_SUPPORT
99 typedef struct ns_bitmap_record Bitmap_Record
;
101 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
104 #define PIX_MASK_RETAIN 0
105 #define PIX_MASK_DRAW 1
107 #define x_defined_color(f, name, color_def, alloc) \
108 ns_defined_color (f, name, color_def, alloc, 0)
109 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
113 /* The symbol `postscript' identifying images of this type. */
115 static Lisp_Object Qpostscript
;
117 static void x_disable_image (struct frame
*, struct image
*);
118 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
121 static void init_color_table (void);
122 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
123 #ifdef COLOR_TABLE_SUPPORT
124 static void free_color_table (void);
125 static unsigned long *colors_in_color_table (int *n
);
128 static Lisp_Object QCmax_width
, QCmax_height
;
130 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
131 id, which is just an int that this section returns. Bitmaps are
132 reference counted so they can be shared among frames.
134 Bitmap indices are guaranteed to be > 0, so a negative number can
135 be used to indicate no bitmap.
137 If you use x_create_bitmap_from_data, then you must keep track of
138 the bitmaps yourself. That is, creating a bitmap from the same
139 data more than once will not be caught. */
142 /* Use with images created by ns_image_for_XPM. */
144 XGetPixel (XImagePtr ximage
, int x
, int y
)
146 return ns_get_pixel (ximage
, x
, y
);
149 /* Use with images created by ns_image_for_XPM; alpha set to 1;
150 pixel is assumed to be in RGB form. */
152 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
154 ns_put_pixel (ximage
, x
, y
, pixel
);
159 /* Functions to access the contents of a bitmap, given an id. */
161 #ifdef HAVE_X_WINDOWS
163 x_bitmap_height (struct frame
*f
, ptrdiff_t id
)
165 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
169 x_bitmap_width (struct frame
*f
, ptrdiff_t id
)
171 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
175 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
177 x_bitmap_pixmap (struct frame
*f
, ptrdiff_t id
)
179 /* HAVE_NTGUI needs the explicit cast here. */
180 return (ptrdiff_t) FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
184 #ifdef HAVE_X_WINDOWS
186 x_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
188 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
192 /* Allocate a new bitmap record. Returns index of new record. */
195 x_allocate_bitmap_record (struct frame
*f
)
197 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
200 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
201 return ++dpyinfo
->bitmaps_last
;
203 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
204 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
208 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
209 10, -1, sizeof *dpyinfo
->bitmaps
);
210 return ++dpyinfo
->bitmaps_last
;
213 /* Add one reference to the reference count of the bitmap with id ID. */
216 x_reference_bitmap (struct frame
*f
, ptrdiff_t id
)
218 ++FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
221 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
224 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
226 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
229 #ifdef HAVE_X_WINDOWS
231 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
232 bits
, width
, height
);
235 #endif /* HAVE_X_WINDOWS */
239 bitmap
= CreateBitmap (width
, height
,
240 FRAME_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
241 FRAME_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
245 #endif /* HAVE_NTGUI */
248 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
253 id
= x_allocate_bitmap_record (f
);
256 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
257 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
260 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
261 dpyinfo
->bitmaps
[id
- 1].height
= height
;
262 dpyinfo
->bitmaps
[id
- 1].width
= width
;
263 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
265 #ifdef HAVE_X_WINDOWS
266 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
267 dpyinfo
->bitmaps
[id
- 1].have_mask
= false;
268 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
269 #endif /* HAVE_X_WINDOWS */
272 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
273 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
274 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
275 #endif /* HAVE_NTGUI */
280 /* Create bitmap from file FILE for frame F. */
283 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
285 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
288 return -1; /* W32_TODO : bitmap support */
289 #endif /* HAVE_NTGUI */
293 void *bitmap
= ns_image_from_file (file
);
299 id
= x_allocate_bitmap_record (f
);
300 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
301 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
302 dpyinfo
->bitmaps
[id
- 1].file
= xlispstrdup (file
);
303 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
304 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
305 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
309 #ifdef HAVE_X_WINDOWS
310 unsigned int width
, height
;
312 int xhot
, yhot
, result
;
317 /* Look for an existing bitmap with the same name. */
318 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
320 if (dpyinfo
->bitmaps
[id
].refcount
321 && dpyinfo
->bitmaps
[id
].file
322 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
324 ++dpyinfo
->bitmaps
[id
].refcount
;
329 /* Search bitmap-file-path for the file, if appropriate. */
330 if (openp (Vx_bitmap_file_path
, file
, Qnil
, &found
,
331 make_number (R_OK
), false)
335 filename
= SSDATA (found
);
337 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
338 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
339 if (result
!= BitmapSuccess
)
342 id
= x_allocate_bitmap_record (f
);
343 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
344 dpyinfo
->bitmaps
[id
- 1].have_mask
= false;
345 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
346 dpyinfo
->bitmaps
[id
- 1].file
= xlispstrdup (file
);
347 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
348 dpyinfo
->bitmaps
[id
- 1].height
= height
;
349 dpyinfo
->bitmaps
[id
- 1].width
= width
;
352 #endif /* HAVE_X_WINDOWS */
358 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
360 #ifdef HAVE_X_WINDOWS
361 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
363 XFreePixmap (dpyinfo
->display
, bm
->mask
);
364 #endif /* HAVE_X_WINDOWS */
367 DeleteObject (bm
->pixmap
);
368 #endif /* HAVE_NTGUI */
371 ns_release_object (bm
->img
);
381 /* Remove reference to bitmap with id number ID. */
384 x_destroy_bitmap (struct frame
*f
, ptrdiff_t id
)
386 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
390 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
392 if (--bm
->refcount
== 0)
395 free_bitmap_record (dpyinfo
, bm
);
401 /* Free all the bitmaps for the display specified by DPYINFO. */
404 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
407 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
409 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
410 if (bm
->refcount
> 0)
411 free_bitmap_record (dpyinfo
, bm
);
413 dpyinfo
->bitmaps_last
= 0;
416 static bool x_create_x_image_and_pixmap (struct frame
*, int, int, int,
417 XImagePtr
*, Pixmap
*);
418 static void x_destroy_x_image (XImagePtr ximg
);
421 static XImagePtr_or_DC
image_get_x_image_or_dc (struct frame
*, struct image
*,
423 static void image_unget_x_image_or_dc (struct image
*, bool, XImagePtr_or_DC
,
426 static XImagePtr
image_get_x_image (struct frame
*, struct image
*, bool);
427 static void image_unget_x_image (struct image
*, bool, XImagePtr
);
428 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
429 image_get_x_image (f, img, mask_p)
430 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
431 image_unget_x_image (img, mask_p, ximg)
434 #ifdef HAVE_X_WINDOWS
436 static void image_sync_to_pixmaps (struct frame
*, struct image
*);
438 /* Useful functions defined in the section
439 `Image type independent image structures' below. */
441 static unsigned long four_corners_best (XImagePtr ximg
,
444 unsigned long height
);
447 /* Create a mask of a bitmap. Note is this not a perfect mask.
448 It's nicer with some borders in this context */
451 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
454 XImagePtr ximg
, mask_img
;
455 unsigned long width
, height
;
458 unsigned long x
, y
, xp
, xm
, yp
, ym
;
461 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
466 pixmap
= x_bitmap_pixmap (f
, id
);
467 width
= x_bitmap_width (f
, id
);
468 height
= x_bitmap_height (f
, id
);
471 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
480 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
485 XDestroyImage (ximg
);
489 bg
= four_corners_best (ximg
, NULL
, width
, height
);
491 for (y
= 0; y
< ximg
->height
; ++y
)
493 for (x
= 0; x
< ximg
->width
; ++x
)
495 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
496 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
497 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
498 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
499 if (XGetPixel (ximg
, x
, y
) == bg
500 && XGetPixel (ximg
, x
, yp
) == bg
501 && XGetPixel (ximg
, x
, ym
) == bg
502 && XGetPixel (ximg
, xp
, y
) == bg
503 && XGetPixel (ximg
, xp
, yp
) == bg
504 && XGetPixel (ximg
, xp
, ym
) == bg
505 && XGetPixel (ximg
, xm
, y
) == bg
506 && XGetPixel (ximg
, xm
, yp
) == bg
507 && XGetPixel (ximg
, xm
, ym
) == bg
)
508 XPutPixel (mask_img
, x
, y
, 0);
510 XPutPixel (mask_img
, x
, y
, 1);
514 eassert (input_blocked_p ());
515 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
516 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
518 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
520 dpyinfo
->bitmaps
[id
- 1].have_mask
= true;
521 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
523 XDestroyImage (ximg
);
524 x_destroy_x_image (mask_img
);
527 #endif /* HAVE_X_WINDOWS */
530 /***********************************************************************
532 ***********************************************************************/
534 /* List of supported image types. Use define_image_type to add new
535 types. Use lookup_image_type to find a type for a given symbol. */
537 static struct image_type
*image_types
;
539 /* The symbol `xbm' which is used as the type symbol for XBM images. */
541 static Lisp_Object Qxbm
;
545 Lisp_Object QCascent
, QCmargin
, QCrelief
;
546 Lisp_Object QCconversion
;
547 static Lisp_Object QCheuristic_mask
;
548 static Lisp_Object QCcolor_symbols
;
549 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
550 static Lisp_Object QCcrop
, QCrotation
;
554 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
555 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
557 /* Forward function prototypes. */
559 static struct image_type
*lookup_image_type (Lisp_Object
);
560 static void x_laplace (struct frame
*, struct image
*);
561 static void x_emboss (struct frame
*, struct image
*);
562 static void x_build_heuristic_mask (struct frame
*, struct image
*,
565 #define CACHE_IMAGE_TYPE(type, status) \
566 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
568 #define CACHE_IMAGE_TYPE(type, status)
571 #define ADD_IMAGE_TYPE(type) \
572 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
574 /* Define a new image type from TYPE. This adds a copy of TYPE to
575 image_types and caches the loading status of TYPE. */
577 static struct image_type
*
578 define_image_type (struct image_type
*type
)
580 struct image_type
*p
= NULL
;
581 Lisp_Object target_type
= *type
->type
;
586 for (p
= image_types
; p
; p
= p
->next
)
587 if (EQ (*p
->type
, target_type
))
592 #if defined HAVE_NTGUI && defined WINDOWSNT
593 /* If we failed to load the library before, don't try again. */
594 Lisp_Object tested
= Fassq (target_type
, Vlibrary_cache
);
595 if (CONSP (tested
) && NILP (XCDR (tested
)))
600 type_valid
= type
->init ();
601 CACHE_IMAGE_TYPE (target_type
, type_valid
? Qt
: Qnil
);
607 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
608 The initialized data segment is read-only. */
609 p
= xmalloc (sizeof *p
);
611 p
->next
= image_types
;
621 /* Value is true if OBJECT is a valid Lisp image specification. A
622 valid image specification is a list whose car is the symbol
623 `image', and whose rest is a property list. The property list must
624 contain a value for key `:type'. That value must be the name of a
625 supported image type. The rest of the property list depends on the
629 valid_image_p (Lisp_Object object
)
637 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
638 if (EQ (XCAR (tem
), QCtype
))
641 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
643 struct image_type
*type
;
644 type
= lookup_image_type (XCAR (tem
));
646 valid_p
= type
->valid_p (object
);
657 /* Log error message with format string FORMAT and argument ARG.
658 Signaling an error, e.g. when an image cannot be loaded, is not a
659 good idea because this would interrupt redisplay, and the error
660 message display would lead to another redisplay. This function
661 therefore simply displays a message. */
664 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
666 add_to_log (format
, arg1
, arg2
);
671 /***********************************************************************
673 ***********************************************************************/
675 enum image_value_type
677 IMAGE_DONT_CHECK_VALUE_TYPE
,
679 IMAGE_STRING_OR_NIL_VALUE
,
681 IMAGE_POSITIVE_INTEGER_VALUE
,
682 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
683 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
686 IMAGE_FUNCTION_VALUE
,
691 /* Structure used when parsing image specifications. */
695 /* Name of keyword. */
698 /* The type of value allowed. */
699 enum image_value_type type
;
701 /* True means key must be present. */
704 /* Used to recognize duplicate keywords in a property list. */
707 /* The value that was found. */
712 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
713 has the format (image KEYWORD VALUE ...). One of the keyword/
714 value pairs must be `:type TYPE'. KEYWORDS is a vector of
715 image_keywords structures of size NKEYWORDS describing other
716 allowed keyword/value pairs. Value is true if SPEC is valid. */
719 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
720 int nkeywords
, Lisp_Object type
)
729 while (CONSP (plist
))
731 Lisp_Object key
, value
;
733 /* First element of a pair must be a symbol. */
735 plist
= XCDR (plist
);
739 /* There must follow a value. */
742 value
= XCAR (plist
);
743 plist
= XCDR (plist
);
745 /* Find key in KEYWORDS. Error if not found. */
746 for (i
= 0; i
< nkeywords
; ++i
)
747 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
753 /* Record that we recognized the keyword. If a keywords
754 was found more than once, it's an error. */
755 keywords
[i
].value
= value
;
756 if (keywords
[i
].count
> 1)
760 /* Check type of value against allowed type. */
761 switch (keywords
[i
].type
)
763 case IMAGE_STRING_VALUE
:
764 if (!STRINGP (value
))
768 case IMAGE_STRING_OR_NIL_VALUE
:
769 if (!STRINGP (value
) && !NILP (value
))
773 case IMAGE_SYMBOL_VALUE
:
774 if (!SYMBOLP (value
))
778 case IMAGE_POSITIVE_INTEGER_VALUE
:
779 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
783 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
784 if (RANGED_INTEGERP (0, value
, INT_MAX
))
787 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
788 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
792 case IMAGE_ASCENT_VALUE
:
793 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
795 else if (RANGED_INTEGERP (0, value
, 100))
799 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
800 /* Unlike the other integer-related cases, this one does not
801 verify that VALUE fits in 'int'. This is because callers
803 if (!INTEGERP (value
) || XINT (value
) < 0)
807 case IMAGE_DONT_CHECK_VALUE_TYPE
:
810 case IMAGE_FUNCTION_VALUE
:
811 value
= indirect_function (value
);
812 if (!NILP (Ffunctionp (value
)))
816 case IMAGE_NUMBER_VALUE
:
817 if (!INTEGERP (value
) && !FLOATP (value
))
821 case IMAGE_INTEGER_VALUE
:
822 if (! TYPE_RANGED_INTEGERP (int, value
))
826 case IMAGE_BOOL_VALUE
:
827 if (!NILP (value
) && !EQ (value
, Qt
))
836 if (EQ (key
, QCtype
) && !EQ (type
, value
))
840 /* Check that all mandatory fields are present. */
841 for (i
= 0; i
< nkeywords
; ++i
)
842 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
849 /* Return the value of KEY in image specification SPEC. Value is nil
850 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
851 was found in SPEC. */
854 image_spec_value (Lisp_Object spec
, Lisp_Object key
, bool *found
)
858 eassert (valid_image_p (spec
));
860 for (tail
= XCDR (spec
);
861 CONSP (tail
) && CONSP (XCDR (tail
));
862 tail
= XCDR (XCDR (tail
)))
864 if (EQ (XCAR (tail
), key
))
868 return XCAR (XCDR (tail
));
878 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
879 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
880 PIXELS non-nil means return the size in pixels, otherwise return the
881 size in canonical character units.
882 FRAME is the frame on which the image will be displayed. FRAME nil
883 or omitted means use the selected frame. */)
884 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
889 if (valid_image_p (spec
))
891 struct frame
*f
= decode_window_system_frame (frame
);
892 ptrdiff_t id
= lookup_image (f
, spec
);
893 struct image
*img
= IMAGE_FROM_ID (f
, id
);
894 int width
= img
->width
+ 2 * img
->hmargin
;
895 int height
= img
->height
+ 2 * img
->vmargin
;
898 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
899 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
901 size
= Fcons (make_number (width
), make_number (height
));
904 error ("Invalid image specification");
910 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
911 doc
: /* Return t if image SPEC has a mask bitmap.
912 FRAME is the frame on which the image will be displayed. FRAME nil
913 or omitted means use the selected frame. */)
914 (Lisp_Object spec
, Lisp_Object frame
)
919 if (valid_image_p (spec
))
921 struct frame
*f
= decode_window_system_frame (frame
);
922 ptrdiff_t id
= lookup_image (f
, spec
);
923 struct image
*img
= IMAGE_FROM_ID (f
, id
);
928 error ("Invalid image specification");
933 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
934 doc
: /* Return metadata for image SPEC.
935 FRAME is the frame on which the image will be displayed. FRAME nil
936 or omitted means use the selected frame. */)
937 (Lisp_Object spec
, Lisp_Object frame
)
942 if (valid_image_p (spec
))
944 struct frame
*f
= decode_window_system_frame (frame
);
945 ptrdiff_t id
= lookup_image (f
, spec
);
946 struct image
*img
= IMAGE_FROM_ID (f
, id
);
947 ext
= img
->lisp_data
;
954 /***********************************************************************
955 Image type independent image structures
956 ***********************************************************************/
958 #define MAX_IMAGE_SIZE 10.0
959 /* Allocate and return a new image structure for image specification
960 SPEC. SPEC has a hash value of HASH. */
962 static struct image
*
963 make_image (Lisp_Object spec
, EMACS_UINT hash
)
965 struct image
*img
= xzalloc (sizeof *img
);
966 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
968 eassert (valid_image_p (spec
));
969 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
970 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
971 eassert (img
->type
!= NULL
);
973 img
->lisp_data
= Qnil
;
974 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
976 img
->corners
[BOT_CORNER
] = -1; /* Full image */
981 /* Free image IMG which was used on frame F, including its resources. */
984 free_image (struct frame
*f
, struct image
*img
)
988 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
990 /* Remove IMG from the hash table of its cache. */
992 img
->prev
->next
= img
->next
;
994 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
997 img
->next
->prev
= img
->prev
;
999 c
->images
[img
->id
] = NULL
;
1001 /* Windows NT redefines 'free', but in this file, we need to
1002 avoid the redefinition. */
1006 /* Free resources, then free IMG. */
1007 img
->type
->free (f
, img
);
1012 /* Return true if the given widths and heights are valid for display. */
1015 check_image_size (struct frame
*f
, int width
, int height
)
1019 if (width
<= 0 || height
<= 0)
1022 if (INTEGERP (Vmax_image_size
))
1023 return (width
<= XINT (Vmax_image_size
)
1024 && height
<= XINT (Vmax_image_size
));
1025 else if (FLOATP (Vmax_image_size
))
1029 w
= FRAME_PIXEL_WIDTH (f
);
1030 h
= FRAME_PIXEL_HEIGHT (f
);
1033 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1034 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1035 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1041 /* Prepare image IMG for display on frame F. Must be called before
1042 drawing an image. */
1045 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1047 /* We're about to display IMG, so set its timestamp to `now'. */
1048 img
->timestamp
= current_timespec ();
1050 /* If IMG doesn't have a pixmap yet, load it now, using the image
1051 type dependent loader function. */
1052 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1053 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1055 #ifdef HAVE_X_WINDOWS
1056 if (!img
->load_failed_p
)
1059 image_sync_to_pixmaps (f
, img
);
1066 /* Value is the number of pixels for the ascent of image IMG when
1067 drawn in face FACE. */
1070 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1075 if (slice
->height
== img
->height
)
1076 height
= img
->height
+ img
->vmargin
;
1077 else if (slice
->y
== 0)
1078 height
= slice
->height
+ img
->vmargin
;
1080 height
= slice
->height
;
1082 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1087 /* W32 specific version. Why?. ++kfs */
1088 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1089 - FONT_BASE (face
->font
)) / 2;
1091 /* This expression is arranged so that if the image can't be
1092 exactly centered, it will be moved slightly up. This is
1093 because a typical font is `top-heavy' (due to the presence
1094 uppercase letters), so the image placement should err towards
1095 being top-heavy too. It also just generally looks better. */
1096 ascent
= (height
+ FONT_BASE (face
->font
)
1097 - FONT_DESCENT (face
->font
) + 1) / 2;
1098 #endif /* HAVE_NTGUI */
1101 ascent
= height
/ 2;
1104 ascent
= height
* (img
->ascent
/ 100.0);
1110 /* Image background colors. */
1112 /* Find the "best" corner color of a bitmap.
1113 On W32, XIMG is assumed to a device context with the bitmap selected. */
1115 static RGB_PIXEL_COLOR
1116 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1117 unsigned long width
, unsigned long height
)
1119 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1122 if (corners
&& corners
[BOT_CORNER
] >= 0)
1124 /* Get the colors at the corner_pixels of ximg. */
1125 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1126 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1127 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1128 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1132 /* Get the colors at the corner_pixels of ximg. */
1133 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1134 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1135 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1136 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1138 /* Choose the most frequently found color as background. */
1139 for (i
= best_count
= 0; i
< 4; ++i
)
1143 for (j
= n
= 0; j
< 4; ++j
)
1144 if (corner_pixels
[i
] == corner_pixels
[j
])
1148 best
= corner_pixels
[i
], best_count
= n
;
1154 /* Portability macros */
1158 #define Free_Pixmap(display, pixmap) \
1159 DeleteObject (pixmap)
1161 #elif defined (HAVE_NS)
1163 #define Free_Pixmap(display, pixmap) \
1164 ns_release_object (pixmap)
1168 #define Free_Pixmap(display, pixmap) \
1169 XFreePixmap (display, pixmap)
1171 #endif /* !HAVE_NTGUI && !HAVE_NS */
1174 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1175 it is guessed heuristically. If non-zero, XIMG is an existing
1176 XImage object (or device context with the image selected on W32) to
1177 use for the heuristic. */
1180 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1182 if (! img
->background_valid
)
1183 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1185 bool free_ximg
= !ximg
;
1188 #endif /* HAVE_NTGUI */
1191 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
1193 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1196 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
1198 img
->background_valid
= 1;
1201 return img
->background
;
1204 /* Return the `background_transparent' field of IMG. If IMG doesn't
1205 have one yet, it is guessed heuristically. If non-zero, MASK is an
1206 existing XImage object to use for the heuristic. */
1209 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1211 if (! img
->background_transparent_valid
)
1212 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1216 bool free_mask
= !mask
;
1219 #endif /* HAVE_NTGUI */
1222 mask
= image_get_x_image_or_dc (f
, img
, 1, &prev
);
1224 img
->background_transparent
1225 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1228 image_unget_x_image_or_dc (img
, 1, mask
, prev
);
1231 img
->background_transparent
= 0;
1233 img
->background_transparent_valid
= 1;
1236 return img
->background_transparent
;
1239 #if defined (HAVE_PNG) || defined (HAVE_NS) \
1240 || defined (HAVE_IMAGEMAGICK) || defined (HAVE_RSVG)
1242 /* Store F's background color into *BGCOLOR. */
1244 x_query_frame_background_color (struct frame
*f
, XColor
*bgcolor
)
1247 bgcolor
->pixel
= FRAME_BACKGROUND_PIXEL (f
);
1248 x_query_color (f
, bgcolor
);
1250 ns_query_color (FRAME_BACKGROUND_COLOR (f
), bgcolor
, 1);
1254 #endif /* HAVE_PNG || HAVE_NS || HAVE_IMAGEMAGICK || HAVE_RSVG */
1256 /***********************************************************************
1257 Helper functions for X image types
1258 ***********************************************************************/
1260 /* Clear X resources of image IMG on frame F according to FLAGS.
1261 FLAGS is bitwise-or of the following masks:
1262 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1263 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1264 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1267 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1268 #define CLEAR_IMAGE_MASK (1 << 1)
1269 #define CLEAR_IMAGE_COLORS (1 << 2)
1272 x_clear_image_1 (struct frame
*f
, struct image
*img
, int flags
)
1274 if (flags
& CLEAR_IMAGE_PIXMAP
)
1278 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1279 img
->pixmap
= NO_PIXMAP
;
1280 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1281 img
->background_valid
= 0;
1283 #ifdef HAVE_X_WINDOWS
1286 x_destroy_x_image (img
->ximg
);
1288 img
->background_valid
= 0;
1293 if (flags
& CLEAR_IMAGE_MASK
)
1297 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1298 img
->mask
= NO_PIXMAP
;
1299 img
->background_transparent_valid
= 0;
1301 #ifdef HAVE_X_WINDOWS
1304 x_destroy_x_image (img
->mask_img
);
1305 img
->mask_img
= NULL
;
1306 img
->background_transparent_valid
= 0;
1311 if ((flags
& CLEAR_IMAGE_COLORS
) && img
->ncolors
)
1313 /* W32_TODO: color table support. */
1314 #ifdef HAVE_X_WINDOWS
1315 x_free_colors (f
, img
->colors
, img
->ncolors
);
1316 #endif /* HAVE_X_WINDOWS */
1317 xfree (img
->colors
);
1324 /* Free X resources of image IMG which is used on frame F. */
1327 x_clear_image (struct frame
*f
, struct image
*img
)
1330 x_clear_image_1 (f
, img
,
1331 CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_MASK
| CLEAR_IMAGE_COLORS
);
1336 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1337 cannot be allocated, use DFLT. Add a newly allocated color to
1338 IMG->colors, so that it can be freed again. Value is the pixel
1341 static unsigned long
1342 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1346 unsigned long result
;
1348 eassert (STRINGP (color_name
));
1350 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1351 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1354 /* This isn't called frequently so we get away with simply
1355 reallocating the color vector to the needed size, here. */
1356 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1357 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1358 img
->colors
[ncolors
- 1] = color
.pixel
;
1359 img
->ncolors
= ncolors
;
1360 result
= color
.pixel
;
1370 /***********************************************************************
1372 ***********************************************************************/
1374 static void cache_image (struct frame
*f
, struct image
*img
);
1376 /* Return a new, initialized image cache that is allocated from the
1377 heap. Call free_image_cache to free an image cache. */
1379 struct image_cache
*
1380 make_image_cache (void)
1382 struct image_cache
*c
= xmalloc (sizeof *c
);
1385 c
->used
= c
->refcount
= 0;
1386 c
->images
= xmalloc (c
->size
* sizeof *c
->images
);
1387 c
->buckets
= xzalloc (IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
);
1392 /* Find an image matching SPEC in the cache, and return it. If no
1393 image is found, return NULL. */
1394 static struct image
*
1395 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1398 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1399 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1401 if (!c
) return NULL
;
1403 /* If the image spec does not specify a background color, the cached
1404 image must have the same background color as the current frame.
1405 The foreground color must also match, for the sake of monochrome
1408 In fact, we could ignore the foreground color matching condition
1409 for color images, or if the image spec specifies :foreground;
1410 similarly we could ignore the background color matching condition
1411 for formats that don't use transparency (such as jpeg), or if the
1412 image spec specifies :background. However, the extra memory
1413 usage is probably negligible in practice, so we don't bother. */
1415 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1416 if (img
->hash
== hash
1417 && !NILP (Fequal (img
->spec
, spec
))
1418 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1419 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1425 /* Search frame F for an image with spec SPEC, and free it. */
1428 uncache_image (struct frame
*f
, Lisp_Object spec
)
1430 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1433 free_image (f
, img
);
1434 /* As display glyphs may still be referring to the image ID, we
1435 must garbage the frame (Bug#6426). */
1436 SET_FRAME_GARBAGED (f
);
1441 /* Free image cache of frame F. Be aware that X frames share images
1445 free_image_cache (struct frame
*f
)
1447 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1452 /* Cache should not be referenced by any frame when freed. */
1453 eassert (c
->refcount
== 0);
1455 for (i
= 0; i
< c
->used
; ++i
)
1456 free_image (f
, c
->images
[i
]);
1460 FRAME_IMAGE_CACHE (f
) = NULL
;
1465 /* Clear image cache of frame F. FILTER=t means free all images.
1466 FILTER=nil means clear only images that haven't been
1467 displayed for some time.
1468 Else, only free the images which have FILTER in their `dependencies'.
1469 Should be called from time to time to reduce the number of loaded images.
1470 If image-cache-eviction-delay is non-nil, this frees images in the cache
1471 which weren't displayed for at least that many seconds. */
1474 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1476 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1480 ptrdiff_t i
, nfreed
= 0;
1482 /* Block input so that we won't be interrupted by a SIGIO
1483 while being in an inconsistent state. */
1488 /* Filter image cache. */
1489 for (i
= 0; i
< c
->used
; ++i
)
1491 struct image
*img
= c
->images
[i
];
1492 if (img
&& (EQ (Qt
, filter
)
1493 || !NILP (Fmember (filter
, img
->dependencies
))))
1495 free_image (f
, img
);
1500 else if (INTEGERP (Vimage_cache_eviction_delay
))
1502 /* Free cache based on timestamp. */
1503 struct timespec old
, t
;
1505 ptrdiff_t nimages
= 0;
1507 for (i
= 0; i
< c
->used
; ++i
)
1511 /* If the number of cached images has grown unusually large,
1512 decrease the cache eviction delay (Bug#6230). */
1513 delay
= XINT (Vimage_cache_eviction_delay
);
1515 delay
= 1600 * delay
/ nimages
/ nimages
;
1516 delay
= max (delay
, 1);
1518 t
= current_timespec ();
1519 old
= timespec_sub (t
, dtotimespec (delay
));
1521 for (i
= 0; i
< c
->used
; ++i
)
1523 struct image
*img
= c
->images
[i
];
1524 if (img
&& timespec_cmp (img
->timestamp
, old
) < 0)
1526 free_image (f
, img
);
1532 /* We may be clearing the image cache because, for example,
1533 Emacs was iconified for a longer period of time. In that
1534 case, current matrices may still contain references to
1535 images freed above. So, clear these matrices. */
1538 Lisp_Object tail
, frame
;
1540 FOR_EACH_FRAME (tail
, frame
)
1542 struct frame
*fr
= XFRAME (frame
);
1543 if (FRAME_IMAGE_CACHE (fr
) == c
)
1544 clear_current_matrices (fr
);
1547 windows_or_buffers_changed
= 19;
1555 clear_image_caches (Lisp_Object filter
)
1557 /* FIXME: We want to do
1558 * struct terminal *t;
1559 * for (t = terminal_list; t; t = t->next_terminal)
1560 * clear_image_cache (t, filter); */
1561 Lisp_Object tail
, frame
;
1562 FOR_EACH_FRAME (tail
, frame
)
1563 if (FRAME_WINDOW_P (XFRAME (frame
)))
1564 clear_image_cache (XFRAME (frame
), filter
);
1567 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1569 doc
: /* Clear the image cache.
1570 FILTER nil or a frame means clear all images in the selected frame.
1571 FILTER t means clear the image caches of all frames.
1572 Anything else, means only clear those images which refer to FILTER,
1573 which is then usually a filename. */)
1574 (Lisp_Object filter
)
1576 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1577 clear_image_caches (filter
);
1579 clear_image_cache (decode_window_system_frame (filter
), Qt
);
1585 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1587 doc
: /* Flush the image with specification SPEC on frame FRAME.
1588 This removes the image from the Emacs image cache. If SPEC specifies
1589 an image file, the next redisplay of this image will read from the
1590 current contents of that file.
1592 FRAME nil or omitted means use the selected frame.
1593 FRAME t means refresh the image on all frames. */)
1594 (Lisp_Object spec
, Lisp_Object frame
)
1596 if (!valid_image_p (spec
))
1597 error ("Invalid image specification");
1602 FOR_EACH_FRAME (tail
, frame
)
1604 struct frame
*f
= XFRAME (frame
);
1605 if (FRAME_WINDOW_P (f
))
1606 uncache_image (f
, spec
);
1610 uncache_image (decode_window_system_frame (frame
), spec
);
1616 /* Compute masks and transform image IMG on frame F, as specified
1617 by the image's specification, */
1620 postprocess_image (struct frame
*f
, struct image
*img
)
1622 /* Manipulation of the image's mask. */
1625 Lisp_Object conversion
, spec
;
1630 /* `:heuristic-mask t'
1632 means build a mask heuristically.
1633 `:heuristic-mask (R G B)'
1634 `:mask (heuristic (R G B))'
1635 means build a mask from color (R G B) in the
1638 means remove a mask, if any. */
1640 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1642 x_build_heuristic_mask (f
, img
, mask
);
1647 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1649 if (EQ (mask
, Qheuristic
))
1650 x_build_heuristic_mask (f
, img
, Qt
);
1651 else if (CONSP (mask
)
1652 && EQ (XCAR (mask
), Qheuristic
))
1654 if (CONSP (XCDR (mask
)))
1655 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1657 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1659 else if (NILP (mask
) && found_p
&& img
->mask
)
1660 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
1664 /* Should we apply an image transformation algorithm? */
1665 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1666 if (EQ (conversion
, Qdisabled
))
1667 x_disable_image (f
, img
);
1668 else if (EQ (conversion
, Qlaplace
))
1670 else if (EQ (conversion
, Qemboss
))
1672 else if (CONSP (conversion
)
1673 && EQ (XCAR (conversion
), Qedge_detection
))
1676 tem
= XCDR (conversion
);
1678 x_edge_detection (f
, img
,
1679 Fplist_get (tem
, QCmatrix
),
1680 Fplist_get (tem
, QCcolor_adjustment
));
1686 /* Return the id of image with Lisp specification SPEC on frame F.
1687 SPEC must be a valid Lisp image specification (see valid_image_p). */
1690 lookup_image (struct frame
*f
, Lisp_Object spec
)
1695 /* F must be a window-system frame, and SPEC must be a valid image
1697 eassert (FRAME_WINDOW_P (f
));
1698 eassert (valid_image_p (spec
));
1700 /* Look up SPEC in the hash table of the image cache. */
1701 hash
= sxhash (spec
, 0);
1702 img
= search_image_cache (f
, spec
, hash
);
1703 if (img
&& img
->load_failed_p
)
1705 free_image (f
, img
);
1709 /* If not found, create a new image and cache it. */
1713 img
= make_image (spec
, hash
);
1714 cache_image (f
, img
);
1715 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1716 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1717 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1719 /* If we can't load the image, and we don't have a width and
1720 height, use some arbitrary width and height so that we can
1721 draw a rectangle for it. */
1722 if (img
->load_failed_p
)
1726 value
= image_spec_value (spec
, QCwidth
, NULL
);
1727 img
->width
= (INTEGERP (value
)
1728 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1729 value
= image_spec_value (spec
, QCheight
, NULL
);
1730 img
->height
= (INTEGERP (value
)
1731 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1735 /* Handle image type independent image attributes
1736 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1737 `:background COLOR'. */
1738 Lisp_Object ascent
, margin
, relief
, bg
;
1741 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1742 if (INTEGERP (ascent
))
1743 img
->ascent
= XFASTINT (ascent
);
1744 else if (EQ (ascent
, Qcenter
))
1745 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1747 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1748 if (INTEGERP (margin
))
1749 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1750 else if (CONSP (margin
))
1752 img
->hmargin
= XFASTINT (XCAR (margin
));
1753 img
->vmargin
= XFASTINT (XCDR (margin
));
1756 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1757 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1758 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1760 img
->relief
= XINT (relief
);
1761 img
->hmargin
+= eabs (img
->relief
);
1762 img
->vmargin
+= eabs (img
->relief
);
1765 if (! img
->background_valid
)
1767 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1771 = x_alloc_image_color (f
, img
, bg
,
1772 FRAME_BACKGROUND_PIXEL (f
));
1773 img
->background_valid
= 1;
1777 /* Do image transformations and compute masks, unless we
1778 don't have the image yet. */
1779 if (!EQ (*img
->type
->type
, Qpostscript
))
1780 postprocess_image (f
, img
);
1786 /* We're using IMG, so set its timestamp to `now'. */
1787 img
->timestamp
= current_timespec ();
1789 /* Value is the image id. */
1794 /* Cache image IMG in the image cache of frame F. */
1797 cache_image (struct frame
*f
, struct image
*img
)
1799 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1802 /* Find a free slot in c->images. */
1803 for (i
= 0; i
< c
->used
; ++i
)
1804 if (c
->images
[i
] == NULL
)
1807 /* If no free slot found, maybe enlarge c->images. */
1808 if (i
== c
->used
&& c
->used
== c
->size
)
1809 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1811 /* Add IMG to c->images, and assign IMG an id. */
1817 /* Add IMG to the cache's hash table. */
1818 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1819 img
->next
= c
->buckets
[i
];
1821 img
->next
->prev
= img
;
1823 c
->buckets
[i
] = img
;
1827 /* Call FN on every image in the image cache of frame F. Used to mark
1828 Lisp Objects in the image cache. */
1830 /* Mark Lisp objects in image IMG. */
1833 mark_image (struct image
*img
)
1835 mark_object (img
->spec
);
1836 mark_object (img
->dependencies
);
1838 if (!NILP (img
->lisp_data
))
1839 mark_object (img
->lisp_data
);
1844 mark_image_cache (struct image_cache
*c
)
1849 for (i
= 0; i
< c
->used
; ++i
)
1851 mark_image (c
->images
[i
]);
1857 /***********************************************************************
1858 X / NS / W32 support code
1859 ***********************************************************************/
1863 /* Macro for defining functions that will be loaded from image DLLs. */
1864 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1866 /* Macro for loading those image functions from the library. */
1867 #define LOAD_IMGLIB_FN(lib,func) { \
1868 fn_##func = (void *) GetProcAddress (lib, #func); \
1869 if (!fn_##func) return 0; \
1872 #endif /* WINDOWSNT */
1874 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1876 WIDTH and HEIGHT must both be positive.
1877 If XIMG is null, assume it is a bitmap. */
1879 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1881 #ifdef HAVE_X_WINDOWS
1882 /* Respect Xlib's limits: it cannot deal with images that have more
1883 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1884 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1887 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1888 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1891 int bitmap_pad
, depth
, bytes_per_line
;
1894 bitmap_pad
= ximg
->bitmap_pad
;
1895 depth
= ximg
->depth
;
1896 bytes_per_line
= ximg
->bytes_per_line
;
1902 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1904 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1905 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1907 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1908 For now, assume that every image size is allowed on these systems. */
1913 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1914 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1915 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1916 via xmalloc. Print error messages via image_error if an error
1917 occurs. Value is true if successful.
1919 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1920 should indicate the bit depth of the image. */
1923 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1924 XImagePtr
*ximg
, Pixmap
*pixmap
)
1926 #ifdef HAVE_X_WINDOWS
1927 Display
*display
= FRAME_X_DISPLAY (f
);
1928 Window window
= FRAME_X_WINDOW (f
);
1929 Screen
*screen
= FRAME_X_SCREEN (f
);
1931 eassert (input_blocked_p ());
1934 depth
= DefaultDepthOfScreen (screen
);
1935 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1936 depth
, ZPixmap
, 0, NULL
, width
, height
,
1937 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1940 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1944 if (! x_check_image_size (*ximg
, width
, height
))
1946 x_destroy_x_image (*ximg
);
1948 image_error ("Image too large (%dx%d)",
1949 make_number (width
), make_number (height
));
1953 /* Allocate image raster. */
1954 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1956 /* Allocate a pixmap of the same size. */
1957 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1958 if (*pixmap
== NO_PIXMAP
)
1960 x_destroy_x_image (*ximg
);
1962 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1967 #endif /* HAVE_X_WINDOWS */
1971 BITMAPINFOHEADER
*header
;
1973 int scanline_width_bits
;
1975 int palette_colors
= 0;
1980 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1981 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1983 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1987 scanline_width_bits
= width
* depth
;
1988 remainder
= scanline_width_bits
% 32;
1991 scanline_width_bits
+= 32 - remainder
;
1993 /* Bitmaps with a depth less than 16 need a palette. */
1994 /* BITMAPINFO structure already contains the first RGBQUAD. */
1996 palette_colors
= 1 << (depth
- 1);
1998 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2000 header
= &(*ximg
)->info
.bmiHeader
;
2001 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2002 header
->biSize
= sizeof (*header
);
2003 header
->biWidth
= width
;
2004 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2005 header
->biPlanes
= 1;
2006 header
->biBitCount
= depth
;
2007 header
->biCompression
= BI_RGB
;
2008 header
->biClrUsed
= palette_colors
;
2010 /* TODO: fill in palette. */
2013 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2014 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2015 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2016 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2017 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2018 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2019 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2020 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2023 hdc
= get_frame_dc (f
);
2025 /* Create a DIBSection and raster array for the bitmap,
2026 and store its handle in *pixmap. */
2027 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2028 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2029 /* casting avoids a GCC warning */
2030 (void **)&((*ximg
)->data
), NULL
, 0);
2032 /* Realize display palette and garbage all frames. */
2033 release_frame_dc (f
, hdc
);
2035 if (*pixmap
== NULL
)
2037 DWORD err
= GetLastError ();
2038 Lisp_Object errcode
;
2039 /* All system errors are < 10000, so the following is safe. */
2040 XSETINT (errcode
, err
);
2041 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2042 x_destroy_x_image (*ximg
);
2049 #endif /* HAVE_NTGUI */
2052 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2056 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2065 /* Destroy XImage XIMG. Free XIMG->data. */
2068 x_destroy_x_image (XImagePtr ximg
)
2070 eassert (input_blocked_p ());
2073 #ifdef HAVE_X_WINDOWS
2076 XDestroyImage (ximg
);
2077 #endif /* HAVE_X_WINDOWS */
2079 /* Data will be freed by DestroyObject. */
2082 #endif /* HAVE_NTGUI */
2084 ns_release_object (ximg
);
2085 #endif /* HAVE_NS */
2090 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2091 are width and height of both the image and pixmap. */
2094 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2096 #ifdef HAVE_X_WINDOWS
2099 eassert (input_blocked_p ());
2100 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2101 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2102 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2103 #endif /* HAVE_X_WINDOWS */
2106 #if 0 /* I don't think this is necessary looking at where it is used. */
2107 HDC hdc
= get_frame_dc (f
);
2108 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2109 release_frame_dc (f
, hdc
);
2111 #endif /* HAVE_NTGUI */
2114 eassert (ximg
== pixmap
);
2115 ns_retain_object (ximg
);
2119 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2120 with image_put_x_image. */
2123 image_create_x_image_and_pixmap (struct frame
*f
, struct image
*img
,
2124 int width
, int height
, int depth
,
2125 XImagePtr
*ximg
, bool mask_p
)
2127 eassert ((!mask_p
? img
->pixmap
: img
->mask
) == NO_PIXMAP
);
2129 return x_create_x_image_and_pixmap (f
, width
, height
, depth
, ximg
,
2130 !mask_p
? &img
->pixmap
: &img
->mask
);
2133 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2134 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2135 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2136 On the other platforms, it puts XIMG into the pixmap, then frees
2137 the X image and its buffer. */
2140 image_put_x_image (struct frame
*f
, struct image
*img
, XImagePtr ximg
,
2143 #ifdef HAVE_X_WINDOWS
2146 eassert (img
->ximg
== NULL
);
2151 eassert (img
->mask_img
== NULL
);
2152 img
->mask_img
= ximg
;
2155 x_put_x_image (f
, ximg
, !mask_p
? img
->pixmap
: img
->mask
,
2156 img
->width
, img
->height
);
2157 x_destroy_x_image (ximg
);
2161 #ifdef HAVE_X_WINDOWS
2162 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2163 the X images and their buffers. */
2166 image_sync_to_pixmaps (struct frame
*f
, struct image
*img
)
2170 x_put_x_image (f
, img
->ximg
, img
->pixmap
, img
->width
, img
->height
);
2171 x_destroy_x_image (img
->ximg
);
2176 x_put_x_image (f
, img
->mask_img
, img
->mask
, img
->width
, img
->height
);
2177 x_destroy_x_image (img
->mask_img
);
2178 img
->mask_img
= NULL
;
2184 /* Create a memory device context for IMG on frame F. It stores the
2185 currently selected GDI object into *PREV for future restoration by
2186 image_unget_x_image_or_dc. */
2188 static XImagePtr_or_DC
2189 image_get_x_image_or_dc (struct frame
*f
, struct image
*img
, bool mask_p
,
2192 HDC frame_dc
= get_frame_dc (f
);
2193 XImagePtr_or_DC ximg
= CreateCompatibleDC (frame_dc
);
2195 release_frame_dc (f
, frame_dc
);
2196 *prev
= SelectObject (ximg
, !mask_p
? img
->pixmap
: img
->mask
);
2202 image_unget_x_image_or_dc (struct image
*img
, bool mask_p
,
2203 XImagePtr_or_DC ximg
, HGDIOBJ prev
)
2205 SelectObject (ximg
, prev
);
2208 #else /* !HAVE_NTGUI */
2209 /* Get the X image for IMG on frame F. The resulting X image data
2210 should be treated as read-only at least on X. */
2213 image_get_x_image (struct frame
*f
, struct image
*img
, bool mask_p
)
2215 #ifdef HAVE_X_WINDOWS
2216 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2221 return XGetImage (FRAME_X_DISPLAY (f
), !mask_p
? img
->pixmap
: img
->mask
,
2222 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
2223 #elif defined (HAVE_NS)
2224 XImagePtr pixmap
= !mask_p
? img
->pixmap
: img
->mask
;
2226 ns_retain_object (pixmap
);
2232 image_unget_x_image (struct image
*img
, bool mask_p
, XImagePtr ximg
)
2234 #ifdef HAVE_X_WINDOWS
2235 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2238 eassert (ximg
== ximg_in_img
);
2240 XDestroyImage (ximg
);
2241 #elif defined (HAVE_NS)
2242 ns_release_object (ximg
);
2245 #endif /* !HAVE_NTGUI */
2248 /***********************************************************************
2250 ***********************************************************************/
2252 /* Find image file FILE. Look in data-directory/images, then
2253 x-bitmap-file-path. Value is the encoded full name of the file
2254 found, or nil if not found. */
2257 x_find_image_file (Lisp_Object file
)
2259 Lisp_Object file_found
, search_path
;
2262 /* TODO I think this should use something like image-load-path
2263 instead. Unfortunately, that can contain non-string elements. */
2264 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2266 Vx_bitmap_file_path
);
2268 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2269 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
, false);
2275 file_found
= ENCODE_FILE (file_found
);
2284 /* Read FILE into memory. Value is a pointer to a buffer allocated
2285 with xmalloc holding FILE's contents. Value is null if an error
2286 occurred. *SIZE is set to the size of the file. */
2288 static unsigned char *
2289 slurp_file (char *file
, ptrdiff_t *size
)
2291 FILE *fp
= emacs_fopen (file
, "rb");
2292 unsigned char *buf
= NULL
;
2297 ptrdiff_t count
= SPECPDL_INDEX ();
2298 record_unwind_protect_ptr (fclose_unwind
, fp
);
2300 if (fstat (fileno (fp
), &st
) == 0
2301 && 0 <= st
.st_size
&& st
.st_size
< min (PTRDIFF_MAX
, SIZE_MAX
))
2303 /* Report an error if we read past the purported EOF.
2304 This can happen if the file grows as we read it. */
2305 ptrdiff_t buflen
= st
.st_size
;
2306 buf
= xmalloc (buflen
+ 1);
2307 if (fread (buf
, 1, buflen
+ 1, fp
) == buflen
)
2316 unbind_to (count
, Qnil
);
2324 /***********************************************************************
2326 ***********************************************************************/
2328 static bool xbm_load (struct frame
*f
, struct image
*img
);
2329 static bool xbm_image_p (Lisp_Object object
);
2330 static bool xbm_file_p (Lisp_Object
);
2333 /* Indices of image specification fields in xbm_format, below. */
2335 enum xbm_keyword_index
2353 /* Vector of image_keyword structures describing the format
2354 of valid XBM image specifications. */
2356 static const struct image_keyword xbm_format
[XBM_LAST
] =
2358 {":type", IMAGE_SYMBOL_VALUE
, 1},
2359 {":file", IMAGE_STRING_VALUE
, 0},
2360 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2361 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2362 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2363 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2364 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2365 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2366 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2367 {":relief", IMAGE_INTEGER_VALUE
, 0},
2368 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2369 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2370 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2373 /* Structure describing the image type XBM. */
2375 static struct image_type xbm_type
=
2385 /* Tokens returned from xbm_scan. */
2394 /* Return true if OBJECT is a valid XBM-type image specification.
2395 A valid specification is a list starting with the symbol `image'
2396 The rest of the list is a property list which must contain an
2399 If the specification specifies a file to load, it must contain
2400 an entry `:file FILENAME' where FILENAME is a string.
2402 If the specification is for a bitmap loaded from memory it must
2403 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2404 WIDTH and HEIGHT are integers > 0. DATA may be:
2406 1. a string large enough to hold the bitmap data, i.e. it must
2407 have a size >= (WIDTH + 7) / 8 * HEIGHT
2409 2. a bool-vector of size >= WIDTH * HEIGHT
2411 3. a vector of strings or bool-vectors, one for each line of the
2414 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2415 may not be specified in this case because they are defined in the
2418 Both the file and data forms may contain the additional entries
2419 `:background COLOR' and `:foreground COLOR'. If not present,
2420 foreground and background of the frame on which the image is
2421 displayed is used. */
2424 xbm_image_p (Lisp_Object object
)
2426 struct image_keyword kw
[XBM_LAST
];
2428 memcpy (kw
, xbm_format
, sizeof kw
);
2429 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2432 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2434 if (kw
[XBM_FILE
].count
)
2436 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2439 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2441 /* In-memory XBM file. */
2442 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2450 /* Entries for `:width', `:height' and `:data' must be present. */
2451 if (!kw
[XBM_WIDTH
].count
2452 || !kw
[XBM_HEIGHT
].count
2453 || !kw
[XBM_DATA
].count
)
2456 data
= kw
[XBM_DATA
].value
;
2457 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2458 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2460 /* Check type of data, and width and height against contents of
2466 /* Number of elements of the vector must be >= height. */
2467 if (ASIZE (data
) < height
)
2470 /* Each string or bool-vector in data must be large enough
2471 for one line of the image. */
2472 for (i
= 0; i
< height
; ++i
)
2474 Lisp_Object elt
= AREF (data
, i
);
2479 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2482 else if (BOOL_VECTOR_P (elt
))
2484 if (bool_vector_size (elt
) < width
)
2491 else if (STRINGP (data
))
2494 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2497 else if (BOOL_VECTOR_P (data
))
2499 if (bool_vector_size (data
) / height
< width
)
2510 /* Scan a bitmap file. FP is the stream to read from. Value is
2511 either an enumerator from enum xbm_token, or a character for a
2512 single-character token, or 0 at end of file. If scanning an
2513 identifier, store the lexeme of the identifier in SVAL. If
2514 scanning a number, store its value in *IVAL. */
2517 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2523 /* Skip white space. */
2524 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2529 else if (c_isdigit (c
))
2531 int value
= 0, digit
;
2533 if (c
== '0' && *s
< end
)
2536 if (c
== 'x' || c
== 'X')
2543 else if (c
>= 'a' && c
<= 'f')
2544 digit
= c
- 'a' + 10;
2545 else if (c
>= 'A' && c
<= 'F')
2546 digit
= c
- 'A' + 10;
2549 value
= 16 * value
+ digit
;
2552 else if (c_isdigit (c
))
2556 && (c
= *(*s
)++, c_isdigit (c
)))
2557 value
= 8 * value
+ c
- '0';
2564 && (c
= *(*s
)++, c_isdigit (c
)))
2565 value
= 10 * value
+ c
- '0';
2573 else if (c_isalpha (c
) || c
== '_')
2577 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2584 else if (c
== '/' && **s
== '*')
2586 /* C-style comment. */
2588 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2602 /* Create a Windows bitmap from X bitmap data. */
2604 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2606 static unsigned char swap_nibble
[16]
2607 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2608 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2609 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2610 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2612 unsigned char *bits
, *p
;
2615 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2616 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2617 bits
= alloca (height
* w2
);
2618 memset (bits
, 0, height
* w2
);
2619 for (i
= 0; i
< height
; i
++)
2622 for (j
= 0; j
< w1
; j
++)
2624 /* Bitswap XBM bytes to match how Windows does things. */
2625 unsigned char c
= *data
++;
2626 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2627 | (swap_nibble
[(c
>>4) & 0xf]));
2630 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2636 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2637 COLORREF foreground
, COLORREF background
)
2639 HDC hdc
, old_img_dc
, new_img_dc
;
2640 HGDIOBJ old_prev
, new_prev
;
2643 hdc
= get_frame_dc (f
);
2644 old_img_dc
= CreateCompatibleDC (hdc
);
2645 new_img_dc
= CreateCompatibleDC (hdc
);
2646 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2647 release_frame_dc (f
, hdc
);
2648 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2649 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2650 /* Windows convention for mono bitmaps is black = background,
2651 white = foreground. */
2652 SetTextColor (new_img_dc
, background
);
2653 SetBkColor (new_img_dc
, foreground
);
2655 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2658 SelectObject (old_img_dc
, old_prev
);
2659 SelectObject (new_img_dc
, new_prev
);
2660 DeleteDC (old_img_dc
);
2661 DeleteDC (new_img_dc
);
2662 DeleteObject (img
->pixmap
);
2663 if (new_pixmap
== 0)
2664 fprintf (stderr
, "Failed to convert image to color.\n");
2666 img
->pixmap
= new_pixmap
;
2669 #define XBM_BIT_SHUFFLE(b) (~(b))
2673 #define XBM_BIT_SHUFFLE(b) (b)
2675 #endif /* HAVE_NTGUI */
2679 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2680 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2681 bool non_default_colors
)
2685 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2687 /* If colors were specified, transfer the bitmap to a color one. */
2688 if (non_default_colors
)
2689 convert_mono_to_color_image (f
, img
, fg
, bg
);
2691 #elif defined (HAVE_NS)
2692 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2696 (x_check_image_size (0, img
->width
, img
->height
)
2697 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2700 img
->width
, img
->height
,
2702 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2704 #endif /* !HAVE_NTGUI && !HAVE_NS */
2709 /* Replacement for XReadBitmapFileData which isn't available under old
2710 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2711 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2712 the image. Return in *DATA the bitmap data allocated with xmalloc.
2713 Value is true if successful. DATA null means just test if
2714 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2715 inhibit the call to image_error when the image size is invalid (the
2716 bitmap remains unread). */
2719 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2720 int *width
, int *height
, char **data
,
2721 bool inhibit_image_error
)
2723 unsigned char *s
= contents
;
2724 char buffer
[BUFSIZ
];
2727 int bytes_per_line
, i
, nbytes
;
2733 LA1 = xbm_scan (&s, end, buffer, &value)
2735 #define expect(TOKEN) \
2738 if (LA1 != (TOKEN)) \
2744 #define expect_ident(IDENT) \
2745 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2750 *width
= *height
= -1;
2753 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2755 /* Parse defines for width, height and hot-spots. */
2759 expect_ident ("define");
2760 expect (XBM_TK_IDENT
);
2762 if (LA1
== XBM_TK_NUMBER
)
2764 char *q
= strrchr (buffer
, '_');
2765 q
= q
? q
+ 1 : buffer
;
2766 if (strcmp (q
, "width") == 0)
2768 else if (strcmp (q
, "height") == 0)
2771 expect (XBM_TK_NUMBER
);
2774 if (!check_image_size (f
, *width
, *height
))
2776 if (!inhibit_image_error
)
2777 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2780 else if (data
== NULL
)
2783 /* Parse bits. Must start with `static'. */
2784 expect_ident ("static");
2785 if (LA1
== XBM_TK_IDENT
)
2787 if (strcmp (buffer
, "unsigned") == 0)
2790 expect_ident ("char");
2792 else if (strcmp (buffer
, "short") == 0)
2796 if (*width
% 16 && *width
% 16 < 9)
2799 else if (strcmp (buffer
, "char") == 0)
2807 expect (XBM_TK_IDENT
);
2813 if (! x_check_image_size (0, *width
, *height
))
2815 if (!inhibit_image_error
)
2816 image_error ("Image too large (%dx%d)",
2817 make_number (*width
), make_number (*height
));
2820 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2821 nbytes
= bytes_per_line
* *height
;
2822 p
= *data
= xmalloc (nbytes
);
2826 for (i
= 0; i
< nbytes
; i
+= 2)
2829 expect (XBM_TK_NUMBER
);
2831 *p
++ = XBM_BIT_SHUFFLE (val
);
2832 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2833 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2835 if (LA1
== ',' || LA1
== '}')
2843 for (i
= 0; i
< nbytes
; ++i
)
2846 expect (XBM_TK_NUMBER
);
2848 *p
++ = XBM_BIT_SHUFFLE (val
);
2850 if (LA1
== ',' || LA1
== '}')
2875 /* Load XBM image IMG which will be displayed on frame F from buffer
2876 CONTENTS. END is the end of the buffer. Value is true if
2880 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2887 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2891 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2892 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2893 bool non_default_colors
= 0;
2896 eassert (img
->width
> 0 && img
->height
> 0);
2898 /* Get foreground and background colors, maybe allocate colors. */
2899 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2902 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2903 non_default_colors
= 1;
2905 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2908 background
= x_alloc_image_color (f
, img
, value
, background
);
2909 img
->background
= background
;
2910 img
->background_valid
= 1;
2911 non_default_colors
= 1;
2914 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2915 foreground
, background
,
2916 non_default_colors
);
2919 if (img
->pixmap
== NO_PIXMAP
)
2921 x_clear_image (f
, img
);
2922 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2928 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2934 /* Value is true if DATA looks like an in-memory XBM file. */
2937 xbm_file_p (Lisp_Object data
)
2940 return (STRINGP (data
)
2941 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2942 (SDATA (data
) + SBYTES (data
)),
2947 /* Fill image IMG which is used on frame F with pixmap data. Value is
2948 true if successful. */
2951 xbm_load (struct frame
*f
, struct image
*img
)
2954 Lisp_Object file_name
;
2956 eassert (xbm_image_p (img
->spec
));
2958 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2959 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2960 if (STRINGP (file_name
))
2963 unsigned char *contents
;
2966 file
= x_find_image_file (file_name
);
2967 if (!STRINGP (file
))
2969 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2973 contents
= slurp_file (SSDATA (file
), &size
);
2974 if (contents
== NULL
)
2976 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2980 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2985 struct image_keyword fmt
[XBM_LAST
];
2987 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2988 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2989 bool non_default_colors
= 0;
2992 bool in_memory_file_p
= 0;
2994 /* See if data looks like an in-memory XBM file. */
2995 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2996 in_memory_file_p
= xbm_file_p (data
);
2998 /* Parse the image specification. */
2999 memcpy (fmt
, xbm_format
, sizeof fmt
);
3000 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
3003 /* Get specified width, and height. */
3004 if (!in_memory_file_p
)
3006 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
3007 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
3008 eassert (img
->width
> 0 && img
->height
> 0);
3009 if (!check_image_size (f
, img
->width
, img
->height
))
3011 image_error ("Invalid image size (see `max-image-size')",
3017 /* Get foreground and background colors, maybe allocate colors. */
3018 if (fmt
[XBM_FOREGROUND
].count
3019 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
3021 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
3023 non_default_colors
= 1;
3026 if (fmt
[XBM_BACKGROUND
].count
3027 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
3029 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
3031 non_default_colors
= 1;
3034 if (in_memory_file_p
)
3035 success_p
= xbm_load_image (f
, img
, SDATA (data
),
3046 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
3048 SAFE_NALLOCA (bits
, nbytes
, img
->height
);
3050 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
3052 Lisp_Object line
= AREF (data
, i
);
3054 memcpy (p
, SDATA (line
), nbytes
);
3056 memcpy (p
, bool_vector_data (line
), nbytes
);
3059 else if (STRINGP (data
))
3060 bits
= SSDATA (data
);
3062 bits
= (char *) bool_vector_data (data
);
3068 /* Windows mono bitmaps are reversed compared with X. */
3069 invertedBits
= bits
;
3070 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
3071 SAFE_NALLOCA (bits
, nbytes
, img
->height
);
3072 for (i
= 0; i
< nbytes
; i
++)
3073 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
3076 /* Create the pixmap. */
3078 if (x_check_image_size (0, img
->width
, img
->height
))
3079 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
3080 foreground
, background
,
3081 non_default_colors
);
3083 img
->pixmap
= NO_PIXMAP
;
3089 image_error ("Unable to create pixmap for XBM image `%s'",
3091 x_clear_image (f
, img
);
3103 /***********************************************************************
3105 ***********************************************************************/
3107 #if defined (HAVE_XPM) || defined (HAVE_NS)
3109 static bool xpm_image_p (Lisp_Object object
);
3110 static bool xpm_load (struct frame
*f
, struct image
*img
);
3112 #endif /* HAVE_XPM || HAVE_NS */
3116 /* Indicate to xpm.h that we don't have Xlib. */
3118 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3119 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3120 #define XColor xpm_XColor
3121 #define XImage xpm_XImage
3122 #define Display xpm_Display
3123 #define PIXEL_ALREADY_TYPEDEFED
3124 #include "X11/xpm.h"
3129 #undef PIXEL_ALREADY_TYPEDEFED
3131 #include "X11/xpm.h"
3132 #endif /* HAVE_NTGUI */
3133 #endif /* HAVE_XPM */
3135 #if defined (HAVE_XPM) || defined (HAVE_NS)
3136 /* The symbol `xpm' identifying XPM-format images. */
3138 static Lisp_Object Qxpm
;
3140 /* Indices of image specification fields in xpm_format, below. */
3142 enum xpm_keyword_index
3158 /* Vector of image_keyword structures describing the format
3159 of valid XPM image specifications. */
3161 static const struct image_keyword xpm_format
[XPM_LAST
] =
3163 {":type", IMAGE_SYMBOL_VALUE
, 1},
3164 {":file", IMAGE_STRING_VALUE
, 0},
3165 {":data", IMAGE_STRING_VALUE
, 0},
3166 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3167 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3168 {":relief", IMAGE_INTEGER_VALUE
, 0},
3169 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3170 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3171 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3172 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3173 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3176 #if defined HAVE_NTGUI && defined WINDOWSNT
3177 static bool init_xpm_functions (void);
3179 #define init_xpm_functions NULL
3182 /* Structure describing the image type XPM. */
3184 static struct image_type xpm_type
=
3194 #ifdef HAVE_X_WINDOWS
3196 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3197 functions for allocating image colors. Our own functions handle
3198 color allocation failures more gracefully than the ones on the XPM
3201 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3202 #define ALLOC_XPM_COLORS
3204 #endif /* HAVE_X_WINDOWS */
3206 #ifdef ALLOC_XPM_COLORS
3208 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3211 /* An entry in a hash table used to cache color definitions of named
3212 colors. This cache is necessary to speed up XPM image loading in
3213 case we do color allocations ourselves. Without it, we would need
3214 a call to XParseColor per pixel in the image. */
3216 struct xpm_cached_color
3218 /* Next in collision chain. */
3219 struct xpm_cached_color
*next
;
3221 /* Color definition (RGB and pixel color). */
3225 char name
[FLEXIBLE_ARRAY_MEMBER
];
3228 /* The hash table used for the color cache, and its bucket vector
3231 #define XPM_COLOR_CACHE_BUCKETS 1001
3232 static struct xpm_cached_color
**xpm_color_cache
;
3234 /* Initialize the color cache. */
3237 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3239 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3240 xpm_color_cache
= xzalloc (nbytes
);
3241 init_color_table ();
3243 if (attrs
->valuemask
& XpmColorSymbols
)
3248 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3249 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3250 attrs
->colorsymbols
[i
].value
, &color
))
3252 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3254 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3259 /* Free the color cache. */
3262 xpm_free_color_cache (void)
3264 struct xpm_cached_color
*p
, *next
;
3267 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3268 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3274 xfree (xpm_color_cache
);
3275 xpm_color_cache
= NULL
;
3276 free_color_table ();
3279 /* Return the bucket index for color named COLOR_NAME in the color
3283 xpm_color_bucket (char *color_name
)
3285 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3286 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3290 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3291 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3294 static struct xpm_cached_color
*
3295 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3298 struct xpm_cached_color
*p
;
3301 bucket
= xpm_color_bucket (color_name
);
3303 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3304 p
= xmalloc (nbytes
);
3305 strcpy (p
->name
, color_name
);
3307 p
->next
= xpm_color_cache
[bucket
];
3308 xpm_color_cache
[bucket
] = p
;
3312 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3313 return the cached definition in *COLOR. Otherwise, make a new
3314 entry in the cache and allocate the color. Value is false if color
3315 allocation failed. */
3318 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3320 struct xpm_cached_color
*p
;
3321 int h
= xpm_color_bucket (color_name
);
3323 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3324 if (strcmp (p
->name
, color_name
) == 0)
3329 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3332 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3334 p
= xpm_cache_color (f
, color_name
, color
, h
);
3336 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3337 with transparency, and it's useful. */
3338 else if (strcmp ("opaque", color_name
) == 0)
3340 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3341 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3342 p
= xpm_cache_color (f
, color_name
, color
, h
);
3349 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3350 CLOSURE is a pointer to the frame on which we allocate the
3351 color. Return in *COLOR the allocated color. Value is non-zero
3355 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3358 return xpm_lookup_color (closure
, color_name
, color
);
3362 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3363 is a pointer to the frame on which we allocate the color. Value is
3364 non-zero if successful. */
3367 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3372 #endif /* ALLOC_XPM_COLORS */
3377 /* XPM library details. */
3379 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3380 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3381 xpm_XImage
**, XpmAttributes
*));
3382 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3383 xpm_XImage
**, XpmAttributes
*));
3384 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3387 init_xpm_functions (void)
3391 if (!(library
= w32_delayed_load (Qxpm
)))
3394 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3395 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3396 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3397 LOAD_IMGLIB_FN (library
, XImageFree
);
3401 #endif /* WINDOWSNT */
3403 #if defined HAVE_NTGUI && !defined WINDOWSNT
3404 /* Glue for code below */
3405 #define fn_XpmReadFileToImage XpmReadFileToImage
3406 #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
3407 #define fn_XImageFree XImageFree
3408 #define fn_XpmFreeAttributes XpmFreeAttributes
3409 #endif /* HAVE_NTGUI && !WINDOWSNT */
3411 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3412 for XPM images. Such a list must consist of conses whose car and
3416 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3418 while (CONSP (color_symbols
))
3420 Lisp_Object sym
= XCAR (color_symbols
);
3422 || !STRINGP (XCAR (sym
))
3423 || !STRINGP (XCDR (sym
)))
3425 color_symbols
= XCDR (color_symbols
);
3428 return NILP (color_symbols
);
3432 /* Value is true if OBJECT is a valid XPM image specification. */
3435 xpm_image_p (Lisp_Object object
)
3437 struct image_keyword fmt
[XPM_LAST
];
3438 memcpy (fmt
, xpm_format
, sizeof fmt
);
3439 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3440 /* Either `:file' or `:data' must be present. */
3441 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3442 /* Either no `:color-symbols' or it's a list of conses
3443 whose car and cdr are strings. */
3444 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3445 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3448 #endif /* HAVE_XPM || HAVE_NS */
3450 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3452 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3454 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
3457 XpmAttributes attrs
;
3458 Pixmap bitmap
, mask
;
3460 memset (&attrs
, 0, sizeof attrs
);
3462 attrs
.visual
= FRAME_X_VISUAL (f
);
3463 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3464 attrs
.valuemask
|= XpmVisual
;
3465 attrs
.valuemask
|= XpmColormap
;
3467 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3468 (char **) bits
, &bitmap
, &mask
, &attrs
);
3469 if (rc
!= XpmSuccess
)
3471 XpmFreeAttributes (&attrs
);
3475 id
= x_allocate_bitmap_record (f
);
3476 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3477 dpyinfo
->bitmaps
[id
- 1].have_mask
= true;
3478 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3479 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3480 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3481 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3482 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3483 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3485 XpmFreeAttributes (&attrs
);
3488 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3490 /* Load image IMG which will be displayed on frame F. Value is
3491 true if successful. */
3496 xpm_load (struct frame
*f
, struct image
*img
)
3499 XpmAttributes attrs
;
3500 Lisp_Object specified_file
, color_symbols
;
3505 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3506 #endif /* HAVE_NTGUI */
3508 /* Configure the XPM lib. Use the visual of frame F. Allocate
3509 close colors. Return colors allocated. */
3510 memset (&attrs
, 0, sizeof attrs
);
3513 attrs
.visual
= FRAME_X_VISUAL (f
);
3514 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3515 attrs
.valuemask
|= XpmVisual
;
3516 attrs
.valuemask
|= XpmColormap
;
3517 #endif /* HAVE_NTGUI */
3519 #ifdef ALLOC_XPM_COLORS
3520 /* Allocate colors with our own functions which handle
3521 failing color allocation more gracefully. */
3522 attrs
.color_closure
= f
;
3523 attrs
.alloc_color
= xpm_alloc_color
;
3524 attrs
.free_colors
= xpm_free_colors
;
3525 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3526 #else /* not ALLOC_XPM_COLORS */
3527 /* Let the XPM lib allocate colors. */
3528 attrs
.valuemask
|= XpmReturnAllocPixels
;
3529 #ifdef XpmAllocCloseColors
3530 attrs
.alloc_close_colors
= 1;
3531 attrs
.valuemask
|= XpmAllocCloseColors
;
3532 #else /* not XpmAllocCloseColors */
3533 attrs
.closeness
= 600;
3534 attrs
.valuemask
|= XpmCloseness
;
3535 #endif /* not XpmAllocCloseColors */
3536 #endif /* ALLOC_XPM_COLORS */
3538 /* If image specification contains symbolic color definitions, add
3539 these to `attrs'. */
3540 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3541 if (CONSP (color_symbols
))
3544 XpmColorSymbol
*xpm_syms
;
3547 attrs
.valuemask
|= XpmColorSymbols
;
3549 /* Count number of symbols. */
3550 attrs
.numsymbols
= 0;
3551 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3554 /* Allocate an XpmColorSymbol array. */
3555 SAFE_NALLOCA (xpm_syms
, 1, attrs
.numsymbols
);
3556 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3557 memset (xpm_syms
, 0, size
);
3558 attrs
.colorsymbols
= xpm_syms
;
3560 /* Fill the color symbol array. */
3561 for (tail
= color_symbols
, i
= 0;
3563 ++i
, tail
= XCDR (tail
))
3567 char *empty_string
= (char *) "";
3569 if (!CONSP (XCAR (tail
)))
3571 xpm_syms
[i
].name
= empty_string
;
3572 xpm_syms
[i
].value
= empty_string
;
3575 name
= XCAR (XCAR (tail
));
3576 color
= XCDR (XCAR (tail
));
3578 SAFE_ALLOCA_STRING (xpm_syms
[i
].name
, name
);
3580 xpm_syms
[i
].name
= empty_string
;
3581 if (STRINGP (color
))
3582 SAFE_ALLOCA_STRING (xpm_syms
[i
].value
, color
);
3584 xpm_syms
[i
].value
= empty_string
;
3588 /* Create a pixmap for the image, either from a file, or from a
3589 string buffer containing data in the same format as an XPM file. */
3590 #ifdef ALLOC_XPM_COLORS
3591 xpm_init_color_cache (f
, &attrs
);
3594 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3598 HDC frame_dc
= get_frame_dc (f
);
3599 hdc
= CreateCompatibleDC (frame_dc
);
3600 release_frame_dc (f
, frame_dc
);
3602 #endif /* HAVE_NTGUI */
3604 if (STRINGP (specified_file
))
3606 Lisp_Object file
= x_find_image_file (specified_file
);
3607 if (!STRINGP (file
))
3609 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3610 #ifdef ALLOC_XPM_COLORS
3611 xpm_free_color_cache ();
3619 /* FILE is encoded in UTF-8, but image libraries on Windows
3620 support neither UTF-8 nor UTF-16 encoded file names. So we
3621 need to re-encode it in ANSI. */
3622 file
= ansi_encode_filename (file
);
3624 /* XpmReadFileToPixmap is not available in the Windows port of
3625 libxpm. But XpmReadFileToImage almost does what we want. */
3626 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3627 &xpm_image
, &xpm_mask
,
3630 rc
= XpmReadFileToImage (FRAME_X_DISPLAY (f
), SSDATA (file
),
3631 &img
->ximg
, &img
->mask_img
,
3633 #endif /* HAVE_NTGUI */
3637 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3638 if (!STRINGP (buffer
))
3640 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3641 #ifdef ALLOC_XPM_COLORS
3642 xpm_free_color_cache ();
3648 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3649 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3650 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3651 &xpm_image
, &xpm_mask
,
3654 rc
= XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f
), SSDATA (buffer
),
3655 &img
->ximg
, &img
->mask_img
,
3657 #endif /* HAVE_NTGUI */
3660 #ifdef HAVE_X_WINDOWS
3661 if (rc
== XpmSuccess
)
3663 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3664 img
->ximg
->width
, img
->ximg
->height
,
3666 if (img
->pixmap
== NO_PIXMAP
)
3668 x_clear_image (f
, img
);
3671 else if (img
->mask_img
)
3673 img
->mask
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3674 img
->mask_img
->width
,
3675 img
->mask_img
->height
,
3676 img
->mask_img
->depth
);
3677 if (img
->mask
== NO_PIXMAP
)
3679 x_clear_image (f
, img
);
3686 if (rc
== XpmSuccess
)
3688 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3689 img
->colors
= colors_in_color_table (&img
->ncolors
);
3690 #else /* not ALLOC_XPM_COLORS */
3694 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3695 plus some duplicate attributes. */
3696 if (xpm_image
&& xpm_image
->bitmap
)
3698 img
->pixmap
= xpm_image
->bitmap
;
3699 /* XImageFree in libXpm frees XImage struct without destroying
3700 the bitmap, which is what we want. */
3701 fn_XImageFree (xpm_image
);
3703 if (xpm_mask
&& xpm_mask
->bitmap
)
3705 /* The mask appears to be inverted compared with what we expect.
3706 TODO: invert our expectations. See other places where we
3707 have to invert bits because our idea of masks is backwards. */
3709 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3711 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3712 SelectObject (hdc
, old_obj
);
3714 img
->mask
= xpm_mask
->bitmap
;
3715 fn_XImageFree (xpm_mask
);
3720 #endif /* HAVE_NTGUI */
3722 /* Remember allocated colors. */
3723 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3724 img
->ncolors
= attrs
.nalloc_pixels
;
3725 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3727 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3728 #ifdef DEBUG_X_COLORS
3729 register_color (img
->colors
[i
]);
3732 #endif /* not ALLOC_XPM_COLORS */
3734 img
->width
= attrs
.width
;
3735 img
->height
= attrs
.height
;
3736 eassert (img
->width
> 0 && img
->height
> 0);
3738 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3740 fn_XpmFreeAttributes (&attrs
);
3742 XpmFreeAttributes (&attrs
);
3743 #endif /* HAVE_NTGUI */
3745 #ifdef HAVE_X_WINDOWS
3746 /* Maybe fill in the background field while we have ximg handy. */
3747 IMAGE_BACKGROUND (img
, f
, img
->ximg
);
3749 /* Fill in the background_transparent field while we have the
3751 image_background_transparent (img
, f
, img
->mask_img
);
3758 #endif /* HAVE_NTGUI */
3763 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3766 case XpmFileInvalid
:
3767 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3771 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3774 case XpmColorFailed
:
3775 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3779 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3784 #ifdef ALLOC_XPM_COLORS
3785 xpm_free_color_cache ();
3788 return rc
== XpmSuccess
;
3791 #endif /* HAVE_XPM */
3793 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3795 /* XPM support functions for NS where libxpm is not available.
3796 Only XPM version 3 (without any extensions) is supported. */
3798 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3800 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3801 const unsigned char *, int);
3802 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3804 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3805 const unsigned char *, int);
3807 /* Tokens returned from xpm_scan. */
3816 /* Scan an XPM data and return a character (< 256) or a token defined
3817 by enum xpm_token above. *S and END are the start (inclusive) and
3818 the end (exclusive) addresses of the data, respectively. Advance
3819 *S while scanning. If token is either XPM_TK_IDENT or
3820 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3821 length of the corresponding token, respectively. */
3824 xpm_scan (const unsigned char **s
,
3825 const unsigned char *end
,
3826 const unsigned char **beg
,
3833 /* Skip white-space. */
3834 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3837 /* gnus-pointer.xpm uses '-' in its identifier.
3838 sb-dir-plus.xpm uses '+' in its identifier. */
3839 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3843 && (c
= **s
, c_isalnum (c
)
3844 || c
== '_' || c
== '-' || c
== '+'))
3847 return XPM_TK_IDENT
;
3852 while (*s
< end
&& **s
!= '"')
3857 return XPM_TK_STRING
;
3861 if (*s
< end
&& **s
== '*')
3863 /* C-style comment. */
3867 while (*s
< end
&& *(*s
)++ != '*')
3870 while (*s
< end
&& **s
!= '/');
3884 /* Functions for color table lookup in XPM data. A key is a string
3885 specifying the color of each pixel in XPM data. A value is either
3886 an integer that specifies a pixel color, Qt that specifies
3887 transparency, or Qnil for the unspecified color. If the length of
3888 the key string is one, a vector is used as a table. Otherwise, a
3889 hash table is used. */
3892 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3893 const unsigned char *,
3896 Lisp_Object (**get_func
) (Lisp_Object
,
3897 const unsigned char *,
3900 *put_func
= xpm_put_color_table_v
;
3901 *get_func
= xpm_get_color_table_v
;
3902 return Fmake_vector (make_number (256), Qnil
);
3906 xpm_put_color_table_v (Lisp_Object color_table
,
3907 const unsigned char *chars_start
,
3911 ASET (color_table
, *chars_start
, color
);
3915 xpm_get_color_table_v (Lisp_Object color_table
,
3916 const unsigned char *chars_start
,
3919 return AREF (color_table
, *chars_start
);
3923 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3924 const unsigned char *,
3927 Lisp_Object (**get_func
) (Lisp_Object
,
3928 const unsigned char *,
3931 *put_func
= xpm_put_color_table_h
;
3932 *get_func
= xpm_get_color_table_h
;
3933 return make_hash_table (hashtest_equal
, make_number (DEFAULT_HASH_SIZE
),
3934 make_float (DEFAULT_REHASH_SIZE
),
3935 make_float (DEFAULT_REHASH_THRESHOLD
),
3940 xpm_put_color_table_h (Lisp_Object color_table
,
3941 const unsigned char *chars_start
,
3945 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3946 EMACS_UINT hash_code
;
3947 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3949 hash_lookup (table
, chars
, &hash_code
);
3950 hash_put (table
, chars
, color
, hash_code
);
3954 xpm_get_color_table_h (Lisp_Object color_table
,
3955 const unsigned char *chars_start
,
3958 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3960 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3962 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3965 enum xpm_color_key
{
3973 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3976 xpm_str_to_color_key (const char *s
)
3980 for (i
= 0; i
< ARRAYELTS (xpm_color_key_strings
); i
++)
3981 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3987 xpm_load_image (struct frame
*f
,
3989 const unsigned char *contents
,
3990 const unsigned char *end
)
3992 const unsigned char *s
= contents
, *beg
, *str
;
3993 unsigned char buffer
[BUFSIZ
];
3994 int width
, height
, x
, y
;
3995 int num_colors
, chars_per_pixel
;
3998 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3999 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
4000 Lisp_Object frame
, color_symbols
, color_table
;
4002 bool have_mask
= false;
4003 XImagePtr ximg
= NULL
, mask_img
= NULL
;
4006 LA1 = xpm_scan (&s, end, &beg, &len)
4008 #define expect(TOKEN) \
4011 if (LA1 != (TOKEN)) \
4017 #define expect_ident(IDENT) \
4018 if (LA1 == XPM_TK_IDENT \
4019 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
4024 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
4028 expect_ident ("static");
4029 expect_ident ("char");
4031 expect (XPM_TK_IDENT
);
4036 expect (XPM_TK_STRING
);
4039 memcpy (buffer
, beg
, len
);
4041 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
4042 &num_colors
, &chars_per_pixel
) != 4
4043 || width
<= 0 || height
<= 0
4044 || num_colors
<= 0 || chars_per_pixel
<= 0)
4047 if (!check_image_size (f
, width
, height
))
4049 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
4053 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0)
4055 || !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
4060 image_error ("Image too large", Qnil
, Qnil
);
4066 XSETFRAME (frame
, f
);
4067 if (!NILP (Fxw_display_color_p (frame
)))
4068 best_key
= XPM_COLOR_KEY_C
;
4069 else if (!NILP (Fx_display_grayscale_p (frame
)))
4070 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
4071 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
4073 best_key
= XPM_COLOR_KEY_M
;
4075 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
4076 if (chars_per_pixel
== 1)
4077 color_table
= xpm_make_color_table_v (&put_color_table
,
4080 color_table
= xpm_make_color_table_h (&put_color_table
,
4083 while (num_colors
-- > 0)
4085 char *color
, *max_color
;
4086 int key
, next_key
, max_key
= 0;
4087 Lisp_Object symbol_color
= Qnil
, color_val
;
4090 expect (XPM_TK_STRING
);
4091 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
4093 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
4094 buffer
[len
- chars_per_pixel
] = '\0';
4096 str
= strtok (buffer
, " \t");
4099 key
= xpm_str_to_color_key (str
);
4104 color
= strtok (NULL
, " \t");
4108 while ((str
= strtok (NULL
, " \t")) != NULL
)
4110 next_key
= xpm_str_to_color_key (str
);
4113 color
[strlen (color
)] = ' ';
4116 if (key
== XPM_COLOR_KEY_S
)
4118 if (NILP (symbol_color
))
4119 symbol_color
= build_string (color
);
4121 else if (max_key
< key
&& key
<= best_key
)
4131 if (!NILP (color_symbols
) && !NILP (symbol_color
))
4133 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
4135 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
4137 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
4139 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
4141 color_val
= make_number (cdef
.pixel
);
4144 if (NILP (color_val
) && max_key
> 0)
4146 if (xstrcasecmp (max_color
, "None") == 0)
4148 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4149 color_val
= make_number (cdef
.pixel
);
4151 if (!NILP (color_val
))
4152 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4157 for (y
= 0; y
< height
; y
++)
4159 expect (XPM_TK_STRING
);
4161 if (len
< width
* chars_per_pixel
)
4163 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4165 Lisp_Object color_val
=
4166 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4168 XPutPixel (ximg
, x
, y
,
4169 (INTEGERP (color_val
) ? XINT (color_val
)
4170 : FRAME_FOREGROUND_PIXEL (f
)));
4172 XPutPixel (mask_img
, x
, y
,
4173 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4174 : (have_mask
= true, PIX_MASK_RETAIN
)));
4176 if (EQ (color_val
, Qt
))
4177 ns_set_alpha (ximg
, x
, y
, 0);
4185 img
->height
= height
;
4187 /* Maybe fill in the background field while we have ximg handy. */
4188 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4189 IMAGE_BACKGROUND (img
, f
, ximg
);
4191 image_put_x_image (f
, img
, ximg
, 0);
4195 /* Fill in the background_transparent field while we have the
4197 image_background_transparent (img
, f
, mask_img
);
4199 image_put_x_image (f
, img
, mask_img
, 1);
4203 x_destroy_x_image (mask_img
);
4204 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4210 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4211 x_destroy_x_image (ximg
);
4212 x_destroy_x_image (mask_img
);
4213 x_clear_image (f
, img
);
4222 xpm_load (struct frame
*f
,
4226 Lisp_Object file_name
;
4228 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4229 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4230 if (STRINGP (file_name
))
4233 unsigned char *contents
;
4236 file
= x_find_image_file (file_name
);
4237 if (!STRINGP (file
))
4239 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4243 contents
= slurp_file (SSDATA (file
), &size
);
4244 if (contents
== NULL
)
4246 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4250 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4257 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4258 if (!STRINGP (data
))
4260 image_error ("Invalid image data `%s'", data
, Qnil
);
4263 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4264 SDATA (data
) + SBYTES (data
));
4270 #endif /* HAVE_NS && !HAVE_XPM */
4274 /***********************************************************************
4276 ***********************************************************************/
4278 #ifdef COLOR_TABLE_SUPPORT
4280 /* An entry in the color table mapping an RGB color to a pixel color. */
4285 unsigned long pixel
;
4287 /* Next in color table collision list. */
4288 struct ct_color
*next
;
4291 /* The bucket vector size to use. Must be prime. */
4295 /* Value is a hash of the RGB color given by R, G, and B. */
4297 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4299 /* The color hash table. */
4301 static struct ct_color
**ct_table
;
4303 /* Number of entries in the color table. */
4305 static int ct_colors_allocated
;
4308 ct_colors_allocated_max
=
4310 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4313 /* Initialize the color table. */
4316 init_color_table (void)
4318 int size
= CT_SIZE
* sizeof (*ct_table
);
4319 ct_table
= xzalloc (size
);
4320 ct_colors_allocated
= 0;
4324 /* Free memory associated with the color table. */
4327 free_color_table (void)
4330 struct ct_color
*p
, *next
;
4332 for (i
= 0; i
< CT_SIZE
; ++i
)
4333 for (p
= ct_table
[i
]; p
; p
= next
)
4344 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4345 entry for that color already is in the color table, return the
4346 pixel color of that entry. Otherwise, allocate a new color for R,
4347 G, B, and make an entry in the color table. */
4349 static unsigned long
4350 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4352 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4353 int i
= hash
% CT_SIZE
;
4355 Display_Info
*dpyinfo
;
4357 /* Handle TrueColor visuals specially, which improves performance by
4358 two orders of magnitude. Freeing colors on TrueColor visuals is
4359 a nop, and pixel colors specify RGB values directly. See also
4360 the Xlib spec, chapter 3.1. */
4361 dpyinfo
= FRAME_DISPLAY_INFO (f
);
4362 if (dpyinfo
->red_bits
> 0)
4364 unsigned long pr
, pg
, pb
;
4366 /* Apply gamma-correction like normal color allocation does. */
4370 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4371 gamma_correct (f
, &color
);
4372 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4375 /* Scale down RGB values to the visual's bits per RGB, and shift
4376 them to the right position in the pixel color. Note that the
4377 original RGB values are 16-bit values, as usual in X. */
4378 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4379 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4380 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4382 /* Assemble the pixel color. */
4383 return pr
| pg
| pb
;
4386 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4387 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4393 #ifdef HAVE_X_WINDOWS
4401 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4402 return FRAME_FOREGROUND_PIXEL (f
);
4404 #ifdef HAVE_X_WINDOWS
4409 cmap
= FRAME_X_COLORMAP (f
);
4410 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4413 ++ct_colors_allocated
;
4414 p
= xmalloc (sizeof *p
);
4418 p
->pixel
= color
.pixel
;
4419 p
->next
= ct_table
[i
];
4423 return FRAME_FOREGROUND_PIXEL (f
);
4427 color
= PALETTERGB (r
, g
, b
);
4429 color
= RGB_TO_ULONG (r
, g
, b
);
4430 #endif /* HAVE_NTGUI */
4431 ++ct_colors_allocated
;
4432 p
= xmalloc (sizeof *p
);
4437 p
->next
= ct_table
[i
];
4439 #endif /* HAVE_X_WINDOWS */
4447 /* Look up pixel color PIXEL which is used on frame F in the color
4448 table. If not already present, allocate it. Value is PIXEL. */
4450 static unsigned long
4451 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4453 int i
= pixel
% CT_SIZE
;
4456 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4457 if (p
->pixel
== pixel
)
4466 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4467 return FRAME_FOREGROUND_PIXEL (f
);
4469 #ifdef HAVE_X_WINDOWS
4470 cmap
= FRAME_X_COLORMAP (f
);
4471 color
.pixel
= pixel
;
4472 x_query_color (f
, &color
);
4473 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4476 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4477 color
.pixel
= pixel
;
4478 XQueryColor (NULL
, cmap
, &color
);
4479 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4481 #endif /* HAVE_X_WINDOWS */
4485 ++ct_colors_allocated
;
4487 p
= xmalloc (sizeof *p
);
4492 p
->next
= ct_table
[i
];
4496 return FRAME_FOREGROUND_PIXEL (f
);
4502 /* Value is a vector of all pixel colors contained in the color table,
4503 allocated via xmalloc. Set *N to the number of colors. */
4505 static unsigned long *
4506 colors_in_color_table (int *n
)
4510 unsigned long *colors
;
4512 if (ct_colors_allocated
== 0)
4519 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4520 *n
= ct_colors_allocated
;
4522 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4523 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4524 colors
[j
++] = p
->pixel
;
4530 #else /* COLOR_TABLE_SUPPORT */
4532 static unsigned long
4533 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4535 unsigned long pixel
;
4538 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4539 #endif /* HAVE_NTGUI */
4542 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4543 #endif /* HAVE_NS */
4548 init_color_table (void)
4551 #endif /* COLOR_TABLE_SUPPORT */
4554 /***********************************************************************
4556 ***********************************************************************/
4558 /* Edge detection matrices for different edge-detection
4561 static int emboss_matrix
[9] = {
4563 2, -1, 0, /* y - 1 */
4565 0, 1, -2 /* y + 1 */
4568 static int laplace_matrix
[9] = {
4570 1, 0, 0, /* y - 1 */
4572 0, 0, -1 /* y + 1 */
4575 /* Value is the intensity of the color whose red/green/blue values
4578 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4581 /* On frame F, return an array of XColor structures describing image
4582 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4583 means also fill the red/green/blue members of the XColor
4584 structures. Value is a pointer to the array of XColors structures,
4585 allocated with xmalloc; it must be freed by the caller. */
4588 x_to_xcolors (struct frame
*f
, struct image
*img
, bool rgb_p
)
4592 XImagePtr_or_DC ximg
;
4595 #endif /* HAVE_NTGUI */
4597 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4598 memory_full (SIZE_MAX
);
4599 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4601 /* Get the X image or create a memory device context for IMG. */
4602 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4604 /* Fill the `pixel' members of the XColor array. I wished there
4605 were an easy and portable way to circumvent XGetPixel. */
4607 for (y
= 0; y
< img
->height
; ++y
)
4609 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4611 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4612 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4614 x_query_colors (f
, row
, img
->width
);
4618 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4620 /* W32_TODO: palette support needed here? */
4621 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4624 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4625 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4626 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4629 #endif /* HAVE_X_WINDOWS */
4632 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
4639 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4640 created with CreateDIBSection, with the pointer to the bit values
4641 stored in ximg->data. */
4644 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4646 int width
= ximg
->info
.bmiHeader
.biWidth
;
4647 unsigned char * pixel
;
4649 /* True color images. */
4650 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4652 int rowbytes
= width
* 3;
4653 /* Ensure scanlines are aligned on 4 byte boundaries. */
4655 rowbytes
+= 4 - (rowbytes
% 4);
4657 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4658 /* Windows bitmaps are in BGR order. */
4659 *pixel
= GetBValue (color
);
4660 *(pixel
+ 1) = GetGValue (color
);
4661 *(pixel
+ 2) = GetRValue (color
);
4663 /* Monochrome images. */
4664 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4666 int rowbytes
= width
/ 8;
4667 /* Ensure scanlines are aligned on 4 byte boundaries. */
4669 rowbytes
+= 4 - (rowbytes
% 4);
4670 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4671 /* Filter out palette info. */
4672 if (color
& 0x00ffffff)
4673 *pixel
= *pixel
| (1 << x
% 8);
4675 *pixel
= *pixel
& ~(1 << x
% 8);
4678 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4681 #endif /* HAVE_NTGUI */
4683 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4684 RGB members are set. F is the frame on which this all happens.
4685 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4688 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4691 XImagePtr oimg
= NULL
;
4694 init_color_table ();
4696 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_COLORS
);
4697 image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 0,
4700 for (y
= 0; y
< img
->height
; ++y
)
4701 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4703 unsigned long pixel
;
4704 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4705 XPutPixel (oimg
, x
, y
, pixel
);
4710 image_put_x_image (f
, img
, oimg
, 0);
4711 #ifdef COLOR_TABLE_SUPPORT
4712 img
->colors
= colors_in_color_table (&img
->ncolors
);
4713 free_color_table ();
4714 #endif /* COLOR_TABLE_SUPPORT */
4718 /* On frame F, perform edge-detection on image IMG.
4720 MATRIX is a nine-element array specifying the transformation
4721 matrix. See emboss_matrix for an example.
4723 COLOR_ADJUST is a color adjustment added to each pixel of the
4727 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4729 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4733 for (i
= sum
= 0; i
< 9; ++i
)
4734 sum
+= eabs (matrix
[i
]);
4736 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4738 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4739 memory_full (SIZE_MAX
);
4740 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4742 for (y
= 0; y
< img
->height
; ++y
)
4744 p
= COLOR (new, 0, y
);
4745 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4746 p
= COLOR (new, img
->width
- 1, y
);
4747 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4750 for (x
= 1; x
< img
->width
- 1; ++x
)
4752 p
= COLOR (new, x
, 0);
4753 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4754 p
= COLOR (new, x
, img
->height
- 1);
4755 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4758 for (y
= 1; y
< img
->height
- 1; ++y
)
4760 p
= COLOR (new, 1, y
);
4762 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4764 int r
, g
, b
, yy
, xx
;
4767 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4768 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4771 XColor
*t
= COLOR (colors
, xx
, yy
);
4772 r
+= matrix
[i
] * t
->red
;
4773 g
+= matrix
[i
] * t
->green
;
4774 b
+= matrix
[i
] * t
->blue
;
4777 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4778 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4779 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4780 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4785 x_from_xcolors (f
, img
, new);
4791 /* Perform the pre-defined `emboss' edge-detection on image IMG
4795 x_emboss (struct frame
*f
, struct image
*img
)
4797 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4801 /* Transform image IMG which is used on frame F with a Laplace
4802 edge-detection algorithm. The result is an image that can be used
4803 to draw disabled buttons, for example. */
4806 x_laplace (struct frame
*f
, struct image
*img
)
4808 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4812 /* Perform edge-detection on image IMG on frame F, with specified
4813 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4815 MATRIX must be either
4817 - a list of at least 9 numbers in row-major form
4818 - a vector of at least 9 numbers
4820 COLOR_ADJUST nil means use a default; otherwise it must be a
4824 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4825 Lisp_Object color_adjust
)
4833 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4834 ++i
, matrix
= XCDR (matrix
))
4835 trans
[i
] = XFLOATINT (XCAR (matrix
));
4837 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4839 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4840 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4843 if (NILP (color_adjust
))
4844 color_adjust
= make_number (0xffff / 2);
4846 if (i
== 9 && NUMBERP (color_adjust
))
4847 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4851 /* Transform image IMG on frame F so that it looks disabled. */
4854 x_disable_image (struct frame
*f
, struct image
*img
)
4856 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
4858 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4860 int n_planes
= dpyinfo
->n_planes
;
4861 #endif /* HAVE_NTGUI */
4865 /* Color (or grayscale). Convert to gray, and equalize. Just
4866 drawing such images with a stipple can look very odd, so
4867 we're using this method instead. */
4868 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4870 const int h
= 15000;
4871 const int l
= 30000;
4873 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4877 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4878 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4879 p
->red
= p
->green
= p
->blue
= i2
;
4882 x_from_xcolors (f
, img
, colors
);
4885 /* Draw a cross over the disabled image, if we must or if we
4887 if (n_planes
< 2 || cross_disabled_images
)
4890 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4892 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4894 Display
*dpy
= FRAME_X_DISPLAY (f
);
4897 image_sync_to_pixmaps (f
, img
);
4898 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4899 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4900 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4901 img
->width
- 1, img
->height
- 1);
4902 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4908 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4909 XSetForeground (dpy
, gc
, MaskForeground (f
));
4910 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4911 img
->width
- 1, img
->height
- 1);
4912 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4916 #endif /* !HAVE_NS */
4921 hdc
= get_frame_dc (f
);
4922 bmpdc
= CreateCompatibleDC (hdc
);
4923 release_frame_dc (f
, hdc
);
4925 prev
= SelectObject (bmpdc
, img
->pixmap
);
4927 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4928 MoveToEx (bmpdc
, 0, 0, NULL
);
4929 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4930 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4931 LineTo (bmpdc
, img
->width
- 1, 0);
4935 SelectObject (bmpdc
, img
->mask
);
4936 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4937 MoveToEx (bmpdc
, 0, 0, NULL
);
4938 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4939 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4940 LineTo (bmpdc
, img
->width
- 1, 0);
4942 SelectObject (bmpdc
, prev
);
4944 #endif /* HAVE_NTGUI */
4949 /* Build a mask for image IMG which is used on frame F. FILE is the
4950 name of an image file, for error messages. HOW determines how to
4951 determine the background color of IMG. If it is a list '(R G B)',
4952 with R, G, and B being integers >= 0, take that as the color of the
4953 background. Otherwise, determine the background color of IMG
4957 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4959 XImagePtr_or_DC ximg
;
4966 #endif /* HAVE_NTGUI */
4968 bool use_img_background
;
4969 unsigned long bg
= 0;
4972 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4976 /* Create an image and pixmap serving as mask. */
4977 if (! image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 1,
4980 #endif /* !HAVE_NS */
4982 /* Create the bit array serving as mask. */
4983 row_width
= (img
->width
+ 7) / 8;
4984 mask_img
= xzalloc (row_width
* img
->height
);
4985 #endif /* HAVE_NTGUI */
4987 /* Get the X image or create a memory device context for IMG. */
4988 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4990 /* Determine the background color of ximg. If HOW is `(R G B)'
4991 take that as color. Otherwise, use the image's background color. */
4992 use_img_background
= 1;
4998 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
5000 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
5004 if (i
== 3 && NILP (how
))
5006 char color_name
[30];
5007 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
5010 0x00ffffff & /* Filter out palette info. */
5011 #endif /* HAVE_NTGUI */
5012 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
5013 use_img_background
= 0;
5017 if (use_img_background
)
5018 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
5020 /* Set all bits in mask_img to 1 whose color in ximg is different
5021 from the background color bg. */
5023 for (y
= 0; y
< img
->height
; ++y
)
5024 for (x
= 0; x
< img
->width
; ++x
)
5026 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
5027 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
5029 if (XGetPixel (ximg
, x
, y
) == bg
)
5030 ns_set_alpha (ximg
, x
, y
, 0);
5031 #endif /* HAVE_NS */
5033 /* Fill in the background_transparent field while we have the mask handy. */
5034 image_background_transparent (img
, f
, mask_img
);
5036 /* Put mask_img into the image. */
5037 image_put_x_image (f
, img
, mask_img
, 1);
5038 #endif /* !HAVE_NS */
5040 for (y
= 0; y
< img
->height
; ++y
)
5041 for (x
= 0; x
< img
->width
; ++x
)
5043 COLORREF p
= GetPixel (ximg
, x
, y
);
5045 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
5048 /* Create the mask image. */
5049 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
5051 /* Fill in the background_transparent field while we have the mask handy. */
5052 SelectObject (ximg
, img
->mask
);
5053 image_background_transparent (img
, f
, ximg
);
5055 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5057 #endif /* HAVE_NTGUI */
5059 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
5063 /***********************************************************************
5064 PBM (mono, gray, color)
5065 ***********************************************************************/
5067 static bool pbm_image_p (Lisp_Object object
);
5068 static bool pbm_load (struct frame
*f
, struct image
*img
);
5070 /* The symbol `pbm' identifying images of this type. */
5072 static Lisp_Object Qpbm
;
5074 /* Indices of image specification fields in gs_format, below. */
5076 enum pbm_keyword_index
5092 /* Vector of image_keyword structures describing the format
5093 of valid user-defined image specifications. */
5095 static const struct image_keyword pbm_format
[PBM_LAST
] =
5097 {":type", IMAGE_SYMBOL_VALUE
, 1},
5098 {":file", IMAGE_STRING_VALUE
, 0},
5099 {":data", IMAGE_STRING_VALUE
, 0},
5100 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5101 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5102 {":relief", IMAGE_INTEGER_VALUE
, 0},
5103 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5104 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5105 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5106 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5107 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5110 /* Structure describing the image type `pbm'. */
5112 static struct image_type pbm_type
=
5123 /* Return true if OBJECT is a valid PBM image specification. */
5126 pbm_image_p (Lisp_Object object
)
5128 struct image_keyword fmt
[PBM_LAST
];
5130 memcpy (fmt
, pbm_format
, sizeof fmt
);
5132 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5135 /* Must specify either :data or :file. */
5136 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5140 /* Get next char skipping comments in Netpbm header. Returns -1 at
5144 pbm_next_char (unsigned char **s
, unsigned char *end
)
5148 while (*s
< end
&& (c
= *(*s
)++, c
== '#'))
5150 /* Skip to the next line break. */
5151 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n' && c
!= '\r'))
5161 /* Scan a decimal number from *S and return it. Advance *S while
5162 reading the number. END is the end of the string. Value is -1 at
5166 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5168 int c
= 0, val
= -1;
5170 /* Skip white-space. */
5171 while ((c
= pbm_next_char (s
, end
)) != -1 && c_isspace (c
))
5176 /* Read decimal number. */
5178 while ((c
= pbm_next_char (s
, end
)) != -1 && c_isdigit (c
))
5179 val
= 10 * val
+ c
- '0';
5186 /* Load PBM image IMG for use on frame F. */
5189 pbm_load (struct frame
*f
, struct image
*img
)
5193 int width
, height
, max_color_idx
= 0;
5195 Lisp_Object file
, specified_file
;
5196 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5197 unsigned char *contents
= NULL
;
5198 unsigned char *end
, *p
;
5201 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5203 if (STRINGP (specified_file
))
5205 file
= x_find_image_file (specified_file
);
5206 if (!STRINGP (file
))
5208 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5212 contents
= slurp_file (SSDATA (file
), &size
);
5213 if (contents
== NULL
)
5215 image_error ("Error reading `%s'", file
, Qnil
);
5220 end
= contents
+ size
;
5225 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5226 if (!STRINGP (data
))
5228 image_error ("Invalid image data `%s'", data
, Qnil
);
5232 end
= p
+ SBYTES (data
);
5235 /* Check magic number. */
5236 if (end
- p
< 2 || *p
++ != 'P')
5238 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5241 img
->pixmap
= NO_PIXMAP
;
5248 raw_p
= 0, type
= PBM_MONO
;
5252 raw_p
= 0, type
= PBM_GRAY
;
5256 raw_p
= 0, type
= PBM_COLOR
;
5260 raw_p
= 1, type
= PBM_MONO
;
5264 raw_p
= 1, type
= PBM_GRAY
;
5268 raw_p
= 1, type
= PBM_COLOR
;
5272 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5276 /* Read width, height, maximum color-component. Characters
5277 starting with `#' up to the end of a line are ignored. */
5278 width
= pbm_scan_number (&p
, end
);
5279 height
= pbm_scan_number (&p
, end
);
5281 if (type
!= PBM_MONO
)
5283 max_color_idx
= pbm_scan_number (&p
, end
);
5284 if (max_color_idx
> 65535 || max_color_idx
< 0)
5286 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5291 if (!check_image_size (f
, width
, height
))
5293 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5297 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5300 /* Initialize the color hash table. */
5301 init_color_table ();
5303 if (type
== PBM_MONO
)
5306 struct image_keyword fmt
[PBM_LAST
];
5307 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5308 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5310 /* Parse the image specification. */
5311 memcpy (fmt
, pbm_format
, sizeof fmt
);
5312 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5314 /* Get foreground and background colors, maybe allocate colors. */
5315 if (fmt
[PBM_FOREGROUND
].count
5316 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5317 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5318 if (fmt
[PBM_BACKGROUND
].count
5319 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5321 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5322 img
->background
= bg
;
5323 img
->background_valid
= 1;
5326 for (y
= 0; y
< height
; ++y
)
5327 for (x
= 0; x
< width
; ++x
)
5335 x_destroy_x_image (ximg
);
5336 x_clear_image (f
, img
);
5337 image_error ("Invalid image size in image `%s'",
5347 g
= pbm_scan_number (&p
, end
);
5349 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5354 int expected_size
= height
* width
;
5355 if (max_color_idx
> 255)
5357 if (type
== PBM_COLOR
)
5360 if (raw_p
&& p
+ expected_size
> end
)
5362 x_destroy_x_image (ximg
);
5363 x_clear_image (f
, img
);
5364 image_error ("Invalid image size in image `%s'",
5369 for (y
= 0; y
< height
; ++y
)
5370 for (x
= 0; x
< width
; ++x
)
5374 if (type
== PBM_GRAY
&& raw_p
)
5377 if (max_color_idx
> 255)
5378 r
= g
= b
= r
* 256 + *p
++;
5380 else if (type
== PBM_GRAY
)
5381 r
= g
= b
= pbm_scan_number (&p
, end
);
5385 if (max_color_idx
> 255)
5388 if (max_color_idx
> 255)
5391 if (max_color_idx
> 255)
5396 r
= pbm_scan_number (&p
, end
);
5397 g
= pbm_scan_number (&p
, end
);
5398 b
= pbm_scan_number (&p
, end
);
5401 if (r
< 0 || g
< 0 || b
< 0)
5403 x_destroy_x_image (ximg
);
5404 image_error ("Invalid pixel value in image `%s'",
5409 /* RGB values are now in the range 0..max_color_idx.
5410 Scale this to the range 0..0xffff supported by X. */
5411 r
= (double) r
* 65535 / max_color_idx
;
5412 g
= (double) g
* 65535 / max_color_idx
;
5413 b
= (double) b
* 65535 / max_color_idx
;
5414 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5418 #ifdef COLOR_TABLE_SUPPORT
5419 /* Store in IMG->colors the colors allocated for the image, and
5420 free the color table. */
5421 img
->colors
= colors_in_color_table (&img
->ncolors
);
5422 free_color_table ();
5423 #endif /* COLOR_TABLE_SUPPORT */
5426 img
->height
= height
;
5428 /* Maybe fill in the background field while we have ximg handy. */
5430 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5431 /* Casting avoids a GCC warning. */
5432 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5434 /* Put ximg into the image. */
5435 image_put_x_image (f
, img
, ximg
, 0);
5437 /* X and W32 versions did it here, MAC version above. ++kfs
5439 img->height = height; */
5446 /***********************************************************************
5448 ***********************************************************************/
5450 #if defined (HAVE_PNG) || defined (HAVE_NS)
5452 /* Function prototypes. */
5454 static bool png_image_p (Lisp_Object object
);
5455 static bool png_load (struct frame
*f
, struct image
*img
);
5457 /* The symbol `png' identifying images of this type. */
5459 static Lisp_Object Qpng
;
5461 /* Indices of image specification fields in png_format, below. */
5463 enum png_keyword_index
5478 /* Vector of image_keyword structures describing the format
5479 of valid user-defined image specifications. */
5481 static const struct image_keyword png_format
[PNG_LAST
] =
5483 {":type", IMAGE_SYMBOL_VALUE
, 1},
5484 {":data", IMAGE_STRING_VALUE
, 0},
5485 {":file", IMAGE_STRING_VALUE
, 0},
5486 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5487 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5488 {":relief", IMAGE_INTEGER_VALUE
, 0},
5489 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5490 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5491 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5492 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5495 #if defined HAVE_NTGUI && defined WINDOWSNT
5496 static bool init_png_functions (void);
5498 #define init_png_functions NULL
5501 /* Structure describing the image type `png'. */
5503 static struct image_type png_type
=
5513 /* Return true if OBJECT is a valid PNG image specification. */
5516 png_image_p (Lisp_Object object
)
5518 struct image_keyword fmt
[PNG_LAST
];
5519 memcpy (fmt
, png_format
, sizeof fmt
);
5521 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5524 /* Must specify either the :data or :file keyword. */
5525 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5528 #endif /* HAVE_PNG || HAVE_NS */
5531 #if defined HAVE_PNG && !defined HAVE_NS
5534 /* PNG library details. */
5536 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5537 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5538 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5539 png_error_ptr
, png_error_ptr
));
5540 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5541 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5542 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5543 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5544 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5545 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5546 png_uint_32
*, png_uint_32
*,
5547 int *, int *, int *, int *, int *));
5548 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5549 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5550 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5551 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5552 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5554 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5555 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5556 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5557 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5558 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5559 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5560 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5562 #if (PNG_LIBPNG_VER >= 10500)
5563 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int)) PNG_NORETURN
;
5564 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5565 #endif /* libpng version >= 1.5 */
5568 init_png_functions (void)
5572 if (!(library
= w32_delayed_load (Qpng
)))
5575 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5576 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5577 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5578 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5579 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5580 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5581 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5582 LOAD_IMGLIB_FN (library
, png_read_info
);
5583 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5584 LOAD_IMGLIB_FN (library
, png_get_valid
);
5585 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5586 LOAD_IMGLIB_FN (library
, png_set_expand
);
5587 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5588 LOAD_IMGLIB_FN (library
, png_set_background
);
5589 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5590 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5591 LOAD_IMGLIB_FN (library
, png_get_channels
);
5592 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5593 LOAD_IMGLIB_FN (library
, png_read_image
);
5594 LOAD_IMGLIB_FN (library
, png_read_end
);
5595 LOAD_IMGLIB_FN (library
, png_error
);
5597 #if (PNG_LIBPNG_VER >= 10500)
5598 LOAD_IMGLIB_FN (library
, png_longjmp
);
5599 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5600 #endif /* libpng version >= 1.5 */
5606 #define fn_png_get_io_ptr png_get_io_ptr
5607 #define fn_png_sig_cmp png_sig_cmp
5608 #define fn_png_create_read_struct png_create_read_struct
5609 #define fn_png_create_info_struct png_create_info_struct
5610 #define fn_png_destroy_read_struct png_destroy_read_struct
5611 #define fn_png_set_read_fn png_set_read_fn
5612 #define fn_png_set_sig_bytes png_set_sig_bytes
5613 #define fn_png_read_info png_read_info
5614 #define fn_png_get_IHDR png_get_IHDR
5615 #define fn_png_get_valid png_get_valid
5616 #define fn_png_set_strip_16 png_set_strip_16
5617 #define fn_png_set_expand png_set_expand
5618 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5619 #define fn_png_set_background png_set_background
5620 #define fn_png_get_bKGD png_get_bKGD
5621 #define fn_png_read_update_info png_read_update_info
5622 #define fn_png_get_channels png_get_channels
5623 #define fn_png_get_rowbytes png_get_rowbytes
5624 #define fn_png_read_image png_read_image
5625 #define fn_png_read_end png_read_end
5626 #define fn_png_error png_error
5628 #if (PNG_LIBPNG_VER >= 10500)
5629 #define fn_png_longjmp png_longjmp
5630 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5631 #endif /* libpng version >= 1.5 */
5633 #endif /* WINDOWSNT */
5635 /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp
5636 will do, POSIX _setjmp and _longjmp (if available) are often faster.
5637 Do not use sys_setjmp, as PNG supports only jmp_buf.
5638 It's OK if the longjmp substitute restores the signal mask. */
5640 # define FAST_SETJMP(j) _setjmp (j)
5641 # define FAST_LONGJMP _longjmp
5643 # define FAST_SETJMP(j) setjmp (j)
5644 # define FAST_LONGJMP longjmp
5647 #if PNG_LIBPNG_VER < 10500
5648 #define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1)
5649 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5651 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5652 #define PNG_LONGJMP(ptr) fn_png_longjmp (ptr, 1)
5653 #define PNG_JMPBUF(ptr) \
5654 (*fn_png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf)))
5657 /* Error and warning handlers installed when the PNG library
5660 static _Noreturn
void
5661 my_png_error (png_struct
*png_ptr
, const char *msg
)
5663 eassert (png_ptr
!= NULL
);
5664 /* Avoid compiler warning about deprecated direct access to
5665 png_ptr's fields in libpng versions 1.4.x. */
5666 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5667 PNG_LONGJMP (png_ptr
);
5672 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5674 eassert (png_ptr
!= NULL
);
5675 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5678 /* Memory source for PNG decoding. */
5680 struct png_memory_storage
5682 unsigned char *bytes
; /* The data */
5683 ptrdiff_t len
; /* How big is it? */
5684 ptrdiff_t index
; /* Where are we? */
5688 /* Function set as reader function when reading PNG image from memory.
5689 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5690 bytes from the input to DATA. */
5693 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5695 struct png_memory_storage
*tbr
= fn_png_get_io_ptr (png_ptr
);
5697 if (length
> tbr
->len
- tbr
->index
)
5698 fn_png_error (png_ptr
, "Read error");
5700 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5701 tbr
->index
= tbr
->index
+ length
;
5705 /* Function set as reader function when reading PNG image from a file.
5706 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5707 bytes from the input to DATA. */
5710 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5712 FILE *fp
= fn_png_get_io_ptr (png_ptr
);
5714 if (fread (data
, 1, length
, fp
) < length
)
5715 fn_png_error (png_ptr
, "Read error");
5719 /* Load PNG image IMG for use on frame F. Value is true if
5722 struct png_load_context
5724 /* These are members so that longjmp doesn't munge local variables. */
5725 png_struct
*png_ptr
;
5734 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5736 Lisp_Object file
, specified_file
;
5737 Lisp_Object specified_data
;
5740 XImagePtr ximg
, mask_img
= NULL
;
5741 png_struct
*png_ptr
;
5742 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5745 png_byte
*pixels
= NULL
;
5746 png_byte
**rows
= NULL
;
5747 png_uint_32 width
, height
;
5748 int bit_depth
, color_type
, interlace_type
;
5750 png_uint_32 row_bytes
;
5752 struct png_memory_storage tbr
; /* Data to be read */
5754 /* Find out what file to load. */
5755 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5756 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5758 if (NILP (specified_data
))
5760 file
= x_find_image_file (specified_file
);
5761 if (!STRINGP (file
))
5763 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5767 /* Open the image file. */
5768 fp
= emacs_fopen (SSDATA (file
), "rb");
5771 image_error ("Cannot open image file `%s'", file
, Qnil
);
5775 /* Check PNG signature. */
5776 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5777 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5780 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5786 if (!STRINGP (specified_data
))
5788 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5792 /* Read from memory. */
5793 tbr
.bytes
= SDATA (specified_data
);
5794 tbr
.len
= SBYTES (specified_data
);
5797 /* Check PNG signature. */
5798 if (tbr
.len
< sizeof sig
5799 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5801 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5805 /* Need to skip past the signature. */
5806 tbr
.bytes
+= sizeof (sig
);
5809 /* Initialize read and info structs for PNG lib. */
5810 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5815 info_ptr
= fn_png_create_info_struct (png_ptr
);
5816 end_info
= fn_png_create_info_struct (png_ptr
);
5819 c
->png_ptr
= png_ptr
;
5820 c
->info_ptr
= info_ptr
;
5821 c
->end_info
= end_info
;
5826 if (! (info_ptr
&& end_info
))
5828 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5833 if (fp
) fclose (fp
);
5837 /* Set error jump-back. We come back here when the PNG library
5838 detects an error. */
5839 if (FAST_SETJMP (PNG_JMPBUF (png_ptr
)))
5843 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5851 /* Silence a bogus diagnostic; see GCC bug 54561. */
5852 IF_LINT (fp
= c
->fp
);
5854 /* Read image info. */
5855 if (!NILP (specified_data
))
5856 fn_png_set_read_fn (png_ptr
, &tbr
, png_read_from_memory
);
5858 fn_png_set_read_fn (png_ptr
, fp
, png_read_from_file
);
5860 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5861 fn_png_read_info (png_ptr
, info_ptr
);
5862 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5863 &interlace_type
, NULL
, NULL
);
5865 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5866 && check_image_size (f
, width
, height
)))
5868 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5872 /* Create the X image and pixmap now, so that the work below can be
5873 omitted if the image is too large for X. */
5874 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5877 /* If image contains simply transparency data, we prefer to
5878 construct a clipping mask. */
5879 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5884 /* This function is easier to write if we only have to handle
5885 one data format: RGB or RGBA with 8 bits per channel. Let's
5886 transform other formats into that format. */
5888 /* Strip more than 8 bits per channel. */
5889 if (bit_depth
== 16)
5890 fn_png_set_strip_16 (png_ptr
);
5892 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5894 fn_png_set_expand (png_ptr
);
5896 /* Convert grayscale images to RGB. */
5897 if (color_type
== PNG_COLOR_TYPE_GRAY
5898 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5899 fn_png_set_gray_to_rgb (png_ptr
);
5901 /* Handle alpha channel by combining the image with a background
5902 color. Do this only if a real alpha channel is supplied. For
5903 simple transparency, we prefer a clipping mask. */
5906 /* png_color_16 *image_bg; */
5907 Lisp_Object specified_bg
5908 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5911 /* If the user specified a color, try to use it; if not, use the
5912 current frame background, ignoring any default background
5913 color set by the image. */
5914 if (STRINGP (specified_bg
)
5915 ? x_defined_color (f
, SSDATA (specified_bg
), &color
, false)
5916 : (x_query_frame_background_color (f
, &color
), true))
5917 /* The user specified `:background', use that. */
5919 int shift
= bit_depth
== 16 ? 0 : 8;
5920 png_color_16 bg
= { 0 };
5921 bg
.red
= color
.red
>> shift
;
5922 bg
.green
= color
.green
>> shift
;
5923 bg
.blue
= color
.blue
>> shift
;
5925 fn_png_set_background (png_ptr
, &bg
,
5926 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5930 /* Update info structure. */
5931 fn_png_read_update_info (png_ptr
, info_ptr
);
5933 /* Get number of channels. Valid values are 1 for grayscale images
5934 and images with a palette, 2 for grayscale images with transparency
5935 information (alpha channel), 3 for RGB images, and 4 for RGB
5936 images with alpha channel, i.e. RGBA. If conversions above were
5937 sufficient we should only have 3 or 4 channels here. */
5938 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5939 eassert (channels
== 3 || channels
== 4);
5941 /* Number of bytes needed for one row of the image. */
5942 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5944 /* Allocate memory for the image. */
5945 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5946 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5947 memory_full (SIZE_MAX
);
5948 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5949 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5950 for (i
= 0; i
< height
; ++i
)
5951 rows
[i
] = pixels
+ i
* row_bytes
;
5953 /* Read the entire image. */
5954 fn_png_read_image (png_ptr
, rows
);
5955 fn_png_read_end (png_ptr
, info_ptr
);
5962 /* Create an image and pixmap serving as mask if the PNG image
5963 contains an alpha channel. */
5966 && !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
5969 x_destroy_x_image (ximg
);
5970 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
);
5974 /* Fill the X image and mask from PNG data. */
5975 init_color_table ();
5977 for (y
= 0; y
< height
; ++y
)
5979 png_byte
*p
= rows
[y
];
5981 for (x
= 0; x
< width
; ++x
)
5988 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5989 /* An alpha channel, aka mask channel, associates variable
5990 transparency with an image. Where other image formats
5991 support binary transparency---fully transparent or fully
5992 opaque---PNG allows up to 254 levels of partial transparency.
5993 The PNG library implements partial transparency by combining
5994 the image with a specified background color.
5996 I'm not sure how to handle this here nicely: because the
5997 background on which the image is displayed may change, for
5998 real alpha channel support, it would be necessary to create
5999 a new image for each possible background.
6001 What I'm doing now is that a mask is created if we have
6002 boolean transparency information. Otherwise I'm using
6003 the frame's background color to combine the image with. */
6008 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
6014 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6015 /* Set IMG's background color from the PNG image, unless the user
6019 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
6021 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
6022 img
->background_valid
= 1;
6026 #ifdef COLOR_TABLE_SUPPORT
6027 /* Remember colors allocated for this image. */
6028 img
->colors
= colors_in_color_table (&img
->ncolors
);
6029 free_color_table ();
6030 #endif /* COLOR_TABLE_SUPPORT */
6033 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
6038 img
->height
= height
;
6040 /* Maybe fill in the background field while we have ximg handy.
6041 Casting avoids a GCC warning. */
6042 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6044 /* Put ximg into the image. */
6045 image_put_x_image (f
, img
, ximg
, 0);
6047 /* Same for the mask. */
6050 /* Fill in the background_transparent field while we have the
6051 mask handy. Casting avoids a GCC warning. */
6052 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
6054 image_put_x_image (f
, img
, mask_img
, 1);
6061 png_load (struct frame
*f
, struct image
*img
)
6063 struct png_load_context c
;
6064 return png_load_body (f
, img
, &c
);
6067 #elif defined HAVE_NS
6070 png_load (struct frame
*f
, struct image
*img
)
6072 return ns_load_image (f
, img
,
6073 image_spec_value (img
->spec
, QCfile
, NULL
),
6074 image_spec_value (img
->spec
, QCdata
, NULL
));
6077 #endif /* HAVE_NS */
6081 /***********************************************************************
6083 ***********************************************************************/
6085 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6087 static bool jpeg_image_p (Lisp_Object object
);
6088 static bool jpeg_load (struct frame
*f
, struct image
*img
);
6090 /* The symbol `jpeg' identifying images of this type. */
6092 static Lisp_Object Qjpeg
;
6094 /* Indices of image specification fields in gs_format, below. */
6096 enum jpeg_keyword_index
6105 JPEG_HEURISTIC_MASK
,
6111 /* Vector of image_keyword structures describing the format
6112 of valid user-defined image specifications. */
6114 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6116 {":type", IMAGE_SYMBOL_VALUE
, 1},
6117 {":data", IMAGE_STRING_VALUE
, 0},
6118 {":file", IMAGE_STRING_VALUE
, 0},
6119 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6120 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6121 {":relief", IMAGE_INTEGER_VALUE
, 0},
6122 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6123 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6124 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6125 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6128 #if defined HAVE_NTGUI && defined WINDOWSNT
6129 static bool init_jpeg_functions (void);
6131 #define init_jpeg_functions NULL
6134 /* Structure describing the image type `jpeg'. */
6136 static struct image_type jpeg_type
=
6142 init_jpeg_functions
,
6146 /* Return true if OBJECT is a valid JPEG image specification. */
6149 jpeg_image_p (Lisp_Object object
)
6151 struct image_keyword fmt
[JPEG_LAST
];
6153 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6155 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6158 /* Must specify either the :data or :file keyword. */
6159 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6162 #endif /* HAVE_JPEG || HAVE_NS */
6166 /* Work around a warning about HAVE_STDLIB_H being redefined in
6168 #ifdef HAVE_STDLIB_H
6169 #undef HAVE_STDLIB_H
6170 #endif /* HAVE_STLIB_H */
6172 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6173 /* In older releases of the jpeg library, jpeglib.h will define boolean
6174 differently depending on __WIN32__, so make sure it is defined. */
6178 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6179 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6180 using the Windows boolean type instead of the jpeglib boolean type
6181 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6182 headers are included along with windows.h, so under Cygwin, jpeglib
6183 attempts to define a conflicting boolean type. Worse, forcing
6184 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6185 because it created an ABI incompatibility between the
6186 already-compiled jpeg library and the header interface definition.
6188 The best we can do is to define jpeglib's boolean type to a
6189 different name. This name, jpeg_boolean, remains in effect through
6190 the rest of image.c.
6192 #if defined CYGWIN && defined HAVE_NTGUI
6193 #define boolean jpeg_boolean
6195 #include <jpeglib.h>
6200 /* JPEG library details. */
6201 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6202 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6203 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6204 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6205 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6206 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6207 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6208 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6211 init_jpeg_functions (void)
6215 if (!(library
= w32_delayed_load (Qjpeg
)))
6218 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6219 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6220 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6221 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6222 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6223 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6224 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6225 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6229 /* Wrapper since we can't directly assign the function pointer
6230 to another function pointer that was declared more completely easily. */
6232 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6234 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6239 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6240 #define fn_jpeg_start_decompress jpeg_start_decompress
6241 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6242 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6243 #define fn_jpeg_read_header jpeg_read_header
6244 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6245 #define fn_jpeg_std_error jpeg_std_error
6246 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6248 #endif /* WINDOWSNT */
6250 struct my_jpeg_error_mgr
6252 struct jpeg_error_mgr pub
;
6253 sys_jmp_buf setjmp_buffer
;
6255 /* The remaining members are so that longjmp doesn't munge local
6257 struct jpeg_decompress_struct cinfo
;
6261 MY_JPEG_INVALID_IMAGE_SIZE
,
6262 MY_JPEG_CANNOT_CREATE_X
6267 static _Noreturn
void
6268 my_error_exit (j_common_ptr cinfo
)
6270 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6271 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6272 sys_longjmp (mgr
->setjmp_buffer
, 1);
6276 /* Init source method for JPEG data source manager. Called by
6277 jpeg_read_header() before any data is actually read. See
6278 libjpeg.doc from the JPEG lib distribution. */
6281 our_common_init_source (j_decompress_ptr cinfo
)
6286 /* Method to terminate data source. Called by
6287 jpeg_finish_decompress() after all data has been processed. */
6290 our_common_term_source (j_decompress_ptr cinfo
)
6295 /* Fill input buffer method for JPEG data source manager. Called
6296 whenever more data is needed. We read the whole image in one step,
6297 so this only adds a fake end of input marker at the end. */
6299 static JOCTET our_memory_buffer
[2];
6302 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6304 /* Insert a fake EOI marker. */
6305 struct jpeg_source_mgr
*src
= cinfo
->src
;
6307 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6308 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6310 src
->next_input_byte
= our_memory_buffer
;
6311 src
->bytes_in_buffer
= 2;
6316 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6317 is the JPEG data source manager. */
6320 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6322 struct jpeg_source_mgr
*src
= cinfo
->src
;
6326 if (num_bytes
> src
->bytes_in_buffer
)
6327 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6329 src
->bytes_in_buffer
-= num_bytes
;
6330 src
->next_input_byte
+= num_bytes
;
6335 /* Set up the JPEG lib for reading an image from DATA which contains
6336 LEN bytes. CINFO is the decompression info structure created for
6337 reading the image. */
6340 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6342 struct jpeg_source_mgr
*src
= cinfo
->src
;
6346 /* First time for this JPEG object? */
6347 src
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6348 JPOOL_PERMANENT
, sizeof *src
);
6350 src
->next_input_byte
= data
;
6353 src
->init_source
= our_common_init_source
;
6354 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6355 src
->skip_input_data
= our_memory_skip_input_data
;
6356 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6357 src
->term_source
= our_common_term_source
;
6358 src
->bytes_in_buffer
= len
;
6359 src
->next_input_byte
= data
;
6363 struct jpeg_stdio_mgr
6365 struct jpeg_source_mgr mgr
;
6372 /* Size of buffer to read JPEG from file.
6373 Not too big, as we want to use alloc_small. */
6374 #define JPEG_STDIO_BUFFER_SIZE 8192
6377 /* Fill input buffer method for JPEG data source manager. Called
6378 whenever more data is needed. The data is read from a FILE *. */
6381 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6383 struct jpeg_stdio_mgr
*src
;
6385 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6390 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6392 src
->mgr
.bytes_in_buffer
= bytes
;
6395 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6397 src
->buffer
[0] = (JOCTET
) 0xFF;
6398 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6399 src
->mgr
.bytes_in_buffer
= 2;
6401 src
->mgr
.next_input_byte
= src
->buffer
;
6408 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6409 is the JPEG data source manager. */
6412 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6414 struct jpeg_stdio_mgr
*src
;
6415 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6417 while (num_bytes
> 0 && !src
->finished
)
6419 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6421 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6422 src
->mgr
.next_input_byte
+= num_bytes
;
6427 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6428 src
->mgr
.bytes_in_buffer
= 0;
6429 src
->mgr
.next_input_byte
= NULL
;
6431 our_stdio_fill_input_buffer (cinfo
);
6437 /* Set up the JPEG lib for reading an image from a FILE *.
6438 CINFO is the decompression info structure created for
6439 reading the image. */
6442 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6444 struct jpeg_stdio_mgr
*src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6448 /* First time for this JPEG object? */
6449 src
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6450 JPOOL_PERMANENT
, sizeof *src
);
6451 cinfo
->src
= (struct jpeg_source_mgr
*) src
;
6452 src
->buffer
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6454 JPEG_STDIO_BUFFER_SIZE
);
6459 src
->mgr
.init_source
= our_common_init_source
;
6460 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6461 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6462 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6463 src
->mgr
.term_source
= our_common_term_source
;
6464 src
->mgr
.bytes_in_buffer
= 0;
6465 src
->mgr
.next_input_byte
= NULL
;
6468 /* Load image IMG for use on frame F. Patterned after example.c
6469 from the JPEG lib. */
6472 jpeg_load_body (struct frame
*f
, struct image
*img
,
6473 struct my_jpeg_error_mgr
*mgr
)
6475 Lisp_Object file
, specified_file
;
6476 Lisp_Object specified_data
;
6477 /* The 'volatile' silences a bogus diagnostic; see GCC bug 54561. */
6478 FILE * IF_LINT (volatile) fp
= NULL
;
6480 int row_stride
, x
, y
;
6481 XImagePtr ximg
= NULL
;
6482 unsigned long *colors
;
6485 /* Open the JPEG file. */
6486 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6487 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6489 if (NILP (specified_data
))
6491 file
= x_find_image_file (specified_file
);
6492 if (!STRINGP (file
))
6494 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6498 fp
= emacs_fopen (SSDATA (file
), "rb");
6501 image_error ("Cannot open `%s'", file
, Qnil
);
6505 else if (!STRINGP (specified_data
))
6507 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6511 /* Customize libjpeg's error handling to call my_error_exit when an
6512 error is detected. This function will perform a longjmp. */
6513 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6514 mgr
->pub
.error_exit
= my_error_exit
;
6515 if (sys_setjmp (mgr
->setjmp_buffer
))
6517 switch (mgr
->failure_code
)
6519 case MY_JPEG_ERROR_EXIT
:
6521 char buf
[JMSG_LENGTH_MAX
];
6522 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6523 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6524 build_string (buf
));
6528 case MY_JPEG_INVALID_IMAGE_SIZE
:
6529 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6532 case MY_JPEG_CANNOT_CREATE_X
:
6536 /* Close the input file and destroy the JPEG object. */
6539 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6541 /* If we already have an XImage, free that. */
6542 x_destroy_x_image (ximg
);
6544 /* Free pixmap and colors. */
6545 x_clear_image (f
, img
);
6549 /* Create the JPEG decompression object. Let it read from fp.
6550 Read the JPEG image header. */
6551 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6553 if (NILP (specified_data
))
6554 jpeg_file_src (&mgr
->cinfo
, fp
);
6556 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6557 SBYTES (specified_data
));
6559 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6561 /* Customize decompression so that color quantization will be used.
6562 Start decompression. */
6563 mgr
->cinfo
.quantize_colors
= 1;
6564 fn_jpeg_start_decompress (&mgr
->cinfo
);
6565 width
= img
->width
= mgr
->cinfo
.output_width
;
6566 height
= img
->height
= mgr
->cinfo
.output_height
;
6568 if (!check_image_size (f
, width
, height
))
6570 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6571 sys_longjmp (mgr
->setjmp_buffer
, 1);
6574 /* Create X image and pixmap. */
6575 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
6577 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6578 sys_longjmp (mgr
->setjmp_buffer
, 1);
6581 /* Allocate colors. When color quantization is used,
6582 mgr->cinfo.actual_number_of_colors has been set with the number of
6583 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6584 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6585 No more than 255 colors will be generated. */
6590 if (mgr
->cinfo
.out_color_components
> 2)
6591 ir
= 0, ig
= 1, ib
= 2;
6592 else if (mgr
->cinfo
.out_color_components
> 1)
6593 ir
= 0, ig
= 1, ib
= 0;
6595 ir
= 0, ig
= 0, ib
= 0;
6597 /* Use the color table mechanism because it handles colors that
6598 cannot be allocated nicely. Such colors will be replaced with
6599 a default color, and we don't have to care about which colors
6600 can be freed safely, and which can't. */
6601 init_color_table ();
6602 SAFE_NALLOCA (colors
, 1, mgr
->cinfo
.actual_number_of_colors
);
6604 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6606 /* Multiply RGB values with 255 because X expects RGB values
6607 in the range 0..0xffff. */
6608 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6609 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6610 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6611 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6614 #ifdef COLOR_TABLE_SUPPORT
6615 /* Remember those colors actually allocated. */
6616 img
->colors
= colors_in_color_table (&img
->ncolors
);
6617 free_color_table ();
6618 #endif /* COLOR_TABLE_SUPPORT */
6622 row_stride
= width
* mgr
->cinfo
.output_components
;
6623 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6624 JPOOL_IMAGE
, row_stride
, 1);
6625 for (y
= 0; y
< height
; ++y
)
6627 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6628 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6629 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6633 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6634 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6638 /* Maybe fill in the background field while we have ximg handy. */
6639 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6640 /* Casting avoids a GCC warning. */
6641 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6643 /* Put ximg into the image. */
6644 image_put_x_image (f
, img
, ximg
, 0);
6650 jpeg_load (struct frame
*f
, struct image
*img
)
6652 struct my_jpeg_error_mgr mgr
;
6653 return jpeg_load_body (f
, img
, &mgr
);
6656 #else /* HAVE_JPEG */
6660 jpeg_load (struct frame
*f
, struct image
*img
)
6662 return ns_load_image (f
, img
,
6663 image_spec_value (img
->spec
, QCfile
, NULL
),
6664 image_spec_value (img
->spec
, QCdata
, NULL
));
6666 #endif /* HAVE_NS */
6668 #endif /* !HAVE_JPEG */
6672 /***********************************************************************
6674 ***********************************************************************/
6676 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6678 static bool tiff_image_p (Lisp_Object object
);
6679 static bool tiff_load (struct frame
*f
, struct image
*img
);
6681 /* The symbol `tiff' identifying images of this type. */
6683 static Lisp_Object Qtiff
;
6685 /* Indices of image specification fields in tiff_format, below. */
6687 enum tiff_keyword_index
6696 TIFF_HEURISTIC_MASK
,
6703 /* Vector of image_keyword structures describing the format
6704 of valid user-defined image specifications. */
6706 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6708 {":type", IMAGE_SYMBOL_VALUE
, 1},
6709 {":data", IMAGE_STRING_VALUE
, 0},
6710 {":file", IMAGE_STRING_VALUE
, 0},
6711 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6712 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6713 {":relief", IMAGE_INTEGER_VALUE
, 0},
6714 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6715 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6716 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6717 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6718 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6721 #if defined HAVE_NTGUI && defined WINDOWSNT
6722 static bool init_tiff_functions (void);
6724 #define init_tiff_functions NULL
6727 /* Structure describing the image type `tiff'. */
6729 static struct image_type tiff_type
=
6735 init_tiff_functions
,
6739 /* Return true if OBJECT is a valid TIFF image specification. */
6742 tiff_image_p (Lisp_Object object
)
6744 struct image_keyword fmt
[TIFF_LAST
];
6745 memcpy (fmt
, tiff_format
, sizeof fmt
);
6747 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6750 /* Must specify either the :data or :file keyword. */
6751 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6754 #endif /* HAVE_TIFF || HAVE_NS */
6762 /* TIFF library details. */
6763 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6764 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6765 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6766 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6767 TIFFReadWriteProc
, TIFFReadWriteProc
,
6768 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6769 TIFFMapFileProc
, TIFFUnmapFileProc
));
6770 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6771 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6772 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6773 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6776 init_tiff_functions (void)
6780 if (!(library
= w32_delayed_load (Qtiff
)))
6783 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6784 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6785 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6786 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6787 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6788 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6789 LOAD_IMGLIB_FN (library
, TIFFClose
);
6790 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6796 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6797 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6798 #define fn_TIFFOpen TIFFOpen
6799 #define fn_TIFFClientOpen TIFFClientOpen
6800 #define fn_TIFFGetField TIFFGetField
6801 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6802 #define fn_TIFFClose TIFFClose
6803 #define fn_TIFFSetDirectory TIFFSetDirectory
6804 #endif /* WINDOWSNT */
6807 /* Reading from a memory buffer for TIFF images Based on the PNG
6808 memory source, but we have to provide a lot of extra functions.
6811 We really only need to implement read and seek, but I am not
6812 convinced that the TIFF library is smart enough not to destroy
6813 itself if we only hand it the function pointers we need to
6818 unsigned char *bytes
;
6825 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6827 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6829 size
= min (size
, src
->len
- src
->index
);
6830 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6836 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6842 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6844 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6849 case SEEK_SET
: /* Go from beginning of source. */
6853 case SEEK_END
: /* Go from end of source. */
6854 idx
= src
->len
+ off
;
6857 case SEEK_CUR
: /* Go from current position. */
6858 idx
= src
->index
+ off
;
6861 default: /* Invalid `whence'. */
6865 if (idx
> src
->len
|| idx
< 0)
6873 tiff_close_memory (thandle_t data
)
6880 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6882 /* It is already _IN_ memory. */
6887 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6889 /* We don't need to do this. */
6893 tiff_size_of_memory (thandle_t data
)
6895 return ((tiff_memory_source
*) data
)->len
;
6898 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6899 compiler error compiling tiff_handler, see Bugzilla bug #17406
6900 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6901 this function as external works around that problem. */
6902 #if defined (__MINGW32__) && __GNUC__ == 3
6903 # define MINGW_STATIC
6905 # define MINGW_STATIC static
6909 tiff_handler (const char *, const char *, const char *, va_list)
6910 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6912 tiff_handler (const char *log_format
, const char *title
,
6913 const char *format
, va_list ap
)
6915 /* doprnt is not suitable here, as TIFF handlers are called from
6916 libtiff and are passed arbitrary printf directives. Instead, use
6917 vsnprintf, taking care to be portable to nonstandard environments
6918 where vsnprintf returns -1 on buffer overflow. Since it's just a
6919 log entry, it's OK to truncate it. */
6921 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6922 add_to_log (log_format
, build_string (title
),
6923 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6927 static void tiff_error_handler (const char *, const char *, va_list)
6928 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6930 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6932 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6936 static void tiff_warning_handler (const char *, const char *, va_list)
6937 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6939 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6941 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6945 /* Load TIFF image IMG for use on frame F. Value is true if
6949 tiff_load (struct frame
*f
, struct image
*img
)
6951 Lisp_Object file
, specified_file
;
6952 Lisp_Object specified_data
;
6954 int width
, height
, x
, y
, count
;
6958 tiff_memory_source memsrc
;
6961 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6962 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6964 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6965 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6967 if (NILP (specified_data
))
6969 /* Read from a file */
6970 file
= x_find_image_file (specified_file
);
6971 if (!STRINGP (file
))
6973 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6977 file
= ansi_encode_filename (file
);
6980 /* Try to open the image file. */
6981 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6984 image_error ("Cannot open `%s'", file
, Qnil
);
6990 if (!STRINGP (specified_data
))
6992 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6996 /* Memory source! */
6997 memsrc
.bytes
= SDATA (specified_data
);
6998 memsrc
.len
= SBYTES (specified_data
);
7001 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
7002 tiff_read_from_memory
,
7003 tiff_write_from_memory
,
7004 tiff_seek_in_memory
,
7006 tiff_size_of_memory
,
7012 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
7017 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7018 if (INTEGERP (image
))
7020 EMACS_INT ino
= XFASTINT (image
);
7021 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
7022 && fn_TIFFSetDirectory (tiff
, ino
)))
7024 image_error ("Invalid image number `%s' in image `%s'",
7026 fn_TIFFClose (tiff
);
7031 /* Get width and height of the image, and allocate a raster buffer
7032 of width x height 32-bit values. */
7033 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
7034 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
7036 if (!check_image_size (f
, width
, height
))
7038 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7039 fn_TIFFClose (tiff
);
7043 /* Create the X image and pixmap. */
7044 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
7045 && image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
7048 fn_TIFFClose (tiff
);
7052 buf
= xmalloc (sizeof *buf
* width
* height
);
7054 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
7056 /* Count the number of images in the file. */
7057 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
7061 img
->lisp_data
= Fcons (Qcount
,
7062 Fcons (make_number (count
),
7065 fn_TIFFClose (tiff
);
7068 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
7073 /* Initialize the color table. */
7074 init_color_table ();
7076 /* Process the pixel raster. Origin is in the lower-left corner. */
7077 for (y
= 0; y
< height
; ++y
)
7079 uint32
*row
= buf
+ y
* width
;
7081 for (x
= 0; x
< width
; ++x
)
7083 uint32 abgr
= row
[x
];
7084 int r
= TIFFGetR (abgr
) << 8;
7085 int g
= TIFFGetG (abgr
) << 8;
7086 int b
= TIFFGetB (abgr
) << 8;
7087 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
7091 #ifdef COLOR_TABLE_SUPPORT
7092 /* Remember the colors allocated for the image. Free the color table. */
7093 img
->colors
= colors_in_color_table (&img
->ncolors
);
7094 free_color_table ();
7095 #endif /* COLOR_TABLE_SUPPORT */
7098 img
->height
= height
;
7100 /* Maybe fill in the background field while we have ximg handy. */
7101 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7102 /* Casting avoids a GCC warning on W32. */
7103 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7105 /* Put ximg into the image. */
7106 image_put_x_image (f
, img
, ximg
, 0);
7112 #else /* HAVE_TIFF */
7116 tiff_load (struct frame
*f
, struct image
*img
)
7118 return ns_load_image (f
, img
,
7119 image_spec_value (img
->spec
, QCfile
, NULL
),
7120 image_spec_value (img
->spec
, QCdata
, NULL
));
7122 #endif /* HAVE_NS */
7124 #endif /* !HAVE_TIFF */
7128 /***********************************************************************
7130 ***********************************************************************/
7132 #if defined (HAVE_GIF) || defined (HAVE_NS)
7134 static bool gif_image_p (Lisp_Object object
);
7135 static bool gif_load (struct frame
*f
, struct image
*img
);
7136 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7138 /* The symbol `gif' identifying images of this type. */
7140 static Lisp_Object Qgif
;
7142 /* Indices of image specification fields in gif_format, below. */
7144 enum gif_keyword_index
7160 /* Vector of image_keyword structures describing the format
7161 of valid user-defined image specifications. */
7163 static const struct image_keyword gif_format
[GIF_LAST
] =
7165 {":type", IMAGE_SYMBOL_VALUE
, 1},
7166 {":data", IMAGE_STRING_VALUE
, 0},
7167 {":file", IMAGE_STRING_VALUE
, 0},
7168 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7169 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7170 {":relief", IMAGE_INTEGER_VALUE
, 0},
7171 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7172 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7173 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7174 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7175 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7178 #if defined HAVE_NTGUI && defined WINDOWSNT
7179 static bool init_gif_functions (void);
7181 #define init_gif_functions NULL
7184 /* Structure describing the image type `gif'. */
7186 static struct image_type gif_type
=
7196 /* Free X resources of GIF image IMG which is used on frame F. */
7199 gif_clear_image (struct frame
*f
, struct image
*img
)
7201 img
->lisp_data
= Qnil
;
7202 x_clear_image (f
, img
);
7205 /* Return true if OBJECT is a valid GIF image specification. */
7208 gif_image_p (Lisp_Object object
)
7210 struct image_keyword fmt
[GIF_LAST
];
7211 memcpy (fmt
, gif_format
, sizeof fmt
);
7213 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7216 /* Must specify either the :data or :file keyword. */
7217 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7220 #endif /* HAVE_GIF */
7224 #if defined (HAVE_NTGUI)
7226 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7227 Undefine before redefining to avoid a preprocessor warning. */
7231 /* avoid conflict with QuickdrawText.h */
7232 #define DrawText gif_DrawText
7233 #include <gif_lib.h>
7236 /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */
7237 #ifndef GIFLIB_MINOR
7238 #define GIFLIB_MINOR 0
7240 #ifndef GIFLIB_RELEASE
7241 #define GIFLIB_RELEASE 0
7244 #else /* HAVE_NTGUI */
7246 #include <gif_lib.h>
7248 #endif /* HAVE_NTGUI */
7250 /* Giflib before 5.0 didn't define these macros. */
7251 #ifndef GIFLIB_MAJOR
7252 #define GIFLIB_MAJOR 4
7257 /* GIF library details. */
7258 #if 5 < GIFLIB_MAJOR + (1 <= GIFLIB_MINOR)
7259 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*, int *));
7261 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7263 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7264 #if GIFLIB_MAJOR < 5
7265 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7266 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7268 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
, int *));
7269 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *, int *));
7270 DEF_IMGLIB_FN (char *, GifErrorString
, (int));
7274 init_gif_functions (void)
7278 if (!(library
= w32_delayed_load (Qgif
)))
7281 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7282 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7283 LOAD_IMGLIB_FN (library
, DGifOpen
);
7284 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7285 #if GIFLIB_MAJOR >= 5
7286 LOAD_IMGLIB_FN (library
, GifErrorString
);
7293 #define fn_DGifCloseFile DGifCloseFile
7294 #define fn_DGifSlurp DGifSlurp
7295 #define fn_DGifOpen DGifOpen
7296 #define fn_DGifOpenFileName DGifOpenFileName
7297 #if 5 <= GIFLIB_MAJOR
7298 # define fn_GifErrorString GifErrorString
7301 #endif /* WINDOWSNT */
7303 /* Reading a GIF image from memory
7304 Based on the PNG memory stuff to a certain extent. */
7308 unsigned char *bytes
;
7314 /* Make the current memory source available to gif_read_from_memory.
7315 It's done this way because not all versions of libungif support
7316 a UserData field in the GifFileType structure. */
7317 static gif_memory_source
*current_gif_memory_src
;
7320 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7322 gif_memory_source
*src
= current_gif_memory_src
;
7324 if (len
> src
->len
- src
->index
)
7327 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7333 gif_close (GifFileType
*gif
, int *err
)
7337 #if 5 < GIFLIB_MAJOR + (1 <= GIFLIB_MINOR)
7338 retval
= fn_DGifCloseFile (gif
, err
);
7340 retval
= fn_DGifCloseFile (gif
);
7341 #if GIFLIB_MAJOR >= 5
7349 /* Load GIF image IMG for use on frame F. Value is true if
7352 static const int interlace_start
[] = {0, 4, 2, 1};
7353 static const int interlace_increment
[] = {8, 8, 4, 2};
7355 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7358 gif_load (struct frame
*f
, struct image
*img
)
7361 int rc
, width
, height
, x
, y
, i
, j
;
7363 ColorMapObject
*gif_color_map
;
7364 unsigned long pixel_colors
[256];
7366 gif_memory_source memsrc
;
7367 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7368 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7369 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7370 unsigned long bgcolor
= 0;
7374 if (NILP (specified_data
))
7376 file
= x_find_image_file (specified_file
);
7377 if (!STRINGP (file
))
7379 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7383 file
= ansi_encode_filename (file
);
7386 /* Open the GIF file. */
7387 #if GIFLIB_MAJOR < 5
7388 gif
= fn_DGifOpenFileName (SSDATA (file
));
7391 image_error ("Cannot open `%s'", file
, Qnil
);
7395 gif
= fn_DGifOpenFileName (SSDATA (file
), &gif_err
);
7398 image_error ("Cannot open `%s': %s",
7399 file
, build_string (fn_GifErrorString (gif_err
)));
7406 if (!STRINGP (specified_data
))
7408 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7412 /* Read from memory! */
7413 current_gif_memory_src
= &memsrc
;
7414 memsrc
.bytes
= SDATA (specified_data
);
7415 memsrc
.len
= SBYTES (specified_data
);
7418 #if GIFLIB_MAJOR < 5
7419 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7422 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7426 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
, &gif_err
);
7429 image_error ("Cannot open memory source `%s': %s",
7430 img
->spec
, build_string (fn_GifErrorString (gif_err
)));
7436 /* Before reading entire contents, check the declared image size. */
7437 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7439 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7440 gif_close (gif
, NULL
);
7444 /* Read entire contents. */
7445 rc
= fn_DGifSlurp (gif
);
7446 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7448 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7449 gif_close (gif
, NULL
);
7453 /* Which sub-image are we to display? */
7455 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7456 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7457 if (idx
< 0 || idx
>= gif
->ImageCount
)
7459 image_error ("Invalid image number `%s' in image `%s'",
7460 image_number
, img
->spec
);
7461 gif_close (gif
, NULL
);
7466 width
= img
->width
= gif
->SWidth
;
7467 height
= img
->height
= gif
->SHeight
;
7469 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7470 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7471 img
->corners
[BOT_CORNER
]
7472 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7473 img
->corners
[RIGHT_CORNER
]
7474 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7476 if (!check_image_size (f
, width
, height
))
7478 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7479 gif_close (gif
, NULL
);
7483 /* Check that the selected subimages fit. It's not clear whether
7484 the GIF spec requires this, but Emacs can crash if they don't fit. */
7485 for (j
= 0; j
<= idx
; ++j
)
7487 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7488 int subimg_width
= subimage
->ImageDesc
.Width
;
7489 int subimg_height
= subimage
->ImageDesc
.Height
;
7490 int subimg_top
= subimage
->ImageDesc
.Top
;
7491 int subimg_left
= subimage
->ImageDesc
.Left
;
7492 if (! (0 <= subimg_width
&& 0 <= subimg_height
7493 && 0 <= subimg_top
&& subimg_top
<= height
- subimg_height
7494 && 0 <= subimg_left
&& subimg_left
<= width
- subimg_width
))
7496 image_error ("Subimage does not fit in image", Qnil
, Qnil
);
7497 gif_close (gif
, NULL
);
7502 /* Create the X image and pixmap. */
7503 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
7505 gif_close (gif
, NULL
);
7509 /* Clear the part of the screen image not covered by the image.
7510 Full animated GIF support requires more here (see the gif89 spec,
7511 disposal methods). Let's simply assume that the part not covered
7512 by a sub-image is in the frame's background color. */
7513 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7514 for (x
= 0; x
< width
; ++x
)
7515 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7517 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7518 for (x
= 0; x
< width
; ++x
)
7519 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7521 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7523 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7524 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7525 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7526 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7529 /* Read the GIF image into the X image. */
7531 /* FIXME: With the current implementation, loading an animated gif
7532 is quadratic in the number of animation frames, since each frame
7533 is a separate struct image. We must provide a way for a single
7534 gif_load call to construct and save all animation frames. */
7536 init_color_table ();
7537 if (STRINGP (specified_bg
))
7538 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7539 FRAME_BACKGROUND_PIXEL (f
));
7540 for (j
= 0; j
<= idx
; ++j
)
7542 /* We use a local variable `raster' here because RasterBits is a
7543 char *, which invites problems with bytes >= 0x80. */
7544 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7545 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7546 int transparency_color_index
= -1;
7548 int subimg_width
= subimage
->ImageDesc
.Width
;
7549 int subimg_height
= subimage
->ImageDesc
.Height
;
7550 int subimg_top
= subimage
->ImageDesc
.Top
;
7551 int subimg_left
= subimage
->ImageDesc
.Left
;
7553 /* Find the Graphic Control Extension block for this sub-image.
7554 Extract the disposal method and transparency color. */
7555 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7557 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7559 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7560 && extblock
->ByteCount
== 4
7561 && extblock
->Bytes
[0] & 1)
7563 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7564 to background". Treat any other value like 2. */
7565 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7566 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7571 /* We can't "keep in place" the first subimage. */
7575 /* For disposal == 0, the spec says "No disposal specified. The
7576 decoder is not required to take any action." In practice, it
7577 seems we need to treat this like "keep in place", see e.g.
7578 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7582 /* Allocate subimage colors. */
7583 memset (pixel_colors
, 0, sizeof pixel_colors
);
7584 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7586 gif_color_map
= gif
->SColorMap
;
7589 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7591 if (transparency_color_index
== i
)
7592 pixel_colors
[i
] = STRINGP (specified_bg
)
7593 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7596 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7597 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7598 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7599 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7603 /* Apply the pixel values. */
7604 if (GIFLIB_MAJOR
< 5 && gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7608 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7610 y
++, row
+= interlace_increment
[pass
])
7612 while (subimg_height
<= row
)
7613 row
= interlace_start
[++pass
];
7615 for (x
= 0; x
< subimg_width
; x
++)
7617 int c
= raster
[y
* subimg_width
+ x
];
7618 if (transparency_color_index
!= c
|| disposal
!= 1)
7619 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7626 for (y
= 0; y
< subimg_height
; ++y
)
7627 for (x
= 0; x
< subimg_width
; ++x
)
7629 int c
= raster
[y
* subimg_width
+ x
];
7630 if (transparency_color_index
!= c
|| disposal
!= 1)
7631 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7637 #ifdef COLOR_TABLE_SUPPORT
7638 img
->colors
= colors_in_color_table (&img
->ncolors
);
7639 free_color_table ();
7640 #endif /* COLOR_TABLE_SUPPORT */
7642 /* Save GIF image extension data for `image-metadata'.
7643 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7644 img
->lisp_data
= Qnil
;
7645 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7648 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7649 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7650 /* Append (... FUNCTION "BYTES") */
7653 = Fcons (make_number (ext
->Function
),
7654 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7656 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7657 && ext
->ByteCount
== 4)
7659 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7660 delay
|= ext
->Bytes
[1];
7663 img
->lisp_data
= list2 (Qextension_data
, img
->lisp_data
);
7667 Fcons (make_float (delay
/ 100.0),
7671 if (gif
->ImageCount
> 1)
7672 img
->lisp_data
= Fcons (Qcount
,
7673 Fcons (make_number (gif
->ImageCount
),
7676 if (gif_close (gif
, &gif_err
) == GIF_ERROR
)
7678 #if 5 <= GIFLIB_MAJOR
7679 char *error_text
= fn_GifErrorString (gif_err
);
7682 image_error ("Error closing `%s': %s",
7683 img
->spec
, build_string (error_text
));
7685 image_error ("Error closing `%s'", img
->spec
, Qnil
);
7689 /* Maybe fill in the background field while we have ximg handy. */
7690 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7691 /* Casting avoids a GCC warning. */
7692 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7694 /* Put ximg into the image. */
7695 image_put_x_image (f
, img
, ximg
, 0);
7700 #else /* !HAVE_GIF */
7704 gif_load (struct frame
*f
, struct image
*img
)
7706 return ns_load_image (f
, img
,
7707 image_spec_value (img
->spec
, QCfile
, NULL
),
7708 image_spec_value (img
->spec
, QCdata
, NULL
));
7710 #endif /* HAVE_NS */
7712 #endif /* HAVE_GIF */
7715 #ifdef HAVE_IMAGEMAGICK
7717 /***********************************************************************
7719 ***********************************************************************/
7721 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
7722 safely rounded and clipped to int range. */
7725 scale_image_size (int size
, size_t divisor
, size_t multiplier
)
7730 double scaled
= s
* multiplier
/ divisor
+ 0.5;
7731 if (scaled
< INT_MAX
)
7737 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
7738 Use SPEC to deduce the size. Store the desired size into
7739 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
7741 compute_image_size (size_t width
, size_t height
,
7743 int *d_width
, int *d_height
)
7746 int desired_width
, desired_height
;
7748 /* If width and/or height is set in the display spec assume we want
7749 to scale to those values. If either h or w is unspecified, the
7750 unspecified should be calculated from the specified to preserve
7752 value
= image_spec_value (spec
, QCwidth
, NULL
);
7753 desired_width
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7754 value
= image_spec_value (spec
, QCheight
, NULL
);
7755 desired_height
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7757 if (desired_width
== -1)
7759 value
= image_spec_value (spec
, QCmax_width
, NULL
);
7760 if (NATNUMP (value
))
7762 int max_width
= min (XFASTINT (value
), INT_MAX
);
7763 if (max_width
< width
)
7765 /* The image is wider than :max-width. */
7766 desired_width
= max_width
;
7767 if (desired_height
== -1)
7769 desired_height
= scale_image_size (desired_width
,
7771 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7772 if (NATNUMP (value
))
7774 int max_height
= min (XFASTINT (value
), INT_MAX
);
7775 if (max_height
< desired_height
)
7777 desired_height
= max_height
;
7778 desired_width
= scale_image_size (desired_height
,
7787 if (desired_height
== -1)
7789 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7790 if (NATNUMP (value
))
7792 int max_height
= min (XFASTINT (value
), INT_MAX
);
7793 if (max_height
< height
)
7794 desired_height
= max_height
;
7798 if (desired_width
!= -1 && desired_height
== -1)
7799 /* w known, calculate h. */
7800 desired_height
= scale_image_size (desired_width
, width
, height
);
7802 if (desired_width
== -1 && desired_height
!= -1)
7803 /* h known, calculate w. */
7804 desired_width
= scale_image_size (desired_height
, height
, width
);
7806 *d_width
= desired_width
;
7807 *d_height
= desired_height
;
7810 static Lisp_Object Qimagemagick
;
7812 static bool imagemagick_image_p (Lisp_Object
);
7813 static bool imagemagick_load (struct frame
*, struct image
*);
7814 static void imagemagick_clear_image (struct frame
*, struct image
*);
7816 /* Indices of image specification fields in imagemagick_format. */
7818 enum imagemagick_keyword_index
7826 IMAGEMAGICK_ALGORITHM
,
7827 IMAGEMAGICK_HEURISTIC_MASK
,
7829 IMAGEMAGICK_BACKGROUND
,
7832 IMAGEMAGICK_MAX_HEIGHT
,
7833 IMAGEMAGICK_MAX_WIDTH
,
7835 IMAGEMAGICK_ROTATION
,
7840 /* Vector of image_keyword structures describing the format
7841 of valid user-defined image specifications. */
7843 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7845 {":type", IMAGE_SYMBOL_VALUE
, 1},
7846 {":data", IMAGE_STRING_VALUE
, 0},
7847 {":file", IMAGE_STRING_VALUE
, 0},
7848 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7849 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7850 {":relief", IMAGE_INTEGER_VALUE
, 0},
7851 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7852 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7853 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7854 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7855 {":height", IMAGE_INTEGER_VALUE
, 0},
7856 {":width", IMAGE_INTEGER_VALUE
, 0},
7857 {":max-height", IMAGE_INTEGER_VALUE
, 0},
7858 {":max-width", IMAGE_INTEGER_VALUE
, 0},
7859 {":format", IMAGE_SYMBOL_VALUE
, 0},
7860 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7861 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7864 #if defined HAVE_NTGUI && defined WINDOWSNT
7865 static bool init_imagemagick_functions (void);
7867 #define init_imagemagick_functions NULL
7870 /* Structure describing the image type for any image handled via
7873 static struct image_type imagemagick_type
=
7876 imagemagick_image_p
,
7878 imagemagick_clear_image
,
7879 init_imagemagick_functions
,
7883 /* Free X resources of imagemagick image IMG which is used on frame F. */
7886 imagemagick_clear_image (struct frame
*f
,
7889 x_clear_image (f
, img
);
7892 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7893 this by calling parse_image_spec and supplying the keywords that
7894 identify the IMAGEMAGICK format. */
7897 imagemagick_image_p (Lisp_Object object
)
7899 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7900 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7902 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7905 /* Must specify either the :data or :file keyword. */
7906 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7909 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7910 Therefore rename the function so it doesn't collide with ImageMagick. */
7911 #define DrawRectangle DrawRectangleGif
7912 #include <wand/MagickWand.h>
7914 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7915 Emacs seems to work fine with the hidden version, so unhide it. */
7916 #include <magick/version.h>
7917 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7918 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7919 MagickPixelPacket
*);
7922 /* Log ImageMagick error message.
7923 Useful when a ImageMagick function returns the status `MagickFalse'. */
7926 imagemagick_error (MagickWand
*wand
)
7929 ExceptionType severity
;
7931 description
= MagickGetException (wand
, &severity
);
7932 image_error ("ImageMagick error: %s",
7933 build_string (description
),
7935 MagickRelinquishMemory (description
);
7938 /* Possibly give ImageMagick some extra help to determine the image
7939 type by supplying a "dummy" filename based on the Content-Type. */
7942 imagemagick_filename_hint (Lisp_Object spec
, char hint_buffer
[MaxTextExtent
])
7944 Lisp_Object symbol
= intern ("image-format-suffixes");
7945 Lisp_Object val
= find_symbol_value (symbol
);
7951 format
= image_spec_value (spec
, intern (":format"), NULL
);
7952 val
= Fcar_safe (Fcdr_safe (Fassq (format
, val
)));
7953 if (! STRINGP (val
))
7956 /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
7957 as ImageMagick would ignore the extra bytes anyway. */
7958 snprintf (hint_buffer
, MaxTextExtent
, "/tmp/foo.%s", SSDATA (val
));
7962 /* Animated images (e.g., GIF89a) are composed from one "master image"
7963 (which is the first one, and then there's a number of images that
7964 follow. If following images have non-transparent colors, these are
7965 composed "on top" of the master image. So, in general, one has to
7966 compute ann the preceding images to be able to display a particular
7969 Computing all the preceding images is too slow, so we maintain a
7970 cache of previously computed images. We have to maintain a cache
7971 separate from the image cache, because the images may be scaled
7974 struct animation_cache
7978 struct timespec update_time
;
7979 struct animation_cache
*next
;
7980 char signature
[FLEXIBLE_ARRAY_MEMBER
];
7983 static struct animation_cache
*animation_cache
= NULL
;
7985 static struct animation_cache
*
7986 imagemagick_create_cache (char *signature
)
7988 struct animation_cache
*cache
7989 = xmalloc (offsetof (struct animation_cache
, signature
)
7990 + strlen (signature
) + 1);
7994 strcpy (cache
->signature
, signature
);
7998 /* Discard cached images that haven't been used for a minute. */
8000 imagemagick_prune_animation_cache (void)
8002 struct animation_cache
**pcache
= &animation_cache
;
8003 struct timespec old
= timespec_sub (current_timespec (),
8004 make_timespec (60, 0));
8008 struct animation_cache
*cache
= *pcache
;
8009 if (timespec_cmp (old
, cache
->update_time
) <= 0)
8010 pcache
= &cache
->next
;
8014 DestroyMagickWand (cache
->wand
);
8015 *pcache
= cache
->next
;
8021 static struct animation_cache
*
8022 imagemagick_get_animation_cache (MagickWand
*wand
)
8024 char *signature
= MagickGetImageSignature (wand
);
8025 struct animation_cache
*cache
;
8026 struct animation_cache
**pcache
= &animation_cache
;
8028 imagemagick_prune_animation_cache ();
8035 *pcache
= cache
= imagemagick_create_cache (signature
);
8038 if (strcmp (signature
, cache
->signature
) == 0)
8040 pcache
= &cache
->next
;
8043 DestroyString (signature
);
8044 cache
->update_time
= current_timespec ();
8049 imagemagick_compute_animated_image (MagickWand
*super_wand
, int ino
)
8052 MagickWand
*composite_wand
;
8053 size_t dest_width
, dest_height
;
8054 struct animation_cache
*cache
= imagemagick_get_animation_cache (super_wand
);
8056 MagickSetIteratorIndex (super_wand
, 0);
8058 if (ino
== 0 || cache
->wand
== NULL
|| cache
->index
> ino
)
8060 composite_wand
= MagickGetImage (super_wand
);
8062 DestroyMagickWand (cache
->wand
);
8065 composite_wand
= cache
->wand
;
8067 dest_height
= MagickGetImageHeight (composite_wand
);
8069 for (i
= max (1, cache
->index
+ 1); i
<= ino
; i
++)
8071 MagickWand
*sub_wand
;
8072 PixelIterator
*source_iterator
, *dest_iterator
;
8073 PixelWand
**source
, **dest
;
8074 size_t source_width
, source_height
;
8075 ssize_t source_left
, source_top
;
8076 MagickPixelPacket pixel
;
8077 DisposeType dispose
;
8078 ptrdiff_t lines
= 0;
8080 MagickSetIteratorIndex (super_wand
, i
);
8081 sub_wand
= MagickGetImage (super_wand
);
8083 MagickGetImagePage (sub_wand
, &source_width
, &source_height
,
8084 &source_left
, &source_top
);
8086 /* This flag says how to handle transparent pixels. */
8087 dispose
= MagickGetImageDispose (sub_wand
);
8089 source_iterator
= NewPixelIterator (sub_wand
);
8090 if (! source_iterator
)
8092 DestroyMagickWand (composite_wand
);
8093 DestroyMagickWand (sub_wand
);
8095 image_error ("Imagemagick pixel iterator creation failed",
8100 dest_iterator
= NewPixelIterator (composite_wand
);
8101 if (! dest_iterator
)
8103 DestroyMagickWand (composite_wand
);
8104 DestroyMagickWand (sub_wand
);
8105 DestroyPixelIterator (source_iterator
);
8107 image_error ("Imagemagick pixel iterator creation failed",
8112 /* The sub-image may not start at origin, so move the destination
8113 iterator to where the sub-image should start. */
8116 PixelSetIteratorRow (dest_iterator
, source_top
);
8120 while ((source
= PixelGetNextIteratorRow (source_iterator
, &source_width
))
8125 /* Sanity check. This shouldn't happen, but apparently
8126 does in some pictures. */
8127 if (++lines
>= dest_height
)
8130 dest
= PixelGetNextIteratorRow (dest_iterator
, &dest_width
);
8131 for (x
= 0; x
< source_width
; x
++)
8133 /* Sanity check. This shouldn't happen, but apparently
8134 also does in some pictures. */
8135 if (x
+ source_left
>= dest_width
)
8137 /* Normally we only copy over non-transparent pixels,
8138 but if the disposal method is "Background", then we
8139 copy over all pixels. */
8140 if (dispose
== BackgroundDispose
|| PixelGetAlpha (source
[x
]))
8142 PixelGetMagickColor (source
[x
], &pixel
);
8143 PixelSetMagickColor (dest
[x
+ source_left
], &pixel
);
8146 PixelSyncIterator (dest_iterator
);
8149 DestroyPixelIterator (source_iterator
);
8150 DestroyPixelIterator (dest_iterator
);
8151 DestroyMagickWand (sub_wand
);
8154 /* Cache a copy for the next iteration. The current wand will be
8155 destroyed by the caller. */
8156 cache
->wand
= CloneMagickWand (composite_wand
);
8159 return composite_wand
;
8163 /* Helper function for imagemagick_load, which does the actual loading
8164 given contents and size, apart from frame and image structures,
8165 passed from imagemagick_load. Uses librimagemagick to do most of
8166 the image processing.
8168 F is a pointer to the Emacs frame; IMG to the image structure to
8169 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
8170 be parsed; SIZE is the number of bytes of data; and FILENAME is
8171 either the file name or the image data.
8173 Return true if successful. */
8176 imagemagick_load_image (struct frame
*f
, struct image
*img
,
8177 unsigned char *contents
, unsigned int size
,
8181 size_t image_width
, image_height
;
8182 MagickBooleanType status
;
8185 MagickWand
*image_wand
;
8186 PixelIterator
*iterator
;
8187 PixelWand
**pixels
, *bg_wand
= NULL
;
8188 MagickPixelPacket pixel
;
8193 int desired_width
, desired_height
;
8196 char hint_buffer
[MaxTextExtent
];
8197 char *filename_hint
= NULL
;
8199 /* Handle image index for image types who can contain more than one image.
8200 Interface :index is same as for GIF. First we "ping" the image to see how
8201 many sub-images it contains. Pinging is faster than loading the image to
8202 find out things about it. */
8204 /* Initialize the imagemagick environment. */
8205 MagickWandGenesis ();
8206 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
8207 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
8208 image_wand
= NewMagickWand ();
8211 status
= MagickReadImage (image_wand
, filename
);
8214 filename_hint
= imagemagick_filename_hint (img
->spec
, hint_buffer
);
8215 MagickSetFilename (image_wand
, filename_hint
);
8216 status
= MagickReadImageBlob (image_wand
, contents
, size
);
8219 if (status
== MagickFalse
)
8221 imagemagick_error (image_wand
);
8222 DestroyMagickWand (image_wand
);
8226 if (ino
< 0 || ino
>= MagickGetNumberImages (image_wand
))
8228 image_error ("Invalid image number `%s' in image `%s'",
8230 DestroyMagickWand (image_wand
);
8234 if (MagickGetNumberImages (image_wand
) > 1)
8237 Fcons (make_number (MagickGetNumberImages (image_wand
)),
8240 /* If we have an animated image, get the new wand based on the
8242 if (MagickGetNumberImages (image_wand
) > 1)
8244 MagickWand
*super_wand
= image_wand
;
8245 image_wand
= imagemagick_compute_animated_image (super_wand
, ino
);
8247 image_wand
= super_wand
;
8249 DestroyMagickWand (super_wand
);
8252 /* Retrieve the frame's background color, for use later. */
8255 Lisp_Object specified_bg
;
8257 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8258 if (!STRINGP (specified_bg
)
8259 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
8260 x_query_frame_background_color (f
, &bgcolor
);
8262 bg_wand
= NewPixelWand ();
8263 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
8264 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
8265 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
8268 compute_image_size (MagickGetImageWidth (image_wand
),
8269 MagickGetImageHeight (image_wand
),
8270 img
->spec
, &desired_width
, &desired_height
);
8272 if (desired_width
!= -1 && desired_height
!= -1)
8274 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
8275 if (status
== MagickFalse
)
8277 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
8278 imagemagick_error (image_wand
);
8279 goto imagemagick_error
;
8283 /* crop behaves similar to image slicing in Emacs but is more memory
8285 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
8287 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
8289 /* After some testing, it seems MagickCropImage is the fastest crop
8290 function in ImageMagick. This crop function seems to do less copying
8291 than the alternatives, but it still reads the entire image into memory
8292 before cropping, which is apparently difficult to avoid when using
8294 size_t crop_width
= XINT (XCAR (crop
));
8296 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
8298 size_t crop_height
= XINT (XCAR (crop
));
8300 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
8302 ssize_t crop_x
= XINT (XCAR (crop
));
8304 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
8306 ssize_t crop_y
= XINT (XCAR (crop
));
8307 MagickCropImage (image_wand
, crop_width
, crop_height
,
8314 /* Furthermore :rotation. we need background color and angle for
8317 TODO background handling for rotation specified_bg =
8318 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8320 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
8323 rotation
= extract_float (value
);
8324 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
8325 if (status
== MagickFalse
)
8327 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
8328 imagemagick_error (image_wand
);
8329 goto imagemagick_error
;
8333 /* Set the canvas background color to the frame or specified
8334 background, and flatten the image. Note: as of ImageMagick
8335 6.6.0, SVG image transparency is not handled properly
8336 (e.g. etc/images/splash.svg shows a white background always). */
8338 MagickWand
*new_wand
;
8339 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
8340 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8341 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
8343 new_wand
= MagickFlattenImages (image_wand
);
8345 DestroyMagickWand (image_wand
);
8346 image_wand
= new_wand
;
8349 /* Finally we are done manipulating the image. Figure out the
8350 resulting width/height and transfer ownership to Emacs. */
8351 image_height
= MagickGetImageHeight (image_wand
);
8352 image_width
= MagickGetImageWidth (image_wand
);
8354 if (! (image_width
<= INT_MAX
&& image_height
<= INT_MAX
8355 && check_image_size (f
, image_width
, image_height
)))
8357 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8358 goto imagemagick_error
;
8361 width
= image_width
;
8362 height
= image_height
;
8364 /* We can now get a valid pixel buffer from the imagemagick file, if all
8367 init_color_table ();
8369 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8370 if (imagemagick_render_type
!= 0)
8372 /* Magicexportimage is normally faster than pixelpushing. This
8373 method is also well tested. Some aspects of this method are
8374 ad-hoc and needs to be more researched. */
8375 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
8376 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
8377 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8378 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, imagedepth
,
8381 #ifdef COLOR_TABLE_SUPPORT
8382 free_color_table ();
8384 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8385 goto imagemagick_error
;
8388 /* Oddly, the below code doesn't seem to work:*/
8389 /* switch(ximg->bitmap_unit){ */
8391 /* pixelwidth=CharPixel; */
8394 /* pixelwidth=ShortPixel; */
8397 /* pixelwidth=LongPixel; */
8401 Here im just guessing the format of the bitmap.
8402 happens to work fine for:
8405 seems about 3 times as fast as pixel pushing(not carefully measured)
8407 pixelwidth
= CharPixel
; /*??? TODO figure out*/
8408 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
8409 exportdepth
, pixelwidth
, ximg
->data
);
8412 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8414 size_t image_height
;
8415 MagickRealType color_scale
= 65535.0 / QuantumRange
;
8417 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8418 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
8421 #ifdef COLOR_TABLE_SUPPORT
8422 free_color_table ();
8424 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8425 goto imagemagick_error
;
8428 /* Copy imagemagick image to x with primitive yet robust pixel
8429 pusher loop. This has been tested a lot with many different
8432 /* Copy pixels from the imagemagick image structure to the x image map. */
8433 iterator
= NewPixelIterator (image_wand
);
8436 #ifdef COLOR_TABLE_SUPPORT
8437 free_color_table ();
8439 x_destroy_x_image (ximg
);
8440 image_error ("Imagemagick pixel iterator creation failed",
8442 goto imagemagick_error
;
8445 image_height
= MagickGetImageHeight (image_wand
);
8446 for (y
= 0; y
< image_height
; y
++)
8449 pixels
= PixelGetNextIteratorRow (iterator
, &row_width
);
8452 int xlim
= min (row_width
, width
);
8453 for (x
= 0; x
< xlim
; x
++)
8455 PixelGetMagickColor (pixels
[x
], &pixel
);
8456 XPutPixel (ximg
, x
, y
,
8457 lookup_rgb_color (f
,
8458 color_scale
* pixel
.red
,
8459 color_scale
* pixel
.green
,
8460 color_scale
* pixel
.blue
));
8463 DestroyPixelIterator (iterator
);
8466 #ifdef COLOR_TABLE_SUPPORT
8467 /* Remember colors allocated for this image. */
8468 img
->colors
= colors_in_color_table (&img
->ncolors
);
8469 free_color_table ();
8470 #endif /* COLOR_TABLE_SUPPORT */
8473 img
->height
= height
;
8475 /* Put ximg into the image. */
8476 image_put_x_image (f
, img
, ximg
, 0);
8478 /* Final cleanup. image_wand should be the only resource left. */
8479 DestroyMagickWand (image_wand
);
8480 if (bg_wand
) DestroyPixelWand (bg_wand
);
8482 /* `MagickWandTerminus' terminates the imagemagick environment. */
8483 MagickWandTerminus ();
8488 DestroyMagickWand (image_wand
);
8489 if (bg_wand
) DestroyPixelWand (bg_wand
);
8491 MagickWandTerminus ();
8492 /* TODO more cleanup. */
8493 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
8498 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8499 successful. this function will go into the imagemagick_type structure, and
8500 the prototype thus needs to be compatible with that structure. */
8503 imagemagick_load (struct frame
*f
, struct image
*img
)
8506 Lisp_Object file_name
;
8508 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8509 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8510 if (STRINGP (file_name
))
8514 file
= x_find_image_file (file_name
);
8515 if (!STRINGP (file
))
8517 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8521 file
= ansi_encode_filename (file
);
8523 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8525 /* Else its not a file, its a lisp object. Load the image from a
8526 lisp object rather than a file. */
8531 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8532 if (!STRINGP (data
))
8534 image_error ("Invalid image data `%s'", data
, Qnil
);
8537 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8538 SBYTES (data
), NULL
);
8544 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8545 doc
: /* Return a list of image types supported by ImageMagick.
8546 Each entry in this list is a symbol named after an ImageMagick format
8547 tag. See the ImageMagick manual for a list of ImageMagick formats and
8548 their descriptions (http://www.imagemagick.org/script/formats.php).
8549 You can also try the shell command: `identify -list format'.
8551 Note that ImageMagick recognizes many file-types that Emacs does not
8552 recognize as images, such as C. See `imagemagick-types-enable'
8553 and `imagemagick-types-inhibit'. */)
8556 Lisp_Object typelist
= Qnil
;
8562 GetExceptionInfo(&ex
);
8563 imtypes
= GetMagickList ("*", &numf
, &ex
);
8564 DestroyExceptionInfo(&ex
);
8566 for (i
= 0; i
< numf
; i
++)
8568 Lisp_Object imagemagicktype
= intern (imtypes
[i
]);
8569 typelist
= Fcons (imagemagicktype
, typelist
);
8570 imtypes
[i
] = MagickRelinquishMemory (imtypes
[i
]);
8573 MagickRelinquishMemory (imtypes
);
8574 return Fnreverse (typelist
);
8577 #endif /* defined (HAVE_IMAGEMAGICK) */
8581 /***********************************************************************
8583 ***********************************************************************/
8585 #if defined (HAVE_RSVG)
8587 /* Function prototypes. */
8589 static bool svg_image_p (Lisp_Object object
);
8590 static bool svg_load (struct frame
*f
, struct image
*img
);
8592 static bool svg_load_image (struct frame
*, struct image
*,
8593 unsigned char *, ptrdiff_t);
8595 /* The symbol `svg' identifying images of this type. */
8597 static Lisp_Object Qsvg
;
8599 /* Indices of image specification fields in svg_format, below. */
8601 enum svg_keyword_index
8616 /* Vector of image_keyword structures describing the format
8617 of valid user-defined image specifications. */
8619 static const struct image_keyword svg_format
[SVG_LAST
] =
8621 {":type", IMAGE_SYMBOL_VALUE
, 1},
8622 {":data", IMAGE_STRING_VALUE
, 0},
8623 {":file", IMAGE_STRING_VALUE
, 0},
8624 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8625 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8626 {":relief", IMAGE_INTEGER_VALUE
, 0},
8627 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8628 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8629 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8630 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8633 #if defined HAVE_NTGUI && defined WINDOWSNT
8634 static bool init_svg_functions (void);
8636 #define init_svg_functions NULL
8639 /* Structure describing the image type `svg'. Its the same type of
8640 structure defined for all image formats, handled by emacs image
8641 functions. See struct image_type in dispextern.h. */
8643 static struct image_type svg_type
=
8654 /* Return true if OBJECT is a valid SVG image specification. Do
8655 this by calling parse_image_spec and supplying the keywords that
8656 identify the SVG format. */
8659 svg_image_p (Lisp_Object object
)
8661 struct image_keyword fmt
[SVG_LAST
];
8662 memcpy (fmt
, svg_format
, sizeof fmt
);
8664 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8667 /* Must specify either the :data or :file keyword. */
8668 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8671 #include <librsvg/rsvg.h>
8675 /* SVG library functions. */
8676 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
, (void));
8677 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
, (RsvgHandle
*, RsvgDimensionData
*));
8678 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
, (RsvgHandle
*, const guchar
*, gsize
, GError
**));
8679 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
, (RsvgHandle
*, GError
**));
8680 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
, (RsvgHandle
*));
8682 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
, (const GdkPixbuf
*));
8683 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
, (const GdkPixbuf
*));
8684 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
, (const GdkPixbuf
*));
8685 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
, (const GdkPixbuf
*));
8686 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
, (const GdkPixbuf
*));
8687 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
, (const GdkPixbuf
*));
8688 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
, (const GdkPixbuf
*));
8689 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
, (const GdkPixbuf
*));
8691 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8692 DEF_IMGLIB_FN (void, g_type_init
, (void));
8694 DEF_IMGLIB_FN (void, g_object_unref
, (gpointer
));
8695 DEF_IMGLIB_FN (void, g_error_free
, (GError
*));
8697 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8700 init_svg_functions (void)
8702 HMODULE library
, gdklib
= NULL
, glib
= NULL
, gobject
= NULL
;
8704 if (!(glib
= w32_delayed_load (Qglib
))
8705 || !(gobject
= w32_delayed_load (Qgobject
))
8706 || !(gdklib
= w32_delayed_load (Qgdk_pixbuf
))
8707 || !(library
= w32_delayed_load (Qsvg
)))
8709 if (gdklib
) FreeLibrary (gdklib
);
8710 if (gobject
) FreeLibrary (gobject
);
8711 if (glib
) FreeLibrary (glib
);
8715 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8716 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8717 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8718 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8719 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8721 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8722 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8723 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8724 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8725 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8726 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8727 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8728 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8730 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8731 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8733 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8734 LOAD_IMGLIB_FN (glib
, g_error_free
);
8740 /* The following aliases for library functions allow dynamic loading
8741 to be used on some platforms. */
8742 #define fn_rsvg_handle_new rsvg_handle_new
8743 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8744 #define fn_rsvg_handle_write rsvg_handle_write
8745 #define fn_rsvg_handle_close rsvg_handle_close
8746 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8748 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8749 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8750 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8751 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8752 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8753 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8754 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8755 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8757 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8758 #define fn_g_type_init g_type_init
8760 #define fn_g_object_unref g_object_unref
8761 #define fn_g_error_free g_error_free
8762 #endif /* !WINDOWSNT */
8764 /* Load SVG image IMG for use on frame F. Value is true if
8768 svg_load (struct frame
*f
, struct image
*img
)
8771 Lisp_Object file_name
;
8773 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8774 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8775 if (STRINGP (file_name
))
8778 unsigned char *contents
;
8781 file
= x_find_image_file (file_name
);
8782 if (!STRINGP (file
))
8784 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8788 /* Read the entire file into memory. */
8789 contents
= slurp_file (SSDATA (file
), &size
);
8790 if (contents
== NULL
)
8792 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8795 /* If the file was slurped into memory properly, parse it. */
8796 success_p
= svg_load_image (f
, img
, contents
, size
);
8799 /* Else its not a file, its a lisp object. Load the image from a
8800 lisp object rather than a file. */
8805 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8806 if (!STRINGP (data
))
8808 image_error ("Invalid image data `%s'", data
, Qnil
);
8811 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8817 /* svg_load_image is a helper function for svg_load, which does the
8818 actual loading given contents and size, apart from frame and image
8819 structures, passed from svg_load.
8821 Uses librsvg to do most of the image processing.
8823 Returns true when successful. */
8825 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8826 struct image
*img
, /* Pointer to emacs image structure. */
8827 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8828 ptrdiff_t size
) /* Size of data in bytes. */
8830 RsvgHandle
*rsvg_handle
;
8831 RsvgDimensionData dimension_data
;
8836 const guint8
*pixels
;
8839 Lisp_Object specified_bg
;
8844 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8845 /* g_type_init is a glib function that must be called prior to
8846 using gnome type library functions (obsolete since 2.36.0). */
8850 /* Make a handle to a new rsvg object. */
8851 rsvg_handle
= fn_rsvg_handle_new ();
8853 /* Parse the contents argument and fill in the rsvg_handle. */
8854 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8855 if (err
) goto rsvg_error
;
8857 /* The parsing is complete, rsvg_handle is ready to used, close it
8858 for further writes. */
8859 fn_rsvg_handle_close (rsvg_handle
, &err
);
8860 if (err
) goto rsvg_error
;
8862 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8863 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8865 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8869 /* We can now get a valid pixel buffer from the svg file, if all
8871 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8872 if (!pixbuf
) goto rsvg_error
;
8873 fn_g_object_unref (rsvg_handle
);
8875 /* Extract some meta data from the svg handle. */
8876 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8877 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8878 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8879 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8881 /* Validate the svg meta data. */
8882 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8883 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8884 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8885 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8887 /* Try to create a x pixmap to hold the svg pixmap. */
8888 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
8890 fn_g_object_unref (pixbuf
);
8894 init_color_table ();
8896 /* Handle alpha channel by combining the image with a background
8898 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8899 if (!STRINGP (specified_bg
)
8900 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8901 x_query_frame_background_color (f
, &background
);
8903 /* SVG pixmaps specify transparency in the last byte, so right
8904 shift 8 bits to get rid of it, since emacs doesn't support
8906 background
.red
>>= 8;
8907 background
.green
>>= 8;
8908 background
.blue
>>= 8;
8910 /* This loop handles opacity values, since Emacs assumes
8911 non-transparent images. Each pixel must be "flattened" by
8912 calculating the resulting color, given the transparency of the
8913 pixel, and the image background color. */
8914 for (y
= 0; y
< height
; ++y
)
8916 for (x
= 0; x
< width
; ++x
)
8926 opacity
= *pixels
++;
8928 red
= ((red
* opacity
)
8929 + (background
.red
* ((1 << 8) - opacity
)));
8930 green
= ((green
* opacity
)
8931 + (background
.green
* ((1 << 8) - opacity
)));
8932 blue
= ((blue
* opacity
)
8933 + (background
.blue
* ((1 << 8) - opacity
)));
8935 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8938 pixels
+= rowstride
- 4 * width
;
8941 #ifdef COLOR_TABLE_SUPPORT
8942 /* Remember colors allocated for this image. */
8943 img
->colors
= colors_in_color_table (&img
->ncolors
);
8944 free_color_table ();
8945 #endif /* COLOR_TABLE_SUPPORT */
8947 fn_g_object_unref (pixbuf
);
8950 img
->height
= height
;
8952 /* Maybe fill in the background field while we have ximg handy.
8953 Casting avoids a GCC warning. */
8954 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8956 /* Put ximg into the image. */
8957 image_put_x_image (f
, img
, ximg
, 0);
8962 fn_g_object_unref (rsvg_handle
);
8963 /* FIXME: Use error->message so the user knows what is the actual
8964 problem with the image. */
8965 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8966 fn_g_error_free (err
);
8970 #endif /* defined (HAVE_RSVG) */
8975 /***********************************************************************
8977 ***********************************************************************/
8979 #ifdef HAVE_X_WINDOWS
8980 #define HAVE_GHOSTSCRIPT 1
8981 #endif /* HAVE_X_WINDOWS */
8983 #ifdef HAVE_GHOSTSCRIPT
8985 static bool gs_image_p (Lisp_Object object
);
8986 static bool gs_load (struct frame
*f
, struct image
*img
);
8987 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8989 /* Keyword symbols. */
8991 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8993 /* Indices of image specification fields in gs_format, below. */
8995 enum gs_keyword_index
9013 /* Vector of image_keyword structures describing the format
9014 of valid user-defined image specifications. */
9016 static const struct image_keyword gs_format
[GS_LAST
] =
9018 {":type", IMAGE_SYMBOL_VALUE
, 1},
9019 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9020 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9021 {":file", IMAGE_STRING_VALUE
, 1},
9022 {":loader", IMAGE_FUNCTION_VALUE
, 0},
9023 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
9024 {":ascent", IMAGE_ASCENT_VALUE
, 0},
9025 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
9026 {":relief", IMAGE_INTEGER_VALUE
, 0},
9027 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9028 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9029 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9030 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
9033 /* Structure describing the image type `ghostscript'. */
9035 static struct image_type gs_type
=
9046 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9049 gs_clear_image (struct frame
*f
, struct image
*img
)
9051 x_clear_image (f
, img
);
9055 /* Return true if OBJECT is a valid Ghostscript image
9059 gs_image_p (Lisp_Object object
)
9061 struct image_keyword fmt
[GS_LAST
];
9065 memcpy (fmt
, gs_format
, sizeof fmt
);
9067 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
9070 /* Bounding box must be a list or vector containing 4 integers. */
9071 tem
= fmt
[GS_BOUNDING_BOX
].value
;
9074 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
9075 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
9080 else if (VECTORP (tem
))
9082 if (ASIZE (tem
) != 4)
9084 for (i
= 0; i
< 4; ++i
)
9085 if (!INTEGERP (AREF (tem
, i
)))
9095 /* Load Ghostscript image IMG for use on frame F. Value is true
9099 gs_load (struct frame
*f
, struct image
*img
)
9101 uprintmax_t printnum1
, printnum2
;
9102 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
9103 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
9105 double in_width
, in_height
;
9106 Lisp_Object pixel_colors
= Qnil
;
9108 /* Compute pixel size of pixmap needed from the given size in the
9109 image specification. Sizes in the specification are in pt. 1 pt
9110 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9112 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
9113 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
9114 in_width
*= FRAME_RES_X (f
);
9115 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
9116 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
9117 in_height
*= FRAME_RES_Y (f
);
9119 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
9120 && check_image_size (f
, in_width
, in_height
)))
9122 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
9125 img
->width
= in_width
;
9126 img
->height
= in_height
;
9128 /* Create the pixmap. */
9129 eassert (img
->pixmap
== NO_PIXMAP
);
9131 if (x_check_image_size (0, img
->width
, img
->height
))
9133 /* Only W32 version did BLOCK_INPUT here. ++kfs */
9135 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9136 img
->width
, img
->height
,
9137 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
9143 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
9147 /* Call the loader to fill the pixmap. It returns a process object
9148 if successful. We do not record_unwind_protect here because
9149 other places in redisplay like calling window scroll functions
9150 don't either. Let the Lisp loader use `unwind-protect' instead. */
9151 printnum1
= FRAME_X_WINDOW (f
);
9152 printnum2
= img
->pixmap
;
9153 window_and_pixmap_id
9154 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
9156 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
9157 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
9159 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
9161 XSETFRAME (frame
, f
);
9162 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
9164 loader
= intern ("gs-load-image");
9166 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
9167 make_number (img
->width
),
9168 make_number (img
->height
),
9169 window_and_pixmap_id
,
9171 return PROCESSP (img
->lisp_data
);
9175 /* Kill the Ghostscript process that was started to fill PIXMAP on
9176 frame F. Called from XTread_socket when receiving an event
9177 telling Emacs that Ghostscript has finished drawing. */
9180 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
9182 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
9187 /* Find the image containing PIXMAP. */
9188 for (i
= 0; i
< c
->used
; ++i
)
9189 if (c
->images
[i
]->pixmap
== pixmap
)
9192 /* Should someone in between have cleared the image cache, for
9193 instance, give up. */
9197 /* Kill the GS process. We should have found PIXMAP in the image
9198 cache and its image should contain a process object. */
9200 eassert (PROCESSP (img
->lisp_data
));
9201 Fkill_process (img
->lisp_data
, Qnil
);
9202 img
->lisp_data
= Qnil
;
9204 #if defined (HAVE_X_WINDOWS)
9206 /* On displays with a mutable colormap, figure out the colors
9207 allocated for the image by looking at the pixels of an XImage for
9209 class = FRAME_X_VISUAL (f
)->class;
9210 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
9216 /* Try to get an XImage for img->pixmep. */
9217 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
9218 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
9223 /* Initialize the color table. */
9224 init_color_table ();
9226 /* For each pixel of the image, look its color up in the
9227 color table. After having done so, the color table will
9228 contain an entry for each color used by the image. */
9229 for (y
= 0; y
< img
->height
; ++y
)
9230 for (x
= 0; x
< img
->width
; ++x
)
9232 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
9233 lookup_pixel_color (f
, pixel
);
9236 /* Record colors in the image. Free color table and XImage. */
9237 #ifdef COLOR_TABLE_SUPPORT
9238 img
->colors
= colors_in_color_table (&img
->ncolors
);
9239 free_color_table ();
9241 XDestroyImage (ximg
);
9243 #if 0 /* This doesn't seem to be the case. If we free the colors
9244 here, we get a BadAccess later in x_clear_image when
9245 freeing the colors. */
9246 /* We have allocated colors once, but Ghostscript has also
9247 allocated colors on behalf of us. So, to get the
9248 reference counts right, free them once. */
9250 x_free_colors (f
, img
->colors
, img
->ncolors
);
9254 image_error ("Cannot get X image of `%s'; colors will not be freed",
9259 #endif /* HAVE_X_WINDOWS */
9261 /* Now that we have the pixmap, compute mask and transform the
9262 image if requested. */
9264 postprocess_image (f
, img
);
9268 #endif /* HAVE_GHOSTSCRIPT */
9271 /***********************************************************************
9273 ***********************************************************************/
9277 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
9278 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
9281 return valid_image_p (spec
) ? Qt
: Qnil
;
9285 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
9290 if (valid_image_p (spec
))
9291 id
= lookup_image (SELECTED_FRAME (), spec
);
9294 return make_number (id
);
9297 #endif /* GLYPH_DEBUG */
9300 /***********************************************************************
9302 ***********************************************************************/
9304 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 1, 1, 0,
9305 doc
: /* Initialize image library implementing image type TYPE.
9306 Return non-nil if TYPE is a supported image type.
9308 If image libraries are loaded dynamically (currently only the case on
9309 MS-Windows), load the library for TYPE if it is not yet loaded, using
9310 the library file(s) specified by `dynamic-library-alist'. */)
9313 return lookup_image_type (type
) ? Qt
: Qnil
;
9316 /* Look up image type TYPE, and return a pointer to its image_type
9317 structure. Return 0 if TYPE is not a known image type. */
9319 static struct image_type
*
9320 lookup_image_type (Lisp_Object type
)
9322 /* Types pbm and xbm are built-in and always available. */
9323 if (EQ (type
, Qpbm
))
9324 return define_image_type (&pbm_type
);
9326 if (EQ (type
, Qxbm
))
9327 return define_image_type (&xbm_type
);
9329 #if defined (HAVE_XPM) || defined (HAVE_NS)
9330 if (EQ (type
, Qxpm
))
9331 return define_image_type (&xpm_type
);
9334 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9335 if (EQ (type
, Qjpeg
))
9336 return define_image_type (&jpeg_type
);
9339 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9340 if (EQ (type
, Qtiff
))
9341 return define_image_type (&tiff_type
);
9344 #if defined (HAVE_GIF) || defined (HAVE_NS)
9345 if (EQ (type
, Qgif
))
9346 return define_image_type (&gif_type
);
9349 #if defined (HAVE_PNG) || defined (HAVE_NS)
9350 if (EQ (type
, Qpng
))
9351 return define_image_type (&png_type
);
9354 #if defined (HAVE_RSVG)
9355 if (EQ (type
, Qsvg
))
9356 return define_image_type (&svg_type
);
9359 #if defined (HAVE_IMAGEMAGICK)
9360 if (EQ (type
, Qimagemagick
))
9361 return define_image_type (&imagemagick_type
);
9364 #ifdef HAVE_GHOSTSCRIPT
9365 if (EQ (type
, Qpostscript
))
9366 return define_image_type (&gs_type
);
9372 /* Reset image_types before dumping.
9373 Called from Fdump_emacs. */
9376 reset_image_types (void)
9380 struct image_type
*next
= image_types
->next
;
9381 xfree (image_types
);
9387 syms_of_image (void)
9389 /* Initialize this only once; it will be reset before dumping. */
9392 /* Must be defined now because we're going to update it below, while
9393 defining the supported image types. */
9394 DEFVAR_LISP ("image-types", Vimage_types
,
9395 doc
: /* List of potentially supported image types.
9396 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9397 To check whether it is really supported, use `image-type-available-p'. */);
9398 Vimage_types
= Qnil
;
9400 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
9401 doc
: /* Maximum size of images.
9402 Emacs will not load an image into memory if its pixel width or
9403 pixel height exceeds this limit.
9405 If the value is an integer, it directly specifies the maximum
9406 image height and width, measured in pixels. If it is a floating
9407 point number, it specifies the maximum image height and width
9408 as a ratio to the frame height and width. If the value is
9409 non-numeric, there is no explicit limit on the size of images. */);
9410 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
9412 DEFSYM (Qcount
, "count");
9413 DEFSYM (Qextension_data
, "extension-data");
9414 DEFSYM (Qdelay
, "delay");
9416 DEFSYM (QCascent
, ":ascent");
9417 DEFSYM (QCmargin
, ":margin");
9418 DEFSYM (QCrelief
, ":relief");
9419 DEFSYM (QCconversion
, ":conversion");
9420 DEFSYM (QCcolor_symbols
, ":color-symbols");
9421 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
9422 DEFSYM (QCindex
, ":index");
9423 DEFSYM (QCgeometry
, ":geometry");
9424 DEFSYM (QCcrop
, ":crop");
9425 DEFSYM (QCrotation
, ":rotation");
9426 DEFSYM (QCmatrix
, ":matrix");
9427 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
9428 DEFSYM (QCmask
, ":mask");
9430 DEFSYM (Qlaplace
, "laplace");
9431 DEFSYM (Qemboss
, "emboss");
9432 DEFSYM (Qedge_detection
, "edge-detection");
9433 DEFSYM (Qheuristic
, "heuristic");
9435 DEFSYM (Qpostscript
, "postscript");
9436 DEFSYM (QCmax_width
, ":max-width");
9437 DEFSYM (QCmax_height
, ":max-height");
9438 #ifdef HAVE_GHOSTSCRIPT
9439 ADD_IMAGE_TYPE (Qpostscript
);
9440 DEFSYM (QCloader
, ":loader");
9441 DEFSYM (QCbounding_box
, ":bounding-box");
9442 DEFSYM (QCpt_width
, ":pt-width");
9443 DEFSYM (QCpt_height
, ":pt-height");
9444 #endif /* HAVE_GHOSTSCRIPT */
9447 DEFSYM (Qlibpng_version
, "libpng-version");
9448 Fset (Qlibpng_version
,
9450 make_number (PNG_LIBPNG_VER
)
9455 DEFSYM (Qlibgif_version
, "libgif-version");
9456 Fset (Qlibgif_version
,
9458 make_number (GIFLIB_MAJOR
* 10000
9459 + GIFLIB_MINOR
* 100
9465 DEFSYM (Qlibjpeg_version
, "libjpeg-version");
9466 Fset (Qlibjpeg_version
,
9468 make_number (JPEG_LIB_VERSION
)
9475 DEFSYM (Qpbm
, "pbm");
9476 ADD_IMAGE_TYPE (Qpbm
);
9478 DEFSYM (Qxbm
, "xbm");
9479 ADD_IMAGE_TYPE (Qxbm
);
9481 #if defined (HAVE_XPM) || defined (HAVE_NS)
9482 DEFSYM (Qxpm
, "xpm");
9483 ADD_IMAGE_TYPE (Qxpm
);
9486 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9487 DEFSYM (Qjpeg
, "jpeg");
9488 ADD_IMAGE_TYPE (Qjpeg
);
9491 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9492 DEFSYM (Qtiff
, "tiff");
9493 ADD_IMAGE_TYPE (Qtiff
);
9496 #if defined (HAVE_GIF) || defined (HAVE_NS)
9497 DEFSYM (Qgif
, "gif");
9498 ADD_IMAGE_TYPE (Qgif
);
9501 #if defined (HAVE_PNG) || defined (HAVE_NS)
9502 DEFSYM (Qpng
, "png");
9503 ADD_IMAGE_TYPE (Qpng
);
9506 #if defined (HAVE_IMAGEMAGICK)
9507 DEFSYM (Qimagemagick
, "imagemagick");
9508 ADD_IMAGE_TYPE (Qimagemagick
);
9511 #if defined (HAVE_RSVG)
9512 DEFSYM (Qsvg
, "svg");
9513 ADD_IMAGE_TYPE (Qsvg
);
9515 /* Other libraries used directly by svg code. */
9516 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
9517 DEFSYM (Qglib
, "glib");
9518 DEFSYM (Qgobject
, "gobject");
9519 #endif /* HAVE_NTGUI */
9520 #endif /* HAVE_RSVG */
9522 defsubr (&Sinit_image_library
);
9523 #ifdef HAVE_IMAGEMAGICK
9524 defsubr (&Simagemagick_types
);
9526 defsubr (&Sclear_image_cache
);
9527 defsubr (&Simage_flush
);
9528 defsubr (&Simage_size
);
9529 defsubr (&Simage_mask_p
);
9530 defsubr (&Simage_metadata
);
9534 defsubr (&Slookup_image
);
9537 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
9538 doc
: /* Non-nil means always draw a cross over disabled images.
9539 Disabled images are those having a `:conversion disabled' property.
9540 A cross is always drawn on black & white displays. */);
9541 cross_disabled_images
= 0;
9543 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
9544 doc
: /* List of directories to search for window system bitmap files. */);
9545 Vx_bitmap_file_path
= decode_env_path (0, PATH_BITMAPS
, 0);
9547 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
9548 doc
: /* Maximum time after which images are removed from the cache.
9549 When an image has not been displayed this many seconds, Emacs
9550 automatically removes it from the image cache. If the cache contains
9551 a large number of images, the actual eviction time may be shorter.
9552 The value can also be nil, meaning the cache is never cleared.
9554 The function `clear-image-cache' disregards this variable. */);
9555 Vimage_cache_eviction_delay
= make_number (300);
9556 #ifdef HAVE_IMAGEMAGICK
9557 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9558 doc
: /* Integer indicating which ImageMagick rendering method to use.
9560 0 -- the default method (pixel pushing)
9561 1 -- a newer method ("MagickExportImagePixels") that may perform
9562 better (speed etc) in some cases, but has not been as thoroughly
9563 tested with Emacs as the default method. This method requires
9564 ImageMagick version 6.4.6 (approximately) or later.
9566 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9567 imagemagick_render_type
= 0;