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
);
141 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
142 id, which is just an int that this section returns. Bitmaps are
143 reference counted so they can be shared among frames.
145 Bitmap indices are guaranteed to be > 0, so a negative number can
146 be used to indicate no bitmap.
148 If you use x_create_bitmap_from_data, then you must keep track of
149 the bitmaps yourself. That is, creating a bitmap from the same
150 data more than once will not be caught. */
154 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
155 unsigned int width
, unsigned int height
,
156 unsigned long plane_mask
, int format
)
158 /* TODO: not sure what this function is supposed to do.. */
159 ns_retain_object (pixmap
);
163 /* use with imgs created by ns_image_for_XPM */
165 XGetPixel (XImagePtr ximage
, int x
, int y
)
167 return ns_get_pixel (ximage
, x
, y
);
170 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
171 pixel is assumed to be in form RGB */
173 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
175 ns_put_pixel (ximage
, x
, y
, pixel
);
180 /* Functions to access the contents of a bitmap, given an id. */
183 x_bitmap_height (FRAME_PTR f
, int id
)
185 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
189 x_bitmap_width (FRAME_PTR f
, int id
)
191 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
194 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
196 x_bitmap_pixmap (FRAME_PTR f
, int id
)
198 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
202 #ifdef HAVE_X_WINDOWS
204 x_bitmap_mask (FRAME_PTR f
, int id
)
206 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
210 /* Allocate a new bitmap record. Returns index of new record. */
213 x_allocate_bitmap_record (FRAME_PTR f
)
215 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
218 if (dpyinfo
->bitmaps
== NULL
)
220 dpyinfo
->bitmaps_size
= 10;
222 = (Bitmap_Record
*) xmalloc (dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
223 dpyinfo
->bitmaps_last
= 1;
227 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
228 return ++dpyinfo
->bitmaps_last
;
230 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
231 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
234 dpyinfo
->bitmaps_size
*= 2;
236 = (Bitmap_Record
*) xrealloc (dpyinfo
->bitmaps
,
237 dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
238 return ++dpyinfo
->bitmaps_last
;
241 /* Add one reference to the reference count of the bitmap with id ID. */
244 x_reference_bitmap (FRAME_PTR f
, int id
)
246 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
249 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
252 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
254 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
257 #ifdef HAVE_X_WINDOWS
259 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
260 bits
, width
, height
);
263 #endif /* HAVE_X_WINDOWS */
267 bitmap
= CreateBitmap (width
, height
,
268 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
269 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
273 #endif /* HAVE_NTGUI */
276 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
281 id
= x_allocate_bitmap_record (f
);
284 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
285 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
288 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
289 dpyinfo
->bitmaps
[id
- 1].height
= height
;
290 dpyinfo
->bitmaps
[id
- 1].width
= width
;
291 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
293 #ifdef HAVE_X_WINDOWS
294 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
295 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
296 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
297 #endif /* HAVE_X_WINDOWS */
300 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
301 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
302 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
303 #endif /* HAVE_NTGUI */
308 /* Create bitmap from file FILE for frame F. */
311 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
313 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
316 return -1; /* W32_TODO : bitmap support */
317 #endif /* HAVE_NTGUI */
321 void *bitmap
= ns_image_from_file (file
);
327 id
= x_allocate_bitmap_record (f
);
328 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
329 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
330 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
331 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
332 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
333 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
334 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
338 #ifdef HAVE_X_WINDOWS
339 unsigned int width
, height
;
341 int xhot
, yhot
, result
, id
;
346 /* Look for an existing bitmap with the same name. */
347 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
349 if (dpyinfo
->bitmaps
[id
].refcount
350 && dpyinfo
->bitmaps
[id
].file
351 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
353 ++dpyinfo
->bitmaps
[id
].refcount
;
358 /* Search bitmap-file-path for the file, if appropriate. */
359 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
364 filename
= SSDATA (found
);
366 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
367 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
368 if (result
!= BitmapSuccess
)
371 id
= x_allocate_bitmap_record (f
);
372 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
373 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
374 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
375 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
376 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
377 dpyinfo
->bitmaps
[id
- 1].height
= height
;
378 dpyinfo
->bitmaps
[id
- 1].width
= width
;
379 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
382 #endif /* HAVE_X_WINDOWS */
388 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
390 #ifdef HAVE_X_WINDOWS
391 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
393 XFreePixmap (dpyinfo
->display
, bm
->mask
);
394 #endif /* HAVE_X_WINDOWS */
397 DeleteObject (bm
->pixmap
);
398 #endif /* HAVE_NTGUI */
401 ns_release_object (bm
->img
);
411 /* Remove reference to bitmap with id number ID. */
414 x_destroy_bitmap (FRAME_PTR f
, int id
)
416 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
420 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
422 if (--bm
->refcount
== 0)
425 free_bitmap_record (dpyinfo
, bm
);
431 /* Free all the bitmaps for the display specified by DPYINFO. */
434 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
437 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
439 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
440 if (bm
->refcount
> 0)
441 free_bitmap_record (dpyinfo
, bm
);
443 dpyinfo
->bitmaps_last
= 0;
447 #ifdef HAVE_X_WINDOWS
449 /* Useful functions defined in the section
450 `Image type independent image structures' below. */
452 static unsigned long four_corners_best (XImagePtr ximg
,
455 unsigned long height
);
457 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
458 int depth
, XImagePtr
*ximg
,
461 static void x_destroy_x_image (XImagePtr ximg
);
464 /* Create a mask of a bitmap. Note is this not a perfect mask.
465 It's nicer with some borders in this context */
468 x_create_bitmap_mask (struct frame
*f
, int id
)
471 XImagePtr ximg
, mask_img
;
472 unsigned long width
, height
;
475 unsigned long x
, y
, xp
, xm
, yp
, ym
;
478 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
483 pixmap
= x_bitmap_pixmap (f
, id
);
484 width
= x_bitmap_width (f
, id
);
485 height
= x_bitmap_height (f
, id
);
488 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
497 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
502 XDestroyImage (ximg
);
506 bg
= four_corners_best (ximg
, NULL
, width
, height
);
508 for (y
= 0; y
< ximg
->height
; ++y
)
510 for (x
= 0; x
< ximg
->width
; ++x
)
512 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
513 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
514 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
515 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
516 if (XGetPixel (ximg
, x
, y
) == bg
517 && XGetPixel (ximg
, x
, yp
) == bg
518 && XGetPixel (ximg
, x
, ym
) == bg
519 && XGetPixel (ximg
, xp
, y
) == bg
520 && XGetPixel (ximg
, xp
, yp
) == bg
521 && XGetPixel (ximg
, xp
, ym
) == bg
522 && XGetPixel (ximg
, xm
, y
) == bg
523 && XGetPixel (ximg
, xm
, yp
) == bg
524 && XGetPixel (ximg
, xm
, ym
) == bg
)
525 XPutPixel (mask_img
, x
, y
, 0);
527 XPutPixel (mask_img
, x
, y
, 1);
531 xassert (interrupt_input_blocked
);
532 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
533 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
535 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
537 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
538 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
540 XDestroyImage (ximg
);
541 x_destroy_x_image (mask_img
);
546 #endif /* HAVE_X_WINDOWS */
549 /***********************************************************************
551 ***********************************************************************/
553 /* Value is the number of elements of vector VECTOR. */
555 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
557 /* List of supported image types. Use define_image_type to add new
558 types. Use lookup_image_type to find a type for a given symbol. */
560 static struct image_type
*image_types
;
562 /* Cache for delayed-loading image types. */
564 static Lisp_Object Vimage_type_cache
;
566 /* The symbol `xbm' which is used as the type symbol for XBM images. */
572 Lisp_Object QCascent
, QCmargin
, QCrelief
, Qcount
, Qextension_data
;
573 Lisp_Object QCconversion
, QCcolor_symbols
, QCheuristic_mask
;
574 Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
, QCcrop
, QCrotation
;
578 Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
580 /* Function prototypes. */
582 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
583 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
584 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
585 static void x_laplace (struct frame
*, struct image
*);
586 static void x_emboss (struct frame
*, struct image
*);
587 static int x_build_heuristic_mask (struct frame
*, struct image
*,
590 #define CACHE_IMAGE_TYPE(type, status) \
591 do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
593 #define ADD_IMAGE_TYPE(type) \
594 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
596 /* Define a new image type from TYPE. This adds a copy of TYPE to
597 image_types and caches the loading status of TYPE. */
600 define_image_type (struct image_type
*type
, int loaded
)
608 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
609 The initialized data segment is read-only. */
610 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
611 memcpy (p
, type
, sizeof *p
);
612 p
->next
= image_types
;
617 CACHE_IMAGE_TYPE (*type
->type
, success
);
622 /* Look up image type SYMBOL, and return a pointer to its image_type
623 structure. Value is null if SYMBOL is not a known image type. */
625 static INLINE
struct image_type
*
626 lookup_image_type (Lisp_Object symbol
)
628 struct image_type
*type
;
630 /* We must initialize the image-type if it hasn't been already. */
631 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
632 return 0; /* unimplemented */
634 for (type
= image_types
; type
; type
= type
->next
)
635 if (EQ (symbol
, *type
->type
))
642 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
643 valid image specification is a list whose car is the symbol
644 `image', and whose rest is a property list. The property list must
645 contain a value for key `:type'. That value must be the name of a
646 supported image type. The rest of the property list depends on the
650 valid_image_p (Lisp_Object object
)
658 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
659 if (EQ (XCAR (tem
), QCtype
))
662 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
664 struct image_type
*type
;
665 type
= lookup_image_type (XCAR (tem
));
667 valid_p
= type
->valid_p (object
);
678 /* Log error message with format string FORMAT and argument ARG.
679 Signaling an error, e.g. when an image cannot be loaded, is not a
680 good idea because this would interrupt redisplay, and the error
681 message display would lead to another redisplay. This function
682 therefore simply displays a message. */
685 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
687 add_to_log (format
, arg1
, arg2
);
692 /***********************************************************************
694 ***********************************************************************/
696 enum image_value_type
698 IMAGE_DONT_CHECK_VALUE_TYPE
,
700 IMAGE_STRING_OR_NIL_VALUE
,
702 IMAGE_POSITIVE_INTEGER_VALUE
,
703 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
704 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
707 IMAGE_FUNCTION_VALUE
,
712 /* Structure used when parsing image specifications. */
716 /* Name of keyword. */
719 /* The type of value allowed. */
720 enum image_value_type type
;
722 /* Non-zero means key must be present. */
725 /* Used to recognize duplicate keywords in a property list. */
728 /* The value that was found. */
733 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
735 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
738 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
739 has the format (image KEYWORD VALUE ...). One of the keyword/
740 value pairs must be `:type TYPE'. KEYWORDS is a vector of
741 image_keywords structures of size NKEYWORDS describing other
742 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
745 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
746 int nkeywords
, Lisp_Object type
)
755 while (CONSP (plist
))
757 Lisp_Object key
, value
;
759 /* First element of a pair must be a symbol. */
761 plist
= XCDR (plist
);
765 /* There must follow a value. */
768 value
= XCAR (plist
);
769 plist
= XCDR (plist
);
771 /* Find key in KEYWORDS. Error if not found. */
772 for (i
= 0; i
< nkeywords
; ++i
)
773 if (strcmp (keywords
[i
].name
, SDATA (SYMBOL_NAME (key
))) == 0)
779 /* Record that we recognized the keyword. If a keywords
780 was found more than once, it's an error. */
781 keywords
[i
].value
= value
;
784 if (keywords
[i
].count
> 1)
787 /* Check type of value against allowed type. */
788 switch (keywords
[i
].type
)
790 case IMAGE_STRING_VALUE
:
791 if (!STRINGP (value
))
795 case IMAGE_STRING_OR_NIL_VALUE
:
796 if (!STRINGP (value
) && !NILP (value
))
800 case IMAGE_SYMBOL_VALUE
:
801 if (!SYMBOLP (value
))
805 case IMAGE_POSITIVE_INTEGER_VALUE
:
806 if (!INTEGERP (value
) || XINT (value
) <= 0)
810 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
811 if (INTEGERP (value
) && XINT (value
) >= 0)
814 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
815 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
819 case IMAGE_ASCENT_VALUE
:
820 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
822 else if (INTEGERP (value
)
824 && XINT (value
) <= 100)
828 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
829 if (!INTEGERP (value
) || XINT (value
) < 0)
833 case IMAGE_DONT_CHECK_VALUE_TYPE
:
836 case IMAGE_FUNCTION_VALUE
:
837 value
= indirect_function (value
);
840 || (CONSP (value
) && EQ (XCAR (value
), Qlambda
)))
844 case IMAGE_NUMBER_VALUE
:
845 if (!INTEGERP (value
) && !FLOATP (value
))
849 case IMAGE_INTEGER_VALUE
:
850 if (!INTEGERP (value
))
854 case IMAGE_BOOL_VALUE
:
855 if (!NILP (value
) && !EQ (value
, Qt
))
864 if (EQ (key
, QCtype
) && !EQ (type
, value
))
868 /* Check that all mandatory fields are present. */
869 for (i
= 0; i
< nkeywords
; ++i
)
870 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
877 /* Return the value of KEY in image specification SPEC. Value is nil
878 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
879 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
882 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
886 xassert (valid_image_p (spec
));
888 for (tail
= XCDR (spec
);
889 CONSP (tail
) && CONSP (XCDR (tail
));
890 tail
= XCDR (XCDR (tail
)))
892 if (EQ (XCAR (tail
), key
))
896 return XCAR (XCDR (tail
));
906 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
907 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
908 PIXELS non-nil means return the size in pixels, otherwise return the
909 size in canonical character units.
910 FRAME is the frame on which the image will be displayed. FRAME nil
911 or omitted means use the selected frame. */)
912 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
917 if (valid_image_p (spec
))
919 struct frame
*f
= check_x_frame (frame
);
920 int id
= lookup_image (f
, spec
);
921 struct image
*img
= IMAGE_FROM_ID (f
, id
);
922 int width
= img
->width
+ 2 * img
->hmargin
;
923 int height
= img
->height
+ 2 * img
->vmargin
;
926 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
927 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
929 size
= Fcons (make_number (width
), make_number (height
));
932 error ("Invalid image specification");
938 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
939 doc
: /* Return t if image SPEC has a mask bitmap.
940 FRAME is the frame on which the image will be displayed. FRAME nil
941 or omitted means use the selected frame. */)
942 (Lisp_Object spec
, Lisp_Object frame
)
947 if (valid_image_p (spec
))
949 struct frame
*f
= check_x_frame (frame
);
950 int id
= lookup_image (f
, spec
);
951 struct image
*img
= IMAGE_FROM_ID (f
, id
);
956 error ("Invalid image specification");
961 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
962 doc
: /* Return metadata for image SPEC.
963 FRAME is the frame on which the image will be displayed. FRAME nil
964 or omitted means use the selected frame. */)
965 (Lisp_Object spec
, Lisp_Object frame
)
970 if (valid_image_p (spec
))
972 struct frame
*f
= check_x_frame (frame
);
973 int id
= lookup_image (f
, spec
);
974 struct image
*img
= IMAGE_FROM_ID (f
, id
);
975 ext
= img
->data
.lisp_val
;
982 /***********************************************************************
983 Image type independent image structures
984 ***********************************************************************/
986 static struct image
*make_image (Lisp_Object spec
, unsigned hash
);
987 static void free_image (struct frame
*f
, struct image
*img
);
988 static int check_image_size (struct frame
*f
, int width
, int height
);
990 #define MAX_IMAGE_SIZE 6.0
991 /* Allocate and return a new image structure for image specification
992 SPEC. SPEC has a hash value of HASH. */
994 static struct image
*
995 make_image (Lisp_Object spec
, unsigned int hash
)
997 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
998 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
1000 xassert (valid_image_p (spec
));
1001 memset (img
, 0, sizeof *img
);
1002 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
1003 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
1004 xassert (img
->type
!= NULL
);
1006 img
->data
.lisp_val
= Qnil
;
1007 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1009 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1014 /* Free image IMG which was used on frame F, including its resources. */
1017 free_image (struct frame
*f
, struct image
*img
)
1021 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1023 /* Remove IMG from the hash table of its cache. */
1025 img
->prev
->next
= img
->next
;
1027 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1030 img
->next
->prev
= img
->prev
;
1032 c
->images
[img
->id
] = NULL
;
1034 /* Free resources, then free IMG. */
1035 img
->type
->free (f
, img
);
1040 /* Return 1 if the given widths and heights are valid for display;
1041 otherwise, return 0. */
1044 check_image_size (struct frame
*f
, int width
, int height
)
1048 if (width
<= 0 || height
<= 0)
1051 if (INTEGERP (Vmax_image_size
))
1052 w
= h
= XINT (Vmax_image_size
);
1053 else if (FLOATP (Vmax_image_size
))
1057 w
= FRAME_PIXEL_WIDTH (f
);
1058 h
= FRAME_PIXEL_HEIGHT (f
);
1061 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1062 w
= (int) (XFLOAT_DATA (Vmax_image_size
) * w
);
1063 h
= (int) (XFLOAT_DATA (Vmax_image_size
) * h
);
1068 return (width
<= w
&& height
<= h
);
1071 /* Prepare image IMG for display on frame F. Must be called before
1072 drawing an image. */
1075 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1079 /* We're about to display IMG, so set its timestamp to `now'. */
1081 img
->timestamp
= EMACS_SECS (t
);
1083 /* If IMG doesn't have a pixmap yet, load it now, using the image
1084 type dependent loader function. */
1085 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1086 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1091 /* Value is the number of pixels for the ascent of image IMG when
1092 drawn in face FACE. */
1095 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1100 if (slice
->height
== img
->height
)
1101 height
= img
->height
+ img
->vmargin
;
1102 else if (slice
->y
== 0)
1103 height
= slice
->height
+ img
->vmargin
;
1105 height
= slice
->height
;
1107 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1112 /* W32 specific version. Why?. ++kfs */
1113 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1114 - FONT_BASE (face
->font
)) / 2;
1116 /* This expression is arranged so that if the image can't be
1117 exactly centered, it will be moved slightly up. This is
1118 because a typical font is `top-heavy' (due to the presence
1119 uppercase letters), so the image placement should err towards
1120 being top-heavy too. It also just generally looks better. */
1121 ascent
= (height
+ FONT_BASE(face
->font
)
1122 - FONT_DESCENT(face
->font
) + 1) / 2;
1123 #endif /* HAVE_NTGUI */
1126 ascent
= height
/ 2;
1129 ascent
= (int) (height
* img
->ascent
/ 100.0);
1135 /* Image background colors. */
1137 /* Find the "best" corner color of a bitmap.
1138 On W32, XIMG is assumed to a device context with the bitmap selected. */
1140 static RGB_PIXEL_COLOR
1141 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1142 unsigned long width
, unsigned long height
)
1144 RGB_PIXEL_COLOR corner_pixels
[4], best
;
1147 if (corners
&& corners
[BOT_CORNER
] >= 0)
1149 /* Get the colors at the corner_pixels of ximg. */
1150 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1151 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1152 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1153 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1157 /* Get the colors at the corner_pixels of ximg. */
1158 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1159 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1160 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1161 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1163 /* Choose the most frequently found color as background. */
1164 for (i
= best_count
= 0; i
< 4; ++i
)
1168 for (j
= n
= 0; j
< 4; ++j
)
1169 if (corner_pixels
[i
] == corner_pixels
[j
])
1173 best
= corner_pixels
[i
], best_count
= n
;
1179 /* Portability macros */
1183 #define Destroy_Image(img_dc, prev) \
1184 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1186 #define Free_Pixmap(display, pixmap) \
1187 DeleteObject (pixmap)
1189 #elif defined (HAVE_NS)
1191 #define Destroy_Image(ximg, dummy) \
1192 ns_release_object (ximg)
1194 #define Free_Pixmap(display, pixmap) \
1195 ns_release_object (pixmap)
1199 #define Destroy_Image(ximg, dummy) \
1200 XDestroyImage (ximg)
1202 #define Free_Pixmap(display, pixmap) \
1203 XFreePixmap (display, pixmap)
1205 #endif /* !HAVE_NTGUI && !HAVE_NS */
1208 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1209 it is guessed heuristically. If non-zero, XIMG is an existing
1210 XImage object (or device context with the image selected on W32) to
1211 use for the heuristic. */
1214 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1216 if (! img
->background_valid
)
1217 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1219 int free_ximg
= !ximg
;
1222 #endif /* HAVE_NTGUI */
1227 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1228 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1230 HDC frame_dc
= get_frame_dc (f
);
1231 ximg
= CreateCompatibleDC (frame_dc
);
1232 release_frame_dc (f
, frame_dc
);
1233 prev
= SelectObject (ximg
, img
->pixmap
);
1234 #endif /* !HAVE_NTGUI */
1237 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1240 Destroy_Image (ximg
, prev
);
1242 img
->background_valid
= 1;
1245 return img
->background
;
1248 /* Return the `background_transparent' field of IMG. If IMG doesn't
1249 have one yet, it is guessed heuristically. If non-zero, MASK is an
1250 existing XImage object to use for the heuristic. */
1253 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1255 if (! img
->background_transparent_valid
)
1256 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1260 int free_mask
= !mask
;
1263 #endif /* HAVE_NTGUI */
1268 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1269 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1271 HDC frame_dc
= get_frame_dc (f
);
1272 mask
= CreateCompatibleDC (frame_dc
);
1273 release_frame_dc (f
, frame_dc
);
1274 prev
= SelectObject (mask
, img
->mask
);
1275 #endif /* HAVE_NTGUI */
1278 img
->background_transparent
1279 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1282 Destroy_Image (mask
, prev
);
1285 img
->background_transparent
= 0;
1287 img
->background_transparent_valid
= 1;
1290 return img
->background_transparent
;
1294 /***********************************************************************
1295 Helper functions for X image types
1296 ***********************************************************************/
1298 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1300 static void x_clear_image (struct frame
*f
, struct image
*img
);
1301 static unsigned long x_alloc_image_color (struct frame
*f
,
1303 Lisp_Object color_name
,
1304 unsigned long dflt
);
1307 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1308 free the pixmap if any. MASK_P non-zero means clear the mask
1309 pixmap if any. COLORS_P non-zero means free colors allocated for
1310 the image, if any. */
1313 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1316 if (pixmap_p
&& img
->pixmap
)
1318 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1319 img
->pixmap
= NO_PIXMAP
;
1320 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1321 img
->background_valid
= 0;
1324 if (mask_p
&& img
->mask
)
1326 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1327 img
->mask
= NO_PIXMAP
;
1328 img
->background_transparent_valid
= 0;
1331 if (colors_p
&& img
->ncolors
)
1333 /* W32_TODO: color table support. */
1334 #ifdef HAVE_X_WINDOWS
1335 x_free_colors (f
, img
->colors
, img
->ncolors
);
1336 #endif /* HAVE_X_WINDOWS */
1337 xfree (img
->colors
);
1344 /* Free X resources of image IMG which is used on frame F. */
1347 x_clear_image (struct frame
*f
, struct image
*img
)
1350 x_clear_image_1 (f
, img
, 1, 1, 1);
1355 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1356 cannot be allocated, use DFLT. Add a newly allocated color to
1357 IMG->colors, so that it can be freed again. Value is the pixel
1360 static unsigned long
1361 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1365 unsigned long result
;
1367 xassert (STRINGP (color_name
));
1369 if (x_defined_color (f
, SDATA (color_name
), &color
, 1))
1371 /* This isn't called frequently so we get away with simply
1372 reallocating the color vector to the needed size, here. */
1375 (unsigned long *) xrealloc (img
->colors
,
1376 img
->ncolors
* sizeof *img
->colors
);
1377 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
1378 result
= color
.pixel
;
1388 /***********************************************************************
1390 ***********************************************************************/
1392 static struct image
*search_image_cache (struct frame
*, Lisp_Object
, unsigned);
1393 static void cache_image (struct frame
*f
, struct image
*img
);
1394 static void postprocess_image (struct frame
*, struct image
*);
1396 /* Return a new, initialized image cache that is allocated from the
1397 heap. Call free_image_cache to free an image cache. */
1399 struct image_cache
*
1400 make_image_cache (void)
1402 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1405 memset (c
, 0, sizeof *c
);
1407 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
1408 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1409 c
->buckets
= (struct image
**) xmalloc (size
);
1410 memset (c
->buckets
, 0, size
);
1415 /* Find an image matching SPEC in the cache, and return it. If no
1416 image is found, return NULL. */
1417 static struct image
*
1418 search_image_cache (struct frame
*f
, Lisp_Object spec
, unsigned int hash
)
1421 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1422 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1424 if (!c
) return NULL
;
1426 /* If the image spec does not specify a background color, the cached
1427 image must have the same background color as the current frame.
1428 The foreground color must also match, for the sake of monochrome
1431 In fact, we could ignore the foreground color matching condition
1432 for color images, or if the image spec specifies :foreground;
1433 similarly we could ignore the background color matching condition
1434 for formats that don't use transparency (such as jpeg), or if the
1435 image spec specifies :background. However, the extra memory
1436 usage is probably negligible in practice, so we don't bother. */
1438 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1439 if (img
->hash
== hash
1440 && !NILP (Fequal (img
->spec
, spec
))
1441 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1442 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1448 /* Search frame F for an image with spec SPEC, and free it. */
1451 uncache_image (struct frame
*f
, Lisp_Object spec
)
1453 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1456 free_image (f
, img
);
1457 /* As display glyphs may still be referring to the image ID, we
1458 must garbage the frame (Bug#6426). */
1459 SET_FRAME_GARBAGED (f
);
1464 /* Free image cache of frame F. Be aware that X frames share images
1468 free_image_cache (struct frame
*f
)
1470 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1475 /* Cache should not be referenced by any frame when freed. */
1476 xassert (c
->refcount
== 0);
1478 for (i
= 0; i
< c
->used
; ++i
)
1479 free_image (f
, c
->images
[i
]);
1483 FRAME_IMAGE_CACHE (f
) = NULL
;
1488 /* Clear image cache of frame F. FILTER=t means free all images.
1489 FILTER=nil means clear only images that haven't been
1490 displayed for some time.
1491 Else, only free the images which have FILTER in their `dependencies'.
1492 Should be called from time to time to reduce the number of loaded images.
1493 If image-cache-eviction-delay is non-nil, this frees images in the cache
1494 which weren't displayed for at least that many seconds. */
1497 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1499 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1505 /* Block input so that we won't be interrupted by a SIGIO
1506 while being in an inconsistent state. */
1511 /* Filter image cache. */
1512 for (i
= 0; i
< c
->used
; ++i
)
1514 struct image
*img
= c
->images
[i
];
1515 if (img
&& (EQ (Qt
, filter
)
1516 || !NILP (Fmember (filter
, img
->dependencies
))))
1518 free_image (f
, img
);
1523 else if (INTEGERP (Vimage_cache_eviction_delay
))
1525 /* Free cache based on timestamp. */
1528 int delay
, nimages
= 0;
1530 for (i
= 0; i
< c
->used
; ++i
)
1534 /* If the number of cached images has grown unusually large,
1535 decrease the cache eviction delay (Bug#6230). */
1536 delay
= XFASTINT (Vimage_cache_eviction_delay
);
1538 delay
= max (1, 1600 * delay
/ (nimages
*nimages
));
1541 old
= EMACS_SECS (t
) - delay
;
1543 for (i
= 0; i
< c
->used
; ++i
)
1545 struct image
*img
= c
->images
[i
];
1546 if (img
&& img
->timestamp
< old
)
1548 free_image (f
, img
);
1554 /* We may be clearing the image cache because, for example,
1555 Emacs was iconified for a longer period of time. In that
1556 case, current matrices may still contain references to
1557 images freed above. So, clear these matrices. */
1560 Lisp_Object tail
, frame
;
1562 FOR_EACH_FRAME (tail
, frame
)
1564 struct frame
*f
= XFRAME (frame
);
1565 if (FRAME_IMAGE_CACHE (f
) == c
)
1566 clear_current_matrices (f
);
1569 ++windows_or_buffers_changed
;
1577 clear_image_caches (Lisp_Object filter
)
1579 /* FIXME: We want to do
1580 * struct terminal *t;
1581 * for (t = terminal_list; t; t = t->next_terminal)
1582 * clear_image_cache (t, filter); */
1583 Lisp_Object tail
, frame
;
1584 FOR_EACH_FRAME (tail
, frame
)
1585 if (FRAME_WINDOW_P (XFRAME (frame
)))
1586 clear_image_cache (XFRAME (frame
), filter
);
1589 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1591 doc
: /* Clear the image cache.
1592 FILTER nil or a frame means clear all images in the selected frame.
1593 FILTER t means clear the image caches of all frames.
1594 Anything else, means only clear those images which refer to FILTER,
1595 which is then usually a filename. */)
1596 (Lisp_Object filter
)
1598 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1599 clear_image_caches (filter
);
1601 clear_image_cache (check_x_frame (filter
), Qt
);
1607 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1609 doc
: /* Fush the image with specification SPEC on frame FRAME.
1610 This removes the image from the Emacs image cache. If SPEC specifies
1611 an image file, the next redisplay of this image will read from the
1612 current contents of that file.
1614 FRAME nil or omitted means use the selected frame.
1615 FRAME t means refresh the image on all frames. */)
1616 (Lisp_Object spec
, Lisp_Object frame
)
1618 if (!valid_image_p (spec
))
1619 error ("Invalid image specification");
1624 FOR_EACH_FRAME (tail
, frame
)
1626 struct frame
*f
= XFRAME (frame
);
1627 if (FRAME_WINDOW_P (f
))
1628 uncache_image (f
, spec
);
1632 uncache_image (check_x_frame (frame
), spec
);
1638 /* Compute masks and transform image IMG on frame F, as specified
1639 by the image's specification, */
1642 postprocess_image (struct frame
*f
, struct image
*img
)
1644 /* Manipulation of the image's mask. */
1647 Lisp_Object conversion
, spec
;
1652 /* `:heuristic-mask t'
1654 means build a mask heuristically.
1655 `:heuristic-mask (R G B)'
1656 `:mask (heuristic (R G B))'
1657 means build a mask from color (R G B) in the
1660 means remove a mask, if any. */
1662 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1664 x_build_heuristic_mask (f
, img
, mask
);
1669 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1671 if (EQ (mask
, Qheuristic
))
1672 x_build_heuristic_mask (f
, img
, Qt
);
1673 else if (CONSP (mask
)
1674 && EQ (XCAR (mask
), Qheuristic
))
1676 if (CONSP (XCDR (mask
)))
1677 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1679 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1681 else if (NILP (mask
) && found_p
&& img
->mask
)
1683 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1684 img
->mask
= NO_PIXMAP
;
1689 /* Should we apply an image transformation algorithm? */
1690 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1691 if (EQ (conversion
, Qdisabled
))
1692 x_disable_image (f
, img
);
1693 else if (EQ (conversion
, Qlaplace
))
1695 else if (EQ (conversion
, Qemboss
))
1697 else if (CONSP (conversion
)
1698 && EQ (XCAR (conversion
), Qedge_detection
))
1701 tem
= XCDR (conversion
);
1703 x_edge_detection (f
, img
,
1704 Fplist_get (tem
, QCmatrix
),
1705 Fplist_get (tem
, QCcolor_adjustment
));
1711 /* Return the id of image with Lisp specification SPEC on frame F.
1712 SPEC must be a valid Lisp image specification (see valid_image_p). */
1715 lookup_image (struct frame
*f
, Lisp_Object spec
)
1717 struct image_cache
*c
;
1722 /* F must be a window-system frame, and SPEC must be a valid image
1724 xassert (FRAME_WINDOW_P (f
));
1725 xassert (valid_image_p (spec
));
1727 c
= FRAME_IMAGE_CACHE (f
);
1729 /* Look up SPEC in the hash table of the image cache. */
1730 hash
= sxhash (spec
, 0);
1731 img
= search_image_cache (f
, spec
, hash
);
1732 if (img
&& img
->load_failed_p
)
1734 free_image (f
, img
);
1738 /* If not found, create a new image and cache it. */
1742 img
= make_image (spec
, hash
);
1743 cache_image (f
, img
);
1744 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1745 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1746 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1748 /* If we can't load the image, and we don't have a width and
1749 height, use some arbitrary width and height so that we can
1750 draw a rectangle for it. */
1751 if (img
->load_failed_p
)
1755 value
= image_spec_value (spec
, QCwidth
, NULL
);
1756 img
->width
= (INTEGERP (value
)
1757 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1758 value
= image_spec_value (spec
, QCheight
, NULL
);
1759 img
->height
= (INTEGERP (value
)
1760 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1764 /* Handle image type independent image attributes
1765 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1766 `:background COLOR'. */
1767 Lisp_Object ascent
, margin
, relief
, bg
;
1769 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1770 if (INTEGERP (ascent
))
1771 img
->ascent
= XFASTINT (ascent
);
1772 else if (EQ (ascent
, Qcenter
))
1773 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1775 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1776 if (INTEGERP (margin
) && XINT (margin
) >= 0)
1777 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1778 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
1779 && INTEGERP (XCDR (margin
)))
1781 if (XINT (XCAR (margin
)) > 0)
1782 img
->hmargin
= XFASTINT (XCAR (margin
));
1783 if (XINT (XCDR (margin
)) > 0)
1784 img
->vmargin
= XFASTINT (XCDR (margin
));
1787 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1788 if (INTEGERP (relief
))
1790 img
->relief
= XINT (relief
);
1791 img
->hmargin
+= eabs (img
->relief
);
1792 img
->vmargin
+= eabs (img
->relief
);
1795 if (! img
->background_valid
)
1797 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1801 = x_alloc_image_color (f
, img
, bg
,
1802 FRAME_BACKGROUND_PIXEL (f
));
1803 img
->background_valid
= 1;
1807 /* Do image transformations and compute masks, unless we
1808 don't have the image yet. */
1809 if (!EQ (*img
->type
->type
, Qpostscript
))
1810 postprocess_image (f
, img
);
1816 /* We're using IMG, so set its timestamp to `now'. */
1817 EMACS_GET_TIME (now
);
1818 img
->timestamp
= EMACS_SECS (now
);
1820 /* Value is the image id. */
1825 /* Cache image IMG in the image cache of frame F. */
1828 cache_image (struct frame
*f
, struct image
*img
)
1830 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1833 /* Find a free slot in c->images. */
1834 for (i
= 0; i
< c
->used
; ++i
)
1835 if (c
->images
[i
] == NULL
)
1838 /* If no free slot found, maybe enlarge c->images. */
1839 if (i
== c
->used
&& c
->used
== c
->size
)
1842 c
->images
= (struct image
**) xrealloc (c
->images
,
1843 c
->size
* sizeof *c
->images
);
1846 /* Add IMG to c->images, and assign IMG an id. */
1852 /* Add IMG to the cache's hash table. */
1853 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1854 img
->next
= c
->buckets
[i
];
1856 img
->next
->prev
= img
;
1858 c
->buckets
[i
] = img
;
1862 /* Call FN on every image in the image cache of frame F. Used to mark
1863 Lisp Objects in the image cache. */
1865 /* Mark Lisp objects in image IMG. */
1868 mark_image (struct image
*img
)
1870 mark_object (img
->spec
);
1871 mark_object (img
->dependencies
);
1873 if (!NILP (img
->data
.lisp_val
))
1874 mark_object (img
->data
.lisp_val
);
1879 mark_image_cache (struct image_cache
*c
)
1884 for (i
= 0; i
< c
->used
; ++i
)
1886 mark_image (c
->images
[i
]);
1892 /***********************************************************************
1893 X / NS / W32 support code
1894 ***********************************************************************/
1898 /* Macro for defining functions that will be loaded from image DLLs. */
1899 #define DEF_IMGLIB_FN(func,args) int (FAR CDECL *fn_##func)args
1901 /* Macro for loading those image functions from the library. */
1902 #define LOAD_IMGLIB_FN(lib,func) { \
1903 fn_##func = (void *) GetProcAddress (lib, #func); \
1904 if (!fn_##func) return 0; \
1907 /* Load a DLL implementing an image type.
1908 The argument LIBRARIES is usually the variable
1909 `dynamic-library-alist', which associates a symbol, identifying
1910 an external DLL library, to a list of possible filenames.
1911 The function returns NULL if no library could be loaded for
1912 the given symbol, or if the library was previously loaded;
1913 else the handle of the DLL. */
1915 w32_delayed_load (Lisp_Object libraries
, Lisp_Object type
)
1917 HMODULE library
= NULL
;
1919 if (CONSP (libraries
) && NILP (Fassq (type
, Vimage_type_cache
)))
1921 Lisp_Object dlls
= Fassq (type
, libraries
);
1924 for (dlls
= XCDR (dlls
); CONSP (dlls
); dlls
= XCDR (dlls
))
1926 CHECK_STRING_CAR (dlls
);
1927 if (library
= LoadLibrary (SDATA (XCAR (dlls
))))
1935 #endif /* HAVE_NTGUI */
1937 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1938 XImagePtr
*, Pixmap
*);
1939 static void x_destroy_x_image (XImagePtr
);
1940 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1943 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1944 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1945 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1946 via xmalloc. Print error messages via image_error if an error
1947 occurs. Value is non-zero if successful.
1949 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1950 should indicate the bit depth of the image. */
1953 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1954 XImagePtr
*ximg
, Pixmap
*pixmap
)
1956 #ifdef HAVE_X_WINDOWS
1957 Display
*display
= FRAME_X_DISPLAY (f
);
1958 Window window
= FRAME_X_WINDOW (f
);
1959 Screen
*screen
= FRAME_X_SCREEN (f
);
1961 xassert (interrupt_input_blocked
);
1964 depth
= DefaultDepthOfScreen (screen
);
1965 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1966 depth
, ZPixmap
, 0, NULL
, width
, height
,
1967 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1970 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1974 /* Allocate image raster. */
1975 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
1977 /* Allocate a pixmap of the same size. */
1978 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1979 if (*pixmap
== NO_PIXMAP
)
1981 x_destroy_x_image (*ximg
);
1983 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1988 #endif /* HAVE_X_WINDOWS */
1992 BITMAPINFOHEADER
*header
;
1994 int scanline_width_bits
;
1996 int palette_colors
= 0;
2001 if (depth
!= 1 && depth
!= 4 && depth
!= 8
2002 && depth
!= 16 && depth
!= 24 && depth
!= 32)
2004 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2008 scanline_width_bits
= width
* depth
;
2009 remainder
= scanline_width_bits
% 32;
2012 scanline_width_bits
+= 32 - remainder
;
2014 /* Bitmaps with a depth less than 16 need a palette. */
2015 /* BITMAPINFO structure already contains the first RGBQUAD. */
2017 palette_colors
= 1 << depth
- 1;
2019 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2022 image_error ("Unable to allocate memory for XImage", Qnil
, Qnil
);
2026 header
= &(*ximg
)->info
.bmiHeader
;
2027 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2028 header
->biSize
= sizeof (*header
);
2029 header
->biWidth
= width
;
2030 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2031 header
->biPlanes
= 1;
2032 header
->biBitCount
= depth
;
2033 header
->biCompression
= BI_RGB
;
2034 header
->biClrUsed
= palette_colors
;
2036 /* TODO: fill in palette. */
2039 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2040 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2041 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2042 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2043 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2044 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2045 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2046 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2049 hdc
= get_frame_dc (f
);
2051 /* Create a DIBSection and raster array for the bitmap,
2052 and store its handle in *pixmap. */
2053 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2054 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2055 /* casting avoids a GCC warning */
2056 (void **)&((*ximg
)->data
), NULL
, 0);
2058 /* Realize display palette and garbage all frames. */
2059 release_frame_dc (f
, hdc
);
2061 if (*pixmap
== NULL
)
2063 DWORD err
= GetLastError ();
2064 Lisp_Object errcode
;
2065 /* All system errors are < 10000, so the following is safe. */
2066 XSETINT (errcode
, (int) err
);
2067 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2068 x_destroy_x_image (*ximg
);
2074 #endif /* HAVE_NTGUI */
2077 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2081 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2090 /* Destroy XImage XIMG. Free XIMG->data. */
2093 x_destroy_x_image (XImagePtr ximg
)
2095 xassert (interrupt_input_blocked
);
2098 #ifdef HAVE_X_WINDOWS
2101 XDestroyImage (ximg
);
2102 #endif /* HAVE_X_WINDOWS */
2104 /* Data will be freed by DestroyObject. */
2107 #endif /* HAVE_NTGUI */
2109 ns_release_object (ximg
);
2110 #endif /* HAVE_NS */
2115 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2116 are width and height of both the image and pixmap. */
2119 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2121 #ifdef HAVE_X_WINDOWS
2124 xassert (interrupt_input_blocked
);
2125 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2126 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2127 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2128 #endif /* HAVE_X_WINDOWS */
2131 #if 0 /* I don't think this is necessary looking at where it is used. */
2132 HDC hdc
= get_frame_dc (f
);
2133 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2134 release_frame_dc (f
, hdc
);
2136 #endif /* HAVE_NTGUI */
2139 xassert (ximg
== pixmap
);
2140 ns_retain_object (ximg
);
2145 /***********************************************************************
2147 ***********************************************************************/
2149 static unsigned char *slurp_file (char *, int *);
2152 /* Find image file FILE. Look in data-directory/images, then
2153 x-bitmap-file-path. Value is the encoded full name of the file
2154 found, or nil if not found. */
2157 x_find_image_file (Lisp_Object file
)
2159 Lisp_Object file_found
, search_path
;
2162 /* TODO I think this should use something like image-load-path
2163 instead. Unfortunately, that can contain non-string elements. */
2164 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2166 Vx_bitmap_file_path
);
2168 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2169 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2175 file_found
= ENCODE_FILE (file_found
);
2183 /* Read FILE into memory. Value is a pointer to a buffer allocated
2184 with xmalloc holding FILE's contents. Value is null if an error
2185 occurred. *SIZE is set to the size of the file. */
2187 static unsigned char *
2188 slurp_file (char *file
, int *size
)
2191 unsigned char *buf
= NULL
;
2194 if (stat (file
, &st
) == 0
2195 && (fp
= fopen (file
, "rb")) != NULL
2196 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2197 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2218 /***********************************************************************
2220 ***********************************************************************/
2222 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2223 static int xbm_load (struct frame
*f
, struct image
*img
);
2224 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2225 unsigned char *, unsigned char *);
2226 static int xbm_image_p (Lisp_Object object
);
2227 static int xbm_read_bitmap_data (struct frame
*f
,
2228 unsigned char *, unsigned char *,
2229 int *, int *, unsigned char **, int);
2230 static int xbm_file_p (Lisp_Object
);
2233 /* Indices of image specification fields in xbm_format, below. */
2235 enum xbm_keyword_index
2253 /* Vector of image_keyword structures describing the format
2254 of valid XBM image specifications. */
2256 static const struct image_keyword xbm_format
[XBM_LAST
] =
2258 {":type", IMAGE_SYMBOL_VALUE
, 1},
2259 {":file", IMAGE_STRING_VALUE
, 0},
2260 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2261 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2262 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2263 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2264 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2265 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2266 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2267 {":relief", IMAGE_INTEGER_VALUE
, 0},
2268 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2269 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2270 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2273 /* Structure describing the image type XBM. */
2275 static struct image_type xbm_type
=
2284 /* Tokens returned from xbm_scan. */
2293 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2294 A valid specification is a list starting with the symbol `image'
2295 The rest of the list is a property list which must contain an
2298 If the specification specifies a file to load, it must contain
2299 an entry `:file FILENAME' where FILENAME is a string.
2301 If the specification is for a bitmap loaded from memory it must
2302 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2303 WIDTH and HEIGHT are integers > 0. DATA may be:
2305 1. a string large enough to hold the bitmap data, i.e. it must
2306 have a size >= (WIDTH + 7) / 8 * HEIGHT
2308 2. a bool-vector of size >= WIDTH * HEIGHT
2310 3. a vector of strings or bool-vectors, one for each line of the
2313 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2314 may not be specified in this case because they are defined in the
2317 Both the file and data forms may contain the additional entries
2318 `:background COLOR' and `:foreground COLOR'. If not present,
2319 foreground and background of the frame on which the image is
2320 displayed is used. */
2323 xbm_image_p (Lisp_Object object
)
2325 struct image_keyword kw
[XBM_LAST
];
2327 memcpy (kw
, xbm_format
, sizeof kw
);
2328 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2331 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2333 if (kw
[XBM_FILE
].count
)
2335 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2338 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2340 /* In-memory XBM file. */
2341 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2349 /* Entries for `:width', `:height' and `:data' must be present. */
2350 if (!kw
[XBM_WIDTH
].count
2351 || !kw
[XBM_HEIGHT
].count
2352 || !kw
[XBM_DATA
].count
)
2355 data
= kw
[XBM_DATA
].value
;
2356 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2357 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2359 /* Check type of data, and width and height against contents of
2365 /* Number of elements of the vector must be >= height. */
2366 if (XVECTOR (data
)->size
< height
)
2369 /* Each string or bool-vector in data must be large enough
2370 for one line of the image. */
2371 for (i
= 0; i
< height
; ++i
)
2373 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2378 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2381 else if (BOOL_VECTOR_P (elt
))
2383 if (XBOOL_VECTOR (elt
)->size
< width
)
2390 else if (STRINGP (data
))
2393 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2396 else if (BOOL_VECTOR_P (data
))
2398 if (XBOOL_VECTOR (data
)->size
< width
* height
)
2409 /* Scan a bitmap file. FP is the stream to read from. Value is
2410 either an enumerator from enum xbm_token, or a character for a
2411 single-character token, or 0 at end of file. If scanning an
2412 identifier, store the lexeme of the identifier in SVAL. If
2413 scanning a number, store its value in *IVAL. */
2416 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2422 /* Skip white space. */
2423 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2428 else if (isdigit (c
))
2430 int value
= 0, digit
;
2432 if (c
== '0' && *s
< end
)
2435 if (c
== 'x' || c
== 'X')
2442 else if (c
>= 'a' && c
<= 'f')
2443 digit
= c
- 'a' + 10;
2444 else if (c
>= 'A' && c
<= 'F')
2445 digit
= c
- 'A' + 10;
2448 value
= 16 * value
+ digit
;
2451 else if (isdigit (c
))
2455 && (c
= *(*s
)++, isdigit (c
)))
2456 value
= 8 * value
+ c
- '0';
2463 && (c
= *(*s
)++, isdigit (c
)))
2464 value
= 10 * value
+ c
- '0';
2472 else if (isalpha (c
) || c
== '_')
2476 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2483 else if (c
== '/' && **s
== '*')
2485 /* C-style comment. */
2487 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2501 /* Create a Windows bitmap from X bitmap data. */
2503 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2505 static unsigned char swap_nibble
[16]
2506 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2507 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2508 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2509 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2511 unsigned char *bits
, *p
;
2514 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2515 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2516 bits
= (unsigned char *) alloca (height
* w2
);
2517 memset (bits
, 0, height
* w2
);
2518 for (i
= 0; i
< height
; i
++)
2521 for (j
= 0; j
< w1
; j
++)
2523 /* Bitswap XBM bytes to match how Windows does things. */
2524 unsigned char c
= *data
++;
2525 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2526 | (swap_nibble
[(c
>>4) & 0xf]));
2529 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2535 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2536 COLORREF foreground
, COLORREF background
)
2538 HDC hdc
, old_img_dc
, new_img_dc
;
2539 HGDIOBJ old_prev
, new_prev
;
2542 hdc
= get_frame_dc (f
);
2543 old_img_dc
= CreateCompatibleDC (hdc
);
2544 new_img_dc
= CreateCompatibleDC (hdc
);
2545 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2546 release_frame_dc (f
, hdc
);
2547 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2548 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2549 /* Windows convention for mono bitmaps is black = background,
2550 white = foreground. */
2551 SetTextColor (new_img_dc
, background
);
2552 SetBkColor (new_img_dc
, foreground
);
2554 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2557 SelectObject (old_img_dc
, old_prev
);
2558 SelectObject (new_img_dc
, new_prev
);
2559 DeleteDC (old_img_dc
);
2560 DeleteDC (new_img_dc
);
2561 DeleteObject (img
->pixmap
);
2562 if (new_pixmap
== 0)
2563 fprintf (stderr
, "Failed to convert image to color.\n");
2565 img
->pixmap
= new_pixmap
;
2568 #define XBM_BIT_SHUFFLE(b) (~(b))
2572 #define XBM_BIT_SHUFFLE(b) (b)
2574 #endif /* HAVE_NTGUI */
2578 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2579 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2580 int non_default_colors
)
2584 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2586 /* If colors were specified, transfer the bitmap to a color one. */
2587 if (non_default_colors
)
2588 convert_mono_to_color_image (f
, img
, fg
, bg
);
2590 #elif defined (HAVE_NS)
2591 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2595 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2598 img
->width
, img
->height
,
2600 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
2601 #endif /* !HAVE_NTGUI && !HAVE_NS */
2606 /* Replacement for XReadBitmapFileData which isn't available under old
2607 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2608 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2609 the image. Return in *DATA the bitmap data allocated with xmalloc.
2610 Value is non-zero if successful. DATA null means just test if
2611 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2612 is non-zero, inhibit the call to image_error when the image size is
2613 invalid (the bitmap remains unread). */
2616 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2617 int *width
, int *height
, unsigned char **data
,
2618 int inhibit_image_error
)
2620 unsigned char *s
= contents
;
2621 char buffer
[BUFSIZ
];
2624 int bytes_per_line
, i
, nbytes
;
2630 LA1 = xbm_scan (&s, end, buffer, &value)
2632 #define expect(TOKEN) \
2633 if (LA1 != (TOKEN)) \
2638 #define expect_ident(IDENT) \
2639 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2644 *width
= *height
= -1;
2647 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2649 /* Parse defines for width, height and hot-spots. */
2653 expect_ident ("define");
2654 expect (XBM_TK_IDENT
);
2656 if (LA1
== XBM_TK_NUMBER
)
2658 char *p
= strrchr (buffer
, '_');
2659 p
= p
? p
+ 1 : buffer
;
2660 if (strcmp (p
, "width") == 0)
2662 else if (strcmp (p
, "height") == 0)
2665 expect (XBM_TK_NUMBER
);
2668 if (!check_image_size (f
, *width
, *height
))
2670 if (!inhibit_image_error
)
2671 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2674 else if (data
== NULL
)
2677 /* Parse bits. Must start with `static'. */
2678 expect_ident ("static");
2679 if (LA1
== XBM_TK_IDENT
)
2681 if (strcmp (buffer
, "unsigned") == 0)
2684 expect_ident ("char");
2686 else if (strcmp (buffer
, "short") == 0)
2690 if (*width
% 16 && *width
% 16 < 9)
2693 else if (strcmp (buffer
, "char") == 0)
2701 expect (XBM_TK_IDENT
);
2707 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2708 nbytes
= bytes_per_line
* *height
;
2709 p
= *data
= (unsigned char *) xmalloc (nbytes
);
2713 for (i
= 0; i
< nbytes
; i
+= 2)
2716 expect (XBM_TK_NUMBER
);
2718 *p
++ = XBM_BIT_SHUFFLE (val
);
2719 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2720 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2722 if (LA1
== ',' || LA1
== '}')
2730 for (i
= 0; i
< nbytes
; ++i
)
2733 expect (XBM_TK_NUMBER
);
2735 *p
++ = XBM_BIT_SHUFFLE (val
);
2737 if (LA1
== ',' || LA1
== '}')
2762 /* Load XBM image IMG which will be displayed on frame F from buffer
2763 CONTENTS. END is the end of the buffer. Value is non-zero if
2767 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2771 unsigned char *data
;
2774 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2778 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2779 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2780 int non_default_colors
= 0;
2783 xassert (img
->width
> 0 && img
->height
> 0);
2785 /* Get foreground and background colors, maybe allocate colors. */
2786 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2789 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2790 non_default_colors
= 1;
2792 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2795 background
= x_alloc_image_color (f
, img
, value
, background
);
2796 img
->background
= background
;
2797 img
->background_valid
= 1;
2798 non_default_colors
= 1;
2801 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2802 foreground
, background
,
2803 non_default_colors
);
2806 if (img
->pixmap
== NO_PIXMAP
)
2808 x_clear_image (f
, img
);
2809 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2815 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2821 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2824 xbm_file_p (Lisp_Object data
)
2827 return (STRINGP (data
)
2828 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2829 (SDATA (data
) + SBYTES (data
)),
2834 /* Fill image IMG which is used on frame F with pixmap data. Value is
2835 non-zero if successful. */
2838 xbm_load (struct frame
*f
, struct image
*img
)
2841 Lisp_Object file_name
;
2843 xassert (xbm_image_p (img
->spec
));
2845 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2846 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2847 if (STRINGP (file_name
))
2850 unsigned char *contents
;
2853 file
= x_find_image_file (file_name
);
2854 if (!STRINGP (file
))
2856 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2860 contents
= slurp_file (SDATA (file
), &size
);
2861 if (contents
== NULL
)
2863 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2867 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2871 struct image_keyword fmt
[XBM_LAST
];
2873 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2874 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2875 int non_default_colors
= 0;
2878 int in_memory_file_p
= 0;
2880 /* See if data looks like an in-memory XBM file. */
2881 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2882 in_memory_file_p
= xbm_file_p (data
);
2884 /* Parse the image specification. */
2885 memcpy (fmt
, xbm_format
, sizeof fmt
);
2886 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2889 /* Get specified width, and height. */
2890 if (!in_memory_file_p
)
2892 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2893 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2894 xassert (img
->width
> 0 && img
->height
> 0);
2897 /* Get foreground and background colors, maybe allocate colors. */
2898 if (fmt
[XBM_FOREGROUND
].count
2899 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2901 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2903 non_default_colors
= 1;
2906 if (fmt
[XBM_BACKGROUND
].count
2907 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2909 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2911 non_default_colors
= 1;
2914 if (in_memory_file_p
)
2915 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2924 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2926 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2927 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2929 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2931 memcpy (p
, SDATA (line
), nbytes
);
2933 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2936 else if (STRINGP (data
))
2937 bits
= SDATA (data
);
2939 bits
= XBOOL_VECTOR (data
)->data
;
2945 /* Windows mono bitmaps are reversed compared with X. */
2946 invertedBits
= bits
;
2947 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2949 bits
= (char *) alloca (nbytes
);
2950 for (i
= 0; i
< nbytes
; i
++)
2951 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2954 /* Create the pixmap. */
2956 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2957 foreground
, background
,
2958 non_default_colors
);
2963 image_error ("Unable to create pixmap for XBM image `%s'",
2965 x_clear_image (f
, img
);
2975 /***********************************************************************
2977 ***********************************************************************/
2979 #if defined (HAVE_XPM) || defined (HAVE_NS)
2981 static int xpm_image_p (Lisp_Object object
);
2982 static int xpm_load (struct frame
*f
, struct image
*img
);
2983 static int xpm_valid_color_symbols_p (Lisp_Object
);
2985 #endif /* HAVE_XPM || HAVE_NS */
2989 /* Indicate to xpm.h that we don't have Xlib. */
2991 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2992 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2993 #define XColor xpm_XColor
2994 #define XImage xpm_XImage
2995 #define Display xpm_Display
2996 #define PIXEL_ALREADY_TYPEDEFED
2997 #include "X11/xpm.h"
3002 #undef PIXEL_ALREADY_TYPEDEFED
3004 #include "X11/xpm.h"
3005 #endif /* HAVE_NTGUI */
3006 #endif /* HAVE_XPM */
3008 #if defined (HAVE_XPM) || defined (HAVE_NS)
3009 /* The symbol `xpm' identifying XPM-format images. */
3013 /* Indices of image specification fields in xpm_format, below. */
3015 enum xpm_keyword_index
3031 /* Vector of image_keyword structures describing the format
3032 of valid XPM image specifications. */
3034 static const struct image_keyword xpm_format
[XPM_LAST
] =
3036 {":type", IMAGE_SYMBOL_VALUE
, 1},
3037 {":file", IMAGE_STRING_VALUE
, 0},
3038 {":data", IMAGE_STRING_VALUE
, 0},
3039 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3040 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3041 {":relief", IMAGE_INTEGER_VALUE
, 0},
3042 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3043 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3044 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3045 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3046 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3049 /* Structure describing the image type XPM. */
3051 static struct image_type xpm_type
=
3060 #ifdef HAVE_X_WINDOWS
3062 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3063 functions for allocating image colors. Our own functions handle
3064 color allocation failures more gracefully than the ones on the XPM
3067 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3068 #define ALLOC_XPM_COLORS
3070 #endif /* HAVE_X_WINDOWS */
3072 #ifdef ALLOC_XPM_COLORS
3074 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3075 static void xpm_free_color_cache (void);
3076 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3077 static int xpm_color_bucket (char *);
3078 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3081 /* An entry in a hash table used to cache color definitions of named
3082 colors. This cache is necessary to speed up XPM image loading in
3083 case we do color allocations ourselves. Without it, we would need
3084 a call to XParseColor per pixel in the image. */
3086 struct xpm_cached_color
3088 /* Next in collision chain. */
3089 struct xpm_cached_color
*next
;
3091 /* Color definition (RGB and pixel color). */
3098 /* The hash table used for the color cache, and its bucket vector
3101 #define XPM_COLOR_CACHE_BUCKETS 1001
3102 struct xpm_cached_color
**xpm_color_cache
;
3104 /* Initialize the color cache. */
3107 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3109 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3110 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3111 memset (xpm_color_cache
, 0, nbytes
);
3112 init_color_table ();
3114 if (attrs
->valuemask
& XpmColorSymbols
)
3119 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3120 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3121 attrs
->colorsymbols
[i
].value
, &color
))
3123 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3125 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3130 /* Free the color cache. */
3133 xpm_free_color_cache (void)
3135 struct xpm_cached_color
*p
, *next
;
3138 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3139 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3145 xfree (xpm_color_cache
);
3146 xpm_color_cache
= NULL
;
3147 free_color_table ();
3150 /* Return the bucket index for color named COLOR_NAME in the color
3154 xpm_color_bucket (char *color_name
)
3159 for (s
= color_name
; *s
; ++s
)
3161 return h
%= XPM_COLOR_CACHE_BUCKETS
;
3165 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3166 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3169 static struct xpm_cached_color
*
3170 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3173 struct xpm_cached_color
*p
;
3176 bucket
= xpm_color_bucket (color_name
);
3178 nbytes
= sizeof *p
+ strlen (color_name
);
3179 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3180 strcpy (p
->name
, color_name
);
3182 p
->next
= xpm_color_cache
[bucket
];
3183 xpm_color_cache
[bucket
] = p
;
3187 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3188 return the cached definition in *COLOR. Otherwise, make a new
3189 entry in the cache and allocate the color. Value is zero if color
3190 allocation failed. */
3193 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3195 struct xpm_cached_color
*p
;
3196 int h
= xpm_color_bucket (color_name
);
3198 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3199 if (strcmp (p
->name
, color_name
) == 0)
3204 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3207 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3209 p
= xpm_cache_color (f
, color_name
, color
, h
);
3211 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3212 with transparency, and it's useful. */
3213 else if (strcmp ("opaque", color_name
) == 0)
3215 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3216 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3217 p
= xpm_cache_color (f
, color_name
, color
, h
);
3224 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3225 CLOSURE is a pointer to the frame on which we allocate the
3226 color. Return in *COLOR the allocated color. Value is non-zero
3230 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3233 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3237 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3238 is a pointer to the frame on which we allocate the color. Value is
3239 non-zero if successful. */
3242 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3247 #endif /* ALLOC_XPM_COLORS */
3252 /* XPM library details. */
3254 DEF_IMGLIB_FN (XpmFreeAttributes
, (XpmAttributes
*));
3255 DEF_IMGLIB_FN (XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3256 xpm_XImage
**, XpmAttributes
*));
3257 DEF_IMGLIB_FN (XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3258 xpm_XImage
**, XpmAttributes
*));
3259 DEF_IMGLIB_FN (XImageFree
, (xpm_XImage
*));
3262 init_xpm_functions (Lisp_Object libraries
)
3266 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3269 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3270 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3271 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3272 LOAD_IMGLIB_FN (library
, XImageFree
);
3276 #endif /* HAVE_NTGUI */
3279 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3280 for XPM images. Such a list must consist of conses whose car and
3284 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3286 while (CONSP (color_symbols
))
3288 Lisp_Object sym
= XCAR (color_symbols
);
3290 || !STRINGP (XCAR (sym
))
3291 || !STRINGP (XCDR (sym
)))
3293 color_symbols
= XCDR (color_symbols
);
3296 return NILP (color_symbols
);
3300 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3303 xpm_image_p (Lisp_Object object
)
3305 struct image_keyword fmt
[XPM_LAST
];
3306 memcpy (fmt
, xpm_format
, sizeof fmt
);
3307 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3308 /* Either `:file' or `:data' must be present. */
3309 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3310 /* Either no `:color-symbols' or it's a list of conses
3311 whose car and cdr are strings. */
3312 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3313 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3316 #endif /* HAVE_XPM || HAVE_NS */
3318 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
3320 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3322 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3324 XpmAttributes attrs
;
3325 Pixmap bitmap
, mask
;
3327 memset (&attrs
, 0, sizeof attrs
);
3329 attrs
.visual
= FRAME_X_VISUAL (f
);
3330 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3331 attrs
.valuemask
|= XpmVisual
;
3332 attrs
.valuemask
|= XpmColormap
;
3334 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3335 (char **) bits
, &bitmap
, &mask
, &attrs
);
3336 if (rc
!= XpmSuccess
)
3338 XpmFreeAttributes (&attrs
);
3342 id
= x_allocate_bitmap_record (f
);
3343 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3344 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3345 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3346 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3347 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3348 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3349 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3350 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3352 XpmFreeAttributes (&attrs
);
3355 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3357 /* Load image IMG which will be displayed on frame F. Value is
3358 non-zero if successful. */
3363 xpm_load (struct frame
*f
, struct image
*img
)
3366 XpmAttributes attrs
;
3367 Lisp_Object specified_file
, color_symbols
;
3370 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3371 #endif /* HAVE_NTGUI */
3373 /* Configure the XPM lib. Use the visual of frame F. Allocate
3374 close colors. Return colors allocated. */
3375 memset (&attrs
, 0, sizeof attrs
);
3378 attrs
.visual
= FRAME_X_VISUAL (f
);
3379 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3380 attrs
.valuemask
|= XpmVisual
;
3381 attrs
.valuemask
|= XpmColormap
;
3382 #endif /* HAVE_NTGUI */
3384 #ifdef ALLOC_XPM_COLORS
3385 /* Allocate colors with our own functions which handle
3386 failing color allocation more gracefully. */
3387 attrs
.color_closure
= f
;
3388 attrs
.alloc_color
= xpm_alloc_color
;
3389 attrs
.free_colors
= xpm_free_colors
;
3390 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3391 #else /* not ALLOC_XPM_COLORS */
3392 /* Let the XPM lib allocate colors. */
3393 attrs
.valuemask
|= XpmReturnAllocPixels
;
3394 #ifdef XpmAllocCloseColors
3395 attrs
.alloc_close_colors
= 1;
3396 attrs
.valuemask
|= XpmAllocCloseColors
;
3397 #else /* not XpmAllocCloseColors */
3398 attrs
.closeness
= 600;
3399 attrs
.valuemask
|= XpmCloseness
;
3400 #endif /* not XpmAllocCloseColors */
3401 #endif /* ALLOC_XPM_COLORS */
3403 /* If image specification contains symbolic color definitions, add
3404 these to `attrs'. */
3405 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3406 if (CONSP (color_symbols
))
3409 XpmColorSymbol
*xpm_syms
;
3412 attrs
.valuemask
|= XpmColorSymbols
;
3414 /* Count number of symbols. */
3415 attrs
.numsymbols
= 0;
3416 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3419 /* Allocate an XpmColorSymbol array. */
3420 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3421 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3422 memset (xpm_syms
, 0, size
);
3423 attrs
.colorsymbols
= xpm_syms
;
3425 /* Fill the color symbol array. */
3426 for (tail
= color_symbols
, i
= 0;
3428 ++i
, tail
= XCDR (tail
))
3433 if (!CONSP (XCAR (tail
)))
3435 xpm_syms
[i
].name
= "";
3436 xpm_syms
[i
].value
= "";
3439 name
= XCAR (XCAR (tail
));
3440 color
= XCDR (XCAR (tail
));
3443 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3444 strcpy (xpm_syms
[i
].name
, SDATA (name
));
3447 xpm_syms
[i
].name
= "";
3448 if (STRINGP (color
))
3450 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3451 strcpy (xpm_syms
[i
].value
, SDATA (color
));
3454 xpm_syms
[i
].value
= "";
3458 /* Create a pixmap for the image, either from a file, or from a
3459 string buffer containing data in the same format as an XPM file. */
3460 #ifdef ALLOC_XPM_COLORS
3461 xpm_init_color_cache (f
, &attrs
);
3464 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3468 HDC frame_dc
= get_frame_dc (f
);
3469 hdc
= CreateCompatibleDC (frame_dc
);
3470 release_frame_dc (f
, frame_dc
);
3472 #endif /* HAVE_NTGUI */
3474 if (STRINGP (specified_file
))
3476 Lisp_Object file
= x_find_image_file (specified_file
);
3477 if (!STRINGP (file
))
3479 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3480 #ifdef ALLOC_XPM_COLORS
3481 xpm_free_color_cache ();
3487 /* XpmReadFileToPixmap is not available in the Windows port of
3488 libxpm. But XpmReadFileToImage almost does what we want. */
3489 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3490 &xpm_image
, &xpm_mask
,
3493 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3494 SDATA (file
), &img
->pixmap
, &img
->mask
,
3496 #endif /* HAVE_NTGUI */
3500 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3501 if (!STRINGP (buffer
))
3503 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3504 #ifdef ALLOC_XPM_COLORS
3505 xpm_free_color_cache ();
3510 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3511 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3512 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3513 &xpm_image
, &xpm_mask
,
3516 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3518 &img
->pixmap
, &img
->mask
,
3520 #endif /* HAVE_NTGUI */
3523 if (rc
== XpmSuccess
)
3525 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3526 img
->colors
= colors_in_color_table (&img
->ncolors
);
3527 #else /* not ALLOC_XPM_COLORS */
3531 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3532 plus some duplicate attributes. */
3533 if (xpm_image
&& xpm_image
->bitmap
)
3535 img
->pixmap
= xpm_image
->bitmap
;
3536 /* XImageFree in libXpm frees XImage struct without destroying
3537 the bitmap, which is what we want. */
3538 fn_XImageFree (xpm_image
);
3540 if (xpm_mask
&& xpm_mask
->bitmap
)
3542 /* The mask appears to be inverted compared with what we expect.
3543 TODO: invert our expectations. See other places where we
3544 have to invert bits because our idea of masks is backwards. */
3546 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3548 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3549 SelectObject (hdc
, old_obj
);
3551 img
->mask
= xpm_mask
->bitmap
;
3552 fn_XImageFree (xpm_mask
);
3557 #endif /* HAVE_NTGUI */
3559 /* Remember allocated colors. */
3560 img
->ncolors
= attrs
.nalloc_pixels
;
3561 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
3562 * sizeof *img
->colors
);
3563 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3565 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3566 #ifdef DEBUG_X_COLORS
3567 register_color (img
->colors
[i
]);
3570 #endif /* not ALLOC_XPM_COLORS */
3572 img
->width
= attrs
.width
;
3573 img
->height
= attrs
.height
;
3574 xassert (img
->width
> 0 && img
->height
> 0);
3576 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3578 fn_XpmFreeAttributes (&attrs
);
3580 XpmFreeAttributes (&attrs
);
3581 #endif /* HAVE_NTGUI */
3587 #endif /* HAVE_NTGUI */
3592 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3595 case XpmFileInvalid
:
3596 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3600 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3603 case XpmColorFailed
:
3604 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3608 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3613 #ifdef ALLOC_XPM_COLORS
3614 xpm_free_color_cache ();
3616 return rc
== XpmSuccess
;
3619 #endif /* HAVE_XPM */
3621 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3623 /* XPM support functions for NS where libxpm is not available.
3624 Only XPM version 3 (without any extensions) is supported. */
3626 static int xpm_scan (const unsigned char **, const unsigned char *,
3627 const unsigned char **, int *);
3628 static Lisp_Object xpm_make_color_table_v
3629 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3630 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3631 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3633 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3634 const unsigned char *, int);
3635 static Lisp_Object xpm_make_color_table_h
3636 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3637 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3638 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3640 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3641 const unsigned char *, int);
3642 static int xpm_str_to_color_key (const char *);
3643 static int xpm_load_image (struct frame
*, struct image
*,
3644 const unsigned char *, const unsigned char *);
3646 /* Tokens returned from xpm_scan. */
3655 /* Scan an XPM data and return a character (< 256) or a token defined
3656 by enum xpm_token above. *S and END are the start (inclusive) and
3657 the end (exclusive) addresses of the data, respectively. Advance
3658 *S while scanning. If token is either XPM_TK_IDENT or
3659 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3660 length of the corresponding token, respectively. */
3663 xpm_scan (const unsigned char **s
,
3664 const unsigned char *end
,
3665 const unsigned char **beg
,
3672 /* Skip white-space. */
3673 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3676 /* gnus-pointer.xpm uses '-' in its identifier.
3677 sb-dir-plus.xpm uses '+' in its identifier. */
3678 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3682 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3685 return XPM_TK_IDENT
;
3690 while (*s
< end
&& **s
!= '"')
3695 return XPM_TK_STRING
;
3699 if (*s
< end
&& **s
== '*')
3701 /* C-style comment. */
3705 while (*s
< end
&& *(*s
)++ != '*')
3708 while (*s
< end
&& **s
!= '/');
3722 /* Functions for color table lookup in XPM data. A key is a string
3723 specifying the color of each pixel in XPM data. A value is either
3724 an integer that specifies a pixel color, Qt that specifies
3725 transparency, or Qnil for the unspecified color. If the length of
3726 the key string is one, a vector is used as a table. Otherwise, a
3727 hash table is used. */
3730 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3731 const unsigned char *,
3734 Lisp_Object (**get_func
) (Lisp_Object
,
3735 const unsigned char *,
3738 *put_func
= xpm_put_color_table_v
;
3739 *get_func
= xpm_get_color_table_v
;
3740 return Fmake_vector (make_number (256), Qnil
);
3744 xpm_put_color_table_v (Lisp_Object color_table
,
3745 const unsigned char *chars_start
,
3749 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3753 xpm_get_color_table_v (Lisp_Object color_table
,
3754 const unsigned char *chars_start
,
3757 return XVECTOR (color_table
)->contents
[*chars_start
];
3761 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3762 const unsigned char *,
3765 Lisp_Object (**get_func
) (Lisp_Object
,
3766 const unsigned char *,
3769 *put_func
= xpm_put_color_table_h
;
3770 *get_func
= xpm_get_color_table_h
;
3771 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3772 make_float (DEFAULT_REHASH_SIZE
),
3773 make_float (DEFAULT_REHASH_THRESHOLD
),
3778 xpm_put_color_table_h (Lisp_Object color_table
,
3779 const unsigned char *chars_start
,
3783 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3785 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3787 hash_lookup (table
, chars
, &hash_code
);
3788 hash_put (table
, chars
, color
, hash_code
);
3792 xpm_get_color_table_h (Lisp_Object color_table
,
3793 const unsigned char *chars_start
,
3796 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3797 int i
= hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
),
3800 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3803 enum xpm_color_key
{
3811 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3814 xpm_str_to_color_key (const char *s
)
3819 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3821 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3827 xpm_load_image (struct frame
*f
,
3829 const unsigned char *contents
,
3830 const unsigned char *end
)
3832 const unsigned char *s
= contents
, *beg
, *str
;
3833 unsigned char buffer
[BUFSIZ
];
3834 int width
, height
, x
, y
;
3835 int num_colors
, chars_per_pixel
;
3837 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3838 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3839 Lisp_Object frame
, color_symbols
, color_table
;
3840 int best_key
, have_mask
= 0;
3841 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3844 LA1 = xpm_scan (&s, end, &beg, &len)
3846 #define expect(TOKEN) \
3847 if (LA1 != (TOKEN)) \
3852 #define expect_ident(IDENT) \
3853 if (LA1 == XPM_TK_IDENT \
3854 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3859 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3863 expect_ident ("static");
3864 expect_ident ("char");
3866 expect (XPM_TK_IDENT
);
3871 expect (XPM_TK_STRING
);
3874 memcpy (buffer
, beg
, len
);
3876 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3877 &num_colors
, &chars_per_pixel
) != 4
3878 || width
<= 0 || height
<= 0
3879 || num_colors
<= 0 || chars_per_pixel
<= 0)
3882 if (!check_image_size (f
, width
, height
))
3884 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3890 XSETFRAME (frame
, f
);
3891 if (!NILP (Fxw_display_color_p (frame
)))
3892 best_key
= XPM_COLOR_KEY_C
;
3893 else if (!NILP (Fx_display_grayscale_p (frame
)))
3894 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3895 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3897 best_key
= XPM_COLOR_KEY_M
;
3899 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3900 if (chars_per_pixel
== 1)
3901 color_table
= xpm_make_color_table_v (&put_color_table
,
3904 color_table
= xpm_make_color_table_h (&put_color_table
,
3907 while (num_colors
-- > 0)
3909 unsigned char *color
, *max_color
;
3910 int key
, next_key
, max_key
= 0;
3911 Lisp_Object symbol_color
= Qnil
, color_val
;
3914 expect (XPM_TK_STRING
);
3915 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3917 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3918 buffer
[len
- chars_per_pixel
] = '\0';
3920 str
= strtok (buffer
, " \t");
3923 key
= xpm_str_to_color_key (str
);
3928 color
= strtok (NULL
, " \t");
3932 while ((str
= strtok (NULL
, " \t")) != NULL
)
3934 next_key
= xpm_str_to_color_key (str
);
3937 color
[strlen (color
)] = ' ';
3940 if (key
== XPM_COLOR_KEY_S
)
3942 if (NILP (symbol_color
))
3943 symbol_color
= build_string (color
);
3945 else if (max_key
< key
&& key
<= best_key
)
3955 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3957 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3959 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3961 if (xstrcasecmp (SDATA (XCDR (specified_color
)), "None") == 0)
3963 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3965 color_val
= make_number (cdef
.pixel
);
3968 if (NILP (color_val
) && max_key
> 0)
3970 if (xstrcasecmp (max_color
, "None") == 0)
3972 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3973 color_val
= make_number (cdef
.pixel
);
3975 if (!NILP (color_val
))
3976 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3981 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3982 &ximg
, &img
->pixmap
)
3984 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3985 &mask_img
, &img
->mask
)
3989 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3993 for (y
= 0; y
< height
; y
++)
3995 expect (XPM_TK_STRING
);
3997 if (len
< width
* chars_per_pixel
)
3999 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4001 Lisp_Object color_val
=
4002 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4004 XPutPixel (ximg
, x
, y
,
4005 (INTEGERP (color_val
) ? XINT (color_val
)
4006 : FRAME_FOREGROUND_PIXEL (f
)));
4008 XPutPixel (mask_img
, x
, y
,
4009 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4010 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4012 if (EQ (color_val
, Qt
))
4013 ns_set_alpha (ximg
, x
, y
, 0);
4021 img
->height
= height
;
4023 /* Maybe fill in the background field while we have ximg handy. */
4024 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4025 IMAGE_BACKGROUND (img
, f
, ximg
);
4027 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4028 x_destroy_x_image (ximg
);
4032 /* Fill in the background_transparent field while we have the
4034 image_background_transparent (img
, f
, mask_img
);
4036 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4037 x_destroy_x_image (mask_img
);
4041 x_destroy_x_image (mask_img
);
4042 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4043 img
->mask
= NO_PIXMAP
;
4049 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4051 x_destroy_x_image (ximg
);
4052 x_destroy_x_image (mask_img
);
4053 x_clear_image (f
, img
);
4062 xpm_load (struct frame
*f
,
4066 Lisp_Object file_name
;
4068 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4069 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4070 if (STRINGP (file_name
))
4073 unsigned char *contents
;
4076 file
= x_find_image_file (file_name
);
4077 if (!STRINGP (file
))
4079 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4083 contents
= slurp_file (SDATA (file
), &size
);
4084 if (contents
== NULL
)
4086 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4090 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4097 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4098 if (!STRINGP (data
))
4100 image_error ("Invalid image data `%s'", data
, Qnil
);
4103 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4104 SDATA (data
) + SBYTES (data
));
4110 #endif /* HAVE_NS && !HAVE_XPM */
4114 /***********************************************************************
4116 ***********************************************************************/
4118 #ifdef COLOR_TABLE_SUPPORT
4120 /* An entry in the color table mapping an RGB color to a pixel color. */
4125 unsigned long pixel
;
4127 /* Next in color table collision list. */
4128 struct ct_color
*next
;
4131 /* The bucket vector size to use. Must be prime. */
4135 /* Value is a hash of the RGB color given by R, G, and B. */
4137 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4139 /* The color hash table. */
4141 struct ct_color
**ct_table
;
4143 /* Number of entries in the color table. */
4145 int ct_colors_allocated
;
4147 /* Initialize the color table. */
4150 init_color_table (void)
4152 int size
= CT_SIZE
* sizeof (*ct_table
);
4153 ct_table
= (struct ct_color
**) xmalloc (size
);
4154 memset (ct_table
, 0, size
);
4155 ct_colors_allocated
= 0;
4159 /* Free memory associated with the color table. */
4162 free_color_table (void)
4165 struct ct_color
*p
, *next
;
4167 for (i
= 0; i
< CT_SIZE
; ++i
)
4168 for (p
= ct_table
[i
]; p
; p
= next
)
4179 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4180 entry for that color already is in the color table, return the
4181 pixel color of that entry. Otherwise, allocate a new color for R,
4182 G, B, and make an entry in the color table. */
4184 static unsigned long
4185 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4187 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4188 int i
= hash
% CT_SIZE
;
4190 Display_Info
*dpyinfo
;
4192 /* Handle TrueColor visuals specially, which improves performance by
4193 two orders of magnitude. Freeing colors on TrueColor visuals is
4194 a nop, and pixel colors specify RGB values directly. See also
4195 the Xlib spec, chapter 3.1. */
4196 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4197 if (dpyinfo
->red_bits
> 0)
4199 unsigned long pr
, pg
, pb
;
4201 /* Apply gamma-correction like normal color allocation does. */
4205 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4206 gamma_correct (f
, &color
);
4207 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4210 /* Scale down RGB values to the visual's bits per RGB, and shift
4211 them to the right position in the pixel color. Note that the
4212 original RGB values are 16-bit values, as usual in X. */
4213 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4214 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4215 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4217 /* Assemble the pixel color. */
4218 return pr
| pg
| pb
;
4221 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4222 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4228 #ifdef HAVE_X_WINDOWS
4237 cmap
= FRAME_X_COLORMAP (f
);
4238 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4241 ++ct_colors_allocated
;
4242 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4246 p
->pixel
= color
.pixel
;
4247 p
->next
= ct_table
[i
];
4251 return FRAME_FOREGROUND_PIXEL (f
);
4256 color
= PALETTERGB (r
, g
, b
);
4258 color
= RGB_TO_ULONG (r
, g
, b
);
4259 #endif /* HAVE_NTGUI */
4260 ++ct_colors_allocated
;
4261 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4266 p
->next
= ct_table
[i
];
4268 #endif /* HAVE_X_WINDOWS */
4276 /* Look up pixel color PIXEL which is used on frame F in the color
4277 table. If not already present, allocate it. Value is PIXEL. */
4279 static unsigned long
4280 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4282 int i
= pixel
% CT_SIZE
;
4285 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4286 if (p
->pixel
== pixel
)
4295 #ifdef HAVE_X_WINDOWS
4296 cmap
= FRAME_X_COLORMAP (f
);
4297 color
.pixel
= pixel
;
4298 x_query_color (f
, &color
);
4299 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4302 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4303 color
.pixel
= pixel
;
4304 XQueryColor (NULL
, cmap
, &color
);
4305 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4307 #endif /* HAVE_X_WINDOWS */
4311 ++ct_colors_allocated
;
4313 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4318 p
->next
= ct_table
[i
];
4322 return FRAME_FOREGROUND_PIXEL (f
);
4328 /* Value is a vector of all pixel colors contained in the color table,
4329 allocated via xmalloc. Set *N to the number of colors. */
4331 static unsigned long *
4332 colors_in_color_table (int *n
)
4336 unsigned long *colors
;
4338 if (ct_colors_allocated
== 0)
4345 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4347 *n
= ct_colors_allocated
;
4349 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4350 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4351 colors
[j
++] = p
->pixel
;
4357 #else /* COLOR_TABLE_SUPPORT */
4359 static unsigned long
4360 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4362 unsigned long pixel
;
4365 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4366 #endif /* HAVE_NTGUI */
4369 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4370 #endif /* HAVE_NS */
4375 init_color_table (void)
4378 #endif /* COLOR_TABLE_SUPPORT */
4381 /***********************************************************************
4383 ***********************************************************************/
4385 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4386 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4387 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4390 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4391 #endif /* HAVE_NTGUI */
4393 /* Edge detection matrices for different edge-detection
4396 static int emboss_matrix
[9] = {
4398 2, -1, 0, /* y - 1 */
4400 0, 1, -2 /* y + 1 */
4403 static int laplace_matrix
[9] = {
4405 1, 0, 0, /* y - 1 */
4407 0, 0, -1 /* y + 1 */
4410 /* Value is the intensity of the color whose red/green/blue values
4413 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4416 /* On frame F, return an array of XColor structures describing image
4417 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4418 non-zero means also fill the red/green/blue members of the XColor
4419 structures. Value is a pointer to the array of XColors structures,
4420 allocated with xmalloc; it must be freed by the caller. */
4423 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4427 XImagePtr_or_DC ximg
;
4431 #endif /* HAVE_NTGUI */
4433 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
4436 /* Get the X image IMG->pixmap. */
4437 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4438 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4440 /* Load the image into a memory device context. */
4441 hdc
= get_frame_dc (f
);
4442 ximg
= CreateCompatibleDC (hdc
);
4443 release_frame_dc (f
, hdc
);
4444 prev
= SelectObject (ximg
, img
->pixmap
);
4445 #endif /* HAVE_NTGUI */
4447 /* Fill the `pixel' members of the XColor array. I wished there
4448 were an easy and portable way to circumvent XGetPixel. */
4450 for (y
= 0; y
< img
->height
; ++y
)
4454 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4455 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4456 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4458 x_query_colors (f
, row
, img
->width
);
4462 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4464 /* W32_TODO: palette support needed here? */
4465 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4468 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4469 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4470 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4473 #endif /* HAVE_X_WINDOWS */
4476 Destroy_Image (ximg
, prev
);
4483 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4484 created with CreateDIBSection, with the pointer to the bit values
4485 stored in ximg->data. */
4488 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4490 int width
= ximg
->info
.bmiHeader
.biWidth
;
4491 int height
= ximg
->info
.bmiHeader
.biHeight
;
4492 unsigned char * pixel
;
4494 /* True color images. */
4495 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4497 int rowbytes
= width
* 3;
4498 /* Ensure scanlines are aligned on 4 byte boundaries. */
4500 rowbytes
+= 4 - (rowbytes
% 4);
4502 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4503 /* Windows bitmaps are in BGR order. */
4504 *pixel
= GetBValue (color
);
4505 *(pixel
+ 1) = GetGValue (color
);
4506 *(pixel
+ 2) = GetRValue (color
);
4508 /* Monochrome images. */
4509 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4511 int rowbytes
= width
/ 8;
4512 /* Ensure scanlines are aligned on 4 byte boundaries. */
4514 rowbytes
+= 4 - (rowbytes
% 4);
4515 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4516 /* Filter out palette info. */
4517 if (color
& 0x00ffffff)
4518 *pixel
= *pixel
| (1 << x
% 8);
4520 *pixel
= *pixel
& ~(1 << x
% 8);
4523 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4526 #endif /* HAVE_NTGUI */
4528 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4529 RGB members are set. F is the frame on which this all happens.
4530 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4533 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4536 XImagePtr oimg
= NULL
;
4540 init_color_table ();
4542 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4545 for (y
= 0; y
< img
->height
; ++y
)
4546 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4548 unsigned long pixel
;
4549 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4550 XPutPixel (oimg
, x
, y
, pixel
);
4554 x_clear_image_1 (f
, img
, 1, 0, 1);
4556 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4557 x_destroy_x_image (oimg
);
4558 img
->pixmap
= pixmap
;
4559 #ifdef COLOR_TABLE_SUPPORT
4560 img
->colors
= colors_in_color_table (&img
->ncolors
);
4561 free_color_table ();
4562 #endif /* COLOR_TABLE_SUPPORT */
4566 /* On frame F, perform edge-detection on image IMG.
4568 MATRIX is a nine-element array specifying the transformation
4569 matrix. See emboss_matrix for an example.
4571 COLOR_ADJUST is a color adjustment added to each pixel of the
4575 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4577 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4581 for (i
= sum
= 0; i
< 9; ++i
)
4582 sum
+= eabs (matrix
[i
]);
4584 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4586 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
4588 for (y
= 0; y
< img
->height
; ++y
)
4590 p
= COLOR (new, 0, y
);
4591 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4592 p
= COLOR (new, img
->width
- 1, y
);
4593 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4596 for (x
= 1; x
< img
->width
- 1; ++x
)
4598 p
= COLOR (new, x
, 0);
4599 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4600 p
= COLOR (new, x
, img
->height
- 1);
4601 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4604 for (y
= 1; y
< img
->height
- 1; ++y
)
4606 p
= COLOR (new, 1, y
);
4608 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4610 int r
, g
, b
, y1
, x1
;
4613 for (y1
= y
- 1; y1
< y
+ 2; ++y1
)
4614 for (x1
= x
- 1; x1
< x
+ 2; ++x1
, ++i
)
4617 XColor
*t
= COLOR (colors
, x1
, y1
);
4618 r
+= matrix
[i
] * t
->red
;
4619 g
+= matrix
[i
] * t
->green
;
4620 b
+= matrix
[i
] * t
->blue
;
4623 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4624 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4625 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4626 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4631 x_from_xcolors (f
, img
, new);
4637 /* Perform the pre-defined `emboss' edge-detection on image IMG
4641 x_emboss (struct frame
*f
, struct image
*img
)
4643 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4647 /* Transform image IMG which is used on frame F with a Laplace
4648 edge-detection algorithm. The result is an image that can be used
4649 to draw disabled buttons, for example. */
4652 x_laplace (struct frame
*f
, struct image
*img
)
4654 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4658 /* Perform edge-detection on image IMG on frame F, with specified
4659 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4661 MATRIX must be either
4663 - a list of at least 9 numbers in row-major form
4664 - a vector of at least 9 numbers
4666 COLOR_ADJUST nil means use a default; otherwise it must be a
4670 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4671 Lisp_Object color_adjust
)
4679 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4680 ++i
, matrix
= XCDR (matrix
))
4681 trans
[i
] = XFLOATINT (XCAR (matrix
));
4683 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4685 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4686 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4689 if (NILP (color_adjust
))
4690 color_adjust
= make_number (0xffff / 2);
4692 if (i
== 9 && NUMBERP (color_adjust
))
4693 x_detect_edges (f
, img
, trans
, (int) XFLOATINT (color_adjust
));
4697 /* Transform image IMG on frame F so that it looks disabled. */
4700 x_disable_image (struct frame
*f
, struct image
*img
)
4702 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4704 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4706 int n_planes
= dpyinfo
->n_planes
;
4707 #endif /* HAVE_NTGUI */
4711 /* Color (or grayscale). Convert to gray, and equalize. Just
4712 drawing such images with a stipple can look very odd, so
4713 we're using this method instead. */
4714 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4716 const int h
= 15000;
4717 const int l
= 30000;
4719 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4723 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4724 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4725 p
->red
= p
->green
= p
->blue
= i2
;
4728 x_from_xcolors (f
, img
, colors
);
4731 /* Draw a cross over the disabled image, if we must or if we
4733 if (n_planes
< 2 || cross_disabled_images
)
4736 Display
*dpy
= FRAME_X_DISPLAY (f
);
4739 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4741 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4743 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4744 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4745 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4746 img
->width
- 1, img
->height
- 1);
4747 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4753 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4754 XSetForeground (dpy
, gc
, MaskForeground (f
));
4755 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4756 img
->width
- 1, img
->height
- 1);
4757 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4761 #endif /* !HAVE_NS */
4766 hdc
= get_frame_dc (f
);
4767 bmpdc
= CreateCompatibleDC (hdc
);
4768 release_frame_dc (f
, hdc
);
4770 prev
= SelectObject (bmpdc
, img
->pixmap
);
4772 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4773 MoveToEx (bmpdc
, 0, 0, NULL
);
4774 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4775 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4776 LineTo (bmpdc
, img
->width
- 1, 0);
4780 SelectObject (bmpdc
, img
->mask
);
4781 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4782 MoveToEx (bmpdc
, 0, 0, NULL
);
4783 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4784 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4785 LineTo (bmpdc
, img
->width
- 1, 0);
4787 SelectObject (bmpdc
, prev
);
4789 #endif /* HAVE_NTGUI */
4794 /* Build a mask for image IMG which is used on frame F. FILE is the
4795 name of an image file, for error messages. HOW determines how to
4796 determine the background color of IMG. If it is a list '(R G B)',
4797 with R, G, and B being integers >= 0, take that as the color of the
4798 background. Otherwise, determine the background color of IMG
4799 heuristically. Value is non-zero if successful. */
4802 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4804 XImagePtr_or_DC ximg
;
4812 #endif /* HAVE_NTGUI */
4813 int x
, y
, rc
, use_img_background
;
4814 unsigned long bg
= 0;
4818 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4819 img
->mask
= NO_PIXMAP
;
4820 img
->background_transparent_valid
= 0;
4825 /* Create an image and pixmap serving as mask. */
4826 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4827 &mask_img
, &img
->mask
);
4830 #endif /* !HAVE_NS */
4832 /* Get the X image of IMG->pixmap. */
4833 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4834 img
->width
, img
->height
,
4837 /* Create the bit array serving as mask. */
4838 row_width
= (img
->width
+ 7) / 8;
4839 mask_img
= xmalloc (row_width
* img
->height
);
4840 memset (mask_img
, 0, row_width
* img
->height
);
4842 /* Create a memory device context for IMG->pixmap. */
4843 frame_dc
= get_frame_dc (f
);
4844 ximg
= CreateCompatibleDC (frame_dc
);
4845 release_frame_dc (f
, frame_dc
);
4846 prev
= SelectObject (ximg
, img
->pixmap
);
4847 #endif /* HAVE_NTGUI */
4849 /* Determine the background color of ximg. If HOW is `(R G B)'
4850 take that as color. Otherwise, use the image's background color. */
4851 use_img_background
= 1;
4857 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4859 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4863 if (i
== 3 && NILP (how
))
4865 char color_name
[30];
4866 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4869 0x00ffffff & /* Filter out palette info. */
4870 #endif /* HAVE_NTGUI */
4871 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4872 use_img_background
= 0;
4876 if (use_img_background
)
4877 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4879 /* Set all bits in mask_img to 1 whose color in ximg is different
4880 from the background color bg. */
4882 for (y
= 0; y
< img
->height
; ++y
)
4883 for (x
= 0; x
< img
->width
; ++x
)
4885 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4886 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4888 if (XGetPixel (ximg
, x
, y
) == bg
)
4889 ns_set_alpha (ximg
, x
, y
, 0);
4890 #endif /* HAVE_NS */
4892 /* Fill in the background_transparent field while we have the mask handy. */
4893 image_background_transparent (img
, f
, mask_img
);
4895 /* Put mask_img into img->mask. */
4896 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4897 x_destroy_x_image (mask_img
);
4898 #endif /* !HAVE_NS */
4900 for (y
= 0; y
< img
->height
; ++y
)
4901 for (x
= 0; x
< img
->width
; ++x
)
4903 COLORREF p
= GetPixel (ximg
, x
, y
);
4905 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4908 /* Create the mask image. */
4909 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4911 /* Fill in the background_transparent field while we have the mask handy. */
4912 SelectObject (ximg
, img
->mask
);
4913 image_background_transparent (img
, f
, ximg
);
4915 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4917 #endif /* HAVE_NTGUI */
4919 Destroy_Image (ximg
, prev
);
4925 /***********************************************************************
4926 PBM (mono, gray, color)
4927 ***********************************************************************/
4929 static int pbm_image_p (Lisp_Object object
);
4930 static int pbm_load (struct frame
*f
, struct image
*img
);
4931 static int pbm_scan_number (unsigned char **, unsigned char *);
4933 /* The symbol `pbm' identifying images of this type. */
4937 /* Indices of image specification fields in gs_format, below. */
4939 enum pbm_keyword_index
4955 /* Vector of image_keyword structures describing the format
4956 of valid user-defined image specifications. */
4958 static const struct image_keyword pbm_format
[PBM_LAST
] =
4960 {":type", IMAGE_SYMBOL_VALUE
, 1},
4961 {":file", IMAGE_STRING_VALUE
, 0},
4962 {":data", IMAGE_STRING_VALUE
, 0},
4963 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4964 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
4965 {":relief", IMAGE_INTEGER_VALUE
, 0},
4966 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4967 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4968 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4969 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4970 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4973 /* Structure describing the image type `pbm'. */
4975 static struct image_type pbm_type
=
4985 /* Return non-zero if OBJECT is a valid PBM image specification. */
4988 pbm_image_p (Lisp_Object object
)
4990 struct image_keyword fmt
[PBM_LAST
];
4992 memcpy (fmt
, pbm_format
, sizeof fmt
);
4994 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4997 /* Must specify either :data or :file. */
4998 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5002 /* Scan a decimal number from *S and return it. Advance *S while
5003 reading the number. END is the end of the string. Value is -1 at
5007 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5009 int c
= 0, val
= -1;
5013 /* Skip white-space. */
5014 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5019 /* Skip comment to end of line. */
5020 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5023 else if (isdigit (c
))
5025 /* Read decimal number. */
5027 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5028 val
= 10 * val
+ c
- '0';
5040 #if 0 /* Unused. ++kfs */
5042 /* Read FILE into memory. Value is a pointer to a buffer allocated
5043 with xmalloc holding FILE's contents. Value is null if an error
5044 occurred. *SIZE is set to the size of the file. */
5047 pbm_read_file (file
, size
)
5055 if (stat (SDATA (file
), &st
) == 0
5056 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5057 && (buf
= (char *) xmalloc (st
.st_size
),
5058 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5077 #endif /* HAVE_NTGUI */
5079 /* Load PBM image IMG for use on frame F. */
5082 pbm_load (struct frame
*f
, struct image
*img
)
5085 int width
, height
, max_color_idx
= 0;
5087 Lisp_Object file
, specified_file
;
5088 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5089 unsigned char *contents
= NULL
;
5090 unsigned char *end
, *p
;
5093 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5095 if (STRINGP (specified_file
))
5097 file
= x_find_image_file (specified_file
);
5098 if (!STRINGP (file
))
5100 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5104 contents
= slurp_file (SDATA (file
), &size
);
5105 if (contents
== NULL
)
5107 image_error ("Error reading `%s'", file
, Qnil
);
5112 end
= contents
+ size
;
5117 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5118 if (!STRINGP (data
))
5120 image_error ("Invalid image data `%s'", data
, Qnil
);
5124 end
= p
+ SBYTES (data
);
5127 /* Check magic number. */
5128 if (end
- p
< 2 || *p
++ != 'P')
5130 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5139 raw_p
= 0, type
= PBM_MONO
;
5143 raw_p
= 0, type
= PBM_GRAY
;
5147 raw_p
= 0, type
= PBM_COLOR
;
5151 raw_p
= 1, type
= PBM_MONO
;
5155 raw_p
= 1, type
= PBM_GRAY
;
5159 raw_p
= 1, type
= PBM_COLOR
;
5163 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5167 /* Read width, height, maximum color-component. Characters
5168 starting with `#' up to the end of a line are ignored. */
5169 width
= pbm_scan_number (&p
, end
);
5170 height
= pbm_scan_number (&p
, end
);
5172 if (type
!= PBM_MONO
)
5174 max_color_idx
= pbm_scan_number (&p
, end
);
5175 if (max_color_idx
> 65535 || max_color_idx
< 0)
5177 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5182 if (!check_image_size (f
, width
, height
))
5184 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5188 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5189 &ximg
, &img
->pixmap
))
5192 /* Initialize the color hash table. */
5193 init_color_table ();
5195 if (type
== PBM_MONO
)
5198 struct image_keyword fmt
[PBM_LAST
];
5199 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5200 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5202 /* Parse the image specification. */
5203 memcpy (fmt
, pbm_format
, sizeof fmt
);
5204 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5206 /* Get foreground and background colors, maybe allocate colors. */
5207 if (fmt
[PBM_FOREGROUND
].count
5208 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5209 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5210 if (fmt
[PBM_BACKGROUND
].count
5211 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5213 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5214 img
->background
= bg
;
5215 img
->background_valid
= 1;
5218 for (y
= 0; y
< height
; ++y
)
5219 for (x
= 0; x
< width
; ++x
)
5227 x_destroy_x_image (ximg
);
5228 x_clear_image (f
, img
);
5229 image_error ("Invalid image size in image `%s'",
5239 g
= pbm_scan_number (&p
, end
);
5241 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5246 int expected_size
= height
* width
;
5247 if (max_color_idx
> 255)
5249 if (type
== PBM_COLOR
)
5252 if (raw_p
&& p
+ expected_size
> end
)
5254 x_destroy_x_image (ximg
);
5255 x_clear_image (f
, img
);
5256 image_error ("Invalid image size in image `%s'",
5261 for (y
= 0; y
< height
; ++y
)
5262 for (x
= 0; x
< width
; ++x
)
5266 if (type
== PBM_GRAY
&& raw_p
)
5269 if (max_color_idx
> 255)
5270 r
= g
= b
= r
* 256 + *p
++;
5272 else if (type
== PBM_GRAY
)
5273 r
= g
= b
= pbm_scan_number (&p
, end
);
5277 if (max_color_idx
> 255)
5280 if (max_color_idx
> 255)
5283 if (max_color_idx
> 255)
5288 r
= pbm_scan_number (&p
, end
);
5289 g
= pbm_scan_number (&p
, end
);
5290 b
= pbm_scan_number (&p
, end
);
5293 if (r
< 0 || g
< 0 || b
< 0)
5295 x_destroy_x_image (ximg
);
5296 image_error ("Invalid pixel value in image `%s'",
5301 /* RGB values are now in the range 0..max_color_idx.
5302 Scale this to the range 0..0xffff supported by X. */
5303 r
= (double) r
* 65535 / max_color_idx
;
5304 g
= (double) g
* 65535 / max_color_idx
;
5305 b
= (double) b
* 65535 / max_color_idx
;
5306 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5310 #ifdef COLOR_TABLE_SUPPORT
5311 /* Store in IMG->colors the colors allocated for the image, and
5312 free the color table. */
5313 img
->colors
= colors_in_color_table (&img
->ncolors
);
5314 free_color_table ();
5315 #endif /* COLOR_TABLE_SUPPORT */
5318 img
->height
= height
;
5320 /* Maybe fill in the background field while we have ximg handy. */
5322 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5323 /* Casting avoids a GCC warning. */
5324 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5326 /* Put the image into a pixmap. */
5327 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5328 x_destroy_x_image (ximg
);
5330 /* X and W32 versions did it here, MAC version above. ++kfs
5332 img->height = height; */
5339 /***********************************************************************
5341 ***********************************************************************/
5343 #if defined (HAVE_PNG) || defined (HAVE_NS)
5345 /* Function prototypes. */
5347 static int png_image_p (Lisp_Object object
);
5348 static int png_load (struct frame
*f
, struct image
*img
);
5350 /* The symbol `png' identifying images of this type. */
5354 /* Indices of image specification fields in png_format, below. */
5356 enum png_keyword_index
5371 /* Vector of image_keyword structures describing the format
5372 of valid user-defined image specifications. */
5374 static const struct image_keyword png_format
[PNG_LAST
] =
5376 {":type", IMAGE_SYMBOL_VALUE
, 1},
5377 {":data", IMAGE_STRING_VALUE
, 0},
5378 {":file", IMAGE_STRING_VALUE
, 0},
5379 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5380 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5381 {":relief", IMAGE_INTEGER_VALUE
, 0},
5382 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5383 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5384 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5385 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5388 /* Structure describing the image type `png'. */
5390 static struct image_type png_type
=
5399 /* Return non-zero if OBJECT is a valid PNG image specification. */
5402 png_image_p (Lisp_Object object
)
5404 struct image_keyword fmt
[PNG_LAST
];
5405 memcpy (fmt
, png_format
, sizeof fmt
);
5407 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5410 /* Must specify either the :data or :file keyword. */
5411 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5414 #endif /* HAVE_PNG || HAVE_NS */
5420 /* PNG library details. */
5422 DEF_IMGLIB_FN (png_get_io_ptr
, (png_structp
));
5423 DEF_IMGLIB_FN (png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5424 DEF_IMGLIB_FN (png_create_read_struct
, (png_const_charp
, png_voidp
,
5425 png_error_ptr
, png_error_ptr
));
5426 DEF_IMGLIB_FN (png_create_info_struct
, (png_structp
));
5427 DEF_IMGLIB_FN (png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5428 DEF_IMGLIB_FN (png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5429 DEF_IMGLIB_FN (png_set_sig_bytes
, (png_structp
, int));
5430 DEF_IMGLIB_FN (png_read_info
, (png_structp
, png_infop
));
5431 DEF_IMGLIB_FN (png_get_IHDR
, (png_structp
, png_infop
,
5432 png_uint_32
*, png_uint_32
*,
5433 int *, int *, int *, int *, int *));
5434 DEF_IMGLIB_FN (png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5435 DEF_IMGLIB_FN (png_set_strip_16
, (png_structp
));
5436 DEF_IMGLIB_FN (png_set_expand
, (png_structp
));
5437 DEF_IMGLIB_FN (png_set_gray_to_rgb
, (png_structp
));
5438 DEF_IMGLIB_FN (png_set_background
, (png_structp
, png_color_16p
,
5440 DEF_IMGLIB_FN (png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5441 DEF_IMGLIB_FN (png_read_update_info
, (png_structp
, png_infop
));
5442 DEF_IMGLIB_FN (png_get_channels
, (png_structp
, png_infop
));
5443 DEF_IMGLIB_FN (png_get_rowbytes
, (png_structp
, png_infop
));
5444 DEF_IMGLIB_FN (png_read_image
, (png_structp
, png_bytepp
));
5445 DEF_IMGLIB_FN (png_read_end
, (png_structp
, png_infop
));
5446 DEF_IMGLIB_FN (png_error
, (png_structp
, png_const_charp
));
5449 init_png_functions (Lisp_Object libraries
)
5453 /* Try loading libpng under probable names. */
5454 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5457 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5458 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5459 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5460 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5461 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5462 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5463 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5464 LOAD_IMGLIB_FN (library
, png_read_info
);
5465 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5466 LOAD_IMGLIB_FN (library
, png_get_valid
);
5467 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5468 LOAD_IMGLIB_FN (library
, png_set_expand
);
5469 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5470 LOAD_IMGLIB_FN (library
, png_set_background
);
5471 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5472 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5473 LOAD_IMGLIB_FN (library
, png_get_channels
);
5474 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5475 LOAD_IMGLIB_FN (library
, png_read_image
);
5476 LOAD_IMGLIB_FN (library
, png_read_end
);
5477 LOAD_IMGLIB_FN (library
, png_error
);
5482 #define fn_png_get_io_ptr png_get_io_ptr
5483 #define fn_png_sig_cmp png_sig_cmp
5484 #define fn_png_create_read_struct png_create_read_struct
5485 #define fn_png_create_info_struct png_create_info_struct
5486 #define fn_png_destroy_read_struct png_destroy_read_struct
5487 #define fn_png_set_read_fn png_set_read_fn
5488 #define fn_png_set_sig_bytes png_set_sig_bytes
5489 #define fn_png_read_info png_read_info
5490 #define fn_png_get_IHDR png_get_IHDR
5491 #define fn_png_get_valid png_get_valid
5492 #define fn_png_set_strip_16 png_set_strip_16
5493 #define fn_png_set_expand png_set_expand
5494 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5495 #define fn_png_set_background png_set_background
5496 #define fn_png_get_bKGD png_get_bKGD
5497 #define fn_png_read_update_info png_read_update_info
5498 #define fn_png_get_channels png_get_channels
5499 #define fn_png_get_rowbytes png_get_rowbytes
5500 #define fn_png_read_image png_read_image
5501 #define fn_png_read_end png_read_end
5502 #define fn_png_error png_error
5504 #endif /* HAVE_NTGUI */
5506 /* Error and warning handlers installed when the PNG library
5510 my_png_error (png_struct
*png_ptr
, const char *msg
)
5512 xassert (png_ptr
!= NULL
);
5513 /* Avoid compiler warning about deprecated direct access to
5514 png_ptr's fields in libpng versions 1.4.x. */
5515 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5516 longjmp (png_ptr
->jmpbuf
, 1);
5521 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5523 xassert (png_ptr
!= NULL
);
5524 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5527 /* Memory source for PNG decoding. */
5529 struct png_memory_storage
5531 unsigned char *bytes
; /* The data */
5532 size_t len
; /* How big is it? */
5533 int index
; /* Where are we? */
5537 /* Function set as reader function when reading PNG image from memory.
5538 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5539 bytes from the input to DATA. */
5542 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5544 struct png_memory_storage
*tbr
5545 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5547 if (length
> tbr
->len
- tbr
->index
)
5548 fn_png_error (png_ptr
, "Read error");
5550 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5551 tbr
->index
= tbr
->index
+ length
;
5555 /* Function set as reader function when reading PNG image from a file.
5556 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5557 bytes from the input to DATA. */
5560 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5562 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5564 if (fread (data
, 1, length
, fp
) < length
)
5565 fn_png_error (png_ptr
, "Read error");
5569 /* Load PNG image IMG for use on frame F. Value is non-zero if
5573 png_load (struct frame
*f
, struct image
*img
)
5575 Lisp_Object file
, specified_file
;
5576 Lisp_Object specified_data
;
5578 XImagePtr ximg
, mask_img
= NULL
;
5579 png_struct
*png_ptr
= NULL
;
5580 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5581 FILE *volatile fp
= NULL
;
5583 png_byte
* volatile pixels
= NULL
;
5584 png_byte
** volatile rows
= NULL
;
5585 png_uint_32 width
, height
;
5586 int bit_depth
, color_type
, interlace_type
;
5588 png_uint_32 row_bytes
;
5590 struct png_memory_storage tbr
; /* Data to be read */
5592 /* Find out what file to load. */
5593 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5594 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5596 if (NILP (specified_data
))
5598 file
= x_find_image_file (specified_file
);
5599 if (!STRINGP (file
))
5601 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5605 /* Open the image file. */
5606 fp
= fopen (SDATA (file
), "rb");
5609 image_error ("Cannot open image file `%s'", file
, Qnil
);
5613 /* Check PNG signature. */
5614 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5615 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5617 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5624 if (!STRINGP (specified_data
))
5626 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5630 /* Read from memory. */
5631 tbr
.bytes
= SDATA (specified_data
);
5632 tbr
.len
= SBYTES (specified_data
);
5635 /* Check PNG signature. */
5636 if (tbr
.len
< sizeof sig
5637 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5639 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5643 /* Need to skip past the signature. */
5644 tbr
.bytes
+= sizeof (sig
);
5647 /* Initialize read and info structs for PNG lib. Casting return
5648 value avoids a GCC warning on W32. */
5649 png_ptr
= (png_structp
)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5654 if (fp
) fclose (fp
);
5658 /* Casting return value avoids a GCC warning on W32. */
5659 info_ptr
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5662 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5663 if (fp
) fclose (fp
);
5667 /* Casting return value avoids a GCC warning on W32. */
5668 end_info
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5671 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5672 if (fp
) fclose (fp
);
5676 /* Set error jump-back. We come back here when the PNG library
5677 detects an error. */
5678 if (setjmp (png_ptr
->jmpbuf
))
5682 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5685 if (fp
) fclose (fp
);
5689 /* Read image info. */
5690 if (!NILP (specified_data
))
5691 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5693 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5695 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5696 fn_png_read_info (png_ptr
, info_ptr
);
5697 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5698 &interlace_type
, NULL
, NULL
);
5700 if (!check_image_size (f
, width
, height
))
5702 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5705 /* If image contains simply transparency data, we prefer to
5706 construct a clipping mask. */
5707 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5712 /* This function is easier to write if we only have to handle
5713 one data format: RGB or RGBA with 8 bits per channel. Let's
5714 transform other formats into that format. */
5716 /* Strip more than 8 bits per channel. */
5717 if (bit_depth
== 16)
5718 fn_png_set_strip_16 (png_ptr
);
5720 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5722 fn_png_set_expand (png_ptr
);
5724 /* Convert grayscale images to RGB. */
5725 if (color_type
== PNG_COLOR_TYPE_GRAY
5726 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5727 fn_png_set_gray_to_rgb (png_ptr
);
5729 /* Handle alpha channel by combining the image with a background
5730 color. Do this only if a real alpha channel is supplied. For
5731 simple transparency, we prefer a clipping mask. */
5734 /* png_color_16 *image_bg; */
5735 Lisp_Object specified_bg
5736 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5737 int shift
= (bit_depth
== 16) ? 0 : 8;
5739 if (STRINGP (specified_bg
))
5740 /* The user specified `:background', use that. */
5743 if (x_defined_color (f
, SDATA (specified_bg
), &color
, 0))
5745 png_color_16 user_bg
;
5747 memset (&user_bg
, 0, sizeof user_bg
);
5748 user_bg
.red
= color
.red
>> shift
;
5749 user_bg
.green
= color
.green
>> shift
;
5750 user_bg
.blue
= color
.blue
>> shift
;
5752 fn_png_set_background (png_ptr
, &user_bg
,
5753 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5758 /* We use the current frame background, ignoring any default
5759 background color set by the image. */
5760 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5762 png_color_16 frame_background
;
5764 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5765 x_query_color (f
, &color
);
5767 memset (&frame_background
, 0, sizeof frame_background
);
5768 frame_background
.red
= color
.red
>> shift
;
5769 frame_background
.green
= color
.green
>> shift
;
5770 frame_background
.blue
= color
.blue
>> shift
;
5771 #endif /* HAVE_X_WINDOWS */
5773 fn_png_set_background (png_ptr
, &frame_background
,
5774 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5778 /* Update info structure. */
5779 fn_png_read_update_info (png_ptr
, info_ptr
);
5781 /* Get number of channels. Valid values are 1 for grayscale images
5782 and images with a palette, 2 for grayscale images with transparency
5783 information (alpha channel), 3 for RGB images, and 4 for RGB
5784 images with alpha channel, i.e. RGBA. If conversions above were
5785 sufficient we should only have 3 or 4 channels here. */
5786 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5787 xassert (channels
== 3 || channels
== 4);
5789 /* Number of bytes needed for one row of the image. */
5790 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5792 /* Allocate memory for the image. */
5793 pixels
= (png_byte
*) xmalloc (row_bytes
* height
* sizeof *pixels
);
5794 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5795 for (i
= 0; i
< height
; ++i
)
5796 rows
[i
] = pixels
+ i
* row_bytes
;
5798 /* Read the entire image. */
5799 fn_png_read_image (png_ptr
, rows
);
5800 fn_png_read_end (png_ptr
, info_ptr
);
5807 /* Create the X image and pixmap. */
5808 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5812 /* Create an image and pixmap serving as mask if the PNG image
5813 contains an alpha channel. */
5816 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5817 &mask_img
, &img
->mask
))
5819 x_destroy_x_image (ximg
);
5820 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5821 img
->pixmap
= NO_PIXMAP
;
5825 /* Fill the X image and mask from PNG data. */
5826 init_color_table ();
5828 for (y
= 0; y
< height
; ++y
)
5830 png_byte
*p
= rows
[y
];
5832 for (x
= 0; x
< width
; ++x
)
5839 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5840 /* An alpha channel, aka mask channel, associates variable
5841 transparency with an image. Where other image formats
5842 support binary transparency---fully transparent or fully
5843 opaque---PNG allows up to 254 levels of partial transparency.
5844 The PNG library implements partial transparency by combining
5845 the image with a specified background color.
5847 I'm not sure how to handle this here nicely: because the
5848 background on which the image is displayed may change, for
5849 real alpha channel support, it would be necessary to create
5850 a new image for each possible background.
5852 What I'm doing now is that a mask is created if we have
5853 boolean transparency information. Otherwise I'm using
5854 the frame's background color to combine the image with. */
5859 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5865 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5866 /* Set IMG's background color from the PNG image, unless the user
5870 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5872 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5873 img
->background_valid
= 1;
5877 #ifdef COLOR_TABLE_SUPPORT
5878 /* Remember colors allocated for this image. */
5879 img
->colors
= colors_in_color_table (&img
->ncolors
);
5880 free_color_table ();
5881 #endif /* COLOR_TABLE_SUPPORT */
5884 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5889 img
->height
= height
;
5891 /* Maybe fill in the background field while we have ximg handy.
5892 Casting avoids a GCC warning. */
5893 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5895 /* Put the image into the pixmap, then free the X image and its buffer. */
5896 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5897 x_destroy_x_image (ximg
);
5899 /* Same for the mask. */
5902 /* Fill in the background_transparent field while we have the
5903 mask handy. Casting avoids a GCC warning. */
5904 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5906 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5907 x_destroy_x_image (mask_img
);
5913 #else /* HAVE_PNG */
5917 png_load (struct frame
*f
, struct image
*img
)
5919 return ns_load_image(f
, img
,
5920 image_spec_value (img
->spec
, QCfile
, NULL
),
5921 image_spec_value (img
->spec
, QCdata
, NULL
));
5923 #endif /* HAVE_NS */
5926 #endif /* !HAVE_PNG */
5930 /***********************************************************************
5932 ***********************************************************************/
5934 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5936 static int jpeg_image_p (Lisp_Object object
);
5937 static int jpeg_load (struct frame
*f
, struct image
*img
);
5939 /* The symbol `jpeg' identifying images of this type. */
5943 /* Indices of image specification fields in gs_format, below. */
5945 enum jpeg_keyword_index
5954 JPEG_HEURISTIC_MASK
,
5960 /* Vector of image_keyword structures describing the format
5961 of valid user-defined image specifications. */
5963 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5965 {":type", IMAGE_SYMBOL_VALUE
, 1},
5966 {":data", IMAGE_STRING_VALUE
, 0},
5967 {":file", IMAGE_STRING_VALUE
, 0},
5968 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5969 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5970 {":relief", IMAGE_INTEGER_VALUE
, 0},
5971 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5972 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5973 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5974 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5977 /* Structure describing the image type `jpeg'. */
5979 static struct image_type jpeg_type
=
5988 /* Return non-zero if OBJECT is a valid JPEG image specification. */
5991 jpeg_image_p (Lisp_Object object
)
5993 struct image_keyword fmt
[JPEG_LAST
];
5995 memcpy (fmt
, jpeg_format
, sizeof fmt
);
5997 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6000 /* Must specify either the :data or :file keyword. */
6001 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6004 #endif /* HAVE_JPEG || HAVE_NS */
6008 /* Work around a warning about HAVE_STDLIB_H being redefined in
6010 #ifdef HAVE_STDLIB_H
6011 #define HAVE_STDLIB_H_1
6012 #undef HAVE_STDLIB_H
6013 #endif /* HAVE_STLIB_H */
6015 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6016 /* In older releases of the jpeg library, jpeglib.h will define boolean
6017 differently depending on __WIN32__, so make sure it is defined. */
6021 #include <jpeglib.h>
6024 #ifdef HAVE_STLIB_H_1
6025 #define HAVE_STDLIB_H 1
6030 /* JPEG library details. */
6031 DEF_IMGLIB_FN (jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6032 DEF_IMGLIB_FN (jpeg_start_decompress
, (j_decompress_ptr
));
6033 DEF_IMGLIB_FN (jpeg_finish_decompress
, (j_decompress_ptr
));
6034 DEF_IMGLIB_FN (jpeg_destroy_decompress
, (j_decompress_ptr
));
6035 DEF_IMGLIB_FN (jpeg_read_header
, (j_decompress_ptr
, boolean
));
6036 DEF_IMGLIB_FN (jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6037 DEF_IMGLIB_FN (jpeg_std_error
, (struct jpeg_error_mgr
*));
6038 DEF_IMGLIB_FN (jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6041 init_jpeg_functions (Lisp_Object libraries
)
6045 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6048 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6049 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6050 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6051 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6052 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6053 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6054 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6055 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6059 /* Wrapper since we can't directly assign the function pointer
6060 to another function pointer that was declared more completely easily. */
6062 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6064 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6069 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6070 #define fn_jpeg_start_decompress jpeg_start_decompress
6071 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6072 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6073 #define fn_jpeg_read_header jpeg_read_header
6074 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6075 #define fn_jpeg_std_error jpeg_std_error
6076 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6078 #endif /* HAVE_NTGUI */
6080 struct my_jpeg_error_mgr
6082 struct jpeg_error_mgr pub
;
6083 jmp_buf setjmp_buffer
;
6088 my_error_exit (j_common_ptr cinfo
)
6090 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6091 longjmp (mgr
->setjmp_buffer
, 1);
6095 /* Init source method for JPEG data source manager. Called by
6096 jpeg_read_header() before any data is actually read. See
6097 libjpeg.doc from the JPEG lib distribution. */
6100 our_common_init_source (j_decompress_ptr cinfo
)
6105 /* Method to terminate data source. Called by
6106 jpeg_finish_decompress() after all data has been processed. */
6109 our_common_term_source (j_decompress_ptr cinfo
)
6114 /* Fill input buffer method for JPEG data source manager. Called
6115 whenever more data is needed. We read the whole image in one step,
6116 so this only adds a fake end of input marker at the end. */
6118 static JOCTET our_memory_buffer
[2];
6121 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6123 /* Insert a fake EOI marker. */
6124 struct jpeg_source_mgr
*src
= cinfo
->src
;
6126 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6127 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6129 src
->next_input_byte
= our_memory_buffer
;
6130 src
->bytes_in_buffer
= 2;
6135 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6136 is the JPEG data source manager. */
6139 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6141 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6145 if (num_bytes
> src
->bytes_in_buffer
)
6146 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6148 src
->bytes_in_buffer
-= num_bytes
;
6149 src
->next_input_byte
+= num_bytes
;
6154 /* Set up the JPEG lib for reading an image from DATA which contains
6155 LEN bytes. CINFO is the decompression info structure created for
6156 reading the image. */
6159 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6161 struct jpeg_source_mgr
*src
;
6163 if (cinfo
->src
== NULL
)
6165 /* First time for this JPEG object? */
6166 cinfo
->src
= (struct jpeg_source_mgr
*)
6167 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6168 sizeof (struct jpeg_source_mgr
));
6169 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6170 src
->next_input_byte
= data
;
6173 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6174 src
->init_source
= our_common_init_source
;
6175 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6176 src
->skip_input_data
= our_memory_skip_input_data
;
6177 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6178 src
->term_source
= our_common_term_source
;
6179 src
->bytes_in_buffer
= len
;
6180 src
->next_input_byte
= data
;
6184 struct jpeg_stdio_mgr
6186 struct jpeg_source_mgr mgr
;
6193 /* Size of buffer to read JPEG from file.
6194 Not too big, as we want to use alloc_small. */
6195 #define JPEG_STDIO_BUFFER_SIZE 8192
6198 /* Fill input buffer method for JPEG data source manager. Called
6199 whenever more data is needed. The data is read from a FILE *. */
6202 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6204 struct jpeg_stdio_mgr
*src
;
6206 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6211 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6213 src
->mgr
.bytes_in_buffer
= bytes
;
6216 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6218 src
->buffer
[0] = (JOCTET
) 0xFF;
6219 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6220 src
->mgr
.bytes_in_buffer
= 2;
6222 src
->mgr
.next_input_byte
= src
->buffer
;
6229 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6230 is the JPEG data source manager. */
6233 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6235 struct jpeg_stdio_mgr
*src
;
6236 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6238 while (num_bytes
> 0 && !src
->finished
)
6240 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6242 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6243 src
->mgr
.next_input_byte
+= num_bytes
;
6248 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6249 src
->mgr
.bytes_in_buffer
= 0;
6250 src
->mgr
.next_input_byte
= NULL
;
6252 our_stdio_fill_input_buffer (cinfo
);
6258 /* Set up the JPEG lib for reading an image from a FILE *.
6259 CINFO is the decompression info structure created for
6260 reading the image. */
6263 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6265 struct jpeg_stdio_mgr
*src
;
6267 if (cinfo
->src
!= NULL
)
6268 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6271 /* First time for this JPEG object? */
6272 cinfo
->src
= (struct jpeg_source_mgr
*)
6273 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6274 sizeof (struct jpeg_stdio_mgr
));
6275 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6276 src
->buffer
= (JOCTET
*)
6277 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6278 JPEG_STDIO_BUFFER_SIZE
);
6283 src
->mgr
.init_source
= our_common_init_source
;
6284 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6285 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6286 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6287 src
->mgr
.term_source
= our_common_term_source
;
6288 src
->mgr
.bytes_in_buffer
= 0;
6289 src
->mgr
.next_input_byte
= NULL
;
6293 /* Load image IMG for use on frame F. Patterned after example.c
6294 from the JPEG lib. */
6297 jpeg_load (struct frame
*f
, struct image
*img
)
6299 struct jpeg_decompress_struct cinfo
;
6300 struct my_jpeg_error_mgr mgr
;
6301 Lisp_Object file
, specified_file
;
6302 Lisp_Object specified_data
;
6303 FILE * volatile fp
= NULL
;
6305 int row_stride
, x
, y
;
6306 XImagePtr ximg
= NULL
;
6308 unsigned long *colors
;
6311 /* Open the JPEG file. */
6312 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6313 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6315 if (NILP (specified_data
))
6317 file
= x_find_image_file (specified_file
);
6318 if (!STRINGP (file
))
6320 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6324 fp
= fopen (SDATA (file
), "rb");
6327 image_error ("Cannot open `%s'", file
, Qnil
);
6331 else if (!STRINGP (specified_data
))
6333 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6337 /* Customize libjpeg's error handling to call my_error_exit when an
6338 error is detected. This function will perform a longjmp.
6339 Casting return value avoids a GCC warning on W32. */
6340 cinfo
.err
= (struct jpeg_error_mgr
*)fn_jpeg_std_error (&mgr
.pub
);
6341 mgr
.pub
.error_exit
= my_error_exit
;
6343 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6347 /* Called from my_error_exit. Display a JPEG error. */
6348 char buffer
[JMSG_LENGTH_MAX
];
6349 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buffer
);
6350 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6351 build_string (buffer
));
6354 /* Close the input file and destroy the JPEG object. */
6356 fclose ((FILE *) fp
);
6357 fn_jpeg_destroy_decompress (&cinfo
);
6359 /* If we already have an XImage, free that. */
6360 x_destroy_x_image (ximg
);
6362 /* Free pixmap and colors. */
6363 x_clear_image (f
, img
);
6367 /* Create the JPEG decompression object. Let it read from fp.
6368 Read the JPEG image header. */
6369 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6371 if (NILP (specified_data
))
6372 jpeg_file_src (&cinfo
, (FILE *) fp
);
6374 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6375 SBYTES (specified_data
));
6377 fn_jpeg_read_header (&cinfo
, 1);
6379 /* Customize decompression so that color quantization will be used.
6380 Start decompression. */
6381 cinfo
.quantize_colors
= 1;
6382 fn_jpeg_start_decompress (&cinfo
);
6383 width
= img
->width
= cinfo
.output_width
;
6384 height
= img
->height
= cinfo
.output_height
;
6386 if (!check_image_size (f
, width
, height
))
6388 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6389 longjmp (mgr
.setjmp_buffer
, 2);
6392 /* Create X image and pixmap. */
6393 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6394 longjmp (mgr
.setjmp_buffer
, 2);
6396 /* Allocate colors. When color quantization is used,
6397 cinfo.actual_number_of_colors has been set with the number of
6398 colors generated, and cinfo.colormap is a two-dimensional array
6399 of color indices in the range 0..cinfo.actual_number_of_colors.
6400 No more than 255 colors will be generated. */
6404 if (cinfo
.out_color_components
> 2)
6405 ir
= 0, ig
= 1, ib
= 2;
6406 else if (cinfo
.out_color_components
> 1)
6407 ir
= 0, ig
= 1, ib
= 0;
6409 ir
= 0, ig
= 0, ib
= 0;
6411 /* Use the color table mechanism because it handles colors that
6412 cannot be allocated nicely. Such colors will be replaced with
6413 a default color, and we don't have to care about which colors
6414 can be freed safely, and which can't. */
6415 init_color_table ();
6416 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6419 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6421 /* Multiply RGB values with 255 because X expects RGB values
6422 in the range 0..0xffff. */
6423 int r
= cinfo
.colormap
[ir
][i
] << 8;
6424 int g
= cinfo
.colormap
[ig
][i
] << 8;
6425 int b
= cinfo
.colormap
[ib
][i
] << 8;
6426 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6429 #ifdef COLOR_TABLE_SUPPORT
6430 /* Remember those colors actually allocated. */
6431 img
->colors
= colors_in_color_table (&img
->ncolors
);
6432 free_color_table ();
6433 #endif /* COLOR_TABLE_SUPPORT */
6437 row_stride
= width
* cinfo
.output_components
;
6438 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6440 for (y
= 0; y
< height
; ++y
)
6442 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6443 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6444 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6448 fn_jpeg_finish_decompress (&cinfo
);
6449 fn_jpeg_destroy_decompress (&cinfo
);
6451 fclose ((FILE *) fp
);
6453 /* Maybe fill in the background field while we have ximg handy. */
6454 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6455 /* Casting avoids a GCC warning. */
6456 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6458 /* Put the image into the pixmap. */
6459 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6460 x_destroy_x_image (ximg
);
6464 #else /* HAVE_JPEG */
6468 jpeg_load (struct frame
*f
, struct image
*img
)
6470 return ns_load_image (f
, img
,
6471 image_spec_value (img
->spec
, QCfile
, NULL
),
6472 image_spec_value (img
->spec
, QCdata
, NULL
));
6474 #endif /* HAVE_NS */
6476 #endif /* !HAVE_JPEG */
6480 /***********************************************************************
6482 ***********************************************************************/
6484 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6486 static int tiff_image_p (Lisp_Object object
);
6487 static int tiff_load (struct frame
*f
, struct image
*img
);
6489 /* The symbol `tiff' identifying images of this type. */
6493 /* Indices of image specification fields in tiff_format, below. */
6495 enum tiff_keyword_index
6504 TIFF_HEURISTIC_MASK
,
6511 /* Vector of image_keyword structures describing the format
6512 of valid user-defined image specifications. */
6514 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6516 {":type", IMAGE_SYMBOL_VALUE
, 1},
6517 {":data", IMAGE_STRING_VALUE
, 0},
6518 {":file", IMAGE_STRING_VALUE
, 0},
6519 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6520 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6521 {":relief", IMAGE_INTEGER_VALUE
, 0},
6522 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6523 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6524 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6525 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6526 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6529 /* Structure describing the image type `tiff'. */
6531 static struct image_type tiff_type
=
6540 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6543 tiff_image_p (Lisp_Object object
)
6545 struct image_keyword fmt
[TIFF_LAST
];
6546 memcpy (fmt
, tiff_format
, sizeof fmt
);
6548 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6551 /* Must specify either the :data or :file keyword. */
6552 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6555 #endif /* HAVE_TIFF || HAVE_NS */
6563 /* TIFF library details. */
6564 DEF_IMGLIB_FN (TIFFSetErrorHandler
, (TIFFErrorHandler
));
6565 DEF_IMGLIB_FN (TIFFSetWarningHandler
, (TIFFErrorHandler
));
6566 DEF_IMGLIB_FN (TIFFOpen
, (const char *, const char *));
6567 DEF_IMGLIB_FN (TIFFClientOpen
, (const char *, const char *, thandle_t
,
6568 TIFFReadWriteProc
, TIFFReadWriteProc
,
6569 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6570 TIFFMapFileProc
, TIFFUnmapFileProc
));
6571 DEF_IMGLIB_FN (TIFFGetField
, (TIFF
*, ttag_t
, ...));
6572 DEF_IMGLIB_FN (TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6573 DEF_IMGLIB_FN (TIFFClose
, (TIFF
*));
6574 DEF_IMGLIB_FN (TIFFSetDirectory
, (TIFF
*, tdir_t
));
6577 init_tiff_functions (Lisp_Object libraries
)
6581 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6584 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6585 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6586 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6587 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6588 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6589 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6590 LOAD_IMGLIB_FN (library
, TIFFClose
);
6591 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6597 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6598 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6599 #define fn_TIFFOpen TIFFOpen
6600 #define fn_TIFFClientOpen TIFFClientOpen
6601 #define fn_TIFFGetField TIFFGetField
6602 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6603 #define fn_TIFFClose TIFFClose
6604 #define fn_TIFFSetDirectory TIFFSetDirectory
6605 #endif /* HAVE_NTGUI */
6608 /* Reading from a memory buffer for TIFF images Based on the PNG
6609 memory source, but we have to provide a lot of extra functions.
6612 We really only need to implement read and seek, but I am not
6613 convinced that the TIFF library is smart enough not to destroy
6614 itself if we only hand it the function pointers we need to
6619 unsigned char *bytes
;
6626 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6628 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6630 if (size
> src
->len
- src
->index
)
6632 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6638 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6644 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6646 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6651 case SEEK_SET
: /* Go from beginning of source. */
6655 case SEEK_END
: /* Go from end of source. */
6656 idx
= src
->len
+ off
;
6659 case SEEK_CUR
: /* Go from current position. */
6660 idx
= src
->index
+ off
;
6663 default: /* Invalid `whence'. */
6667 if (idx
> src
->len
|| idx
< 0)
6675 tiff_close_memory (thandle_t data
)
6682 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6684 /* It is already _IN_ memory. */
6689 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6691 /* We don't need to do this. */
6695 tiff_size_of_memory (thandle_t data
)
6697 return ((tiff_memory_source
*) data
)->len
;
6702 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6707 len
= sprintf (buf
, "TIFF error: %s ", title
);
6708 vsprintf (buf
+ len
, format
, ap
);
6709 add_to_log (buf
, Qnil
, Qnil
);
6714 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6719 len
= sprintf (buf
, "TIFF warning: %s ", title
);
6720 vsprintf (buf
+ len
, format
, ap
);
6721 add_to_log (buf
, Qnil
, Qnil
);
6725 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6729 tiff_load (struct frame
*f
, struct image
*img
)
6731 Lisp_Object file
, specified_file
;
6732 Lisp_Object specified_data
;
6734 int width
, height
, x
, y
, count
;
6738 tiff_memory_source memsrc
;
6741 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6742 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6744 fn_TIFFSetErrorHandler (tiff_error_handler
);
6745 fn_TIFFSetWarningHandler (tiff_warning_handler
);
6747 if (NILP (specified_data
))
6749 /* Read from a file */
6750 file
= x_find_image_file (specified_file
);
6751 if (!STRINGP (file
))
6753 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6757 /* Try to open the image file. Casting return value avoids a
6758 GCC warning on W32. */
6759 tiff
= (TIFF
*)fn_TIFFOpen (SDATA (file
), "r");
6762 image_error ("Cannot open `%s'", file
, Qnil
);
6768 if (!STRINGP (specified_data
))
6770 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6774 /* Memory source! */
6775 memsrc
.bytes
= SDATA (specified_data
);
6776 memsrc
.len
= SBYTES (specified_data
);
6779 /* Casting arguments return value avoids a GCC warning on W32. */
6780 tiff
= (TIFF
*)fn_TIFFClientOpen ("memory_source", "r",
6781 (thandle_t
) &memsrc
,
6782 (TIFFReadWriteProc
) tiff_read_from_memory
,
6783 (TIFFReadWriteProc
) tiff_write_from_memory
,
6784 tiff_seek_in_memory
,
6786 tiff_size_of_memory
,
6792 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6797 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6798 if (INTEGERP (image
))
6800 int ino
= XFASTINT (image
);
6801 if (!fn_TIFFSetDirectory (tiff
, ino
))
6803 image_error ("Invalid image number `%s' in image `%s'",
6805 fn_TIFFClose (tiff
);
6810 /* Get width and height of the image, and allocate a raster buffer
6811 of width x height 32-bit values. */
6812 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6813 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6815 if (!check_image_size (f
, width
, height
))
6817 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6818 fn_TIFFClose (tiff
);
6822 buf
= (uint32
*) xmalloc (width
* height
* sizeof *buf
);
6824 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6826 /* Count the number of images in the file. */
6827 for (count
= 1, rc2
= 1; rc2
; count
++)
6828 rc2
= fn_TIFFSetDirectory (tiff
, count
);
6831 img
->data
.lisp_val
= Fcons (Qcount
,
6832 Fcons (make_number (count
),
6833 img
->data
.lisp_val
));
6835 fn_TIFFClose (tiff
);
6838 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6843 /* Create the X image and pixmap. */
6844 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6850 /* Initialize the color table. */
6851 init_color_table ();
6853 /* Process the pixel raster. Origin is in the lower-left corner. */
6854 for (y
= 0; y
< height
; ++y
)
6856 uint32
*row
= buf
+ y
* width
;
6858 for (x
= 0; x
< width
; ++x
)
6860 uint32 abgr
= row
[x
];
6861 int r
= TIFFGetR (abgr
) << 8;
6862 int g
= TIFFGetG (abgr
) << 8;
6863 int b
= TIFFGetB (abgr
) << 8;
6864 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6868 #ifdef COLOR_TABLE_SUPPORT
6869 /* Remember the colors allocated for the image. Free the color table. */
6870 img
->colors
= colors_in_color_table (&img
->ncolors
);
6871 free_color_table ();
6872 #endif /* COLOR_TABLE_SUPPORT */
6875 img
->height
= height
;
6877 /* Maybe fill in the background field while we have ximg handy. */
6878 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6879 /* Casting avoids a GCC warning on W32. */
6880 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6882 /* Put the image into the pixmap, then free the X image and its buffer. */
6883 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6884 x_destroy_x_image (ximg
);
6890 #else /* HAVE_TIFF */
6894 tiff_load (struct frame
*f
, struct image
*img
)
6896 return ns_load_image (f
, img
,
6897 image_spec_value (img
->spec
, QCfile
, NULL
),
6898 image_spec_value (img
->spec
, QCdata
, NULL
));
6900 #endif /* HAVE_NS */
6902 #endif /* !HAVE_TIFF */
6906 /***********************************************************************
6908 ***********************************************************************/
6910 #if defined (HAVE_GIF) || defined (HAVE_NS)
6912 static int gif_image_p (Lisp_Object object
);
6913 static int gif_load (struct frame
*f
, struct image
*img
);
6914 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6916 /* The symbol `gif' identifying images of this type. */
6920 /* Indices of image specification fields in gif_format, below. */
6922 enum gif_keyword_index
6938 /* Vector of image_keyword structures describing the format
6939 of valid user-defined image specifications. */
6941 static const struct image_keyword gif_format
[GIF_LAST
] =
6943 {":type", IMAGE_SYMBOL_VALUE
, 1},
6944 {":data", IMAGE_STRING_VALUE
, 0},
6945 {":file", IMAGE_STRING_VALUE
, 0},
6946 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6947 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6948 {":relief", IMAGE_INTEGER_VALUE
, 0},
6949 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6950 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6951 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6952 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
6953 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6956 /* Structure describing the image type `gif'. */
6958 static struct image_type gif_type
=
6967 /* Free X resources of GIF image IMG which is used on frame F. */
6970 gif_clear_image (struct frame
*f
, struct image
*img
)
6972 /* IMG->data.ptr_val may contain metadata with extension data. */
6973 img
->data
.lisp_val
= Qnil
;
6974 x_clear_image (f
, img
);
6977 /* Return non-zero if OBJECT is a valid GIF image specification. */
6980 gif_image_p (Lisp_Object object
)
6982 struct image_keyword fmt
[GIF_LAST
];
6983 memcpy (fmt
, gif_format
, sizeof fmt
);
6985 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
6988 /* Must specify either the :data or :file keyword. */
6989 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
6992 #endif /* HAVE_GIF */
6996 #if defined (HAVE_NTGUI)
6997 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
6998 Undefine before redefining to avoid a preprocessor warning. */
7002 /* avoid conflict with QuickdrawText.h */
7003 #define DrawText gif_DrawText
7004 #include <gif_lib.h>
7007 #else /* HAVE_NTGUI */
7009 #include <gif_lib.h>
7011 #endif /* HAVE_NTGUI */
7016 /* GIF library details. */
7017 DEF_IMGLIB_FN (DGifCloseFile
, (GifFileType
*));
7018 DEF_IMGLIB_FN (DGifSlurp
, (GifFileType
*));
7019 DEF_IMGLIB_FN (DGifOpen
, (void *, InputFunc
));
7020 DEF_IMGLIB_FN (DGifOpenFileName
, (const char *));
7023 init_gif_functions (Lisp_Object libraries
)
7027 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7030 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7031 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7032 LOAD_IMGLIB_FN (library
, DGifOpen
);
7033 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7039 #define fn_DGifCloseFile DGifCloseFile
7040 #define fn_DGifSlurp DGifSlurp
7041 #define fn_DGifOpen DGifOpen
7042 #define fn_DGifOpenFileName DGifOpenFileName
7044 #endif /* HAVE_NTGUI */
7046 /* Reading a GIF image from memory
7047 Based on the PNG memory stuff to a certain extent. */
7051 unsigned char *bytes
;
7057 /* Make the current memory source available to gif_read_from_memory.
7058 It's done this way because not all versions of libungif support
7059 a UserData field in the GifFileType structure. */
7060 static gif_memory_source
*current_gif_memory_src
;
7063 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7065 gif_memory_source
*src
= current_gif_memory_src
;
7067 if (len
> src
->len
- src
->index
)
7070 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7076 /* Load GIF image IMG for use on frame F. Value is non-zero if
7079 static const int interlace_start
[] = {0, 4, 2, 1};
7080 static const int interlace_increment
[] = {8, 8, 4, 2};
7082 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7085 gif_load (struct frame
*f
, struct image
*img
)
7087 Lisp_Object file
, specified_file
;
7088 Lisp_Object specified_data
;
7089 int rc
, width
, height
, x
, y
, i
;
7090 boolean transparent_p
;
7092 ColorMapObject
*gif_color_map
;
7093 unsigned long pixel_colors
[256];
7096 int ino
, image_height
, image_width
;
7097 gif_memory_source memsrc
;
7098 unsigned char *raster
;
7099 unsigned int transparency_color_index
;
7101 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7102 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7104 if (NILP (specified_data
))
7106 file
= x_find_image_file (specified_file
);
7107 if (!STRINGP (file
))
7109 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7113 /* Open the GIF file. Casting return value avoids a GCC warning
7115 gif
= (GifFileType
*)fn_DGifOpenFileName (SDATA (file
));
7118 image_error ("Cannot open `%s'", file
, Qnil
);
7124 if (!STRINGP (specified_data
))
7126 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7130 /* Read from memory! */
7131 current_gif_memory_src
= &memsrc
;
7132 memsrc
.bytes
= SDATA (specified_data
);
7133 memsrc
.len
= SBYTES (specified_data
);
7136 /* Casting return value avoids a GCC warning on W32. */
7137 gif
= (GifFileType
*) fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7140 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7145 /* Before reading entire contents, check the declared image size. */
7146 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7148 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7149 fn_DGifCloseFile (gif
);
7153 /* Read entire contents. */
7154 rc
= fn_DGifSlurp (gif
);
7155 if (rc
== GIF_ERROR
)
7157 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7158 fn_DGifCloseFile (gif
);
7162 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7163 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7164 if (ino
>= gif
->ImageCount
)
7166 image_error ("Invalid image number `%s' in image `%s'",
7168 fn_DGifCloseFile (gif
);
7172 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++)
7173 if ((gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Function
7174 == GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7175 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].ByteCount
== 4
7176 /* Transparency enabled? */
7177 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[0] & 1)
7180 transparency_color_index
7181 = (unsigned char) gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[3];
7184 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Top
;
7185 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Left
;
7186 image_height
= gif
->SavedImages
[ino
].ImageDesc
.Height
;
7187 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7188 image_width
= gif
->SavedImages
[ino
].ImageDesc
.Width
;
7189 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7191 width
= img
->width
= max (gif
->SWidth
,
7192 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7193 img
->corners
[RIGHT_CORNER
]));
7194 height
= img
->height
= max (gif
->SHeight
,
7195 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7196 img
->corners
[BOT_CORNER
]));
7198 if (!check_image_size (f
, width
, height
))
7200 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7201 fn_DGifCloseFile (gif
);
7205 /* Create the X image and pixmap. */
7206 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7208 fn_DGifCloseFile (gif
);
7212 /* Allocate colors. */
7213 gif_color_map
= gif
->SavedImages
[ino
].ImageDesc
.ColorMap
;
7215 gif_color_map
= gif
->SColorMap
;
7216 init_color_table ();
7217 memset (pixel_colors
, 0, sizeof pixel_colors
);
7220 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7222 if (transparent_p
&& transparency_color_index
== i
)
7224 Lisp_Object specified_bg
7225 = image_spec_value (img
->spec
, QCbackground
, NULL
);
7226 pixel_colors
[i
] = STRINGP (specified_bg
)
7227 ? x_alloc_image_color (f
, img
, specified_bg
,
7228 FRAME_BACKGROUND_PIXEL (f
))
7229 : FRAME_BACKGROUND_PIXEL (f
);
7233 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7234 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7235 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7236 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7240 #ifdef COLOR_TABLE_SUPPORT
7241 img
->colors
= colors_in_color_table (&img
->ncolors
);
7242 free_color_table ();
7243 #endif /* COLOR_TABLE_SUPPORT */
7245 /* Clear the part of the screen image that are not covered by
7246 the image from the GIF file. Full animated GIF support
7247 requires more than can be done here (see the gif89 spec,
7248 disposal methods). Let's simply assume that the part
7249 not covered by a sub-image is in the frame's background color. */
7250 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7251 for (x
= 0; x
< width
; ++x
)
7252 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7254 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7255 for (x
= 0; x
< width
; ++x
)
7256 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7258 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7260 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7261 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7262 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7263 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7266 /* Read the GIF image into the X image. We use a local variable
7267 `raster' here because RasterBits below is a char *, and invites
7268 problems with bytes >= 0x80. */
7269 raster
= (unsigned char *) gif
->SavedImages
[ino
].RasterBits
;
7271 if (gif
->SavedImages
[ino
].ImageDesc
.Interlace
)
7274 int row
= interlace_start
[0];
7278 for (y
= 0; y
< image_height
; y
++)
7280 if (row
>= image_height
)
7282 row
= interlace_start
[++pass
];
7283 while (row
>= image_height
)
7284 row
= interlace_start
[++pass
];
7287 for (x
= 0; x
< image_width
; x
++)
7289 int i
= raster
[(y
* image_width
) + x
];
7290 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7291 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7294 row
+= interlace_increment
[pass
];
7299 for (y
= 0; y
< image_height
; ++y
)
7300 for (x
= 0; x
< image_width
; ++x
)
7302 int i
= raster
[y
* image_width
+ x
];
7303 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7304 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7308 /* Save GIF image extension data for `image-metadata'.
7309 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7310 img
->data
.lisp_val
= Qnil
;
7311 if (gif
->SavedImages
[ino
].ExtensionBlockCount
> 0)
7313 ExtensionBlock
*ext
= gif
->SavedImages
[ino
].ExtensionBlocks
;
7314 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++, ext
++)
7315 /* Append (... FUNCTION "BYTES") */
7316 img
->data
.lisp_val
= Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7317 Fcons (make_number (ext
->Function
),
7318 img
->data
.lisp_val
));
7319 img
->data
.lisp_val
= Fcons (Qextension_data
,
7320 Fcons (Fnreverse (img
->data
.lisp_val
),
7323 if (gif
->ImageCount
> 1)
7324 img
->data
.lisp_val
= Fcons (Qcount
,
7325 Fcons (make_number (gif
->ImageCount
),
7326 img
->data
.lisp_val
));
7328 fn_DGifCloseFile (gif
);
7330 /* Maybe fill in the background field while we have ximg handy. */
7331 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7332 /* Casting avoids a GCC warning. */
7333 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7335 /* Put the image into the pixmap, then free the X image and its buffer. */
7336 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7337 x_destroy_x_image (ximg
);
7342 #else /* !HAVE_GIF */
7346 gif_load (struct frame
*f
, struct image
*img
)
7348 return ns_load_image (f
, img
,
7349 image_spec_value (img
->spec
, QCfile
, NULL
),
7350 image_spec_value (img
->spec
, QCdata
, NULL
));
7352 #endif /* HAVE_NS */
7354 #endif /* HAVE_GIF */
7357 /***********************************************************************
7359 ***********************************************************************/
7360 #if defined (HAVE_IMAGEMAGICK)
7362 /* The symbol `imagemagick' identifying images of this type. */
7364 Lisp_Object Qimagemagick
;
7365 /* Indices of image specification fields in imagemagick_format, below. */
7367 enum imagemagick_keyword_index
7375 IMAGEMAGICK_ALGORITHM
,
7376 IMAGEMAGICK_HEURISTIC_MASK
,
7378 IMAGEMAGICK_BACKGROUND
,
7381 IMAGEMAGICK_ROTATION
,
7386 /* Vector of image_keyword structures describing the format
7387 of valid user-defined image specifications. */
7389 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7391 {":type", IMAGE_SYMBOL_VALUE
, 1},
7392 {":data", IMAGE_STRING_VALUE
, 0},
7393 {":file", IMAGE_STRING_VALUE
, 0},
7394 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7395 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7396 {":relief", IMAGE_INTEGER_VALUE
, 0},
7397 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7398 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7399 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7400 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7401 {":height", IMAGE_INTEGER_VALUE
, 0},
7402 {":width", IMAGE_INTEGER_VALUE
, 0},
7403 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7404 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7406 /* Free X resources of imagemagick image IMG which is used on frame F. */
7409 imagemagick_clear_image (struct frame
*f
,
7412 x_clear_image (f
, img
);
7417 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7418 this by calling parse_image_spec and supplying the keywords that
7419 identify the IMAGEMAGICK format. */
7422 imagemagick_image_p (Lisp_Object object
)
7424 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7425 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7427 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7430 /* Must specify either the :data or :file keyword. */
7431 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7434 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7435 Therefore rename the function so it doesnt collide with ImageMagick. */
7436 #define DrawRectangle DrawRectangleGif
7437 #include <wand/MagickWand.h>
7439 /* imagemagick_load_image is a helper function for imagemagick_load,
7440 which does the actual loading given contents and size, apart from
7441 frame and image structures, passed from imagemagick_load.
7443 Uses librimagemagick to do most of the image processing.
7445 non-zero when successful.
7449 imagemagick_load_image (/* Pointer to emacs frame structure. */
7451 /* Pointer to emacs image structure. */
7453 /* String containing the IMAGEMAGICK data to
7455 unsigned char *contents
,
7456 /* Size of data in bytes. */
7458 /* Filename, either pass filename or
7460 unsigned char *filename
)
7462 unsigned long width
;
7463 unsigned long height
;
7469 Lisp_Object specified_bg
;
7474 MagickWand
*image_wand
;
7475 MagickWand
*ping_wand
;
7476 PixelIterator
*iterator
;
7478 MagickPixelPacket pixel
;
7481 Lisp_Object crop
, geometry
;
7483 int desired_width
, desired_height
;
7485 int imagemagick_rendermethod
;
7487 ImageInfo
*image_info
;
7488 ExceptionInfo
*exception
;
7492 /* Handle image index for image types who can contain more than one
7493 image. Interface :index is same as for GIF. First we "ping" the
7494 image to see how many sub-images it contains. Pinging is faster
7495 than loading the image to find out things about it. */
7497 /* `MagickWandGenesis' initializes the imagemagick environment. */
7498 MagickWandGenesis ();
7499 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7500 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7501 ping_wand
= NewMagickWand ();
7502 MagickSetResolution (ping_wand
, 2, 2);
7503 if (filename
!= NULL
)
7505 status
= MagickPingImage (ping_wand
, filename
);
7509 status
= MagickPingImageBlob (ping_wand
, contents
, size
);
7512 if (ino
>= MagickGetNumberImages (ping_wand
))
7514 image_error ("Invalid image number `%s' in image `%s'",
7516 DestroyMagickWand (ping_wand
);
7520 if (MagickGetNumberImages(ping_wand
) > 1)
7521 img
->data
.lisp_val
=
7523 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7524 img
->data
.lisp_val
));
7526 DestroyMagickWand (ping_wand
);
7528 /* Now, after pinging, we know how many images are inside the
7529 file. If its not a bundle, just one. */
7531 if (filename
!= NULL
)
7533 image_info
= CloneImageInfo ((ImageInfo
*) NULL
);
7534 (void) strcpy (image_info
->filename
, filename
);
7535 image_info
->number_scenes
= 1;
7536 image_info
->scene
= ino
;
7537 exception
= AcquireExceptionInfo ();
7539 im_image
= ReadImage (image_info
, exception
);
7540 DestroyExceptionInfo (exception
);
7542 if (im_image
!= NULL
)
7544 image_wand
= NewMagickWandFromImage (im_image
);
7545 DestroyImage(im_image
);
7546 status
= MagickTrue
;
7549 status
= MagickFalse
;
7553 image_wand
= NewMagickWand ();
7554 status
= MagickReadImageBlob (image_wand
, contents
, size
);
7557 if (status
== MagickFalse
) goto imagemagick_error
;
7559 /* If width and/or height is set in the display spec assume we want
7560 to scale to those values. if either h or w is unspecified, the
7561 unspecified should be calculated from the specified to preserve
7564 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7565 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7566 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7567 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7569 height
= MagickGetImageHeight (image_wand
);
7570 width
= MagickGetImageWidth (image_wand
);
7572 if(desired_width
!= -1 && desired_height
== -1)
7574 /* w known, calculate h. */
7575 desired_height
= (double) desired_width
/ width
* height
;
7577 if(desired_width
== -1 && desired_height
!= -1)
7579 /* h known, calculate w. */
7580 desired_width
= (double) desired_height
/ height
* width
;
7582 if(desired_width
!= -1 && desired_height
!= -1)
7584 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7585 if (status
== MagickFalse
)
7587 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7588 goto imagemagick_error
;
7593 /* crop behaves similar to image slicing in Emacs but is more memory
7595 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7597 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7599 /* After some testing, it seems MagickCropImage is the fastest
7600 crop function in ImageMagick. This crop function seems to do
7601 less copying than the alternatives, but it still reads the
7602 entire image into memory before croping, which is aparently
7603 difficult to avoid when using imagemagick. */
7606 w
= XFASTINT (XCAR (crop
));
7608 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7610 h
= XFASTINT (XCAR (crop
));
7612 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7614 x
= XFASTINT (XCAR (crop
));
7616 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7618 y
= XFASTINT (XCAR (crop
));
7619 MagickCropImage (image_wand
, w
, h
, x
, y
);
7625 /* Furthermore :rotation. we need background color and angle for
7628 TODO background handling for rotation specified_bg =
7629 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7631 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7634 PixelWand
* background
= NewPixelWand ();
7635 PixelSetColor (background
, "#ffffff");/*TODO remove hardcode*/
7637 rotation
= extract_float (value
);
7639 status
= MagickRotateImage (image_wand
, background
, rotation
);
7640 DestroyPixelWand (background
);
7641 if (status
== MagickFalse
)
7643 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7644 goto imagemagick_error
;
7648 /* Finaly we are done manipulating the image, figure out resulting
7649 width, height, and then transfer ownerwship to Emacs. */
7650 height
= MagickGetImageHeight (image_wand
);
7651 width
= MagickGetImageWidth (image_wand
);
7653 if (! check_image_size (f
, width
, height
))
7655 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7656 goto imagemagick_error
;
7659 /* We can now get a valid pixel buffer from the imagemagick file, if all
7662 init_color_table ();
7663 imagemagick_rendermethod
= (INTEGERP (Vimagemagick_render_type
)
7664 ? XFASTINT (Vimagemagick_render_type
) : 0);
7665 if (imagemagick_rendermethod
== 0)
7667 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7668 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7669 &ximg
, &img
->pixmap
))
7671 #ifdef COLOR_TABLE_SUPPORT
7672 free_color_table ();
7674 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7675 goto imagemagick_error
;
7678 /* Copy imagegmagick image to x with primitive yet robust pixel
7679 pusher loop. This has been tested a lot with many different
7682 /* Copy pixels from the imagemagick image structure to the x image map. */
7683 iterator
= NewPixelIterator (image_wand
);
7684 if (iterator
== (PixelIterator
*) NULL
)
7686 #ifdef COLOR_TABLE_SUPPORT
7687 free_color_table ();
7689 x_destroy_x_image (ximg
);
7690 image_error ("Imagemagick pixel iterator creation failed",
7692 goto imagemagick_error
;
7695 for (y
= 0; y
< (long) MagickGetImageHeight (image_wand
); y
++)
7697 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7698 if (pixels
== (PixelWand
**) NULL
)
7700 for (x
= 0; x
< (long) width
; x
++)
7702 PixelGetMagickColor (pixels
[x
], &pixel
);
7703 XPutPixel (ximg
, x
, y
,
7704 lookup_rgb_color (f
,
7710 DestroyPixelIterator (iterator
);
7713 if (imagemagick_rendermethod
== 1)
7715 /* Magicexportimage is normaly faster than pixelpushing. This
7716 method is also well tested. Some aspects of this method are
7717 ad-hoc and needs to be more researched. */
7718 int imagedepth
= 24;/*MagickGetImageDepth(image_wand);*/
7719 char* exportdepth
= imagedepth
<= 8 ? "I" : "BGRP";/*"RGBP";*/
7720 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7721 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7722 &ximg
, &img
->pixmap
))
7724 #ifdef COLOR_TABLE_SUPPORT
7725 free_color_table ();
7727 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7728 goto imagemagick_error
;
7732 /* Oddly, the below code doesnt seem to work:*/
7733 /* switch(ximg->bitmap_unit){ */
7735 /* pixelwidth=CharPixel; */
7738 /* pixelwidth=ShortPixel; */
7741 /* pixelwidth=LongPixel; */
7745 Here im just guessing the format of the bitmap.
7746 happens to work fine for:
7749 seems about 3 times as fast as pixel pushing(not carefully measured)
7751 pixelwidth
= CharPixel
;/*??? TODO figure out*/
7752 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7753 MagickExportImagePixels (image_wand
,
7758 /*&(img->pixmap));*/
7761 image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
7767 #ifdef COLOR_TABLE_SUPPORT
7768 /* Remember colors allocated for this image. */
7769 img
->colors
= colors_in_color_table (&img
->ncolors
);
7770 free_color_table ();
7771 #endif /* COLOR_TABLE_SUPPORT */
7775 img
->height
= height
;
7777 /* Put the image into the pixmap, then free the X image and its
7779 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7780 x_destroy_x_image (ximg
);
7783 /* Final cleanup. image_wand should be the only resource left. */
7784 DestroyMagickWand (image_wand
);
7785 /* `MagickWandTerminus' terminates the imagemagick environment. */
7786 MagickWandTerminus ();
7791 DestroyMagickWand (image_wand
);
7792 MagickWandTerminus ();
7793 /* TODO more cleanup. */
7794 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7799 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7800 successful. this function will go into the imagemagick_type structure, and
7801 the prototype thus needs to be compatible with that structure. */
7804 imagemagick_load (struct frame
*f
,
7808 Lisp_Object file_name
;
7810 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7811 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7812 if (STRINGP (file_name
))
7816 file
= x_find_image_file (file_name
);
7817 if (!STRINGP (file
))
7819 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7822 success_p
= imagemagick_load_image (f
, img
, 0, 0, SDATA (file
));
7824 /* Else its not a file, its a lisp object. Load the image from a
7825 lisp object rather than a file. */
7830 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7831 if (!STRINGP (data
))
7833 image_error ("Invalid image data `%s'", data
, Qnil
);
7836 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7837 SBYTES (data
), NULL
);
7843 /* Structure describing the image type `imagemagick'. Its the same
7844 type of structure defined for all image formats, handled by Emacs
7845 image functions. See struct image_type in dispextern.h. */
7847 static struct image_type imagemagick_type
=
7849 /* An identifier showing that this is an image structure for the
7850 IMAGEMAGICK format. */
7852 /* Handle to a function that can be used to identify a IMAGEMAGICK
7854 imagemagick_image_p
,
7855 /* Handle to function used to load a IMAGEMAGICK file. */
7857 /* Handle to function to free resources for IMAGEMAGICK. */
7858 imagemagick_clear_image
,
7859 /* An internal field to link to the next image type in a list of
7860 image types, will be filled in when registering the format. */
7867 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7868 doc
: /* Return image file types supported by ImageMagick.
7869 Since ImageMagick recognizes a lot of file-types that clash with Emacs,
7870 such as .c, we want to be able to alter the list at the lisp level. */)
7873 Lisp_Object typelist
= Qnil
;
7876 char **imtypes
= GetMagickList ("*", &numf
, &ex
);
7878 Lisp_Object Qimagemagicktype
;
7879 for (i
= 0; i
< numf
; i
++)
7881 Qimagemagicktype
= intern (imtypes
[i
]);
7882 typelist
= Fcons (Qimagemagicktype
, typelist
);
7887 #endif /* defined (HAVE_IMAGEMAGICK) */
7891 /***********************************************************************
7893 ***********************************************************************/
7895 #if defined (HAVE_RSVG)
7897 /* Function prototypes. */
7899 static int svg_image_p (Lisp_Object object
);
7900 static int svg_load (struct frame
*f
, struct image
*img
);
7902 static int svg_load_image (struct frame
*, struct image
*,
7903 unsigned char *, unsigned int);
7905 /* The symbol `svg' identifying images of this type. */
7909 /* Indices of image specification fields in svg_format, below. */
7911 enum svg_keyword_index
7926 /* Vector of image_keyword structures describing the format
7927 of valid user-defined image specifications. */
7929 static const struct image_keyword svg_format
[SVG_LAST
] =
7931 {":type", IMAGE_SYMBOL_VALUE
, 1},
7932 {":data", IMAGE_STRING_VALUE
, 0},
7933 {":file", IMAGE_STRING_VALUE
, 0},
7934 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7935 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7936 {":relief", IMAGE_INTEGER_VALUE
, 0},
7937 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7938 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7939 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7940 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7943 /* Structure describing the image type `svg'. Its the same type of
7944 structure defined for all image formats, handled by emacs image
7945 functions. See struct image_type in dispextern.h. */
7947 static struct image_type svg_type
=
7949 /* An identifier showing that this is an image structure for the SVG format. */
7951 /* Handle to a function that can be used to identify a SVG file. */
7953 /* Handle to function used to load a SVG file. */
7955 /* Handle to function to free sresources for SVG. */
7957 /* An internal field to link to the next image type in a list of
7958 image types, will be filled in when registering the format. */
7963 /* Return non-zero if OBJECT is a valid SVG image specification. Do
7964 this by calling parse_image_spec and supplying the keywords that
7965 identify the SVG format. */
7968 svg_image_p (Lisp_Object object
)
7970 struct image_keyword fmt
[SVG_LAST
];
7971 memcpy (fmt
, svg_format
, sizeof fmt
);
7973 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
7976 /* Must specify either the :data or :file keyword. */
7977 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
7980 #include <librsvg/rsvg.h>
7984 /* SVG library functions. */
7985 DEF_IMGLIB_FN (rsvg_handle_new
);
7986 DEF_IMGLIB_FN (rsvg_handle_get_dimensions
);
7987 DEF_IMGLIB_FN (rsvg_handle_write
);
7988 DEF_IMGLIB_FN (rsvg_handle_close
);
7989 DEF_IMGLIB_FN (rsvg_handle_get_pixbuf
);
7990 DEF_IMGLIB_FN (rsvg_handle_free
);
7992 DEF_IMGLIB_FN (gdk_pixbuf_get_width
);
7993 DEF_IMGLIB_FN (gdk_pixbuf_get_height
);
7994 DEF_IMGLIB_FN (gdk_pixbuf_get_pixels
);
7995 DEF_IMGLIB_FN (gdk_pixbuf_get_rowstride
);
7996 DEF_IMGLIB_FN (gdk_pixbuf_get_colorspace
);
7997 DEF_IMGLIB_FN (gdk_pixbuf_get_n_channels
);
7998 DEF_IMGLIB_FN (gdk_pixbuf_get_has_alpha
);
7999 DEF_IMGLIB_FN (gdk_pixbuf_get_bits_per_sample
);
8001 DEF_IMGLIB_FN (g_type_init
);
8002 DEF_IMGLIB_FN (g_object_unref
);
8003 DEF_IMGLIB_FN (g_error_free
);
8005 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8008 init_svg_functions (Lisp_Object libraries
)
8010 HMODULE library
, gdklib
, glib
, gobject
;
8012 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8013 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8014 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8015 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8018 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8019 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8020 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8021 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8022 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8023 LOAD_IMGLIB_FN (library
, rsvg_handle_free
);
8025 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8026 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8027 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8028 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8029 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8030 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8031 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8032 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8034 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8035 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8036 LOAD_IMGLIB_FN (glib
, g_error_free
);
8042 /* The following aliases for library functions allow dynamic loading
8043 to be used on some platforms. */
8044 #define fn_rsvg_handle_new rsvg_handle_new
8045 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8046 #define fn_rsvg_handle_write rsvg_handle_write
8047 #define fn_rsvg_handle_close rsvg_handle_close
8048 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8049 #define fn_rsvg_handle_free rsvg_handle_free
8051 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8052 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8053 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8054 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8055 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8056 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8057 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8058 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8060 #define fn_g_type_init g_type_init
8061 #define fn_g_object_unref g_object_unref
8062 #define fn_g_error_free g_error_free
8063 #endif /* !HAVE_NTGUI */
8065 /* Load SVG image IMG for use on frame F. Value is non-zero if
8066 successful. this function will go into the svg_type structure, and
8067 the prototype thus needs to be compatible with that structure. */
8070 svg_load (struct frame
*f
, struct image
*img
)
8073 Lisp_Object file_name
;
8075 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8076 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8077 if (STRINGP (file_name
))
8080 unsigned char *contents
;
8083 file
= x_find_image_file (file_name
);
8084 if (!STRINGP (file
))
8086 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8090 /* Read the entire file into memory. */
8091 contents
= slurp_file (SDATA (file
), &size
);
8092 if (contents
== NULL
)
8094 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8097 /* If the file was slurped into memory properly, parse it. */
8098 success_p
= svg_load_image (f
, img
, contents
, size
);
8101 /* Else its not a file, its a lisp object. Load the image from a
8102 lisp object rather than a file. */
8107 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8108 if (!STRINGP (data
))
8110 image_error ("Invalid image data `%s'", data
, Qnil
);
8113 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8119 /* svg_load_image is a helper function for svg_load, which does the
8120 actual loading given contents and size, apart from frame and image
8121 structures, passed from svg_load.
8123 Uses librsvg to do most of the image processing.
8125 Returns non-zero when successful. */
8127 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8128 struct image
*img
, /* Pointer to emacs image structure. */
8129 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8130 unsigned int size
) /* Size of data in bytes. */
8132 RsvgHandle
*rsvg_handle
;
8133 RsvgDimensionData dimension_data
;
8134 GError
*error
= NULL
;
8138 const guint8
*pixels
;
8141 Lisp_Object specified_bg
;
8146 /* g_type_init is a glib function that must be called prior to using
8147 gnome type library functions. */
8149 /* Make a handle to a new rsvg object. */
8150 rsvg_handle
= (RsvgHandle
*) fn_rsvg_handle_new ();
8152 /* Parse the contents argument and fill in the rsvg_handle. */
8153 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &error
);
8154 if (error
) goto rsvg_error
;
8156 /* The parsing is complete, rsvg_handle is ready to used, close it
8157 for further writes. */
8158 fn_rsvg_handle_close (rsvg_handle
, &error
);
8159 if (error
) goto rsvg_error
;
8161 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8162 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8164 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8168 /* We can now get a valid pixel buffer from the svg file, if all
8170 pixbuf
= (GdkPixbuf
*) fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8171 if (!pixbuf
) goto rsvg_error
;
8172 fn_g_object_unref (rsvg_handle
);
8174 /* Extract some meta data from the svg handle. */
8175 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8176 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8177 pixels
= (const guint8
*) fn_gdk_pixbuf_get_pixels (pixbuf
);
8178 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8180 /* Validate the svg meta data. */
8181 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8182 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8183 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8184 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8186 /* Try to create a x pixmap to hold the svg pixmap. */
8187 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8189 fn_g_object_unref (pixbuf
);
8193 init_color_table ();
8195 /* Handle alpha channel by combining the image with a background
8197 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8198 if (!STRINGP (specified_bg
)
8199 || !x_defined_color (f
, SDATA (specified_bg
), &background
, 0))
8202 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8203 x_query_color (f
, &background
);
8205 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8209 /* SVG pixmaps specify transparency in the last byte, so right
8210 shift 8 bits to get rid of it, since emacs doesn't support
8212 background
.red
>>= 8;
8213 background
.green
>>= 8;
8214 background
.blue
>>= 8;
8216 /* This loop handles opacity values, since Emacs assumes
8217 non-transparent images. Each pixel must be "flattened" by
8218 calculating the resulting color, given the transparency of the
8219 pixel, and the image background color. */
8220 for (y
= 0; y
< height
; ++y
)
8222 for (x
= 0; x
< width
; ++x
)
8232 opacity
= *pixels
++;
8234 red
= ((red
* opacity
)
8235 + (background
.red
* ((1 << 8) - opacity
)));
8236 green
= ((green
* opacity
)
8237 + (background
.green
* ((1 << 8) - opacity
)));
8238 blue
= ((blue
* opacity
)
8239 + (background
.blue
* ((1 << 8) - opacity
)));
8241 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8244 pixels
+= rowstride
- 4 * width
;
8247 #ifdef COLOR_TABLE_SUPPORT
8248 /* Remember colors allocated for this image. */
8249 img
->colors
= colors_in_color_table (&img
->ncolors
);
8250 free_color_table ();
8251 #endif /* COLOR_TABLE_SUPPORT */
8253 fn_g_object_unref (pixbuf
);
8256 img
->height
= height
;
8258 /* Maybe fill in the background field while we have ximg handy.
8259 Casting avoids a GCC warning. */
8260 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8262 /* Put the image into the pixmap, then free the X image and its
8264 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8265 x_destroy_x_image (ximg
);
8270 fn_g_object_unref (rsvg_handle
);
8271 /* FIXME: Use error->message so the user knows what is the actual
8272 problem with the image. */
8273 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8274 fn_g_error_free (error
);
8278 #endif /* defined (HAVE_RSVG) */
8283 /***********************************************************************
8285 ***********************************************************************/
8287 #ifdef HAVE_X_WINDOWS
8288 #define HAVE_GHOSTSCRIPT 1
8289 #endif /* HAVE_X_WINDOWS */
8291 #ifdef HAVE_GHOSTSCRIPT
8293 static int gs_image_p (Lisp_Object object
);
8294 static int gs_load (struct frame
*f
, struct image
*img
);
8295 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8297 /* Keyword symbols. */
8299 Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8301 /* Indices of image specification fields in gs_format, below. */
8303 enum gs_keyword_index
8321 /* Vector of image_keyword structures describing the format
8322 of valid user-defined image specifications. */
8324 static const struct image_keyword gs_format
[GS_LAST
] =
8326 {":type", IMAGE_SYMBOL_VALUE
, 1},
8327 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8328 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8329 {":file", IMAGE_STRING_VALUE
, 1},
8330 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8331 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8332 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8333 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8334 {":relief", IMAGE_INTEGER_VALUE
, 0},
8335 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8336 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8337 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8338 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8341 /* Structure describing the image type `ghostscript'. */
8343 static struct image_type gs_type
=
8353 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8356 gs_clear_image (struct frame
*f
, struct image
*img
)
8358 /* IMG->data.ptr_val may contain a recorded colormap. */
8359 xfree (img
->data
.ptr_val
);
8360 x_clear_image (f
, img
);
8364 /* Return non-zero if OBJECT is a valid Ghostscript image
8368 gs_image_p (Lisp_Object object
)
8370 struct image_keyword fmt
[GS_LAST
];
8374 memcpy (fmt
, gs_format
, sizeof fmt
);
8376 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8379 /* Bounding box must be a list or vector containing 4 integers. */
8380 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8383 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8384 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8389 else if (VECTORP (tem
))
8391 if (XVECTOR (tem
)->size
!= 4)
8393 for (i
= 0; i
< 4; ++i
)
8394 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
8404 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8408 gs_load (struct frame
*f
, struct image
*img
)
8411 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8413 double in_width
, in_height
;
8414 Lisp_Object pixel_colors
= Qnil
;
8416 /* Compute pixel size of pixmap needed from the given size in the
8417 image specification. Sizes in the specification are in pt. 1 pt
8418 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8420 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8421 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8422 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
8423 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8424 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8425 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
8427 if (!check_image_size (f
, img
->width
, img
->height
))
8429 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8433 /* Create the pixmap. */
8434 xassert (img
->pixmap
== NO_PIXMAP
);
8436 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8438 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8439 img
->width
, img
->height
,
8440 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8445 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8449 /* Call the loader to fill the pixmap. It returns a process object
8450 if successful. We do not record_unwind_protect here because
8451 other places in redisplay like calling window scroll functions
8452 don't either. Let the Lisp loader use `unwind-protect' instead. */
8453 sprintf (buffer
, "%lu %lu",
8454 (unsigned long) FRAME_X_WINDOW (f
),
8455 (unsigned long) img
->pixmap
);
8456 window_and_pixmap_id
= build_string (buffer
);
8458 sprintf (buffer
, "%lu %lu",
8459 FRAME_FOREGROUND_PIXEL (f
),
8460 FRAME_BACKGROUND_PIXEL (f
));
8461 pixel_colors
= build_string (buffer
);
8463 XSETFRAME (frame
, f
);
8464 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8466 loader
= intern ("gs-load-image");
8468 img
->data
.lisp_val
= call6 (loader
, frame
, img
->spec
,
8469 make_number (img
->width
),
8470 make_number (img
->height
),
8471 window_and_pixmap_id
,
8473 return PROCESSP (img
->data
.lisp_val
);
8477 /* Kill the Ghostscript process that was started to fill PIXMAP on
8478 frame F. Called from XTread_socket when receiving an event
8479 telling Emacs that Ghostscript has finished drawing. */
8482 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8484 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8488 /* Find the image containing PIXMAP. */
8489 for (i
= 0; i
< c
->used
; ++i
)
8490 if (c
->images
[i
]->pixmap
== pixmap
)
8493 /* Should someone in between have cleared the image cache, for
8494 instance, give up. */
8498 /* Kill the GS process. We should have found PIXMAP in the image
8499 cache and its image should contain a process object. */
8501 xassert (PROCESSP (img
->data
.lisp_val
));
8502 Fkill_process (img
->data
.lisp_val
, Qnil
);
8503 img
->data
.lisp_val
= Qnil
;
8505 #if defined (HAVE_X_WINDOWS)
8507 /* On displays with a mutable colormap, figure out the colors
8508 allocated for the image by looking at the pixels of an XImage for
8510 class = FRAME_X_VISUAL (f
)->class;
8511 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8517 /* Try to get an XImage for img->pixmep. */
8518 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8519 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8524 /* Initialize the color table. */
8525 init_color_table ();
8527 /* For each pixel of the image, look its color up in the
8528 color table. After having done so, the color table will
8529 contain an entry for each color used by the image. */
8530 for (y
= 0; y
< img
->height
; ++y
)
8531 for (x
= 0; x
< img
->width
; ++x
)
8533 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8534 lookup_pixel_color (f
, pixel
);
8537 /* Record colors in the image. Free color table and XImage. */
8538 #ifdef COLOR_TABLE_SUPPORT
8539 img
->colors
= colors_in_color_table (&img
->ncolors
);
8540 free_color_table ();
8542 XDestroyImage (ximg
);
8544 #if 0 /* This doesn't seem to be the case. If we free the colors
8545 here, we get a BadAccess later in x_clear_image when
8546 freeing the colors. */
8547 /* We have allocated colors once, but Ghostscript has also
8548 allocated colors on behalf of us. So, to get the
8549 reference counts right, free them once. */
8551 x_free_colors (f
, img
->colors
, img
->ncolors
);
8555 image_error ("Cannot get X image of `%s'; colors will not be freed",
8560 #endif /* HAVE_X_WINDOWS */
8562 /* Now that we have the pixmap, compute mask and transform the
8563 image if requested. */
8565 postprocess_image (f
, img
);
8569 #endif /* HAVE_GHOSTSCRIPT */
8572 /***********************************************************************
8574 ***********************************************************************/
8578 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8579 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8582 return valid_image_p (spec
) ? Qt
: Qnil
;
8586 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8591 if (valid_image_p (spec
))
8592 id
= lookup_image (SELECTED_FRAME (), spec
);
8595 return make_number (id
);
8598 #endif /* GLYPH_DEBUG != 0 */
8601 /***********************************************************************
8603 ***********************************************************************/
8606 /* Image types that rely on external libraries are loaded dynamically
8607 if the library is available. */
8608 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8609 define_image_type (image_type, init_lib_fn (libraries))
8611 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8612 define_image_type (image_type, 1)
8613 #endif /* HAVE_NTGUI */
8615 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8616 doc
: /* Initialize image library implementing image type TYPE.
8617 Return non-nil if TYPE is a supported image type.
8619 Image types pbm and xbm are prebuilt; other types are loaded here.
8620 Libraries to load are specified in alist LIBRARIES (usually, the value
8621 of `dynamic-library-alist', which see). */)
8622 (Lisp_Object type
, Lisp_Object libraries
)
8626 /* Don't try to reload the library. */
8627 tested
= Fassq (type
, Vimage_type_cache
);
8629 return XCDR (tested
);
8631 #if defined (HAVE_XPM) || defined (HAVE_NS)
8632 if (EQ (type
, Qxpm
))
8633 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8636 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8637 if (EQ (type
, Qjpeg
))
8638 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8641 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8642 if (EQ (type
, Qtiff
))
8643 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8646 #if defined (HAVE_GIF) || defined (HAVE_NS)
8647 if (EQ (type
, Qgif
))
8648 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8651 #if defined (HAVE_PNG) || defined (HAVE_NS)
8652 if (EQ (type
, Qpng
))
8653 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8656 #if defined (HAVE_RSVG)
8657 if (EQ (type
, Qsvg
))
8658 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8661 #if defined (HAVE_IMAGEMAGICK)
8662 if (EQ (type
, Qimagemagick
))
8664 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8669 #ifdef HAVE_GHOSTSCRIPT
8670 if (EQ (type
, Qpostscript
))
8671 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8674 /* If the type is not recognized, avoid testing it ever again. */
8675 CACHE_IMAGE_TYPE (type
, Qnil
);
8680 syms_of_image (void)
8682 /* Initialize this only once, since that's what we do with Vimage_types
8683 and they are supposed to be in sync. Initializing here gives correct
8684 operation on GNU/Linux of calling dump-emacs after loading some images. */
8687 /* Must be defined now becase we're going to update it below, while
8688 defining the supported image types. */
8689 DEFVAR_LISP ("image-types", Vimage_types
,
8690 doc
: /* List of potentially supported image types.
8691 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8692 To check whether it is really supported, use `image-type-available-p'. */);
8693 Vimage_types
= Qnil
;
8695 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8696 doc
: /* Maximum size of images.
8697 Emacs will not load an image into memory if its pixel width or
8698 pixel height exceeds this limit.
8700 If the value is an integer, it directly specifies the maximum
8701 image height and width, measured in pixels. If it is a floating
8702 point number, it specifies the maximum image height and width
8703 as a ratio to the frame height and width. If the value is
8704 non-numeric, there is no explicit limit on the size of images. */);
8705 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8707 Vimage_type_cache
= Qnil
;
8708 staticpro (&Vimage_type_cache
);
8710 Qpbm
= intern_c_string ("pbm");
8712 ADD_IMAGE_TYPE (Qpbm
);
8714 Qxbm
= intern_c_string ("xbm");
8716 ADD_IMAGE_TYPE (Qxbm
);
8718 define_image_type (&xbm_type
, 1);
8719 define_image_type (&pbm_type
, 1);
8721 Qcount
= intern_c_string ("count");
8722 staticpro (&Qcount
);
8723 Qextension_data
= intern_c_string ("extension-data");
8724 staticpro (&Qextension_data
);
8726 QCascent
= intern_c_string (":ascent");
8727 staticpro (&QCascent
);
8728 QCmargin
= intern_c_string (":margin");
8729 staticpro (&QCmargin
);
8730 QCrelief
= intern_c_string (":relief");
8731 staticpro (&QCrelief
);
8732 QCconversion
= intern_c_string (":conversion");
8733 staticpro (&QCconversion
);
8734 QCcolor_symbols
= intern_c_string (":color-symbols");
8735 staticpro (&QCcolor_symbols
);
8736 QCheuristic_mask
= intern_c_string (":heuristic-mask");
8737 staticpro (&QCheuristic_mask
);
8738 QCindex
= intern_c_string (":index");
8739 staticpro (&QCindex
);
8740 QCgeometry
= intern_c_string (":geometry");
8741 staticpro (&QCgeometry
);
8742 QCcrop
= intern_c_string (":crop");
8743 staticpro (&QCcrop
);
8744 QCrotation
= intern_c_string (":rotation");
8745 staticpro (&QCrotation
);
8746 QCmatrix
= intern_c_string (":matrix");
8747 staticpro (&QCmatrix
);
8748 QCcolor_adjustment
= intern_c_string (":color-adjustment");
8749 staticpro (&QCcolor_adjustment
);
8750 QCmask
= intern_c_string (":mask");
8751 staticpro (&QCmask
);
8753 Qlaplace
= intern_c_string ("laplace");
8754 staticpro (&Qlaplace
);
8755 Qemboss
= intern_c_string ("emboss");
8756 staticpro (&Qemboss
);
8757 Qedge_detection
= intern_c_string ("edge-detection");
8758 staticpro (&Qedge_detection
);
8759 Qheuristic
= intern_c_string ("heuristic");
8760 staticpro (&Qheuristic
);
8762 Qpostscript
= intern_c_string ("postscript");
8763 staticpro (&Qpostscript
);
8764 #ifdef HAVE_GHOSTSCRIPT
8765 ADD_IMAGE_TYPE (Qpostscript
);
8766 QCloader
= intern_c_string (":loader");
8767 staticpro (&QCloader
);
8768 QCbounding_box
= intern_c_string (":bounding-box");
8769 staticpro (&QCbounding_box
);
8770 QCpt_width
= intern_c_string (":pt-width");
8771 staticpro (&QCpt_width
);
8772 QCpt_height
= intern_c_string (":pt-height");
8773 staticpro (&QCpt_height
);
8774 #endif /* HAVE_GHOSTSCRIPT */
8777 Qlibpng_version
= intern_c_string ("libpng-version");
8778 staticpro (&Qlibpng_version
);
8779 Fset (Qlibpng_version
,
8781 make_number (PNG_LIBPNG_VER
)
8788 #if defined (HAVE_XPM) || defined (HAVE_NS)
8789 Qxpm
= intern_c_string ("xpm");
8791 ADD_IMAGE_TYPE (Qxpm
);
8794 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8795 Qjpeg
= intern_c_string ("jpeg");
8797 ADD_IMAGE_TYPE (Qjpeg
);
8800 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8801 Qtiff
= intern_c_string ("tiff");
8803 ADD_IMAGE_TYPE (Qtiff
);
8806 #if defined (HAVE_GIF) || defined (HAVE_NS)
8807 Qgif
= intern_c_string ("gif");
8809 ADD_IMAGE_TYPE (Qgif
);
8812 #if defined (HAVE_PNG) || defined (HAVE_NS)
8813 Qpng
= intern_c_string ("png");
8815 ADD_IMAGE_TYPE (Qpng
);
8818 #if defined (HAVE_IMAGEMAGICK)
8819 Qimagemagick
= intern_c_string ("imagemagick");
8820 staticpro (&Qimagemagick
);
8821 ADD_IMAGE_TYPE (Qimagemagick
);
8824 #if defined (HAVE_RSVG)
8825 Qsvg
= intern_c_string ("svg");
8827 ADD_IMAGE_TYPE (Qsvg
);
8829 /* Other libraries used directly by svg code. */
8830 Qgdk_pixbuf
= intern_c_string ("gdk-pixbuf");
8831 staticpro (&Qgdk_pixbuf
);
8832 Qglib
= intern_c_string ("glib");
8834 Qgobject
= intern_c_string ("gobject");
8835 staticpro (&Qgobject
);
8836 #endif /* HAVE_NTGUI */
8837 #endif /* HAVE_RSVG */
8839 defsubr (&Sinit_image_library
);
8840 #ifdef HAVE_IMAGEMAGICK
8841 defsubr (&Simagemagick_types
);
8843 defsubr (&Sclear_image_cache
);
8844 defsubr (&Simage_flush
);
8845 defsubr (&Simage_size
);
8846 defsubr (&Simage_mask_p
);
8847 defsubr (&Simage_metadata
);
8851 defsubr (&Slookup_image
);
8854 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8855 doc
: /* Non-nil means always draw a cross over disabled images.
8856 Disabled images are those having a `:conversion disabled' property.
8857 A cross is always drawn on black & white displays. */);
8858 cross_disabled_images
= 0;
8860 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8861 doc
: /* List of directories to search for window system bitmap files. */);
8862 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8864 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8865 doc
: /* Maximum time after which images are removed from the cache.
8866 When an image has not been displayed this many seconds, Emacs
8867 automatically removes it from the image cache. If the cache contains
8868 a large number of images, the actual eviction time may be shorter.
8869 The value can also be nil, meaning the cache is never cleared.
8871 The function `clear-image-cache' disregards this variable. */);
8872 Vimage_cache_eviction_delay
= make_number (300);
8873 #ifdef HAVE_IMAGEMAGICK
8874 DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type
,
8875 doc
: /* Choose between ImageMagick render methods. */);