1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #if defined HAVE_LIBPNG_PNG_H
27 # include <libpng/png.h>
35 /* This makes the fields of a Display accessible, in Xlib header files. */
37 #define XLIB_ILLEGAL_ACCESS
42 #include "dispextern.h"
43 #include "blockinput.h"
46 #include "character.h"
48 #include "termhooks.h"
53 #include <sys/types.h>
56 #define COLOR_TABLE_SUPPORT 1
58 typedef struct x_bitmap_record Bitmap_Record
;
59 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
60 #define NO_PIXMAP None
62 #define RGB_PIXEL_COLOR unsigned long
64 #define PIX_MASK_RETAIN 0
65 #define PIX_MASK_DRAW 1
66 #endif /* HAVE_X_WINDOWS */
72 /* W32_TODO : Color tables on W32. */
73 #undef COLOR_TABLE_SUPPORT
75 typedef struct w32_bitmap_record Bitmap_Record
;
76 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
79 #define RGB_PIXEL_COLOR COLORREF
81 #define PIX_MASK_RETAIN 0
82 #define PIX_MASK_DRAW 1
84 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
85 #define x_defined_color w32_defined_color
86 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
88 /* Functions from w32term.c that depend on XColor (so can't go in w32term.h
89 without modifying lots of files). */
90 extern void x_query_colors (struct frame
*f
, XColor
*colors
, int ncolors
);
91 extern void x_query_color (struct frame
*f
, XColor
*color
);
93 /* Version of libpng that we were compiled with, or -1 if no PNG
94 support was compiled in. This is tested by w32-win.el to correctly
95 set up the alist used to search for PNG libraries. */
96 Lisp_Object Qlibpng_version
;
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 /* The symbol `postscript' identifying images of this type. */
127 Lisp_Object Qpostscript
;
129 static void x_disable_image (struct frame
*, struct image
*);
130 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
133 static void init_color_table (void);
134 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
135 #ifdef COLOR_TABLE_SUPPORT
136 static void free_color_table (void);
137 static unsigned long *colors_in_color_table (int *n
);
138 static unsigned long lookup_pixel_color (struct frame
*f
, unsigned long p
);
140 INFUN (Finit_image_library
, 2);
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
, SSDATA (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
= SSDATA (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
, SSDATA (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 /* List of supported image types. Use define_image_type to add new
555 types. Use lookup_image_type to find a type for a given symbol. */
557 static struct image_type
*image_types
;
559 /* Cache for delayed-loading image types. */
561 static Lisp_Object Vimage_type_cache
;
563 /* The symbol `xbm' which is used as the type symbol for XBM images. */
569 Lisp_Object QCascent
, QCmargin
, QCrelief
, Qcount
, Qextension_data
;
570 Lisp_Object QCconversion
, QCcolor_symbols
, QCheuristic_mask
;
571 Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
, QCcrop
, QCrotation
;
575 Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
577 /* Function prototypes. */
579 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
580 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
581 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
582 static void x_laplace (struct frame
*, struct image
*);
583 static void x_emboss (struct frame
*, struct image
*);
584 static int x_build_heuristic_mask (struct frame
*, struct image
*,
587 #define CACHE_IMAGE_TYPE(type, status) \
588 do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
590 #define ADD_IMAGE_TYPE(type) \
591 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
593 /* Define a new image type from TYPE. This adds a copy of TYPE to
594 image_types and caches the loading status of TYPE. */
597 define_image_type (struct image_type
*type
, int loaded
)
605 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
606 The initialized data segment is read-only. */
607 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
608 memcpy (p
, type
, sizeof *p
);
609 p
->next
= image_types
;
614 CACHE_IMAGE_TYPE (*type
->type
, success
);
619 /* Look up image type SYMBOL, and return a pointer to its image_type
620 structure. Value is null if SYMBOL is not a known image type. */
622 static INLINE
struct image_type
*
623 lookup_image_type (Lisp_Object symbol
)
625 struct image_type
*type
;
627 /* We must initialize the image-type if it hasn't been already. */
628 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
629 return 0; /* unimplemented */
631 for (type
= image_types
; type
; type
= type
->next
)
632 if (EQ (symbol
, *type
->type
))
639 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
640 valid image specification is a list whose car is the symbol
641 `image', and whose rest is a property list. The property list must
642 contain a value for key `:type'. That value must be the name of a
643 supported image type. The rest of the property list depends on the
647 valid_image_p (Lisp_Object object
)
655 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
656 if (EQ (XCAR (tem
), QCtype
))
659 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
661 struct image_type
*type
;
662 type
= lookup_image_type (XCAR (tem
));
664 valid_p
= type
->valid_p (object
);
675 /* Log error message with format string FORMAT and argument ARG.
676 Signaling an error, e.g. when an image cannot be loaded, is not a
677 good idea because this would interrupt redisplay, and the error
678 message display would lead to another redisplay. This function
679 therefore simply displays a message. */
682 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
684 add_to_log (format
, arg1
, arg2
);
689 /***********************************************************************
691 ***********************************************************************/
693 enum image_value_type
695 IMAGE_DONT_CHECK_VALUE_TYPE
,
697 IMAGE_STRING_OR_NIL_VALUE
,
699 IMAGE_POSITIVE_INTEGER_VALUE
,
700 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
701 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
704 IMAGE_FUNCTION_VALUE
,
709 /* Structure used when parsing image specifications. */
713 /* Name of keyword. */
716 /* The type of value allowed. */
717 enum image_value_type type
;
719 /* Non-zero means key must be present. */
722 /* Used to recognize duplicate keywords in a property list. */
725 /* The value that was found. */
730 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
732 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
735 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
736 has the format (image KEYWORD VALUE ...). One of the keyword/
737 value pairs must be `:type TYPE'. KEYWORDS is a vector of
738 image_keywords structures of size NKEYWORDS describing other
739 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
742 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
743 int nkeywords
, Lisp_Object type
)
752 while (CONSP (plist
))
754 Lisp_Object key
, value
;
756 /* First element of a pair must be a symbol. */
758 plist
= XCDR (plist
);
762 /* There must follow a value. */
765 value
= XCAR (plist
);
766 plist
= XCDR (plist
);
768 /* Find key in KEYWORDS. Error if not found. */
769 for (i
= 0; i
< nkeywords
; ++i
)
770 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
776 /* Record that we recognized the keyword. If a keywords
777 was found more than once, it's an error. */
778 keywords
[i
].value
= value
;
781 if (keywords
[i
].count
> 1)
784 /* Check type of value against allowed type. */
785 switch (keywords
[i
].type
)
787 case IMAGE_STRING_VALUE
:
788 if (!STRINGP (value
))
792 case IMAGE_STRING_OR_NIL_VALUE
:
793 if (!STRINGP (value
) && !NILP (value
))
797 case IMAGE_SYMBOL_VALUE
:
798 if (!SYMBOLP (value
))
802 case IMAGE_POSITIVE_INTEGER_VALUE
:
803 if (!INTEGERP (value
) || XINT (value
) <= 0)
807 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
808 if (INTEGERP (value
) && XINT (value
) >= 0)
811 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
812 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
816 case IMAGE_ASCENT_VALUE
:
817 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
819 else if (INTEGERP (value
)
821 && XINT (value
) <= 100)
825 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
826 if (!INTEGERP (value
) || XINT (value
) < 0)
830 case IMAGE_DONT_CHECK_VALUE_TYPE
:
833 case IMAGE_FUNCTION_VALUE
:
834 value
= indirect_function (value
);
835 if (!NILP (Ffunctionp (value
)))
839 case IMAGE_NUMBER_VALUE
:
840 if (!INTEGERP (value
) && !FLOATP (value
))
844 case IMAGE_INTEGER_VALUE
:
845 if (!INTEGERP (value
))
849 case IMAGE_BOOL_VALUE
:
850 if (!NILP (value
) && !EQ (value
, Qt
))
859 if (EQ (key
, QCtype
) && !EQ (type
, value
))
863 /* Check that all mandatory fields are present. */
864 for (i
= 0; i
< nkeywords
; ++i
)
865 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
872 /* Return the value of KEY in image specification SPEC. Value is nil
873 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
874 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
877 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
881 xassert (valid_image_p (spec
));
883 for (tail
= XCDR (spec
);
884 CONSP (tail
) && CONSP (XCDR (tail
));
885 tail
= XCDR (XCDR (tail
)))
887 if (EQ (XCAR (tail
), key
))
891 return XCAR (XCDR (tail
));
901 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
902 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
903 PIXELS non-nil means return the size in pixels, otherwise return the
904 size in canonical character units.
905 FRAME is the frame on which the image will be displayed. FRAME nil
906 or omitted means use the selected frame. */)
907 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
912 if (valid_image_p (spec
))
914 struct frame
*f
= check_x_frame (frame
);
915 int id
= lookup_image (f
, spec
);
916 struct image
*img
= IMAGE_FROM_ID (f
, id
);
917 int width
= img
->width
+ 2 * img
->hmargin
;
918 int height
= img
->height
+ 2 * img
->vmargin
;
921 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
922 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
924 size
= Fcons (make_number (width
), make_number (height
));
927 error ("Invalid image specification");
933 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
934 doc
: /* Return t if image SPEC has a mask bitmap.
935 FRAME is the frame on which the image will be displayed. FRAME nil
936 or omitted means use the selected frame. */)
937 (Lisp_Object spec
, Lisp_Object frame
)
942 if (valid_image_p (spec
))
944 struct frame
*f
= check_x_frame (frame
);
945 int id
= lookup_image (f
, spec
);
946 struct image
*img
= IMAGE_FROM_ID (f
, id
);
951 error ("Invalid image specification");
956 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
957 doc
: /* Return metadata for image SPEC.
958 FRAME is the frame on which the image will be displayed. FRAME nil
959 or omitted means use the selected frame. */)
960 (Lisp_Object spec
, Lisp_Object frame
)
965 if (valid_image_p (spec
))
967 struct frame
*f
= check_x_frame (frame
);
968 int id
= lookup_image (f
, spec
);
969 struct image
*img
= IMAGE_FROM_ID (f
, id
);
970 ext
= img
->data
.lisp_val
;
977 /***********************************************************************
978 Image type independent image structures
979 ***********************************************************************/
981 static struct image
*make_image (Lisp_Object spec
, unsigned hash
);
982 static void free_image (struct frame
*f
, struct image
*img
);
983 static int check_image_size (struct frame
*f
, int width
, int height
);
985 #define MAX_IMAGE_SIZE 6.0
986 /* Allocate and return a new image structure for image specification
987 SPEC. SPEC has a hash value of HASH. */
989 static struct image
*
990 make_image (Lisp_Object spec
, unsigned int hash
)
992 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
993 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
995 xassert (valid_image_p (spec
));
996 memset (img
, 0, sizeof *img
);
997 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
998 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
999 xassert (img
->type
!= NULL
);
1001 img
->data
.lisp_val
= Qnil
;
1002 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1004 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1009 /* Free image IMG which was used on frame F, including its resources. */
1012 free_image (struct frame
*f
, struct image
*img
)
1016 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1018 /* Remove IMG from the hash table of its cache. */
1020 img
->prev
->next
= img
->next
;
1022 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1025 img
->next
->prev
= img
->prev
;
1027 c
->images
[img
->id
] = NULL
;
1029 /* Free resources, then free IMG. */
1030 img
->type
->free (f
, img
);
1035 /* Return 1 if the given widths and heights are valid for display;
1036 otherwise, return 0. */
1039 check_image_size (struct frame
*f
, int width
, int height
)
1043 if (width
<= 0 || height
<= 0)
1046 if (INTEGERP (Vmax_image_size
))
1047 w
= h
= XINT (Vmax_image_size
);
1048 else if (FLOATP (Vmax_image_size
))
1052 w
= FRAME_PIXEL_WIDTH (f
);
1053 h
= FRAME_PIXEL_HEIGHT (f
);
1056 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1057 w
= (int) (XFLOAT_DATA (Vmax_image_size
) * w
);
1058 h
= (int) (XFLOAT_DATA (Vmax_image_size
) * h
);
1063 return (width
<= w
&& height
<= h
);
1066 /* Prepare image IMG for display on frame F. Must be called before
1067 drawing an image. */
1070 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1074 /* We're about to display IMG, so set its timestamp to `now'. */
1076 img
->timestamp
= EMACS_SECS (t
);
1078 /* If IMG doesn't have a pixmap yet, load it now, using the image
1079 type dependent loader function. */
1080 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1081 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1086 /* Value is the number of pixels for the ascent of image IMG when
1087 drawn in face FACE. */
1090 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1095 if (slice
->height
== img
->height
)
1096 height
= img
->height
+ img
->vmargin
;
1097 else if (slice
->y
== 0)
1098 height
= slice
->height
+ img
->vmargin
;
1100 height
= slice
->height
;
1102 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1107 /* W32 specific version. Why?. ++kfs */
1108 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1109 - FONT_BASE (face
->font
)) / 2;
1111 /* This expression is arranged so that if the image can't be
1112 exactly centered, it will be moved slightly up. This is
1113 because a typical font is `top-heavy' (due to the presence
1114 uppercase letters), so the image placement should err towards
1115 being top-heavy too. It also just generally looks better. */
1116 ascent
= (height
+ FONT_BASE(face
->font
)
1117 - FONT_DESCENT(face
->font
) + 1) / 2;
1118 #endif /* HAVE_NTGUI */
1121 ascent
= height
/ 2;
1124 ascent
= (int) (height
* img
->ascent
/ 100.0);
1130 /* Image background colors. */
1132 /* Find the "best" corner color of a bitmap.
1133 On W32, XIMG is assumed to a device context with the bitmap selected. */
1135 static RGB_PIXEL_COLOR
1136 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1137 unsigned long width
, unsigned long height
)
1139 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1142 if (corners
&& corners
[BOT_CORNER
] >= 0)
1144 /* Get the colors at the corner_pixels of ximg. */
1145 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1146 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1147 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1148 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1152 /* Get the colors at the corner_pixels of ximg. */
1153 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1154 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1155 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1156 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1158 /* Choose the most frequently found color as background. */
1159 for (i
= best_count
= 0; i
< 4; ++i
)
1163 for (j
= n
= 0; j
< 4; ++j
)
1164 if (corner_pixels
[i
] == corner_pixels
[j
])
1168 best
= corner_pixels
[i
], best_count
= n
;
1174 /* Portability macros */
1178 #define Destroy_Image(img_dc, prev) \
1179 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1181 #define Free_Pixmap(display, pixmap) \
1182 DeleteObject (pixmap)
1184 #elif defined (HAVE_NS)
1186 #define Destroy_Image(ximg, dummy) \
1187 ns_release_object (ximg)
1189 #define Free_Pixmap(display, pixmap) \
1190 ns_release_object (pixmap)
1194 #define Destroy_Image(ximg, dummy) \
1195 XDestroyImage (ximg)
1197 #define Free_Pixmap(display, pixmap) \
1198 XFreePixmap (display, pixmap)
1200 #endif /* !HAVE_NTGUI && !HAVE_NS */
1203 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1204 it is guessed heuristically. If non-zero, XIMG is an existing
1205 XImage object (or device context with the image selected on W32) to
1206 use for the heuristic. */
1209 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1211 if (! img
->background_valid
)
1212 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1214 int free_ximg
= !ximg
;
1217 #endif /* HAVE_NTGUI */
1222 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1223 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1225 HDC frame_dc
= get_frame_dc (f
);
1226 ximg
= CreateCompatibleDC (frame_dc
);
1227 release_frame_dc (f
, frame_dc
);
1228 prev
= SelectObject (ximg
, img
->pixmap
);
1229 #endif /* !HAVE_NTGUI */
1232 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1235 Destroy_Image (ximg
, prev
);
1237 img
->background_valid
= 1;
1240 return img
->background
;
1243 /* Return the `background_transparent' field of IMG. If IMG doesn't
1244 have one yet, it is guessed heuristically. If non-zero, MASK is an
1245 existing XImage object to use for the heuristic. */
1248 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1250 if (! img
->background_transparent_valid
)
1251 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1255 int free_mask
= !mask
;
1258 #endif /* HAVE_NTGUI */
1263 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1264 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1266 HDC frame_dc
= get_frame_dc (f
);
1267 mask
= CreateCompatibleDC (frame_dc
);
1268 release_frame_dc (f
, frame_dc
);
1269 prev
= SelectObject (mask
, img
->mask
);
1270 #endif /* HAVE_NTGUI */
1273 img
->background_transparent
1274 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1277 Destroy_Image (mask
, prev
);
1280 img
->background_transparent
= 0;
1282 img
->background_transparent_valid
= 1;
1285 return img
->background_transparent
;
1289 /***********************************************************************
1290 Helper functions for X image types
1291 ***********************************************************************/
1293 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1295 static void x_clear_image (struct frame
*f
, struct image
*img
);
1296 static unsigned long x_alloc_image_color (struct frame
*f
,
1298 Lisp_Object color_name
,
1299 unsigned long dflt
);
1302 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1303 free the pixmap if any. MASK_P non-zero means clear the mask
1304 pixmap if any. COLORS_P non-zero means free colors allocated for
1305 the image, if any. */
1308 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1311 if (pixmap_p
&& img
->pixmap
)
1313 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1314 img
->pixmap
= NO_PIXMAP
;
1315 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1316 img
->background_valid
= 0;
1319 if (mask_p
&& img
->mask
)
1321 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1322 img
->mask
= NO_PIXMAP
;
1323 img
->background_transparent_valid
= 0;
1326 if (colors_p
&& img
->ncolors
)
1328 /* W32_TODO: color table support. */
1329 #ifdef HAVE_X_WINDOWS
1330 x_free_colors (f
, img
->colors
, img
->ncolors
);
1331 #endif /* HAVE_X_WINDOWS */
1332 xfree (img
->colors
);
1339 /* Free X resources of image IMG which is used on frame F. */
1342 x_clear_image (struct frame
*f
, struct image
*img
)
1345 x_clear_image_1 (f
, img
, 1, 1, 1);
1350 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1351 cannot be allocated, use DFLT. Add a newly allocated color to
1352 IMG->colors, so that it can be freed again. Value is the pixel
1355 static unsigned long
1356 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1360 unsigned long result
;
1362 xassert (STRINGP (color_name
));
1364 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1))
1366 /* This isn't called frequently so we get away with simply
1367 reallocating the color vector to the needed size, here. */
1370 (unsigned long *) xrealloc (img
->colors
,
1371 img
->ncolors
* sizeof *img
->colors
);
1372 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
1373 result
= color
.pixel
;
1383 /***********************************************************************
1385 ***********************************************************************/
1387 static struct image
*search_image_cache (struct frame
*, Lisp_Object
, unsigned);
1388 static void cache_image (struct frame
*f
, struct image
*img
);
1389 static void postprocess_image (struct frame
*, struct image
*);
1391 /* Return a new, initialized image cache that is allocated from the
1392 heap. Call free_image_cache to free an image cache. */
1394 struct image_cache
*
1395 make_image_cache (void)
1397 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1400 memset (c
, 0, sizeof *c
);
1402 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
1403 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1404 c
->buckets
= (struct image
**) xmalloc (size
);
1405 memset (c
->buckets
, 0, size
);
1410 /* Find an image matching SPEC in the cache, and return it. If no
1411 image is found, return NULL. */
1412 static struct image
*
1413 search_image_cache (struct frame
*f
, Lisp_Object spec
, unsigned int hash
)
1416 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1417 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1419 if (!c
) return NULL
;
1421 /* If the image spec does not specify a background color, the cached
1422 image must have the same background color as the current frame.
1423 The foreground color must also match, for the sake of monochrome
1426 In fact, we could ignore the foreground color matching condition
1427 for color images, or if the image spec specifies :foreground;
1428 similarly we could ignore the background color matching condition
1429 for formats that don't use transparency (such as jpeg), or if the
1430 image spec specifies :background. However, the extra memory
1431 usage is probably negligible in practice, so we don't bother. */
1433 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1434 if (img
->hash
== hash
1435 && !NILP (Fequal (img
->spec
, spec
))
1436 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1437 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1443 /* Search frame F for an image with spec SPEC, and free it. */
1446 uncache_image (struct frame
*f
, Lisp_Object spec
)
1448 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1451 free_image (f
, img
);
1452 /* As display glyphs may still be referring to the image ID, we
1453 must garbage the frame (Bug#6426). */
1454 SET_FRAME_GARBAGED (f
);
1459 /* Free image cache of frame F. Be aware that X frames share images
1463 free_image_cache (struct frame
*f
)
1465 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1470 /* Cache should not be referenced by any frame when freed. */
1471 xassert (c
->refcount
== 0);
1473 for (i
= 0; i
< c
->used
; ++i
)
1474 free_image (f
, c
->images
[i
]);
1478 FRAME_IMAGE_CACHE (f
) = NULL
;
1483 /* Clear image cache of frame F. FILTER=t means free all images.
1484 FILTER=nil means clear only images that haven't been
1485 displayed for some time.
1486 Else, only free the images which have FILTER in their `dependencies'.
1487 Should be called from time to time to reduce the number of loaded images.
1488 If image-cache-eviction-delay is non-nil, this frees images in the cache
1489 which weren't displayed for at least that many seconds. */
1492 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1494 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1500 /* Block input so that we won't be interrupted by a SIGIO
1501 while being in an inconsistent state. */
1506 /* Filter image cache. */
1507 for (i
= 0; i
< c
->used
; ++i
)
1509 struct image
*img
= c
->images
[i
];
1510 if (img
&& (EQ (Qt
, filter
)
1511 || !NILP (Fmember (filter
, img
->dependencies
))))
1513 free_image (f
, img
);
1518 else if (INTEGERP (Vimage_cache_eviction_delay
))
1520 /* Free cache based on timestamp. */
1523 int delay
, nimages
= 0;
1525 for (i
= 0; i
< c
->used
; ++i
)
1529 /* If the number of cached images has grown unusually large,
1530 decrease the cache eviction delay (Bug#6230). */
1531 delay
= XFASTINT (Vimage_cache_eviction_delay
);
1533 delay
= max (1, 1600 * delay
/ (nimages
*nimages
));
1536 old
= EMACS_SECS (t
) - delay
;
1538 for (i
= 0; i
< c
->used
; ++i
)
1540 struct image
*img
= c
->images
[i
];
1541 if (img
&& img
->timestamp
< old
)
1543 free_image (f
, img
);
1549 /* We may be clearing the image cache because, for example,
1550 Emacs was iconified for a longer period of time. In that
1551 case, current matrices may still contain references to
1552 images freed above. So, clear these matrices. */
1555 Lisp_Object tail
, frame
;
1557 FOR_EACH_FRAME (tail
, frame
)
1559 struct frame
*fr
= XFRAME (frame
);
1560 if (FRAME_IMAGE_CACHE (fr
) == c
)
1561 clear_current_matrices (fr
);
1564 ++windows_or_buffers_changed
;
1572 clear_image_caches (Lisp_Object filter
)
1574 /* FIXME: We want to do
1575 * struct terminal *t;
1576 * for (t = terminal_list; t; t = t->next_terminal)
1577 * clear_image_cache (t, filter); */
1578 Lisp_Object tail
, frame
;
1579 FOR_EACH_FRAME (tail
, frame
)
1580 if (FRAME_WINDOW_P (XFRAME (frame
)))
1581 clear_image_cache (XFRAME (frame
), filter
);
1584 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1586 doc
: /* Clear the image cache.
1587 FILTER nil or a frame means clear all images in the selected frame.
1588 FILTER t means clear the image caches of all frames.
1589 Anything else, means only clear those images which refer to FILTER,
1590 which is then usually a filename. */)
1591 (Lisp_Object filter
)
1593 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1594 clear_image_caches (filter
);
1596 clear_image_cache (check_x_frame (filter
), Qt
);
1602 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1604 doc
: /* Fush the image with specification SPEC on frame FRAME.
1605 This removes the image from the Emacs image cache. If SPEC specifies
1606 an image file, the next redisplay of this image will read from the
1607 current contents of that file.
1609 FRAME nil or omitted means use the selected frame.
1610 FRAME t means refresh the image on all frames. */)
1611 (Lisp_Object spec
, Lisp_Object frame
)
1613 if (!valid_image_p (spec
))
1614 error ("Invalid image specification");
1619 FOR_EACH_FRAME (tail
, frame
)
1621 struct frame
*f
= XFRAME (frame
);
1622 if (FRAME_WINDOW_P (f
))
1623 uncache_image (f
, spec
);
1627 uncache_image (check_x_frame (frame
), spec
);
1633 /* Compute masks and transform image IMG on frame F, as specified
1634 by the image's specification, */
1637 postprocess_image (struct frame
*f
, struct image
*img
)
1639 /* Manipulation of the image's mask. */
1642 Lisp_Object conversion
, spec
;
1647 /* `:heuristic-mask t'
1649 means build a mask heuristically.
1650 `:heuristic-mask (R G B)'
1651 `:mask (heuristic (R G B))'
1652 means build a mask from color (R G B) in the
1655 means remove a mask, if any. */
1657 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1659 x_build_heuristic_mask (f
, img
, mask
);
1664 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1666 if (EQ (mask
, Qheuristic
))
1667 x_build_heuristic_mask (f
, img
, Qt
);
1668 else if (CONSP (mask
)
1669 && EQ (XCAR (mask
), Qheuristic
))
1671 if (CONSP (XCDR (mask
)))
1672 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1674 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1676 else if (NILP (mask
) && found_p
&& img
->mask
)
1678 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1679 img
->mask
= NO_PIXMAP
;
1684 /* Should we apply an image transformation algorithm? */
1685 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1686 if (EQ (conversion
, Qdisabled
))
1687 x_disable_image (f
, img
);
1688 else if (EQ (conversion
, Qlaplace
))
1690 else if (EQ (conversion
, Qemboss
))
1692 else if (CONSP (conversion
)
1693 && EQ (XCAR (conversion
), Qedge_detection
))
1696 tem
= XCDR (conversion
);
1698 x_edge_detection (f
, img
,
1699 Fplist_get (tem
, QCmatrix
),
1700 Fplist_get (tem
, QCcolor_adjustment
));
1706 /* Return the id of image with Lisp specification SPEC on frame F.
1707 SPEC must be a valid Lisp image specification (see valid_image_p). */
1710 lookup_image (struct frame
*f
, Lisp_Object spec
)
1716 /* F must be a window-system frame, and SPEC must be a valid image
1718 xassert (FRAME_WINDOW_P (f
));
1719 xassert (valid_image_p (spec
));
1721 /* Look up SPEC in the hash table of the image cache. */
1722 hash
= sxhash (spec
, 0);
1723 img
= search_image_cache (f
, spec
, hash
);
1724 if (img
&& img
->load_failed_p
)
1726 free_image (f
, img
);
1730 /* If not found, create a new image and cache it. */
1734 img
= make_image (spec
, hash
);
1735 cache_image (f
, img
);
1736 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1737 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1738 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1740 /* If we can't load the image, and we don't have a width and
1741 height, use some arbitrary width and height so that we can
1742 draw a rectangle for it. */
1743 if (img
->load_failed_p
)
1747 value
= image_spec_value (spec
, QCwidth
, NULL
);
1748 img
->width
= (INTEGERP (value
)
1749 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1750 value
= image_spec_value (spec
, QCheight
, NULL
);
1751 img
->height
= (INTEGERP (value
)
1752 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1756 /* Handle image type independent image attributes
1757 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1758 `:background COLOR'. */
1759 Lisp_Object ascent
, margin
, relief
, bg
;
1761 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1762 if (INTEGERP (ascent
))
1763 img
->ascent
= XFASTINT (ascent
);
1764 else if (EQ (ascent
, Qcenter
))
1765 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1767 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1768 if (INTEGERP (margin
) && XINT (margin
) >= 0)
1769 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1770 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
1771 && INTEGERP (XCDR (margin
)))
1773 if (XINT (XCAR (margin
)) > 0)
1774 img
->hmargin
= XFASTINT (XCAR (margin
));
1775 if (XINT (XCDR (margin
)) > 0)
1776 img
->vmargin
= XFASTINT (XCDR (margin
));
1779 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1780 if (INTEGERP (relief
))
1782 img
->relief
= XINT (relief
);
1783 img
->hmargin
+= eabs (img
->relief
);
1784 img
->vmargin
+= eabs (img
->relief
);
1787 if (! img
->background_valid
)
1789 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1793 = x_alloc_image_color (f
, img
, bg
,
1794 FRAME_BACKGROUND_PIXEL (f
));
1795 img
->background_valid
= 1;
1799 /* Do image transformations and compute masks, unless we
1800 don't have the image yet. */
1801 if (!EQ (*img
->type
->type
, Qpostscript
))
1802 postprocess_image (f
, img
);
1808 /* We're using IMG, so set its timestamp to `now'. */
1809 EMACS_GET_TIME (now
);
1810 img
->timestamp
= EMACS_SECS (now
);
1812 /* Value is the image id. */
1817 /* Cache image IMG in the image cache of frame F. */
1820 cache_image (struct frame
*f
, struct image
*img
)
1822 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1825 /* Find a free slot in c->images. */
1826 for (i
= 0; i
< c
->used
; ++i
)
1827 if (c
->images
[i
] == NULL
)
1830 /* If no free slot found, maybe enlarge c->images. */
1831 if (i
== c
->used
&& c
->used
== c
->size
)
1834 c
->images
= (struct image
**) xrealloc (c
->images
,
1835 c
->size
* sizeof *c
->images
);
1838 /* Add IMG to c->images, and assign IMG an id. */
1844 /* Add IMG to the cache's hash table. */
1845 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1846 img
->next
= c
->buckets
[i
];
1848 img
->next
->prev
= img
;
1850 c
->buckets
[i
] = img
;
1854 /* Call FN on every image in the image cache of frame F. Used to mark
1855 Lisp Objects in the image cache. */
1857 /* Mark Lisp objects in image IMG. */
1860 mark_image (struct image
*img
)
1862 mark_object (img
->spec
);
1863 mark_object (img
->dependencies
);
1865 if (!NILP (img
->data
.lisp_val
))
1866 mark_object (img
->data
.lisp_val
);
1871 mark_image_cache (struct image_cache
*c
)
1876 for (i
= 0; i
< c
->used
; ++i
)
1878 mark_image (c
->images
[i
]);
1884 /***********************************************************************
1885 X / NS / W32 support code
1886 ***********************************************************************/
1890 /* Macro for defining functions that will be loaded from image DLLs. */
1891 #define DEF_IMGLIB_FN(rettype,func,args) rettype (FAR CDECL *fn_##func)args
1893 /* Macro for loading those image functions from the library. */
1894 #define LOAD_IMGLIB_FN(lib,func) { \
1895 fn_##func = (void *) GetProcAddress (lib, #func); \
1896 if (!fn_##func) return 0; \
1899 /* Load a DLL implementing an image type.
1900 The argument LIBRARIES is usually the variable
1901 `dynamic-library-alist', which associates a symbol, identifying
1902 an external DLL library, to a list of possible filenames.
1903 The function returns NULL if no library could be loaded for
1904 the given symbol, or if the library was previously loaded;
1905 else the handle of the DLL. */
1907 w32_delayed_load (Lisp_Object libraries
, Lisp_Object type
)
1909 HMODULE library
= NULL
;
1911 if (CONSP (libraries
) && NILP (Fassq (type
, Vimage_type_cache
)))
1913 Lisp_Object dlls
= Fassq (type
, libraries
);
1916 for (dlls
= XCDR (dlls
); CONSP (dlls
); dlls
= XCDR (dlls
))
1918 CHECK_STRING_CAR (dlls
);
1919 if (library
= LoadLibrary (SDATA (XCAR (dlls
))))
1927 #endif /* HAVE_NTGUI */
1929 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1930 XImagePtr
*, Pixmap
*);
1931 static void x_destroy_x_image (XImagePtr
);
1932 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1935 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1936 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1937 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1938 via xmalloc. Print error messages via image_error if an error
1939 occurs. Value is non-zero if successful.
1941 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1942 should indicate the bit depth of the image. */
1945 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1946 XImagePtr
*ximg
, Pixmap
*pixmap
)
1948 #ifdef HAVE_X_WINDOWS
1949 Display
*display
= FRAME_X_DISPLAY (f
);
1950 Window window
= FRAME_X_WINDOW (f
);
1951 Screen
*screen
= FRAME_X_SCREEN (f
);
1953 xassert (interrupt_input_blocked
);
1956 depth
= DefaultDepthOfScreen (screen
);
1957 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1958 depth
, ZPixmap
, 0, NULL
, width
, height
,
1959 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1962 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1966 /* Allocate image raster. */
1967 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
1969 /* Allocate a pixmap of the same size. */
1970 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1971 if (*pixmap
== NO_PIXMAP
)
1973 x_destroy_x_image (*ximg
);
1975 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1980 #endif /* HAVE_X_WINDOWS */
1984 BITMAPINFOHEADER
*header
;
1986 int scanline_width_bits
;
1988 int palette_colors
= 0;
1993 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1994 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1996 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2000 scanline_width_bits
= width
* depth
;
2001 remainder
= scanline_width_bits
% 32;
2004 scanline_width_bits
+= 32 - remainder
;
2006 /* Bitmaps with a depth less than 16 need a palette. */
2007 /* BITMAPINFO structure already contains the first RGBQUAD. */
2009 palette_colors
= 1 << depth
- 1;
2011 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2014 image_error ("Unable to allocate memory for XImage", Qnil
, Qnil
);
2018 header
= &(*ximg
)->info
.bmiHeader
;
2019 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2020 header
->biSize
= sizeof (*header
);
2021 header
->biWidth
= width
;
2022 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2023 header
->biPlanes
= 1;
2024 header
->biBitCount
= depth
;
2025 header
->biCompression
= BI_RGB
;
2026 header
->biClrUsed
= palette_colors
;
2028 /* TODO: fill in palette. */
2031 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2032 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2033 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2034 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2035 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2036 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2037 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2038 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2041 hdc
= get_frame_dc (f
);
2043 /* Create a DIBSection and raster array for the bitmap,
2044 and store its handle in *pixmap. */
2045 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2046 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2047 /* casting avoids a GCC warning */
2048 (void **)&((*ximg
)->data
), NULL
, 0);
2050 /* Realize display palette and garbage all frames. */
2051 release_frame_dc (f
, hdc
);
2053 if (*pixmap
== NULL
)
2055 DWORD err
= GetLastError ();
2056 Lisp_Object errcode
;
2057 /* All system errors are < 10000, so the following is safe. */
2058 XSETINT (errcode
, (int) err
);
2059 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2060 x_destroy_x_image (*ximg
);
2066 #endif /* HAVE_NTGUI */
2069 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2073 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2082 /* Destroy XImage XIMG. Free XIMG->data. */
2085 x_destroy_x_image (XImagePtr ximg
)
2087 xassert (interrupt_input_blocked
);
2090 #ifdef HAVE_X_WINDOWS
2093 XDestroyImage (ximg
);
2094 #endif /* HAVE_X_WINDOWS */
2096 /* Data will be freed by DestroyObject. */
2099 #endif /* HAVE_NTGUI */
2101 ns_release_object (ximg
);
2102 #endif /* HAVE_NS */
2107 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2108 are width and height of both the image and pixmap. */
2111 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2113 #ifdef HAVE_X_WINDOWS
2116 xassert (interrupt_input_blocked
);
2117 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2118 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2119 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2120 #endif /* HAVE_X_WINDOWS */
2123 #if 0 /* I don't think this is necessary looking at where it is used. */
2124 HDC hdc
= get_frame_dc (f
);
2125 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2126 release_frame_dc (f
, hdc
);
2128 #endif /* HAVE_NTGUI */
2131 xassert (ximg
== pixmap
);
2132 ns_retain_object (ximg
);
2137 /***********************************************************************
2139 ***********************************************************************/
2141 static unsigned char *slurp_file (char *, int *);
2144 /* Find image file FILE. Look in data-directory/images, then
2145 x-bitmap-file-path. Value is the encoded full name of the file
2146 found, or nil if not found. */
2149 x_find_image_file (Lisp_Object file
)
2151 Lisp_Object file_found
, search_path
;
2154 /* TODO I think this should use something like image-load-path
2155 instead. Unfortunately, that can contain non-string elements. */
2156 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2158 Vx_bitmap_file_path
);
2160 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2161 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2167 file_found
= ENCODE_FILE (file_found
);
2175 /* Read FILE into memory. Value is a pointer to a buffer allocated
2176 with xmalloc holding FILE's contents. Value is null if an error
2177 occurred. *SIZE is set to the size of the file. */
2179 static unsigned char *
2180 slurp_file (char *file
, int *size
)
2183 unsigned char *buf
= NULL
;
2186 if (stat (file
, &st
) == 0
2187 && (fp
= fopen (file
, "rb")) != NULL
2188 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2189 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2210 /***********************************************************************
2212 ***********************************************************************/
2214 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2215 static int xbm_load (struct frame
*f
, struct image
*img
);
2216 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2217 unsigned char *, unsigned char *);
2218 static int xbm_image_p (Lisp_Object object
);
2219 static int xbm_read_bitmap_data (struct frame
*f
,
2220 unsigned char *, unsigned char *,
2221 int *, int *, char **, int);
2222 static int xbm_file_p (Lisp_Object
);
2225 /* Indices of image specification fields in xbm_format, below. */
2227 enum xbm_keyword_index
2245 /* Vector of image_keyword structures describing the format
2246 of valid XBM image specifications. */
2248 static const struct image_keyword xbm_format
[XBM_LAST
] =
2250 {":type", IMAGE_SYMBOL_VALUE
, 1},
2251 {":file", IMAGE_STRING_VALUE
, 0},
2252 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2253 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2254 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2255 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2256 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2257 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2258 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2259 {":relief", IMAGE_INTEGER_VALUE
, 0},
2260 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2261 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2262 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2265 /* Structure describing the image type XBM. */
2267 static struct image_type xbm_type
=
2276 /* Tokens returned from xbm_scan. */
2285 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2286 A valid specification is a list starting with the symbol `image'
2287 The rest of the list is a property list which must contain an
2290 If the specification specifies a file to load, it must contain
2291 an entry `:file FILENAME' where FILENAME is a string.
2293 If the specification is for a bitmap loaded from memory it must
2294 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2295 WIDTH and HEIGHT are integers > 0. DATA may be:
2297 1. a string large enough to hold the bitmap data, i.e. it must
2298 have a size >= (WIDTH + 7) / 8 * HEIGHT
2300 2. a bool-vector of size >= WIDTH * HEIGHT
2302 3. a vector of strings or bool-vectors, one for each line of the
2305 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2306 may not be specified in this case because they are defined in the
2309 Both the file and data forms may contain the additional entries
2310 `:background COLOR' and `:foreground COLOR'. If not present,
2311 foreground and background of the frame on which the image is
2312 displayed is used. */
2315 xbm_image_p (Lisp_Object object
)
2317 struct image_keyword kw
[XBM_LAST
];
2319 memcpy (kw
, xbm_format
, sizeof kw
);
2320 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2323 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2325 if (kw
[XBM_FILE
].count
)
2327 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2330 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2332 /* In-memory XBM file. */
2333 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2341 /* Entries for `:width', `:height' and `:data' must be present. */
2342 if (!kw
[XBM_WIDTH
].count
2343 || !kw
[XBM_HEIGHT
].count
2344 || !kw
[XBM_DATA
].count
)
2347 data
= kw
[XBM_DATA
].value
;
2348 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2349 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2351 /* Check type of data, and width and height against contents of
2357 /* Number of elements of the vector must be >= height. */
2358 if (XVECTOR (data
)->size
< height
)
2361 /* Each string or bool-vector in data must be large enough
2362 for one line of the image. */
2363 for (i
= 0; i
< height
; ++i
)
2365 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2370 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2373 else if (BOOL_VECTOR_P (elt
))
2375 if (XBOOL_VECTOR (elt
)->size
< width
)
2382 else if (STRINGP (data
))
2385 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2388 else if (BOOL_VECTOR_P (data
))
2390 if (XBOOL_VECTOR (data
)->size
< width
* height
)
2401 /* Scan a bitmap file. FP is the stream to read from. Value is
2402 either an enumerator from enum xbm_token, or a character for a
2403 single-character token, or 0 at end of file. If scanning an
2404 identifier, store the lexeme of the identifier in SVAL. If
2405 scanning a number, store its value in *IVAL. */
2408 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2414 /* Skip white space. */
2415 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2420 else if (isdigit (c
))
2422 int value
= 0, digit
;
2424 if (c
== '0' && *s
< end
)
2427 if (c
== 'x' || c
== 'X')
2434 else if (c
>= 'a' && c
<= 'f')
2435 digit
= c
- 'a' + 10;
2436 else if (c
>= 'A' && c
<= 'F')
2437 digit
= c
- 'A' + 10;
2440 value
= 16 * value
+ digit
;
2443 else if (isdigit (c
))
2447 && (c
= *(*s
)++, isdigit (c
)))
2448 value
= 8 * value
+ c
- '0';
2455 && (c
= *(*s
)++, isdigit (c
)))
2456 value
= 10 * value
+ c
- '0';
2464 else if (isalpha (c
) || c
== '_')
2468 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2475 else if (c
== '/' && **s
== '*')
2477 /* C-style comment. */
2479 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2493 /* Create a Windows bitmap from X bitmap data. */
2495 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2497 static unsigned char swap_nibble
[16]
2498 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2499 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2500 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2501 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2503 unsigned char *bits
, *p
;
2506 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2507 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2508 bits
= (unsigned char *) alloca (height
* w2
);
2509 memset (bits
, 0, height
* w2
);
2510 for (i
= 0; i
< height
; i
++)
2513 for (j
= 0; j
< w1
; j
++)
2515 /* Bitswap XBM bytes to match how Windows does things. */
2516 unsigned char c
= *data
++;
2517 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2518 | (swap_nibble
[(c
>>4) & 0xf]));
2521 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2527 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2528 COLORREF foreground
, COLORREF background
)
2530 HDC hdc
, old_img_dc
, new_img_dc
;
2531 HGDIOBJ old_prev
, new_prev
;
2534 hdc
= get_frame_dc (f
);
2535 old_img_dc
= CreateCompatibleDC (hdc
);
2536 new_img_dc
= CreateCompatibleDC (hdc
);
2537 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2538 release_frame_dc (f
, hdc
);
2539 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2540 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2541 /* Windows convention for mono bitmaps is black = background,
2542 white = foreground. */
2543 SetTextColor (new_img_dc
, background
);
2544 SetBkColor (new_img_dc
, foreground
);
2546 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2549 SelectObject (old_img_dc
, old_prev
);
2550 SelectObject (new_img_dc
, new_prev
);
2551 DeleteDC (old_img_dc
);
2552 DeleteDC (new_img_dc
);
2553 DeleteObject (img
->pixmap
);
2554 if (new_pixmap
== 0)
2555 fprintf (stderr
, "Failed to convert image to color.\n");
2557 img
->pixmap
= new_pixmap
;
2560 #define XBM_BIT_SHUFFLE(b) (~(b))
2564 #define XBM_BIT_SHUFFLE(b) (b)
2566 #endif /* HAVE_NTGUI */
2570 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2571 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2572 int non_default_colors
)
2576 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2578 /* If colors were specified, transfer the bitmap to a color one. */
2579 if (non_default_colors
)
2580 convert_mono_to_color_image (f
, img
, fg
, bg
);
2582 #elif defined (HAVE_NS)
2583 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2587 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2590 img
->width
, img
->height
,
2592 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
2593 #endif /* !HAVE_NTGUI && !HAVE_NS */
2598 /* Replacement for XReadBitmapFileData which isn't available under old
2599 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2600 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2601 the image. Return in *DATA the bitmap data allocated with xmalloc.
2602 Value is non-zero if successful. DATA null means just test if
2603 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2604 is non-zero, inhibit the call to image_error when the image size is
2605 invalid (the bitmap remains unread). */
2608 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2609 int *width
, int *height
, char **data
,
2610 int inhibit_image_error
)
2612 unsigned char *s
= contents
;
2613 char buffer
[BUFSIZ
];
2616 int bytes_per_line
, i
, nbytes
;
2622 LA1 = xbm_scan (&s, end, buffer, &value)
2624 #define expect(TOKEN) \
2625 if (LA1 != (TOKEN)) \
2630 #define expect_ident(IDENT) \
2631 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2636 *width
= *height
= -1;
2639 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2641 /* Parse defines for width, height and hot-spots. */
2645 expect_ident ("define");
2646 expect (XBM_TK_IDENT
);
2648 if (LA1
== XBM_TK_NUMBER
)
2650 char *q
= strrchr (buffer
, '_');
2651 q
= q
? q
+ 1 : buffer
;
2652 if (strcmp (q
, "width") == 0)
2654 else if (strcmp (q
, "height") == 0)
2657 expect (XBM_TK_NUMBER
);
2660 if (!check_image_size (f
, *width
, *height
))
2662 if (!inhibit_image_error
)
2663 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2666 else if (data
== NULL
)
2669 /* Parse bits. Must start with `static'. */
2670 expect_ident ("static");
2671 if (LA1
== XBM_TK_IDENT
)
2673 if (strcmp (buffer
, "unsigned") == 0)
2676 expect_ident ("char");
2678 else if (strcmp (buffer
, "short") == 0)
2682 if (*width
% 16 && *width
% 16 < 9)
2685 else if (strcmp (buffer
, "char") == 0)
2693 expect (XBM_TK_IDENT
);
2699 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2700 nbytes
= bytes_per_line
* *height
;
2701 p
= *data
= (char *) xmalloc (nbytes
);
2705 for (i
= 0; i
< nbytes
; i
+= 2)
2708 expect (XBM_TK_NUMBER
);
2710 *p
++ = XBM_BIT_SHUFFLE (val
);
2711 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2712 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2714 if (LA1
== ',' || LA1
== '}')
2722 for (i
= 0; i
< nbytes
; ++i
)
2725 expect (XBM_TK_NUMBER
);
2727 *p
++ = XBM_BIT_SHUFFLE (val
);
2729 if (LA1
== ',' || LA1
== '}')
2754 /* Load XBM image IMG which will be displayed on frame F from buffer
2755 CONTENTS. END is the end of the buffer. Value is non-zero if
2759 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2766 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2770 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2771 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2772 int non_default_colors
= 0;
2775 xassert (img
->width
> 0 && img
->height
> 0);
2777 /* Get foreground and background colors, maybe allocate colors. */
2778 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2781 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2782 non_default_colors
= 1;
2784 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2787 background
= x_alloc_image_color (f
, img
, value
, background
);
2788 img
->background
= background
;
2789 img
->background_valid
= 1;
2790 non_default_colors
= 1;
2793 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2794 foreground
, background
,
2795 non_default_colors
);
2798 if (img
->pixmap
== NO_PIXMAP
)
2800 x_clear_image (f
, img
);
2801 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2807 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2813 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2816 xbm_file_p (Lisp_Object data
)
2819 return (STRINGP (data
)
2820 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2821 (SDATA (data
) + SBYTES (data
)),
2826 /* Fill image IMG which is used on frame F with pixmap data. Value is
2827 non-zero if successful. */
2830 xbm_load (struct frame
*f
, struct image
*img
)
2833 Lisp_Object file_name
;
2835 xassert (xbm_image_p (img
->spec
));
2837 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2838 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2839 if (STRINGP (file_name
))
2842 unsigned char *contents
;
2845 file
= x_find_image_file (file_name
);
2846 if (!STRINGP (file
))
2848 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2852 contents
= slurp_file (SSDATA (file
), &size
);
2853 if (contents
== NULL
)
2855 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2859 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2863 struct image_keyword fmt
[XBM_LAST
];
2865 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2866 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2867 int non_default_colors
= 0;
2870 int in_memory_file_p
= 0;
2872 /* See if data looks like an in-memory XBM file. */
2873 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2874 in_memory_file_p
= xbm_file_p (data
);
2876 /* Parse the image specification. */
2877 memcpy (fmt
, xbm_format
, sizeof fmt
);
2878 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2882 /* Get specified width, and height. */
2883 if (!in_memory_file_p
)
2885 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2886 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2887 xassert (img
->width
> 0 && img
->height
> 0);
2890 /* Get foreground and background colors, maybe allocate colors. */
2891 if (fmt
[XBM_FOREGROUND
].count
2892 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2894 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2896 non_default_colors
= 1;
2899 if (fmt
[XBM_BACKGROUND
].count
2900 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2902 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2904 non_default_colors
= 1;
2907 if (in_memory_file_p
)
2908 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2917 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2919 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2920 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2922 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2924 memcpy (p
, SDATA (line
), nbytes
);
2926 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2929 else if (STRINGP (data
))
2930 bits
= SSDATA (data
);
2932 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2938 /* Windows mono bitmaps are reversed compared with X. */
2939 invertedBits
= bits
;
2940 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2942 bits
= (char *) alloca (nbytes
);
2943 for (i
= 0; i
< nbytes
; i
++)
2944 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2947 /* Create the pixmap. */
2949 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2950 foreground
, background
,
2951 non_default_colors
);
2956 image_error ("Unable to create pixmap for XBM image `%s'",
2958 x_clear_image (f
, img
);
2968 /***********************************************************************
2970 ***********************************************************************/
2972 #if defined (HAVE_XPM) || defined (HAVE_NS)
2974 static int xpm_image_p (Lisp_Object object
);
2975 static int xpm_load (struct frame
*f
, struct image
*img
);
2976 static int xpm_valid_color_symbols_p (Lisp_Object
);
2978 #endif /* HAVE_XPM || HAVE_NS */
2982 /* Indicate to xpm.h that we don't have Xlib. */
2984 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2985 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2986 #define XColor xpm_XColor
2987 #define XImage xpm_XImage
2988 #define Display xpm_Display
2989 #define PIXEL_ALREADY_TYPEDEFED
2990 #include "X11/xpm.h"
2995 #undef PIXEL_ALREADY_TYPEDEFED
2997 #include "X11/xpm.h"
2998 #endif /* HAVE_NTGUI */
2999 #endif /* HAVE_XPM */
3001 #if defined (HAVE_XPM) || defined (HAVE_NS)
3002 /* The symbol `xpm' identifying XPM-format images. */
3006 /* Indices of image specification fields in xpm_format, below. */
3008 enum xpm_keyword_index
3024 /* Vector of image_keyword structures describing the format
3025 of valid XPM image specifications. */
3027 static const struct image_keyword xpm_format
[XPM_LAST
] =
3029 {":type", IMAGE_SYMBOL_VALUE
, 1},
3030 {":file", IMAGE_STRING_VALUE
, 0},
3031 {":data", IMAGE_STRING_VALUE
, 0},
3032 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3033 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3034 {":relief", IMAGE_INTEGER_VALUE
, 0},
3035 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3036 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3037 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3038 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3039 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3042 /* Structure describing the image type XPM. */
3044 static struct image_type xpm_type
=
3053 #ifdef HAVE_X_WINDOWS
3055 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3056 functions for allocating image colors. Our own functions handle
3057 color allocation failures more gracefully than the ones on the XPM
3060 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3061 #define ALLOC_XPM_COLORS
3063 #endif /* HAVE_X_WINDOWS */
3065 #ifdef ALLOC_XPM_COLORS
3067 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3068 static void xpm_free_color_cache (void);
3069 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3070 static int xpm_color_bucket (char *);
3071 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3074 /* An entry in a hash table used to cache color definitions of named
3075 colors. This cache is necessary to speed up XPM image loading in
3076 case we do color allocations ourselves. Without it, we would need
3077 a call to XParseColor per pixel in the image. */
3079 struct xpm_cached_color
3081 /* Next in collision chain. */
3082 struct xpm_cached_color
*next
;
3084 /* Color definition (RGB and pixel color). */
3091 /* The hash table used for the color cache, and its bucket vector
3094 #define XPM_COLOR_CACHE_BUCKETS 1001
3095 struct xpm_cached_color
**xpm_color_cache
;
3097 /* Initialize the color cache. */
3100 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3102 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3103 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3104 memset (xpm_color_cache
, 0, nbytes
);
3105 init_color_table ();
3107 if (attrs
->valuemask
& XpmColorSymbols
)
3112 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3113 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3114 attrs
->colorsymbols
[i
].value
, &color
))
3116 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3118 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3123 /* Free the color cache. */
3126 xpm_free_color_cache (void)
3128 struct xpm_cached_color
*p
, *next
;
3131 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3132 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3138 xfree (xpm_color_cache
);
3139 xpm_color_cache
= NULL
;
3140 free_color_table ();
3143 /* Return the bucket index for color named COLOR_NAME in the color
3147 xpm_color_bucket (char *color_name
)
3152 for (s
= color_name
; *s
; ++s
)
3154 return h
%= XPM_COLOR_CACHE_BUCKETS
;
3158 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3159 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3162 static struct xpm_cached_color
*
3163 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3166 struct xpm_cached_color
*p
;
3169 bucket
= xpm_color_bucket (color_name
);
3171 nbytes
= sizeof *p
+ strlen (color_name
);
3172 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3173 strcpy (p
->name
, color_name
);
3175 p
->next
= xpm_color_cache
[bucket
];
3176 xpm_color_cache
[bucket
] = p
;
3180 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3181 return the cached definition in *COLOR. Otherwise, make a new
3182 entry in the cache and allocate the color. Value is zero if color
3183 allocation failed. */
3186 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3188 struct xpm_cached_color
*p
;
3189 int h
= xpm_color_bucket (color_name
);
3191 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3192 if (strcmp (p
->name
, color_name
) == 0)
3197 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3200 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3202 p
= xpm_cache_color (f
, color_name
, color
, h
);
3204 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3205 with transparency, and it's useful. */
3206 else if (strcmp ("opaque", color_name
) == 0)
3208 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3209 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3210 p
= xpm_cache_color (f
, color_name
, color
, h
);
3217 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3218 CLOSURE is a pointer to the frame on which we allocate the
3219 color. Return in *COLOR the allocated color. Value is non-zero
3223 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3226 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3230 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3231 is a pointer to the frame on which we allocate the color. Value is
3232 non-zero if successful. */
3235 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3240 #endif /* ALLOC_XPM_COLORS */
3245 /* XPM library details. */
3247 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3248 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3249 xpm_XImage
**, XpmAttributes
*));
3250 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3251 xpm_XImage
**, XpmAttributes
*));
3252 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3255 init_xpm_functions (Lisp_Object libraries
)
3259 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3262 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3263 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3264 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3265 LOAD_IMGLIB_FN (library
, XImageFree
);
3269 #endif /* HAVE_NTGUI */
3272 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3273 for XPM images. Such a list must consist of conses whose car and
3277 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3279 while (CONSP (color_symbols
))
3281 Lisp_Object sym
= XCAR (color_symbols
);
3283 || !STRINGP (XCAR (sym
))
3284 || !STRINGP (XCDR (sym
)))
3286 color_symbols
= XCDR (color_symbols
);
3289 return NILP (color_symbols
);
3293 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3296 xpm_image_p (Lisp_Object object
)
3298 struct image_keyword fmt
[XPM_LAST
];
3299 memcpy (fmt
, xpm_format
, sizeof fmt
);
3300 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3301 /* Either `:file' or `:data' must be present. */
3302 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3303 /* Either no `:color-symbols' or it's a list of conses
3304 whose car and cdr are strings. */
3305 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3306 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3309 #endif /* HAVE_XPM || HAVE_NS */
3311 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
3313 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3315 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3317 XpmAttributes attrs
;
3318 Pixmap bitmap
, mask
;
3320 memset (&attrs
, 0, sizeof attrs
);
3322 attrs
.visual
= FRAME_X_VISUAL (f
);
3323 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3324 attrs
.valuemask
|= XpmVisual
;
3325 attrs
.valuemask
|= XpmColormap
;
3327 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3328 (char **) bits
, &bitmap
, &mask
, &attrs
);
3329 if (rc
!= XpmSuccess
)
3331 XpmFreeAttributes (&attrs
);
3335 id
= x_allocate_bitmap_record (f
);
3336 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3337 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3338 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3339 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3340 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3341 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3342 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3343 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3345 XpmFreeAttributes (&attrs
);
3348 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3350 /* Load image IMG which will be displayed on frame F. Value is
3351 non-zero if successful. */
3356 xpm_load (struct frame
*f
, struct image
*img
)
3359 XpmAttributes attrs
;
3360 Lisp_Object specified_file
, color_symbols
;
3363 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3364 #endif /* HAVE_NTGUI */
3366 /* Configure the XPM lib. Use the visual of frame F. Allocate
3367 close colors. Return colors allocated. */
3368 memset (&attrs
, 0, sizeof attrs
);
3371 attrs
.visual
= FRAME_X_VISUAL (f
);
3372 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3373 attrs
.valuemask
|= XpmVisual
;
3374 attrs
.valuemask
|= XpmColormap
;
3375 #endif /* HAVE_NTGUI */
3377 #ifdef ALLOC_XPM_COLORS
3378 /* Allocate colors with our own functions which handle
3379 failing color allocation more gracefully. */
3380 attrs
.color_closure
= f
;
3381 attrs
.alloc_color
= xpm_alloc_color
;
3382 attrs
.free_colors
= xpm_free_colors
;
3383 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3384 #else /* not ALLOC_XPM_COLORS */
3385 /* Let the XPM lib allocate colors. */
3386 attrs
.valuemask
|= XpmReturnAllocPixels
;
3387 #ifdef XpmAllocCloseColors
3388 attrs
.alloc_close_colors
= 1;
3389 attrs
.valuemask
|= XpmAllocCloseColors
;
3390 #else /* not XpmAllocCloseColors */
3391 attrs
.closeness
= 600;
3392 attrs
.valuemask
|= XpmCloseness
;
3393 #endif /* not XpmAllocCloseColors */
3394 #endif /* ALLOC_XPM_COLORS */
3396 /* If image specification contains symbolic color definitions, add
3397 these to `attrs'. */
3398 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3399 if (CONSP (color_symbols
))
3402 XpmColorSymbol
*xpm_syms
;
3405 attrs
.valuemask
|= XpmColorSymbols
;
3407 /* Count number of symbols. */
3408 attrs
.numsymbols
= 0;
3409 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3412 /* Allocate an XpmColorSymbol array. */
3413 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3414 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3415 memset (xpm_syms
, 0, size
);
3416 attrs
.colorsymbols
= xpm_syms
;
3418 /* Fill the color symbol array. */
3419 for (tail
= color_symbols
, i
= 0;
3421 ++i
, tail
= XCDR (tail
))
3425 char *empty_string
= (char *) "";
3427 if (!CONSP (XCAR (tail
)))
3429 xpm_syms
[i
].name
= empty_string
;
3430 xpm_syms
[i
].value
= empty_string
;
3433 name
= XCAR (XCAR (tail
));
3434 color
= XCDR (XCAR (tail
));
3437 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3438 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3441 xpm_syms
[i
].name
= empty_string
;
3442 if (STRINGP (color
))
3444 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3445 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3448 xpm_syms
[i
].value
= empty_string
;
3452 /* Create a pixmap for the image, either from a file, or from a
3453 string buffer containing data in the same format as an XPM file. */
3454 #ifdef ALLOC_XPM_COLORS
3455 xpm_init_color_cache (f
, &attrs
);
3458 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3462 HDC frame_dc
= get_frame_dc (f
);
3463 hdc
= CreateCompatibleDC (frame_dc
);
3464 release_frame_dc (f
, frame_dc
);
3466 #endif /* HAVE_NTGUI */
3468 if (STRINGP (specified_file
))
3470 Lisp_Object file
= x_find_image_file (specified_file
);
3471 if (!STRINGP (file
))
3473 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3474 #ifdef ALLOC_XPM_COLORS
3475 xpm_free_color_cache ();
3481 /* XpmReadFileToPixmap is not available in the Windows port of
3482 libxpm. But XpmReadFileToImage almost does what we want. */
3483 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3484 &xpm_image
, &xpm_mask
,
3487 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3488 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3490 #endif /* HAVE_NTGUI */
3494 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3495 if (!STRINGP (buffer
))
3497 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3498 #ifdef ALLOC_XPM_COLORS
3499 xpm_free_color_cache ();
3504 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3505 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3506 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3507 &xpm_image
, &xpm_mask
,
3510 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3512 &img
->pixmap
, &img
->mask
,
3514 #endif /* HAVE_NTGUI */
3517 if (rc
== XpmSuccess
)
3519 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3520 img
->colors
= colors_in_color_table (&img
->ncolors
);
3521 #else /* not ALLOC_XPM_COLORS */
3525 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3526 plus some duplicate attributes. */
3527 if (xpm_image
&& xpm_image
->bitmap
)
3529 img
->pixmap
= xpm_image
->bitmap
;
3530 /* XImageFree in libXpm frees XImage struct without destroying
3531 the bitmap, which is what we want. */
3532 fn_XImageFree (xpm_image
);
3534 if (xpm_mask
&& xpm_mask
->bitmap
)
3536 /* The mask appears to be inverted compared with what we expect.
3537 TODO: invert our expectations. See other places where we
3538 have to invert bits because our idea of masks is backwards. */
3540 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3542 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3543 SelectObject (hdc
, old_obj
);
3545 img
->mask
= xpm_mask
->bitmap
;
3546 fn_XImageFree (xpm_mask
);
3551 #endif /* HAVE_NTGUI */
3553 /* Remember allocated colors. */
3554 img
->ncolors
= attrs
.nalloc_pixels
;
3555 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
3556 * sizeof *img
->colors
);
3557 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3559 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3560 #ifdef DEBUG_X_COLORS
3561 register_color (img
->colors
[i
]);
3564 #endif /* not ALLOC_XPM_COLORS */
3566 img
->width
= attrs
.width
;
3567 img
->height
= attrs
.height
;
3568 xassert (img
->width
> 0 && img
->height
> 0);
3570 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3572 fn_XpmFreeAttributes (&attrs
);
3574 XpmFreeAttributes (&attrs
);
3575 #endif /* HAVE_NTGUI */
3581 #endif /* HAVE_NTGUI */
3586 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3589 case XpmFileInvalid
:
3590 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3594 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3597 case XpmColorFailed
:
3598 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3602 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3607 #ifdef ALLOC_XPM_COLORS
3608 xpm_free_color_cache ();
3610 return rc
== XpmSuccess
;
3613 #endif /* HAVE_XPM */
3615 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3617 /* XPM support functions for NS where libxpm is not available.
3618 Only XPM version 3 (without any extensions) is supported. */
3620 static int xpm_scan (const unsigned char **, const unsigned char *,
3621 const unsigned char **, int *);
3622 static Lisp_Object xpm_make_color_table_v
3623 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3624 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3625 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3627 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3628 const unsigned char *, int);
3629 static Lisp_Object xpm_make_color_table_h
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_h (Lisp_Object
, const unsigned char *,
3634 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3635 const unsigned char *, int);
3636 static int xpm_str_to_color_key (const char *);
3637 static int xpm_load_image (struct frame
*, struct image
*,
3638 const unsigned char *, const unsigned char *);
3640 /* Tokens returned from xpm_scan. */
3649 /* Scan an XPM data and return a character (< 256) or a token defined
3650 by enum xpm_token above. *S and END are the start (inclusive) and
3651 the end (exclusive) addresses of the data, respectively. Advance
3652 *S while scanning. If token is either XPM_TK_IDENT or
3653 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3654 length of the corresponding token, respectively. */
3657 xpm_scan (const unsigned char **s
,
3658 const unsigned char *end
,
3659 const unsigned char **beg
,
3666 /* Skip white-space. */
3667 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3670 /* gnus-pointer.xpm uses '-' in its identifier.
3671 sb-dir-plus.xpm uses '+' in its identifier. */
3672 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3676 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3679 return XPM_TK_IDENT
;
3684 while (*s
< end
&& **s
!= '"')
3689 return XPM_TK_STRING
;
3693 if (*s
< end
&& **s
== '*')
3695 /* C-style comment. */
3699 while (*s
< end
&& *(*s
)++ != '*')
3702 while (*s
< end
&& **s
!= '/');
3716 /* Functions for color table lookup in XPM data. A key is a string
3717 specifying the color of each pixel in XPM data. A value is either
3718 an integer that specifies a pixel color, Qt that specifies
3719 transparency, or Qnil for the unspecified color. If the length of
3720 the key string is one, a vector is used as a table. Otherwise, a
3721 hash table is used. */
3724 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3725 const unsigned char *,
3728 Lisp_Object (**get_func
) (Lisp_Object
,
3729 const unsigned char *,
3732 *put_func
= xpm_put_color_table_v
;
3733 *get_func
= xpm_get_color_table_v
;
3734 return Fmake_vector (make_number (256), Qnil
);
3738 xpm_put_color_table_v (Lisp_Object color_table
,
3739 const unsigned char *chars_start
,
3743 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3747 xpm_get_color_table_v (Lisp_Object color_table
,
3748 const unsigned char *chars_start
,
3751 return XVECTOR (color_table
)->contents
[*chars_start
];
3755 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3756 const unsigned char *,
3759 Lisp_Object (**get_func
) (Lisp_Object
,
3760 const unsigned char *,
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 (Lisp_Object color_table
,
3773 const unsigned char *chars_start
,
3777 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3779 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3781 hash_lookup (table
, chars
, &hash_code
);
3782 hash_put (table
, chars
, color
, hash_code
);
3786 xpm_get_color_table_h (Lisp_Object color_table
,
3787 const unsigned char *chars_start
,
3790 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3791 int i
= hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
),
3794 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3797 enum xpm_color_key
{
3805 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3808 xpm_str_to_color_key (const char *s
)
3813 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3815 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3821 xpm_load_image (struct frame
*f
,
3823 const unsigned char *contents
,
3824 const unsigned char *end
)
3826 const unsigned char *s
= contents
, *beg
, *str
;
3827 unsigned char buffer
[BUFSIZ
];
3828 int width
, height
, x
, y
;
3829 int num_colors
, chars_per_pixel
;
3831 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3832 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3833 Lisp_Object frame
, color_symbols
, color_table
;
3834 int best_key
, have_mask
= 0;
3835 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3838 LA1 = xpm_scan (&s, end, &beg, &len)
3840 #define expect(TOKEN) \
3841 if (LA1 != (TOKEN)) \
3846 #define expect_ident(IDENT) \
3847 if (LA1 == XPM_TK_IDENT \
3848 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3853 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3857 expect_ident ("static");
3858 expect_ident ("char");
3860 expect (XPM_TK_IDENT
);
3865 expect (XPM_TK_STRING
);
3868 memcpy (buffer
, beg
, len
);
3870 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3871 &num_colors
, &chars_per_pixel
) != 4
3872 || width
<= 0 || height
<= 0
3873 || num_colors
<= 0 || chars_per_pixel
<= 0)
3876 if (!check_image_size (f
, width
, height
))
3878 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3884 XSETFRAME (frame
, f
);
3885 if (!NILP (Fxw_display_color_p (frame
)))
3886 best_key
= XPM_COLOR_KEY_C
;
3887 else if (!NILP (Fx_display_grayscale_p (frame
)))
3888 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3889 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3891 best_key
= XPM_COLOR_KEY_M
;
3893 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3894 if (chars_per_pixel
== 1)
3895 color_table
= xpm_make_color_table_v (&put_color_table
,
3898 color_table
= xpm_make_color_table_h (&put_color_table
,
3901 while (num_colors
-- > 0)
3903 char *color
, *max_color
;
3904 int key
, next_key
, max_key
= 0;
3905 Lisp_Object symbol_color
= Qnil
, color_val
;
3908 expect (XPM_TK_STRING
);
3909 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3911 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3912 buffer
[len
- chars_per_pixel
] = '\0';
3914 str
= strtok (buffer
, " \t");
3917 key
= xpm_str_to_color_key (str
);
3922 color
= strtok (NULL
, " \t");
3926 while ((str
= strtok (NULL
, " \t")) != NULL
)
3928 next_key
= xpm_str_to_color_key (str
);
3931 color
[strlen (color
)] = ' ';
3934 if (key
== XPM_COLOR_KEY_S
)
3936 if (NILP (symbol_color
))
3937 symbol_color
= build_string (color
);
3939 else if (max_key
< key
&& key
<= best_key
)
3949 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3951 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3953 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3955 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3957 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3959 color_val
= make_number (cdef
.pixel
);
3962 if (NILP (color_val
) && max_key
> 0)
3964 if (xstrcasecmp (max_color
, "None") == 0)
3966 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3967 color_val
= make_number (cdef
.pixel
);
3969 if (!NILP (color_val
))
3970 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3975 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3976 &ximg
, &img
->pixmap
)
3978 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3979 &mask_img
, &img
->mask
)
3983 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3987 for (y
= 0; y
< height
; y
++)
3989 expect (XPM_TK_STRING
);
3991 if (len
< width
* chars_per_pixel
)
3993 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3995 Lisp_Object color_val
=
3996 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
3998 XPutPixel (ximg
, x
, y
,
3999 (INTEGERP (color_val
) ? XINT (color_val
)
4000 : FRAME_FOREGROUND_PIXEL (f
)));
4002 XPutPixel (mask_img
, x
, y
,
4003 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4004 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4006 if (EQ (color_val
, Qt
))
4007 ns_set_alpha (ximg
, x
, y
, 0);
4015 img
->height
= height
;
4017 /* Maybe fill in the background field while we have ximg handy. */
4018 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4019 IMAGE_BACKGROUND (img
, f
, ximg
);
4021 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4022 x_destroy_x_image (ximg
);
4026 /* Fill in the background_transparent field while we have the
4028 image_background_transparent (img
, f
, mask_img
);
4030 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4031 x_destroy_x_image (mask_img
);
4035 x_destroy_x_image (mask_img
);
4036 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4037 img
->mask
= NO_PIXMAP
;
4043 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4045 x_destroy_x_image (ximg
);
4046 x_destroy_x_image (mask_img
);
4047 x_clear_image (f
, img
);
4056 xpm_load (struct frame
*f
,
4060 Lisp_Object file_name
;
4062 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4063 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4064 if (STRINGP (file_name
))
4067 unsigned char *contents
;
4070 file
= x_find_image_file (file_name
);
4071 if (!STRINGP (file
))
4073 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4077 contents
= slurp_file (SDATA (file
), &size
);
4078 if (contents
== NULL
)
4080 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4084 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4091 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4092 if (!STRINGP (data
))
4094 image_error ("Invalid image data `%s'", data
, Qnil
);
4097 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4098 SDATA (data
) + SBYTES (data
));
4104 #endif /* HAVE_NS && !HAVE_XPM */
4108 /***********************************************************************
4110 ***********************************************************************/
4112 #ifdef COLOR_TABLE_SUPPORT
4114 /* An entry in the color table mapping an RGB color to a pixel color. */
4119 unsigned long pixel
;
4121 /* Next in color table collision list. */
4122 struct ct_color
*next
;
4125 /* The bucket vector size to use. Must be prime. */
4129 /* Value is a hash of the RGB color given by R, G, and B. */
4131 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4133 /* The color hash table. */
4135 struct ct_color
**ct_table
;
4137 /* Number of entries in the color table. */
4139 int ct_colors_allocated
;
4141 /* Initialize the color table. */
4144 init_color_table (void)
4146 int size
= CT_SIZE
* sizeof (*ct_table
);
4147 ct_table
= (struct ct_color
**) xmalloc (size
);
4148 memset (ct_table
, 0, size
);
4149 ct_colors_allocated
= 0;
4153 /* Free memory associated with the color table. */
4156 free_color_table (void)
4159 struct ct_color
*p
, *next
;
4161 for (i
= 0; i
< CT_SIZE
; ++i
)
4162 for (p
= ct_table
[i
]; p
; p
= next
)
4173 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4174 entry for that color already is in the color table, return the
4175 pixel color of that entry. Otherwise, allocate a new color for R,
4176 G, B, and make an entry in the color table. */
4178 static unsigned long
4179 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4181 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4182 int i
= hash
% CT_SIZE
;
4184 Display_Info
*dpyinfo
;
4186 /* Handle TrueColor visuals specially, which improves performance by
4187 two orders of magnitude. Freeing colors on TrueColor visuals is
4188 a nop, and pixel colors specify RGB values directly. See also
4189 the Xlib spec, chapter 3.1. */
4190 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4191 if (dpyinfo
->red_bits
> 0)
4193 unsigned long pr
, pg
, pb
;
4195 /* Apply gamma-correction like normal color allocation does. */
4199 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4200 gamma_correct (f
, &color
);
4201 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4204 /* Scale down RGB values to the visual's bits per RGB, and shift
4205 them to the right position in the pixel color. Note that the
4206 original RGB values are 16-bit values, as usual in X. */
4207 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4208 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4209 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4211 /* Assemble the pixel color. */
4212 return pr
| pg
| pb
;
4215 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4216 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4222 #ifdef HAVE_X_WINDOWS
4231 cmap
= FRAME_X_COLORMAP (f
);
4232 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4235 ++ct_colors_allocated
;
4236 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4240 p
->pixel
= color
.pixel
;
4241 p
->next
= ct_table
[i
];
4245 return FRAME_FOREGROUND_PIXEL (f
);
4250 color
= PALETTERGB (r
, g
, b
);
4252 color
= RGB_TO_ULONG (r
, g
, b
);
4253 #endif /* HAVE_NTGUI */
4254 ++ct_colors_allocated
;
4255 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4260 p
->next
= ct_table
[i
];
4262 #endif /* HAVE_X_WINDOWS */
4270 /* Look up pixel color PIXEL which is used on frame F in the color
4271 table. If not already present, allocate it. Value is PIXEL. */
4273 static unsigned long
4274 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4276 int i
= pixel
% CT_SIZE
;
4279 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4280 if (p
->pixel
== pixel
)
4289 #ifdef HAVE_X_WINDOWS
4290 cmap
= FRAME_X_COLORMAP (f
);
4291 color
.pixel
= pixel
;
4292 x_query_color (f
, &color
);
4293 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4296 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4297 color
.pixel
= pixel
;
4298 XQueryColor (NULL
, cmap
, &color
);
4299 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4301 #endif /* HAVE_X_WINDOWS */
4305 ++ct_colors_allocated
;
4307 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4312 p
->next
= ct_table
[i
];
4316 return FRAME_FOREGROUND_PIXEL (f
);
4322 /* Value is a vector of all pixel colors contained in the color table,
4323 allocated via xmalloc. Set *N to the number of colors. */
4325 static unsigned long *
4326 colors_in_color_table (int *n
)
4330 unsigned long *colors
;
4332 if (ct_colors_allocated
== 0)
4339 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4341 *n
= ct_colors_allocated
;
4343 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4344 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4345 colors
[j
++] = p
->pixel
;
4351 #else /* COLOR_TABLE_SUPPORT */
4353 static unsigned long
4354 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4356 unsigned long pixel
;
4359 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4360 #endif /* HAVE_NTGUI */
4363 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4364 #endif /* HAVE_NS */
4369 init_color_table (void)
4372 #endif /* COLOR_TABLE_SUPPORT */
4375 /***********************************************************************
4377 ***********************************************************************/
4379 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4380 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4381 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4384 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4385 #endif /* HAVE_NTGUI */
4387 /* Edge detection matrices for different edge-detection
4390 static int emboss_matrix
[9] = {
4392 2, -1, 0, /* y - 1 */
4394 0, 1, -2 /* y + 1 */
4397 static int laplace_matrix
[9] = {
4399 1, 0, 0, /* y - 1 */
4401 0, 0, -1 /* y + 1 */
4404 /* Value is the intensity of the color whose red/green/blue values
4407 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4410 /* On frame F, return an array of XColor structures describing image
4411 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4412 non-zero means also fill the red/green/blue members of the XColor
4413 structures. Value is a pointer to the array of XColors structures,
4414 allocated with xmalloc; it must be freed by the caller. */
4417 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4421 XImagePtr_or_DC ximg
;
4425 #endif /* HAVE_NTGUI */
4427 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
4430 /* Get the X image IMG->pixmap. */
4431 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4432 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4434 /* Load the image into a memory device context. */
4435 hdc
= get_frame_dc (f
);
4436 ximg
= CreateCompatibleDC (hdc
);
4437 release_frame_dc (f
, hdc
);
4438 prev
= SelectObject (ximg
, img
->pixmap
);
4439 #endif /* HAVE_NTGUI */
4441 /* Fill the `pixel' members of the XColor array. I wished there
4442 were an easy and portable way to circumvent XGetPixel. */
4444 for (y
= 0; y
< img
->height
; ++y
)
4448 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4449 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4450 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4452 x_query_colors (f
, row
, img
->width
);
4456 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4458 /* W32_TODO: palette support needed here? */
4459 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4462 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4463 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4464 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4467 #endif /* HAVE_X_WINDOWS */
4470 Destroy_Image (ximg
, prev
);
4477 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4478 created with CreateDIBSection, with the pointer to the bit values
4479 stored in ximg->data. */
4482 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4484 int width
= ximg
->info
.bmiHeader
.biWidth
;
4485 unsigned char * pixel
;
4487 /* True color images. */
4488 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4490 int rowbytes
= width
* 3;
4491 /* Ensure scanlines are aligned on 4 byte boundaries. */
4493 rowbytes
+= 4 - (rowbytes
% 4);
4495 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4496 /* Windows bitmaps are in BGR order. */
4497 *pixel
= GetBValue (color
);
4498 *(pixel
+ 1) = GetGValue (color
);
4499 *(pixel
+ 2) = GetRValue (color
);
4501 /* Monochrome images. */
4502 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4504 int rowbytes
= width
/ 8;
4505 /* Ensure scanlines are aligned on 4 byte boundaries. */
4507 rowbytes
+= 4 - (rowbytes
% 4);
4508 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4509 /* Filter out palette info. */
4510 if (color
& 0x00ffffff)
4511 *pixel
= *pixel
| (1 << x
% 8);
4513 *pixel
= *pixel
& ~(1 << x
% 8);
4516 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4519 #endif /* HAVE_NTGUI */
4521 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4522 RGB members are set. F is the frame on which this all happens.
4523 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4526 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4529 XImagePtr oimg
= NULL
;
4533 init_color_table ();
4535 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4538 for (y
= 0; y
< img
->height
; ++y
)
4539 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4541 unsigned long pixel
;
4542 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4543 XPutPixel (oimg
, x
, y
, pixel
);
4547 x_clear_image_1 (f
, img
, 1, 0, 1);
4549 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4550 x_destroy_x_image (oimg
);
4551 img
->pixmap
= pixmap
;
4552 #ifdef COLOR_TABLE_SUPPORT
4553 img
->colors
= colors_in_color_table (&img
->ncolors
);
4554 free_color_table ();
4555 #endif /* COLOR_TABLE_SUPPORT */
4559 /* On frame F, perform edge-detection on image IMG.
4561 MATRIX is a nine-element array specifying the transformation
4562 matrix. See emboss_matrix for an example.
4564 COLOR_ADJUST is a color adjustment added to each pixel of the
4568 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4570 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4574 for (i
= sum
= 0; i
< 9; ++i
)
4575 sum
+= eabs (matrix
[i
]);
4577 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4579 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
4581 for (y
= 0; y
< img
->height
; ++y
)
4583 p
= COLOR (new, 0, y
);
4584 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4585 p
= COLOR (new, img
->width
- 1, y
);
4586 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4589 for (x
= 1; x
< img
->width
- 1; ++x
)
4591 p
= COLOR (new, x
, 0);
4592 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4593 p
= COLOR (new, x
, img
->height
- 1);
4594 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4597 for (y
= 1; y
< img
->height
- 1; ++y
)
4599 p
= COLOR (new, 1, y
);
4601 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4603 int r
, g
, b
, yy
, xx
;
4606 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4607 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4610 XColor
*t
= COLOR (colors
, xx
, yy
);
4611 r
+= matrix
[i
] * t
->red
;
4612 g
+= matrix
[i
] * t
->green
;
4613 b
+= matrix
[i
] * t
->blue
;
4616 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4617 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4618 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4619 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4624 x_from_xcolors (f
, img
, new);
4630 /* Perform the pre-defined `emboss' edge-detection on image IMG
4634 x_emboss (struct frame
*f
, struct image
*img
)
4636 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4640 /* Transform image IMG which is used on frame F with a Laplace
4641 edge-detection algorithm. The result is an image that can be used
4642 to draw disabled buttons, for example. */
4645 x_laplace (struct frame
*f
, struct image
*img
)
4647 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4651 /* Perform edge-detection on image IMG on frame F, with specified
4652 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4654 MATRIX must be either
4656 - a list of at least 9 numbers in row-major form
4657 - a vector of at least 9 numbers
4659 COLOR_ADJUST nil means use a default; otherwise it must be a
4663 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4664 Lisp_Object color_adjust
)
4672 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4673 ++i
, matrix
= XCDR (matrix
))
4674 trans
[i
] = XFLOATINT (XCAR (matrix
));
4676 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4678 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4679 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4682 if (NILP (color_adjust
))
4683 color_adjust
= make_number (0xffff / 2);
4685 if (i
== 9 && NUMBERP (color_adjust
))
4686 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4690 /* Transform image IMG on frame F so that it looks disabled. */
4693 x_disable_image (struct frame
*f
, struct image
*img
)
4695 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4697 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4699 int n_planes
= dpyinfo
->n_planes
;
4700 #endif /* HAVE_NTGUI */
4704 /* Color (or grayscale). Convert to gray, and equalize. Just
4705 drawing such images with a stipple can look very odd, so
4706 we're using this method instead. */
4707 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4709 const int h
= 15000;
4710 const int l
= 30000;
4712 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4716 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4717 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4718 p
->red
= p
->green
= p
->blue
= i2
;
4721 x_from_xcolors (f
, img
, colors
);
4724 /* Draw a cross over the disabled image, if we must or if we
4726 if (n_planes
< 2 || cross_disabled_images
)
4729 Display
*dpy
= FRAME_X_DISPLAY (f
);
4732 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4734 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4736 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4737 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4738 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4739 img
->width
- 1, img
->height
- 1);
4740 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4746 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4747 XSetForeground (dpy
, gc
, MaskForeground (f
));
4748 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4749 img
->width
- 1, img
->height
- 1);
4750 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4754 #endif /* !HAVE_NS */
4759 hdc
= get_frame_dc (f
);
4760 bmpdc
= CreateCompatibleDC (hdc
);
4761 release_frame_dc (f
, hdc
);
4763 prev
= SelectObject (bmpdc
, img
->pixmap
);
4765 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4766 MoveToEx (bmpdc
, 0, 0, NULL
);
4767 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4768 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4769 LineTo (bmpdc
, img
->width
- 1, 0);
4773 SelectObject (bmpdc
, img
->mask
);
4774 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4775 MoveToEx (bmpdc
, 0, 0, NULL
);
4776 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4777 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4778 LineTo (bmpdc
, img
->width
- 1, 0);
4780 SelectObject (bmpdc
, prev
);
4782 #endif /* HAVE_NTGUI */
4787 /* Build a mask for image IMG which is used on frame F. FILE is the
4788 name of an image file, for error messages. HOW determines how to
4789 determine the background color of IMG. If it is a list '(R G B)',
4790 with R, G, and B being integers >= 0, take that as the color of the
4791 background. Otherwise, determine the background color of IMG
4792 heuristically. Value is non-zero if successful. */
4795 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4797 XImagePtr_or_DC ximg
;
4805 #endif /* HAVE_NTGUI */
4806 int x
, y
, rc
, use_img_background
;
4807 unsigned long bg
= 0;
4811 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4812 img
->mask
= NO_PIXMAP
;
4813 img
->background_transparent_valid
= 0;
4818 /* Create an image and pixmap serving as mask. */
4819 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4820 &mask_img
, &img
->mask
);
4823 #endif /* !HAVE_NS */
4825 /* Get the X image of IMG->pixmap. */
4826 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4827 img
->width
, img
->height
,
4830 /* Create the bit array serving as mask. */
4831 row_width
= (img
->width
+ 7) / 8;
4832 mask_img
= xmalloc (row_width
* img
->height
);
4833 memset (mask_img
, 0, row_width
* img
->height
);
4835 /* Create a memory device context for IMG->pixmap. */
4836 frame_dc
= get_frame_dc (f
);
4837 ximg
= CreateCompatibleDC (frame_dc
);
4838 release_frame_dc (f
, frame_dc
);
4839 prev
= SelectObject (ximg
, img
->pixmap
);
4840 #endif /* HAVE_NTGUI */
4842 /* Determine the background color of ximg. If HOW is `(R G B)'
4843 take that as color. Otherwise, use the image's background color. */
4844 use_img_background
= 1;
4850 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4852 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4856 if (i
== 3 && NILP (how
))
4858 char color_name
[30];
4859 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4862 0x00ffffff & /* Filter out palette info. */
4863 #endif /* HAVE_NTGUI */
4864 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4865 use_img_background
= 0;
4869 if (use_img_background
)
4870 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4872 /* Set all bits in mask_img to 1 whose color in ximg is different
4873 from the background color bg. */
4875 for (y
= 0; y
< img
->height
; ++y
)
4876 for (x
= 0; x
< img
->width
; ++x
)
4878 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4879 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4881 if (XGetPixel (ximg
, x
, y
) == bg
)
4882 ns_set_alpha (ximg
, x
, y
, 0);
4883 #endif /* HAVE_NS */
4885 /* Fill in the background_transparent field while we have the mask handy. */
4886 image_background_transparent (img
, f
, mask_img
);
4888 /* Put mask_img into img->mask. */
4889 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4890 x_destroy_x_image (mask_img
);
4891 #endif /* !HAVE_NS */
4893 for (y
= 0; y
< img
->height
; ++y
)
4894 for (x
= 0; x
< img
->width
; ++x
)
4896 COLORREF p
= GetPixel (ximg
, x
, y
);
4898 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4901 /* Create the mask image. */
4902 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4904 /* Fill in the background_transparent field while we have the mask handy. */
4905 SelectObject (ximg
, img
->mask
);
4906 image_background_transparent (img
, f
, ximg
);
4908 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4910 #endif /* HAVE_NTGUI */
4912 Destroy_Image (ximg
, prev
);
4918 /***********************************************************************
4919 PBM (mono, gray, color)
4920 ***********************************************************************/
4922 static int pbm_image_p (Lisp_Object object
);
4923 static int pbm_load (struct frame
*f
, struct image
*img
);
4924 static int pbm_scan_number (unsigned char **, unsigned char *);
4926 /* The symbol `pbm' identifying images of this type. */
4930 /* Indices of image specification fields in gs_format, below. */
4932 enum pbm_keyword_index
4948 /* Vector of image_keyword structures describing the format
4949 of valid user-defined image specifications. */
4951 static const struct image_keyword pbm_format
[PBM_LAST
] =
4953 {":type", IMAGE_SYMBOL_VALUE
, 1},
4954 {":file", IMAGE_STRING_VALUE
, 0},
4955 {":data", IMAGE_STRING_VALUE
, 0},
4956 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4957 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
4958 {":relief", IMAGE_INTEGER_VALUE
, 0},
4959 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4960 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4961 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4962 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4963 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4966 /* Structure describing the image type `pbm'. */
4968 static struct image_type pbm_type
=
4978 /* Return non-zero if OBJECT is a valid PBM image specification. */
4981 pbm_image_p (Lisp_Object object
)
4983 struct image_keyword fmt
[PBM_LAST
];
4985 memcpy (fmt
, pbm_format
, sizeof fmt
);
4987 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4990 /* Must specify either :data or :file. */
4991 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
4995 /* Scan a decimal number from *S and return it. Advance *S while
4996 reading the number. END is the end of the string. Value is -1 at
5000 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5002 int c
= 0, val
= -1;
5006 /* Skip white-space. */
5007 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5012 /* Skip comment to end of line. */
5013 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5016 else if (isdigit (c
))
5018 /* Read decimal number. */
5020 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5021 val
= 10 * val
+ c
- '0';
5033 #if 0 /* Unused. ++kfs */
5035 /* Read FILE into memory. Value is a pointer to a buffer allocated
5036 with xmalloc holding FILE's contents. Value is null if an error
5037 occurred. *SIZE is set to the size of the file. */
5040 pbm_read_file (file
, size
)
5048 if (stat (SDATA (file
), &st
) == 0
5049 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5050 && (buf
= (char *) xmalloc (st
.st_size
),
5051 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5070 #endif /* HAVE_NTGUI */
5072 /* Load PBM image IMG for use on frame F. */
5075 pbm_load (struct frame
*f
, struct image
*img
)
5078 int width
, height
, max_color_idx
= 0;
5080 Lisp_Object file
, specified_file
;
5081 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5082 unsigned char *contents
= NULL
;
5083 unsigned char *end
, *p
;
5086 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5088 if (STRINGP (specified_file
))
5090 file
= x_find_image_file (specified_file
);
5091 if (!STRINGP (file
))
5093 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5097 contents
= slurp_file (SSDATA (file
), &size
);
5098 if (contents
== NULL
)
5100 image_error ("Error reading `%s'", file
, Qnil
);
5105 end
= contents
+ size
;
5110 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5111 if (!STRINGP (data
))
5113 image_error ("Invalid image data `%s'", data
, Qnil
);
5117 end
= p
+ SBYTES (data
);
5120 /* Check magic number. */
5121 if (end
- p
< 2 || *p
++ != 'P')
5123 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5132 raw_p
= 0, type
= PBM_MONO
;
5136 raw_p
= 0, type
= PBM_GRAY
;
5140 raw_p
= 0, type
= PBM_COLOR
;
5144 raw_p
= 1, type
= PBM_MONO
;
5148 raw_p
= 1, type
= PBM_GRAY
;
5152 raw_p
= 1, type
= PBM_COLOR
;
5156 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5160 /* Read width, height, maximum color-component. Characters
5161 starting with `#' up to the end of a line are ignored. */
5162 width
= pbm_scan_number (&p
, end
);
5163 height
= pbm_scan_number (&p
, end
);
5165 if (type
!= PBM_MONO
)
5167 max_color_idx
= pbm_scan_number (&p
, end
);
5168 if (max_color_idx
> 65535 || max_color_idx
< 0)
5170 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5175 if (!check_image_size (f
, width
, height
))
5177 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5181 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5182 &ximg
, &img
->pixmap
))
5185 /* Initialize the color hash table. */
5186 init_color_table ();
5188 if (type
== PBM_MONO
)
5191 struct image_keyword fmt
[PBM_LAST
];
5192 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5193 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5195 /* Parse the image specification. */
5196 memcpy (fmt
, pbm_format
, sizeof fmt
);
5197 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5199 /* Get foreground and background colors, maybe allocate colors. */
5200 if (fmt
[PBM_FOREGROUND
].count
5201 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5202 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5203 if (fmt
[PBM_BACKGROUND
].count
5204 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5206 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5207 img
->background
= bg
;
5208 img
->background_valid
= 1;
5211 for (y
= 0; y
< height
; ++y
)
5212 for (x
= 0; x
< width
; ++x
)
5220 x_destroy_x_image (ximg
);
5221 x_clear_image (f
, img
);
5222 image_error ("Invalid image size in image `%s'",
5232 g
= pbm_scan_number (&p
, end
);
5234 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5239 int expected_size
= height
* width
;
5240 if (max_color_idx
> 255)
5242 if (type
== PBM_COLOR
)
5245 if (raw_p
&& p
+ expected_size
> end
)
5247 x_destroy_x_image (ximg
);
5248 x_clear_image (f
, img
);
5249 image_error ("Invalid image size in image `%s'",
5254 for (y
= 0; y
< height
; ++y
)
5255 for (x
= 0; x
< width
; ++x
)
5259 if (type
== PBM_GRAY
&& raw_p
)
5262 if (max_color_idx
> 255)
5263 r
= g
= b
= r
* 256 + *p
++;
5265 else if (type
== PBM_GRAY
)
5266 r
= g
= b
= pbm_scan_number (&p
, end
);
5270 if (max_color_idx
> 255)
5273 if (max_color_idx
> 255)
5276 if (max_color_idx
> 255)
5281 r
= pbm_scan_number (&p
, end
);
5282 g
= pbm_scan_number (&p
, end
);
5283 b
= pbm_scan_number (&p
, end
);
5286 if (r
< 0 || g
< 0 || b
< 0)
5288 x_destroy_x_image (ximg
);
5289 image_error ("Invalid pixel value in image `%s'",
5294 /* RGB values are now in the range 0..max_color_idx.
5295 Scale this to the range 0..0xffff supported by X. */
5296 r
= (double) r
* 65535 / max_color_idx
;
5297 g
= (double) g
* 65535 / max_color_idx
;
5298 b
= (double) b
* 65535 / max_color_idx
;
5299 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5303 #ifdef COLOR_TABLE_SUPPORT
5304 /* Store in IMG->colors the colors allocated for the image, and
5305 free the color table. */
5306 img
->colors
= colors_in_color_table (&img
->ncolors
);
5307 free_color_table ();
5308 #endif /* COLOR_TABLE_SUPPORT */
5311 img
->height
= height
;
5313 /* Maybe fill in the background field while we have ximg handy. */
5315 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5316 /* Casting avoids a GCC warning. */
5317 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5319 /* Put the image into a pixmap. */
5320 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5321 x_destroy_x_image (ximg
);
5323 /* X and W32 versions did it here, MAC version above. ++kfs
5325 img->height = height; */
5332 /***********************************************************************
5334 ***********************************************************************/
5336 #if defined (HAVE_PNG) || defined (HAVE_NS)
5338 /* Function prototypes. */
5340 static int png_image_p (Lisp_Object object
);
5341 static int png_load (struct frame
*f
, struct image
*img
);
5343 /* The symbol `png' identifying images of this type. */
5347 /* Indices of image specification fields in png_format, below. */
5349 enum png_keyword_index
5364 /* Vector of image_keyword structures describing the format
5365 of valid user-defined image specifications. */
5367 static const struct image_keyword png_format
[PNG_LAST
] =
5369 {":type", IMAGE_SYMBOL_VALUE
, 1},
5370 {":data", IMAGE_STRING_VALUE
, 0},
5371 {":file", IMAGE_STRING_VALUE
, 0},
5372 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5373 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5374 {":relief", IMAGE_INTEGER_VALUE
, 0},
5375 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5376 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5377 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5378 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5381 /* Structure describing the image type `png'. */
5383 static struct image_type png_type
=
5392 /* Return non-zero if OBJECT is a valid PNG image specification. */
5395 png_image_p (Lisp_Object object
)
5397 struct image_keyword fmt
[PNG_LAST
];
5398 memcpy (fmt
, png_format
, sizeof fmt
);
5400 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5403 /* Must specify either the :data or :file keyword. */
5404 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5407 #endif /* HAVE_PNG || HAVE_NS */
5413 /* PNG library details. */
5415 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5416 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5417 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5418 png_error_ptr
, png_error_ptr
));
5419 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5420 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5421 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5422 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5423 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5424 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5425 png_uint_32
*, png_uint_32
*,
5426 int *, int *, int *, int *, int *));
5427 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5428 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5429 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5430 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5431 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5433 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5434 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5435 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5436 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5437 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5438 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5439 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5441 #if (PNG_LIBPNG_VER >= 10500)
5442 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5443 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5444 #endif /* libpng version >= 1.5 */
5447 init_png_functions (Lisp_Object libraries
)
5451 /* Try loading libpng under probable names. */
5452 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5455 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5456 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5457 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5458 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5459 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5460 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5461 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5462 LOAD_IMGLIB_FN (library
, png_read_info
);
5463 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5464 LOAD_IMGLIB_FN (library
, png_get_valid
);
5465 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5466 LOAD_IMGLIB_FN (library
, png_set_expand
);
5467 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5468 LOAD_IMGLIB_FN (library
, png_set_background
);
5469 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5470 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5471 LOAD_IMGLIB_FN (library
, png_get_channels
);
5472 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5473 LOAD_IMGLIB_FN (library
, png_read_image
);
5474 LOAD_IMGLIB_FN (library
, png_read_end
);
5475 LOAD_IMGLIB_FN (library
, png_error
);
5477 #if (PNG_LIBPNG_VER >= 10500)
5478 LOAD_IMGLIB_FN (library
, png_longjmp
);
5479 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5480 #endif /* libpng version >= 1.5 */
5486 #define fn_png_get_io_ptr png_get_io_ptr
5487 #define fn_png_sig_cmp png_sig_cmp
5488 #define fn_png_create_read_struct png_create_read_struct
5489 #define fn_png_create_info_struct png_create_info_struct
5490 #define fn_png_destroy_read_struct png_destroy_read_struct
5491 #define fn_png_set_read_fn png_set_read_fn
5492 #define fn_png_set_sig_bytes png_set_sig_bytes
5493 #define fn_png_read_info png_read_info
5494 #define fn_png_get_IHDR png_get_IHDR
5495 #define fn_png_get_valid png_get_valid
5496 #define fn_png_set_strip_16 png_set_strip_16
5497 #define fn_png_set_expand png_set_expand
5498 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5499 #define fn_png_set_background png_set_background
5500 #define fn_png_get_bKGD png_get_bKGD
5501 #define fn_png_read_update_info png_read_update_info
5502 #define fn_png_get_channels png_get_channels
5503 #define fn_png_get_rowbytes png_get_rowbytes
5504 #define fn_png_read_image png_read_image
5505 #define fn_png_read_end png_read_end
5506 #define fn_png_error png_error
5508 #if (PNG_LIBPNG_VER >= 10500)
5509 #define fn_png_longjmp png_longjmp
5510 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5511 #endif /* libpng version >= 1.5 */
5513 #endif /* HAVE_NTGUI */
5516 #if (PNG_LIBPNG_VER < 10500)
5517 #define PNG_LONGJMP(ptr) (longjmp ((ptr)->jmpbuf, 1))
5518 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5520 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5521 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5522 #define PNG_JMPBUF(ptr) \
5523 (*fn_png_set_longjmp_fn((ptr), longjmp, sizeof (jmp_buf)))
5526 /* Error and warning handlers installed when the PNG library
5529 static void my_png_error (png_struct
*, const char *) NO_RETURN
;
5531 my_png_error (png_struct
*png_ptr
, const char *msg
)
5533 xassert (png_ptr
!= NULL
);
5534 /* Avoid compiler warning about deprecated direct access to
5535 png_ptr's fields in libpng versions 1.4.x. */
5536 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5537 PNG_LONGJMP (png_ptr
);
5542 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5544 xassert (png_ptr
!= NULL
);
5545 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5548 /* Memory source for PNG decoding. */
5550 struct png_memory_storage
5552 unsigned char *bytes
; /* The data */
5553 size_t len
; /* How big is it? */
5554 int index
; /* Where are we? */
5558 /* Function set as reader function when reading PNG image from memory.
5559 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5560 bytes from the input to DATA. */
5563 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5565 struct png_memory_storage
*tbr
5566 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5568 if (length
> tbr
->len
- tbr
->index
)
5569 fn_png_error (png_ptr
, "Read error");
5571 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5572 tbr
->index
= tbr
->index
+ length
;
5576 /* Function set as reader function when reading PNG image from a file.
5577 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5578 bytes from the input to DATA. */
5581 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5583 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5585 if (fread (data
, 1, length
, fp
) < length
)
5586 fn_png_error (png_ptr
, "Read error");
5590 /* Load PNG image IMG for use on frame F. Value is non-zero if
5594 png_load (struct frame
*f
, struct image
*img
)
5596 Lisp_Object file
, specified_file
;
5597 Lisp_Object specified_data
;
5599 XImagePtr ximg
, mask_img
= NULL
;
5600 png_struct
*png_ptr
= NULL
;
5601 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5602 FILE *volatile fp
= NULL
;
5604 png_byte
* volatile pixels
= NULL
;
5605 png_byte
** volatile rows
= NULL
;
5606 png_uint_32 width
, height
;
5607 int bit_depth
, color_type
, interlace_type
;
5609 png_uint_32 row_bytes
;
5611 struct png_memory_storage tbr
; /* Data to be read */
5613 /* Find out what file to load. */
5614 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5615 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5617 if (NILP (specified_data
))
5619 file
= x_find_image_file (specified_file
);
5620 if (!STRINGP (file
))
5622 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5626 /* Open the image file. */
5627 fp
= fopen (SSDATA (file
), "rb");
5630 image_error ("Cannot open image file `%s'", file
, Qnil
);
5634 /* Check PNG signature. */
5635 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5636 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5638 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5645 if (!STRINGP (specified_data
))
5647 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5651 /* Read from memory. */
5652 tbr
.bytes
= SDATA (specified_data
);
5653 tbr
.len
= SBYTES (specified_data
);
5656 /* Check PNG signature. */
5657 if (tbr
.len
< sizeof sig
5658 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5660 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5664 /* Need to skip past the signature. */
5665 tbr
.bytes
+= sizeof (sig
);
5668 /* Initialize read and info structs for PNG lib. */
5669 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5674 if (fp
) fclose (fp
);
5678 info_ptr
= fn_png_create_info_struct (png_ptr
);
5681 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5682 if (fp
) fclose (fp
);
5686 end_info
= fn_png_create_info_struct (png_ptr
);
5689 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5690 if (fp
) fclose (fp
);
5694 /* Set error jump-back. We come back here when the PNG library
5695 detects an error. */
5696 if (setjmp (PNG_JMPBUF (png_ptr
)))
5700 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5703 if (fp
) fclose (fp
);
5707 /* Read image info. */
5708 if (!NILP (specified_data
))
5709 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5711 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5713 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5714 fn_png_read_info (png_ptr
, info_ptr
);
5715 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5716 &interlace_type
, NULL
, NULL
);
5718 if (!check_image_size (f
, width
, height
))
5720 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5723 /* If image contains simply transparency data, we prefer to
5724 construct a clipping mask. */
5725 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5730 /* This function is easier to write if we only have to handle
5731 one data format: RGB or RGBA with 8 bits per channel. Let's
5732 transform other formats into that format. */
5734 /* Strip more than 8 bits per channel. */
5735 if (bit_depth
== 16)
5736 fn_png_set_strip_16 (png_ptr
);
5738 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5740 fn_png_set_expand (png_ptr
);
5742 /* Convert grayscale images to RGB. */
5743 if (color_type
== PNG_COLOR_TYPE_GRAY
5744 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5745 fn_png_set_gray_to_rgb (png_ptr
);
5747 /* Handle alpha channel by combining the image with a background
5748 color. Do this only if a real alpha channel is supplied. For
5749 simple transparency, we prefer a clipping mask. */
5752 /* png_color_16 *image_bg; */
5753 Lisp_Object specified_bg
5754 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5755 int shift
= (bit_depth
== 16) ? 0 : 8;
5757 if (STRINGP (specified_bg
))
5758 /* The user specified `:background', use that. */
5761 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5763 png_color_16 user_bg
;
5765 memset (&user_bg
, 0, sizeof user_bg
);
5766 user_bg
.red
= color
.red
>> shift
;
5767 user_bg
.green
= color
.green
>> shift
;
5768 user_bg
.blue
= color
.blue
>> shift
;
5770 fn_png_set_background (png_ptr
, &user_bg
,
5771 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5776 /* We use the current frame background, ignoring any default
5777 background color set by the image. */
5778 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5780 png_color_16 frame_background
;
5782 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5783 x_query_color (f
, &color
);
5785 memset (&frame_background
, 0, sizeof frame_background
);
5786 frame_background
.red
= color
.red
>> shift
;
5787 frame_background
.green
= color
.green
>> shift
;
5788 frame_background
.blue
= color
.blue
>> shift
;
5789 #endif /* HAVE_X_WINDOWS */
5791 fn_png_set_background (png_ptr
, &frame_background
,
5792 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5796 /* Update info structure. */
5797 fn_png_read_update_info (png_ptr
, info_ptr
);
5799 /* Get number of channels. Valid values are 1 for grayscale images
5800 and images with a palette, 2 for grayscale images with transparency
5801 information (alpha channel), 3 for RGB images, and 4 for RGB
5802 images with alpha channel, i.e. RGBA. If conversions above were
5803 sufficient we should only have 3 or 4 channels here. */
5804 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5805 xassert (channels
== 3 || channels
== 4);
5807 /* Number of bytes needed for one row of the image. */
5808 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5810 /* Allocate memory for the image. */
5811 pixels
= (png_byte
*) xmalloc (row_bytes
* height
* sizeof *pixels
);
5812 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5813 for (i
= 0; i
< height
; ++i
)
5814 rows
[i
] = pixels
+ i
* row_bytes
;
5816 /* Read the entire image. */
5817 fn_png_read_image (png_ptr
, rows
);
5818 fn_png_read_end (png_ptr
, info_ptr
);
5825 /* Create the X image and pixmap. */
5826 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5830 /* Create an image and pixmap serving as mask if the PNG image
5831 contains an alpha channel. */
5834 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5835 &mask_img
, &img
->mask
))
5837 x_destroy_x_image (ximg
);
5838 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5839 img
->pixmap
= NO_PIXMAP
;
5843 /* Fill the X image and mask from PNG data. */
5844 init_color_table ();
5846 for (y
= 0; y
< height
; ++y
)
5848 png_byte
*p
= rows
[y
];
5850 for (x
= 0; x
< width
; ++x
)
5857 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5858 /* An alpha channel, aka mask channel, associates variable
5859 transparency with an image. Where other image formats
5860 support binary transparency---fully transparent or fully
5861 opaque---PNG allows up to 254 levels of partial transparency.
5862 The PNG library implements partial transparency by combining
5863 the image with a specified background color.
5865 I'm not sure how to handle this here nicely: because the
5866 background on which the image is displayed may change, for
5867 real alpha channel support, it would be necessary to create
5868 a new image for each possible background.
5870 What I'm doing now is that a mask is created if we have
5871 boolean transparency information. Otherwise I'm using
5872 the frame's background color to combine the image with. */
5877 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5883 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5884 /* Set IMG's background color from the PNG image, unless the user
5888 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5890 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5891 img
->background_valid
= 1;
5895 #ifdef COLOR_TABLE_SUPPORT
5896 /* Remember colors allocated for this image. */
5897 img
->colors
= colors_in_color_table (&img
->ncolors
);
5898 free_color_table ();
5899 #endif /* COLOR_TABLE_SUPPORT */
5902 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5907 img
->height
= height
;
5909 /* Maybe fill in the background field while we have ximg handy.
5910 Casting avoids a GCC warning. */
5911 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5913 /* Put the image into the pixmap, then free the X image and its buffer. */
5914 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5915 x_destroy_x_image (ximg
);
5917 /* Same for the mask. */
5920 /* Fill in the background_transparent field while we have the
5921 mask handy. Casting avoids a GCC warning. */
5922 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5924 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5925 x_destroy_x_image (mask_img
);
5931 #else /* HAVE_PNG */
5935 png_load (struct frame
*f
, struct image
*img
)
5937 return ns_load_image(f
, img
,
5938 image_spec_value (img
->spec
, QCfile
, NULL
),
5939 image_spec_value (img
->spec
, QCdata
, NULL
));
5941 #endif /* HAVE_NS */
5944 #endif /* !HAVE_PNG */
5948 /***********************************************************************
5950 ***********************************************************************/
5952 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5954 static int jpeg_image_p (Lisp_Object object
);
5955 static int jpeg_load (struct frame
*f
, struct image
*img
);
5957 /* The symbol `jpeg' identifying images of this type. */
5961 /* Indices of image specification fields in gs_format, below. */
5963 enum jpeg_keyword_index
5972 JPEG_HEURISTIC_MASK
,
5978 /* Vector of image_keyword structures describing the format
5979 of valid user-defined image specifications. */
5981 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5983 {":type", IMAGE_SYMBOL_VALUE
, 1},
5984 {":data", IMAGE_STRING_VALUE
, 0},
5985 {":file", IMAGE_STRING_VALUE
, 0},
5986 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5987 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5988 {":relief", IMAGE_INTEGER_VALUE
, 0},
5989 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5990 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5991 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5992 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5995 /* Structure describing the image type `jpeg'. */
5997 static struct image_type jpeg_type
=
6006 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6009 jpeg_image_p (Lisp_Object object
)
6011 struct image_keyword fmt
[JPEG_LAST
];
6013 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6015 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6018 /* Must specify either the :data or :file keyword. */
6019 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6022 #endif /* HAVE_JPEG || HAVE_NS */
6026 /* Work around a warning about HAVE_STDLIB_H being redefined in
6028 #ifdef HAVE_STDLIB_H
6029 #undef HAVE_STDLIB_H
6030 #endif /* HAVE_STLIB_H */
6032 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6033 /* In older releases of the jpeg library, jpeglib.h will define boolean
6034 differently depending on __WIN32__, so make sure it is defined. */
6038 #include <jpeglib.h>
6041 #ifdef HAVE_STLIB_H_1
6042 #define HAVE_STDLIB_H 1
6047 /* JPEG library details. */
6048 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6049 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6050 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6051 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6052 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6053 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6054 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6055 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6058 init_jpeg_functions (Lisp_Object libraries
)
6062 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6065 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6066 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6067 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6068 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6069 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6070 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6071 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6072 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6076 /* Wrapper since we can't directly assign the function pointer
6077 to another function pointer that was declared more completely easily. */
6079 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6081 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6086 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6087 #define fn_jpeg_start_decompress jpeg_start_decompress
6088 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6089 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6090 #define fn_jpeg_read_header jpeg_read_header
6091 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6092 #define fn_jpeg_std_error jpeg_std_error
6093 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6095 #endif /* HAVE_NTGUI */
6097 struct my_jpeg_error_mgr
6099 struct jpeg_error_mgr pub
;
6100 jmp_buf setjmp_buffer
;
6104 static void my_error_exit (j_common_ptr
) NO_RETURN
;
6106 my_error_exit (j_common_ptr cinfo
)
6108 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6109 longjmp (mgr
->setjmp_buffer
, 1);
6113 /* Init source method for JPEG data source manager. Called by
6114 jpeg_read_header() before any data is actually read. See
6115 libjpeg.doc from the JPEG lib distribution. */
6118 our_common_init_source (j_decompress_ptr cinfo
)
6123 /* Method to terminate data source. Called by
6124 jpeg_finish_decompress() after all data has been processed. */
6127 our_common_term_source (j_decompress_ptr cinfo
)
6132 /* Fill input buffer method for JPEG data source manager. Called
6133 whenever more data is needed. We read the whole image in one step,
6134 so this only adds a fake end of input marker at the end. */
6136 static JOCTET our_memory_buffer
[2];
6139 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6141 /* Insert a fake EOI marker. */
6142 struct jpeg_source_mgr
*src
= cinfo
->src
;
6144 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6145 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6147 src
->next_input_byte
= our_memory_buffer
;
6148 src
->bytes_in_buffer
= 2;
6153 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6154 is the JPEG data source manager. */
6157 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6159 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6163 if (num_bytes
> src
->bytes_in_buffer
)
6164 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6166 src
->bytes_in_buffer
-= num_bytes
;
6167 src
->next_input_byte
+= num_bytes
;
6172 /* Set up the JPEG lib for reading an image from DATA which contains
6173 LEN bytes. CINFO is the decompression info structure created for
6174 reading the image. */
6177 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6179 struct jpeg_source_mgr
*src
;
6181 if (cinfo
->src
== NULL
)
6183 /* First time for this JPEG object? */
6184 cinfo
->src
= (struct jpeg_source_mgr
*)
6185 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6186 sizeof (struct jpeg_source_mgr
));
6187 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6188 src
->next_input_byte
= data
;
6191 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6192 src
->init_source
= our_common_init_source
;
6193 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6194 src
->skip_input_data
= our_memory_skip_input_data
;
6195 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6196 src
->term_source
= our_common_term_source
;
6197 src
->bytes_in_buffer
= len
;
6198 src
->next_input_byte
= data
;
6202 struct jpeg_stdio_mgr
6204 struct jpeg_source_mgr mgr
;
6211 /* Size of buffer to read JPEG from file.
6212 Not too big, as we want to use alloc_small. */
6213 #define JPEG_STDIO_BUFFER_SIZE 8192
6216 /* Fill input buffer method for JPEG data source manager. Called
6217 whenever more data is needed. The data is read from a FILE *. */
6220 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6222 struct jpeg_stdio_mgr
*src
;
6224 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6229 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6231 src
->mgr
.bytes_in_buffer
= bytes
;
6234 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6236 src
->buffer
[0] = (JOCTET
) 0xFF;
6237 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6238 src
->mgr
.bytes_in_buffer
= 2;
6240 src
->mgr
.next_input_byte
= src
->buffer
;
6247 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6248 is the JPEG data source manager. */
6251 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6253 struct jpeg_stdio_mgr
*src
;
6254 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6256 while (num_bytes
> 0 && !src
->finished
)
6258 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6260 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6261 src
->mgr
.next_input_byte
+= num_bytes
;
6266 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6267 src
->mgr
.bytes_in_buffer
= 0;
6268 src
->mgr
.next_input_byte
= NULL
;
6270 our_stdio_fill_input_buffer (cinfo
);
6276 /* Set up the JPEG lib for reading an image from a FILE *.
6277 CINFO is the decompression info structure created for
6278 reading the image. */
6281 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6283 struct jpeg_stdio_mgr
*src
;
6285 if (cinfo
->src
!= NULL
)
6286 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6289 /* First time for this JPEG object? */
6290 cinfo
->src
= (struct jpeg_source_mgr
*)
6291 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6292 sizeof (struct jpeg_stdio_mgr
));
6293 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6294 src
->buffer
= (JOCTET
*)
6295 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6296 JPEG_STDIO_BUFFER_SIZE
);
6301 src
->mgr
.init_source
= our_common_init_source
;
6302 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6303 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6304 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6305 src
->mgr
.term_source
= our_common_term_source
;
6306 src
->mgr
.bytes_in_buffer
= 0;
6307 src
->mgr
.next_input_byte
= NULL
;
6311 /* Load image IMG for use on frame F. Patterned after example.c
6312 from the JPEG lib. */
6315 jpeg_load (struct frame
*f
, struct image
*img
)
6317 struct jpeg_decompress_struct cinfo
;
6318 struct my_jpeg_error_mgr mgr
;
6319 Lisp_Object file
, specified_file
;
6320 Lisp_Object specified_data
;
6321 FILE * volatile fp
= NULL
;
6323 int row_stride
, x
, y
;
6324 XImagePtr ximg
= NULL
;
6326 unsigned long *colors
;
6329 /* Open the JPEG file. */
6330 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6331 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6333 if (NILP (specified_data
))
6335 file
= x_find_image_file (specified_file
);
6336 if (!STRINGP (file
))
6338 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6342 fp
= fopen (SSDATA (file
), "rb");
6345 image_error ("Cannot open `%s'", file
, Qnil
);
6349 else if (!STRINGP (specified_data
))
6351 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6355 /* Customize libjpeg's error handling to call my_error_exit when an
6356 error is detected. This function will perform a longjmp. */
6357 cinfo
.err
= fn_jpeg_std_error (&mgr
.pub
);
6358 mgr
.pub
.error_exit
= my_error_exit
;
6360 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6364 /* Called from my_error_exit. Display a JPEG error. */
6365 char buf
[JMSG_LENGTH_MAX
];
6366 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buf
);
6367 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6368 build_string (buf
));
6371 /* Close the input file and destroy the JPEG object. */
6373 fclose ((FILE *) fp
);
6374 fn_jpeg_destroy_decompress (&cinfo
);
6376 /* If we already have an XImage, free that. */
6377 x_destroy_x_image (ximg
);
6379 /* Free pixmap and colors. */
6380 x_clear_image (f
, img
);
6384 /* Create the JPEG decompression object. Let it read from fp.
6385 Read the JPEG image header. */
6386 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6388 if (NILP (specified_data
))
6389 jpeg_file_src (&cinfo
, (FILE *) fp
);
6391 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6392 SBYTES (specified_data
));
6394 fn_jpeg_read_header (&cinfo
, 1);
6396 /* Customize decompression so that color quantization will be used.
6397 Start decompression. */
6398 cinfo
.quantize_colors
= 1;
6399 fn_jpeg_start_decompress (&cinfo
);
6400 width
= img
->width
= cinfo
.output_width
;
6401 height
= img
->height
= cinfo
.output_height
;
6403 if (!check_image_size (f
, width
, height
))
6405 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6406 longjmp (mgr
.setjmp_buffer
, 2);
6409 /* Create X image and pixmap. */
6410 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6411 longjmp (mgr
.setjmp_buffer
, 2);
6413 /* Allocate colors. When color quantization is used,
6414 cinfo.actual_number_of_colors has been set with the number of
6415 colors generated, and cinfo.colormap is a two-dimensional array
6416 of color indices in the range 0..cinfo.actual_number_of_colors.
6417 No more than 255 colors will be generated. */
6421 if (cinfo
.out_color_components
> 2)
6422 ir
= 0, ig
= 1, ib
= 2;
6423 else if (cinfo
.out_color_components
> 1)
6424 ir
= 0, ig
= 1, ib
= 0;
6426 ir
= 0, ig
= 0, ib
= 0;
6428 /* Use the color table mechanism because it handles colors that
6429 cannot be allocated nicely. Such colors will be replaced with
6430 a default color, and we don't have to care about which colors
6431 can be freed safely, and which can't. */
6432 init_color_table ();
6433 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6436 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6438 /* Multiply RGB values with 255 because X expects RGB values
6439 in the range 0..0xffff. */
6440 int r
= cinfo
.colormap
[ir
][i
] << 8;
6441 int g
= cinfo
.colormap
[ig
][i
] << 8;
6442 int b
= cinfo
.colormap
[ib
][i
] << 8;
6443 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6446 #ifdef COLOR_TABLE_SUPPORT
6447 /* Remember those colors actually allocated. */
6448 img
->colors
= colors_in_color_table (&img
->ncolors
);
6449 free_color_table ();
6450 #endif /* COLOR_TABLE_SUPPORT */
6454 row_stride
= width
* cinfo
.output_components
;
6455 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6457 for (y
= 0; y
< height
; ++y
)
6459 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6460 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6461 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6465 fn_jpeg_finish_decompress (&cinfo
);
6466 fn_jpeg_destroy_decompress (&cinfo
);
6468 fclose ((FILE *) fp
);
6470 /* Maybe fill in the background field while we have ximg handy. */
6471 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6472 /* Casting avoids a GCC warning. */
6473 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6475 /* Put the image into the pixmap. */
6476 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6477 x_destroy_x_image (ximg
);
6481 #else /* HAVE_JPEG */
6485 jpeg_load (struct frame
*f
, struct image
*img
)
6487 return ns_load_image (f
, img
,
6488 image_spec_value (img
->spec
, QCfile
, NULL
),
6489 image_spec_value (img
->spec
, QCdata
, NULL
));
6491 #endif /* HAVE_NS */
6493 #endif /* !HAVE_JPEG */
6497 /***********************************************************************
6499 ***********************************************************************/
6501 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6503 static int tiff_image_p (Lisp_Object object
);
6504 static int tiff_load (struct frame
*f
, struct image
*img
);
6506 /* The symbol `tiff' identifying images of this type. */
6510 /* Indices of image specification fields in tiff_format, below. */
6512 enum tiff_keyword_index
6521 TIFF_HEURISTIC_MASK
,
6528 /* Vector of image_keyword structures describing the format
6529 of valid user-defined image specifications. */
6531 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6533 {":type", IMAGE_SYMBOL_VALUE
, 1},
6534 {":data", IMAGE_STRING_VALUE
, 0},
6535 {":file", IMAGE_STRING_VALUE
, 0},
6536 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6537 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6538 {":relief", IMAGE_INTEGER_VALUE
, 0},
6539 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6540 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6541 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6542 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6543 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6546 /* Structure describing the image type `tiff'. */
6548 static struct image_type tiff_type
=
6557 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6560 tiff_image_p (Lisp_Object object
)
6562 struct image_keyword fmt
[TIFF_LAST
];
6563 memcpy (fmt
, tiff_format
, sizeof fmt
);
6565 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6568 /* Must specify either the :data or :file keyword. */
6569 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6572 #endif /* HAVE_TIFF || HAVE_NS */
6580 /* TIFF library details. */
6581 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6582 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6583 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6584 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6585 TIFFReadWriteProc
, TIFFReadWriteProc
,
6586 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6587 TIFFMapFileProc
, TIFFUnmapFileProc
));
6588 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6589 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6590 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6591 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6594 init_tiff_functions (Lisp_Object libraries
)
6598 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6601 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6602 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6603 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6604 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6605 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6606 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6607 LOAD_IMGLIB_FN (library
, TIFFClose
);
6608 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6614 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6615 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6616 #define fn_TIFFOpen TIFFOpen
6617 #define fn_TIFFClientOpen TIFFClientOpen
6618 #define fn_TIFFGetField TIFFGetField
6619 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6620 #define fn_TIFFClose TIFFClose
6621 #define fn_TIFFSetDirectory TIFFSetDirectory
6622 #endif /* HAVE_NTGUI */
6625 /* Reading from a memory buffer for TIFF images Based on the PNG
6626 memory source, but we have to provide a lot of extra functions.
6629 We really only need to implement read and seek, but I am not
6630 convinced that the TIFF library is smart enough not to destroy
6631 itself if we only hand it the function pointers we need to
6636 unsigned char *bytes
;
6643 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6645 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6647 if (size
> src
->len
- src
->index
)
6649 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6655 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6661 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6663 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6668 case SEEK_SET
: /* Go from beginning of source. */
6672 case SEEK_END
: /* Go from end of source. */
6673 idx
= src
->len
+ off
;
6676 case SEEK_CUR
: /* Go from current position. */
6677 idx
= src
->index
+ off
;
6680 default: /* Invalid `whence'. */
6684 if (idx
> src
->len
|| idx
< 0)
6692 tiff_close_memory (thandle_t data
)
6699 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6701 /* It is already _IN_ memory. */
6706 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6708 /* We don't need to do this. */
6712 tiff_size_of_memory (thandle_t data
)
6714 return ((tiff_memory_source
*) data
)->len
;
6718 static void tiff_error_handler (const char *, const char *, va_list)
6719 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6721 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6726 len
= sprintf (buf
, "TIFF error: %s ", title
);
6727 vsprintf (buf
+ len
, format
, ap
);
6728 add_to_log (buf
, Qnil
, Qnil
);
6732 static void tiff_warning_handler (const char *, const char *, va_list)
6733 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6735 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6740 len
= sprintf (buf
, "TIFF warning: %s ", title
);
6741 vsprintf (buf
+ len
, format
, ap
);
6742 add_to_log (buf
, Qnil
, Qnil
);
6746 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6750 tiff_load (struct frame
*f
, struct image
*img
)
6752 Lisp_Object file
, specified_file
;
6753 Lisp_Object specified_data
;
6755 int width
, height
, x
, y
, count
;
6759 tiff_memory_source memsrc
;
6762 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6763 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6765 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6766 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6768 if (NILP (specified_data
))
6770 /* Read from a file */
6771 file
= x_find_image_file (specified_file
);
6772 if (!STRINGP (file
))
6774 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6778 /* Try to open the image file. */
6779 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6782 image_error ("Cannot open `%s'", file
, Qnil
);
6788 if (!STRINGP (specified_data
))
6790 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6794 /* Memory source! */
6795 memsrc
.bytes
= SDATA (specified_data
);
6796 memsrc
.len
= SBYTES (specified_data
);
6799 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6800 (TIFFReadWriteProc
) tiff_read_from_memory
,
6801 (TIFFReadWriteProc
) tiff_write_from_memory
,
6802 tiff_seek_in_memory
,
6804 tiff_size_of_memory
,
6810 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6815 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6816 if (INTEGERP (image
))
6818 int ino
= XFASTINT (image
);
6819 if (!fn_TIFFSetDirectory (tiff
, ino
))
6821 image_error ("Invalid image number `%s' in image `%s'",
6823 fn_TIFFClose (tiff
);
6828 /* Get width and height of the image, and allocate a raster buffer
6829 of width x height 32-bit values. */
6830 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6831 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6833 if (!check_image_size (f
, width
, height
))
6835 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6836 fn_TIFFClose (tiff
);
6840 buf
= (uint32
*) xmalloc (width
* height
* sizeof *buf
);
6842 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6844 /* Count the number of images in the file. */
6845 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6849 img
->data
.lisp_val
= Fcons (Qcount
,
6850 Fcons (make_number (count
),
6851 img
->data
.lisp_val
));
6853 fn_TIFFClose (tiff
);
6856 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6861 /* Create the X image and pixmap. */
6862 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6868 /* Initialize the color table. */
6869 init_color_table ();
6871 /* Process the pixel raster. Origin is in the lower-left corner. */
6872 for (y
= 0; y
< height
; ++y
)
6874 uint32
*row
= buf
+ y
* width
;
6876 for (x
= 0; x
< width
; ++x
)
6878 uint32 abgr
= row
[x
];
6879 int r
= TIFFGetR (abgr
) << 8;
6880 int g
= TIFFGetG (abgr
) << 8;
6881 int b
= TIFFGetB (abgr
) << 8;
6882 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6886 #ifdef COLOR_TABLE_SUPPORT
6887 /* Remember the colors allocated for the image. Free the color table. */
6888 img
->colors
= colors_in_color_table (&img
->ncolors
);
6889 free_color_table ();
6890 #endif /* COLOR_TABLE_SUPPORT */
6893 img
->height
= height
;
6895 /* Maybe fill in the background field while we have ximg handy. */
6896 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6897 /* Casting avoids a GCC warning on W32. */
6898 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6900 /* Put the image into the pixmap, then free the X image and its buffer. */
6901 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6902 x_destroy_x_image (ximg
);
6908 #else /* HAVE_TIFF */
6912 tiff_load (struct frame
*f
, struct image
*img
)
6914 return ns_load_image (f
, img
,
6915 image_spec_value (img
->spec
, QCfile
, NULL
),
6916 image_spec_value (img
->spec
, QCdata
, NULL
));
6918 #endif /* HAVE_NS */
6920 #endif /* !HAVE_TIFF */
6924 /***********************************************************************
6926 ***********************************************************************/
6928 #if defined (HAVE_GIF) || defined (HAVE_NS)
6930 static int gif_image_p (Lisp_Object object
);
6931 static int gif_load (struct frame
*f
, struct image
*img
);
6932 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6934 /* The symbol `gif' identifying images of this type. */
6938 /* Indices of image specification fields in gif_format, below. */
6940 enum gif_keyword_index
6956 /* Vector of image_keyword structures describing the format
6957 of valid user-defined image specifications. */
6959 static const struct image_keyword gif_format
[GIF_LAST
] =
6961 {":type", IMAGE_SYMBOL_VALUE
, 1},
6962 {":data", IMAGE_STRING_VALUE
, 0},
6963 {":file", IMAGE_STRING_VALUE
, 0},
6964 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6965 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6966 {":relief", IMAGE_INTEGER_VALUE
, 0},
6967 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6968 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6969 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6970 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
6971 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6974 /* Structure describing the image type `gif'. */
6976 static struct image_type gif_type
=
6985 /* Free X resources of GIF image IMG which is used on frame F. */
6988 gif_clear_image (struct frame
*f
, struct image
*img
)
6990 /* IMG->data.ptr_val may contain metadata with extension data. */
6991 img
->data
.lisp_val
= Qnil
;
6992 x_clear_image (f
, img
);
6995 /* Return non-zero if OBJECT is a valid GIF image specification. */
6998 gif_image_p (Lisp_Object object
)
7000 struct image_keyword fmt
[GIF_LAST
];
7001 memcpy (fmt
, gif_format
, sizeof fmt
);
7003 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7006 /* Must specify either the :data or :file keyword. */
7007 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7010 #endif /* HAVE_GIF */
7014 #if defined (HAVE_NTGUI)
7015 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7016 Undefine before redefining to avoid a preprocessor warning. */
7020 /* avoid conflict with QuickdrawText.h */
7021 #define DrawText gif_DrawText
7022 #include <gif_lib.h>
7025 #else /* HAVE_NTGUI */
7027 #include <gif_lib.h>
7029 #endif /* HAVE_NTGUI */
7034 /* GIF library details. */
7035 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7036 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7037 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7038 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7041 init_gif_functions (Lisp_Object libraries
)
7045 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7048 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7049 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7050 LOAD_IMGLIB_FN (library
, DGifOpen
);
7051 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7057 #define fn_DGifCloseFile DGifCloseFile
7058 #define fn_DGifSlurp DGifSlurp
7059 #define fn_DGifOpen DGifOpen
7060 #define fn_DGifOpenFileName DGifOpenFileName
7062 #endif /* HAVE_NTGUI */
7064 /* Reading a GIF image from memory
7065 Based on the PNG memory stuff to a certain extent. */
7069 unsigned char *bytes
;
7075 /* Make the current memory source available to gif_read_from_memory.
7076 It's done this way because not all versions of libungif support
7077 a UserData field in the GifFileType structure. */
7078 static gif_memory_source
*current_gif_memory_src
;
7081 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7083 gif_memory_source
*src
= current_gif_memory_src
;
7085 if (len
> src
->len
- src
->index
)
7088 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7094 /* Load GIF image IMG for use on frame F. Value is non-zero if
7097 static const int interlace_start
[] = {0, 4, 2, 1};
7098 static const int interlace_increment
[] = {8, 8, 4, 2};
7100 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7103 gif_load (struct frame
*f
, struct image
*img
)
7105 Lisp_Object file
, specified_file
;
7106 Lisp_Object specified_data
;
7107 int rc
, width
, height
, x
, y
, i
;
7108 boolean transparent_p
= 0;
7110 ColorMapObject
*gif_color_map
;
7111 unsigned long pixel_colors
[256];
7114 int ino
, image_height
, image_width
;
7115 gif_memory_source memsrc
;
7116 unsigned char *raster
;
7117 unsigned int transparency_color_index
IF_LINT (= 0);
7119 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7120 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7122 if (NILP (specified_data
))
7124 file
= x_find_image_file (specified_file
);
7125 if (!STRINGP (file
))
7127 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7131 /* Open the GIF file. */
7132 gif
= fn_DGifOpenFileName (SSDATA (file
));
7135 image_error ("Cannot open `%s'", file
, Qnil
);
7141 if (!STRINGP (specified_data
))
7143 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7147 /* Read from memory! */
7148 current_gif_memory_src
= &memsrc
;
7149 memsrc
.bytes
= SDATA (specified_data
);
7150 memsrc
.len
= SBYTES (specified_data
);
7153 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7156 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7161 /* Before reading entire contents, check the declared image size. */
7162 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7164 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7165 fn_DGifCloseFile (gif
);
7169 /* Read entire contents. */
7170 rc
= fn_DGifSlurp (gif
);
7171 if (rc
== GIF_ERROR
)
7173 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7174 fn_DGifCloseFile (gif
);
7178 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7179 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7180 if (ino
>= gif
->ImageCount
)
7182 image_error ("Invalid image number `%s' in image `%s'",
7184 fn_DGifCloseFile (gif
);
7188 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++)
7189 if ((gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Function
7190 == GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7191 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].ByteCount
== 4
7192 /* Transparency enabled? */
7193 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[0] & 1)
7196 transparency_color_index
7197 = (unsigned char) gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[3];
7200 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Top
;
7201 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Left
;
7202 image_height
= gif
->SavedImages
[ino
].ImageDesc
.Height
;
7203 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7204 image_width
= gif
->SavedImages
[ino
].ImageDesc
.Width
;
7205 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7207 width
= img
->width
= max (gif
->SWidth
,
7208 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7209 img
->corners
[RIGHT_CORNER
]));
7210 height
= img
->height
= max (gif
->SHeight
,
7211 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7212 img
->corners
[BOT_CORNER
]));
7214 if (!check_image_size (f
, width
, height
))
7216 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7217 fn_DGifCloseFile (gif
);
7221 /* Create the X image and pixmap. */
7222 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7224 fn_DGifCloseFile (gif
);
7228 /* Allocate colors. */
7229 gif_color_map
= gif
->SavedImages
[ino
].ImageDesc
.ColorMap
;
7231 gif_color_map
= gif
->SColorMap
;
7232 init_color_table ();
7233 memset (pixel_colors
, 0, sizeof pixel_colors
);
7236 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7238 if (transparent_p
&& transparency_color_index
== i
)
7240 Lisp_Object specified_bg
7241 = image_spec_value (img
->spec
, QCbackground
, NULL
);
7242 pixel_colors
[i
] = STRINGP (specified_bg
)
7243 ? x_alloc_image_color (f
, img
, specified_bg
,
7244 FRAME_BACKGROUND_PIXEL (f
))
7245 : FRAME_BACKGROUND_PIXEL (f
);
7249 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7250 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7251 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7252 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7256 #ifdef COLOR_TABLE_SUPPORT
7257 img
->colors
= colors_in_color_table (&img
->ncolors
);
7258 free_color_table ();
7259 #endif /* COLOR_TABLE_SUPPORT */
7261 /* Clear the part of the screen image that are not covered by
7262 the image from the GIF file. Full animated GIF support
7263 requires more than can be done here (see the gif89 spec,
7264 disposal methods). Let's simply assume that the part
7265 not covered by a sub-image is in the frame's background color. */
7266 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7267 for (x
= 0; x
< width
; ++x
)
7268 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7270 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7271 for (x
= 0; x
< width
; ++x
)
7272 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7274 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7276 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7277 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7278 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7279 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7282 /* Read the GIF image into the X image. We use a local variable
7283 `raster' here because RasterBits below is a char *, and invites
7284 problems with bytes >= 0x80. */
7285 raster
= (unsigned char *) gif
->SavedImages
[ino
].RasterBits
;
7287 if (gif
->SavedImages
[ino
].ImageDesc
.Interlace
)
7290 int row
= interlace_start
[0];
7294 for (y
= 0; y
< image_height
; y
++)
7296 if (row
>= image_height
)
7298 row
= interlace_start
[++pass
];
7299 while (row
>= image_height
)
7300 row
= interlace_start
[++pass
];
7303 for (x
= 0; x
< image_width
; x
++)
7305 int c
= raster
[(y
* image_width
) + x
];
7306 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7307 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7310 row
+= interlace_increment
[pass
];
7315 for (y
= 0; y
< image_height
; ++y
)
7316 for (x
= 0; x
< image_width
; ++x
)
7318 int c
= raster
[y
* image_width
+ x
];
7319 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7320 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7324 /* Save GIF image extension data for `image-metadata'.
7325 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7326 img
->data
.lisp_val
= Qnil
;
7327 if (gif
->SavedImages
[ino
].ExtensionBlockCount
> 0)
7329 ExtensionBlock
*ext
= gif
->SavedImages
[ino
].ExtensionBlocks
;
7330 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++, ext
++)
7331 /* Append (... FUNCTION "BYTES") */
7332 img
->data
.lisp_val
= Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7333 Fcons (make_number (ext
->Function
),
7334 img
->data
.lisp_val
));
7335 img
->data
.lisp_val
= Fcons (Qextension_data
,
7336 Fcons (Fnreverse (img
->data
.lisp_val
),
7339 if (gif
->ImageCount
> 1)
7340 img
->data
.lisp_val
= Fcons (Qcount
,
7341 Fcons (make_number (gif
->ImageCount
),
7342 img
->data
.lisp_val
));
7344 fn_DGifCloseFile (gif
);
7346 /* Maybe fill in the background field while we have ximg handy. */
7347 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7348 /* Casting avoids a GCC warning. */
7349 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7351 /* Put the image into the pixmap, then free the X image and its buffer. */
7352 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7353 x_destroy_x_image (ximg
);
7358 #else /* !HAVE_GIF */
7362 gif_load (struct frame
*f
, struct image
*img
)
7364 return ns_load_image (f
, img
,
7365 image_spec_value (img
->spec
, QCfile
, NULL
),
7366 image_spec_value (img
->spec
, QCdata
, NULL
));
7368 #endif /* HAVE_NS */
7370 #endif /* HAVE_GIF */
7373 /***********************************************************************
7375 ***********************************************************************/
7376 #if defined (HAVE_IMAGEMAGICK)
7378 Lisp_Object Qimagemagick
;
7380 /* Indices of image specification fields in imagemagick_format. */
7382 enum imagemagick_keyword_index
7390 IMAGEMAGICK_ALGORITHM
,
7391 IMAGEMAGICK_HEURISTIC_MASK
,
7393 IMAGEMAGICK_BACKGROUND
,
7396 IMAGEMAGICK_ROTATION
,
7401 /* Vector of image_keyword structures describing the format
7402 of valid user-defined image specifications. */
7404 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7406 {":type", IMAGE_SYMBOL_VALUE
, 1},
7407 {":data", IMAGE_STRING_VALUE
, 0},
7408 {":file", IMAGE_STRING_VALUE
, 0},
7409 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7410 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7411 {":relief", IMAGE_INTEGER_VALUE
, 0},
7412 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7413 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7414 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7415 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7416 {":height", IMAGE_INTEGER_VALUE
, 0},
7417 {":width", IMAGE_INTEGER_VALUE
, 0},
7418 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7419 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7422 /* Free X resources of imagemagick image IMG which is used on frame F. */
7425 imagemagick_clear_image (struct frame
*f
,
7428 x_clear_image (f
, img
);
7431 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7432 this by calling parse_image_spec and supplying the keywords that
7433 identify the IMAGEMAGICK format. */
7436 imagemagick_image_p (Lisp_Object object
)
7438 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7439 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7441 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7444 /* Must specify either the :data or :file keyword. */
7445 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7448 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7449 Therefore rename the function so it doesnt collide with ImageMagick. */
7450 #define DrawRectangle DrawRectangleGif
7451 #include <wand/MagickWand.h>
7453 /* imagemagick_load_image is a helper function for imagemagick_load,
7454 which does the actual loading given contents and size, apart from
7455 frame and image structures, passed from imagemagick_load.
7457 Uses librimagemagick to do most of the image processing.
7459 Return non-zero if successful.
7463 imagemagick_load_image (/* Pointer to emacs frame structure. */
7465 /* Pointer to emacs image structure. */
7467 /* String containing the IMAGEMAGICK data to
7469 unsigned char *contents
,
7470 /* Size of data in bytes. */
7472 /* Filename, either pass filename or
7474 unsigned char *filename
)
7476 unsigned long width
;
7477 unsigned long height
;
7483 Lisp_Object specified_bg
;
7488 MagickWand
*image_wand
;
7489 MagickWand
*ping_wand
;
7490 PixelIterator
*iterator
;
7492 MagickPixelPacket pixel
;
7495 Lisp_Object crop
, geometry
;
7497 int desired_width
, desired_height
;
7499 int imagemagick_rendermethod
;
7501 ImageInfo
*image_info
;
7502 ExceptionInfo
*exception
;
7506 /* Handle image index for image types who can contain more than one image.
7507 Interface :index is same as for GIF. First we "ping" the image to see how
7508 many sub-images it contains. Pinging is faster than loading the image to
7509 find out things about it. */
7511 /* Initialize the imagemagick environment. */
7512 MagickWandGenesis ();
7513 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7514 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7515 ping_wand
= NewMagickWand ();
7516 MagickSetResolution (ping_wand
, 2, 2);
7517 if (filename
!= NULL
)
7519 status
= MagickPingImage (ping_wand
, filename
);
7523 status
= MagickPingImageBlob (ping_wand
, contents
, size
);
7526 if (ino
>= MagickGetNumberImages (ping_wand
))
7528 image_error ("Invalid image number `%s' in image `%s'",
7530 DestroyMagickWand (ping_wand
);
7534 if (MagickGetNumberImages(ping_wand
) > 1)
7535 img
->data
.lisp_val
=
7537 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7538 img
->data
.lisp_val
));
7540 DestroyMagickWand (ping_wand
);
7542 /* Now, after pinging, we know how many images are inside the
7543 file. If it's not a bundle, the number is one. */
7545 if (filename
!= NULL
)
7547 image_info
= CloneImageInfo ((ImageInfo
*) NULL
);
7548 (void) strcpy (image_info
->filename
, filename
);
7549 image_info
->number_scenes
= 1;
7550 image_info
->scene
= ino
;
7551 exception
= AcquireExceptionInfo ();
7553 im_image
= ReadImage (image_info
, exception
);
7554 DestroyExceptionInfo (exception
);
7556 if (im_image
!= NULL
)
7558 image_wand
= NewMagickWandFromImage (im_image
);
7559 DestroyImage(im_image
);
7560 status
= MagickTrue
;
7563 status
= MagickFalse
;
7567 image_wand
= NewMagickWand ();
7568 status
= MagickReadImageBlob (image_wand
, contents
, size
);
7571 if (status
== MagickFalse
) goto imagemagick_error
;
7573 /* If width and/or height is set in the display spec assume we want
7574 to scale to those values. If either h or w is unspecified, the
7575 unspecified should be calculated from the specified to preserve
7578 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7579 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7580 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7581 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7583 height
= MagickGetImageHeight (image_wand
);
7584 width
= MagickGetImageWidth (image_wand
);
7586 if (desired_width
!= -1 && desired_height
== -1)
7587 /* w known, calculate h. */
7588 desired_height
= (double) desired_width
/ width
* height
;
7589 if (desired_width
== -1 && desired_height
!= -1)
7590 /* h known, calculate w. */
7591 desired_width
= (double) desired_height
/ height
* width
;
7592 if (desired_width
!= -1 && desired_height
!= -1)
7594 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7595 if (status
== MagickFalse
)
7597 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7598 goto imagemagick_error
;
7602 /* crop behaves similar to image slicing in Emacs but is more memory
7604 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7606 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7608 /* After some testing, it seems MagickCropImage is the fastest crop
7609 function in ImageMagick. This crop function seems to do less copying
7610 than the alternatives, but it still reads the entire image into memory
7611 before croping, which is aparently difficult to avoid when using
7614 w
= XFASTINT (XCAR (crop
));
7616 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7618 h
= XFASTINT (XCAR (crop
));
7620 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7622 x
= XFASTINT (XCAR (crop
));
7624 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7626 y
= XFASTINT (XCAR (crop
));
7627 MagickCropImage (image_wand
, w
, h
, x
, y
);
7633 /* Furthermore :rotation. we need background color and angle for
7636 TODO background handling for rotation specified_bg =
7637 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7639 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7642 PixelWand
* background
= NewPixelWand ();
7643 PixelSetColor (background
, "#ffffff");/*TODO remove hardcode*/
7645 rotation
= extract_float (value
);
7647 status
= MagickRotateImage (image_wand
, background
, rotation
);
7648 DestroyPixelWand (background
);
7649 if (status
== MagickFalse
)
7651 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7652 goto imagemagick_error
;
7656 /* Finaly we are done manipulating the image, figure out resulting
7657 width, height, and then transfer ownerwship to Emacs. */
7658 height
= MagickGetImageHeight (image_wand
);
7659 width
= MagickGetImageWidth (image_wand
);
7661 if (! check_image_size (f
, width
, height
))
7663 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7664 goto imagemagick_error
;
7667 /* We can now get a valid pixel buffer from the imagemagick file, if all
7670 init_color_table ();
7671 imagemagick_rendermethod
= (INTEGERP (Vimagemagick_render_type
)
7672 ? XFASTINT (Vimagemagick_render_type
) : 0);
7673 if (imagemagick_rendermethod
== 0)
7675 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7676 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7677 &ximg
, &img
->pixmap
))
7679 #ifdef COLOR_TABLE_SUPPORT
7680 free_color_table ();
7682 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7683 goto imagemagick_error
;
7686 /* Copy imagegmagick image to x with primitive yet robust pixel
7687 pusher loop. This has been tested a lot with many different
7690 /* Copy pixels from the imagemagick image structure to the x image map. */
7691 iterator
= NewPixelIterator (image_wand
);
7692 if (iterator
== (PixelIterator
*) NULL
)
7694 #ifdef COLOR_TABLE_SUPPORT
7695 free_color_table ();
7697 x_destroy_x_image (ximg
);
7698 image_error ("Imagemagick pixel iterator creation failed",
7700 goto imagemagick_error
;
7703 for (y
= 0; y
< (long) MagickGetImageHeight (image_wand
); y
++)
7705 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7706 if (pixels
== (PixelWand
**) NULL
)
7708 for (x
= 0; x
< (long) width
; x
++)
7710 PixelGetMagickColor (pixels
[x
], &pixel
);
7711 XPutPixel (ximg
, x
, y
,
7712 lookup_rgb_color (f
,
7718 DestroyPixelIterator (iterator
);
7721 if (imagemagick_rendermethod
== 1)
7723 /* Magicexportimage is normaly faster than pixelpushing. This
7724 method is also well tested. Some aspects of this method are
7725 ad-hoc and needs to be more researched. */
7726 int imagedepth
= 24;/*MagickGetImageDepth(image_wand);*/
7727 char* exportdepth
= imagedepth
<= 8 ? "I" : "BGRP";/*"RGBP";*/
7728 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7729 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7730 &ximg
, &img
->pixmap
))
7732 #ifdef COLOR_TABLE_SUPPORT
7733 free_color_table ();
7735 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7736 goto imagemagick_error
;
7740 /* Oddly, the below code doesnt seem to work:*/
7741 /* switch(ximg->bitmap_unit){ */
7743 /* pixelwidth=CharPixel; */
7746 /* pixelwidth=ShortPixel; */
7749 /* pixelwidth=LongPixel; */
7753 Here im just guessing the format of the bitmap.
7754 happens to work fine for:
7757 seems about 3 times as fast as pixel pushing(not carefully measured)
7759 pixelwidth
= CharPixel
;/*??? TODO figure out*/
7760 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7761 MagickExportImagePixels (image_wand
,
7766 /*&(img->pixmap));*/
7769 image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
7775 #ifdef COLOR_TABLE_SUPPORT
7776 /* Remember colors allocated for this image. */
7777 img
->colors
= colors_in_color_table (&img
->ncolors
);
7778 free_color_table ();
7779 #endif /* COLOR_TABLE_SUPPORT */
7783 img
->height
= height
;
7785 /* Put the image into the pixmap, then free the X image and its
7787 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7788 x_destroy_x_image (ximg
);
7791 /* Final cleanup. image_wand should be the only resource left. */
7792 DestroyMagickWand (image_wand
);
7793 /* `MagickWandTerminus' terminates the imagemagick environment. */
7794 MagickWandTerminus ();
7799 DestroyMagickWand (image_wand
);
7800 MagickWandTerminus ();
7801 /* TODO more cleanup. */
7802 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7807 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7808 successful. this function will go into the imagemagick_type structure, and
7809 the prototype thus needs to be compatible with that structure. */
7812 imagemagick_load (struct frame
*f
,
7816 Lisp_Object file_name
;
7818 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7819 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7820 if (STRINGP (file_name
))
7824 file
= x_find_image_file (file_name
);
7825 if (!STRINGP (file
))
7827 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7830 success_p
= imagemagick_load_image (f
, img
, 0, 0, SDATA (file
));
7832 /* Else its not a file, its a lisp object. Load the image from a
7833 lisp object rather than a file. */
7838 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7839 if (!STRINGP (data
))
7841 image_error ("Invalid image data `%s'", data
, Qnil
);
7844 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7845 SBYTES (data
), NULL
);
7851 /* Structure describing the image type `imagemagick'. Its the same
7852 type of structure defined for all image formats, handled by Emacs
7853 image functions. See struct image_type in dispextern.h. */
7855 static struct image_type imagemagick_type
=
7857 /* An identifier showing that this is an image structure for the
7858 IMAGEMAGICK format. */
7860 /* Handle to a function that can be used to identify a IMAGEMAGICK
7862 imagemagick_image_p
,
7863 /* Handle to function used to load a IMAGEMAGICK file. */
7865 /* Handle to function to free resources for IMAGEMAGICK. */
7866 imagemagick_clear_image
,
7867 /* An internal field to link to the next image type in a list of
7868 image types, will be filled in when registering the format. */
7873 DEFUE ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7874 doc
: /* Return the image types supported by ImageMagick.
7875 Note that ImageMagick recognizes many file-types that Emacs does not recognize
7876 as images, such as .c. */)
7879 Lisp_Object typelist
= Qnil
;
7882 char **imtypes
= GetMagickList ("*", &numf
, &ex
);
7884 Lisp_Object Qimagemagicktype
;
7885 for (i
= 0; i
< numf
; i
++)
7887 Qimagemagicktype
= intern (imtypes
[i
]);
7888 typelist
= Fcons (Qimagemagicktype
, typelist
);
7893 #endif /* defined (HAVE_IMAGEMAGICK) */
7897 /***********************************************************************
7899 ***********************************************************************/
7901 #if defined (HAVE_RSVG)
7903 /* Function prototypes. */
7905 static int svg_image_p (Lisp_Object object
);
7906 static int svg_load (struct frame
*f
, struct image
*img
);
7908 static int svg_load_image (struct frame
*, struct image
*,
7909 unsigned char *, unsigned int);
7911 /* The symbol `svg' identifying images of this type. */
7915 /* Indices of image specification fields in svg_format, below. */
7917 enum svg_keyword_index
7932 /* Vector of image_keyword structures describing the format
7933 of valid user-defined image specifications. */
7935 static const struct image_keyword svg_format
[SVG_LAST
] =
7937 {":type", IMAGE_SYMBOL_VALUE
, 1},
7938 {":data", IMAGE_STRING_VALUE
, 0},
7939 {":file", IMAGE_STRING_VALUE
, 0},
7940 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7941 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7942 {":relief", IMAGE_INTEGER_VALUE
, 0},
7943 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7944 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7945 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7946 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7949 /* Structure describing the image type `svg'. Its the same type of
7950 structure defined for all image formats, handled by emacs image
7951 functions. See struct image_type in dispextern.h. */
7953 static struct image_type svg_type
=
7955 /* An identifier showing that this is an image structure for the SVG format. */
7957 /* Handle to a function that can be used to identify a SVG file. */
7959 /* Handle to function used to load a SVG file. */
7961 /* Handle to function to free sresources for SVG. */
7963 /* An internal field to link to the next image type in a list of
7964 image types, will be filled in when registering the format. */
7969 /* Return non-zero if OBJECT is a valid SVG image specification. Do
7970 this by calling parse_image_spec and supplying the keywords that
7971 identify the SVG format. */
7974 svg_image_p (Lisp_Object object
)
7976 struct image_keyword fmt
[SVG_LAST
];
7977 memcpy (fmt
, svg_format
, sizeof fmt
);
7979 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
7982 /* Must specify either the :data or :file keyword. */
7983 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
7986 #include <librsvg/rsvg.h>
7990 /* SVG library functions. */
7991 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
7992 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
7993 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
7994 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
7995 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
7996 DEF_IMGLIB_FN (void, rsvg_handle_free
);
7998 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
7999 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8000 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8001 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8002 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8003 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8004 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8005 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8007 DEF_IMGLIB_FN (void, g_type_init
);
8008 DEF_IMGLIB_FN (void, g_object_unref
);
8009 DEF_IMGLIB_FN (void, g_error_free
);
8011 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8014 init_svg_functions (Lisp_Object libraries
)
8016 HMODULE library
, gdklib
, glib
, gobject
;
8018 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8019 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8020 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8021 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8024 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8025 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8026 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8027 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8028 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8029 LOAD_IMGLIB_FN (library
, rsvg_handle_free
);
8031 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8032 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8033 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8034 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8035 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8036 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8037 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8038 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8040 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8041 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8042 LOAD_IMGLIB_FN (glib
, g_error_free
);
8048 /* The following aliases for library functions allow dynamic loading
8049 to be used on some platforms. */
8050 #define fn_rsvg_handle_new rsvg_handle_new
8051 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8052 #define fn_rsvg_handle_write rsvg_handle_write
8053 #define fn_rsvg_handle_close rsvg_handle_close
8054 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8055 #define fn_rsvg_handle_free rsvg_handle_free
8057 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8058 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8059 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8060 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8061 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8062 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8063 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8064 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8066 #define fn_g_type_init g_type_init
8067 #define fn_g_object_unref g_object_unref
8068 #define fn_g_error_free g_error_free
8069 #endif /* !HAVE_NTGUI */
8071 /* Load SVG image IMG for use on frame F. Value is non-zero if
8072 successful. this function will go into the svg_type structure, and
8073 the prototype thus needs to be compatible with that structure. */
8076 svg_load (struct frame
*f
, struct image
*img
)
8079 Lisp_Object file_name
;
8081 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8082 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8083 if (STRINGP (file_name
))
8086 unsigned char *contents
;
8089 file
= x_find_image_file (file_name
);
8090 if (!STRINGP (file
))
8092 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8096 /* Read the entire file into memory. */
8097 contents
= slurp_file (SDATA (file
), &size
);
8098 if (contents
== NULL
)
8100 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8103 /* If the file was slurped into memory properly, parse it. */
8104 success_p
= svg_load_image (f
, img
, contents
, size
);
8107 /* Else its not a file, its a lisp object. Load the image from a
8108 lisp object rather than a file. */
8113 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8114 if (!STRINGP (data
))
8116 image_error ("Invalid image data `%s'", data
, Qnil
);
8119 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8125 /* svg_load_image is a helper function for svg_load, which does the
8126 actual loading given contents and size, apart from frame and image
8127 structures, passed from svg_load.
8129 Uses librsvg to do most of the image processing.
8131 Returns non-zero when successful. */
8133 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8134 struct image
*img
, /* Pointer to emacs image structure. */
8135 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8136 unsigned int size
) /* Size of data in bytes. */
8138 RsvgHandle
*rsvg_handle
;
8139 RsvgDimensionData dimension_data
;
8140 GError
*error
= NULL
;
8144 const guint8
*pixels
;
8147 Lisp_Object specified_bg
;
8152 /* g_type_init is a glib function that must be called prior to using
8153 gnome type library functions. */
8155 /* Make a handle to a new rsvg object. */
8156 rsvg_handle
= fn_rsvg_handle_new ();
8158 /* Parse the contents argument and fill in the rsvg_handle. */
8159 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &error
);
8160 if (error
) goto rsvg_error
;
8162 /* The parsing is complete, rsvg_handle is ready to used, close it
8163 for further writes. */
8164 fn_rsvg_handle_close (rsvg_handle
, &error
);
8165 if (error
) goto rsvg_error
;
8167 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8168 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8170 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8174 /* We can now get a valid pixel buffer from the svg file, if all
8176 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8177 if (!pixbuf
) goto rsvg_error
;
8178 fn_g_object_unref (rsvg_handle
);
8180 /* Extract some meta data from the svg handle. */
8181 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8182 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8183 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8184 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8186 /* Validate the svg meta data. */
8187 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8188 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8189 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8190 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8192 /* Try to create a x pixmap to hold the svg pixmap. */
8193 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8195 fn_g_object_unref (pixbuf
);
8199 init_color_table ();
8201 /* Handle alpha channel by combining the image with a background
8203 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8204 if (!STRINGP (specified_bg
)
8205 || !x_defined_color (f
, SDATA (specified_bg
), &background
, 0))
8208 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8209 x_query_color (f
, &background
);
8211 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8215 /* SVG pixmaps specify transparency in the last byte, so right
8216 shift 8 bits to get rid of it, since emacs doesn't support
8218 background
.red
>>= 8;
8219 background
.green
>>= 8;
8220 background
.blue
>>= 8;
8222 /* This loop handles opacity values, since Emacs assumes
8223 non-transparent images. Each pixel must be "flattened" by
8224 calculating the resulting color, given the transparency of the
8225 pixel, and the image background color. */
8226 for (y
= 0; y
< height
; ++y
)
8228 for (x
= 0; x
< width
; ++x
)
8238 opacity
= *pixels
++;
8240 red
= ((red
* opacity
)
8241 + (background
.red
* ((1 << 8) - opacity
)));
8242 green
= ((green
* opacity
)
8243 + (background
.green
* ((1 << 8) - opacity
)));
8244 blue
= ((blue
* opacity
)
8245 + (background
.blue
* ((1 << 8) - opacity
)));
8247 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8250 pixels
+= rowstride
- 4 * width
;
8253 #ifdef COLOR_TABLE_SUPPORT
8254 /* Remember colors allocated for this image. */
8255 img
->colors
= colors_in_color_table (&img
->ncolors
);
8256 free_color_table ();
8257 #endif /* COLOR_TABLE_SUPPORT */
8259 fn_g_object_unref (pixbuf
);
8262 img
->height
= height
;
8264 /* Maybe fill in the background field while we have ximg handy.
8265 Casting avoids a GCC warning. */
8266 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8268 /* Put the image into the pixmap, then free the X image and its
8270 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8271 x_destroy_x_image (ximg
);
8276 fn_g_object_unref (rsvg_handle
);
8277 /* FIXME: Use error->message so the user knows what is the actual
8278 problem with the image. */
8279 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8280 fn_g_error_free (error
);
8284 #endif /* defined (HAVE_RSVG) */
8289 /***********************************************************************
8291 ***********************************************************************/
8293 #ifdef HAVE_X_WINDOWS
8294 #define HAVE_GHOSTSCRIPT 1
8295 #endif /* HAVE_X_WINDOWS */
8297 #ifdef HAVE_GHOSTSCRIPT
8299 static int gs_image_p (Lisp_Object object
);
8300 static int gs_load (struct frame
*f
, struct image
*img
);
8301 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8303 /* Keyword symbols. */
8305 Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8307 /* Indices of image specification fields in gs_format, below. */
8309 enum gs_keyword_index
8327 /* Vector of image_keyword structures describing the format
8328 of valid user-defined image specifications. */
8330 static const struct image_keyword gs_format
[GS_LAST
] =
8332 {":type", IMAGE_SYMBOL_VALUE
, 1},
8333 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8334 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8335 {":file", IMAGE_STRING_VALUE
, 1},
8336 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8337 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8338 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8339 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8340 {":relief", IMAGE_INTEGER_VALUE
, 0},
8341 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8342 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8343 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8344 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8347 /* Structure describing the image type `ghostscript'. */
8349 static struct image_type gs_type
=
8359 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8362 gs_clear_image (struct frame
*f
, struct image
*img
)
8364 /* IMG->data.ptr_val may contain a recorded colormap. */
8365 xfree (img
->data
.ptr_val
);
8366 x_clear_image (f
, img
);
8370 /* Return non-zero if OBJECT is a valid Ghostscript image
8374 gs_image_p (Lisp_Object object
)
8376 struct image_keyword fmt
[GS_LAST
];
8380 memcpy (fmt
, gs_format
, sizeof fmt
);
8382 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8385 /* Bounding box must be a list or vector containing 4 integers. */
8386 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8389 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8390 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8395 else if (VECTORP (tem
))
8397 if (XVECTOR (tem
)->size
!= 4)
8399 for (i
= 0; i
< 4; ++i
)
8400 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
8410 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8414 gs_load (struct frame
*f
, struct image
*img
)
8417 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8419 double in_width
, in_height
;
8420 Lisp_Object pixel_colors
= Qnil
;
8422 /* Compute pixel size of pixmap needed from the given size in the
8423 image specification. Sizes in the specification are in pt. 1 pt
8424 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8426 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8427 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8428 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
8429 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8430 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8431 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
8433 if (!check_image_size (f
, img
->width
, img
->height
))
8435 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8439 /* Create the pixmap. */
8440 xassert (img
->pixmap
== NO_PIXMAP
);
8442 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8444 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8445 img
->width
, img
->height
,
8446 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8451 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8455 /* Call the loader to fill the pixmap. It returns a process object
8456 if successful. We do not record_unwind_protect here because
8457 other places in redisplay like calling window scroll functions
8458 don't either. Let the Lisp loader use `unwind-protect' instead. */
8459 sprintf (buffer
, "%lu %lu",
8460 (unsigned long) FRAME_X_WINDOW (f
),
8461 (unsigned long) img
->pixmap
);
8462 window_and_pixmap_id
= build_string (buffer
);
8464 sprintf (buffer
, "%lu %lu",
8465 FRAME_FOREGROUND_PIXEL (f
),
8466 FRAME_BACKGROUND_PIXEL (f
));
8467 pixel_colors
= build_string (buffer
);
8469 XSETFRAME (frame
, f
);
8470 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8472 loader
= intern ("gs-load-image");
8474 img
->data
.lisp_val
= call6 (loader
, frame
, img
->spec
,
8475 make_number (img
->width
),
8476 make_number (img
->height
),
8477 window_and_pixmap_id
,
8479 return PROCESSP (img
->data
.lisp_val
);
8483 /* Kill the Ghostscript process that was started to fill PIXMAP on
8484 frame F. Called from XTread_socket when receiving an event
8485 telling Emacs that Ghostscript has finished drawing. */
8488 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8490 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8494 /* Find the image containing PIXMAP. */
8495 for (i
= 0; i
< c
->used
; ++i
)
8496 if (c
->images
[i
]->pixmap
== pixmap
)
8499 /* Should someone in between have cleared the image cache, for
8500 instance, give up. */
8504 /* Kill the GS process. We should have found PIXMAP in the image
8505 cache and its image should contain a process object. */
8507 xassert (PROCESSP (img
->data
.lisp_val
));
8508 Fkill_process (img
->data
.lisp_val
, Qnil
);
8509 img
->data
.lisp_val
= Qnil
;
8511 #if defined (HAVE_X_WINDOWS)
8513 /* On displays with a mutable colormap, figure out the colors
8514 allocated for the image by looking at the pixels of an XImage for
8516 class = FRAME_X_VISUAL (f
)->class;
8517 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8523 /* Try to get an XImage for img->pixmep. */
8524 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8525 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8530 /* Initialize the color table. */
8531 init_color_table ();
8533 /* For each pixel of the image, look its color up in the
8534 color table. After having done so, the color table will
8535 contain an entry for each color used by the image. */
8536 for (y
= 0; y
< img
->height
; ++y
)
8537 for (x
= 0; x
< img
->width
; ++x
)
8539 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8540 lookup_pixel_color (f
, pixel
);
8543 /* Record colors in the image. Free color table and XImage. */
8544 #ifdef COLOR_TABLE_SUPPORT
8545 img
->colors
= colors_in_color_table (&img
->ncolors
);
8546 free_color_table ();
8548 XDestroyImage (ximg
);
8550 #if 0 /* This doesn't seem to be the case. If we free the colors
8551 here, we get a BadAccess later in x_clear_image when
8552 freeing the colors. */
8553 /* We have allocated colors once, but Ghostscript has also
8554 allocated colors on behalf of us. So, to get the
8555 reference counts right, free them once. */
8557 x_free_colors (f
, img
->colors
, img
->ncolors
);
8561 image_error ("Cannot get X image of `%s'; colors will not be freed",
8566 #endif /* HAVE_X_WINDOWS */
8568 /* Now that we have the pixmap, compute mask and transform the
8569 image if requested. */
8571 postprocess_image (f
, img
);
8575 #endif /* HAVE_GHOSTSCRIPT */
8578 /***********************************************************************
8580 ***********************************************************************/
8584 DEFUE ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8585 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8588 return valid_image_p (spec
) ? Qt
: Qnil
;
8592 DEFUE ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8597 if (valid_image_p (spec
))
8598 id
= lookup_image (SELECTED_FRAME (), spec
);
8601 return make_number (id
);
8604 #endif /* GLYPH_DEBUG != 0 */
8607 /***********************************************************************
8609 ***********************************************************************/
8612 /* Image types that rely on external libraries are loaded dynamically
8613 if the library is available. */
8614 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8615 define_image_type (image_type, init_lib_fn (libraries))
8617 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8618 define_image_type (image_type, 1)
8619 #endif /* HAVE_NTGUI */
8621 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8622 doc
: /* Initialize image library implementing image type TYPE.
8623 Return non-nil if TYPE is a supported image type.
8625 Image types pbm and xbm are prebuilt; other types are loaded here.
8626 Libraries to load are specified in alist LIBRARIES (usually, the value
8627 of `dynamic-library-alist', which see). */)
8628 (Lisp_Object type
, Lisp_Object libraries
)
8632 /* Don't try to reload the library. */
8633 tested
= Fassq (type
, Vimage_type_cache
);
8635 return XCDR (tested
);
8637 #if defined (HAVE_XPM) || defined (HAVE_NS)
8638 if (EQ (type
, Qxpm
))
8639 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8642 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8643 if (EQ (type
, Qjpeg
))
8644 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8647 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8648 if (EQ (type
, Qtiff
))
8649 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8652 #if defined (HAVE_GIF) || defined (HAVE_NS)
8653 if (EQ (type
, Qgif
))
8654 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8657 #if defined (HAVE_PNG) || defined (HAVE_NS)
8658 if (EQ (type
, Qpng
))
8659 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8662 #if defined (HAVE_RSVG)
8663 if (EQ (type
, Qsvg
))
8664 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8667 #if defined (HAVE_IMAGEMAGICK)
8668 if (EQ (type
, Qimagemagick
))
8670 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8675 #ifdef HAVE_GHOSTSCRIPT
8676 if (EQ (type
, Qpostscript
))
8677 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8680 /* If the type is not recognized, avoid testing it ever again. */
8681 CACHE_IMAGE_TYPE (type
, Qnil
);
8686 syms_of_image (void)
8688 /* Initialize this only once, since that's what we do with Vimage_types
8689 and they are supposed to be in sync. Initializing here gives correct
8690 operation on GNU/Linux of calling dump-emacs after loading some images. */
8693 /* Must be defined now becase we're going to update it below, while
8694 defining the supported image types. */
8695 DEFVAR_LISP ("image-types", Vimage_types
,
8696 doc
: /* List of potentially supported image types.
8697 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8698 To check whether it is really supported, use `image-type-available-p'. */);
8699 Vimage_types
= Qnil
;
8701 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8702 doc
: /* Maximum size of images.
8703 Emacs will not load an image into memory if its pixel width or
8704 pixel height exceeds this limit.
8706 If the value is an integer, it directly specifies the maximum
8707 image height and width, measured in pixels. If it is a floating
8708 point number, it specifies the maximum image height and width
8709 as a ratio to the frame height and width. If the value is
8710 non-numeric, there is no explicit limit on the size of images. */);
8711 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8713 Vimage_type_cache
= Qnil
;
8714 staticpro (&Vimage_type_cache
);
8716 Qpbm
= intern_c_string ("pbm");
8718 ADD_IMAGE_TYPE (Qpbm
);
8720 Qxbm
= intern_c_string ("xbm");
8722 ADD_IMAGE_TYPE (Qxbm
);
8724 define_image_type (&xbm_type
, 1);
8725 define_image_type (&pbm_type
, 1);
8727 Qcount
= intern_c_string ("count");
8728 staticpro (&Qcount
);
8729 Qextension_data
= intern_c_string ("extension-data");
8730 staticpro (&Qextension_data
);
8732 QCascent
= intern_c_string (":ascent");
8733 staticpro (&QCascent
);
8734 QCmargin
= intern_c_string (":margin");
8735 staticpro (&QCmargin
);
8736 QCrelief
= intern_c_string (":relief");
8737 staticpro (&QCrelief
);
8738 QCconversion
= intern_c_string (":conversion");
8739 staticpro (&QCconversion
);
8740 QCcolor_symbols
= intern_c_string (":color-symbols");
8741 staticpro (&QCcolor_symbols
);
8742 QCheuristic_mask
= intern_c_string (":heuristic-mask");
8743 staticpro (&QCheuristic_mask
);
8744 QCindex
= intern_c_string (":index");
8745 staticpro (&QCindex
);
8746 QCgeometry
= intern_c_string (":geometry");
8747 staticpro (&QCgeometry
);
8748 QCcrop
= intern_c_string (":crop");
8749 staticpro (&QCcrop
);
8750 QCrotation
= intern_c_string (":rotation");
8751 staticpro (&QCrotation
);
8752 QCmatrix
= intern_c_string (":matrix");
8753 staticpro (&QCmatrix
);
8754 QCcolor_adjustment
= intern_c_string (":color-adjustment");
8755 staticpro (&QCcolor_adjustment
);
8756 QCmask
= intern_c_string (":mask");
8757 staticpro (&QCmask
);
8759 Qlaplace
= intern_c_string ("laplace");
8760 staticpro (&Qlaplace
);
8761 Qemboss
= intern_c_string ("emboss");
8762 staticpro (&Qemboss
);
8763 Qedge_detection
= intern_c_string ("edge-detection");
8764 staticpro (&Qedge_detection
);
8765 Qheuristic
= intern_c_string ("heuristic");
8766 staticpro (&Qheuristic
);
8768 Qpostscript
= intern_c_string ("postscript");
8769 staticpro (&Qpostscript
);
8770 #ifdef HAVE_GHOSTSCRIPT
8771 ADD_IMAGE_TYPE (Qpostscript
);
8772 QCloader
= intern_c_string (":loader");
8773 staticpro (&QCloader
);
8774 QCbounding_box
= intern_c_string (":bounding-box");
8775 staticpro (&QCbounding_box
);
8776 QCpt_width
= intern_c_string (":pt-width");
8777 staticpro (&QCpt_width
);
8778 QCpt_height
= intern_c_string (":pt-height");
8779 staticpro (&QCpt_height
);
8780 #endif /* HAVE_GHOSTSCRIPT */
8783 Qlibpng_version
= intern_c_string ("libpng-version");
8784 staticpro (&Qlibpng_version
);
8785 Fset (Qlibpng_version
,
8787 make_number (PNG_LIBPNG_VER
)
8794 #if defined (HAVE_XPM) || defined (HAVE_NS)
8795 Qxpm
= intern_c_string ("xpm");
8797 ADD_IMAGE_TYPE (Qxpm
);
8800 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8801 Qjpeg
= intern_c_string ("jpeg");
8803 ADD_IMAGE_TYPE (Qjpeg
);
8806 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8807 Qtiff
= intern_c_string ("tiff");
8809 ADD_IMAGE_TYPE (Qtiff
);
8812 #if defined (HAVE_GIF) || defined (HAVE_NS)
8813 Qgif
= intern_c_string ("gif");
8815 ADD_IMAGE_TYPE (Qgif
);
8818 #if defined (HAVE_PNG) || defined (HAVE_NS)
8819 Qpng
= intern_c_string ("png");
8821 ADD_IMAGE_TYPE (Qpng
);
8824 #if defined (HAVE_IMAGEMAGICK)
8825 Qimagemagick
= intern_c_string ("imagemagick");
8826 staticpro (&Qimagemagick
);
8827 ADD_IMAGE_TYPE (Qimagemagick
);
8830 #if defined (HAVE_RSVG)
8831 Qsvg
= intern_c_string ("svg");
8833 ADD_IMAGE_TYPE (Qsvg
);
8835 /* Other libraries used directly by svg code. */
8836 Qgdk_pixbuf
= intern_c_string ("gdk-pixbuf");
8837 staticpro (&Qgdk_pixbuf
);
8838 Qglib
= intern_c_string ("glib");
8840 Qgobject
= intern_c_string ("gobject");
8841 staticpro (&Qgobject
);
8842 #endif /* HAVE_NTGUI */
8843 #endif /* HAVE_RSVG */
8845 defsubr (&Sinit_image_library
);
8846 #ifdef HAVE_IMAGEMAGICK
8847 defsubr (&Simagemagick_types
);
8849 defsubr (&Sclear_image_cache
);
8850 defsubr (&Simage_flush
);
8851 defsubr (&Simage_size
);
8852 defsubr (&Simage_mask_p
);
8853 defsubr (&Simage_metadata
);
8857 defsubr (&Slookup_image
);
8860 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8861 doc
: /* Non-nil means always draw a cross over disabled images.
8862 Disabled images are those having a `:conversion disabled' property.
8863 A cross is always drawn on black & white displays. */);
8864 cross_disabled_images
= 0;
8866 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8867 doc
: /* List of directories to search for window system bitmap files. */);
8868 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8870 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8871 doc
: /* Maximum time after which images are removed from the cache.
8872 When an image has not been displayed this many seconds, Emacs
8873 automatically removes it from the image cache. If the cache contains
8874 a large number of images, the actual eviction time may be shorter.
8875 The value can also be nil, meaning the cache is never cleared.
8877 The function `clear-image-cache' disregards this variable. */);
8878 Vimage_cache_eviction_delay
= make_number (300);
8879 #ifdef HAVE_IMAGEMAGICK
8880 DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type
,
8881 doc
: /* Choose between ImageMagick render methods. */);