1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
31 #if defined HAVE_LIBPNG_PNG_H
32 # include <libpng/png.h>
40 /* This makes the fields of a Display accessible, in Xlib header files. */
42 #define XLIB_ILLEGAL_ACCESS
47 #include "dispextern.h"
48 #include "blockinput.h"
51 #include "character.h"
53 #include "termhooks.h"
58 #include <sys/types.h>
61 #define COLOR_TABLE_SUPPORT 1
63 typedef struct x_bitmap_record Bitmap_Record
;
64 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
65 #define NO_PIXMAP None
67 #define RGB_PIXEL_COLOR unsigned long
69 #define PIX_MASK_RETAIN 0
70 #define PIX_MASK_DRAW 1
71 #endif /* HAVE_X_WINDOWS */
77 /* W32_TODO : Color tables on W32. */
78 #undef COLOR_TABLE_SUPPORT
80 typedef struct w32_bitmap_record Bitmap_Record
;
81 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
84 #define RGB_PIXEL_COLOR COLORREF
86 #define PIX_MASK_RETAIN 0
87 #define PIX_MASK_DRAW 1
89 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
90 #define x_defined_color w32_defined_color
91 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
93 /* Functions from w32term.c that depend on XColor (so can't go in w32term.h
94 without modifying lots of files). */
95 extern void x_query_colors (struct frame
*f
, XColor
*colors
, int ncolors
);
96 extern void x_query_color (struct frame
*f
, XColor
*color
);
97 #endif /* HAVE_NTGUI */
101 #include <sys/types.h>
102 #include <sys/stat.h>
104 #undef COLOR_TABLE_SUPPORT
106 typedef struct ns_bitmap_record Bitmap_Record
;
108 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
111 #define RGB_PIXEL_COLOR unsigned long
114 #define PIX_MASK_RETAIN 0
115 #define PIX_MASK_DRAW 1
117 #define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual
118 #define x_defined_color(f, name, color_def, alloc) \
119 ns_defined_color (f, name, color_def, alloc, 0)
120 #define FRAME_X_SCREEN(f) 0
121 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
125 /* Search path for bitmap files. */
127 Lisp_Object Vx_bitmap_file_path
;
130 static void x_disable_image (struct frame
*, struct image
*);
131 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
134 static void init_color_table (void);
135 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
136 #ifdef COLOR_TABLE_SUPPORT
137 static void free_color_table (void);
138 static unsigned long *colors_in_color_table (int *n
);
139 static unsigned long lookup_pixel_color (struct frame
*f
, unsigned long p
);
142 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
143 id, which is just an int that this section returns. Bitmaps are
144 reference counted so they can be shared among frames.
146 Bitmap indices are guaranteed to be > 0, so a negative number can
147 be used to indicate no bitmap.
149 If you use x_create_bitmap_from_data, then you must keep track of
150 the bitmaps yourself. That is, creating a bitmap from the same
151 data more than once will not be caught. */
155 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
156 unsigned int width
, unsigned int height
,
157 unsigned long plane_mask
, int format
)
159 /* TODO: not sure what this function is supposed to do.. */
160 ns_retain_object (pixmap
);
164 /* use with imgs created by ns_image_for_XPM */
166 XGetPixel (XImagePtr ximage
, int x
, int y
)
168 return ns_get_pixel (ximage
, x
, y
);
171 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
172 pixel is assumed to be in form RGB */
174 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
176 ns_put_pixel (ximage
, x
, y
, pixel
);
181 /* Functions to access the contents of a bitmap, given an id. */
184 x_bitmap_height (FRAME_PTR f
, int id
)
186 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
190 x_bitmap_width (FRAME_PTR f
, int id
)
192 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
195 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
197 x_bitmap_pixmap (FRAME_PTR f
, int id
)
199 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
203 #ifdef HAVE_X_WINDOWS
205 x_bitmap_mask (FRAME_PTR f
, int id
)
207 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
211 /* Allocate a new bitmap record. Returns index of new record. */
214 x_allocate_bitmap_record (FRAME_PTR f
)
216 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
219 if (dpyinfo
->bitmaps
== NULL
)
221 dpyinfo
->bitmaps_size
= 10;
223 = (Bitmap_Record
*) xmalloc (dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
224 dpyinfo
->bitmaps_last
= 1;
228 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
229 return ++dpyinfo
->bitmaps_last
;
231 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
232 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
235 dpyinfo
->bitmaps_size
*= 2;
237 = (Bitmap_Record
*) xrealloc (dpyinfo
->bitmaps
,
238 dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
239 return ++dpyinfo
->bitmaps_last
;
242 /* Add one reference to the reference count of the bitmap with id ID. */
245 x_reference_bitmap (FRAME_PTR f
, int id
)
247 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
250 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
253 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
255 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
258 #ifdef HAVE_X_WINDOWS
260 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
261 bits
, width
, height
);
264 #endif /* HAVE_X_WINDOWS */
268 bitmap
= CreateBitmap (width
, height
,
269 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
270 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
274 #endif /* HAVE_NTGUI */
277 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
282 id
= x_allocate_bitmap_record (f
);
285 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
286 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
289 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
290 dpyinfo
->bitmaps
[id
- 1].height
= height
;
291 dpyinfo
->bitmaps
[id
- 1].width
= width
;
292 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
294 #ifdef HAVE_X_WINDOWS
295 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
296 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
297 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
298 #endif /* HAVE_X_WINDOWS */
301 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
302 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
303 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
304 #endif /* HAVE_NTGUI */
309 /* Create bitmap from file FILE for frame F. */
312 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
314 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
317 return -1; /* W32_TODO : bitmap support */
318 #endif /* HAVE_NTGUI */
322 void *bitmap
= ns_image_from_file (file
);
328 id
= x_allocate_bitmap_record (f
);
329 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
330 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
331 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
332 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
333 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
334 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
335 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
339 #ifdef HAVE_X_WINDOWS
340 unsigned int width
, height
;
342 int xhot
, yhot
, result
, id
;
347 /* Look for an existing bitmap with the same name. */
348 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
350 if (dpyinfo
->bitmaps
[id
].refcount
351 && dpyinfo
->bitmaps
[id
].file
352 && !strcmp (dpyinfo
->bitmaps
[id
].file
, (char *) SDATA (file
)))
354 ++dpyinfo
->bitmaps
[id
].refcount
;
359 /* Search bitmap-file-path for the file, if appropriate. */
360 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
365 filename
= (char *) SDATA (found
);
367 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
368 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
369 if (result
!= BitmapSuccess
)
372 id
= x_allocate_bitmap_record (f
);
373 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
374 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
375 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
376 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
377 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
378 dpyinfo
->bitmaps
[id
- 1].height
= height
;
379 dpyinfo
->bitmaps
[id
- 1].width
= width
;
380 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
383 #endif /* HAVE_X_WINDOWS */
389 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
391 #ifdef HAVE_X_WINDOWS
392 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
394 XFreePixmap (dpyinfo
->display
, bm
->mask
);
395 #endif /* HAVE_X_WINDOWS */
398 DeleteObject (bm
->pixmap
);
399 #endif /* HAVE_NTGUI */
402 ns_release_object (bm
->img
);
412 /* Remove reference to bitmap with id number ID. */
415 x_destroy_bitmap (FRAME_PTR f
, int id
)
417 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
421 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
423 if (--bm
->refcount
== 0)
426 free_bitmap_record (dpyinfo
, bm
);
432 /* Free all the bitmaps for the display specified by DPYINFO. */
435 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
438 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
440 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
441 if (bm
->refcount
> 0)
442 free_bitmap_record (dpyinfo
, bm
);
444 dpyinfo
->bitmaps_last
= 0;
448 #ifdef HAVE_X_WINDOWS
450 /* Useful functions defined in the section
451 `Image type independent image structures' below. */
453 static unsigned long four_corners_best (XImagePtr ximg
,
456 unsigned long height
);
458 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
459 int depth
, XImagePtr
*ximg
,
462 static void x_destroy_x_image (XImagePtr ximg
);
465 /* Create a mask of a bitmap. Note is this not a perfect mask.
466 It's nicer with some borders in this context */
469 x_create_bitmap_mask (struct frame
*f
, int id
)
472 XImagePtr ximg
, mask_img
;
473 unsigned long width
, height
;
476 unsigned long x
, y
, xp
, xm
, yp
, ym
;
479 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
484 pixmap
= x_bitmap_pixmap (f
, id
);
485 width
= x_bitmap_width (f
, id
);
486 height
= x_bitmap_height (f
, id
);
489 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
498 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
503 XDestroyImage (ximg
);
507 bg
= four_corners_best (ximg
, NULL
, width
, height
);
509 for (y
= 0; y
< ximg
->height
; ++y
)
511 for (x
= 0; x
< ximg
->width
; ++x
)
513 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
514 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
515 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
516 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
517 if (XGetPixel (ximg
, x
, y
) == bg
518 && XGetPixel (ximg
, x
, yp
) == bg
519 && XGetPixel (ximg
, x
, ym
) == bg
520 && XGetPixel (ximg
, xp
, y
) == bg
521 && XGetPixel (ximg
, xp
, yp
) == bg
522 && XGetPixel (ximg
, xp
, ym
) == bg
523 && XGetPixel (ximg
, xm
, y
) == bg
524 && XGetPixel (ximg
, xm
, yp
) == bg
525 && XGetPixel (ximg
, xm
, ym
) == bg
)
526 XPutPixel (mask_img
, x
, y
, 0);
528 XPutPixel (mask_img
, x
, y
, 1);
532 xassert (interrupt_input_blocked
);
533 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
534 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
536 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
538 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
539 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
541 XDestroyImage (ximg
);
542 x_destroy_x_image (mask_img
);
547 #endif /* HAVE_X_WINDOWS */
550 /***********************************************************************
552 ***********************************************************************/
554 /* Value is the number of elements of vector VECTOR. */
556 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
558 /* List of supported image types. Use define_image_type to add new
559 types. Use lookup_image_type to find a type for a given symbol. */
561 static struct image_type
*image_types
;
563 /* A list of symbols, one for each supported image type. */
565 Lisp_Object Vimage_types
;
567 /* An alist of image types and libraries that implement the type. */
569 Lisp_Object Vimage_library_alist
;
571 /* Cache for delayed-loading image types. */
573 static Lisp_Object Vimage_type_cache
;
575 /* The symbol `xbm' which is used as the type symbol for XBM images. */
581 extern Lisp_Object QCwidth
, QCheight
, QCforeground
, QCbackground
, QCfile
;
582 extern Lisp_Object QCdata
, QCtype
;
583 extern Lisp_Object Qcenter
;
584 Lisp_Object QCascent
, QCmargin
, QCrelief
, Qcount
, Qextension_data
;
585 Lisp_Object QCconversion
, QCcolor_symbols
, QCheuristic_mask
;
586 Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
;
590 Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
592 /* Time in seconds after which images should be removed from the cache
595 Lisp_Object Vimage_cache_eviction_delay
;
597 /* Function prototypes. */
599 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
600 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
601 static void image_error (char *format
, Lisp_Object
, Lisp_Object
);
602 static void x_laplace (struct frame
*, struct image
*);
603 static void x_emboss (struct frame
*, struct image
*);
604 static int x_build_heuristic_mask (struct frame
*, struct image
*,
607 #define CACHE_IMAGE_TYPE(type, status) \
608 do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
610 #define ADD_IMAGE_TYPE(type) \
611 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
613 /* Define a new image type from TYPE. This adds a copy of TYPE to
614 image_types and caches the loading status of TYPE. */
617 define_image_type (struct image_type
*type
, int loaded
)
625 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
626 The initialized data segment is read-only. */
627 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
628 memcpy (p
, type
, sizeof *p
);
629 p
->next
= image_types
;
634 CACHE_IMAGE_TYPE (*type
->type
, success
);
639 /* Look up image type SYMBOL, and return a pointer to its image_type
640 structure. Value is null if SYMBOL is not a known image type. */
642 static INLINE
struct image_type
*
643 lookup_image_type (Lisp_Object symbol
)
645 struct image_type
*type
;
647 /* We must initialize the image-type if it hasn't been already. */
648 if (NILP (Finit_image_library (symbol
, Vimage_library_alist
)))
649 return 0; /* unimplemented */
651 for (type
= image_types
; type
; type
= type
->next
)
652 if (EQ (symbol
, *type
->type
))
659 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
660 valid image specification is a list whose car is the symbol
661 `image', and whose rest is a property list. The property list must
662 contain a value for key `:type'. That value must be the name of a
663 supported image type. The rest of the property list depends on the
667 valid_image_p (Lisp_Object object
)
675 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
676 if (EQ (XCAR (tem
), QCtype
))
679 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
681 struct image_type
*type
;
682 type
= lookup_image_type (XCAR (tem
));
684 valid_p
= type
->valid_p (object
);
695 /* Log error message with format string FORMAT and argument ARG.
696 Signaling an error, e.g. when an image cannot be loaded, is not a
697 good idea because this would interrupt redisplay, and the error
698 message display would lead to another redisplay. This function
699 therefore simply displays a message. */
702 image_error (char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
704 add_to_log (format
, arg1
, arg2
);
709 /***********************************************************************
711 ***********************************************************************/
713 enum image_value_type
715 IMAGE_DONT_CHECK_VALUE_TYPE
,
717 IMAGE_STRING_OR_NIL_VALUE
,
719 IMAGE_POSITIVE_INTEGER_VALUE
,
720 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
721 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
724 IMAGE_FUNCTION_VALUE
,
729 /* Structure used when parsing image specifications. */
733 /* Name of keyword. */
736 /* The type of value allowed. */
737 enum image_value_type type
;
739 /* Non-zero means key must be present. */
742 /* Used to recognize duplicate keywords in a property list. */
745 /* The value that was found. */
750 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
752 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
755 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
756 has the format (image KEYWORD VALUE ...). One of the keyword/
757 value pairs must be `:type TYPE'. KEYWORDS is a vector of
758 image_keywords structures of size NKEYWORDS describing other
759 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
762 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
763 int nkeywords
, Lisp_Object type
)
772 while (CONSP (plist
))
774 Lisp_Object key
, value
;
776 /* First element of a pair must be a symbol. */
778 plist
= XCDR (plist
);
782 /* There must follow a value. */
785 value
= XCAR (plist
);
786 plist
= XCDR (plist
);
788 /* Find key in KEYWORDS. Error if not found. */
789 for (i
= 0; i
< nkeywords
; ++i
)
790 if (strcmp (keywords
[i
].name
, SDATA (SYMBOL_NAME (key
))) == 0)
796 /* Record that we recognized the keyword. If a keywords
797 was found more than once, it's an error. */
798 keywords
[i
].value
= value
;
801 if (keywords
[i
].count
> 1)
804 /* Check type of value against allowed type. */
805 switch (keywords
[i
].type
)
807 case IMAGE_STRING_VALUE
:
808 if (!STRINGP (value
))
812 case IMAGE_STRING_OR_NIL_VALUE
:
813 if (!STRINGP (value
) && !NILP (value
))
817 case IMAGE_SYMBOL_VALUE
:
818 if (!SYMBOLP (value
))
822 case IMAGE_POSITIVE_INTEGER_VALUE
:
823 if (!INTEGERP (value
) || XINT (value
) <= 0)
827 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
828 if (INTEGERP (value
) && XINT (value
) >= 0)
831 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
832 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
836 case IMAGE_ASCENT_VALUE
:
837 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
839 else if (INTEGERP (value
)
841 && XINT (value
) <= 100)
845 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
846 if (!INTEGERP (value
) || XINT (value
) < 0)
850 case IMAGE_DONT_CHECK_VALUE_TYPE
:
853 case IMAGE_FUNCTION_VALUE
:
854 value
= indirect_function (value
);
857 || (CONSP (value
) && EQ (XCAR (value
), Qlambda
)))
861 case IMAGE_NUMBER_VALUE
:
862 if (!INTEGERP (value
) && !FLOATP (value
))
866 case IMAGE_INTEGER_VALUE
:
867 if (!INTEGERP (value
))
871 case IMAGE_BOOL_VALUE
:
872 if (!NILP (value
) && !EQ (value
, Qt
))
881 if (EQ (key
, QCtype
) && !EQ (type
, value
))
885 /* Check that all mandatory fields are present. */
886 for (i
= 0; i
< nkeywords
; ++i
)
887 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
894 /* Return the value of KEY in image specification SPEC. Value is nil
895 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
896 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
899 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
903 xassert (valid_image_p (spec
));
905 for (tail
= XCDR (spec
);
906 CONSP (tail
) && CONSP (XCDR (tail
));
907 tail
= XCDR (XCDR (tail
)))
909 if (EQ (XCAR (tail
), key
))
913 return XCAR (XCDR (tail
));
923 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
924 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
925 PIXELS non-nil means return the size in pixels, otherwise return the
926 size in canonical character units.
927 FRAME is the frame on which the image will be displayed. FRAME nil
928 or omitted means use the selected frame. */)
929 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
934 if (valid_image_p (spec
))
936 struct frame
*f
= check_x_frame (frame
);
937 int id
= lookup_image (f
, spec
);
938 struct image
*img
= IMAGE_FROM_ID (f
, id
);
939 int width
= img
->width
+ 2 * img
->hmargin
;
940 int height
= img
->height
+ 2 * img
->vmargin
;
943 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
944 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
946 size
= Fcons (make_number (width
), make_number (height
));
949 error ("Invalid image specification");
955 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
956 doc
: /* Return t if image SPEC has a mask bitmap.
957 FRAME is the frame on which the image will be displayed. FRAME nil
958 or omitted means use the selected frame. */)
959 (Lisp_Object spec
, Lisp_Object frame
)
964 if (valid_image_p (spec
))
966 struct frame
*f
= check_x_frame (frame
);
967 int id
= lookup_image (f
, spec
);
968 struct image
*img
= IMAGE_FROM_ID (f
, id
);
973 error ("Invalid image specification");
978 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
979 doc
: /* Return metadata for image SPEC.
980 FRAME is the frame on which the image will be displayed. FRAME nil
981 or omitted means use the selected frame. */)
982 (Lisp_Object spec
, Lisp_Object frame
)
987 if (valid_image_p (spec
))
989 struct frame
*f
= check_x_frame (frame
);
990 int id
= lookup_image (f
, spec
);
991 struct image
*img
= IMAGE_FROM_ID (f
, id
);
992 ext
= img
->data
.lisp_val
;
999 /***********************************************************************
1000 Image type independent image structures
1001 ***********************************************************************/
1003 static struct image
*make_image (Lisp_Object spec
, unsigned hash
);
1004 static void free_image (struct frame
*f
, struct image
*img
);
1005 static int check_image_size (struct frame
*f
, int width
, int height
);
1007 #define MAX_IMAGE_SIZE 6.0
1008 Lisp_Object Vmax_image_size
;
1010 /* Allocate and return a new image structure for image specification
1011 SPEC. SPEC has a hash value of HASH. */
1013 static struct image
*
1014 make_image (Lisp_Object spec
, unsigned int hash
)
1016 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
1017 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
1019 xassert (valid_image_p (spec
));
1020 memset (img
, 0, sizeof *img
);
1021 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
1022 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
1023 xassert (img
->type
!= NULL
);
1025 img
->data
.lisp_val
= Qnil
;
1026 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1028 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1033 /* Free image IMG which was used on frame F, including its resources. */
1036 free_image (struct frame
*f
, struct image
*img
)
1040 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1042 /* Remove IMG from the hash table of its cache. */
1044 img
->prev
->next
= img
->next
;
1046 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1049 img
->next
->prev
= img
->prev
;
1051 c
->images
[img
->id
] = NULL
;
1053 /* Free resources, then free IMG. */
1054 img
->type
->free (f
, img
);
1057 /* As display glyphs may still be referring to the image ID, we
1058 must garbage the frame (Bug#6426). */
1059 SET_FRAME_GARBAGED (f
);
1063 /* Return 1 if the given widths and heights are valid for display;
1064 otherwise, return 0. */
1067 check_image_size (struct frame
*f
, int width
, int height
)
1071 if (width
<= 0 || height
<= 0)
1074 if (INTEGERP (Vmax_image_size
))
1075 w
= h
= XINT (Vmax_image_size
);
1076 else if (FLOATP (Vmax_image_size
))
1080 w
= FRAME_PIXEL_WIDTH (f
);
1081 h
= FRAME_PIXEL_HEIGHT (f
);
1084 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1085 w
= (int) (XFLOAT_DATA (Vmax_image_size
) * w
);
1086 h
= (int) (XFLOAT_DATA (Vmax_image_size
) * h
);
1091 return (width
<= w
&& height
<= h
);
1094 /* Prepare image IMG for display on frame F. Must be called before
1095 drawing an image. */
1098 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1102 /* We're about to display IMG, so set its timestamp to `now'. */
1104 img
->timestamp
= EMACS_SECS (t
);
1106 /* If IMG doesn't have a pixmap yet, load it now, using the image
1107 type dependent loader function. */
1108 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1109 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1114 /* Value is the number of pixels for the ascent of image IMG when
1115 drawn in face FACE. */
1118 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1123 if (slice
->height
== img
->height
)
1124 height
= img
->height
+ img
->vmargin
;
1125 else if (slice
->y
== 0)
1126 height
= slice
->height
+ img
->vmargin
;
1128 height
= slice
->height
;
1130 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1135 /* W32 specific version. Why?. ++kfs */
1136 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1137 - FONT_BASE (face
->font
)) / 2;
1139 /* This expression is arranged so that if the image can't be
1140 exactly centered, it will be moved slightly up. This is
1141 because a typical font is `top-heavy' (due to the presence
1142 uppercase letters), so the image placement should err towards
1143 being top-heavy too. It also just generally looks better. */
1144 ascent
= (height
+ FONT_BASE(face
->font
)
1145 - FONT_DESCENT(face
->font
) + 1) / 2;
1146 #endif /* HAVE_NTGUI */
1149 ascent
= height
/ 2;
1152 ascent
= (int) (height
* img
->ascent
/ 100.0);
1158 /* Image background colors. */
1160 /* Find the "best" corner color of a bitmap.
1161 On W32, XIMG is assumed to a device context with the bitmap selected. */
1163 static RGB_PIXEL_COLOR
1164 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1165 unsigned long width
, unsigned long height
)
1167 RGB_PIXEL_COLOR corner_pixels
[4], best
;
1170 if (corners
&& corners
[BOT_CORNER
] >= 0)
1172 /* Get the colors at the corner_pixels of ximg. */
1173 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1174 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1175 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1176 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1180 /* Get the colors at the corner_pixels of ximg. */
1181 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1182 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1183 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1184 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1186 /* Choose the most frequently found color as background. */
1187 for (i
= best_count
= 0; i
< 4; ++i
)
1191 for (j
= n
= 0; j
< 4; ++j
)
1192 if (corner_pixels
[i
] == corner_pixels
[j
])
1196 best
= corner_pixels
[i
], best_count
= n
;
1202 /* Portability macros */
1206 #define Destroy_Image(img_dc, prev) \
1207 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1209 #define Free_Pixmap(display, pixmap) \
1210 DeleteObject (pixmap)
1212 #elif defined (HAVE_NS)
1214 #define Destroy_Image(ximg, dummy) \
1215 ns_release_object (ximg)
1217 #define Free_Pixmap(display, pixmap) \
1218 ns_release_object (pixmap)
1222 #define Destroy_Image(ximg, dummy) \
1223 XDestroyImage (ximg)
1225 #define Free_Pixmap(display, pixmap) \
1226 XFreePixmap (display, pixmap)
1228 #endif /* !HAVE_NTGUI && !HAVE_NS */
1231 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1232 it is guessed heuristically. If non-zero, XIMG is an existing
1233 XImage object (or device context with the image selected on W32) to
1234 use for the heuristic. */
1237 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1239 if (! img
->background_valid
)
1240 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1242 int free_ximg
= !ximg
;
1245 #endif /* HAVE_NTGUI */
1250 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1251 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1253 HDC frame_dc
= get_frame_dc (f
);
1254 ximg
= CreateCompatibleDC (frame_dc
);
1255 release_frame_dc (f
, frame_dc
);
1256 prev
= SelectObject (ximg
, img
->pixmap
);
1257 #endif /* !HAVE_NTGUI */
1260 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1263 Destroy_Image (ximg
, prev
);
1265 img
->background_valid
= 1;
1268 return img
->background
;
1271 /* Return the `background_transparent' field of IMG. If IMG doesn't
1272 have one yet, it is guessed heuristically. If non-zero, MASK is an
1273 existing XImage object to use for the heuristic. */
1276 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1278 if (! img
->background_transparent_valid
)
1279 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1283 int free_mask
= !mask
;
1286 #endif /* HAVE_NTGUI */
1291 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1292 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1294 HDC frame_dc
= get_frame_dc (f
);
1295 mask
= CreateCompatibleDC (frame_dc
);
1296 release_frame_dc (f
, frame_dc
);
1297 prev
= SelectObject (mask
, img
->mask
);
1298 #endif /* HAVE_NTGUI */
1301 img
->background_transparent
1302 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1305 Destroy_Image (mask
, prev
);
1308 img
->background_transparent
= 0;
1310 img
->background_transparent_valid
= 1;
1313 return img
->background_transparent
;
1317 /***********************************************************************
1318 Helper functions for X image types
1319 ***********************************************************************/
1321 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1323 static void x_clear_image (struct frame
*f
, struct image
*img
);
1324 static unsigned long x_alloc_image_color (struct frame
*f
,
1326 Lisp_Object color_name
,
1327 unsigned long dflt
);
1330 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1331 free the pixmap if any. MASK_P non-zero means clear the mask
1332 pixmap if any. COLORS_P non-zero means free colors allocated for
1333 the image, if any. */
1336 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1339 if (pixmap_p
&& img
->pixmap
)
1341 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1342 img
->pixmap
= NO_PIXMAP
;
1343 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1344 img
->background_valid
= 0;
1347 if (mask_p
&& img
->mask
)
1349 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1350 img
->mask
= NO_PIXMAP
;
1351 img
->background_transparent_valid
= 0;
1354 if (colors_p
&& img
->ncolors
)
1356 /* W32_TODO: color table support. */
1357 #ifdef HAVE_X_WINDOWS
1358 x_free_colors (f
, img
->colors
, img
->ncolors
);
1359 #endif /* HAVE_X_WINDOWS */
1360 xfree (img
->colors
);
1367 /* Free X resources of image IMG which is used on frame F. */
1370 x_clear_image (struct frame
*f
, struct image
*img
)
1373 x_clear_image_1 (f
, img
, 1, 1, 1);
1378 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1379 cannot be allocated, use DFLT. Add a newly allocated color to
1380 IMG->colors, so that it can be freed again. Value is the pixel
1383 static unsigned long
1384 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1388 unsigned long result
;
1390 xassert (STRINGP (color_name
));
1392 if (x_defined_color (f
, SDATA (color_name
), &color
, 1))
1394 /* This isn't called frequently so we get away with simply
1395 reallocating the color vector to the needed size, here. */
1398 (unsigned long *) xrealloc (img
->colors
,
1399 img
->ncolors
* sizeof *img
->colors
);
1400 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
1401 result
= color
.pixel
;
1411 /***********************************************************************
1413 ***********************************************************************/
1415 static struct image
*search_image_cache (struct frame
*, Lisp_Object
, unsigned);
1416 static void cache_image (struct frame
*f
, struct image
*img
);
1417 static void postprocess_image (struct frame
*, struct image
*);
1419 /* Return a new, initialized image cache that is allocated from the
1420 heap. Call free_image_cache to free an image cache. */
1422 struct image_cache
*
1423 make_image_cache (void)
1425 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1428 memset (c
, 0, sizeof *c
);
1430 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
1431 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1432 c
->buckets
= (struct image
**) xmalloc (size
);
1433 memset (c
->buckets
, 0, size
);
1438 /* Find an image matching SPEC in the cache, and return it. If no
1439 image is found, return NULL. */
1440 static struct image
*
1441 search_image_cache (struct frame
*f
, Lisp_Object spec
, unsigned int hash
)
1444 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1445 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1447 if (!c
) return NULL
;
1449 /* If the image spec does not specify a background color, the cached
1450 image must have the same background color as the current frame.
1451 The foreground color must also match, for the sake of monochrome
1454 In fact, we could ignore the foreground color matching condition
1455 for color images, or if the image spec specifies :foreground;
1456 similarly we could ignore the background color matching condition
1457 for formats that don't use transparency (such as jpeg), or if the
1458 image spec specifies :background. However, the extra memory
1459 usage is probably negligible in practice, so we don't bother. */
1461 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1462 if (img
->hash
== hash
1463 && !NILP (Fequal (img
->spec
, spec
))
1464 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1465 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1471 /* Search frame F for an image with spec SPEC, and free it. */
1474 uncache_image (struct frame
*f
, Lisp_Object spec
)
1476 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1478 free_image (f
, img
);
1482 /* Free image cache of frame F. Be aware that X frames share images
1486 free_image_cache (struct frame
*f
)
1488 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1493 /* Cache should not be referenced by any frame when freed. */
1494 xassert (c
->refcount
== 0);
1496 for (i
= 0; i
< c
->used
; ++i
)
1497 free_image (f
, c
->images
[i
]);
1501 FRAME_IMAGE_CACHE (f
) = NULL
;
1506 /* Clear image cache of frame F. FILTER=t means free all images.
1507 FILTER=nil means clear only images that haven't been
1508 displayed for some time.
1509 Else, only free the images which have FILTER in their `dependencies'.
1510 Should be called from time to time to reduce the number of loaded images.
1511 If image-cache-eviction-delay is non-nil, this frees images in the cache
1512 which weren't displayed for at least that many seconds. */
1515 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1517 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1523 /* Block input so that we won't be interrupted by a SIGIO
1524 while being in an inconsistent state. */
1529 /* Filter image cache. */
1530 for (i
= 0; i
< c
->used
; ++i
)
1532 struct image
*img
= c
->images
[i
];
1533 if (img
&& (EQ (Qt
, filter
)
1534 || !NILP (Fmember (filter
, img
->dependencies
))))
1536 free_image (f
, img
);
1541 else if (INTEGERP (Vimage_cache_eviction_delay
))
1543 /* Free cache based on timestamp. */
1546 int delay
, nimages
= 0;
1548 for (i
= 0; i
< c
->used
; ++i
)
1552 /* If the number of cached images has grown unusually large,
1553 decrease the cache eviction delay (Bug#6230). */
1554 delay
= XFASTINT (Vimage_cache_eviction_delay
);
1556 delay
= max (1, 1600 * delay
/ (nimages
*nimages
));
1559 old
= EMACS_SECS (t
) - delay
;
1561 for (i
= 0; i
< c
->used
; ++i
)
1563 struct image
*img
= c
->images
[i
];
1564 if (img
&& img
->timestamp
< old
)
1566 free_image (f
, img
);
1572 /* We may be clearing the image cache because, for example,
1573 Emacs was iconified for a longer period of time. In that
1574 case, current matrices may still contain references to
1575 images freed above. So, clear these matrices. */
1578 Lisp_Object tail
, frame
;
1580 FOR_EACH_FRAME (tail
, frame
)
1582 struct frame
*f
= XFRAME (frame
);
1583 if (FRAME_IMAGE_CACHE (f
) == c
)
1584 clear_current_matrices (f
);
1587 ++windows_or_buffers_changed
;
1595 clear_image_caches (Lisp_Object filter
)
1597 /* FIXME: We want to do
1598 * struct terminal *t;
1599 * for (t = terminal_list; t; t = t->next_terminal)
1600 * clear_image_cache (t, filter); */
1601 Lisp_Object tail
, frame
;
1602 FOR_EACH_FRAME (tail
, frame
)
1603 if (FRAME_WINDOW_P (XFRAME (frame
)))
1604 clear_image_cache (XFRAME (frame
), filter
);
1607 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1609 doc
: /* Clear the image cache.
1610 FILTER nil or a frame means clear all images in the selected frame.
1611 FILTER t means clear the image caches of all frames.
1612 Anything else, means only clear those images which refer to FILTER,
1613 which is then usually a filename. */)
1614 (Lisp_Object filter
)
1616 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1617 clear_image_caches (filter
);
1619 clear_image_cache (check_x_frame (filter
), Qt
);
1625 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1627 doc
: /* Fush the image with specification SPEC on frame FRAME.
1628 This removes the image from the Emacs image cache. If SPEC specifies
1629 an image file, the next redisplay of this image will read from the
1630 current contents of that file.
1632 FRAME nil or omitted means use the selected frame.
1633 FRAME t means refresh the image on all frames. */)
1634 (Lisp_Object spec
, Lisp_Object frame
)
1636 if (!valid_image_p (spec
))
1637 error ("Invalid image specification");
1642 FOR_EACH_FRAME (tail
, frame
)
1644 struct frame
*f
= XFRAME (frame
);
1645 if (FRAME_WINDOW_P (f
))
1646 uncache_image (f
, spec
);
1650 uncache_image (check_x_frame (frame
), spec
);
1656 /* Compute masks and transform image IMG on frame F, as specified
1657 by the image's specification, */
1660 postprocess_image (struct frame
*f
, struct image
*img
)
1662 /* Manipulation of the image's mask. */
1665 Lisp_Object conversion
, spec
;
1670 /* `:heuristic-mask t'
1672 means build a mask heuristically.
1673 `:heuristic-mask (R G B)'
1674 `:mask (heuristic (R G B))'
1675 means build a mask from color (R G B) in the
1678 means remove a mask, if any. */
1680 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1682 x_build_heuristic_mask (f
, img
, mask
);
1687 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1689 if (EQ (mask
, Qheuristic
))
1690 x_build_heuristic_mask (f
, img
, Qt
);
1691 else if (CONSP (mask
)
1692 && EQ (XCAR (mask
), Qheuristic
))
1694 if (CONSP (XCDR (mask
)))
1695 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1697 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1699 else if (NILP (mask
) && found_p
&& img
->mask
)
1701 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1702 img
->mask
= NO_PIXMAP
;
1707 /* Should we apply an image transformation algorithm? */
1708 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1709 if (EQ (conversion
, Qdisabled
))
1710 x_disable_image (f
, img
);
1711 else if (EQ (conversion
, Qlaplace
))
1713 else if (EQ (conversion
, Qemboss
))
1715 else if (CONSP (conversion
)
1716 && EQ (XCAR (conversion
), Qedge_detection
))
1719 tem
= XCDR (conversion
);
1721 x_edge_detection (f
, img
,
1722 Fplist_get (tem
, QCmatrix
),
1723 Fplist_get (tem
, QCcolor_adjustment
));
1729 /* Return the id of image with Lisp specification SPEC on frame F.
1730 SPEC must be a valid Lisp image specification (see valid_image_p). */
1733 lookup_image (struct frame
*f
, Lisp_Object spec
)
1735 struct image_cache
*c
;
1738 struct gcpro gcpro1
;
1741 /* F must be a window-system frame, and SPEC must be a valid image
1743 xassert (FRAME_WINDOW_P (f
));
1744 xassert (valid_image_p (spec
));
1746 c
= FRAME_IMAGE_CACHE (f
);
1750 /* Look up SPEC in the hash table of the image cache. */
1751 hash
= sxhash (spec
, 0);
1752 img
= search_image_cache (f
, spec
, hash
);
1753 if (img
&& img
->load_failed_p
)
1755 free_image (f
, img
);
1759 /* If not found, create a new image and cache it. */
1762 extern Lisp_Object Qpostscript
;
1765 img
= make_image (spec
, hash
);
1766 cache_image (f
, img
);
1767 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1768 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1769 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1771 /* If we can't load the image, and we don't have a width and
1772 height, use some arbitrary width and height so that we can
1773 draw a rectangle for it. */
1774 if (img
->load_failed_p
)
1778 value
= image_spec_value (spec
, QCwidth
, NULL
);
1779 img
->width
= (INTEGERP (value
)
1780 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1781 value
= image_spec_value (spec
, QCheight
, NULL
);
1782 img
->height
= (INTEGERP (value
)
1783 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1787 /* Handle image type independent image attributes
1788 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1789 `:background COLOR'. */
1790 Lisp_Object ascent
, margin
, relief
, bg
;
1792 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1793 if (INTEGERP (ascent
))
1794 img
->ascent
= XFASTINT (ascent
);
1795 else if (EQ (ascent
, Qcenter
))
1796 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1798 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1799 if (INTEGERP (margin
) && XINT (margin
) >= 0)
1800 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1801 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
1802 && INTEGERP (XCDR (margin
)))
1804 if (XINT (XCAR (margin
)) > 0)
1805 img
->hmargin
= XFASTINT (XCAR (margin
));
1806 if (XINT (XCDR (margin
)) > 0)
1807 img
->vmargin
= XFASTINT (XCDR (margin
));
1810 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1811 if (INTEGERP (relief
))
1813 img
->relief
= XINT (relief
);
1814 img
->hmargin
+= eabs (img
->relief
);
1815 img
->vmargin
+= eabs (img
->relief
);
1818 if (! img
->background_valid
)
1820 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1824 = x_alloc_image_color (f
, img
, bg
,
1825 FRAME_BACKGROUND_PIXEL (f
));
1826 img
->background_valid
= 1;
1830 /* Do image transformations and compute masks, unless we
1831 don't have the image yet. */
1832 if (!EQ (*img
->type
->type
, Qpostscript
))
1833 postprocess_image (f
, img
);
1839 /* We're using IMG, so set its timestamp to `now'. */
1840 EMACS_GET_TIME (now
);
1841 img
->timestamp
= EMACS_SECS (now
);
1845 /* Value is the image id. */
1850 /* Cache image IMG in the image cache of frame F. */
1853 cache_image (struct frame
*f
, struct image
*img
)
1855 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1858 /* Find a free slot in c->images. */
1859 for (i
= 0; i
< c
->used
; ++i
)
1860 if (c
->images
[i
] == NULL
)
1863 /* If no free slot found, maybe enlarge c->images. */
1864 if (i
== c
->used
&& c
->used
== c
->size
)
1867 c
->images
= (struct image
**) xrealloc (c
->images
,
1868 c
->size
* sizeof *c
->images
);
1871 /* Add IMG to c->images, and assign IMG an id. */
1877 /* Add IMG to the cache's hash table. */
1878 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1879 img
->next
= c
->buckets
[i
];
1881 img
->next
->prev
= img
;
1883 c
->buckets
[i
] = img
;
1887 /* Call FN on every image in the image cache of frame F. Used to mark
1888 Lisp Objects in the image cache. */
1890 /* Mark Lisp objects in image IMG. */
1893 mark_image (struct image
*img
)
1895 mark_object (img
->spec
);
1896 mark_object (img
->dependencies
);
1898 if (!NILP (img
->data
.lisp_val
))
1899 mark_object (img
->data
.lisp_val
);
1904 mark_image_cache (struct image_cache
*c
)
1909 for (i
= 0; i
< c
->used
; ++i
)
1911 mark_image (c
->images
[i
]);
1917 /***********************************************************************
1918 X / NS / W32 support code
1919 ***********************************************************************/
1923 /* Macro for defining functions that will be loaded from image DLLs. */
1924 #define DEF_IMGLIB_FN(func) int (FAR CDECL *fn_##func)()
1926 /* Macro for loading those image functions from the library. */
1927 #define LOAD_IMGLIB_FN(lib,func) { \
1928 fn_##func = (void *) GetProcAddress (lib, #func); \
1929 if (!fn_##func) return 0; \
1932 /* Load a DLL implementing an image type.
1933 The `image-library-alist' variable associates a symbol,
1934 identifying an image type, to a list of possible filenames.
1935 The function returns NULL if no library could be loaded for
1936 the given image type, or if the library was previously loaded;
1937 else the handle of the DLL. */
1939 w32_delayed_load (Lisp_Object libraries
, Lisp_Object type
)
1941 HMODULE library
= NULL
;
1943 if (CONSP (libraries
) && NILP (Fassq (type
, Vimage_type_cache
)))
1945 Lisp_Object dlls
= Fassq (type
, libraries
);
1948 for (dlls
= XCDR (dlls
); CONSP (dlls
); dlls
= XCDR (dlls
))
1950 CHECK_STRING_CAR (dlls
);
1951 if (library
= LoadLibrary (SDATA (XCAR (dlls
))))
1959 #endif /* HAVE_NTGUI */
1961 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1962 XImagePtr
*, Pixmap
*);
1963 static void x_destroy_x_image (XImagePtr
);
1964 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1967 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1968 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1969 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1970 via xmalloc. Print error messages via image_error if an error
1971 occurs. Value is non-zero if successful.
1973 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1974 should indicate the bit depth of the image. */
1977 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1978 XImagePtr
*ximg
, Pixmap
*pixmap
)
1980 #ifdef HAVE_X_WINDOWS
1981 Display
*display
= FRAME_X_DISPLAY (f
);
1982 Window window
= FRAME_X_WINDOW (f
);
1983 Screen
*screen
= FRAME_X_SCREEN (f
);
1985 xassert (interrupt_input_blocked
);
1988 depth
= DefaultDepthOfScreen (screen
);
1989 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1990 depth
, ZPixmap
, 0, NULL
, width
, height
,
1991 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1994 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1998 /* Allocate image raster. */
1999 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
2001 /* Allocate a pixmap of the same size. */
2002 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
2003 if (*pixmap
== NO_PIXMAP
)
2005 x_destroy_x_image (*ximg
);
2007 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
2012 #endif /* HAVE_X_WINDOWS */
2016 BITMAPINFOHEADER
*header
;
2018 int scanline_width_bits
;
2020 int palette_colors
= 0;
2025 if (depth
!= 1 && depth
!= 4 && depth
!= 8
2026 && depth
!= 16 && depth
!= 24 && depth
!= 32)
2028 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2032 scanline_width_bits
= width
* depth
;
2033 remainder
= scanline_width_bits
% 32;
2036 scanline_width_bits
+= 32 - remainder
;
2038 /* Bitmaps with a depth less than 16 need a palette. */
2039 /* BITMAPINFO structure already contains the first RGBQUAD. */
2041 palette_colors
= 1 << depth
- 1;
2043 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2046 image_error ("Unable to allocate memory for XImage", Qnil
, Qnil
);
2050 header
= &(*ximg
)->info
.bmiHeader
;
2051 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2052 header
->biSize
= sizeof (*header
);
2053 header
->biWidth
= width
;
2054 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2055 header
->biPlanes
= 1;
2056 header
->biBitCount
= depth
;
2057 header
->biCompression
= BI_RGB
;
2058 header
->biClrUsed
= palette_colors
;
2060 /* TODO: fill in palette. */
2063 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2064 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2065 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2066 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2067 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2068 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2069 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2070 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2073 hdc
= get_frame_dc (f
);
2075 /* Create a DIBSection and raster array for the bitmap,
2076 and store its handle in *pixmap. */
2077 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2078 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2079 /* casting avoids a GCC warning */
2080 (void **)&((*ximg
)->data
), NULL
, 0);
2082 /* Realize display palette and garbage all frames. */
2083 release_frame_dc (f
, hdc
);
2085 if (*pixmap
== NULL
)
2087 DWORD err
= GetLastError ();
2088 Lisp_Object errcode
;
2089 /* All system errors are < 10000, so the following is safe. */
2090 XSETINT (errcode
, (int) err
);
2091 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2092 x_destroy_x_image (*ximg
);
2098 #endif /* HAVE_NTGUI */
2101 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2105 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2114 /* Destroy XImage XIMG. Free XIMG->data. */
2117 x_destroy_x_image (XImagePtr ximg
)
2119 xassert (interrupt_input_blocked
);
2122 #ifdef HAVE_X_WINDOWS
2125 XDestroyImage (ximg
);
2126 #endif /* HAVE_X_WINDOWS */
2128 /* Data will be freed by DestroyObject. */
2131 #endif /* HAVE_NTGUI */
2133 ns_release_object (ximg
);
2134 #endif /* HAVE_NS */
2139 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2140 are width and height of both the image and pixmap. */
2143 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2145 #ifdef HAVE_X_WINDOWS
2148 xassert (interrupt_input_blocked
);
2149 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2150 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2151 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2152 #endif /* HAVE_X_WINDOWS */
2155 #if 0 /* I don't think this is necessary looking at where it is used. */
2156 HDC hdc
= get_frame_dc (f
);
2157 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2158 release_frame_dc (f
, hdc
);
2160 #endif /* HAVE_NTGUI */
2163 xassert (ximg
== pixmap
);
2164 ns_retain_object (ximg
);
2169 /***********************************************************************
2171 ***********************************************************************/
2173 static unsigned char *slurp_file (char *, int *);
2176 /* Find image file FILE. Look in data-directory/images, then
2177 x-bitmap-file-path. Value is the encoded full name of the file
2178 found, or nil if not found. */
2181 x_find_image_file (Lisp_Object file
)
2183 Lisp_Object file_found
, search_path
;
2184 struct gcpro gcpro1
, gcpro2
;
2188 /* TODO I think this should use something like image-load-path
2189 instead. Unfortunately, that can contain non-string elements. */
2190 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2192 Vx_bitmap_file_path
);
2193 GCPRO2 (file_found
, search_path
);
2195 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2196 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2202 file_found
= ENCODE_FILE (file_found
);
2211 /* Read FILE into memory. Value is a pointer to a buffer allocated
2212 with xmalloc holding FILE's contents. Value is null if an error
2213 occurred. *SIZE is set to the size of the file. */
2215 static unsigned char *
2216 slurp_file (char *file
, int *size
)
2219 unsigned char *buf
= NULL
;
2222 if (stat (file
, &st
) == 0
2223 && (fp
= fopen (file
, "rb")) != NULL
2224 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2225 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2246 /***********************************************************************
2248 ***********************************************************************/
2250 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2251 static int xbm_load (struct frame
*f
, struct image
*img
);
2252 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2253 unsigned char *, unsigned char *);
2254 static int xbm_image_p (Lisp_Object object
);
2255 static int xbm_read_bitmap_data (struct frame
*f
,
2256 unsigned char *, unsigned char *,
2257 int *, int *, unsigned char **, int);
2258 static int xbm_file_p (Lisp_Object
);
2261 /* Indices of image specification fields in xbm_format, below. */
2263 enum xbm_keyword_index
2281 /* Vector of image_keyword structures describing the format
2282 of valid XBM image specifications. */
2284 static const struct image_keyword xbm_format
[XBM_LAST
] =
2286 {":type", IMAGE_SYMBOL_VALUE
, 1},
2287 {":file", IMAGE_STRING_VALUE
, 0},
2288 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2289 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2290 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2291 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2292 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2293 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2294 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2295 {":relief", IMAGE_INTEGER_VALUE
, 0},
2296 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2297 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2298 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2301 /* Structure describing the image type XBM. */
2303 static struct image_type xbm_type
=
2312 /* Tokens returned from xbm_scan. */
2321 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2322 A valid specification is a list starting with the symbol `image'
2323 The rest of the list is a property list which must contain an
2326 If the specification specifies a file to load, it must contain
2327 an entry `:file FILENAME' where FILENAME is a string.
2329 If the specification is for a bitmap loaded from memory it must
2330 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2331 WIDTH and HEIGHT are integers > 0. DATA may be:
2333 1. a string large enough to hold the bitmap data, i.e. it must
2334 have a size >= (WIDTH + 7) / 8 * HEIGHT
2336 2. a bool-vector of size >= WIDTH * HEIGHT
2338 3. a vector of strings or bool-vectors, one for each line of the
2341 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2342 may not be specified in this case because they are defined in the
2345 Both the file and data forms may contain the additional entries
2346 `:background COLOR' and `:foreground COLOR'. If not present,
2347 foreground and background of the frame on which the image is
2348 displayed is used. */
2351 xbm_image_p (Lisp_Object object
)
2353 struct image_keyword kw
[XBM_LAST
];
2355 memcpy (kw
, xbm_format
, sizeof kw
);
2356 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2359 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2361 if (kw
[XBM_FILE
].count
)
2363 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2366 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2368 /* In-memory XBM file. */
2369 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2377 /* Entries for `:width', `:height' and `:data' must be present. */
2378 if (!kw
[XBM_WIDTH
].count
2379 || !kw
[XBM_HEIGHT
].count
2380 || !kw
[XBM_DATA
].count
)
2383 data
= kw
[XBM_DATA
].value
;
2384 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2385 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2387 /* Check type of data, and width and height against contents of
2393 /* Number of elements of the vector must be >= height. */
2394 if (XVECTOR (data
)->size
< height
)
2397 /* Each string or bool-vector in data must be large enough
2398 for one line of the image. */
2399 for (i
= 0; i
< height
; ++i
)
2401 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2406 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2409 else if (BOOL_VECTOR_P (elt
))
2411 if (XBOOL_VECTOR (elt
)->size
< width
)
2418 else if (STRINGP (data
))
2421 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2424 else if (BOOL_VECTOR_P (data
))
2426 if (XBOOL_VECTOR (data
)->size
< width
* height
)
2437 /* Scan a bitmap file. FP is the stream to read from. Value is
2438 either an enumerator from enum xbm_token, or a character for a
2439 single-character token, or 0 at end of file. If scanning an
2440 identifier, store the lexeme of the identifier in SVAL. If
2441 scanning a number, store its value in *IVAL. */
2444 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2450 /* Skip white space. */
2451 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2456 else if (isdigit (c
))
2458 int value
= 0, digit
;
2460 if (c
== '0' && *s
< end
)
2463 if (c
== 'x' || c
== 'X')
2470 else if (c
>= 'a' && c
<= 'f')
2471 digit
= c
- 'a' + 10;
2472 else if (c
>= 'A' && c
<= 'F')
2473 digit
= c
- 'A' + 10;
2476 value
= 16 * value
+ digit
;
2479 else if (isdigit (c
))
2483 && (c
= *(*s
)++, isdigit (c
)))
2484 value
= 8 * value
+ c
- '0';
2491 && (c
= *(*s
)++, isdigit (c
)))
2492 value
= 10 * value
+ c
- '0';
2500 else if (isalpha (c
) || c
== '_')
2504 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2511 else if (c
== '/' && **s
== '*')
2513 /* C-style comment. */
2515 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2529 /* Create a Windows bitmap from X bitmap data. */
2531 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2533 static unsigned char swap_nibble
[16]
2534 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2535 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2536 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2537 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2539 unsigned char *bits
, *p
;
2542 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2543 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2544 bits
= (unsigned char *) alloca (height
* w2
);
2545 memset (bits
, 0, height
* w2
);
2546 for (i
= 0; i
< height
; i
++)
2549 for (j
= 0; j
< w1
; j
++)
2551 /* Bitswap XBM bytes to match how Windows does things. */
2552 unsigned char c
= *data
++;
2553 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2554 | (swap_nibble
[(c
>>4) & 0xf]));
2557 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2563 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2564 COLORREF foreground
, COLORREF background
)
2566 HDC hdc
, old_img_dc
, new_img_dc
;
2567 HGDIOBJ old_prev
, new_prev
;
2570 hdc
= get_frame_dc (f
);
2571 old_img_dc
= CreateCompatibleDC (hdc
);
2572 new_img_dc
= CreateCompatibleDC (hdc
);
2573 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2574 release_frame_dc (f
, hdc
);
2575 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2576 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2577 /* Windows convention for mono bitmaps is black = background,
2578 white = foreground. */
2579 SetTextColor (new_img_dc
, background
);
2580 SetBkColor (new_img_dc
, foreground
);
2582 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2585 SelectObject (old_img_dc
, old_prev
);
2586 SelectObject (new_img_dc
, new_prev
);
2587 DeleteDC (old_img_dc
);
2588 DeleteDC (new_img_dc
);
2589 DeleteObject (img
->pixmap
);
2590 if (new_pixmap
== 0)
2591 fprintf (stderr
, "Failed to convert image to color.\n");
2593 img
->pixmap
= new_pixmap
;
2596 #define XBM_BIT_SHUFFLE(b) (~(b))
2600 #define XBM_BIT_SHUFFLE(b) (b)
2602 #endif /* HAVE_NTGUI */
2606 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2607 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2608 int non_default_colors
)
2612 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2614 /* If colors were specified, transfer the bitmap to a color one. */
2615 if (non_default_colors
)
2616 convert_mono_to_color_image (f
, img
, fg
, bg
);
2618 #elif defined (HAVE_NS)
2619 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2623 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2626 img
->width
, img
->height
,
2628 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
2629 #endif /* !HAVE_NTGUI && !HAVE_NS */
2634 /* Replacement for XReadBitmapFileData which isn't available under old
2635 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2636 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2637 the image. Return in *DATA the bitmap data allocated with xmalloc.
2638 Value is non-zero if successful. DATA null means just test if
2639 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2640 is non-zero, inhibit the call to image_error when the image size is
2641 invalid (the bitmap remains unread). */
2644 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2645 int *width
, int *height
, unsigned char **data
,
2646 int inhibit_image_error
)
2648 unsigned char *s
= contents
;
2649 char buffer
[BUFSIZ
];
2652 int bytes_per_line
, i
, nbytes
;
2658 LA1 = xbm_scan (&s, end, buffer, &value)
2660 #define expect(TOKEN) \
2661 if (LA1 != (TOKEN)) \
2666 #define expect_ident(IDENT) \
2667 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2672 *width
= *height
= -1;
2675 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2677 /* Parse defines for width, height and hot-spots. */
2681 expect_ident ("define");
2682 expect (XBM_TK_IDENT
);
2684 if (LA1
== XBM_TK_NUMBER
)
2686 char *p
= strrchr (buffer
, '_');
2687 p
= p
? p
+ 1 : buffer
;
2688 if (strcmp (p
, "width") == 0)
2690 else if (strcmp (p
, "height") == 0)
2693 expect (XBM_TK_NUMBER
);
2696 if (!check_image_size (f
, *width
, *height
))
2698 if (!inhibit_image_error
)
2699 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2702 else if (data
== NULL
)
2705 /* Parse bits. Must start with `static'. */
2706 expect_ident ("static");
2707 if (LA1
== XBM_TK_IDENT
)
2709 if (strcmp (buffer
, "unsigned") == 0)
2712 expect_ident ("char");
2714 else if (strcmp (buffer
, "short") == 0)
2718 if (*width
% 16 && *width
% 16 < 9)
2721 else if (strcmp (buffer
, "char") == 0)
2729 expect (XBM_TK_IDENT
);
2735 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2736 nbytes
= bytes_per_line
* *height
;
2737 p
= *data
= (unsigned char *) xmalloc (nbytes
);
2741 for (i
= 0; i
< nbytes
; i
+= 2)
2744 expect (XBM_TK_NUMBER
);
2746 *p
++ = XBM_BIT_SHUFFLE (val
);
2747 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2748 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2750 if (LA1
== ',' || LA1
== '}')
2758 for (i
= 0; i
< nbytes
; ++i
)
2761 expect (XBM_TK_NUMBER
);
2763 *p
++ = XBM_BIT_SHUFFLE (val
);
2765 if (LA1
== ',' || LA1
== '}')
2790 /* Load XBM image IMG which will be displayed on frame F from buffer
2791 CONTENTS. END is the end of the buffer. Value is non-zero if
2795 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2799 unsigned char *data
;
2802 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2806 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2807 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2808 int non_default_colors
= 0;
2811 xassert (img
->width
> 0 && img
->height
> 0);
2813 /* Get foreground and background colors, maybe allocate colors. */
2814 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2817 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2818 non_default_colors
= 1;
2820 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2823 background
= x_alloc_image_color (f
, img
, value
, background
);
2824 img
->background
= background
;
2825 img
->background_valid
= 1;
2826 non_default_colors
= 1;
2829 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2830 foreground
, background
,
2831 non_default_colors
);
2834 if (img
->pixmap
== NO_PIXMAP
)
2836 x_clear_image (f
, img
);
2837 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2843 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2849 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2852 xbm_file_p (Lisp_Object data
)
2855 return (STRINGP (data
)
2856 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2857 (SDATA (data
) + SBYTES (data
)),
2862 /* Fill image IMG which is used on frame F with pixmap data. Value is
2863 non-zero if successful. */
2866 xbm_load (struct frame
*f
, struct image
*img
)
2869 Lisp_Object file_name
;
2871 xassert (xbm_image_p (img
->spec
));
2873 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2874 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2875 if (STRINGP (file_name
))
2878 unsigned char *contents
;
2880 struct gcpro gcpro1
;
2882 file
= x_find_image_file (file_name
);
2884 if (!STRINGP (file
))
2886 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2891 contents
= slurp_file (SDATA (file
), &size
);
2892 if (contents
== NULL
)
2894 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2899 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2904 struct image_keyword fmt
[XBM_LAST
];
2906 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2907 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2908 int non_default_colors
= 0;
2911 int in_memory_file_p
= 0;
2913 /* See if data looks like an in-memory XBM file. */
2914 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2915 in_memory_file_p
= xbm_file_p (data
);
2917 /* Parse the image specification. */
2918 memcpy (fmt
, xbm_format
, sizeof fmt
);
2919 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2922 /* Get specified width, and height. */
2923 if (!in_memory_file_p
)
2925 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2926 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2927 xassert (img
->width
> 0 && img
->height
> 0);
2930 /* Get foreground and background colors, maybe allocate colors. */
2931 if (fmt
[XBM_FOREGROUND
].count
2932 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2934 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2936 non_default_colors
= 1;
2939 if (fmt
[XBM_BACKGROUND
].count
2940 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2942 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2944 non_default_colors
= 1;
2947 if (in_memory_file_p
)
2948 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2957 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2959 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2960 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2962 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2964 memcpy (p
, SDATA (line
), nbytes
);
2966 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2969 else if (STRINGP (data
))
2970 bits
= SDATA (data
);
2972 bits
= XBOOL_VECTOR (data
)->data
;
2978 /* Windows mono bitmaps are reversed compared with X. */
2979 invertedBits
= bits
;
2980 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2982 bits
= (char *) alloca (nbytes
);
2983 for (i
= 0; i
< nbytes
; i
++)
2984 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2987 /* Create the pixmap. */
2989 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2990 foreground
, background
,
2991 non_default_colors
);
2996 image_error ("Unable to create pixmap for XBM image `%s'",
2998 x_clear_image (f
, img
);
3008 /***********************************************************************
3010 ***********************************************************************/
3012 #if defined (HAVE_XPM) || defined (HAVE_NS)
3014 static int xpm_image_p (Lisp_Object object
);
3015 static int xpm_load (struct frame
*f
, struct image
*img
);
3016 static int xpm_valid_color_symbols_p (Lisp_Object
);
3018 #endif /* HAVE_XPM || HAVE_NS */
3022 /* Indicate to xpm.h that we don't have Xlib. */
3024 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3025 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3026 #define XColor xpm_XColor
3027 #define XImage xpm_XImage
3028 #define Display xpm_Display
3029 #define PIXEL_ALREADY_TYPEDEFED
3030 #include "X11/xpm.h"
3035 #undef PIXEL_ALREADY_TYPEDEFED
3037 #include "X11/xpm.h"
3038 #endif /* HAVE_NTGUI */
3039 #endif /* HAVE_XPM */
3041 #if defined (HAVE_XPM) || defined (HAVE_NS)
3042 /* The symbol `xpm' identifying XPM-format images. */
3046 /* Indices of image specification fields in xpm_format, below. */
3048 enum xpm_keyword_index
3064 /* Vector of image_keyword structures describing the format
3065 of valid XPM image specifications. */
3067 static const struct image_keyword xpm_format
[XPM_LAST
] =
3069 {":type", IMAGE_SYMBOL_VALUE
, 1},
3070 {":file", IMAGE_STRING_VALUE
, 0},
3071 {":data", IMAGE_STRING_VALUE
, 0},
3072 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3073 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3074 {":relief", IMAGE_INTEGER_VALUE
, 0},
3075 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3076 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3077 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3078 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3079 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3082 /* Structure describing the image type XPM. */
3084 static struct image_type xpm_type
=
3093 #ifdef HAVE_X_WINDOWS
3095 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3096 functions for allocating image colors. Our own functions handle
3097 color allocation failures more gracefully than the ones on the XPM
3100 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3101 #define ALLOC_XPM_COLORS
3103 #endif /* HAVE_X_WINDOWS */
3105 #ifdef ALLOC_XPM_COLORS
3107 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3108 static void xpm_free_color_cache (void);
3109 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3110 static int xpm_color_bucket (char *);
3111 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3114 /* An entry in a hash table used to cache color definitions of named
3115 colors. This cache is necessary to speed up XPM image loading in
3116 case we do color allocations ourselves. Without it, we would need
3117 a call to XParseColor per pixel in the image. */
3119 struct xpm_cached_color
3121 /* Next in collision chain. */
3122 struct xpm_cached_color
*next
;
3124 /* Color definition (RGB and pixel color). */
3131 /* The hash table used for the color cache, and its bucket vector
3134 #define XPM_COLOR_CACHE_BUCKETS 1001
3135 struct xpm_cached_color
**xpm_color_cache
;
3137 /* Initialize the color cache. */
3140 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3142 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3143 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3144 memset (xpm_color_cache
, 0, nbytes
);
3145 init_color_table ();
3147 if (attrs
->valuemask
& XpmColorSymbols
)
3152 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3153 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3154 attrs
->colorsymbols
[i
].value
, &color
))
3156 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3158 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3163 /* Free the color cache. */
3166 xpm_free_color_cache (void)
3168 struct xpm_cached_color
*p
, *next
;
3171 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3172 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3178 xfree (xpm_color_cache
);
3179 xpm_color_cache
= NULL
;
3180 free_color_table ();
3183 /* Return the bucket index for color named COLOR_NAME in the color
3187 xpm_color_bucket (char *color_name
)
3192 for (s
= color_name
; *s
; ++s
)
3194 return h
%= XPM_COLOR_CACHE_BUCKETS
;
3198 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3199 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3202 static struct xpm_cached_color
*
3203 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3206 struct xpm_cached_color
*p
;
3209 bucket
= xpm_color_bucket (color_name
);
3211 nbytes
= sizeof *p
+ strlen (color_name
);
3212 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3213 strcpy (p
->name
, color_name
);
3215 p
->next
= xpm_color_cache
[bucket
];
3216 xpm_color_cache
[bucket
] = p
;
3220 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3221 return the cached definition in *COLOR. Otherwise, make a new
3222 entry in the cache and allocate the color. Value is zero if color
3223 allocation failed. */
3226 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3228 struct xpm_cached_color
*p
;
3229 int h
= xpm_color_bucket (color_name
);
3231 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3232 if (strcmp (p
->name
, color_name
) == 0)
3237 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3240 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3242 p
= xpm_cache_color (f
, color_name
, color
, h
);
3244 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3245 with transparency, and it's useful. */
3246 else if (strcmp ("opaque", color_name
) == 0)
3248 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3249 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3250 p
= xpm_cache_color (f
, color_name
, color
, h
);
3257 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3258 CLOSURE is a pointer to the frame on which we allocate the
3259 color. Return in *COLOR the allocated color. Value is non-zero
3263 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3266 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3270 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3271 is a pointer to the frame on which we allocate the color. Value is
3272 non-zero if successful. */
3275 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3280 #endif /* ALLOC_XPM_COLORS */
3285 /* XPM library details. */
3287 DEF_IMGLIB_FN (XpmFreeAttributes
);
3288 DEF_IMGLIB_FN (XpmCreateImageFromBuffer
);
3289 DEF_IMGLIB_FN (XpmReadFileToImage
);
3290 DEF_IMGLIB_FN (XImageFree
);
3293 init_xpm_functions (Lisp_Object libraries
)
3297 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3300 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3301 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3302 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3303 LOAD_IMGLIB_FN (library
, XImageFree
);
3307 #endif /* HAVE_NTGUI */
3310 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3311 for XPM images. Such a list must consist of conses whose car and
3315 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3317 while (CONSP (color_symbols
))
3319 Lisp_Object sym
= XCAR (color_symbols
);
3321 || !STRINGP (XCAR (sym
))
3322 || !STRINGP (XCDR (sym
)))
3324 color_symbols
= XCDR (color_symbols
);
3327 return NILP (color_symbols
);
3331 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3334 xpm_image_p (Lisp_Object object
)
3336 struct image_keyword fmt
[XPM_LAST
];
3337 memcpy (fmt
, xpm_format
, sizeof fmt
);
3338 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3339 /* Either `:file' or `:data' must be present. */
3340 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3341 /* Either no `:color-symbols' or it's a list of conses
3342 whose car and cdr are strings. */
3343 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3344 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3347 #endif /* HAVE_XPM || HAVE_NS */
3349 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
3351 x_create_bitmap_from_xpm_data (struct frame
*f
, char **bits
)
3353 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3355 XpmAttributes attrs
;
3356 Pixmap bitmap
, mask
;
3358 memset (&attrs
, 0, sizeof attrs
);
3360 attrs
.visual
= FRAME_X_VISUAL (f
);
3361 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3362 attrs
.valuemask
|= XpmVisual
;
3363 attrs
.valuemask
|= XpmColormap
;
3365 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3366 bits
, &bitmap
, &mask
, &attrs
);
3367 if (rc
!= XpmSuccess
)
3369 XpmFreeAttributes (&attrs
);
3373 id
= x_allocate_bitmap_record (f
);
3374 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3375 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3376 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3377 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3378 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3379 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3380 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3381 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3383 XpmFreeAttributes (&attrs
);
3386 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3388 /* Load image IMG which will be displayed on frame F. Value is
3389 non-zero if successful. */
3394 xpm_load (struct frame
*f
, struct image
*img
)
3397 XpmAttributes attrs
;
3398 Lisp_Object specified_file
, color_symbols
;
3401 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3402 #endif /* HAVE_NTGUI */
3404 /* Configure the XPM lib. Use the visual of frame F. Allocate
3405 close colors. Return colors allocated. */
3406 memset (&attrs
, 0, sizeof attrs
);
3409 attrs
.visual
= FRAME_X_VISUAL (f
);
3410 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3411 attrs
.valuemask
|= XpmVisual
;
3412 attrs
.valuemask
|= XpmColormap
;
3413 #endif /* HAVE_NTGUI */
3415 #ifdef ALLOC_XPM_COLORS
3416 /* Allocate colors with our own functions which handle
3417 failing color allocation more gracefully. */
3418 attrs
.color_closure
= f
;
3419 attrs
.alloc_color
= xpm_alloc_color
;
3420 attrs
.free_colors
= xpm_free_colors
;
3421 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3422 #else /* not ALLOC_XPM_COLORS */
3423 /* Let the XPM lib allocate colors. */
3424 attrs
.valuemask
|= XpmReturnAllocPixels
;
3425 #ifdef XpmAllocCloseColors
3426 attrs
.alloc_close_colors
= 1;
3427 attrs
.valuemask
|= XpmAllocCloseColors
;
3428 #else /* not XpmAllocCloseColors */
3429 attrs
.closeness
= 600;
3430 attrs
.valuemask
|= XpmCloseness
;
3431 #endif /* not XpmAllocCloseColors */
3432 #endif /* ALLOC_XPM_COLORS */
3434 /* If image specification contains symbolic color definitions, add
3435 these to `attrs'. */
3436 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3437 if (CONSP (color_symbols
))
3440 XpmColorSymbol
*xpm_syms
;
3443 attrs
.valuemask
|= XpmColorSymbols
;
3445 /* Count number of symbols. */
3446 attrs
.numsymbols
= 0;
3447 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3450 /* Allocate an XpmColorSymbol array. */
3451 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3452 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3453 memset (xpm_syms
, 0, size
);
3454 attrs
.colorsymbols
= xpm_syms
;
3456 /* Fill the color symbol array. */
3457 for (tail
= color_symbols
, i
= 0;
3459 ++i
, tail
= XCDR (tail
))
3461 Lisp_Object name
= XCAR (XCAR (tail
));
3462 Lisp_Object color
= XCDR (XCAR (tail
));
3463 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3464 strcpy (xpm_syms
[i
].name
, SDATA (name
));
3465 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3466 strcpy (xpm_syms
[i
].value
, SDATA (color
));
3470 /* Create a pixmap for the image, either from a file, or from a
3471 string buffer containing data in the same format as an XPM file. */
3472 #ifdef ALLOC_XPM_COLORS
3473 xpm_init_color_cache (f
, &attrs
);
3476 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3480 HDC frame_dc
= get_frame_dc (f
);
3481 hdc
= CreateCompatibleDC (frame_dc
);
3482 release_frame_dc (f
, frame_dc
);
3484 #endif /* HAVE_NTGUI */
3486 if (STRINGP (specified_file
))
3488 Lisp_Object file
= x_find_image_file (specified_file
);
3489 if (!STRINGP (file
))
3491 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3496 /* XpmReadFileToPixmap is not available in the Windows port of
3497 libxpm. But XpmReadFileToImage almost does what we want. */
3498 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3499 &xpm_image
, &xpm_mask
,
3502 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3503 SDATA (file
), &img
->pixmap
, &img
->mask
,
3505 #endif /* HAVE_NTGUI */
3509 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3511 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3512 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3513 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3514 &xpm_image
, &xpm_mask
,
3517 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3519 &img
->pixmap
, &img
->mask
,
3521 #endif /* HAVE_NTGUI */
3524 if (rc
== XpmSuccess
)
3526 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3527 img
->colors
= colors_in_color_table (&img
->ncolors
);
3528 #else /* not ALLOC_XPM_COLORS */
3532 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3533 plus some duplicate attributes. */
3534 if (xpm_image
&& xpm_image
->bitmap
)
3536 img
->pixmap
= xpm_image
->bitmap
;
3537 /* XImageFree in libXpm frees XImage struct without destroying
3538 the bitmap, which is what we want. */
3539 fn_XImageFree (xpm_image
);
3541 if (xpm_mask
&& xpm_mask
->bitmap
)
3543 /* The mask appears to be inverted compared with what we expect.
3544 TODO: invert our expectations. See other places where we
3545 have to invert bits because our idea of masks is backwards. */
3547 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3549 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3550 SelectObject (hdc
, old_obj
);
3552 img
->mask
= xpm_mask
->bitmap
;
3553 fn_XImageFree (xpm_mask
);
3558 #endif /* HAVE_NTGUI */
3560 /* Remember allocated colors. */
3561 img
->ncolors
= attrs
.nalloc_pixels
;
3562 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
3563 * sizeof *img
->colors
);
3564 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3566 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3567 #ifdef DEBUG_X_COLORS
3568 register_color (img
->colors
[i
]);
3571 #endif /* not ALLOC_XPM_COLORS */
3573 img
->width
= attrs
.width
;
3574 img
->height
= attrs
.height
;
3575 xassert (img
->width
> 0 && img
->height
> 0);
3577 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3579 fn_XpmFreeAttributes (&attrs
);
3581 XpmFreeAttributes (&attrs
);
3582 #endif /* HAVE_NTGUI */
3588 #endif /* HAVE_NTGUI */
3593 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3596 case XpmFileInvalid
:
3597 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3601 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3604 case XpmColorFailed
:
3605 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3609 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3614 #ifdef ALLOC_XPM_COLORS
3615 xpm_free_color_cache ();
3617 return rc
== XpmSuccess
;
3620 #endif /* HAVE_XPM */
3622 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3624 /* XPM support functions for NS where libxpm is not available.
3625 Only XPM version 3 (without any extensions) is supported. */
3627 static int xpm_scan (const unsigned char **, const unsigned char *,
3628 const unsigned char **, int *);
3629 static Lisp_Object xpm_make_color_table_v
3630 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3631 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3632 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3634 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3635 const unsigned char *, int);
3636 static Lisp_Object xpm_make_color_table_h
3637 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3638 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3639 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3641 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3642 const unsigned char *, int);
3643 static int xpm_str_to_color_key (const char *);
3644 static int xpm_load_image (struct frame
*, struct image
*,
3645 const unsigned char *, const unsigned char *);
3647 /* Tokens returned from xpm_scan. */
3656 /* Scan an XPM data and return a character (< 256) or a token defined
3657 by enum xpm_token above. *S and END are the start (inclusive) and
3658 the end (exclusive) addresses of the data, respectively. Advance
3659 *S while scanning. If token is either XPM_TK_IDENT or
3660 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3661 length of the corresponding token, respectively. */
3664 xpm_scan (s
, end
, beg
, len
)
3665 const unsigned char **s
, *end
, **beg
;
3672 /* Skip white-space. */
3673 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3676 /* gnus-pointer.xpm uses '-' in its identifier.
3677 sb-dir-plus.xpm uses '+' in its identifier. */
3678 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3682 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3685 return XPM_TK_IDENT
;
3690 while (*s
< end
&& **s
!= '"')
3695 return XPM_TK_STRING
;
3699 if (*s
< end
&& **s
== '*')
3701 /* C-style comment. */
3705 while (*s
< end
&& *(*s
)++ != '*')
3708 while (*s
< end
&& **s
!= '/');
3722 /* Functions for color table lookup in XPM data. A key is a string
3723 specifying the color of each pixel in XPM data. A value is either
3724 an integer that specifies a pixel color, Qt that specifies
3725 transparency, or Qnil for the unspecified color. If the length of
3726 the key string is one, a vector is used as a table. Otherwise, a
3727 hash table is used. */
3730 xpm_make_color_table_v (put_func
, get_func
)
3731 void (**put_func
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3732 Lisp_Object (**get_func
) (Lisp_Object
, const unsigned char *, int);
3734 *put_func
= xpm_put_color_table_v
;
3735 *get_func
= xpm_get_color_table_v
;
3736 return Fmake_vector (make_number (256), Qnil
);
3740 xpm_put_color_table_v (color_table
, chars_start
, chars_len
, color
)
3741 Lisp_Object color_table
;
3742 const unsigned char *chars_start
;
3746 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3750 xpm_get_color_table_v (color_table
, chars_start
, chars_len
)
3751 Lisp_Object color_table
;
3752 const unsigned char *chars_start
;
3755 return XVECTOR (color_table
)->contents
[*chars_start
];
3759 xpm_make_color_table_h (put_func
, get_func
)
3760 void (**put_func
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3761 Lisp_Object (**get_func
) (Lisp_Object
, const unsigned char *, int);
3763 *put_func
= xpm_put_color_table_h
;
3764 *get_func
= xpm_get_color_table_h
;
3765 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3766 make_float (DEFAULT_REHASH_SIZE
),
3767 make_float (DEFAULT_REHASH_THRESHOLD
),
3772 xpm_put_color_table_h (color_table
, chars_start
, chars_len
, color
)
3773 Lisp_Object color_table
;
3774 const unsigned char *chars_start
;
3778 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3780 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3782 hash_lookup (table
, chars
, &hash_code
);
3783 hash_put (table
, chars
, color
, hash_code
);
3787 xpm_get_color_table_h (color_table
, chars_start
, chars_len
)
3788 Lisp_Object color_table
;
3789 const unsigned char *chars_start
;
3792 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3793 int i
= hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
),
3796 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3799 enum xpm_color_key
{
3807 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3810 xpm_str_to_color_key (s
)
3816 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3818 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3824 xpm_load_image (f
, img
, contents
, end
)
3827 const unsigned char *contents
, *end
;
3829 const unsigned char *s
= contents
, *beg
, *str
;
3830 unsigned char buffer
[BUFSIZ
];
3831 int width
, height
, x
, y
;
3832 int num_colors
, chars_per_pixel
;
3834 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3835 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3836 Lisp_Object frame
, color_symbols
, color_table
;
3837 int best_key
, have_mask
= 0;
3838 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3841 LA1 = xpm_scan (&s, end, &beg, &len)
3843 #define expect(TOKEN) \
3844 if (LA1 != (TOKEN)) \
3849 #define expect_ident(IDENT) \
3850 if (LA1 == XPM_TK_IDENT \
3851 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3856 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3860 expect_ident ("static");
3861 expect_ident ("char");
3863 expect (XPM_TK_IDENT
);
3868 expect (XPM_TK_STRING
);
3871 memcpy (buffer
, beg
, len
);
3873 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3874 &num_colors
, &chars_per_pixel
) != 4
3875 || width
<= 0 || height
<= 0
3876 || num_colors
<= 0 || chars_per_pixel
<= 0)
3879 if (!check_image_size (f
, width
, height
))
3881 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3887 XSETFRAME (frame
, f
);
3888 if (!NILP (Fxw_display_color_p (frame
)))
3889 best_key
= XPM_COLOR_KEY_C
;
3890 else if (!NILP (Fx_display_grayscale_p (frame
)))
3891 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3892 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3894 best_key
= XPM_COLOR_KEY_M
;
3896 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3897 if (chars_per_pixel
== 1)
3898 color_table
= xpm_make_color_table_v (&put_color_table
,
3901 color_table
= xpm_make_color_table_h (&put_color_table
,
3904 while (num_colors
-- > 0)
3906 unsigned char *color
, *max_color
;
3907 int key
, next_key
, max_key
= 0;
3908 Lisp_Object symbol_color
= Qnil
, color_val
;
3911 expect (XPM_TK_STRING
);
3912 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3914 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3915 buffer
[len
- chars_per_pixel
] = '\0';
3917 str
= strtok (buffer
, " \t");
3920 key
= xpm_str_to_color_key (str
);
3925 color
= strtok (NULL
, " \t");
3929 while ((str
= strtok (NULL
, " \t")) != NULL
)
3931 next_key
= xpm_str_to_color_key (str
);
3934 color
[strlen (color
)] = ' ';
3937 if (key
== XPM_COLOR_KEY_S
)
3939 if (NILP (symbol_color
))
3940 symbol_color
= build_string (color
);
3942 else if (max_key
< key
&& key
<= best_key
)
3952 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3954 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3956 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3958 if (xstrcasecmp (SDATA (XCDR (specified_color
)), "None") == 0)
3960 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3962 color_val
= make_number (cdef
.pixel
);
3965 if (NILP (color_val
) && max_key
> 0)
3967 if (xstrcasecmp (max_color
, "None") == 0)
3969 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3970 color_val
= make_number (cdef
.pixel
);
3972 if (!NILP (color_val
))
3973 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3978 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3979 &ximg
, &img
->pixmap
)
3981 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3982 &mask_img
, &img
->mask
)
3986 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3990 for (y
= 0; y
< height
; y
++)
3992 expect (XPM_TK_STRING
);
3994 if (len
< width
* chars_per_pixel
)
3996 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3998 Lisp_Object color_val
=
3999 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4001 XPutPixel (ximg
, x
, y
,
4002 (INTEGERP (color_val
) ? XINT (color_val
)
4003 : FRAME_FOREGROUND_PIXEL (f
)));
4005 XPutPixel (mask_img
, x
, y
,
4006 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4007 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4009 if (EQ (color_val
, Qt
))
4010 ns_set_alpha (ximg
, x
, y
, 0);
4018 img
->height
= height
;
4020 /* Maybe fill in the background field while we have ximg handy. */
4021 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4022 IMAGE_BACKGROUND (img
, f
, ximg
);
4024 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4025 x_destroy_x_image (ximg
);
4029 /* Fill in the background_transparent field while we have the
4031 image_background_transparent (img
, f
, mask_img
);
4033 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4034 x_destroy_x_image (mask_img
);
4038 x_destroy_x_image (mask_img
);
4039 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4040 img
->mask
= NO_PIXMAP
;
4046 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4048 x_destroy_x_image (ximg
);
4049 x_destroy_x_image (mask_img
);
4050 x_clear_image (f
, img
);
4064 Lisp_Object file_name
;
4066 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4067 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4068 if (STRINGP (file_name
))
4071 unsigned char *contents
;
4073 struct gcpro gcpro1
;
4075 file
= x_find_image_file (file_name
);
4077 if (!STRINGP (file
))
4079 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4084 contents
= slurp_file (SDATA (file
), &size
);
4085 if (contents
== NULL
)
4087 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4092 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4100 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4101 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4102 SDATA (data
) + SBYTES (data
));
4108 #endif /* HAVE_NS && !HAVE_XPM */
4112 /***********************************************************************
4114 ***********************************************************************/
4116 #ifdef COLOR_TABLE_SUPPORT
4118 /* An entry in the color table mapping an RGB color to a pixel color. */
4123 unsigned long pixel
;
4125 /* Next in color table collision list. */
4126 struct ct_color
*next
;
4129 /* The bucket vector size to use. Must be prime. */
4133 /* Value is a hash of the RGB color given by R, G, and B. */
4135 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4137 /* The color hash table. */
4139 struct ct_color
**ct_table
;
4141 /* Number of entries in the color table. */
4143 int ct_colors_allocated
;
4145 /* Initialize the color table. */
4148 init_color_table (void)
4150 int size
= CT_SIZE
* sizeof (*ct_table
);
4151 ct_table
= (struct ct_color
**) xmalloc (size
);
4152 memset (ct_table
, 0, size
);
4153 ct_colors_allocated
= 0;
4157 /* Free memory associated with the color table. */
4160 free_color_table (void)
4163 struct ct_color
*p
, *next
;
4165 for (i
= 0; i
< CT_SIZE
; ++i
)
4166 for (p
= ct_table
[i
]; p
; p
= next
)
4177 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4178 entry for that color already is in the color table, return the
4179 pixel color of that entry. Otherwise, allocate a new color for R,
4180 G, B, and make an entry in the color table. */
4182 static unsigned long
4183 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4185 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4186 int i
= hash
% CT_SIZE
;
4188 Display_Info
*dpyinfo
;
4190 /* Handle TrueColor visuals specially, which improves performance by
4191 two orders of magnitude. Freeing colors on TrueColor visuals is
4192 a nop, and pixel colors specify RGB values directly. See also
4193 the Xlib spec, chapter 3.1. */
4194 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4195 if (dpyinfo
->red_bits
> 0)
4197 unsigned long pr
, pg
, pb
;
4199 /* Apply gamma-correction like normal color allocation does. */
4203 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4204 gamma_correct (f
, &color
);
4205 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4208 /* Scale down RGB values to the visual's bits per RGB, and shift
4209 them to the right position in the pixel color. Note that the
4210 original RGB values are 16-bit values, as usual in X. */
4211 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4212 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4213 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4215 /* Assemble the pixel color. */
4216 return pr
| pg
| pb
;
4219 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4220 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4226 #ifdef HAVE_X_WINDOWS
4235 cmap
= FRAME_X_COLORMAP (f
);
4236 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4239 ++ct_colors_allocated
;
4240 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4244 p
->pixel
= color
.pixel
;
4245 p
->next
= ct_table
[i
];
4249 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
= (struct ct_color
*) 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 #ifdef HAVE_X_WINDOWS
4294 cmap
= FRAME_X_COLORMAP (f
);
4295 color
.pixel
= pixel
;
4296 x_query_color (f
, &color
);
4297 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4300 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4301 color
.pixel
= pixel
;
4302 XQueryColor (NULL
, cmap
, &color
);
4303 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4305 #endif /* HAVE_X_WINDOWS */
4309 ++ct_colors_allocated
;
4311 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4316 p
->next
= ct_table
[i
];
4320 return FRAME_FOREGROUND_PIXEL (f
);
4326 /* Value is a vector of all pixel colors contained in the color table,
4327 allocated via xmalloc. Set *N to the number of colors. */
4329 static unsigned long *
4330 colors_in_color_table (int *n
)
4334 unsigned long *colors
;
4336 if (ct_colors_allocated
== 0)
4343 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4345 *n
= ct_colors_allocated
;
4347 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4348 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4349 colors
[j
++] = p
->pixel
;
4355 #else /* COLOR_TABLE_SUPPORT */
4357 static unsigned long
4358 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4360 unsigned long pixel
;
4363 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4364 #endif /* HAVE_NTGUI */
4367 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4368 #endif /* HAVE_NS */
4373 init_color_table (void)
4376 #endif /* COLOR_TABLE_SUPPORT */
4379 /***********************************************************************
4381 ***********************************************************************/
4383 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4384 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4385 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4388 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4389 #endif /* HAVE_NTGUI */
4391 /* Non-zero means draw a cross on images having `:conversion
4394 int cross_disabled_images
;
4396 /* Edge detection matrices for different edge-detection
4399 static int emboss_matrix
[9] = {
4401 2, -1, 0, /* y - 1 */
4403 0, 1, -2 /* y + 1 */
4406 static int laplace_matrix
[9] = {
4408 1, 0, 0, /* y - 1 */
4410 0, 0, -1 /* y + 1 */
4413 /* Value is the intensity of the color whose red/green/blue values
4416 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4419 /* On frame F, return an array of XColor structures describing image
4420 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4421 non-zero means also fill the red/green/blue members of the XColor
4422 structures. Value is a pointer to the array of XColors structures,
4423 allocated with xmalloc; it must be freed by the caller. */
4426 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4430 XImagePtr_or_DC ximg
;
4434 #endif /* HAVE_NTGUI */
4436 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
4439 /* Get the X image IMG->pixmap. */
4440 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4441 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4443 /* Load the image into a memory device context. */
4444 hdc
= get_frame_dc (f
);
4445 ximg
= CreateCompatibleDC (hdc
);
4446 release_frame_dc (f
, hdc
);
4447 prev
= SelectObject (ximg
, img
->pixmap
);
4448 #endif /* HAVE_NTGUI */
4450 /* Fill the `pixel' members of the XColor array. I wished there
4451 were an easy and portable way to circumvent XGetPixel. */
4453 for (y
= 0; y
< img
->height
; ++y
)
4457 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4458 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4459 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4461 x_query_colors (f
, row
, img
->width
);
4465 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4467 /* W32_TODO: palette support needed here? */
4468 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4471 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4472 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4473 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4476 #endif /* HAVE_X_WINDOWS */
4479 Destroy_Image (ximg
, prev
);
4486 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4487 created with CreateDIBSection, with the pointer to the bit values
4488 stored in ximg->data. */
4491 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4493 int width
= ximg
->info
.bmiHeader
.biWidth
;
4494 int height
= ximg
->info
.bmiHeader
.biHeight
;
4495 unsigned char * pixel
;
4497 /* True color images. */
4498 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4500 int rowbytes
= width
* 3;
4501 /* Ensure scanlines are aligned on 4 byte boundaries. */
4503 rowbytes
+= 4 - (rowbytes
% 4);
4505 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4506 /* Windows bitmaps are in BGR order. */
4507 *pixel
= GetBValue (color
);
4508 *(pixel
+ 1) = GetGValue (color
);
4509 *(pixel
+ 2) = GetRValue (color
);
4511 /* Monochrome images. */
4512 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4514 int rowbytes
= width
/ 8;
4515 /* Ensure scanlines are aligned on 4 byte boundaries. */
4517 rowbytes
+= 4 - (rowbytes
% 4);
4518 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4519 /* Filter out palette info. */
4520 if (color
& 0x00ffffff)
4521 *pixel
= *pixel
| (1 << x
% 8);
4523 *pixel
= *pixel
& ~(1 << x
% 8);
4526 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4529 #endif /* HAVE_NTGUI */
4531 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4532 RGB members are set. F is the frame on which this all happens.
4533 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4536 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4539 XImagePtr oimg
= NULL
;
4543 init_color_table ();
4545 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4548 for (y
= 0; y
< img
->height
; ++y
)
4549 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4551 unsigned long pixel
;
4552 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4553 XPutPixel (oimg
, x
, y
, pixel
);
4557 x_clear_image_1 (f
, img
, 1, 0, 1);
4559 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4560 x_destroy_x_image (oimg
);
4561 img
->pixmap
= pixmap
;
4562 #ifdef COLOR_TABLE_SUPPORT
4563 img
->colors
= colors_in_color_table (&img
->ncolors
);
4564 free_color_table ();
4565 #endif /* COLOR_TABLE_SUPPORT */
4569 /* On frame F, perform edge-detection on image IMG.
4571 MATRIX is a nine-element array specifying the transformation
4572 matrix. See emboss_matrix for an example.
4574 COLOR_ADJUST is a color adjustment added to each pixel of the
4578 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4580 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4584 for (i
= sum
= 0; i
< 9; ++i
)
4585 sum
+= eabs (matrix
[i
]);
4587 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4589 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
4591 for (y
= 0; y
< img
->height
; ++y
)
4593 p
= COLOR (new, 0, y
);
4594 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4595 p
= COLOR (new, img
->width
- 1, y
);
4596 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4599 for (x
= 1; x
< img
->width
- 1; ++x
)
4601 p
= COLOR (new, x
, 0);
4602 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4603 p
= COLOR (new, x
, img
->height
- 1);
4604 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4607 for (y
= 1; y
< img
->height
- 1; ++y
)
4609 p
= COLOR (new, 1, y
);
4611 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4613 int r
, g
, b
, y1
, x1
;
4616 for (y1
= y
- 1; y1
< y
+ 2; ++y1
)
4617 for (x1
= x
- 1; x1
< x
+ 2; ++x1
, ++i
)
4620 XColor
*t
= COLOR (colors
, x1
, y1
);
4621 r
+= matrix
[i
] * t
->red
;
4622 g
+= matrix
[i
] * t
->green
;
4623 b
+= matrix
[i
] * t
->blue
;
4626 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4627 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4628 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4629 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4634 x_from_xcolors (f
, img
, new);
4640 /* Perform the pre-defined `emboss' edge-detection on image IMG
4644 x_emboss (struct frame
*f
, struct image
*img
)
4646 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4650 /* Transform image IMG which is used on frame F with a Laplace
4651 edge-detection algorithm. The result is an image that can be used
4652 to draw disabled buttons, for example. */
4655 x_laplace (struct frame
*f
, struct image
*img
)
4657 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4661 /* Perform edge-detection on image IMG on frame F, with specified
4662 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4664 MATRIX must be either
4666 - a list of at least 9 numbers in row-major form
4667 - a vector of at least 9 numbers
4669 COLOR_ADJUST nil means use a default; otherwise it must be a
4673 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4674 Lisp_Object color_adjust
)
4682 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4683 ++i
, matrix
= XCDR (matrix
))
4684 trans
[i
] = XFLOATINT (XCAR (matrix
));
4686 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4688 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4689 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4692 if (NILP (color_adjust
))
4693 color_adjust
= make_number (0xffff / 2);
4695 if (i
== 9 && NUMBERP (color_adjust
))
4696 x_detect_edges (f
, img
, trans
, (int) XFLOATINT (color_adjust
));
4700 /* Transform image IMG on frame F so that it looks disabled. */
4703 x_disable_image (struct frame
*f
, struct image
*img
)
4705 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4707 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4709 int n_planes
= dpyinfo
->n_planes
;
4710 #endif /* HAVE_NTGUI */
4714 /* Color (or grayscale). Convert to gray, and equalize. Just
4715 drawing such images with a stipple can look very odd, so
4716 we're using this method instead. */
4717 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4719 const int h
= 15000;
4720 const int l
= 30000;
4722 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4726 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4727 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4728 p
->red
= p
->green
= p
->blue
= i2
;
4731 x_from_xcolors (f
, img
, colors
);
4734 /* Draw a cross over the disabled image, if we must or if we
4736 if (n_planes
< 2 || cross_disabled_images
)
4739 Display
*dpy
= FRAME_X_DISPLAY (f
);
4742 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4744 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4746 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4747 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4748 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4749 img
->width
- 1, img
->height
- 1);
4750 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4756 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4757 XSetForeground (dpy
, gc
, MaskForeground (f
));
4758 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4759 img
->width
- 1, img
->height
- 1);
4760 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4764 #endif /* !HAVE_NS */
4769 hdc
= get_frame_dc (f
);
4770 bmpdc
= CreateCompatibleDC (hdc
);
4771 release_frame_dc (f
, hdc
);
4773 prev
= SelectObject (bmpdc
, img
->pixmap
);
4775 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4776 MoveToEx (bmpdc
, 0, 0, NULL
);
4777 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4778 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4779 LineTo (bmpdc
, img
->width
- 1, 0);
4783 SelectObject (bmpdc
, img
->mask
);
4784 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4785 MoveToEx (bmpdc
, 0, 0, NULL
);
4786 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4787 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4788 LineTo (bmpdc
, img
->width
- 1, 0);
4790 SelectObject (bmpdc
, prev
);
4792 #endif /* HAVE_NTGUI */
4797 /* Build a mask for image IMG which is used on frame F. FILE is the
4798 name of an image file, for error messages. HOW determines how to
4799 determine the background color of IMG. If it is a list '(R G B)',
4800 with R, G, and B being integers >= 0, take that as the color of the
4801 background. Otherwise, determine the background color of IMG
4802 heuristically. Value is non-zero if successful. */
4805 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4807 XImagePtr_or_DC ximg
;
4815 #endif /* HAVE_NTGUI */
4816 int x
, y
, rc
, use_img_background
;
4817 unsigned long bg
= 0;
4821 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4822 img
->mask
= NO_PIXMAP
;
4823 img
->background_transparent_valid
= 0;
4828 /* Create an image and pixmap serving as mask. */
4829 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4830 &mask_img
, &img
->mask
);
4833 #endif /* !HAVE_NS */
4835 /* Get the X image of IMG->pixmap. */
4836 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4837 img
->width
, img
->height
,
4840 /* Create the bit array serving as mask. */
4841 row_width
= (img
->width
+ 7) / 8;
4842 mask_img
= xmalloc (row_width
* img
->height
);
4843 memset (mask_img
, 0, row_width
* img
->height
);
4845 /* Create a memory device context for IMG->pixmap. */
4846 frame_dc
= get_frame_dc (f
);
4847 ximg
= CreateCompatibleDC (frame_dc
);
4848 release_frame_dc (f
, frame_dc
);
4849 prev
= SelectObject (ximg
, img
->pixmap
);
4850 #endif /* HAVE_NTGUI */
4852 /* Determine the background color of ximg. If HOW is `(R G B)'
4853 take that as color. Otherwise, use the image's background color. */
4854 use_img_background
= 1;
4860 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4862 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4866 if (i
== 3 && NILP (how
))
4868 char color_name
[30];
4869 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4872 0x00ffffff & /* Filter out palette info. */
4873 #endif /* HAVE_NTGUI */
4874 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4875 use_img_background
= 0;
4879 if (use_img_background
)
4880 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4882 /* Set all bits in mask_img to 1 whose color in ximg is different
4883 from the background color bg. */
4885 for (y
= 0; y
< img
->height
; ++y
)
4886 for (x
= 0; x
< img
->width
; ++x
)
4888 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4889 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4891 if (XGetPixel (ximg
, x
, y
) == bg
)
4892 ns_set_alpha (ximg
, x
, y
, 0);
4893 #endif /* HAVE_NS */
4895 /* Fill in the background_transparent field while we have the mask handy. */
4896 image_background_transparent (img
, f
, mask_img
);
4898 /* Put mask_img into img->mask. */
4899 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4900 x_destroy_x_image (mask_img
);
4901 #endif /* !HAVE_NS */
4903 for (y
= 0; y
< img
->height
; ++y
)
4904 for (x
= 0; x
< img
->width
; ++x
)
4906 COLORREF p
= GetPixel (ximg
, x
, y
);
4908 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4911 /* Create the mask image. */
4912 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4914 /* Fill in the background_transparent field while we have the mask handy. */
4915 SelectObject (ximg
, img
->mask
);
4916 image_background_transparent (img
, f
, ximg
);
4918 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4920 #endif /* HAVE_NTGUI */
4922 Destroy_Image (ximg
, prev
);
4928 /***********************************************************************
4929 PBM (mono, gray, color)
4930 ***********************************************************************/
4932 static int pbm_image_p (Lisp_Object object
);
4933 static int pbm_load (struct frame
*f
, struct image
*img
);
4934 static int pbm_scan_number (unsigned char **, unsigned char *);
4936 /* The symbol `pbm' identifying images of this type. */
4940 /* Indices of image specification fields in gs_format, below. */
4942 enum pbm_keyword_index
4958 /* Vector of image_keyword structures describing the format
4959 of valid user-defined image specifications. */
4961 static const struct image_keyword pbm_format
[PBM_LAST
] =
4963 {":type", IMAGE_SYMBOL_VALUE
, 1},
4964 {":file", IMAGE_STRING_VALUE
, 0},
4965 {":data", IMAGE_STRING_VALUE
, 0},
4966 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4967 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
4968 {":relief", IMAGE_INTEGER_VALUE
, 0},
4969 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4970 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4971 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4972 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4973 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4976 /* Structure describing the image type `pbm'. */
4978 static struct image_type pbm_type
=
4988 /* Return non-zero if OBJECT is a valid PBM image specification. */
4991 pbm_image_p (Lisp_Object object
)
4993 struct image_keyword fmt
[PBM_LAST
];
4995 memcpy (fmt
, pbm_format
, sizeof fmt
);
4997 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5000 /* Must specify either :data or :file. */
5001 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5005 /* Scan a decimal number from *S and return it. Advance *S while
5006 reading the number. END is the end of the string. Value is -1 at
5010 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5012 int c
= 0, val
= -1;
5016 /* Skip white-space. */
5017 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5022 /* Skip comment to end of line. */
5023 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5026 else if (isdigit (c
))
5028 /* Read decimal number. */
5030 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5031 val
= 10 * val
+ c
- '0';
5043 #if 0 /* Unused. ++kfs */
5045 /* Read FILE into memory. Value is a pointer to a buffer allocated
5046 with xmalloc holding FILE's contents. Value is null if an error
5047 occurred. *SIZE is set to the size of the file. */
5050 pbm_read_file (file
, size
)
5058 if (stat (SDATA (file
), &st
) == 0
5059 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5060 && (buf
= (char *) xmalloc (st
.st_size
),
5061 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5080 #endif /* HAVE_NTGUI */
5082 /* Load PBM image IMG for use on frame F. */
5085 pbm_load (struct frame
*f
, struct image
*img
)
5088 int width
, height
, max_color_idx
= 0;
5090 Lisp_Object file
, specified_file
;
5091 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5092 struct gcpro gcpro1
;
5093 unsigned char *contents
= NULL
;
5094 unsigned char *end
, *p
;
5097 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5101 if (STRINGP (specified_file
))
5103 file
= x_find_image_file (specified_file
);
5104 if (!STRINGP (file
))
5106 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5111 contents
= slurp_file (SDATA (file
), &size
);
5112 if (contents
== NULL
)
5114 image_error ("Error reading `%s'", file
, Qnil
);
5120 end
= contents
+ size
;
5125 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5127 end
= p
+ SBYTES (data
);
5130 /* Check magic number. */
5131 if (end
- p
< 2 || *p
++ != 'P')
5133 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5143 raw_p
= 0, type
= PBM_MONO
;
5147 raw_p
= 0, type
= PBM_GRAY
;
5151 raw_p
= 0, type
= PBM_COLOR
;
5155 raw_p
= 1, type
= PBM_MONO
;
5159 raw_p
= 1, type
= PBM_GRAY
;
5163 raw_p
= 1, type
= PBM_COLOR
;
5167 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5171 /* Read width, height, maximum color-component. Characters
5172 starting with `#' up to the end of a line are ignored. */
5173 width
= pbm_scan_number (&p
, end
);
5174 height
= pbm_scan_number (&p
, end
);
5176 if (type
!= PBM_MONO
)
5178 max_color_idx
= pbm_scan_number (&p
, end
);
5179 if (max_color_idx
> 65535 || max_color_idx
< 0)
5181 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5186 if (!check_image_size (f
, width
, height
))
5188 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5192 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5193 &ximg
, &img
->pixmap
))
5196 /* Initialize the color hash table. */
5197 init_color_table ();
5199 if (type
== PBM_MONO
)
5202 struct image_keyword fmt
[PBM_LAST
];
5203 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5204 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5206 /* Parse the image specification. */
5207 memcpy (fmt
, pbm_format
, sizeof fmt
);
5208 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5210 /* Get foreground and background colors, maybe allocate colors. */
5211 if (fmt
[PBM_FOREGROUND
].count
5212 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5213 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5214 if (fmt
[PBM_BACKGROUND
].count
5215 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5217 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5218 img
->background
= bg
;
5219 img
->background_valid
= 1;
5222 for (y
= 0; y
< height
; ++y
)
5223 for (x
= 0; x
< width
; ++x
)
5231 x_destroy_x_image (ximg
);
5232 x_clear_image (f
, img
);
5233 image_error ("Invalid image size in image `%s'",
5243 g
= pbm_scan_number (&p
, end
);
5245 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5250 int expected_size
= height
* width
;
5251 if (max_color_idx
> 255)
5253 if (type
== PBM_COLOR
)
5256 if (raw_p
&& p
+ expected_size
> end
)
5258 x_destroy_x_image (ximg
);
5259 x_clear_image (f
, img
);
5260 image_error ("Invalid image size in image `%s'",
5265 for (y
= 0; y
< height
; ++y
)
5266 for (x
= 0; x
< width
; ++x
)
5270 if (type
== PBM_GRAY
&& raw_p
)
5273 if (max_color_idx
> 255)
5274 r
= g
= b
= r
* 256 + *p
++;
5276 else if (type
== PBM_GRAY
)
5277 r
= g
= b
= pbm_scan_number (&p
, end
);
5281 if (max_color_idx
> 255)
5284 if (max_color_idx
> 255)
5287 if (max_color_idx
> 255)
5292 r
= pbm_scan_number (&p
, end
);
5293 g
= pbm_scan_number (&p
, end
);
5294 b
= pbm_scan_number (&p
, end
);
5297 if (r
< 0 || g
< 0 || b
< 0)
5299 x_destroy_x_image (ximg
);
5300 image_error ("Invalid pixel value in image `%s'",
5305 /* RGB values are now in the range 0..max_color_idx.
5306 Scale this to the range 0..0xffff supported by X. */
5307 r
= (double) r
* 65535 / max_color_idx
;
5308 g
= (double) g
* 65535 / max_color_idx
;
5309 b
= (double) b
* 65535 / max_color_idx
;
5310 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5314 #ifdef COLOR_TABLE_SUPPORT
5315 /* Store in IMG->colors the colors allocated for the image, and
5316 free the color table. */
5317 img
->colors
= colors_in_color_table (&img
->ncolors
);
5318 free_color_table ();
5319 #endif /* COLOR_TABLE_SUPPORT */
5322 img
->height
= height
;
5324 /* Maybe fill in the background field while we have ximg handy. */
5326 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5327 /* Casting avoids a GCC warning. */
5328 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5330 /* Put the image into a pixmap. */
5331 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5332 x_destroy_x_image (ximg
);
5334 /* X and W32 versions did it here, MAC version above. ++kfs
5336 img->height = height; */
5344 /***********************************************************************
5346 ***********************************************************************/
5348 #if defined (HAVE_PNG) || defined (HAVE_NS)
5350 /* Function prototypes. */
5352 static int png_image_p (Lisp_Object object
);
5353 static int png_load (struct frame
*f
, struct image
*img
);
5355 /* The symbol `png' identifying images of this type. */
5359 /* Indices of image specification fields in png_format, below. */
5361 enum png_keyword_index
5376 /* Vector of image_keyword structures describing the format
5377 of valid user-defined image specifications. */
5379 static const struct image_keyword png_format
[PNG_LAST
] =
5381 {":type", IMAGE_SYMBOL_VALUE
, 1},
5382 {":data", IMAGE_STRING_VALUE
, 0},
5383 {":file", IMAGE_STRING_VALUE
, 0},
5384 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5385 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5386 {":relief", IMAGE_INTEGER_VALUE
, 0},
5387 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5388 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5389 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5390 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5393 /* Structure describing the image type `png'. */
5395 static struct image_type png_type
=
5404 /* Return non-zero if OBJECT is a valid PNG image specification. */
5407 png_image_p (Lisp_Object object
)
5409 struct image_keyword fmt
[PNG_LAST
];
5410 memcpy (fmt
, png_format
, sizeof fmt
);
5412 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5415 /* Must specify either the :data or :file keyword. */
5416 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5419 #endif /* HAVE_PNG || HAVE_NS */
5425 /* PNG library details. */
5427 DEF_IMGLIB_FN (png_get_io_ptr
);
5428 DEF_IMGLIB_FN (png_sig_cmp
);
5429 DEF_IMGLIB_FN (png_create_read_struct
);
5430 DEF_IMGLIB_FN (png_create_info_struct
);
5431 DEF_IMGLIB_FN (png_destroy_read_struct
);
5432 DEF_IMGLIB_FN (png_set_read_fn
);
5433 DEF_IMGLIB_FN (png_set_sig_bytes
);
5434 DEF_IMGLIB_FN (png_read_info
);
5435 DEF_IMGLIB_FN (png_get_IHDR
);
5436 DEF_IMGLIB_FN (png_get_valid
);
5437 DEF_IMGLIB_FN (png_set_strip_16
);
5438 DEF_IMGLIB_FN (png_set_expand
);
5439 DEF_IMGLIB_FN (png_set_gray_to_rgb
);
5440 DEF_IMGLIB_FN (png_set_background
);
5441 DEF_IMGLIB_FN (png_get_bKGD
);
5442 DEF_IMGLIB_FN (png_read_update_info
);
5443 DEF_IMGLIB_FN (png_get_channels
);
5444 DEF_IMGLIB_FN (png_get_rowbytes
);
5445 DEF_IMGLIB_FN (png_read_image
);
5446 DEF_IMGLIB_FN (png_read_end
);
5447 DEF_IMGLIB_FN (png_error
);
5450 init_png_functions (Lisp_Object libraries
)
5454 /* Try loading libpng under probable names. */
5455 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5458 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5459 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5460 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5461 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5462 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5463 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5464 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5465 LOAD_IMGLIB_FN (library
, png_read_info
);
5466 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5467 LOAD_IMGLIB_FN (library
, png_get_valid
);
5468 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5469 LOAD_IMGLIB_FN (library
, png_set_expand
);
5470 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5471 LOAD_IMGLIB_FN (library
, png_set_background
);
5472 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5473 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5474 LOAD_IMGLIB_FN (library
, png_get_channels
);
5475 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5476 LOAD_IMGLIB_FN (library
, png_read_image
);
5477 LOAD_IMGLIB_FN (library
, png_read_end
);
5478 LOAD_IMGLIB_FN (library
, png_error
);
5483 #define fn_png_get_io_ptr png_get_io_ptr
5484 #define fn_png_sig_cmp png_sig_cmp
5485 #define fn_png_create_read_struct png_create_read_struct
5486 #define fn_png_create_info_struct png_create_info_struct
5487 #define fn_png_destroy_read_struct png_destroy_read_struct
5488 #define fn_png_set_read_fn png_set_read_fn
5489 #define fn_png_set_sig_bytes png_set_sig_bytes
5490 #define fn_png_read_info png_read_info
5491 #define fn_png_get_IHDR png_get_IHDR
5492 #define fn_png_get_valid png_get_valid
5493 #define fn_png_set_strip_16 png_set_strip_16
5494 #define fn_png_set_expand png_set_expand
5495 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5496 #define fn_png_set_background png_set_background
5497 #define fn_png_get_bKGD png_get_bKGD
5498 #define fn_png_read_update_info png_read_update_info
5499 #define fn_png_get_channels png_get_channels
5500 #define fn_png_get_rowbytes png_get_rowbytes
5501 #define fn_png_read_image png_read_image
5502 #define fn_png_read_end png_read_end
5503 #define fn_png_error png_error
5505 #endif /* HAVE_NTGUI */
5507 /* Error and warning handlers installed when the PNG library
5511 my_png_error (png_struct
*png_ptr
, const char *msg
)
5513 xassert (png_ptr
!= NULL
);
5514 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5515 longjmp (png_ptr
->jmpbuf
, 1);
5520 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5522 xassert (png_ptr
!= NULL
);
5523 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5526 /* Memory source for PNG decoding. */
5528 struct png_memory_storage
5530 unsigned char *bytes
; /* The data */
5531 size_t len
; /* How big is it? */
5532 int index
; /* Where are we? */
5536 /* Function set as reader function when reading PNG image from memory.
5537 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5538 bytes from the input to DATA. */
5541 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5543 struct png_memory_storage
*tbr
5544 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5546 if (length
> tbr
->len
- tbr
->index
)
5547 fn_png_error (png_ptr
, "Read error");
5549 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5550 tbr
->index
= tbr
->index
+ length
;
5554 /* Function set as reader function when reading PNG image from a file.
5555 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5556 bytes from the input to DATA. */
5559 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5561 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5563 if (fread (data
, 1, length
, fp
) < length
)
5564 fn_png_error (png_ptr
, "Read error");
5568 /* Load PNG image IMG for use on frame F. Value is non-zero if
5572 png_load (struct frame
*f
, struct image
*img
)
5574 Lisp_Object file
, specified_file
;
5575 Lisp_Object specified_data
;
5577 XImagePtr ximg
, mask_img
= NULL
;
5578 struct gcpro gcpro1
;
5579 png_struct
*png_ptr
= NULL
;
5580 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5581 FILE *volatile fp
= NULL
;
5583 png_byte
* volatile pixels
= NULL
;
5584 png_byte
** volatile rows
= NULL
;
5585 png_uint_32 width
, height
;
5586 int bit_depth
, color_type
, interlace_type
;
5588 png_uint_32 row_bytes
;
5590 struct png_memory_storage tbr
; /* Data to be read */
5592 /* Find out what file to load. */
5593 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5594 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5598 if (NILP (specified_data
))
5600 file
= x_find_image_file (specified_file
);
5601 if (!STRINGP (file
))
5603 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5608 /* Open the image file. */
5609 fp
= fopen (SDATA (file
), "rb");
5612 image_error ("Cannot open image file `%s'", file
, Qnil
);
5617 /* Check PNG signature. */
5618 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5619 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5621 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5629 /* Read from memory. */
5630 tbr
.bytes
= SDATA (specified_data
);
5631 tbr
.len
= SBYTES (specified_data
);
5634 /* Check PNG signature. */
5635 if (tbr
.len
< sizeof sig
5636 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5638 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5643 /* Need to skip past the signature. */
5644 tbr
.bytes
+= sizeof (sig
);
5647 /* Initialize read and info structs for PNG lib. Casting return
5648 value avoids a GCC warning on W32. */
5649 png_ptr
= (png_structp
)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5654 if (fp
) fclose (fp
);
5659 /* Casting return value avoids a GCC warning on W32. */
5660 info_ptr
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5663 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5664 if (fp
) fclose (fp
);
5669 /* Casting return value avoids a GCC warning on W32. */
5670 end_info
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5673 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5674 if (fp
) fclose (fp
);
5679 /* Set error jump-back. We come back here when the PNG library
5680 detects an error. */
5681 if (setjmp (png_ptr
->jmpbuf
))
5685 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5688 if (fp
) fclose (fp
);
5693 /* Read image info. */
5694 if (!NILP (specified_data
))
5695 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5697 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5699 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5700 fn_png_read_info (png_ptr
, info_ptr
);
5701 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5702 &interlace_type
, NULL
, NULL
);
5704 if (!check_image_size (f
, width
, height
))
5706 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5709 /* If image contains simply transparency data, we prefer to
5710 construct a clipping mask. */
5711 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5716 /* This function is easier to write if we only have to handle
5717 one data format: RGB or RGBA with 8 bits per channel. Let's
5718 transform other formats into that format. */
5720 /* Strip more than 8 bits per channel. */
5721 if (bit_depth
== 16)
5722 fn_png_set_strip_16 (png_ptr
);
5724 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5726 fn_png_set_expand (png_ptr
);
5728 /* Convert grayscale images to RGB. */
5729 if (color_type
== PNG_COLOR_TYPE_GRAY
5730 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5731 fn_png_set_gray_to_rgb (png_ptr
);
5733 /* Handle alpha channel by combining the image with a background
5734 color. Do this only if a real alpha channel is supplied. For
5735 simple transparency, we prefer a clipping mask. */
5738 /* png_color_16 *image_bg; */
5739 Lisp_Object specified_bg
5740 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5741 int shift
= (bit_depth
== 16) ? 0 : 8;
5743 if (STRINGP (specified_bg
))
5744 /* The user specified `:background', use that. */
5747 if (x_defined_color (f
, SDATA (specified_bg
), &color
, 0))
5749 png_color_16 user_bg
;
5751 memset (&user_bg
, 0, sizeof user_bg
);
5752 user_bg
.red
= color
.red
>> shift
;
5753 user_bg
.green
= color
.green
>> shift
;
5754 user_bg
.blue
= color
.blue
>> shift
;
5756 fn_png_set_background (png_ptr
, &user_bg
,
5757 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5762 /* We use the current frame background, ignoring any default
5763 background color set by the image. */
5764 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5766 png_color_16 frame_background
;
5768 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5769 x_query_color (f
, &color
);
5771 memset (&frame_background
, 0, sizeof frame_background
);
5772 frame_background
.red
= color
.red
>> shift
;
5773 frame_background
.green
= color
.green
>> shift
;
5774 frame_background
.blue
= color
.blue
>> shift
;
5775 #endif /* HAVE_X_WINDOWS */
5777 fn_png_set_background (png_ptr
, &frame_background
,
5778 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5782 /* Update info structure. */
5783 fn_png_read_update_info (png_ptr
, info_ptr
);
5785 /* Get number of channels. Valid values are 1 for grayscale images
5786 and images with a palette, 2 for grayscale images with transparency
5787 information (alpha channel), 3 for RGB images, and 4 for RGB
5788 images with alpha channel, i.e. RGBA. If conversions above were
5789 sufficient we should only have 3 or 4 channels here. */
5790 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5791 xassert (channels
== 3 || channels
== 4);
5793 /* Number of bytes needed for one row of the image. */
5794 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5796 /* Allocate memory for the image. */
5797 pixels
= (png_byte
*) xmalloc (row_bytes
* height
* sizeof *pixels
);
5798 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5799 for (i
= 0; i
< height
; ++i
)
5800 rows
[i
] = pixels
+ i
* row_bytes
;
5802 /* Read the entire image. */
5803 fn_png_read_image (png_ptr
, rows
);
5804 fn_png_read_end (png_ptr
, info_ptr
);
5811 /* Create the X image and pixmap. */
5812 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5816 /* Create an image and pixmap serving as mask if the PNG image
5817 contains an alpha channel. */
5820 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5821 &mask_img
, &img
->mask
))
5823 x_destroy_x_image (ximg
);
5824 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5825 img
->pixmap
= NO_PIXMAP
;
5829 /* Fill the X image and mask from PNG data. */
5830 init_color_table ();
5832 for (y
= 0; y
< height
; ++y
)
5834 png_byte
*p
= rows
[y
];
5836 for (x
= 0; x
< width
; ++x
)
5843 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5844 /* An alpha channel, aka mask channel, associates variable
5845 transparency with an image. Where other image formats
5846 support binary transparency---fully transparent or fully
5847 opaque---PNG allows up to 254 levels of partial transparency.
5848 The PNG library implements partial transparency by combining
5849 the image with a specified background color.
5851 I'm not sure how to handle this here nicely: because the
5852 background on which the image is displayed may change, for
5853 real alpha channel support, it would be necessary to create
5854 a new image for each possible background.
5856 What I'm doing now is that a mask is created if we have
5857 boolean transparency information. Otherwise I'm using
5858 the frame's background color to combine the image with. */
5863 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5869 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5870 /* Set IMG's background color from the PNG image, unless the user
5874 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5876 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5877 img
->background_valid
= 1;
5881 #ifdef COLOR_TABLE_SUPPORT
5882 /* Remember colors allocated for this image. */
5883 img
->colors
= colors_in_color_table (&img
->ncolors
);
5884 free_color_table ();
5885 #endif /* COLOR_TABLE_SUPPORT */
5888 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5893 img
->height
= height
;
5895 /* Maybe fill in the background field while we have ximg handy.
5896 Casting avoids a GCC warning. */
5897 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5899 /* Put the image into the pixmap, then free the X image and its buffer. */
5900 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5901 x_destroy_x_image (ximg
);
5903 /* Same for the mask. */
5906 /* Fill in the background_transparent field while we have the
5907 mask handy. Casting avoids a GCC warning. */
5908 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5910 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5911 x_destroy_x_image (mask_img
);
5918 #else /* HAVE_PNG */
5922 png_load (struct frame
*f
, struct image
*img
)
5924 return ns_load_image(f
, img
,
5925 image_spec_value (img
->spec
, QCfile
, NULL
),
5926 image_spec_value (img
->spec
, QCdata
, NULL
));
5928 #endif /* HAVE_NS */
5931 #endif /* !HAVE_PNG */
5935 /***********************************************************************
5937 ***********************************************************************/
5939 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5941 static int jpeg_image_p (Lisp_Object object
);
5942 static int jpeg_load (struct frame
*f
, struct image
*img
);
5944 /* The symbol `jpeg' identifying images of this type. */
5948 /* Indices of image specification fields in gs_format, below. */
5950 enum jpeg_keyword_index
5959 JPEG_HEURISTIC_MASK
,
5965 /* Vector of image_keyword structures describing the format
5966 of valid user-defined image specifications. */
5968 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5970 {":type", IMAGE_SYMBOL_VALUE
, 1},
5971 {":data", IMAGE_STRING_VALUE
, 0},
5972 {":file", IMAGE_STRING_VALUE
, 0},
5973 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5974 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5975 {":relief", IMAGE_INTEGER_VALUE
, 0},
5976 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5977 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5978 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5979 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5982 /* Structure describing the image type `jpeg'. */
5984 static struct image_type jpeg_type
=
5993 /* Return non-zero if OBJECT is a valid JPEG image specification. */
5996 jpeg_image_p (Lisp_Object object
)
5998 struct image_keyword fmt
[JPEG_LAST
];
6000 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6002 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6005 /* Must specify either the :data or :file keyword. */
6006 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6009 #endif /* HAVE_JPEG || HAVE_NS */
6013 /* Work around a warning about HAVE_STDLIB_H being redefined in
6015 #ifdef HAVE_STDLIB_H
6016 #define HAVE_STDLIB_H_1
6017 #undef HAVE_STDLIB_H
6018 #endif /* HAVE_STLIB_H */
6020 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6021 /* In older releases of the jpeg library, jpeglib.h will define boolean
6022 differently depending on __WIN32__, so make sure it is defined. */
6026 #include <jpeglib.h>
6029 #ifdef HAVE_STLIB_H_1
6030 #define HAVE_STDLIB_H 1
6035 /* JPEG library details. */
6036 DEF_IMGLIB_FN (jpeg_CreateDecompress
);
6037 DEF_IMGLIB_FN (jpeg_start_decompress
);
6038 DEF_IMGLIB_FN (jpeg_finish_decompress
);
6039 DEF_IMGLIB_FN (jpeg_destroy_decompress
);
6040 DEF_IMGLIB_FN (jpeg_read_header
);
6041 DEF_IMGLIB_FN (jpeg_read_scanlines
);
6042 DEF_IMGLIB_FN (jpeg_std_error
);
6043 DEF_IMGLIB_FN (jpeg_resync_to_restart
);
6046 init_jpeg_functions (Lisp_Object libraries
)
6050 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6053 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6054 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6055 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6056 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6057 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6058 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6059 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6060 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6064 /* Wrapper since we can't directly assign the function pointer
6065 to another function pointer that was declared more completely easily. */
6067 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6069 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6074 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6075 #define fn_jpeg_start_decompress jpeg_start_decompress
6076 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6077 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6078 #define fn_jpeg_read_header jpeg_read_header
6079 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6080 #define fn_jpeg_std_error jpeg_std_error
6081 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6083 #endif /* HAVE_NTGUI */
6085 struct my_jpeg_error_mgr
6087 struct jpeg_error_mgr pub
;
6088 jmp_buf setjmp_buffer
;
6093 my_error_exit (j_common_ptr cinfo
)
6095 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6096 longjmp (mgr
->setjmp_buffer
, 1);
6100 /* Init source method for JPEG data source manager. Called by
6101 jpeg_read_header() before any data is actually read. See
6102 libjpeg.doc from the JPEG lib distribution. */
6105 our_common_init_source (j_decompress_ptr cinfo
)
6110 /* Method to terminate data source. Called by
6111 jpeg_finish_decompress() after all data has been processed. */
6114 our_common_term_source (j_decompress_ptr cinfo
)
6119 /* Fill input buffer method for JPEG data source manager. Called
6120 whenever more data is needed. We read the whole image in one step,
6121 so this only adds a fake end of input marker at the end. */
6123 static JOCTET our_memory_buffer
[2];
6126 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6128 /* Insert a fake EOI marker. */
6129 struct jpeg_source_mgr
*src
= cinfo
->src
;
6131 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6132 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6134 src
->next_input_byte
= our_memory_buffer
;
6135 src
->bytes_in_buffer
= 2;
6140 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6141 is the JPEG data source manager. */
6144 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6146 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6150 if (num_bytes
> src
->bytes_in_buffer
)
6151 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6153 src
->bytes_in_buffer
-= num_bytes
;
6154 src
->next_input_byte
+= num_bytes
;
6159 /* Set up the JPEG lib for reading an image from DATA which contains
6160 LEN bytes. CINFO is the decompression info structure created for
6161 reading the image. */
6164 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6166 struct jpeg_source_mgr
*src
;
6168 if (cinfo
->src
== NULL
)
6170 /* First time for this JPEG object? */
6171 cinfo
->src
= (struct jpeg_source_mgr
*)
6172 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6173 sizeof (struct jpeg_source_mgr
));
6174 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6175 src
->next_input_byte
= data
;
6178 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6179 src
->init_source
= our_common_init_source
;
6180 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6181 src
->skip_input_data
= our_memory_skip_input_data
;
6182 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6183 src
->term_source
= our_common_term_source
;
6184 src
->bytes_in_buffer
= len
;
6185 src
->next_input_byte
= data
;
6189 struct jpeg_stdio_mgr
6191 struct jpeg_source_mgr mgr
;
6198 /* Size of buffer to read JPEG from file.
6199 Not too big, as we want to use alloc_small. */
6200 #define JPEG_STDIO_BUFFER_SIZE 8192
6203 /* Fill input buffer method for JPEG data source manager. Called
6204 whenever more data is needed. The data is read from a FILE *. */
6207 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6209 struct jpeg_stdio_mgr
*src
;
6211 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6216 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6218 src
->mgr
.bytes_in_buffer
= bytes
;
6221 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6223 src
->buffer
[0] = (JOCTET
) 0xFF;
6224 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6225 src
->mgr
.bytes_in_buffer
= 2;
6227 src
->mgr
.next_input_byte
= src
->buffer
;
6234 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6235 is the JPEG data source manager. */
6238 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6240 struct jpeg_stdio_mgr
*src
;
6241 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6243 while (num_bytes
> 0 && !src
->finished
)
6245 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6247 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6248 src
->mgr
.next_input_byte
+= num_bytes
;
6253 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6254 src
->mgr
.bytes_in_buffer
= 0;
6255 src
->mgr
.next_input_byte
= NULL
;
6257 our_stdio_fill_input_buffer (cinfo
);
6263 /* Set up the JPEG lib for reading an image from a FILE *.
6264 CINFO is the decompression info structure created for
6265 reading the image. */
6268 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6270 struct jpeg_stdio_mgr
*src
;
6272 if (cinfo
->src
!= NULL
)
6273 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6276 /* First time for this JPEG object? */
6277 cinfo
->src
= (struct jpeg_source_mgr
*)
6278 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6279 sizeof (struct jpeg_stdio_mgr
));
6280 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6281 src
->buffer
= (JOCTET
*)
6282 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6283 JPEG_STDIO_BUFFER_SIZE
);
6288 src
->mgr
.init_source
= our_common_init_source
;
6289 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6290 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6291 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6292 src
->mgr
.term_source
= our_common_term_source
;
6293 src
->mgr
.bytes_in_buffer
= 0;
6294 src
->mgr
.next_input_byte
= NULL
;
6298 /* Load image IMG for use on frame F. Patterned after example.c
6299 from the JPEG lib. */
6302 jpeg_load (struct frame
*f
, struct image
*img
)
6304 struct jpeg_decompress_struct cinfo
;
6305 struct my_jpeg_error_mgr mgr
;
6306 Lisp_Object file
, specified_file
;
6307 Lisp_Object specified_data
;
6308 FILE * volatile fp
= NULL
;
6310 int row_stride
, x
, y
;
6311 XImagePtr ximg
= NULL
;
6313 unsigned long *colors
;
6315 struct gcpro gcpro1
;
6317 /* Open the JPEG file. */
6318 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6319 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6323 if (NILP (specified_data
))
6325 file
= x_find_image_file (specified_file
);
6326 if (!STRINGP (file
))
6328 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6333 fp
= fopen (SDATA (file
), "rb");
6336 image_error ("Cannot open `%s'", file
, Qnil
);
6342 /* Customize libjpeg's error handling to call my_error_exit when an
6343 error is detected. This function will perform a longjmp.
6344 Casting return value avoids a GCC warning on W32. */
6345 cinfo
.err
= (struct jpeg_error_mgr
*)fn_jpeg_std_error (&mgr
.pub
);
6346 mgr
.pub
.error_exit
= my_error_exit
;
6348 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6352 /* Called from my_error_exit. Display a JPEG error. */
6353 char buffer
[JMSG_LENGTH_MAX
];
6354 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buffer
);
6355 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6356 build_string (buffer
));
6359 /* Close the input file and destroy the JPEG object. */
6361 fclose ((FILE *) fp
);
6362 fn_jpeg_destroy_decompress (&cinfo
);
6364 /* If we already have an XImage, free that. */
6365 x_destroy_x_image (ximg
);
6367 /* Free pixmap and colors. */
6368 x_clear_image (f
, img
);
6374 /* Create the JPEG decompression object. Let it read from fp.
6375 Read the JPEG image header. */
6376 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6378 if (NILP (specified_data
))
6379 jpeg_file_src (&cinfo
, (FILE *) fp
);
6381 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6382 SBYTES (specified_data
));
6384 fn_jpeg_read_header (&cinfo
, 1);
6386 /* Customize decompression so that color quantization will be used.
6387 Start decompression. */
6388 cinfo
.quantize_colors
= 1;
6389 fn_jpeg_start_decompress (&cinfo
);
6390 width
= img
->width
= cinfo
.output_width
;
6391 height
= img
->height
= cinfo
.output_height
;
6393 if (!check_image_size (f
, width
, height
))
6395 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6396 longjmp (mgr
.setjmp_buffer
, 2);
6399 /* Create X image and pixmap. */
6400 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6401 longjmp (mgr
.setjmp_buffer
, 2);
6403 /* Allocate colors. When color quantization is used,
6404 cinfo.actual_number_of_colors has been set with the number of
6405 colors generated, and cinfo.colormap is a two-dimensional array
6406 of color indices in the range 0..cinfo.actual_number_of_colors.
6407 No more than 255 colors will be generated. */
6411 if (cinfo
.out_color_components
> 2)
6412 ir
= 0, ig
= 1, ib
= 2;
6413 else if (cinfo
.out_color_components
> 1)
6414 ir
= 0, ig
= 1, ib
= 0;
6416 ir
= 0, ig
= 0, ib
= 0;
6418 /* Use the color table mechanism because it handles colors that
6419 cannot be allocated nicely. Such colors will be replaced with
6420 a default color, and we don't have to care about which colors
6421 can be freed safely, and which can't. */
6422 init_color_table ();
6423 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6426 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6428 /* Multiply RGB values with 255 because X expects RGB values
6429 in the range 0..0xffff. */
6430 int r
= cinfo
.colormap
[ir
][i
] << 8;
6431 int g
= cinfo
.colormap
[ig
][i
] << 8;
6432 int b
= cinfo
.colormap
[ib
][i
] << 8;
6433 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6436 #ifdef COLOR_TABLE_SUPPORT
6437 /* Remember those colors actually allocated. */
6438 img
->colors
= colors_in_color_table (&img
->ncolors
);
6439 free_color_table ();
6440 #endif /* COLOR_TABLE_SUPPORT */
6444 row_stride
= width
* cinfo
.output_components
;
6445 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6447 for (y
= 0; y
< height
; ++y
)
6449 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6450 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6451 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6455 fn_jpeg_finish_decompress (&cinfo
);
6456 fn_jpeg_destroy_decompress (&cinfo
);
6458 fclose ((FILE *) fp
);
6460 /* Maybe fill in the background field while we have ximg handy. */
6461 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6462 /* Casting avoids a GCC warning. */
6463 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6465 /* Put the image into the pixmap. */
6466 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6467 x_destroy_x_image (ximg
);
6472 #else /* HAVE_JPEG */
6476 jpeg_load (struct frame
*f
, struct image
*img
)
6478 return ns_load_image (f
, img
,
6479 image_spec_value (img
->spec
, QCfile
, NULL
),
6480 image_spec_value (img
->spec
, QCdata
, NULL
));
6482 #endif /* HAVE_NS */
6484 #endif /* !HAVE_JPEG */
6488 /***********************************************************************
6490 ***********************************************************************/
6492 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6494 static int tiff_image_p (Lisp_Object object
);
6495 static int tiff_load (struct frame
*f
, struct image
*img
);
6497 /* The symbol `tiff' identifying images of this type. */
6501 /* Indices of image specification fields in tiff_format, below. */
6503 enum tiff_keyword_index
6512 TIFF_HEURISTIC_MASK
,
6519 /* Vector of image_keyword structures describing the format
6520 of valid user-defined image specifications. */
6522 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6524 {":type", IMAGE_SYMBOL_VALUE
, 1},
6525 {":data", IMAGE_STRING_VALUE
, 0},
6526 {":file", IMAGE_STRING_VALUE
, 0},
6527 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6528 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6529 {":relief", IMAGE_INTEGER_VALUE
, 0},
6530 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6531 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6532 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6533 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6534 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6537 /* Structure describing the image type `tiff'. */
6539 static struct image_type tiff_type
=
6548 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6551 tiff_image_p (Lisp_Object object
)
6553 struct image_keyword fmt
[TIFF_LAST
];
6554 memcpy (fmt
, tiff_format
, sizeof fmt
);
6556 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6559 /* Must specify either the :data or :file keyword. */
6560 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6563 #endif /* HAVE_TIFF || HAVE_NS */
6571 /* TIFF library details. */
6572 DEF_IMGLIB_FN (TIFFSetErrorHandler
);
6573 DEF_IMGLIB_FN (TIFFSetWarningHandler
);
6574 DEF_IMGLIB_FN (TIFFOpen
);
6575 DEF_IMGLIB_FN (TIFFClientOpen
);
6576 DEF_IMGLIB_FN (TIFFGetField
);
6577 DEF_IMGLIB_FN (TIFFReadRGBAImage
);
6578 DEF_IMGLIB_FN (TIFFClose
);
6579 DEF_IMGLIB_FN (TIFFSetDirectory
);
6582 init_tiff_functions (Lisp_Object libraries
)
6586 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6589 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6590 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6591 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6592 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6593 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6594 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6595 LOAD_IMGLIB_FN (library
, TIFFClose
);
6596 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6602 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6603 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6604 #define fn_TIFFOpen TIFFOpen
6605 #define fn_TIFFClientOpen TIFFClientOpen
6606 #define fn_TIFFGetField TIFFGetField
6607 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6608 #define fn_TIFFClose TIFFClose
6609 #define fn_TIFFSetDirectory TIFFSetDirectory
6610 #endif /* HAVE_NTGUI */
6613 /* Reading from a memory buffer for TIFF images Based on the PNG
6614 memory source, but we have to provide a lot of extra functions.
6617 We really only need to implement read and seek, but I am not
6618 convinced that the TIFF library is smart enough not to destroy
6619 itself if we only hand it the function pointers we need to
6624 unsigned char *bytes
;
6631 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6633 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6635 if (size
> src
->len
- src
->index
)
6637 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6643 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6649 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6651 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6656 case SEEK_SET
: /* Go from beginning of source. */
6660 case SEEK_END
: /* Go from end of source. */
6661 idx
= src
->len
+ off
;
6664 case SEEK_CUR
: /* Go from current position. */
6665 idx
= src
->index
+ off
;
6668 default: /* Invalid `whence'. */
6672 if (idx
> src
->len
|| idx
< 0)
6680 tiff_close_memory (thandle_t data
)
6687 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6689 /* It is already _IN_ memory. */
6694 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6696 /* We don't need to do this. */
6700 tiff_size_of_memory (thandle_t data
)
6702 return ((tiff_memory_source
*) data
)->len
;
6707 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6712 len
= sprintf (buf
, "TIFF error: %s ", title
);
6713 vsprintf (buf
+ len
, format
, ap
);
6714 add_to_log (buf
, Qnil
, Qnil
);
6719 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6724 len
= sprintf (buf
, "TIFF warning: %s ", title
);
6725 vsprintf (buf
+ len
, format
, ap
);
6726 add_to_log (buf
, Qnil
, Qnil
);
6730 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6734 tiff_load (struct frame
*f
, struct image
*img
)
6736 Lisp_Object file
, specified_file
;
6737 Lisp_Object specified_data
;
6739 int width
, height
, x
, y
, count
;
6743 struct gcpro gcpro1
;
6744 tiff_memory_source memsrc
;
6747 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6748 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6752 fn_TIFFSetErrorHandler (tiff_error_handler
);
6753 fn_TIFFSetWarningHandler (tiff_warning_handler
);
6755 if (NILP (specified_data
))
6757 /* Read from a file */
6758 file
= x_find_image_file (specified_file
);
6759 if (!STRINGP (file
))
6761 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6766 /* Try to open the image file. Casting return value avoids a
6767 GCC warning on W32. */
6768 tiff
= (TIFF
*)fn_TIFFOpen (SDATA (file
), "r");
6771 image_error ("Cannot open `%s'", file
, Qnil
);
6778 /* Memory source! */
6779 memsrc
.bytes
= SDATA (specified_data
);
6780 memsrc
.len
= SBYTES (specified_data
);
6783 /* Casting return value avoids a GCC warning on W32. */
6784 tiff
= (TIFF
*)fn_TIFFClientOpen ("memory_source", "r", &memsrc
,
6785 (TIFFReadWriteProc
) tiff_read_from_memory
,
6786 (TIFFReadWriteProc
) tiff_write_from_memory
,
6787 tiff_seek_in_memory
,
6789 tiff_size_of_memory
,
6795 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6801 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6802 if (INTEGERP (image
))
6804 int ino
= XFASTINT (image
);
6805 if (!fn_TIFFSetDirectory (tiff
, ino
))
6807 image_error ("Invalid image number `%s' in image `%s'",
6809 fn_TIFFClose (tiff
);
6815 /* Get width and height of the image, and allocate a raster buffer
6816 of width x height 32-bit values. */
6817 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6818 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6820 if (!check_image_size (f
, width
, height
))
6822 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6823 fn_TIFFClose (tiff
);
6828 buf
= (uint32
*) xmalloc (width
* height
* sizeof *buf
);
6830 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6832 /* Count the number of images in the file. */
6833 for (count
= 1, rc2
= 1; rc2
; count
++)
6834 rc2
= fn_TIFFSetDirectory (tiff
, count
);
6837 img
->data
.lisp_val
= Fcons (Qcount
,
6838 Fcons (make_number (count
),
6839 img
->data
.lisp_val
));
6841 fn_TIFFClose (tiff
);
6844 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6850 /* Create the X image and pixmap. */
6851 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6858 /* Initialize the color table. */
6859 init_color_table ();
6861 /* Process the pixel raster. Origin is in the lower-left corner. */
6862 for (y
= 0; y
< height
; ++y
)
6864 uint32
*row
= buf
+ y
* width
;
6866 for (x
= 0; x
< width
; ++x
)
6868 uint32 abgr
= row
[x
];
6869 int r
= TIFFGetR (abgr
) << 8;
6870 int g
= TIFFGetG (abgr
) << 8;
6871 int b
= TIFFGetB (abgr
) << 8;
6872 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6876 #ifdef COLOR_TABLE_SUPPORT
6877 /* Remember the colors allocated for the image. Free the color table. */
6878 img
->colors
= colors_in_color_table (&img
->ncolors
);
6879 free_color_table ();
6880 #endif /* COLOR_TABLE_SUPPORT */
6883 img
->height
= height
;
6885 /* Maybe fill in the background field while we have ximg handy. */
6886 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6887 /* Casting avoids a GCC warning on W32. */
6888 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6890 /* Put the image into the pixmap, then free the X image and its buffer. */
6891 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6892 x_destroy_x_image (ximg
);
6899 #else /* HAVE_TIFF */
6903 tiff_load (struct frame
*f
, struct image
*img
)
6905 return ns_load_image (f
, img
,
6906 image_spec_value (img
->spec
, QCfile
, NULL
),
6907 image_spec_value (img
->spec
, QCdata
, NULL
));
6909 #endif /* HAVE_NS */
6911 #endif /* !HAVE_TIFF */
6915 /***********************************************************************
6917 ***********************************************************************/
6919 #if defined (HAVE_GIF) || defined (HAVE_NS)
6921 static int gif_image_p (Lisp_Object object
);
6922 static int gif_load (struct frame
*f
, struct image
*img
);
6923 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6925 /* The symbol `gif' identifying images of this type. */
6929 /* Indices of image specification fields in gif_format, below. */
6931 enum gif_keyword_index
6947 /* Vector of image_keyword structures describing the format
6948 of valid user-defined image specifications. */
6950 static const struct image_keyword gif_format
[GIF_LAST
] =
6952 {":type", IMAGE_SYMBOL_VALUE
, 1},
6953 {":data", IMAGE_STRING_VALUE
, 0},
6954 {":file", IMAGE_STRING_VALUE
, 0},
6955 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6956 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6957 {":relief", IMAGE_INTEGER_VALUE
, 0},
6958 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6959 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6960 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6961 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
6962 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6965 /* Structure describing the image type `gif'. */
6967 static struct image_type gif_type
=
6976 /* Free X resources of GIF image IMG which is used on frame F. */
6979 gif_clear_image (struct frame
*f
, struct image
*img
)
6981 /* IMG->data.ptr_val may contain metadata with extension data. */
6982 img
->data
.lisp_val
= Qnil
;
6983 x_clear_image (f
, img
);
6986 /* Return non-zero if OBJECT is a valid GIF image specification. */
6989 gif_image_p (Lisp_Object object
)
6991 struct image_keyword fmt
[GIF_LAST
];
6992 memcpy (fmt
, gif_format
, sizeof fmt
);
6994 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
6997 /* Must specify either the :data or :file keyword. */
6998 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7001 #endif /* HAVE_GIF */
7005 #if defined (HAVE_NTGUI)
7006 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7007 Undefine before redefining to avoid a preprocessor warning. */
7011 /* avoid conflict with QuickdrawText.h */
7012 #define DrawText gif_DrawText
7013 #include <gif_lib.h>
7016 #else /* HAVE_NTGUI */
7018 #include <gif_lib.h>
7020 #endif /* HAVE_NTGUI */
7025 /* GIF library details. */
7026 DEF_IMGLIB_FN (DGifCloseFile
);
7027 DEF_IMGLIB_FN (DGifSlurp
);
7028 DEF_IMGLIB_FN (DGifOpen
);
7029 DEF_IMGLIB_FN (DGifOpenFileName
);
7032 init_gif_functions (Lisp_Object libraries
)
7036 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7039 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7040 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7041 LOAD_IMGLIB_FN (library
, DGifOpen
);
7042 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7048 #define fn_DGifCloseFile DGifCloseFile
7049 #define fn_DGifSlurp DGifSlurp
7050 #define fn_DGifOpen DGifOpen
7051 #define fn_DGifOpenFileName DGifOpenFileName
7053 #endif /* HAVE_NTGUI */
7055 /* Reading a GIF image from memory
7056 Based on the PNG memory stuff to a certain extent. */
7060 unsigned char *bytes
;
7066 /* Make the current memory source available to gif_read_from_memory.
7067 It's done this way because not all versions of libungif support
7068 a UserData field in the GifFileType structure. */
7069 static gif_memory_source
*current_gif_memory_src
;
7072 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7074 gif_memory_source
*src
= current_gif_memory_src
;
7076 if (len
> src
->len
- src
->index
)
7079 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7085 /* Load GIF image IMG for use on frame F. Value is non-zero if
7088 static const int interlace_start
[] = {0, 4, 2, 1};
7089 static const int interlace_increment
[] = {8, 8, 4, 2};
7092 gif_load (struct frame
*f
, struct image
*img
)
7094 Lisp_Object file
, specified_file
;
7095 Lisp_Object specified_data
;
7096 int rc
, width
, height
, x
, y
, i
;
7098 ColorMapObject
*gif_color_map
;
7099 unsigned long pixel_colors
[256];
7101 struct gcpro gcpro1
;
7103 int ino
, image_height
, image_width
;
7104 gif_memory_source memsrc
;
7105 unsigned char *raster
;
7107 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7108 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7112 if (NILP (specified_data
))
7114 file
= x_find_image_file (specified_file
);
7115 if (!STRINGP (file
))
7117 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7122 /* Open the GIF file. Casting return value avoids a GCC warning
7124 gif
= (GifFileType
*)fn_DGifOpenFileName (SDATA (file
));
7127 image_error ("Cannot open `%s'", file
, Qnil
);
7134 /* Read from memory! */
7135 current_gif_memory_src
= &memsrc
;
7136 memsrc
.bytes
= SDATA (specified_data
);
7137 memsrc
.len
= SBYTES (specified_data
);
7140 /* Casting return value avoids a GCC warning on W32. */
7141 gif
= (GifFileType
*) fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7144 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7150 /* Before reading entire contents, check the declared image size. */
7151 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7153 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7154 fn_DGifCloseFile (gif
);
7159 /* Read entire contents. */
7160 rc
= fn_DGifSlurp (gif
);
7161 if (rc
== GIF_ERROR
)
7163 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7164 fn_DGifCloseFile (gif
);
7169 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7170 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7171 if (ino
>= gif
->ImageCount
)
7173 image_error ("Invalid image number `%s' in image `%s'",
7175 fn_DGifCloseFile (gif
);
7180 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Top
;
7181 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Left
;
7182 image_height
= gif
->SavedImages
[ino
].ImageDesc
.Height
;
7183 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7184 image_width
= gif
->SavedImages
[ino
].ImageDesc
.Width
;
7185 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7187 width
= img
->width
= max (gif
->SWidth
,
7188 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7189 img
->corners
[RIGHT_CORNER
]));
7190 height
= img
->height
= max (gif
->SHeight
,
7191 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7192 img
->corners
[BOT_CORNER
]));
7194 if (!check_image_size (f
, width
, height
))
7196 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7197 fn_DGifCloseFile (gif
);
7202 /* Create the X image and pixmap. */
7203 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7205 fn_DGifCloseFile (gif
);
7210 /* Allocate colors. */
7211 gif_color_map
= gif
->SavedImages
[ino
].ImageDesc
.ColorMap
;
7213 gif_color_map
= gif
->SColorMap
;
7214 init_color_table ();
7215 memset (pixel_colors
, 0, sizeof pixel_colors
);
7218 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7220 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7221 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7222 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7223 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7226 #ifdef COLOR_TABLE_SUPPORT
7227 img
->colors
= colors_in_color_table (&img
->ncolors
);
7228 free_color_table ();
7229 #endif /* COLOR_TABLE_SUPPORT */
7231 /* Clear the part of the screen image that are not covered by
7232 the image from the GIF file. Full animated GIF support
7233 requires more than can be done here (see the gif89 spec,
7234 disposal methods). Let's simply assume that the part
7235 not covered by a sub-image is in the frame's background color. */
7236 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7237 for (x
= 0; x
< width
; ++x
)
7238 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7240 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7241 for (x
= 0; x
< width
; ++x
)
7242 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7244 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7246 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7247 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7248 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7249 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7252 /* Read the GIF image into the X image. We use a local variable
7253 `raster' here because RasterBits below is a char *, and invites
7254 problems with bytes >= 0x80. */
7255 raster
= (unsigned char *) gif
->SavedImages
[ino
].RasterBits
;
7257 if (gif
->SavedImages
[ino
].ImageDesc
.Interlace
)
7260 int row
= interlace_start
[0];
7264 for (y
= 0; y
< image_height
; y
++)
7266 if (row
>= image_height
)
7268 row
= interlace_start
[++pass
];
7269 while (row
>= image_height
)
7270 row
= interlace_start
[++pass
];
7273 for (x
= 0; x
< image_width
; x
++)
7275 int i
= raster
[(y
* image_width
) + x
];
7276 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7277 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7280 row
+= interlace_increment
[pass
];
7285 for (y
= 0; y
< image_height
; ++y
)
7286 for (x
= 0; x
< image_width
; ++x
)
7288 int i
= raster
[y
* image_width
+ x
];
7289 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7290 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7294 /* Save GIF image extension data for `image-metadata'.
7295 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7296 img
->data
.lisp_val
= Qnil
;
7297 if (gif
->SavedImages
[ino
].ExtensionBlockCount
> 0)
7299 ExtensionBlock
*ext
= gif
->SavedImages
[ino
].ExtensionBlocks
;
7300 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++, ext
++)
7301 /* Append (... FUNCTION "BYTES") */
7302 img
->data
.lisp_val
= Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7303 Fcons (make_number (ext
->Function
),
7304 img
->data
.lisp_val
));
7305 img
->data
.lisp_val
= Fcons (Qextension_data
,
7306 Fcons (Fnreverse (img
->data
.lisp_val
),
7309 if (gif
->ImageCount
> 1)
7310 img
->data
.lisp_val
= Fcons (Qcount
,
7311 Fcons (make_number (gif
->ImageCount
),
7312 img
->data
.lisp_val
));
7314 fn_DGifCloseFile (gif
);
7316 /* Maybe fill in the background field while we have ximg handy. */
7317 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7318 /* Casting avoids a GCC warning. */
7319 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7321 /* Put the image into the pixmap, then free the X image and its buffer. */
7322 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7323 x_destroy_x_image (ximg
);
7329 #else /* !HAVE_GIF */
7333 gif_load (struct frame
*f
, struct image
*img
)
7335 return ns_load_image (f
, img
,
7336 image_spec_value (img
->spec
, QCfile
, NULL
),
7337 image_spec_value (img
->spec
, QCdata
, NULL
));
7339 #endif /* HAVE_NS */
7341 #endif /* HAVE_GIF */
7345 /***********************************************************************
7347 ***********************************************************************/
7349 #if defined (HAVE_RSVG)
7351 /* Function prototypes. */
7353 static int svg_image_p (Lisp_Object object
);
7354 static int svg_load (struct frame
*f
, struct image
*img
);
7356 static int svg_load_image (struct frame
*, struct image
*,
7357 unsigned char *, unsigned int);
7359 /* The symbol `svg' identifying images of this type. */
7363 /* Indices of image specification fields in svg_format, below. */
7365 enum svg_keyword_index
7380 /* Vector of image_keyword structures describing the format
7381 of valid user-defined image specifications. */
7383 static const struct image_keyword svg_format
[SVG_LAST
] =
7385 {":type", IMAGE_SYMBOL_VALUE
, 1},
7386 {":data", IMAGE_STRING_VALUE
, 0},
7387 {":file", IMAGE_STRING_VALUE
, 0},
7388 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7389 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7390 {":relief", IMAGE_INTEGER_VALUE
, 0},
7391 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7392 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7393 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7394 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7397 /* Structure describing the image type `svg'. Its the same type of
7398 structure defined for all image formats, handled by emacs image
7399 functions. See struct image_type in dispextern.h. */
7401 static struct image_type svg_type
=
7403 /* An identifier showing that this is an image structure for the SVG format. */
7405 /* Handle to a function that can be used to identify a SVG file. */
7407 /* Handle to function used to load a SVG file. */
7409 /* Handle to function to free sresources for SVG. */
7411 /* An internal field to link to the next image type in a list of
7412 image types, will be filled in when registering the format. */
7417 /* Return non-zero if OBJECT is a valid SVG image specification. Do
7418 this by calling parse_image_spec and supplying the keywords that
7419 identify the SVG format. */
7422 svg_image_p (Lisp_Object object
)
7424 struct image_keyword fmt
[SVG_LAST
];
7425 memcpy (fmt
, svg_format
, sizeof fmt
);
7427 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
7430 /* Must specify either the :data or :file keyword. */
7431 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
7434 #include <librsvg/rsvg.h>
7438 /* SVG library functions. */
7439 DEF_IMGLIB_FN (rsvg_handle_new
);
7440 DEF_IMGLIB_FN (rsvg_handle_get_dimensions
);
7441 DEF_IMGLIB_FN (rsvg_handle_write
);
7442 DEF_IMGLIB_FN (rsvg_handle_close
);
7443 DEF_IMGLIB_FN (rsvg_handle_get_pixbuf
);
7444 DEF_IMGLIB_FN (rsvg_handle_free
);
7446 DEF_IMGLIB_FN (gdk_pixbuf_get_width
);
7447 DEF_IMGLIB_FN (gdk_pixbuf_get_height
);
7448 DEF_IMGLIB_FN (gdk_pixbuf_get_pixels
);
7449 DEF_IMGLIB_FN (gdk_pixbuf_get_rowstride
);
7450 DEF_IMGLIB_FN (gdk_pixbuf_get_colorspace
);
7451 DEF_IMGLIB_FN (gdk_pixbuf_get_n_channels
);
7452 DEF_IMGLIB_FN (gdk_pixbuf_get_has_alpha
);
7453 DEF_IMGLIB_FN (gdk_pixbuf_get_bits_per_sample
);
7455 DEF_IMGLIB_FN (g_type_init
);
7456 DEF_IMGLIB_FN (g_object_unref
);
7457 DEF_IMGLIB_FN (g_error_free
);
7459 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
7462 init_svg_functions (Lisp_Object libraries
)
7464 HMODULE library
, gdklib
, glib
, gobject
;
7466 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
7467 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
7468 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
7469 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
7472 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
7473 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
7474 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
7475 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
7476 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
7477 LOAD_IMGLIB_FN (library
, rsvg_handle_free
);
7479 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
7480 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
7481 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
7482 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
7483 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
7484 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
7485 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
7486 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
7488 LOAD_IMGLIB_FN (gobject
, g_type_init
);
7489 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
7490 LOAD_IMGLIB_FN (glib
, g_error_free
);
7496 /* The following aliases for library functions allow dynamic loading
7497 to be used on some platforms. */
7498 #define fn_rsvg_handle_new rsvg_handle_new
7499 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
7500 #define fn_rsvg_handle_write rsvg_handle_write
7501 #define fn_rsvg_handle_close rsvg_handle_close
7502 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
7503 #define fn_rsvg_handle_free rsvg_handle_free
7505 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
7506 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
7507 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
7508 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
7509 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
7510 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
7511 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
7512 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
7514 #define fn_g_type_init g_type_init
7515 #define fn_g_object_unref g_object_unref
7516 #define fn_g_error_free g_error_free
7517 #endif /* !HAVE_NTGUI */
7519 /* Load SVG image IMG for use on frame F. Value is non-zero if
7520 successful. this function will go into the svg_type structure, and
7521 the prototype thus needs to be compatible with that structure. */
7524 svg_load (struct frame
*f
, struct image
*img
)
7527 Lisp_Object file_name
;
7529 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7530 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7531 if (STRINGP (file_name
))
7534 unsigned char *contents
;
7536 struct gcpro gcpro1
;
7538 file
= x_find_image_file (file_name
);
7540 if (!STRINGP (file
))
7542 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7547 /* Read the entire file into memory. */
7548 contents
= slurp_file (SDATA (file
), &size
);
7549 if (contents
== NULL
)
7551 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
7555 /* If the file was slurped into memory properly, parse it. */
7556 success_p
= svg_load_image (f
, img
, contents
, size
);
7560 /* Else its not a file, its a lisp object. Load the image from a
7561 lisp object rather than a file. */
7566 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7567 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
7573 /* svg_load_image is a helper function for svg_load, which does the
7574 actual loading given contents and size, apart from frame and image
7575 structures, passed from svg_load.
7577 Uses librsvg to do most of the image processing.
7579 Returns non-zero when successful. */
7581 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
7582 struct image
*img
, /* Pointer to emacs image structure. */
7583 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
7584 unsigned int size
) /* Size of data in bytes. */
7586 RsvgHandle
*rsvg_handle
;
7587 RsvgDimensionData dimension_data
;
7588 GError
*error
= NULL
;
7592 const guint8
*pixels
;
7595 Lisp_Object specified_bg
;
7600 /* g_type_init is a glib function that must be called prior to using
7601 gnome type library functions. */
7603 /* Make a handle to a new rsvg object. */
7604 rsvg_handle
= (RsvgHandle
*) fn_rsvg_handle_new ();
7606 /* Parse the contents argument and fill in the rsvg_handle. */
7607 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &error
);
7608 if (error
) goto rsvg_error
;
7610 /* The parsing is complete, rsvg_handle is ready to used, close it
7611 for further writes. */
7612 fn_rsvg_handle_close (rsvg_handle
, &error
);
7613 if (error
) goto rsvg_error
;
7615 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
7616 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
7618 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7622 /* We can now get a valid pixel buffer from the svg file, if all
7624 pixbuf
= (GdkPixbuf
*) fn_rsvg_handle_get_pixbuf (rsvg_handle
);
7625 if (!pixbuf
) goto rsvg_error
;
7626 fn_g_object_unref (rsvg_handle
);
7628 /* Extract some meta data from the svg handle. */
7629 width
= fn_gdk_pixbuf_get_width (pixbuf
);
7630 height
= fn_gdk_pixbuf_get_height (pixbuf
);
7631 pixels
= (const guint8
*) fn_gdk_pixbuf_get_pixels (pixbuf
);
7632 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
7634 /* Validate the svg meta data. */
7635 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
7636 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
7637 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
7638 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
7640 /* Try to create a x pixmap to hold the svg pixmap. */
7641 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7643 fn_g_object_unref (pixbuf
);
7647 init_color_table ();
7649 /* Handle alpha channel by combining the image with a background
7651 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7652 if (!STRINGP (specified_bg
)
7653 || !x_defined_color (f
, SDATA (specified_bg
), &background
, 0))
7656 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7657 x_query_color (f
, &background
);
7659 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
7663 /* SVG pixmaps specify transparency in the last byte, so right
7664 shift 8 bits to get rid of it, since emacs doesn't support
7666 background
.red
>>= 8;
7667 background
.green
>>= 8;
7668 background
.blue
>>= 8;
7670 /* This loop handles opacity values, since Emacs assumes
7671 non-transparent images. Each pixel must be "flattened" by
7672 calculating the resulting color, given the transparency of the
7673 pixel, and the image background color. */
7674 for (y
= 0; y
< height
; ++y
)
7676 for (x
= 0; x
< width
; ++x
)
7686 opacity
= *pixels
++;
7688 red
= ((red
* opacity
)
7689 + (background
.red
* ((1 << 8) - opacity
)));
7690 green
= ((green
* opacity
)
7691 + (background
.green
* ((1 << 8) - opacity
)));
7692 blue
= ((blue
* opacity
)
7693 + (background
.blue
* ((1 << 8) - opacity
)));
7695 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
7698 pixels
+= rowstride
- 4 * width
;
7701 #ifdef COLOR_TABLE_SUPPORT
7702 /* Remember colors allocated for this image. */
7703 img
->colors
= colors_in_color_table (&img
->ncolors
);
7704 free_color_table ();
7705 #endif /* COLOR_TABLE_SUPPORT */
7707 fn_g_object_unref (pixbuf
);
7710 img
->height
= height
;
7712 /* Maybe fill in the background field while we have ximg handy.
7713 Casting avoids a GCC warning. */
7714 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7716 /* Put the image into the pixmap, then free the X image and its
7718 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7719 x_destroy_x_image (ximg
);
7724 fn_g_object_unref (rsvg_handle
);
7725 /* FIXME: Use error->message so the user knows what is the actual
7726 problem with the image. */
7727 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
7728 fn_g_error_free (error
);
7732 #endif /* defined (HAVE_RSVG) */
7737 /***********************************************************************
7739 ***********************************************************************/
7741 #ifdef HAVE_X_WINDOWS
7742 #define HAVE_GHOSTSCRIPT 1
7743 #endif /* HAVE_X_WINDOWS */
7745 /* The symbol `postscript' identifying images of this type. */
7747 Lisp_Object Qpostscript
;
7749 #ifdef HAVE_GHOSTSCRIPT
7751 static int gs_image_p (Lisp_Object object
);
7752 static int gs_load (struct frame
*f
, struct image
*img
);
7753 static void gs_clear_image (struct frame
*f
, struct image
*img
);
7755 /* Keyword symbols. */
7757 Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
7759 /* Indices of image specification fields in gs_format, below. */
7761 enum gs_keyword_index
7779 /* Vector of image_keyword structures describing the format
7780 of valid user-defined image specifications. */
7782 static const struct image_keyword gs_format
[GS_LAST
] =
7784 {":type", IMAGE_SYMBOL_VALUE
, 1},
7785 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
7786 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
7787 {":file", IMAGE_STRING_VALUE
, 1},
7788 {":loader", IMAGE_FUNCTION_VALUE
, 0},
7789 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
7790 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7791 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7792 {":relief", IMAGE_INTEGER_VALUE
, 0},
7793 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7794 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7795 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7796 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7799 /* Structure describing the image type `ghostscript'. */
7801 static struct image_type gs_type
=
7811 /* Free X resources of Ghostscript image IMG which is used on frame F. */
7814 gs_clear_image (struct frame
*f
, struct image
*img
)
7816 /* IMG->data.ptr_val may contain a recorded colormap. */
7817 xfree (img
->data
.ptr_val
);
7818 x_clear_image (f
, img
);
7822 /* Return non-zero if OBJECT is a valid Ghostscript image
7826 gs_image_p (Lisp_Object object
)
7828 struct image_keyword fmt
[GS_LAST
];
7832 memcpy (fmt
, gs_format
, sizeof fmt
);
7834 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
7837 /* Bounding box must be a list or vector containing 4 integers. */
7838 tem
= fmt
[GS_BOUNDING_BOX
].value
;
7841 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
7842 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
7847 else if (VECTORP (tem
))
7849 if (XVECTOR (tem
)->size
!= 4)
7851 for (i
= 0; i
< 4; ++i
)
7852 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
7862 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
7866 gs_load (struct frame
*f
, struct image
*img
)
7869 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
7870 struct gcpro gcpro1
, gcpro2
;
7872 double in_width
, in_height
;
7873 Lisp_Object pixel_colors
= Qnil
;
7875 /* Compute pixel size of pixmap needed from the given size in the
7876 image specification. Sizes in the specification are in pt. 1 pt
7877 = 1/72 in, xdpi and ydpi are stored in the frame's X display
7879 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
7880 in_width
= XFASTINT (pt_width
) / 72.0;
7881 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
7882 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
7883 in_height
= XFASTINT (pt_height
) / 72.0;
7884 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
7886 if (!check_image_size (f
, img
->width
, img
->height
))
7888 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7892 /* Create the pixmap. */
7893 xassert (img
->pixmap
== NO_PIXMAP
);
7895 /* Only W32 version did BLOCK_INPUT here. ++kfs */
7897 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
7898 img
->width
, img
->height
,
7899 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
7904 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
7908 /* Call the loader to fill the pixmap. It returns a process object
7909 if successful. We do not record_unwind_protect here because
7910 other places in redisplay like calling window scroll functions
7911 don't either. Let the Lisp loader use `unwind-protect' instead. */
7912 GCPRO2 (window_and_pixmap_id
, pixel_colors
);
7914 sprintf (buffer
, "%lu %lu",
7915 (unsigned long) FRAME_X_WINDOW (f
),
7916 (unsigned long) img
->pixmap
);
7917 window_and_pixmap_id
= build_string (buffer
);
7919 sprintf (buffer
, "%lu %lu",
7920 FRAME_FOREGROUND_PIXEL (f
),
7921 FRAME_BACKGROUND_PIXEL (f
));
7922 pixel_colors
= build_string (buffer
);
7924 XSETFRAME (frame
, f
);
7925 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
7927 loader
= intern ("gs-load-image");
7929 img
->data
.lisp_val
= call6 (loader
, frame
, img
->spec
,
7930 make_number (img
->width
),
7931 make_number (img
->height
),
7932 window_and_pixmap_id
,
7935 return PROCESSP (img
->data
.lisp_val
);
7939 /* Kill the Ghostscript process that was started to fill PIXMAP on
7940 frame F. Called from XTread_socket when receiving an event
7941 telling Emacs that Ghostscript has finished drawing. */
7944 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
7946 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
7950 /* Find the image containing PIXMAP. */
7951 for (i
= 0; i
< c
->used
; ++i
)
7952 if (c
->images
[i
]->pixmap
== pixmap
)
7955 /* Should someone in between have cleared the image cache, for
7956 instance, give up. */
7960 /* Kill the GS process. We should have found PIXMAP in the image
7961 cache and its image should contain a process object. */
7963 xassert (PROCESSP (img
->data
.lisp_val
));
7964 Fkill_process (img
->data
.lisp_val
, Qnil
);
7965 img
->data
.lisp_val
= Qnil
;
7967 #if defined (HAVE_X_WINDOWS)
7969 /* On displays with a mutable colormap, figure out the colors
7970 allocated for the image by looking at the pixels of an XImage for
7972 class = FRAME_X_VISUAL (f
)->class;
7973 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
7979 /* Try to get an XImage for img->pixmep. */
7980 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
7981 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
7986 /* Initialize the color table. */
7987 init_color_table ();
7989 /* For each pixel of the image, look its color up in the
7990 color table. After having done so, the color table will
7991 contain an entry for each color used by the image. */
7992 for (y
= 0; y
< img
->height
; ++y
)
7993 for (x
= 0; x
< img
->width
; ++x
)
7995 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
7996 lookup_pixel_color (f
, pixel
);
7999 /* Record colors in the image. Free color table and XImage. */
8000 #ifdef COLOR_TABLE_SUPPORT
8001 img
->colors
= colors_in_color_table (&img
->ncolors
);
8002 free_color_table ();
8004 XDestroyImage (ximg
);
8006 #if 0 /* This doesn't seem to be the case. If we free the colors
8007 here, we get a BadAccess later in x_clear_image when
8008 freeing the colors. */
8009 /* We have allocated colors once, but Ghostscript has also
8010 allocated colors on behalf of us. So, to get the
8011 reference counts right, free them once. */
8013 x_free_colors (f
, img
->colors
, img
->ncolors
);
8017 image_error ("Cannot get X image of `%s'; colors will not be freed",
8022 #endif /* HAVE_X_WINDOWS */
8024 /* Now that we have the pixmap, compute mask and transform the
8025 image if requested. */
8027 postprocess_image (f
, img
);
8031 #endif /* HAVE_GHOSTSCRIPT */
8034 /***********************************************************************
8036 ***********************************************************************/
8040 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8041 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8044 return valid_image_p (spec
) ? Qt
: Qnil
;
8048 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8053 if (valid_image_p (spec
))
8054 id
= lookup_image (SELECTED_FRAME (), spec
);
8057 return make_number (id
);
8060 #endif /* GLYPH_DEBUG != 0 */
8063 /***********************************************************************
8065 ***********************************************************************/
8068 /* Image types that rely on external libraries are loaded dynamically
8069 if the library is available. */
8070 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8071 define_image_type (image_type, init_lib_fn (libraries))
8073 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8074 define_image_type (image_type, 1)
8075 #endif /* HAVE_NTGUI */
8077 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8078 doc
: /* Initialize image library implementing image type TYPE.
8079 Return non-nil if TYPE is a supported image type.
8081 Image types pbm and xbm are prebuilt; other types are loaded here.
8082 Libraries to load are specified in alist LIBRARIES (usually, the value
8083 of `image-library-alist', which see). */)
8084 (Lisp_Object type
, Lisp_Object libraries
)
8088 /* Don't try to reload the library. */
8089 tested
= Fassq (type
, Vimage_type_cache
);
8091 return XCDR (tested
);
8093 #if defined (HAVE_XPM) || defined (HAVE_NS)
8094 if (EQ (type
, Qxpm
))
8095 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8098 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8099 if (EQ (type
, Qjpeg
))
8100 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8103 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8104 if (EQ (type
, Qtiff
))
8105 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8108 #if defined (HAVE_GIF) || defined (HAVE_NS)
8109 if (EQ (type
, Qgif
))
8110 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8113 #if defined (HAVE_PNG) || defined (HAVE_NS)
8114 if (EQ (type
, Qpng
))
8115 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8118 #if defined (HAVE_RSVG)
8119 if (EQ (type
, Qsvg
))
8120 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8123 #ifdef HAVE_GHOSTSCRIPT
8124 if (EQ (type
, Qpostscript
))
8125 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8128 /* If the type is not recognized, avoid testing it ever again. */
8129 CACHE_IMAGE_TYPE (type
, Qnil
);
8134 syms_of_image (void)
8136 extern Lisp_Object Qrisky_local_variable
; /* Syms_of_xdisp has already run. */
8138 /* Initialize this only once, since that's what we do with Vimage_types
8139 and they are supposed to be in sync. Initializing here gives correct
8140 operation on GNU/Linux of calling dump-emacs after loading some images. */
8143 /* Must be defined now becase we're going to update it below, while
8144 defining the supported image types. */
8145 DEFVAR_LISP ("image-types", &Vimage_types
,
8146 doc
: /* List of potentially supported image types.
8147 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8148 To check whether it is really supported, use `image-type-available-p'. */);
8149 Vimage_types
= Qnil
;
8151 DEFVAR_LISP ("image-library-alist", &Vimage_library_alist
,
8152 doc
: /* Alist of image types vs external libraries needed to display them.
8154 Each element is a list (IMAGE-TYPE LIBRARY...), where the car is a symbol
8155 representing a supported image type, and the rest are strings giving
8156 alternate filenames for the corresponding external libraries.
8158 Emacs tries to load the libraries in the order they appear on the
8159 list; if none is loaded, the running session of Emacs won't
8160 support the image type. Types 'pbm and 'xbm don't need to be
8161 listed; they are always supported. */);
8162 Vimage_library_alist
= Qnil
;
8163 Fput (intern_c_string ("image-library-alist"), Qrisky_local_variable
, Qt
);
8165 DEFVAR_LISP ("max-image-size", &Vmax_image_size
,
8166 doc
: /* Maximum size of images.
8167 Emacs will not load an image into memory if its pixel width or
8168 pixel height exceeds this limit.
8170 If the value is an integer, it directly specifies the maximum
8171 image height and width, measured in pixels. If it is a floating
8172 point number, it specifies the maximum image height and width
8173 as a ratio to the frame height and width. If the value is
8174 non-numeric, there is no explicit limit on the size of images. */);
8175 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8177 Vimage_type_cache
= Qnil
;
8178 staticpro (&Vimage_type_cache
);
8180 Qpbm
= intern_c_string ("pbm");
8182 ADD_IMAGE_TYPE (Qpbm
);
8184 Qxbm
= intern_c_string ("xbm");
8186 ADD_IMAGE_TYPE (Qxbm
);
8188 define_image_type (&xbm_type
, 1);
8189 define_image_type (&pbm_type
, 1);
8191 Qcount
= intern_c_string ("count");
8192 staticpro (&Qcount
);
8193 Qextension_data
= intern_c_string ("extension-data");
8194 staticpro (&Qextension_data
);
8196 QCascent
= intern_c_string (":ascent");
8197 staticpro (&QCascent
);
8198 QCmargin
= intern_c_string (":margin");
8199 staticpro (&QCmargin
);
8200 QCrelief
= intern_c_string (":relief");
8201 staticpro (&QCrelief
);
8202 QCconversion
= intern_c_string (":conversion");
8203 staticpro (&QCconversion
);
8204 QCcolor_symbols
= intern_c_string (":color-symbols");
8205 staticpro (&QCcolor_symbols
);
8206 QCheuristic_mask
= intern_c_string (":heuristic-mask");
8207 staticpro (&QCheuristic_mask
);
8208 QCindex
= intern_c_string (":index");
8209 staticpro (&QCindex
);
8210 QCmatrix
= intern_c_string (":matrix");
8211 staticpro (&QCmatrix
);
8212 QCcolor_adjustment
= intern_c_string (":color-adjustment");
8213 staticpro (&QCcolor_adjustment
);
8214 QCmask
= intern_c_string (":mask");
8215 staticpro (&QCmask
);
8217 Qlaplace
= intern_c_string ("laplace");
8218 staticpro (&Qlaplace
);
8219 Qemboss
= intern_c_string ("emboss");
8220 staticpro (&Qemboss
);
8221 Qedge_detection
= intern_c_string ("edge-detection");
8222 staticpro (&Qedge_detection
);
8223 Qheuristic
= intern_c_string ("heuristic");
8224 staticpro (&Qheuristic
);
8226 Qpostscript
= intern_c_string ("postscript");
8227 staticpro (&Qpostscript
);
8228 #ifdef HAVE_GHOSTSCRIPT
8229 ADD_IMAGE_TYPE (Qpostscript
);
8230 QCloader
= intern_c_string (":loader");
8231 staticpro (&QCloader
);
8232 QCbounding_box
= intern_c_string (":bounding-box");
8233 staticpro (&QCbounding_box
);
8234 QCpt_width
= intern_c_string (":pt-width");
8235 staticpro (&QCpt_width
);
8236 QCpt_height
= intern_c_string (":pt-height");
8237 staticpro (&QCpt_height
);
8238 #endif /* HAVE_GHOSTSCRIPT */
8240 #if defined (HAVE_XPM) || defined (HAVE_NS)
8241 Qxpm
= intern_c_string ("xpm");
8243 ADD_IMAGE_TYPE (Qxpm
);
8246 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8247 Qjpeg
= intern_c_string ("jpeg");
8249 ADD_IMAGE_TYPE (Qjpeg
);
8252 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8253 Qtiff
= intern_c_string ("tiff");
8255 ADD_IMAGE_TYPE (Qtiff
);
8258 #if defined (HAVE_GIF) || defined (HAVE_NS)
8259 Qgif
= intern_c_string ("gif");
8261 ADD_IMAGE_TYPE (Qgif
);
8264 #if defined (HAVE_PNG) || defined (HAVE_NS)
8265 Qpng
= intern_c_string ("png");
8267 ADD_IMAGE_TYPE (Qpng
);
8270 #if defined (HAVE_RSVG)
8271 Qsvg
= intern_c_string ("svg");
8273 ADD_IMAGE_TYPE (Qsvg
);
8275 /* Other libraries used directly by svg code. */
8276 Qgdk_pixbuf
= intern_c_string ("gdk-pixbuf");
8277 staticpro (&Qgdk_pixbuf
);
8278 Qglib
= intern_c_string ("glib");
8280 Qgobject
= intern_c_string ("gobject");
8281 staticpro (&Qgobject
);
8282 #endif /* HAVE_NTGUI */
8283 #endif /* HAVE_RSVG */
8285 defsubr (&Sinit_image_library
);
8286 defsubr (&Sclear_image_cache
);
8287 defsubr (&Simage_flush
);
8288 defsubr (&Simage_size
);
8289 defsubr (&Simage_mask_p
);
8290 defsubr (&Simage_metadata
);
8294 defsubr (&Slookup_image
);
8297 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images
,
8298 doc
: /* Non-nil means always draw a cross over disabled images.
8299 Disabled images are those having a `:conversion disabled' property.
8300 A cross is always drawn on black & white displays. */);
8301 cross_disabled_images
= 0;
8303 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path
,
8304 doc
: /* List of directories to search for window system bitmap files. */);
8305 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8307 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay
,
8308 doc
: /* Maximum time after which images are removed from the cache.
8309 When an image has not been displayed this many seconds, Emacs
8310 automatically removes it from the image cache. If the cache contains
8311 a large number of images, the actual eviction time may be shorter.
8312 The value can also be nil, meaning the cache is never cleared.
8314 The function `clear-image-cache' disregards this variable. */);
8315 Vimage_cache_eviction_delay
= make_number (300);
8323 /* arch-tag: 123c2a5e-14a8-4c53-ab95-af47d7db49b9
8324 (do not change this comment) */