1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2012 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/>. */
25 #if defined HAVE_LIBPNG_PNG_H
26 # include <libpng/png.h>
36 /* This makes the fields of a Display accessible, in Xlib header files. */
38 #define XLIB_ILLEGAL_ACCESS
43 #include "dispextern.h"
44 #include "blockinput.h"
47 #include "character.h"
49 #include "termhooks.h"
52 #ifdef HAVE_SYS_STAT_H
54 #endif /* HAVE_SYS_STAT_H */
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif /* HAVE_SYS_TYPES_H */
60 #ifdef HAVE_WINDOW_SYSTEM
62 #endif /* HAVE_WINDOW_SYSTEM */
65 #define COLOR_TABLE_SUPPORT 1
67 typedef struct x_bitmap_record Bitmap_Record
;
68 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
69 #define NO_PIXMAP None
71 #define RGB_PIXEL_COLOR unsigned long
73 #define PIX_MASK_RETAIN 0
74 #define PIX_MASK_DRAW 1
75 #endif /* HAVE_X_WINDOWS */
79 /* W32_TODO : Color tables on W32. */
80 #undef COLOR_TABLE_SUPPORT
82 typedef struct w32_bitmap_record Bitmap_Record
;
83 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
86 #define RGB_PIXEL_COLOR COLORREF
88 #define PIX_MASK_RETAIN 0
89 #define PIX_MASK_DRAW 1
91 #define x_defined_color w32_defined_color
92 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
94 /* Version of libpng that we were compiled with, or -1 if no PNG
95 support was compiled in. This is tested by w32-win.el to correctly
96 set up the alist used to search for PNG libraries. */
97 Lisp_Object Qlibpng_version
;
98 #endif /* HAVE_NTGUI */
101 #undef COLOR_TABLE_SUPPORT
103 typedef struct ns_bitmap_record Bitmap_Record
;
105 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
108 #define RGB_PIXEL_COLOR unsigned long
111 #define PIX_MASK_RETAIN 0
112 #define PIX_MASK_DRAW 1
114 #define x_defined_color(f, name, color_def, alloc) \
115 ns_defined_color (f, name, color_def, alloc, 0)
116 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
120 /* The symbol `postscript' identifying images of this type. */
122 static Lisp_Object Qpostscript
;
124 static void x_disable_image (struct frame
*, struct image
*);
125 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
128 static void init_color_table (void);
129 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
130 #ifdef COLOR_TABLE_SUPPORT
131 static void free_color_table (void);
132 static unsigned long *colors_in_color_table (int *n
);
135 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
136 id, which is just an int that this section returns. Bitmaps are
137 reference counted so they can be shared among frames.
139 Bitmap indices are guaranteed to be > 0, so a negative number can
140 be used to indicate no bitmap.
142 If you use x_create_bitmap_from_data, then you must keep track of
143 the bitmaps yourself. That is, creating a bitmap from the same
144 data more than once will not be caught. */
148 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
149 unsigned int width
, unsigned int height
,
150 unsigned long plane_mask
, int format
)
152 /* TODO: not sure what this function is supposed to do.. */
153 ns_retain_object (pixmap
);
157 /* use with imgs created by ns_image_for_XPM */
159 XGetPixel (XImagePtr ximage
, int x
, int y
)
161 return ns_get_pixel (ximage
, x
, y
);
164 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
165 pixel is assumed to be in form RGB */
167 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
169 ns_put_pixel (ximage
, x
, y
, pixel
);
174 /* Functions to access the contents of a bitmap, given an id. */
177 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
179 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
183 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
185 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
188 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
190 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
192 /* HAVE_NTGUI needs the explicit cast here. */
193 return (ptrdiff_t) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
197 #ifdef HAVE_X_WINDOWS
199 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
201 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
205 /* Allocate a new bitmap record. Returns index of new record. */
208 x_allocate_bitmap_record (FRAME_PTR f
)
210 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
213 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
214 return ++dpyinfo
->bitmaps_last
;
216 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
217 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
221 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
222 10, -1, sizeof *dpyinfo
->bitmaps
);
223 return ++dpyinfo
->bitmaps_last
;
226 /* Add one reference to the reference count of the bitmap with id ID. */
229 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
231 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
234 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
237 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
239 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
242 #ifdef HAVE_X_WINDOWS
244 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
245 bits
, width
, height
);
248 #endif /* HAVE_X_WINDOWS */
252 bitmap
= CreateBitmap (width
, height
,
253 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
254 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
258 #endif /* HAVE_NTGUI */
261 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
266 id
= x_allocate_bitmap_record (f
);
269 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
270 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
273 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
274 dpyinfo
->bitmaps
[id
- 1].height
= height
;
275 dpyinfo
->bitmaps
[id
- 1].width
= width
;
276 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
278 #ifdef HAVE_X_WINDOWS
279 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
280 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
281 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
282 #endif /* HAVE_X_WINDOWS */
285 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
286 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
287 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
288 #endif /* HAVE_NTGUI */
293 /* Create bitmap from file FILE for frame F. */
296 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
298 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
301 return -1; /* W32_TODO : bitmap support */
302 #endif /* HAVE_NTGUI */
306 void *bitmap
= ns_image_from_file (file
);
312 id
= x_allocate_bitmap_record (f
);
313 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
314 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
315 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
316 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
317 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
318 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
319 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
323 #ifdef HAVE_X_WINDOWS
324 unsigned int width
, height
;
326 int xhot
, yhot
, result
;
332 /* Look for an existing bitmap with the same name. */
333 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
335 if (dpyinfo
->bitmaps
[id
].refcount
336 && dpyinfo
->bitmaps
[id
].file
337 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
339 ++dpyinfo
->bitmaps
[id
].refcount
;
344 /* Search bitmap-file-path for the file, if appropriate. */
345 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
350 filename
= SSDATA (found
);
352 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
353 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
354 if (result
!= BitmapSuccess
)
357 id
= x_allocate_bitmap_record (f
);
358 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
359 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
360 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
361 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
362 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
363 dpyinfo
->bitmaps
[id
- 1].height
= height
;
364 dpyinfo
->bitmaps
[id
- 1].width
= width
;
365 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
368 #endif /* HAVE_X_WINDOWS */
374 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
376 #ifdef HAVE_X_WINDOWS
377 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
379 XFreePixmap (dpyinfo
->display
, bm
->mask
);
380 #endif /* HAVE_X_WINDOWS */
383 DeleteObject (bm
->pixmap
);
384 #endif /* HAVE_NTGUI */
387 ns_release_object (bm
->img
);
397 /* Remove reference to bitmap with id number ID. */
400 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
402 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
406 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
408 if (--bm
->refcount
== 0)
411 free_bitmap_record (dpyinfo
, bm
);
417 /* Free all the bitmaps for the display specified by DPYINFO. */
420 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
423 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
425 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
426 if (bm
->refcount
> 0)
427 free_bitmap_record (dpyinfo
, bm
);
429 dpyinfo
->bitmaps_last
= 0;
432 static bool x_create_x_image_and_pixmap (struct frame
*, int, int, int,
433 XImagePtr
*, Pixmap
*);
434 static void x_destroy_x_image (XImagePtr ximg
);
436 #ifdef HAVE_X_WINDOWS
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_X_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
= 1;
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
))
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
= check_x_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
= check_x_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
= check_x_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 /* Free resources, then free IMG. */
1002 img
->type
->free (f
, img
);
1007 /* Return true if the given widths and heights are valid for display. */
1010 check_image_size (struct frame
*f
, int width
, int height
)
1014 if (width
<= 0 || height
<= 0)
1017 if (INTEGERP (Vmax_image_size
))
1018 return (width
<= XINT (Vmax_image_size
)
1019 && height
<= XINT (Vmax_image_size
));
1020 else if (FLOATP (Vmax_image_size
))
1024 w
= FRAME_PIXEL_WIDTH (f
);
1025 h
= FRAME_PIXEL_HEIGHT (f
);
1028 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1029 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1030 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1036 /* Prepare image IMG for display on frame F. Must be called before
1037 drawing an image. */
1040 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1042 /* We're about to display IMG, so set its timestamp to `now'. */
1043 img
->timestamp
= current_emacs_time ();
1045 /* If IMG doesn't have a pixmap yet, load it now, using the image
1046 type dependent loader function. */
1047 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1048 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1053 /* Value is the number of pixels for the ascent of image IMG when
1054 drawn in face FACE. */
1057 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1062 if (slice
->height
== img
->height
)
1063 height
= img
->height
+ img
->vmargin
;
1064 else if (slice
->y
== 0)
1065 height
= slice
->height
+ img
->vmargin
;
1067 height
= slice
->height
;
1069 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1074 /* W32 specific version. Why?. ++kfs */
1075 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1076 - FONT_BASE (face
->font
)) / 2;
1078 /* This expression is arranged so that if the image can't be
1079 exactly centered, it will be moved slightly up. This is
1080 because a typical font is `top-heavy' (due to the presence
1081 uppercase letters), so the image placement should err towards
1082 being top-heavy too. It also just generally looks better. */
1083 ascent
= (height
+ FONT_BASE (face
->font
)
1084 - FONT_DESCENT (face
->font
) + 1) / 2;
1085 #endif /* HAVE_NTGUI */
1088 ascent
= height
/ 2;
1091 ascent
= height
* (img
->ascent
/ 100.0);
1097 /* Image background colors. */
1099 /* Find the "best" corner color of a bitmap.
1100 On W32, XIMG is assumed to a device context with the bitmap selected. */
1102 static RGB_PIXEL_COLOR
1103 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1104 unsigned long width
, unsigned long height
)
1106 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1109 if (corners
&& corners
[BOT_CORNER
] >= 0)
1111 /* Get the colors at the corner_pixels of ximg. */
1112 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1113 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1114 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1115 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1119 /* Get the colors at the corner_pixels of ximg. */
1120 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1121 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1122 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1123 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1125 /* Choose the most frequently found color as background. */
1126 for (i
= best_count
= 0; i
< 4; ++i
)
1130 for (j
= n
= 0; j
< 4; ++j
)
1131 if (corner_pixels
[i
] == corner_pixels
[j
])
1135 best
= corner_pixels
[i
], best_count
= n
;
1141 /* Portability macros */
1145 #define Destroy_Image(img_dc, prev) \
1146 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1148 #define Free_Pixmap(display, pixmap) \
1149 DeleteObject (pixmap)
1151 #elif defined (HAVE_NS)
1153 #define Destroy_Image(ximg, dummy) \
1154 ns_release_object (ximg)
1156 #define Free_Pixmap(display, pixmap) \
1157 ns_release_object (pixmap)
1161 #define Destroy_Image(ximg, dummy) \
1162 XDestroyImage (ximg)
1164 #define Free_Pixmap(display, pixmap) \
1165 XFreePixmap (display, pixmap)
1167 #endif /* !HAVE_NTGUI && !HAVE_NS */
1170 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1171 it is guessed heuristically. If non-zero, XIMG is an existing
1172 XImage object (or device context with the image selected on W32) to
1173 use for the heuristic. */
1176 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1178 if (! img
->background_valid
)
1179 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1181 bool free_ximg
= !ximg
;
1184 #endif /* HAVE_NTGUI */
1189 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1190 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1192 HDC frame_dc
= get_frame_dc (f
);
1193 ximg
= CreateCompatibleDC (frame_dc
);
1194 release_frame_dc (f
, frame_dc
);
1195 prev
= SelectObject (ximg
, img
->pixmap
);
1196 #endif /* !HAVE_NTGUI */
1199 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1202 Destroy_Image (ximg
, prev
);
1204 img
->background_valid
= 1;
1207 return img
->background
;
1210 /* Return the `background_transparent' field of IMG. If IMG doesn't
1211 have one yet, it is guessed heuristically. If non-zero, MASK is an
1212 existing XImage object to use for the heuristic. */
1215 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1217 if (! img
->background_transparent_valid
)
1218 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1222 bool free_mask
= !mask
;
1225 #endif /* HAVE_NTGUI */
1230 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1231 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1233 HDC frame_dc
= get_frame_dc (f
);
1234 mask
= CreateCompatibleDC (frame_dc
);
1235 release_frame_dc (f
, frame_dc
);
1236 prev
= SelectObject (mask
, img
->mask
);
1237 #endif /* HAVE_NTGUI */
1240 img
->background_transparent
1241 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1244 Destroy_Image (mask
, prev
);
1247 img
->background_transparent
= 0;
1249 img
->background_transparent_valid
= 1;
1252 return img
->background_transparent
;
1256 /***********************************************************************
1257 Helper functions for X image types
1258 ***********************************************************************/
1260 /* Clear X resources of image IMG on frame F. PIXMAP_P means free the
1261 pixmap if any. MASK_P means clear the mask pixmap if any.
1262 COLORS_P means free colors allocated for the image, if any. */
1265 x_clear_image_1 (struct frame
*f
, struct image
*img
, bool pixmap_p
,
1266 bool mask_p
, bool colors_p
)
1268 if (pixmap_p
&& img
->pixmap
)
1270 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1271 img
->pixmap
= NO_PIXMAP
;
1272 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1273 img
->background_valid
= 0;
1276 if (mask_p
&& img
->mask
)
1278 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1279 img
->mask
= NO_PIXMAP
;
1280 img
->background_transparent_valid
= 0;
1283 if (colors_p
&& img
->ncolors
)
1285 /* W32_TODO: color table support. */
1286 #ifdef HAVE_X_WINDOWS
1287 x_free_colors (f
, img
->colors
, img
->ncolors
);
1288 #endif /* HAVE_X_WINDOWS */
1289 xfree (img
->colors
);
1296 /* Free X resources of image IMG which is used on frame F. */
1299 x_clear_image (struct frame
*f
, struct image
*img
)
1302 x_clear_image_1 (f
, img
, 1, 1, 1);
1307 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1308 cannot be allocated, use DFLT. Add a newly allocated color to
1309 IMG->colors, so that it can be freed again. Value is the pixel
1312 static unsigned long
1313 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1317 unsigned long result
;
1319 eassert (STRINGP (color_name
));
1321 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1322 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1325 /* This isn't called frequently so we get away with simply
1326 reallocating the color vector to the needed size, here. */
1327 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1328 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1329 img
->colors
[ncolors
- 1] = color
.pixel
;
1330 img
->ncolors
= ncolors
;
1331 result
= color
.pixel
;
1341 /***********************************************************************
1343 ***********************************************************************/
1345 static void cache_image (struct frame
*f
, struct image
*img
);
1347 /* Return a new, initialized image cache that is allocated from the
1348 heap. Call free_image_cache to free an image cache. */
1350 struct image_cache
*
1351 make_image_cache (void)
1353 struct image_cache
*c
= xzalloc (sizeof *c
);
1357 c
->images
= xmalloc (size
* sizeof *c
->images
);
1359 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1360 c
->buckets
= xzalloc (size
);
1365 /* Find an image matching SPEC in the cache, and return it. If no
1366 image is found, return NULL. */
1367 static struct image
*
1368 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1371 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1372 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1374 if (!c
) return NULL
;
1376 /* If the image spec does not specify a background color, the cached
1377 image must have the same background color as the current frame.
1378 The foreground color must also match, for the sake of monochrome
1381 In fact, we could ignore the foreground color matching condition
1382 for color images, or if the image spec specifies :foreground;
1383 similarly we could ignore the background color matching condition
1384 for formats that don't use transparency (such as jpeg), or if the
1385 image spec specifies :background. However, the extra memory
1386 usage is probably negligible in practice, so we don't bother. */
1388 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1389 if (img
->hash
== hash
1390 && !NILP (Fequal (img
->spec
, spec
))
1391 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1392 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1398 /* Search frame F for an image with spec SPEC, and free it. */
1401 uncache_image (struct frame
*f
, Lisp_Object spec
)
1403 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1406 free_image (f
, img
);
1407 /* As display glyphs may still be referring to the image ID, we
1408 must garbage the frame (Bug#6426). */
1409 SET_FRAME_GARBAGED (f
);
1414 /* Free image cache of frame F. Be aware that X frames share images
1418 free_image_cache (struct frame
*f
)
1420 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1425 /* Cache should not be referenced by any frame when freed. */
1426 eassert (c
->refcount
== 0);
1428 for (i
= 0; i
< c
->used
; ++i
)
1429 free_image (f
, c
->images
[i
]);
1433 FRAME_IMAGE_CACHE (f
) = NULL
;
1438 /* Clear image cache of frame F. FILTER=t means free all images.
1439 FILTER=nil means clear only images that haven't been
1440 displayed for some time.
1441 Else, only free the images which have FILTER in their `dependencies'.
1442 Should be called from time to time to reduce the number of loaded images.
1443 If image-cache-eviction-delay is non-nil, this frees images in the cache
1444 which weren't displayed for at least that many seconds. */
1447 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1449 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1453 ptrdiff_t i
, nfreed
= 0;
1455 /* Block input so that we won't be interrupted by a SIGIO
1456 while being in an inconsistent state. */
1461 /* Filter image cache. */
1462 for (i
= 0; i
< c
->used
; ++i
)
1464 struct image
*img
= c
->images
[i
];
1465 if (img
&& (EQ (Qt
, filter
)
1466 || !NILP (Fmember (filter
, img
->dependencies
))))
1468 free_image (f
, img
);
1473 else if (INTEGERP (Vimage_cache_eviction_delay
))
1475 /* Free cache based on timestamp. */
1478 ptrdiff_t nimages
= 0;
1480 for (i
= 0; i
< c
->used
; ++i
)
1484 /* If the number of cached images has grown unusually large,
1485 decrease the cache eviction delay (Bug#6230). */
1486 delay
= XINT (Vimage_cache_eviction_delay
);
1488 delay
= 1600 * delay
/ nimages
/ nimages
;
1489 delay
= max (delay
, 1);
1491 t
= current_emacs_time ();
1492 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1494 for (i
= 0; i
< c
->used
; ++i
)
1496 struct image
*img
= c
->images
[i
];
1497 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1499 free_image (f
, img
);
1505 /* We may be clearing the image cache because, for example,
1506 Emacs was iconified for a longer period of time. In that
1507 case, current matrices may still contain references to
1508 images freed above. So, clear these matrices. */
1511 Lisp_Object tail
, frame
;
1513 FOR_EACH_FRAME (tail
, frame
)
1515 struct frame
*fr
= XFRAME (frame
);
1516 if (FRAME_IMAGE_CACHE (fr
) == c
)
1517 clear_current_matrices (fr
);
1520 ++windows_or_buffers_changed
;
1528 clear_image_caches (Lisp_Object filter
)
1530 /* FIXME: We want to do
1531 * struct terminal *t;
1532 * for (t = terminal_list; t; t = t->next_terminal)
1533 * clear_image_cache (t, filter); */
1534 Lisp_Object tail
, frame
;
1535 FOR_EACH_FRAME (tail
, frame
)
1536 if (FRAME_WINDOW_P (XFRAME (frame
)))
1537 clear_image_cache (XFRAME (frame
), filter
);
1540 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1542 doc
: /* Clear the image cache.
1543 FILTER nil or a frame means clear all images in the selected frame.
1544 FILTER t means clear the image caches of all frames.
1545 Anything else, means only clear those images which refer to FILTER,
1546 which is then usually a filename. */)
1547 (Lisp_Object filter
)
1549 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1550 clear_image_caches (filter
);
1552 clear_image_cache (check_x_frame (filter
), Qt
);
1558 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1560 doc
: /* Fush the image with specification SPEC on frame FRAME.
1561 This removes the image from the Emacs image cache. If SPEC specifies
1562 an image file, the next redisplay of this image will read from the
1563 current contents of that file.
1565 FRAME nil or omitted means use the selected frame.
1566 FRAME t means refresh the image on all frames. */)
1567 (Lisp_Object spec
, Lisp_Object frame
)
1569 if (!valid_image_p (spec
))
1570 error ("Invalid image specification");
1575 FOR_EACH_FRAME (tail
, frame
)
1577 struct frame
*f
= XFRAME (frame
);
1578 if (FRAME_WINDOW_P (f
))
1579 uncache_image (f
, spec
);
1583 uncache_image (check_x_frame (frame
), spec
);
1589 /* Compute masks and transform image IMG on frame F, as specified
1590 by the image's specification, */
1593 postprocess_image (struct frame
*f
, struct image
*img
)
1595 /* Manipulation of the image's mask. */
1598 Lisp_Object conversion
, spec
;
1603 /* `:heuristic-mask t'
1605 means build a mask heuristically.
1606 `:heuristic-mask (R G B)'
1607 `:mask (heuristic (R G B))'
1608 means build a mask from color (R G B) in the
1611 means remove a mask, if any. */
1613 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1615 x_build_heuristic_mask (f
, img
, mask
);
1620 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1622 if (EQ (mask
, Qheuristic
))
1623 x_build_heuristic_mask (f
, img
, Qt
);
1624 else if (CONSP (mask
)
1625 && EQ (XCAR (mask
), Qheuristic
))
1627 if (CONSP (XCDR (mask
)))
1628 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1630 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1632 else if (NILP (mask
) && found_p
&& img
->mask
)
1634 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1635 img
->mask
= NO_PIXMAP
;
1640 /* Should we apply an image transformation algorithm? */
1641 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1642 if (EQ (conversion
, Qdisabled
))
1643 x_disable_image (f
, img
);
1644 else if (EQ (conversion
, Qlaplace
))
1646 else if (EQ (conversion
, Qemboss
))
1648 else if (CONSP (conversion
)
1649 && EQ (XCAR (conversion
), Qedge_detection
))
1652 tem
= XCDR (conversion
);
1654 x_edge_detection (f
, img
,
1655 Fplist_get (tem
, QCmatrix
),
1656 Fplist_get (tem
, QCcolor_adjustment
));
1662 /* Return the id of image with Lisp specification SPEC on frame F.
1663 SPEC must be a valid Lisp image specification (see valid_image_p). */
1666 lookup_image (struct frame
*f
, Lisp_Object spec
)
1671 /* F must be a window-system frame, and SPEC must be a valid image
1673 eassert (FRAME_WINDOW_P (f
));
1674 eassert (valid_image_p (spec
));
1676 /* Look up SPEC in the hash table of the image cache. */
1677 hash
= sxhash (spec
, 0);
1678 img
= search_image_cache (f
, spec
, hash
);
1679 if (img
&& img
->load_failed_p
)
1681 free_image (f
, img
);
1685 /* If not found, create a new image and cache it. */
1689 img
= make_image (spec
, hash
);
1690 cache_image (f
, img
);
1691 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1692 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1693 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1695 /* If we can't load the image, and we don't have a width and
1696 height, use some arbitrary width and height so that we can
1697 draw a rectangle for it. */
1698 if (img
->load_failed_p
)
1702 value
= image_spec_value (spec
, QCwidth
, NULL
);
1703 img
->width
= (INTEGERP (value
)
1704 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1705 value
= image_spec_value (spec
, QCheight
, NULL
);
1706 img
->height
= (INTEGERP (value
)
1707 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1711 /* Handle image type independent image attributes
1712 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1713 `:background COLOR'. */
1714 Lisp_Object ascent
, margin
, relief
, bg
;
1717 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1718 if (INTEGERP (ascent
))
1719 img
->ascent
= XFASTINT (ascent
);
1720 else if (EQ (ascent
, Qcenter
))
1721 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1723 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1724 if (INTEGERP (margin
))
1725 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1726 else if (CONSP (margin
))
1728 img
->hmargin
= XFASTINT (XCAR (margin
));
1729 img
->vmargin
= XFASTINT (XCDR (margin
));
1732 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1733 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1734 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1736 img
->relief
= XINT (relief
);
1737 img
->hmargin
+= eabs (img
->relief
);
1738 img
->vmargin
+= eabs (img
->relief
);
1741 if (! img
->background_valid
)
1743 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1747 = x_alloc_image_color (f
, img
, bg
,
1748 FRAME_BACKGROUND_PIXEL (f
));
1749 img
->background_valid
= 1;
1753 /* Do image transformations and compute masks, unless we
1754 don't have the image yet. */
1755 if (!EQ (*img
->type
->type
, Qpostscript
))
1756 postprocess_image (f
, img
);
1762 /* We're using IMG, so set its timestamp to `now'. */
1763 img
->timestamp
= current_emacs_time ();
1765 /* Value is the image id. */
1770 /* Cache image IMG in the image cache of frame F. */
1773 cache_image (struct frame
*f
, struct image
*img
)
1775 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1778 /* Find a free slot in c->images. */
1779 for (i
= 0; i
< c
->used
; ++i
)
1780 if (c
->images
[i
] == NULL
)
1783 /* If no free slot found, maybe enlarge c->images. */
1784 if (i
== c
->used
&& c
->used
== c
->size
)
1785 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1787 /* Add IMG to c->images, and assign IMG an id. */
1793 /* Add IMG to the cache's hash table. */
1794 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1795 img
->next
= c
->buckets
[i
];
1797 img
->next
->prev
= img
;
1799 c
->buckets
[i
] = img
;
1803 /* Call FN on every image in the image cache of frame F. Used to mark
1804 Lisp Objects in the image cache. */
1806 /* Mark Lisp objects in image IMG. */
1809 mark_image (struct image
*img
)
1811 mark_object (img
->spec
);
1812 mark_object (img
->dependencies
);
1814 if (!NILP (img
->lisp_data
))
1815 mark_object (img
->lisp_data
);
1820 mark_image_cache (struct image_cache
*c
)
1825 for (i
= 0; i
< c
->used
; ++i
)
1827 mark_image (c
->images
[i
]);
1833 /***********************************************************************
1834 X / NS / W32 support code
1835 ***********************************************************************/
1839 /* Macro for defining functions that will be loaded from image DLLs. */
1840 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1842 /* Macro for loading those image functions from the library. */
1843 #define LOAD_IMGLIB_FN(lib,func) { \
1844 fn_##func = (void *) GetProcAddress (lib, #func); \
1845 if (!fn_##func) return 0; \
1848 #endif /* HAVE_NTGUI */
1850 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1852 WIDTH and HEIGHT must both be positive.
1853 If XIMG is null, assume it is a bitmap. */
1855 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1857 #ifdef HAVE_X_WINDOWS
1858 /* Respect Xlib's limits: it cannot deal with images that have more
1859 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1860 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1863 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1864 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1867 int bitmap_pad
, depth
, bytes_per_line
;
1870 bitmap_pad
= ximg
->bitmap_pad
;
1871 depth
= ximg
->depth
;
1872 bytes_per_line
= ximg
->bytes_per_line
;
1878 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1880 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1881 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1883 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1884 For now, assume that every image size is allowed on these systems. */
1889 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1890 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1891 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1892 via xmalloc. Print error messages via image_error if an error
1893 occurs. Value is true if successful.
1895 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1896 should indicate the bit depth of the image. */
1899 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1900 XImagePtr
*ximg
, Pixmap
*pixmap
)
1902 #ifdef HAVE_X_WINDOWS
1903 Display
*display
= FRAME_X_DISPLAY (f
);
1904 Window window
= FRAME_X_WINDOW (f
);
1905 Screen
*screen
= FRAME_X_SCREEN (f
);
1907 eassert (input_blocked_p ());
1910 depth
= DefaultDepthOfScreen (screen
);
1911 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1912 depth
, ZPixmap
, 0, NULL
, width
, height
,
1913 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1916 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1920 if (! x_check_image_size (*ximg
, width
, height
))
1922 x_destroy_x_image (*ximg
);
1924 image_error ("Image too large (%dx%d)",
1925 make_number (width
), make_number (height
));
1929 /* Allocate image raster. */
1930 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1932 /* Allocate a pixmap of the same size. */
1933 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1934 if (*pixmap
== NO_PIXMAP
)
1936 x_destroy_x_image (*ximg
);
1938 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1943 #endif /* HAVE_X_WINDOWS */
1947 BITMAPINFOHEADER
*header
;
1949 int scanline_width_bits
;
1951 int palette_colors
= 0;
1956 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1957 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1959 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1963 scanline_width_bits
= width
* depth
;
1964 remainder
= scanline_width_bits
% 32;
1967 scanline_width_bits
+= 32 - remainder
;
1969 /* Bitmaps with a depth less than 16 need a palette. */
1970 /* BITMAPINFO structure already contains the first RGBQUAD. */
1972 palette_colors
= 1 << (depth
- 1);
1974 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
1976 header
= &(*ximg
)->info
.bmiHeader
;
1977 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
1978 header
->biSize
= sizeof (*header
);
1979 header
->biWidth
= width
;
1980 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
1981 header
->biPlanes
= 1;
1982 header
->biBitCount
= depth
;
1983 header
->biCompression
= BI_RGB
;
1984 header
->biClrUsed
= palette_colors
;
1986 /* TODO: fill in palette. */
1989 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
1990 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
1991 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
1992 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
1993 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
1994 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
1995 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
1996 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
1999 hdc
= get_frame_dc (f
);
2001 /* Create a DIBSection and raster array for the bitmap,
2002 and store its handle in *pixmap. */
2003 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2004 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2005 /* casting avoids a GCC warning */
2006 (void **)&((*ximg
)->data
), NULL
, 0);
2008 /* Realize display palette and garbage all frames. */
2009 release_frame_dc (f
, hdc
);
2011 if (*pixmap
== NULL
)
2013 DWORD err
= GetLastError ();
2014 Lisp_Object errcode
;
2015 /* All system errors are < 10000, so the following is safe. */
2016 XSETINT (errcode
, err
);
2017 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2018 x_destroy_x_image (*ximg
);
2024 #endif /* HAVE_NTGUI */
2027 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2031 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2040 /* Destroy XImage XIMG. Free XIMG->data. */
2043 x_destroy_x_image (XImagePtr ximg
)
2045 eassert (input_blocked_p ());
2048 #ifdef HAVE_X_WINDOWS
2051 XDestroyImage (ximg
);
2052 #endif /* HAVE_X_WINDOWS */
2054 /* Data will be freed by DestroyObject. */
2057 #endif /* HAVE_NTGUI */
2059 ns_release_object (ximg
);
2060 #endif /* HAVE_NS */
2065 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2066 are width and height of both the image and pixmap. */
2069 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2071 #ifdef HAVE_X_WINDOWS
2074 eassert (input_blocked_p ());
2075 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2076 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2077 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2078 #endif /* HAVE_X_WINDOWS */
2081 #if 0 /* I don't think this is necessary looking at where it is used. */
2082 HDC hdc
= get_frame_dc (f
);
2083 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2084 release_frame_dc (f
, hdc
);
2086 #endif /* HAVE_NTGUI */
2089 eassert (ximg
== pixmap
);
2090 ns_retain_object (ximg
);
2095 /***********************************************************************
2097 ***********************************************************************/
2099 /* Find image file FILE. Look in data-directory/images, then
2100 x-bitmap-file-path. Value is the encoded full name of the file
2101 found, or nil if not found. */
2104 x_find_image_file (Lisp_Object file
)
2106 Lisp_Object file_found
, search_path
;
2109 /* TODO I think this should use something like image-load-path
2110 instead. Unfortunately, that can contain non-string elements. */
2111 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2113 Vx_bitmap_file_path
);
2115 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2116 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2122 file_found
= ENCODE_FILE (file_found
);
2130 /* Read FILE into memory. Value is a pointer to a buffer allocated
2131 with xmalloc holding FILE's contents. Value is null if an error
2132 occurred. *SIZE is set to the size of the file. */
2134 static unsigned char *
2135 slurp_file (char *file
, ptrdiff_t *size
)
2138 unsigned char *buf
= NULL
;
2141 if (stat (file
, &st
) == 0
2142 && (fp
= fopen (file
, "rb")) != NULL
2143 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2144 && (buf
= xmalloc (st
.st_size
),
2145 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2166 /***********************************************************************
2168 ***********************************************************************/
2170 static bool xbm_load (struct frame
*f
, struct image
*img
);
2171 static bool xbm_image_p (Lisp_Object object
);
2172 static bool xbm_file_p (Lisp_Object
);
2175 /* Indices of image specification fields in xbm_format, below. */
2177 enum xbm_keyword_index
2195 /* Vector of image_keyword structures describing the format
2196 of valid XBM image specifications. */
2198 static const struct image_keyword xbm_format
[XBM_LAST
] =
2200 {":type", IMAGE_SYMBOL_VALUE
, 1},
2201 {":file", IMAGE_STRING_VALUE
, 0},
2202 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2203 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2204 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2205 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2206 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2207 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2208 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2209 {":relief", IMAGE_INTEGER_VALUE
, 0},
2210 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2211 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2212 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2215 /* Structure describing the image type XBM. */
2217 static struct image_type xbm_type
=
2227 /* Tokens returned from xbm_scan. */
2236 /* Return true if OBJECT is a valid XBM-type image specification.
2237 A valid specification is a list starting with the symbol `image'
2238 The rest of the list is a property list which must contain an
2241 If the specification specifies a file to load, it must contain
2242 an entry `:file FILENAME' where FILENAME is a string.
2244 If the specification is for a bitmap loaded from memory it must
2245 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2246 WIDTH and HEIGHT are integers > 0. DATA may be:
2248 1. a string large enough to hold the bitmap data, i.e. it must
2249 have a size >= (WIDTH + 7) / 8 * HEIGHT
2251 2. a bool-vector of size >= WIDTH * HEIGHT
2253 3. a vector of strings or bool-vectors, one for each line of the
2256 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2257 may not be specified in this case because they are defined in the
2260 Both the file and data forms may contain the additional entries
2261 `:background COLOR' and `:foreground COLOR'. If not present,
2262 foreground and background of the frame on which the image is
2263 displayed is used. */
2266 xbm_image_p (Lisp_Object object
)
2268 struct image_keyword kw
[XBM_LAST
];
2270 memcpy (kw
, xbm_format
, sizeof kw
);
2271 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2274 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2276 if (kw
[XBM_FILE
].count
)
2278 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2281 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2283 /* In-memory XBM file. */
2284 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2292 /* Entries for `:width', `:height' and `:data' must be present. */
2293 if (!kw
[XBM_WIDTH
].count
2294 || !kw
[XBM_HEIGHT
].count
2295 || !kw
[XBM_DATA
].count
)
2298 data
= kw
[XBM_DATA
].value
;
2299 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2300 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2302 /* Check type of data, and width and height against contents of
2308 /* Number of elements of the vector must be >= height. */
2309 if (ASIZE (data
) < height
)
2312 /* Each string or bool-vector in data must be large enough
2313 for one line of the image. */
2314 for (i
= 0; i
< height
; ++i
)
2316 Lisp_Object elt
= AREF (data
, i
);
2321 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2324 else if (BOOL_VECTOR_P (elt
))
2326 if (XBOOL_VECTOR (elt
)->size
< width
)
2333 else if (STRINGP (data
))
2336 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2339 else if (BOOL_VECTOR_P (data
))
2341 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2352 /* Scan a bitmap file. FP is the stream to read from. Value is
2353 either an enumerator from enum xbm_token, or a character for a
2354 single-character token, or 0 at end of file. If scanning an
2355 identifier, store the lexeme of the identifier in SVAL. If
2356 scanning a number, store its value in *IVAL. */
2359 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2365 /* Skip white space. */
2366 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2371 else if (c_isdigit (c
))
2373 int value
= 0, digit
;
2375 if (c
== '0' && *s
< end
)
2378 if (c
== 'x' || c
== 'X')
2385 else if (c
>= 'a' && c
<= 'f')
2386 digit
= c
- 'a' + 10;
2387 else if (c
>= 'A' && c
<= 'F')
2388 digit
= c
- 'A' + 10;
2391 value
= 16 * value
+ digit
;
2394 else if (c_isdigit (c
))
2398 && (c
= *(*s
)++, c_isdigit (c
)))
2399 value
= 8 * value
+ c
- '0';
2406 && (c
= *(*s
)++, c_isdigit (c
)))
2407 value
= 10 * value
+ c
- '0';
2415 else if (c_isalpha (c
) || c
== '_')
2419 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2426 else if (c
== '/' && **s
== '*')
2428 /* C-style comment. */
2430 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2444 /* Create a Windows bitmap from X bitmap data. */
2446 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2448 static unsigned char swap_nibble
[16]
2449 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2450 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2451 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2452 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2454 unsigned char *bits
, *p
;
2457 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2458 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2459 bits
= alloca (height
* w2
);
2460 memset (bits
, 0, height
* w2
);
2461 for (i
= 0; i
< height
; i
++)
2464 for (j
= 0; j
< w1
; j
++)
2466 /* Bitswap XBM bytes to match how Windows does things. */
2467 unsigned char c
= *data
++;
2468 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2469 | (swap_nibble
[(c
>>4) & 0xf]));
2472 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2478 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2479 COLORREF foreground
, COLORREF background
)
2481 HDC hdc
, old_img_dc
, new_img_dc
;
2482 HGDIOBJ old_prev
, new_prev
;
2485 hdc
= get_frame_dc (f
);
2486 old_img_dc
= CreateCompatibleDC (hdc
);
2487 new_img_dc
= CreateCompatibleDC (hdc
);
2488 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2489 release_frame_dc (f
, hdc
);
2490 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2491 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2492 /* Windows convention for mono bitmaps is black = background,
2493 white = foreground. */
2494 SetTextColor (new_img_dc
, background
);
2495 SetBkColor (new_img_dc
, foreground
);
2497 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2500 SelectObject (old_img_dc
, old_prev
);
2501 SelectObject (new_img_dc
, new_prev
);
2502 DeleteDC (old_img_dc
);
2503 DeleteDC (new_img_dc
);
2504 DeleteObject (img
->pixmap
);
2505 if (new_pixmap
== 0)
2506 fprintf (stderr
, "Failed to convert image to color.\n");
2508 img
->pixmap
= new_pixmap
;
2511 #define XBM_BIT_SHUFFLE(b) (~(b))
2515 #define XBM_BIT_SHUFFLE(b) (b)
2517 #endif /* HAVE_NTGUI */
2521 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2522 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2523 bool non_default_colors
)
2527 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2529 /* If colors were specified, transfer the bitmap to a color one. */
2530 if (non_default_colors
)
2531 convert_mono_to_color_image (f
, img
, fg
, bg
);
2533 #elif defined (HAVE_NS)
2534 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2538 (x_check_image_size (0, img
->width
, img
->height
)
2539 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2542 img
->width
, img
->height
,
2544 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2546 #endif /* !HAVE_NTGUI && !HAVE_NS */
2551 /* Replacement for XReadBitmapFileData which isn't available under old
2552 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2553 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2554 the image. Return in *DATA the bitmap data allocated with xmalloc.
2555 Value is true if successful. DATA null means just test if
2556 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2557 inhibit the call to image_error when the image size is invalid (the
2558 bitmap remains unread). */
2561 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2562 int *width
, int *height
, char **data
,
2563 bool inhibit_image_error
)
2565 unsigned char *s
= contents
;
2566 char buffer
[BUFSIZ
];
2569 int bytes_per_line
, i
, nbytes
;
2575 LA1 = xbm_scan (&s, end, buffer, &value)
2577 #define expect(TOKEN) \
2578 if (LA1 != (TOKEN)) \
2583 #define expect_ident(IDENT) \
2584 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2589 *width
= *height
= -1;
2592 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2594 /* Parse defines for width, height and hot-spots. */
2598 expect_ident ("define");
2599 expect (XBM_TK_IDENT
);
2601 if (LA1
== XBM_TK_NUMBER
)
2603 char *q
= strrchr (buffer
, '_');
2604 q
= q
? q
+ 1 : buffer
;
2605 if (strcmp (q
, "width") == 0)
2607 else if (strcmp (q
, "height") == 0)
2610 expect (XBM_TK_NUMBER
);
2613 if (!check_image_size (f
, *width
, *height
))
2615 if (!inhibit_image_error
)
2616 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2619 else if (data
== NULL
)
2622 /* Parse bits. Must start with `static'. */
2623 expect_ident ("static");
2624 if (LA1
== XBM_TK_IDENT
)
2626 if (strcmp (buffer
, "unsigned") == 0)
2629 expect_ident ("char");
2631 else if (strcmp (buffer
, "short") == 0)
2635 if (*width
% 16 && *width
% 16 < 9)
2638 else if (strcmp (buffer
, "char") == 0)
2646 expect (XBM_TK_IDENT
);
2652 if (! x_check_image_size (0, *width
, *height
))
2654 if (!inhibit_image_error
)
2655 image_error ("Image too large (%dx%d)",
2656 make_number (*width
), make_number (*height
));
2659 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2660 nbytes
= bytes_per_line
* *height
;
2661 p
= *data
= xmalloc (nbytes
);
2665 for (i
= 0; i
< nbytes
; i
+= 2)
2668 expect (XBM_TK_NUMBER
);
2670 *p
++ = XBM_BIT_SHUFFLE (val
);
2671 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2672 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2674 if (LA1
== ',' || LA1
== '}')
2682 for (i
= 0; i
< nbytes
; ++i
)
2685 expect (XBM_TK_NUMBER
);
2687 *p
++ = XBM_BIT_SHUFFLE (val
);
2689 if (LA1
== ',' || LA1
== '}')
2714 /* Load XBM image IMG which will be displayed on frame F from buffer
2715 CONTENTS. END is the end of the buffer. Value is true if
2719 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2726 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2730 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2731 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2732 bool non_default_colors
= 0;
2735 eassert (img
->width
> 0 && img
->height
> 0);
2737 /* Get foreground and background colors, maybe allocate colors. */
2738 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2741 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2742 non_default_colors
= 1;
2744 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2747 background
= x_alloc_image_color (f
, img
, value
, background
);
2748 img
->background
= background
;
2749 img
->background_valid
= 1;
2750 non_default_colors
= 1;
2753 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2754 foreground
, background
,
2755 non_default_colors
);
2758 if (img
->pixmap
== NO_PIXMAP
)
2760 x_clear_image (f
, img
);
2761 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2767 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2773 /* Value is true if DATA looks like an in-memory XBM file. */
2776 xbm_file_p (Lisp_Object data
)
2779 return (STRINGP (data
)
2780 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2781 (SDATA (data
) + SBYTES (data
)),
2786 /* Fill image IMG which is used on frame F with pixmap data. Value is
2787 true if successful. */
2790 xbm_load (struct frame
*f
, struct image
*img
)
2793 Lisp_Object file_name
;
2795 eassert (xbm_image_p (img
->spec
));
2797 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2798 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2799 if (STRINGP (file_name
))
2802 unsigned char *contents
;
2805 file
= x_find_image_file (file_name
);
2806 if (!STRINGP (file
))
2808 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2812 contents
= slurp_file (SSDATA (file
), &size
);
2813 if (contents
== NULL
)
2815 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2819 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2824 struct image_keyword fmt
[XBM_LAST
];
2826 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2827 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2828 bool non_default_colors
= 0;
2831 bool in_memory_file_p
= 0;
2833 /* See if data looks like an in-memory XBM file. */
2834 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2835 in_memory_file_p
= xbm_file_p (data
);
2837 /* Parse the image specification. */
2838 memcpy (fmt
, xbm_format
, sizeof fmt
);
2839 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2842 /* Get specified width, and height. */
2843 if (!in_memory_file_p
)
2845 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2846 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2847 eassert (img
->width
> 0 && img
->height
> 0);
2848 if (!check_image_size (f
, img
->width
, img
->height
))
2850 image_error ("Invalid image size (see `max-image-size')",
2856 /* Get foreground and background colors, maybe allocate colors. */
2857 if (fmt
[XBM_FOREGROUND
].count
2858 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2860 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2862 non_default_colors
= 1;
2865 if (fmt
[XBM_BACKGROUND
].count
2866 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2868 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2870 non_default_colors
= 1;
2873 if (in_memory_file_p
)
2874 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2883 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2885 p
= bits
= alloca (nbytes
* img
->height
);
2886 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2888 Lisp_Object line
= AREF (data
, i
);
2890 memcpy (p
, SDATA (line
), nbytes
);
2892 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2895 else if (STRINGP (data
))
2896 bits
= SSDATA (data
);
2898 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2904 /* Windows mono bitmaps are reversed compared with X. */
2905 invertedBits
= bits
;
2906 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2908 bits
= alloca (nbytes
);
2909 for (i
= 0; i
< nbytes
; i
++)
2910 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2913 /* Create the pixmap. */
2915 if (x_check_image_size (0, img
->width
, img
->height
))
2916 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2917 foreground
, background
,
2918 non_default_colors
);
2920 img
->pixmap
= NO_PIXMAP
;
2926 image_error ("Unable to create pixmap for XBM image `%s'",
2928 x_clear_image (f
, img
);
2938 /***********************************************************************
2940 ***********************************************************************/
2942 #if defined (HAVE_XPM) || defined (HAVE_NS)
2944 static bool xpm_image_p (Lisp_Object object
);
2945 static bool xpm_load (struct frame
*f
, struct image
*img
);
2947 #endif /* HAVE_XPM || HAVE_NS */
2951 /* Indicate to xpm.h that we don't have Xlib. */
2953 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2954 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2955 #define XColor xpm_XColor
2956 #define XImage xpm_XImage
2957 #define Display xpm_Display
2958 #define PIXEL_ALREADY_TYPEDEFED
2959 #include "X11/xpm.h"
2964 #undef PIXEL_ALREADY_TYPEDEFED
2966 #include "X11/xpm.h"
2967 #endif /* HAVE_NTGUI */
2968 #endif /* HAVE_XPM */
2970 #if defined (HAVE_XPM) || defined (HAVE_NS)
2971 /* The symbol `xpm' identifying XPM-format images. */
2973 static Lisp_Object Qxpm
;
2975 /* Indices of image specification fields in xpm_format, below. */
2977 enum xpm_keyword_index
2993 /* Vector of image_keyword structures describing the format
2994 of valid XPM image specifications. */
2996 static const struct image_keyword xpm_format
[XPM_LAST
] =
2998 {":type", IMAGE_SYMBOL_VALUE
, 1},
2999 {":file", IMAGE_STRING_VALUE
, 0},
3000 {":data", IMAGE_STRING_VALUE
, 0},
3001 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3002 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3003 {":relief", IMAGE_INTEGER_VALUE
, 0},
3004 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3005 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3006 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3007 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3008 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3012 static bool init_xpm_functions (void);
3014 #define init_xpm_functions NULL
3017 /* Structure describing the image type XPM. */
3019 static struct image_type xpm_type
=
3029 #ifdef HAVE_X_WINDOWS
3031 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3032 functions for allocating image colors. Our own functions handle
3033 color allocation failures more gracefully than the ones on the XPM
3036 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3037 #define ALLOC_XPM_COLORS
3039 #endif /* HAVE_X_WINDOWS */
3041 #ifdef ALLOC_XPM_COLORS
3043 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3046 /* An entry in a hash table used to cache color definitions of named
3047 colors. This cache is necessary to speed up XPM image loading in
3048 case we do color allocations ourselves. Without it, we would need
3049 a call to XParseColor per pixel in the image. */
3051 struct xpm_cached_color
3053 /* Next in collision chain. */
3054 struct xpm_cached_color
*next
;
3056 /* Color definition (RGB and pixel color). */
3063 /* The hash table used for the color cache, and its bucket vector
3066 #define XPM_COLOR_CACHE_BUCKETS 1001
3067 static struct xpm_cached_color
**xpm_color_cache
;
3069 /* Initialize the color cache. */
3072 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3074 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3075 xpm_color_cache
= xzalloc (nbytes
);
3076 init_color_table ();
3078 if (attrs
->valuemask
& XpmColorSymbols
)
3083 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3084 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3085 attrs
->colorsymbols
[i
].value
, &color
))
3087 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3089 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3094 /* Free the color cache. */
3097 xpm_free_color_cache (void)
3099 struct xpm_cached_color
*p
, *next
;
3102 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3103 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3109 xfree (xpm_color_cache
);
3110 xpm_color_cache
= NULL
;
3111 free_color_table ();
3114 /* Return the bucket index for color named COLOR_NAME in the color
3118 xpm_color_bucket (char *color_name
)
3120 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3121 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3125 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3126 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3129 static struct xpm_cached_color
*
3130 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3133 struct xpm_cached_color
*p
;
3136 bucket
= xpm_color_bucket (color_name
);
3138 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3139 p
= xmalloc (nbytes
);
3140 strcpy (p
->name
, color_name
);
3142 p
->next
= xpm_color_cache
[bucket
];
3143 xpm_color_cache
[bucket
] = p
;
3147 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3148 return the cached definition in *COLOR. Otherwise, make a new
3149 entry in the cache and allocate the color. Value is false if color
3150 allocation failed. */
3153 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3155 struct xpm_cached_color
*p
;
3156 int h
= xpm_color_bucket (color_name
);
3158 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3159 if (strcmp (p
->name
, color_name
) == 0)
3164 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3167 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3169 p
= xpm_cache_color (f
, color_name
, color
, h
);
3171 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3172 with transparency, and it's useful. */
3173 else if (strcmp ("opaque", color_name
) == 0)
3175 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3176 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3177 p
= xpm_cache_color (f
, color_name
, color
, h
);
3184 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3185 CLOSURE is a pointer to the frame on which we allocate the
3186 color. Return in *COLOR the allocated color. Value is non-zero
3190 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3193 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3197 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3198 is a pointer to the frame on which we allocate the color. Value is
3199 non-zero if successful. */
3202 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3207 #endif /* ALLOC_XPM_COLORS */
3212 /* XPM library details. */
3214 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3215 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3216 xpm_XImage
**, XpmAttributes
*));
3217 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3218 xpm_XImage
**, XpmAttributes
*));
3219 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3222 init_xpm_functions (void)
3226 if (!(library
= w32_delayed_load (Qxpm
)))
3229 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3230 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3231 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3232 LOAD_IMGLIB_FN (library
, XImageFree
);
3236 #endif /* HAVE_NTGUI */
3239 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3240 for XPM images. Such a list must consist of conses whose car and
3244 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3246 while (CONSP (color_symbols
))
3248 Lisp_Object sym
= XCAR (color_symbols
);
3250 || !STRINGP (XCAR (sym
))
3251 || !STRINGP (XCDR (sym
)))
3253 color_symbols
= XCDR (color_symbols
);
3256 return NILP (color_symbols
);
3260 /* Value is true if OBJECT is a valid XPM image specification. */
3263 xpm_image_p (Lisp_Object object
)
3265 struct image_keyword fmt
[XPM_LAST
];
3266 memcpy (fmt
, xpm_format
, sizeof fmt
);
3267 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3268 /* Either `:file' or `:data' must be present. */
3269 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3270 /* Either no `:color-symbols' or it's a list of conses
3271 whose car and cdr are strings. */
3272 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3273 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3276 #endif /* HAVE_XPM || HAVE_NS */
3278 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3280 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3282 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3285 XpmAttributes attrs
;
3286 Pixmap bitmap
, mask
;
3288 memset (&attrs
, 0, sizeof attrs
);
3290 attrs
.visual
= FRAME_X_VISUAL (f
);
3291 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3292 attrs
.valuemask
|= XpmVisual
;
3293 attrs
.valuemask
|= XpmColormap
;
3295 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3296 (char **) bits
, &bitmap
, &mask
, &attrs
);
3297 if (rc
!= XpmSuccess
)
3299 XpmFreeAttributes (&attrs
);
3303 id
= x_allocate_bitmap_record (f
);
3304 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3305 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3306 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3307 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3308 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3309 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3310 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3311 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3313 XpmFreeAttributes (&attrs
);
3316 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3318 /* Load image IMG which will be displayed on frame F. Value is
3319 true if successful. */
3324 xpm_load (struct frame
*f
, struct image
*img
)
3327 XpmAttributes attrs
;
3328 Lisp_Object specified_file
, color_symbols
;
3331 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3332 #endif /* HAVE_NTGUI */
3334 /* Configure the XPM lib. Use the visual of frame F. Allocate
3335 close colors. Return colors allocated. */
3336 memset (&attrs
, 0, sizeof attrs
);
3339 attrs
.visual
= FRAME_X_VISUAL (f
);
3340 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3341 attrs
.valuemask
|= XpmVisual
;
3342 attrs
.valuemask
|= XpmColormap
;
3343 #endif /* HAVE_NTGUI */
3345 #ifdef ALLOC_XPM_COLORS
3346 /* Allocate colors with our own functions which handle
3347 failing color allocation more gracefully. */
3348 attrs
.color_closure
= f
;
3349 attrs
.alloc_color
= xpm_alloc_color
;
3350 attrs
.free_colors
= xpm_free_colors
;
3351 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3352 #else /* not ALLOC_XPM_COLORS */
3353 /* Let the XPM lib allocate colors. */
3354 attrs
.valuemask
|= XpmReturnAllocPixels
;
3355 #ifdef XpmAllocCloseColors
3356 attrs
.alloc_close_colors
= 1;
3357 attrs
.valuemask
|= XpmAllocCloseColors
;
3358 #else /* not XpmAllocCloseColors */
3359 attrs
.closeness
= 600;
3360 attrs
.valuemask
|= XpmCloseness
;
3361 #endif /* not XpmAllocCloseColors */
3362 #endif /* ALLOC_XPM_COLORS */
3364 /* If image specification contains symbolic color definitions, add
3365 these to `attrs'. */
3366 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3367 if (CONSP (color_symbols
))
3370 XpmColorSymbol
*xpm_syms
;
3373 attrs
.valuemask
|= XpmColorSymbols
;
3375 /* Count number of symbols. */
3376 attrs
.numsymbols
= 0;
3377 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3380 /* Allocate an XpmColorSymbol array. */
3381 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3382 xpm_syms
= alloca (size
);
3383 memset (xpm_syms
, 0, size
);
3384 attrs
.colorsymbols
= xpm_syms
;
3386 /* Fill the color symbol array. */
3387 for (tail
= color_symbols
, i
= 0;
3389 ++i
, tail
= XCDR (tail
))
3393 char *empty_string
= (char *) "";
3395 if (!CONSP (XCAR (tail
)))
3397 xpm_syms
[i
].name
= empty_string
;
3398 xpm_syms
[i
].value
= empty_string
;
3401 name
= XCAR (XCAR (tail
));
3402 color
= XCDR (XCAR (tail
));
3405 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3406 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3409 xpm_syms
[i
].name
= empty_string
;
3410 if (STRINGP (color
))
3412 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3413 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3416 xpm_syms
[i
].value
= empty_string
;
3420 /* Create a pixmap for the image, either from a file, or from a
3421 string buffer containing data in the same format as an XPM file. */
3422 #ifdef ALLOC_XPM_COLORS
3423 xpm_init_color_cache (f
, &attrs
);
3426 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3430 HDC frame_dc
= get_frame_dc (f
);
3431 hdc
= CreateCompatibleDC (frame_dc
);
3432 release_frame_dc (f
, frame_dc
);
3434 #endif /* HAVE_NTGUI */
3436 if (STRINGP (specified_file
))
3438 Lisp_Object file
= x_find_image_file (specified_file
);
3439 if (!STRINGP (file
))
3441 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3442 #ifdef ALLOC_XPM_COLORS
3443 xpm_free_color_cache ();
3449 /* XpmReadFileToPixmap is not available in the Windows port of
3450 libxpm. But XpmReadFileToImage almost does what we want. */
3451 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3452 &xpm_image
, &xpm_mask
,
3455 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3456 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3458 #endif /* HAVE_NTGUI */
3462 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3463 if (!STRINGP (buffer
))
3465 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3466 #ifdef ALLOC_XPM_COLORS
3467 xpm_free_color_cache ();
3472 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3473 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3474 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3475 &xpm_image
, &xpm_mask
,
3478 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3480 &img
->pixmap
, &img
->mask
,
3482 #endif /* HAVE_NTGUI */
3485 if (rc
== XpmSuccess
)
3487 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3488 img
->colors
= colors_in_color_table (&img
->ncolors
);
3489 #else /* not ALLOC_XPM_COLORS */
3493 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3494 plus some duplicate attributes. */
3495 if (xpm_image
&& xpm_image
->bitmap
)
3497 img
->pixmap
= xpm_image
->bitmap
;
3498 /* XImageFree in libXpm frees XImage struct without destroying
3499 the bitmap, which is what we want. */
3500 fn_XImageFree (xpm_image
);
3502 if (xpm_mask
&& xpm_mask
->bitmap
)
3504 /* The mask appears to be inverted compared with what we expect.
3505 TODO: invert our expectations. See other places where we
3506 have to invert bits because our idea of masks is backwards. */
3508 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3510 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3511 SelectObject (hdc
, old_obj
);
3513 img
->mask
= xpm_mask
->bitmap
;
3514 fn_XImageFree (xpm_mask
);
3519 #endif /* HAVE_NTGUI */
3521 /* Remember allocated colors. */
3522 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3523 img
->ncolors
= attrs
.nalloc_pixels
;
3524 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3526 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3527 #ifdef DEBUG_X_COLORS
3528 register_color (img
->colors
[i
]);
3531 #endif /* not ALLOC_XPM_COLORS */
3533 img
->width
= attrs
.width
;
3534 img
->height
= attrs
.height
;
3535 eassert (img
->width
> 0 && img
->height
> 0);
3537 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3539 fn_XpmFreeAttributes (&attrs
);
3541 XpmFreeAttributes (&attrs
);
3542 #endif /* HAVE_NTGUI */
3548 #endif /* HAVE_NTGUI */
3553 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3556 case XpmFileInvalid
:
3557 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3561 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3564 case XpmColorFailed
:
3565 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3569 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3574 #ifdef ALLOC_XPM_COLORS
3575 xpm_free_color_cache ();
3577 return rc
== XpmSuccess
;
3580 #endif /* HAVE_XPM */
3582 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3584 /* XPM support functions for NS where libxpm is not available.
3585 Only XPM version 3 (without any extensions) is supported. */
3587 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3589 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3590 const unsigned char *, int);
3591 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3593 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3594 const unsigned char *, int);
3596 /* Tokens returned from xpm_scan. */
3605 /* Scan an XPM data and return a character (< 256) or a token defined
3606 by enum xpm_token above. *S and END are the start (inclusive) and
3607 the end (exclusive) addresses of the data, respectively. Advance
3608 *S while scanning. If token is either XPM_TK_IDENT or
3609 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3610 length of the corresponding token, respectively. */
3613 xpm_scan (const unsigned char **s
,
3614 const unsigned char *end
,
3615 const unsigned char **beg
,
3622 /* Skip white-space. */
3623 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3626 /* gnus-pointer.xpm uses '-' in its identifier.
3627 sb-dir-plus.xpm uses '+' in its identifier. */
3628 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3632 && (c
= **s
, c_isalnum (c
)
3633 || c
== '_' || c
== '-' || c
== '+'))
3636 return XPM_TK_IDENT
;
3641 while (*s
< end
&& **s
!= '"')
3646 return XPM_TK_STRING
;
3650 if (*s
< end
&& **s
== '*')
3652 /* C-style comment. */
3656 while (*s
< end
&& *(*s
)++ != '*')
3659 while (*s
< end
&& **s
!= '/');
3673 /* Functions for color table lookup in XPM data. A key is a string
3674 specifying the color of each pixel in XPM data. A value is either
3675 an integer that specifies a pixel color, Qt that specifies
3676 transparency, or Qnil for the unspecified color. If the length of
3677 the key string is one, a vector is used as a table. Otherwise, a
3678 hash table is used. */
3681 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3682 const unsigned char *,
3685 Lisp_Object (**get_func
) (Lisp_Object
,
3686 const unsigned char *,
3689 *put_func
= xpm_put_color_table_v
;
3690 *get_func
= xpm_get_color_table_v
;
3691 return Fmake_vector (make_number (256), Qnil
);
3695 xpm_put_color_table_v (Lisp_Object color_table
,
3696 const unsigned char *chars_start
,
3700 ASET (color_table
, *chars_start
, color
);
3704 xpm_get_color_table_v (Lisp_Object color_table
,
3705 const unsigned char *chars_start
,
3708 return AREF (color_table
, *chars_start
);
3712 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3713 const unsigned char *,
3716 Lisp_Object (**get_func
) (Lisp_Object
,
3717 const unsigned char *,
3720 *put_func
= xpm_put_color_table_h
;
3721 *get_func
= xpm_get_color_table_h
;
3722 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3723 make_float (DEFAULT_REHASH_SIZE
),
3724 make_float (DEFAULT_REHASH_THRESHOLD
),
3729 xpm_put_color_table_h (Lisp_Object color_table
,
3730 const unsigned char *chars_start
,
3734 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3735 EMACS_UINT hash_code
;
3736 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3738 hash_lookup (table
, chars
, &hash_code
);
3739 hash_put (table
, chars
, color
, hash_code
);
3743 xpm_get_color_table_h (Lisp_Object color_table
,
3744 const unsigned char *chars_start
,
3747 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3749 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3751 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3754 enum xpm_color_key
{
3762 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3765 xpm_str_to_color_key (const char *s
)
3770 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3772 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3778 xpm_load_image (struct frame
*f
,
3780 const unsigned char *contents
,
3781 const unsigned char *end
)
3783 const unsigned char *s
= contents
, *beg
, *str
;
3784 unsigned char buffer
[BUFSIZ
];
3785 int width
, height
, x
, y
;
3786 int num_colors
, chars_per_pixel
;
3789 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3790 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3791 Lisp_Object frame
, color_symbols
, color_table
;
3794 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3797 LA1 = xpm_scan (&s, end, &beg, &len)
3799 #define expect(TOKEN) \
3800 if (LA1 != (TOKEN)) \
3805 #define expect_ident(IDENT) \
3806 if (LA1 == XPM_TK_IDENT \
3807 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3812 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3816 expect_ident ("static");
3817 expect_ident ("char");
3819 expect (XPM_TK_IDENT
);
3824 expect (XPM_TK_STRING
);
3827 memcpy (buffer
, beg
, len
);
3829 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3830 &num_colors
, &chars_per_pixel
) != 4
3831 || width
<= 0 || height
<= 0
3832 || num_colors
<= 0 || chars_per_pixel
<= 0)
3835 if (!check_image_size (f
, width
, height
))
3837 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3841 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3842 &ximg
, &img
->pixmap
)
3844 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3845 &mask_img
, &img
->mask
)
3849 image_error ("Image too large", Qnil
, Qnil
);
3855 XSETFRAME (frame
, f
);
3856 if (!NILP (Fxw_display_color_p (frame
)))
3857 best_key
= XPM_COLOR_KEY_C
;
3858 else if (!NILP (Fx_display_grayscale_p (frame
)))
3859 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3860 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3862 best_key
= XPM_COLOR_KEY_M
;
3864 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3865 if (chars_per_pixel
== 1)
3866 color_table
= xpm_make_color_table_v (&put_color_table
,
3869 color_table
= xpm_make_color_table_h (&put_color_table
,
3872 while (num_colors
-- > 0)
3874 char *color
, *max_color
;
3875 int key
, next_key
, max_key
= 0;
3876 Lisp_Object symbol_color
= Qnil
, color_val
;
3879 expect (XPM_TK_STRING
);
3880 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3882 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3883 buffer
[len
- chars_per_pixel
] = '\0';
3885 str
= strtok (buffer
, " \t");
3888 key
= xpm_str_to_color_key (str
);
3893 color
= strtok (NULL
, " \t");
3897 while ((str
= strtok (NULL
, " \t")) != NULL
)
3899 next_key
= xpm_str_to_color_key (str
);
3902 color
[strlen (color
)] = ' ';
3905 if (key
== XPM_COLOR_KEY_S
)
3907 if (NILP (symbol_color
))
3908 symbol_color
= build_string (color
);
3910 else if (max_key
< key
&& key
<= best_key
)
3920 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3922 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3924 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3926 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3928 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
3930 color_val
= make_number (cdef
.pixel
);
3933 if (NILP (color_val
) && max_key
> 0)
3935 if (xstrcasecmp (max_color
, "None") == 0)
3937 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3938 color_val
= make_number (cdef
.pixel
);
3940 if (!NILP (color_val
))
3941 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3946 for (y
= 0; y
< height
; y
++)
3948 expect (XPM_TK_STRING
);
3950 if (len
< width
* chars_per_pixel
)
3952 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3954 Lisp_Object color_val
=
3955 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
3957 XPutPixel (ximg
, x
, y
,
3958 (INTEGERP (color_val
) ? XINT (color_val
)
3959 : FRAME_FOREGROUND_PIXEL (f
)));
3961 XPutPixel (mask_img
, x
, y
,
3962 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
3963 : (have_mask
= 1, PIX_MASK_RETAIN
)));
3965 if (EQ (color_val
, Qt
))
3966 ns_set_alpha (ximg
, x
, y
, 0);
3974 img
->height
= height
;
3976 /* Maybe fill in the background field while we have ximg handy. */
3977 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
3978 IMAGE_BACKGROUND (img
, f
, ximg
);
3980 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
3981 x_destroy_x_image (ximg
);
3985 /* Fill in the background_transparent field while we have the
3987 image_background_transparent (img
, f
, mask_img
);
3989 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
3990 x_destroy_x_image (mask_img
);
3994 x_destroy_x_image (mask_img
);
3995 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
3996 img
->mask
= NO_PIXMAP
;
4002 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4003 x_destroy_x_image (ximg
);
4004 x_destroy_x_image (mask_img
);
4005 x_clear_image (f
, img
);
4014 xpm_load (struct frame
*f
,
4018 Lisp_Object file_name
;
4020 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4021 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4022 if (STRINGP (file_name
))
4025 unsigned char *contents
;
4028 file
= x_find_image_file (file_name
);
4029 if (!STRINGP (file
))
4031 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4035 contents
= slurp_file (SSDATA (file
), &size
);
4036 if (contents
== NULL
)
4038 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4042 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4049 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4050 if (!STRINGP (data
))
4052 image_error ("Invalid image data `%s'", data
, Qnil
);
4055 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4056 SDATA (data
) + SBYTES (data
));
4062 #endif /* HAVE_NS && !HAVE_XPM */
4066 /***********************************************************************
4068 ***********************************************************************/
4070 #ifdef COLOR_TABLE_SUPPORT
4072 /* An entry in the color table mapping an RGB color to a pixel color. */
4077 unsigned long pixel
;
4079 /* Next in color table collision list. */
4080 struct ct_color
*next
;
4083 /* The bucket vector size to use. Must be prime. */
4087 /* Value is a hash of the RGB color given by R, G, and B. */
4089 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4091 /* The color hash table. */
4093 static struct ct_color
**ct_table
;
4095 /* Number of entries in the color table. */
4097 static int ct_colors_allocated
;
4100 ct_colors_allocated_max
=
4102 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4105 /* Initialize the color table. */
4108 init_color_table (void)
4110 int size
= CT_SIZE
* sizeof (*ct_table
);
4111 ct_table
= xzalloc (size
);
4112 ct_colors_allocated
= 0;
4116 /* Free memory associated with the color table. */
4119 free_color_table (void)
4122 struct ct_color
*p
, *next
;
4124 for (i
= 0; i
< CT_SIZE
; ++i
)
4125 for (p
= ct_table
[i
]; p
; p
= next
)
4136 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4137 entry for that color already is in the color table, return the
4138 pixel color of that entry. Otherwise, allocate a new color for R,
4139 G, B, and make an entry in the color table. */
4141 static unsigned long
4142 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4144 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4145 int i
= hash
% CT_SIZE
;
4147 Display_Info
*dpyinfo
;
4149 /* Handle TrueColor visuals specially, which improves performance by
4150 two orders of magnitude. Freeing colors on TrueColor visuals is
4151 a nop, and pixel colors specify RGB values directly. See also
4152 the Xlib spec, chapter 3.1. */
4153 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4154 if (dpyinfo
->red_bits
> 0)
4156 unsigned long pr
, pg
, pb
;
4158 /* Apply gamma-correction like normal color allocation does. */
4162 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4163 gamma_correct (f
, &color
);
4164 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4167 /* Scale down RGB values to the visual's bits per RGB, and shift
4168 them to the right position in the pixel color. Note that the
4169 original RGB values are 16-bit values, as usual in X. */
4170 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4171 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4172 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4174 /* Assemble the pixel color. */
4175 return pr
| pg
| pb
;
4178 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4179 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4185 #ifdef HAVE_X_WINDOWS
4193 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4194 return FRAME_FOREGROUND_PIXEL (f
);
4196 #ifdef HAVE_X_WINDOWS
4201 cmap
= FRAME_X_COLORMAP (f
);
4202 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4205 ++ct_colors_allocated
;
4206 p
= xmalloc (sizeof *p
);
4210 p
->pixel
= color
.pixel
;
4211 p
->next
= ct_table
[i
];
4215 return FRAME_FOREGROUND_PIXEL (f
);
4219 color
= PALETTERGB (r
, g
, b
);
4221 color
= RGB_TO_ULONG (r
, g
, b
);
4222 #endif /* HAVE_NTGUI */
4223 ++ct_colors_allocated
;
4224 p
= xmalloc (sizeof *p
);
4229 p
->next
= ct_table
[i
];
4231 #endif /* HAVE_X_WINDOWS */
4239 /* Look up pixel color PIXEL which is used on frame F in the color
4240 table. If not already present, allocate it. Value is PIXEL. */
4242 static unsigned long
4243 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4245 int i
= pixel
% CT_SIZE
;
4248 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4249 if (p
->pixel
== pixel
)
4258 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4259 return FRAME_FOREGROUND_PIXEL (f
);
4261 #ifdef HAVE_X_WINDOWS
4262 cmap
= FRAME_X_COLORMAP (f
);
4263 color
.pixel
= pixel
;
4264 x_query_color (f
, &color
);
4265 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4268 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4269 color
.pixel
= pixel
;
4270 XQueryColor (NULL
, cmap
, &color
);
4271 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4273 #endif /* HAVE_X_WINDOWS */
4277 ++ct_colors_allocated
;
4279 p
= xmalloc (sizeof *p
);
4284 p
->next
= ct_table
[i
];
4288 return FRAME_FOREGROUND_PIXEL (f
);
4294 /* Value is a vector of all pixel colors contained in the color table,
4295 allocated via xmalloc. Set *N to the number of colors. */
4297 static unsigned long *
4298 colors_in_color_table (int *n
)
4302 unsigned long *colors
;
4304 if (ct_colors_allocated
== 0)
4311 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4312 *n
= ct_colors_allocated
;
4314 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4315 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4316 colors
[j
++] = p
->pixel
;
4322 #else /* COLOR_TABLE_SUPPORT */
4324 static unsigned long
4325 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4327 unsigned long pixel
;
4330 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4331 #endif /* HAVE_NTGUI */
4334 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4335 #endif /* HAVE_NS */
4340 init_color_table (void)
4343 #endif /* COLOR_TABLE_SUPPORT */
4346 /***********************************************************************
4348 ***********************************************************************/
4350 /* Edge detection matrices for different edge-detection
4353 static int emboss_matrix
[9] = {
4355 2, -1, 0, /* y - 1 */
4357 0, 1, -2 /* y + 1 */
4360 static int laplace_matrix
[9] = {
4362 1, 0, 0, /* y - 1 */
4364 0, 0, -1 /* y + 1 */
4367 /* Value is the intensity of the color whose red/green/blue values
4370 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4373 /* On frame F, return an array of XColor structures describing image
4374 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4375 means also fill the red/green/blue members of the XColor
4376 structures. Value is a pointer to the array of XColors structures,
4377 allocated with xmalloc; it must be freed by the caller. */
4380 x_to_xcolors (struct frame
*f
, struct image
*img
, bool rgb_p
)
4384 XImagePtr_or_DC ximg
;
4388 #endif /* HAVE_NTGUI */
4390 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4391 memory_full (SIZE_MAX
);
4392 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4395 /* Get the X image IMG->pixmap. */
4396 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4397 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4399 /* Load the image into a memory device context. */
4400 hdc
= get_frame_dc (f
);
4401 ximg
= CreateCompatibleDC (hdc
);
4402 release_frame_dc (f
, hdc
);
4403 prev
= SelectObject (ximg
, img
->pixmap
);
4404 #endif /* HAVE_NTGUI */
4406 /* Fill the `pixel' members of the XColor array. I wished there
4407 were an easy and portable way to circumvent XGetPixel. */
4409 for (y
= 0; y
< img
->height
; ++y
)
4411 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4413 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4414 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4416 x_query_colors (f
, row
, img
->width
);
4420 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4422 /* W32_TODO: palette support needed here? */
4423 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4426 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4427 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4428 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4431 #endif /* HAVE_X_WINDOWS */
4434 Destroy_Image (ximg
, prev
);
4441 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4442 created with CreateDIBSection, with the pointer to the bit values
4443 stored in ximg->data. */
4446 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4448 int width
= ximg
->info
.bmiHeader
.biWidth
;
4449 unsigned char * pixel
;
4451 /* True color images. */
4452 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4454 int rowbytes
= width
* 3;
4455 /* Ensure scanlines are aligned on 4 byte boundaries. */
4457 rowbytes
+= 4 - (rowbytes
% 4);
4459 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4460 /* Windows bitmaps are in BGR order. */
4461 *pixel
= GetBValue (color
);
4462 *(pixel
+ 1) = GetGValue (color
);
4463 *(pixel
+ 2) = GetRValue (color
);
4465 /* Monochrome images. */
4466 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4468 int rowbytes
= width
/ 8;
4469 /* Ensure scanlines are aligned on 4 byte boundaries. */
4471 rowbytes
+= 4 - (rowbytes
% 4);
4472 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4473 /* Filter out palette info. */
4474 if (color
& 0x00ffffff)
4475 *pixel
= *pixel
| (1 << x
% 8);
4477 *pixel
= *pixel
& ~(1 << x
% 8);
4480 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4483 #endif /* HAVE_NTGUI */
4485 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4486 RGB members are set. F is the frame on which this all happens.
4487 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4490 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4493 XImagePtr oimg
= NULL
;
4497 init_color_table ();
4499 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4502 for (y
= 0; y
< img
->height
; ++y
)
4503 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4505 unsigned long pixel
;
4506 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4507 XPutPixel (oimg
, x
, y
, pixel
);
4511 x_clear_image_1 (f
, img
, 1, 0, 1);
4513 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4514 x_destroy_x_image (oimg
);
4515 img
->pixmap
= pixmap
;
4516 #ifdef COLOR_TABLE_SUPPORT
4517 img
->colors
= colors_in_color_table (&img
->ncolors
);
4518 free_color_table ();
4519 #endif /* COLOR_TABLE_SUPPORT */
4523 /* On frame F, perform edge-detection on image IMG.
4525 MATRIX is a nine-element array specifying the transformation
4526 matrix. See emboss_matrix for an example.
4528 COLOR_ADJUST is a color adjustment added to each pixel of the
4532 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4534 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4538 for (i
= sum
= 0; i
< 9; ++i
)
4539 sum
+= eabs (matrix
[i
]);
4541 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4543 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4544 memory_full (SIZE_MAX
);
4545 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4547 for (y
= 0; y
< img
->height
; ++y
)
4549 p
= COLOR (new, 0, y
);
4550 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4551 p
= COLOR (new, img
->width
- 1, y
);
4552 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4555 for (x
= 1; x
< img
->width
- 1; ++x
)
4557 p
= COLOR (new, x
, 0);
4558 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4559 p
= COLOR (new, x
, img
->height
- 1);
4560 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4563 for (y
= 1; y
< img
->height
- 1; ++y
)
4565 p
= COLOR (new, 1, y
);
4567 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4569 int r
, g
, b
, yy
, xx
;
4572 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4573 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4576 XColor
*t
= COLOR (colors
, xx
, yy
);
4577 r
+= matrix
[i
] * t
->red
;
4578 g
+= matrix
[i
] * t
->green
;
4579 b
+= matrix
[i
] * t
->blue
;
4582 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4583 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4584 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4585 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4590 x_from_xcolors (f
, img
, new);
4596 /* Perform the pre-defined `emboss' edge-detection on image IMG
4600 x_emboss (struct frame
*f
, struct image
*img
)
4602 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4606 /* Transform image IMG which is used on frame F with a Laplace
4607 edge-detection algorithm. The result is an image that can be used
4608 to draw disabled buttons, for example. */
4611 x_laplace (struct frame
*f
, struct image
*img
)
4613 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4617 /* Perform edge-detection on image IMG on frame F, with specified
4618 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4620 MATRIX must be either
4622 - a list of at least 9 numbers in row-major form
4623 - a vector of at least 9 numbers
4625 COLOR_ADJUST nil means use a default; otherwise it must be a
4629 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4630 Lisp_Object color_adjust
)
4638 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4639 ++i
, matrix
= XCDR (matrix
))
4640 trans
[i
] = XFLOATINT (XCAR (matrix
));
4642 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4644 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4645 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4648 if (NILP (color_adjust
))
4649 color_adjust
= make_number (0xffff / 2);
4651 if (i
== 9 && NUMBERP (color_adjust
))
4652 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4656 /* Transform image IMG on frame F so that it looks disabled. */
4659 x_disable_image (struct frame
*f
, struct image
*img
)
4661 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4663 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4665 int n_planes
= dpyinfo
->n_planes
;
4666 #endif /* HAVE_NTGUI */
4670 /* Color (or grayscale). Convert to gray, and equalize. Just
4671 drawing such images with a stipple can look very odd, so
4672 we're using this method instead. */
4673 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4675 const int h
= 15000;
4676 const int l
= 30000;
4678 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4682 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4683 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4684 p
->red
= p
->green
= p
->blue
= i2
;
4687 x_from_xcolors (f
, img
, colors
);
4690 /* Draw a cross over the disabled image, if we must or if we
4692 if (n_planes
< 2 || cross_disabled_images
)
4695 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4697 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4699 Display
*dpy
= FRAME_X_DISPLAY (f
);
4700 GC gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4701 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4702 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4703 img
->width
- 1, img
->height
- 1);
4704 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4710 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4711 XSetForeground (dpy
, gc
, MaskForeground (f
));
4712 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4713 img
->width
- 1, img
->height
- 1);
4714 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4718 #endif /* !HAVE_NS */
4723 hdc
= get_frame_dc (f
);
4724 bmpdc
= CreateCompatibleDC (hdc
);
4725 release_frame_dc (f
, hdc
);
4727 prev
= SelectObject (bmpdc
, img
->pixmap
);
4729 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4730 MoveToEx (bmpdc
, 0, 0, NULL
);
4731 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4732 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4733 LineTo (bmpdc
, img
->width
- 1, 0);
4737 SelectObject (bmpdc
, img
->mask
);
4738 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4739 MoveToEx (bmpdc
, 0, 0, NULL
);
4740 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4741 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4742 LineTo (bmpdc
, img
->width
- 1, 0);
4744 SelectObject (bmpdc
, prev
);
4746 #endif /* HAVE_NTGUI */
4751 /* Build a mask for image IMG which is used on frame F. FILE is the
4752 name of an image file, for error messages. HOW determines how to
4753 determine the background color of IMG. If it is a list '(R G B)',
4754 with R, G, and B being integers >= 0, take that as the color of the
4755 background. Otherwise, determine the background color of IMG
4759 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4761 XImagePtr_or_DC ximg
;
4769 #endif /* HAVE_NTGUI */
4771 bool rc
, use_img_background
;
4772 unsigned long bg
= 0;
4776 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4777 img
->mask
= NO_PIXMAP
;
4778 img
->background_transparent_valid
= 0;
4783 /* Create an image and pixmap serving as mask. */
4784 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4785 &mask_img
, &img
->mask
);
4788 #endif /* !HAVE_NS */
4790 /* Get the X image of IMG->pixmap. */
4791 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4792 img
->width
, img
->height
,
4795 /* Create the bit array serving as mask. */
4796 row_width
= (img
->width
+ 7) / 8;
4797 mask_img
= xzalloc (row_width
* img
->height
);
4799 /* Create a memory device context for IMG->pixmap. */
4800 frame_dc
= get_frame_dc (f
);
4801 ximg
= CreateCompatibleDC (frame_dc
);
4802 release_frame_dc (f
, frame_dc
);
4803 prev
= SelectObject (ximg
, img
->pixmap
);
4804 #endif /* HAVE_NTGUI */
4806 /* Determine the background color of ximg. If HOW is `(R G B)'
4807 take that as color. Otherwise, use the image's background color. */
4808 use_img_background
= 1;
4814 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4816 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4820 if (i
== 3 && NILP (how
))
4822 char color_name
[30];
4823 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4826 0x00ffffff & /* Filter out palette info. */
4827 #endif /* HAVE_NTGUI */
4828 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4829 use_img_background
= 0;
4833 if (use_img_background
)
4834 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4836 /* Set all bits in mask_img to 1 whose color in ximg is different
4837 from the background color bg. */
4839 for (y
= 0; y
< img
->height
; ++y
)
4840 for (x
= 0; x
< img
->width
; ++x
)
4842 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4843 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4845 if (XGetPixel (ximg
, x
, y
) == bg
)
4846 ns_set_alpha (ximg
, x
, y
, 0);
4847 #endif /* HAVE_NS */
4849 /* Fill in the background_transparent field while we have the mask handy. */
4850 image_background_transparent (img
, f
, mask_img
);
4852 /* Put mask_img into img->mask. */
4853 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4854 x_destroy_x_image (mask_img
);
4855 #endif /* !HAVE_NS */
4857 for (y
= 0; y
< img
->height
; ++y
)
4858 for (x
= 0; x
< img
->width
; ++x
)
4860 COLORREF p
= GetPixel (ximg
, x
, y
);
4862 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4865 /* Create the mask image. */
4866 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4868 /* Fill in the background_transparent field while we have the mask handy. */
4869 SelectObject (ximg
, img
->mask
);
4870 image_background_transparent (img
, f
, ximg
);
4872 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4874 #endif /* HAVE_NTGUI */
4876 Destroy_Image (ximg
, prev
);
4880 /***********************************************************************
4881 PBM (mono, gray, color)
4882 ***********************************************************************/
4884 static bool pbm_image_p (Lisp_Object object
);
4885 static bool pbm_load (struct frame
*f
, struct image
*img
);
4887 /* The symbol `pbm' identifying images of this type. */
4889 static Lisp_Object Qpbm
;
4891 /* Indices of image specification fields in gs_format, below. */
4893 enum pbm_keyword_index
4909 /* Vector of image_keyword structures describing the format
4910 of valid user-defined image specifications. */
4912 static const struct image_keyword pbm_format
[PBM_LAST
] =
4914 {":type", IMAGE_SYMBOL_VALUE
, 1},
4915 {":file", IMAGE_STRING_VALUE
, 0},
4916 {":data", IMAGE_STRING_VALUE
, 0},
4917 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4918 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
4919 {":relief", IMAGE_INTEGER_VALUE
, 0},
4920 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4921 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4922 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4923 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4924 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4927 /* Structure describing the image type `pbm'. */
4929 static struct image_type pbm_type
=
4940 /* Return true if OBJECT is a valid PBM image specification. */
4943 pbm_image_p (Lisp_Object object
)
4945 struct image_keyword fmt
[PBM_LAST
];
4947 memcpy (fmt
, pbm_format
, sizeof fmt
);
4949 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4952 /* Must specify either :data or :file. */
4953 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
4957 /* Scan a decimal number from *S and return it. Advance *S while
4958 reading the number. END is the end of the string. Value is -1 at
4962 pbm_scan_number (unsigned char **s
, unsigned char *end
)
4964 int c
= 0, val
= -1;
4968 /* Skip white-space. */
4969 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
4974 /* Skip comment to end of line. */
4975 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
4978 else if (c_isdigit (c
))
4980 /* Read decimal number. */
4982 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
4983 val
= 10 * val
+ c
- '0';
4995 #if 0 /* Unused. ++kfs */
4997 /* Read FILE into memory. Value is a pointer to a buffer allocated
4998 with xmalloc holding FILE's contents. Value is null if an error
4999 occurred. *SIZE is set to the size of the file. */
5002 pbm_read_file (Lisp_Object file
, int *size
)
5008 if (stat (SDATA (file
), &st
) == 0
5009 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5010 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5011 && (buf
= xmalloc (st
.st_size
),
5012 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5031 #endif /* HAVE_NTGUI */
5033 /* Load PBM image IMG for use on frame F. */
5036 pbm_load (struct frame
*f
, struct image
*img
)
5040 int width
, height
, max_color_idx
= 0;
5042 Lisp_Object file
, specified_file
;
5043 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5044 unsigned char *contents
= NULL
;
5045 unsigned char *end
, *p
;
5048 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5050 if (STRINGP (specified_file
))
5052 file
= x_find_image_file (specified_file
);
5053 if (!STRINGP (file
))
5055 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5059 contents
= slurp_file (SSDATA (file
), &size
);
5060 if (contents
== NULL
)
5062 image_error ("Error reading `%s'", file
, Qnil
);
5067 end
= contents
+ size
;
5072 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5073 if (!STRINGP (data
))
5075 image_error ("Invalid image data `%s'", data
, Qnil
);
5079 end
= p
+ SBYTES (data
);
5082 /* Check magic number. */
5083 if (end
- p
< 2 || *p
++ != 'P')
5085 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5094 raw_p
= 0, type
= PBM_MONO
;
5098 raw_p
= 0, type
= PBM_GRAY
;
5102 raw_p
= 0, type
= PBM_COLOR
;
5106 raw_p
= 1, type
= PBM_MONO
;
5110 raw_p
= 1, type
= PBM_GRAY
;
5114 raw_p
= 1, type
= PBM_COLOR
;
5118 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5122 /* Read width, height, maximum color-component. Characters
5123 starting with `#' up to the end of a line are ignored. */
5124 width
= pbm_scan_number (&p
, end
);
5125 height
= pbm_scan_number (&p
, end
);
5127 if (type
!= PBM_MONO
)
5129 max_color_idx
= pbm_scan_number (&p
, end
);
5130 if (max_color_idx
> 65535 || max_color_idx
< 0)
5132 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5137 if (!check_image_size (f
, width
, height
))
5139 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5143 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5144 &ximg
, &img
->pixmap
))
5147 /* Initialize the color hash table. */
5148 init_color_table ();
5150 if (type
== PBM_MONO
)
5153 struct image_keyword fmt
[PBM_LAST
];
5154 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5155 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5157 /* Parse the image specification. */
5158 memcpy (fmt
, pbm_format
, sizeof fmt
);
5159 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5161 /* Get foreground and background colors, maybe allocate colors. */
5162 if (fmt
[PBM_FOREGROUND
].count
5163 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5164 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5165 if (fmt
[PBM_BACKGROUND
].count
5166 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5168 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5169 img
->background
= bg
;
5170 img
->background_valid
= 1;
5173 for (y
= 0; y
< height
; ++y
)
5174 for (x
= 0; x
< width
; ++x
)
5182 x_destroy_x_image (ximg
);
5183 x_clear_image (f
, img
);
5184 image_error ("Invalid image size in image `%s'",
5194 g
= pbm_scan_number (&p
, end
);
5196 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5201 int expected_size
= height
* width
;
5202 if (max_color_idx
> 255)
5204 if (type
== PBM_COLOR
)
5207 if (raw_p
&& p
+ expected_size
> end
)
5209 x_destroy_x_image (ximg
);
5210 x_clear_image (f
, img
);
5211 image_error ("Invalid image size in image `%s'",
5216 for (y
= 0; y
< height
; ++y
)
5217 for (x
= 0; x
< width
; ++x
)
5221 if (type
== PBM_GRAY
&& raw_p
)
5224 if (max_color_idx
> 255)
5225 r
= g
= b
= r
* 256 + *p
++;
5227 else if (type
== PBM_GRAY
)
5228 r
= g
= b
= pbm_scan_number (&p
, end
);
5232 if (max_color_idx
> 255)
5235 if (max_color_idx
> 255)
5238 if (max_color_idx
> 255)
5243 r
= pbm_scan_number (&p
, end
);
5244 g
= pbm_scan_number (&p
, end
);
5245 b
= pbm_scan_number (&p
, end
);
5248 if (r
< 0 || g
< 0 || b
< 0)
5250 x_destroy_x_image (ximg
);
5251 image_error ("Invalid pixel value in image `%s'",
5256 /* RGB values are now in the range 0..max_color_idx.
5257 Scale this to the range 0..0xffff supported by X. */
5258 r
= (double) r
* 65535 / max_color_idx
;
5259 g
= (double) g
* 65535 / max_color_idx
;
5260 b
= (double) b
* 65535 / max_color_idx
;
5261 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5265 #ifdef COLOR_TABLE_SUPPORT
5266 /* Store in IMG->colors the colors allocated for the image, and
5267 free the color table. */
5268 img
->colors
= colors_in_color_table (&img
->ncolors
);
5269 free_color_table ();
5270 #endif /* COLOR_TABLE_SUPPORT */
5273 img
->height
= height
;
5275 /* Maybe fill in the background field while we have ximg handy. */
5277 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5278 /* Casting avoids a GCC warning. */
5279 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5281 /* Put the image into a pixmap. */
5282 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5283 x_destroy_x_image (ximg
);
5285 /* X and W32 versions did it here, MAC version above. ++kfs
5287 img->height = height; */
5294 /***********************************************************************
5296 ***********************************************************************/
5298 #if defined (HAVE_PNG) || defined (HAVE_NS)
5300 /* Function prototypes. */
5302 static bool png_image_p (Lisp_Object object
);
5303 static bool png_load (struct frame
*f
, struct image
*img
);
5305 /* The symbol `png' identifying images of this type. */
5307 static Lisp_Object Qpng
;
5309 /* Indices of image specification fields in png_format, below. */
5311 enum png_keyword_index
5326 /* Vector of image_keyword structures describing the format
5327 of valid user-defined image specifications. */
5329 static const struct image_keyword png_format
[PNG_LAST
] =
5331 {":type", IMAGE_SYMBOL_VALUE
, 1},
5332 {":data", IMAGE_STRING_VALUE
, 0},
5333 {":file", IMAGE_STRING_VALUE
, 0},
5334 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5335 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5336 {":relief", IMAGE_INTEGER_VALUE
, 0},
5337 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5338 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5339 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5340 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5344 static bool init_png_functions (void);
5346 #define init_png_functions NULL
5349 /* Structure describing the image type `png'. */
5351 static struct image_type png_type
=
5361 /* Return true if OBJECT is a valid PNG image specification. */
5364 png_image_p (Lisp_Object object
)
5366 struct image_keyword fmt
[PNG_LAST
];
5367 memcpy (fmt
, png_format
, sizeof fmt
);
5369 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5372 /* Must specify either the :data or :file keyword. */
5373 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5376 #endif /* HAVE_PNG || HAVE_NS */
5382 /* PNG library details. */
5384 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5385 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5386 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5387 png_error_ptr
, png_error_ptr
));
5388 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5389 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5390 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5391 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5392 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5393 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5394 png_uint_32
*, png_uint_32
*,
5395 int *, int *, int *, int *, int *));
5396 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5397 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5398 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5399 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5400 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5402 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5403 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5404 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5405 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5406 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5407 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5408 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5410 #if (PNG_LIBPNG_VER >= 10500)
5411 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5412 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5413 #endif /* libpng version >= 1.5 */
5416 init_png_functions (void)
5420 if (!(library
= w32_delayed_load (Qpng
)))
5423 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5424 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5425 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5426 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5427 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5428 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5429 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5430 LOAD_IMGLIB_FN (library
, png_read_info
);
5431 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5432 LOAD_IMGLIB_FN (library
, png_get_valid
);
5433 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5434 LOAD_IMGLIB_FN (library
, png_set_expand
);
5435 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5436 LOAD_IMGLIB_FN (library
, png_set_background
);
5437 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5438 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5439 LOAD_IMGLIB_FN (library
, png_get_channels
);
5440 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5441 LOAD_IMGLIB_FN (library
, png_read_image
);
5442 LOAD_IMGLIB_FN (library
, png_read_end
);
5443 LOAD_IMGLIB_FN (library
, png_error
);
5445 #if (PNG_LIBPNG_VER >= 10500)
5446 LOAD_IMGLIB_FN (library
, png_longjmp
);
5447 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5448 #endif /* libpng version >= 1.5 */
5454 #define fn_png_get_io_ptr png_get_io_ptr
5455 #define fn_png_sig_cmp png_sig_cmp
5456 #define fn_png_create_read_struct png_create_read_struct
5457 #define fn_png_create_info_struct png_create_info_struct
5458 #define fn_png_destroy_read_struct png_destroy_read_struct
5459 #define fn_png_set_read_fn png_set_read_fn
5460 #define fn_png_set_sig_bytes png_set_sig_bytes
5461 #define fn_png_read_info png_read_info
5462 #define fn_png_get_IHDR png_get_IHDR
5463 #define fn_png_get_valid png_get_valid
5464 #define fn_png_set_strip_16 png_set_strip_16
5465 #define fn_png_set_expand png_set_expand
5466 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5467 #define fn_png_set_background png_set_background
5468 #define fn_png_get_bKGD png_get_bKGD
5469 #define fn_png_read_update_info png_read_update_info
5470 #define fn_png_get_channels png_get_channels
5471 #define fn_png_get_rowbytes png_get_rowbytes
5472 #define fn_png_read_image png_read_image
5473 #define fn_png_read_end png_read_end
5474 #define fn_png_error png_error
5476 #if (PNG_LIBPNG_VER >= 10500)
5477 #define fn_png_longjmp png_longjmp
5478 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5479 #endif /* libpng version >= 1.5 */
5481 #endif /* HAVE_NTGUI */
5483 /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp.
5484 Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp
5485 substitute may munge the signal mask, but that should be OK here.
5486 MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in
5487 the system header setjmp.h; don't mess up that. */
5488 #ifndef HAVE__SETJMP
5489 # define _setjmp(j) setjmp (j)
5490 # define _longjmp longjmp
5493 #if (PNG_LIBPNG_VER < 10500)
5494 #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1))
5495 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5497 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5498 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5499 #define PNG_JMPBUF(ptr) \
5500 (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf)))
5503 /* Error and warning handlers installed when the PNG library
5506 static _Noreturn
void
5507 my_png_error (png_struct
*png_ptr
, const char *msg
)
5509 eassert (png_ptr
!= NULL
);
5510 /* Avoid compiler warning about deprecated direct access to
5511 png_ptr's fields in libpng versions 1.4.x. */
5512 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5513 PNG_LONGJMP (png_ptr
);
5518 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5520 eassert (png_ptr
!= NULL
);
5521 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5524 /* Memory source for PNG decoding. */
5526 struct png_memory_storage
5528 unsigned char *bytes
; /* The data */
5529 ptrdiff_t len
; /* How big is it? */
5530 ptrdiff_t index
; /* Where are we? */
5534 /* Function set as reader function when reading PNG image from memory.
5535 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5536 bytes from the input to DATA. */
5539 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5541 struct png_memory_storage
*tbr
5542 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5544 if (length
> tbr
->len
- tbr
->index
)
5545 fn_png_error (png_ptr
, "Read error");
5547 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5548 tbr
->index
= tbr
->index
+ length
;
5552 /* Function set as reader function when reading PNG image from a file.
5553 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5554 bytes from the input to DATA. */
5557 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5559 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5561 if (fread (data
, 1, length
, fp
) < length
)
5562 fn_png_error (png_ptr
, "Read error");
5566 /* Load PNG image IMG for use on frame F. Value is true if
5569 struct png_load_context
5571 /* These are members so that longjmp doesn't munge local variables. */
5572 png_struct
*png_ptr
;
5581 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5583 Lisp_Object file
, specified_file
;
5584 Lisp_Object specified_data
;
5587 XImagePtr ximg
, mask_img
= NULL
;
5588 png_struct
*png_ptr
;
5589 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5592 png_byte
*pixels
= NULL
;
5593 png_byte
**rows
= NULL
;
5594 png_uint_32 width
, height
;
5595 int bit_depth
, color_type
, interlace_type
;
5597 png_uint_32 row_bytes
;
5599 struct png_memory_storage tbr
; /* Data to be read */
5601 /* Find out what file to load. */
5602 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5603 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5605 if (NILP (specified_data
))
5607 file
= x_find_image_file (specified_file
);
5608 if (!STRINGP (file
))
5610 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5614 /* Open the image file. */
5615 fp
= fopen (SSDATA (file
), "rb");
5618 image_error ("Cannot open image file `%s'", file
, Qnil
);
5622 /* Check PNG signature. */
5623 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5624 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5626 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5633 if (!STRINGP (specified_data
))
5635 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5639 /* Read from memory. */
5640 tbr
.bytes
= SDATA (specified_data
);
5641 tbr
.len
= SBYTES (specified_data
);
5644 /* Check PNG signature. */
5645 if (tbr
.len
< sizeof sig
5646 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5648 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5652 /* Need to skip past the signature. */
5653 tbr
.bytes
+= sizeof (sig
);
5656 /* Initialize read and info structs for PNG lib. */
5657 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5662 info_ptr
= fn_png_create_info_struct (png_ptr
);
5663 end_info
= fn_png_create_info_struct (png_ptr
);
5666 c
->png_ptr
= png_ptr
;
5667 c
->info_ptr
= info_ptr
;
5668 c
->end_info
= end_info
;
5673 if (! (info_ptr
&& end_info
))
5675 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5680 if (fp
) fclose (fp
);
5684 /* Set error jump-back. We come back here when the PNG library
5685 detects an error. */
5686 if (_setjmp (PNG_JMPBUF (png_ptr
)))
5690 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5698 /* Silence a bogus diagnostic; see GCC bug 54561. */
5699 IF_LINT (fp
= c
->fp
);
5701 /* Read image info. */
5702 if (!NILP (specified_data
))
5703 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5705 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5707 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5708 fn_png_read_info (png_ptr
, info_ptr
);
5709 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5710 &interlace_type
, NULL
, NULL
);
5712 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5713 && check_image_size (f
, width
, height
)))
5715 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5719 /* Create the X image and pixmap now, so that the work below can be
5720 omitted if the image is too large for X. */
5721 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5725 /* If image contains simply transparency data, we prefer to
5726 construct a clipping mask. */
5727 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5732 /* This function is easier to write if we only have to handle
5733 one data format: RGB or RGBA with 8 bits per channel. Let's
5734 transform other formats into that format. */
5736 /* Strip more than 8 bits per channel. */
5737 if (bit_depth
== 16)
5738 fn_png_set_strip_16 (png_ptr
);
5740 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5742 fn_png_set_expand (png_ptr
);
5744 /* Convert grayscale images to RGB. */
5745 if (color_type
== PNG_COLOR_TYPE_GRAY
5746 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5747 fn_png_set_gray_to_rgb (png_ptr
);
5749 /* Handle alpha channel by combining the image with a background
5750 color. Do this only if a real alpha channel is supplied. For
5751 simple transparency, we prefer a clipping mask. */
5754 /* png_color_16 *image_bg; */
5755 Lisp_Object specified_bg
5756 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5757 int shift
= (bit_depth
== 16) ? 0 : 8;
5759 if (STRINGP (specified_bg
))
5760 /* The user specified `:background', use that. */
5763 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5765 png_color_16 user_bg
;
5767 memset (&user_bg
, 0, sizeof user_bg
);
5768 user_bg
.red
= color
.red
>> shift
;
5769 user_bg
.green
= color
.green
>> shift
;
5770 user_bg
.blue
= color
.blue
>> shift
;
5772 fn_png_set_background (png_ptr
, &user_bg
,
5773 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5778 /* We use the current frame background, ignoring any default
5779 background color set by the image. */
5780 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5782 png_color_16 frame_background
;
5784 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5785 x_query_color (f
, &color
);
5787 memset (&frame_background
, 0, sizeof frame_background
);
5788 frame_background
.red
= color
.red
>> shift
;
5789 frame_background
.green
= color
.green
>> shift
;
5790 frame_background
.blue
= color
.blue
>> shift
;
5791 #endif /* HAVE_X_WINDOWS */
5793 fn_png_set_background (png_ptr
, &frame_background
,
5794 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5798 /* Update info structure. */
5799 fn_png_read_update_info (png_ptr
, info_ptr
);
5801 /* Get number of channels. Valid values are 1 for grayscale images
5802 and images with a palette, 2 for grayscale images with transparency
5803 information (alpha channel), 3 for RGB images, and 4 for RGB
5804 images with alpha channel, i.e. RGBA. If conversions above were
5805 sufficient we should only have 3 or 4 channels here. */
5806 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5807 eassert (channels
== 3 || channels
== 4);
5809 /* Number of bytes needed for one row of the image. */
5810 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5812 /* Allocate memory for the image. */
5813 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5814 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5815 memory_full (SIZE_MAX
);
5816 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5817 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5818 for (i
= 0; i
< height
; ++i
)
5819 rows
[i
] = pixels
+ i
* row_bytes
;
5821 /* Read the entire image. */
5822 fn_png_read_image (png_ptr
, rows
);
5823 fn_png_read_end (png_ptr
, info_ptr
);
5830 /* Create an image and pixmap serving as mask if the PNG image
5831 contains an alpha channel. */
5834 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5835 &mask_img
, &img
->mask
))
5837 x_destroy_x_image (ximg
);
5838 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5839 img
->pixmap
= NO_PIXMAP
;
5843 /* Fill the X image and mask from PNG data. */
5844 init_color_table ();
5846 for (y
= 0; y
< height
; ++y
)
5848 png_byte
*p
= rows
[y
];
5850 for (x
= 0; x
< width
; ++x
)
5857 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5858 /* An alpha channel, aka mask channel, associates variable
5859 transparency with an image. Where other image formats
5860 support binary transparency---fully transparent or fully
5861 opaque---PNG allows up to 254 levels of partial transparency.
5862 The PNG library implements partial transparency by combining
5863 the image with a specified background color.
5865 I'm not sure how to handle this here nicely: because the
5866 background on which the image is displayed may change, for
5867 real alpha channel support, it would be necessary to create
5868 a new image for each possible background.
5870 What I'm doing now is that a mask is created if we have
5871 boolean transparency information. Otherwise I'm using
5872 the frame's background color to combine the image with. */
5877 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5883 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5884 /* Set IMG's background color from the PNG image, unless the user
5888 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5890 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5891 img
->background_valid
= 1;
5895 #ifdef COLOR_TABLE_SUPPORT
5896 /* Remember colors allocated for this image. */
5897 img
->colors
= colors_in_color_table (&img
->ncolors
);
5898 free_color_table ();
5899 #endif /* COLOR_TABLE_SUPPORT */
5902 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5907 img
->height
= height
;
5909 /* Maybe fill in the background field while we have ximg handy.
5910 Casting avoids a GCC warning. */
5911 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5913 /* Put the image into the pixmap, then free the X image and its buffer. */
5914 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5915 x_destroy_x_image (ximg
);
5917 /* Same for the mask. */
5920 /* Fill in the background_transparent field while we have the
5921 mask handy. Casting avoids a GCC warning. */
5922 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5924 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5925 x_destroy_x_image (mask_img
);
5932 png_load (struct frame
*f
, struct image
*img
)
5934 struct png_load_context c
;
5935 return png_load_body (f
, img
, &c
);
5938 #else /* HAVE_PNG */
5942 png_load (struct frame
*f
, struct image
*img
)
5944 return ns_load_image (f
, img
,
5945 image_spec_value (img
->spec
, QCfile
, NULL
),
5946 image_spec_value (img
->spec
, QCdata
, NULL
));
5948 #endif /* HAVE_NS */
5951 #endif /* !HAVE_PNG */
5955 /***********************************************************************
5957 ***********************************************************************/
5959 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5961 static bool jpeg_image_p (Lisp_Object object
);
5962 static bool jpeg_load (struct frame
*f
, struct image
*img
);
5964 /* The symbol `jpeg' identifying images of this type. */
5966 static Lisp_Object Qjpeg
;
5968 /* Indices of image specification fields in gs_format, below. */
5970 enum jpeg_keyword_index
5979 JPEG_HEURISTIC_MASK
,
5985 /* Vector of image_keyword structures describing the format
5986 of valid user-defined image specifications. */
5988 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5990 {":type", IMAGE_SYMBOL_VALUE
, 1},
5991 {":data", IMAGE_STRING_VALUE
, 0},
5992 {":file", IMAGE_STRING_VALUE
, 0},
5993 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5994 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5995 {":relief", IMAGE_INTEGER_VALUE
, 0},
5996 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5997 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5998 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5999 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6003 static bool init_jpeg_functions (void);
6005 #define init_jpeg_functions NULL
6008 /* Structure describing the image type `jpeg'. */
6010 static struct image_type jpeg_type
=
6016 init_jpeg_functions
,
6020 /* Return true if OBJECT is a valid JPEG image specification. */
6023 jpeg_image_p (Lisp_Object object
)
6025 struct image_keyword fmt
[JPEG_LAST
];
6027 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6029 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6032 /* Must specify either the :data or :file keyword. */
6033 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6036 #endif /* HAVE_JPEG || HAVE_NS */
6040 /* Work around a warning about HAVE_STDLIB_H being redefined in
6042 #ifdef HAVE_STDLIB_H
6043 #undef HAVE_STDLIB_H
6044 #endif /* HAVE_STLIB_H */
6046 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6047 /* In older releases of the jpeg library, jpeglib.h will define boolean
6048 differently depending on __WIN32__, so make sure it is defined. */
6052 #include <jpeglib.h>
6055 #ifdef HAVE_STLIB_H_1
6056 #define HAVE_STDLIB_H 1
6061 /* JPEG library details. */
6062 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6063 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6064 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6065 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6066 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6067 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6068 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6069 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6072 init_jpeg_functions (void)
6076 if (!(library
= w32_delayed_load (Qjpeg
)))
6079 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6080 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6081 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6082 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6083 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6084 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6085 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6086 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6090 /* Wrapper since we can't directly assign the function pointer
6091 to another function pointer that was declared more completely easily. */
6093 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6095 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6100 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6101 #define fn_jpeg_start_decompress jpeg_start_decompress
6102 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6103 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6104 #define fn_jpeg_read_header jpeg_read_header
6105 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6106 #define fn_jpeg_std_error jpeg_std_error
6107 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6109 #endif /* HAVE_NTGUI */
6111 struct my_jpeg_error_mgr
6113 struct jpeg_error_mgr pub
;
6114 sys_jmp_buf setjmp_buffer
;
6116 /* The remaining members are so that longjmp doesn't munge local
6118 struct jpeg_decompress_struct cinfo
;
6122 MY_JPEG_INVALID_IMAGE_SIZE
,
6123 MY_JPEG_CANNOT_CREATE_X
6131 static _Noreturn
void
6132 my_error_exit (j_common_ptr cinfo
)
6134 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6135 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6136 sys_longjmp (mgr
->setjmp_buffer
, 1);
6140 /* Init source method for JPEG data source manager. Called by
6141 jpeg_read_header() before any data is actually read. See
6142 libjpeg.doc from the JPEG lib distribution. */
6145 our_common_init_source (j_decompress_ptr cinfo
)
6150 /* Method to terminate data source. Called by
6151 jpeg_finish_decompress() after all data has been processed. */
6154 our_common_term_source (j_decompress_ptr cinfo
)
6159 /* Fill input buffer method for JPEG data source manager. Called
6160 whenever more data is needed. We read the whole image in one step,
6161 so this only adds a fake end of input marker at the end. */
6163 static JOCTET our_memory_buffer
[2];
6166 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6168 /* Insert a fake EOI marker. */
6169 struct jpeg_source_mgr
*src
= cinfo
->src
;
6171 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6172 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6174 src
->next_input_byte
= our_memory_buffer
;
6175 src
->bytes_in_buffer
= 2;
6180 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6181 is the JPEG data source manager. */
6184 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6186 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6190 if (num_bytes
> src
->bytes_in_buffer
)
6191 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6193 src
->bytes_in_buffer
-= num_bytes
;
6194 src
->next_input_byte
+= num_bytes
;
6199 /* Set up the JPEG lib for reading an image from DATA which contains
6200 LEN bytes. CINFO is the decompression info structure created for
6201 reading the image. */
6204 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6206 struct jpeg_source_mgr
*src
;
6208 if (cinfo
->src
== NULL
)
6210 /* First time for this JPEG object? */
6211 cinfo
->src
= (struct jpeg_source_mgr
*)
6212 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6213 sizeof (struct jpeg_source_mgr
));
6214 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6215 src
->next_input_byte
= data
;
6218 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6219 src
->init_source
= our_common_init_source
;
6220 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6221 src
->skip_input_data
= our_memory_skip_input_data
;
6222 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6223 src
->term_source
= our_common_term_source
;
6224 src
->bytes_in_buffer
= len
;
6225 src
->next_input_byte
= data
;
6229 struct jpeg_stdio_mgr
6231 struct jpeg_source_mgr mgr
;
6238 /* Size of buffer to read JPEG from file.
6239 Not too big, as we want to use alloc_small. */
6240 #define JPEG_STDIO_BUFFER_SIZE 8192
6243 /* Fill input buffer method for JPEG data source manager. Called
6244 whenever more data is needed. The data is read from a FILE *. */
6247 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6249 struct jpeg_stdio_mgr
*src
;
6251 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6256 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6258 src
->mgr
.bytes_in_buffer
= bytes
;
6261 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6263 src
->buffer
[0] = (JOCTET
) 0xFF;
6264 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6265 src
->mgr
.bytes_in_buffer
= 2;
6267 src
->mgr
.next_input_byte
= src
->buffer
;
6274 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6275 is the JPEG data source manager. */
6278 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6280 struct jpeg_stdio_mgr
*src
;
6281 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6283 while (num_bytes
> 0 && !src
->finished
)
6285 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6287 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6288 src
->mgr
.next_input_byte
+= num_bytes
;
6293 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6294 src
->mgr
.bytes_in_buffer
= 0;
6295 src
->mgr
.next_input_byte
= NULL
;
6297 our_stdio_fill_input_buffer (cinfo
);
6303 /* Set up the JPEG lib for reading an image from a FILE *.
6304 CINFO is the decompression info structure created for
6305 reading the image. */
6308 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6310 struct jpeg_stdio_mgr
*src
;
6312 if (cinfo
->src
!= NULL
)
6313 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6316 /* First time for this JPEG object? */
6317 cinfo
->src
= (struct jpeg_source_mgr
*)
6318 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6319 sizeof (struct jpeg_stdio_mgr
));
6320 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6321 src
->buffer
= (JOCTET
*)
6322 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6323 JPEG_STDIO_BUFFER_SIZE
);
6328 src
->mgr
.init_source
= our_common_init_source
;
6329 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6330 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6331 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6332 src
->mgr
.term_source
= our_common_term_source
;
6333 src
->mgr
.bytes_in_buffer
= 0;
6334 src
->mgr
.next_input_byte
= NULL
;
6338 /* Load image IMG for use on frame F. Patterned after example.c
6339 from the JPEG lib. */
6342 jpeg_load_body (struct frame
*f
, struct image
*img
,
6343 struct my_jpeg_error_mgr
*mgr
)
6345 Lisp_Object file
, specified_file
;
6346 Lisp_Object specified_data
;
6349 int row_stride
, x
, y
;
6350 XImagePtr ximg
= NULL
;
6351 unsigned long *colors
;
6354 /* Open the JPEG file. */
6355 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6356 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6358 if (NILP (specified_data
))
6360 file
= x_find_image_file (specified_file
);
6361 if (!STRINGP (file
))
6363 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6367 fp
= fopen (SSDATA (file
), "rb");
6370 image_error ("Cannot open `%s'", file
, Qnil
);
6374 else if (!STRINGP (specified_data
))
6376 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6380 IF_LINT (mgr
->fp
= fp
);
6382 /* Customize libjpeg's error handling to call my_error_exit when an
6383 error is detected. This function will perform a longjmp. */
6384 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6385 mgr
->pub
.error_exit
= my_error_exit
;
6386 if (sys_setjmp (mgr
->setjmp_buffer
))
6388 switch (mgr
->failure_code
)
6390 case MY_JPEG_ERROR_EXIT
:
6392 char buf
[JMSG_LENGTH_MAX
];
6393 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6394 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6395 build_string (buf
));
6399 case MY_JPEG_INVALID_IMAGE_SIZE
:
6400 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6403 case MY_JPEG_CANNOT_CREATE_X
:
6407 /* Close the input file and destroy the JPEG object. */
6410 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6412 /* If we already have an XImage, free that. */
6413 x_destroy_x_image (ximg
);
6415 /* Free pixmap and colors. */
6416 x_clear_image (f
, img
);
6420 /* Silence a bogus diagnostic; see GCC bug 54561. */
6421 IF_LINT (fp
= mgr
->fp
);
6423 /* Create the JPEG decompression object. Let it read from fp.
6424 Read the JPEG image header. */
6425 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6427 if (NILP (specified_data
))
6428 jpeg_file_src (&mgr
->cinfo
, fp
);
6430 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6431 SBYTES (specified_data
));
6433 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6435 /* Customize decompression so that color quantization will be used.
6436 Start decompression. */
6437 mgr
->cinfo
.quantize_colors
= 1;
6438 fn_jpeg_start_decompress (&mgr
->cinfo
);
6439 width
= img
->width
= mgr
->cinfo
.output_width
;
6440 height
= img
->height
= mgr
->cinfo
.output_height
;
6442 if (!check_image_size (f
, width
, height
))
6444 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6445 sys_longjmp (mgr
->setjmp_buffer
, 1);
6448 /* Create X image and pixmap. */
6449 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6451 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6452 sys_longjmp (mgr
->setjmp_buffer
, 1);
6455 /* Allocate colors. When color quantization is used,
6456 mgr->cinfo.actual_number_of_colors has been set with the number of
6457 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6458 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6459 No more than 255 colors will be generated. */
6463 if (mgr
->cinfo
.out_color_components
> 2)
6464 ir
= 0, ig
= 1, ib
= 2;
6465 else if (mgr
->cinfo
.out_color_components
> 1)
6466 ir
= 0, ig
= 1, ib
= 0;
6468 ir
= 0, ig
= 0, ib
= 0;
6470 /* Use the color table mechanism because it handles colors that
6471 cannot be allocated nicely. Such colors will be replaced with
6472 a default color, and we don't have to care about which colors
6473 can be freed safely, and which can't. */
6474 init_color_table ();
6475 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6477 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6479 /* Multiply RGB values with 255 because X expects RGB values
6480 in the range 0..0xffff. */
6481 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6482 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6483 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6484 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6487 #ifdef COLOR_TABLE_SUPPORT
6488 /* Remember those colors actually allocated. */
6489 img
->colors
= colors_in_color_table (&img
->ncolors
);
6490 free_color_table ();
6491 #endif /* COLOR_TABLE_SUPPORT */
6495 row_stride
= width
* mgr
->cinfo
.output_components
;
6496 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6497 JPOOL_IMAGE
, row_stride
, 1);
6498 for (y
= 0; y
< height
; ++y
)
6500 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6501 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6502 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6506 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6507 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6511 /* Maybe fill in the background field while we have ximg handy. */
6512 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6513 /* Casting avoids a GCC warning. */
6514 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6516 /* Put the image into the pixmap. */
6517 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6518 x_destroy_x_image (ximg
);
6523 jpeg_load (struct frame
*f
, struct image
*img
)
6525 struct my_jpeg_error_mgr mgr
;
6526 return jpeg_load_body (f
, img
, &mgr
);
6529 #else /* HAVE_JPEG */
6533 jpeg_load (struct frame
*f
, struct image
*img
)
6535 return ns_load_image (f
, img
,
6536 image_spec_value (img
->spec
, QCfile
, NULL
),
6537 image_spec_value (img
->spec
, QCdata
, NULL
));
6539 #endif /* HAVE_NS */
6541 #endif /* !HAVE_JPEG */
6545 /***********************************************************************
6547 ***********************************************************************/
6549 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6551 static bool tiff_image_p (Lisp_Object object
);
6552 static bool tiff_load (struct frame
*f
, struct image
*img
);
6554 /* The symbol `tiff' identifying images of this type. */
6556 static Lisp_Object Qtiff
;
6558 /* Indices of image specification fields in tiff_format, below. */
6560 enum tiff_keyword_index
6569 TIFF_HEURISTIC_MASK
,
6576 /* Vector of image_keyword structures describing the format
6577 of valid user-defined image specifications. */
6579 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6581 {":type", IMAGE_SYMBOL_VALUE
, 1},
6582 {":data", IMAGE_STRING_VALUE
, 0},
6583 {":file", IMAGE_STRING_VALUE
, 0},
6584 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6585 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6586 {":relief", IMAGE_INTEGER_VALUE
, 0},
6587 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6588 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6589 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6590 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6591 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6595 static bool init_tiff_functions (void);
6597 #define init_tiff_functions NULL
6600 /* Structure describing the image type `tiff'. */
6602 static struct image_type tiff_type
=
6608 init_tiff_functions
,
6612 /* Return true if OBJECT is a valid TIFF image specification. */
6615 tiff_image_p (Lisp_Object object
)
6617 struct image_keyword fmt
[TIFF_LAST
];
6618 memcpy (fmt
, tiff_format
, sizeof fmt
);
6620 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6623 /* Must specify either the :data or :file keyword. */
6624 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6627 #endif /* HAVE_TIFF || HAVE_NS */
6635 /* TIFF library details. */
6636 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6637 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6638 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6639 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6640 TIFFReadWriteProc
, TIFFReadWriteProc
,
6641 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6642 TIFFMapFileProc
, TIFFUnmapFileProc
));
6643 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6644 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6645 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6646 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6649 init_tiff_functions (void)
6653 if (!(library
= w32_delayed_load (Qtiff
)))
6656 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6657 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6658 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6659 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6660 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6661 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6662 LOAD_IMGLIB_FN (library
, TIFFClose
);
6663 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6669 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6670 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6671 #define fn_TIFFOpen TIFFOpen
6672 #define fn_TIFFClientOpen TIFFClientOpen
6673 #define fn_TIFFGetField TIFFGetField
6674 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6675 #define fn_TIFFClose TIFFClose
6676 #define fn_TIFFSetDirectory TIFFSetDirectory
6677 #endif /* HAVE_NTGUI */
6680 /* Reading from a memory buffer for TIFF images Based on the PNG
6681 memory source, but we have to provide a lot of extra functions.
6684 We really only need to implement read and seek, but I am not
6685 convinced that the TIFF library is smart enough not to destroy
6686 itself if we only hand it the function pointers we need to
6691 unsigned char *bytes
;
6698 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6700 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6702 size
= min (size
, src
->len
- src
->index
);
6703 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6709 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6715 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6717 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6722 case SEEK_SET
: /* Go from beginning of source. */
6726 case SEEK_END
: /* Go from end of source. */
6727 idx
= src
->len
+ off
;
6730 case SEEK_CUR
: /* Go from current position. */
6731 idx
= src
->index
+ off
;
6734 default: /* Invalid `whence'. */
6738 if (idx
> src
->len
|| idx
< 0)
6746 tiff_close_memory (thandle_t data
)
6753 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6755 /* It is already _IN_ memory. */
6760 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6762 /* We don't need to do this. */
6766 tiff_size_of_memory (thandle_t data
)
6768 return ((tiff_memory_source
*) data
)->len
;
6771 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6772 compiler error compiling tiff_handler, see Bugzilla bug #17406
6773 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6774 this function as external works around that problem. */
6775 #if defined (__MINGW32__) && __GNUC__ == 3
6776 # define MINGW_STATIC
6778 # define MINGW_STATIC static
6782 tiff_handler (const char *, const char *, const char *, va_list)
6783 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6785 tiff_handler (const char *log_format
, const char *title
,
6786 const char *format
, va_list ap
)
6788 /* doprnt is not suitable here, as TIFF handlers are called from
6789 libtiff and are passed arbitrary printf directives. Instead, use
6790 vsnprintf, taking care to be portable to nonstandard environments
6791 where vsnprintf returns -1 on buffer overflow. Since it's just a
6792 log entry, it's OK to truncate it. */
6794 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6795 add_to_log (log_format
, build_string (title
),
6796 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6800 static void tiff_error_handler (const char *, const char *, va_list)
6801 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6803 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6805 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6809 static void tiff_warning_handler (const char *, const char *, va_list)
6810 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6812 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6814 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6818 /* Load TIFF image IMG for use on frame F. Value is true if
6822 tiff_load (struct frame
*f
, struct image
*img
)
6824 Lisp_Object file
, specified_file
;
6825 Lisp_Object specified_data
;
6827 int width
, height
, x
, y
, count
;
6831 tiff_memory_source memsrc
;
6834 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6835 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6837 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6838 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6840 if (NILP (specified_data
))
6842 /* Read from a file */
6843 file
= x_find_image_file (specified_file
);
6844 if (!STRINGP (file
))
6846 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6850 /* Try to open the image file. */
6851 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6854 image_error ("Cannot open `%s'", file
, Qnil
);
6860 if (!STRINGP (specified_data
))
6862 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6866 /* Memory source! */
6867 memsrc
.bytes
= SDATA (specified_data
);
6868 memsrc
.len
= SBYTES (specified_data
);
6871 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6872 tiff_read_from_memory
,
6873 tiff_write_from_memory
,
6874 tiff_seek_in_memory
,
6876 tiff_size_of_memory
,
6882 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6887 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6888 if (INTEGERP (image
))
6890 EMACS_INT ino
= XFASTINT (image
);
6891 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
6892 && fn_TIFFSetDirectory (tiff
, ino
)))
6894 image_error ("Invalid image number `%s' in image `%s'",
6896 fn_TIFFClose (tiff
);
6901 /* Get width and height of the image, and allocate a raster buffer
6902 of width x height 32-bit values. */
6903 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6904 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6906 if (!check_image_size (f
, width
, height
))
6908 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6909 fn_TIFFClose (tiff
);
6913 /* Create the X image and pixmap. */
6914 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6915 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6916 &ximg
, &img
->pixmap
)))
6918 fn_TIFFClose (tiff
);
6922 buf
= xmalloc (sizeof *buf
* width
* height
);
6924 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6926 /* Count the number of images in the file. */
6927 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6931 img
->lisp_data
= Fcons (Qcount
,
6932 Fcons (make_number (count
),
6935 fn_TIFFClose (tiff
);
6938 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6943 /* Initialize the color table. */
6944 init_color_table ();
6946 /* Process the pixel raster. Origin is in the lower-left corner. */
6947 for (y
= 0; y
< height
; ++y
)
6949 uint32
*row
= buf
+ y
* width
;
6951 for (x
= 0; x
< width
; ++x
)
6953 uint32 abgr
= row
[x
];
6954 int r
= TIFFGetR (abgr
) << 8;
6955 int g
= TIFFGetG (abgr
) << 8;
6956 int b
= TIFFGetB (abgr
) << 8;
6957 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6961 #ifdef COLOR_TABLE_SUPPORT
6962 /* Remember the colors allocated for the image. Free the color table. */
6963 img
->colors
= colors_in_color_table (&img
->ncolors
);
6964 free_color_table ();
6965 #endif /* COLOR_TABLE_SUPPORT */
6968 img
->height
= height
;
6970 /* Maybe fill in the background field while we have ximg handy. */
6971 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6972 /* Casting avoids a GCC warning on W32. */
6973 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6975 /* Put the image into the pixmap, then free the X image and its buffer. */
6976 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6977 x_destroy_x_image (ximg
);
6983 #else /* HAVE_TIFF */
6987 tiff_load (struct frame
*f
, struct image
*img
)
6989 return ns_load_image (f
, img
,
6990 image_spec_value (img
->spec
, QCfile
, NULL
),
6991 image_spec_value (img
->spec
, QCdata
, NULL
));
6993 #endif /* HAVE_NS */
6995 #endif /* !HAVE_TIFF */
6999 /***********************************************************************
7001 ***********************************************************************/
7003 #if defined (HAVE_GIF) || defined (HAVE_NS)
7005 static bool gif_image_p (Lisp_Object object
);
7006 static bool gif_load (struct frame
*f
, struct image
*img
);
7007 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7009 /* The symbol `gif' identifying images of this type. */
7011 static Lisp_Object Qgif
;
7013 /* Indices of image specification fields in gif_format, below. */
7015 enum gif_keyword_index
7031 /* Vector of image_keyword structures describing the format
7032 of valid user-defined image specifications. */
7034 static const struct image_keyword gif_format
[GIF_LAST
] =
7036 {":type", IMAGE_SYMBOL_VALUE
, 1},
7037 {":data", IMAGE_STRING_VALUE
, 0},
7038 {":file", IMAGE_STRING_VALUE
, 0},
7039 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7040 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7041 {":relief", IMAGE_INTEGER_VALUE
, 0},
7042 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7043 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7044 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7045 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7046 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7050 static bool init_gif_functions (void);
7052 #define init_gif_functions NULL
7055 /* Structure describing the image type `gif'. */
7057 static struct image_type gif_type
=
7067 /* Free X resources of GIF image IMG which is used on frame F. */
7070 gif_clear_image (struct frame
*f
, struct image
*img
)
7072 img
->lisp_data
= Qnil
;
7073 x_clear_image (f
, img
);
7076 /* Return true if OBJECT is a valid GIF image specification. */
7079 gif_image_p (Lisp_Object object
)
7081 struct image_keyword fmt
[GIF_LAST
];
7082 memcpy (fmt
, gif_format
, sizeof fmt
);
7084 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7087 /* Must specify either the :data or :file keyword. */
7088 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7091 #endif /* HAVE_GIF */
7095 #if defined (HAVE_NTGUI)
7096 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7097 Undefine before redefining to avoid a preprocessor warning. */
7101 /* avoid conflict with QuickdrawText.h */
7102 #define DrawText gif_DrawText
7103 #include <gif_lib.h>
7106 #else /* HAVE_NTGUI */
7108 #include <gif_lib.h>
7110 #endif /* HAVE_NTGUI */
7115 /* GIF library details. */
7116 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7117 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7118 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7119 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7122 init_gif_functions (void)
7126 if (!(library
= w32_delayed_load (Qgif
)))
7129 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7130 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7131 LOAD_IMGLIB_FN (library
, DGifOpen
);
7132 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7138 #define fn_DGifCloseFile DGifCloseFile
7139 #define fn_DGifSlurp DGifSlurp
7140 #define fn_DGifOpen DGifOpen
7141 #define fn_DGifOpenFileName DGifOpenFileName
7143 #endif /* HAVE_NTGUI */
7145 /* Reading a GIF image from memory
7146 Based on the PNG memory stuff to a certain extent. */
7150 unsigned char *bytes
;
7156 /* Make the current memory source available to gif_read_from_memory.
7157 It's done this way because not all versions of libungif support
7158 a UserData field in the GifFileType structure. */
7159 static gif_memory_source
*current_gif_memory_src
;
7162 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7164 gif_memory_source
*src
= current_gif_memory_src
;
7166 if (len
> src
->len
- src
->index
)
7169 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7175 /* Load GIF image IMG for use on frame F. Value is true if
7178 static const int interlace_start
[] = {0, 4, 2, 1};
7179 static const int interlace_increment
[] = {8, 8, 4, 2};
7181 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7184 gif_load (struct frame
*f
, struct image
*img
)
7187 int rc
, width
, height
, x
, y
, i
, j
;
7189 ColorMapObject
*gif_color_map
;
7190 unsigned long pixel_colors
[256];
7192 gif_memory_source memsrc
;
7193 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7194 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7195 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7196 unsigned long bgcolor
= 0;
7199 if (NILP (specified_data
))
7201 file
= x_find_image_file (specified_file
);
7202 if (!STRINGP (file
))
7204 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7208 /* Open the GIF file. */
7209 gif
= fn_DGifOpenFileName (SSDATA (file
));
7212 image_error ("Cannot open `%s'", file
, Qnil
);
7218 if (!STRINGP (specified_data
))
7220 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7224 /* Read from memory! */
7225 current_gif_memory_src
= &memsrc
;
7226 memsrc
.bytes
= SDATA (specified_data
);
7227 memsrc
.len
= SBYTES (specified_data
);
7230 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7233 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7238 /* Before reading entire contents, check the declared image size. */
7239 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7241 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7242 fn_DGifCloseFile (gif
);
7246 /* Read entire contents. */
7247 rc
= fn_DGifSlurp (gif
);
7248 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7250 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7251 fn_DGifCloseFile (gif
);
7255 /* Which sub-image are we to display? */
7257 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7258 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7259 if (idx
< 0 || idx
>= gif
->ImageCount
)
7261 image_error ("Invalid image number `%s' in image `%s'",
7262 image_number
, img
->spec
);
7263 fn_DGifCloseFile (gif
);
7268 width
= img
->width
= gif
->SWidth
;
7269 height
= img
->height
= gif
->SHeight
;
7271 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7272 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7273 img
->corners
[BOT_CORNER
]
7274 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7275 img
->corners
[RIGHT_CORNER
]
7276 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7278 if (!check_image_size (f
, width
, height
))
7280 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7281 fn_DGifCloseFile (gif
);
7285 /* Create the X image and pixmap. */
7286 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7288 fn_DGifCloseFile (gif
);
7292 /* Clear the part of the screen image not covered by the image.
7293 Full animated GIF support requires more here (see the gif89 spec,
7294 disposal methods). Let's simply assume that the part not covered
7295 by a sub-image is in the frame's background color. */
7296 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7297 for (x
= 0; x
< width
; ++x
)
7298 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7300 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7301 for (x
= 0; x
< width
; ++x
)
7302 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7304 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7306 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7307 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7308 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7309 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7312 /* Read the GIF image into the X image. */
7314 /* FIXME: With the current implementation, loading an animated gif
7315 is quadratic in the number of animation frames, since each frame
7316 is a separate struct image. We must provide a way for a single
7317 gif_load call to construct and save all animation frames. */
7319 init_color_table ();
7320 if (STRINGP (specified_bg
))
7321 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7322 FRAME_BACKGROUND_PIXEL (f
));
7323 for (j
= 0; j
<= idx
; ++j
)
7325 /* We use a local variable `raster' here because RasterBits is a
7326 char *, which invites problems with bytes >= 0x80. */
7327 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7328 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7329 int transparency_color_index
= -1;
7331 int subimg_width
= subimage
->ImageDesc
.Width
;
7332 int subimg_height
= subimage
->ImageDesc
.Height
;
7333 int subimg_top
= subimage
->ImageDesc
.Top
;
7334 int subimg_left
= subimage
->ImageDesc
.Left
;
7336 /* Find the Graphic Control Extension block for this sub-image.
7337 Extract the disposal method and transparency color. */
7338 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7340 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7342 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7343 && extblock
->ByteCount
== 4
7344 && extblock
->Bytes
[0] & 1)
7346 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7347 to background". Treat any other value like 2. */
7348 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7349 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7354 /* We can't "keep in place" the first subimage. */
7358 /* For disposal == 0, the spec says "No disposal specified. The
7359 decoder is not required to take any action." In practice, it
7360 seems we need to treat this like "keep in place", see e.g.
7361 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7365 /* Allocate subimage colors. */
7366 memset (pixel_colors
, 0, sizeof pixel_colors
);
7367 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7369 gif_color_map
= gif
->SColorMap
;
7372 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7374 if (transparency_color_index
== i
)
7375 pixel_colors
[i
] = STRINGP (specified_bg
)
7376 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7379 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7380 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7381 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7382 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7386 /* Apply the pixel values. */
7387 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7391 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7393 y
++, row
+= interlace_increment
[pass
])
7395 if (row
>= subimg_height
)
7397 row
= interlace_start
[++pass
];
7398 while (row
>= subimg_height
)
7399 row
= interlace_start
[++pass
];
7402 for (x
= 0; x
< subimg_width
; x
++)
7404 int c
= raster
[y
* subimg_width
+ x
];
7405 if (transparency_color_index
!= c
|| disposal
!= 1)
7406 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7413 for (y
= 0; y
< subimg_height
; ++y
)
7414 for (x
= 0; x
< subimg_width
; ++x
)
7416 int c
= raster
[y
* subimg_width
+ x
];
7417 if (transparency_color_index
!= c
|| disposal
!= 1)
7418 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7424 #ifdef COLOR_TABLE_SUPPORT
7425 img
->colors
= colors_in_color_table (&img
->ncolors
);
7426 free_color_table ();
7427 #endif /* COLOR_TABLE_SUPPORT */
7429 /* Save GIF image extension data for `image-metadata'.
7430 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7431 img
->lisp_data
= Qnil
;
7432 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7435 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7436 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7437 /* Append (... FUNCTION "BYTES") */
7440 = Fcons (make_number (ext
->Function
),
7441 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7443 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7444 && ext
->ByteCount
== 4)
7446 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7447 delay
|= ext
->Bytes
[1];
7450 img
->lisp_data
= Fcons (Qextension_data
,
7451 Fcons (img
->lisp_data
, Qnil
));
7455 Fcons (make_float (delay
/ 100.0),
7459 if (gif
->ImageCount
> 1)
7460 img
->lisp_data
= Fcons (Qcount
,
7461 Fcons (make_number (gif
->ImageCount
),
7464 fn_DGifCloseFile (gif
);
7466 /* Maybe fill in the background field while we have ximg handy. */
7467 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7468 /* Casting avoids a GCC warning. */
7469 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7471 /* Put the image into the pixmap, then free the X image and its buffer. */
7472 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7473 x_destroy_x_image (ximg
);
7478 #else /* !HAVE_GIF */
7482 gif_load (struct frame
*f
, struct image
*img
)
7484 return ns_load_image (f
, img
,
7485 image_spec_value (img
->spec
, QCfile
, NULL
),
7486 image_spec_value (img
->spec
, QCdata
, NULL
));
7488 #endif /* HAVE_NS */
7490 #endif /* HAVE_GIF */
7493 /***********************************************************************
7495 ***********************************************************************/
7496 #if defined (HAVE_IMAGEMAGICK)
7498 static Lisp_Object Qimagemagick
;
7500 static bool imagemagick_image_p (Lisp_Object
);
7501 static bool imagemagick_load (struct frame
*, struct image
*);
7502 static void imagemagick_clear_image (struct frame
*, struct image
*);
7504 /* Indices of image specification fields in imagemagick_format. */
7506 enum imagemagick_keyword_index
7514 IMAGEMAGICK_ALGORITHM
,
7515 IMAGEMAGICK_HEURISTIC_MASK
,
7517 IMAGEMAGICK_BACKGROUND
,
7520 IMAGEMAGICK_ROTATION
,
7525 /* Vector of image_keyword structures describing the format
7526 of valid user-defined image specifications. */
7528 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7530 {":type", IMAGE_SYMBOL_VALUE
, 1},
7531 {":data", IMAGE_STRING_VALUE
, 0},
7532 {":file", IMAGE_STRING_VALUE
, 0},
7533 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7534 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7535 {":relief", IMAGE_INTEGER_VALUE
, 0},
7536 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7537 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7538 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7539 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7540 {":height", IMAGE_INTEGER_VALUE
, 0},
7541 {":width", IMAGE_INTEGER_VALUE
, 0},
7542 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7543 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7547 static bool init_imagemagick_functions (void);
7549 #define init_imagemagick_functions NULL
7552 /* Structure describing the image type for any image handled via
7555 static struct image_type imagemagick_type
=
7558 imagemagick_image_p
,
7560 imagemagick_clear_image
,
7561 init_imagemagick_functions
,
7565 /* Free X resources of imagemagick image IMG which is used on frame F. */
7568 imagemagick_clear_image (struct frame
*f
,
7571 x_clear_image (f
, img
);
7574 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7575 this by calling parse_image_spec and supplying the keywords that
7576 identify the IMAGEMAGICK format. */
7579 imagemagick_image_p (Lisp_Object object
)
7581 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7582 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7584 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7587 /* Must specify either the :data or :file keyword. */
7588 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7591 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7592 Therefore rename the function so it doesn't collide with ImageMagick. */
7593 #define DrawRectangle DrawRectangleGif
7594 #include <wand/MagickWand.h>
7596 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7597 Emacs seems to work fine with the hidden version, so unhide it. */
7598 #include <magick/version.h>
7599 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7600 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7601 MagickPixelPacket
*);
7604 /* Log ImageMagick error message.
7605 Useful when a ImageMagick function returns the status `MagickFalse'. */
7608 imagemagick_error (MagickWand
*wand
)
7611 ExceptionType severity
;
7613 description
= MagickGetException (wand
, &severity
);
7614 image_error ("ImageMagick error: %s",
7615 build_string (description
),
7617 description
= (char *) MagickRelinquishMemory (description
);
7620 /* Helper function for imagemagick_load, which does the actual loading
7621 given contents and size, apart from frame and image structures,
7622 passed from imagemagick_load. Uses librimagemagick to do most of
7623 the image processing.
7625 F is a pointer to the Emacs frame; IMG to the image structure to
7626 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7627 be parsed; SIZE is the number of bytes of data; and FILENAME is
7628 either the file name or the image data.
7630 Return true if successful. */
7633 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7634 unsigned char *contents
, unsigned int size
,
7637 size_t width
, height
;
7638 MagickBooleanType status
;
7641 MagickWand
*image_wand
;
7642 MagickWand
*ping_wand
;
7643 PixelIterator
*iterator
;
7644 PixelWand
**pixels
, *bg_wand
= NULL
;
7645 MagickPixelPacket pixel
;
7650 int desired_width
, desired_height
;
7654 /* Handle image index for image types who can contain more than one image.
7655 Interface :index is same as for GIF. First we "ping" the image to see how
7656 many sub-images it contains. Pinging is faster than loading the image to
7657 find out things about it. */
7659 /* Initialize the imagemagick environment. */
7660 MagickWandGenesis ();
7661 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7662 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7663 ping_wand
= NewMagickWand ();
7664 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7667 ? MagickPingImage (ping_wand
, filename
)
7668 : MagickPingImageBlob (ping_wand
, contents
, size
);
7670 if (status
== MagickFalse
)
7672 imagemagick_error (ping_wand
);
7673 DestroyMagickWand (ping_wand
);
7677 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7679 image_error ("Invalid image number `%s' in image `%s'",
7681 DestroyMagickWand (ping_wand
);
7685 if (MagickGetNumberImages (ping_wand
) > 1)
7688 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7691 DestroyMagickWand (ping_wand
);
7693 /* Now we know how many images are inside the file. If it's not a
7694 bundle, the number is one. Load the image data. */
7696 image_wand
= NewMagickWand ();
7699 ? MagickReadImage (image_wand
, filename
)
7700 : MagickReadImageBlob (image_wand
, contents
, size
))
7703 imagemagick_error (image_wand
);
7704 goto imagemagick_error
;
7707 /* Retrieve the frame's background color, for use later. */
7710 Lisp_Object specified_bg
;
7712 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7713 if (!STRINGP (specified_bg
)
7714 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7717 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7718 x_query_color (f
, &bgcolor
);
7720 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7724 bg_wand
= NewPixelWand ();
7725 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7726 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7727 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7730 /* If width and/or height is set in the display spec assume we want
7731 to scale to those values. If either h or w is unspecified, the
7732 unspecified should be calculated from the specified to preserve
7734 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7735 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7736 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7737 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7739 height
= MagickGetImageHeight (image_wand
);
7740 width
= MagickGetImageWidth (image_wand
);
7742 if (desired_width
!= -1 && desired_height
== -1)
7743 /* w known, calculate h. */
7744 desired_height
= (double) desired_width
/ width
* height
;
7745 if (desired_width
== -1 && desired_height
!= -1)
7746 /* h known, calculate w. */
7747 desired_width
= (double) desired_height
/ height
* width
;
7748 if (desired_width
!= -1 && desired_height
!= -1)
7750 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7751 if (status
== MagickFalse
)
7753 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7754 imagemagick_error (image_wand
);
7755 goto imagemagick_error
;
7759 /* crop behaves similar to image slicing in Emacs but is more memory
7761 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7763 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7765 /* After some testing, it seems MagickCropImage is the fastest crop
7766 function in ImageMagick. This crop function seems to do less copying
7767 than the alternatives, but it still reads the entire image into memory
7768 before cropping, which is apparently difficult to avoid when using
7770 size_t crop_width
= XINT (XCAR (crop
));
7772 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7774 size_t crop_height
= XINT (XCAR (crop
));
7776 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7778 ssize_t crop_x
= XINT (XCAR (crop
));
7780 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7782 ssize_t crop_y
= XINT (XCAR (crop
));
7783 MagickCropImage (image_wand
, crop_width
, crop_height
,
7790 /* Furthermore :rotation. we need background color and angle for
7793 TODO background handling for rotation specified_bg =
7794 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7796 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7799 rotation
= extract_float (value
);
7800 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
7801 if (status
== MagickFalse
)
7803 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7804 imagemagick_error (image_wand
);
7805 goto imagemagick_error
;
7809 /* Finally we are done manipulating the image. Figure out the
7810 resulting width/height and transfer ownership to Emacs. */
7811 height
= MagickGetImageHeight (image_wand
);
7812 width
= MagickGetImageWidth (image_wand
);
7814 /* Set the canvas background color to the frame or specified
7815 background, and flatten the image. Note: as of ImageMagick
7816 6.6.0, SVG image transparency is not handled properly
7817 (e.g. etc/images/splash.svg shows a white background always). */
7819 MagickWand
*new_wand
;
7820 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
7821 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
7822 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
7824 new_wand
= MagickFlattenImages (image_wand
);
7826 DestroyMagickWand (image_wand
);
7827 image_wand
= new_wand
;
7830 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7831 && check_image_size (f
, width
, height
)))
7833 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7834 goto imagemagick_error
;
7837 /* We can now get a valid pixel buffer from the imagemagick file, if all
7840 init_color_table ();
7842 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7843 if (imagemagick_render_type
!= 0)
7845 /* Magicexportimage is normally faster than pixelpushing. This
7846 method is also well tested. Some aspects of this method are
7847 ad-hoc and needs to be more researched. */
7848 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
7849 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
7850 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7851 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7852 &ximg
, &img
->pixmap
))
7854 #ifdef COLOR_TABLE_SUPPORT
7855 free_color_table ();
7857 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7858 goto imagemagick_error
;
7861 /* Oddly, the below code doesn't seem to work:*/
7862 /* switch(ximg->bitmap_unit){ */
7864 /* pixelwidth=CharPixel; */
7867 /* pixelwidth=ShortPixel; */
7870 /* pixelwidth=LongPixel; */
7874 Here im just guessing the format of the bitmap.
7875 happens to work fine for:
7878 seems about 3 times as fast as pixel pushing(not carefully measured)
7880 pixelwidth
= CharPixel
; /*??? TODO figure out*/
7881 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
7882 exportdepth
, pixelwidth
, ximg
->data
);
7885 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
7887 size_t image_height
;
7889 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7890 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7891 &ximg
, &img
->pixmap
))
7893 #ifdef COLOR_TABLE_SUPPORT
7894 free_color_table ();
7896 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7897 goto imagemagick_error
;
7900 /* Copy imagemagick image to x with primitive yet robust pixel
7901 pusher loop. This has been tested a lot with many different
7904 /* Copy pixels from the imagemagick image structure to the x image map. */
7905 iterator
= NewPixelIterator (image_wand
);
7906 if (iterator
== (PixelIterator
*) NULL
)
7908 #ifdef COLOR_TABLE_SUPPORT
7909 free_color_table ();
7911 x_destroy_x_image (ximg
);
7912 image_error ("Imagemagick pixel iterator creation failed",
7914 goto imagemagick_error
;
7917 image_height
= MagickGetImageHeight (image_wand
);
7918 for (y
= 0; y
< image_height
; y
++)
7920 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7921 if (pixels
== (PixelWand
**) NULL
)
7923 for (x
= 0; x
< (long) width
; x
++)
7925 PixelGetMagickColor (pixels
[x
], &pixel
);
7926 XPutPixel (ximg
, x
, y
,
7927 lookup_rgb_color (f
,
7933 DestroyPixelIterator (iterator
);
7936 #ifdef COLOR_TABLE_SUPPORT
7937 /* Remember colors allocated for this image. */
7938 img
->colors
= colors_in_color_table (&img
->ncolors
);
7939 free_color_table ();
7940 #endif /* COLOR_TABLE_SUPPORT */
7943 img
->height
= height
;
7945 /* Put the image into the pixmap, then free the X image and its
7947 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7948 x_destroy_x_image (ximg
);
7950 /* Final cleanup. image_wand should be the only resource left. */
7951 DestroyMagickWand (image_wand
);
7952 if (bg_wand
) DestroyPixelWand (bg_wand
);
7954 /* `MagickWandTerminus' terminates the imagemagick environment. */
7955 MagickWandTerminus ();
7960 DestroyMagickWand (image_wand
);
7961 if (bg_wand
) DestroyPixelWand (bg_wand
);
7963 MagickWandTerminus ();
7964 /* TODO more cleanup. */
7965 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7970 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
7971 successful. this function will go into the imagemagick_type structure, and
7972 the prototype thus needs to be compatible with that structure. */
7975 imagemagick_load (struct frame
*f
, struct image
*img
)
7978 Lisp_Object file_name
;
7980 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7981 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7982 if (STRINGP (file_name
))
7986 file
= x_find_image_file (file_name
);
7987 if (!STRINGP (file
))
7989 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7992 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
7994 /* Else its not a file, its a lisp object. Load the image from a
7995 lisp object rather than a file. */
8000 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8001 if (!STRINGP (data
))
8003 image_error ("Invalid image data `%s'", data
, Qnil
);
8006 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8007 SBYTES (data
), NULL
);
8013 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8014 doc
: /* Return a list of image types supported by ImageMagick.
8015 Each entry in this list is a symbol named after an ImageMagick format
8016 tag. See the ImageMagick manual for a list of ImageMagick formats and
8017 their descriptions (http://www.imagemagick.org/script/formats.php).
8018 You can also try the shell command: `identify -list format'.
8020 Note that ImageMagick recognizes many file-types that Emacs does not
8021 recognize as images, such as C. See `imagemagick-types-enable'
8022 and `imagemagick-types-inhibit'. */)
8025 Lisp_Object typelist
= Qnil
;
8030 Lisp_Object Qimagemagicktype
;
8032 GetExceptionInfo(&ex
);
8033 imtypes
= GetMagickList ("*", &numf
, &ex
);
8034 DestroyExceptionInfo(&ex
);
8036 for (i
= 0; i
< numf
; i
++)
8038 Qimagemagicktype
= intern (imtypes
[i
]);
8039 typelist
= Fcons (Qimagemagicktype
, typelist
);
8041 return Fnreverse (typelist
);
8044 #endif /* defined (HAVE_IMAGEMAGICK) */
8048 /***********************************************************************
8050 ***********************************************************************/
8052 #if defined (HAVE_RSVG)
8054 /* Function prototypes. */
8056 static bool svg_image_p (Lisp_Object object
);
8057 static bool svg_load (struct frame
*f
, struct image
*img
);
8059 static bool svg_load_image (struct frame
*, struct image
*,
8060 unsigned char *, ptrdiff_t);
8062 /* The symbol `svg' identifying images of this type. */
8064 static Lisp_Object Qsvg
;
8066 /* Indices of image specification fields in svg_format, below. */
8068 enum svg_keyword_index
8083 /* Vector of image_keyword structures describing the format
8084 of valid user-defined image specifications. */
8086 static const struct image_keyword svg_format
[SVG_LAST
] =
8088 {":type", IMAGE_SYMBOL_VALUE
, 1},
8089 {":data", IMAGE_STRING_VALUE
, 0},
8090 {":file", IMAGE_STRING_VALUE
, 0},
8091 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8092 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8093 {":relief", IMAGE_INTEGER_VALUE
, 0},
8094 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8095 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8096 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8097 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8101 static bool init_svg_functions (void);
8103 #define init_svg_functions NULL
8106 /* Structure describing the image type `svg'. Its the same type of
8107 structure defined for all image formats, handled by emacs image
8108 functions. See struct image_type in dispextern.h. */
8110 static struct image_type svg_type
=
8121 /* Return true if OBJECT is a valid SVG image specification. Do
8122 this by calling parse_image_spec and supplying the keywords that
8123 identify the SVG format. */
8126 svg_image_p (Lisp_Object object
)
8128 struct image_keyword fmt
[SVG_LAST
];
8129 memcpy (fmt
, svg_format
, sizeof fmt
);
8131 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8134 /* Must specify either the :data or :file keyword. */
8135 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8138 #include <librsvg/rsvg.h>
8142 /* SVG library functions. */
8143 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8144 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8145 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8146 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8147 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8149 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8150 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8151 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8152 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8153 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8154 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8155 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8156 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8158 DEF_IMGLIB_FN (void, g_type_init
);
8159 DEF_IMGLIB_FN (void, g_object_unref
);
8160 DEF_IMGLIB_FN (void, g_error_free
);
8162 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8165 init_svg_functions (void)
8167 HMODULE library
, gdklib
, glib
, gobject
;
8169 if (!(glib
= w32_delayed_load (Qglib
))
8170 || !(gobject
= w32_delayed_load (Qgobject
))
8171 || !(gdklib
= w32_delayed_load (Qgdk_pixbuf
))
8172 || !(library
= w32_delayed_load (Qsvg
)))
8175 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8176 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8177 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8178 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8179 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8181 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8182 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8183 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8184 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8185 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8186 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8187 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8188 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8190 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8191 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8192 LOAD_IMGLIB_FN (glib
, g_error_free
);
8198 /* The following aliases for library functions allow dynamic loading
8199 to be used on some platforms. */
8200 #define fn_rsvg_handle_new rsvg_handle_new
8201 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8202 #define fn_rsvg_handle_write rsvg_handle_write
8203 #define fn_rsvg_handle_close rsvg_handle_close
8204 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8206 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8207 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8208 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8209 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8210 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8211 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8212 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8213 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8215 #define fn_g_type_init g_type_init
8216 #define fn_g_object_unref g_object_unref
8217 #define fn_g_error_free g_error_free
8218 #endif /* !HAVE_NTGUI */
8220 /* Load SVG image IMG for use on frame F. Value is true if
8224 svg_load (struct frame
*f
, struct image
*img
)
8227 Lisp_Object file_name
;
8229 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8230 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8231 if (STRINGP (file_name
))
8234 unsigned char *contents
;
8237 file
= x_find_image_file (file_name
);
8238 if (!STRINGP (file
))
8240 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8244 /* Read the entire file into memory. */
8245 contents
= slurp_file (SSDATA (file
), &size
);
8246 if (contents
== NULL
)
8248 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8251 /* If the file was slurped into memory properly, parse it. */
8252 success_p
= svg_load_image (f
, img
, contents
, size
);
8255 /* Else its not a file, its a lisp object. Load the image from a
8256 lisp object rather than a file. */
8261 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8262 if (!STRINGP (data
))
8264 image_error ("Invalid image data `%s'", data
, Qnil
);
8267 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8273 /* svg_load_image is a helper function for svg_load, which does the
8274 actual loading given contents and size, apart from frame and image
8275 structures, passed from svg_load.
8277 Uses librsvg to do most of the image processing.
8279 Returns true when successful. */
8281 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8282 struct image
*img
, /* Pointer to emacs image structure. */
8283 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8284 ptrdiff_t size
) /* Size of data in bytes. */
8286 RsvgHandle
*rsvg_handle
;
8287 RsvgDimensionData dimension_data
;
8292 const guint8
*pixels
;
8295 Lisp_Object specified_bg
;
8300 /* g_type_init is a glib function that must be called prior to using
8301 gnome type library functions. */
8303 /* Make a handle to a new rsvg object. */
8304 rsvg_handle
= fn_rsvg_handle_new ();
8306 /* Parse the contents argument and fill in the rsvg_handle. */
8307 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8308 if (err
) goto rsvg_error
;
8310 /* The parsing is complete, rsvg_handle is ready to used, close it
8311 for further writes. */
8312 fn_rsvg_handle_close (rsvg_handle
, &err
);
8313 if (err
) goto rsvg_error
;
8315 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8316 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8318 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8322 /* We can now get a valid pixel buffer from the svg file, if all
8324 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8325 if (!pixbuf
) goto rsvg_error
;
8326 fn_g_object_unref (rsvg_handle
);
8328 /* Extract some meta data from the svg handle. */
8329 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8330 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8331 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8332 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8334 /* Validate the svg meta data. */
8335 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8336 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8337 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8338 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8340 /* Try to create a x pixmap to hold the svg pixmap. */
8341 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8343 fn_g_object_unref (pixbuf
);
8347 init_color_table ();
8349 /* Handle alpha channel by combining the image with a background
8351 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8352 if (!STRINGP (specified_bg
)
8353 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8356 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8357 x_query_color (f
, &background
);
8359 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8363 /* SVG pixmaps specify transparency in the last byte, so right
8364 shift 8 bits to get rid of it, since emacs doesn't support
8366 background
.red
>>= 8;
8367 background
.green
>>= 8;
8368 background
.blue
>>= 8;
8370 /* This loop handles opacity values, since Emacs assumes
8371 non-transparent images. Each pixel must be "flattened" by
8372 calculating the resulting color, given the transparency of the
8373 pixel, and the image background color. */
8374 for (y
= 0; y
< height
; ++y
)
8376 for (x
= 0; x
< width
; ++x
)
8386 opacity
= *pixels
++;
8388 red
= ((red
* opacity
)
8389 + (background
.red
* ((1 << 8) - opacity
)));
8390 green
= ((green
* opacity
)
8391 + (background
.green
* ((1 << 8) - opacity
)));
8392 blue
= ((blue
* opacity
)
8393 + (background
.blue
* ((1 << 8) - opacity
)));
8395 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8398 pixels
+= rowstride
- 4 * width
;
8401 #ifdef COLOR_TABLE_SUPPORT
8402 /* Remember colors allocated for this image. */
8403 img
->colors
= colors_in_color_table (&img
->ncolors
);
8404 free_color_table ();
8405 #endif /* COLOR_TABLE_SUPPORT */
8407 fn_g_object_unref (pixbuf
);
8410 img
->height
= height
;
8412 /* Maybe fill in the background field while we have ximg handy.
8413 Casting avoids a GCC warning. */
8414 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8416 /* Put the image into the pixmap, then free the X image and its
8418 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8419 x_destroy_x_image (ximg
);
8424 fn_g_object_unref (rsvg_handle
);
8425 /* FIXME: Use error->message so the user knows what is the actual
8426 problem with the image. */
8427 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8428 fn_g_error_free (err
);
8432 #endif /* defined (HAVE_RSVG) */
8437 /***********************************************************************
8439 ***********************************************************************/
8441 #ifdef HAVE_X_WINDOWS
8442 #define HAVE_GHOSTSCRIPT 1
8443 #endif /* HAVE_X_WINDOWS */
8445 #ifdef HAVE_GHOSTSCRIPT
8447 static bool gs_image_p (Lisp_Object object
);
8448 static bool gs_load (struct frame
*f
, struct image
*img
);
8449 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8451 /* Keyword symbols. */
8453 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8455 /* Indices of image specification fields in gs_format, below. */
8457 enum gs_keyword_index
8475 /* Vector of image_keyword structures describing the format
8476 of valid user-defined image specifications. */
8478 static const struct image_keyword gs_format
[GS_LAST
] =
8480 {":type", IMAGE_SYMBOL_VALUE
, 1},
8481 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8482 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8483 {":file", IMAGE_STRING_VALUE
, 1},
8484 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8485 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8486 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8487 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8488 {":relief", IMAGE_INTEGER_VALUE
, 0},
8489 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8490 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8491 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8492 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8495 /* Structure describing the image type `ghostscript'. */
8497 static struct image_type gs_type
=
8508 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8511 gs_clear_image (struct frame
*f
, struct image
*img
)
8513 x_clear_image (f
, img
);
8517 /* Return true if OBJECT is a valid Ghostscript image
8521 gs_image_p (Lisp_Object object
)
8523 struct image_keyword fmt
[GS_LAST
];
8527 memcpy (fmt
, gs_format
, sizeof fmt
);
8529 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8532 /* Bounding box must be a list or vector containing 4 integers. */
8533 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8536 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8537 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8542 else if (VECTORP (tem
))
8544 if (ASIZE (tem
) != 4)
8546 for (i
= 0; i
< 4; ++i
)
8547 if (!INTEGERP (AREF (tem
, i
)))
8557 /* Load Ghostscript image IMG for use on frame F. Value is true
8561 gs_load (struct frame
*f
, struct image
*img
)
8563 uprintmax_t printnum1
, printnum2
;
8564 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8565 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8567 double in_width
, in_height
;
8568 Lisp_Object pixel_colors
= Qnil
;
8570 /* Compute pixel size of pixmap needed from the given size in the
8571 image specification. Sizes in the specification are in pt. 1 pt
8572 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8574 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8575 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8576 in_width
*= FRAME_X_DISPLAY_INFO (f
)->resx
;
8577 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8578 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8579 in_height
*= FRAME_X_DISPLAY_INFO (f
)->resy
;
8581 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8582 && check_image_size (f
, in_width
, in_height
)))
8584 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8587 img
->width
= in_width
;
8588 img
->height
= in_height
;
8590 /* Create the pixmap. */
8591 eassert (img
->pixmap
== NO_PIXMAP
);
8593 if (x_check_image_size (0, img
->width
, img
->height
))
8595 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8597 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8598 img
->width
, img
->height
,
8599 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8605 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8609 /* Call the loader to fill the pixmap. It returns a process object
8610 if successful. We do not record_unwind_protect here because
8611 other places in redisplay like calling window scroll functions
8612 don't either. Let the Lisp loader use `unwind-protect' instead. */
8613 printnum1
= FRAME_X_WINDOW (f
);
8614 printnum2
= img
->pixmap
;
8615 window_and_pixmap_id
8616 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8618 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8619 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8621 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8623 XSETFRAME (frame
, f
);
8624 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8626 loader
= intern ("gs-load-image");
8628 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8629 make_number (img
->width
),
8630 make_number (img
->height
),
8631 window_and_pixmap_id
,
8633 return PROCESSP (img
->lisp_data
);
8637 /* Kill the Ghostscript process that was started to fill PIXMAP on
8638 frame F. Called from XTread_socket when receiving an event
8639 telling Emacs that Ghostscript has finished drawing. */
8642 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8644 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8649 /* Find the image containing PIXMAP. */
8650 for (i
= 0; i
< c
->used
; ++i
)
8651 if (c
->images
[i
]->pixmap
== pixmap
)
8654 /* Should someone in between have cleared the image cache, for
8655 instance, give up. */
8659 /* Kill the GS process. We should have found PIXMAP in the image
8660 cache and its image should contain a process object. */
8662 eassert (PROCESSP (img
->lisp_data
));
8663 Fkill_process (img
->lisp_data
, Qnil
);
8664 img
->lisp_data
= Qnil
;
8666 #if defined (HAVE_X_WINDOWS)
8668 /* On displays with a mutable colormap, figure out the colors
8669 allocated for the image by looking at the pixels of an XImage for
8671 class = FRAME_X_VISUAL (f
)->class;
8672 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8678 /* Try to get an XImage for img->pixmep. */
8679 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8680 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8685 /* Initialize the color table. */
8686 init_color_table ();
8688 /* For each pixel of the image, look its color up in the
8689 color table. After having done so, the color table will
8690 contain an entry for each color used by the image. */
8691 for (y
= 0; y
< img
->height
; ++y
)
8692 for (x
= 0; x
< img
->width
; ++x
)
8694 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8695 lookup_pixel_color (f
, pixel
);
8698 /* Record colors in the image. Free color table and XImage. */
8699 #ifdef COLOR_TABLE_SUPPORT
8700 img
->colors
= colors_in_color_table (&img
->ncolors
);
8701 free_color_table ();
8703 XDestroyImage (ximg
);
8705 #if 0 /* This doesn't seem to be the case. If we free the colors
8706 here, we get a BadAccess later in x_clear_image when
8707 freeing the colors. */
8708 /* We have allocated colors once, but Ghostscript has also
8709 allocated colors on behalf of us. So, to get the
8710 reference counts right, free them once. */
8712 x_free_colors (f
, img
->colors
, img
->ncolors
);
8716 image_error ("Cannot get X image of `%s'; colors will not be freed",
8721 #endif /* HAVE_X_WINDOWS */
8723 /* Now that we have the pixmap, compute mask and transform the
8724 image if requested. */
8726 postprocess_image (f
, img
);
8730 #endif /* HAVE_GHOSTSCRIPT */
8733 /***********************************************************************
8735 ***********************************************************************/
8739 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8740 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8743 return valid_image_p (spec
) ? Qt
: Qnil
;
8747 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8752 if (valid_image_p (spec
))
8753 id
= lookup_image (SELECTED_FRAME (), spec
);
8756 return make_number (id
);
8759 #endif /* GLYPH_DEBUG */
8762 /***********************************************************************
8764 ***********************************************************************/
8766 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 1, 1, 0,
8767 doc
: /* Initialize image library implementing image type TYPE.
8768 Return non-nil if TYPE is a supported image type.
8770 If image libraries are loaded dynamically (currently only the case on
8771 MS-Windows), load the library for TYPE if it is not yet loaded, using
8772 the library file(s) specified by `dynamic-library-alist'. */)
8775 return lookup_image_type (type
) ? Qt
: Qnil
;
8778 /* Look up image type TYPE, and return a pointer to its image_type
8779 structure. Return 0 if TYPE is not a known image type. */
8781 static struct image_type
*
8782 lookup_image_type (Lisp_Object type
)
8784 /* Types pbm and xbm are built-in and always available. */
8785 if (EQ (type
, Qpbm
))
8786 return define_image_type (&pbm_type
);
8788 if (EQ (type
, Qxbm
))
8789 return define_image_type (&xbm_type
);
8791 #if defined (HAVE_XPM) || defined (HAVE_NS)
8792 if (EQ (type
, Qxpm
))
8793 return define_image_type (&xpm_type
);
8796 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8797 if (EQ (type
, Qjpeg
))
8798 return define_image_type (&jpeg_type
);
8801 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8802 if (EQ (type
, Qtiff
))
8803 return define_image_type (&tiff_type
);
8806 #if defined (HAVE_GIF) || defined (HAVE_NS)
8807 if (EQ (type
, Qgif
))
8808 return define_image_type (&gif_type
);
8811 #if defined (HAVE_PNG) || defined (HAVE_NS)
8812 if (EQ (type
, Qpng
))
8813 return define_image_type (&png_type
);
8816 #if defined (HAVE_RSVG)
8817 if (EQ (type
, Qsvg
))
8818 return define_image_type (&svg_type
);
8821 #if defined (HAVE_IMAGEMAGICK)
8822 if (EQ (type
, Qimagemagick
))
8823 return define_image_type (&imagemagick_type
);
8826 #ifdef HAVE_GHOSTSCRIPT
8827 if (EQ (type
, Qpostscript
))
8828 return define_image_type (&gs_type
);
8834 /* Reset image_types before dumping.
8835 Called from Fdump_emacs. */
8838 reset_image_types (void)
8842 struct image_type
*next
= image_types
->next
;
8843 xfree (image_types
);
8849 syms_of_image (void)
8851 /* Initialize this only once; it will be reset before dumping. */
8854 /* Must be defined now because we're going to update it below, while
8855 defining the supported image types. */
8856 DEFVAR_LISP ("image-types", Vimage_types
,
8857 doc
: /* List of potentially supported image types.
8858 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8859 To check whether it is really supported, use `image-type-available-p'. */);
8860 Vimage_types
= Qnil
;
8862 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8863 doc
: /* Maximum size of images.
8864 Emacs will not load an image into memory if its pixel width or
8865 pixel height exceeds this limit.
8867 If the value is an integer, it directly specifies the maximum
8868 image height and width, measured in pixels. If it is a floating
8869 point number, it specifies the maximum image height and width
8870 as a ratio to the frame height and width. If the value is
8871 non-numeric, there is no explicit limit on the size of images. */);
8872 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8874 DEFSYM (Qcount
, "count");
8875 DEFSYM (Qextension_data
, "extension-data");
8876 DEFSYM (Qdelay
, "delay");
8878 DEFSYM (QCascent
, ":ascent");
8879 DEFSYM (QCmargin
, ":margin");
8880 DEFSYM (QCrelief
, ":relief");
8881 DEFSYM (QCconversion
, ":conversion");
8882 DEFSYM (QCcolor_symbols
, ":color-symbols");
8883 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8884 DEFSYM (QCindex
, ":index");
8885 DEFSYM (QCgeometry
, ":geometry");
8886 DEFSYM (QCcrop
, ":crop");
8887 DEFSYM (QCrotation
, ":rotation");
8888 DEFSYM (QCmatrix
, ":matrix");
8889 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8890 DEFSYM (QCmask
, ":mask");
8892 DEFSYM (Qlaplace
, "laplace");
8893 DEFSYM (Qemboss
, "emboss");
8894 DEFSYM (Qedge_detection
, "edge-detection");
8895 DEFSYM (Qheuristic
, "heuristic");
8897 DEFSYM (Qpostscript
, "postscript");
8898 #ifdef HAVE_GHOSTSCRIPT
8899 ADD_IMAGE_TYPE (Qpostscript
);
8900 DEFSYM (QCloader
, ":loader");
8901 DEFSYM (QCbounding_box
, ":bounding-box");
8902 DEFSYM (QCpt_width
, ":pt-width");
8903 DEFSYM (QCpt_height
, ":pt-height");
8904 #endif /* HAVE_GHOSTSCRIPT */
8907 DEFSYM (Qlibpng_version
, "libpng-version");
8908 Fset (Qlibpng_version
,
8910 make_number (PNG_LIBPNG_VER
)
8917 DEFSYM (Qpbm
, "pbm");
8918 ADD_IMAGE_TYPE (Qpbm
);
8920 DEFSYM (Qxbm
, "xbm");
8921 ADD_IMAGE_TYPE (Qxbm
);
8923 #if defined (HAVE_XPM) || defined (HAVE_NS)
8924 DEFSYM (Qxpm
, "xpm");
8925 ADD_IMAGE_TYPE (Qxpm
);
8928 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8929 DEFSYM (Qjpeg
, "jpeg");
8930 ADD_IMAGE_TYPE (Qjpeg
);
8933 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8934 DEFSYM (Qtiff
, "tiff");
8935 ADD_IMAGE_TYPE (Qtiff
);
8938 #if defined (HAVE_GIF) || defined (HAVE_NS)
8939 DEFSYM (Qgif
, "gif");
8940 ADD_IMAGE_TYPE (Qgif
);
8943 #if defined (HAVE_PNG) || defined (HAVE_NS)
8944 DEFSYM (Qpng
, "png");
8945 ADD_IMAGE_TYPE (Qpng
);
8948 #if defined (HAVE_IMAGEMAGICK)
8949 DEFSYM (Qimagemagick
, "imagemagick");
8950 ADD_IMAGE_TYPE (Qimagemagick
);
8953 #if defined (HAVE_RSVG)
8954 DEFSYM (Qsvg
, "svg");
8955 ADD_IMAGE_TYPE (Qsvg
);
8957 /* Other libraries used directly by svg code. */
8958 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8959 DEFSYM (Qglib
, "glib");
8960 DEFSYM (Qgobject
, "gobject");
8961 #endif /* HAVE_NTGUI */
8962 #endif /* HAVE_RSVG */
8964 defsubr (&Sinit_image_library
);
8965 #ifdef HAVE_IMAGEMAGICK
8966 defsubr (&Simagemagick_types
);
8968 defsubr (&Sclear_image_cache
);
8969 defsubr (&Simage_flush
);
8970 defsubr (&Simage_size
);
8971 defsubr (&Simage_mask_p
);
8972 defsubr (&Simage_metadata
);
8976 defsubr (&Slookup_image
);
8979 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8980 doc
: /* Non-nil means always draw a cross over disabled images.
8981 Disabled images are those having a `:conversion disabled' property.
8982 A cross is always drawn on black & white displays. */);
8983 cross_disabled_images
= 0;
8985 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8986 doc
: /* List of directories to search for window system bitmap files. */);
8987 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8989 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8990 doc
: /* Maximum time after which images are removed from the cache.
8991 When an image has not been displayed this many seconds, Emacs
8992 automatically removes it from the image cache. If the cache contains
8993 a large number of images, the actual eviction time may be shorter.
8994 The value can also be nil, meaning the cache is never cleared.
8996 The function `clear-image-cache' disregards this variable. */);
8997 Vimage_cache_eviction_delay
= make_number (300);
8998 #ifdef HAVE_IMAGEMAGICK
8999 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9000 doc
: /* Integer indicating which ImageMagick rendering method to use.
9002 0 -- the default method (pixel pushing)
9003 1 -- a newer method ("MagickExportImagePixels") that may perform
9004 better (speed etc) in some cases, but has not been as thoroughly
9005 tested with Emacs as the default method. This method requires
9006 ImageMagick version 6.4.6 (approximately) or later.
9008 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9009 imagemagick_render_type
= 0;