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/>. */
26 #if defined HAVE_LIBPNG_PNG_H
27 # include <libpng/png.h>
37 /* This makes the fields of a Display accessible, in Xlib header files. */
39 #define XLIB_ILLEGAL_ACCESS
44 #include "dispextern.h"
45 #include "blockinput.h"
48 #include "character.h"
50 #include "termhooks.h"
53 #ifdef HAVE_SYS_STAT_H
55 #endif /* HAVE_SYS_STAT_H */
57 #ifdef HAVE_SYS_TYPES_H
58 #include <sys/types.h>
59 #endif /* HAVE_SYS_TYPES_H */
61 #ifdef HAVE_WINDOW_SYSTEM
63 #endif /* HAVE_WINDOW_SYSTEM */
66 #define COLOR_TABLE_SUPPORT 1
68 typedef struct x_bitmap_record Bitmap_Record
;
69 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
70 #define NO_PIXMAP None
72 #define RGB_PIXEL_COLOR unsigned long
74 #define PIX_MASK_RETAIN 0
75 #define PIX_MASK_DRAW 1
76 #endif /* HAVE_X_WINDOWS */
80 /* W32_TODO : Color tables on W32. */
81 #undef COLOR_TABLE_SUPPORT
83 typedef struct w32_bitmap_record Bitmap_Record
;
84 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
87 #define RGB_PIXEL_COLOR COLORREF
89 #define PIX_MASK_RETAIN 0
90 #define PIX_MASK_DRAW 1
92 #define x_defined_color w32_defined_color
93 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
95 /* Version of libpng that we were compiled with, or -1 if no PNG
96 support was compiled in. This is tested by w32-win.el to correctly
97 set up the alist used to search for PNG libraries. */
98 Lisp_Object Qlibpng_version
;
99 #endif /* HAVE_NTGUI */
102 #undef COLOR_TABLE_SUPPORT
104 typedef struct ns_bitmap_record Bitmap_Record
;
106 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
109 #define RGB_PIXEL_COLOR unsigned long
112 #define PIX_MASK_RETAIN 0
113 #define PIX_MASK_DRAW 1
115 #define x_defined_color(f, name, color_def, alloc) \
116 ns_defined_color (f, name, color_def, alloc, 0)
117 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
121 /* The symbol `postscript' identifying images of this type. */
123 static Lisp_Object Qpostscript
;
125 static void x_disable_image (struct frame
*, struct image
*);
126 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
129 static void init_color_table (void);
130 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
131 #ifdef COLOR_TABLE_SUPPORT
132 static void free_color_table (void);
133 static unsigned long *colors_in_color_table (int *n
);
136 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
137 id, which is just an int that this section returns. Bitmaps are
138 reference counted so they can be shared among frames.
140 Bitmap indices are guaranteed to be > 0, so a negative number can
141 be used to indicate no bitmap.
143 If you use x_create_bitmap_from_data, then you must keep track of
144 the bitmaps yourself. That is, creating a bitmap from the same
145 data more than once will not be caught. */
149 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
150 unsigned int width
, unsigned int height
,
151 unsigned long plane_mask
, int format
)
153 /* TODO: not sure what this function is supposed to do.. */
154 ns_retain_object (pixmap
);
158 /* use with imgs created by ns_image_for_XPM */
160 XGetPixel (XImagePtr ximage
, int x
, int y
)
162 return ns_get_pixel (ximage
, x
, y
);
165 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
166 pixel is assumed to be in form RGB */
168 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
170 ns_put_pixel (ximage
, x
, y
, pixel
);
175 /* Functions to access the contents of a bitmap, given an id. */
178 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
180 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
184 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
186 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
189 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
191 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
193 /* HAVE_NTGUI needs the explicit cast here. */
194 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
198 #ifdef HAVE_X_WINDOWS
200 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
202 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
206 /* Allocate a new bitmap record. Returns index of new record. */
209 x_allocate_bitmap_record (FRAME_PTR f
)
211 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
214 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
215 return ++dpyinfo
->bitmaps_last
;
217 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
218 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
222 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
223 10, -1, sizeof *dpyinfo
->bitmaps
);
224 return ++dpyinfo
->bitmaps_last
;
227 /* Add one reference to the reference count of the bitmap with id ID. */
230 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
232 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
235 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
238 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
240 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
243 #ifdef HAVE_X_WINDOWS
245 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
246 bits
, width
, height
);
249 #endif /* HAVE_X_WINDOWS */
253 bitmap
= CreateBitmap (width
, height
,
254 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
255 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
259 #endif /* HAVE_NTGUI */
262 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
267 id
= x_allocate_bitmap_record (f
);
270 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
271 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
274 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
275 dpyinfo
->bitmaps
[id
- 1].height
= height
;
276 dpyinfo
->bitmaps
[id
- 1].width
= width
;
277 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
279 #ifdef HAVE_X_WINDOWS
280 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
281 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
282 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
283 #endif /* HAVE_X_WINDOWS */
286 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
287 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
288 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
289 #endif /* HAVE_NTGUI */
294 /* Create bitmap from file FILE for frame F. */
297 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
299 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
302 return -1; /* W32_TODO : bitmap support */
303 #endif /* HAVE_NTGUI */
307 void *bitmap
= ns_image_from_file (file
);
313 id
= x_allocate_bitmap_record (f
);
314 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
315 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
316 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
317 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
318 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
319 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
320 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
324 #ifdef HAVE_X_WINDOWS
325 unsigned int width
, height
;
327 int xhot
, yhot
, result
;
333 /* Look for an existing bitmap with the same name. */
334 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
336 if (dpyinfo
->bitmaps
[id
].refcount
337 && dpyinfo
->bitmaps
[id
].file
338 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
340 ++dpyinfo
->bitmaps
[id
].refcount
;
345 /* Search bitmap-file-path for the file, if appropriate. */
346 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
351 filename
= SSDATA (found
);
353 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
354 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
355 if (result
!= BitmapSuccess
)
358 id
= x_allocate_bitmap_record (f
);
359 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
360 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
361 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
362 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
363 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
364 dpyinfo
->bitmaps
[id
- 1].height
= height
;
365 dpyinfo
->bitmaps
[id
- 1].width
= width
;
366 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
369 #endif /* HAVE_X_WINDOWS */
375 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
377 #ifdef HAVE_X_WINDOWS
378 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
380 XFreePixmap (dpyinfo
->display
, bm
->mask
);
381 #endif /* HAVE_X_WINDOWS */
384 DeleteObject (bm
->pixmap
);
385 #endif /* HAVE_NTGUI */
388 ns_release_object (bm
->img
);
398 /* Remove reference to bitmap with id number ID. */
401 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
403 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
407 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
409 if (--bm
->refcount
== 0)
412 free_bitmap_record (dpyinfo
, bm
);
418 /* Free all the bitmaps for the display specified by DPYINFO. */
421 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
424 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
426 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
427 if (bm
->refcount
> 0)
428 free_bitmap_record (dpyinfo
, bm
);
430 dpyinfo
->bitmaps_last
= 0;
434 #ifdef HAVE_X_WINDOWS
436 /* Useful functions defined in the section
437 `Image type independent image structures' below. */
439 static unsigned long four_corners_best (XImagePtr ximg
,
442 unsigned long height
);
444 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
445 int depth
, XImagePtr
*ximg
,
448 static void x_destroy_x_image (XImagePtr ximg
);
451 /* Create a mask of a bitmap. Note is this not a perfect mask.
452 It's nicer with some borders in this context */
455 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
458 XImagePtr ximg
, mask_img
;
459 unsigned long width
, height
;
462 unsigned long x
, y
, xp
, xm
, yp
, ym
;
465 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
470 pixmap
= x_bitmap_pixmap (f
, id
);
471 width
= x_bitmap_width (f
, id
);
472 height
= x_bitmap_height (f
, id
);
475 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
484 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
489 XDestroyImage (ximg
);
493 bg
= four_corners_best (ximg
, NULL
, width
, height
);
495 for (y
= 0; y
< ximg
->height
; ++y
)
497 for (x
= 0; x
< ximg
->width
; ++x
)
499 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
500 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
501 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
502 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
503 if (XGetPixel (ximg
, x
, y
) == bg
504 && XGetPixel (ximg
, x
, yp
) == bg
505 && XGetPixel (ximg
, x
, ym
) == bg
506 && XGetPixel (ximg
, xp
, y
) == bg
507 && XGetPixel (ximg
, xp
, yp
) == bg
508 && XGetPixel (ximg
, xp
, ym
) == bg
509 && XGetPixel (ximg
, xm
, y
) == bg
510 && XGetPixel (ximg
, xm
, yp
) == bg
511 && XGetPixel (ximg
, xm
, ym
) == bg
)
512 XPutPixel (mask_img
, x
, y
, 0);
514 XPutPixel (mask_img
, x
, y
, 1);
518 eassert (interrupt_input_blocked
);
519 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
520 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
522 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
524 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
525 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
527 XDestroyImage (ximg
);
528 x_destroy_x_image (mask_img
);
533 #endif /* HAVE_X_WINDOWS */
536 /***********************************************************************
538 ***********************************************************************/
540 /* List of supported image types. Use define_image_type to add new
541 types. Use lookup_image_type to find a type for a given symbol. */
543 static struct image_type
*image_types
;
545 /* The symbol `xbm' which is used as the type symbol for XBM images. */
547 static Lisp_Object Qxbm
;
551 Lisp_Object QCascent
, QCmargin
, QCrelief
;
552 Lisp_Object QCconversion
;
553 static Lisp_Object QCheuristic_mask
;
554 static Lisp_Object QCcolor_symbols
;
555 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
556 static Lisp_Object QCcrop
, QCrotation
;
560 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
561 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
563 /* Function prototypes. */
565 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
566 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
567 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
568 static void x_laplace (struct frame
*, struct image
*);
569 static void x_emboss (struct frame
*, struct image
*);
570 static int x_build_heuristic_mask (struct frame
*, struct image
*,
573 extern Lisp_Object Vlibrary_cache
, QCloaded_from
;
574 #define CACHE_IMAGE_TYPE(type, status) \
575 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
577 #define CACHE_IMAGE_TYPE(type, status)
580 #define ADD_IMAGE_TYPE(type) \
581 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
583 /* Define a new image type from TYPE. This adds a copy of TYPE to
584 image_types and caches the loading status of TYPE. */
587 define_image_type (struct image_type
*type
, int loaded
)
595 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
596 The initialized data segment is read-only. */
597 struct image_type
*p
= xmalloc (sizeof *p
);
598 memcpy (p
, type
, sizeof *p
);
599 p
->next
= image_types
;
604 CACHE_IMAGE_TYPE (*type
->type
, success
);
609 /* Look up image type SYMBOL, and return a pointer to its image_type
610 structure. Value is null if SYMBOL is not a known image type. */
612 static inline struct image_type
*
613 lookup_image_type (Lisp_Object symbol
)
615 struct image_type
*type
;
617 /* We must initialize the image-type if it hasn't been already. */
618 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
619 return 0; /* unimplemented */
621 for (type
= image_types
; type
; type
= type
->next
)
622 if (EQ (symbol
, *type
->type
))
629 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
630 valid image specification is a list whose car is the symbol
631 `image', and whose rest is a property list. The property list must
632 contain a value for key `:type'. That value must be the name of a
633 supported image type. The rest of the property list depends on the
637 valid_image_p (Lisp_Object object
)
645 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
646 if (EQ (XCAR (tem
), QCtype
))
649 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
651 struct image_type
*type
;
652 type
= lookup_image_type (XCAR (tem
));
654 valid_p
= type
->valid_p (object
);
665 /* Log error message with format string FORMAT and argument ARG.
666 Signaling an error, e.g. when an image cannot be loaded, is not a
667 good idea because this would interrupt redisplay, and the error
668 message display would lead to another redisplay. This function
669 therefore simply displays a message. */
672 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
674 add_to_log (format
, arg1
, arg2
);
679 /***********************************************************************
681 ***********************************************************************/
683 enum image_value_type
685 IMAGE_DONT_CHECK_VALUE_TYPE
,
687 IMAGE_STRING_OR_NIL_VALUE
,
689 IMAGE_POSITIVE_INTEGER_VALUE
,
690 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
691 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
694 IMAGE_FUNCTION_VALUE
,
699 /* Structure used when parsing image specifications. */
703 /* Name of keyword. */
706 /* The type of value allowed. */
707 enum image_value_type type
;
709 /* Non-zero means key must be present. */
712 /* Used to recognize duplicate keywords in a property list. */
715 /* The value that was found. */
720 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
722 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
725 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
726 has the format (image KEYWORD VALUE ...). One of the keyword/
727 value pairs must be `:type TYPE'. KEYWORDS is a vector of
728 image_keywords structures of size NKEYWORDS describing other
729 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
732 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
733 int nkeywords
, Lisp_Object type
)
742 while (CONSP (plist
))
744 Lisp_Object key
, value
;
746 /* First element of a pair must be a symbol. */
748 plist
= XCDR (plist
);
752 /* There must follow a value. */
755 value
= XCAR (plist
);
756 plist
= XCDR (plist
);
758 /* Find key in KEYWORDS. Error if not found. */
759 for (i
= 0; i
< nkeywords
; ++i
)
760 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
766 /* Record that we recognized the keyword. If a keywords
767 was found more than once, it's an error. */
768 keywords
[i
].value
= value
;
769 if (keywords
[i
].count
> 1)
773 /* Check type of value against allowed type. */
774 switch (keywords
[i
].type
)
776 case IMAGE_STRING_VALUE
:
777 if (!STRINGP (value
))
781 case IMAGE_STRING_OR_NIL_VALUE
:
782 if (!STRINGP (value
) && !NILP (value
))
786 case IMAGE_SYMBOL_VALUE
:
787 if (!SYMBOLP (value
))
791 case IMAGE_POSITIVE_INTEGER_VALUE
:
792 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
796 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
797 if (RANGED_INTEGERP (0, value
, INT_MAX
))
800 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
801 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
805 case IMAGE_ASCENT_VALUE
:
806 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
808 else if (RANGED_INTEGERP (0, value
, 100))
812 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
813 /* Unlike the other integer-related cases, this one does not
814 verify that VALUE fits in 'int'. This is because callers
816 if (!INTEGERP (value
) || XINT (value
) < 0)
820 case IMAGE_DONT_CHECK_VALUE_TYPE
:
823 case IMAGE_FUNCTION_VALUE
:
824 value
= indirect_function (value
);
825 if (!NILP (Ffunctionp (value
)))
829 case IMAGE_NUMBER_VALUE
:
830 if (!INTEGERP (value
) && !FLOATP (value
))
834 case IMAGE_INTEGER_VALUE
:
835 if (! TYPE_RANGED_INTEGERP (int, value
))
839 case IMAGE_BOOL_VALUE
:
840 if (!NILP (value
) && !EQ (value
, Qt
))
849 if (EQ (key
, QCtype
) && !EQ (type
, value
))
853 /* Check that all mandatory fields are present. */
854 for (i
= 0; i
< nkeywords
; ++i
)
855 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
862 /* Return the value of KEY in image specification SPEC. Value is nil
863 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
864 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
867 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
871 eassert (valid_image_p (spec
));
873 for (tail
= XCDR (spec
);
874 CONSP (tail
) && CONSP (XCDR (tail
));
875 tail
= XCDR (XCDR (tail
)))
877 if (EQ (XCAR (tail
), key
))
881 return XCAR (XCDR (tail
));
891 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
892 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
893 PIXELS non-nil means return the size in pixels, otherwise return the
894 size in canonical character units.
895 FRAME is the frame on which the image will be displayed. FRAME nil
896 or omitted means use the selected frame. */)
897 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
902 if (valid_image_p (spec
))
904 struct frame
*f
= check_x_frame (frame
);
905 ptrdiff_t id
= lookup_image (f
, spec
);
906 struct image
*img
= IMAGE_FROM_ID (f
, id
);
907 int width
= img
->width
+ 2 * img
->hmargin
;
908 int height
= img
->height
+ 2 * img
->vmargin
;
911 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
912 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
914 size
= Fcons (make_number (width
), make_number (height
));
917 error ("Invalid image specification");
923 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
924 doc
: /* Return t if image SPEC has a mask bitmap.
925 FRAME is the frame on which the image will be displayed. FRAME nil
926 or omitted means use the selected frame. */)
927 (Lisp_Object spec
, Lisp_Object frame
)
932 if (valid_image_p (spec
))
934 struct frame
*f
= check_x_frame (frame
);
935 ptrdiff_t id
= lookup_image (f
, spec
);
936 struct image
*img
= IMAGE_FROM_ID (f
, id
);
941 error ("Invalid image specification");
946 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
947 doc
: /* Return metadata for image SPEC.
948 FRAME is the frame on which the image will be displayed. FRAME nil
949 or omitted means use the selected frame. */)
950 (Lisp_Object spec
, Lisp_Object frame
)
955 if (valid_image_p (spec
))
957 struct frame
*f
= check_x_frame (frame
);
958 ptrdiff_t id
= lookup_image (f
, spec
);
959 struct image
*img
= IMAGE_FROM_ID (f
, id
);
960 ext
= img
->lisp_data
;
967 /***********************************************************************
968 Image type independent image structures
969 ***********************************************************************/
971 static void free_image (struct frame
*f
, struct image
*img
);
973 #define MAX_IMAGE_SIZE 10.0
974 /* Allocate and return a new image structure for image specification
975 SPEC. SPEC has a hash value of HASH. */
977 static struct image
*
978 make_image (Lisp_Object spec
, EMACS_UINT hash
)
980 struct image
*img
= xzalloc (sizeof *img
);
981 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
983 eassert (valid_image_p (spec
));
984 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
985 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
986 eassert (img
->type
!= NULL
);
988 img
->lisp_data
= Qnil
;
989 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
991 img
->corners
[BOT_CORNER
] = -1; /* Full image */
996 /* Free image IMG which was used on frame F, including its resources. */
999 free_image (struct frame
*f
, struct image
*img
)
1003 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1005 /* Remove IMG from the hash table of its cache. */
1007 img
->prev
->next
= img
->next
;
1009 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1012 img
->next
->prev
= img
->prev
;
1014 c
->images
[img
->id
] = NULL
;
1016 /* Free resources, then free IMG. */
1017 img
->type
->free (f
, img
);
1022 /* Return 1 if the given widths and heights are valid for display;
1023 otherwise, return 0. */
1026 check_image_size (struct frame
*f
, int width
, int height
)
1030 if (width
<= 0 || height
<= 0)
1033 if (INTEGERP (Vmax_image_size
))
1034 return (width
<= XINT (Vmax_image_size
)
1035 && height
<= XINT (Vmax_image_size
));
1036 else if (FLOATP (Vmax_image_size
))
1040 w
= FRAME_PIXEL_WIDTH (f
);
1041 h
= FRAME_PIXEL_HEIGHT (f
);
1044 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1045 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1046 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1052 /* Prepare image IMG for display on frame F. Must be called before
1053 drawing an image. */
1056 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1058 /* We're about to display IMG, so set its timestamp to `now'. */
1059 img
->timestamp
= current_emacs_time ();
1061 /* If IMG doesn't have a pixmap yet, load it now, using the image
1062 type dependent loader function. */
1063 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1064 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1069 /* Value is the number of pixels for the ascent of image IMG when
1070 drawn in face FACE. */
1073 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1078 if (slice
->height
== img
->height
)
1079 height
= img
->height
+ img
->vmargin
;
1080 else if (slice
->y
== 0)
1081 height
= slice
->height
+ img
->vmargin
;
1083 height
= slice
->height
;
1085 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1090 /* W32 specific version. Why?. ++kfs */
1091 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1092 - FONT_BASE (face
->font
)) / 2;
1094 /* This expression is arranged so that if the image can't be
1095 exactly centered, it will be moved slightly up. This is
1096 because a typical font is `top-heavy' (due to the presence
1097 uppercase letters), so the image placement should err towards
1098 being top-heavy too. It also just generally looks better. */
1099 ascent
= (height
+ FONT_BASE (face
->font
)
1100 - FONT_DESCENT (face
->font
) + 1) / 2;
1101 #endif /* HAVE_NTGUI */
1104 ascent
= height
/ 2;
1107 ascent
= height
* (img
->ascent
/ 100.0);
1113 /* Image background colors. */
1115 /* Find the "best" corner color of a bitmap.
1116 On W32, XIMG is assumed to a device context with the bitmap selected. */
1118 static RGB_PIXEL_COLOR
1119 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1120 unsigned long width
, unsigned long height
)
1122 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1125 if (corners
&& corners
[BOT_CORNER
] >= 0)
1127 /* Get the colors at the corner_pixels of ximg. */
1128 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1129 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1130 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1131 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1135 /* Get the colors at the corner_pixels of ximg. */
1136 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1137 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1138 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1139 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1141 /* Choose the most frequently found color as background. */
1142 for (i
= best_count
= 0; i
< 4; ++i
)
1146 for (j
= n
= 0; j
< 4; ++j
)
1147 if (corner_pixels
[i
] == corner_pixels
[j
])
1151 best
= corner_pixels
[i
], best_count
= n
;
1157 /* Portability macros */
1161 #define Destroy_Image(img_dc, prev) \
1162 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1164 #define Free_Pixmap(display, pixmap) \
1165 DeleteObject (pixmap)
1167 #elif defined (HAVE_NS)
1169 #define Destroy_Image(ximg, dummy) \
1170 ns_release_object (ximg)
1172 #define Free_Pixmap(display, pixmap) \
1173 ns_release_object (pixmap)
1177 #define Destroy_Image(ximg, dummy) \
1178 XDestroyImage (ximg)
1180 #define Free_Pixmap(display, pixmap) \
1181 XFreePixmap (display, pixmap)
1183 #endif /* !HAVE_NTGUI && !HAVE_NS */
1186 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1187 it is guessed heuristically. If non-zero, XIMG is an existing
1188 XImage object (or device context with the image selected on W32) to
1189 use for the heuristic. */
1192 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1194 if (! img
->background_valid
)
1195 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1197 int free_ximg
= !ximg
;
1200 #endif /* HAVE_NTGUI */
1205 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1206 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1208 HDC frame_dc
= get_frame_dc (f
);
1209 ximg
= CreateCompatibleDC (frame_dc
);
1210 release_frame_dc (f
, frame_dc
);
1211 prev
= SelectObject (ximg
, img
->pixmap
);
1212 #endif /* !HAVE_NTGUI */
1215 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1218 Destroy_Image (ximg
, prev
);
1220 img
->background_valid
= 1;
1223 return img
->background
;
1226 /* Return the `background_transparent' field of IMG. If IMG doesn't
1227 have one yet, it is guessed heuristically. If non-zero, MASK is an
1228 existing XImage object to use for the heuristic. */
1231 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1233 if (! img
->background_transparent_valid
)
1234 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1238 int free_mask
= !mask
;
1241 #endif /* HAVE_NTGUI */
1246 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1247 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1249 HDC frame_dc
= get_frame_dc (f
);
1250 mask
= CreateCompatibleDC (frame_dc
);
1251 release_frame_dc (f
, frame_dc
);
1252 prev
= SelectObject (mask
, img
->mask
);
1253 #endif /* HAVE_NTGUI */
1256 img
->background_transparent
1257 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1260 Destroy_Image (mask
, prev
);
1263 img
->background_transparent
= 0;
1265 img
->background_transparent_valid
= 1;
1268 return img
->background_transparent
;
1272 /***********************************************************************
1273 Helper functions for X image types
1274 ***********************************************************************/
1276 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1278 static void x_clear_image (struct frame
*f
, struct image
*img
);
1279 static unsigned long x_alloc_image_color (struct frame
*f
,
1281 Lisp_Object color_name
,
1282 unsigned long dflt
);
1285 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1286 free the pixmap if any. MASK_P non-zero means clear the mask
1287 pixmap if any. COLORS_P non-zero means free colors allocated for
1288 the image, if any. */
1291 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1294 if (pixmap_p
&& img
->pixmap
)
1296 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1297 img
->pixmap
= NO_PIXMAP
;
1298 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1299 img
->background_valid
= 0;
1302 if (mask_p
&& img
->mask
)
1304 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1305 img
->mask
= NO_PIXMAP
;
1306 img
->background_transparent_valid
= 0;
1309 if (colors_p
&& img
->ncolors
)
1311 /* W32_TODO: color table support. */
1312 #ifdef HAVE_X_WINDOWS
1313 x_free_colors (f
, img
->colors
, img
->ncolors
);
1314 #endif /* HAVE_X_WINDOWS */
1315 xfree (img
->colors
);
1322 /* Free X resources of image IMG which is used on frame F. */
1325 x_clear_image (struct frame
*f
, struct image
*img
)
1328 x_clear_image_1 (f
, img
, 1, 1, 1);
1333 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1334 cannot be allocated, use DFLT. Add a newly allocated color to
1335 IMG->colors, so that it can be freed again. Value is the pixel
1338 static unsigned long
1339 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1343 unsigned long result
;
1345 eassert (STRINGP (color_name
));
1347 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1348 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1351 /* This isn't called frequently so we get away with simply
1352 reallocating the color vector to the needed size, here. */
1353 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1354 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1355 img
->colors
[ncolors
- 1] = color
.pixel
;
1356 img
->ncolors
= ncolors
;
1357 result
= color
.pixel
;
1367 /***********************************************************************
1369 ***********************************************************************/
1371 static void cache_image (struct frame
*f
, struct image
*img
);
1372 static void postprocess_image (struct frame
*, struct image
*);
1374 /* Return a new, initialized image cache that is allocated from the
1375 heap. Call free_image_cache to free an image cache. */
1377 struct image_cache
*
1378 make_image_cache (void)
1380 struct image_cache
*c
= xzalloc (sizeof *c
);
1384 c
->images
= xmalloc (size
* sizeof *c
->images
);
1386 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1387 c
->buckets
= xzalloc (size
);
1392 /* Find an image matching SPEC in the cache, and return it. If no
1393 image is found, return NULL. */
1394 static struct image
*
1395 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1398 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1399 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1401 if (!c
) return NULL
;
1403 /* If the image spec does not specify a background color, the cached
1404 image must have the same background color as the current frame.
1405 The foreground color must also match, for the sake of monochrome
1408 In fact, we could ignore the foreground color matching condition
1409 for color images, or if the image spec specifies :foreground;
1410 similarly we could ignore the background color matching condition
1411 for formats that don't use transparency (such as jpeg), or if the
1412 image spec specifies :background. However, the extra memory
1413 usage is probably negligible in practice, so we don't bother. */
1415 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1416 if (img
->hash
== hash
1417 && !NILP (Fequal (img
->spec
, spec
))
1418 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1419 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1425 /* Search frame F for an image with spec SPEC, and free it. */
1428 uncache_image (struct frame
*f
, Lisp_Object spec
)
1430 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1433 free_image (f
, img
);
1434 /* As display glyphs may still be referring to the image ID, we
1435 must garbage the frame (Bug#6426). */
1436 SET_FRAME_GARBAGED (f
);
1441 /* Free image cache of frame F. Be aware that X frames share images
1445 free_image_cache (struct frame
*f
)
1447 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1452 /* Cache should not be referenced by any frame when freed. */
1453 eassert (c
->refcount
== 0);
1455 for (i
= 0; i
< c
->used
; ++i
)
1456 free_image (f
, c
->images
[i
]);
1460 FRAME_IMAGE_CACHE (f
) = NULL
;
1465 /* Clear image cache of frame F. FILTER=t means free all images.
1466 FILTER=nil means clear only images that haven't been
1467 displayed for some time.
1468 Else, only free the images which have FILTER in their `dependencies'.
1469 Should be called from time to time to reduce the number of loaded images.
1470 If image-cache-eviction-delay is non-nil, this frees images in the cache
1471 which weren't displayed for at least that many seconds. */
1474 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1476 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1480 ptrdiff_t i
, nfreed
= 0;
1482 /* Block input so that we won't be interrupted by a SIGIO
1483 while being in an inconsistent state. */
1488 /* Filter image cache. */
1489 for (i
= 0; i
< c
->used
; ++i
)
1491 struct image
*img
= c
->images
[i
];
1492 if (img
&& (EQ (Qt
, filter
)
1493 || !NILP (Fmember (filter
, img
->dependencies
))))
1495 free_image (f
, img
);
1500 else if (INTEGERP (Vimage_cache_eviction_delay
))
1502 /* Free cache based on timestamp. */
1505 ptrdiff_t nimages
= 0;
1507 for (i
= 0; i
< c
->used
; ++i
)
1511 /* If the number of cached images has grown unusually large,
1512 decrease the cache eviction delay (Bug#6230). */
1513 delay
= XINT (Vimage_cache_eviction_delay
);
1515 delay
= 1600 * delay
/ nimages
/ nimages
;
1516 delay
= max (delay
, 1);
1518 t
= current_emacs_time ();
1519 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1521 for (i
= 0; i
< c
->used
; ++i
)
1523 struct image
*img
= c
->images
[i
];
1524 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1526 free_image (f
, img
);
1532 /* We may be clearing the image cache because, for example,
1533 Emacs was iconified for a longer period of time. In that
1534 case, current matrices may still contain references to
1535 images freed above. So, clear these matrices. */
1538 Lisp_Object tail
, frame
;
1540 FOR_EACH_FRAME (tail
, frame
)
1542 struct frame
*fr
= XFRAME (frame
);
1543 if (FRAME_IMAGE_CACHE (fr
) == c
)
1544 clear_current_matrices (fr
);
1547 ++windows_or_buffers_changed
;
1555 clear_image_caches (Lisp_Object filter
)
1557 /* FIXME: We want to do
1558 * struct terminal *t;
1559 * for (t = terminal_list; t; t = t->next_terminal)
1560 * clear_image_cache (t, filter); */
1561 Lisp_Object tail
, frame
;
1562 FOR_EACH_FRAME (tail
, frame
)
1563 if (FRAME_WINDOW_P (XFRAME (frame
)))
1564 clear_image_cache (XFRAME (frame
), filter
);
1567 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1569 doc
: /* Clear the image cache.
1570 FILTER nil or a frame means clear all images in the selected frame.
1571 FILTER t means clear the image caches of all frames.
1572 Anything else, means only clear those images which refer to FILTER,
1573 which is then usually a filename. */)
1574 (Lisp_Object filter
)
1576 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1577 clear_image_caches (filter
);
1579 clear_image_cache (check_x_frame (filter
), Qt
);
1585 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1587 doc
: /* Fush the image with specification SPEC on frame FRAME.
1588 This removes the image from the Emacs image cache. If SPEC specifies
1589 an image file, the next redisplay of this image will read from the
1590 current contents of that file.
1592 FRAME nil or omitted means use the selected frame.
1593 FRAME t means refresh the image on all frames. */)
1594 (Lisp_Object spec
, Lisp_Object frame
)
1596 if (!valid_image_p (spec
))
1597 error ("Invalid image specification");
1602 FOR_EACH_FRAME (tail
, frame
)
1604 struct frame
*f
= XFRAME (frame
);
1605 if (FRAME_WINDOW_P (f
))
1606 uncache_image (f
, spec
);
1610 uncache_image (check_x_frame (frame
), spec
);
1616 /* Compute masks and transform image IMG on frame F, as specified
1617 by the image's specification, */
1620 postprocess_image (struct frame
*f
, struct image
*img
)
1622 /* Manipulation of the image's mask. */
1625 Lisp_Object conversion
, spec
;
1630 /* `:heuristic-mask t'
1632 means build a mask heuristically.
1633 `:heuristic-mask (R G B)'
1634 `:mask (heuristic (R G B))'
1635 means build a mask from color (R G B) in the
1638 means remove a mask, if any. */
1640 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1642 x_build_heuristic_mask (f
, img
, mask
);
1647 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1649 if (EQ (mask
, Qheuristic
))
1650 x_build_heuristic_mask (f
, img
, Qt
);
1651 else if (CONSP (mask
)
1652 && EQ (XCAR (mask
), Qheuristic
))
1654 if (CONSP (XCDR (mask
)))
1655 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1657 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1659 else if (NILP (mask
) && found_p
&& img
->mask
)
1661 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1662 img
->mask
= NO_PIXMAP
;
1667 /* Should we apply an image transformation algorithm? */
1668 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1669 if (EQ (conversion
, Qdisabled
))
1670 x_disable_image (f
, img
);
1671 else if (EQ (conversion
, Qlaplace
))
1673 else if (EQ (conversion
, Qemboss
))
1675 else if (CONSP (conversion
)
1676 && EQ (XCAR (conversion
), Qedge_detection
))
1679 tem
= XCDR (conversion
);
1681 x_edge_detection (f
, img
,
1682 Fplist_get (tem
, QCmatrix
),
1683 Fplist_get (tem
, QCcolor_adjustment
));
1689 /* Return the id of image with Lisp specification SPEC on frame F.
1690 SPEC must be a valid Lisp image specification (see valid_image_p). */
1693 lookup_image (struct frame
*f
, Lisp_Object spec
)
1698 /* F must be a window-system frame, and SPEC must be a valid image
1700 eassert (FRAME_WINDOW_P (f
));
1701 eassert (valid_image_p (spec
));
1703 /* Look up SPEC in the hash table of the image cache. */
1704 hash
= sxhash (spec
, 0);
1705 img
= search_image_cache (f
, spec
, hash
);
1706 if (img
&& img
->load_failed_p
)
1708 free_image (f
, img
);
1712 /* If not found, create a new image and cache it. */
1716 img
= make_image (spec
, hash
);
1717 cache_image (f
, img
);
1718 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1719 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1720 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1722 /* If we can't load the image, and we don't have a width and
1723 height, use some arbitrary width and height so that we can
1724 draw a rectangle for it. */
1725 if (img
->load_failed_p
)
1729 value
= image_spec_value (spec
, QCwidth
, NULL
);
1730 img
->width
= (INTEGERP (value
)
1731 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1732 value
= image_spec_value (spec
, QCheight
, NULL
);
1733 img
->height
= (INTEGERP (value
)
1734 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1738 /* Handle image type independent image attributes
1739 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1740 `:background COLOR'. */
1741 Lisp_Object ascent
, margin
, relief
, bg
;
1744 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1745 if (INTEGERP (ascent
))
1746 img
->ascent
= XFASTINT (ascent
);
1747 else if (EQ (ascent
, Qcenter
))
1748 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1750 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1751 if (INTEGERP (margin
))
1752 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1753 else if (CONSP (margin
))
1755 img
->hmargin
= XFASTINT (XCAR (margin
));
1756 img
->vmargin
= XFASTINT (XCDR (margin
));
1759 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1760 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1761 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1763 img
->relief
= XINT (relief
);
1764 img
->hmargin
+= eabs (img
->relief
);
1765 img
->vmargin
+= eabs (img
->relief
);
1768 if (! img
->background_valid
)
1770 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1774 = x_alloc_image_color (f
, img
, bg
,
1775 FRAME_BACKGROUND_PIXEL (f
));
1776 img
->background_valid
= 1;
1780 /* Do image transformations and compute masks, unless we
1781 don't have the image yet. */
1782 if (!EQ (*img
->type
->type
, Qpostscript
))
1783 postprocess_image (f
, img
);
1789 /* We're using IMG, so set its timestamp to `now'. */
1790 img
->timestamp
= current_emacs_time ();
1792 /* Value is the image id. */
1797 /* Cache image IMG in the image cache of frame F. */
1800 cache_image (struct frame
*f
, struct image
*img
)
1802 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1805 /* Find a free slot in c->images. */
1806 for (i
= 0; i
< c
->used
; ++i
)
1807 if (c
->images
[i
] == NULL
)
1810 /* If no free slot found, maybe enlarge c->images. */
1811 if (i
== c
->used
&& c
->used
== c
->size
)
1812 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1814 /* Add IMG to c->images, and assign IMG an id. */
1820 /* Add IMG to the cache's hash table. */
1821 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1822 img
->next
= c
->buckets
[i
];
1824 img
->next
->prev
= img
;
1826 c
->buckets
[i
] = img
;
1830 /* Call FN on every image in the image cache of frame F. Used to mark
1831 Lisp Objects in the image cache. */
1833 /* Mark Lisp objects in image IMG. */
1836 mark_image (struct image
*img
)
1838 mark_object (img
->spec
);
1839 mark_object (img
->dependencies
);
1841 if (!NILP (img
->lisp_data
))
1842 mark_object (img
->lisp_data
);
1847 mark_image_cache (struct image_cache
*c
)
1852 for (i
= 0; i
< c
->used
; ++i
)
1854 mark_image (c
->images
[i
]);
1860 /***********************************************************************
1861 X / NS / W32 support code
1862 ***********************************************************************/
1866 /* Macro for defining functions that will be loaded from image DLLs. */
1867 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1869 /* Macro for loading those image functions from the library. */
1870 #define LOAD_IMGLIB_FN(lib,func) { \
1871 fn_##func = (void *) GetProcAddress (lib, #func); \
1872 if (!fn_##func) return 0; \
1875 #endif /* HAVE_NTGUI */
1877 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1878 XImagePtr
*, Pixmap
*);
1879 static void x_destroy_x_image (XImagePtr
);
1880 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1882 /* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the
1884 WIDTH and HEIGHT must both be positive.
1885 If XIMG is null, assume it is a bitmap. */
1887 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1889 #ifdef HAVE_X_WINDOWS
1890 /* Respect Xlib's limits: it cannot deal with images that have more
1891 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1892 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1895 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1896 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1899 int bitmap_pad
, depth
, bytes_per_line
;
1902 bitmap_pad
= ximg
->bitmap_pad
;
1903 depth
= ximg
->depth
;
1904 bytes_per_line
= ximg
->bytes_per_line
;
1910 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1912 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1913 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1915 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1916 For now, assume that every image size is allowed on these systems. */
1921 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1922 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1923 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1924 via xmalloc. Print error messages via image_error if an error
1925 occurs. Value is non-zero if successful.
1927 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1928 should indicate the bit depth of the image. */
1931 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1932 XImagePtr
*ximg
, Pixmap
*pixmap
)
1934 #ifdef HAVE_X_WINDOWS
1935 Display
*display
= FRAME_X_DISPLAY (f
);
1936 Window window
= FRAME_X_WINDOW (f
);
1937 Screen
*screen
= FRAME_X_SCREEN (f
);
1939 eassert (interrupt_input_blocked
);
1942 depth
= DefaultDepthOfScreen (screen
);
1943 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1944 depth
, ZPixmap
, 0, NULL
, width
, height
,
1945 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1948 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1952 if (! x_check_image_size (*ximg
, width
, height
))
1954 x_destroy_x_image (*ximg
);
1956 image_error ("Image too large (%dx%d)",
1957 make_number (width
), make_number (height
));
1961 /* Allocate image raster. */
1962 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1964 /* Allocate a pixmap of the same size. */
1965 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1966 if (*pixmap
== NO_PIXMAP
)
1968 x_destroy_x_image (*ximg
);
1970 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1975 #endif /* HAVE_X_WINDOWS */
1979 BITMAPINFOHEADER
*header
;
1981 int scanline_width_bits
;
1983 int palette_colors
= 0;
1988 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1989 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1991 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1995 scanline_width_bits
= width
* depth
;
1996 remainder
= scanline_width_bits
% 32;
1999 scanline_width_bits
+= 32 - remainder
;
2001 /* Bitmaps with a depth less than 16 need a palette. */
2002 /* BITMAPINFO structure already contains the first RGBQUAD. */
2004 palette_colors
= 1 << (depth
- 1);
2006 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2008 header
= &(*ximg
)->info
.bmiHeader
;
2009 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2010 header
->biSize
= sizeof (*header
);
2011 header
->biWidth
= width
;
2012 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2013 header
->biPlanes
= 1;
2014 header
->biBitCount
= depth
;
2015 header
->biCompression
= BI_RGB
;
2016 header
->biClrUsed
= palette_colors
;
2018 /* TODO: fill in palette. */
2021 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2022 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2023 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2024 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2025 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2026 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2027 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2028 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2031 hdc
= get_frame_dc (f
);
2033 /* Create a DIBSection and raster array for the bitmap,
2034 and store its handle in *pixmap. */
2035 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2036 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2037 /* casting avoids a GCC warning */
2038 (void **)&((*ximg
)->data
), NULL
, 0);
2040 /* Realize display palette and garbage all frames. */
2041 release_frame_dc (f
, hdc
);
2043 if (*pixmap
== NULL
)
2045 DWORD err
= GetLastError ();
2046 Lisp_Object errcode
;
2047 /* All system errors are < 10000, so the following is safe. */
2048 XSETINT (errcode
, err
);
2049 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2050 x_destroy_x_image (*ximg
);
2056 #endif /* HAVE_NTGUI */
2059 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2063 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2072 /* Destroy XImage XIMG. Free XIMG->data. */
2075 x_destroy_x_image (XImagePtr ximg
)
2077 eassert (interrupt_input_blocked
);
2080 #ifdef HAVE_X_WINDOWS
2083 XDestroyImage (ximg
);
2084 #endif /* HAVE_X_WINDOWS */
2086 /* Data will be freed by DestroyObject. */
2089 #endif /* HAVE_NTGUI */
2091 ns_release_object (ximg
);
2092 #endif /* HAVE_NS */
2097 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2098 are width and height of both the image and pixmap. */
2101 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2103 #ifdef HAVE_X_WINDOWS
2106 eassert (interrupt_input_blocked
);
2107 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2108 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2109 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2110 #endif /* HAVE_X_WINDOWS */
2113 #if 0 /* I don't think this is necessary looking at where it is used. */
2114 HDC hdc
= get_frame_dc (f
);
2115 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2116 release_frame_dc (f
, hdc
);
2118 #endif /* HAVE_NTGUI */
2121 eassert (ximg
== pixmap
);
2122 ns_retain_object (ximg
);
2127 /***********************************************************************
2129 ***********************************************************************/
2131 /* Find image file FILE. Look in data-directory/images, then
2132 x-bitmap-file-path. Value is the encoded full name of the file
2133 found, or nil if not found. */
2136 x_find_image_file (Lisp_Object file
)
2138 Lisp_Object file_found
, search_path
;
2141 /* TODO I think this should use something like image-load-path
2142 instead. Unfortunately, that can contain non-string elements. */
2143 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2145 Vx_bitmap_file_path
);
2147 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2148 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2154 file_found
= ENCODE_FILE (file_found
);
2162 /* Read FILE into memory. Value is a pointer to a buffer allocated
2163 with xmalloc holding FILE's contents. Value is null if an error
2164 occurred. *SIZE is set to the size of the file. */
2166 static unsigned char *
2167 slurp_file (char *file
, ptrdiff_t *size
)
2170 unsigned char *buf
= NULL
;
2173 if (stat (file
, &st
) == 0
2174 && (fp
= fopen (file
, "rb")) != NULL
2175 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2176 && (buf
= xmalloc (st
.st_size
),
2177 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2198 /***********************************************************************
2200 ***********************************************************************/
2202 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2203 static int xbm_load (struct frame
*f
, struct image
*img
);
2204 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2205 unsigned char *, unsigned char *);
2206 static int xbm_image_p (Lisp_Object object
);
2207 static int xbm_read_bitmap_data (struct frame
*f
,
2208 unsigned char *, unsigned char *,
2209 int *, int *, char **, int);
2210 static int xbm_file_p (Lisp_Object
);
2213 /* Indices of image specification fields in xbm_format, below. */
2215 enum xbm_keyword_index
2233 /* Vector of image_keyword structures describing the format
2234 of valid XBM image specifications. */
2236 static const struct image_keyword xbm_format
[XBM_LAST
] =
2238 {":type", IMAGE_SYMBOL_VALUE
, 1},
2239 {":file", IMAGE_STRING_VALUE
, 0},
2240 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2241 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2242 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2243 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2244 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2245 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2246 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2247 {":relief", IMAGE_INTEGER_VALUE
, 0},
2248 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2249 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2250 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2253 /* Structure describing the image type XBM. */
2255 static struct image_type xbm_type
=
2264 /* Tokens returned from xbm_scan. */
2273 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2274 A valid specification is a list starting with the symbol `image'
2275 The rest of the list is a property list which must contain an
2278 If the specification specifies a file to load, it must contain
2279 an entry `:file FILENAME' where FILENAME is a string.
2281 If the specification is for a bitmap loaded from memory it must
2282 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2283 WIDTH and HEIGHT are integers > 0. DATA may be:
2285 1. a string large enough to hold the bitmap data, i.e. it must
2286 have a size >= (WIDTH + 7) / 8 * HEIGHT
2288 2. a bool-vector of size >= WIDTH * HEIGHT
2290 3. a vector of strings or bool-vectors, one for each line of the
2293 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2294 may not be specified in this case because they are defined in the
2297 Both the file and data forms may contain the additional entries
2298 `:background COLOR' and `:foreground COLOR'. If not present,
2299 foreground and background of the frame on which the image is
2300 displayed is used. */
2303 xbm_image_p (Lisp_Object object
)
2305 struct image_keyword kw
[XBM_LAST
];
2307 memcpy (kw
, xbm_format
, sizeof kw
);
2308 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2311 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2313 if (kw
[XBM_FILE
].count
)
2315 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2318 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2320 /* In-memory XBM file. */
2321 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2329 /* Entries for `:width', `:height' and `:data' must be present. */
2330 if (!kw
[XBM_WIDTH
].count
2331 || !kw
[XBM_HEIGHT
].count
2332 || !kw
[XBM_DATA
].count
)
2335 data
= kw
[XBM_DATA
].value
;
2336 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2337 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2339 /* Check type of data, and width and height against contents of
2345 /* Number of elements of the vector must be >= height. */
2346 if (ASIZE (data
) < height
)
2349 /* Each string or bool-vector in data must be large enough
2350 for one line of the image. */
2351 for (i
= 0; i
< height
; ++i
)
2353 Lisp_Object elt
= AREF (data
, i
);
2358 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2361 else if (BOOL_VECTOR_P (elt
))
2363 if (XBOOL_VECTOR (elt
)->size
< width
)
2370 else if (STRINGP (data
))
2373 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2376 else if (BOOL_VECTOR_P (data
))
2378 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2389 /* Scan a bitmap file. FP is the stream to read from. Value is
2390 either an enumerator from enum xbm_token, or a character for a
2391 single-character token, or 0 at end of file. If scanning an
2392 identifier, store the lexeme of the identifier in SVAL. If
2393 scanning a number, store its value in *IVAL. */
2396 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2402 /* Skip white space. */
2403 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2408 else if (c_isdigit (c
))
2410 int value
= 0, digit
;
2412 if (c
== '0' && *s
< end
)
2415 if (c
== 'x' || c
== 'X')
2422 else if (c
>= 'a' && c
<= 'f')
2423 digit
= c
- 'a' + 10;
2424 else if (c
>= 'A' && c
<= 'F')
2425 digit
= c
- 'A' + 10;
2428 value
= 16 * value
+ digit
;
2431 else if (c_isdigit (c
))
2435 && (c
= *(*s
)++, c_isdigit (c
)))
2436 value
= 8 * value
+ c
- '0';
2443 && (c
= *(*s
)++, c_isdigit (c
)))
2444 value
= 10 * value
+ c
- '0';
2452 else if (c_isalpha (c
) || c
== '_')
2456 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2463 else if (c
== '/' && **s
== '*')
2465 /* C-style comment. */
2467 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2481 /* Create a Windows bitmap from X bitmap data. */
2483 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2485 static unsigned char swap_nibble
[16]
2486 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2487 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2488 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2489 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2491 unsigned char *bits
, *p
;
2494 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2495 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2496 bits
= alloca (height
* w2
);
2497 memset (bits
, 0, height
* w2
);
2498 for (i
= 0; i
< height
; i
++)
2501 for (j
= 0; j
< w1
; j
++)
2503 /* Bitswap XBM bytes to match how Windows does things. */
2504 unsigned char c
= *data
++;
2505 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2506 | (swap_nibble
[(c
>>4) & 0xf]));
2509 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2515 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2516 COLORREF foreground
, COLORREF background
)
2518 HDC hdc
, old_img_dc
, new_img_dc
;
2519 HGDIOBJ old_prev
, new_prev
;
2522 hdc
= get_frame_dc (f
);
2523 old_img_dc
= CreateCompatibleDC (hdc
);
2524 new_img_dc
= CreateCompatibleDC (hdc
);
2525 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2526 release_frame_dc (f
, hdc
);
2527 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2528 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2529 /* Windows convention for mono bitmaps is black = background,
2530 white = foreground. */
2531 SetTextColor (new_img_dc
, background
);
2532 SetBkColor (new_img_dc
, foreground
);
2534 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2537 SelectObject (old_img_dc
, old_prev
);
2538 SelectObject (new_img_dc
, new_prev
);
2539 DeleteDC (old_img_dc
);
2540 DeleteDC (new_img_dc
);
2541 DeleteObject (img
->pixmap
);
2542 if (new_pixmap
== 0)
2543 fprintf (stderr
, "Failed to convert image to color.\n");
2545 img
->pixmap
= new_pixmap
;
2548 #define XBM_BIT_SHUFFLE(b) (~(b))
2552 #define XBM_BIT_SHUFFLE(b) (b)
2554 #endif /* HAVE_NTGUI */
2558 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2559 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2560 int non_default_colors
)
2564 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2566 /* If colors were specified, transfer the bitmap to a color one. */
2567 if (non_default_colors
)
2568 convert_mono_to_color_image (f
, img
, fg
, bg
);
2570 #elif defined (HAVE_NS)
2571 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2575 (x_check_image_size (0, img
->width
, img
->height
)
2576 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2579 img
->width
, img
->height
,
2581 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2583 #endif /* !HAVE_NTGUI && !HAVE_NS */
2588 /* Replacement for XReadBitmapFileData which isn't available under old
2589 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2590 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2591 the image. Return in *DATA the bitmap data allocated with xmalloc.
2592 Value is non-zero if successful. DATA null means just test if
2593 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2594 is non-zero, inhibit the call to image_error when the image size is
2595 invalid (the bitmap remains unread). */
2598 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2599 int *width
, int *height
, char **data
,
2600 int inhibit_image_error
)
2602 unsigned char *s
= contents
;
2603 char buffer
[BUFSIZ
];
2606 int bytes_per_line
, i
, nbytes
;
2612 LA1 = xbm_scan (&s, end, buffer, &value)
2614 #define expect(TOKEN) \
2615 if (LA1 != (TOKEN)) \
2620 #define expect_ident(IDENT) \
2621 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2626 *width
= *height
= -1;
2629 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2631 /* Parse defines for width, height and hot-spots. */
2635 expect_ident ("define");
2636 expect (XBM_TK_IDENT
);
2638 if (LA1
== XBM_TK_NUMBER
)
2640 char *q
= strrchr (buffer
, '_');
2641 q
= q
? q
+ 1 : buffer
;
2642 if (strcmp (q
, "width") == 0)
2644 else if (strcmp (q
, "height") == 0)
2647 expect (XBM_TK_NUMBER
);
2650 if (!check_image_size (f
, *width
, *height
))
2652 if (!inhibit_image_error
)
2653 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2656 else if (data
== NULL
)
2659 /* Parse bits. Must start with `static'. */
2660 expect_ident ("static");
2661 if (LA1
== XBM_TK_IDENT
)
2663 if (strcmp (buffer
, "unsigned") == 0)
2666 expect_ident ("char");
2668 else if (strcmp (buffer
, "short") == 0)
2672 if (*width
% 16 && *width
% 16 < 9)
2675 else if (strcmp (buffer
, "char") == 0)
2683 expect (XBM_TK_IDENT
);
2689 if (! x_check_image_size (0, *width
, *height
))
2691 if (!inhibit_image_error
)
2692 image_error ("Image too large (%dx%d)",
2693 make_number (*width
), make_number (*height
));
2696 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2697 nbytes
= bytes_per_line
* *height
;
2698 p
= *data
= xmalloc (nbytes
);
2702 for (i
= 0; i
< nbytes
; i
+= 2)
2705 expect (XBM_TK_NUMBER
);
2707 *p
++ = XBM_BIT_SHUFFLE (val
);
2708 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2709 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2711 if (LA1
== ',' || LA1
== '}')
2719 for (i
= 0; i
< nbytes
; ++i
)
2722 expect (XBM_TK_NUMBER
);
2724 *p
++ = XBM_BIT_SHUFFLE (val
);
2726 if (LA1
== ',' || LA1
== '}')
2751 /* Load XBM image IMG which will be displayed on frame F from buffer
2752 CONTENTS. END is the end of the buffer. Value is non-zero if
2756 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2763 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2767 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2768 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2769 int non_default_colors
= 0;
2772 eassert (img
->width
> 0 && img
->height
> 0);
2774 /* Get foreground and background colors, maybe allocate colors. */
2775 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2778 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2779 non_default_colors
= 1;
2781 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2784 background
= x_alloc_image_color (f
, img
, value
, background
);
2785 img
->background
= background
;
2786 img
->background_valid
= 1;
2787 non_default_colors
= 1;
2790 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2791 foreground
, background
,
2792 non_default_colors
);
2795 if (img
->pixmap
== NO_PIXMAP
)
2797 x_clear_image (f
, img
);
2798 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2804 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2810 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2813 xbm_file_p (Lisp_Object data
)
2816 return (STRINGP (data
)
2817 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2818 (SDATA (data
) + SBYTES (data
)),
2823 /* Fill image IMG which is used on frame F with pixmap data. Value is
2824 non-zero if successful. */
2827 xbm_load (struct frame
*f
, struct image
*img
)
2830 Lisp_Object file_name
;
2832 eassert (xbm_image_p (img
->spec
));
2834 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2835 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2836 if (STRINGP (file_name
))
2839 unsigned char *contents
;
2842 file
= x_find_image_file (file_name
);
2843 if (!STRINGP (file
))
2845 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2849 contents
= slurp_file (SSDATA (file
), &size
);
2850 if (contents
== NULL
)
2852 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2856 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2861 struct image_keyword fmt
[XBM_LAST
];
2863 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2864 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2865 int non_default_colors
= 0;
2868 int in_memory_file_p
= 0;
2870 /* See if data looks like an in-memory XBM file. */
2871 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2872 in_memory_file_p
= xbm_file_p (data
);
2874 /* Parse the image specification. */
2875 memcpy (fmt
, xbm_format
, sizeof fmt
);
2876 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2880 /* Get specified width, and height. */
2881 if (!in_memory_file_p
)
2883 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2884 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2885 eassert (img
->width
> 0 && img
->height
> 0);
2886 if (!check_image_size (f
, img
->width
, img
->height
))
2888 image_error ("Invalid image size (see `max-image-size')",
2894 /* Get foreground and background colors, maybe allocate colors. */
2895 if (fmt
[XBM_FOREGROUND
].count
2896 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2898 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2900 non_default_colors
= 1;
2903 if (fmt
[XBM_BACKGROUND
].count
2904 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2906 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2908 non_default_colors
= 1;
2911 if (in_memory_file_p
)
2912 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2921 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2923 p
= bits
= alloca (nbytes
* img
->height
);
2924 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2926 Lisp_Object line
= AREF (data
, i
);
2928 memcpy (p
, SDATA (line
), nbytes
);
2930 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2933 else if (STRINGP (data
))
2934 bits
= SSDATA (data
);
2936 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2942 /* Windows mono bitmaps are reversed compared with X. */
2943 invertedBits
= bits
;
2944 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2946 bits
= alloca (nbytes
);
2947 for (i
= 0; i
< nbytes
; i
++)
2948 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2951 /* Create the pixmap. */
2953 if (x_check_image_size (0, img
->width
, img
->height
))
2954 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2955 foreground
, background
,
2956 non_default_colors
);
2958 img
->pixmap
= NO_PIXMAP
;
2964 image_error ("Unable to create pixmap for XBM image `%s'",
2966 x_clear_image (f
, img
);
2976 /***********************************************************************
2978 ***********************************************************************/
2980 #if defined (HAVE_XPM) || defined (HAVE_NS)
2982 static int xpm_image_p (Lisp_Object object
);
2983 static int xpm_load (struct frame
*f
, struct image
*img
);
2984 static int xpm_valid_color_symbols_p (Lisp_Object
);
2986 #endif /* HAVE_XPM || HAVE_NS */
2990 /* Indicate to xpm.h that we don't have Xlib. */
2992 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2993 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2994 #define XColor xpm_XColor
2995 #define XImage xpm_XImage
2996 #define Display xpm_Display
2997 #define PIXEL_ALREADY_TYPEDEFED
2998 #include "X11/xpm.h"
3003 #undef PIXEL_ALREADY_TYPEDEFED
3005 #include "X11/xpm.h"
3006 #endif /* HAVE_NTGUI */
3007 #endif /* HAVE_XPM */
3009 #if defined (HAVE_XPM) || defined (HAVE_NS)
3010 /* The symbol `xpm' identifying XPM-format images. */
3012 static Lisp_Object Qxpm
;
3014 /* Indices of image specification fields in xpm_format, below. */
3016 enum xpm_keyword_index
3032 /* Vector of image_keyword structures describing the format
3033 of valid XPM image specifications. */
3035 static const struct image_keyword xpm_format
[XPM_LAST
] =
3037 {":type", IMAGE_SYMBOL_VALUE
, 1},
3038 {":file", IMAGE_STRING_VALUE
, 0},
3039 {":data", IMAGE_STRING_VALUE
, 0},
3040 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3041 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3042 {":relief", IMAGE_INTEGER_VALUE
, 0},
3043 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3044 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3045 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3046 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3047 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3050 /* Structure describing the image type XPM. */
3052 static struct image_type xpm_type
=
3061 #ifdef HAVE_X_WINDOWS
3063 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3064 functions for allocating image colors. Our own functions handle
3065 color allocation failures more gracefully than the ones on the XPM
3068 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3069 #define ALLOC_XPM_COLORS
3071 #endif /* HAVE_X_WINDOWS */
3073 #ifdef ALLOC_XPM_COLORS
3075 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3076 static void xpm_free_color_cache (void);
3077 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3078 static int xpm_color_bucket (char *);
3079 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3082 /* An entry in a hash table used to cache color definitions of named
3083 colors. This cache is necessary to speed up XPM image loading in
3084 case we do color allocations ourselves. Without it, we would need
3085 a call to XParseColor per pixel in the image. */
3087 struct xpm_cached_color
3089 /* Next in collision chain. */
3090 struct xpm_cached_color
*next
;
3092 /* Color definition (RGB and pixel color). */
3099 /* The hash table used for the color cache, and its bucket vector
3102 #define XPM_COLOR_CACHE_BUCKETS 1001
3103 static struct xpm_cached_color
**xpm_color_cache
;
3105 /* Initialize the color cache. */
3108 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3110 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3111 xpm_color_cache
= xzalloc (nbytes
);
3112 init_color_table ();
3114 if (attrs
->valuemask
& XpmColorSymbols
)
3119 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3120 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3121 attrs
->colorsymbols
[i
].value
, &color
))
3123 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3125 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3130 /* Free the color cache. */
3133 xpm_free_color_cache (void)
3135 struct xpm_cached_color
*p
, *next
;
3138 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3139 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3145 xfree (xpm_color_cache
);
3146 xpm_color_cache
= NULL
;
3147 free_color_table ();
3150 /* Return the bucket index for color named COLOR_NAME in the color
3154 xpm_color_bucket (char *color_name
)
3156 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3157 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3161 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3162 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3165 static struct xpm_cached_color
*
3166 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3169 struct xpm_cached_color
*p
;
3172 bucket
= xpm_color_bucket (color_name
);
3174 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3175 p
= xmalloc (nbytes
);
3176 strcpy (p
->name
, color_name
);
3178 p
->next
= xpm_color_cache
[bucket
];
3179 xpm_color_cache
[bucket
] = p
;
3183 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3184 return the cached definition in *COLOR. Otherwise, make a new
3185 entry in the cache and allocate the color. Value is zero if color
3186 allocation failed. */
3189 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3191 struct xpm_cached_color
*p
;
3192 int h
= xpm_color_bucket (color_name
);
3194 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3195 if (strcmp (p
->name
, color_name
) == 0)
3200 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3203 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3205 p
= xpm_cache_color (f
, color_name
, color
, h
);
3207 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3208 with transparency, and it's useful. */
3209 else if (strcmp ("opaque", color_name
) == 0)
3211 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3212 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3213 p
= xpm_cache_color (f
, color_name
, color
, h
);
3220 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3221 CLOSURE is a pointer to the frame on which we allocate the
3222 color. Return in *COLOR the allocated color. Value is non-zero
3226 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3229 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3233 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3234 is a pointer to the frame on which we allocate the color. Value is
3235 non-zero if successful. */
3238 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3243 #endif /* ALLOC_XPM_COLORS */
3248 /* XPM library details. */
3250 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3251 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3252 xpm_XImage
**, XpmAttributes
*));
3253 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3254 xpm_XImage
**, XpmAttributes
*));
3255 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3258 init_xpm_functions (Lisp_Object libraries
)
3262 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3265 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3266 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3267 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3268 LOAD_IMGLIB_FN (library
, XImageFree
);
3272 #endif /* HAVE_NTGUI */
3275 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3276 for XPM images. Such a list must consist of conses whose car and
3280 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3282 while (CONSP (color_symbols
))
3284 Lisp_Object sym
= XCAR (color_symbols
);
3286 || !STRINGP (XCAR (sym
))
3287 || !STRINGP (XCDR (sym
)))
3289 color_symbols
= XCDR (color_symbols
);
3292 return NILP (color_symbols
);
3296 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3299 xpm_image_p (Lisp_Object object
)
3301 struct image_keyword fmt
[XPM_LAST
];
3302 memcpy (fmt
, xpm_format
, sizeof fmt
);
3303 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3304 /* Either `:file' or `:data' must be present. */
3305 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3306 /* Either no `:color-symbols' or it's a list of conses
3307 whose car and cdr are strings. */
3308 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3309 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3312 #endif /* HAVE_XPM || HAVE_NS */
3314 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3316 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3318 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3321 XpmAttributes attrs
;
3322 Pixmap bitmap
, mask
;
3324 memset (&attrs
, 0, sizeof attrs
);
3326 attrs
.visual
= FRAME_X_VISUAL (f
);
3327 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3328 attrs
.valuemask
|= XpmVisual
;
3329 attrs
.valuemask
|= XpmColormap
;
3331 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3332 (char **) bits
, &bitmap
, &mask
, &attrs
);
3333 if (rc
!= XpmSuccess
)
3335 XpmFreeAttributes (&attrs
);
3339 id
= x_allocate_bitmap_record (f
);
3340 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3341 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3342 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3343 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3344 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3345 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3346 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3347 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3349 XpmFreeAttributes (&attrs
);
3352 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3354 /* Load image IMG which will be displayed on frame F. Value is
3355 non-zero if successful. */
3360 xpm_load (struct frame
*f
, struct image
*img
)
3363 XpmAttributes attrs
;
3364 Lisp_Object specified_file
, color_symbols
;
3367 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3368 #endif /* HAVE_NTGUI */
3370 /* Configure the XPM lib. Use the visual of frame F. Allocate
3371 close colors. Return colors allocated. */
3372 memset (&attrs
, 0, sizeof attrs
);
3375 attrs
.visual
= FRAME_X_VISUAL (f
);
3376 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3377 attrs
.valuemask
|= XpmVisual
;
3378 attrs
.valuemask
|= XpmColormap
;
3379 #endif /* HAVE_NTGUI */
3381 #ifdef ALLOC_XPM_COLORS
3382 /* Allocate colors with our own functions which handle
3383 failing color allocation more gracefully. */
3384 attrs
.color_closure
= f
;
3385 attrs
.alloc_color
= xpm_alloc_color
;
3386 attrs
.free_colors
= xpm_free_colors
;
3387 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3388 #else /* not ALLOC_XPM_COLORS */
3389 /* Let the XPM lib allocate colors. */
3390 attrs
.valuemask
|= XpmReturnAllocPixels
;
3391 #ifdef XpmAllocCloseColors
3392 attrs
.alloc_close_colors
= 1;
3393 attrs
.valuemask
|= XpmAllocCloseColors
;
3394 #else /* not XpmAllocCloseColors */
3395 attrs
.closeness
= 600;
3396 attrs
.valuemask
|= XpmCloseness
;
3397 #endif /* not XpmAllocCloseColors */
3398 #endif /* ALLOC_XPM_COLORS */
3400 /* If image specification contains symbolic color definitions, add
3401 these to `attrs'. */
3402 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3403 if (CONSP (color_symbols
))
3406 XpmColorSymbol
*xpm_syms
;
3409 attrs
.valuemask
|= XpmColorSymbols
;
3411 /* Count number of symbols. */
3412 attrs
.numsymbols
= 0;
3413 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3416 /* Allocate an XpmColorSymbol array. */
3417 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3418 xpm_syms
= alloca (size
);
3419 memset (xpm_syms
, 0, size
);
3420 attrs
.colorsymbols
= xpm_syms
;
3422 /* Fill the color symbol array. */
3423 for (tail
= color_symbols
, i
= 0;
3425 ++i
, tail
= XCDR (tail
))
3429 char *empty_string
= (char *) "";
3431 if (!CONSP (XCAR (tail
)))
3433 xpm_syms
[i
].name
= empty_string
;
3434 xpm_syms
[i
].value
= empty_string
;
3437 name
= XCAR (XCAR (tail
));
3438 color
= XCDR (XCAR (tail
));
3441 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3442 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3445 xpm_syms
[i
].name
= empty_string
;
3446 if (STRINGP (color
))
3448 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3449 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3452 xpm_syms
[i
].value
= empty_string
;
3456 /* Create a pixmap for the image, either from a file, or from a
3457 string buffer containing data in the same format as an XPM file. */
3458 #ifdef ALLOC_XPM_COLORS
3459 xpm_init_color_cache (f
, &attrs
);
3462 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3466 HDC frame_dc
= get_frame_dc (f
);
3467 hdc
= CreateCompatibleDC (frame_dc
);
3468 release_frame_dc (f
, frame_dc
);
3470 #endif /* HAVE_NTGUI */
3472 if (STRINGP (specified_file
))
3474 Lisp_Object file
= x_find_image_file (specified_file
);
3475 if (!STRINGP (file
))
3477 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3478 #ifdef ALLOC_XPM_COLORS
3479 xpm_free_color_cache ();
3485 /* XpmReadFileToPixmap is not available in the Windows port of
3486 libxpm. But XpmReadFileToImage almost does what we want. */
3487 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3488 &xpm_image
, &xpm_mask
,
3491 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3492 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3494 #endif /* HAVE_NTGUI */
3498 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3499 if (!STRINGP (buffer
))
3501 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3502 #ifdef ALLOC_XPM_COLORS
3503 xpm_free_color_cache ();
3508 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3509 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3510 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3511 &xpm_image
, &xpm_mask
,
3514 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3516 &img
->pixmap
, &img
->mask
,
3518 #endif /* HAVE_NTGUI */
3521 if (rc
== XpmSuccess
)
3523 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3524 img
->colors
= colors_in_color_table (&img
->ncolors
);
3525 #else /* not ALLOC_XPM_COLORS */
3529 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3530 plus some duplicate attributes. */
3531 if (xpm_image
&& xpm_image
->bitmap
)
3533 img
->pixmap
= xpm_image
->bitmap
;
3534 /* XImageFree in libXpm frees XImage struct without destroying
3535 the bitmap, which is what we want. */
3536 fn_XImageFree (xpm_image
);
3538 if (xpm_mask
&& xpm_mask
->bitmap
)
3540 /* The mask appears to be inverted compared with what we expect.
3541 TODO: invert our expectations. See other places where we
3542 have to invert bits because our idea of masks is backwards. */
3544 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3546 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3547 SelectObject (hdc
, old_obj
);
3549 img
->mask
= xpm_mask
->bitmap
;
3550 fn_XImageFree (xpm_mask
);
3555 #endif /* HAVE_NTGUI */
3557 /* Remember allocated colors. */
3558 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3559 img
->ncolors
= attrs
.nalloc_pixels
;
3560 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3562 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3563 #ifdef DEBUG_X_COLORS
3564 register_color (img
->colors
[i
]);
3567 #endif /* not ALLOC_XPM_COLORS */
3569 img
->width
= attrs
.width
;
3570 img
->height
= attrs
.height
;
3571 eassert (img
->width
> 0 && img
->height
> 0);
3573 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3575 fn_XpmFreeAttributes (&attrs
);
3577 XpmFreeAttributes (&attrs
);
3578 #endif /* HAVE_NTGUI */
3584 #endif /* HAVE_NTGUI */
3589 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3592 case XpmFileInvalid
:
3593 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3597 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3600 case XpmColorFailed
:
3601 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3605 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3610 #ifdef ALLOC_XPM_COLORS
3611 xpm_free_color_cache ();
3613 return rc
== XpmSuccess
;
3616 #endif /* HAVE_XPM */
3618 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3620 /* XPM support functions for NS where libxpm is not available.
3621 Only XPM version 3 (without any extensions) is supported. */
3623 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3625 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3626 const unsigned char *, int);
3627 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3629 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3630 const unsigned char *, int);
3632 /* Tokens returned from xpm_scan. */
3641 /* Scan an XPM data and return a character (< 256) or a token defined
3642 by enum xpm_token above. *S and END are the start (inclusive) and
3643 the end (exclusive) addresses of the data, respectively. Advance
3644 *S while scanning. If token is either XPM_TK_IDENT or
3645 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3646 length of the corresponding token, respectively. */
3649 xpm_scan (const unsigned char **s
,
3650 const unsigned char *end
,
3651 const unsigned char **beg
,
3658 /* Skip white-space. */
3659 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3662 /* gnus-pointer.xpm uses '-' in its identifier.
3663 sb-dir-plus.xpm uses '+' in its identifier. */
3664 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3668 && (c
= **s
, c_isalnum (c
)
3669 || c
== '_' || c
== '-' || c
== '+'))
3672 return XPM_TK_IDENT
;
3677 while (*s
< end
&& **s
!= '"')
3682 return XPM_TK_STRING
;
3686 if (*s
< end
&& **s
== '*')
3688 /* C-style comment. */
3692 while (*s
< end
&& *(*s
)++ != '*')
3695 while (*s
< end
&& **s
!= '/');
3709 /* Functions for color table lookup in XPM data. A key is a string
3710 specifying the color of each pixel in XPM data. A value is either
3711 an integer that specifies a pixel color, Qt that specifies
3712 transparency, or Qnil for the unspecified color. If the length of
3713 the key string is one, a vector is used as a table. Otherwise, a
3714 hash table is used. */
3717 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3718 const unsigned char *,
3721 Lisp_Object (**get_func
) (Lisp_Object
,
3722 const unsigned char *,
3725 *put_func
= xpm_put_color_table_v
;
3726 *get_func
= xpm_get_color_table_v
;
3727 return Fmake_vector (make_number (256), Qnil
);
3731 xpm_put_color_table_v (Lisp_Object color_table
,
3732 const unsigned char *chars_start
,
3736 ASET (color_table
, *chars_start
, color
);
3740 xpm_get_color_table_v (Lisp_Object color_table
,
3741 const unsigned char *chars_start
,
3744 return AREF (color_table
, *chars_start
);
3748 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3749 const unsigned char *,
3752 Lisp_Object (**get_func
) (Lisp_Object
,
3753 const unsigned char *,
3756 *put_func
= xpm_put_color_table_h
;
3757 *get_func
= xpm_get_color_table_h
;
3758 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3759 make_float (DEFAULT_REHASH_SIZE
),
3760 make_float (DEFAULT_REHASH_THRESHOLD
),
3765 xpm_put_color_table_h (Lisp_Object color_table
,
3766 const unsigned char *chars_start
,
3770 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3771 EMACS_UINT hash_code
;
3772 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3774 hash_lookup (table
, chars
, &hash_code
);
3775 hash_put (table
, chars
, color
, hash_code
);
3779 xpm_get_color_table_h (Lisp_Object color_table
,
3780 const unsigned char *chars_start
,
3783 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3785 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3787 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3790 enum xpm_color_key
{
3798 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3801 xpm_str_to_color_key (const char *s
)
3806 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3808 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3814 xpm_load_image (struct frame
*f
,
3816 const unsigned char *contents
,
3817 const unsigned char *end
)
3819 const unsigned char *s
= contents
, *beg
, *str
;
3820 unsigned char buffer
[BUFSIZ
];
3821 int width
, height
, x
, y
;
3822 int num_colors
, chars_per_pixel
;
3825 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3826 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3827 Lisp_Object frame
, color_symbols
, color_table
;
3828 int best_key
, have_mask
= 0;
3829 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3832 LA1 = xpm_scan (&s, end, &beg, &len)
3834 #define expect(TOKEN) \
3835 if (LA1 != (TOKEN)) \
3840 #define expect_ident(IDENT) \
3841 if (LA1 == XPM_TK_IDENT \
3842 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3847 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3851 expect_ident ("static");
3852 expect_ident ("char");
3854 expect (XPM_TK_IDENT
);
3859 expect (XPM_TK_STRING
);
3862 memcpy (buffer
, beg
, len
);
3864 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3865 &num_colors
, &chars_per_pixel
) != 4
3866 || width
<= 0 || height
<= 0
3867 || num_colors
<= 0 || chars_per_pixel
<= 0)
3870 if (!check_image_size (f
, width
, height
))
3872 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3876 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3877 &ximg
, &img
->pixmap
)
3879 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3880 &mask_img
, &img
->mask
)
3884 image_error ("Image too large", Qnil
, Qnil
);
3890 XSETFRAME (frame
, f
);
3891 if (!NILP (Fxw_display_color_p (frame
)))
3892 best_key
= XPM_COLOR_KEY_C
;
3893 else if (!NILP (Fx_display_grayscale_p (frame
)))
3894 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3895 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3897 best_key
= XPM_COLOR_KEY_M
;
3899 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3900 if (chars_per_pixel
== 1)
3901 color_table
= xpm_make_color_table_v (&put_color_table
,
3904 color_table
= xpm_make_color_table_h (&put_color_table
,
3907 while (num_colors
-- > 0)
3909 char *color
, *max_color
;
3910 int key
, next_key
, max_key
= 0;
3911 Lisp_Object symbol_color
= Qnil
, color_val
;
3914 expect (XPM_TK_STRING
);
3915 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3917 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3918 buffer
[len
- chars_per_pixel
] = '\0';
3920 str
= strtok (buffer
, " \t");
3923 key
= xpm_str_to_color_key (str
);
3928 color
= strtok (NULL
, " \t");
3932 while ((str
= strtok (NULL
, " \t")) != NULL
)
3934 next_key
= xpm_str_to_color_key (str
);
3937 color
[strlen (color
)] = ' ';
3940 if (key
== XPM_COLOR_KEY_S
)
3942 if (NILP (symbol_color
))
3943 symbol_color
= build_string (color
);
3945 else if (max_key
< key
&& key
<= best_key
)
3955 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3957 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3959 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3961 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3963 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
3965 color_val
= make_number (cdef
.pixel
);
3968 if (NILP (color_val
) && max_key
> 0)
3970 if (xstrcasecmp (max_color
, "None") == 0)
3972 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3973 color_val
= make_number (cdef
.pixel
);
3975 if (!NILP (color_val
))
3976 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3981 for (y
= 0; y
< height
; y
++)
3983 expect (XPM_TK_STRING
);
3985 if (len
< width
* chars_per_pixel
)
3987 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3989 Lisp_Object color_val
=
3990 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
3992 XPutPixel (ximg
, x
, y
,
3993 (INTEGERP (color_val
) ? XINT (color_val
)
3994 : FRAME_FOREGROUND_PIXEL (f
)));
3996 XPutPixel (mask_img
, x
, y
,
3997 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
3998 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4000 if (EQ (color_val
, Qt
))
4001 ns_set_alpha (ximg
, x
, y
, 0);
4009 img
->height
= height
;
4011 /* Maybe fill in the background field while we have ximg handy. */
4012 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4013 IMAGE_BACKGROUND (img
, f
, ximg
);
4015 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4016 x_destroy_x_image (ximg
);
4020 /* Fill in the background_transparent field while we have the
4022 image_background_transparent (img
, f
, mask_img
);
4024 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4025 x_destroy_x_image (mask_img
);
4029 x_destroy_x_image (mask_img
);
4030 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4031 img
->mask
= NO_PIXMAP
;
4037 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4038 x_destroy_x_image (ximg
);
4039 x_destroy_x_image (mask_img
);
4040 x_clear_image (f
, img
);
4049 xpm_load (struct frame
*f
,
4053 Lisp_Object file_name
;
4055 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4056 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4057 if (STRINGP (file_name
))
4060 unsigned char *contents
;
4063 file
= x_find_image_file (file_name
);
4064 if (!STRINGP (file
))
4066 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4070 contents
= slurp_file (SSDATA (file
), &size
);
4071 if (contents
== NULL
)
4073 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4077 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4084 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4085 if (!STRINGP (data
))
4087 image_error ("Invalid image data `%s'", data
, Qnil
);
4090 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4091 SDATA (data
) + SBYTES (data
));
4097 #endif /* HAVE_NS && !HAVE_XPM */
4101 /***********************************************************************
4103 ***********************************************************************/
4105 #ifdef COLOR_TABLE_SUPPORT
4107 /* An entry in the color table mapping an RGB color to a pixel color. */
4112 unsigned long pixel
;
4114 /* Next in color table collision list. */
4115 struct ct_color
*next
;
4118 /* The bucket vector size to use. Must be prime. */
4122 /* Value is a hash of the RGB color given by R, G, and B. */
4124 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4126 /* The color hash table. */
4128 static struct ct_color
**ct_table
;
4130 /* Number of entries in the color table. */
4132 static int ct_colors_allocated
;
4135 ct_colors_allocated_max
=
4137 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4140 /* Initialize the color table. */
4143 init_color_table (void)
4145 int size
= CT_SIZE
* sizeof (*ct_table
);
4146 ct_table
= xzalloc (size
);
4147 ct_colors_allocated
= 0;
4151 /* Free memory associated with the color table. */
4154 free_color_table (void)
4157 struct ct_color
*p
, *next
;
4159 for (i
= 0; i
< CT_SIZE
; ++i
)
4160 for (p
= ct_table
[i
]; p
; p
= next
)
4171 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4172 entry for that color already is in the color table, return the
4173 pixel color of that entry. Otherwise, allocate a new color for R,
4174 G, B, and make an entry in the color table. */
4176 static unsigned long
4177 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4179 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4180 int i
= hash
% CT_SIZE
;
4182 Display_Info
*dpyinfo
;
4184 /* Handle TrueColor visuals specially, which improves performance by
4185 two orders of magnitude. Freeing colors on TrueColor visuals is
4186 a nop, and pixel colors specify RGB values directly. See also
4187 the Xlib spec, chapter 3.1. */
4188 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4189 if (dpyinfo
->red_bits
> 0)
4191 unsigned long pr
, pg
, pb
;
4193 /* Apply gamma-correction like normal color allocation does. */
4197 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4198 gamma_correct (f
, &color
);
4199 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4202 /* Scale down RGB values to the visual's bits per RGB, and shift
4203 them to the right position in the pixel color. Note that the
4204 original RGB values are 16-bit values, as usual in X. */
4205 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4206 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4207 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4209 /* Assemble the pixel color. */
4210 return pr
| pg
| pb
;
4213 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4214 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4220 #ifdef HAVE_X_WINDOWS
4228 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4229 return FRAME_FOREGROUND_PIXEL (f
);
4231 #ifdef HAVE_X_WINDOWS
4236 cmap
= FRAME_X_COLORMAP (f
);
4237 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4240 ++ct_colors_allocated
;
4241 p
= xmalloc (sizeof *p
);
4245 p
->pixel
= color
.pixel
;
4246 p
->next
= ct_table
[i
];
4250 return FRAME_FOREGROUND_PIXEL (f
);
4254 color
= PALETTERGB (r
, g
, b
);
4256 color
= RGB_TO_ULONG (r
, g
, b
);
4257 #endif /* HAVE_NTGUI */
4258 ++ct_colors_allocated
;
4259 p
= xmalloc (sizeof *p
);
4264 p
->next
= ct_table
[i
];
4266 #endif /* HAVE_X_WINDOWS */
4274 /* Look up pixel color PIXEL which is used on frame F in the color
4275 table. If not already present, allocate it. Value is PIXEL. */
4277 static unsigned long
4278 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4280 int i
= pixel
% CT_SIZE
;
4283 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4284 if (p
->pixel
== pixel
)
4293 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4294 return FRAME_FOREGROUND_PIXEL (f
);
4296 #ifdef HAVE_X_WINDOWS
4297 cmap
= FRAME_X_COLORMAP (f
);
4298 color
.pixel
= pixel
;
4299 x_query_color (f
, &color
);
4300 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4303 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4304 color
.pixel
= pixel
;
4305 XQueryColor (NULL
, cmap
, &color
);
4306 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4308 #endif /* HAVE_X_WINDOWS */
4312 ++ct_colors_allocated
;
4314 p
= xmalloc (sizeof *p
);
4319 p
->next
= ct_table
[i
];
4323 return FRAME_FOREGROUND_PIXEL (f
);
4329 /* Value is a vector of all pixel colors contained in the color table,
4330 allocated via xmalloc. Set *N to the number of colors. */
4332 static unsigned long *
4333 colors_in_color_table (int *n
)
4337 unsigned long *colors
;
4339 if (ct_colors_allocated
== 0)
4346 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4347 *n
= ct_colors_allocated
;
4349 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4350 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4351 colors
[j
++] = p
->pixel
;
4357 #else /* COLOR_TABLE_SUPPORT */
4359 static unsigned long
4360 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4362 unsigned long pixel
;
4365 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4366 #endif /* HAVE_NTGUI */
4369 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4370 #endif /* HAVE_NS */
4375 init_color_table (void)
4378 #endif /* COLOR_TABLE_SUPPORT */
4381 /***********************************************************************
4383 ***********************************************************************/
4385 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4386 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4387 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4390 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4391 #endif /* HAVE_NTGUI */
4393 /* Edge detection matrices for different edge-detection
4396 static int emboss_matrix
[9] = {
4398 2, -1, 0, /* y - 1 */
4400 0, 1, -2 /* y + 1 */
4403 static int laplace_matrix
[9] = {
4405 1, 0, 0, /* y - 1 */
4407 0, 0, -1 /* y + 1 */
4410 /* Value is the intensity of the color whose red/green/blue values
4413 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4416 /* On frame F, return an array of XColor structures describing image
4417 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4418 non-zero means also fill the red/green/blue members of the XColor
4419 structures. Value is a pointer to the array of XColors structures,
4420 allocated with xmalloc; it must be freed by the caller. */
4423 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4427 XImagePtr_or_DC ximg
;
4431 #endif /* HAVE_NTGUI */
4433 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4434 memory_full (SIZE_MAX
);
4435 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4438 /* Get the X image IMG->pixmap. */
4439 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4440 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4442 /* Load the image into a memory device context. */
4443 hdc
= get_frame_dc (f
);
4444 ximg
= CreateCompatibleDC (hdc
);
4445 release_frame_dc (f
, hdc
);
4446 prev
= SelectObject (ximg
, img
->pixmap
);
4447 #endif /* HAVE_NTGUI */
4449 /* Fill the `pixel' members of the XColor array. I wished there
4450 were an easy and portable way to circumvent XGetPixel. */
4452 for (y
= 0; y
< img
->height
; ++y
)
4454 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4456 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4457 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4459 x_query_colors (f
, row
, img
->width
);
4463 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4465 /* W32_TODO: palette support needed here? */
4466 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4469 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4470 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4471 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4474 #endif /* HAVE_X_WINDOWS */
4477 Destroy_Image (ximg
, prev
);
4484 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4485 created with CreateDIBSection, with the pointer to the bit values
4486 stored in ximg->data. */
4489 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4491 int width
= ximg
->info
.bmiHeader
.biWidth
;
4492 unsigned char * pixel
;
4494 /* True color images. */
4495 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4497 int rowbytes
= width
* 3;
4498 /* Ensure scanlines are aligned on 4 byte boundaries. */
4500 rowbytes
+= 4 - (rowbytes
% 4);
4502 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4503 /* Windows bitmaps are in BGR order. */
4504 *pixel
= GetBValue (color
);
4505 *(pixel
+ 1) = GetGValue (color
);
4506 *(pixel
+ 2) = GetRValue (color
);
4508 /* Monochrome images. */
4509 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4511 int rowbytes
= width
/ 8;
4512 /* Ensure scanlines are aligned on 4 byte boundaries. */
4514 rowbytes
+= 4 - (rowbytes
% 4);
4515 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4516 /* Filter out palette info. */
4517 if (color
& 0x00ffffff)
4518 *pixel
= *pixel
| (1 << x
% 8);
4520 *pixel
= *pixel
& ~(1 << x
% 8);
4523 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4526 #endif /* HAVE_NTGUI */
4528 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4529 RGB members are set. F is the frame on which this all happens.
4530 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4533 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4536 XImagePtr oimg
= NULL
;
4540 init_color_table ();
4542 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4545 for (y
= 0; y
< img
->height
; ++y
)
4546 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4548 unsigned long pixel
;
4549 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4550 XPutPixel (oimg
, x
, y
, pixel
);
4554 x_clear_image_1 (f
, img
, 1, 0, 1);
4556 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4557 x_destroy_x_image (oimg
);
4558 img
->pixmap
= pixmap
;
4559 #ifdef COLOR_TABLE_SUPPORT
4560 img
->colors
= colors_in_color_table (&img
->ncolors
);
4561 free_color_table ();
4562 #endif /* COLOR_TABLE_SUPPORT */
4566 /* On frame F, perform edge-detection on image IMG.
4568 MATRIX is a nine-element array specifying the transformation
4569 matrix. See emboss_matrix for an example.
4571 COLOR_ADJUST is a color adjustment added to each pixel of the
4575 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4577 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4581 for (i
= sum
= 0; i
< 9; ++i
)
4582 sum
+= eabs (matrix
[i
]);
4584 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4586 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4587 memory_full (SIZE_MAX
);
4588 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4590 for (y
= 0; y
< img
->height
; ++y
)
4592 p
= COLOR (new, 0, y
);
4593 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4594 p
= COLOR (new, img
->width
- 1, y
);
4595 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4598 for (x
= 1; x
< img
->width
- 1; ++x
)
4600 p
= COLOR (new, x
, 0);
4601 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4602 p
= COLOR (new, x
, img
->height
- 1);
4603 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4606 for (y
= 1; y
< img
->height
- 1; ++y
)
4608 p
= COLOR (new, 1, y
);
4610 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4612 int r
, g
, b
, yy
, xx
;
4615 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4616 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4619 XColor
*t
= COLOR (colors
, xx
, yy
);
4620 r
+= matrix
[i
] * t
->red
;
4621 g
+= matrix
[i
] * t
->green
;
4622 b
+= matrix
[i
] * t
->blue
;
4625 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4626 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4627 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4628 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4633 x_from_xcolors (f
, img
, new);
4639 /* Perform the pre-defined `emboss' edge-detection on image IMG
4643 x_emboss (struct frame
*f
, struct image
*img
)
4645 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4649 /* Transform image IMG which is used on frame F with a Laplace
4650 edge-detection algorithm. The result is an image that can be used
4651 to draw disabled buttons, for example. */
4654 x_laplace (struct frame
*f
, struct image
*img
)
4656 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4660 /* Perform edge-detection on image IMG on frame F, with specified
4661 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4663 MATRIX must be either
4665 - a list of at least 9 numbers in row-major form
4666 - a vector of at least 9 numbers
4668 COLOR_ADJUST nil means use a default; otherwise it must be a
4672 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4673 Lisp_Object color_adjust
)
4681 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4682 ++i
, matrix
= XCDR (matrix
))
4683 trans
[i
] = XFLOATINT (XCAR (matrix
));
4685 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4687 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4688 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4691 if (NILP (color_adjust
))
4692 color_adjust
= make_number (0xffff / 2);
4694 if (i
== 9 && NUMBERP (color_adjust
))
4695 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4699 /* Transform image IMG on frame F so that it looks disabled. */
4702 x_disable_image (struct frame
*f
, struct image
*img
)
4704 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4706 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4708 int n_planes
= dpyinfo
->n_planes
;
4709 #endif /* HAVE_NTGUI */
4713 /* Color (or grayscale). Convert to gray, and equalize. Just
4714 drawing such images with a stipple can look very odd, so
4715 we're using this method instead. */
4716 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4718 const int h
= 15000;
4719 const int l
= 30000;
4721 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4725 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4726 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4727 p
->red
= p
->green
= p
->blue
= i2
;
4730 x_from_xcolors (f
, img
, colors
);
4733 /* Draw a cross over the disabled image, if we must or if we
4735 if (n_planes
< 2 || cross_disabled_images
)
4738 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4740 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4742 Display
*dpy
= FRAME_X_DISPLAY (f
);
4743 GC gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4744 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4745 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4746 img
->width
- 1, img
->height
- 1);
4747 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4753 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4754 XSetForeground (dpy
, gc
, MaskForeground (f
));
4755 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4756 img
->width
- 1, img
->height
- 1);
4757 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4761 #endif /* !HAVE_NS */
4766 hdc
= get_frame_dc (f
);
4767 bmpdc
= CreateCompatibleDC (hdc
);
4768 release_frame_dc (f
, hdc
);
4770 prev
= SelectObject (bmpdc
, img
->pixmap
);
4772 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4773 MoveToEx (bmpdc
, 0, 0, NULL
);
4774 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4775 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4776 LineTo (bmpdc
, img
->width
- 1, 0);
4780 SelectObject (bmpdc
, img
->mask
);
4781 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4782 MoveToEx (bmpdc
, 0, 0, NULL
);
4783 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4784 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4785 LineTo (bmpdc
, img
->width
- 1, 0);
4787 SelectObject (bmpdc
, prev
);
4789 #endif /* HAVE_NTGUI */
4794 /* Build a mask for image IMG which is used on frame F. FILE is the
4795 name of an image file, for error messages. HOW determines how to
4796 determine the background color of IMG. If it is a list '(R G B)',
4797 with R, G, and B being integers >= 0, take that as the color of the
4798 background. Otherwise, determine the background color of IMG
4799 heuristically. Value is non-zero if successful. */
4802 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4804 XImagePtr_or_DC ximg
;
4812 #endif /* HAVE_NTGUI */
4813 int x
, y
, rc
, use_img_background
;
4814 unsigned long bg
= 0;
4818 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4819 img
->mask
= NO_PIXMAP
;
4820 img
->background_transparent_valid
= 0;
4825 /* Create an image and pixmap serving as mask. */
4826 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4827 &mask_img
, &img
->mask
);
4830 #endif /* !HAVE_NS */
4832 /* Get the X image of IMG->pixmap. */
4833 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4834 img
->width
, img
->height
,
4837 /* Create the bit array serving as mask. */
4838 row_width
= (img
->width
+ 7) / 8;
4839 mask_img
= xzalloc (row_width
* img
->height
);
4841 /* Create a memory device context for IMG->pixmap. */
4842 frame_dc
= get_frame_dc (f
);
4843 ximg
= CreateCompatibleDC (frame_dc
);
4844 release_frame_dc (f
, frame_dc
);
4845 prev
= SelectObject (ximg
, img
->pixmap
);
4846 #endif /* HAVE_NTGUI */
4848 /* Determine the background color of ximg. If HOW is `(R G B)'
4849 take that as color. Otherwise, use the image's background color. */
4850 use_img_background
= 1;
4856 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4858 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4862 if (i
== 3 && NILP (how
))
4864 char color_name
[30];
4865 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4868 0x00ffffff & /* Filter out palette info. */
4869 #endif /* HAVE_NTGUI */
4870 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4871 use_img_background
= 0;
4875 if (use_img_background
)
4876 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4878 /* Set all bits in mask_img to 1 whose color in ximg is different
4879 from the background color bg. */
4881 for (y
= 0; y
< img
->height
; ++y
)
4882 for (x
= 0; x
< img
->width
; ++x
)
4884 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4885 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4887 if (XGetPixel (ximg
, x
, y
) == bg
)
4888 ns_set_alpha (ximg
, x
, y
, 0);
4889 #endif /* HAVE_NS */
4891 /* Fill in the background_transparent field while we have the mask handy. */
4892 image_background_transparent (img
, f
, mask_img
);
4894 /* Put mask_img into img->mask. */
4895 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4896 x_destroy_x_image (mask_img
);
4897 #endif /* !HAVE_NS */
4899 for (y
= 0; y
< img
->height
; ++y
)
4900 for (x
= 0; x
< img
->width
; ++x
)
4902 COLORREF p
= GetPixel (ximg
, x
, y
);
4904 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4907 /* Create the mask image. */
4908 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4910 /* Fill in the background_transparent field while we have the mask handy. */
4911 SelectObject (ximg
, img
->mask
);
4912 image_background_transparent (img
, f
, ximg
);
4914 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4916 #endif /* HAVE_NTGUI */
4918 Destroy_Image (ximg
, prev
);
4924 /***********************************************************************
4925 PBM (mono, gray, color)
4926 ***********************************************************************/
4928 static int pbm_image_p (Lisp_Object object
);
4929 static int pbm_load (struct frame
*f
, struct image
*img
);
4930 static int pbm_scan_number (unsigned char **, unsigned char *);
4932 /* The symbol `pbm' identifying images of this type. */
4934 static Lisp_Object Qpbm
;
4936 /* Indices of image specification fields in gs_format, below. */
4938 enum pbm_keyword_index
4954 /* Vector of image_keyword structures describing the format
4955 of valid user-defined image specifications. */
4957 static const struct image_keyword pbm_format
[PBM_LAST
] =
4959 {":type", IMAGE_SYMBOL_VALUE
, 1},
4960 {":file", IMAGE_STRING_VALUE
, 0},
4961 {":data", IMAGE_STRING_VALUE
, 0},
4962 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4963 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
4964 {":relief", IMAGE_INTEGER_VALUE
, 0},
4965 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4966 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4967 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4968 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4969 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4972 /* Structure describing the image type `pbm'. */
4974 static struct image_type pbm_type
=
4984 /* Return non-zero if OBJECT is a valid PBM image specification. */
4987 pbm_image_p (Lisp_Object object
)
4989 struct image_keyword fmt
[PBM_LAST
];
4991 memcpy (fmt
, pbm_format
, sizeof fmt
);
4993 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4996 /* Must specify either :data or :file. */
4997 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5001 /* Scan a decimal number from *S and return it. Advance *S while
5002 reading the number. END is the end of the string. Value is -1 at
5006 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5008 int c
= 0, val
= -1;
5012 /* Skip white-space. */
5013 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
5018 /* Skip comment to end of line. */
5019 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5022 else if (c_isdigit (c
))
5024 /* Read decimal number. */
5026 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
5027 val
= 10 * val
+ c
- '0';
5039 #if 0 /* Unused. ++kfs */
5041 /* Read FILE into memory. Value is a pointer to a buffer allocated
5042 with xmalloc holding FILE's contents. Value is null if an error
5043 occurred. *SIZE is set to the size of the file. */
5046 pbm_read_file (Lisp_Object file
, int *size
)
5052 if (stat (SDATA (file
), &st
) == 0
5053 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5054 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5055 && (buf
= xmalloc (st
.st_size
),
5056 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5075 #endif /* HAVE_NTGUI */
5077 /* Load PBM image IMG for use on frame F. */
5080 pbm_load (struct frame
*f
, struct image
*img
)
5083 int width
, height
, max_color_idx
= 0;
5085 Lisp_Object file
, specified_file
;
5086 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5087 unsigned char *contents
= NULL
;
5088 unsigned char *end
, *p
;
5091 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5093 if (STRINGP (specified_file
))
5095 file
= x_find_image_file (specified_file
);
5096 if (!STRINGP (file
))
5098 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5102 contents
= slurp_file (SSDATA (file
), &size
);
5103 if (contents
== NULL
)
5105 image_error ("Error reading `%s'", file
, Qnil
);
5110 end
= contents
+ size
;
5115 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5116 if (!STRINGP (data
))
5118 image_error ("Invalid image data `%s'", data
, Qnil
);
5122 end
= p
+ SBYTES (data
);
5125 /* Check magic number. */
5126 if (end
- p
< 2 || *p
++ != 'P')
5128 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5137 raw_p
= 0, type
= PBM_MONO
;
5141 raw_p
= 0, type
= PBM_GRAY
;
5145 raw_p
= 0, type
= PBM_COLOR
;
5149 raw_p
= 1, type
= PBM_MONO
;
5153 raw_p
= 1, type
= PBM_GRAY
;
5157 raw_p
= 1, type
= PBM_COLOR
;
5161 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5165 /* Read width, height, maximum color-component. Characters
5166 starting with `#' up to the end of a line are ignored. */
5167 width
= pbm_scan_number (&p
, end
);
5168 height
= pbm_scan_number (&p
, end
);
5170 if (type
!= PBM_MONO
)
5172 max_color_idx
= pbm_scan_number (&p
, end
);
5173 if (max_color_idx
> 65535 || max_color_idx
< 0)
5175 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5180 if (!check_image_size (f
, width
, height
))
5182 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5186 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5187 &ximg
, &img
->pixmap
))
5190 /* Initialize the color hash table. */
5191 init_color_table ();
5193 if (type
== PBM_MONO
)
5196 struct image_keyword fmt
[PBM_LAST
];
5197 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5198 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5200 /* Parse the image specification. */
5201 memcpy (fmt
, pbm_format
, sizeof fmt
);
5202 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5204 /* Get foreground and background colors, maybe allocate colors. */
5205 if (fmt
[PBM_FOREGROUND
].count
5206 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5207 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5208 if (fmt
[PBM_BACKGROUND
].count
5209 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5211 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5212 img
->background
= bg
;
5213 img
->background_valid
= 1;
5216 for (y
= 0; y
< height
; ++y
)
5217 for (x
= 0; x
< width
; ++x
)
5225 x_destroy_x_image (ximg
);
5226 x_clear_image (f
, img
);
5227 image_error ("Invalid image size in image `%s'",
5237 g
= pbm_scan_number (&p
, end
);
5239 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5244 int expected_size
= height
* width
;
5245 if (max_color_idx
> 255)
5247 if (type
== PBM_COLOR
)
5250 if (raw_p
&& p
+ expected_size
> end
)
5252 x_destroy_x_image (ximg
);
5253 x_clear_image (f
, img
);
5254 image_error ("Invalid image size in image `%s'",
5259 for (y
= 0; y
< height
; ++y
)
5260 for (x
= 0; x
< width
; ++x
)
5264 if (type
== PBM_GRAY
&& raw_p
)
5267 if (max_color_idx
> 255)
5268 r
= g
= b
= r
* 256 + *p
++;
5270 else if (type
== PBM_GRAY
)
5271 r
= g
= b
= pbm_scan_number (&p
, end
);
5275 if (max_color_idx
> 255)
5278 if (max_color_idx
> 255)
5281 if (max_color_idx
> 255)
5286 r
= pbm_scan_number (&p
, end
);
5287 g
= pbm_scan_number (&p
, end
);
5288 b
= pbm_scan_number (&p
, end
);
5291 if (r
< 0 || g
< 0 || b
< 0)
5293 x_destroy_x_image (ximg
);
5294 image_error ("Invalid pixel value in image `%s'",
5299 /* RGB values are now in the range 0..max_color_idx.
5300 Scale this to the range 0..0xffff supported by X. */
5301 r
= (double) r
* 65535 / max_color_idx
;
5302 g
= (double) g
* 65535 / max_color_idx
;
5303 b
= (double) b
* 65535 / max_color_idx
;
5304 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5308 #ifdef COLOR_TABLE_SUPPORT
5309 /* Store in IMG->colors the colors allocated for the image, and
5310 free the color table. */
5311 img
->colors
= colors_in_color_table (&img
->ncolors
);
5312 free_color_table ();
5313 #endif /* COLOR_TABLE_SUPPORT */
5316 img
->height
= height
;
5318 /* Maybe fill in the background field while we have ximg handy. */
5320 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5321 /* Casting avoids a GCC warning. */
5322 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5324 /* Put the image into a pixmap. */
5325 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5326 x_destroy_x_image (ximg
);
5328 /* X and W32 versions did it here, MAC version above. ++kfs
5330 img->height = height; */
5337 /***********************************************************************
5339 ***********************************************************************/
5341 #if defined (HAVE_PNG) || defined (HAVE_NS)
5343 /* Function prototypes. */
5345 static int png_image_p (Lisp_Object object
);
5346 static int png_load (struct frame
*f
, struct image
*img
);
5348 /* The symbol `png' identifying images of this type. */
5350 static Lisp_Object Qpng
;
5352 /* Indices of image specification fields in png_format, below. */
5354 enum png_keyword_index
5369 /* Vector of image_keyword structures describing the format
5370 of valid user-defined image specifications. */
5372 static const struct image_keyword png_format
[PNG_LAST
] =
5374 {":type", IMAGE_SYMBOL_VALUE
, 1},
5375 {":data", IMAGE_STRING_VALUE
, 0},
5376 {":file", IMAGE_STRING_VALUE
, 0},
5377 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5378 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5379 {":relief", IMAGE_INTEGER_VALUE
, 0},
5380 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5381 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5382 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5383 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5386 /* Structure describing the image type `png'. */
5388 static struct image_type png_type
=
5397 /* Return non-zero if OBJECT is a valid PNG image specification. */
5400 png_image_p (Lisp_Object object
)
5402 struct image_keyword fmt
[PNG_LAST
];
5403 memcpy (fmt
, png_format
, sizeof fmt
);
5405 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5408 /* Must specify either the :data or :file keyword. */
5409 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5412 #endif /* HAVE_PNG || HAVE_NS */
5418 /* PNG library details. */
5420 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5421 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5422 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5423 png_error_ptr
, png_error_ptr
));
5424 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5425 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5426 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5427 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5428 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5429 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5430 png_uint_32
*, png_uint_32
*,
5431 int *, int *, int *, int *, int *));
5432 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5433 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5434 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5435 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5436 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5438 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5439 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5440 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5441 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5442 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5443 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5444 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5446 #if (PNG_LIBPNG_VER >= 10500)
5447 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5448 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5449 #endif /* libpng version >= 1.5 */
5452 init_png_functions (Lisp_Object libraries
)
5456 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5459 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5460 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5461 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5462 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5463 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5464 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5465 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5466 LOAD_IMGLIB_FN (library
, png_read_info
);
5467 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5468 LOAD_IMGLIB_FN (library
, png_get_valid
);
5469 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5470 LOAD_IMGLIB_FN (library
, png_set_expand
);
5471 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5472 LOAD_IMGLIB_FN (library
, png_set_background
);
5473 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5474 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5475 LOAD_IMGLIB_FN (library
, png_get_channels
);
5476 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5477 LOAD_IMGLIB_FN (library
, png_read_image
);
5478 LOAD_IMGLIB_FN (library
, png_read_end
);
5479 LOAD_IMGLIB_FN (library
, png_error
);
5481 #if (PNG_LIBPNG_VER >= 10500)
5482 LOAD_IMGLIB_FN (library
, png_longjmp
);
5483 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5484 #endif /* libpng version >= 1.5 */
5490 #define fn_png_get_io_ptr png_get_io_ptr
5491 #define fn_png_sig_cmp png_sig_cmp
5492 #define fn_png_create_read_struct png_create_read_struct
5493 #define fn_png_create_info_struct png_create_info_struct
5494 #define fn_png_destroy_read_struct png_destroy_read_struct
5495 #define fn_png_set_read_fn png_set_read_fn
5496 #define fn_png_set_sig_bytes png_set_sig_bytes
5497 #define fn_png_read_info png_read_info
5498 #define fn_png_get_IHDR png_get_IHDR
5499 #define fn_png_get_valid png_get_valid
5500 #define fn_png_set_strip_16 png_set_strip_16
5501 #define fn_png_set_expand png_set_expand
5502 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5503 #define fn_png_set_background png_set_background
5504 #define fn_png_get_bKGD png_get_bKGD
5505 #define fn_png_read_update_info png_read_update_info
5506 #define fn_png_get_channels png_get_channels
5507 #define fn_png_get_rowbytes png_get_rowbytes
5508 #define fn_png_read_image png_read_image
5509 #define fn_png_read_end png_read_end
5510 #define fn_png_error png_error
5512 #if (PNG_LIBPNG_VER >= 10500)
5513 #define fn_png_longjmp png_longjmp
5514 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5515 #endif /* libpng version >= 1.5 */
5517 #endif /* HAVE_NTGUI */
5520 #if (PNG_LIBPNG_VER < 10500)
5521 #define PNG_LONGJMP(ptr) (longjmp ((ptr)->jmpbuf, 1))
5522 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5524 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5525 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5526 #define PNG_JMPBUF(ptr) \
5527 (*fn_png_set_longjmp_fn ((ptr), longjmp, sizeof (jmp_buf)))
5530 /* Error and warning handlers installed when the PNG library
5533 static _Noreturn
void
5534 my_png_error (png_struct
*png_ptr
, const char *msg
)
5536 eassert (png_ptr
!= NULL
);
5537 /* Avoid compiler warning about deprecated direct access to
5538 png_ptr's fields in libpng versions 1.4.x. */
5539 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5540 PNG_LONGJMP (png_ptr
);
5545 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5547 eassert (png_ptr
!= NULL
);
5548 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5551 /* Memory source for PNG decoding. */
5553 struct png_memory_storage
5555 unsigned char *bytes
; /* The data */
5556 ptrdiff_t len
; /* How big is it? */
5557 ptrdiff_t index
; /* Where are we? */
5561 /* Function set as reader function when reading PNG image from memory.
5562 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5563 bytes from the input to DATA. */
5566 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5568 struct png_memory_storage
*tbr
5569 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5571 if (length
> tbr
->len
- tbr
->index
)
5572 fn_png_error (png_ptr
, "Read error");
5574 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5575 tbr
->index
= tbr
->index
+ length
;
5579 /* Function set as reader function when reading PNG image from a file.
5580 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5581 bytes from the input to DATA. */
5584 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5586 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5588 if (fread (data
, 1, length
, fp
) < length
)
5589 fn_png_error (png_ptr
, "Read error");
5593 /* Load PNG image IMG for use on frame F. Value is non-zero if
5597 png_load (struct frame
*f
, struct image
*img
)
5599 Lisp_Object file
, specified_file
;
5600 Lisp_Object specified_data
;
5603 XImagePtr ximg
, mask_img
= NULL
;
5604 png_struct
*png_ptr
= NULL
;
5605 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5606 FILE *volatile fp
= NULL
;
5608 png_byte
* volatile pixels
= NULL
;
5609 png_byte
** volatile rows
= NULL
;
5610 png_uint_32 width
, height
;
5611 int bit_depth
, color_type
, interlace_type
;
5613 png_uint_32 row_bytes
;
5615 struct png_memory_storage tbr
; /* Data to be read */
5617 /* Find out what file to load. */
5618 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5619 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5621 if (NILP (specified_data
))
5623 file
= x_find_image_file (specified_file
);
5624 if (!STRINGP (file
))
5626 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5630 /* Open the image file. */
5631 fp
= fopen (SSDATA (file
), "rb");
5634 image_error ("Cannot open image file `%s'", file
, Qnil
);
5638 /* Check PNG signature. */
5639 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5640 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5642 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5649 if (!STRINGP (specified_data
))
5651 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5655 /* Read from memory. */
5656 tbr
.bytes
= SDATA (specified_data
);
5657 tbr
.len
= SBYTES (specified_data
);
5660 /* Check PNG signature. */
5661 if (tbr
.len
< sizeof sig
5662 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5664 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5668 /* Need to skip past the signature. */
5669 tbr
.bytes
+= sizeof (sig
);
5672 /* Initialize read and info structs for PNG lib. */
5673 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5678 if (fp
) fclose (fp
);
5682 info_ptr
= fn_png_create_info_struct (png_ptr
);
5685 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5686 if (fp
) fclose (fp
);
5690 end_info
= fn_png_create_info_struct (png_ptr
);
5693 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5694 if (fp
) fclose (fp
);
5698 /* Set error jump-back. We come back here when the PNG library
5699 detects an error. */
5700 if (setjmp (PNG_JMPBUF (png_ptr
)))
5704 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5707 if (fp
) fclose (fp
);
5711 /* Read image info. */
5712 if (!NILP (specified_data
))
5713 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5715 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5717 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5718 fn_png_read_info (png_ptr
, info_ptr
);
5719 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5720 &interlace_type
, NULL
, NULL
);
5722 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5723 && check_image_size (f
, width
, height
)))
5725 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5729 /* Create the X image and pixmap now, so that the work below can be
5730 omitted if the image is too large for X. */
5731 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5735 /* If image contains simply transparency data, we prefer to
5736 construct a clipping mask. */
5737 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5742 /* This function is easier to write if we only have to handle
5743 one data format: RGB or RGBA with 8 bits per channel. Let's
5744 transform other formats into that format. */
5746 /* Strip more than 8 bits per channel. */
5747 if (bit_depth
== 16)
5748 fn_png_set_strip_16 (png_ptr
);
5750 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5752 fn_png_set_expand (png_ptr
);
5754 /* Convert grayscale images to RGB. */
5755 if (color_type
== PNG_COLOR_TYPE_GRAY
5756 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5757 fn_png_set_gray_to_rgb (png_ptr
);
5759 /* Handle alpha channel by combining the image with a background
5760 color. Do this only if a real alpha channel is supplied. For
5761 simple transparency, we prefer a clipping mask. */
5764 /* png_color_16 *image_bg; */
5765 Lisp_Object specified_bg
5766 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5767 int shift
= (bit_depth
== 16) ? 0 : 8;
5769 if (STRINGP (specified_bg
))
5770 /* The user specified `:background', use that. */
5773 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5775 png_color_16 user_bg
;
5777 memset (&user_bg
, 0, sizeof user_bg
);
5778 user_bg
.red
= color
.red
>> shift
;
5779 user_bg
.green
= color
.green
>> shift
;
5780 user_bg
.blue
= color
.blue
>> shift
;
5782 fn_png_set_background (png_ptr
, &user_bg
,
5783 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5788 /* We use the current frame background, ignoring any default
5789 background color set by the image. */
5790 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5792 png_color_16 frame_background
;
5794 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5795 x_query_color (f
, &color
);
5797 memset (&frame_background
, 0, sizeof frame_background
);
5798 frame_background
.red
= color
.red
>> shift
;
5799 frame_background
.green
= color
.green
>> shift
;
5800 frame_background
.blue
= color
.blue
>> shift
;
5801 #endif /* HAVE_X_WINDOWS */
5803 fn_png_set_background (png_ptr
, &frame_background
,
5804 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5808 /* Update info structure. */
5809 fn_png_read_update_info (png_ptr
, info_ptr
);
5811 /* Get number of channels. Valid values are 1 for grayscale images
5812 and images with a palette, 2 for grayscale images with transparency
5813 information (alpha channel), 3 for RGB images, and 4 for RGB
5814 images with alpha channel, i.e. RGBA. If conversions above were
5815 sufficient we should only have 3 or 4 channels here. */
5816 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5817 eassert (channels
== 3 || channels
== 4);
5819 /* Number of bytes needed for one row of the image. */
5820 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5822 /* Allocate memory for the image. */
5823 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5824 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5825 memory_full (SIZE_MAX
);
5826 pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5827 rows
= xmalloc (height
* sizeof *rows
);
5828 for (i
= 0; i
< height
; ++i
)
5829 rows
[i
] = pixels
+ i
* row_bytes
;
5831 /* Read the entire image. */
5832 fn_png_read_image (png_ptr
, rows
);
5833 fn_png_read_end (png_ptr
, info_ptr
);
5840 /* Create an image and pixmap serving as mask if the PNG image
5841 contains an alpha channel. */
5844 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5845 &mask_img
, &img
->mask
))
5847 x_destroy_x_image (ximg
);
5848 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5849 img
->pixmap
= NO_PIXMAP
;
5853 /* Fill the X image and mask from PNG data. */
5854 init_color_table ();
5856 for (y
= 0; y
< height
; ++y
)
5858 png_byte
*p
= rows
[y
];
5860 for (x
= 0; x
< width
; ++x
)
5867 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5868 /* An alpha channel, aka mask channel, associates variable
5869 transparency with an image. Where other image formats
5870 support binary transparency---fully transparent or fully
5871 opaque---PNG allows up to 254 levels of partial transparency.
5872 The PNG library implements partial transparency by combining
5873 the image with a specified background color.
5875 I'm not sure how to handle this here nicely: because the
5876 background on which the image is displayed may change, for
5877 real alpha channel support, it would be necessary to create
5878 a new image for each possible background.
5880 What I'm doing now is that a mask is created if we have
5881 boolean transparency information. Otherwise I'm using
5882 the frame's background color to combine the image with. */
5887 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5893 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5894 /* Set IMG's background color from the PNG image, unless the user
5898 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5900 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5901 img
->background_valid
= 1;
5905 #ifdef COLOR_TABLE_SUPPORT
5906 /* Remember colors allocated for this image. */
5907 img
->colors
= colors_in_color_table (&img
->ncolors
);
5908 free_color_table ();
5909 #endif /* COLOR_TABLE_SUPPORT */
5912 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5917 img
->height
= height
;
5919 /* Maybe fill in the background field while we have ximg handy.
5920 Casting avoids a GCC warning. */
5921 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5923 /* Put the image into the pixmap, then free the X image and its buffer. */
5924 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5925 x_destroy_x_image (ximg
);
5927 /* Same for the mask. */
5930 /* Fill in the background_transparent field while we have the
5931 mask handy. Casting avoids a GCC warning. */
5932 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5934 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5935 x_destroy_x_image (mask_img
);
5941 #else /* HAVE_PNG */
5945 png_load (struct frame
*f
, struct image
*img
)
5947 return ns_load_image (f
, img
,
5948 image_spec_value (img
->spec
, QCfile
, NULL
),
5949 image_spec_value (img
->spec
, QCdata
, NULL
));
5951 #endif /* HAVE_NS */
5954 #endif /* !HAVE_PNG */
5958 /***********************************************************************
5960 ***********************************************************************/
5962 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5964 static int jpeg_image_p (Lisp_Object object
);
5965 static int jpeg_load (struct frame
*f
, struct image
*img
);
5967 /* The symbol `jpeg' identifying images of this type. */
5969 static Lisp_Object Qjpeg
;
5971 /* Indices of image specification fields in gs_format, below. */
5973 enum jpeg_keyword_index
5982 JPEG_HEURISTIC_MASK
,
5988 /* Vector of image_keyword structures describing the format
5989 of valid user-defined image specifications. */
5991 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5993 {":type", IMAGE_SYMBOL_VALUE
, 1},
5994 {":data", IMAGE_STRING_VALUE
, 0},
5995 {":file", IMAGE_STRING_VALUE
, 0},
5996 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5997 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5998 {":relief", IMAGE_INTEGER_VALUE
, 0},
5999 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6000 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6001 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6002 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6005 /* Structure describing the image type `jpeg'. */
6007 static struct image_type jpeg_type
=
6016 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6019 jpeg_image_p (Lisp_Object object
)
6021 struct image_keyword fmt
[JPEG_LAST
];
6023 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6025 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6028 /* Must specify either the :data or :file keyword. */
6029 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6032 #endif /* HAVE_JPEG || HAVE_NS */
6036 /* Work around a warning about HAVE_STDLIB_H being redefined in
6038 #ifdef HAVE_STDLIB_H
6039 #undef HAVE_STDLIB_H
6040 #endif /* HAVE_STLIB_H */
6042 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6043 /* In older releases of the jpeg library, jpeglib.h will define boolean
6044 differently depending on __WIN32__, so make sure it is defined. */
6048 #include <jpeglib.h>
6051 #ifdef HAVE_STLIB_H_1
6052 #define HAVE_STDLIB_H 1
6057 /* JPEG library details. */
6058 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6059 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6060 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6061 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6062 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6063 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6064 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6065 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6068 init_jpeg_functions (Lisp_Object libraries
)
6072 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6075 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6076 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6077 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6078 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6079 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6080 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6081 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6082 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6086 /* Wrapper since we can't directly assign the function pointer
6087 to another function pointer that was declared more completely easily. */
6089 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6091 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6096 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6097 #define fn_jpeg_start_decompress jpeg_start_decompress
6098 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6099 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6100 #define fn_jpeg_read_header jpeg_read_header
6101 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6102 #define fn_jpeg_std_error jpeg_std_error
6103 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6105 #endif /* HAVE_NTGUI */
6107 struct my_jpeg_error_mgr
6109 struct jpeg_error_mgr pub
;
6110 jmp_buf setjmp_buffer
;
6114 static _Noreturn
void
6115 my_error_exit (j_common_ptr cinfo
)
6117 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6118 longjmp (mgr
->setjmp_buffer
, 1);
6122 /* Init source method for JPEG data source manager. Called by
6123 jpeg_read_header() before any data is actually read. See
6124 libjpeg.doc from the JPEG lib distribution. */
6127 our_common_init_source (j_decompress_ptr cinfo
)
6132 /* Method to terminate data source. Called by
6133 jpeg_finish_decompress() after all data has been processed. */
6136 our_common_term_source (j_decompress_ptr cinfo
)
6141 /* Fill input buffer method for JPEG data source manager. Called
6142 whenever more data is needed. We read the whole image in one step,
6143 so this only adds a fake end of input marker at the end. */
6145 static JOCTET our_memory_buffer
[2];
6148 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6150 /* Insert a fake EOI marker. */
6151 struct jpeg_source_mgr
*src
= cinfo
->src
;
6153 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6154 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6156 src
->next_input_byte
= our_memory_buffer
;
6157 src
->bytes_in_buffer
= 2;
6162 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6163 is the JPEG data source manager. */
6166 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6168 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6172 if (num_bytes
> src
->bytes_in_buffer
)
6173 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6175 src
->bytes_in_buffer
-= num_bytes
;
6176 src
->next_input_byte
+= num_bytes
;
6181 /* Set up the JPEG lib for reading an image from DATA which contains
6182 LEN bytes. CINFO is the decompression info structure created for
6183 reading the image. */
6186 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6188 struct jpeg_source_mgr
*src
;
6190 if (cinfo
->src
== NULL
)
6192 /* First time for this JPEG object? */
6193 cinfo
->src
= (struct jpeg_source_mgr
*)
6194 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6195 sizeof (struct jpeg_source_mgr
));
6196 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6197 src
->next_input_byte
= data
;
6200 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6201 src
->init_source
= our_common_init_source
;
6202 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6203 src
->skip_input_data
= our_memory_skip_input_data
;
6204 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6205 src
->term_source
= our_common_term_source
;
6206 src
->bytes_in_buffer
= len
;
6207 src
->next_input_byte
= data
;
6211 struct jpeg_stdio_mgr
6213 struct jpeg_source_mgr mgr
;
6220 /* Size of buffer to read JPEG from file.
6221 Not too big, as we want to use alloc_small. */
6222 #define JPEG_STDIO_BUFFER_SIZE 8192
6225 /* Fill input buffer method for JPEG data source manager. Called
6226 whenever more data is needed. The data is read from a FILE *. */
6229 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6231 struct jpeg_stdio_mgr
*src
;
6233 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6238 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6240 src
->mgr
.bytes_in_buffer
= bytes
;
6243 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6245 src
->buffer
[0] = (JOCTET
) 0xFF;
6246 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6247 src
->mgr
.bytes_in_buffer
= 2;
6249 src
->mgr
.next_input_byte
= src
->buffer
;
6256 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6257 is the JPEG data source manager. */
6260 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6262 struct jpeg_stdio_mgr
*src
;
6263 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6265 while (num_bytes
> 0 && !src
->finished
)
6267 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6269 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6270 src
->mgr
.next_input_byte
+= num_bytes
;
6275 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6276 src
->mgr
.bytes_in_buffer
= 0;
6277 src
->mgr
.next_input_byte
= NULL
;
6279 our_stdio_fill_input_buffer (cinfo
);
6285 /* Set up the JPEG lib for reading an image from a FILE *.
6286 CINFO is the decompression info structure created for
6287 reading the image. */
6290 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6292 struct jpeg_stdio_mgr
*src
;
6294 if (cinfo
->src
!= NULL
)
6295 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6298 /* First time for this JPEG object? */
6299 cinfo
->src
= (struct jpeg_source_mgr
*)
6300 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6301 sizeof (struct jpeg_stdio_mgr
));
6302 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6303 src
->buffer
= (JOCTET
*)
6304 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6305 JPEG_STDIO_BUFFER_SIZE
);
6310 src
->mgr
.init_source
= our_common_init_source
;
6311 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6312 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6313 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6314 src
->mgr
.term_source
= our_common_term_source
;
6315 src
->mgr
.bytes_in_buffer
= 0;
6316 src
->mgr
.next_input_byte
= NULL
;
6320 /* Load image IMG for use on frame F. Patterned after example.c
6321 from the JPEG lib. */
6324 jpeg_load (struct frame
*f
, struct image
*img
)
6326 struct jpeg_decompress_struct cinfo
;
6327 struct my_jpeg_error_mgr mgr
;
6328 Lisp_Object file
, specified_file
;
6329 Lisp_Object specified_data
;
6330 FILE * volatile fp
= NULL
;
6332 int row_stride
, x
, y
;
6333 XImagePtr ximg
= NULL
;
6335 unsigned long *colors
;
6338 /* Open the JPEG file. */
6339 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6340 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6342 if (NILP (specified_data
))
6344 file
= x_find_image_file (specified_file
);
6345 if (!STRINGP (file
))
6347 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6351 fp
= fopen (SSDATA (file
), "rb");
6354 image_error ("Cannot open `%s'", file
, Qnil
);
6358 else if (!STRINGP (specified_data
))
6360 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6364 /* Customize libjpeg's error handling to call my_error_exit when an
6365 error is detected. This function will perform a longjmp. */
6366 cinfo
.err
= fn_jpeg_std_error (&mgr
.pub
);
6367 mgr
.pub
.error_exit
= my_error_exit
;
6369 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6373 /* Called from my_error_exit. Display a JPEG error. */
6374 char buf
[JMSG_LENGTH_MAX
];
6375 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buf
);
6376 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6377 build_string (buf
));
6380 /* Close the input file and destroy the JPEG object. */
6382 fclose ((FILE *) fp
);
6383 fn_jpeg_destroy_decompress (&cinfo
);
6385 /* If we already have an XImage, free that. */
6386 x_destroy_x_image (ximg
);
6388 /* Free pixmap and colors. */
6389 x_clear_image (f
, img
);
6393 /* Create the JPEG decompression object. Let it read from fp.
6394 Read the JPEG image header. */
6395 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6397 if (NILP (specified_data
))
6398 jpeg_file_src (&cinfo
, (FILE *) fp
);
6400 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6401 SBYTES (specified_data
));
6403 fn_jpeg_read_header (&cinfo
, 1);
6405 /* Customize decompression so that color quantization will be used.
6406 Start decompression. */
6407 cinfo
.quantize_colors
= 1;
6408 fn_jpeg_start_decompress (&cinfo
);
6409 width
= img
->width
= cinfo
.output_width
;
6410 height
= img
->height
= cinfo
.output_height
;
6412 if (!check_image_size (f
, width
, height
))
6414 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6415 longjmp (mgr
.setjmp_buffer
, 2);
6418 /* Create X image and pixmap. */
6419 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6420 longjmp (mgr
.setjmp_buffer
, 2);
6422 /* Allocate colors. When color quantization is used,
6423 cinfo.actual_number_of_colors has been set with the number of
6424 colors generated, and cinfo.colormap is a two-dimensional array
6425 of color indices in the range 0..cinfo.actual_number_of_colors.
6426 No more than 255 colors will be generated. */
6430 if (cinfo
.out_color_components
> 2)
6431 ir
= 0, ig
= 1, ib
= 2;
6432 else if (cinfo
.out_color_components
> 1)
6433 ir
= 0, ig
= 1, ib
= 0;
6435 ir
= 0, ig
= 0, ib
= 0;
6437 /* Use the color table mechanism because it handles colors that
6438 cannot be allocated nicely. Such colors will be replaced with
6439 a default color, and we don't have to care about which colors
6440 can be freed safely, and which can't. */
6441 init_color_table ();
6442 colors
= alloca (cinfo
.actual_number_of_colors
* sizeof *colors
);
6444 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6446 /* Multiply RGB values with 255 because X expects RGB values
6447 in the range 0..0xffff. */
6448 int r
= cinfo
.colormap
[ir
][i
] << 8;
6449 int g
= cinfo
.colormap
[ig
][i
] << 8;
6450 int b
= cinfo
.colormap
[ib
][i
] << 8;
6451 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6454 #ifdef COLOR_TABLE_SUPPORT
6455 /* Remember those colors actually allocated. */
6456 img
->colors
= colors_in_color_table (&img
->ncolors
);
6457 free_color_table ();
6458 #endif /* COLOR_TABLE_SUPPORT */
6462 row_stride
= width
* cinfo
.output_components
;
6463 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6465 for (y
= 0; y
< height
; ++y
)
6467 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6468 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6469 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6473 fn_jpeg_finish_decompress (&cinfo
);
6474 fn_jpeg_destroy_decompress (&cinfo
);
6476 fclose ((FILE *) fp
);
6478 /* Maybe fill in the background field while we have ximg handy. */
6479 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6480 /* Casting avoids a GCC warning. */
6481 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6483 /* Put the image into the pixmap. */
6484 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6485 x_destroy_x_image (ximg
);
6489 #else /* HAVE_JPEG */
6493 jpeg_load (struct frame
*f
, struct image
*img
)
6495 return ns_load_image (f
, img
,
6496 image_spec_value (img
->spec
, QCfile
, NULL
),
6497 image_spec_value (img
->spec
, QCdata
, NULL
));
6499 #endif /* HAVE_NS */
6501 #endif /* !HAVE_JPEG */
6505 /***********************************************************************
6507 ***********************************************************************/
6509 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6511 static int tiff_image_p (Lisp_Object object
);
6512 static int tiff_load (struct frame
*f
, struct image
*img
);
6514 /* The symbol `tiff' identifying images of this type. */
6516 static Lisp_Object Qtiff
;
6518 /* Indices of image specification fields in tiff_format, below. */
6520 enum tiff_keyword_index
6529 TIFF_HEURISTIC_MASK
,
6536 /* Vector of image_keyword structures describing the format
6537 of valid user-defined image specifications. */
6539 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6541 {":type", IMAGE_SYMBOL_VALUE
, 1},
6542 {":data", IMAGE_STRING_VALUE
, 0},
6543 {":file", IMAGE_STRING_VALUE
, 0},
6544 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6545 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6546 {":relief", IMAGE_INTEGER_VALUE
, 0},
6547 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6548 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6549 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6550 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6551 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6554 /* Structure describing the image type `tiff'. */
6556 static struct image_type tiff_type
=
6565 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6568 tiff_image_p (Lisp_Object object
)
6570 struct image_keyword fmt
[TIFF_LAST
];
6571 memcpy (fmt
, tiff_format
, sizeof fmt
);
6573 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6576 /* Must specify either the :data or :file keyword. */
6577 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6580 #endif /* HAVE_TIFF || HAVE_NS */
6588 /* TIFF library details. */
6589 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6590 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6591 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6592 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6593 TIFFReadWriteProc
, TIFFReadWriteProc
,
6594 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6595 TIFFMapFileProc
, TIFFUnmapFileProc
));
6596 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6597 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6598 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6599 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6602 init_tiff_functions (Lisp_Object libraries
)
6606 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6609 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6610 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6611 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6612 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6613 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6614 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6615 LOAD_IMGLIB_FN (library
, TIFFClose
);
6616 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6622 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6623 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6624 #define fn_TIFFOpen TIFFOpen
6625 #define fn_TIFFClientOpen TIFFClientOpen
6626 #define fn_TIFFGetField TIFFGetField
6627 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6628 #define fn_TIFFClose TIFFClose
6629 #define fn_TIFFSetDirectory TIFFSetDirectory
6630 #endif /* HAVE_NTGUI */
6633 /* Reading from a memory buffer for TIFF images Based on the PNG
6634 memory source, but we have to provide a lot of extra functions.
6637 We really only need to implement read and seek, but I am not
6638 convinced that the TIFF library is smart enough not to destroy
6639 itself if we only hand it the function pointers we need to
6644 unsigned char *bytes
;
6651 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6653 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6655 size
= min (size
, src
->len
- src
->index
);
6656 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6662 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6668 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6670 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6675 case SEEK_SET
: /* Go from beginning of source. */
6679 case SEEK_END
: /* Go from end of source. */
6680 idx
= src
->len
+ off
;
6683 case SEEK_CUR
: /* Go from current position. */
6684 idx
= src
->index
+ off
;
6687 default: /* Invalid `whence'. */
6691 if (idx
> src
->len
|| idx
< 0)
6699 tiff_close_memory (thandle_t data
)
6706 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6708 /* It is already _IN_ memory. */
6713 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6715 /* We don't need to do this. */
6719 tiff_size_of_memory (thandle_t data
)
6721 return ((tiff_memory_source
*) data
)->len
;
6724 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6725 compiler error compiling tiff_handler, see Bugzilla bug #17406
6726 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6727 this function as external works around that problem. */
6728 #if defined (__MINGW32__) && __GNUC__ == 3
6729 # define MINGW_STATIC
6731 # define MINGW_STATIC static
6735 tiff_handler (const char *, const char *, const char *, va_list)
6736 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6738 tiff_handler (const char *log_format
, const char *title
,
6739 const char *format
, va_list ap
)
6741 /* doprnt is not suitable here, as TIFF handlers are called from
6742 libtiff and are passed arbitrary printf directives. Instead, use
6743 vsnprintf, taking care to be portable to nonstandard environments
6744 where vsnprintf returns -1 on buffer overflow. Since it's just a
6745 log entry, it's OK to truncate it. */
6747 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6748 add_to_log (log_format
, build_string (title
),
6749 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6753 static void tiff_error_handler (const char *, const char *, va_list)
6754 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6756 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6758 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6762 static void tiff_warning_handler (const char *, const char *, va_list)
6763 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6765 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6767 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6771 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6775 tiff_load (struct frame
*f
, struct image
*img
)
6777 Lisp_Object file
, specified_file
;
6778 Lisp_Object specified_data
;
6780 int width
, height
, x
, y
, count
;
6784 tiff_memory_source memsrc
;
6787 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6788 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6790 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6791 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6793 if (NILP (specified_data
))
6795 /* Read from a file */
6796 file
= x_find_image_file (specified_file
);
6797 if (!STRINGP (file
))
6799 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6803 /* Try to open the image file. */
6804 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6807 image_error ("Cannot open `%s'", file
, Qnil
);
6813 if (!STRINGP (specified_data
))
6815 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6819 /* Memory source! */
6820 memsrc
.bytes
= SDATA (specified_data
);
6821 memsrc
.len
= SBYTES (specified_data
);
6824 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6825 tiff_read_from_memory
,
6826 tiff_write_from_memory
,
6827 tiff_seek_in_memory
,
6829 tiff_size_of_memory
,
6835 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6840 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6841 if (INTEGERP (image
))
6843 EMACS_INT ino
= XFASTINT (image
);
6844 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
6845 && fn_TIFFSetDirectory (tiff
, ino
)))
6847 image_error ("Invalid image number `%s' in image `%s'",
6849 fn_TIFFClose (tiff
);
6854 /* Get width and height of the image, and allocate a raster buffer
6855 of width x height 32-bit values. */
6856 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6857 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6859 if (!check_image_size (f
, width
, height
))
6861 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6862 fn_TIFFClose (tiff
);
6866 /* Create the X image and pixmap. */
6867 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6868 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6869 &ximg
, &img
->pixmap
)))
6871 fn_TIFFClose (tiff
);
6875 buf
= xmalloc (sizeof *buf
* width
* height
);
6877 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6879 /* Count the number of images in the file. */
6880 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6884 img
->lisp_data
= Fcons (Qcount
,
6885 Fcons (make_number (count
),
6888 fn_TIFFClose (tiff
);
6891 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6896 /* Initialize the color table. */
6897 init_color_table ();
6899 /* Process the pixel raster. Origin is in the lower-left corner. */
6900 for (y
= 0; y
< height
; ++y
)
6902 uint32
*row
= buf
+ y
* width
;
6904 for (x
= 0; x
< width
; ++x
)
6906 uint32 abgr
= row
[x
];
6907 int r
= TIFFGetR (abgr
) << 8;
6908 int g
= TIFFGetG (abgr
) << 8;
6909 int b
= TIFFGetB (abgr
) << 8;
6910 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6914 #ifdef COLOR_TABLE_SUPPORT
6915 /* Remember the colors allocated for the image. Free the color table. */
6916 img
->colors
= colors_in_color_table (&img
->ncolors
);
6917 free_color_table ();
6918 #endif /* COLOR_TABLE_SUPPORT */
6921 img
->height
= height
;
6923 /* Maybe fill in the background field while we have ximg handy. */
6924 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6925 /* Casting avoids a GCC warning on W32. */
6926 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6928 /* Put the image into the pixmap, then free the X image and its buffer. */
6929 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6930 x_destroy_x_image (ximg
);
6936 #else /* HAVE_TIFF */
6940 tiff_load (struct frame
*f
, struct image
*img
)
6942 return ns_load_image (f
, img
,
6943 image_spec_value (img
->spec
, QCfile
, NULL
),
6944 image_spec_value (img
->spec
, QCdata
, NULL
));
6946 #endif /* HAVE_NS */
6948 #endif /* !HAVE_TIFF */
6952 /***********************************************************************
6954 ***********************************************************************/
6956 #if defined (HAVE_GIF) || defined (HAVE_NS)
6958 static int gif_image_p (Lisp_Object object
);
6959 static int gif_load (struct frame
*f
, struct image
*img
);
6960 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6962 /* The symbol `gif' identifying images of this type. */
6964 static Lisp_Object Qgif
;
6966 /* Indices of image specification fields in gif_format, below. */
6968 enum gif_keyword_index
6984 /* Vector of image_keyword structures describing the format
6985 of valid user-defined image specifications. */
6987 static const struct image_keyword gif_format
[GIF_LAST
] =
6989 {":type", IMAGE_SYMBOL_VALUE
, 1},
6990 {":data", IMAGE_STRING_VALUE
, 0},
6991 {":file", IMAGE_STRING_VALUE
, 0},
6992 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6993 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6994 {":relief", IMAGE_INTEGER_VALUE
, 0},
6995 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6996 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6997 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6998 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
6999 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7002 /* Structure describing the image type `gif'. */
7004 static struct image_type gif_type
=
7013 /* Free X resources of GIF image IMG which is used on frame F. */
7016 gif_clear_image (struct frame
*f
, struct image
*img
)
7018 img
->lisp_data
= Qnil
;
7019 x_clear_image (f
, img
);
7022 /* Return non-zero if OBJECT is a valid GIF image specification. */
7025 gif_image_p (Lisp_Object object
)
7027 struct image_keyword fmt
[GIF_LAST
];
7028 memcpy (fmt
, gif_format
, sizeof fmt
);
7030 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7033 /* Must specify either the :data or :file keyword. */
7034 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7037 #endif /* HAVE_GIF */
7041 #if defined (HAVE_NTGUI)
7042 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7043 Undefine before redefining to avoid a preprocessor warning. */
7047 /* avoid conflict with QuickdrawText.h */
7048 #define DrawText gif_DrawText
7049 #include <gif_lib.h>
7052 #else /* HAVE_NTGUI */
7054 #include <gif_lib.h>
7056 #endif /* HAVE_NTGUI */
7061 /* GIF library details. */
7062 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7063 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7064 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7065 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7068 init_gif_functions (Lisp_Object libraries
)
7072 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7075 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7076 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7077 LOAD_IMGLIB_FN (library
, DGifOpen
);
7078 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7084 #define fn_DGifCloseFile DGifCloseFile
7085 #define fn_DGifSlurp DGifSlurp
7086 #define fn_DGifOpen DGifOpen
7087 #define fn_DGifOpenFileName DGifOpenFileName
7089 #endif /* HAVE_NTGUI */
7091 /* Reading a GIF image from memory
7092 Based on the PNG memory stuff to a certain extent. */
7096 unsigned char *bytes
;
7102 /* Make the current memory source available to gif_read_from_memory.
7103 It's done this way because not all versions of libungif support
7104 a UserData field in the GifFileType structure. */
7105 static gif_memory_source
*current_gif_memory_src
;
7108 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7110 gif_memory_source
*src
= current_gif_memory_src
;
7112 if (len
> src
->len
- src
->index
)
7115 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7121 /* Load GIF image IMG for use on frame F. Value is non-zero if
7124 static const int interlace_start
[] = {0, 4, 2, 1};
7125 static const int interlace_increment
[] = {8, 8, 4, 2};
7127 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7130 gif_load (struct frame
*f
, struct image
*img
)
7133 int rc
, width
, height
, x
, y
, i
, j
;
7135 ColorMapObject
*gif_color_map
;
7136 unsigned long pixel_colors
[256];
7138 gif_memory_source memsrc
;
7139 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7140 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7141 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7142 unsigned long bgcolor
= 0;
7145 if (NILP (specified_data
))
7147 file
= x_find_image_file (specified_file
);
7148 if (!STRINGP (file
))
7150 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7154 /* Open the GIF file. */
7155 gif
= fn_DGifOpenFileName (SSDATA (file
));
7158 image_error ("Cannot open `%s'", file
, Qnil
);
7164 if (!STRINGP (specified_data
))
7166 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7170 /* Read from memory! */
7171 current_gif_memory_src
= &memsrc
;
7172 memsrc
.bytes
= SDATA (specified_data
);
7173 memsrc
.len
= SBYTES (specified_data
);
7176 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7179 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7184 /* Before reading entire contents, check the declared image size. */
7185 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7187 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7188 fn_DGifCloseFile (gif
);
7192 /* Read entire contents. */
7193 rc
= fn_DGifSlurp (gif
);
7194 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7196 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7197 fn_DGifCloseFile (gif
);
7201 /* Which sub-image are we to display? */
7203 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7204 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7205 if (idx
< 0 || idx
>= gif
->ImageCount
)
7207 image_error ("Invalid image number `%s' in image `%s'",
7208 image_number
, img
->spec
);
7209 fn_DGifCloseFile (gif
);
7214 width
= img
->width
= gif
->SWidth
;
7215 height
= img
->height
= gif
->SHeight
;
7217 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7218 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7219 img
->corners
[BOT_CORNER
]
7220 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7221 img
->corners
[RIGHT_CORNER
]
7222 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7224 if (!check_image_size (f
, width
, height
))
7226 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7227 fn_DGifCloseFile (gif
);
7231 /* Create the X image and pixmap. */
7232 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7234 fn_DGifCloseFile (gif
);
7238 /* Clear the part of the screen image not covered by the image.
7239 Full animated GIF support requires more here (see the gif89 spec,
7240 disposal methods). Let's simply assume that the part not covered
7241 by a sub-image is in the frame's background color. */
7242 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7243 for (x
= 0; x
< width
; ++x
)
7244 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7246 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7247 for (x
= 0; x
< width
; ++x
)
7248 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7250 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7252 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7253 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7254 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7255 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7258 /* Read the GIF image into the X image. */
7260 /* FIXME: With the current implementation, loading an animated gif
7261 is quadratic in the number of animation frames, since each frame
7262 is a separate struct image. We must provide a way for a single
7263 gif_load call to construct and save all animation frames. */
7265 init_color_table ();
7266 if (STRINGP (specified_bg
))
7267 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7268 FRAME_BACKGROUND_PIXEL (f
));
7269 for (j
= 0; j
<= idx
; ++j
)
7271 /* We use a local variable `raster' here because RasterBits is a
7272 char *, which invites problems with bytes >= 0x80. */
7273 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7274 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7275 int transparency_color_index
= -1;
7277 int subimg_width
= subimage
->ImageDesc
.Width
;
7278 int subimg_height
= subimage
->ImageDesc
.Height
;
7279 int subimg_top
= subimage
->ImageDesc
.Top
;
7280 int subimg_left
= subimage
->ImageDesc
.Left
;
7282 /* Find the Graphic Control Extension block for this sub-image.
7283 Extract the disposal method and transparency color. */
7284 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7286 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7288 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7289 && extblock
->ByteCount
== 4
7290 && extblock
->Bytes
[0] & 1)
7292 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7293 to background". Treat any other value like 2. */
7294 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7295 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7300 /* We can't "keep in place" the first subimage. */
7304 /* For disposal == 0, the spec says "No disposal specified. The
7305 decoder is not required to take any action." In practice, it
7306 seems we need to treat this like "keep in place", see e.g.
7307 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7311 /* Allocate subimage colors. */
7312 memset (pixel_colors
, 0, sizeof pixel_colors
);
7313 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7315 gif_color_map
= gif
->SColorMap
;
7318 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7320 if (transparency_color_index
== i
)
7321 pixel_colors
[i
] = STRINGP (specified_bg
)
7322 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7325 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7326 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7327 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7328 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7332 /* Apply the pixel values. */
7333 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7337 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7339 y
++, row
+= interlace_increment
[pass
])
7341 if (row
>= subimg_height
)
7343 row
= interlace_start
[++pass
];
7344 while (row
>= subimg_height
)
7345 row
= interlace_start
[++pass
];
7348 for (x
= 0; x
< subimg_width
; x
++)
7350 int c
= raster
[y
* subimg_width
+ x
];
7351 if (transparency_color_index
!= c
|| disposal
!= 1)
7352 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7359 for (y
= 0; y
< subimg_height
; ++y
)
7360 for (x
= 0; x
< subimg_width
; ++x
)
7362 int c
= raster
[y
* subimg_width
+ x
];
7363 if (transparency_color_index
!= c
|| disposal
!= 1)
7364 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7370 #ifdef COLOR_TABLE_SUPPORT
7371 img
->colors
= colors_in_color_table (&img
->ncolors
);
7372 free_color_table ();
7373 #endif /* COLOR_TABLE_SUPPORT */
7375 /* Save GIF image extension data for `image-metadata'.
7376 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7377 img
->lisp_data
= Qnil
;
7378 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7381 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7382 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7383 /* Append (... FUNCTION "BYTES") */
7386 = Fcons (make_number (ext
->Function
),
7387 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7389 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7390 && ext
->ByteCount
== 4)
7392 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7393 delay
|= ext
->Bytes
[1];
7396 img
->lisp_data
= Fcons (Qextension_data
,
7397 Fcons (img
->lisp_data
, Qnil
));
7401 Fcons (make_float (delay
/ 100.0),
7405 if (gif
->ImageCount
> 1)
7406 img
->lisp_data
= Fcons (Qcount
,
7407 Fcons (make_number (gif
->ImageCount
),
7410 fn_DGifCloseFile (gif
);
7412 /* Maybe fill in the background field while we have ximg handy. */
7413 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7414 /* Casting avoids a GCC warning. */
7415 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7417 /* Put the image into the pixmap, then free the X image and its buffer. */
7418 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7419 x_destroy_x_image (ximg
);
7424 #else /* !HAVE_GIF */
7428 gif_load (struct frame
*f
, struct image
*img
)
7430 return ns_load_image (f
, img
,
7431 image_spec_value (img
->spec
, QCfile
, NULL
),
7432 image_spec_value (img
->spec
, QCdata
, NULL
));
7434 #endif /* HAVE_NS */
7436 #endif /* HAVE_GIF */
7439 /***********************************************************************
7441 ***********************************************************************/
7442 #if defined (HAVE_IMAGEMAGICK)
7444 static Lisp_Object Qimagemagick
;
7446 static int imagemagick_image_p (Lisp_Object
);
7447 static int imagemagick_load (struct frame
*, struct image
*);
7448 static void imagemagick_clear_image (struct frame
*, struct image
*);
7450 /* Indices of image specification fields in imagemagick_format. */
7452 enum imagemagick_keyword_index
7460 IMAGEMAGICK_ALGORITHM
,
7461 IMAGEMAGICK_HEURISTIC_MASK
,
7463 IMAGEMAGICK_BACKGROUND
,
7466 IMAGEMAGICK_ROTATION
,
7471 /* Vector of image_keyword structures describing the format
7472 of valid user-defined image specifications. */
7474 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7476 {":type", IMAGE_SYMBOL_VALUE
, 1},
7477 {":data", IMAGE_STRING_VALUE
, 0},
7478 {":file", IMAGE_STRING_VALUE
, 0},
7479 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7480 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7481 {":relief", IMAGE_INTEGER_VALUE
, 0},
7482 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7483 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7484 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7485 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7486 {":height", IMAGE_INTEGER_VALUE
, 0},
7487 {":width", IMAGE_INTEGER_VALUE
, 0},
7488 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7489 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7492 /* Structure describing the image type for any image handled via
7495 static struct image_type imagemagick_type
=
7498 imagemagick_image_p
,
7500 imagemagick_clear_image
,
7504 /* Free X resources of imagemagick image IMG which is used on frame F. */
7507 imagemagick_clear_image (struct frame
*f
,
7510 x_clear_image (f
, img
);
7513 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7514 this by calling parse_image_spec and supplying the keywords that
7515 identify the IMAGEMAGICK format. */
7518 imagemagick_image_p (Lisp_Object object
)
7520 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7521 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7523 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7526 /* Must specify either the :data or :file keyword. */
7527 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7530 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7531 Therefore rename the function so it doesn't collide with ImageMagick. */
7532 #define DrawRectangle DrawRectangleGif
7533 #include <wand/MagickWand.h>
7535 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7536 Emacs seems to work fine with the hidden version, so unhide it. */
7537 #include <magick/version.h>
7538 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7539 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7540 MagickPixelPacket
*);
7543 /* Log ImageMagick error message.
7544 Useful when a ImageMagick function returns the status `MagickFalse'. */
7547 imagemagick_error (MagickWand
*wand
)
7550 ExceptionType severity
;
7552 description
= MagickGetException (wand
, &severity
);
7553 image_error ("ImageMagick error: %s",
7554 build_string (description
),
7556 description
= (char *) MagickRelinquishMemory (description
);
7559 /* Helper function for imagemagick_load, which does the actual loading
7560 given contents and size, apart from frame and image structures,
7561 passed from imagemagick_load. Uses librimagemagick to do most of
7562 the image processing.
7564 F is a pointer to the Emacs frame; IMG to the image structure to
7565 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7566 be parsed; SIZE is the number of bytes of data; and FILENAME is
7567 either the file name or the image data.
7569 Return non-zero if successful. */
7572 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7573 unsigned char *contents
, unsigned int size
,
7576 size_t width
, height
;
7577 MagickBooleanType status
;
7580 MagickWand
*image_wand
;
7581 MagickWand
*ping_wand
;
7582 PixelIterator
*iterator
;
7583 PixelWand
**pixels
, *bg_wand
= NULL
;
7584 MagickPixelPacket pixel
;
7589 int desired_width
, desired_height
;
7593 /* Handle image index for image types who can contain more than one image.
7594 Interface :index is same as for GIF. First we "ping" the image to see how
7595 many sub-images it contains. Pinging is faster than loading the image to
7596 find out things about it. */
7598 /* Initialize the imagemagick environment. */
7599 MagickWandGenesis ();
7600 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7601 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7602 ping_wand
= NewMagickWand ();
7603 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7606 ? MagickPingImage (ping_wand
, filename
)
7607 : MagickPingImageBlob (ping_wand
, contents
, size
);
7609 if (status
== MagickFalse
)
7611 imagemagick_error (ping_wand
);
7612 DestroyMagickWand (ping_wand
);
7616 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7618 image_error ("Invalid image number `%s' in image `%s'",
7620 DestroyMagickWand (ping_wand
);
7624 if (MagickGetNumberImages (ping_wand
) > 1)
7627 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7630 DestroyMagickWand (ping_wand
);
7632 /* Now we know how many images are inside the file. If it's not a
7633 bundle, the number is one. Load the image data. */
7635 image_wand
= NewMagickWand ();
7638 ? MagickReadImage (image_wand
, filename
)
7639 : MagickReadImageBlob (image_wand
, contents
, size
))
7642 imagemagick_error (image_wand
);
7643 goto imagemagick_error
;
7646 /* Retrieve the frame's background color, for use later. */
7649 Lisp_Object specified_bg
;
7651 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7652 if (!STRINGP (specified_bg
)
7653 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7656 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7657 x_query_color (f
, &bgcolor
);
7659 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7663 bg_wand
= NewPixelWand ();
7664 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7665 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7666 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7669 /* If width and/or height is set in the display spec assume we want
7670 to scale to those values. If either h or w is unspecified, the
7671 unspecified should be calculated from the specified to preserve
7673 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7674 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7675 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7676 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7678 height
= MagickGetImageHeight (image_wand
);
7679 width
= MagickGetImageWidth (image_wand
);
7681 if (desired_width
!= -1 && desired_height
== -1)
7682 /* w known, calculate h. */
7683 desired_height
= (double) desired_width
/ width
* height
;
7684 if (desired_width
== -1 && desired_height
!= -1)
7685 /* h known, calculate w. */
7686 desired_width
= (double) desired_height
/ height
* width
;
7687 if (desired_width
!= -1 && desired_height
!= -1)
7689 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7690 if (status
== MagickFalse
)
7692 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7693 imagemagick_error (image_wand
);
7694 goto imagemagick_error
;
7698 /* crop behaves similar to image slicing in Emacs but is more memory
7700 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7702 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7704 /* After some testing, it seems MagickCropImage is the fastest crop
7705 function in ImageMagick. This crop function seems to do less copying
7706 than the alternatives, but it still reads the entire image into memory
7707 before cropping, which is apparently difficult to avoid when using
7709 size_t crop_width
= XINT (XCAR (crop
));
7711 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7713 size_t crop_height
= XINT (XCAR (crop
));
7715 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7717 ssize_t crop_x
= XINT (XCAR (crop
));
7719 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7721 ssize_t crop_y
= XINT (XCAR (crop
));
7722 MagickCropImage (image_wand
, crop_width
, crop_height
,
7729 /* Furthermore :rotation. we need background color and angle for
7732 TODO background handling for rotation specified_bg =
7733 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7735 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7738 rotation
= extract_float (value
);
7739 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
7740 if (status
== MagickFalse
)
7742 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7743 imagemagick_error (image_wand
);
7744 goto imagemagick_error
;
7748 /* Finally we are done manipulating the image. Figure out the
7749 resulting width/height and transfer ownership to Emacs. */
7750 height
= MagickGetImageHeight (image_wand
);
7751 width
= MagickGetImageWidth (image_wand
);
7753 /* Set the canvas background color to the frame or specified
7754 background, and flatten the image. Note: as of ImageMagick
7755 6.6.0, SVG image transparency is not handled properly
7756 (e.g. etc/images/splash.svg shows a white background always). */
7758 MagickWand
*new_wand
;
7759 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
7760 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
7761 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
7763 new_wand
= MagickFlattenImages (image_wand
);
7765 DestroyMagickWand (image_wand
);
7766 image_wand
= new_wand
;
7769 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7770 && check_image_size (f
, width
, height
)))
7772 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7773 goto imagemagick_error
;
7776 /* We can now get a valid pixel buffer from the imagemagick file, if all
7779 init_color_table ();
7781 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7782 if (imagemagick_render_type
!= 0)
7784 /* Magicexportimage is normally faster than pixelpushing. This
7785 method is also well tested. Some aspects of this method are
7786 ad-hoc and needs to be more researched. */
7787 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
7788 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
7789 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7790 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7791 &ximg
, &img
->pixmap
))
7793 #ifdef COLOR_TABLE_SUPPORT
7794 free_color_table ();
7796 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7797 goto imagemagick_error
;
7800 /* Oddly, the below code doesn't seem to work:*/
7801 /* switch(ximg->bitmap_unit){ */
7803 /* pixelwidth=CharPixel; */
7806 /* pixelwidth=ShortPixel; */
7809 /* pixelwidth=LongPixel; */
7813 Here im just guessing the format of the bitmap.
7814 happens to work fine for:
7817 seems about 3 times as fast as pixel pushing(not carefully measured)
7819 pixelwidth
= CharPixel
; /*??? TODO figure out*/
7820 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
7821 exportdepth
, pixelwidth
, ximg
->data
);
7824 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
7826 size_t image_height
;
7828 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7829 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7830 &ximg
, &img
->pixmap
))
7832 #ifdef COLOR_TABLE_SUPPORT
7833 free_color_table ();
7835 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7836 goto imagemagick_error
;
7839 /* Copy imagemagick image to x with primitive yet robust pixel
7840 pusher loop. This has been tested a lot with many different
7843 /* Copy pixels from the imagemagick image structure to the x image map. */
7844 iterator
= NewPixelIterator (image_wand
);
7845 if (iterator
== (PixelIterator
*) NULL
)
7847 #ifdef COLOR_TABLE_SUPPORT
7848 free_color_table ();
7850 x_destroy_x_image (ximg
);
7851 image_error ("Imagemagick pixel iterator creation failed",
7853 goto imagemagick_error
;
7856 image_height
= MagickGetImageHeight (image_wand
);
7857 for (y
= 0; y
< image_height
; y
++)
7859 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7860 if (pixels
== (PixelWand
**) NULL
)
7862 for (x
= 0; x
< (long) width
; x
++)
7864 PixelGetMagickColor (pixels
[x
], &pixel
);
7865 XPutPixel (ximg
, x
, y
,
7866 lookup_rgb_color (f
,
7872 DestroyPixelIterator (iterator
);
7875 #ifdef COLOR_TABLE_SUPPORT
7876 /* Remember colors allocated for this image. */
7877 img
->colors
= colors_in_color_table (&img
->ncolors
);
7878 free_color_table ();
7879 #endif /* COLOR_TABLE_SUPPORT */
7882 img
->height
= height
;
7884 /* Put the image into the pixmap, then free the X image and its
7886 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7887 x_destroy_x_image (ximg
);
7889 /* Final cleanup. image_wand should be the only resource left. */
7890 DestroyMagickWand (image_wand
);
7891 if (bg_wand
) DestroyPixelWand (bg_wand
);
7893 /* `MagickWandTerminus' terminates the imagemagick environment. */
7894 MagickWandTerminus ();
7899 DestroyMagickWand (image_wand
);
7900 if (bg_wand
) DestroyPixelWand (bg_wand
);
7902 MagickWandTerminus ();
7903 /* TODO more cleanup. */
7904 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7909 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7910 successful. this function will go into the imagemagick_type structure, and
7911 the prototype thus needs to be compatible with that structure. */
7914 imagemagick_load (struct frame
*f
, struct image
*img
)
7917 Lisp_Object file_name
;
7919 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7920 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7921 if (STRINGP (file_name
))
7925 file
= x_find_image_file (file_name
);
7926 if (!STRINGP (file
))
7928 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7931 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
7933 /* Else its not a file, its a lisp object. Load the image from a
7934 lisp object rather than a file. */
7939 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7940 if (!STRINGP (data
))
7942 image_error ("Invalid image data `%s'", data
, Qnil
);
7945 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7946 SBYTES (data
), NULL
);
7952 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7953 doc
: /* Return a list of image types supported by ImageMagick.
7954 Each entry in this list is a symbol named after an ImageMagick format
7955 tag. See the ImageMagick manual for a list of ImageMagick formats and
7956 their descriptions (http://www.imagemagick.org/script/formats.php).
7957 You can also try the shell command: `identify -list format'.
7959 Note that ImageMagick recognizes many file-types that Emacs does not
7960 recognize as images, such as C. See `imagemagick-types-enable'
7961 and `imagemagick-types-inhibit'. */)
7964 Lisp_Object typelist
= Qnil
;
7969 Lisp_Object Qimagemagicktype
;
7971 GetExceptionInfo(&ex
);
7972 imtypes
= GetMagickList ("*", &numf
, &ex
);
7973 DestroyExceptionInfo(&ex
);
7975 for (i
= 0; i
< numf
; i
++)
7977 Qimagemagicktype
= intern (imtypes
[i
]);
7978 typelist
= Fcons (Qimagemagicktype
, typelist
);
7980 return Fnreverse (typelist
);
7983 #endif /* defined (HAVE_IMAGEMAGICK) */
7987 /***********************************************************************
7989 ***********************************************************************/
7991 #if defined (HAVE_RSVG)
7993 /* Function prototypes. */
7995 static int svg_image_p (Lisp_Object object
);
7996 static int svg_load (struct frame
*f
, struct image
*img
);
7998 static int svg_load_image (struct frame
*, struct image
*,
7999 unsigned char *, ptrdiff_t);
8001 /* The symbol `svg' identifying images of this type. */
8003 static Lisp_Object Qsvg
;
8005 /* Indices of image specification fields in svg_format, below. */
8007 enum svg_keyword_index
8022 /* Vector of image_keyword structures describing the format
8023 of valid user-defined image specifications. */
8025 static const struct image_keyword svg_format
[SVG_LAST
] =
8027 {":type", IMAGE_SYMBOL_VALUE
, 1},
8028 {":data", IMAGE_STRING_VALUE
, 0},
8029 {":file", IMAGE_STRING_VALUE
, 0},
8030 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8031 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8032 {":relief", IMAGE_INTEGER_VALUE
, 0},
8033 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8034 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8035 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8036 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8039 /* Structure describing the image type `svg'. Its the same type of
8040 structure defined for all image formats, handled by emacs image
8041 functions. See struct image_type in dispextern.h. */
8043 static struct image_type svg_type
=
8045 /* An identifier showing that this is an image structure for the SVG format. */
8047 /* Handle to a function that can be used to identify a SVG file. */
8049 /* Handle to function used to load a SVG file. */
8051 /* Handle to function to free sresources for SVG. */
8053 /* An internal field to link to the next image type in a list of
8054 image types, will be filled in when registering the format. */
8059 /* Return non-zero if OBJECT is a valid SVG image specification. Do
8060 this by calling parse_image_spec and supplying the keywords that
8061 identify the SVG format. */
8064 svg_image_p (Lisp_Object object
)
8066 struct image_keyword fmt
[SVG_LAST
];
8067 memcpy (fmt
, svg_format
, sizeof fmt
);
8069 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8072 /* Must specify either the :data or :file keyword. */
8073 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8076 #include <librsvg/rsvg.h>
8080 /* SVG library functions. */
8081 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8082 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8083 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8084 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8085 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8087 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8088 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8089 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8090 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8091 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8092 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8093 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8094 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8096 DEF_IMGLIB_FN (void, g_type_init
);
8097 DEF_IMGLIB_FN (void, g_object_unref
);
8098 DEF_IMGLIB_FN (void, g_error_free
);
8100 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8103 init_svg_functions (Lisp_Object libraries
)
8105 HMODULE library
, gdklib
, glib
, gobject
;
8107 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8108 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8109 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8110 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8113 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8114 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8115 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8116 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8117 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8119 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8120 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8121 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8122 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8123 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8124 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8125 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8126 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8128 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8129 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8130 LOAD_IMGLIB_FN (glib
, g_error_free
);
8136 /* The following aliases for library functions allow dynamic loading
8137 to be used on some platforms. */
8138 #define fn_rsvg_handle_new rsvg_handle_new
8139 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8140 #define fn_rsvg_handle_write rsvg_handle_write
8141 #define fn_rsvg_handle_close rsvg_handle_close
8142 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8144 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8145 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8146 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8147 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8148 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8149 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8150 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8151 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8153 #define fn_g_type_init g_type_init
8154 #define fn_g_object_unref g_object_unref
8155 #define fn_g_error_free g_error_free
8156 #endif /* !HAVE_NTGUI */
8158 /* Load SVG image IMG for use on frame F. Value is non-zero if
8159 successful. this function will go into the svg_type structure, and
8160 the prototype thus needs to be compatible with that structure. */
8163 svg_load (struct frame
*f
, struct image
*img
)
8166 Lisp_Object file_name
;
8168 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8169 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8170 if (STRINGP (file_name
))
8173 unsigned char *contents
;
8176 file
= x_find_image_file (file_name
);
8177 if (!STRINGP (file
))
8179 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8183 /* Read the entire file into memory. */
8184 contents
= slurp_file (SSDATA (file
), &size
);
8185 if (contents
== NULL
)
8187 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8190 /* If the file was slurped into memory properly, parse it. */
8191 success_p
= svg_load_image (f
, img
, contents
, size
);
8194 /* Else its not a file, its a lisp object. Load the image from a
8195 lisp object rather than a file. */
8200 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8201 if (!STRINGP (data
))
8203 image_error ("Invalid image data `%s'", data
, Qnil
);
8206 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8212 /* svg_load_image is a helper function for svg_load, which does the
8213 actual loading given contents and size, apart from frame and image
8214 structures, passed from svg_load.
8216 Uses librsvg to do most of the image processing.
8218 Returns non-zero when successful. */
8220 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8221 struct image
*img
, /* Pointer to emacs image structure. */
8222 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8223 ptrdiff_t size
) /* Size of data in bytes. */
8225 RsvgHandle
*rsvg_handle
;
8226 RsvgDimensionData dimension_data
;
8231 const guint8
*pixels
;
8234 Lisp_Object specified_bg
;
8239 /* g_type_init is a glib function that must be called prior to using
8240 gnome type library functions. */
8242 /* Make a handle to a new rsvg object. */
8243 rsvg_handle
= fn_rsvg_handle_new ();
8245 /* Parse the contents argument and fill in the rsvg_handle. */
8246 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8247 if (err
) goto rsvg_error
;
8249 /* The parsing is complete, rsvg_handle is ready to used, close it
8250 for further writes. */
8251 fn_rsvg_handle_close (rsvg_handle
, &err
);
8252 if (err
) goto rsvg_error
;
8254 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8255 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8257 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8261 /* We can now get a valid pixel buffer from the svg file, if all
8263 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8264 if (!pixbuf
) goto rsvg_error
;
8265 fn_g_object_unref (rsvg_handle
);
8267 /* Extract some meta data from the svg handle. */
8268 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8269 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8270 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8271 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8273 /* Validate the svg meta data. */
8274 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8275 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8276 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8277 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8279 /* Try to create a x pixmap to hold the svg pixmap. */
8280 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8282 fn_g_object_unref (pixbuf
);
8286 init_color_table ();
8288 /* Handle alpha channel by combining the image with a background
8290 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8291 if (!STRINGP (specified_bg
)
8292 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8295 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8296 x_query_color (f
, &background
);
8298 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8302 /* SVG pixmaps specify transparency in the last byte, so right
8303 shift 8 bits to get rid of it, since emacs doesn't support
8305 background
.red
>>= 8;
8306 background
.green
>>= 8;
8307 background
.blue
>>= 8;
8309 /* This loop handles opacity values, since Emacs assumes
8310 non-transparent images. Each pixel must be "flattened" by
8311 calculating the resulting color, given the transparency of the
8312 pixel, and the image background color. */
8313 for (y
= 0; y
< height
; ++y
)
8315 for (x
= 0; x
< width
; ++x
)
8325 opacity
= *pixels
++;
8327 red
= ((red
* opacity
)
8328 + (background
.red
* ((1 << 8) - opacity
)));
8329 green
= ((green
* opacity
)
8330 + (background
.green
* ((1 << 8) - opacity
)));
8331 blue
= ((blue
* opacity
)
8332 + (background
.blue
* ((1 << 8) - opacity
)));
8334 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8337 pixels
+= rowstride
- 4 * width
;
8340 #ifdef COLOR_TABLE_SUPPORT
8341 /* Remember colors allocated for this image. */
8342 img
->colors
= colors_in_color_table (&img
->ncolors
);
8343 free_color_table ();
8344 #endif /* COLOR_TABLE_SUPPORT */
8346 fn_g_object_unref (pixbuf
);
8349 img
->height
= height
;
8351 /* Maybe fill in the background field while we have ximg handy.
8352 Casting avoids a GCC warning. */
8353 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8355 /* Put the image into the pixmap, then free the X image and its
8357 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8358 x_destroy_x_image (ximg
);
8363 fn_g_object_unref (rsvg_handle
);
8364 /* FIXME: Use error->message so the user knows what is the actual
8365 problem with the image. */
8366 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8367 fn_g_error_free (err
);
8371 #endif /* defined (HAVE_RSVG) */
8376 /***********************************************************************
8378 ***********************************************************************/
8380 #ifdef HAVE_X_WINDOWS
8381 #define HAVE_GHOSTSCRIPT 1
8382 #endif /* HAVE_X_WINDOWS */
8384 #ifdef HAVE_GHOSTSCRIPT
8386 static int gs_image_p (Lisp_Object object
);
8387 static int gs_load (struct frame
*f
, struct image
*img
);
8388 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8390 /* Keyword symbols. */
8392 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8394 /* Indices of image specification fields in gs_format, below. */
8396 enum gs_keyword_index
8414 /* Vector of image_keyword structures describing the format
8415 of valid user-defined image specifications. */
8417 static const struct image_keyword gs_format
[GS_LAST
] =
8419 {":type", IMAGE_SYMBOL_VALUE
, 1},
8420 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8421 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8422 {":file", IMAGE_STRING_VALUE
, 1},
8423 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8424 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8425 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8426 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8427 {":relief", IMAGE_INTEGER_VALUE
, 0},
8428 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8429 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8430 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8431 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8434 /* Structure describing the image type `ghostscript'. */
8436 static struct image_type gs_type
=
8446 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8449 gs_clear_image (struct frame
*f
, struct image
*img
)
8451 x_clear_image (f
, img
);
8455 /* Return non-zero if OBJECT is a valid Ghostscript image
8459 gs_image_p (Lisp_Object object
)
8461 struct image_keyword fmt
[GS_LAST
];
8465 memcpy (fmt
, gs_format
, sizeof fmt
);
8467 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8470 /* Bounding box must be a list or vector containing 4 integers. */
8471 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8474 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8475 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8480 else if (VECTORP (tem
))
8482 if (ASIZE (tem
) != 4)
8484 for (i
= 0; i
< 4; ++i
)
8485 if (!INTEGERP (AREF (tem
, i
)))
8495 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8499 gs_load (struct frame
*f
, struct image
*img
)
8501 uprintmax_t printnum1
, printnum2
;
8502 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8503 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8505 double in_width
, in_height
;
8506 Lisp_Object pixel_colors
= Qnil
;
8508 /* Compute pixel size of pixmap needed from the given size in the
8509 image specification. Sizes in the specification are in pt. 1 pt
8510 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8512 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8513 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8514 in_width
*= FRAME_X_DISPLAY_INFO (f
)->resx
;
8515 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8516 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8517 in_height
*= FRAME_X_DISPLAY_INFO (f
)->resy
;
8519 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8520 && check_image_size (f
, in_width
, in_height
)))
8522 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8525 img
->width
= in_width
;
8526 img
->height
= in_height
;
8528 /* Create the pixmap. */
8529 eassert (img
->pixmap
== NO_PIXMAP
);
8531 if (x_check_image_size (0, img
->width
, img
->height
))
8533 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8535 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8536 img
->width
, img
->height
,
8537 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8543 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8547 /* Call the loader to fill the pixmap. It returns a process object
8548 if successful. We do not record_unwind_protect here because
8549 other places in redisplay like calling window scroll functions
8550 don't either. Let the Lisp loader use `unwind-protect' instead. */
8551 printnum1
= FRAME_X_WINDOW (f
);
8552 printnum2
= img
->pixmap
;
8553 window_and_pixmap_id
8554 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8556 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8557 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8559 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8561 XSETFRAME (frame
, f
);
8562 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8564 loader
= intern ("gs-load-image");
8566 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8567 make_number (img
->width
),
8568 make_number (img
->height
),
8569 window_and_pixmap_id
,
8571 return PROCESSP (img
->lisp_data
);
8575 /* Kill the Ghostscript process that was started to fill PIXMAP on
8576 frame F. Called from XTread_socket when receiving an event
8577 telling Emacs that Ghostscript has finished drawing. */
8580 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8582 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8587 /* Find the image containing PIXMAP. */
8588 for (i
= 0; i
< c
->used
; ++i
)
8589 if (c
->images
[i
]->pixmap
== pixmap
)
8592 /* Should someone in between have cleared the image cache, for
8593 instance, give up. */
8597 /* Kill the GS process. We should have found PIXMAP in the image
8598 cache and its image should contain a process object. */
8600 eassert (PROCESSP (img
->lisp_data
));
8601 Fkill_process (img
->lisp_data
, Qnil
);
8602 img
->lisp_data
= Qnil
;
8604 #if defined (HAVE_X_WINDOWS)
8606 /* On displays with a mutable colormap, figure out the colors
8607 allocated for the image by looking at the pixels of an XImage for
8609 class = FRAME_X_VISUAL (f
)->class;
8610 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8616 /* Try to get an XImage for img->pixmep. */
8617 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8618 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8623 /* Initialize the color table. */
8624 init_color_table ();
8626 /* For each pixel of the image, look its color up in the
8627 color table. After having done so, the color table will
8628 contain an entry for each color used by the image. */
8629 for (y
= 0; y
< img
->height
; ++y
)
8630 for (x
= 0; x
< img
->width
; ++x
)
8632 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8633 lookup_pixel_color (f
, pixel
);
8636 /* Record colors in the image. Free color table and XImage. */
8637 #ifdef COLOR_TABLE_SUPPORT
8638 img
->colors
= colors_in_color_table (&img
->ncolors
);
8639 free_color_table ();
8641 XDestroyImage (ximg
);
8643 #if 0 /* This doesn't seem to be the case. If we free the colors
8644 here, we get a BadAccess later in x_clear_image when
8645 freeing the colors. */
8646 /* We have allocated colors once, but Ghostscript has also
8647 allocated colors on behalf of us. So, to get the
8648 reference counts right, free them once. */
8650 x_free_colors (f
, img
->colors
, img
->ncolors
);
8654 image_error ("Cannot get X image of `%s'; colors will not be freed",
8659 #endif /* HAVE_X_WINDOWS */
8661 /* Now that we have the pixmap, compute mask and transform the
8662 image if requested. */
8664 postprocess_image (f
, img
);
8668 #endif /* HAVE_GHOSTSCRIPT */
8671 /***********************************************************************
8673 ***********************************************************************/
8677 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8678 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8681 return valid_image_p (spec
) ? Qt
: Qnil
;
8685 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8690 if (valid_image_p (spec
))
8691 id
= lookup_image (SELECTED_FRAME (), spec
);
8694 return make_number (id
);
8697 #endif /* GLYPH_DEBUG */
8700 /***********************************************************************
8702 ***********************************************************************/
8705 /* Image types that rely on external libraries are loaded dynamically
8706 if the library is available. */
8707 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8708 define_image_type (image_type, init_lib_fn (libraries))
8710 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8711 define_image_type (image_type, 1)
8712 #endif /* HAVE_NTGUI */
8714 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8715 doc
: /* Initialize image library implementing image type TYPE.
8716 Return non-nil if TYPE is a supported image type.
8718 Image types pbm and xbm are prebuilt; other types are loaded here.
8719 Libraries to load are specified in alist LIBRARIES (usually, the value
8720 of `dynamic-library-alist', which see). */)
8721 (Lisp_Object type
, Lisp_Object libraries
)
8724 /* Don't try to reload the library. */
8725 Lisp_Object tested
= Fassq (type
, Vlibrary_cache
);
8727 return XCDR (tested
);
8730 /* Types pbm and xbm are built-in and always available. */
8731 if (EQ (type
, Qpbm
) || EQ (type
, Qxbm
))
8734 #if defined (HAVE_XPM) || defined (HAVE_NS)
8735 if (EQ (type
, Qxpm
))
8736 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8739 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8740 if (EQ (type
, Qjpeg
))
8741 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8744 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8745 if (EQ (type
, Qtiff
))
8746 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8749 #if defined (HAVE_GIF) || defined (HAVE_NS)
8750 if (EQ (type
, Qgif
))
8751 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8754 #if defined (HAVE_PNG) || defined (HAVE_NS)
8755 if (EQ (type
, Qpng
))
8756 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8759 #if defined (HAVE_RSVG)
8760 if (EQ (type
, Qsvg
))
8761 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8764 #if defined (HAVE_IMAGEMAGICK)
8765 if (EQ (type
, Qimagemagick
))
8766 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8770 #ifdef HAVE_GHOSTSCRIPT
8771 if (EQ (type
, Qpostscript
))
8772 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8775 /* If the type is not recognized, avoid testing it ever again. */
8776 CACHE_IMAGE_TYPE (type
, Qnil
);
8781 syms_of_image (void)
8783 /* Initialize this only once, since that's what we do with Vimage_types
8784 and they are supposed to be in sync. Initializing here gives correct
8785 operation on GNU/Linux of calling dump-emacs after loading some images. */
8788 /* Must be defined now because we're going to update it below, while
8789 defining the supported image types. */
8790 DEFVAR_LISP ("image-types", Vimage_types
,
8791 doc
: /* List of potentially supported image types.
8792 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8793 To check whether it is really supported, use `image-type-available-p'. */);
8794 Vimage_types
= Qnil
;
8796 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8797 doc
: /* Maximum size of images.
8798 Emacs will not load an image into memory if its pixel width or
8799 pixel height exceeds this limit.
8801 If the value is an integer, it directly specifies the maximum
8802 image height and width, measured in pixels. If it is a floating
8803 point number, it specifies the maximum image height and width
8804 as a ratio to the frame height and width. If the value is
8805 non-numeric, there is no explicit limit on the size of images. */);
8806 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8808 DEFSYM (Qpbm
, "pbm");
8809 ADD_IMAGE_TYPE (Qpbm
);
8811 DEFSYM (Qxbm
, "xbm");
8812 ADD_IMAGE_TYPE (Qxbm
);
8814 define_image_type (&xbm_type
, 1);
8815 define_image_type (&pbm_type
, 1);
8817 DEFSYM (Qcount
, "count");
8818 DEFSYM (Qextension_data
, "extension-data");
8819 DEFSYM (Qdelay
, "delay");
8821 DEFSYM (QCascent
, ":ascent");
8822 DEFSYM (QCmargin
, ":margin");
8823 DEFSYM (QCrelief
, ":relief");
8824 DEFSYM (QCconversion
, ":conversion");
8825 DEFSYM (QCcolor_symbols
, ":color-symbols");
8826 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8827 DEFSYM (QCindex
, ":index");
8828 DEFSYM (QCgeometry
, ":geometry");
8829 DEFSYM (QCcrop
, ":crop");
8830 DEFSYM (QCrotation
, ":rotation");
8831 DEFSYM (QCmatrix
, ":matrix");
8832 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8833 DEFSYM (QCmask
, ":mask");
8835 DEFSYM (Qlaplace
, "laplace");
8836 DEFSYM (Qemboss
, "emboss");
8837 DEFSYM (Qedge_detection
, "edge-detection");
8838 DEFSYM (Qheuristic
, "heuristic");
8840 DEFSYM (Qpostscript
, "postscript");
8841 #ifdef HAVE_GHOSTSCRIPT
8842 ADD_IMAGE_TYPE (Qpostscript
);
8843 DEFSYM (QCloader
, ":loader");
8844 DEFSYM (QCbounding_box
, ":bounding-box");
8845 DEFSYM (QCpt_width
, ":pt-width");
8846 DEFSYM (QCpt_height
, ":pt-height");
8847 #endif /* HAVE_GHOSTSCRIPT */
8850 DEFSYM (Qlibpng_version
, "libpng-version");
8851 Fset (Qlibpng_version
,
8853 make_number (PNG_LIBPNG_VER
)
8860 #if defined (HAVE_XPM) || defined (HAVE_NS)
8861 DEFSYM (Qxpm
, "xpm");
8862 ADD_IMAGE_TYPE (Qxpm
);
8865 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8866 DEFSYM (Qjpeg
, "jpeg");
8867 ADD_IMAGE_TYPE (Qjpeg
);
8870 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8871 DEFSYM (Qtiff
, "tiff");
8872 ADD_IMAGE_TYPE (Qtiff
);
8875 #if defined (HAVE_GIF) || defined (HAVE_NS)
8876 DEFSYM (Qgif
, "gif");
8877 ADD_IMAGE_TYPE (Qgif
);
8880 #if defined (HAVE_PNG) || defined (HAVE_NS)
8881 DEFSYM (Qpng
, "png");
8882 ADD_IMAGE_TYPE (Qpng
);
8885 #if defined (HAVE_IMAGEMAGICK)
8886 DEFSYM (Qimagemagick
, "imagemagick");
8887 ADD_IMAGE_TYPE (Qimagemagick
);
8890 #if defined (HAVE_RSVG)
8891 DEFSYM (Qsvg
, "svg");
8892 ADD_IMAGE_TYPE (Qsvg
);
8894 /* Other libraries used directly by svg code. */
8895 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8896 DEFSYM (Qglib
, "glib");
8897 DEFSYM (Qgobject
, "gobject");
8898 #endif /* HAVE_NTGUI */
8899 #endif /* HAVE_RSVG */
8901 defsubr (&Sinit_image_library
);
8902 #ifdef HAVE_IMAGEMAGICK
8903 defsubr (&Simagemagick_types
);
8905 defsubr (&Sclear_image_cache
);
8906 defsubr (&Simage_flush
);
8907 defsubr (&Simage_size
);
8908 defsubr (&Simage_mask_p
);
8909 defsubr (&Simage_metadata
);
8913 defsubr (&Slookup_image
);
8916 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8917 doc
: /* Non-nil means always draw a cross over disabled images.
8918 Disabled images are those having a `:conversion disabled' property.
8919 A cross is always drawn on black & white displays. */);
8920 cross_disabled_images
= 0;
8922 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8923 doc
: /* List of directories to search for window system bitmap files. */);
8924 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8926 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8927 doc
: /* Maximum time after which images are removed from the cache.
8928 When an image has not been displayed this many seconds, Emacs
8929 automatically removes it from the image cache. If the cache contains
8930 a large number of images, the actual eviction time may be shorter.
8931 The value can also be nil, meaning the cache is never cleared.
8933 The function `clear-image-cache' disregards this variable. */);
8934 Vimage_cache_eviction_delay
= make_number (300);
8935 #ifdef HAVE_IMAGEMAGICK
8936 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
8937 doc
: /* Integer indicating which ImageMagick rendering method to use.
8939 0 -- the default method (pixel pushing)
8940 1 -- a newer method ("MagickExportImagePixels") that may perform
8941 better (speed etc) in some cases, but has not been as thoroughly
8942 tested with Emacs as the default method. This method requires
8943 ImageMagick version 6.4.6 (approximately) or later.
8945 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
8946 imagemagick_render_type
= 0;